media/mtransport/third_party/nICEr/src/stun/stun_util.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2 Copyright (c) 2007, Adobe Systems, Incorporated
     3 All rights reserved.
     5 Redistribution and use in source and binary forms, with or without
     6 modification, are permitted provided that the following conditions are
     7 met:
     9 * Redistributions of source code must retain the above copyright
    10   notice, this list of conditions and the following disclaimer.
    12 * Redistributions in binary form must reproduce the above copyright
    13   notice, this list of conditions and the following disclaimer in the
    14   documentation and/or other materials provided with the distribution.
    16 * Neither the name of Adobe Systems, Network Resonance nor the names of its
    17   contributors may be used to endorse or promote products derived from
    18   this software without specific prior written permission.
    20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31 */
    34 static char *RCSSTRING __UNUSED__="$Id: stun_util.c,v 1.2 2008/04/28 18:21:30 ekr Exp $";
    36 #include <errno.h>
    37 #include <csi_platform.h>
    39 #ifdef WIN32
    40 #include <winsock2.h>
    41 #include <stdlib.h>
    42 #include <io.h>
    43 #include <time.h>
    44 #else   /* UNIX */
    45 #include <string.h>
    46 #endif  /* end UNIX */
    47 #include <assert.h>
    49 #include "stun.h"
    50 #include "stun_reg.h"
    51 #include "registry.h"
    52 #include "addrs.h"
    53 #include "transport_addr_reg.h"
    54 #include "nr_crypto.h"
    55 #include "hex.h"
    58 int NR_LOG_STUN = 0;
    60 int
    61 nr_stun_startup(void)
    62 {
    63    int r,_status;
    65     if ((r=r_log_register("stun", &NR_LOG_STUN)))
    66       ABORT(r);
    68    _status=0;
    69  abort:
    70    return _status;
    71 }
    73 int
    74 nr_stun_xor_mapped_address(UINT4 magicCookie, nr_transport_addr *from, nr_transport_addr *to)
    75 {
    76     int _status;
    78     switch (from->ip_version) {
    79     case NR_IPV4:
    80         nr_ip4_port_to_transport_addr(
    81             (ntohl(from->u.addr4.sin_addr.s_addr) ^ magicCookie),
    82             (ntohs(from->u.addr4.sin_port) ^ (magicCookie>>16)),
    83             from->protocol, to);
    84         break;
    85     case NR_IPV6:
    86         assert(0);
    87         ABORT(R_INTERNAL);
    88         break;
    89     default:
    90         assert(0);
    91         ABORT(R_INTERNAL);
    92         break;
    93     }
    95     _status = 0;
    96   abort:
    97     return _status;
    98 }
   100 int
   101 nr_stun_find_local_addresses(nr_local_addr addrs[], int maxaddrs, int *count)
   102 {
   103     int r,_status;
   104     NR_registry *children = 0;
   106     if ((r=NR_reg_get_child_count(NR_STUN_REG_PREF_ADDRESS_PRFX, (unsigned int*)count)))
   107         if (r == R_NOT_FOUND)
   108             *count = 0;
   109         else
   110             ABORT(r);
   112     if (*count == 0) {
   113         if ((r=nr_stun_get_addrs(addrs, maxaddrs, 1, count)))
   114             ABORT(r);
   116         goto done;
   117     }
   119     if (*count >= maxaddrs) {
   120         r_log(NR_LOG_STUN, LOG_INFO, "Address list truncated from %d to %d", *count, maxaddrs);
   121        *count = maxaddrs;
   122     }
   124 #if 0
   125     if (*count > 0) {
   126       /* TODO(ekr@rtfm.com): Commented out 2012-07-26.
   128          This code is currently not used in Firefox and needs to be
   129          ported to 64-bit */
   130         children = RCALLOC((*count + 10) * sizeof(*children));
   131         if (!children)
   132             ABORT(R_NO_MEMORY);
   134         assert(sizeof(size_t) == sizeof(*count));
   136         if ((r=NR_reg_get_children(NR_STUN_REG_PREF_ADDRESS_PRFX, children, (size_t)(*count + 10), (size_t*)count)))
   137             ABORT(r);
   139         for (i = 0; i < *count; ++i) {
   140             if ((r=nr_reg_get_transport_addr(children[i], 0, &addrs[i].addr)))
   141                 ABORT(r);
   142         }
   143     }
   144 #endif
   146   done:
   148      _status=0;
   149  abort:
   150      RFREE(children);
   151      return _status;
   152 }
   154 int
   155 nr_stun_different_transaction(UCHAR *msg, int len, nr_stun_message *req)
   156 {
   157     int _status;
   158     nr_stun_message_header header;
   159     char reqid[44];
   160     char msgid[44];
   161     int len2;
   163     if (sizeof(header) > len)
   164         ABORT(R_FAILED);
   166     assert(sizeof(header.id) == sizeof(UINT12));
   168     memcpy(&header, msg, sizeof(header));
   170     if (memcmp(&req->header.id, &header.id, sizeof(header.id))) {
   171         nr_nbin2hex((UCHAR*)&req->header.id, sizeof(req->header.id), reqid, sizeof(reqid), &len2);
   172         nr_nbin2hex((UCHAR*)&header.id, sizeof(header.id), msgid, sizeof(msgid), &len2);
   173         r_log(NR_LOG_STUN, LOG_DEBUG, "Mismatched message IDs %s/%s", reqid, msgid);
   174         ABORT(R_NOT_FOUND);
   175     }
   177    _status=0;
   178  abort:
   179    return _status;
   180 }
   182 char*
   183 nr_stun_msg_type(int type)
   184 {
   185     char *ret = 0;
   187     switch (type) {
   188     case NR_STUN_MSG_BINDING_REQUEST:
   189          ret = "BINDING-REQUEST";
   190          break;
   191     case NR_STUN_MSG_BINDING_INDICATION:
   192          ret = "BINDING-INDICATION";
   193          break;
   194     case NR_STUN_MSG_BINDING_RESPONSE:
   195          ret = "BINDING-RESPONSE";
   196          break;
   197     case NR_STUN_MSG_BINDING_ERROR_RESPONSE:
   198          ret = "BINDING-ERROR-RESPONSE";
   199          break;
   201 #ifdef USE_TURN
   202     case NR_STUN_MSG_ALLOCATE_REQUEST:
   203          ret = "ALLOCATE-REQUEST";
   204          break;
   205     case NR_STUN_MSG_ALLOCATE_RESPONSE:
   206          ret = "ALLOCATE-RESPONSE";
   207          break;
   208     case NR_STUN_MSG_ALLOCATE_ERROR_RESPONSE:
   209          ret = "ALLOCATE-ERROR-RESPONSE";
   210          break;
   211     case NR_STUN_MSG_REFRESH_REQUEST:
   212          ret = "REFRESH-REQUEST";
   213          break;
   214     case NR_STUN_MSG_REFRESH_RESPONSE:
   215          ret = "REFRESH-RESPONSE";
   216          break;
   217     case NR_STUN_MSG_REFRESH_ERROR_RESPONSE:
   218          ret = "REFRESH-ERROR-RESPONSE";
   219          break;
   220     case NR_STUN_MSG_SEND_INDICATION:
   221          ret = "SEND-INDICATION";
   222          break;
   223     case NR_STUN_MSG_DATA_INDICATION:
   224          ret = "DATA-INDICATION";
   225          break;
   226     case NR_STUN_MSG_PERMISSION_REQUEST:
   227          ret = "PERMISSION-REQUEST";
   228          break;
   229     case NR_STUN_MSG_PERMISSION_RESPONSE:
   230          ret = "PERMISSION-RESPONSE";
   231          break;
   232     case NR_STUN_MSG_PERMISSION_ERROR_RESPONSE:
   233          ret = "PERMISSION-ERROR-RESPONSE";
   234          break;
   235 #endif /* USE_TURN */
   237     default:
   238          /* ret remains 0 */
   239          break;
   240     }
   242     return ret;
   243 }
   245 int
   246 nr_random_alphanum(char *alphanum, int size)
   247 {
   248     static char alphanums[256] = {
   249         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
   250         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
   251         'U', 'V', 'W', 'X', 'Y', 'Z',
   252         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
   253         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
   254         'u', 'v', 'w', 'x', 'y', 'z',
   255         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   256         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
   257         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
   258         'U', 'V', 'W', 'X', 'Y', 'Z',
   259         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
   260         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
   261         'u', 'v', 'w', 'x', 'y', 'z',
   262         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   263         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
   264         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
   265         'U', 'V', 'W', 'X', 'Y', 'Z',
   266         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
   267         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
   268         'u', 'v', 'w', 'x', 'y', 'z',
   269         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   270         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
   271         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
   272         'U', 'V', 'W', 'X', 'Y', 'Z',
   273         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
   274         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
   275         'u', 'v', 'w', 'x', 'y', 'z',
   276         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   277         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
   278     int i;
   280     nr_crypto_random_bytes((UCHAR*)alphanum, size);
   282     /* now convert from binary to alphanumeric */
   283     for (i = 0; i < size; ++i)
   284         alphanum[i] = alphanums[(UCHAR)alphanum[i]];
   286     return 0;
   287 }

mercurial