linux下mac广播地址,LINUX 下获取IP地址、MAC地址和广播地址、子网掩码等信息

#define MAX_INTERFACES                  (3)        // max interfaces

int  main(int argc, char **argv)

{

int fd, intrface, ret = 0;

struct ifreq buf[MAX_INTERFACES]; /* ifreq   Array of structures */

struct ifconf ifc;

if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)

{

ifc.ifc_len = sizeof buf;

ifc.ifc_buf = (caddr_t) buf;

if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))

{

//get the number of interfaces information

intrface = ifc.ifc_len / sizeof (struct ifreq);

printf("interface num is intrface=%d

",intrface);

puts("");

//get Device IP and MAC address based interface information circular

while ( (intrface--) > 0)

{

//get the device name

printf ("net device %s

", buf[intrface].ifr_name);

//Determine the type of network card

if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))

{

if (buf[intrface].ifr_flags & IFF_PROMISC)

{

printf("the interface is PROMISC

");

ret++;

}

}

else

{

char str[256];

sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);

perror (str);

}

//determine NIC status

if (buf[intrface].ifr_flags & IFF_UP)

{

printf("the interface status is up

");

}

else

{

printf("the interface status is down

");

}

//get the current NIC IP address

if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))

{

printf("IP address is:");

printf("%s

",(char *)inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));

}

else

{

char str[256];

sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);

perror (str);

}

/* this section can't get Hardware Address,I don't know whether the reason is module driver*/

if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))

{

printf("HW address is:");

printf("%02x:%02x:%02x:%02x:%02x:%02x

",

(unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],

(unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],

(unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],

(unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],

(unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],

(unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);

}

else

{

char str[256];

sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);

perror (str);

}

//sub network mask

if (!(ioctl(fd, SIOCGIFNETMASK, (char *) &buf[intrface])))

{

printf("NETWORK MASK :%s",(char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));

puts("");

}

else

{

char str[256];

sprintf(str, "SIOCGIFADDR ioctl %s", buf[intrface].ifr_name);

perror(str);

}

//broadcast address

if (!(ioctl(fd, SIOCGIFBRDADDR, (char *) &buf[intrface])))

printf("Broadcast Address:%s

",(char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));

puts("");

}

}else

perror ("cpm: ioctl");

}else

perror ("cpm: socket");

close (fd);

return ret;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值