2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008-2009 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.
26 * Alternatively, the contents of this file may be used under the terms of the
27 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
28 * case the provisions of LGPL are applicable instead of those above. If you
29 * wish to allow use of your version of this file only under the terms of the
30 * LGPL and not to allow others to use your version of this file under the
31 * MPL, indicate your decision by deleting the provisions above and replace
32 * them with the notice and other provisions required by the LGPL. If you do
33 * not delete the provisions above, a recipient may use your version of this
34 * file under either the MPL or the LGPL.
38 * Implementation of TCP relay socket interface using TCP Berkeley sockets. (See
39 * http://en.wikipedia.org/wiki/Berkeley_sockets.)
46 #include "agent-priv.h"
47 #include "socket-priv.h"
49 #include "tcp-passive.h"
59 /* FIXME: This should be defined in gio/gnetworking.h, which we should include;
60 * but we cannot do that without refactoring.
61 * (See: https://phabricator.freedesktop.org/D230). */
68 NiceAddress remote_addr;
70 GMainContext *context;
74 NiceSocketWritableCb writable_cb;
75 gpointer writable_data;
76 NiceSocket *passive_parent;
79 #define MAX_QUEUE_LENGTH 20
81 static void socket_close (NiceSocket *sock);
82 static gint socket_recv_messages (NiceSocket *sock,
83 NiceInputMessage *recv_messages, guint n_recv_messages);
84 static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
85 const NiceOutputMessage *messages, guint n_messages);
86 static gint socket_send_messages_reliable (NiceSocket *sock,
87 const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages);
88 static gboolean socket_is_reliable (NiceSocket *sock);
89 static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
90 static void socket_set_writable_callback (NiceSocket *sock,
91 NiceSocketWritableCb callback, gpointer user_data);
93 static gboolean socket_send_more (GSocket *gsocket, GIOCondition condition,
97 nice_tcp_bsd_socket_new_from_gsock (GMainContext *ctx, GSocket *gsock,
98 NiceAddress *local_addr, NiceAddress *remote_addr, gboolean reliable)
103 g_return_val_if_fail (G_IS_SOCKET (gsock), NULL);
105 sock = g_slice_new0 (NiceSocket);
106 sock->priv = priv = g_slice_new0 (TcpPriv);
109 ctx = g_main_context_default ();
110 priv->context = g_main_context_ref (ctx);
111 priv->remote_addr = *remote_addr;
113 priv->reliable = reliable;
114 priv->writable_cb = NULL;
115 priv->writable_data = NULL;
117 sock->type = NICE_SOCKET_TYPE_TCP_BSD;
118 sock->fileno = g_object_ref (gsock);
119 sock->addr = *local_addr;
120 sock->send_messages = socket_send_messages;
121 sock->send_messages_reliable = socket_send_messages_reliable;
122 sock->recv_messages = socket_recv_messages;
123 sock->is_reliable = socket_is_reliable;
124 sock->can_send = socket_can_send;
125 sock->set_writable_callback = socket_set_writable_callback;
126 sock->close = socket_close;
132 nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *local_addr,
133 NiceAddress *remote_addr, gboolean reliable)
136 struct sockaddr_storage storage;
137 struct sockaddr addr;
140 GSocket *gsock = NULL;
142 gboolean gret = FALSE;
143 GSocketAddress *gaddr;
145 if (remote_addr == NULL) {
146 /* We can't connect a tcp socket with no destination address */
150 nice_address_copy_to_sockaddr (remote_addr, &name.addr);
152 if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) {
153 gsock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM,
154 G_SOCKET_PROTOCOL_TCP, NULL);
156 name.storage.ss_family = AF_INET;
158 name.storage.ss_len = sizeof (struct sockaddr_in);
160 } else if (name.storage.ss_family == AF_INET6) {
161 gsock = g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM,
162 G_SOCKET_PROTOCOL_TCP, NULL);
163 name.storage.ss_family = AF_INET6;
165 name.storage.ss_len = sizeof (struct sockaddr_in6);
173 gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));
175 g_object_unref (gsock);
179 /* GSocket: All socket file descriptors are set to be close-on-exec. */
180 g_socket_set_blocking (gsock, false);
182 /* setting TCP_NODELAY to TRUE in order to avoid packet batching */
183 g_socket_set_option (gsock, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);
185 gret = g_socket_connect (gsock, gaddr, NULL, &gerr);
186 g_object_unref (gaddr);
189 if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_PENDING) == FALSE) {
191 g_socket_close (gsock, NULL);
192 g_object_unref (gsock);
198 nice_address_copy_to_sockaddr (local_addr, &name.addr);
199 gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));
201 g_socket_close (gsock, NULL);
202 g_object_unref (gsock);
205 g_socket_bind (gsock, gaddr, FALSE, NULL);
206 g_object_unref (gaddr);
208 sock = nice_tcp_bsd_socket_new_from_gsock (ctx, gsock, local_addr, remote_addr,
210 g_object_unref (gsock);
217 socket_close (NiceSocket *sock)
219 TcpPriv *priv = sock->priv;
221 g_mutex_lock (&mutex);
224 g_socket_close (sock->fileno, NULL);
225 g_object_unref (sock->fileno);
228 if (priv->io_source) {
229 g_source_destroy (priv->io_source);
230 g_source_unref (priv->io_source);
233 if (priv->passive_parent) {
234 nice_tcp_passive_socket_remove_connection (priv->passive_parent, &priv->remote_addr);
237 nice_socket_free_send_queue (&priv->send_queue);
240 g_main_context_unref (priv->context);
242 g_mutex_unlock (&mutex);
244 g_slice_free(TcpPriv, sock->priv);
248 socket_recv_messages (NiceSocket *sock,
249 NiceInputMessage *recv_messages, guint n_recv_messages)
251 TcpPriv *priv = sock->priv;
254 /* Make sure socket has not been freed: */
255 g_assert (sock->priv != NULL);
257 /* Don't try to access the socket if it had an error */
261 for (i = 0; i < n_recv_messages; i++) {
262 gint flags = G_SOCKET_MSG_NONE;
266 len = g_socket_receive_message (sock->fileno, NULL,
267 recv_messages[i].buffers, recv_messages[i].n_buffers,
268 NULL, NULL, &flags, NULL, &gerr);
270 recv_messages[i].length = MAX (len, 0);
272 /* recv returns 0 when the peer performed a shutdown.. we must return -1
273 * here so that the agent destroys the g_source */
280 if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
287 if (recv_messages[i].from)
288 *recv_messages[i].from = priv->remote_addr;
291 /* Was there an error processing the first message? */
292 if (priv->error && i == 0)
299 socket_send_message (NiceSocket *sock,
300 const NiceOutputMessage *message, gboolean reliable)
302 TcpPriv *priv = sock->priv;
307 /* Make sure socket has not been freed: */
308 g_assert (sock->priv != NULL);
310 /* Don't try to access the socket if it had an error, otherwise we risk a
311 * crash with SIGPIPE (Broken pipe) */
315 message_len = output_message_get_size (message);
317 /* First try to send the data, don't send it later if it can be sent now
318 * this way we avoid allocating memory on every send */
319 if (g_queue_is_empty (&priv->send_queue)) {
320 ret = g_socket_send_message (sock->fileno, NULL, message->buffers,
321 message->n_buffers, NULL, 0, G_SOCKET_MSG_NONE, NULL, &gerr);
324 if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK) ||
325 g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_FAILED)) {
326 /* Queue the message and send it later. */
327 nice_socket_queue_send_with_callback (&priv->send_queue,
328 message, 0, message_len, FALSE, sock->fileno, &priv->io_source,
329 priv->context, socket_send_more, sock);
334 } else if ((gsize) ret < message_len) {
336 nice_socket_queue_send_with_callback (&priv->send_queue,
337 message, ret, message_len, TRUE, sock->fileno, &priv->io_source,
338 priv->context, socket_send_more, sock);
342 /* Only queue if we're sending reliably */
344 /* Queue the message and send it later. */
345 nice_socket_queue_send_with_callback (&priv->send_queue,
346 message, 0, message_len, FALSE, sock->fileno, &priv->io_source,
347 priv->context, socket_send_more, sock);
350 /* non reliable send, so we shouldn't queue the message */
358 /* Data sent to this function must be a single entity because buffers can be
359 * dropped if the bandwidth isn't fast enough. So do not send a message in
360 * multiple chunks. */
362 socket_send_messages (NiceSocket *sock, const NiceAddress *to,
363 const NiceOutputMessage *messages, guint n_messages)
367 /* Make sure socket has not been freed: */
368 g_assert (sock->priv != NULL);
370 for (i = 0; i < n_messages; i++) {
371 const NiceOutputMessage *message = &messages[i];
374 len = socket_send_message (sock, message, FALSE);
381 } else if (len == 0) {
391 socket_send_messages_reliable (NiceSocket *sock, const NiceAddress *to,
392 const NiceOutputMessage *messages, guint n_messages)
396 for (i = 0; i < n_messages; i++) {
397 if (socket_send_message (sock, &messages[i], TRUE) < 0) {
407 socket_is_reliable (NiceSocket *sock)
409 TcpPriv *priv = sock->priv;
411 return priv->reliable;
415 socket_can_send (NiceSocket *sock, NiceAddress *addr)
417 TcpPriv *priv = sock->priv;
419 return g_queue_is_empty (&priv->send_queue);
423 socket_set_writable_callback (NiceSocket *sock,
424 NiceSocketWritableCb callback, gpointer user_data)
426 TcpPriv *priv = sock->priv;
428 priv->writable_cb = callback;
429 priv->writable_data = user_data;
435 GIOCondition condition,
438 NiceSocket *sock = (NiceSocket *) data;
441 g_mutex_lock (&mutex);
443 if (g_source_is_destroyed (g_main_current_source ())) {
444 nice_debug ("Source was destroyed. "
445 "Avoided race condition in tcp-bsd.c:socket_send_more");
446 g_mutex_unlock (&mutex);
452 /* connection hangs up or queue was emptied */
453 if (condition & G_IO_HUP ||
454 nice_socket_flush_send_queue_to_socket (sock->fileno,
455 &priv->send_queue)) {
456 g_source_destroy (priv->io_source);
457 g_source_unref (priv->io_source);
458 priv->io_source = NULL;
460 g_mutex_unlock (&mutex);
462 if (priv->writable_cb)
463 priv->writable_cb (sock, priv->writable_data);
468 g_mutex_unlock (&mutex);
473 nice_tcp_bsd_socket_set_passive_parent (NiceSocket *sock, NiceSocket *passive_parent)
475 TcpPriv *priv = sock->priv;
477 g_assert (priv->passive_parent == NULL);
479 priv->passive_parent = passive_parent;
483 nice_tcp_bsd_socket_get_passive_parent (NiceSocket *sock)
485 TcpPriv *priv = sock->priv;
487 return priv->passive_parent;