【问题标题】:Get All of Mac's IP Addresses Using Objective-C使用 Objective-C 获取 Mac 的所有 IP 地址
【发布时间】:2020-05-20 09:29:20
【问题描述】:

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

我正在使用另一台 Mac 来共享它的互联网连接并根据 Apple's documentation for testing/supporting IPv6 networks 创建一个 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 地址是您必须深入了解“高级”的“隐藏”地址...部分找到。提前谢谢你。

【问题讨论】:

  • 我建议单步执行此代码并查看getifaddrs 是否正在返回此地址并且您正在对其进行过滤,或者getifaddrs 是否根本没有返回它。
  • 您可能会遇到 IPv6 隐私扩展,您最终会获得临时地址,并且您会经常更改地址。此外,您还想获得 Link-Local IPv6 地址。最好通过 CLI 查看该界面上的实际内容。我知道 Windows CLI 提供的解释比 GUI 工具更好。
  • 感谢您的建议和抽出宝贵时间审阅此问题。我感觉自己像一个真正的傻瓜,因为我实际上实际上是在过滤掉我认为缺失的值。我已经发布了这个问题的答案。

标签: objective-c macos ip-address ipv6


【解决方案1】:

这里的答案是我是个白痴。正如 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2023-03-15
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多