2009-07-21 18:26:53 +00:00
|
|
|
|
// vim:ts=8:expandtab
|
2009-07-21 17:07:30 +00:00
|
|
|
|
#include <stdio.h>
|
2010-01-04 10:16:59 +00:00
|
|
|
|
#include <stdbool.h>
|
2009-07-21 17:07:30 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <arpa/inet.h>
|
2012-03-25 18:55:55 +00:00
|
|
|
|
#include <yajl/yajl_gen.h>
|
2012-04-08 12:05:47 +00:00
|
|
|
|
#include <yajl/yajl_version.h>
|
2009-07-21 17:07:30 +00:00
|
|
|
|
|
2012-02-16 23:29:29 +00:00
|
|
|
|
#include "i3status.h"
|
|
|
|
|
|
2010-04-06 12:52:13 +00:00
|
|
|
|
static char *get_sockname(struct addrinfo *addr) {
|
2010-01-01 21:01:57 +00:00
|
|
|
|
static char buf[INET6_ADDRSTRLEN+1];
|
|
|
|
|
struct sockaddr_storage local;
|
|
|
|
|
int ret;
|
2010-01-04 10:16:59 +00:00
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
if ((fd = socket(addr->ai_family, SOCK_DGRAM, 0)) == -1) {
|
|
|
|
|
perror("socket()");
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2010-01-04 10:16:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Since the socket was created with SOCK_DGRAM, this is
|
|
|
|
|
* actually not establishing a connection or generating
|
|
|
|
|
* any other network traffic. Instead, as a side-effect,
|
|
|
|
|
* it saves the local address with which packets would
|
|
|
|
|
* be sent to the destination. */
|
|
|
|
|
if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1) {
|
|
|
|
|
/* We don’t display the error here because most
|
|
|
|
|
* likely, there just is no IPv6 connectivity.
|
|
|
|
|
* Thus, don’t spam the user’s console but just
|
|
|
|
|
* try the next address. */
|
|
|
|
|
(void)close(fd);
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2010-01-04 10:16:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-01 21:01:57 +00:00
|
|
|
|
socklen_t local_len = sizeof(struct sockaddr_storage);
|
|
|
|
|
if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
|
|
|
|
|
perror("getsockname()");
|
|
|
|
|
(void)close(fd);
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2010-01-01 21:01:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(buf, 0, INET6_ADDRSTRLEN + 1);
|
|
|
|
|
if ((ret = getnameinfo((struct sockaddr*)&local, local_len,
|
|
|
|
|
buf, sizeof(buf), NULL, 0,
|
|
|
|
|
NI_NUMERICHOST)) != 0) {
|
|
|
|
|
fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));
|
2010-01-04 10:16:59 +00:00
|
|
|
|
(void)close(fd);
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2010-01-01 21:01:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-04 10:16:59 +00:00
|
|
|
|
(void)close(fd);
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return buf;
|
2010-01-01 21:01:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-21 17:07:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* Returns the IPv6 address with which you have connectivity at the moment.
|
2010-04-05 13:47:56 +00:00
|
|
|
|
* The char * is statically allocated and mustn't be freed
|
2009-07-21 17:07:30 +00:00
|
|
|
|
*/
|
2010-04-05 13:47:56 +00:00
|
|
|
|
static char *get_ipv6_addr() {
|
2009-07-21 18:26:53 +00:00
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
struct addrinfo *result, *resp;
|
2010-01-04 10:16:59 +00:00
|
|
|
|
static struct addrinfo *cached = NULL;
|
2010-01-01 21:01:57 +00:00
|
|
|
|
|
|
|
|
|
/* To save dns lookups (if they are not cached locally) and creating
|
|
|
|
|
* sockets, we save the fd and keep it open. */
|
2010-01-04 10:16:59 +00:00
|
|
|
|
if (cached != NULL)
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return get_sockname(cached);
|
2009-07-21 18:26:53 +00:00
|
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
|
hints.ai_family = AF_INET6;
|
2011-01-03 14:00:42 +00:00
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2009-07-21 18:26:53 +00:00
|
|
|
|
|
2009-07-21 18:35:19 +00:00
|
|
|
|
/* We resolve the K root server to get a public IPv6 address. You can
|
|
|
|
|
* replace this with any other host which has an AAAA record, but the
|
|
|
|
|
* K root server is a pretty safe bet. */
|
2009-07-21 18:26:53 +00:00
|
|
|
|
if (getaddrinfo("k.root-servers.net", "domain", &hints, &result) != 0) {
|
2009-08-23 18:20:52 +00:00
|
|
|
|
/* We don’t display the error here because most
|
|
|
|
|
* likely, there just is no connectivity.
|
|
|
|
|
* Thus, don’t spam the user’s console. */
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2009-07-21 18:26:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (resp = result; resp != NULL; resp = resp->ai_next) {
|
2010-04-05 13:47:56 +00:00
|
|
|
|
char *addr_string = get_sockname(resp);
|
2011-01-03 14:00:42 +00:00
|
|
|
|
/* If we could not get our own address and there is more than
|
|
|
|
|
* one result for resolving k.root-servers.net, we cannot
|
|
|
|
|
* cache. Otherwise, no matter if we got IPv6 connectivity or
|
|
|
|
|
* not, we will cache the (single) result and are done. */
|
|
|
|
|
if (!addr_string && result->ai_next != NULL)
|
2009-07-21 18:26:53 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
2010-01-04 10:16:59 +00:00
|
|
|
|
if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2010-01-04 10:16:59 +00:00
|
|
|
|
memcpy(cached, resp, sizeof(struct addrinfo));
|
|
|
|
|
if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
|
|
|
|
|
cached = NULL;
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2009-07-21 18:26:53 +00:00
|
|
|
|
}
|
2010-01-04 10:16:59 +00:00
|
|
|
|
memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
|
|
|
|
|
freeaddrinfo(result);
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return addr_string;
|
2009-07-21 18:26:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-04 10:16:59 +00:00
|
|
|
|
freeaddrinfo(result);
|
2010-04-05 13:47:56 +00:00
|
|
|
|
return NULL;
|
2009-10-11 20:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-25 18:55:55 +00:00
|
|
|
|
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
|
2009-10-11 20:11:09 +00:00
|
|
|
|
const char *walk;
|
2010-04-06 12:52:13 +00:00
|
|
|
|
char *addr_string = get_ipv6_addr();
|
2012-03-25 18:55:55 +00:00
|
|
|
|
char *outwalk = buffer;
|
2012-02-16 23:29:29 +00:00
|
|
|
|
|
2010-04-05 13:47:56 +00:00
|
|
|
|
if (addr_string == NULL) {
|
2012-03-25 18:55:55 +00:00
|
|
|
|
OUTPUT_FULL_TEXT(format_down);
|
2010-04-06 12:52:13 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (walk = format_up; *walk != '\0'; walk++) {
|
|
|
|
|
if (*walk != '%') {
|
2012-03-25 18:55:55 +00:00
|
|
|
|
*(outwalk++) = *walk;
|
2010-04-06 12:52:13 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
|
2012-03-25 18:55:55 +00:00
|
|
|
|
outwalk += sprintf(outwalk, "%s", addr_string);
|
2010-04-06 12:52:13 +00:00
|
|
|
|
walk += strlen("ip");
|
2009-10-11 20:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-16 23:29:29 +00:00
|
|
|
|
|
2012-03-25 18:55:55 +00:00
|
|
|
|
OUTPUT_FULL_TEXT(buffer);
|
2009-07-21 17:07:30 +00:00
|
|
|
|
}
|