Advertisement
Guest User

Untitled

a guest
May 12th, 2013
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/ioctl.h>
  8. #include <unistd.h>
  9. #include <arpa/inet.h>
  10. #include <linux/if_ether.h>
  11. #include <net/if.h>
  12. #include <netpacket/packet.h>
  13.  
  14. struct ethernet {
  15.     unsigned char dest[6];
  16.     unsigned char source[6];
  17.     uint16_t eth_type;
  18. };
  19.  
  20. struct arp {
  21.     uint16_t htype;
  22.     uint16_t ptype;
  23.     unsigned char hlen;
  24.     unsigned char plen;
  25.     uint16_t oper;
  26.     /* addresses */
  27.     unsigned char sender_ha[6];
  28.     unsigned char sender_pa[4];
  29.     unsigned char target_ha[6];
  30.     unsigned char target_pa[4];
  31. };
  32.  
  33. #define ETH_HDR_LEN 14
  34. #define BUFF_SIZE 2048
  35.  
  36. #define ARP_PROTO 0x0806
  37.  
  38. static void dump_arp(struct arp *arp_hdr);
  39.  
  40. int main(void)
  41. {
  42.     int sock, err;
  43.     void *buffer = NULL;
  44.     ssize_t recvd_size;
  45.     struct sockaddr_ll s_ll;
  46.     struct ethernet *eth_hdr = NULL;
  47.     struct arp *arp_hdr = NULL;
  48.  
  49.     if( (sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1)
  50.     // if( (sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
  51.     {
  52.         perror("socket(): ");
  53.         exit(-1);
  54.     }
  55.     s_ll.sll_family = AF_PACKET;
  56.     s_ll.sll_protocol = htons(ETH_P_ARP);
  57.     //s_ll.sll_protocol = htons(ETH_P_ARP);
  58.     s_ll.sll_ifindex = 0; // AHTUNG all ifaces
  59.     //s_ll.sll_ifindex = 2 // eth0
  60.     if( (err = bind(sock, (struct sockaddr *)&s_ll, sizeof(s_ll))) == -1)
  61.     {
  62.         perror("bind(): ");
  63.         exit(-1);
  64.     }
  65.  
  66.     buffer = malloc(BUFF_SIZE);
  67.     while(1)
  68.     {
  69.         if( (recvd_size = recv(sock, buffer, BUFF_SIZE, 0)) == -1)
  70.         {
  71.             perror("recv(): ");
  72.             free(buffer);
  73.             close(sock);
  74.             exit(-1);
  75.         }
  76.         if(recvd_size <= (sizeof(struct ethernet) + sizeof(struct arp)))
  77.         {
  78.             printf("Short packet. Packet len: %d\n", recvd_size);
  79.             continue;
  80.         }
  81.         eth_hdr = (struct ethernet *)buffer;
  82.         if(ntohs(eth_hdr->eth_type) != ARP_PROTO)
  83.             continue;
  84.         arp_hdr = (struct arp *)(buffer+ETH_HDR_LEN);
  85.         dump_arp(arp_hdr);
  86.     }
  87.     free(buffer);
  88.     close(sock);
  89. }
  90.  
  91. static void
  92. dump_arp(struct arp *arp_hdr)
  93. {
  94.     uint16_t htype = ntohs(arp_hdr->htype);
  95.     uint16_t ptype = ntohs(arp_hdr->ptype);
  96.     uint16_t oper = ntohs(arp_hdr->oper);
  97.     switch(htype)
  98.     {
  99.         case 0x0001:
  100.             printf("ARP HTYPE: Ethernet(0x%04X)\n", htype);
  101.             break;
  102.         default:
  103.             printf("ARP HYPE: 0x%04X\n", htype);
  104.             break;
  105.     }
  106.     switch(ptype)
  107.     {
  108.         case 0x0800:
  109.             printf("ARP PTYPE: IPv4(0x%04X)\n", ptype);
  110.             break;
  111.         default:
  112.             printf("ARP PTYPE: 0x%04X\n", ptype);
  113.             break;
  114.     }
  115.     printf("ARP HLEN: %d\n", arp_hdr->hlen);
  116.     printf("ARP PLEN: %d\n", arp_hdr->plen);
  117.     switch(oper)
  118.     {
  119.         case 0x0001:
  120.             printf("ARP OPER: Request(0x%04X)\n", oper);
  121.             break;
  122.         case 0x0002:
  123.             printf("ARP OPER: Response(0x%04X)\n", oper);
  124.             break;
  125.         default:
  126.             printf("ARP OPER: 0x%04X\n", oper);
  127.             break;
  128.     }
  129.     printf("ARP Sender HA: %02X:%02X:%02X:%02X:%02X:%02X\n",
  130.            arp_hdr->sender_ha[0],arp_hdr->sender_ha[1],arp_hdr->sender_ha[2],
  131.            arp_hdr->sender_ha[3], arp_hdr->sender_ha[4], arp_hdr->sender_ha[5]);
  132.     printf("ARP Sender PA: %d.%d.%d.%d\n", arp_hdr->sender_pa[0],
  133.            arp_hdr->sender_pa[1], arp_hdr->sender_pa[2], arp_hdr->sender_pa[3]);
  134.     printf("ARP Target HA: %02X:%02X:%02X:%02X:%02X:%02X\n",
  135.            arp_hdr->target_ha[0],arp_hdr->target_ha[1],arp_hdr->target_ha[2],
  136.            arp_hdr->target_ha[3], arp_hdr->target_ha[4], arp_hdr->target_ha[5]);
  137.     printf("ARP Target PA: %d.%d.%d.%d\n", arp_hdr->target_pa[0],
  138.            arp_hdr->target_pa[1], arp_hdr->target_pa[2], arp_hdr->target_pa[3]);
  139.     printf("ARP DONE =====================\n");
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement