【问题标题】:iOS - Get ARP tableiOS - 获取 ARP 表
【发布时间】:2015-07-05 01:41:11
【问题描述】:

我正在尝试构建网络扫描仪。 我知道这个过程,所以我想 ping 网络中所有可用的主机,然后获取 ARP 表,这样我就可以映射每个 IP 的 MAC 地址。 我在 Google 上搜索了 ARP 表,但没有找到如何实现此功能的任何指南。 我还在 Stack Overflow 上发现了这些类似的问题:

Link1

Link2

关于如何实现 ARP 功能的答案尚不清楚。 这个有官方指南吗? Apple 是否批准 ARP 表功能?

【问题讨论】:

    标签: ios iphone network-programming mac-address


    【解决方案1】:

    10.2 后更新

    查看库here

    我终于搞定了,所以我将详细发布该过程以节省其他人的时间:

    1. 转到应用程序并右键单击 Xcode -> 显示包内容并浏览到:开发人员 ▸ 平台 ▸ MacOSX.platform ▸ 开发人员 ▸ SDKs ▸ MacOSX10.10.sdk ▸ usr ▸ 包含。从“net”文件夹复制route.hif_types.h,从“netinet”文件夹复制if_ether.h到你的Xcode项目中。
    2. 然后导入以下.m.h文件:

    MacFinder.h

    #import <Foundation/Foundation.h>
    #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"
    
    #if TARGET_IPHONE_SIMULATOR
    #include <net/route.h>
    #else
    #include "route.h"
    #endif
    
    #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>
    
    @interface MacFinder : NSObject{
    
        int nflag;
    }
    
    -(NSString*) ip2mac: (char*)ip;
    
    @end
    

    MacFinder.m

    #import "MacFinder.h"
    
    @implementation MacFinder
    -(NSString*) ip2mac: (char*)ip
    {
        int  found_entry = 0;
    
    
    
        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;
        }
    
    
    
    
    }
    @end
    
    1. 然后将 MacFinder.h 文件导入您的ViewController
    2. 示例如何将它用于您要查找 MAC 地址的每个主机:

    MacFinder *mc = [[MacFinder alloc]init]; NSString *mac = [mc ip2mac:"192.168.10.24"]; NSLog(@"Mac:%@",mac);

    如果您仍然遇到问题,here 是教程,here 是完整的工作项目(包括必要的文件)

    【讨论】:

    • malloc 的调用是泄漏。对gethostbyaddr 的调用看起来是多余的。否则,这就像一个魅力。谢谢!
    • 请随时更新答案并修复泄漏!
    • 我一步一步按照这些说明进行操作,但mac地址始终为零,因为found_entry int始终为0。有人知道吗? (IP 100% 有效)
    • @user3191334 如果您仍然遇到问题,这里是教程medium.com/rocknnull/… 和这里github.com/mavris/MacFinder 完整的工作项目(包括必要的文件)
    • 为了解决问题,我们打开了一个问题。 github.com/mavris/MMLanScan/issues/3#issuecomment-267268987
    猜你喜欢
    • 2012-05-10
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多