stunmessage.c 20 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) 2007-2009 Nokia Corporation. All rights reserved.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *  Contact: Rémi Denis-Courmont
 *
 * 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:
25
 *   Youness Alaoui, Collabora Ltd.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *   Rémi Denis-Courmont, Nokia
 *
 * 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

Youness Alaoui's avatar
Youness Alaoui committed
43
#include "stunmessage.h"
44
#include "utils.h"
Youness Alaoui's avatar
Youness Alaoui committed
45 46 47

#ifdef _WIN32
#include <winsock2.h>
48
#include <ws2tcpip.h>
Youness Alaoui's avatar
Youness Alaoui committed
49
#else
50 51
#include <sys/types.h>
#include <sys/socket.h>
Alexis Ballier's avatar
Alexis Ballier committed
52
#include <netinet/in.h>
Youness Alaoui's avatar
Youness Alaoui committed
53
#endif
54 55 56 57 58


#include <string.h>
#include <stdlib.h>

59 60
bool stun_message_init (StunMessage *msg, StunClass c, StunMethod m,
    const StunTransactionId id)
61
{
62

63 64 65 66 67 68
  if (msg->buffer_len < STUN_MESSAGE_HEADER_LENGTH)
    return FALSE;

  memset (msg->buffer, 0, 4);
  stun_set_type (msg->buffer, c, m);

69
  memcpy (msg->buffer + STUN_MESSAGE_TRANS_ID_POS,
70
      id, STUN_MESSAGE_TRANS_ID_LEN);
71 72 73 74 75 76 77 78 79 80 81 82 83 84

  return TRUE;
}

uint16_t stun_message_length (const StunMessage *msg)
{
  return stun_getw (msg->buffer + STUN_MESSAGE_LENGTH_POS) +
      STUN_MESSAGE_HEADER_LENGTH;
}




const void *
85
stun_message_find (const StunMessage *msg, StunAttribute type,
86
    uint16_t *palen)
87 88 89 90
{
  size_t length = stun_message_length (msg);
  size_t offset = 0;

91 92 93 94 95 96 97 98
  /* In MS-TURN, IDs of REALM and NONCE STUN attributes are swapped. */
  if (msg->agent && msg->agent->compatibility == STUN_COMPATIBILITY_OC2007)
  {
    if (type == STUN_ATTRIBUTE_REALM)
      type = STUN_ATTRIBUTE_NONCE;
    else if (type == STUN_ATTRIBUTE_NONCE)
      type = STUN_ATTRIBUTE_REALM;
  }
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

  offset = STUN_MESSAGE_ATTRIBUTES_POS;

  while (offset < length)
  {
    uint16_t atype = stun_getw (msg->buffer + offset);
    size_t alen = stun_getw (msg->buffer + offset + STUN_ATTRIBUTE_TYPE_LEN);


    offset += STUN_ATTRIBUTE_VALUE_POS;

    if (atype == type)
    {
      *palen = alen;
      return msg->buffer + offset;
    }

    /* Look for and ignore misordered attributes */
    switch (atype)
    {
119
      case STUN_ATTRIBUTE_MESSAGE_INTEGRITY:
120
        /* Only fingerprint may come after M-I */
121
        if (type == STUN_ATTRIBUTE_FINGERPRINT)
122 123
          break;

124
      case STUN_ATTRIBUTE_FINGERPRINT:
125 126
        /* Nothing may come after FPR */
        return NULL;
127 128 129 130

      default:
        /* Nothing misordered. */
        break;
131 132
    }

133 134
    if (!(msg->agent &&
            (msg->agent->usage_flags & STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES)))
135 136
      alen = stun_align (alen);

137 138 139 140 141 142 143
    offset += alen;
  }

  return NULL;
}


144
StunMessageReturn
145
stun_message_find_flag (const StunMessage *msg, StunAttribute type)
146 147
{
  const void *ptr;
148
  uint16_t len = 0;
149 150 151

  ptr = stun_message_find (msg, type, &len);
  if (ptr == NULL)
152 153 154
    return STUN_MESSAGE_RETURN_NOT_FOUND;
  return (len == 0) ? STUN_MESSAGE_RETURN_SUCCESS :
      STUN_MESSAGE_RETURN_INVALID;
155 156 157
}


158
StunMessageReturn
159
stun_message_find32 (const StunMessage *msg, StunAttribute type,
160
    uint32_t *pval)
161 162
{
  const void *ptr;
163
  uint16_t len = 0;
164 165 166

  ptr = stun_message_find (msg, type, &len);
  if (ptr == NULL)
167
    return STUN_MESSAGE_RETURN_NOT_FOUND;
168 169 170 171 172 173 174

  if (len == 4)
  {
    uint32_t val;

    memcpy (&val, ptr, sizeof (val));
    *pval = ntohl (val);
175
    return STUN_MESSAGE_RETURN_SUCCESS;
176
  }
177
  return STUN_MESSAGE_RETURN_INVALID;
178 179 180
}


181
StunMessageReturn
182
stun_message_find64 (const StunMessage *msg, StunAttribute type,
183 184 185
    uint64_t *pval)
{
  const void *ptr;
186
  uint16_t len = 0;
187 188 189

  ptr = stun_message_find (msg, type, &len);
  if (ptr == NULL)
190
    return STUN_MESSAGE_RETURN_NOT_FOUND;
191 192 193 194 195 196 197

  if (len == 8)
  {
    uint32_t tab[2];

    memcpy (tab, ptr, sizeof (tab));
    *pval = ((uint64_t)ntohl (tab[0]) << 32) | ntohl (tab[1]);
198
    return STUN_MESSAGE_RETURN_SUCCESS;
199
  }
200
  return STUN_MESSAGE_RETURN_INVALID;
201 202 203
}


204
StunMessageReturn
205
stun_message_find_string (const StunMessage *msg, StunAttribute type,
206 207 208
    char *buf, size_t buflen)
{
  const unsigned char *ptr;
209
  uint16_t len = 0;
210 211 212

  ptr = stun_message_find (msg, type, &len);
  if (ptr == NULL)
213
    return STUN_MESSAGE_RETURN_NOT_FOUND;
214 215

  if (len >= buflen)
216
    return STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE;
217 218 219

  memcpy (buf, ptr, len);
  buf[len] = '\0';
220
  return STUN_MESSAGE_RETURN_SUCCESS;
221 222 223
}


224
StunMessageReturn
225
stun_message_find_addr (const StunMessage *msg, StunAttribute type,
226
    struct sockaddr_storage *addr, socklen_t *addrlen)
227 228
{
  const uint8_t *ptr;
229
  uint16_t len = 0;
230 231 232

  ptr = stun_message_find (msg, type, &len);
  if (ptr == NULL)
233
    return STUN_MESSAGE_RETURN_NOT_FOUND;
234 235

  if (len < 4)
236
    return STUN_MESSAGE_RETURN_INVALID;
237 238 239 240 241 242

  switch (ptr[1])
  {
    case 1:
      {
        struct sockaddr_in *ip4 = (struct sockaddr_in *)addr;
243
        if (((size_t) *addrlen < sizeof (*ip4)) || (len != 8))
244 245
        {
          *addrlen = sizeof (*ip4);
246
          return STUN_MESSAGE_RETURN_INVALID;
247 248 249 250 251 252 253 254 255 256
        }

        memset (ip4, 0, *addrlen);
        ip4->sin_family = AF_INET;
#ifdef HAVE_SA_LEN
        ip4->sin_len =
#endif
            *addrlen = sizeof (*ip4);
        memcpy (&ip4->sin_port, ptr + 2, 2);
        memcpy (&ip4->sin_addr, ptr + 4, 4);
257
        return STUN_MESSAGE_RETURN_SUCCESS;
258 259 260 261 262
      }

    case 2:
      {
        struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)addr;
263
        if (((size_t) *addrlen < sizeof (*ip6)) || (len != 20))
264 265
        {
          *addrlen = sizeof (*ip6);
266
          return STUN_MESSAGE_RETURN_INVALID;
267 268 269 270 271 272 273 274 275 276
        }

        memset (ip6, 0, *addrlen);
        ip6->sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
        ip6->sin6_len =
#endif
            *addrlen = sizeof (*ip6);
        memcpy (&ip6->sin6_port, ptr + 2, 2);
        memcpy (&ip6->sin6_addr, ptr + 4, 16);
277
        return STUN_MESSAGE_RETURN_SUCCESS;
278 279
      }

280 281 282
    default:
      return STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS;
  }
283 284
}

285
StunMessageReturn
286
stun_message_find_xor_addr (const StunMessage *msg, StunAttribute type,
287
    struct sockaddr_storage *addr, socklen_t *addrlen)
288
{
289
  StunMessageReturn val = stun_message_find_addr (msg, type, addr, addrlen);
290 291 292
  if (val)
    return val;

293 294 295
  return stun_xor_address (msg, addr, *addrlen, STUN_MAGIC_COOKIE);
}

296
StunMessageReturn
297
stun_message_find_xor_addr_full (const StunMessage *msg, StunAttribute type,
298
    struct sockaddr_storage *addr, socklen_t *addrlen, uint32_t magic_cookie)
299
{
300
  StunMessageReturn val = stun_message_find_addr (msg, type, addr, addrlen);
301 302 303 304
  if (val)
    return val;

  return stun_xor_address (msg, addr, *addrlen, magic_cookie);
305 306
}

307 308
StunMessageReturn
stun_message_find_error (const StunMessage *msg, int *code)
309
{
310
  uint16_t alen = 0;
311
  const uint8_t *ptr = stun_message_find (msg, STUN_ATTRIBUTE_ERROR_CODE, &alen);
312 313 314
  uint8_t class, number;

  if (ptr == NULL)
315
    return STUN_MESSAGE_RETURN_NOT_FOUND;
316
  if (alen < 4)
317
    return STUN_MESSAGE_RETURN_INVALID;
318 319 320 321

  class = ptr[2] & 0x7;
  number = ptr[3];
  if ((class < 3) || (class > 6) || (number > 99))
322
    return STUN_MESSAGE_RETURN_INVALID;
323 324

  *code = (class * 100) + number;
325
  return STUN_MESSAGE_RETURN_SUCCESS;
326 327 328
}

void *
329
stun_message_append (StunMessage *msg, StunAttribute type, size_t length)
330 331
{
  uint8_t *a;
332
  uint16_t mlen = stun_message_length (msg);
333

334 335 336 337 338 339 340 341 342
  /* In MS-TURN, IDs of REALM and NONCE STUN attributes are swapped. */
  if (msg->agent && msg->agent->compatibility == STUN_COMPATIBILITY_OC2007)
  {
    if (type == STUN_ATTRIBUTE_NONCE)
      type = STUN_ATTRIBUTE_REALM;
    else if (type == STUN_ATTRIBUTE_REALM)
      type = STUN_ATTRIBUTE_NONCE;
  }

343
  if ((size_t)mlen + STUN_ATTRIBUTE_HEADER_LENGTH + length > msg->buffer_len)
344 345 346
    return NULL;


347
  a = msg->buffer + mlen;
348
  a = stun_setw (a, type);
349 350
  if (msg->agent &&
      (msg->agent->usage_flags & STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES))
351 352 353 354 355 356 357
  {
    a = stun_setw (a, length);
  } else {
    /* NOTE: If cookie is not present, we need to force the attribute length
     * to a multiple of 4 for compatibility with old RFC3489 */
    a = stun_setw (a, stun_message_has_cookie (msg) ? length : stun_align (length));

358 359 360 361 362
    /* Add padding if needed. Avoid a zero-length memset() call. */
    if (stun_padding (length) > 0) {
      memset (a + length, ' ', stun_padding (length));
      mlen += stun_padding (length);
    }
363
  }
364

365
  mlen +=  4 + length;
366

367
  stun_setw (msg->buffer + STUN_MESSAGE_LENGTH_POS, mlen - STUN_MESSAGE_HEADER_LENGTH);
368 369 370 371
  return a;
}


372
StunMessageReturn
373
stun_message_append_bytes (StunMessage *msg, StunAttribute type,
374 375 376 377
    const void *data, size_t len)
{
  void *ptr = stun_message_append (msg, type, len);
  if (ptr == NULL)
378
    return STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE;
379

380 381 382
  if (len > 0)
    memcpy (ptr, data, len);

383
  return STUN_MESSAGE_RETURN_SUCCESS;
384 385 386
}


387
StunMessageReturn
388
stun_message_append_flag (StunMessage *msg, StunAttribute type)
389 390 391 392 393
{
  return stun_message_append_bytes (msg, type, NULL, 0);
}


394
StunMessageReturn
395
stun_message_append32 (StunMessage *msg, StunAttribute type,
396 397 398 399 400 401 402
    uint32_t value)
{
  value = htonl (value);
  return stun_message_append_bytes (msg, type, &value, 4);
}


403
StunMessageReturn
404
stun_message_append64 (StunMessage *msg, StunAttribute type,
405 406 407 408 409 410 411 412 413
    uint64_t value)
{
  uint32_t tab[2];
  tab[0] = htonl ((uint32_t)(value >> 32));
  tab[1] = htonl ((uint32_t)value);
  return stun_message_append_bytes (msg, type, tab, 8);
}


414
StunMessageReturn
415
stun_message_append_string (StunMessage * msg, StunAttribute type,
416 417 418 419 420
    const char *str)
{
  return stun_message_append_bytes (msg, type, str, strlen (str));
}

421
StunMessageReturn
422
stun_message_append_addr (StunMessage *msg, StunAttribute type,
423
    const struct sockaddr *addr, socklen_t addrlen)
424 425 426 427 428 429
{
  const void *pa;
  uint8_t *ptr;
  uint16_t alen, port;
  uint8_t family;

430 431 432 433 434 435
  union {
    const struct sockaddr *addr;
    const struct sockaddr_in *in;
    const struct sockaddr_in6 *in6;
  } sa;

436
  if ((size_t) addrlen < sizeof (struct sockaddr))
437
    return STUN_MESSAGE_RETURN_INVALID;
438

439 440
  sa.addr = addr;

441
  switch (addr->sa_family)
442 443 444
  {
    case AF_INET:
      {
445
        const struct sockaddr_in *ip4 = sa.in;
446 447 448 449 450 451 452 453 454
        family = 1;
        port = ip4->sin_port;
        alen = 4;
        pa = &ip4->sin_addr;
        break;
      }

    case AF_INET6:
      {
455
        const struct sockaddr_in6 *ip6 = sa.in6;
456
        if ((size_t) addrlen < sizeof (*ip6))
457
          return STUN_MESSAGE_RETURN_INVALID;
458 459 460 461 462 463 464 465 466

        family = 2;
        port = ip6->sin6_port;
        alen = 16;
        pa = &ip6->sin6_addr;
        break;
      }

    default:
467
      return STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS;
468 469 470 471
  }

  ptr = stun_message_append (msg, type, 4 + alen);
  if (ptr == NULL)
472
    return STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE;
473 474 475 476 477

  ptr[0] = 0;
  ptr[1] = family;
  memcpy (ptr + 2, &port, 2);
  memcpy (ptr + 4, pa, alen);
478
  return STUN_MESSAGE_RETURN_SUCCESS;
479 480 481
}


482
StunMessageReturn
483
stun_message_append_xor_addr (StunMessage *msg, StunAttribute type,
484
    const struct sockaddr_storage *addr, socklen_t addrlen)
485
{
486
  StunMessageReturn val;
487
  /* Must be big enough to hold any supported address: */
488
  struct sockaddr_storage tmpaddr;
489

490 491 492
  if ((size_t) addrlen > sizeof (tmpaddr))
    addrlen = sizeof (tmpaddr);
  memcpy (&tmpaddr, addr, addrlen);
493

494
  val = stun_xor_address (msg, &tmpaddr, addrlen,
495 496 497 498
      STUN_MAGIC_COOKIE);
  if (val)
    return val;

499 500
  return stun_message_append_addr (msg, type, (struct sockaddr *) &tmpaddr,
      addrlen);
501 502
}

503
StunMessageReturn
504
stun_message_append_xor_addr_full (StunMessage *msg, StunAttribute type,
505
    const struct sockaddr_storage *addr, socklen_t addrlen,
506 507
    uint32_t magic_cookie)
{
508
  StunMessageReturn val;
509
  /* Must be big enough to hold any supported address: */
510
  struct sockaddr_storage tmpaddr;
511

512 513 514
  if ((size_t) addrlen > sizeof (tmpaddr))
    addrlen = sizeof (tmpaddr);
  memcpy (&tmpaddr, addr, addrlen);
515

516
  val = stun_xor_address (msg, &tmpaddr, addrlen, magic_cookie);
517 518 519
  if (val)
    return val;

520 521
  return stun_message_append_addr (msg, type, (struct sockaddr *) &tmpaddr,
      addrlen);
522 523 524 525
}



526
StunMessageReturn
527
stun_message_append_error (StunMessage *msg, StunError code)
528 529 530 531
{
  const char *str = stun_strerror (code);
  size_t len = strlen (str);

532
  uint8_t *ptr = stun_message_append (msg, STUN_ATTRIBUTE_ERROR_CODE, 4 + len);
533
  if (ptr == NULL)
534
    return STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE;
535 536

  memset (ptr, 0, 2);
537 538
  ptr[2] = code / 100;
  ptr[3] = code % 100;
539
  memcpy (ptr + 4, str, len);
540
  return STUN_MESSAGE_RETURN_SUCCESS;
541 542
}

543 544 545 546 547 548
/* Fast validity check for a potential STUN packet. Examines the type and
 * length, but none of the attributes. Designed to allow vectored I/O on all
 * incoming packets, filtering packets for closer inspection as to whether
 * they’re STUN packets. If they look like they might be, their buffers are
 * compacted to allow a more thorough check. */
ssize_t stun_message_validate_buffer_length_fast (StunInputVector *buffers,
549
    int n_buffers, size_t total_length, bool has_padding)
550 551 552
{
  size_t mlen;

553
  if (total_length < 1 || n_buffers == 0 || buffers[0].buffer == NULL)
554
  {
555
    stun_debug ("STUN error: No data!");
556
    return STUN_MESSAGE_BUFFER_INVALID;
557 558
  }

559
  if (buffers[0].buffer[0] >> 6)
560
  {
561
    return STUN_MESSAGE_BUFFER_INVALID; // RTP or other non-STUN packet
562 563
  }

564
  if (total_length < STUN_MESSAGE_LENGTH_POS + STUN_MESSAGE_LENGTH_LEN)
565
  {
566
    stun_debug ("STUN error: Incomplete STUN message header!");
567
    return STUN_MESSAGE_BUFFER_INCOMPLETE;
568 569
  }

570 571 572 573 574 575 576 577 578
  if (buffers[0].size >= STUN_MESSAGE_LENGTH_POS + STUN_MESSAGE_LENGTH_LEN) {
    /* Fast path. */
    mlen = stun_getw (buffers[0].buffer + STUN_MESSAGE_LENGTH_POS);
  } else {
    /* Slow path. Tiny buffers abound. */
    size_t skip_remaining = STUN_MESSAGE_LENGTH_POS;
    unsigned int i;

    /* Skip bytes. */
579 580
    for (i = 0; (n_buffers >= 0 && i < (unsigned int) n_buffers) ||
             (n_buffers < 0 && buffers[i].buffer != NULL); i++) {
581 582 583 584 585
      if (buffers[i].size <= skip_remaining)
        skip_remaining -= buffers[i].size;
      else
        break;
    }
586

587 588 589 590 591 592 593 594 595 596 597 598 599
    /* Read bytes. May be split over two buffers. We’ve already checked that
     * @total_length is long enough, so @n_buffers should be too. */
    if (buffers[i].size - skip_remaining > 1) {
      mlen = stun_getw (buffers[i].buffer + skip_remaining);
    } else {
      mlen = (*(buffers[i].buffer + skip_remaining) << 8) |
             (*(buffers[i + 1].buffer));
    }
  }

  mlen += STUN_MESSAGE_HEADER_LENGTH;

  if (has_padding && stun_padding (mlen)) {
600
    stun_debug ("STUN error: Invalid message length: %u!", (unsigned)mlen);
601
    return STUN_MESSAGE_BUFFER_INVALID; // wrong padding
602 603
  }

604
  if (total_length < mlen) {
605
    stun_debug ("STUN error: Incomplete message: %u of %u bytes!",
606
        (unsigned) total_length, (unsigned) mlen);
607
    return STUN_MESSAGE_BUFFER_INCOMPLETE; // partial message
608 609
  }

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
  return mlen;
}

int stun_message_validate_buffer_length (const uint8_t *msg, size_t length,
    bool has_padding)
{
  ssize_t fast_retval;
  size_t mlen;
  size_t len;
  StunInputVector input_buffer = { msg, length };

  /* Fast pre-check first. */
  fast_retval = stun_message_validate_buffer_length_fast (&input_buffer, 1,
      length, has_padding);
  if (fast_retval <= 0)
    return fast_retval;

  mlen = fast_retval;

  /* Skip past the header (validated above). */
630 631 632 633 634 635
  msg += 20;
  len = mlen - 20;

  /* from then on, we know we have the entire packet in buffer */
  while (len > 0)
  {
636 637 638 639 640
    size_t alen;

    if (len < 4)
    {
      stun_debug ("STUN error: Incomplete STUN attribute header of length "
641
          "%u bytes!", (unsigned)len);
642 643 644 645
      return STUN_MESSAGE_BUFFER_INVALID;
    }

    alen = stun_getw (msg + STUN_ATTRIBUTE_TYPE_LEN);
646 647
    if (has_padding)
      alen = stun_align (alen);
648 649 650 651 652 653 654

    /* thanks to padding check, if (end > msg) then there is not only one
     * but at least 4 bytes left */
    len -= 4;

    if (len < alen)
    {
655
      stun_debug ("STUN error: %u instead of %u bytes for attribute!",
656
          (unsigned)len, (unsigned)alen);
657
      return STUN_MESSAGE_BUFFER_INVALID; // no room for attribute value + padding
658 659 660 661 662 663
    }

    len -= alen;
    msg += 4 + alen;
  }

664 665 666
  return mlen;
}

667
void stun_message_id (const StunMessage *msg, StunTransactionId id)
668 669 670 671
{
  memcpy (id, msg->buffer + STUN_MESSAGE_TRANS_ID_POS, STUN_MESSAGE_TRANS_ID_LEN);
}

672
StunMethod stun_message_get_method (const StunMessage *msg)
673 674
{
  uint16_t t = stun_getw (msg->buffer);
Youness Alaoui's avatar
Youness Alaoui committed
675
  /* HACK HACK HACK
676
     A google/msn data indication is 0x0115 which is contrary to the RFC 5389
Youness Alaoui's avatar
Youness Alaoui committed
677 678 679 680 681 682 683 684
     which states that 8th and 12th bits are for the class and that 0x01 is
     for indications...
     So 0x0115 is reported as a "connect error response", while it should be
     a data indication, which message type should actually be 0x0017
     This should fix the issue, and it's considered safe since the "connect"
     method doesn't exist anymore */
  if (t == 0x0115)
    t = 0x0017;
685
  return (StunMethod)(((t & 0x3e00) >> 2) | ((t & 0x00e0) >> 1) |
686 687 688 689
                          (t & 0x000f));
}


690
StunClass stun_message_get_class (const StunMessage *msg)
691 692
{
  uint16_t t = stun_getw (msg->buffer);
Youness Alaoui's avatar
Youness Alaoui committed
693
  /* HACK HACK HACK
694
     A google/msn data indication is 0x0115 which is contrary to the RFC 5389
Youness Alaoui's avatar
Youness Alaoui committed
695 696 697 698 699 700 701 702
     which states that 8th and 12th bits are for the class and that 0x01 is
     for indications...
     So 0x0115 is reported as a "connect error response", while it should be
     a data indication, which message type should actually be 0x0017
     This should fix the issue, and it's considered safe since the "connect"
     method doesn't exist anymore */
  if (t == 0x0115)
    t = 0x0017;
703
  return (StunClass)(((t & 0x0100) >> 7) | ((t & 0x0010) >> 4));
704 705
}

706
bool stun_message_has_attribute (const StunMessage *msg, StunAttribute type)
707 708 709
{
  uint16_t dummy;
  return stun_message_find (msg, type, &dummy) != NULL;
710
}
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760


bool stun_optional (uint16_t t)
{
  return (t >> 15) == 1;
}

const char *stun_strerror (StunError code)
{
  static const struct
  {
    StunError code;
    char     phrase[32];
  } tab[] =
  {
    { STUN_ERROR_TRY_ALTERNATE, "Try alternate server" },
    { STUN_ERROR_BAD_REQUEST, "Bad request" },
    { STUN_ERROR_UNAUTHORIZED, "Unauthorized" },
    { STUN_ERROR_UNKNOWN_ATTRIBUTE, "Unknown Attribute" },
    { STUN_ERROR_ALLOCATION_MISMATCH, "Allocation Mismatch" },
    { STUN_ERROR_STALE_NONCE, "Stale Nonce" },
    { STUN_ERROR_ACT_DST_ALREADY, "Active Destination Already Set" },
    { STUN_ERROR_UNSUPPORTED_FAMILY, "Address Family not Supported" },
    { STUN_ERROR_UNSUPPORTED_TRANSPORT, "Unsupported Transport Protocol" },
    { STUN_ERROR_INVALID_IP, "Invalid IP Address" },
    { STUN_ERROR_INVALID_PORT, "Invalid Port" },
    { STUN_ERROR_OP_TCP_ONLY, "Operation for TCP Only" },
    { STUN_ERROR_CONN_ALREADY, "Connection Already Exists" },
    { STUN_ERROR_ALLOCATION_QUOTA_REACHED, "Allocation Quota Reached" },
    { STUN_ERROR_ROLE_CONFLICT, "Role conflict" },
    { STUN_ERROR_SERVER_ERROR, "Server Error" },
    { STUN_ERROR_SERVER_CAPACITY, "Insufficient Capacity" },
    { STUN_ERROR_INSUFFICIENT_CAPACITY, "Insufficient Capacity" },
  };
  const char *str = "Unknown error";
  size_t i;

  for (i = 0; i < (sizeof (tab) / sizeof (tab[0])); i++)
  {
    if (tab[i].code == code)
    {
      str = tab[i].phrase;
      break;
    }
  }

  /* Maximum allowed error message length */
  //  assert (strlen (str) < 128);
  return str;
}