2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008-2012 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2008-2009 Nokia Corporation. All rights reserved.
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/
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
18 * The Original Code is the Nice GLib ICE library.
20 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21 * Corporation. All Rights Reserved.
24 * Youness Alaoui, Collabora Ltd.
25 * George Kiagiadakis, Collabora Ltd.
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.
42 #include "tcp-passive.h"
43 #include "agent-priv.h"
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). */
60 GMainContext *context;
61 GHashTable *connections;
62 NiceSocketWritableCb writable_cb;
63 gpointer writable_data;
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);
79 static guint nice_address_hash (const NiceAddress * key);
82 nice_tcp_passive_socket_new (GMainContext *ctx, NiceAddress *addr)
85 struct sockaddr_storage storage;
90 GSocket *gsock = NULL;
91 gboolean gret = FALSE;
92 GSocketAddress *gaddr;
95 nice_address_copy_to_sockaddr(addr, &name.addr);
97 memset (&name, 0, sizeof (name));
98 name.storage.ss_family = AF_UNSPEC;
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);
105 name.storage.ss_family = AF_INET;
107 name.storage.ss_len = sizeof (struct sockaddr_in);
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;
114 name.storage.ss_len = sizeof (struct sockaddr_in6);
122 gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));
125 g_object_unref (gsock);
129 /* GSocket: All socket file descriptors are set to be close-on-exec. */
130 g_socket_set_blocking (gsock, false);
132 gret = g_socket_bind (gsock, gaddr, FALSE, NULL) &&
133 g_socket_listen (gsock, NULL);
134 g_object_unref (gaddr);
137 g_socket_close (gsock, NULL);
138 g_object_unref (gsock);
142 gaddr = g_socket_get_local_address (gsock, NULL);
144 !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
145 g_socket_close (gsock, NULL);
146 g_object_unref (gsock);
149 g_object_unref (gaddr);
152 ctx = g_main_context_default ();
155 sock = g_slice_new0 (NiceSocket);
157 nice_address_set_from_sockaddr (&sock->addr, &name.addr);
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;
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;
181 socket_close (NiceSocket *sock)
183 TcpPassivePriv *priv = sock->priv;
185 if (sock->fileno != NULL) {
186 g_socket_close (sock->fileno, NULL);
187 g_object_unref (sock->fileno);
192 g_main_context_unref (priv->context);
193 g_hash_table_unref (priv->connections);
195 g_slice_free (TcpPassivePriv, sock->priv);
198 static gint socket_recv_messages (NiceSocket *sock,
199 NiceInputMessage *recv_messages, guint n_recv_messages)
204 static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
205 const NiceOutputMessage *messages, guint n_messages)
207 TcpPassivePriv *priv = sock->priv;
210 NiceSocket *peer_socket = g_hash_table_lookup (priv->connections, to);
212 return nice_socket_send_messages (peer_socket, to, messages, n_messages);
217 static gint socket_send_messages_reliable (NiceSocket *sock,
218 const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages)
220 TcpPassivePriv *priv = sock->priv;
223 NiceSocket *peer_socket = g_hash_table_lookup (priv->connections, to);
225 return nice_socket_send_messages_reliable (peer_socket, to, messages,
232 socket_is_reliable (NiceSocket *sock)
238 socket_can_send (NiceSocket *sock, NiceAddress *addr)
240 TcpPassivePriv *priv = sock->priv;
241 NiceSocket *peer_socket = NULL;
243 /* FIXME: Danger if child socket was closed */
245 peer_socket = g_hash_table_lookup (priv->connections, addr);
247 return nice_socket_can_send (peer_socket, addr);
252 _child_writable_cb (NiceSocket *child, gpointer data)
254 NiceSocket *sock = data;
255 TcpPassivePriv *priv = sock->priv;
257 if (priv->writable_cb)
258 priv->writable_cb (sock, priv->writable_data);
262 socket_set_writable_callback (NiceSocket *sock,
263 NiceSocketWritableCb callback, gpointer user_data)
265 TcpPassivePriv *priv = sock->priv;
267 priv->writable_cb = callback;
268 priv->writable_data = user_data;
272 nice_tcp_passive_socket_accept (NiceSocket *sock)
275 struct sockaddr_storage storage;
276 struct sockaddr addr;
278 TcpPassivePriv *priv = sock->priv;
279 GSocket *gsock = NULL;
280 GSocketAddress *gaddr;
281 NiceAddress remote_addr;
282 NiceSocket *new_socket = NULL;
284 gsock = g_socket_accept (sock->fileno, NULL, NULL);
290 /* GSocket: All socket file descriptors are set to be close-on-exec. */
291 g_socket_set_blocking (gsock, false);
293 /* setting TCP_NODELAY to TRUE in order to avoid packet batching */
294 g_socket_set_option (gsock, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);
296 gaddr = g_socket_get_remote_address (gsock, NULL);
298 !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
299 g_socket_close (gsock, NULL);
300 g_object_unref (gsock);
303 g_object_unref (gaddr);
305 nice_address_set_from_sockaddr (&remote_addr, &name.addr);
307 new_socket = nice_tcp_bsd_socket_new_from_gsock (priv->context, gsock,
308 &sock->addr, &remote_addr, TRUE);
309 g_object_unref (gsock);
312 NiceAddress *key = nice_address_dup (&remote_addr);
314 nice_tcp_bsd_socket_set_passive_parent (new_socket, sock);
316 nice_socket_set_writable_callback (new_socket, _child_writable_cb, sock);
317 g_hash_table_insert (priv->connections, key, new_socket);
322 static guint nice_address_hash (const NiceAddress * key)
324 gchar ip[INET6_ADDRSTRLEN];
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);
336 void nice_tcp_passive_socket_remove_connection (NiceSocket *sock, const NiceAddress *to)
338 TcpPassivePriv *priv = sock->priv;
340 g_hash_table_remove (priv->connections, to);