6 #include <sys/socket.h>
11 static void print_sockname(int fd) {
12 static char buf[INET6_ADDRSTRLEN+1];
13 struct sockaddr_storage local;
16 socklen_t local_len = sizeof(struct sockaddr_storage);
17 if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
18 perror("getsockname()");
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));
37 * Returns the IPv6 address with which you have connectivity at the moment.
40 static void print_ipv6_addr() {
41 struct addrinfo hints;
42 struct addrinfo *result, *resp;
45 /* To save dns lookups (if they are not cached locally) and creating
46 * sockets, we save the fd and keep it open. */
52 memset(&hints, 0, sizeof(struct addrinfo));
53 hints.ai_family = AF_INET6;
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. */
66 for (resp = result; resp != NULL; resp = resp->ai_next) {
67 if ((fd = socket(resp->ai_family, SOCK_DGRAM, 0)) == -1) {
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. */
97 void print_ipv6_info(const char *format) {
100 for (walk = format; *walk != '\0'; walk++) {
106 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
108 walk += strlen("ip");