Pocha Pocha - 11 months ago 63
C Question

Issues using libpcap in C in OS X

I am trying to compile https://github.com/samueldotj/dhcp-client on Mac OSX. The code compile fine on my linux.

The part of the code (dhcp-client.c) that create issue is replicated below

* Get MAC address of given link(dev_name)
static int
get_mac_address(char *dev_name, u_int8_t *mac)
#ifdef __linux__
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
int result;

strcpy(s.ifr_name, dev_name);
result = ioctl(fd, SIOCGIFHWADDR, &s);
if (result != 0)
return -1;

memcpy((void *)mac, s.ifr_addr.sa_data, 6);
return 0;
struct ifaddrs *ifap, *p;

if (getifaddrs(&ifap) != 0)
return -1;

for (p = ifap; p; p = p->ifa_next)
/* Check the device name */
if ((strcmp(p->ifa_name, dev_name) == 0) &&
(p->ifa_addr->sa_family == AF_LINK))
struct sockaddr_dl* sdp;

sdp = (struct sockaddr_dl*) p->ifa_addr;
memcpy((void *)mac, sdp->sdl_data + sdp->sdl_nlen, 6);

return 0;


LDLIBS = -lpcap

dhcp-client: dhcp-client.c

Relevant Error output

cc dhcp-client.c -lpcap -o dhcp-client
dhcp-client.c:153:36: error: incomplete definition of type 'struct sockaddr_dl'
memcpy((void *)mac, sdp->sdl_data + sdp->sdl_nlen, 6);

Can someone help on this.

Answer Source

Try adding

#ifndef __linux__
#include <net/if_dl.h>

to the top of the file.