7 #include <sys/socket.h>
10 #include <arpa/inet.h>
11 #include <yajl/yajl_gen.h>
12 #include <yajl/yajl_version.h>
16 static char *get_sockname(struct addrinfo *addr) {
17 static char buf[INET6_ADDRSTRLEN+1];
18 struct sockaddr_storage local;
22 if ((fd = socket(addr->ai_family, SOCK_DGRAM, 0)) == -1) {
27 /* Since the socket was created with SOCK_DGRAM, this is
28 * actually not establishing a connection or generating
29 * any other network traffic. Instead, as a side-effect,
30 * it saves the local address with which packets would
31 * be sent to the destination. */
32 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1) {
33 /* We don’t display the error here because most
34 * likely, there just is no IPv6 connectivity.
35 * Thus, don’t spam the user’s console but just
36 * try the next address. */
41 socklen_t local_len = sizeof(struct sockaddr_storage);
42 if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
43 perror("getsockname()");
48 memset(buf, 0, INET6_ADDRSTRLEN + 1);
49 if ((ret = getnameinfo((struct sockaddr*)&local, local_len,
50 buf, sizeof(buf), NULL, 0,
51 NI_NUMERICHOST)) != 0) {
52 fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));
62 * Returns the IPv6 address with which you have connectivity at the moment.
63 * The char * is statically allocated and mustn't be freed
65 static char *get_ipv6_addr() {
66 struct addrinfo hints;
67 struct addrinfo *result, *resp;
68 static struct addrinfo *cached = NULL;
70 /* To save dns lookups (if they are not cached locally) and creating
71 * sockets, we save the fd and keep it open. */
73 return get_sockname(cached);
75 memset(&hints, 0, sizeof(struct addrinfo));
76 hints.ai_family = AF_INET6;
77 hints.ai_socktype = SOCK_DGRAM;
79 /* We resolve the K root server to get a public IPv6 address. You can
80 * replace this with any other host which has an AAAA record, but the
81 * K root server is a pretty safe bet. */
82 if (getaddrinfo("k.root-servers.net", "domain", &hints, &result) != 0) {
83 /* We don’t display the error here because most
84 * likely, there just is no connectivity.
85 * Thus, don’t spam the user’s console. */
89 for (resp = result; resp != NULL; resp = resp->ai_next) {
90 char *addr_string = get_sockname(resp);
91 /* If we could not get our own address and there is more than
92 * one result for resolving k.root-servers.net, we cannot
93 * cache. Otherwise, no matter if we got IPv6 connectivity or
94 * not, we will cache the (single) result and are done. */
95 if (!addr_string && result->ai_next != NULL)
98 if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
100 memcpy(cached, resp, sizeof(struct addrinfo));
101 if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
105 memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
106 freeaddrinfo(result);
110 freeaddrinfo(result);
114 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
116 char *addr_string = get_ipv6_addr();
117 char *outwalk = buffer;
119 if (addr_string == NULL) {
120 OUTPUT_FULL_TEXT(format_down);
124 for (walk = format_up; *walk != '\0'; walk++) {
126 *(outwalk++) = *walk;
130 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
131 outwalk += sprintf(outwalk, "%s", addr_string);
132 walk += strlen("ip");
136 OUTPUT_FULL_TEXT(buffer);