版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、ARP協(xié)議的c語言實現(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年鐵路專用廣播系統(tǒng)改造合同2篇
- 二零二五年鍋爐安裝與遠程故障診斷合同3篇
- 2025年電子商務合同電子簽名法律效力及實施指南3篇
- 2025年西瓜種植基地勞動力采購合同3篇
- 二零二五年航空貨運駕駛員勞務合同范本3篇
- 2025年度汽車修理廠轉(zhuǎn)讓及租賃合同模板6篇
- 二零二五版集體合同范本:企業(yè)員工培訓與發(fā)展策略2篇
- 二零二五版房屋買賣更名與租金調(diào)整補充協(xié)議3篇
- 2025年會員權(quán)益協(xié)議解除協(xié)議
- 二零二五版辦公場地租賃合同及增值服務協(xié)議3篇
- 2025年河北供水有限責任公司招聘筆試參考題庫含答案解析
- Unit3 Sports and fitness Discovering Useful Structures 說課稿-2024-2025學年高中英語人教版(2019)必修第一冊
- 農(nóng)發(fā)行案防知識培訓課件
- 社區(qū)醫(yī)療抗菌藥物分級管理方案
- NB/T 11536-2024煤礦帶壓開采底板井下注漿加固改造技術(shù)規(guī)范
- 巴布亞新幾內(nèi)亞離網(wǎng)光儲微網(wǎng)供電方案
- 高度限位裝置類型及原理
- 中文版gcs electrospeed ii manual apri rev8v00印刷稿修改版
- 新生兒預防接種護理質(zhì)量考核標準
- 除氧器出水溶解氧不合格的原因有哪些
- 沖擊式機組水輪機安裝概述與流程
評論
0/150
提交評論