3
0

arpping.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * arpping.c
  4. *
  5. * Mostly stolen from: dhcpcd - DHCP client daemon
  6. * by Yoichi Hariguchi <yoichi@fore.com>
  7. */
  8. #include <netinet/if_ether.h>
  9. #include <net/if_arp.h>
  10. #include "common.h"
  11. #include "dhcpd.h"
  12. struct arpMsg {
  13. /* Ethernet header */
  14. uint8_t h_dest[6]; /* 00 destination ether addr */
  15. uint8_t h_source[6]; /* 06 source ether addr */
  16. uint16_t h_proto; /* 0c packet type ID field */
  17. /* ARP packet */
  18. uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
  19. uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
  20. uint8_t hlen; /* 12 hardware address length (must be 6) */
  21. uint8_t plen; /* 13 protocol address length (must be 4) */
  22. uint16_t operation; /* 14 ARP opcode */
  23. uint8_t sHaddr[6]; /* 16 sender's hardware address */
  24. uint8_t sInaddr[4]; /* 1c sender's IP address */
  25. uint8_t tHaddr[6]; /* 20 target's hardware address */
  26. uint8_t tInaddr[4]; /* 26 target's IP address */
  27. uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
  28. } ATTRIBUTE_PACKED;
  29. /* Returns 1 if no reply received */
  30. int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)
  31. {
  32. int timeout_ms = 2000;
  33. struct pollfd pfd[1];
  34. #define s (pfd[0].fd) /* socket */
  35. int rv = 1; /* "no reply received" yet */
  36. struct sockaddr addr; /* for interface name */
  37. struct arpMsg arp;
  38. s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
  39. if (s == -1) {
  40. bb_perror_msg(bb_msg_can_not_create_raw_socket);
  41. return -1;
  42. }
  43. if (setsockopt_broadcast(s) == -1) {
  44. bb_perror_msg("cannot setsocketopt on raw socket");
  45. goto ret;
  46. }
  47. /* send arp request */
  48. memset(&arp, 0, sizeof(arp));
  49. memset(arp.h_dest, 0xff, 6); /* MAC DA */
  50. memcpy(arp.h_source, from_mac, 6); /* MAC SA */
  51. arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
  52. arp.htype = htons(ARPHRD_ETHER); /* hardware type */
  53. arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
  54. arp.hlen = 6; /* hardware address length */
  55. arp.plen = 4; /* protocol address length */
  56. arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
  57. memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
  58. memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
  59. /* tHaddr */ /* target hardware address */
  60. memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* target IP address */
  61. memset(&addr, 0, sizeof(addr));
  62. safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
  63. if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
  64. goto ret;
  65. /* wait for arp reply, and check it */
  66. do {
  67. int r;
  68. unsigned prevTime = monotonic_us();
  69. pfd[0].events = POLLIN;
  70. r = safe_poll(pfd, 1, timeout_ms);
  71. if (r < 0) {
  72. break;
  73. } else if (r) {
  74. if (read(s, &arp, sizeof(arp)) < 0)
  75. break;
  76. if (arp.operation == htons(ARPOP_REPLY)
  77. && memcmp(arp.tHaddr, from_mac, 6) == 0
  78. && *((uint32_t *) arp.sInaddr) == test_ip
  79. ) {
  80. rv = 0;
  81. break;
  82. }
  83. }
  84. timeout_ms -= (monotonic_us() - prevTime) / 1000;
  85. } while (timeout_ms > 0);
  86. ret:
  87. close(s);
  88. DEBUG("%srp reply received for this address", rv ? "No a" : "A");
  89. return rv;
  90. }