udhcp: add PXELINUX config file option (code 209) definition
[oweals/busybox.git] / networking / udhcp / arpping.c
index 89400e296a0cd86648cb512f94c8bddae3ce2e33..b43e52e964e0b48c3031b41e9c820b9da2d21969 100644 (file)
@@ -1,20 +1,16 @@
 /* vi: set sw=4 ts=4: */
 /*
- * arpping.c
- *
  * Mostly stolen from: dhcpcd - DHCP client daemon
  * by Yoichi Hariguchi <yoichi@fore.com>
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
-
 #include <netinet/if_ether.h>
 #include <net/if_arp.h>
 
 #include "common.h"
 #include "dhcpd.h"
 
-
 struct arpMsg {
        /* Ethernet header */
        uint8_t  h_dest[6];     /* 00 destination ether addr */
@@ -38,9 +34,7 @@ enum {
        ARP_MSG_SIZE = 0x2a
 };
 
-
 /* Returns 1 if no reply received */
-
 int FAST_FUNC arpping(uint32_t test_nip,
                const uint8_t *safe_mac,
                uint32_t from_ip,
@@ -61,7 +55,7 @@ int FAST_FUNC arpping(uint32_t test_nip,
        }
 
        if (setsockopt_broadcast(s) == -1) {
-               bb_perror_msg("cannot enable bcast on raw socket");
+               bb_perror_msg("can't enable bcast on raw socket");
                goto ret;
        }
 
@@ -91,19 +85,20 @@ int FAST_FUNC arpping(uint32_t test_nip,
        /* wait for arp reply, and check it */
        timeout_ms = 2000;
        do {
+               typedef uint32_t aliased_uint32_t FIX_ALIASING;
                int r;
-               unsigned prevTime = monotonic_us();
+               unsigned prevTime = monotonic_ms();
 
                pfd[0].events = POLLIN;
                r = safe_poll(pfd, 1, timeout_ms);
                if (r < 0)
                        break;
                if (r) {
-                       r = read(s, &arp, sizeof(arp));
+                       r = safe_read(s, &arp, sizeof(arp));
                        if (r < 0)
                                break;
 
-                       //bb_error_msg("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
+                       //log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
                        //      arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
                        //      arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
 
@@ -111,20 +106,25 @@ int FAST_FUNC arpping(uint32_t test_nip,
                         && arp.operation == htons(ARPOP_REPLY)
                         /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
                         /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
-                        && *((uint32_t *) arp.sInaddr) == test_nip
+                        && *(aliased_uint32_t*)arp.sInaddr == test_nip
                        ) {
-                                /* if ARP source MAC matches safe_mac
-                                 * (which is client's MAC), then it's not a conflict
-                                 * (client simply already has this IP and replies to ARPs!)
-                                 */
+                               /* if ARP source MAC matches safe_mac
+                                * (which is client's MAC), then it's not a conflict
+                                * (client simply already has this IP and replies to ARPs!)
+                                */
                                if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0)
                                        rv = 0;
-                               //else bb_error_msg("sHaddr == safe_mac");
+                               //else log2("sHaddr == safe_mac");
                                break;
                        }
                }
-               timeout_ms -= ((unsigned)monotonic_us() - prevTime) / 1000;
-       } while (timeout_ms > 0);
+               timeout_ms -= (unsigned)monotonic_ms() - prevTime + 1;
+
+               /* We used to check "timeout_ms > 0", but
+                * this is more under/overflow-resistant
+                * (people did see overflows here when system time jumps):
+                */
+       } while ((unsigned)timeout_ms <= 2000);
 
  ret:
        close(s);