3 #include <sys/socket.h>
4 #include <netinet/in.h>
13 #include <yajl/yajl_gen.h>
14 #include <yajl/yajl_version.h>
18 static char *get_sockname(struct addrinfo *addr) {
19 static char buf[INET6_ADDRSTRLEN+1];
20 struct sockaddr_storage local;
24 if ((fd = socket(addr->ai_family, SOCK_DGRAM, 0)) == -1) {
29 /* Since the socket was created with SOCK_DGRAM, this is
30 * actually not establishing a connection or generating
31 * any other network traffic. Instead, as a side-effect,
32 * it saves the local address with which packets would
33 * be sent to the destination. */
34 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1) {
35 /* We don’t display the error here because most
36 * likely, there just is no IPv6 connectivity.
37 * Thus, don’t spam the user’s console but just
38 * try the next address. */
43 socklen_t local_len = sizeof(struct sockaddr_storage);
44 if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
45 perror("getsockname()");
50 memset(buf, 0, INET6_ADDRSTRLEN + 1);
51 if ((ret = getnameinfo((struct sockaddr*)&local, local_len,
52 buf, sizeof(buf), NULL, 0,
53 NI_NUMERICHOST)) != 0) {
54 fprintf(stderr, "i3status: getnameinfo(): %s\n", gai_strerror(ret));
64 * Returns the IPv6 address with which you have connectivity at the moment.
65 * The char * is statically allocated and mustn't be freed
67 static char *get_ipv6_addr(void) {
68 struct addrinfo hints;
69 struct addrinfo *result, *resp;
70 static struct addrinfo *cached = NULL;
72 /* To save dns lookups (if they are not cached locally) and creating
73 * sockets, we save the fd and keep it open. */
75 return get_sockname(cached);
77 memset(&hints, 0, sizeof(struct addrinfo));
78 hints.ai_family = AF_INET6;
79 hints.ai_socktype = SOCK_DGRAM;
81 /* We resolve the K root server to get a public IPv6 address. You can
82 * replace this with any other host which has an AAAA record, but the
83 * K root server is a pretty safe bet. */
84 if (getaddrinfo("k.root-servers.net", "domain", &hints, &result) != 0) {
85 /* We don’t display the error here because most
86 * likely, there just is no connectivity.
87 * Thus, don’t spam the user’s console. */
91 for (resp = result; resp != NULL; resp = resp->ai_next) {
92 char *addr_string = get_sockname(resp);
93 /* If we could not get our own address and there is more than
94 * one result for resolving k.root-servers.net, we cannot
95 * cache. Otherwise, no matter if we got IPv6 connectivity or
96 * not, we will cache the (single) result and are done. */
97 if (!addr_string && result->ai_next != NULL)
100 if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
102 memcpy(cached, resp, sizeof(struct addrinfo));
103 if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
107 memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
108 freeaddrinfo(result);
112 freeaddrinfo(result);
116 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
118 char *addr_string = get_ipv6_addr();
119 char *outwalk = buffer;
121 if (addr_string == NULL) {
122 START_COLOR("color_bad");
123 OUTPUT_FULL_TEXT(format_down);
128 for (walk = format_up; *walk != '\0'; walk++) {
130 *(outwalk++) = *walk;
134 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
135 outwalk += sprintf(outwalk, "%s", addr_string);
136 walk += strlen("ip");
139 START_COLOR("color_good");
140 OUTPUT_FULL_TEXT(buffer);