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>
36 # include <sys/types.h>
42 #include "ginetaddress.h"
43 #include "ginet4address.h"
44 #include "ginet6address.h"
46 #include "gnioerror.h"
47 #include "ginetsocketaddress.h"
49 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
61 struct _GSocketPrivate
66 GSocketAddress *local_address;
67 GSocketAddress *remote_address;
71 g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
73 GSocket *socket = G_SOCKET (object);
78 g_value_set_int (value, socket->priv->fd);
82 g_value_set_boolean (value, socket->priv->blocking);
86 g_value_set_int (value, socket->priv->backlog);
89 case PROP_LOCAL_ADDRESS:
90 g_value_set_object (value, socket->priv->local_address);
93 case PROP_REMOTE_ADDRESS:
94 g_value_set_object (value, socket->priv->remote_address);
98 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103 g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
105 GSocket *socket = G_SOCKET (object);
110 socket->priv->fd = g_value_get_int (value);
114 g_socket_set_blocking (socket, g_value_get_boolean (value));
118 socket->priv->backlog = g_value_get_int (value);
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 g_socket_finalize (GObject *object)
129 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
131 if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
132 (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
136 g_socket_dispose (GObject *object)
138 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
140 if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
141 (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
145 g_socket_class_init (GSocketClass *klass)
147 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
151 g_type_class_add_private (klass, sizeof (GSocketPrivate));
153 gobject_class->finalize = g_socket_finalize;
154 gobject_class->dispose = g_socket_dispose;
155 gobject_class->set_property = g_socket_set_property;
156 gobject_class->get_property = g_socket_get_property;
158 g_object_class_install_property (gobject_class, PROP_FD,
159 g_param_spec_int ("fd",
161 "the socket's file descriptor",
165 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
167 g_object_class_install_property (gobject_class, PROP_BLOCKING,
168 g_param_spec_boolean ("blocking",
170 "whether or not this socket is blocking",
172 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
174 g_object_class_install_property (gobject_class, PROP_BACKLOG,
175 g_param_spec_int ("backlog",
177 "outstanding connections in the listen queue",
181 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
183 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
184 g_param_spec_object ("local-address",
186 "the local address the socket is bound to",
187 G_TYPE_SOCKET_ADDRESS,
188 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
190 g_object_class_install_property (gobject_class, PROP_REMOTE_ADDRESS,
191 g_param_spec_object ("remote-address",
193 "the remote address the socket is connected to",
194 G_TYPE_SOCKET_ADDRESS,
195 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
199 g_socket_init (GSocket *socket)
201 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket, G_TYPE_SOCKET, GSocketPrivate);
203 socket->priv->fd = -1;
204 socket->priv->blocking = TRUE;
205 socket->priv->backlog = 10;
206 socket->priv->remote_address = NULL;
207 socket->priv->local_address = NULL;
211 g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GError **error)
213 static GStaticMutex getprotobyname_mutex = G_STATIC_MUTEX_INIT;
214 gint fd, native_domain, native_type, native_protocol;
218 case G_SOCKET_DOMAIN_INET:
219 native_domain = PF_INET;
222 case G_SOCKET_DOMAIN_INET6:
223 native_domain = PF_INET6;
226 case G_SOCKET_DOMAIN_UNIX:
227 native_domain = PF_UNIX;
231 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain");
237 case G_SOCKET_TYPE_STREAM:
238 native_type = SOCK_STREAM;
241 case G_SOCKET_TYPE_DATAGRAM:
242 native_type = SOCK_DGRAM;
245 case G_SOCKET_TYPE_SEQPACKET:
246 native_type = SOCK_SEQPACKET;
250 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type");
254 if (protocol == NULL)
258 struct protoent *ent;
259 g_static_mutex_lock (&getprotobyname_mutex);
260 if (!(ent = getprotobyname (protocol)))
262 g_static_mutex_unlock (&getprotobyname_mutex);
263 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket protocol");
266 native_protocol = ent->p_proto;
267 g_static_mutex_unlock (&getprotobyname_mutex);
270 fd = socket(native_domain, native_type, native_protocol);
274 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno));
278 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, "blocking", TRUE, NULL));
282 g_socket_new_from_fd (gint fd)
287 if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
288 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
290 blocking = ((arg & O_NONBLOCK) == 0);
292 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, "blocking", blocking, NULL));
296 g_socket_set_blocking (GSocket *socket,
301 g_return_if_fail (G_IS_SOCKET (socket));
303 if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
304 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
306 arg = blocking ? arg & ~O_NONBLOCK : arg | O_NONBLOCK;
308 if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
309 g_warning ("Error setting socket status flags: %s", g_strerror (errno));
311 socket->priv->blocking = blocking;
315 g_socket_get_blocking (GSocket *socket)
317 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
319 return socket->priv->blocking;
323 g_socket_get_local_address (GSocket *socket,
329 g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
331 if (socket->priv->local_address)
332 return socket->priv->local_address;
334 if (getsockname (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
336 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get local address: %s", g_strerror (errno));
340 return (socket->priv->local_address = g_object_ref_sink (g_socket_address_from_native (buffer, len)));
344 g_socket_get_remote_address (GSocket *socket,
350 g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
352 if (socket->priv->remote_address)
353 return socket->priv->remote_address;
355 if (getpeername (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
357 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get remote address: %s", g_strerror (errno));
361 return (socket->priv->remote_address = g_object_ref_sink (g_socket_address_from_native (buffer, len)));
365 g_socket_listen (GSocket *socket)
367 g_return_if_fail (G_IS_SOCKET (socket));
369 listen (socket->priv->fd, socket->priv->backlog);
373 g_socket_bind (GSocket *socket,
374 GSocketAddress *address,
377 g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
382 if (!g_socket_address_to_native (address, addr))
385 if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_native_size (address)) < 0)
387 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error binding to address: %s", g_strerror (errno));
391 g_object_ref_sink (address);
393 socket->priv->local_address = address;
400 g_socket_accept (GSocket *socket,
405 if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
407 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection: %s", g_strerror (errno));
411 return g_socket_new_from_fd (ret);
415 g_socket_connect (GSocket *socket,
416 GSocketAddress *address,
421 g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
423 g_socket_address_to_native (address, buffer);
425 if (connect (socket->priv->fd, (struct sockaddr *) buffer, g_socket_address_native_size (address)) < 0)
427 if (errno == EINPROGRESS)
428 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
430 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
434 socket->priv->remote_address = g_object_ref_sink (address);
440 g_socket_receive (GSocket *socket,
447 g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, FALSE);
449 if ((ret = recv (socket->priv->fd, buffer, size, 0)) < 0)
451 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error receiving data: %s", g_strerror (errno));
459 g_socket_send (GSocket *socket,
466 g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, FALSE);
468 if ((ret = send (socket->priv->fd, buffer, size, 0)) < 0)
470 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error sending data: %s", g_strerror (errno));
478 g_socket_close (GSocket *socket)
480 g_return_if_fail (G_IS_SOCKET (socket));
483 closesocket (socket->priv->fd);
485 close (socket->priv->fd);
490 g_socket_create_source (GSocket *socket,
491 GIOCondition condition,
492 GCancellable *cancellable)
494 g_return_val_if_fail (G_IS_SOCKET (socket) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL);
496 return _g_fd_source_new (socket->priv->fd, G_IO_IN | G_IO_HUP | G_IO_ERR, cancellable);