discover: Determine connectivity with getaddrinfo()
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Wed, 23 May 2018 04:39:17 +0000 (14:39 +1000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 10 Jul 2018 04:42:56 +0000 (14:42 +1000)
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
discover/sysinfo.c
discover/sysinfo.h

index c5b577823746f1c7cab1cc02f176baf494782ff5..dd54ba2bc178289e6ba240c04e24af485ee8c1cf 100644 (file)
@@ -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;
index 8497959320296b0b2f0e371a24c78d99099188cf..a9d7ff547b11ffb22a40bbdf4e738448de4f6a16 100644 (file)
@@ -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)
 {
index 835dfbe949811f7978e9b4ed9a63bd0cdabbf924..c570951db312e3c3385d5d66ce91ff7c9ff165c3 100644 (file)
@@ -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,