1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/third_party/nICEr/src/stun/stun_util.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,287 @@ 1.4 +/* 1.5 +Copyright (c) 2007, Adobe Systems, Incorporated 1.6 +All rights reserved. 1.7 + 1.8 +Redistribution and use in source and binary forms, with or without 1.9 +modification, are permitted provided that the following conditions are 1.10 +met: 1.11 + 1.12 +* Redistributions of source code must retain the above copyright 1.13 + notice, this list of conditions and the following disclaimer. 1.14 + 1.15 +* Redistributions in binary form must reproduce the above copyright 1.16 + notice, this list of conditions and the following disclaimer in the 1.17 + documentation and/or other materials provided with the distribution. 1.18 + 1.19 +* Neither the name of Adobe Systems, Network Resonance nor the names of its 1.20 + contributors may be used to endorse or promote products derived from 1.21 + this software without specific prior written permission. 1.22 + 1.23 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.27 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.28 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.29 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.30 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.31 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.32 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.33 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 +*/ 1.35 + 1.36 + 1.37 +static char *RCSSTRING __UNUSED__="$Id: stun_util.c,v 1.2 2008/04/28 18:21:30 ekr Exp $"; 1.38 + 1.39 +#include <errno.h> 1.40 +#include <csi_platform.h> 1.41 + 1.42 +#ifdef WIN32 1.43 +#include <winsock2.h> 1.44 +#include <stdlib.h> 1.45 +#include <io.h> 1.46 +#include <time.h> 1.47 +#else /* UNIX */ 1.48 +#include <string.h> 1.49 +#endif /* end UNIX */ 1.50 +#include <assert.h> 1.51 + 1.52 +#include "stun.h" 1.53 +#include "stun_reg.h" 1.54 +#include "registry.h" 1.55 +#include "addrs.h" 1.56 +#include "transport_addr_reg.h" 1.57 +#include "nr_crypto.h" 1.58 +#include "hex.h" 1.59 + 1.60 + 1.61 +int NR_LOG_STUN = 0; 1.62 + 1.63 +int 1.64 +nr_stun_startup(void) 1.65 +{ 1.66 + int r,_status; 1.67 + 1.68 + if ((r=r_log_register("stun", &NR_LOG_STUN))) 1.69 + ABORT(r); 1.70 + 1.71 + _status=0; 1.72 + abort: 1.73 + return _status; 1.74 +} 1.75 + 1.76 +int 1.77 +nr_stun_xor_mapped_address(UINT4 magicCookie, nr_transport_addr *from, nr_transport_addr *to) 1.78 +{ 1.79 + int _status; 1.80 + 1.81 + switch (from->ip_version) { 1.82 + case NR_IPV4: 1.83 + nr_ip4_port_to_transport_addr( 1.84 + (ntohl(from->u.addr4.sin_addr.s_addr) ^ magicCookie), 1.85 + (ntohs(from->u.addr4.sin_port) ^ (magicCookie>>16)), 1.86 + from->protocol, to); 1.87 + break; 1.88 + case NR_IPV6: 1.89 + assert(0); 1.90 + ABORT(R_INTERNAL); 1.91 + break; 1.92 + default: 1.93 + assert(0); 1.94 + ABORT(R_INTERNAL); 1.95 + break; 1.96 + } 1.97 + 1.98 + _status = 0; 1.99 + abort: 1.100 + return _status; 1.101 +} 1.102 + 1.103 +int 1.104 +nr_stun_find_local_addresses(nr_local_addr addrs[], int maxaddrs, int *count) 1.105 +{ 1.106 + int r,_status; 1.107 + NR_registry *children = 0; 1.108 + 1.109 + if ((r=NR_reg_get_child_count(NR_STUN_REG_PREF_ADDRESS_PRFX, (unsigned int*)count))) 1.110 + if (r == R_NOT_FOUND) 1.111 + *count = 0; 1.112 + else 1.113 + ABORT(r); 1.114 + 1.115 + if (*count == 0) { 1.116 + if ((r=nr_stun_get_addrs(addrs, maxaddrs, 1, count))) 1.117 + ABORT(r); 1.118 + 1.119 + goto done; 1.120 + } 1.121 + 1.122 + if (*count >= maxaddrs) { 1.123 + r_log(NR_LOG_STUN, LOG_INFO, "Address list truncated from %d to %d", *count, maxaddrs); 1.124 + *count = maxaddrs; 1.125 + } 1.126 + 1.127 +#if 0 1.128 + if (*count > 0) { 1.129 + /* TODO(ekr@rtfm.com): Commented out 2012-07-26. 1.130 + 1.131 + This code is currently not used in Firefox and needs to be 1.132 + ported to 64-bit */ 1.133 + children = RCALLOC((*count + 10) * sizeof(*children)); 1.134 + if (!children) 1.135 + ABORT(R_NO_MEMORY); 1.136 + 1.137 + assert(sizeof(size_t) == sizeof(*count)); 1.138 + 1.139 + if ((r=NR_reg_get_children(NR_STUN_REG_PREF_ADDRESS_PRFX, children, (size_t)(*count + 10), (size_t*)count))) 1.140 + ABORT(r); 1.141 + 1.142 + for (i = 0; i < *count; ++i) { 1.143 + if ((r=nr_reg_get_transport_addr(children[i], 0, &addrs[i].addr))) 1.144 + ABORT(r); 1.145 + } 1.146 + } 1.147 +#endif 1.148 + 1.149 + done: 1.150 + 1.151 + _status=0; 1.152 + abort: 1.153 + RFREE(children); 1.154 + return _status; 1.155 +} 1.156 + 1.157 +int 1.158 +nr_stun_different_transaction(UCHAR *msg, int len, nr_stun_message *req) 1.159 +{ 1.160 + int _status; 1.161 + nr_stun_message_header header; 1.162 + char reqid[44]; 1.163 + char msgid[44]; 1.164 + int len2; 1.165 + 1.166 + if (sizeof(header) > len) 1.167 + ABORT(R_FAILED); 1.168 + 1.169 + assert(sizeof(header.id) == sizeof(UINT12)); 1.170 + 1.171 + memcpy(&header, msg, sizeof(header)); 1.172 + 1.173 + if (memcmp(&req->header.id, &header.id, sizeof(header.id))) { 1.174 + nr_nbin2hex((UCHAR*)&req->header.id, sizeof(req->header.id), reqid, sizeof(reqid), &len2); 1.175 + nr_nbin2hex((UCHAR*)&header.id, sizeof(header.id), msgid, sizeof(msgid), &len2); 1.176 + r_log(NR_LOG_STUN, LOG_DEBUG, "Mismatched message IDs %s/%s", reqid, msgid); 1.177 + ABORT(R_NOT_FOUND); 1.178 + } 1.179 + 1.180 + _status=0; 1.181 + abort: 1.182 + return _status; 1.183 +} 1.184 + 1.185 +char* 1.186 +nr_stun_msg_type(int type) 1.187 +{ 1.188 + char *ret = 0; 1.189 + 1.190 + switch (type) { 1.191 + case NR_STUN_MSG_BINDING_REQUEST: 1.192 + ret = "BINDING-REQUEST"; 1.193 + break; 1.194 + case NR_STUN_MSG_BINDING_INDICATION: 1.195 + ret = "BINDING-INDICATION"; 1.196 + break; 1.197 + case NR_STUN_MSG_BINDING_RESPONSE: 1.198 + ret = "BINDING-RESPONSE"; 1.199 + break; 1.200 + case NR_STUN_MSG_BINDING_ERROR_RESPONSE: 1.201 + ret = "BINDING-ERROR-RESPONSE"; 1.202 + break; 1.203 + 1.204 +#ifdef USE_TURN 1.205 + case NR_STUN_MSG_ALLOCATE_REQUEST: 1.206 + ret = "ALLOCATE-REQUEST"; 1.207 + break; 1.208 + case NR_STUN_MSG_ALLOCATE_RESPONSE: 1.209 + ret = "ALLOCATE-RESPONSE"; 1.210 + break; 1.211 + case NR_STUN_MSG_ALLOCATE_ERROR_RESPONSE: 1.212 + ret = "ALLOCATE-ERROR-RESPONSE"; 1.213 + break; 1.214 + case NR_STUN_MSG_REFRESH_REQUEST: 1.215 + ret = "REFRESH-REQUEST"; 1.216 + break; 1.217 + case NR_STUN_MSG_REFRESH_RESPONSE: 1.218 + ret = "REFRESH-RESPONSE"; 1.219 + break; 1.220 + case NR_STUN_MSG_REFRESH_ERROR_RESPONSE: 1.221 + ret = "REFRESH-ERROR-RESPONSE"; 1.222 + break; 1.223 + case NR_STUN_MSG_SEND_INDICATION: 1.224 + ret = "SEND-INDICATION"; 1.225 + break; 1.226 + case NR_STUN_MSG_DATA_INDICATION: 1.227 + ret = "DATA-INDICATION"; 1.228 + break; 1.229 + case NR_STUN_MSG_PERMISSION_REQUEST: 1.230 + ret = "PERMISSION-REQUEST"; 1.231 + break; 1.232 + case NR_STUN_MSG_PERMISSION_RESPONSE: 1.233 + ret = "PERMISSION-RESPONSE"; 1.234 + break; 1.235 + case NR_STUN_MSG_PERMISSION_ERROR_RESPONSE: 1.236 + ret = "PERMISSION-ERROR-RESPONSE"; 1.237 + break; 1.238 +#endif /* USE_TURN */ 1.239 + 1.240 + default: 1.241 + /* ret remains 0 */ 1.242 + break; 1.243 + } 1.244 + 1.245 + return ret; 1.246 +} 1.247 + 1.248 +int 1.249 +nr_random_alphanum(char *alphanum, int size) 1.250 +{ 1.251 + static char alphanums[256] = { 1.252 + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 1.253 + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 1.254 + 'U', 'V', 'W', 'X', 'Y', 'Z', 1.255 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.256 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 1.257 + 'u', 'v', 'w', 'x', 'y', 'z', 1.258 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 1.259 + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 1.260 + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 1.261 + 'U', 'V', 'W', 'X', 'Y', 'Z', 1.262 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.263 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 1.264 + 'u', 'v', 'w', 'x', 'y', 'z', 1.265 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 1.266 + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 1.267 + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 1.268 + 'U', 'V', 'W', 'X', 'Y', 'Z', 1.269 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.270 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 1.271 + 'u', 'v', 'w', 'x', 'y', 'z', 1.272 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 1.273 + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 1.274 + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 1.275 + 'U', 'V', 'W', 'X', 'Y', 'Z', 1.276 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.277 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 1.278 + 'u', 'v', 'w', 'x', 'y', 'z', 1.279 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 1.280 + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' }; 1.281 + int i; 1.282 + 1.283 + nr_crypto_random_bytes((UCHAR*)alphanum, size); 1.284 + 1.285 + /* now convert from binary to alphanumeric */ 1.286 + for (i = 0; i < size; ++i) 1.287 + alphanum[i] = alphanums[(UCHAR)alphanum[i]]; 1.288 + 1.289 + return 0; 1.290 +}