Mac地址计算ipv6地址

int GetLocalMac(u8 * HostIP)
{
int sock_mac;

struct ifreq ifr_mac;
char mac_addr[30];

sock_mac = socket( AF_INET, SOCK_STREAM, 0 );
if( sock_mac == -1)
{
perror("create socket falise...mac\n");
return 0;
}

memset(&ifr_mac,0,sizeof(ifr_mac));
strncpy(ifr_mac.ifr_name, DFLT_SCOPE_ID, sizeof(ifr_mac.ifr_name)-1);

if( (ioctl( sock_mac, SIOCGIFHWADDR, &ifr_mac)) < 0)
{
printf("mac ioctl error\n");
return 0;
}

/*sprintf(mac_addr,"%02x:%02x:%02x:%02x:%02x:%02x",
(u8)ifr_mac.ifr_hwaddr.sa_data[0],
(u8)ifr_mac.ifr_hwaddr.sa_data[1],
(u8)ifr_mac.ifr_hwaddr.sa_data[2],
(u8)ifr_mac.ifr_hwaddr.sa_data[3],
(u8)ifr_mac.ifr_hwaddr.sa_data[4],
(u8)ifr_mac.ifr_hwaddr.sa_data[5]);

printf("local mac:%s \n",mac_addr);*/
u8 u=0x00;
memset(HostIP,0x00,16);
u=(u8)ifr_mac.ifr_hwaddr.sa_data[0]^0x02;
HostIP[0]=0xfe;HostIP[1]=0x80;

HostIP[2]=0x00;HostIP[3]=0x00;
HostIP[4]=0x00;HostIP[5]=0x00;
HostIP[6]=0x00;HostIP[7]=0x00;

HostIP[8]=u;HostIP[9]=(u8)ifr_mac.ifr_hwaddr.sa_data[1];
HostIP[10]=(u8)ifr_mac.ifr_hwaddr.sa_data[2];HostIP[11]=0xff;
HostIP[12]=0xfe;HostIP[13]=(u8)ifr_mac.ifr_hwaddr.sa_data[3];
HostIP[14]=(u8)ifr_mac.ifr_hwaddr.sa_data[4];HostIP[15]=(u8)ifr_mac.ifr_hwaddr.sa_data[5];

close( sock_mac );
return 0;
}


你可能感兴趣的:(Mac地址计算ipv6地址)