[v2,8/9] discover: Determine connectivity with getaddrinfo()

Message ID 20180524044742.25889-9-sam@mendozajonas.com
State Accepted
Headers show
Series
  • IPv6 Support
Related show

Commit Message

Samuel Mendoza-Jonas May 24, 2018, 4:47 a.m.
Use getaddrinfo() to determine if a remote URL is reachable instead of
only checking if we have an addresses configured. This avoids, for
example, trying to load an IPv4 URL when only an IPv6 address is
available.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
---
 discover/paths.c   |  8 ++++++--
 discover/sysinfo.c | 10 ----------
 discover/sysinfo.h |  1 -
 3 files changed, 6 insertions(+), 13 deletions(-)

Patch

diff --git a/discover/paths.c b/discover/paths.c
index c5b5778..dd54ba2 100644
--- a/discover/paths.c
+++ b/discover/paths.c
@@ -3,11 +3,13 @@ 
 #endif
 
 #include <assert.h>
+#include <netdb.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 #include <talloc/talloc.h>
 #include <system/system.h>
@@ -547,6 +549,7 @@  struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
 {
 	struct load_url_result *result;
 	struct load_task *task;
+	struct addrinfo *res;
 	int flags = 0;
 
 	if (!url)
@@ -579,7 +582,8 @@  struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
 
 	/* If the url is remote but network is not yet available queue up this
 	 * load for later */
-	if (!system_info_network_available() && url->scheme != pb_url_file) {
+	if (url->scheme != pb_url_file &&
+			getaddrinfo(url->host, NULL, NULL, &res) != 0) {
 		pb_log("load task for %s queued pending network\n", url->full);
 		pending_network_jobs_add(task, flags);
 		task->result->status = LOAD_ASYNC;
diff --git a/discover/sysinfo.c b/discover/sysinfo.c
index b7286f1..7037437 100644
--- a/discover/sysinfo.c
+++ b/discover/sysinfo.c
@@ -18,16 +18,6 @@  const struct system_info *system_info_get(void)
 	return sysinfo;
 }
 
-bool system_info_network_available(void)
-{
-	unsigned int i;
-
-	for (i = 0; i < sysinfo->n_interfaces; i++)
-		if (sysinfo->interfaces[i]->address)
-			return true;
-	return false;
-}
-
 void system_info_set_interface_address(unsigned int hwaddr_size,
 		uint8_t *hwaddr, const char *address)
 {
diff --git a/discover/sysinfo.h b/discover/sysinfo.h
index 835dfbe..c570951 100644
--- a/discover/sysinfo.h
+++ b/discover/sysinfo.h
@@ -7,7 +7,6 @@  struct discover_server;
 
 const struct system_info *system_info_get(void);
 
-bool system_info_network_available(void);
 void system_info_set_interface_address(unsigned int hwaddr_size,
 		uint8_t *hwaddr, const char *address);
 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,