iphone - 如何在iPhone上查询ARP表?

标签 iphone ip-address mac-address arp

我是 iPhone 开发新手,我希望将 LAN 唤醒功能集成到我的应用程序中,而不需要用户在 IP 已知时输入计算机 MAC 地址。 我在谷歌上搜索了大约几个小时,得到了一个 ARP 工具的源代码,但我不知道如何在 iPhone 上管理它。

最佳答案

由于没有人回答我的问题...这就是答案;)

#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 的 header 包含到您的项目文件夹中并添加到类中

关于iphone - 如何在iPhone上查询ARP表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2258172/

相关文章:

java - 使用JAVA获取IP方向

actionscript-3 - 如何在 Flex 中查找系统(本地)IP 地址?

iphone - 从 MAC 地址识别设备类型

iphone - 防止系统框架记录错误信息

iphone - NSDecimalNumber 和从末尾弹出数字

Java:如何获取可以到达远程IP的本地接口(interface)的IP?

android - 如何在 c# 中使用 6.0 或更高版本获取 Android 设备的 Mac 地址?

flutter - 如何使用 flutter 扫描本地网络上的所有设备

iphone - player.duration 在视频文件的 MPMoviePlayerController 中始终显示为零

iphone - 从第一个 View 调用第二个 View