From c31f626c3a252a0d6805a5f3ca9154d934ea41f5 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 28 Jan 2021 22:26:00 +0100 Subject: [PATCH] Debugging for getifaddrs(). This logs which interfaces and addresses are returned by getifaddrs() that are usable for MeshLink. It also adds the /scan command to the chat example to force rescanning the network interfaces. --- examples/chat.c | 2 ++ src/discovery.c | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/chat.c b/examples/chat.c index 88fe127a..2dc13be1 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -86,6 +86,8 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno)); exit(1); } + } else if(!strcasecmp(buf, "scan")) { + meshlink_hint_network_change(mesh); } else if(!strcasecmp(buf, "kick")) { if(!arg) { fprintf(stderr, "/kick requires an argument!\n"); diff --git a/src/discovery.c b/src/discovery.c index 18919745..8fc6895e 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -434,6 +434,7 @@ static void addr_del(meshlink_handle_t *mesh, const discovery_address_t *addr) { void scan_ifaddrs(meshlink_handle_t *mesh) { #ifdef HAVE_GETIFADDRS + logger(mesh, MESHLINK_WARNING, "Calling getifaddrs()"); struct ifaddrs *ifa = NULL; if(getifaddrs(&ifa) == -1) { @@ -525,6 +526,13 @@ void scan_ifaddrs(meshlink_handle_t *mesh) { addr.address.sa.sa_family = AF_UNKNOWN; } + if(addr.address.sa.sa_family != AF_UNKNOWN) { + char *host = NULL; + sockaddr2str(sa, &host, NULL); + logger(mesh, MESHLINK_WARNING, "iface %d (%s) address %s", index, ifap->ifa_name, host); + free(host); + } + if(addr.address.sa.sa_family != AF_UNKNOWN) { addr_add(mesh, &addr); } @@ -532,7 +540,7 @@ void scan_ifaddrs(meshlink_handle_t *mesh) { freeifaddrs(ifa); #else - (void)mesh; + logger(mesh, MESHLINK_ERROR, "getifaddrs() not supported"); #endif } -- 2.39.5