Starting on TcpClient, some formatting fixes
[cascardo/gnio.git] / gnio / ginetsocketaddress.c
index 4c1a232..040064a 100644 (file)
@@ -1,6 +1,6 @@
 /* GNIO - GLib Network Layer of GIO
- * 
- * Copyright (C) 2008 Christian Kellner 
+ *
+ * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  * Boston, MA 02111-1307, USA.
  *
- * Author: Christian Kellner <gicmo@gnome.org>
+ * Authors: Christian Kellner <gicmo@gnome.org>
+ *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
  */
 
 #include <config.h>
 #include <glib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
 
 #include "ginetsocketaddress.h"
-
+#include "ginetaddress.h"
+#include "ginet4address.h"
+#include "ginet6address.h"
 
 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
 
-struct _GInetSocketAddressPrivate {
-
-    GInetAddress *address;
-    guint16       port;
+enum {
+  PROP_0,
+  PROP_ADDRESS,
+  PROP_PORT
 };
 
+struct _GInetSocketAddressPrivate
+{
+  GInetAddress *address;
+  guint16       port;
+};
 
 static void
 g_inet_socket_address_finalize (GObject *object)
 {
-  GInetSocketAddress *address;
+  GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
 
-  address = G_INET_SOCKET_ADDRESS (object);
-  
   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
 }
@@ -49,25 +58,136 @@ g_inet_socket_address_finalize (GObject *object)
 static void
 g_inet_socket_address_dispose (GObject *object)
 {
-  GInetSocketAddress *address;
+  GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
+
+  g_object_unref (address->priv->address);
 
-  address = G_INET_SOCKET_ADDRESS (object);
-  
   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
 }
 
+static void
+g_inet_socket_address_get_property (GObject    *object,
+                                    guint       prop_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+  GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
+
+  switch (prop_id)
+    {
+      case PROP_ADDRESS:
+        g_value_set_object (value, address->priv->address);
+        break;
+
+      case PROP_PORT:
+        g_value_set_uint (value, address->priv->port);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_inet_socket_address_set_property (GObject      *object,
+                                    guint         prop_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+  GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
+
+  switch (prop_id)
+    {
+      case PROP_ADDRESS:
+        address->priv->address = G_INET_ADDRESS (g_object_ref_sink (g_value_get_object (value)));
+        break;
+
+      case PROP_PORT:
+        address->priv->port = (guint16) g_value_get_uint (value);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static gssize
+g_inet_socket_address_native_size (GSocketAddress *address)
+{
+  GInetSocketAddress *addr;
+
+  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
+
+  addr = G_INET_SOCKET_ADDRESS (address);
+
+  if (G_IS_INET4_ADDRESS (addr->priv->address))
+    return sizeof (struct sockaddr_in);
+  else if (G_IS_INET6_ADDRESS (addr->priv->address))
+    return sizeof (struct sockaddr_in6);
+  else
+    return -1;
+}
+
+static gboolean
+g_inet_socket_address_to_native (GSocketAddress *address,
+                                 gpointer        dest)
+{
+  GInetSocketAddress *addr;
+
+  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
+
+  addr = G_INET_SOCKET_ADDRESS (address);
+
+  if (G_IS_INET4_ADDRESS (addr->priv->address))
+    {
+      struct sockaddr_in *sock = (struct sockaddr_in *) dest;
+
+      sock->sin_family = AF_INET;
+      sock->sin_port = g_htons (addr->priv->port);
+      memcpy (&(sock->sin_addr.s_addr), g_inet4_address_to_bytes (G_INET4_ADDRESS (addr->priv->address)), sizeof (sock->sin_addr));
+      memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
+      return TRUE;
+    }
+  else if (G_IS_INET6_ADDRESS (addr->priv->address))
+    {
+      return FALSE;
+    }
+  else
+    return FALSE;
+}
 
 static void
 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
-  
+  GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
+
   g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
-  
+
   gobject_class->finalize = g_inet_socket_address_finalize;
   gobject_class->dispose = g_inet_socket_address_dispose;
-
+  gobject_class->set_property = g_inet_socket_address_set_property;
+  gobject_class->get_property = g_inet_socket_address_get_property;
+
+  gsocketaddress_class->to_native = g_inet_socket_address_to_native;
+  gsocketaddress_class->native_size = g_inet_socket_address_native_size;
+
+  g_object_class_install_property (gobject_class, PROP_ADDRESS,
+                                   g_param_spec_object ("address",
+                                                        "address",
+                                                        "address",
+                                                        G_TYPE_INET_ADDRESS,
+                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+
+  g_object_class_install_property (gobject_class, PROP_PORT,
+                                   g_param_spec_uint ("port",
+                                                      "port",
+                                                      "port",
+                                                      0,
+                                                      65535,
+                                                      0,
+                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
 }
 
 static void
@@ -76,25 +196,32 @@ g_inet_socket_address_init (GInetSocketAddress *address)
   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
                                                G_TYPE_INET_SOCKET_ADDRESS,
                                                GInetSocketAddressPrivate);
+
+  address->priv->address = NULL;
+  address->priv->port = 0;
 }
 
 
 GInetSocketAddress *
-g_inet_socket_address_new (GInetAddress *address, guint16 port)
+g_inet_socket_address_new (GInetAddress *address,
+                           guint16       port)
 {
-    return NULL;
+  return G_INET_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS, "address", address, "port", port, NULL));
 }
 
-
 GInetAddress *
 g_inet_socket_address_get_address (GInetSocketAddress *sockaddr)
 {
-    return NULL;
+  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), NULL);
+
+  return sockaddr->priv->address;
 }
 
 guint16 
 g_inet_socket_address_get_port (GInetSocketAddress *sockaddr)
 {
-    return 0;
+  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), 0);
+
+  return sockaddr->priv->port;
 }