Imported Upstream version 0.1.17
[platform/upstream/libnice.git] / socket / tcp-passive.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2008-2012 Collabora Ltd.
5  *  Contact: Youness Alaoui
6  * (C) 2008-2009 Nokia Corporation. All rights reserved.
7  *
8  * The contents of this file are subject to the Mozilla Public License Version
9  * 1.1 (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  * http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the
16  * License.
17  *
18  * The Original Code is the Nice GLib ICE library.
19  *
20  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21  * Corporation. All Rights Reserved.
22  *
23  * Contributors:
24  *   Youness Alaoui, Collabora Ltd.
25  *   George Kiagiadakis, Collabora Ltd.
26  *
27  * Alternatively, the contents of this file may be used under the terms of the
28  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
29  * case the provisions of LGPL are applicable instead of those above. If you
30  * wish to allow use of your version of this file only under the terms of the
31  * LGPL and not to allow others to use your version of this file under the
32  * MPL, indicate your decision by deleting the provisions above and replace
33  * them with the notice and other provisions required by the LGPL. If you do
34  * not delete the provisions above, a recipient may use your version of this
35  * file under either the MPL or the LGPL.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include "tcp-passive.h"
43 #include "agent-priv.h"
44
45 #include <string.h>
46 #include <errno.h>
47 #include <fcntl.h>
48
49 #ifndef G_OS_WIN32
50 #include <unistd.h>
51 #endif
52
53 /* FIXME: This should be defined in gio/gnetworking.h, which we should include;
54  * but we cannot do that without refactoring.
55  * (See: https://phabricator.freedesktop.org/D230). */
56 #undef TCP_NODELAY
57 #define TCP_NODELAY 1
58
59 typedef struct {
60   GMainContext *context;
61   GHashTable *connections;
62   NiceSocketWritableCb writable_cb;
63   gpointer writable_data;
64 } TcpPassivePriv;
65
66
67 static void socket_close (NiceSocket *sock);
68 static gint socket_recv_messages (NiceSocket *sock,
69     NiceInputMessage *recv_messages, guint n_recv_messages);
70 static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
71     const NiceOutputMessage *messages, guint n_messages);
72 static gint socket_send_messages_reliable (NiceSocket *sock,
73     const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages);
74 static gboolean socket_is_reliable (NiceSocket *sock);
75 static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
76 static void socket_set_writable_callback (NiceSocket *sock,
77     NiceSocketWritableCb callback, gpointer user_data);
78
79 static guint nice_address_hash (const NiceAddress * key);
80
81 NiceSocket *
82 nice_tcp_passive_socket_new (GMainContext *ctx, NiceAddress *addr)
83 {
84   union {
85     struct sockaddr_storage storage;
86     struct sockaddr addr;
87   } name;
88   NiceSocket *sock;
89   TcpPassivePriv *priv;
90   GSocket *gsock = NULL;
91   gboolean gret = FALSE;
92   GSocketAddress *gaddr;
93
94   if (addr != NULL) {
95     nice_address_copy_to_sockaddr(addr, &name.addr);
96   } else {
97     memset (&name, 0, sizeof (name));
98     name.storage.ss_family = AF_UNSPEC;
99   }
100
101   if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) {
102     gsock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM,
103         G_SOCKET_PROTOCOL_TCP, NULL);
104
105     name.storage.ss_family = AF_INET;
106 #ifdef HAVE_SA_LEN
107     name.storage.ss_len = sizeof (struct sockaddr_in);
108 #endif
109   } else if (name.storage.ss_family == AF_INET6) {
110     gsock = g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM,
111         G_SOCKET_PROTOCOL_TCP, NULL);
112     name.storage.ss_family = AF_INET6;
113 #ifdef HAVE_SA_LEN
114     name.storage.ss_len = sizeof (struct sockaddr_in6);
115 #endif
116   }
117
118   if (gsock == NULL) {
119     return NULL;
120   }
121
122   gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));
123
124   if (gaddr == NULL) {
125     g_object_unref (gsock);
126     return NULL;
127   }
128
129   /* GSocket: All socket file descriptors are set to be close-on-exec. */
130   g_socket_set_blocking (gsock, false);
131
132   gret = g_socket_bind (gsock, gaddr, FALSE, NULL) &&
133       g_socket_listen (gsock, NULL);
134   g_object_unref (gaddr);
135
136   if (gret == FALSE) {
137     g_socket_close (gsock, NULL);
138     g_object_unref (gsock);
139     return NULL;
140   }
141
142   gaddr = g_socket_get_local_address (gsock, NULL);
143   if (gaddr == NULL ||
144       !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
145     g_socket_close (gsock, NULL);
146     g_object_unref (gsock);
147     return NULL;
148   }
149   g_object_unref (gaddr);
150
151   if (ctx == NULL) {
152     ctx = g_main_context_default ();
153   }
154
155   sock = g_slice_new0 (NiceSocket);
156
157   nice_address_set_from_sockaddr (&sock->addr, &name.addr);
158
159   sock->priv = priv = g_slice_new0 (TcpPassivePriv);
160   priv->context = g_main_context_ref (ctx);
161   priv->connections = g_hash_table_new_full ((GHashFunc) nice_address_hash,
162       (GEqualFunc) nice_address_equal, (
163           GDestroyNotify) nice_address_free, NULL);
164   priv->writable_cb = NULL;
165   priv->writable_data = NULL;
166
167   sock->type = NICE_SOCKET_TYPE_TCP_PASSIVE;
168   sock->fileno = gsock;
169   sock->send_messages = socket_send_messages;
170   sock->send_messages_reliable = socket_send_messages_reliable;
171   sock->recv_messages = socket_recv_messages;
172   sock->is_reliable = socket_is_reliable;
173   sock->can_send = socket_can_send;
174   sock->set_writable_callback = socket_set_writable_callback;
175   sock->close = socket_close;
176
177   return sock;
178 }
179
180 static void
181 socket_close (NiceSocket *sock)
182 {
183   TcpPassivePriv *priv = sock->priv;
184
185   if (sock->fileno != NULL) {
186     g_socket_close (sock->fileno, NULL);
187     g_object_unref (sock->fileno);
188     sock->fileno = NULL;
189   }
190
191   if (priv->context)
192     g_main_context_unref (priv->context);
193   g_hash_table_unref (priv->connections);
194
195   g_slice_free (TcpPassivePriv, sock->priv);
196 }
197
198 static gint socket_recv_messages (NiceSocket *sock,
199     NiceInputMessage *recv_messages, guint n_recv_messages)
200 {
201   return -1;
202 }
203
204 static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
205     const NiceOutputMessage *messages, guint n_messages)
206 {
207   TcpPassivePriv *priv = sock->priv;
208
209   if (to) {
210     NiceSocket *peer_socket = g_hash_table_lookup (priv->connections, to);
211     if (peer_socket)
212       return nice_socket_send_messages (peer_socket, to, messages, n_messages);
213   }
214   return -1;
215 }
216
217 static gint socket_send_messages_reliable (NiceSocket *sock,
218     const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages)
219 {
220   TcpPassivePriv *priv = sock->priv;
221
222   if (to) {
223     NiceSocket *peer_socket = g_hash_table_lookup (priv->connections, to);
224     if (peer_socket)
225       return nice_socket_send_messages_reliable (peer_socket, to, messages,
226           n_messages);
227   }
228   return -1;
229 }
230
231 static gboolean
232 socket_is_reliable (NiceSocket *sock)
233 {
234   return TRUE;
235 }
236
237 static gboolean
238 socket_can_send (NiceSocket *sock, NiceAddress *addr)
239 {
240   TcpPassivePriv *priv = sock->priv;
241   NiceSocket *peer_socket = NULL;
242
243   /* FIXME: Danger if child socket was closed */
244   if (addr)
245     peer_socket = g_hash_table_lookup (priv->connections, addr);
246   if (peer_socket)
247     return nice_socket_can_send (peer_socket, addr);
248   return FALSE;
249 }
250
251 static void
252 _child_writable_cb (NiceSocket *child, gpointer data)
253 {
254   NiceSocket *sock = data;
255   TcpPassivePriv *priv = sock->priv;
256
257   if (priv->writable_cb)
258     priv->writable_cb (sock, priv->writable_data);
259 }
260
261 static void
262 socket_set_writable_callback (NiceSocket *sock,
263     NiceSocketWritableCb callback, gpointer user_data)
264 {
265   TcpPassivePriv *priv = sock->priv;
266
267   priv->writable_cb = callback;
268   priv->writable_data = user_data;
269 }
270
271 NiceSocket *
272 nice_tcp_passive_socket_accept (NiceSocket *sock)
273 {
274   union {
275     struct sockaddr_storage storage;
276     struct sockaddr addr;
277   } name;
278   TcpPassivePriv *priv = sock->priv;
279   GSocket *gsock = NULL;
280   GSocketAddress *gaddr;
281   NiceAddress remote_addr;
282   NiceSocket *new_socket = NULL;
283
284   gsock = g_socket_accept (sock->fileno, NULL, NULL);
285
286   if (gsock == NULL) {
287     return NULL;
288   }
289
290   /* GSocket: All socket file descriptors are set to be close-on-exec. */
291   g_socket_set_blocking (gsock, false);
292
293   /* setting TCP_NODELAY to TRUE in order to avoid packet batching */
294   g_socket_set_option (gsock, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);
295
296   gaddr = g_socket_get_remote_address (gsock, NULL);
297   if (gaddr == NULL ||
298       !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
299     g_socket_close (gsock, NULL);
300     g_object_unref (gsock);
301     return NULL;
302   }
303   g_object_unref (gaddr);
304
305   nice_address_set_from_sockaddr (&remote_addr, &name.addr);
306
307   new_socket = nice_tcp_bsd_socket_new_from_gsock (priv->context, gsock,
308       &sock->addr, &remote_addr, TRUE);
309   g_object_unref (gsock);
310
311   if (new_socket) {
312     NiceAddress *key = nice_address_dup (&remote_addr);
313
314     nice_tcp_bsd_socket_set_passive_parent (new_socket, sock);
315
316     nice_socket_set_writable_callback (new_socket, _child_writable_cb, sock);
317     g_hash_table_insert (priv->connections, key, new_socket);
318   }
319   return new_socket;
320 }
321
322 static guint nice_address_hash (const NiceAddress * key)
323 {
324   gchar ip[INET6_ADDRSTRLEN];
325   gchar *str;
326   guint hash;
327
328   nice_address_to_string (key, ip);
329   str = g_strdup_printf ("%s:%u", ip, nice_address_get_port (key));
330   hash = g_str_hash (str);
331   g_free (str);
332
333   return hash;
334 }
335
336 void nice_tcp_passive_socket_remove_connection (NiceSocket *sock, const NiceAddress *to)
337 {
338   TcpPassivePriv *priv = sock->priv;
339
340   g_hash_table_remove (priv->connections, to);
341 }