IPv6 link local address resolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <arpa/inet.h> | |
#include <ifaddrs.h> | |
#include <iostream> | |
#include <netdb.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
int main() { | |
struct ifaddrs* addr; | |
getifaddrs(&addr); | |
for(auto a = addr; a != nullptr; a = a->ifa_next) { | |
auto a2 = a->ifa_addr; | |
if( a2 == nullptr ) continue; | |
auto af = a2->sa_family; | |
char host[100]; | |
char serv[100]; | |
std::size_t salen = sizeof(struct sockaddr_in6); | |
switch(af) { | |
case AF_INET: | |
salen = sizeof(struct sockaddr_in); | |
case AF_INET6: | |
getnameinfo(a2, (socklen_t)salen, | |
host, sizeof(host), | |
serv, sizeof(serv), | |
NI_NUMERICHOST); | |
std::cout << "Name: " << a->ifa_name << std::endl | |
<< "Family: " << af << std::endl | |
<< "Address: " << host << std::endl | |
<< "Scope ID: " << reinterpret_cast<struct sockaddr_in6*>(a2)->sin6_scope_id << std::endl | |
<< std::endl; | |
break; | |
default: | |
continue; | |
} | |
} | |
freeifaddrs(addr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
リンクローカルアドレス(ひいては IPv6 アドレス)の一意性は
sin6_addr
(128bit) +sin6_scope_id
の組で決まると確認できた。