• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 列出本地网络中的设备名称

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:18:14 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我能够获取连接的设备 IP 和它们的 MAC 地址,并且还想获取设备的名称和服务(开放端口)。

有人可以帮我解决这个问题吗?



Best Answer-推荐答案


这里是可以提供帮助的示例项目An iOS Local Area Network / wifi Scanner

您也可以尝试使用Bonjour

这里 - even more关于局域网中设备的名称


我发现了几个变种:​​

1.

struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, [address UTF8String], &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
if (he) {
    printf("Host name: %s\n", he->h_name);
}

where [address UTF8String] - 比如 127.0.0.1 (ip)

2.

- (NSArray *)hostnamesForAddressNSString *)address {
    // Get the host reference for the given address.
    struct addrinfo      hints;
    struct addrinfo      *result = NULL;
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags    = AI_NUMERICHOST;
    hints.ai_family   = PF_UNSPEC; /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    hints.ai_canonname = NULL;
    hints.ai_addr = NULL;
    hints.ai_next = NULL;

    int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
    if (errorStatus != 0) return @[[self getErrorDescription:errorStatus]];

    CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
    if (addressRef == nil) return nil;

    freeaddrinfo(result);
    CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
    if (hostRef == nil) return nil;
    CFRelease(addressRef);
    BOOL isSuccess = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
    if (!isSuccess) return nil;

    // Get the hostnames for the host reference.
    CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
    NSMutableArray *hostnames = [NSMutableArray array];
    for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; currentIndex++) {
        [hostnames addObject:[(__bridge NSArray *)hostnamesRef objectAtIndex:currentIndex]];
    }

    return hostnames;
}

- (NSString *)getErrorDescriptionNSInteger)errorCode
{
    NSString *errorDescription = @"";;
    switch (errorCode) {
        case EAI_ADDRFAMILY: {
            errorDescription = @" address family for hostname not supported";
            break;
        }
        case EAI_AGAIN: {
            errorDescription = @" temporary failure in name resolution";
            break;
        }
        case EAI_BADFLAGS: {
            errorDescription = @" invalid value for ai_flags";
            break;
        }
        case EAI_FAIL: {
            errorDescription = @" non-recoverable failure in name resolution";
            break;
        }
        case EAI_FAMILY: {
            errorDescription = @" ai_family not supported";
            break;
        }
        case EAI_MEMORY: {
            errorDescription = @" memory allocation failure";
            break;
        }
        case EAI_NODATA: {
            errorDescription = @" no address associated with hostname";
            break;
        }
        case EAI_NONAME: {
            errorDescription = @" hostname nor servname provided, or not known";
            break;
        }
        case EAI_SERVICE: {
            errorDescription = @" servname not supported for ai_socktype";
            break;
        }
        case EAI_SOCKTYPE: {
            errorDescription = @" ai_socktype not supported";
            break;
        }
        case EAI_SYSTEM: {
            errorDescription = @" system error returned in errno";
            break;
        }
        case EAI_BADHINTS: {
            errorDescription = @" invalid value for hints";
            break;
        }
        case EAI_PROTOCOL: {
            errorDescription = @" resolved protocol is unknown";
            break;
        }
        case EAI_OVERFLOW: {
            errorDescription = @" argument buffer overflow";
            break;
        }
    }
    return errorDescription;
}
    3.

您可以从 getifaddrs(&interfaces) 获得其他信息,其中 struct ifaddrs* interfaces 喜欢

    unsigned char *ptr;
    ptr = (unsigned char *)LLADDR((struct sockaddr_dl *)(temp_addr)->ifa_addr);

    NSString *ip = [NSString stringWithUTF8String: inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
    NSArray *hostsNames = [self hostnamesForAddress:ip];
    NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:
                                     @{
                                       @"name": @(temp_addr->ifa_name),
                                       @"flags": @(temp_addr->ifa_flags),
                                       @"ip" : ip,
                                       @"family": @(temp_addr->ifa_addr->sa_family),
                                       @"mac" :  [NSString stringWithFormat"MAC[%02x:%02x:%02x:%02x:%02x:%02x]", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)],
                                       @"hostName" : hostsNames.count ? hostsNames : @""
                                       }
                                     ];

结果你会得到类似的东西:

{
    family = 2;
    flags = 32841;
    hostName =     (
        localhost
    );
    ip = "127.0.0.1";
    mac = "MAC[00:00:00:00:00:00]";
    name = lo0;
}
{
    family = 18;
    flags = 34915;
    hostName = ""; //here empty due to one of the following problem or due to CFHostStartInfoResolution return NO - o this point required a little bit more works to be done
    ip = "6.3.6.0";
    mac = "MAC[02:00:00:00:00:00]";
    name = en1;
}

还有 this post very interesting并且可能有用(我稍后会调查)

关于 ifa_name(接口(interface))的一些有用信息

    //            pdp_ip -  interfaces are those that are used for 3G and cellular data
    //            lo = localhost
    //            en = ether
    //            eth - ethernet
    //            wlan, ww, wl - Wifi - Wireless LAN
    //            awdl - ???
    //            utun - ???
    //            ap - is used to represent currently active data connection, Wi-Fi, cellular data or bluetooth or for access point
    //            bridge - Active hotSpot connection
    //            sl -- serial line IP (slip)

关于ios - 列出本地网络中的设备名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32717953/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap