Skip to content

Instantly share code, notes, and snippets.

@KJTsanaktsidis
Created December 8, 2023 09:40
Show Gist options
  • Save KJTsanaktsidis/9f58e332d2bf3ccdbc18a3ff148b5bd4 to your computer and use it in GitHub Desktop.
Save KJTsanaktsidis/9f58e332d2bf3ccdbc18a3ff148b5bd4 to your computer and use it in GitHub Desktop.
Bug #20048 debugging
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
static void flags_to_str(int flags, char *buf, size_t len) {
size_t ix;
const char *flag_name;
if (flags == 0) {
snprintf(buf, len, "0");
return;
}
ix = 0;
while (flags) {
if (flags & AI_ADDRCONFIG) {
flag_name = "AI_ADDRCONFIG";
flags &= ~AI_ADDRCONFIG;
} else if (flags & AI_NUMERICHOST) {
flag_name = "AI_NUMERICHOST";
flags &= ~AI_NUMERICHOST;
} else if (flags & AI_NUMERICSERV) {
flag_name = "AI_NUMERICSERV";
flags &= ~AI_NUMERICSERV;
} else {
flag_name = NULL;
}
if (flag_name) {
ix += snprintf(buf + ix, len - ix, "%s", flag_name);
if (flags) {
ix += snprintf(buf + ix, len - ix, " | ");
}
} else {
snprintf(buf + ix, len - ix, "UNKNOWN_FLAGS (%d)", flags);
return;
}
}
}
static void family_to_str(int family, char *buf, size_t len) {
switch (family) {
case AF_INET:
snprintf(buf, len,"AF_INET");
break;
case AF_INET6:
snprintf(buf, len, "AF_INET6");
break;
default:
snprintf(buf, len, "Family %d", family);
break;
}
}
static void addr_to_str(struct sockaddr *sa, char *buf, size_t len) {
const char *r;
switch (sa->sa_family) {
case AF_INET:
r = inet_ntop(sa->sa_family, &(((struct sockaddr_in *)sa)->sin_addr), buf, len);
if (!r) {
snprintf(buf, len, "(inet_ntop error: %d (%s))", errno, strerror(errno));
}
break;
case AF_INET6:
r = inet_ntop(sa->sa_family, &(((struct sockaddr_in6 *)sa)->sin6_addr), buf, len);
if (!r) {
snprintf(buf, len, "(inet_ntop error: %d (%s))", errno, strerror(errno));
}
break;
default:
snprintf(buf, len, "(unknown family %d)", sa->sa_family);
break;
}
}
static void do_addrinfo_test(char *host, char *service, int family, int flags, bool use_hints) {
int r;
char addr[INET6_ADDRSTRLEN];
char family_str[64];
char flags_str[128];
struct addrinfo *res = NULL;
struct addrinfo hints = {
.ai_socktype = 0,
.ai_protocol = 0,
.ai_family = family,
.ai_flags = flags,
};
struct addrinfo *hints_to_pass;
flags_to_str(flags, flags_str, sizeof(flags_str));
family_to_str(family, family_str, sizeof(family_str));
printf("TEST: host %s, service %s, flags %s, family %s (using hints: %d)\n", host, service, flags_str, family_str, use_hints);
if (use_hints) {
hints_to_pass = &hints;
} else {
hints_to_pass = NULL;
}
r = getaddrinfo(host, service, hints_to_pass, &res);
if (r == 0) {
struct addrinfo *node = res;
while (node) {
family_to_str(node->ai_family, family_str, sizeof(family_str));
addr_to_str(node->ai_addr, addr, sizeof(addr));
printf("\tRESOLVED: %s %s\n", addr, family_str);
node = node->ai_next;
}
} else {
printf("\tFAILED: err %d (%s)\n", r, gai_strerror(r));
}
if (res) {
freeaddrinfo(res);
}
}
int main(int argc, char **argv) {
do_addrinfo_test("localhost", "8000", AF_UNSPEC, AI_NUMERICSERV | AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_UNSPEC, AI_NUMERICSERV | AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_UNSPEC, AI_NUMERICSERV, true);
do_addrinfo_test(NULL, "8000", AF_UNSPEC,AI_NUMERICSERV, true);
do_addrinfo_test("localhost", NULL, AF_UNSPEC, 0, true);
do_addrinfo_test("localhost", NULL, AF_UNSPEC, 0, false);
do_addrinfo_test(NULL, "8000", AF_UNSPEC, 0, true);
do_addrinfo_test(NULL, "8000", AF_UNSPEC, 0, false);
do_addrinfo_test("localhost", "8000", AF_INET, AI_NUMERICSERV | AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_INET, AI_NUMERICSERV | AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_INET, AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_INET, AI_NUMERICSERV, true);
do_addrinfo_test("localhost", "8000", AF_INET6, AI_NUMERICSERV | AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_INET6, AI_NUMERICSERV | AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_INET6, AI_ADDRCONFIG, true);
do_addrinfo_test(NULL, "8000", AF_INET6, AI_NUMERICSERV, true);
}
TEST: host localhost, service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service (null), flags 0, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service (null), flags 0, family Family 0 (using hints: 0)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags 0, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags 0, family Family 0 (using hints: 0)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET (using hints: 1)
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET (using hints: 1)
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_ADDRCONFIG, family AF_INET (using hints: 1)
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_NUMERICSERV, family AF_INET (using hints: 1)
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET6 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
TEST: host (null), service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET6 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
TEST: host (null), service 8000, flags AI_ADDRCONFIG, family AF_INET6 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
TEST: host (null), service 8000, flags AI_NUMERICSERV, family AF_INET6 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
TEST: host localhost, service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags AI_NUMERICSERV, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service (null), flags 0, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service (null), flags 0, family Family 0 (using hints: 0)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags 0, family Family 0 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host (null), service 8000, flags 0, family Family 0 (using hints: 0)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET (using hints: 1)
FAILED: err -2 (Name or service not known)
TEST: host (null), service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET (using hints: 1)
FAILED: err -2 (Name or service not known)
TEST: host (null), service 8000, flags AI_ADDRCONFIG, family AF_INET (using hints: 1)
FAILED: err -2 (Name or service not known)
TEST: host (null), service 8000, flags AI_NUMERICSERV, family AF_INET (using hints: 1)
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
RESOLVED: 127.0.0.1 AF_INET
TEST: host localhost, service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET6 (using hints: 1)
FAILED: err -2 (Name or service not known)
TEST: host (null), service 8000, flags AI_ADDRCONFIG | AI_NUMERICSERV, family AF_INET6 (using hints: 1)
FAILED: err -2 (Name or service not known)
TEST: host (null), service 8000, flags AI_ADDRCONFIG, family AF_INET6 (using hints: 1)
FAILED: err -2 (Name or service not known)
TEST: host (null), service 8000, flags AI_NUMERICSERV, family AF_INET6 (using hints: 1)
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
RESOLVED: ::1 AF_INET6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment