当前位置: 技术问答>linux和unix
如何获取网卡的地址?
来源: 互联网 发布时间:2014-10-24
本文导语: 环境BLUEPOINT 1.0.THANKS. |Linux下,学内核分析时参考了别人的程序照着编的。 和内核有关的做了一点注释,见笑。 redhat6.0 正宗的源码可以从Linux的netkit源码包(rpm)里找到。 /* arp.c Programed by Terry Reffer...
环境BLUEPOINT 1.0.THANKS.
|
Linux下,学内核分析时参考了别人的程序照着编的。 和内核有关的做了一点注释,见笑。 redhat6.0 正宗的源码可以从Linux的netkit源码包(rpm)里找到。 /* arp.c Programed by Terry Reffered cpu's source code in bbs.gznet.edu.cn socket ban send arp request, get arp reply get the mac address of the hosts in a subnet gcc arp.c -o arp1 interface:default is eth0 Note : run it need root right or root has permitted it. Example: [root@46# work]# ./arp1 202.114.67.254 request mac 00:e0:4c:dd:79:e1, request IP 202.114.67.211 replied mac 00:e0:34:f0:18:08, replied IP 202.114.67.254 ----------------------------------------------------------------------------- Total Time Delay : 2314 us [root@46# work]# ./arp1 202.114.67.254 request mac 00:e0:4c:dd:79:e1, request IP 202.114.67.211 --User Break!-- Total Time Delay : 3294147 us */ #include #include #include #include #include /* struct in_addr : /usr/include/netinet/in.h */ #include #include /* /usr/src/linux/include/linux/socket.h */ #include #include /* use the ifreq */ #include /* use the arphdr */ #include /* use the ethernethdr */ #include #include #include /* use the timeval */ #define DEF_INTERFACE "eth0" #define PACKET_SIZE 2048 struct arp_hdr { /* ehter header from /net/ethernet.h */ u_char ether_dhost[ETH_ALEN]; /* destination eth addr */ u_char ether_shost[ETH_ALEN]; /* source ether addr */ u_short ether_type; /* packet type ID field */ /* arp or rarp header from /net/if_arp.h */ u_short ar_hrd; /* Format of hardware address. */ u_short ar_pro; /* Format of protocol address. */ u_char ar_hln; /* Length of hardware address. */ u_char ar_pln; /* Length of protocol address. */ u_short ar_op; /* ARP opcode (command). */ u_char ar_sha[ETH_ALEN]; /* Sender hardware address. */ u_char ar_sip[4]; /* Sender IP address. */ u_char ar_tha[ETH_ALEN]; /* Target hardware address. */ u_char ar_tip[4]; /* Target IP address. */ u_char padding[18]; /* padding */ }; struct in_addr myself, dstaddr; /* defined in /usr/include/netinet/in.h */ /* uint32_t s_addr;Internet address */ int fd_arp; /* socket fd for receiving packets */ struct ifreq ifr; /* ifreq structure : */ /* defined in /usr/include/net/if.h */ long send_time,recv_time; /* time that send & receive arp packet */ long time_now() /* return time passed by */ /* since 1970.1.1 00:00:00, */ /* in 1/1000000 second */ { struct timeval now; long lPassed; gettimeofday(&now, 0); lPassed = now.tv_sec * 1000000 + now.tv_usec; /* now.tv_sec in second */ /* now.tv_usec in 1/1000000 second */ return lPassed; } void stat() /* signal process : quit */ { printf( "n--User Break!--nTotal Time Delay : %ld usn",time_now()-send_time); exit(-1); } main(int argc, char *argv[]) { char device[32]; /* ethernet device name */ struct sockaddr from, to; int fromlen; struct sockaddr_in * sin_ptr; u_char * ptr; int n; u_char buf[PACKET_SIZE]; /* buffer for send and recv */ struct arp_hdr * arp = (struct arp_hdr *)buf; if (argc = 3) { strncpy(device, argv[2], 32); /* interface name */ device[31] = ''; /* make device a string */ } else strcpy(device, DEF_INTERFACE); dstaddr.s_addr = inet_addr(argv[1]); /* convert IP xx.xx.. to binary */ if ((fd_arp = socket(AF_INET, SOCK_PACKET, htons(0x0806))) ether_shost, 4); arp->ether_type = htons(ETHERTYPE_ARP); /* arp header */ arp->ar_hrd = htons(ARPHRD_ETHER); arp->ar_pro = htons(ETHERTYPE_IP); arp->ar_hln = 6; arp->ar_pln = 4; arp->ar_op = htons(1); bcopy(ptr,arp->ar_sha, 6); bcopy(&myself, arp->ar_sip, 4); bzero(arp->ar_tha, 6); bcopy(&dstaddr, arp->ar_tip, 4); bzero(arp->padding, 18); strcpy(to.sa_data, device); if ((n = sendto(fd_arp, buf, sizeof(struct arp_hdr), 0, &to, sizeof(to))) ar_op == 0x0200 || !memcmp(arp->ar_sip, &dstaddr, 4)) { printf( "replied mac %02x:%02x:%02x:%02x:%02x:%02x, ", arp->ar_sha[0], arp->ar_sha[1], arp->ar_sha[2], arp->ar_sha[3], arp->ar_sha[4], arp->ar_sha[5] ); printf( "replied IP %sn", inet_ntoa(dstaddr)); printf( "-----------------------------------------------------------------------------n"); printf( "Total Time Delay : %ld usn",recv_time-send_time); return; } else sigset(SIGINT, stat); /* when press del or ctrl+c, call stat */ /* to statistic the result , and then exit */ goto Loop; } }