media/mtransport/third_party/nICEr/src/ice/ice_parser.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.

michael@0 1 /*
michael@0 2 Copyright (c) 2007, Adobe Systems, Incorporated
michael@0 3 All rights reserved.
michael@0 4
michael@0 5 Redistribution and use in source and binary forms, with or without
michael@0 6 modification, are permitted provided that the following conditions are
michael@0 7 met:
michael@0 8
michael@0 9 * Redistributions of source code must retain the above copyright
michael@0 10 notice, this list of conditions and the following disclaimer.
michael@0 11
michael@0 12 * Redistributions in binary form must reproduce the above copyright
michael@0 13 notice, this list of conditions and the following disclaimer in the
michael@0 14 documentation and/or other materials provided with the distribution.
michael@0 15
michael@0 16 * Neither the name of Adobe Systems, Network Resonance nor the names of its
michael@0 17 contributors may be used to endorse or promote products derived from
michael@0 18 this software without specific prior written permission.
michael@0 19
michael@0 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32
michael@0 33
michael@0 34
michael@0 35 static char *RCSSTRING __UNUSED__="$Id: ice_parser.c,v 1.2 2008/04/28 17:59:01 ekr Exp $";
michael@0 36
michael@0 37 #include <csi_platform.h>
michael@0 38 #include <sys/types.h>
michael@0 39 #ifdef WIN32
michael@0 40 #include <winsock2.h>
michael@0 41 #else
michael@0 42 #include <sys/socket.h>
michael@0 43 #include <netinet/in.h>
michael@0 44 #include <arpa/inet.h>
michael@0 45 #include <strings.h>
michael@0 46 #endif
michael@0 47 #include <string.h>
michael@0 48 #include <assert.h>
michael@0 49 #include <ctype.h>
michael@0 50 #include "nr_api.h"
michael@0 51 #include "ice_ctx.h"
michael@0 52 #include "ice_candidate.h"
michael@0 53 #include "ice_reg.h"
michael@0 54
michael@0 55 static void
michael@0 56 skip_whitespace(char **str)
michael@0 57 {
michael@0 58 char *c = *str;
michael@0 59 while (*c == ' ')
michael@0 60 ++c;
michael@0 61
michael@0 62 *str = c;
michael@0 63 }
michael@0 64
michael@0 65 static void
michael@0 66 fast_forward(char **str, int skip)
michael@0 67 {
michael@0 68 char *c = *str;
michael@0 69 while (*c != '\0' && skip-- > 0)
michael@0 70 ++c;
michael@0 71
michael@0 72 *str = c;
michael@0 73 }
michael@0 74
michael@0 75 static void
michael@0 76 skip_to_past_space(char **str)
michael@0 77 {
michael@0 78 char *c = *str;
michael@0 79 while (*c != ' ' && *c != '\0')
michael@0 80 ++c;
michael@0 81
michael@0 82 *str = c;
michael@0 83
michael@0 84 skip_whitespace(str);
michael@0 85 }
michael@0 86
michael@0 87 static int
michael@0 88 grab_token(char **str, char **out)
michael@0 89 {
michael@0 90 int _status;
michael@0 91 char *c = *str;
michael@0 92 int len;
michael@0 93 char *tmp;
michael@0 94
michael@0 95 while (*c != ' ' && *c != '\0')
michael@0 96 ++c;
michael@0 97
michael@0 98 len = c - *str;
michael@0 99
michael@0 100 tmp = RMALLOC(len + 1);
michael@0 101 if (!tmp)
michael@0 102 ABORT(R_NO_MEMORY);
michael@0 103
michael@0 104 memcpy(tmp, *str, len);
michael@0 105 tmp[len] = '\0';
michael@0 106
michael@0 107 *str = c;
michael@0 108 *out = tmp;
michael@0 109
michael@0 110 _status = 0;
michael@0 111 abort:
michael@0 112 return _status;
michael@0 113 }
michael@0 114
michael@0 115 int
michael@0 116 nr_ice_peer_candidate_from_attribute(nr_ice_ctx *ctx,char *orig,nr_ice_media_stream *stream,nr_ice_candidate **candp)
michael@0 117 {
michael@0 118 int r,_status;
michael@0 119 char* str = orig;
michael@0 120 nr_ice_candidate *cand;
michael@0 121 char *connection_address=0;
michael@0 122 unsigned int port;
michael@0 123 in_addr_t addr;
michael@0 124 int i;
michael@0 125 unsigned int component_id;
michael@0 126 char *rel_addr=0;
michael@0 127
michael@0 128 if(!(cand=RCALLOC(sizeof(nr_ice_candidate))))
michael@0 129 ABORT(R_NO_MEMORY);
michael@0 130
michael@0 131 if(!(cand->label=r_strdup(orig)))
michael@0 132 ABORT(R_NO_MEMORY);
michael@0 133
michael@0 134 cand->ctx=ctx;
michael@0 135 cand->isock=0;
michael@0 136 cand->state=NR_ICE_CAND_PEER_CANDIDATE_UNPAIRED;
michael@0 137 cand->stream=stream;
michael@0 138 skip_whitespace(&str);
michael@0 139
michael@0 140 /* Candidate attr */
michael@0 141 if (strncasecmp(str, "candidate:", 10))
michael@0 142 ABORT(R_BAD_DATA);
michael@0 143
michael@0 144 fast_forward(&str, 10);
michael@0 145 if (*str == '\0')
michael@0 146 ABORT(R_BAD_DATA);
michael@0 147
michael@0 148 skip_whitespace(&str);
michael@0 149 if (*str == '\0')
michael@0 150 ABORT(R_BAD_DATA);
michael@0 151
michael@0 152 /* Foundation */
michael@0 153 if ((r=grab_token(&str, &cand->foundation)))
michael@0 154 ABORT(r);
michael@0 155
michael@0 156 if (*str == '\0')
michael@0 157 ABORT(R_BAD_DATA);
michael@0 158
michael@0 159 skip_whitespace(&str);
michael@0 160 if (*str == '\0')
michael@0 161 ABORT(R_BAD_DATA);
michael@0 162
michael@0 163 /* component */
michael@0 164 if (sscanf(str, "%u", &component_id) != 1)
michael@0 165 ABORT(R_BAD_DATA);
michael@0 166
michael@0 167 if (component_id < 1 || component_id > 256)
michael@0 168 ABORT(R_BAD_DATA);
michael@0 169
michael@0 170 cand->component_id = (UCHAR)component_id;
michael@0 171
michael@0 172 skip_to_past_space(&str);
michael@0 173 if (*str == '\0')
michael@0 174 ABORT(R_BAD_DATA);
michael@0 175
michael@0 176 /* Protocol */
michael@0 177 if (strncasecmp(str, "UDP", 3))
michael@0 178 ABORT(R_BAD_DATA);
michael@0 179
michael@0 180 fast_forward(&str, 3);
michael@0 181 if (*str == '\0')
michael@0 182 ABORT(R_BAD_DATA);
michael@0 183
michael@0 184 skip_whitespace(&str);
michael@0 185 if (*str == '\0')
michael@0 186 ABORT(R_BAD_DATA);
michael@0 187
michael@0 188 /* priority */
michael@0 189 if (sscanf(str, "%u", &cand->priority) != 1)
michael@0 190 ABORT(R_BAD_DATA);
michael@0 191
michael@0 192 if (cand->priority < 1)
michael@0 193 ABORT(R_BAD_DATA);
michael@0 194
michael@0 195 skip_to_past_space(&str);
michael@0 196 if (*str == '\0')
michael@0 197 ABORT(R_BAD_DATA);
michael@0 198
michael@0 199 /* Peer address/port */
michael@0 200 if ((r=grab_token(&str, &connection_address)))
michael@0 201 ABORT(r);
michael@0 202
michael@0 203 if (*str == '\0')
michael@0 204 ABORT(R_BAD_DATA);
michael@0 205
michael@0 206 addr = inet_addr(connection_address);
michael@0 207 if (addr == INADDR_NONE)
michael@0 208 ABORT(R_BAD_DATA);
michael@0 209
michael@0 210 skip_whitespace(&str);
michael@0 211 if (*str == '\0')
michael@0 212 ABORT(R_BAD_DATA);
michael@0 213
michael@0 214 if (sscanf(str, "%u", &port) != 1)
michael@0 215 ABORT(R_BAD_DATA);
michael@0 216
michael@0 217 if (port < 1 || port > 0x0FFFF)
michael@0 218 ABORT(R_BAD_DATA);
michael@0 219
michael@0 220 /* Assume v4 for now */
michael@0 221 if(r=nr_ip4_port_to_transport_addr(ntohl(addr),port,IPPROTO_UDP,&cand->addr))
michael@0 222 ABORT(r);
michael@0 223
michael@0 224 skip_to_past_space(&str);
michael@0 225 if (*str == '\0')
michael@0 226 ABORT(R_BAD_DATA);
michael@0 227
michael@0 228 /* Type */
michael@0 229 if (strncasecmp("typ", str, 3))
michael@0 230 ABORT(R_BAD_DATA);
michael@0 231
michael@0 232 fast_forward(&str, 3);
michael@0 233 if (*str == '\0')
michael@0 234 ABORT(R_BAD_DATA);
michael@0 235
michael@0 236 skip_whitespace(&str);
michael@0 237 if (*str == '\0')
michael@0 238 ABORT(R_BAD_DATA);
michael@0 239
michael@0 240 assert(nr_ice_candidate_type_names[0] == 0);
michael@0 241 #if __STDC_VERSION__ >= 201112L
michael@0 242 _Static_assert(nr_ice_candidate_type_names[0] == 0,"Candidate name array is misformatted");
michael@0 243 #endif
michael@0 244
michael@0 245 for (i = 1; nr_ice_candidate_type_names[i]; ++i) {
michael@0 246 if(!strncasecmp(nr_ice_candidate_type_names[i], str, strlen(nr_ice_candidate_type_names[i]))) {
michael@0 247 cand->type=i;
michael@0 248 break;
michael@0 249 }
michael@0 250 }
michael@0 251 if (nr_ice_candidate_type_names[i] == 0)
michael@0 252 ABORT(R_BAD_DATA);
michael@0 253
michael@0 254 fast_forward(&str, strlen(nr_ice_candidate_type_names[i]));
michael@0 255
michael@0 256 /* Look for the other side's raddr, rport */
michael@0 257 /* raddr, rport */
michael@0 258 switch (cand->type) {
michael@0 259 case HOST:
michael@0 260 break;
michael@0 261 case SERVER_REFLEXIVE:
michael@0 262 case PEER_REFLEXIVE:
michael@0 263 case RELAYED:
michael@0 264
michael@0 265 skip_whitespace(&str);
michael@0 266 if (*str == '\0')
michael@0 267 ABORT(R_BAD_DATA);
michael@0 268
michael@0 269 if (strncasecmp("raddr", str, 5))
michael@0 270 ABORT(R_BAD_DATA);
michael@0 271
michael@0 272 fast_forward(&str, 5);
michael@0 273 if (*str == '\0')
michael@0 274 ABORT(R_BAD_DATA);
michael@0 275
michael@0 276 skip_whitespace(&str);
michael@0 277 if (*str == '\0')
michael@0 278 ABORT(R_BAD_DATA);
michael@0 279
michael@0 280 if ((r=grab_token(&str, &rel_addr)))
michael@0 281 ABORT(r);
michael@0 282
michael@0 283 if (*str == '\0')
michael@0 284 ABORT(R_BAD_DATA);
michael@0 285
michael@0 286 addr = inet_addr(rel_addr);
michael@0 287 if (addr == INADDR_NONE)
michael@0 288 ABORT(R_BAD_DATA);
michael@0 289
michael@0 290 skip_whitespace(&str);
michael@0 291 if (*str == '\0')
michael@0 292 ABORT(R_BAD_DATA);
michael@0 293
michael@0 294 if (strncasecmp("rport", str, 5))
michael@0 295 ABORT(R_BAD_DATA);
michael@0 296
michael@0 297 fast_forward(&str, 5);
michael@0 298 if (*str == '\0')
michael@0 299 ABORT(R_BAD_DATA);
michael@0 300
michael@0 301 skip_whitespace(&str);
michael@0 302 if (*str == '\0')
michael@0 303 ABORT(R_BAD_DATA);
michael@0 304
michael@0 305 if (sscanf(str, "%u", &port) != 1)
michael@0 306 ABORT(R_BAD_DATA);
michael@0 307
michael@0 308 if (port < 1 || port > 0x0FFFF)
michael@0 309 ABORT(R_BAD_DATA);
michael@0 310
michael@0 311 /* Assume v4 for now */
michael@0 312 if(r=nr_ip4_port_to_transport_addr(ntohl(addr),port,IPPROTO_UDP,&cand->base))
michael@0 313 ABORT(r);
michael@0 314
michael@0 315 skip_to_past_space(&str);
michael@0 316 /* it's expected to be at EOD at this point */
michael@0 317
michael@0 318 break;
michael@0 319 default:
michael@0 320 ABORT(R_INTERNAL);
michael@0 321 break;
michael@0 322 }
michael@0 323
michael@0 324 skip_whitespace(&str);
michael@0 325
michael@0 326 /* Ignore extensions per RFC 5245 S 15.1 */
michael@0 327 #if 0
michael@0 328 /* This used to be an assert, but we don't want to exit on invalid
michael@0 329 remote data */
michael@0 330 if (strlen(str) != 0) {
michael@0 331 ABORT(R_BAD_DATA);
michael@0 332 }
michael@0 333 #endif
michael@0 334
michael@0 335 nr_ice_candidate_compute_codeword(cand);
michael@0 336
michael@0 337 *candp=cand;
michael@0 338
michael@0 339 _status=0;
michael@0 340 abort:
michael@0 341 if (_status){
michael@0 342 r_log(LOG_ICE,LOG_WARNING,"ICE(%s): Error parsing attribute: %s",ctx->label,orig);
michael@0 343 nr_ice_candidate_destroy(&cand);
michael@0 344 }
michael@0 345
michael@0 346 RFREE(connection_address);
michael@0 347 RFREE(rel_addr);
michael@0 348 return(_status);
michael@0 349 }
michael@0 350
michael@0 351
michael@0 352 int
michael@0 353 nr_ice_peer_ctx_parse_media_stream_attribute(nr_ice_peer_ctx *pctx, nr_ice_media_stream *stream, char *attr)
michael@0 354 {
michael@0 355 int r,_status;
michael@0 356 char *orig = 0;
michael@0 357 char *str;
michael@0 358
michael@0 359 orig = str = attr;
michael@0 360
michael@0 361 if (!strncasecmp(str, "ice-ufrag:", 10)) {
michael@0 362 fast_forward(&str, 10);
michael@0 363 if (*str == '\0')
michael@0 364 ABORT(R_BAD_DATA);
michael@0 365
michael@0 366 skip_whitespace(&str);
michael@0 367 if (*str == '\0')
michael@0 368 ABORT(R_BAD_DATA);
michael@0 369
michael@0 370 if ((r=grab_token(&str, &stream->ufrag)))
michael@0 371 ABORT(r);
michael@0 372 }
michael@0 373 else if (!strncasecmp(str, "ice-pwd:", 8)) {
michael@0 374 fast_forward(&str, 8);
michael@0 375 if (*str == '\0')
michael@0 376 ABORT(R_BAD_DATA);
michael@0 377
michael@0 378 skip_whitespace(&str);
michael@0 379 if (*str == '\0')
michael@0 380 ABORT(R_BAD_DATA);
michael@0 381
michael@0 382 if ((r=grab_token(&str, &stream->pwd)))
michael@0 383 ABORT(r);
michael@0 384 }
michael@0 385 else {
michael@0 386 ABORT(R_BAD_DATA);
michael@0 387 }
michael@0 388
michael@0 389 skip_whitespace(&str);
michael@0 390
michael@0 391 /* RFC 5245 grammar doesn't have an extension point for ice-pwd or
michael@0 392 ice-ufrag: if there's anything left on the line, we treat it as bad. */
michael@0 393 if (str[0] != '\0') {
michael@0 394 ABORT(R_BAD_DATA);
michael@0 395 }
michael@0 396
michael@0 397 _status=0;
michael@0 398 abort:
michael@0 399 if (_status) {
michael@0 400 if (orig)
michael@0 401 r_log(LOG_ICE,LOG_WARNING,"ICE-PEER(%s): Error parsing attribute: %s",pctx->label,orig);
michael@0 402 }
michael@0 403
michael@0 404 return(_status);
michael@0 405 }
michael@0 406
michael@0 407 int
michael@0 408 nr_ice_peer_ctx_parse_global_attributes(nr_ice_peer_ctx *pctx, char **attrs, int attr_ct)
michael@0 409 {
michael@0 410 int r,_status;
michael@0 411 int i;
michael@0 412 char *orig = 0;
michael@0 413 char *str;
michael@0 414 char *component_id = 0;
michael@0 415 char *connection_address = 0;
michael@0 416 unsigned int port;
michael@0 417 in_addr_t addr;
michael@0 418 char *ice_option_tag = 0;
michael@0 419
michael@0 420 for(i=0;i<attr_ct;i++){
michael@0 421 orig = str = attrs[i];
michael@0 422
michael@0 423 component_id = 0;
michael@0 424 connection_address = 0;
michael@0 425 ice_option_tag = 0;
michael@0 426
michael@0 427 if (!strncasecmp(str, "remote-candidates:", 18)) {
michael@0 428 fast_forward(&str, 18);
michael@0 429 skip_whitespace(&str);
michael@0 430
michael@0 431 while (*str != '\0') {
michael@0 432 if ((r=grab_token(&str, &component_id)))
michael@0 433 ABORT(r);
michael@0 434
michael@0 435 if (*str == '\0')
michael@0 436 ABORT(R_BAD_DATA);
michael@0 437
michael@0 438 skip_whitespace(&str);
michael@0 439 if (*str == '\0')
michael@0 440 ABORT(R_BAD_DATA);
michael@0 441
michael@0 442 if ((r=grab_token(&str, &connection_address)))
michael@0 443 ABORT(r);
michael@0 444
michael@0 445 if (*str == '\0')
michael@0 446 ABORT(R_BAD_DATA);
michael@0 447
michael@0 448 addr = inet_addr(connection_address);
michael@0 449 if (addr == INADDR_NONE)
michael@0 450 ABORT(R_BAD_DATA);
michael@0 451
michael@0 452 skip_whitespace(&str);
michael@0 453 if (*str == '\0')
michael@0 454 ABORT(R_BAD_DATA);
michael@0 455
michael@0 456 if (sscanf(str, "%u", &port) != 1)
michael@0 457 ABORT(R_BAD_DATA);
michael@0 458
michael@0 459 if (port < 1 || port > 0x0FFFF)
michael@0 460 ABORT(R_BAD_DATA);
michael@0 461
michael@0 462 skip_to_past_space(&str);
michael@0 463
michael@0 464 #if 0
michael@0 465 /* TODO: !nn! just drop on the floor for now, later put somewhere */
michael@0 466 /* Assume v4 for now */
michael@0 467 if(r=nr_ip4_port_to_transport_addr(ntohl(addr),port,IPPROTO_UDP,&candidate->base))
michael@0 468 ABORT(r);
michael@0 469
michael@0 470 TAILQ_INSERT_TAIL(head, elm, field);
michael@0 471 #endif
michael@0 472
michael@0 473 component_id = 0; /* prevent free */
michael@0 474 RFREE(connection_address);
michael@0 475 connection_address = 0; /* prevent free */
michael@0 476 }
michael@0 477 }
michael@0 478 else if (!strncasecmp(str, "ice-lite", 8)) {
michael@0 479 pctx->peer_lite = 1;
michael@0 480
michael@0 481 fast_forward(&str, 8);
michael@0 482 }
michael@0 483 else if (!strncasecmp(str, "ice-mismatch", 12)) {
michael@0 484 pctx->peer_ice_mismatch = 1;
michael@0 485
michael@0 486 fast_forward(&str, 12);
michael@0 487 }
michael@0 488 else if (!strncasecmp(str, "ice-ufrag:", 10)) {
michael@0 489 fast_forward(&str, 10);
michael@0 490 if (*str == '\0')
michael@0 491 ABORT(R_BAD_DATA);
michael@0 492
michael@0 493 skip_whitespace(&str);
michael@0 494 if (*str == '\0')
michael@0 495 ABORT(R_BAD_DATA);
michael@0 496
michael@0 497 RFREE(pctx->peer_ufrag);
michael@0 498 pctx->peer_ufrag = 0;
michael@0 499 if ((r=grab_token(&str, &pctx->peer_ufrag)))
michael@0 500 ABORT(r);
michael@0 501 }
michael@0 502 else if (!strncasecmp(str, "ice-pwd:", 8)) {
michael@0 503 fast_forward(&str, 8);
michael@0 504 if (*str == '\0')
michael@0 505 ABORT(R_BAD_DATA);
michael@0 506
michael@0 507 skip_whitespace(&str);
michael@0 508 if (*str == '\0')
michael@0 509 ABORT(R_BAD_DATA);
michael@0 510
michael@0 511 RFREE(pctx->peer_pwd);
michael@0 512 pctx->peer_pwd = 0;
michael@0 513 if ((r=grab_token(&str, &pctx->peer_pwd)))
michael@0 514 ABORT(r);
michael@0 515 }
michael@0 516 else if (!strncasecmp(str, "ice-options:", 12)) {
michael@0 517 fast_forward(&str, 12);
michael@0 518 skip_whitespace(&str);
michael@0 519
michael@0 520 while (*str != '\0') {
michael@0 521 if ((r=grab_token(&str, &ice_option_tag)))
michael@0 522 ABORT(r);
michael@0 523
michael@0 524 skip_whitespace(&str);
michael@0 525
michael@0 526 //TODO: for now, just throw away; later put somewhere
michael@0 527 RFREE(ice_option_tag);
michael@0 528
michael@0 529 ice_option_tag = 0; /* prevent free */
michael@0 530 }
michael@0 531 }
michael@0 532 else {
michael@0 533 ABORT(R_BAD_DATA);
michael@0 534 }
michael@0 535
michael@0 536 skip_whitespace(&str);
michael@0 537
michael@0 538 /* RFC 5245 grammar doesn't have an extension point for any of the
michael@0 539 preceding attributes: if there's anything left on the line, we
michael@0 540 treat it as bad data. */
michael@0 541 if (str[0] != '\0') {
michael@0 542 ABORT(R_BAD_DATA);
michael@0 543 }
michael@0 544 }
michael@0 545
michael@0 546 _status=0;
michael@0 547 abort:
michael@0 548 if (_status) {
michael@0 549 if (orig)
michael@0 550 r_log(LOG_ICE,LOG_WARNING,"ICE-PEER(%s): Error parsing attribute: %s",pctx->label,orig);
michael@0 551 }
michael@0 552
michael@0 553 RFREE(connection_address);
michael@0 554 RFREE(component_id);
michael@0 555 RFREE(ice_option_tag);
michael@0 556 return(_status);
michael@0 557 }
michael@0 558

mercurial