发送arp包.docx_第1页
发送arp包.docx_第2页
发送arp包.docx_第3页
发送arp包.docx_第4页
发送arp包.docx_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

代码来自busybox,函数询问TEST_IP的mac地址。read_interface函数通过ioctl来获取接口interface相关信息,arpping用于发送arp数据包。也可以使用s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP);则地址则变成了struct sockaddr_ll addr; 在arpping中调用以下函数,则也可以发送# include # include # include # include # include # include # include # include # include # include # include # include # include # include enum ARP_MSG_SIZE = 0x2a ; char * strncpy_IFNAMSIZ( char * dst, const char * src) # ifndef IFNAMSIZ enum IFNAMSIZ = 16 ; # endif return strncpy ( dst, src, IFNAMSIZ) ; struct arpMsg /* Ethernet header */ uint8_t h_dest 6 ; /* 00 destination ether addr */ uint8_t h_source 6 ; /* 06 source ether addr */ uint16_t h_proto; /* 0c packet type ID field */ /* ARP packet */ uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */ uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */ uint8_t hlen; /* 12 hardware address length (must be 6) */ uint8_t plen; /* 13 protocol address length (must be 4) */ uint16_t operation; /* 14 ARP opcode */ uint8_t sHaddr 6 ; /* 16 senders hardware address */ uint8_t sInaddr 4 ; /* 1c senders IP address */ uint8_t tHaddr 6 ; /* 20 targets hardware address */ uint8_t tInaddr 4 ; /* 26 targets IP address */ uint8_t pad 18 ; /* 2a pad for min. ethernet payload (60 bytes) */ PACKED; const int const_int_1 = 1; int setsockopt_broadcast( int fd) return setsockopt ( fd, SOL_SOCKET, SO_BROADCAST, & const_int_1, sizeof ( const_int_1) ) ; char * safe_strncpy( char * dst, const char * src, size_t size) if ( ! size) return dst; dst - - size = 0 ; return strncpy ( dst, src, size) ; int arpping( uint32_t test_ip, uint32_t from_ip, uint8_t * from_mac, const char * interface) int timeout_ms; int s; int rv = 1; /* no reply received yet */ struct sockaddr addr; /* for interface name */ struct arpMsg arp; s = socket ( PF_PACKET , SOCK_PACKET , htons ( ETH_P_ARP) ) ; if ( s = = - 1) perror ( raw_socket ) ; return - 1; if ( setsockopt_broadcast( s) = = - 1) perror ( cannot enable bcast on raw socket ) ; goto ret; /* send arp request */ memset ( & arp, 0, sizeof ( arp) ) ; memset ( arp. h_dest, 0xff, 6) ; /* MAC DA */ memcpy ( arp. h_source, from_mac, 6) ; /* MAC SA */ arp. h_proto = htons ( ETH_P_ARP) ; /* protocol type (Ethernet) */ arp. htype = htons ( ARPHRD_ETHER) ; /* hardware type */ arp. ptype = htons ( ETH_P_IP) ; /* protocol type (ARP message) */ arp. hlen = 6; /* hardware address length */ arp. plen = 4; /* protocol address length */ arp. operation = htons ( ARPOP_REQUEST) ; /* ARP op code */ memcpy ( arp. sHaddr, from_mac, 6) ; /* source hardware address */ memcpy ( arp. sInaddr, & from_ip, sizeof ( from_ip) ) ; /* source IP address */ /* tHaddr is zero-fiiled */ /* target hardware address */ memcpy ( arp. tInaddr, & test_ip, sizeof ( test_ip) ) ; /* target IP address */ memset ( & addr, 0, sizeof ( addr) ) ; safe_strncpy( addr. sa_data, interface, sizeof ( addr. sa_data) ) ; if ( sendto ( s, & arp, sizeof ( arp) , 0, & addr, sizeof ( addr) ) sin_addr. s_addr; printf ( ip of %s = %s n , interface, inet_ntoa( our_ip- sin_addr) ) ; if ( ifindex) if ( ioctl( fd, SIOCGIFINDEX , & ifr) ! = 0) close ( fd) ; return - 1; printf ( adapter index %d , ifr. ifr_ifindex) ; * ifindex = ifr. ifr_ifindex; if ( arp) if ( ioctl( fd, SIOCGIFHWADDR , & ifr) ! = 0) close ( fd) ; return - 1; memcpy ( arp, ifr. ifr_hwaddr. sa_data, 6) ; printf ( adapter hardware address %02x:%02x:%02x:%02x:%02x:%02xn , arp 0 , arp 1 , arp 2 , arp 3 , arp 4 , arp 5 ) ; close ( fd) ; return 0; int main( void ) uint32_t TEST_IP = inet_addr( 94 ) ; char interface = eth0 ; uint32_t ip; uint8_t mac 6 ; read_interface( interface, NULL , & ip, mac) ; while ( 1) arpping( TEST_IP, ip, mac, interface) ; sleep ( 1) ; return 0; 本文来自CSDN博客,转载请标明出处:/cybertan/archive/2010/10/20/5953102.aspx发送arp数据包 (2010-07-19 17:37) 分类: linux.applicaiton 1.arp发送#include #include #include #include #include #include #include #include #include #include #include #include #include #include enumARP_MSG_SIZE = 0x2a;char* strncpy_IFNAMSIZ(char *dst, const char *src)#ifndef IFNAMSIZenum IFNAMSIZ = 16 ;#endifreturn strncpy(dst, src, IFNAMSIZ);struct arpMsg /* Ethernet header */uint8_t h_dest6; /* 00 destination ether addr */uint8_t h_source6; /* 06 source ether addr */uint16_t h_proto; /* 0c packet type ID field */* ARP packet */uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */uint8_t hlen; /* 12 hardware address length (must be 6) */uint8_t plen; /* 13 protocol address length (must be 4) */uint16_t operation; /* 14 ARP opcode */uint8_t sHaddr6; /* 16 senders hardware address */uint8_t sInaddr4; /* 1c senders IP address */uint8_t tHaddr6; /* 20 targets hardware address */uint8_t tInaddr4; /* 26 targets IP address */uint8_t pad18; /* 2a pad for min. ethernet payload (60 bytes) */ PACKED;const int const_int_1 = 1;int setsockopt_broadcast(int fd)return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1);char* safe_strncpy(char *dst, const char *src, size_t size)if (!size) return dst;dst-size = 0;return strncpy(dst, src, size);int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)int timeout_ms;int s;int rv = 1; /* no reply received yet */struct sockaddr addr; /* for interface name */struct arpMsg arp;s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP);if (s = -1) perror(raw_socket);return -1;if (setsockopt_broadcast(s) = -1) perror(cannot enable bcast on raw socket);goto ret;/* send arp request */memset(&arp, 0, sizeof(arp);memset(arp.h_dest, 0xff, 6); /* MAC DA */memcpy(arp.h_source, from_mac, 6); /* MAC SA */arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */arp.htype = htons(ARPHRD_ETHER); /* hardware type */arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */arp.hlen = 6; /* hardware address length */arp.plen = 4; /* protocol address length */arp.operation = htons(ARPOP_REQUEST); /* ARP op code */memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */memcpy(arp.sInaddr, &from_ip, sizeof(from_ip); /* source IP address */* tHaddr is zero-fiiled */ /* target hardware address */memcpy(arp.tInaddr, &test_ip, sizeof(test_ip); /* target IP address */memset(&addr, 0, sizeof(addr);safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data);if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr) sin_addr.s_addr;printf(ip of %s = %s n, interface, inet_ntoa(our_ip-sin_addr);if (ifindex) if (ioctl(fd, SIOCGIFINDEX, &ifr) != 0) close(fd);return -1;printf(adapter index %d, ifr.ifr_ifindex);*ifindex = ifr.ifr_ifindex;if (arp) if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) close(fd);return -1;memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);printf(adapter hardware address %02x:%02x:%02x:%02x:%02x:%02xn,arp0, arp1, arp2, arp3, arp4, arp5);close(fd);return 0;int main(void)uint32_t TEST_IP = inet_addr(94);char interface=eth0;uint32_t ip;uint8_t mac6;read_interface(interface, NULL, &ip, mac); while(1)arpping(TE

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论