summaryrefslogtreecommitdiff
path: root/udp/udp-turn.c
blob: f899c038e1fa6b992bcc09842771d336a2072818 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2008 Collabora Ltd.
 * (C) 2008 Nokia Corporation
 *  Contact: Youness Alaoui
 *
 * 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:
 *   Dafydd Harries, Collabora Ltd.
 *   Youness Alaoui, Collabora Ltd.
 *   RĂ©mi Denis-Courmont, Nokia
 *   Kai Vehmanen
 *
 * 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.
 */

/*
 * Implementation of UDP socket interface using Berkeley sockets. (See
 * http://en.wikipedia.org/wiki/Berkeley_sockets.)
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <arpa/inet.h>

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

#include "udp-turn.h"
#include "udp-bsd.h"
#include <stun/stunagent.h>

typedef struct {
  NiceAddress peer;
  uint16_t channel;
} ChannelBinding;

typedef struct {
  StunAgent agent;
  GList *channels;
  ChannelBinding *current_binding;
  NiceUDPSocket *udp_socket;
  NiceAddress server_addr;
  uint8_t *username;
  size_t username_len;
  uint8_t *password;
  size_t password_len;
  NiceUdpTurnSocketCompatibility compatibility;
} turn_priv;


static gboolean
priv_send_channel_bind (turn_priv *priv,  StunMessage *resp,
    uint16_t channel, NiceAddress *peer) {
  uint32_t channel_attr = channel << 16;
  StunMessage msg;
  uint8_t buffer[STUN_MAX_MESSAGE_SIZE];
  size_t stun_len;
  struct sockaddr_storage sa;

  nice_address_copy_to_sockaddr (peer, (struct sockaddr *)&sa);

  if (!stun_agent_init_request (&priv->agent, &msg,
          buffer, sizeof(buffer), STUN_CHANNELBIND))
    return FALSE;

  if (stun_message_append32 (&msg, STUN_ATTRIBUTE_CHANNEL_NUMBER,
          channel_attr) != 0)
    return FALSE;

  if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_PEER_ADDRESS,
          (struct sockaddr *)&sa, sizeof(sa)) != 0)
    return FALSE;

  if (priv->username != NULL && priv->username_len > 0) {
    if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_USERNAME,
            priv->username, priv->username_len) != 0)
      return FALSE;
  }

  if (resp) {
    uint8_t *realm;
    uint8_t *nonce;
    uint16_t len;

    realm = (uint8_t *) stun_message_find (resp, STUN_ATTRIBUTE_REALM, &len);
    if (realm != NULL) {
      if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_REALM, realm, len) != 0)
        return 0;
    }
    nonce = (uint8_t *) stun_message_find (resp, STUN_ATTRIBUTE_NONCE, &len);
    if (nonce != NULL) {
      if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_NONCE, nonce, len) != 0)
        return 0;
    }
  }

  stun_len = stun_agent_finish_message (&priv->agent, &msg,
      priv->password, priv->password_len);

  if (stun_len > 0) {
    nice_udp_socket_send (priv->udp_socket, &priv->server_addr,
        stun_len, (gchar *)buffer);
    return TRUE;
  }

  return FALSE;
}
NICEAPI_EXPORT gboolean
nice_udp_turn_socket_set_peer (NiceUDPSocket *sock, NiceAddress *peer)
{
  turn_priv *priv = (turn_priv *) sock->priv;
  StunMessage msg;
  uint8_t buffer[STUN_MAX_MESSAGE_SIZE];
  size_t stun_len;
  struct sockaddr_storage sa;

  nice_address_copy_to_sockaddr (peer, (struct sockaddr *)&sa);

  if (priv->current_binding)
    return FALSE;

  if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
    uint16_t channel = 0x4000;
    GList *i = priv->channels;
    for (; i; i = i->next) {
      ChannelBinding *b = i->data;
      if (channel == b->channel) {
        i = priv->channels;
        channel++;
        continue;
      }
    }

    if (channel >= 0x4000 && channel < 0xffff) {
      gboolean ret = priv_send_channel_bind (priv, NULL, channel, peer);
      if (ret) {
        priv->current_binding = g_new0 (ChannelBinding, 1);
        priv->current_binding->channel = channel;
        priv->current_binding->peer = *peer;
      }
      return ret;
    }
    return FALSE;
  } else if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN) {
    if (!stun_agent_init_request (&priv->agent, &msg,
            buffer, sizeof(buffer), STUN_OLD_SET_ACTIVE_DST))
      return FALSE;

    if (stun_message_append32 (&msg, STUN_ATTRIBUTE_MAGIC_COOKIE,
            TURN_MAGIC_COOKIE) != 0)
      return FALSE;

    if (priv->username != NULL && priv->username_len > 0) {
      if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_USERNAME,
              priv->username, priv->username_len) != 0)
        return FALSE;
    }

    if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_DESTINATION_ADDRESS,
            (struct sockaddr *)&sa, sizeof(sa)) != 0)
      return FALSE;

    stun_len = stun_agent_finish_message (&priv->agent, &msg,
        priv->password, priv->password_len);

    if (stun_len > 0) {
      priv->current_binding = g_new0 (ChannelBinding, 1);
      priv->current_binding->channel = 0;
      priv->current_binding->peer = *peer;
      nice_udp_socket_send (priv->udp_socket, &priv->server_addr,
          stun_len, (gchar *)buffer);
    }
    return TRUE;
  } else if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
    priv->current_binding = g_new0 (ChannelBinding, 1);
    priv->current_binding->channel = 0;
    priv->current_binding->peer = *peer;
    return TRUE;
  } else {
    return FALSE;
  }

  return FALSE;
}


gint
nice_udp_turn_socket_parse_recv (
  NiceUDPSocket *sock,
  NiceAddress *from,
  guint len,
  gchar *buf,
  NiceAddress *recv_from,
  gchar *recv_buf,
  guint recv_len)
{

  turn_priv *priv = (turn_priv *) sock->priv;
  StunValidationStatus valid;
  StunMessage msg;
  struct sockaddr_storage sa;
  guint from_len = sizeof (sa);
  GList *i = priv->channels;
  ChannelBinding *binding = NULL;

  if (nice_address_equal (&priv->server_addr, recv_from)) {
    valid = stun_agent_validate (&priv->agent, &msg,
        (uint8_t *) recv_buf, (size_t) recv_len, NULL, NULL);

    if (valid == STUN_VALIDATION_SUCCESS) {
      if (priv->compatibility != NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
        uint32_t cookie;
        if (stun_message_find32 (&msg, STUN_ATTRIBUTE_MAGIC_COOKIE, &cookie) != 0)
          goto recv;
        if (cookie != TURN_MAGIC_COOKIE)
          goto recv;
      }

      if (stun_message_get_class (&msg) == STUN_RESPONSE &&
          stun_message_get_method (&msg) == STUN_SEND) {
        if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
          uint32_t options = 0;
          if (stun_message_find32 (&msg, STUN_ATTRIBUTE_OPTIONS, &options) == 0 &&
              options & 0x1)
            goto msn_google_lock;
        }
        return 0;
      } else if (stun_message_get_class (&msg) == STUN_RESPONSE &&
          stun_message_get_method (&msg) == STUN_OLD_SET_ACTIVE_DST) {
        if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN)
          goto msn_google_lock;

        return 0;
      } else if (stun_message_get_class (&msg) == STUN_ERROR &&
          stun_message_get_method (&msg) == STUN_CHANNELBIND) {
        if (priv->current_binding) {
          priv_send_channel_bind (priv, &msg,
              priv->current_binding->channel, &priv->current_binding->peer);
        }
        return 0;
      } else if (stun_message_get_class (&msg) == STUN_RESPONSE &&
          stun_message_get_method (&msg) == STUN_CHANNELBIND) {
        if (priv->current_binding) {
          priv->channels = g_list_append (priv->channels, priv->current_binding);
          priv->current_binding = NULL;
        }
        return 0;
      } else if (stun_message_get_class (&msg) == STUN_INDICATION &&
          stun_message_get_method (&msg) == STUN_IND_DATA) {
        uint16_t data_len;
        uint8_t *data;

        if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
          if (stun_message_find_xor_addr (&msg, STUN_ATTRIBUTE_REMOTE_ADDRESS,
                  (struct sockaddr *)&sa, &from_len) != 0)
            goto recv;
        } else {
          if (stun_message_find_addr (&msg, STUN_ATTRIBUTE_REMOTE_ADDRESS,
                  (struct sockaddr *)&sa, &from_len) != 0)
            goto recv;
        }

        data = (uint8_t *) stun_message_find (&msg, STUN_ATTRIBUTE_DATA, &data_len);
        if (data == NULL)
          goto recv;

        nice_address_set_from_sockaddr (from, (struct sockaddr *)&sa);

        memmove (buf, data, len > data_len ? data_len : len);
        return len > data_len ? data_len : len;
      } else {
        goto recv;
      }
    }
  }

 recv:
  for (; i; i = i->next) {
    ChannelBinding *b = i->data;
    if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
      if (b->channel == ((uint16_t *)recv_buf)[0]) {
        recv_len = ((uint16_t *)recv_buf)[1];
        recv_buf += sizeof(uint32_t);
        binding = b;
        break;
      }
    } else {
      binding = b;
      break;
    }
  }

  if (binding) {
    *from = binding->peer;
  } else {
    *from = *recv_from;
  }
  memmove (buf, recv_buf, len > recv_len ? recv_len : len);
  return len > recv_len ? recv_len : len;

 msn_google_lock:

  if (priv->current_binding) {
    GList *i = priv->channels;
    for (; i; i = i->next) {
      ChannelBinding *b = i->data;
      g_free (b);
    }
    g_list_free (priv->channels);
    priv->channels = g_list_append (NULL, priv->current_binding);
    priv->current_binding = NULL;
  }

  return 0;
}

static gint
socket_recv (
  NiceUDPSocket *sock,
  NiceAddress *from,
  guint len,
  gchar *buf)
{
  turn_priv *priv = (turn_priv *) sock->priv;
  uint8_t recv_buf[STUN_MAX_MESSAGE_SIZE];
  gint recv_len;
  NiceAddress recv_from;

  recv_len = nice_udp_socket_recv (priv->udp_socket, &recv_from,
      sizeof(recv_buf), (gchar *) recv_buf);

  return nice_udp_turn_socket_parse_recv (sock, from, len, buf, &recv_from,
      (gchar *) recv_buf, (guint) recv_len);
}

static gboolean
socket_send (
  NiceUDPSocket *sock,
  const NiceAddress *to,
  guint len,
  const gchar *buf)
{
  turn_priv *priv = (turn_priv *) sock->priv;
  StunMessage msg;
  uint8_t buffer[STUN_MAX_MESSAGE_SIZE];
  size_t msg_len;
  struct sockaddr_storage sa;
  GList *i = priv->channels;
  ChannelBinding *binding = NULL;

  for (; i; i = i->next) {
    ChannelBinding *b = i->data;
    if (nice_address_equal (&b->peer, to)) {
      binding = b;
      break;
    }
  }

  nice_address_copy_to_sockaddr (to, (struct sockaddr *)&sa);

  if (binding) {
    if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9 &&
        len + sizeof(uint32_t) <= sizeof(buffer)) {
      uint16_t len16 = (uint16_t) len;
      memcpy (buffer, &binding->channel, sizeof(uint16_t));
      memcpy (buffer + sizeof(uint16_t), &len16,sizeof(uint16_t));
      memcpy (buffer + sizeof(uint32_t), buf, len);
      msg_len = len + sizeof(uint32_t);
    } else {
      goto send;
    }
  } else {
    if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
      if (!stun_agent_init_indication (&priv->agent, &msg,
              buffer, sizeof(buffer), STUN_IND_SEND))
        goto send;
      if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_PEER_ADDRESS,
              (struct sockaddr *)&sa, sizeof(sa)) != 0)
        goto send;
    } else {
      if (!stun_agent_init_request (&priv->agent, &msg,
              buffer, sizeof(buffer), STUN_SEND))
        goto send;

      if (stun_message_append32 (&msg, STUN_ATTRIBUTE_MAGIC_COOKIE,
              TURN_MAGIC_COOKIE) != 0)
        goto send;
      if (priv->username != NULL && priv->username_len > 0) {
        if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_USERNAME,
                priv->username, priv->username_len) != 0)
          goto send;
      }
      if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_DESTINATION_ADDRESS,
              (struct sockaddr *)&sa, sizeof(sa)) != 0)
        goto send;

      if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE &&
          priv->current_binding &&
          nice_address_equal (&priv->current_binding->peer, to)) {
        stun_message_append32 (&msg, STUN_ATTRIBUTE_OPTIONS, 1);
      }
    }

    if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_DATA, buf, len) != 0)
      goto send;

    msg_len = stun_agent_finish_message (&priv->agent, &msg,
        priv->password, priv->password_len);
  }

  if (msg_len > 0) {
    nice_udp_socket_send (priv->udp_socket, &priv->server_addr,
        msg_len, (gchar *)buffer);
    return TRUE;
  }
 send:
  nice_udp_socket_send (priv->udp_socket, to, len, buf);

  return TRUE;
}

static void
socket_close (NiceUDPSocket *sock)
{
  turn_priv *priv = (turn_priv *) sock->priv;
  GList *i = priv->channels;
  for (; i; i = i->next) {
    ChannelBinding *b = i->data;
    g_free (b);
  }
  g_list_free (priv->channels);
  g_free (priv->username);
  g_free (priv->password);
  g_free (priv);
}

/*** NiceUDPSocketFactory ***/

static gboolean
socket_factory_init_socket (
  NiceUDPSocketFactory *man,
  NiceUDPSocket *sock,
  NiceAddress *addr)
{
  return FALSE;
}

NICEAPI_EXPORT gboolean
nice_udp_turn_create_socket_full (
  NiceUDPSocketFactory *man,
  NiceUDPSocket *sock,
  NiceAddress *addr,
  NiceUDPSocket *udp_socket,
  NiceAddress *server_addr,
  gchar *username,
  gchar *password,
  NiceUdpTurnSocketCompatibility compatibility,
  gboolean long_term)
{
  turn_priv *priv = g_new0 (turn_priv, 1);

  if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
    stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
        STUN_COMPATIBILITY_3489BIS,
        long_term ? STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS :
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS);
  } else {
    stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
        STUN_COMPATIBILITY_RFC3489,
        long_term ? STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS :
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS);
  }

  priv->channels = NULL;
  priv->current_binding = NULL;
  priv->udp_socket = udp_socket;

  if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN) {
    priv->username = g_base64_decode (username, &priv->username_len);
    priv->password = g_base64_decode (password, &priv->password_len);
  } else {
    priv->username = (uint8_t *)g_strdup (username);
    priv->username_len = (size_t) strlen (username);
    priv->password = (uint8_t *)g_strdup (password);
    priv->password_len = (size_t) strlen (password);
  }
  priv->server_addr = *server_addr;
  priv->compatibility = compatibility;
  sock->addr = *addr;
  sock->fileno = udp_socket->fileno;
  sock->send = socket_send;
  sock->recv = socket_recv;
  sock->close = socket_close;
  sock->priv = (void *) priv;
  return TRUE;
}

static void
socket_factory_close (
  G_GNUC_UNUSED
  NiceUDPSocketFactory *man)
{
}

NICEAPI_EXPORT void
nice_udp_turn_socket_factory_init (
  G_GNUC_UNUSED
  NiceUDPSocketFactory *man)
{

  man->init = socket_factory_init_socket;
  man->close = socket_factory_close;

}