[gssdp] client: Add property for preferred address family



commit 755dcc330e159f773dc107facdffc91c33c864c1
Author: Jens Georg <mail jensge org>
Date:   Sat Dec 1 20:46:47 2018 +0100

    client: Add property for preferred address family

 libgssdp/gssdp-client.c    | 36 ++++++++++++++++++++++++++++++++++++
 libgssdp/gssdp-net-posix.c | 30 ++++++++++++++++++------------
 libgssdp/gssdp-net.h       |  1 +
 3 files changed, 55 insertions(+), 12 deletions(-)
---
diff --git a/libgssdp/gssdp-client.c b/libgssdp/gssdp-client.c
index a06c974..7ec9284 100644
--- a/libgssdp/gssdp-client.c
+++ b/libgssdp/gssdp-client.c
@@ -125,6 +125,7 @@ enum {
         PROP_ACTIVE,
         PROP_SOCKET_TTL,
         PROP_MSEARCH_PORT,
+        PROP_ADDRESS_FAMILY,
 };
 
 enum {
@@ -313,6 +314,9 @@ gssdp_client_get_property (GObject    *object,
         case PROP_MSEARCH_PORT:
                 g_value_set_uint (value, priv->msearch_port);
                 break;
+        case PROP_ADDRESS_FAMILY:
+                g_value_set_enum (value, priv->device.address_family);
+                break;
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                 break;
@@ -360,6 +364,9 @@ gssdp_client_set_property (GObject      *object,
         case PROP_MSEARCH_PORT:
                 priv->msearch_port = g_value_get_uint (value);
                 break;
+        case PROP_ADDRESS_FAMILY:
+                priv->device.address_family = g_value_get_enum (value);
+                break;
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                 break;
@@ -570,6 +577,32 @@ gssdp_client_class_init (GSSDPClientClass *klass)
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS));
 
+        /**
+         * GSSDPClient:address-family:
+         *
+         * The IP protocol address family this client works on. When specified
+         * during construction without giving a concrete address, it will be
+         * used to determine the proper address.
+         *
+         * If not specified, will contain the currrent address family after
+         * the call to g_initable_init()<!-- -->. Use #G_SOCKET_FAMILY_INVALID
+         * to specifiy using the default socket family (legacy IP)
+         *
+         * Since: 1.1.1
+         */
+        g_object_class_install_property
+                (object_class,
+                 PROP_ADDRESS_FAMILY,
+                 g_param_spec_enum
+                        ("address-family",
+                         "IP Address family",
+                         "IP address family to prefer when creating the client",
+                         G_TYPE_SOCKET_FAMILY,
+                         G_SOCKET_FAMILY_INVALID,
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT_ONLY |
+                         G_PARAM_STATIC_STRINGS));
+
         /**
          * GSSDPClient::message-received: (skip)
          * @client: The #GSSDPClient that received the message.
@@ -1548,6 +1581,9 @@ init_network_info (GSSDPClient *client, GError **error)
                  */
                 priv->device.index =
                         gssdp_net_query_ifindex (&priv->device);
+
+                priv->device.address_family =  g_inet_address_get_family
+                                        (priv->device.host_addr);
         }
 
         if (priv->device.iface_name == NULL) {
diff --git a/libgssdp/gssdp-net-posix.c b/libgssdp/gssdp-net-posix.c
index c793fc4..e5a1dc2 100644
--- a/libgssdp/gssdp-net-posix.c
+++ b/libgssdp/gssdp-net-posix.c
@@ -457,20 +457,24 @@ gssdp_net_get_host_ip (GSSDPNetworkDevice *device)
          */
         family = G_SOCKET_FAMILY_INVALID;
 
+        /* If we have an address, its family will take precendence.
+         * Otherwise take the family from the client's config
+         */
         if (device->host_addr) {
                 family = g_inet_address_get_family (device->host_addr);
-        }
-
-        if (family == G_SOCKET_FAMILY_IPV6 &&
-            !g_inet_address_get_is_link_local (device->host_addr) &&
-            !g_inet_address_get_is_site_local (device->host_addr) &&
-            !g_inet_address_get_is_loopback (device->host_addr)) {
-                char *addr = g_inet_address_to_string (device->host_addr);
-                /* FIXME: Discard the address, but use the interface */
-                g_warning("Invalid IP address given: %s, discarding",
-                          addr);
-                g_free (addr);
-                g_clear_object (&device->host_addr);
+                if (family == G_SOCKET_FAMILY_IPV6 &&
+                    !g_inet_address_get_is_link_local (device->host_addr) &&
+                    !g_inet_address_get_is_site_local (device->host_addr) &&
+                    !g_inet_address_get_is_loopback (device->host_addr)) {
+                        char *addr = g_inet_address_to_string (device->host_addr);
+                        /* FIXME: Discard the address, but use the interface */
+                        g_warning("Invalid IP address given: %s, discarding",
+                                        addr);
+                        g_free (addr);
+                        g_clear_object (&device->host_addr);
+                }
+        } else {
+                family = device->address_family;
         }
 
         for (ifaceptr = up_ifaces;
@@ -540,6 +544,8 @@ gssdp_net_get_host_ip (GSSDPNetworkDevice *device)
                 break;
         }
 
+        device->address_family = g_inet_address_get_family (device->host_addr);
+
         g_list_free (up_ifaces);
         freeifaddrs (ifa_list);
 
diff --git a/libgssdp/gssdp-net.h b/libgssdp/gssdp-net.h
index b458fee..59e87c0 100644
--- a/libgssdp/gssdp-net.h
+++ b/libgssdp/gssdp-net.h
@@ -41,6 +41,7 @@ struct _GSSDPNetworkDevice {
         char *host_ip;
         GInetAddress *host_addr;
         GInetAddressMask *host_mask;
+        GSocketFamily address_family;
         char *network;
         gint index;
 };


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