]> git.sur5r.net Git - i3/i3status/blob - src/print_ipv6_addr.c
Implement the i3bar JSON protocol
[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
12 #include "i3status.h"
13
14 static char *get_sockname(struct addrinfo *addr) {
15         static char buf[INET6_ADDRSTRLEN+1];
16         struct sockaddr_storage local;
17         int ret;
18         int fd;
19
20         if ((fd = socket(addr->ai_family, SOCK_DGRAM, 0)) == -1) {
21                 perror("socket()");
22                 return NULL;
23         }
24
25         /* Since the socket was created with SOCK_DGRAM, this is
26          * actually not establishing a connection or generating
27          * any other network traffic. Instead, as a side-effect,
28          * it saves the local address with which packets would
29          * be sent to the destination. */
30         if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1) {
31                 /* We don’t display the error here because most
32                  * likely, there just is no IPv6 connectivity.
33                  * Thus, don’t spam the user’s console but just
34                  * try the next address. */
35                 (void)close(fd);
36                 return NULL;
37         }
38
39         socklen_t local_len = sizeof(struct sockaddr_storage);
40         if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
41                 perror("getsockname()");
42                 (void)close(fd);
43                 return NULL;
44         }
45
46         memset(buf, 0, INET6_ADDRSTRLEN + 1);
47         if ((ret = getnameinfo((struct sockaddr*)&local, local_len,
48                                buf, sizeof(buf), NULL, 0,
49                                NI_NUMERICHOST)) != 0) {
50                 fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));
51                 (void)close(fd);
52                 return NULL;
53         }
54
55         (void)close(fd);
56         return buf;
57 }
58
59 /*
60  * Returns the IPv6 address with which you have connectivity at the moment.
61  * The char * is statically allocated and mustn't be freed
62  */
63 static char *get_ipv6_addr() {
64         struct addrinfo hints;
65         struct addrinfo *result, *resp;
66         static struct addrinfo *cached = NULL;
67
68         /* To save dns lookups (if they are not cached locally) and creating
69          * sockets, we save the fd and keep it open. */
70         if (cached != NULL)
71                 return get_sockname(cached);
72
73         memset(&hints, 0, sizeof(struct addrinfo));
74         hints.ai_family = AF_INET6;
75         hints.ai_socktype = SOCK_DGRAM;
76
77         /* We resolve the K root server to get a public IPv6 address. You can
78          * replace this with any other host which has an AAAA record, but the
79          * K root server is a pretty safe bet. */
80         if (getaddrinfo("k.root-servers.net", "domain", &hints, &result) != 0) {
81                 /* We don’t display the error here because most
82                  * likely, there just is no connectivity.
83                  * Thus, don’t spam the user’s console. */
84                 return NULL;
85         }
86
87         for (resp = result; resp != NULL; resp = resp->ai_next) {
88                 char *addr_string = get_sockname(resp);
89                 /* If we could not get our own address and there is more than
90                  * one result for resolving k.root-servers.net, we cannot
91                  * cache. Otherwise, no matter if we got IPv6 connectivity or
92                  * not, we will cache the (single) result and are done. */
93                 if (!addr_string && result->ai_next != NULL)
94                         continue;
95
96                 if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
97                         return NULL;
98                 memcpy(cached, resp, sizeof(struct addrinfo));
99                 if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
100                         cached = NULL;
101                         return NULL;
102                 }
103                 memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
104                 freeaddrinfo(result);
105                 return addr_string;
106         }
107
108         freeaddrinfo(result);
109         return NULL;
110 }
111
112 void print_ipv6_info(const char *format_up, const char *format_down) {
113         const char *walk;
114         char *addr_string = get_ipv6_addr();
115
116         if (output_format == O_I3BAR)
117                 printf("{\"name\":\"ipv6\", \"full_text\":\"");
118
119         if (addr_string == NULL) {
120                 printf("%s", format_down);
121                 return;
122         }
123
124         for (walk = format_up; *walk != '\0'; walk++) {
125                 if (*walk != '%') {
126                         putchar(*walk);
127                         continue;
128                 }
129
130                 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
131                         printf("%s", addr_string);
132                         walk += strlen("ip");
133                 }
134         }
135
136         if (output_format == O_I3BAR)
137                 printf("\"}");
138 }