Advertisement
ibragimovrinat

arpping.c

Aug 12th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.04 KB | None | 0 0
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3.  * Mostly stolen from: dhcpcd - DHCP client daemon
  4.  * by Yoichi Hariguchi <yoichi@fore.com>
  5.  *
  6.  * Licensed under GPLv2, see file LICENSE in this source tree.
  7.  */
  8. #include <netinet/if_ether.h>
  9. #include <net/if_arp.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <sys/time.h>
  13. #include <poll.h>
  14. #include <netinet/in.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <sys/ioctl.h>
  19. #include <linux/if.h>
  20.  
  21. struct arpMsg {
  22.     /* Ethernet header */
  23.     uint8_t  h_dest[6];     /* 00 destination ether addr */
  24.     uint8_t  h_source[6];   /* 06 source ether addr */
  25.     uint16_t h_proto;       /* 0c packet type ID field */
  26.  
  27.     /* ARP packet */
  28.     uint16_t htype;         /* 0e hardware type (must be ARPHRD_ETHER) */
  29.     uint16_t ptype;         /* 10 protocol type (must be ETH_P_IP) */
  30.     uint8_t  hlen;          /* 12 hardware address length (must be 6) */
  31.     uint8_t  plen;          /* 13 protocol address length (must be 4) */
  32.     uint16_t operation;     /* 14 ARP opcode */
  33.     uint8_t  sHaddr[6];     /* 16 sender's hardware address */
  34.     uint8_t  sInaddr[4];    /* 1c sender's IP address */
  35.     uint8_t  tHaddr[6];     /* 20 target's hardware address */
  36.     uint8_t  tInaddr[4];    /* 26 target's IP address */
  37.     uint8_t  pad[18];       /* 2a pad for min. ethernet payload (60 bytes) */
  38. } PACKED;
  39.  
  40. enum {
  41.     ARP_MSG_SIZE = 0x2a
  42. };
  43.  
  44. const int const_int_1 = 1;
  45.  
  46. /* Like strncpy but make sure the resulting string is always 0 terminated. */
  47. static
  48. char* safe_strncpy(char *dst, const char *src, size_t size)
  49. {
  50.         if (!size) return dst;
  51.         dst[--size] = '\0';
  52.         return strncpy(dst, src, size);
  53. }
  54.  
  55. static
  56. unsigned long long monotonic_ms(void)
  57. {
  58.         struct timeval tv;
  59.         gettimeofday (&tv, NULL);
  60.         return tv.tv_sec * 1000ULL + tv.tv_usec/1000;
  61. }
  62.  
  63. /* Wrapper which restarts poll on EINTR or ENOMEM.
  64.  * On other errors does perror("poll") and returns.
  65.  * Warning! May take longer than timeout_ms to return! */
  66. static
  67. int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout)
  68. {
  69.         while (1) {
  70.                 int n = poll(ufds, nfds, timeout);
  71.                 if (n >= 0)
  72.                         return n;
  73.                 /* Make sure we inch towards completion */
  74.                 if (timeout > 0)
  75.                         timeout--;
  76.                 /* E.g. strace causes poll to return this */
  77.                 if (errno == EINTR)
  78.                         continue;
  79.                 /* Kernel is very low on memory. Retry. */
  80.                 /* I doubt many callers would handle this correctly! */
  81.                 if (errno == ENOMEM)
  82.                         continue;
  83.                 return n;
  84.         }
  85. }
  86.  
  87. static
  88. ssize_t safe_read(int fd, void *buf, size_t count)
  89. {
  90.         ssize_t n;
  91.  
  92.         do {
  93.                 n = read(fd, buf, count);
  94.         } while (n < 0 && errno == EINTR);
  95.  
  96.         return n;
  97. }
  98.  
  99. static
  100. char* strncpy_IFNAMSIZ(char *dst, const char *src)
  101. {
  102. #ifndef IFNAMSIZ
  103.         enum { IFNAMSIZ = 16 };
  104. #endif
  105.         return strncpy(dst, src, IFNAMSIZ);
  106. }
  107.  
  108. static
  109. int udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac)
  110. {
  111.     /* char buffer instead of bona-fide struct avoids aliasing warning */
  112.     char ifr_buf[sizeof(struct ifreq)];
  113.     struct ifreq *const ifr = (void *)ifr_buf;
  114.  
  115.     int fd;
  116.     struct sockaddr_in *our_ip;
  117.  
  118.     memset(ifr, 0, sizeof(*ifr));
  119.     fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  120.  
  121.     ifr->ifr_addr.sa_family = AF_INET;
  122.     strncpy_IFNAMSIZ(ifr->ifr_name, interface);
  123.     if (nip) {
  124.         if (ioctl(fd, SIOCGIFADDR, ifr)) {
  125.             close(fd);
  126.             return -1;
  127.         }
  128.         our_ip = (struct sockaddr_in *) &ifr->ifr_addr;
  129.         *nip = our_ip->sin_addr.s_addr;
  130.     }
  131.  
  132.     if (ifindex) {
  133.         if (ioctl(fd, SIOCGIFINDEX, ifr) != 0) {
  134.             close(fd);
  135.             return -1;
  136.         }
  137.         *ifindex = ifr->ifr_ifindex;
  138.     }
  139.  
  140.     if (mac) {
  141.         if (ioctl(fd, SIOCGIFHWADDR, ifr) != 0) {
  142.             close(fd);
  143.             return -1;
  144.         }
  145.         memcpy(mac, ifr->ifr_hwaddr.sa_data, 6);
  146.     }
  147.  
  148.     close(fd);
  149.     return 0;
  150. }
  151.  
  152. /* Returns 1 if no reply received */
  153. int arpping(uint32_t test_nip,
  154.         char *result_mac,
  155.         const char *interface)
  156. {
  157.     int timeout_ms;
  158.     struct pollfd pfd[1];
  159. #define s (pfd[0].fd)           /* socket */
  160.     int rv = 1;             /* "no reply received" yet */
  161.     struct sockaddr addr;   /* for interface name */
  162.     struct arpMsg arp;
  163.    
  164.     uint32_t from_ip;
  165.     uint8_t from_mac[6];
  166.     udhcp_read_interface (interface, NULL, &from_ip, from_mac);
  167.  
  168.     s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
  169.     if (s == -1) {
  170.         //bb_perror_msg(bb_msg_can_not_create_raw_socket);
  171.         return -1;
  172.     }
  173.  
  174.     if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1)) == -1) {
  175.         //bb_perror_msg("can't enable bcast on raw socket");
  176.         goto ret;
  177.     }
  178.  
  179.     /* send arp request */
  180.     memset(&arp, 0, sizeof(arp));
  181.     memset(arp.h_dest, 0xff, 6);                    /* MAC DA */
  182.     memcpy(arp.h_source, from_mac, 6);              /* MAC SA */
  183.     arp.h_proto = htons(ETH_P_ARP);                 /* protocol type (Ethernet) */
  184.     arp.htype = htons(ARPHRD_ETHER);                /* hardware type */
  185.     arp.ptype = htons(ETH_P_IP);                    /* protocol type (ARP message) */
  186.     arp.hlen = 6;                                   /* hardware address length */
  187.     arp.plen = 4;                                   /* protocol address length */
  188.     arp.operation = htons(ARPOP_REQUEST);           /* ARP op code */
  189.     memcpy(arp.sHaddr, from_mac, 6);                /* source hardware address */
  190.     memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
  191.     /* tHaddr is zero-filled */                     /* target hardware address */
  192.     memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
  193.  
  194.     memset(&addr, 0, sizeof(addr));
  195.     safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
  196.     if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
  197.         // TODO: error message? caller didn't expect us to fail,
  198.         // just returning 1 "no reply received" misleads it.
  199.         goto ret;
  200.     }
  201.  
  202.     /* wait for arp reply, and check it */
  203.     timeout_ms = 1000;
  204.     do {
  205.         typedef uint32_t aliased_uint32_t;
  206.         int r;
  207.         unsigned prevTime = monotonic_ms();
  208.  
  209.         pfd[0].events = POLLIN;
  210.         r = safe_poll(pfd, 1, timeout_ms);
  211.         if (r < 0)
  212.             break;
  213.         if (r) {
  214.             r = safe_read(s, &arp, sizeof(arp));
  215.             if (r < 0)
  216.                 break;
  217.  
  218.             if (r >= ARP_MSG_SIZE
  219.              && arp.operation == htons(ARPOP_REPLY)
  220.              /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
  221.              /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
  222.              && *(aliased_uint32_t*)arp.sInaddr == test_nip
  223.             ) {
  224.                 /* if ARP source MAC matches safe_mac
  225.                  * (which is client's MAC), then it's not a conflict
  226.                  * (client simply already has this IP and replies to ARPs!)
  227.                  */
  228.                 sprintf(result_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
  229.                     arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
  230.                     arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
  231.                 break;
  232.             }
  233.         }
  234.         timeout_ms -= (unsigned)monotonic_ms() - prevTime;
  235.     } while (timeout_ms > 0);
  236.  
  237.  ret:
  238.     close(s);
  239.     return rv;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement