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_SOCKET (object);
131 if (socket->priv->local_address)
132 g_object_unref (socket->priv->local_address);
134 if (socket->priv->remote_address)
135 g_object_unref (socket->priv->remote_address);
137 if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
138 (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
142 g_socket_dispose (GObject *object)
144 GSocket *socket = G_SOCKET (object);
146 g_socket_close (socket);
148 if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
149 (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
153 g_socket_class_init (GSocketClass *klass)
155 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
159 g_type_class_add_private (klass, sizeof (GSocketPrivate));
161 gobject_class->finalize = g_socket_finalize;
162 gobject_class->dispose = g_socket_dispose;
163 gobject_class->set_property = g_socket_set_property;
164 gobject_class->get_property = g_socket_get_property;
166 g_object_class_install_property (gobject_class, PROP_FD,
167 g_param_spec_int ("fd",
169 "the socket's file descriptor",
173 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
175 g_object_class_install_property (gobject_class, PROP_BLOCKING,
176 g_param_spec_boolean ("blocking",
178 "whether or not this socket is blocking",
180 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
182 g_object_class_install_property (gobject_class, PROP_BACKLOG,
183 g_param_spec_int ("backlog",
185 "outstanding connections in the listen queue",
189 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
191 g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
192 g_param_spec_object ("local-address",
194 "the local address the socket is bound to",
195 G_TYPE_SOCKET_ADDRESS,
196 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
198 g_object_class_install_property (gobject_class, PROP_REMOTE_ADDRESS,
199 g_param_spec_object ("remote-address",
201 "the remote address the socket is connected to",
202 G_TYPE_SOCKET_ADDRESS,
203 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
207 g_socket_init (GSocket *socket)
209 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket, G_TYPE_SOCKET, GSocketPrivate);
211 socket->priv->fd = -1;
212 socket->priv->blocking = TRUE;
213 socket->priv->backlog = 10;
214 socket->priv->remote_address = NULL;
215 socket->priv->local_address = NULL;
219 g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GError **error)
221 static GStaticMutex getprotobyname_mutex = G_STATIC_MUTEX_INIT;
222 gint fd, native_domain, native_type, native_protocol;
226 case G_SOCKET_DOMAIN_INET:
227 native_domain = PF_INET;
230 case G_SOCKET_DOMAIN_INET6:
231 native_domain = PF_INET6;
234 case G_SOCKET_DOMAIN_UNIX:
235 native_domain = PF_UNIX;
239 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain");
245 case G_SOCKET_TYPE_STREAM:
246 native_type = SOCK_STREAM;
249 case G_SOCKET_TYPE_DATAGRAM:
250 native_type = SOCK_DGRAM;
253 case G_SOCKET_TYPE_SEQPACKET:
254 native_type = SOCK_SEQPACKET;
258 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type");
262 if (protocol == NULL)
266 struct protoent *ent;
267 g_static_mutex_lock (&getprotobyname_mutex);
268 if (!(ent = getprotobyname (protocol)))
270 g_static_mutex_unlock (&getprotobyname_mutex);
271 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket protocol");
274 native_protocol = ent->p_proto;
275 g_static_mutex_unlock (&getprotobyname_mutex);
278 fd = socket(native_domain, native_type, native_protocol);
282 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno));
286 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, "blocking", TRUE, NULL));
290 g_socket_new_from_fd (gint fd)
295 if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
296 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
298 blocking = ((arg & O_NONBLOCK) == 0);
300 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, "blocking", blocking, NULL));
304 g_socket_set_blocking (GSocket *socket,
309 g_return_if_fail (G_IS_SOCKET (socket));
311 if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
312 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
314 arg = blocking ? arg & ~O_NONBLOCK : arg | O_NONBLOCK;
316 if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
317 g_warning ("Error setting socket status flags: %s", g_strerror (errno));
319 socket->priv->blocking = blocking;
323 g_socket_get_blocking (GSocket *socket)
325 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
327 return socket->priv->blocking;
331 g_socket_get_local_address (GSocket *socket,
337 g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
339 if (socket->priv->local_address)
340 return socket->priv->local_address;
342 if (getsockname (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
344 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get local address: %s", g_strerror (errno));
348 return (socket->priv->local_address = g_object_ref_sink (g_socket_address_from_native (buffer, len)));
352 g_socket_get_remote_address (GSocket *socket,
358 g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
360 if (socket->priv->remote_address)
361 return socket->priv->remote_address;
363 if (getpeername (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
365 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get remote address: %s", g_strerror (errno));
369 return (socket->priv->remote_address = g_object_ref_sink (g_socket_address_from_native (buffer, len)));
373 g_socket_listen (GSocket *socket)
375 g_return_if_fail (G_IS_SOCKET (socket));
377 listen (socket->priv->fd, socket->priv->backlog);
381 g_socket_bind (GSocket *socket,
382 GSocketAddress *address,
385 g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
390 if (!g_socket_address_to_native (address, addr))
393 if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_native_size (address)) < 0)
395 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error binding to address: %s", g_strerror (errno));
399 g_object_ref_sink (address);
401 socket->priv->local_address = address;
408 g_socket_accept (GSocket *socket,
413 if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
415 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection: %s", g_strerror (errno));
419 return g_socket_new_from_fd (ret);
423 g_socket_connect (GSocket *socket,
424 GSocketAddress *address,
429 g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
431 g_socket_address_to_native (address, buffer);
433 if (connect (socket->priv->fd, (struct sockaddr *) buffer, g_socket_address_native_size (address)) < 0)
435 if (errno == EINPROGRESS)
436 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
438 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
442 socket->priv->remote_address = g_object_ref_sink (address);
448 g_socket_receive (GSocket *socket,
455 g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, FALSE);
457 if ((ret = recv (socket->priv->fd, buffer, size, 0)) < 0)
459 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error receiving data: %s", g_strerror (errno));
467 g_socket_send (GSocket *socket,
474 g_return_val_if_fail (G_IS_SOCKET (socket) && buffer != NULL, FALSE);
476 if ((ret = send (socket->priv->fd, buffer, size, 0)) < 0)
478 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error sending data: %s", g_strerror (errno));
486 g_socket_close (GSocket *socket)
488 g_return_if_fail (G_IS_SOCKET (socket));
491 closesocket (socket->priv->fd);
493 close (socket->priv->fd);
498 g_socket_create_source (GSocket *socket,
499 GIOCondition condition,
500 GCancellable *cancellable)
502 g_return_val_if_fail (G_IS_SOCKET (socket) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL);
504 return _g_fd_source_new (socket->priv->fd, G_IO_IN | G_IO_HUP | G_IO_ERR, cancellable);