tcp-turn.c 8.69 KB
Newer Older
1 2 3
/*
 * This file is part of the Nice GLib ICE library.
 *
4 5 6
 * (C) 2008-2009 Collabora Ltd.
 *  Contact: Youness Alaoui
 * (C) 2008-2009 Nokia Corporation. All rights reserved.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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:
24
 *   Youness Alaoui, Collabora Ltd.
25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * 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.
 */

/*
38
 * Implementation of TCP relay socket interface using TCP Berkeley sockets. (See
39 40 41 42 43 44
 * http://en.wikipedia.org/wiki/Berkeley_sockets.)
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

45
#include "tcp-turn.h"
46
#include "agent-priv.h"
47

48 49 50 51
#include <string.h>
#include <errno.h>
#include <fcntl.h>

52 53 54
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
55 56

typedef struct {
57
  NiceTurnSocketCompatibility compatibility;
58 59 60 61 62
  union {
    guint8 u8[65536];
    guint16 u16[32768];
  } recv_buf;
  gsize recv_buf_len;  /* in bytes */
63
  guint expecting_len;
64
  NiceSocket *base_socket;
65 66
} TurnTcpPriv;

67
#define MAX_UDP_MESSAGE_SIZE 65535
68

69
static void socket_close (NiceSocket *sock);
70 71
static gint socket_recv_messages (NiceSocket *sock,
    NiceInputMessage *recv_messages, guint n_recv_messages);
72
static gint socket_send_messages (NiceSocket *sock, const NiceAddress *to,
73
    const NiceOutputMessage *messages, guint n_messages);
74 75 76
static gboolean socket_is_reliable (NiceSocket *sock);

NiceSocket *
77
nice_tcp_turn_socket_new (NiceSocket *base_socket,
78 79 80 81 82 83 84 85 86 87 88
    NiceTurnSocketCompatibility compatibility)
{
  TurnTcpPriv *priv;
  NiceSocket *sock = g_slice_new0 (NiceSocket);
  sock->priv = priv = g_slice_new0 (TurnTcpPriv);

  priv->compatibility = compatibility;
  priv->base_socket = base_socket;

  sock->fileno = priv->base_socket->fileno;
  sock->addr = priv->base_socket->addr;
89
  sock->send_messages = socket_send_messages;
90
  sock->recv_messages = socket_recv_messages;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  sock->is_reliable = socket_is_reliable;
  sock->close = socket_close;

  return sock;
}


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

  if (priv->base_socket)
    nice_socket_free (priv->base_socket);

  g_slice_free(TurnTcpPriv, sock->priv);
}

109 110
static gssize
socket_recv_message (NiceSocket *sock, NiceInputMessage *recv_message)
111 112
{
  TurnTcpPriv *priv = sock->priv;
113
  gssize ret;
114
  guint padlen;
115 116
  GInputVector local_recv_buf;
  NiceInputMessage local_recv_message;
117 118

  if (priv->expecting_len == 0) {
119
    guint headerlen = 0;
120

121 122
    if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 ||
        priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766)
123
      headerlen = 4;
124
    else if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE)
125 126
      headerlen = 2;
    else
127 128
      return -1;

129 130 131 132 133 134 135 136
    local_recv_buf.buffer = priv->recv_buf.u8 + priv->recv_buf_len;
    local_recv_buf.size = headerlen - priv->recv_buf_len;
    local_recv_message.buffers = &local_recv_buf;
    local_recv_message.n_buffers = 1;
    local_recv_message.from = recv_message->from;
    local_recv_message.length = 0;

    ret = nice_socket_recv_messages (priv->base_socket, &local_recv_message, 1);
137
    if (ret < 0)
138 139
        return ret;

140
    priv->recv_buf_len += local_recv_message.length;
141

142
    if (priv->recv_buf_len < headerlen)
143 144
      return 0;

145 146
    if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 ||
        priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766) {
147 148
      guint16 magic = ntohs (*priv->recv_buf.u16);
      guint16 packetlen = ntohs (*(priv->recv_buf.u16 + 1));
149

150 151
      if (magic < 0x4000) {
        /* Its STUN */
152
        priv->expecting_len = 20 + packetlen;
153 154
      } else {
        /* Channel data */
155 156 157
        priv->expecting_len = 4 + packetlen;
      }
    }
158
    else if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
159 160
      guint compat_len = ntohs (*priv->recv_buf.u16);
      priv->expecting_len = compat_len;
161 162 163 164
      priv->recv_buf_len = 0;
    }
  }

165
  if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 ||
166
      priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766)
167 168 169
    padlen = (priv->expecting_len % 4) ?  4 - (priv->expecting_len % 4) : 0;
  else
    padlen = 0;
170

171 172 173 174 175 176
  local_recv_buf.buffer = priv->recv_buf.u8 + priv->recv_buf_len;
  local_recv_buf.size = priv->expecting_len + padlen - priv->recv_buf_len;
  local_recv_message.buffers = &local_recv_buf;
  local_recv_message.n_buffers = 1;
  local_recv_message.from = recv_message->from;
  local_recv_message.length = 0;
177

178
  ret = nice_socket_recv_messages (priv->base_socket, &local_recv_message, 1);
179
  if (ret < 0)
180 181
      return ret;

182
  priv->recv_buf_len += local_recv_message.length;
183 184

  if (priv->recv_buf_len == priv->expecting_len + padlen) {
185 186 187 188
    /* FIXME: Eliminate this memcpy(). */
    ret = memcpy_buffer_to_input_message (recv_message,
        priv->recv_buf.u8, priv->recv_buf_len);

189 190
    priv->expecting_len = 0;
    priv->recv_buf_len = 0;
191

192
    return ret;
193 194 195 196 197
  }

  return 0;
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static gint
socket_recv_messages (NiceSocket *socket,
    NiceInputMessage *recv_messages, guint n_recv_messages)
{
  guint i;
  gboolean error = FALSE;

  for (i = 0; i < n_recv_messages; i++) {
    gssize len;

    len = socket_recv_message (socket, &recv_messages[i]);
    recv_messages[i].length = MAX (len, 0);

    if (len < 0)
      error = TRUE;

    if (len <= 0)
      break;
  }

  /* Was there an error processing the first message? */
  if (error && i == 0)
    return -1;

  return i;
}

225
static gssize
226 227
socket_send_message (NiceSocket *sock, const NiceAddress *to,
    const NiceOutputMessage *message)
228
{
229
  TurnTcpPriv *priv = sock->priv;
230 231 232 233 234 235 236
  guint8 padbuf[3] = {0, 0, 0};
  GOutputVector *local_bufs;
  NiceOutputMessage local_message;
  guint j;
  gint ret;
  guint n_bufs;
  guint16 header_buf;
237
  guint offset = 0;
238 239 240 241 242 243 244 245 246 247

  /* Count the number of buffers. */
  if (message->n_buffers == -1) {
    n_bufs = 0;

    for (j = 0; message->buffers[j].buffer != NULL; j++)
      n_bufs++;
  } else {
    n_bufs = message->n_buffers;
  }
248

249 250
  /* Allocate a new array of buffers, covering all the buffers in the input
   * @message, but with an additional one for a header and one for a footer. */
251
  local_bufs = g_malloc_n (n_bufs + 1, sizeof (GOutputVector));
252
  local_message.buffers = local_bufs;
253
  local_message.n_buffers = n_bufs + 1;
254

255
  if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
256 257
    local_bufs[0].buffer = &header_buf;
    local_bufs[0].size = sizeof (header_buf);
258 259
    offset = 1;
  } else if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 ||
260
      priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766) {
261 262
    gsize message_len = output_message_get_size (message);
    gsize padlen = (message_len % 4) ? 4 - (message_len % 4) : 0;
263 264 265 266

    local_bufs[n_bufs].buffer = &padbuf;
    local_bufs[n_bufs].size = padlen;
  } else {
267
    local_message.n_buffers = n_bufs;
268 269
  }

270 271 272 273 274 275 276
  /* Copy the existing buffers across. */
  for (j = 0; j < n_bufs; j++) {
    local_bufs[j + offset].buffer = message->buffers[j].buffer;
    local_bufs[j + offset].size = message->buffers[j].size;
  }


277
  ret = nice_socket_send_messages (priv->base_socket, to, &local_message, 1);
278

279 280 281
  if (ret == 1)
    ret = output_message_get_size (&local_message);

282 283 284 285 286 287
  g_free (local_bufs);

  return ret;
}

static gint
288 289
socket_send_messages (NiceSocket *sock, const NiceAddress *to,
    const NiceOutputMessage *messages, guint n_messages)
290 291 292 293 294 295 296
{
  guint i;

  for (i = 0; i < n_messages; i++) {
    const NiceOutputMessage *message = &messages[i];
    gssize len;

297
    len = socket_send_message (sock, to, message);
298 299 300 301 302 303 304 305

    if (len < 0) {
      /* Error. */
      return len;
    } else if (len == 0) {
      /* EWOULDBLOCK. */
      break;
    }
306
  }
307

308
  return i;
309 310 311
}


312 313 314 315 316 317
static gboolean
socket_is_reliable (NiceSocket *sock)
{
  return TRUE;
}