Pocha Pocha - 1 month ago 10
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);
close(fd);
if (result != 0)
return -1;

memcpy((void *)mac, s.ifr_addr.sa_data, 6);
return 0;
#else
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);
break;
}
}
freeifaddrs(ifap);
#endif

return 0;
}


Makefile

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

Try adding

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

to the top of the file.