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 use the public IPv6 of the K root server here. It doesn’t matter
82 * which IPv6 address we use (we don’t even send any packets), as long
83 * as it’s considered global by the kernel.
84 * NB: We don’t use a hostname since that would trigger a DNS lookup.
85 * By using an IPv6 address, getaddrinfo() will *not* do a DNS lookup,
86 * but return the address in the appropriate struct. */
87 if (getaddrinfo("2001:7fd::1", "domain", &hints, &result) != 0) {
88 /* We don’t display the error here because most
89 * likely, there just is no connectivity.
90 * Thus, don’t spam the user’s console. */
94 for (resp = result; resp != NULL; resp = resp->ai_next) {
95 char *addr_string = get_sockname(resp);
96 /* If we could not get our own address and there is more than
97 * one result for resolving k.root-servers.net, we cannot
98 * cache. Otherwise, no matter if we got IPv6 connectivity or
99 * not, we will cache the (single) result and are done. */
100 if (!addr_string && result->ai_next != NULL)
103 if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
105 memcpy(cached, resp, sizeof(struct addrinfo));
106 if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
110 memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
111 freeaddrinfo(result);
115 freeaddrinfo(result);
119 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
121 char *addr_string = get_ipv6_addr();
122 char *outwalk = buffer;
124 if (addr_string == NULL) {
125 START_COLOR("color_bad");
126 outwalk += sprintf(outwalk, "%s", format_down);
128 OUTPUT_FULL_TEXT(buffer);
132 START_COLOR("color_good");
133 for (walk = format_up; *walk != '\0'; walk++) {
135 *(outwalk++) = *walk;
139 if (BEGINS_WITH(walk+1, "ip")) {
140 outwalk += sprintf(outwalk, "%s", addr_string);
141 walk += strlen("ip");
145 OUTPUT_FULL_TEXT(buffer);