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