Chinaunix

标题: 求帮助(硬盘序列号检测软件无法编译过) [打印本页]

作者: hacker_xie    时间: 2010-07-23 00:26
标题: 求帮助(硬盘序列号检测软件无法编译过)
本帖最后由 hacker_xie 于 2010-07-23 00:53 编辑

MachineInfo.rar (3.11 KB, 下载次数: 20) 求帮助(硬盘序列号检测软件无法编译过);附带了原码,大虾们走过路过请帮助,谢谢~


附加如下(具体见附件,以下代码可能因为网站的代码出现表情,麻烦各位看看解答下啊,谢谢了):
/*********************
1.MachineInfo.cpp
**********************/

#include "MachineInfo.h"


typedef unsigned short WORD;

/****************************************************************************
//获取MAC地址
int GetMac(char *macAddr)
{
        if(macAddr == NULL)
                return 0;

        struct ifreq ifreq;
        int sock;
        if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
        {
                perror("socket";
                return 0;
        }
        strcpy(ifreq.ifr_name,"eth0";   //取网卡eth0的mac
        if(ioctl(sock,SIOCGIFHWADDR,&ifreq)<0)
        {
                strcpy(ifreq.ifr_name, "eth1";   //如果没取到eth0取eth1的mac
                if(ioctl(sock, SIOCGIFHWADDR, &ifreq)<0)
                {
                        perror("ioctl";
                        return 0;
                }
        }

        sprintf(macAddr, "%02x-%02x-%02x-%02x-%02x-%02x",
                (unsigned char)ifreq.ifr_hwaddr.sa_data[0],
                (unsigned char)ifreq.ifr_hwaddr.sa_data[1],
                (unsigned char)ifreq.ifr_hwaddr.sa_data[2],
                (unsigned char)ifreq.ifr_hwaddr.sa_data[3],
                (unsigned char)ifreq.ifr_hwaddr.sa_data[4],
                (unsigned char)ifreq.ifr_hwaddr.sa_data[5]);

        return 1;

}

*******************************************************************************/

static void dump_bytes (const char *prefix, unsigned char *p, int len)
{
    int i;

    if (prefix)
        fprintf(stderr, "%s: ", prefix);
    for (i = 0; i < len; ++i)
        fprintf(stderr, " %02x", p);
    fprintf(stderr, "\n";
}


void print_ascii(__u16 *p, __u8 length, char *SZID) {
    __u8 ii;
    char cl;
    int len=0;
    /* find first non-space & print it */
    for (ii = 0; ii< length; ii++) {
        if(((char) 0x00ff&((*p)>>) != ' ') break;
        if((cl = (char) 0x00ff&(*p)) != ' ') {
            if(cl != '\0')
                        {
                                SZID[len]=cl;
                                len++;
                        //        printf("%c",cl);
                        }
            p++; ii++;
            break;
        }
        p++;
    }
    /* print the rest */
    for (; ii < length; ii++) {
        __u8 c;
        /* some older devices have NULLs */
        c = (*p) >> 8;
        if (c)
                {
//                        putchar(c);
                        SZID[len]=c;
                        len++;
                }
        c = (*p);
        if (c)
                {
                        //putchar(c);
                        SZID[len]=c;
                        len++;
                }
        p++;
    }
  //  printf("\n";
        SZID[len]='\n';
}


int get_sata_serial(char *szDevice, char *szID)
{
    int fd = 0;

    static __u8 args[512] = { 0 };
    __u16 *id = (__u16 *)(args);
   
    void *data = (void *)(args);
    unsigned int data_bytes = 512;

    unsigned char cdb[SG_ATA_16_LEN] = { 0 };
    unsigned char sb[32], *desc;
    unsigned char ata_status, ata_error;
    struct sg_io_hdr io_hdr;


//打开设备

    fd = open("/dev/sda", O_RDONLY);
    if (fd < 0)   
        {
        printf("open /dev/sda error\n";
                GetIdeSer(szID); //如果打开sda失败,试图打开hda        
        return -1;
        }


//设置cmdp

    cdb[ 0] = SG_ATA_16;
    cdb[ 1] = SG_ATA_PROTO_PIO_IN;
    cdb[ 2] = SG_CDB2_CHECK_COND;
    cdb[2] |= SG_CDB2_TLEN_NSECT | SG_CDB2_TLEN_SECTORS;
    cdb[2] |= SG_CDB2_TDIR_FROM_DEV;
    cdb[13] = ATA_USING_LBA;
    cdb[14] = ATA_OP_IDENTIFY;

//设置sdp

    memset(&(sb[0]), 0, sizeof(sb));

//设置sg_io_hdr结构

    memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
    io_hdr.interface_id    = 'S';
    io_hdr.cmd_len        = SG_ATA_16_LEN;
    io_hdr.mx_sb_len    = sizeof(sb);
    io_hdr.dxfer_direction    = SG_DXFER_FROM_DEV;
    io_hdr.dxfer_len    = data_bytes;
    io_hdr.dxferp        = data;
    io_hdr.cmdp        = cdb;
    io_hdr.sbp        = sb;
    io_hdr.timeout        = 10000; /* msecs */


//调用ioctl

    if (ioctl(fd, SG_IO, &io_hdr) == -1) {
        fprintf(stderr, "SG_IO ioctl not supported\n";
        return -1;    /* SG_IO not supported */
    }

//检查sg_io_hdr中的状态值

    if (io_hdr.host_status || io_hdr.driver_status != SG_DRIVER_SENSE
     || (io_hdr.status && io_hdr.status != SG_CHECK_CONDITION))
    {
         errno = EIO;
        return -2;
    }

//检查sdp中的状态值

    if (sb[0] != 0x72 || sb[7] < 14) {
        errno = EIO;
        return -3;
    }
    desc = sb + 8;
    if (desc[0] != 9 || desc[1] < 12){
        errno = EIO;
        return -4;
    }

    ata_error = desc[3];
    ata_status = desc[13];
    if (ata_status & 0x01) {    /* ERR_STAT */
        errno = EIO;
        return -5;
    }
   
    print_ascii( &id[START_SERIAL], LENGTH_SERIAL, szID);
   
//    dump_bytes( "Dump Serial:", (unsigned char *)&id[START_SERIAL], LENGTH_SERIAL*2 );
   
    return 0;
}


static void dump_identity (const struct hd_driveid *id, char *IdeSer)
{        
        sprintf(IdeSer, "%.20s", id->serial_no);
}


int GetIdeSer(char *IdeSer)
{
        int fd = 0;
        fd = open("/dev/hda",O_RDONLY);
        if(fd<0)
        {
                perror("open /dev/hda error";
                return 0;
        }
        static struct hd_driveid id;
        if (!ioctl(fd, HDIO_GET_IDENTITY,&id))
        {
                dump_identity(&id, IdeSer);
        }
        else
                printf("HDIO_GET_IDENTITY failed";
        return 0;
}




   



/************************
2.MachineInfo.h
************************/



#include <net/if.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <asm/io.h>
#include <sys/io.h> /* for glibc */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <scsi/sg.h>
#include <linux/types.h>
#include <linux/hdreg.h>
#include <fcntl.h>

#define SG_CHECK_CONDITION    0x02
#define SG_DRIVER_SENSE        0x08

#define SG_ATA_16        0x85
#define SG_ATA_16_LEN        16

#define SG_ATA_LBA48        1
#define SG_ATA_PROTO_NON_DATA    ( 3 << 1)
#define SG_ATA_PROTO_PIO_IN    ( 4 << 1)
#define SG_ATA_PROTO_PIO_OUT    ( 5 << 1)
#define SG_ATA_PROTO_DMA    ( 6 << 1)
#define SG_ATA_PROTO_UDMA_IN    (11 << 1) /* not yet supported in libata */
#define SG_ATA_PROTO_UDMA_OUT    (12 << 1) /* not yet supported in libata */

#define ATA_USING_LBA        (1 << 6)

enum {
    ATA_OP_CHECKPOWERMODE1        = 0xe5,
                ATA_OP_CHECKPOWERMODE2        = 0x98,
                ATA_OP_DOORLOCK            = 0xde,
                ATA_OP_DOORUNLOCK        = 0xdf,
                ATA_OP_FLUSHCACHE        = 0xe7,
                ATA_OP_FLUSHCACHE_EXT        = 0xea,
                ATA_OP_IDENTIFY            = 0xec,
                ATA_OP_PIDENTIFY        = 0xa1,
                ATA_OP_SECURITY_DISABLE        = 0xf6,
                ATA_OP_SECURITY_ERASE_PREPARE    = 0xf3,
                ATA_OP_SECURITY_ERASE_UNIT    = 0xf4,
                ATA_OP_SECURITY_FREEZE_LOCK    = 0xf5,
                ATA_OP_SECURITY_SET_PASS    = 0xf1,
                ATA_OP_SECURITY_UNLOCK        = 0xf2,
                ATA_OP_SETFEATURES        = 0xef,
                ATA_OP_SETIDLE1            = 0xe3,
                ATA_OP_SETIDLE2            = 0x97,
                ATA_OP_SLEEPNOW1        = 0xe5,
                ATA_OP_SLEEPNOW2        = 0x99,
                ATA_OP_SMART            = 0xb0,
                ATA_OP_STANDBYNOW1        = 0xe0,
                ATA_OP_STANDBYNOW2        = 0x94,
};

enum {
    SG_CDB2_TLEN_NODATA    = 0 << 0,
                SG_CDB2_TLEN_FEAT    = 1 << 0,
                SG_CDB2_TLEN_NSECT    = 2 << 0,
               
                SG_CDB2_TLEN_BYTES    = 0 << 2,
                SG_CDB2_TLEN_SECTORS    = 1 << 2,
               
                SG_CDB2_TDIR_TO_DEV    = 0 << 3,
                SG_CDB2_TDIR_FROM_DEV    = 1 << 3,
               
                SG_CDB2_CHECK_COND    = 1 << 5,
};


#define START_SERIAL 10 /* ASCII serial number */
#define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */

//int GetMac(char *macAddr);   //取mac地址
void print_ascii(__u16 *p, __u8 length, char *SZID);  //取scsi硬盘序列号
int GetIdeSer(char *IdeSer);    //取ide硬盘序列号
static void dump_bytes (const char *prefix, unsigned char *p, int len);
int get_sata_serial(char *szDevice, char *szID);
static void dump_bytes (const char *prefix, unsigned char *p, int len);

/***********************************************************
3.test.cpp
***********************************************************/

#include "MachineInfo.h"
int main()
{
    int rv = 0;
    char szID[64] = { 0 };
       
    rv= get_sata_serial("/dev/sda", szID);
       
    printf("Serial:%s\r\n",szID);
   
   
    /*************************************
    //获取MAC地址
    char MAC[30]={0};
        GetMac(MAC);
        printf("mac:%s\r\n", MAC);
     ************************************/
}




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2