20100202 获取MAC方法 (转)

1.AF_INET方法获取

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>

#define MAXINTERFACES 16

int main(argc, argv)
register int argc;
register char *argv[];
{
    register int fd, intrface, retn = 0;
    struct ifreq buf[MAXINTERFACES];
    struct arpreq arp;
    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)) {
          intrface = ifc.ifc_len / sizeof(struct ifreq);
          printf("interface num is intrface=%d/n/n/n", intrface);
          while (intrface-- > 0) {
            printf("net device %s/n", buf[intrface].ifr_name);

/*Jugde whether the net card status is promisc*/
            if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[intrface]))) {
                if (buf[intrface].ifr_flags & IFF_PROMISC) {
                  puts("the interface is PROMISC");
                  retn++;
                }
            } else {
                char str[256];

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

/*Jugde whether the net card status is up*/
            if (buf[intrface].ifr_flags & IFF_UP) {
                puts("the interface status is UP");
            } else {
                puts("the interface status is DOWN");
            }

/*Get IP of the net card */
            if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[intrface]))) {
                puts("IP address is:");
                puts(inet_ntoa
                   (((struct sockaddr_in *) (&buf[intrface].
                                       ifr_addr))->sin_addr));
                puts("");
//puts (buf[intrface].ifr_addr.sa_data);
            } else {
                char str[256];

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

/*Get HW ADDRESS of the net card */
            if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[intrface]))) {
                puts("HW address is:");

                printf("%02x:%02x:%02x:%02x:%02x:%02x/n",
                     (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]);

                puts("");
                puts("");
            }

            else {
                char str[256];

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

    } else
      perror("cpm: socket");

    close(fd);
    return retn;
}


2.PF_PACKET方法获取网卡地址

/* Low level network programming in Linux using PF_PACKET

* Need root privileges */

#include <stdio.h>

#include <sys/socket.h>

#include <sys/errno.h>

#include <linux/if.h>

#include <linux/if_ether.h>

#include <linux/if_arp.h>

#include <linux/sockios.h>


int main(int argc, char **argv)

{

    int i, sockfd;

    static struct ifreq req;

    struct sockaddr_ll inside_address;


    /* Low level socket */

    sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

    if (sockfd < 0) {

        perror("Unable to create low level socket: ");

        exit(1);

    }

    memset(&inside_address, 0, sizeof(inside_address));

    inside_address.sll_family = AF_PACKET;

    inside_address.sll_protocol = htons(ETH_P_ALL);

    inside_address.sll_hatype = ARPOP_REQUEST;

    inside_address.sll_pkttype = PACKET_BROADCAST;

    inside_address.sll_halen = 6;

    strcpy(req.ifr_name, "eth1");

    ioctl(sockfd, SIOCGIFINDEX, &req);

    inside_address.sll_ifindex = req.ifr_ifindex;

    ioctl(sockfd, SIOCGIFHWADDR, &req);

    if (bind(sockfd, (struct sockaddr *) &inside_address,

             sizeof(inside_address)) != 0) {

        perror("Error:");

        exit(1);

    }

    for (i = 0; i < 6; i++)

        inside_address.sll_addr[i] =

            (unsigned char) req.ifr_hwaddr.sa_data[i];

    printf("%X/n", inside_address.sll_ifindex);

    for (i = 0; i < 5; i++) {

        printf("%02X", inside_address.sll_addr[i]);

        printf(":");

    }

    printf("%02X/n", inside_address.sll_addr[i]);

    close(sockfd);

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值