[gssdp/wip/property-cleanup: 3/3] Client: Make get_host_ip fill an error




commit f6fd0d2ff1607fd9920f292d5a5f8f672152931d
Author: Jens Georg <mail jensge org>
Date:   Fri Aug 5 09:27:48 2022 +0200

    Client: Make get_host_ip fill an error
    
    Properly exit on error and pass on the reason instead of printing a
    warning on console and just carry on

 libgssdp/gssdp-client.c     | 19 +++++++++----------
 libgssdp/gssdp-net-bionic.c |  2 +-
 libgssdp/gssdp-net-posix.c  | 21 ++++++++++++++++++---
 libgssdp/gssdp-net-win32.c  |  2 +-
 libgssdp/gssdp-net.h        |  2 +-
 tests/test-regression.c     |  2 +-
 6 files changed, 31 insertions(+), 17 deletions(-)
---
diff --git a/libgssdp/gssdp-client.c b/libgssdp/gssdp-client.c
index e9bd4e9..02f81c8 100644
--- a/libgssdp/gssdp-client.c
+++ b/libgssdp/gssdp-client.c
@@ -1885,7 +1885,6 @@ static gboolean
 init_network_info (GSSDPClient *client, GError **error)
 {
         GSSDPClientPrivate *priv = gssdp_client_get_instance_private (client);
-        gboolean ret = TRUE;
 
         /* If we were constructed with a host_ip, try to parse a host_addr from that.
          * Further code will only work with host_addr */
@@ -1928,11 +1927,11 @@ init_network_info (GSSDPClient *client, GError **error)
          * If one is given, try to find the other, otherwise just pick an
          * interface.
          */
-        if (priv->device.iface_name == NULL ||
-            priv->device.host_addr == NULL  ||
-            priv->device.host_mask == NULL)
-                gssdp_net_get_host_ip (&(priv->device));
-        else {
+        if (priv->device.iface_name == NULL || priv->device.host_addr == NULL ||
+            priv->device.host_mask == NULL) {
+                if (!gssdp_net_get_host_ip (&(priv->device), error))
+                        return FALSE;
+        } else {
                 /* Ugly. Ideally, get_host_ip needs to be run everytime, but
                  * it is currently to stupid so just query index here if we
                  * have a name and an interface already.
@@ -1953,7 +1952,7 @@ init_network_info (GSSDPClient *client, GError **error)
                                      GSSDP_ERROR_FAILED,
                                      "No default route?");
 
-                ret = FALSE;
+                return FALSE;
         } else if (priv->device.host_addr == NULL) {
                 g_set_error (error,
                              GSSDP_ERROR,
@@ -1961,13 +1960,13 @@ init_network_info (GSSDPClient *client, GError **error)
                              "Failed to find IP of interface %s",
                              priv->device.iface_name);
 
-                ret = FALSE;
+                return FALSE;
         } else if (priv->device.host_mask == NULL) {
                 g_set_error (error,
                              GSSDP_ERROR,
                              GSSDP_ERROR_FAILED,
                              "No network mask?");
-                ret = FALSE;
+                return FALSE;
         }
 
         g_debug ("Created SSDP client %p", client);
@@ -1979,5 +1978,5 @@ init_network_info (GSSDPClient *client, GError **error)
         g_debug ("  host_addr  : %p", priv->device.host_addr);
         g_debug ("  host_mask  : %p", priv->device.host_mask);
 
-        return ret;
+        return TRUE;
 }
diff --git a/libgssdp/gssdp-net-bionic.c b/libgssdp/gssdp-net-bionic.c
index 0e00b42..831b49b 100644
--- a/libgssdp/gssdp-net-bionic.c
+++ b/libgssdp/gssdp-net-bionic.c
@@ -112,7 +112,7 @@ gssdp_net_mac_lookup (GSSDPNetworkDevice *device, const char *ip_address)
 }
 
 gboolean
-gssdp_net_get_host_ip (GSSDPNetworkDevice *device)
+gssdp_net_get_host_ip (GSSDPNetworkDevice *device, GError **error)
 {
         struct      ifreq *ifaces = NULL;
         struct      ifreq *iface = NULL;
diff --git a/libgssdp/gssdp-net-posix.c b/libgssdp/gssdp-net-posix.c
index b3266a9..cd46014 100644
--- a/libgssdp/gssdp-net-posix.c
+++ b/libgssdp/gssdp-net-posix.c
@@ -12,6 +12,7 @@
 #include <config.h>
 
 #include "gssdp-net.h"
+#include "gssdp-error.h"
 
 #include <arpa/inet.h>
 #include <sys/socket.h>
@@ -389,7 +390,7 @@ get_netmask (struct sockaddr *address,
 
 
 gboolean
-gssdp_net_get_host_ip (GSSDPNetworkDevice *device)
+gssdp_net_get_host_ip (GSSDPNetworkDevice *device, GError **error)
 {
         struct ifaddrs *ifa_list, *ifa;
         GList *up_ifaces, *ifaceptr;
@@ -398,9 +399,15 @@ gssdp_net_get_host_ip (GSSDPNetworkDevice *device)
 
         up_ifaces = NULL;
 
+        errno = 0;
         if (getifaddrs (&ifa_list) != 0) {
-                g_warning ("Failed to retrieve list of network interfaces: %s",
-                           strerror (errno));
+                int saved_errno = errno;
+                g_set_error (
+                        error,
+                        G_IO_ERROR,
+                        g_io_error_from_errno (saved_errno),
+                        "Failed to retrieve list of network interfaces: %s",
+                        g_strerror (errno));
 
                 return FALSE;
         }
@@ -511,6 +518,14 @@ gssdp_net_get_host_ip (GSSDPNetworkDevice *device)
 
                 if (device->iface_name == NULL)
                         device->iface_name = g_strdup (ifa->ifa_name);
+                else {
+                        // We have found the address, and we have an iface. Does it match?
+                        if (!g_str_equal (device->iface_name, ifa->ifa_name)) {
+                                g_set_error(error, GSSDP_ERROR, GSSDP_ERROR_FAILED, "Information mismatch: 
Interface passed address is %s, but requested %s",
+                                             device->iface_name, ifa->ifa_name);
+                                return FALSE;
+                        }
+                }
 
                 if (device->network == NULL)
                         device->network = g_inet_address_mask_to_string (device->host_mask);
diff --git a/libgssdp/gssdp-net-win32.c b/libgssdp/gssdp-net-win32.c
index 2eceffa..0f5e313 100644
--- a/libgssdp/gssdp-net-win32.c
+++ b/libgssdp/gssdp-net-win32.c
@@ -185,7 +185,7 @@ gssdp_net_mac_lookup (GSSDPNetworkDevice *device, const char *ip_address)
 }
 
 gboolean
-gssdp_net_get_host_ip (GSSDPNetworkDevice *device)
+gssdp_net_get_host_ip (GSSDPNetworkDevice *device, GError **error)
 {
         GList *up_ifaces = NULL, *ifaceptr = NULL;
         ULONG flags = GAA_FLAG_INCLUDE_PREFIX |
diff --git a/libgssdp/gssdp-net.h b/libgssdp/gssdp-net.h
index 3d24e63..2673c24 100644
--- a/libgssdp/gssdp-net.h
+++ b/libgssdp/gssdp-net.h
@@ -42,7 +42,7 @@ G_GNUC_INTERNAL void
 gssdp_net_shutdown              (void);
 
 G_GNUC_INTERNAL gboolean
-gssdp_net_get_host_ip           (GSSDPNetworkDevice *device);
+gssdp_net_get_host_ip           (GSSDPNetworkDevice *device, GError **error);
 
 G_GNUC_INTERNAL int
 gssdp_net_query_ifindex         (GSSDPNetworkDevice *device);
diff --git a/tests/test-regression.c b/tests/test-regression.c
index 5e914d5..1d9925d 100644
--- a/tests/test-regression.c
+++ b/tests/test-regression.c
@@ -449,7 +449,7 @@ void test_ggo_7 () {
                                  "host-ip", "127.0.0.1",
                                  "interface", "ThisShouldNotExist",
                                  NULL);
-        g_assert_error (error, GSSDP_ERROR, GSSDP_ERROR_NO_IP_ADDRESS);
+        g_assert_error (error, GSSDP_ERROR, GSSDP_ERROR_FAILED);
         g_assert_null (client);
         g_clear_error (&error);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]