iOS - 获取ARP表

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

Link1

Link2

答案不清楚如何实现ARP功能。 是否有官方指南?苹果是否批准ARP表功能?
1个回答

8

10.2版本更新后

请在这里检查库。

我终于让它工作了,所以我会详细介绍过程,以节省其他人的时间:

  1. 进入应用程序,在Xcode上右键单击 -> 显示包内容并浏览到:Developer ▸ Platforms ▸ MacOSX.platform ▸ Developer ▸ SDKs ▸ MacOSX10.10.sdk ▸ usr ▸ include。从“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. 然后在你的ViewController中导入MacFinder.h文件
  2. 以下示例展示如何为每个主机查找MAC地址:

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

如果你仍然遇到问题,在这里有教程和这���有完整的工作项目(包括必要的文件)


请随意更新答案并修复泄漏! - BlackM
我按照这些步骤一步一步地操作,但MAC地址始终为空,因为found_entry int始终为0。有人有什么想法吗?(IP地址是100%有效的) - user3191334
1
如果您仍然遇到问题,这里有一个教程 https://medium.com/rocknnull/ios-from-ip-to-mac-address-1e3726ff6d2b#.mdrrodsia 和这里 https://github.com/mavris/MacFinder 完整的工作项目(包括必要的文件)。 - BlackM
iOS 10.2中出现故障。 - ecume des jours
2
我们开了一个问题来解决这个问题。https://github.com/mavris/MMLanScan/issues/3#issuecomment-267268987 - BlackM
显示剩余3条评论

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