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.
43 #include "tcp-active.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 GSocketAddress *local_addr;
61 GMainContext *context;
65 static void socket_close (NiceSocket *sock);
66 static gint socket_recv_messages (NiceSocket *sock,
67 NiceInputMessage *recv_messages, guint n_recv_messages);
68 static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
69 const NiceOutputMessage *messages, guint n_messages);
70 static gint socket_send_messages_reliable (NiceSocket *sock,
71 const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages);
72 static gboolean socket_is_reliable (NiceSocket *sock);
73 static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
74 static void socket_set_writable_callback (NiceSocket *sock,
75 NiceSocketWritableCb callback, gpointer user_data);
79 nice_tcp_active_socket_new (GMainContext *ctx, NiceAddress *addr)
82 struct sockaddr_storage storage;
87 GSocketAddress *gaddr;
88 NiceAddress local_addr;
93 /* Make sure we don't bind to any local port */
94 nice_address_set_port (&local_addr, 0);
95 nice_address_copy_to_sockaddr(&local_addr, &name.addr);
97 memset (&local_addr, 0, sizeof (local_addr));
98 memset (&name, 0, sizeof (name));
99 name.storage.ss_family = AF_UNSPEC;
102 gaddr = g_socket_address_new_from_native (&name, sizeof (name));
109 ctx = g_main_context_default ();
112 sock = g_slice_new0 (NiceSocket);
114 sock->priv = priv = g_slice_new0 (TcpActivePriv);
116 priv->context = g_main_context_ref (ctx);
117 priv->local_addr = gaddr;
119 sock->type = NICE_SOCKET_TYPE_TCP_ACTIVE;
121 sock->addr = local_addr;
122 sock->send_messages = socket_send_messages;
123 sock->send_messages_reliable = socket_send_messages_reliable;
124 sock->recv_messages = socket_recv_messages;
125 sock->is_reliable = socket_is_reliable;
126 sock->can_send = socket_can_send;
127 sock->set_writable_callback = socket_set_writable_callback;
128 sock->close = socket_close;
134 socket_close (NiceSocket *sock)
136 TcpActivePriv *priv = sock->priv;
139 g_main_context_unref (priv->context);
140 if (priv->local_addr)
141 g_object_unref (priv->local_addr);
143 g_slice_free(TcpActivePriv, sock->priv);
146 static gint socket_recv_messages (NiceSocket *sock,
147 NiceInputMessage *recv_messages, guint n_recv_messages)
152 static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
153 const NiceOutputMessage *messages, guint n_messages)
158 static gint socket_send_messages_reliable (NiceSocket *sock,
159 const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages)
165 socket_is_reliable (NiceSocket *sock)
171 socket_can_send (NiceSocket *sock, NiceAddress *addr)
177 socket_set_writable_callback (NiceSocket *sock,
178 NiceSocketWritableCb callback, gpointer user_data)
183 nice_tcp_active_socket_connect (NiceSocket *sock, NiceAddress *addr)
186 struct sockaddr_storage storage;
187 struct sockaddr addr;
189 TcpActivePriv *priv = sock->priv;
190 GSocket *gsock = NULL;
192 gboolean gret = FALSE;
193 GSocketAddress *gaddr;
194 NiceAddress local_addr;
195 NiceSocket *new_socket = NULL;
198 /* We can't connect a tcp socket with no destination address */
202 nice_address_copy_to_sockaddr (addr, &name.addr);
204 if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) {
205 gsock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM,
206 G_SOCKET_PROTOCOL_TCP, NULL);
208 name.storage.ss_family = AF_INET;
210 name.storage.ss_len = sizeof (struct sockaddr_in);
212 } else if (name.storage.ss_family == AF_INET6) {
213 gsock = g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM,
214 G_SOCKET_PROTOCOL_TCP, NULL);
215 name.storage.ss_family = AF_INET6;
217 name.storage.ss_len = sizeof (struct sockaddr_in6);
225 gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));
227 g_object_unref (gsock);
231 /* GSocket: All socket file descriptors are set to be close-on-exec. */
232 g_socket_set_blocking (gsock, false);
234 /* setting TCP_NODELAY to TRUE in order to avoid packet batching */
235 g_socket_set_option (gsock, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);
237 /* Allow g_socket_bind to fail */
238 g_socket_bind (gsock, priv->local_addr, FALSE, NULL);
240 gret = g_socket_connect (gsock, gaddr, NULL, &gerr);
241 g_object_unref (gaddr);
244 if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_PENDING) == FALSE) {
246 g_socket_close (gsock, NULL);
247 g_object_unref (gsock);
253 gaddr = g_socket_get_local_address (gsock, NULL);
255 !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
256 g_socket_close (gsock, NULL);
257 g_object_unref (gsock);
260 g_object_unref (gaddr);
262 nice_address_set_from_sockaddr (&local_addr, &name.addr);
264 new_socket = nice_tcp_bsd_socket_new_from_gsock (priv->context, gsock,
265 &local_addr, addr, TRUE);
266 g_object_unref (gsock);