iOS - Get ARP table(iOS - 获取 ARP 表)
问题描述
我正在尝试构建网络扫描仪.我知道这个过程,所以我想 ping 网络中所有可用的主机,然后获取 ARP 表,这样我就可以映射每个 IP 的 MAC 地址.我在 Google 上搜索了 ARP 表,但没有找到如何实现此功能的任何指南.我还在 Stack Overflow 上发现了这些类似的问题:
I am trying to build a network scanner. I know the procedure so I want to ping all available hosts in the network and then get the ARP table so I can map the MAC address for each IP. I Googled for ARP table but I didn't find any guide how to implement this feature. I also found these similar questions on Stack overflow:
Link1
Link2
关于如何实现 ARP 功能的答案尚不清楚.这个有官方指南吗?Apple 是否批准 ARP 表功能?
The answer are unclear on how to implement the ARP feature. Is there any official guide for this one? Is Apple approving the ARP table feature?
推荐答案
10.2后更新
检查库这里
我终于搞定了,所以我将详细发布该过程以节省其他人的时间:
I finally got it working so I will post the procedure in detail to save some time for other guys:
- 转到 Applications 并右键单击 Xcode -> Show package contents 并浏览到:Developer ▸ Platforms ▸ MacOSX.platform ▸ Developer ▸ SDKs ▸ MacOSX10.10.sdk ▸ usr ▸ include.从net"文件夹复制
route.h
和if_types.h
并从netinet"文件夹复制if_ether.h
到您的 Xcode项目. - 然后导入以下
.m
和.h
文件:
- Go to Applications and right click on Xcode -> Show package contents and browse to: Developer ▸ Platforms ▸ MacOSX.platform ▸ Developer ▸ SDKs ▸ MacOSX10.10.sdk ▸ usr ▸ include. From "net" folder copy the
route.h
andif_types.h
and from the "netinet" folder copy theif_ether.h
into your Xcode project. - Then import the following
.m
and.h
files:
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
- 然后在你的
ViewController
中导入 MacFinder.h 文件 - 示例如何将它用于您要查找 MAC 地址的每个主机:
- Then import the MacFinder.h file in your
ViewController
- Example how to use it for each host you want to find the MAC Address:
MacFinder *mc = [[MacFinder alloc]init];NSString *mac = [mc ip2mac:"192.168.10.24"];NSLog(@"Mac:%@",mac);
如果您仍然遇到问题 这里是教程,这里是完整的工作项目(包括必要的文件)
If you still having trouble here is tutorial and here the full working project (Including the necessary files)
这篇关于iOS - 获取 ARP 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS - 获取 ARP 表
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01