udhcpc: an option to perform ARP check (Jonas Danielsson <jonas.danielsson@axis.com>)

configurable, ~+300 bytes when on.
This commit is contained in:
Denis Vlasenko
2007-11-22 00:58:49 +00:00
parent c881c733bb
commit 223bc97f61
9 changed files with 138 additions and 47 deletions

View File

@@ -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;