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