Some more stuff for GSockets
[cascardo/gnio.git] / gnio / gsocket.c
index 42b6394..fe93ca8 100644 (file)
@@ -31,6 +31,7 @@
 # include <arpa/inet.h>
 # include <netdb.h>
 # include <fcntl.h>
+# include <unistd.h>
 #else
 
 #endif
 
 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
 
+enum
+{
+  PROP_0,
+  PROP_FD,
+  PROP_BLOCKING
+};
+
 struct _GSocketPrivate
 {
-  int fd;
+  gint fd;
+  gboolean blocking;
 };
 
+static void
+g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+{
+  GSocket *socket = G_SOCKET (object);
+
+  switch (prop_id)
+    {
+      case PROP_FD:
+        g_value_set_int (value, socket->priv->fd);
+        break;
+
+      case PROP_BLOCKING:
+        g_value_set_boolean (value, socket->priv->blocking);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+{
+  GSocket *socket = G_SOCKET (object);
+
+  switch (prop_id)
+    {
+      case PROP_FD:
+        socket->priv->fd = g_value_get_int (value);
+        break;
+
+      case PROP_BLOCKING:
+        g_socket_set_blocking (socket, g_value_get_boolean (value));
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
 static void
 g_socket_finalize (GObject *object)
 {
@@ -77,28 +126,65 @@ g_socket_class_init (GSocketClass *klass)
 
   gobject_class->finalize = g_socket_finalize;
   gobject_class->dispose = g_socket_dispose;
+  gobject_class->set_property = g_socket_set_property;
+  gobject_class->get_property = g_socket_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_FD,
+                                   g_param_spec_int ("fd",
+                                                     "file descriptor",
+                                                     "the socket's file descriptor",
+                                                     G_MININT,
+                                                     G_MAXINT,
+                                                     -1,
+                                                     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_BLOCKING,
+                                   g_param_spec_boolean ("blocking",
+                                                         "blocking",
+                                                         "whether or not this socket is blocking",
+                                                         FALSE,
+                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
 }
 
 static void
-g_socket_init (GSocket *address)
+g_socket_init (GSocket *socket)
 {
-  address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address, G_TYPE_SOCKET, GSocketPrivate);
+  socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket, G_TYPE_SOCKET, GSocketPrivate);
+
+  socket->priv->fd = -1;
+  socket->priv->blocking = TRUE;
 }
 
 GSocket *
-g_socket_new ()
+g_socket_new (gint domain, gint type, gint protocol)
 {
-  return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
+  gint sock;
+
+  sock = socket(domain, type, protocol);
+
+  if (sock < 0)
+    return NULL;
+
+  return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", sock, NULL));
 }
 
 GSocket *
 g_socket_new_from_fd (gint fd)
 {
-  return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
+  glong arg;
+  gboolean blocking;
+
+  if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
+    g_warning ("Error getting socket status flags: %s", g_strerror (errno));
+
+  blocking = ((arg & O_NONBLOCK) != 0);
+
+  return G_SOCKET (g_object_new (G_TYPE_SOCKET, "blocking", blocking, "fd", fd, NULL));
 }
 
 void
-g_socket_set_blocking (GSocket *socket, gboolean blocking)
+g_socket_set_blocking (GSocket  *socket,
+                       gboolean  blocking)
 {
   glong arg;
 
@@ -113,14 +199,98 @@ g_socket_set_blocking (GSocket *socket, gboolean blocking)
     g_warning ("Error setting socket status flags: %s", g_strerror (errno));
 }
 
+GSocketAddress *
+g_socket_get_peer_address (GSocket  *socket,
+                           GError  **error)
+{
+  gchar buffer[128];
+  gsize len;
+
+  if (getpeername (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
+    {
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get peer address");
+      return NULL;
+    }
+
+  return g_socket_address_from_native (buffer, len);
+}
+
 void
-g_socket_listen (GSocket *socket, gint backlog)
+g_socket_listen (GSocket *socket,
+                 gint     backlog)
 {
   g_return_if_fail (G_IS_SOCKET (socket));
 
   listen (socket->priv->fd, backlog);
 }
 
+gboolean
+g_socket_bind (GSocket         *socket,
+               GSocketAddress  *address,
+               GError         **error)
+{
+  g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
+
+  {
+    gchar addr[g_socket_address_native_size (address)];
+
+    if (!g_socket_address_to_native (address, addr))
+      return FALSE;
+
+    if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_native_size (address)) < 0)
+      {
+        // TODO: set error
+        return FALSE;
+      }
+
+    g_object_unref (address);
+
+    return TRUE;
+  }
+}
+
+GSocket *
+g_socket_accept (GSocket       *socket,
+                 GCancellable  *cancellable,
+                 GError       **error)
+{
+  gint ret;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return NULL;
+
+  if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
+    {
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection");
+      return NULL;
+    }
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    {
+      close (ret);
+      return NULL;
+    }
+
+  return g_socket_new_from_fd (ret);
+}
+
+void
+g_socket_accept_async (GSocket             *socket,
+                       GCancellable        *cancellable,
+                       GAsyncReadyCallback *callback,
+                       gpointer             user_data)
+{
+  
+}
+
+GSocket *
+g_socket_accept_finish (GSocket       *socket,
+                        GAsyncResult  *result,
+                        GError       **error)
+{
+  return NULL;
+}
+
 gboolean
 g_socket_connect (GSocket         *socket,
                   GSocketAddress  *address,
@@ -156,5 +326,5 @@ g_socket_connect_finish (GSocket       *socket,
                          GAsyncResult  *result,
                          GError       **error)
 {
-
+  return FALSE;
 }