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