1 /* GNIO - GLib Network Layer of GIO
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
27 #include <gio/gasynchelper.h>
31 # include <netinet/in.h>
32 # include <arpa/inet.h>
41 #include "ginetaddress.h"
42 #include "ginet4address.h"
43 #include "ginet6address.h"
45 #include "gnioerror.h"
46 #include "ginetsocketaddress.h"
48 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
57 struct _GSocketPrivate
64 g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
66 GSocket *socket = G_SOCKET (object);
71 g_value_set_int (value, socket->priv->fd);
75 g_value_set_boolean (value, socket->priv->blocking);
79 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84 g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
86 GSocket *socket = G_SOCKET (object);
91 socket->priv->fd = g_value_get_int (value);
95 g_socket_set_blocking (socket, g_value_get_boolean (value));
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 g_socket_finalize (GObject *object)
106 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
108 if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
109 (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
113 g_socket_dispose (GObject *object)
115 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
117 if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
118 (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
122 g_socket_class_init (GSocketClass *klass)
124 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
126 g_type_class_add_private (klass, sizeof (GSocketPrivate));
128 gobject_class->finalize = g_socket_finalize;
129 gobject_class->dispose = g_socket_dispose;
130 gobject_class->set_property = g_socket_set_property;
131 gobject_class->get_property = g_socket_get_property;
133 g_object_class_install_property (gobject_class, PROP_FD,
134 g_param_spec_int ("fd",
136 "the socket's file descriptor",
140 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
142 g_object_class_install_property (gobject_class, PROP_BLOCKING,
143 g_param_spec_boolean ("blocking",
145 "whether or not this socket is blocking",
147 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
151 g_socket_init (GSocket *socket)
153 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket, G_TYPE_SOCKET, GSocketPrivate);
155 socket->priv->fd = -1;
156 socket->priv->blocking = TRUE;
160 g_socket_new (gint domain, gint type, gint protocol)
164 sock = socket(domain, type, protocol);
169 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", sock, NULL));
173 g_socket_new_from_fd (gint fd)
178 if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
179 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
181 blocking = ((arg & O_NONBLOCK) != 0);
183 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "blocking", blocking, "fd", fd, NULL));
187 g_socket_set_blocking (GSocket *socket,
192 g_return_if_fail (G_IS_SOCKET (socket));
194 if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
195 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
197 arg = blocking ? arg | O_NONBLOCK : arg & ~O_NONBLOCK;
199 if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
200 g_warning ("Error setting socket status flags: %s", g_strerror (errno));
202 socket->priv->blocking = blocking;
206 g_socket_get_blocking (GSocket *socket)
208 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
210 return socket->priv->blocking;
214 g_socket_get_peer_address (GSocket *socket,
220 g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
222 if (getpeername (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
224 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get peer address");
228 return g_socket_address_from_native (buffer, len);
232 g_socket_listen (GSocket *socket,
235 g_return_if_fail (G_IS_SOCKET (socket));
237 listen (socket->priv->fd, backlog);
241 g_socket_bind (GSocket *socket,
242 GSocketAddress *address,
245 g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
248 gchar addr[g_socket_address_native_size (address)];
250 if (!g_socket_address_to_native (address, addr))
253 if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_native_size (address)) < 0)
259 g_object_unref (address);
266 g_socket_accept (GSocket *socket,
267 GCancellable *cancellable,
272 if (g_cancellable_set_error_if_cancelled (cancellable, error))
275 if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
277 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection");
281 if (g_cancellable_set_error_if_cancelled (cancellable, error))
287 return g_socket_new_from_fd (ret);
291 GAsyncReadyCallback callback;
292 GCancellable *cancellable;
298 accept_callback (AcceptData *data,
299 GIOCondition condition,
302 if (condition & G_IO_IN)
304 g_print ("WE COULD ACCEPT HERE\n");
311 g_socket_accept_async (GSocket *socket,
312 GCancellable *cancellable,
313 GAsyncReadyCallback callback,
317 GSimpleAsyncResult *result;
321 g_return_if_fail (G_IS_SOCKET (socket));
323 if (g_socket_get_blocking (socket))
324 g_socket_set_blocking (socket, FALSE);
326 if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
330 source = _g_fd_source_new (socket->priv->fd, G_IO_IN | G_IO_HUP | G_IO_ERR, cancellable);
332 data = g_new (AcceptData, 1);
334 data->socket = socket;
335 data->callback = callback;
336 data->cancellable = cancellable;
337 data->user_data = user_data;
339 g_source_set_callback (source, (GSourceFunc) accept_callback, data, g_free);
341 g_source_attach (source, NULL);
345 g_simple_async_report_error_in_idle (G_OBJECT (socket), callback, user_data, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection");
350 result = g_simple_async_result_new (G_OBJECT (socket), callback, user_data, g_socket_accept_async);
352 g_simple_async_result_complete_in_idle (result);
354 g_object_unref (result);
359 g_socket_accept_finish (GSocket *socket,
360 GAsyncResult *result,
367 g_socket_connect (GSocket *socket,
368 GSocketAddress *address,
369 GCancellable *cancellable,
372 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
376 if (errno == EINPROGRESS)
377 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
379 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
387 g_socket_connect_async (GSocket *socket,
388 GSocketAddress *address,
389 GCancellable *cancellable,
390 GAsyncReadyCallback *callback,
397 g_socket_connect_finish (GSocket *socket,
398 GAsyncResult *result,