tcp-passive.c 9.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2008-2012 Collabora Ltd.
 *  Contact: Youness Alaoui
 * (C) 2008-2009 Nokia Corporation. All rights reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Youness Alaoui, Collabora Ltd.
 *   George Kiagiadakis, Collabora Ltd.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "tcp-passive.h"
#include "agent-priv.h"

#include <string.h>
#include <errno.h>
#include <fcntl.h>

#ifndef G_OS_WIN32
#include <unistd.h>
#endif

53 54 55
/* FIXME: This should be defined in gio/gnetworking.h, which we should include;
 * but we cannot do that without refactoring.
 * (See: https://phabricator.freedesktop.org/D230). */
Nirbheek Chauhan's avatar
Nirbheek Chauhan committed
56
#undef TCP_NODELAY
57 58
#define TCP_NODELAY 1

59 60
typedef struct {
  GMainContext *context;
61
  GHashTable *connections;
62 63
  NiceSocketWritableCb writable_cb;
  gpointer writable_data;
64 65 66 67 68 69 70 71 72 73 74
} TcpPassivePriv;


static void socket_close (NiceSocket *sock);
static gint socket_recv_messages (NiceSocket *sock,
    NiceInputMessage *recv_messages, guint n_recv_messages);
static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
    const NiceOutputMessage *messages, guint n_messages);
static gint socket_send_messages_reliable (NiceSocket *sock,
    const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages);
static gboolean socket_is_reliable (NiceSocket *sock);
75 76 77 78
static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
static void socket_set_writable_callback (NiceSocket *sock,
    NiceSocketWritableCb callback, gpointer user_data);

79
static guint nice_address_hash (const NiceAddress * key);
80 81

NiceSocket *
82
nice_tcp_passive_socket_new (GMainContext *ctx, NiceAddress *addr)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
  union {
    struct sockaddr_storage storage;
    struct sockaddr addr;
  } name;
  NiceSocket *sock;
  TcpPassivePriv *priv;
  GSocket *gsock = NULL;
  gboolean gret = FALSE;
  GSocketAddress *gaddr;

  if (addr != NULL) {
    nice_address_copy_to_sockaddr(addr, &name.addr);
  } else {
    memset (&name, 0, sizeof (name));
    name.storage.ss_family = AF_UNSPEC;
  }

  if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) {
    gsock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM,
        G_SOCKET_PROTOCOL_TCP, NULL);

    name.storage.ss_family = AF_INET;
#ifdef HAVE_SA_LEN
    name.storage.ss_len = sizeof (struct sockaddr_in);
#endif
  } else if (name.storage.ss_family == AF_INET6) {
    gsock = g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM,
        G_SOCKET_PROTOCOL_TCP, NULL);
    name.storage.ss_family = AF_INET6;
#ifdef HAVE_SA_LEN
    name.storage.ss_len = sizeof (struct sockaddr_in6);
#endif
  }

  if (gsock == NULL) {
    return NULL;
  }

  gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));

  if (gaddr == NULL) {
    g_object_unref (gsock);
    return NULL;
  }

  /* GSocket: All socket file descriptors are set to be close-on-exec. */
  g_socket_set_blocking (gsock, false);

  gret = g_socket_bind (gsock, gaddr, FALSE, NULL) &&
      g_socket_listen (gsock, NULL);
  g_object_unref (gaddr);

  if (gret == FALSE) {
    g_socket_close (gsock, NULL);
    g_object_unref (gsock);
    return NULL;
  }

  gaddr = g_socket_get_local_address (gsock, NULL);
  if (gaddr == NULL ||
      !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
    g_socket_close (gsock, NULL);
    g_object_unref (gsock);
    return NULL;
  }
  g_object_unref (gaddr);

151 152 153 154
  if (ctx == NULL) {
    ctx = g_main_context_default ();
  }

155 156 157 158 159 160
  sock = g_slice_new0 (NiceSocket);

  nice_address_set_from_sockaddr (&sock->addr, &name.addr);

  sock->priv = priv = g_slice_new0 (TcpPassivePriv);
  priv->context = g_main_context_ref (ctx);
161 162 163
  priv->connections = g_hash_table_new_full ((GHashFunc) nice_address_hash,
      (GEqualFunc) nice_address_equal, (
          GDestroyNotify) nice_address_free, NULL);
164 165
  priv->writable_cb = NULL;
  priv->writable_data = NULL;
166 167 168 169 170 171 172

  sock->type = NICE_SOCKET_TYPE_TCP_PASSIVE;
  sock->fileno = gsock;
  sock->send_messages = socket_send_messages;
  sock->send_messages_reliable = socket_send_messages_reliable;
  sock->recv_messages = socket_recv_messages;
  sock->is_reliable = socket_is_reliable;
173 174
  sock->can_send = socket_can_send;
  sock->set_writable_callback = socket_set_writable_callback;
175 176 177 178 179 180 181 182 183 184
  sock->close = socket_close;

  return sock;
}

static void
socket_close (NiceSocket *sock)
{
  TcpPassivePriv *priv = sock->priv;

185 186 187 188 189 190
  if (sock->fileno != NULL) {
    g_socket_close (sock->fileno, NULL);
    g_object_unref (sock->fileno);
    sock->fileno = NULL;
  }

191 192
  if (priv->context)
    g_main_context_unref (priv->context);
193
  g_hash_table_unref (priv->connections);
194 195 196 197 198 199 200 201 202 203 204 205 206

  g_slice_free (TcpPassivePriv, sock->priv);
}

static gint socket_recv_messages (NiceSocket *sock,
    NiceInputMessage *recv_messages, guint n_recv_messages)
{
  return -1;
}

static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
    const NiceOutputMessage *messages, guint n_messages)
{
207 208 209 210 211 212 213
  TcpPassivePriv *priv = sock->priv;

  if (to) {
    NiceSocket *peer_socket = g_hash_table_lookup (priv->connections, to);
    if (peer_socket)
      return nice_socket_send_messages (peer_socket, to, messages, n_messages);
  }
214 215 216 217 218 219
  return -1;
}

static gint socket_send_messages_reliable (NiceSocket *sock,
    const NiceAddress *to, const NiceOutputMessage *messages, guint n_messages)
{
220 221 222 223 224 225 226 227
  TcpPassivePriv *priv = sock->priv;

  if (to) {
    NiceSocket *peer_socket = g_hash_table_lookup (priv->connections, to);
    if (peer_socket)
      return nice_socket_send_messages_reliable (peer_socket, to, messages,
          n_messages);
  }
228 229 230 231 232 233
  return -1;
}

static gboolean
socket_is_reliable (NiceSocket *sock)
{
234
  return TRUE;
235 236
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
static gboolean
socket_can_send (NiceSocket *sock, NiceAddress *addr)
{
  TcpPassivePriv *priv = sock->priv;
  NiceSocket *peer_socket = NULL;

  /* FIXME: Danger if child socket was closed */
  if (addr)
    peer_socket = g_hash_table_lookup (priv->connections, addr);
  if (peer_socket)
    return nice_socket_can_send (peer_socket, addr);
  return FALSE;
}

static void
252
_child_writable_cb (NiceSocket *child, gpointer data)
253
{
254
  NiceSocket *sock = data;
255 256
  TcpPassivePriv *priv = sock->priv;

257 258
  if (priv->writable_cb)
    priv->writable_cb (sock, priv->writable_data);
259 260 261 262 263 264 265 266 267 268 269 270
}

static void
socket_set_writable_callback (NiceSocket *sock,
    NiceSocketWritableCb callback, gpointer user_data)
{
  TcpPassivePriv *priv = sock->priv;

  priv->writable_cb = callback;
  priv->writable_data = user_data;
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
NiceSocket *
nice_tcp_passive_socket_accept (NiceSocket *sock)
{
  union {
    struct sockaddr_storage storage;
    struct sockaddr addr;
  } name;
  TcpPassivePriv *priv = sock->priv;
  GSocket *gsock = NULL;
  GSocketAddress *gaddr;
  NiceAddress remote_addr;
  NiceSocket *new_socket = NULL;

  gsock = g_socket_accept (sock->fileno, NULL, NULL);

  if (gsock == NULL) {
    return NULL;
  }

  /* GSocket: All socket file descriptors are set to be close-on-exec. */
  g_socket_set_blocking (gsock, false);

293 294 295
  /* setting TCP_NODELAY to TRUE in order to avoid packet batching */
  g_socket_set_option (gsock, IPPROTO_TCP, TCP_NODELAY, TRUE, NULL);

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  gaddr = g_socket_get_remote_address (gsock, NULL);
  if (gaddr == NULL ||
      !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
    g_socket_close (gsock, NULL);
    g_object_unref (gsock);
    return NULL;
  }
  g_object_unref (gaddr);

  nice_address_set_from_sockaddr (&remote_addr, &name.addr);

  new_socket = nice_tcp_bsd_socket_new_from_gsock (priv->context, gsock,
      &sock->addr, &remote_addr, TRUE);
  g_object_unref (gsock);

311 312 313
  if (new_socket) {
    NiceAddress *key = nice_address_dup (&remote_addr);

314 315
    nice_tcp_bsd_socket_set_passive_parent (new_socket, sock);

316
    nice_socket_set_writable_callback (new_socket, _child_writable_cb, sock);
317 318
    g_hash_table_insert (priv->connections, key, new_socket);
  }
319 320
  return new_socket;
}
321 322 323 324 325 326 327 328 329 330 331 332 333 334

static guint nice_address_hash (const NiceAddress * key)
{
  gchar ip[INET6_ADDRSTRLEN];
  gchar *str;
  guint hash;

  nice_address_to_string (key, ip);
  str = g_strdup_printf ("%s:%u", ip, nice_address_get_port (key));
  hash = g_str_hash (str);
  g_free (str);

  return hash;
}
335 336 337 338 339 340 341

void nice_tcp_passive_socket_remove_connection (NiceSocket *sock, const NiceAddress *to)
{
  TcpPassivePriv *priv = sock->priv;

  g_hash_table_remove (priv->connections, to);
}