簡體   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