如何在iPhone上查询ARP表?

10

我是 iPhone 开发的新手,想在我的应用程序中集成 Wake-On-LAN 功能,而不会让用户在已知 IP 地址的情况下输入计算机的 MAC 地址。

我花了几个小时在 Google 上搜索,获取了 ARP 工具的源代码,但我不知道如何在 iPhone 上管理它。

3个回答

21

既然没有人回答我的问题,那么这就是答案 ;)

#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/sysctl.h>

#include <net/if.h>
#include <net/if_dl.h>
#include "if_types.h"
#include "route.h"
#include "if_ether.h"
#include <netinet/in.h>


#include <arpa/inet.h>

#include <err.h>
#include <errno.h>
#include <netdb.h>

#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

    -(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;
        }




    }

从苹果的arp.c在iPhone上工作

复制以下内容:

- "if_types.h"
- "route.h"
- "if_ether.h"

将Mac上的头文件包含到您的项目文件夹中,并添加到类中


谢谢你的建议。我只复制了“route.h”头文件,因为其他文件已经包含在SDK(3.2+)中了。 - Laurent Etiemble
1
嗨,@安东尼,我遇到了nflag未声明的标识符错误,我需要包含什么才能使它工作? - Bonnie
1
静态整型变量 nflag; - Nikolay Shubenkov
2
@anthony vage:不好意思,我是Objective-C的新手。我已经将上述代码添加到“X-code项目”中。在添加到“.m文件”后,如何调用“-(NSString *)ip2mac:(in_addr_t)addr”? - Wun
苹果会批准这个API吗? - BlackM
苹果公司是否批准这样的应用程序? - Piyush Mathur

8

我简化了代码并修复了内存泄漏问题。

在我的机器上,头文件可以在 /usr/include/netinet 和 /usr/include/net 中找到。

- (NSString*)ip2mac:(in_addr_t)addr
{
    NSString *ret = nil;

    size_t needed;
    char *buf, *next;

    struct rt_msghdr *rtm;
    struct sockaddr_inarp *sin;
    struct sockaddr_dl *sdl;

    int mib[6];

    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, sizeof(mib) / sizeof(mib[0]), NULL, &needed, NULL, 0) < 0)
        err(1, "route-sysctl-estimate");

    if ((buf = (char*)malloc(needed)) == NULL)
        err(1, "malloc");

    if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &needed, NULL, 0) < 0)
        err(1, "retrieval of routing table");

    for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {

        rtm = (struct rt_msghdr *)next;
        sin = (struct sockaddr_inarp *)(rtm + 1);
        sdl = (struct sockaddr_dl *)(sin + 1);

        if (addr != sin->sin_addr.s_addr || sdl->sdl_alen < 6)
            continue;

        u_char *cp = (u_char*)LLADDR(sdl);

        ret = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
               cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];

        break;
    }

    free(buf);

    return ret;
}

请您能否提供一个简单的演示代码,因为我无法使用它。谢谢。 - Darshan Kunjadiya

2

我不确定sysctl函数是否属于公共API。 我使用了明显的方法,但我的应用程序因“非公共”API使用而被拒绝。 然而,我也认为除了上面展示的方法之外没有其他方法可行。


你好new299,如果我将其用于合法且公开的应用程序中,它会被应用商店拒绝吗? - FujiRoyale
这是对我来说,那是一段时间以前的事了。也许值得一试。 - new299
1
我知道这个话题有些偏离,但你是否有方向可以寻找在网络上编程识别设备的方法,并且这种方法能够被苹果官方所接受?我已经在Stack Overflow上提出了问题链接 - FujiRoyale

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