6 #include <sys/socket.h>
12 * Returns the IPv6 address with which you have connectivity at the moment.
15 const char *get_ipv6_addr() {
16 static char buf[INET6_ADDRSTRLEN+1];
17 struct addrinfo hints;
18 struct addrinfo *result, *resp;
21 memset(&hints, 0, sizeof(struct addrinfo));
22 hints.ai_family = AF_INET6;
24 /* We resolve the K root server to get a public IPv6 address. You can
25 * replace this with any other host which has an AAAA record, but the
26 * K root server is a pretty safe bet. */
27 if (getaddrinfo("k.root-servers.net", "domain", &hints, &result) != 0) {
28 /* We don’t display the error here because most
29 * likely, there just is no connectivity.
30 * Thus, don’t spam the user’s console. */
34 for (resp = result; resp != NULL; resp = resp->ai_next) {
35 if ((fd = socket(resp->ai_family, SOCK_DGRAM, 0)) == -1) {
40 /* Since the socket was created with SOCK_DGRAM, this is
41 * actually not establishing a connection or generating
42 * any other network traffic. Instead, as a side-effect,
43 * it saves the local address with which packets would
44 * be sent to the destination. */
45 if (connect(fd, resp->ai_addr, resp->ai_addrlen) == -1) {
46 /* We don’t display the error here because most
47 * likely, there just is no IPv6 connectivity.
48 * Thus, don’t spam the user’s console but just
49 * try the next address. */
54 struct sockaddr_storage local;
55 socklen_t local_len = sizeof(struct sockaddr_storage);
56 if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
57 perror("getsockname()");
64 memset(buf, 0, INET6_ADDRSTRLEN + 1);
66 if ((ret = getnameinfo((struct sockaddr*)&local, local_len,
67 buf, sizeof(buf), NULL, 0,
68 NI_NUMERICHOST)) != 0) {
69 fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));