Chinaunix首页 | 论坛 | 博客
  • 博客访问: 980007
  • 博文数量: 184
  • 博客积分: 10030
  • 博客等级: 上将
  • 技术积分: 1532
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-27 18:32
文章分类

全部博文(184)

文章存档

2009年(1)

2008年(63)

2007年(39)

2006年(79)

2005年(2)

我的朋友

分类: C/C++

2008-05-13 16:57:36

#include
#include
#include
#include
#include
#include
#include
#include
 
#define MAXINTERFACES 16
 
int main(int argc, char **argv)
{
    register int fd, interface, retn = 0;
    struct ifreq buf[MAXINTERFACES];
    struct arpreq arp;
    struct ifconf ifc;
    char mac[32] = "";
 
    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)) {
            interface = ifc.ifc_len / sizeof(struct ifreq);
            printf("interface num is interface=%d\n\n", interface);
            while (interface-- > 0) {
                printf("net device %s\n", buf[interface].ifr_name);
 
/*Jugde whether the net card status is promisc */
                if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[interface]))) {
                    if (buf[interface].ifr_flags & IFF_PROMISC) {
                        printf("the interface is PROMISC");
                        retn ;
                    }
                } else {
                    char str[256] = "";
 
                    sprintf(str, "cpm: ioctl device %s",
                            buf[interface].ifr_name);
                    perror(str);
                }
 
/*Jugde whether the net card status is up */
                if (buf[interface].ifr_flags & IFF_UP) {
                    printf("the interface status is UP\n");
                } else {
                    printf("the interface status is DOWN\n");
                }
 
/*Get IP of the net card */
                if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[interface]))) {
                    printf("IP address is:");
                    printf("%s\n",
                           inet_ntoa(((struct sockaddr_in
                                       *) (&buf[interface].ifr_addr))->
                                     sin_addr));
                } else {
                    char str[256] = "";
 
                    sprintf(str, "cpm: ioctl device %s",
                            buf[interface].ifr_name);
                    perror(str);
                }
 
/*Get HW ADDRESS of the net card */
                if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface]))) {
                    printf("HW address is:");
 
                    sprintf(mac, "%02x%02x%02x%02x%02x%02x",
                            (unsigned char) buf[interface].ifr_hwaddr.
                            sa_data[0],
                            (unsigned char) buf[interface].ifr_hwaddr.
                            sa_data[1],
                            (unsigned char) buf[interface].ifr_hwaddr.
                            sa_data[2],
                            (unsigned char) buf[interface].ifr_hwaddr.
                            sa_data[3],
                            (unsigned char) buf[interface].ifr_hwaddr.
                            sa_data[4],
                            (unsigned char) buf[interface].ifr_hwaddr.
                            sa_data[5]); // 利用sprintf转换成char *
                    printf("%s\n", mac);
 
                    printf("\n");
                }
 
                else {
                    char str[256];
 
                    sprintf(str, "cpm: ioctl device %s",
                            buf[interface].ifr_name);
                    perror(str);
                }
            }                   //end of while
        } else
            perror("cpm: ioctl");
 
    } else
        perror("cpm: socket");
 
    close(fd);
    return retn;
}
阅读(2277) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~