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 "socket.h"
43 #include "tcp-active.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   GSocketAddress *local_addr;
61   GMainContext *context;
62 } TcpActivePriv;
63 
64 
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);
76 
77 
78 NiceSocket *
nice_tcp_active_socket_new(GMainContext * ctx,NiceAddress * addr)79 nice_tcp_active_socket_new (GMainContext *ctx, NiceAddress *addr)
80 {
81   union {
82     struct sockaddr_storage storage;
83     struct sockaddr addr;
84   } name;
85   NiceSocket *sock;
86   TcpActivePriv *priv;
87   GSocketAddress *gaddr;
88   NiceAddress local_addr;
89 
90   if (addr != NULL) {
91 
92     local_addr = *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);
96   } else {
97     memset (&local_addr, 0, sizeof (local_addr));
98     memset (&name, 0, sizeof (name));
99     name.storage.ss_family = AF_UNSPEC;
100   }
101 
102   gaddr = g_socket_address_new_from_native (&name, sizeof (name));
103 
104   if (gaddr == NULL) {
105     return NULL;
106   }
107 
108   if (ctx == NULL) {
109     ctx = g_main_context_default ();
110   }
111 
112   sock = g_slice_new0 (NiceSocket);
113 
114   sock->priv = priv = g_slice_new0 (TcpActivePriv);
115 
116   priv->context = g_main_context_ref (ctx);
117   priv->local_addr = gaddr;
118 
119   sock->type = NICE_SOCKET_TYPE_TCP_ACTIVE;
120   sock->fileno = NULL;
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;
129 
130   return sock;
131 }
132 
133 static void
socket_close(NiceSocket * sock)134 socket_close (NiceSocket *sock)
135 {
136   TcpActivePriv *priv = sock->priv;
137 
138   if (priv->context)
139     g_main_context_unref (priv->context);
140   if (priv->local_addr)
141     g_object_unref (priv->local_addr);
142 
143   g_slice_free(TcpActivePriv, sock->priv);
144 }
145 
socket_recv_messages(NiceSocket * sock,NiceInputMessage * recv_messages,guint n_recv_messages)146 static gint socket_recv_messages (NiceSocket *sock,
147     NiceInputMessage *recv_messages, guint n_recv_messages)
148 {
149   return -1;
150 }
151 
socket_send_messages(NiceSocket * sock,const NiceAddress * to,const NiceOutputMessage * messages,guint n_messages)152 static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
153     const NiceOutputMessage *messages, guint n_messages)
154 {
155   return -1;
156 }
157 
socket_send_messages_reliable(NiceSocket * sock,const NiceAddress * to,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)
160 {
161   return -1;
162 }
163 
164 static gboolean
socket_is_reliable(NiceSocket * sock)165 socket_is_reliable (NiceSocket *sock)
166 {
167   return TRUE;
168 }
169 
170 static gboolean
socket_can_send(NiceSocket * sock,NiceAddress * addr)171 socket_can_send (NiceSocket *sock, NiceAddress *addr)
172 {
173   return FALSE;
174 }
175 
176 static void
socket_set_writable_callback(NiceSocket * sock,NiceSocketWritableCb callback,gpointer user_data)177 socket_set_writable_callback (NiceSocket *sock,
178     NiceSocketWritableCb callback, gpointer user_data)
179 {
180 }
181 
182 NiceSocket *
nice_tcp_active_socket_connect(NiceSocket * sock,NiceAddress * addr)183 nice_tcp_active_socket_connect (NiceSocket *sock, NiceAddress *addr)
184 {
185   union {
186     struct sockaddr_storage storage;
187     struct sockaddr addr;
188   } name;
189   TcpActivePriv *priv = sock->priv;
190   GSocket *gsock = NULL;
191   GError *gerr = NULL;
192   gboolean gret = FALSE;
193   GSocketAddress *gaddr;
194   NiceAddress local_addr;
195   NiceSocket *new_socket = NULL;
196 
197   if (addr == NULL) {
198     /* We can't connect a tcp socket with no destination address */
199     return NULL;
200   }
201 
202   nice_address_copy_to_sockaddr (addr, &name.addr);
203 
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);
207 
208     name.storage.ss_family = AF_INET;
209 #ifdef HAVE_SA_LEN
210     name.storage.ss_len = sizeof (struct sockaddr_in);
211 #endif
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;
216 #ifdef HAVE_SA_LEN
217     name.storage.ss_len = sizeof (struct sockaddr_in6);
218 #endif
219   }
220 
221   if (gsock == NULL) {
222     return NULL;
223   }
224 
225   gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));
226   if (gaddr == NULL) {
227     g_object_unref (gsock);
228     return NULL;
229   }
230 
231   /* GSocket: All socket file descriptors are set to be close-on-exec. */
232   g_socket_set_blocking (gsock, false);
233 
234   /* setting TCP_NODELAY to TRUE in order to avoid packet batching */
235   g_socket_set_option (gsock, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);
236 
237   /* Allow g_socket_bind to fail */
238   g_socket_bind (gsock, priv->local_addr, FALSE, NULL);
239 
240   gret = g_socket_connect (gsock, gaddr, NULL, &gerr);
241   g_object_unref (gaddr);
242 
243   if (gret == FALSE) {
244     if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_PENDING) == FALSE) {
245       g_error_free (gerr);
246       g_socket_close (gsock, NULL);
247       g_object_unref (gsock);
248       return NULL;
249     }
250     g_error_free (gerr);
251   }
252 
253   gaddr = g_socket_get_local_address (gsock, NULL);
254   if (gaddr == NULL ||
255       !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
256     g_socket_close (gsock, NULL);
257     g_object_unref (gsock);
258     return NULL;
259   }
260   g_object_unref (gaddr);
261 
262   nice_address_set_from_sockaddr (&local_addr, &name.addr);
263 
264   new_socket = nice_tcp_bsd_socket_new_from_gsock (priv->context, gsock,
265       &local_addr, addr, TRUE);
266   g_object_unref (gsock);
267 
268   return new_socket;
269 }
270