Chinaunix首页 | 论坛 | 博客
  • 博客访问: 52539
  • 博文数量: 15
  • 博客积分: 481
  • 博客等级: 下士
  • 技术积分: 195
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-08 22:29
文章分类

全部博文(15)

文章存档

2013年(5)

2012年(1)

2009年(4)

2008年(5)

分类: C/C++

2013-03-14 11:03:19

原文地址:获取ip和mac地址的C程序 作者:yszll

#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;
}
阅读(732) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~