分享

IP冲突检测程序源码及分析

 钱入室攻城仕 2014-04-16

本程序是网上很多被人采用的IP冲突检测程序,在这里感谢作者的分享。

  首先是头文件,这一部分定义了两个结构,一个是ARP包,arpMsg,其中包含了ARP请求和应答的分组格式,实际就是一个固定的arp封装。另外一个是对所有本地信息的配置,这里作者也封装了一个包server_config_t。

 

  1. #ifndef __CHECKIP_H  
  2. #define __CHECKIP_H  
  3.   
  4. struct arpMsg {  
  5.     struct ethhdr ethhdr;       /* Ethernet header */  
  6.     u_short htype;              /* hardware type (must be ARPHRD_ETHER) */  
  7.     u_short ptype;              /* protocol type (must be ETH_P_IP) */  
  8.     u_char  hlen;               /* hardware address length (must be 6) */  
  9.     u_char  plen;               /* protocol address length (must be 4) */  
  10.     u_short operation;          /* ARP opcode */  
  11.     u_char  sHaddr[6];          /* sender's hardware address */  
  12.     u_char  sInaddr[4];         /* sender's IP address */  
  13.     u_char  tHaddr[6];          /* target's hardware address */  
  14.     u_char  tInaddr[4];         /* target's IP address */  
  15.     u_char  pad[18];            /* pad for min. Ethernet payload (60 bytes) */  
  16. };  
  17.   
  18. struct server_config_t {  
  19.     u_int32_t server;       /* Our IP, in network order */  
  20.     u_int32_t start;        /* Start address of leases, network order */  
  21.     u_int32_t end;          /* End of leases, network order */  
  22.     struct option_set *options; /* List of DHCP options loaded from the config file */  
  23.     char *interface;        /* The name of the interface to use */  
  24.     int ifindex;            /* Index number of the interface to use */  
  25.     unsigned char arp[6];       /* Our arp address */  
  26.     unsigned long lease;        /* lease time in seconds (host order) */  
  27.     unsigned long max_leases;   /* maximum number of leases (including reserved address) */  
  28.     char remaining;         /* should the lease file be interpreted as lease time remaining, or 
  29.                      * as the time the lease expires */  
  30.     unsigned long auto_time;    /* how long should udhcpd wait before writing a config file. 
  31.                      * if this is zero, it will only write one on SIGUSR1 */  
  32.     unsigned long decline_time;     /* how long an address is reserved if a client returns a 
  33.                          * decline message */  
  34.     unsigned long conflict_time;    /* how long an arp conflict offender is leased for */  
  35.     unsigned long offer_time;   /* how long an offered address is reserved */  
  36.     unsigned long min_lease;    /* minimum lease a client can request*/  
  37.     char *lease_file;  
  38.     char *pidfile;  
  39.     char *notify_file;      /* What to run whenever leases are written */  
  40.     u_int32_t siaddr;       /* next server bootp option */  
  41.     char *sname;            /* bootp server name */  
  42.     char *boot_file;        /* bootp boot file option */  
  43. };    
  44.   
  45. #endif /* __CHECKIP_H */  

 

 

 

之后是C文件checkip.c,首先是主函数main()

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>   
  4. #include <string.h>  
  5. #include <errno.h>  
  6. #include <time.h>  
  7.  
  8. #include <sys/types.h>  
  9. #include <sys/socket.h>  
  10. #include <sys/ioctl.h>  
  11.                  
  12. #include <netinet/in.h>   
  13. #include <netinet/if_ether.h>  
  14. #include <net/if.h>  
  15. #include <net/if_arp.h>  
  16. #include <arpa/inet.h>    
  17.                  
  18. #include "checkip.h"  
  19.          
  20. #define MAC_BCAST_ADDR      (unsigned char *) "/xff/xff/xff/xff/xff/xff"  
  21. #define ETH_INTERFACE       "eth0"  
  22.               
  23. struct server_config_t server_config;  
  24.   
  25. int main(int argc, char *argv[])  
  26. {         
  27.     if(argc < 2)  
  28.     {     
  29.         printf("Usage: checkip ipaddr/n");  
  30.         exit(0);  
  31.     }  
  32.       
  33.     /*读以太网接口函数,获取一些配置信息*/  
  34.     if (read_interface(ETH_INTERFACE, &server_config.ifindex,  
  35.                &server_config.server, server_config.arp) < 0)  
  36.     {  
  37.         exit(0);  
  38.     }  
  39.       
  40.     /*IP检测函数*/  
  41.     if(check_ip(inet_addr(argv[1])) == 0)  
  42.     {  
  43.         printf("IP:%s can use/n", argv[1]);   
  44.     }  
  45.     else  
  46.     {  
  47.         printf("IP:%s conflict/n", argv[1]);  
  48.     }  
  49.       
  50.     return 0;  
  51. }  

 

先分析read_interface函数

 

  1. /*参数分别表示 网卡设备类型 接口检索索引 主机IP地址 主机arp地址*/  
  2. int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp)  
  3. {  
  4.     int fd;  
  5.     /*ifreq结构定义在/usr/include/net/if.h,用来配置ip地址,激活接口,配置MTU等接口信息的。 
  6.     其中包含了一个接口的名字和具体内容——(是个共用体,有可能是IP地址,广播地址,子网掩码,MAC号,MTU或其他内容)。 
  7.     ifreq包含在ifconf结构中。而ifconf结构通常是用来保存所有接口的信息的。 
  8.     */  
  9.     struct ifreq ifr;  
  10.     struct sockaddr_in *our_ip;  
  11.   
  12.     memset(&ifr, 0, sizeof(struct ifreq));  
  13.     /*建立一个socket函数,SOCK_RAW是为了获取第三个参数的IP包数据, 
  14.      IPPROTO_RAW提供应用程序自行指定IP头部的功能。 
  15.     */  
  16.     if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {  
  17.         ifr.ifr_addr.sa_family = AF_INET;  
  18.         /*将网卡类型赋值给ifr_name*/  
  19.         strcpy(ifr.ifr_name, interface);  
  20.   
  21.         if (addr) {  
  22.             /*SIOCGIFADDR用于检索接口地址*/  
  23.             if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {  
  24.                 /*获取本机IP地址,addr是一个指向该地址的指针*/  
  25.                 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;  
  26.                 *addr = our_ip->sin_addr.s_addr;  
  27.                 printf("%s (our ip) = %s/n", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));  
  28.             } else {  
  29.                 printf("SIOCGIFADDR failed, is the interface up and configured?: %s/n",  
  30.                         strerror(errno));  
  31.                 return -1;  
  32.             }  
  33.         }  
  34.   
  35.         /*SIOCGIFINDEX用于检索接口索引*/  
  36.         if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {  
  37.             printf("adapter index %d/n", ifr.ifr_ifindex);  
  38.             /*指针ifindex 获取索引*/  
  39.             *ifindex = ifr.ifr_ifindex;  
  40.         } else {  
  41.             printf("SIOCGIFINDEX failed!: %s/n", strerror(errno));  
  42.             return -1;  
  43.         }  
  44.         /*SIOCGIFHWADDR用于检索硬件地址*/  
  45.         if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {  
  46.             /*所获取的硬件地址复制到结构server_config的数组arp[6]参数中*/  
  47.             memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);  
  48.             printf("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x/n",  
  49.                 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);  
  50.         } else {  
  51.             printf("SIOCGIFHWADDR failed!: %s/n", strerror(errno));  
  52.             return -1;  
  53.         }  
  54.     }  
  55.     else {  
  56.         printf("socket failed!: %s/n", strerror(errno));  
  57.         return -1;  
  58.     }  
  59.     close(fd);  
  60.     return 0;  
  61. }  

 

读取所有本地网络信息后,执行函数check_ip(),该函数主要执行了arrpping()函数,如果arpping函数的返回值为0,说明检测发出的到ip是冲突的

  1. int check_ip(u_int32_t addr)  
  2. {  
  3.     struct in_addr temp;  
  4.   
  5.     if (arpping(addr, server_config.server, server_config.arp, ETH_INTERFACE) == 0)  
  6.     {  
  7.         temp.s_addr = addr;  
  8.         printf("%s belongs to someone, reserving it for %ld seconds/n",  
  9.             inet_ntoa(temp), server_config.conflict_time);  
  10.         return 1;  
  11.     }  
  12.     else  
  13.         return 0;  
  14. }  

 

  1. /*参数说明 目标IP地址,本机IP地址,本机mac地址,网卡类型*/  
  2. int arpping(u_int32_t yiaddr, u_int32_t ip, unsigned char *mac, char *interface)  
  3. {  
  4.     int timeout = 2;  
  5.     int optval = 1;  
  6.     int s;                      /* socket */  
  7.     int rv = 1;                 /* return value */  
  8.     struct sockaddr addr;       /* for interface name */  
  9.     struct arpMsg arp;  
  10.     fd_set fdset;  
  11.     struct timeval tm;  
  12.     time_t prevTime;  
  13.   
  14.     /*socket发送一个arp包*/  
  15.     if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {  
  16.         printf("Could not open raw socket/n");  
  17.         return -1;  
  18.     }  
  19.       
  20.     /*设置套接口类型为广播,把这个arp包是广播到这个局域网*/  
  21.     if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {  
  22.         printf("Could not setsocketopt on raw socket/n");  
  23.         close(s);  
  24.         return -1;  
  25.     }  
  26.   
  27.     /* 对arp设置,这里按照arp包的封装格式赋值即可,详见http://blog.csdn.net/wanxiao009/archive/2010/05/21/5613581.aspx */  
  28.     memset(&arp, 0, sizeof(arp));  
  29.     memcpy(arp.ethhdr.h_dest, MAC_BCAST_ADDR, 6);   /* MAC DA */  
  30.     memcpy(arp.ethhdr.h_source, mac, 6);        /* MAC SA */  
  31.     arp.ethhdr.h_proto = htons(ETH_P_ARP);      /* protocol type (Ethernet) */  
  32.     arp.htype = htons(ARPHRD_ETHER);        /* hardware type */  
  33.     arp.ptype = htons(ETH_P_IP);            /* protocol type (ARP message) */  
  34.     arp.hlen = 6;                   /* hardware address length */  
  35.     arp.plen = 4;                   /* protocol address length */  
  36.     arp.operation = htons(ARPOP_REQUEST);       /* ARP op code */  
  37.     *((u_int *) arp.sInaddr) = ip;          /* source IP address */  
  38.     memcpy(arp.sHaddr, mac, 6);         /* source hardware address */  
  39.     *((u_int *) arp.tInaddr) = yiaddr;      /* target IP address */  
  40.   
  41.     memset(&addr, 0, sizeof(addr));  
  42.     strcpy(addr.sa_data, interface);  
  43.     /*发送arp请求*/  
  44.     if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)  
  45.         rv = 0;  
  46.   
  47.     /* 利用select函数进行多路等待*/  
  48.     tm.tv_usec = 0;  
  49.     time(&prevTime);  
  50.     while (timeout > 0) {  
  51.         FD_ZERO(&fdset);  
  52.         FD_SET(s, &fdset);  
  53.         tm.tv_sec = timeout;  
  54.         if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {  
  55.             printf("Error on ARPING request: %s/n", strerror(errno));  
  56.             if (errno != EINTR) rv = 0;  
  57.         } else if (FD_ISSET(s, &fdset)) {  
  58.             if (recv(s, &arp, sizeof(arp), 0) < 0 )   
  59.                 rv = 0;  
  60.             /*如果条件 htons(ARPOP_REPLY) bcmp(arp.tHaddr, mac, 6) == 0 *((u_int *) arp.sInaddr) == yiaddr 三者都为真,则ARP应答有效,说明这个地址是已近存在的*/  
  61.             if (arp.operation == htons(ARPOP_REPLY) &&  
  62.                 bcmp(arp.tHaddr, mac, 6) == 0 &&  
  63.                 *((u_int *) arp.sInaddr) == yiaddr) {  
  64.                 printf("Valid arp reply receved for this address/n");  
  65.                 rv = 0;  
  66.                 break;  
  67.             }  
  68.         }  
  69.         timeout -= time(NULL) - prevTime;  
  70.         time(&prevTime);  
  71.     }  
  72.     close(s);  
  73.     return rv;  
  74. }  

 

综上所述,该程序是主要流程是封装好一个arp包,然后广播到局域网中,然后接收,通过接受的应答来解析发出的IP地址是不是一个可用IP。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多