在OS X中使用C语言中的libpcap的问题

时间:2016-10-25 03:53:53

标签: c macos libpcap

我正在尝试在Mac OSX上编译https://github.com/samueldotj/dhcp-client。代码在我的linux上编译正常。

创建问题的代码部分(dhcp-client.c)在

下面复制
/*
 * 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;
}

生成文件

LDLIBS = -lpcap

dhcp-client: dhcp-client.c 

相关错误输出

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);

有人可以为此提供帮助。

1 个答案:

答案 0 :(得分:1)

尝试添加

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

到文件的顶部。