gpt4 book ai didi

ios - 在 iOS 中获取 LAN 网络中的设备信息

转载 作者:行者123 更新时间:2023-11-29 02:55:04 27 4
gpt4 key购买 nike

我需要获取 iPhone 上每个设备的设备列表(打印机、笔记本电脑等)和信息(IP、MAC、名称)。我不知道怎么办。谁能帮帮我。

最佳答案

因为您已经有了 IP 地址,所以您可以像这样查询 ARP 表

    #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
{

static int nflag;

int flags, 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;
}




}

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *s = @"0.0.0.0";
const char *c = [s UTF8String];

NSLog(@"MAC IS %@", [self ip2mac:c]);
}

并且您需要将这些类添加到您的项目类中

  • “if_types.h”
  • “路线.h”
  • “if_ether.h”

此函数将 IP 地址字符串 (x.x.x.x) 作为 arg 并返回 mac 地址作为字符串。

关于ios - 在 iOS 中获取 LAN 网络中的设备信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24008808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com