Converting InetAddresses to native struct in*_addr and SocketAddresses to struct...
[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
28 #include <string.h>
29 #ifndef G_OS_WIN32
30 # include <netinet/in.h>
31 # include <arpa/inet.h>
32 # include <netdb.h>
33 # include <fcntl.h>
34 #else
35
36 #endif
37 #include <errno.h>
38
39 #include "ginetaddress.h"
40 #include "ginet4address.h"
41 #include "ginet6address.h"
42 #include "gsocket.h"
43 #include "gnioerror.h"
44 #include "ginetsocketaddress.h"
45
46 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
47
48 struct _GSocketPrivate
49 {
50   int fd;
51 };
52
53 static void
54 g_socket_finalize (GObject *object)
55 {
56   GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
57
58   if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
59     (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
60 }
61
62 static void
63 g_socket_dispose (GObject *object)
64 {
65   GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
66
67   if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
68     (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
69 }
70
71 static void
72 g_socket_class_init (GSocketClass *klass)
73 {
74   GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
75
76   g_type_class_add_private (klass, sizeof (GSocketPrivate));
77
78   gobject_class->finalize = g_socket_finalize;
79   gobject_class->dispose = g_socket_dispose;
80 }
81
82 static void
83 g_socket_init (GSocket *address)
84 {
85   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address, G_TYPE_SOCKET, GSocketPrivate);
86 }
87
88 GSocket *
89 g_socket_new ()
90 {
91   return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
92 }
93
94 GSocket *
95 g_socket_new_from_fd (gint fd)
96 {
97   return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
98 }
99
100 void
101 g_socket_set_blocking (GSocket *socket, gboolean blocking)
102 {
103   glong arg;
104
105   g_return_if_fail (G_IS_SOCKET (socket));
106
107   if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
108     g_warning ("Error getting socket status flags: %s", g_strerror (errno));
109
110   arg = blocking ? arg | O_NONBLOCK : arg & ~O_NONBLOCK;
111
112   if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
113     g_warning ("Error setting socket status flags: %s", g_strerror (errno));
114 }
115
116 void
117 g_socket_listen (GSocket *socket, gint backlog)
118 {
119   g_return_if_fail (G_IS_SOCKET (socket));
120
121   listen (socket->priv->fd, backlog);
122 }
123
124 gboolean
125 g_socket_connect (GSocket         *socket,
126                   GSocketAddress  *address,
127                   GCancellable    *cancellable,
128                   GError         **error)
129 {
130   g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
131 /*
132   if (connect () < 0)
133     {
134       if (errno == EINPROGRESS)
135         g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
136       else
137         g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
138       return FALSE;
139     }
140 */
141   return TRUE;
142 }
143
144 void
145 g_socket_connect_async (GSocket             *socket,
146                         GSocketAddress      *address,
147                         GCancellable        *cancellable,
148                         GAsyncReadyCallback *callback,
149                         gpointer             user_data)
150 {
151
152 }
153
154 gboolean
155 g_socket_connect_finish (GSocket       *socket,
156                          GAsyncResult  *result,
157                          GError       **error)
158 {
159
160 }