diff options
author | Denis Vlasenko | 2007-11-22 00:58:49 +0000 |
---|---|---|
committer | Denis Vlasenko | 2007-11-22 00:58:49 +0000 |
commit | 223bc97f61f2fd5efbccede0837a374c1669e864 (patch) | |
tree | 0105a1f02dbc344ede7cca99565b6c556e7cab96 /networking/udhcp/arpping.c | |
parent | c881c733bb05286f7147d0ca49ad238b86f6d848 (diff) | |
download | busybox-223bc97f61f2fd5efbccede0837a374c1669e864.zip busybox-223bc97f61f2fd5efbccede0837a374c1669e864.tar.gz |
udhcpc: an option to perform ARP check (Jonas Danielsson <jonas.danielsson@axis.com>)
configurable, ~+300 bytes when on.
Diffstat (limited to 'networking/udhcp/arpping.c')
-rw-r--r-- | networking/udhcp/arpping.c | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c index 7b702d8..45597c0 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; |