Use the standardized low level socket address format.

The raw Ethernet transport code invented its own way of storing the MAC
address into our "struct address" data structure.  However, this private
format is incompatible with the sockaddr_ll returned from the networking
stack.  This patch converts the code to use the proper format.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2015-08-29 10:28:51 +02:00
parent 1e35b91c6a
commit d0eb73c87b
6 changed files with 19 additions and 14 deletions

View File

@ -21,6 +21,7 @@
#define HAVE_ADDRESS_H
#include <netinet/in.h>
#include <netpacket/packet.h>
#include <sys/socket.h>
#include <sys/un.h>
@ -28,6 +29,7 @@ struct address {
socklen_t len;
union {
struct sockaddr_storage ss;
struct sockaddr_ll sll;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
struct sockaddr_un sun;

9
raw.c
View File

@ -188,14 +188,15 @@ no_socket:
static void mac_to_addr(struct address *addr, void *mac)
{
addr->sa.sa_family = AF_UNSPEC;
memcpy(&addr->sa.sa_data, mac, MAC_LEN);
addr->len = sizeof(addr->sa.sa_family) + MAC_LEN;
addr->sll.sll_family = AF_PACKET;
addr->sll.sll_halen = MAC_LEN;
memcpy(addr->sll.sll_addr, mac, MAC_LEN);
addr->len = sizeof(addr->sll);
}
static void addr_to_mac(void *mac, struct address *addr)
{
memcpy(mac, &addr->sa.sa_data, MAC_LEN);
memcpy(mac, &addr->sll.sll_addr, MAC_LEN);
}
static int raw_open(struct transport *t, const char *name,

6
sk.c
View File

@ -170,8 +170,10 @@ int sk_interface_macaddr(const char *name, struct address *mac)
return -1;
}
memcpy(&mac->sa, &ifreq.ifr_hwaddr, sizeof(ifreq.ifr_hwaddr));
mac->len = sizeof(ifreq.ifr_hwaddr.sa_family) + MAC_LEN;
mac->sll.sll_family = AF_PACKET;
mac->sll.sll_halen = MAC_LEN;
memcpy(mac->sll.sll_addr, &ifreq.ifr_hwaddr.sa_data, MAC_LEN);
mac->len = sizeof(mac->sll);
close(fd);
return 0;
}

2
udp.c
View File

@ -256,7 +256,7 @@ static int udp_physical_addr(struct transport *t, uint8_t *addr)
if (udp->mac.len) {
len = MAC_LEN;
memcpy(addr, udp->mac.sa.sa_data, len);
memcpy(addr, udp->mac.sll.sll_addr, len);
}
return len;
}

2
udp6.c
View File

@ -258,7 +258,7 @@ static int udp6_physical_addr(struct transport *t, uint8_t *addr)
if (udp6->mac.len) {
len = MAC_LEN;
memcpy(addr, udp6->mac.sa.sa_data, len);
memcpy(addr, udp6->mac.sll.sll_addr, len);
}
return len;
}

12
util.c
View File

@ -134,14 +134,14 @@ int generate_clock_identity(struct ClockIdentity *ci, const char *name)
if (sk_interface_macaddr(name, &addr))
return -1;
ci->id[0] = addr.sa.sa_data[0];
ci->id[1] = addr.sa.sa_data[1];
ci->id[2] = addr.sa.sa_data[2];
ci->id[0] = addr.sll.sll_addr[0];
ci->id[1] = addr.sll.sll_addr[1];
ci->id[2] = addr.sll.sll_addr[2];
ci->id[3] = 0xFF;
ci->id[4] = 0xFE;
ci->id[5] = addr.sa.sa_data[3];
ci->id[6] = addr.sa.sa_data[4];
ci->id[7] = addr.sa.sa_data[5];
ci->id[5] = addr.sll.sll_addr[3];
ci->id[6] = addr.sll.sll_addr[4];
ci->id[7] = addr.sll.sll_addr[5];
return 0;
}