내 Mac의 모든 IP 주소 (IPv4 + IPv6)를 가져 오려고하는데 예상 한 결과가 표시되지 않습니다. 이 Stack Overflow 게시물의 코드를 사용하여 모든 IP 주소의 배열을 가져 오고 있지만 IP 주소가 누락되었습니다.
다른 Mac을 사용하여 인터넷 연결을 공유하고 IPv6 네트워크 테스트 / 지원을 위해 Apple의 설명서에 따라 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);
}
Any idea what is going on here? Does it matter? I'm trying to execute some other code conditionally based on IP address matching. It'd be one thing if the only IPv6 address returned was the one displayed to the user in System Preferences when first opening the Network pane, but instead the only IPv6 address returned is the "hidden" one that you have to dig into the Advanced... section to find. Thank you in advance.
The answer here is that I am an idiot. As suggested by Rob Napier in their comment on my question, I was essentially filtering out the missing IP addresses:
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;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다