Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535610
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: LINUX

2015-06-18 16:14:48

arpsend_dgram.c

点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <sys/socket.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/types.h>
  9. #include <netinet/in.h>
  10. #include <netinet/ip.h>
  11. #include <netinet/if_ether.h>
  12. #include <net/if_arp.h>
  13. #include <netpacket/packet.h>
  14. #include <net/if.h>
  15. #include <net/ethernet.h>
  16. #include <arpa/inet.h>
  17. #define BUFLEN 42

  18. /*
  19. ************************eth structure**************************************
  20. struct ethhdr {
  21.     unsigned char h_dest[ETH_ALEN];
  22.     unsigned char h_source[ETH_ALEN];
  23.     __be16 h_proto;
  24. } __attribute__((packed));

  25. struct ether_header
  26. {
  27.     u_int8_t ether_dhost[ETH_ALEN]; // destination eth addr
  28.     u_int8_t ether_shost[ETH_ALEN]; // source ether addr
  29.     u_int16_t ether_type; // packet type ID field
  30. } __attribute__ ((__packed__));
  31. **************************arp structrure (if_arp.h)**************************
  32. struct arphdr
  33. {
  34.     __be16 ar_hrd; //format of hardware address
  35.     __be16 ar_pro; // format of protocol address *\/
  36.     unsigned char ar_hln; // length of hardware address *\/
  37.     unsigned char ar_pln; // length of protocol address *\/
  38.     __be16 ar_op; // ARP opcode (command) *\/

  39. #if 0
  40.     // Ethernet looks like this : This bit is variable sized however...*\/
  41.     unsigned char ar_sha[ETH_ALEN]; // sender hardware address *\/
  42.     unsigned char ar_sip[4]; // sender IP address *\/
  43.     unsigned char ar_tha[ETH_ALEN]; // target hardware address *\/
  44.     unsigned char ar_tip[4]; // target IP address *\/
  45. #endif

  46. };
  47. *************************ether_arp structure********************************
  48. __BEGIN_DECLS
  49. //
  50.  * Ethernet Address Resolution Protocol.
  51.  *
  52.  * See RFC 826 for protocol description. Structure below is adapted
  53.  * to resolving internet addresses. Field names used correspond to
  54.  * RFC 826.
  55.  *\/
  56. struct ether_arp {
  57.     struct arphdr ea_hdr; // fixed-size header *\/
  58.     u_int8_t arp_sha[ETH_ALEN]; // sender hardware address *\/
  59.     u_int8_t arp_spa[4]; // sender protocol address *\/
  60.     u_int8_t arp_tha[ETH_ALEN]; // target hardware address *\/
  61.     u_int8_t arp_tpa[4]; // target protocol address *\/
  62. };
  63. #define arp_hrd ea_hdr.ar_hrd
  64. #define arp_pro ea_hdr.ar_pro
  65. #define arp_hln ea_hdr.ar_hln
  66. #define arp_pln ea_hdr.ar_pln
  67. #define arp_op ea_hdr.ar_op

  68. **************************ifreq structure************************************
  69. struct ifreq
  70. {
  71. #define IFHWADDRLEN 6
  72.     union
  73.     {
  74.         char ifrn_name[IFNAMSIZ];
  75.     } ifr_ifrn;

  76.     union {
  77.         struct sockaddr ifru_addr;
  78.         struct sockaddr ifru_dstaddr;
  79.         struct sockaddr ifru_broadaddr;
  80.         struct sockaddr ifru_netmask;
  81.         struct sockaddr ifru_hwaddr;
  82.         short ifru_flags;
  83.         int ifru_ivalue;
  84.         int ifru_mtu;
  85.         struct ifmap ifru_map;
  86.         char ifru_slave[IFNAMSIZ];
  87.         char ifru_newname[IFNAMSIZ];
  88.         void __user * ifru_data;
  89.         struct if_settings ifru_settings;
  90.     } ifr_ifru;
  91. };
  92. #define ifr_name ifr_ifrn.ifrn_name
  93. #define ifr_hwaddr ifr_ifru.ifru_hwaddr
  94. #define ifr_addr ifr_ifru.ifru_addr
  95. #define ifr_dstaddr ifr_ifru.ifru_dstaddr
  96. #define ifr_broadaddr ifr_ifru.ifru_broadaddr
  97. #define ifr_netmask ifr_ifru.ifru_netmask
  98. #define ifr_flags ifr_ifru.ifru_flags
  99. #define ifr_metric ifr_ifru.ifru_ivalue
  100. #define ifr_mtu ifr_ifru.ifru_mtu
  101. #define ifr_map ifr_ifru.ifru_map
  102. #define ifr_slave ifr_ifru.ifru_slave
  103. #define ifr_data ifr_ifru.ifru_data
  104. #define ifr_ifindex ifr_ifru.ifru_ivalue
  105. #define ifr_bandwidth ifr_ifru.ifru_ivalue
  106. #define ifr_qlen ifr_ifru.ifru_ivalue
  107. #define ifr_newname ifr_ifru.ifru_newname
  108. #define ifr_settings ifr_ifru.ifru_settings
  109.  */
  110. int main(int argc, char *argv[])
  111. {
  112.     int skfd,n;
  113.     struct ether_arp *arp;
  114.     struct ifreq ifr;
  115.     unsigned char buff[BUFLEN];
  116.     unsigned char src_mac[ETH_ALEN];
  117.     struct sockaddr_ll toaddr;
  118.     struct in_addr targetIP,srcIP;

  119.     if(argc != 3) {
  120.         printf("Usage: %s outinterface dstIP\n",argv[0]);
  121.         exit(1);
  122.     }
  123.     skfd = socket(AF_PACKET,SOCK_DGRAM,htons(ETH_P_ARP));
  124.     if(skfd < 0 ) {
  125.         perror("Create Error");
  126.         exit(1);
  127.     }
  128.     bzero(&toaddr,sizeof(toaddr));
  129.     bzero(&ifr,sizeof(ifr));
  130.     strcpy(ifr.ifr_name,argv[1]);
  131.     if(-1 == ioctl(skfd,SIOCGIFINDEX,&ifr)) {
  132.         perror("get interface index error");
  133.         exit(1);
  134.     }
  135.     
  136.     toaddr.sll_ifindex = ifr.ifr_ifindex;
  137.     printf("interface Index:%d\n",ifr.ifr_ifindex);

  138.     if(-1 == ioctl(skfd,SIOCGIFHWADDR,&ifr)) {
  139.         perror("get dev mac addr error");
  140.         exit(1);
  141.     }
  142.     memcpy(src_mac,ifr.ifr_hwaddr.sa_data,ETH_ALEN);
  143.     printf("Src mac is %02x:%02x:%02x:%02x:%02x:%02x\n",src_mac[0],src_mac[1],
  144.             src_mac[2],src_mac[3],src_mac[4],
  145.             src_mac[5]);
  146.     if(-1 == ioctl(skfd,SIOCGIFADDR,&ifr)) {
  147.         perror("get ip addr");
  148.         exit(1);
  149.     }
  150.     srcIP.s_addr = ((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr.s_addr;
  151.     printf("IP addr:%s\n",inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
  152.   
  153.     /*construct arp header*/
  154.     arp = (struct ether_arp*)(buff);
  155.     arp->arp_hrd = htons(ARPHRD_ETHER);
  156.     arp->arp_pro = htons(ETHERTYPE_IP);
  157.     arp->arp_hln = ETH_ALEN;
  158.     arp->arp_pln = 4;
  159.     arp->arp_op = htons(ARPOP_REQUEST);
  160.     memcpy(arp->arp_sha,src_mac,ETH_ALEN);
  161.     memcpy(arp->arp_spa,&srcIP,sizeof(srcIP));
  162.     memset(arp->arp_tha,0,ETH_ALEN);
  163.     inet_pton(AF_INET,argv[2],&targetIP);
  164.     memcpy(arp->arp_tpa,&targetIP,4);
  165.     //toaddr.sll_family = PF_PACKET;
  166.     //toaddr.sll_halen = ETHER_ADDR_LEN;
  167.     toaddr.sll_protocol = htons(ETH_P_ARP);
  168.     n = sendto(skfd,buff,BUFLEN,0,(struct sockaddr*)&toaddr,sizeof(toaddr));
  169.     
  170.     printf("send arp request %d bytes\n",n);
  171.     close(skfd);

  172.     return 0;
  173. }
编译&运行:

抓包结果:
阅读(5166) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~