linux下检测ip冲突

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

这里可以看下busybox的dhcp中的检测程序。 
networking/udhcp/arpping.c 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值