在iPhone/iPad上获取ARP表

6
我正在尝试获取我的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; 
    } 




} 

它会给出以下错误信息:
指针进行不完整类型“struct sockaddr_inarp*”的算术运算
当您为iPad模拟器编译代码时,一切正常。
有人有解决这个问题的想法吗? 类似的问题(但未解决)在此处提出: 这里

嗨@Richard:我得到了nflag未声明的标识符,我需要包含/导入哪个文件? - Bonnie
@Richard:我是Objective-C的新手,我想获取连接到热点的客户端的IP地址。我找到了ARP.cARP.h文件。但是如何使用它来获取ARP表中的IP地址呢? - Martin
请问,您是将代码放在 .m 文件中直接编译吗? - Wun
苹果会拒绝这段代码吗? - BlackM
2个回答

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

#include <net/if_arp.h>

to

#include "if_arp.h"

然后在你的项目中也要导入<net/if_arp.h>。这应该可以解决那个错误。

无论如何,编译你发布的代码所需的头文件是:

#include "if_ether.h"
#include "route.h"
#include "if_arp.h"
#include "if_dl.h"

希望这些有所帮助 =)
编辑:
您需要“添加文件到项目”,而不仅仅是使用#import或#include导入它。 您可以在以下链接中找到上述文件: “netinet”下的文件 “net”下的文件

起初我在头文件<netinet/if_ether.h>中找不到入口<net/if_arp.h>。在我的系统上有多个版本。是我的问题。导入并修改正确的版本后,它终于工作了!!! :-) 非常感谢! - Richard Schiks
1
对于那些不知道如何找到这些文件的人,它们位于/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/usr/include/。当然,5.1版本可能会在未来发生变化,但这些文件的位置可能不会改变。 - Scott Berrevoets

3
#include <net/ethernet.h>

不要去修改原始标头,可以参考这里获取更多信息。该错误是由于C99中函数'ether_ntoa'的隐式声明无效所导致的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接