Remove the async stuff per IRC discussion
[cascardo/gnio.git] / gnio / gsocket.c
1 /* GNIO - GLib Network Layer of GIO
2  *
3  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Christian Kellner <gicmo@gnome.org>
21  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
22  */
23
24 #include <config.h>
25 #include <glib.h>
26 #include <gio/gio.h>
27 #include <gio/gasynchelper.h>
28
29 #include <string.h>
30 #ifndef G_OS_WIN32
31 # include <netinet/in.h>
32 # include <arpa/inet.h>
33 # include <netdb.h>
34 # include <fcntl.h>
35 # include <unistd.h>
36 # include <sys/types.h>
37 #else
38
39 #endif
40 #include <errno.h>
41
42 #include "ginetaddress.h"
43 #include "ginet4address.h"
44 #include "ginet6address.h"
45 #include "gsocket.h"
46 #include "gnioerror.h"
47 #include "ginetsocketaddress.h"
48
49 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
50
51 enum
52 {
53   PROP_0,
54   PROP_FD,
55   PROP_BLOCKING,
56   PROP_LOCAL_ADDRESS,
57   PROP_REMOTE_ADDRESS
58 };
59
60 struct _GSocketPrivate
61 {
62   gint fd;
63   gboolean blocking;
64   GSocketAddress *local_address;
65   GSocketAddress *remote_address;
66 };
67
68 static void
69 g_socket_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
70 {
71   GSocket *socket = G_SOCKET (object);
72
73   switch (prop_id)
74     {
75       case PROP_FD:
76         g_value_set_int (value, socket->priv->fd);
77         break;
78
79       case PROP_BLOCKING:
80         g_value_set_boolean (value, socket->priv->blocking);
81         break;
82
83       case PROP_LOCAL_ADDRESS:
84         g_value_set_object (value, socket->priv->local_address);
85         break;
86
87       case PROP_REMOTE_ADDRESS:
88         g_value_set_object (value, socket->priv->remote_address);
89         break;
90
91       default:
92         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93     }
94 }
95
96 static void
97 g_socket_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
98 {
99   GSocket *socket = G_SOCKET (object);
100
101   switch (prop_id)
102     {
103       case PROP_FD:
104         socket->priv->fd = g_value_get_int (value);
105         break;
106
107       case PROP_BLOCKING:
108         g_socket_set_blocking (socket, g_value_get_boolean (value));
109         break;
110
111       default:
112         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113     }
114 }
115
116 static void
117 g_socket_finalize (GObject *object)
118 {
119   GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
120
121   if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
122     (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
123 }
124
125 static void
126 g_socket_dispose (GObject *object)
127 {
128   GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
129
130   if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
131     (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
132 }
133
134 static void
135 g_socket_class_init (GSocketClass *klass)
136 {
137   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
138
139   // TODO: WSAStartup
140
141   g_type_class_add_private (klass, sizeof (GSocketPrivate));
142
143   gobject_class->finalize = g_socket_finalize;
144   gobject_class->dispose = g_socket_dispose;
145   gobject_class->set_property = g_socket_set_property;
146   gobject_class->get_property = g_socket_get_property;
147
148   g_object_class_install_property (gobject_class, PROP_FD,
149                                    g_param_spec_int ("fd",
150                                                      "file descriptor",
151                                                      "the socket's file descriptor",
152                                                      G_MININT,
153                                                      G_MAXINT,
154                                                      -1,
155                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
156
157   g_object_class_install_property (gobject_class, PROP_BLOCKING,
158                                    g_param_spec_boolean ("blocking",
159                                                          "blocking",
160                                                          "whether or not this socket is blocking",
161                                                          TRUE,
162                                                          G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
163
164   g_object_class_install_property (gobject_class, PROP_LOCAL_ADDRESS,
165                                    g_param_spec_object ("local-address",
166                                                         "local address",
167                                                         "the local address the socket is bound to",
168                                                         G_TYPE_SOCKET_ADDRESS,
169                                                         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
170
171   g_object_class_install_property (gobject_class, PROP_REMOTE_ADDRESS,
172                                    g_param_spec_object ("remote-address",
173                                                         "remote address",
174                                                         "the remote address the socket is connected to",
175                                                         G_TYPE_SOCKET_ADDRESS,
176                                                         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
177 }
178
179 static void
180 g_socket_init (GSocket *socket)
181 {
182   socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket, G_TYPE_SOCKET, GSocketPrivate);
183
184   socket->priv->fd = -1;
185   socket->priv->blocking = TRUE;
186   socket->priv->remote_address = NULL;
187   socket->priv->local_address = NULL;
188 }
189
190 GSocket *
191 g_socket_new (GSocketDomain domain, GSocketType type, const gchar *protocol, GError **error)
192 {
193   static GStaticMutex getprotobyname_mutex = G_STATIC_MUTEX_INIT;
194   gint fd, native_domain, native_type, native_protocol;
195
196   switch (domain)
197     {
198       case G_SOCKET_DOMAIN_INET:
199         native_domain = PF_INET;
200         break;
201
202       case G_SOCKET_DOMAIN_INET6:
203         native_domain = PF_INET6;
204         break;
205
206       case G_SOCKET_DOMAIN_UNIX:
207         native_domain = PF_UNIX;
208         break;
209
210       default:
211         g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket domain");
212         return NULL;
213     }
214
215   switch (type)
216     {
217       case G_SOCKET_TYPE_STREAM:
218         native_type = SOCK_STREAM;
219         break;
220
221       case G_SOCKET_TYPE_DATAGRAM:
222         native_type = SOCK_DGRAM;
223         break;
224
225       case G_SOCKET_TYPE_SEQPACKET:
226         native_type = SOCK_SEQPACKET;
227         break;
228
229       default:
230         g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket type");
231         return NULL;
232     }
233
234   if (protocol == NULL)
235     native_protocol = 0;
236   else
237     {
238       struct protoent *ent;
239       g_static_mutex_lock (&getprotobyname_mutex);
240       if (!(ent = getprotobyname (protocol)))
241         {
242           g_static_mutex_unlock (&getprotobyname_mutex);
243           g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "unsupported socket protocol");
244           return NULL;
245         }
246       native_protocol = ent->p_proto;
247       g_static_mutex_unlock (&getprotobyname_mutex);
248     }
249
250   fd = socket(native_domain, native_type, native_protocol);
251
252   if (fd < 0)
253     {
254       g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "unable to create socket: %s", g_strerror (errno));
255       return NULL;
256     }
257
258   return G_SOCKET (g_object_new (G_TYPE_SOCKET, "fd", fd, NULL));
259 }
260
261 GSocket *
262 g_socket_new_from_fd (gint fd)
263 {
264   glong arg;
265   gboolean blocking;
266
267   if ((arg = fcntl (fd, F_GETFL, NULL)) < 0)
268     g_warning ("Error getting socket status flags: %s", g_strerror (errno));
269
270   blocking = ((arg & O_NONBLOCK) != 0);
271
272   return G_SOCKET (g_object_new (G_TYPE_SOCKET, "blocking", blocking, "fd", fd, NULL));
273 }
274
275 void
276 g_socket_set_blocking (GSocket  *socket,
277                        gboolean  blocking)
278 {
279   glong arg;
280
281   g_return_if_fail (G_IS_SOCKET (socket));
282
283   if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
284     g_warning ("Error getting socket status flags: %s", g_strerror (errno));
285
286   arg = blocking ? arg & ~O_NONBLOCK : arg | O_NONBLOCK;
287
288   if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
289     g_warning ("Error setting socket status flags: %s", g_strerror (errno));
290
291   socket->priv->blocking = blocking;
292 }
293
294 gboolean
295 g_socket_get_blocking (GSocket *socket)
296 {
297   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
298
299   return socket->priv->blocking;
300 }
301
302 GSocketAddress *
303 g_socket_get_local_address (GSocket  *socket,
304                             GError  **error)
305 {
306   gchar buffer[256];
307   gsize len;
308
309   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
310
311   if (socket->priv->local_address)
312     return socket->priv->local_address;
313
314   if (getsockname (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
315     {
316       g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get local address: %s", g_strerror (errno));
317       return NULL;
318     }
319
320   return (socket->priv->local_address = g_object_ref_sink (g_socket_address_from_native (buffer, len)));
321 }
322
323 GSocketAddress *
324 g_socket_get_remote_address (GSocket  *socket,
325                              GError  **error)
326 {
327   gchar buffer[256];
328   gsize len;
329
330   g_return_val_if_fail (G_IS_SOCKET (socket), NULL);
331
332   if (socket->priv->remote_address)
333     return socket->priv->remote_address;
334
335   if (getpeername (socket->priv->fd, (struct sockaddr *) buffer, &len) < 0)
336     {
337       g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "could not get remote address: %s", g_strerror (errno));
338       return NULL;
339     }
340
341   return (socket->priv->remote_address = g_object_ref_sink (g_socket_address_from_native (buffer, len)));
342 }
343
344 void
345 g_socket_listen (GSocket *socket,
346                  gint     backlog)
347 {
348   g_return_if_fail (G_IS_SOCKET (socket));
349
350   listen (socket->priv->fd, backlog);
351 }
352
353 gboolean
354 g_socket_bind (GSocket         *socket,
355                GSocketAddress  *address,
356                GError         **error)
357 {
358   g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
359
360   {
361     gchar addr[256];
362
363     if (!g_socket_address_to_native (address, addr))
364       return FALSE;
365
366     if (bind (socket->priv->fd, (struct sockaddr *) addr, g_socket_address_native_size (address)) < 0)
367       {
368         g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error binding to address: %s", g_strerror (errno));
369         return FALSE;
370       }
371
372     g_object_ref_sink (address);
373
374     socket->priv->local_address = address;
375
376     return TRUE;
377   }
378 }
379
380 GSocket *
381 g_socket_accept (GSocket       *socket,
382                  GError       **error)
383 {
384   gint ret;
385
386   if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
387     {
388       g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error accepting connection");
389       return NULL;
390     }
391
392   return g_socket_new_from_fd (ret);
393 }
394
395 gboolean
396 g_socket_connect (GSocket         *socket,
397                   GSocketAddress  *address,
398                   GError         **error)
399 {
400   gchar buffer[256];
401
402   g_return_val_if_fail (G_IS_SOCKET (socket) && G_IS_SOCKET_ADDRESS (address), FALSE);
403
404   g_socket_address_to_native (address, buffer);
405
406   if (connect (socket->priv->fd, (struct sockaddr *) buffer, g_socket_address_native_size (address)) < 0)
407     {
408       if (errno == EINPROGRESS)
409         g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
410       else
411         g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
412       return FALSE;
413     }
414
415   socket->priv->remote_address = g_object_ref_sink (address);
416
417   return TRUE;
418 }