繁体   English   中英

使用 Objective-C 获取所有 Mac 的 IP 地址

[英]Get All of Mac's IP Addresses Using Objective-C

我正在尝试获取我的 Mac 的所有 IP 地址(IPv4 + IPv6),但没有看到我期望的结果。 我正在使用此 Stack Overflow帖子中的代码来获取所有 IP 地址的数组,但缺少一个 IP 地址。

我正在使用另一台 Mac 来共享它的互联网连接,并根据Apple 用于测试/支持 IPv6 网络的文档创建一个 NAT64 网络

例如,系统偏好设置 → 网络面板显示我的 IP 地址是:

在此处输入图片说明

...但我发现经过进一步检查,我实际上有两个 IPv6 地址:

在此处输入图片说明

...但只返回其中之一:

"169.254.38.213",
"2001:2:0:aab1:d0ef:646d:f22a:5d83",
"127.0.0.1"

...使用时:

#include <ifaddrs.h>
#include <arpa/inet.h>
#include <net/if.h>

@interface AppDelegate ()

#define IP_ADDR_IPv4    @"ipv4"
#define IP_ADDR_IPv6    @"ipv6"

@end

@implementation AppDelegate

- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
    NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
    NSMutableArray *ipAddressesArray = [[NSMutableArray alloc] init];

    // retrieve the current interfaces - returns 0 on success
    struct ifaddrs *interfaces;

    if (!getifaddrs(&interfaces))
    {
        // Loop through linked list of interfaces
        struct ifaddrs *interface;

        for (interface=interfaces; interface; interface=interface->ifa_next)
        {
            if (!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */)
            {
                continue; // deeply nested code harder to read
            }

            const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
            char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];

            if (addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6))
            {
                NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
                NSString *type;

                if (addr->sin_family == AF_INET)
                {
                    if (inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN))
                    {
                        type = IP_ADDR_IPv4;
                    }
                }
                else
                {
                    const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;

                    if (inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN))
                    {
                        type = IP_ADDR_IPv6;
                    }
                }

                if (type)
                {
                    NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
                    addresses[key] = [NSString stringWithUTF8String:addrBuf];
                }
            }
        }

        for (id key in addresses)
        {
            if (![[addresses valueForKey:key] hasPrefix:@"fe80"])
            {
                [ipAddressesArray addObject:[addresses valueForKey:key]];
            }
        }

        // Free memory
        freeifaddrs(interfaces);
    }

    NSLog(@"%@", ipAddressesArray);
}

知道这里发生了什么吗? 有关系吗? 我正在尝试根据 IP 地址匹配有条件地执行一些其他代码。 如果返回的唯一 IPv6 地址是第一次打开“网络”窗格时在“系统偏好设置”中向用户显示的地址,那么返回的唯一 IPv6 地址是“隐藏”地址,您必须深入研究高级... 部分找到。 先感谢您。

这里的答案是我是个白痴。 正如 Rob Napier 在他们对我的问题的评论中所建议的那样,我基本上是在过滤掉丢失的 IP 地址:

NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];

每个接口可以有多个 IP 地址,但由于我使用接口类型作为addresses字典中的唯一键,因此字典中每个接口只有一个 IP 地址。 我通过返回一个字典数组而不是一个字典来解决这个问题:

if (![[NSString stringWithFormat:@"%s", addrBuf] hasPrefix:@"fe80"])
{
      NSDictionary *addressDict = [NSDictionary dictionaryWithObjectsAndKeys :
                                                   [NSString stringWithFormat:@"%@/%@", name, type], @"Interface",
                                                   [NSString stringWithFormat:@"%s", addrBuf], @"Address",
                                                    nil];

     [addresses addObject:addressDict];
}

return addresses;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM