在iPhone/iPad上获取ARP表

Ric*_*iks 6 xcode objective-c arp ios

我想在我的iPad上获取ARP条目,就像这里一样.

当编译代码在我的iPad上运行(所以不是模拟器)时,我会丢失标题错误消息.到您在此提到的本地项目,你可以通过复制的头文件解决这些问题.

问题在于线

sdl =(struct sockaddr_dl*)(sin + 1);

在这段代码中:

-(NSString*) ip2mac: (char*) ip 
{ 

    int expire_time, flags, export_only, doing_proxy, found_entry; 

    NSString *mAddr = nil; 
    u_long addr = inet_addr(ip); 
    int mib[6]; 
    size_t needed; 
    char *host, *lim, *buf, *next; 
    struct rt_msghdr *rtm; 
    struct sockaddr_inarp *sin; 
    struct sockaddr_dl *sdl; 
    extern int h_errno; 
    struct hostent *hp; 

    mib[0] = CTL_NET; 
    mib[1] = PF_ROUTE; 
    mib[2] = 0; 
    mib[3] = AF_INET; 
    mib[4] = NET_RT_FLAGS; 
    mib[5] = RTF_LLINFO; 
    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 
        err(1, "route-sysctl-estimate"); 
    if ((buf = malloc(needed)) == NULL) 
        err(1, "malloc"); 
    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 
        err(1, "actual retrieval of routing table"); 


    lim = buf + needed; 
    for (next = buf; next < lim; next += rtm->rtm_msglen) { 
        rtm = (struct rt_msghdr *)next; 
        sin = (struct sockaddr_inarp *)(rtm + 1); 
        sdl = (struct sockaddr_dl *)(sin + 1); 
        if (addr) { 
            if (addr != sin->sin_addr.s_addr) 
                continue; 
            found_entry = 1; 
        } 
        if (nflag == 0) 
            hp = gethostbyaddr((caddr_t)&(sin->sin_addr), 
                               sizeof sin->sin_addr, AF_INET); 
        else 
            hp = 0; 
        if (hp) 
            host = hp->h_name; 
        else { 
            host = "?"; 
            if (h_errno == TRY_AGAIN) 
                nflag = 1; 
        } 



        if (sdl->sdl_alen) { 

            u_char *cp = LLADDR(sdl); 

            mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]]; 


        //  ether_print((u_char *)LLADDR(sdl)); 
        } 
        else 

            mAddr = nil; 



    } 


    if (found_entry == 0) { 
        return nil; 
    } else { 
        return mAddr; 
    } 




} 
Run Code Online (Sandbox Code Playgroud)

它给出以下错误消息:

指向不完整类型'struct sockaddr_inarp*'的指针算术

编译iPad模拟器的代码时,一切运行正常.

有谁知道如何解决这个问题?这里要问一个类似的问题(但没有解决).

Gre*_*nds 3

导入 <netinet/if_ether.h> 后,您应该编辑它并更改行

#include <net/if_arp.h>
Run Code Online (Sandbox Code Playgroud)

#include "if_arp.h"
Run Code Online (Sandbox Code Playgroud)

然后也在您的项目中导入 <net/if_arp.h> 。这应该可以修复该错误。

无论如何,您需要导入来编译您发布的代码的标头是:

#include "if_ether.h"
#include "route.h"
#include "if_arp.h"
#include "if_dl.h"
Run Code Online (Sandbox Code Playgroud)

希望这有帮助 =)

编辑:

您需要“将文件添加到项目”,而不是简单地使用 #import 或 #include 导入它。您可以通过以下链接找到上述文件: “netinet”下的文件 “net”下的文件