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>
30 # include <netinet/in.h>
31 # include <arpa/inet.h>
40 #include "ginetaddress.h"
41 #include "ginet4address.h"
42 #include "ginet6address.h"
44 #include "gnioerror.h"
45 #include "ginetsocketaddress.h"
47 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
56 struct _GSocketPrivate
63 g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
65 GSocket *socket = G_SOCKET (object);
70 g_value_set_int (value, socket->priv->fd);
74 g_value_set_boolean (value, socket->priv->blocking);
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
83 g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
85 GSocket *socket = G_SOCKET (object);
90 socket->priv->fd = g_value_get_int (value);
94 g_socket_set_blocking (socket, g_value_get_boolean (value));
98 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103 g_socket_finalize (GObject *object)
105 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
107 if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
108 (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
112 g_socket_dispose (GObject *object)
114 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
116 if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
117 (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
121 g_socket_class_init (GSocketClass *klass)
123 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
125 g_type_class_add_private (klass, sizeof (GSocketPrivate));
127 gobject_class->finalize = g_socket_finalize;
128 gobject_class->dispose = g_socket_dispose;
129 gobject_class->set_property = g_socket_set_property;
130 gobject_class->get_property = g_socket_get_property;
132 g_object_class_install_property (gobject_class, PROP_FD,
133 g_param_spec_int ("fd",
135 "the socket's file descriptor",
139 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
141 g_object_class_install_property (gobject_class, PROP_BLOCKING,
142 g_param_spec_boolean ("blocking",
144 "whether or not this socket is blocking",
146 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
150 g_socket_init (GSocket *socket)
152 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket, G_TYPE_SOCKET, GSocketPrivate);
154 socket->priv->fd = -1;
155 socket->priv->blocking = TRUE;
159 g_socket_new (gint domain, gint type, gint protocol)
163 sock = socket(domain, type, protocol);
168 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", sock, NULL));
172 g_socket_new_from_fd (gint fd)
177 if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
178 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
180 blocking = ((arg & O_NONBLOCK) != 0);
182 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "blocking", blocking, "fd", fd, NULL));
186 g_socket_set_blocking (GSocket *socket,
191 g_return_if_fail (G_IS_SOCKET (socket));
193 if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
194 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
196 arg = blocking ? arg | O_NONBLOCK : arg & ~O_NONBLOCK;
198 if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
199 g_warning ("Error setting socket status flags: %s", g_strerror (errno));
203 g_socket_get_peer_address (GSocket *socket,
209 if (getpeername (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
211 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get peer address");
215 return g_socket_address_from_native (buffer, len);
219 g_socket_listen (GSocket *socket,
222 g_return_if_fail (G_IS_SOCKET (socket));
224 listen (socket->priv->fd, backlog);
228 g_socket_bind (GSocket *socket,
229 GSocketAddress *address,
232 g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
235 gchar addr[g_socket_address_native_size (address)];
237 if (!g_socket_address_to_native (address, addr))
240 if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_native_size (address)) < 0)
246 g_object_unref (address);
253 g_socket_accept (GSocket *socket,
254 GCancellable *cancellable,
259 if (g_cancellable_set_error_if_cancelled (cancellable, error))
262 if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
264 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection");
268 if (g_cancellable_set_error_if_cancelled (cancellable, error))
274 return g_socket_new_from_fd (ret);
278 g_socket_accept_async (GSocket *socket,
279 GCancellable *cancellable,
280 GAsyncReadyCallback *callback,
287 g_socket_accept_finish (GSocket *socket,
288 GAsyncResult *result,
295 g_socket_connect (GSocket *socket,
296 GSocketAddress *address,
297 GCancellable *cancellable,
300 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
304 if (errno == EINPROGRESS)
305 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
307 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
315 g_socket_connect_async (GSocket *socket,
316 GSocketAddress *address,
317 GCancellable *cancellable,
318 GAsyncReadyCallback *callback,
325 g_socket_connect_finish (GSocket *socket,
326 GAsyncResult *result,