欢迎来到天天文库
浏览记录
ID:39465395
大小:16.00 KB
页数:4页
时间:2019-07-04
《linuxIP冲突检测》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、原理其实很简单,那就是广播一个arp包,然后recv,如果没有数据(这里要设置延时),那么说明这个ip是可用的,否则就检测这个数据是否为回复我们发出的arp的应答包.如果是则证明ip已被使用,否则继续等待.这里可以看下busybox的dhcp中的检测程序。networking/udhcp/arpping.cC代码1./*vi:setsw=4ts=4:*/2./*3.*arpping.c4.*5.*Mostlystolenfrom:dhcpcd-DHCPclientdaemon6.*byYoichiHariguchi7.*/8.9.#include2、et/if_ether.h>10.#include11.12.#include"common.h"13.#include"dhcpd.h"14.15.//这里是arp包的格式,其中的数据格式都是宏了,比如uint_8_t为无符char.16.structarpMsg{17./*Ethernetheader*/18.uint8_th_dest[6];/*00destinationetheraddr*/19.uint8_th_source[6];/*06sourceetheraddr*/20.uint16_th_proto;/*0cpackettypeIDfield*3、/21.22./*ARPpacket*/23.uint16_thtype;/*0ehardwaretype(mustbeARPHRD_ETHER)*/24.uint16_tptype;/*10protocoltype(mustbeETH_P_IP)*/25.uint8_thlen;/*12hardwareaddresslength(mustbe6)*/26.uint8_tplen;/*13protocoladdresslength(mustbe4)*/27.uint16_toperation;/*14ARPopcode*/28.uint8_tsHaddr[6];/*16sender'shar4、dwareaddress*/29.uint8_tsInaddr[4];/*1csender'sIPaddress*/30.uint8_ttHaddr[6];/*20target'shardwareaddress*/31.uint8_ttInaddr[4];/*26target'sIPaddress*/32.uint8_tpad[18];/*2apadformin.ethernetpayload(60bytes)*/33.}PACKED;34.35.enum{36.ARP_MSG_SIZE=0x2a37.};38.39.40./*Returns1ifnoreplyreceived*/41.425、.//主程序,如果返回1说明此ip可用43.intarpping(uint32_ttest_ip,uint32_tfrom_ip,uint8_t*from_mac,constchar*interface)44.{45.46.inttimeout_ms;47.//这里使用poll来检测句柄。48.structpollfdpfd[1];49.#defines(pfd[0].fd)/*socket*/50.intrv=1;/*"noreplyreceived"yet*/51.structsockaddraddr;/*forinterfacename*/52.structarpMsgarp;53.56、4.//建立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("cannotenablebcastonrawsocket");7、64.gotoret;65.}66.//进行组包,由于是要广播,因此目的mac地址为全0.67./*sendarprequest*/68.memset(&arp,0,sizeof(arp));69.memset(arp.h_dest,0xff,6);/*MACDA*/70.memcpy(arp.h_source,from_mac,6);/*MACSA*/71.arp.h_proto=htons(ETH_P_A
2、et/if_ether.h>10.#include11.12.#include"common.h"13.#include"dhcpd.h"14.15.//这里是arp包的格式,其中的数据格式都是宏了,比如uint_8_t为无符char.16.structarpMsg{17./*Ethernetheader*/18.uint8_th_dest[6];/*00destinationetheraddr*/19.uint8_th_source[6];/*06sourceetheraddr*/20.uint16_th_proto;/*0cpackettypeIDfield*
3、/21.22./*ARPpacket*/23.uint16_thtype;/*0ehardwaretype(mustbeARPHRD_ETHER)*/24.uint16_tptype;/*10protocoltype(mustbeETH_P_IP)*/25.uint8_thlen;/*12hardwareaddresslength(mustbe6)*/26.uint8_tplen;/*13protocoladdresslength(mustbe4)*/27.uint16_toperation;/*14ARPopcode*/28.uint8_tsHaddr[6];/*16sender'shar
4、dwareaddress*/29.uint8_tsInaddr[4];/*1csender'sIPaddress*/30.uint8_ttHaddr[6];/*20target'shardwareaddress*/31.uint8_ttInaddr[4];/*26target'sIPaddress*/32.uint8_tpad[18];/*2apadformin.ethernetpayload(60bytes)*/33.}PACKED;34.35.enum{36.ARP_MSG_SIZE=0x2a37.};38.39.40./*Returns1ifnoreplyreceived*/41.42
5、.//主程序,如果返回1说明此ip可用43.intarpping(uint32_ttest_ip,uint32_tfrom_ip,uint8_t*from_mac,constchar*interface)44.{45.46.inttimeout_ms;47.//这里使用poll来检测句柄。48.structpollfdpfd[1];49.#defines(pfd[0].fd)/*socket*/50.intrv=1;/*"noreplyreceived"yet*/51.structsockaddraddr;/*forinterfacename*/52.structarpMsgarp;53.5
6、4.//建立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("cannotenablebcastonrawsocket");
7、64.gotoret;65.}66.//进行组包,由于是要广播,因此目的mac地址为全0.67./*sendarprequest*/68.memset(&arp,0,sizeof(arp));69.memset(arp.h_dest,0xff,6);/*MACDA*/70.memcpy(arp.h_source,from_mac,6);/*MACSA*/71.arp.h_proto=htons(ETH_P_A
此文档下载收益归作者所有