From 223bc97f61f2fd5efbccede0837a374c1669e864 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 22 Nov 2007 00:58:49 +0000 Subject: udhcpc: an option to perform ARP check (Jonas Danielsson ) configurable, ~+300 bytes when on. --- networking/udhcp/arpping.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'networking/udhcp/arpping.c') diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c index 7b702d8f3..45597c04b 100644 --- a/networking/udhcp/arpping.c +++ b/networking/udhcp/arpping.c @@ -32,12 +32,16 @@ struct arpMsg { uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */ } ATTRIBUTE_PACKED; +enum { + ARP_MSG_SIZE = 0x2a +}; + /* Returns 1 if no reply received */ int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface) { - int timeout_ms = 2000; + int timeout_ms; struct pollfd pfd[1]; #define s (pfd[0].fd) /* socket */ int rv = 1; /* "no reply received" yet */ @@ -51,7 +55,7 @@ int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *i } if (setsockopt_broadcast(s) == -1) { - bb_perror_msg("cannot setsocketopt on raw socket"); + bb_perror_msg("cannot enable bcast on raw socket"); goto ret; } @@ -67,28 +71,35 @@ int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *i arp.operation = htons(ARPOP_REQUEST); /* ARP op code */ memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */ memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */ - /* tHaddr */ /* target hardware address */ + /* tHaddr is zero-fiiled */ /* target hardware address */ memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* target IP address */ memset(&addr, 0, sizeof(addr)); safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data)); - if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) + if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) { + // TODO: error message? caller didn't expect us to fail, + // just returning 1 "no reply received" misleads it. goto ret; + } /* wait for arp reply, and check it */ + timeout_ms = 2000; do { int r; unsigned prevTime = monotonic_us(); pfd[0].events = POLLIN; r = safe_poll(pfd, 1, timeout_ms); - if (r < 0) { + if (r < 0) break; - } else if (r) { - if (read(s, &arp, sizeof(arp)) < 0) + if (r) { + r = read(s, &arp, sizeof(arp)); + if (r < 0) break; - if (arp.operation == htons(ARPOP_REPLY) - && memcmp(arp.tHaddr, from_mac, 6) == 0 + if (r >= ARP_MSG_SIZE + && 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_ip ) { rv = 0; -- cgit v1.2.3