Use memcmp() rather than pointer type aliasing in handle_arp_response().
Cosmetic improvements to ARP code.
This commit is contained in:
parent
96f640e36c
commit
96fee03a16
28
ndhc/arp.c
28
ndhc/arp.c
@ -1,5 +1,5 @@
|
||||
/* arp.c - arp ping checking
|
||||
* Time-stamp: <2011-05-31 10:03:34 njk>
|
||||
* Time-stamp: <2011-05-31 11:13:21 njk>
|
||||
*
|
||||
* Copyright 2010-2011 Nicholas J. Kain <njkain@gmail.com>
|
||||
*
|
||||
@ -97,10 +97,10 @@ static int arpping(struct client_state_t *cs, uint32_t test_ip,
|
||||
arp.hlen = 6; /* hardware address length */
|
||||
arp.plen = 4; /* protocol address length */
|
||||
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 is zero-filled */ /* target hardware address */
|
||||
memcpy(arp.tInaddr, &test_ip, sizeof test_ip); /* target IP address */
|
||||
memcpy(arp.smac, from_mac, 6); /* source hardware address */
|
||||
memcpy(arp.sip4, &from_ip, sizeof from_ip); /* source IP address */
|
||||
/* dmac is zero-filled */ /* target hardware address */
|
||||
memcpy(arp.dip4, &test_ip, sizeof test_ip); /* target IP address */
|
||||
|
||||
memset(&addr, 0, sizeof addr);
|
||||
strlcpy(addr.sa_data, interface, sizeof addr.sa_data);
|
||||
@ -244,12 +244,11 @@ static int arp_validate(struct arpMsg *am)
|
||||
return 0;
|
||||
if (memcmp(am->h_dest, client_config.arp, 6))
|
||||
return 0;
|
||||
if (memcmp(am->tHaddr, client_config.arp, 6))
|
||||
if (memcmp(am->dmac, client_config.arp, 6))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
typedef uint32_t aliased_uint32_t __attribute__((__may_alias__));
|
||||
void handle_arp_response(struct client_state_t *cs)
|
||||
{
|
||||
if (arpreply_offset < sizeof arpreply) {
|
||||
@ -283,10 +282,9 @@ void handle_arp_response(struct client_state_t *cs)
|
||||
++arp_packet_num;
|
||||
switch (cs->dhcpState) {
|
||||
case DS_ARP_CHECK:
|
||||
if (*(aliased_uint32_t*)arpreply.sInaddr == arp_dhcp_packet.yiaddr)
|
||||
{
|
||||
if (!memcmp(arpreply.sip4, &arp_dhcp_packet.yiaddr, 4)) {
|
||||
// Check to see if we replied to our own ARP query.
|
||||
if (!memcmp(client_config.arp, arpreply.sHaddr, 6))
|
||||
if (!memcmp(client_config.arp, arpreply.smac, 6))
|
||||
arp_success(cs);
|
||||
else
|
||||
arp_failed(cs);
|
||||
@ -298,10 +296,9 @@ void handle_arp_response(struct client_state_t *cs)
|
||||
}
|
||||
break;
|
||||
case DS_ARP_GW_CHECK:
|
||||
if (*(aliased_uint32_t*)arpreply.sInaddr == cs->routerAddr)
|
||||
{
|
||||
if (!memcmp(arpreply.sip4, &cs->routerAddr, 4)) {
|
||||
// Success only if the router/gw MAC matches stored value
|
||||
if (!memcmp(cs->routerArp, arpreply.sHaddr, 6))
|
||||
if (!memcmp(cs->routerArp, arpreply.smac, 6))
|
||||
arp_gw_success(cs);
|
||||
else
|
||||
arp_gw_failed(cs);
|
||||
@ -313,9 +310,8 @@ void handle_arp_response(struct client_state_t *cs)
|
||||
}
|
||||
break;
|
||||
case DS_BOUND:
|
||||
if (*(aliased_uint32_t*)arpreply.sInaddr == cs->routerAddr)
|
||||
{
|
||||
memcpy(cs->routerArp, arpreply.sHaddr, 6);
|
||||
if (!memcmp(arpreply.sip4, &cs->routerAddr, 4)) {
|
||||
memcpy(cs->routerArp, arpreply.smac, 6);
|
||||
arp_close_fd(cs);
|
||||
|
||||
log_line("gateway hardware address %02x:%02x:%02x:%02x:%02x:%02x",
|
||||
|
10
ndhc/arp.h
10
ndhc/arp.h
@ -1,5 +1,5 @@
|
||||
/* arp.h - functions to call the interface change daemon
|
||||
* Time-stamp: <2011-03-31 02:28:59 nk>
|
||||
* Time-stamp: <2011-05-31 11:11:02 njk>
|
||||
*
|
||||
* Copyright 2010-2011 Nicholas J. Kain <njkain@gmail.com>
|
||||
*
|
||||
@ -42,10 +42,10 @@ struct arpMsg {
|
||||
uint8_t hlen; /* 12 hardware address length (must be 6) */
|
||||
uint8_t plen; /* 13 protocol address length (must be 4) */
|
||||
uint16_t operation; /* 14 ARP opcode */
|
||||
uint8_t sHaddr[6]; /* 16 sender's hardware address */
|
||||
uint8_t sInaddr[4]; /* 1c sender's IP address */
|
||||
uint8_t tHaddr[6]; /* 20 target's hardware address */
|
||||
uint8_t tInaddr[4]; /* 26 target's IP address */
|
||||
uint8_t smac[6]; /* 16 sender's hardware address */
|
||||
uint8_t sip4[4]; /* 1c sender's IP address */
|
||||
uint8_t dmac[6]; /* 20 target's hardware address */
|
||||
uint8_t dip4[4]; /* 26 target's IP address */
|
||||
uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user