int get_mac_ip (const char *ifc, unsigned char mac[6],unsigned char ip[15])
{
register int fd;
struct ifreq ifr;
unsigned char *p = NULL;
int i;
int err = 0;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
memset(&ifr, 0, sizeof(struct ifreq));
sprintf(ifr.ifr_name, "%s", ifc);
if((ioctl (fd, SIOCGIFADDR, &ifr))>=0)
{
strcpy( ip, (char*)inet_ntoa(((struct sockaddr_in*)(&ifr.ifr_addr))->sin_addr));
}
else
{
err = 1;
}
if((ioctl (fd, SIOCGIFHWADDR, &ifr))>=0)
{
p = ifr.ifr_hwaddr.sa_data;
for(i=0;i<6;i++)
mac[i] = p[i];
}
else
{
err = 2;
}
}
close(fd);
return err;
}
阅读(771) | 评论(0) | 转发(0) |