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>
39 #include "ginetaddress.h"
40 #include "ginet4address.h"
41 #include "ginet6address.h"
43 #include "gnioerror.h"
44 #include "ginetsocketaddress.h"
46 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
54 struct _GSocketPrivate
61 g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
63 GSocket *socket = G_SOCKET (object);
68 g_value_set_int (value, socket->priv->fd);
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77 g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
79 GSocket *socket = G_SOCKET (object);
84 socket->priv->fd = g_value_get_int (value);
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 g_socket_finalize (GObject *object)
95 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
97 if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
98 (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
102 g_socket_dispose (GObject *object)
104 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
106 if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
107 (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
111 g_socket_class_init (GSocketClass *klass)
113 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
115 g_type_class_add_private (klass, sizeof (GSocketPrivate));
117 gobject_class->finalize = g_socket_finalize;
118 gobject_class->dispose = g_socket_dispose;
119 gobject_class->set_property = g_socket_set_property;
120 gobject_class->get_property = g_socket_get_property;
124 g_socket_init (GSocket *socket)
126 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket, G_TYPE_SOCKET, GSocketPrivate);
128 socket->priv->fd = -1;
129 socket->priv->blocking = TRUE;
133 g_socket_new (gint domain, gint type, gint protocol)
137 sock = socket(domain, type, protocol);
142 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", sock, NULL));
146 g_socket_new_from_fd (gint fd)
148 return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, NULL));
152 g_socket_set_blocking (GSocket *socket,
157 g_return_if_fail (G_IS_SOCKET (socket));
159 if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
160 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
162 arg = blocking ? arg | O_NONBLOCK : arg & ~O_NONBLOCK;
164 if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
165 g_warning ("Error setting socket status flags: %s", g_strerror (errno));
169 g_socket_listen (GSocket *socket,
172 g_return_if_fail (G_IS_SOCKET (socket));
174 listen (socket->priv->fd, backlog);
178 g_socket_bind (GSocket *socket,
179 GSocketAddress *address,
182 g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
185 gchar addr[g_socket_address_native_size (address)];
187 if (!g_socket_address_to_native (address, addr))
190 if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_native_size (address)) < 0)
196 g_object_unref (address);
203 g_socket_connect (GSocket *socket,
204 GSocketAddress *address,
205 GCancellable *cancellable,
208 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
212 if (errno == EINPROGRESS)
213 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
215 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
223 g_socket_connect_async (GSocket *socket,
224 GSocketAddress *address,
225 GCancellable *cancellable,
226 GAsyncReadyCallback *callback,
233 g_socket_connect_finish (GSocket *socket,
234 GAsyncResult *result,