POSIX PCAP: Platform agnostic MAC address discovery

pcap
Paul Hollinsky 2019-05-06 13:48:42 -04:00
parent ea8e6e6316
commit 4e0ed09d71
4 changed files with 35 additions and 4 deletions

View File

@ -0,0 +1,11 @@
#ifndef __MACADDR_DARWIN_H_
#define __MACADDR_DARWIN_H_
#include <net/if_dl.h>
#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

View File

@ -0,0 +1,11 @@
#ifndef __MACADDR_LINUX_H_
#define __MACADDR_LINUX_H_
#include <netpacket/packet.h>
#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

View File

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

View File

@ -2,13 +2,13 @@
#include "icsneo/communication/network.h" #include "icsneo/communication/network.h"
#include "icsneo/communication/communication.h" #include "icsneo/communication/communication.h"
#include "icsneo/communication/packetizer.h" #include "icsneo/communication/packetizer.h"
#include "icsneo/platform/posix/macaddr.h"
#include <codecvt> #include <codecvt>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netpacket/packet.h>
using namespace icsneo; using namespace icsneo;
@ -52,9 +52,8 @@ std::vector<PCAP::PCAPFoundDevice> PCAP::FindAll() {
pcap_addr* currentAddress = dev->addresses; pcap_addr* currentAddress = dev->addresses;
bool hasAddress = false; bool hasAddress = false;
while(!hasAddress && currentAddress != nullptr) { while(!hasAddress && currentAddress != nullptr) {
if(currentAddress->addr && currentAddress->addr->sa_family == AF_PACKET) { if(currentAddress->addr && currentAddress->addr->sa_family == ICSNEO_AF_MACADDR) {
struct sockaddr_ll* s = (struct sockaddr_ll*)currentAddress->addr; memcpy(netif.macAddress, PLATFORM_MAC_FROM_SOCKADDR(currentAddress->addr), sizeof(netif.macAddress));
memcpy(netif.macAddress, s->sll_addr, sizeof(netif.macAddress));
hasAddress = true; hasAddress = true;
break; break;
} }