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