]> git.sur5r.net Git - i3/i3status/blob - src/print_ipv6_addr.c
3f1c81d60ef49d0178bf03e969bebe834228d97e
[i3/i3status] / src / print_ipv6_addr.c
1 // vim:ts=8:expandtab
2 #include <stdio.h>
3 #include <stdbool.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netdb.h>
9 #include <string.h>
10 #include <arpa/inet.h>
11 #include <yajl/yajl_gen.h>
12
13 #include "i3status.h"
14
15 static char *get_sockname(struct addrinfo *addr) {
16         static char buf[INET6_ADDRSTRLEN+1];
17         struct sockaddr_storage local;
18         int ret;
19         int fd;
20
21         if ((fd = socket(addr->ai_family, SOCK_DGRAM, 0)) == -1) {
22                 perror("socket()");
23                 return NULL;
24         }
25
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. */
36                 (void)close(fd);
37                 return NULL;
38         }
39
40         socklen_t local_len = sizeof(struct sockaddr_storage);
41         if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
42                 perror("getsockname()");
43                 (void)close(fd);
44                 return NULL;
45         }
46
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));
52                 (void)close(fd);
53                 return NULL;
54         }
55
56         (void)close(fd);
57         return buf;
58 }
59
60 /*
61  * Returns the IPv6 address with which you have connectivity at the moment.
62  * The char * is statically allocated and mustn't be freed
63  */
64 static char *get_ipv6_addr() {
65         struct addrinfo hints;
66         struct addrinfo *result, *resp;
67         static struct addrinfo *cached = NULL;
68
69         /* To save dns lookups (if they are not cached locally) and creating
70          * sockets, we save the fd and keep it open. */
71         if (cached != NULL)
72                 return get_sockname(cached);
73
74         memset(&hints, 0, sizeof(struct addrinfo));
75         hints.ai_family = AF_INET6;
76         hints.ai_socktype = SOCK_DGRAM;
77
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. */
85                 return NULL;
86         }
87
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)
95                         continue;
96
97                 if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
98                         return NULL;
99                 memcpy(cached, resp, sizeof(struct addrinfo));
100                 if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
101                         cached = NULL;
102                         return NULL;
103                 }
104                 memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
105                 freeaddrinfo(result);
106                 return addr_string;
107         }
108
109         freeaddrinfo(result);
110         return NULL;
111 }
112
113 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
114         const char *walk;
115         char *addr_string = get_ipv6_addr();
116         char *outwalk = buffer;
117
118         if (addr_string == NULL) {
119                 OUTPUT_FULL_TEXT(format_down);
120                 return;
121         }
122
123         for (walk = format_up; *walk != '\0'; walk++) {
124                 if (*walk != '%') {
125                         *(outwalk++) = *walk;
126                         continue;
127                 }
128
129                 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
130                         outwalk += sprintf(outwalk, "%s", addr_string);
131                         walk += strlen("ip");
132                 }
133         }
134
135         OUTPUT_FULL_TEXT(buffer);
136 }