ARP協(xié)議的c語(yǔ)言實(shí)現(xiàn)源代碼_第1頁(yè)
ARP協(xié)議的c語(yǔ)言實(shí)現(xiàn)源代碼_第2頁(yè)
ARP協(xié)議的c語(yǔ)言實(shí)現(xiàn)源代碼_第3頁(yè)
ARP協(xié)議的c語(yǔ)言實(shí)現(xiàn)源代碼_第4頁(yè)
ARP協(xié)議的c語(yǔ)言實(shí)現(xiàn)源代碼_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、ARP協(xié)議的c語(yǔ)言實(shí)現(xiàn)源代碼【轉(zhuǎn)載】 收藏 什么是ARP協(xié)議英文原義:Address Resolution Protocol 中文釋義:(RFC-826)地址解析協(xié)議 #include <netdb.h>#include <unistd.h>#include <ctype.h>#include <errno.h>#include <signal.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <getopt.

2、h>#include <sys/uio.h>#include <sys/file.h>#include <sys/time.h>#include <sys/signal.h>#include <sys/ioctl.h>#include <sys/param.h>#include <sys/socket.h>#include <linux/sockios.h>#include <linux/if.h>#include <linux/if_arp.h>#include &l

3、t;linux/if_packet.h>#include <linux/types.h>#include <netinet/in.h>#include <arpa/inet.h>#define src_addr "192.168.0.239"#define device "eth0"#define fill_buf "aaaaaaaaaaaa"int socket_id;char *target = src_addr; int send_count = 0;int recv_count = 0

4、;struct in_addr src, dst;struct sockaddr_ll me, he;struct timeval send_time, recv_time;struct in_addr get_src_ip(char * devices)struct sockaddr_in saddr;int sock_id = socket(AF_INET, SOCK_DGRAM, 0);if (sock_id < 0) perror("socket"); exit(2);if (devices) if (setsockopt(sock_id, SOL_SOCKE

5、T, SO_BINDTODEVICE, device, strlen(device)+1) = -1) perror("WARNING: interface is ignored");int alen = sizeof(saddr); memset(&saddr, 0, sizeof(saddr);saddr.sin_port = htons(0x1000);saddr.sin_family = AF_INET; if (connect(sock_id, (struct sockaddr*)&saddr, sizeof(saddr) = -1) perror

6、("connect"); exit(2);if (getsockname(sock_id, (struct sockaddr*)&saddr, &alen) = -1) perror("getsockname"); exit(2);close(sock_id);return saddr.sin_addr;int check_device(char* if_dev, int ss)int ifindex;struct ifreq ifr;memset(&ifr, 0, sizeof(ifr);strncpy(ifr.ifr_name

7、, if_dev, IFNAMSIZ-1); if (ioctl(ss, SIOCGIFINDEX, &ifr) < 0) fprintf(stderr, "arping: unknown iface %sn", if_dev); exit(2); ifindex = ifr.ifr_ifindex; if (ioctl(ss, SIOCGIFFLAGS, (char*)&ifr) perror("ioctl(SIOCGIFFLAGS)"); exit(2); if (!(ifr.ifr_flags&IFF_UP) prin

8、tf("Interface "%s" is downn", if_dev); exit(2); if (ifr.ifr_flags&(IFF_NOARP|IFF_LOOPBACK) printf("Interface "%s" is not ARPablen", if_dev); exit(2); return ifindex; / check_device()int socket_init()int s, s_errno;s = socket(PF_PACKET, SOCK_DGRAM, 0);s_err

9、no = errno;me.sll_family = AF_PACKET;me.sll_ifindex = check_device(device, s); me.sll_protocol = htons(ETH_P_ARP);if (bind(s, (struct sockaddr*)&me, sizeof(me) = -1) perror("bind"); exit(2); int alen = sizeof(me);if (getsockname(s, (struct sockaddr*)&me, &alen) = -1) perror(&qu

10、ot;getsockname"); exit(2);if (me.sll_halen = 0) printf("Interface "%s" is not ARPable (no ll address)n", device); exit(2);he = me;memset(he.sll_addr, -1, he.sll_halen); / set dmac addr FF:FF:FF:FF:FF:FFreturn s;int create_pkt(unsigned char * buf, struct in_addr src, struct i

11、n_addr dst, struct sockaddr_ll * FROM, struct sockaddr_ll * TO)struct arphdr *ah = (struct arphdr*) buf;unsigned char *p = (unsigned char *)(ah+1);ah->ar_hrd = htons(FROM->sll_hatype);if (ah->ar_hrd = htons(ARPHRD_FDDI) ah->ar_hrd = htons(ARPHRD_ETHER);ah->ar_pro = htons(ETH_P_IP);ah-

12、>ar_hln = FROM->sll_halen;ah->ar_pln = 4;ah->ar_op = htons(ARPOP_REQUEST);memcpy(p, &FROM->sll_addr, ah->ar_hln);p+=FROM->sll_halen;memcpy(p, &src, 4);p+=4;memcpy(p, &TO->sll_addr, ah->ar_hln);p+=ah->ar_hln;memcpy(p, &dst, 4);p+=4; memcpy(p, fill_buf, st

13、rlen(fill_buf) );p+=12;return (p-buf);void send_pkt() unsigned char send_buf256; int pkt_size = create_pkt(send_buf, src, dst, &me, &he);gettimeofday(&send_time, NULL); int cc = sendto(socket_id, send_buf, pkt_size, 0, (struct sockaddr*)&he, sizeof(he);if( cc = pkt_size ) send_count+

14、; alarm(1);int chk_recv_pkt(unsigned char * buf, struct sockaddr_ll * FROM)struct arphdr *ah = (struct arphdr*)buf;unsigned char *p = (unsigned char *)(ah+1);struct in_addr src_ip, dst_ip;if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY) return 0;if (ah->ar_pr

15、o != htons(ETH_P_IP) | ah->ar_pln != 4 | ah->ar_hln != me.sll_halen ) return 0;memcpy(&src_ip, p+ah->ar_hln, 4);memcpy(&dst_ip, p+ah->ar_hln+4+ah->ar_hln, 4); if (src_ip.s_addr != dst.s_addr | src.s_addr != dst_ip.s_addr ) return 0;return (p-buf);void disp_info(int received, s

16、truct in_addr dst, int msecs, int usecs, struct sockaddr_ll from)printf("%03d ", received);printf("%s ", from.sll_pkttype=PACKET_HOST ? "Unicast" : "Broadcast");printf("%s from %s", "reply", inet_ntoa(dst) );printf(" %02X:%02X:%02X:%02

17、X:%02X:%02X ", from.sll_addr0, from.sll_addr1, from.sll_addr2, from.sll_addr3, from.sll_addr4, from.sll_addr5); printf(" %ld.%ld msn", (long int)msecs, (long int)usecs);fflush(stdout); void finish()printf("nSent %d ARP probe packet(s) n", send_count);printf("Received %d

18、 response(s)", recv_count);printf("nn");fflush(stdout);exit(!recv_count);/intmain(int argc, char *argv)uid_t uid = getuid(); setuid(uid);if( *(argv+1) != NULL ) target = *(argv+1);if (inet_aton(target, &dst) != 1) struct hostent *hp; hp = gethostbyname2(target, AF_INET); printf(&q

19、uot;ntarget = %s n", target ); if (!hp) fprintf(stderr, "arping: unknown host %sn", target); exit(2); memcpy(&dst, hp->h_addr, 4);src = get_src_ip(device);if (!src.s_addr ) fprintf(stderr, "arping: no source address in not-DAD moden"); exit(2);socket_id = socket_init(); printf("nARPING %s ", inet_ntoa(dst);printf("from %s %snn", inet_ntoa(src), device ? : "");signal(SIGINT, finish);signal(SIGALRM, send_pkt);send_pkt();while(1) str

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論