#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;
}