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