From 4e0ed09d71023ce823ce3f66800a6aa68e3527ea Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Mon, 6 May 2019 13:48:42 -0400 Subject: [PATCH] POSIX PCAP: Platform agnostic MAC address discovery --- include/icsneo/platform/posix/darwin/macaddr.h | 11 +++++++++++ include/icsneo/platform/posix/linux/macaddr.h | 11 +++++++++++ include/icsneo/platform/posix/macaddr.h | 10 ++++++++++ platform/posix/pcap.cpp | 7 +++---- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 include/icsneo/platform/posix/darwin/macaddr.h create mode 100644 include/icsneo/platform/posix/linux/macaddr.h create mode 100644 include/icsneo/platform/posix/macaddr.h diff --git a/include/icsneo/platform/posix/darwin/macaddr.h b/include/icsneo/platform/posix/darwin/macaddr.h new file mode 100644 index 0000000..8e988a1 --- /dev/null +++ b/include/icsneo/platform/posix/darwin/macaddr.h @@ -0,0 +1,11 @@ +#ifndef __MACADDR_DARWIN_H_ +#define __MACADDR_DARWIN_H_ + +#include + +#define ICSNEO_AF_MACADDR (AF_LINK) + +// Points towards the first byte of the MAC address, given a sockaddr* +#define PLATFORM_MAC_FROM_SOCKADDR(SADDR) (((struct sockaddr_dl*)SADDR)->sdl_data + ((struct sockaddr_dl*)SADDR)->sdl_nlen) + +#endif \ No newline at end of file diff --git a/include/icsneo/platform/posix/linux/macaddr.h b/include/icsneo/platform/posix/linux/macaddr.h new file mode 100644 index 0000000..3af0c7c --- /dev/null +++ b/include/icsneo/platform/posix/linux/macaddr.h @@ -0,0 +1,11 @@ +#ifndef __MACADDR_LINUX_H_ +#define __MACADDR_LINUX_H_ + +#include + +#define ICSNEO_AF_MACADDR (AF_PACKET) + +// Points towards the first byte of the MAC address, given a sockaddr* +#define PLATFORM_MAC_FROM_SOCKADDR(SADDR) (((struct sockaddr_ll*)SADDR)->sll_addr) + +#endif \ No newline at end of file diff --git a/include/icsneo/platform/posix/macaddr.h b/include/icsneo/platform/posix/macaddr.h new file mode 100644 index 0000000..74b1e2f --- /dev/null +++ b/include/icsneo/platform/posix/macaddr.h @@ -0,0 +1,10 @@ +#ifndef __MACADDR_POSIX_H_ +#define __MACADDR_POSIX_H_ + +#ifdef __APPLE__ +#include "icsneo/platform/posix/darwin/macaddr.h" +#else +#include "icsneo/platform/posix/linux/macaddr.h" +#endif + +#endif \ No newline at end of file diff --git a/platform/posix/pcap.cpp b/platform/posix/pcap.cpp index 22589d3..5933161 100644 --- a/platform/posix/pcap.cpp +++ b/platform/posix/pcap.cpp @@ -2,13 +2,13 @@ #include "icsneo/communication/network.h" #include "icsneo/communication/communication.h" #include "icsneo/communication/packetizer.h" +#include "icsneo/platform/posix/macaddr.h" #include #include #include #include #include #include -#include using namespace icsneo; @@ -52,9 +52,8 @@ std::vector PCAP::FindAll() { pcap_addr* currentAddress = dev->addresses; bool hasAddress = false; while(!hasAddress && currentAddress != nullptr) { - if(currentAddress->addr && currentAddress->addr->sa_family == AF_PACKET) { - struct sockaddr_ll* s = (struct sockaddr_ll*)currentAddress->addr; - memcpy(netif.macAddress, s->sll_addr, sizeof(netif.macAddress)); + if(currentAddress->addr && currentAddress->addr->sa_family == ICSNEO_AF_MACADDR) { + memcpy(netif.macAddress, PLATFORM_MAC_FROM_SOCKADDR(currentAddress->addr), sizeof(netif.macAddress)); hasAddress = true; break; }