[gssdp/wip/ipv6: 1/7] Consistently use GInetAddress in socket source



commit eb53e13f478bff55c76eb4bdd90ee0c7ce498831
Author: Jens Georg <mail jensge org>
Date:   Wed Feb 17 20:24:05 2016 +0100

    Consistently use GInetAddress in socket source
    
    Don't pass around the ip address as string
    
    Signed-off-by: Jens Georg <mail jensge org>

 libgssdp/gssdp-client.c        |   14 ++++++++--
 libgssdp/gssdp-socket-source.c |   50 +++++++++++++--------------------------
 libgssdp/gssdp-socket-source.h |    2 +-
 3 files changed, 29 insertions(+), 37 deletions(-)
---
diff --git a/libgssdp/gssdp-client.c b/libgssdp/gssdp-client.c
index ad46780..96d401f 100644
--- a/libgssdp/gssdp-client.c
+++ b/libgssdp/gssdp-client.c
@@ -35,6 +35,8 @@
 #include <config.h>
 #include <sys/types.h>
 #include <glib.h>
+#include <gio/gio.h>
+
 #ifndef G_OS_WIN32
 #include <sys/socket.h>
 #include <sys/utsname.h>
@@ -209,6 +211,7 @@ gssdp_client_initable_init (GInitable                   *initable,
                             GError                     **error)
 {
         GSSDPClient *client = GSSDP_CLIENT (initable);
+        GInetAddress *address = NULL;
         GError *internal_error = NULL;
 
         if (client->priv->initialized)
@@ -234,10 +237,12 @@ gssdp_client_initable_init (GInitable                   *initable,
         if (!init_network_info (client, &internal_error))
                 goto errors;
 
+        address = g_inet_address_new_from_string (client->priv->device.host_ip);
+
         /* Set up sockets (Will set errno if it failed) */
         client->priv->request_socket =
                 gssdp_socket_source_new (GSSDP_SOCKET_SOURCE_TYPE_REQUEST,
-                                         gssdp_client_get_host_ip (client),
+                                         address,
                                          client->priv->socket_ttl,
                                          client->priv->device.iface_name,
                                          &internal_error);
@@ -252,7 +257,7 @@ gssdp_client_initable_init (GInitable                   *initable,
 
         client->priv->multicast_socket =
                 gssdp_socket_source_new (GSSDP_SOCKET_SOURCE_TYPE_MULTICAST,
-                                         gssdp_client_get_host_ip (client),
+                                         address,
                                          client->priv->socket_ttl,
                                          client->priv->device.iface_name,
                                          &internal_error);
@@ -272,7 +277,7 @@ gssdp_client_initable_init (GInitable                   *initable,
                                          NULL,
                                          &internal_error,
                                          "type", GSSDP_SOCKET_SOURCE_TYPE_SEARCH,
-                                         "host-ip", gssdp_client_get_host_ip (client),
+                                         "address", address,
                                          "ttl", client->priv->socket_ttl,
                                          "port", client->priv->msearch_port,
                                          "device-name", client->priv->device.iface_name,
@@ -284,7 +289,10 @@ gssdp_client_initable_init (GInitable                   *initable,
                                          (GSourceFunc) search_socket_source_cb,
                                          client);
         }
+
  errors:
+        g_object_unref (address);
+
         if (!client->priv->request_socket ||
             !client->priv->multicast_socket ||
             !client->priv->search_socket) {
diff --git a/libgssdp/gssdp-socket-source.c b/libgssdp/gssdp-socket-source.c
index b72dc68..ed12008 100644
--- a/libgssdp/gssdp-socket-source.c
+++ b/libgssdp/gssdp-socket-source.c
@@ -57,7 +57,7 @@ struct _GSSDPSocketSourcePrivate {
         GSocket              *socket;
         GSSDPSocketSourceType type;
 
-        char                 *host_ip;
+        GInetAddress         *address;
         char                 *device_name;
         guint                 ttl;
         guint                 port;
@@ -66,7 +66,7 @@ struct _GSSDPSocketSourcePrivate {
 enum {
     PROP_0,
     PROP_TYPE,
-    PROP_HOST_IP,
+    PROP_ADDRESS,
     PROP_TTL,
     PROP_PORT,
     PROP_IFA_NAME
@@ -121,12 +121,12 @@ gssdp_socket_source_set_property (GObject          *object,
         case PROP_TYPE:
                 self->priv->type = g_value_get_int (value);
                 break;
-        case PROP_HOST_IP:
-                self->priv->host_ip = g_value_dup_string (value);
-                break;
         case PROP_IFA_NAME:
                 self->priv->device_name = g_value_dup_string (value);
                 break;
+        case PROP_ADDRESS:
+                self->priv->address = g_value_dup_object (value);
+                break;
         case PROP_TTL:
                 self->priv->ttl = g_value_get_uint (value);
                 break;
@@ -146,7 +146,7 @@ gssdp_socket_source_set_property (GObject          *object,
  **/
 GSSDPSocketSource *
 gssdp_socket_source_new (GSSDPSocketSourceType type,
-                         const char           *host_ip,
+                         GInetAddress         *address,
                          guint                 ttl,
                          const char           *device_name,
                          GError              **error)
@@ -156,8 +156,8 @@ gssdp_socket_source_new (GSSDPSocketSourceType type,
                                error,
                                "type",
                                type,
-                               "host-ip",
-                               host_ip,
+                               "address",
+                               address,
                                "ttl",
                                ttl,
                                "device-name",
@@ -171,7 +171,6 @@ gssdp_socket_source_do_init (GInitable                   *initable,
                              GError                     **error)
 {
         GSSDPSocketSource *self = NULL;
-        GInetAddress *iface_address = NULL;
         GSocketAddress *bind_address = NULL;
         GInetAddress *group = NULL;
         GError *inner_error = NULL;
@@ -179,18 +178,8 @@ gssdp_socket_source_do_init (GInitable                   *initable,
         gboolean success = FALSE;
 
         self = GSSDP_SOCKET_SOURCE (initable);
-        iface_address = g_inet_address_new_from_string (self->priv->host_ip);
-        if (iface_address == NULL) {
-                g_set_error (error,
-                             GSSDP_ERROR,
-                             GSSDP_ERROR_FAILED,
-                             "Invalid host ip: %s",
-                             self->priv->host_ip);
-
-                goto error;
-        }
 
-        family = g_inet_address_get_family (iface_address);
+        family = g_inet_address_get_family (self->priv->address);
 
         if (family == G_SOCKET_FAMILY_IPV4)
                 group = g_inet_address_new_from_string (SSDP_ADDR);
@@ -245,7 +234,7 @@ gssdp_socket_source_do_init (GInitable                   *initable,
                 g_socket_set_multicast_loopback (self->priv->socket, TRUE);
 
                 if (!gssdp_socket_mcast_interface_set (self->priv->socket,
-                                                       iface_address,
+                                                       self->priv->address,
                                                        &inner_error)) {
                         g_propagate_prefixed_error (
                                         error,
@@ -270,7 +259,7 @@ gssdp_socket_source_do_init (GInitable                   *initable,
                 if (self->priv->type == GSSDP_SOCKET_SOURCE_TYPE_SEARCH)
                         port = self->priv->port;
 
-                bind_address = g_inet_socket_address_new (iface_address,
+                bind_address = g_inet_socket_address_new (self->priv->address,
                                                           port);
         }
 
@@ -328,8 +317,6 @@ gssdp_socket_source_do_init (GInitable                   *initable,
         success = TRUE;
 
 error:
-        if (iface_address != NULL)
-                g_object_unref (iface_address);
         if (bind_address != NULL)
                 g_object_unref (bind_address);
         if (group != NULL)
@@ -402,10 +389,7 @@ gssdp_socket_source_finalize (GObject *object)
 
         self = GSSDP_SOCKET_SOURCE (object);
 
-        if (self->priv->host_ip != NULL) {
-                g_free (self->priv->host_ip);
-                self->priv->host_ip = NULL;
-        }
+        g_clear_object (&self->priv->address);
 
         if (self->priv->device_name != NULL) {
                 g_free (self->priv->device_name);
@@ -445,12 +429,12 @@ gssdp_socket_source_class_init (GSSDPSocketSourceClass *klass)
 
         g_object_class_install_property
                 (object_class,
-                 PROP_HOST_IP,
-                 g_param_spec_string
-                        ("host-ip",
-                         "Host ip",
+                 PROP_ADDRESS,
+                 g_param_spec_object
+                        ("address",
+                         "Host address",
                          "IP address of associated network interface",
-                         NULL,
+                         G_TYPE_INET_ADDRESS,
                          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
                          G_PARAM_STATIC_BLURB));
diff --git a/libgssdp/gssdp-socket-source.h b/libgssdp/gssdp-socket-source.h
index d2e9611..8aeca21 100644
--- a/libgssdp/gssdp-socket-source.h
+++ b/libgssdp/gssdp-socket-source.h
@@ -74,7 +74,7 @@ typedef struct _GSSDPSocketSourceClass {
 
 G_GNUC_INTERNAL GSSDPSocketSource *
 gssdp_socket_source_new        (GSSDPSocketSourceType  type,
-                                const char            *host_ip,
+                                GInetAddress          *address,
                                 guint                  ttl,
                                 const char            *device_name,
                                 GError               **error);


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