wolflion

判断IP是否冲突的方法

  1. 原理其实很简单,那就是广播一个arp包,然后recv,如果没有数据(这里要设置延时),那么说明这个ip是可用的,否则就检测这个数据是否为回复我们发出的arp的应答包.如果是则证明ip已被使用,否则继续等待.

    这里可以看下busybox的dhcp中的检测程序。
    networking/udhcp/arpping.c
  2. /* vi: set sw=4 ts=4: */  
  3. /*
  4. * arpping.c
  5. *
  6. * Mostly stolen from: dhcpcd - DHCP client daemon
  7. * by Yoichi Hariguchi <yoichi@fore.com>
  8. */  
  9.   
  10. #include <netinet/if_ether.h>   
  11. #include <net/if_arp.h>   
  12.   
  13. #include "common.h"   
  14. #include "dhcpd.h"   
  15.   
  16. //这里是arp包的格式,其中的数据格式都是宏了,比如uint_8_t为无符char.   
  17. struct arpMsg {   
  18.     /* Ethernet header */  
  19.      uint8_t   h_dest[6];     /* 00 destination ether addr */  
  20.      uint8_t   h_source[6];   /* 06 source ether addr */  
  21.      uint16_t h_proto;       /* 0c packet type ID field */  
  22.   
  23.     /* ARP packet */  
  24.      uint16_t htype;         /* 0e hardware type (must be ARPHRD_ETHER) */  
  25.      uint16_t ptype;         /* 10 protocol type (must be ETH_P_IP) */  
  26.      uint8_t   hlen;          /* 12 hardware address length (must be 6) */  
  27.      uint8_t   plen;          /* 13 protocol address length (must be 4) */  
  28.      uint16_t operation;     /* 14 ARP opcode */  
  29.      uint8_t   sHaddr[6];     /* 16 sender's hardware address */  
  30.      uint8_t   sInaddr[4];    /* 1c sender's IP address */  
  31.      uint8_t   tHaddr[6];     /* 20 target's hardware address */  
  32.      uint8_t   tInaddr[4];    /* 26 target's IP address */  
  33.      uint8_t   pad[18];       /* 2a pad for min. ethernet payload (60 bytes) */  
  34. } PACKED;   
  35.   
  36. enum {   
  37.      ARP_MSG_SIZE = 0x2a   
  38. };   
  39.   
  40.   
  41. /* Returns 1 if no reply received */  
  42.   
  43. //主程序,如果返回1说明此ip可用   
  44. int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)   
  45. {   
  46.   
  47.     int timeout_ms;   
  48. //这里使用poll来检测句柄。   
  49.     struct pollfd pfd[1];   
  50. #define s (pfd[0].fd)            /* socket */   
  51.     int rv = 1;             /* "no reply received" yet */  
  52.     struct sockaddr addr;   /* for interface name */  
  53.     struct arpMsg arp;   
  54.   
  55. //建立scoket.由于我们是要直接访问访问链路层并自己组arp包.因此我们使用PF_PACKET协议簇.socket类型为SOCK_PACKET.   
  56.   
  57.      s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));   
  58.     if (s == -1) {   
  59.          bb_perror_msg(bb_msg_can_not_create_raw_socket);   
  60.         return -1;   
  61.      }   
  62.   
  63.     if (setsockopt_broadcast(s) == -1) {   
  64.          bb_perror_msg("cannot enable bcast on raw socket");   
  65.         goto ret;   
  66.      }   
  67. //进行组包,由于是要广播,因此目的mac地址为全0.   
  68.     /* send arp request */  
  69.      memset(&arp, 0, sizeof(arp));   
  70.      memset(arp.h_dest, 0xff, 6);                    /* MAC DA */  
  71.      memcpy(arp.h_source, from_mac, 6);              /* MAC SA */  
  72.     arp.h_proto = htons(ETH_P_ARP);                 /* protocol type (Ethernet) */  
  73.     arp.htype = htons(ARPHRD_ETHER);                /* hardware type */  
  74.     arp.ptype = htons(ETH_P_IP);                    /* protocol type (ARP message) */  
  75.     arp.hlen = 6;                                   /* hardware address length */  
  76.     arp.plen = 4;                                   /* protocol address length */  
  77.     arp.operation = htons(ARPOP_REQUEST);           /* ARP op code */  
  78.      memcpy(arp.sHaddr, from_mac, 6);                /* source hardware address */  
  79.      memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */  
  80.     /* tHaddr is zero-fiiled */                     /* target hardware address */  
  81.      memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* target IP address */  
  82.   
  83.      memset(&addr, 0, sizeof(addr));   
  84.      safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));   
  85. //广播arp包.   
  86.     if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {   
  87.         // TODO: error message? caller didn't expect us to fail,   
  88.         // just returning 1 "no reply received" misleads it.   
  89.         goto ret;   
  90.      }   
  91.   
  92.     /* wait for arp reply, and check it */  
  93. //等待时间,超时则认为此ip地址可用   
  94.      timeout_ms = 2000;   
  95.     do {   
  96.         int r;   
  97.          unsigned prevTime = monotonic_us();   
  98.   
  99.          pfd[0].events = POLLIN;   
  100. //这边他是害怕poll被信号打断,因此加了层循环,其实这边我们还可以使用ppoll的,就可以了。   
  101.          r = safe_poll(pfd, 1, timeout_ms);   
  102.         if (r < 0)   
  103.             break;   
  104.         if (r) {   
  105. //读取返回数据.   
  106.              r = read(s, &arp, sizeof(arp));   
  107.             if (r < 0)   
  108.                 break;   
  109. //检测是否为应打包,发送ip是否为我们所请求的ip,这里是为了防止其他的数据包干扰我们检测。   
  110.             if (r >= ARP_MSG_SIZE   
  111.               && arp.operation == htons(ARPOP_REPLY)   
  112.              /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */  
  113.              /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */  
  114.               && *((uint32_t *) arp.sInaddr) == test_ip   
  115.              ) {   
  116. //说明ip地址已被使用   
  117.                  rv = 0;   
  118.                 break;   
  119.              }   
  120.          }   
  121.          timeout_ms -= ((unsigned)monotonic_us() - prevTime) / 1000;   
  122.      } while (timeout_ms > 0);   
  123.   
  124. ret:   
  125.      close(s);   
  126.      DEBUG("%srp reply received for this address", rv ? "No a" : "A");   
  127.     return rv;   
  128. }  

posted on 2010-04-23 03:50 lionel 阅读(823) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理