sdp_parse.c 46 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
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
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup sdp_parser
 * @CFILE sdp_parse.c 
 * @brief Simple SDP parser interface.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
 *
 * @date  Created: Fri Feb 18 10:25:08 2000 ppessi
Pekka Pessi's avatar
Pekka Pessi committed
33
 * $Date: 2005/09/28 20:05:24 $
Pekka Pessi's avatar
Pekka Pessi committed
34 35 36
 */

const char _sdp_parse_c_id[] =
Pekka Pessi's avatar
Pekka Pessi committed
37
"$Id: sdp_parse.c,v 1.3 2005/09/28 20:05:24 ppessi Exp $";
Pekka Pessi's avatar
Pekka Pessi committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>

#include <su_alloc.h>

#include "sdp.h"

struct sdp_parser_s {
  su_home_t       pr_home[1];
  union {
    char          pru_error[128];
    sdp_session_t pru_session[1];
  } pr_output;
  char      *pr_message;
  su_home_t *pr_orig_home;
60 61 62

  sdp_mode_t pr_session_mode;

Pekka Pessi's avatar
Pekka Pessi committed
63 64 65 66 67
  unsigned   pr_ok : 1;

  unsigned   pr_strict : 1;
  unsigned   pr_anynet : 1;
  unsigned   pr_mode_0000 : 1;
68
  unsigned   pr_mode_manual : 1;
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  unsigned   pr_insane : 1;
  unsigned   pr_c_missing : 1;
  unsigned   pr_config : 1;
};

#define is_posdigit(c) ((c) >= '1' && (c) <= '9')
#define is_digit(c) ((c) >= '0' && (c) <= '9')
#define is_space(c) ((c) == ' ')
#define is_tab(c) ((c) == '\t')

#define pr_error   pr_output.pru_error
#define pr_session pr_output.pru_session

#define STRICT(pr) (pr->pr_strict)

/* Static parser object used when running out of memory */
static const struct sdp_parser_s no_mem_error =
{
  { SU_HOME_INIT(no_mem_error) },
  { "sdp: not enough memory" }
};

/* Internal prototypes */
static void parse_message(sdp_parser_t *p);
93
static void parsing_error(sdp_parser_t *p, char const *fmt, ...);
Pekka Pessi's avatar
Pekka Pessi committed
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

/** Parse an SDP message.
 *
 * The function sdp_parse() parses an SDP message @a msg of size @a
 * msgsize. Parsing is done according to the given @a flags. The SDP message
 * may not contain a @c NUL.
 * 
 * The parsing result is stored to an @c sdp_session_t structure.
 *
 * @param home    memory home
 * @param msg     pointer to message
 * @param msgsize size of the message (excluding final NUL, if any)
 * @param flags   flags affecting the parsing.
 *
 * The following flags are defined:
 *
 * @li @c sdp_f_strict Parser should accept only messages conforming strictly 
 *   to the specification.
 * @li @c sdp_f_anynet Parser accepts unknown network or address types.
 *
 * @return
 * The function sdp_parse() returns always a valid parser handle.  
 */
sdp_parser_t *
sdp_parse(su_home_t *home, char const msg[], int msgsize, int flags)
{
  sdp_parser_t *p = su_home_clone(home, sizeof(*p));
121 122 123 124 125 126 127 128 129 130 131
  char *b;

  if (msgsize == -1 && msg)
    msgsize = strlen(msg);

  if (msgsize < 0 || msg == NULL) {
    parsing_error(p, "invalid input message");
    return p;
  }

  b = su_alloc(p->pr_home, msgsize + 1);
Pekka Pessi's avatar
Pekka Pessi committed
132 133 134 135 136 137 138 139 140 141 142 143 144

  if (p && b) {
    p->pr_orig_home = home;
    strncpy(b, msg, msgsize);
    b[msgsize] = 0;
    p->pr_message = b;
    p->pr_strict = (flags & sdp_f_strict) != 0;
    p->pr_anynet = (flags & sdp_f_anynet) != 0;
    p->pr_mode_0000 = (flags & sdp_f_mode_0000) != 0;
    p->pr_insane = (flags & sdp_f_insane) != 0;
    p->pr_c_missing = (flags & sdp_f_c_missing) != 0;
    if (flags & sdp_f_config)
      p->pr_c_missing = 1, p->pr_config = 1;
145 146 147
    p->pr_mode_manual = (flags & sdp_f_mode_manual) != 0;
    p->pr_session_mode = sdp_sendrecv;

Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152 153 154 155 156 157 158
    parse_message(p);
    
    return p;
  }

  if (p)
    sdp_parser_free(p);

  return (sdp_parser_t*)&no_mem_error;
}

Pekka Pessi's avatar
Pekka Pessi committed
159 160 161 162 163 164 165 166 167 168

/** Obtain memory home used by parser */
su_home_t *sdp_parser_home(sdp_parser_t *parser)
{
  if (parser != &no_mem_error)
    return parser->pr_home;
  else
    return NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
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
/** Retrieve an SDP session structure.
 *
 * The function sdp_session() returns a pointer to the SDP session
 * structure associated with the SDP parser @a p. The pointer and all the
 * data in the structure are valid until @c sdp_parser_free() is called.
 *
 * @param p SDP parser
 *
 * @return
 *   The function sdp_session() returns a pointer to an parsed SDP message
 *   or @c NULL, if an error has occurred.  */
sdp_session_t *
sdp_session(sdp_parser_t *p)
{
  return p && p->pr_ok ? p->pr_session : NULL;
}

/** Get a parsing error message.
 *
 * The function sdp_parsing_error() returns the error message associated
 * with an SDP parser @a p.
 *
 * @param p SDP parser
 *
 * @return 
 * The function sdp_parsing_error() returns a C string describing parsing
 * error, or @c NULL if no error occurred.
 */
char const *sdp_parsing_error(sdp_parser_t *p)
{
  return !p->pr_ok ? p->pr_error : NULL;
}

/** Free an SDP parser.
 *
 * The function sdp_parser_free() frees an SDP parser object along with
 * the memory blocks associated with it.
 *
 * @param p pointer to the SDP parser to be freed 
 */
void sdp_parser_free(sdp_parser_t *p)
{
  if (p && p != &no_mem_error)
    su_free(p->pr_orig_home, p);
}

/* ========================================================================= */

/* =========================================================================
 * Private part
 */

/* Parsing tokens */
#define SPACE " "
#define TAB   "\011"
#define CRLF  "\015\012"
#define ALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define DIGIT "0123456789"

/* ========================================================================= */
/* Parsing functions */

static void post_session(sdp_parser_t *p, sdp_session_t *sdp);
static void parse_origin(sdp_parser_t *p, char *r, sdp_origin_t **result);
static void parse_subject(sdp_parser_t *p, char *r, sdp_text_t **result);
static void parse_information(sdp_parser_t *p, char *r, sdp_text_t **result);
static void parse_uri(sdp_parser_t *p, char *r, sdp_text_t **result);
static void parse_email(sdp_parser_t *p, char *r, sdp_list_t **result);
static void parse_phone(sdp_parser_t *p, char *r, sdp_list_t **result);
static void parse_connection(sdp_parser_t *p, char *r, sdp_connection_t **result);
static void parse_bandwidth(sdp_parser_t *p, char *r, sdp_bandwidth_t **result);
static void parse_time(sdp_parser_t *p, char *r, sdp_time_t **result);
static void parse_repeat(sdp_parser_t *p, char *r, sdp_repeat_t **result);
static void parse_zone(sdp_parser_t *p, char *r, sdp_zone_t **result);
static void parse_key(sdp_parser_t *p, char *r, sdp_key_t **result);
static void parse_session_attr(sdp_parser_t *p, char *r, sdp_attribute_t **result);
static void parse_media(sdp_parser_t *p, char *r, sdp_media_t **result);
static void parse_payload(sdp_parser_t *p, char *r, sdp_rtpmap_t **result);
static void parse_media_attr(sdp_parser_t *p, char *r, sdp_media_t *m, 
			     sdp_attribute_t **result);
static int parse_rtpmap(sdp_parser_t *p, char *r, sdp_media_t *m);
static int parse_fmtp(sdp_parser_t *p, char *r, sdp_media_t *m);
static void parse_text_list(sdp_parser_t *p, char *r, sdp_list_t **result);

static void parse_descs(sdp_parser_t *p, char *r, char *m, sdp_media_t **result);

static int parse_ul(sdp_parser_t *p, char **r, unsigned long *result, 
		    unsigned long max_value);
static int parse_ull(sdp_parser_t *p, char **r, uint64_t *result, 
		     uint64_t max_value);
static void parse_alloc_error(sdp_parser_t *p, const char *typename);
static char *next(char **message, const char *sep, const char *strip);
static char *token(char **message, const char *sep, const char *legal, 
		   const char *strip);
#if 0
static void check_mandatory(sdp_parser_t *p, sdp_session_t *sdp);
#endif

/* -------------------------------------------------------------------------
 * Macro PARSE_ALLOC
 *
 * Description:
 *   This macro declares a pointer (v) of given type (t). It then allocates
 *   an structure of given type (t). If allocation was succesful, it assigns
 *   the XX_size member with appropriate value.
 */
#define PARSE_ALLOC(p, t, v) \
 t *v = su_salloc(p->pr_home, sizeof(*v)); \
 if (!v && (parse_alloc_error(p, #t), 1)) return; 

/* -------------------------------------------------------------------------
 * Macro PARSE_CHECK_REST
 *
 * Description:
 *   This macro check if there is extra data at the end of field.
 */
#define PARSE_CHECK_REST(p, s, n)\
 if (*s && (parsing_error(p, "extra data after %s (\"%.04s\")", n, s), 1)) \
    return

/* -------------------------------------------------------------------------
 * Function parse_message() - parse an SDP message 
 *
 * Description:
 *   This function parses an SDP message, which is copied into the 
 *   p->pr_message. The p->pr_message is modified during the parsing,
 *   and parts of it are returned in p->pr_session.
 *
 * Parameters:
 *   p - pointer to SDP parser object
 */
static void parse_message(sdp_parser_t *p)
{
/*
   announcement =        proto-version
                         origin-field
                         session-name-field
                         information-field
                         uri-field
                         email-fields
                         phone-fields
                         connection-field
                         bandwidth-fields
                         time-fields
                         key-field
                         attribute-fields
                         media-descriptions
*/

  sdp_session_t *sdp = p->pr_session;
  char *record, *rest;
  char const *strip;
  char *message = p->pr_message;
  char field = '\0';
  sdp_list_t **emails = &sdp->sdp_emails;
  sdp_list_t **phones = &sdp->sdp_phones;
  sdp_bandwidth_t **bandwidths = &sdp->sdp_bandwidths;
  sdp_time_t **times = &sdp->sdp_time;
  sdp_repeat_t **repeats = NULL;
  sdp_zone_t **zones = NULL;
  sdp_attribute_t **attributes = &sdp->sdp_attributes;

  if (!STRICT(p))
    strip = SPACE TAB;		/* skip initial whitespace */
  else
    strip = NULL;

  p->pr_ok = 1;
  p->pr_session->sdp_size = sizeof(p->pr_session);

  /* Require that version comes first */
  record = next(&message, CRLF, strip);
341

Pekka Pessi's avatar
Pekka Pessi committed
342
  if (!record || strcmp(record, "v=0")) {
343 344 345 346 347 348 349
    if (!p->pr_config || !record || record[1] != '=') {
      parsing_error(p, "bad SDP message");
      return;
    }
  }
  else {
    record = next(&message, CRLF, strip);
Pekka Pessi's avatar
Pekka Pessi committed
350 351
  }

352 353 354 355
  /*
    XXX - the lines in SDP are in certain order, which we don't check here.
     For stricter parsing we might want to parse o= and s= next.
  */
Pekka Pessi's avatar
Pekka Pessi committed
356

357
  for (;
Pekka Pessi's avatar
Pekka Pessi committed
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
       record && p->pr_ok;
       record = next(&message, CRLF, strip)) {
    field = record[0]; 

    rest = record + 2; rest += strspn(rest, strip);

    if (record[1] != '=') {
      parsing_error(p, "bad line \"%s\"", record);
      return;
    }

    switch (field) {
    case 'o':
      parse_origin(p, rest, &sdp->sdp_origin);
      break;

    case 's':
      parse_subject(p, rest, &sdp->sdp_subject);
      break;

    case 'i':
      parse_information(p, rest, &sdp->sdp_information);
      break;

    case 'u':
      parse_uri(p, rest, &sdp->sdp_uri);
      break;

    case 'e':
      parse_email(p, rest, emails);
      emails = &(*emails)->l_next;
      break;

    case 'p':
      parse_phone(p, rest, phones);
      phones = &(*phones)->l_next;
      break;

    case 'c':
      parse_connection(p, rest, &sdp->sdp_connection);
      break;

    case 'b':
      parse_bandwidth(p, rest, bandwidths);
      bandwidths = &(*bandwidths)->b_next;
      break;

    case 't':
      parse_time(p, rest, times);
      repeats = &(*times)->t_repeat;
      zones = &(*times)->t_zone;
      times = &(*times)->t_next;
      break;

    case 'r':
      if (repeats)
	parse_repeat(p, rest, repeats);
      else
	parsing_error(p, "repeat field without time field");
      break;

    case 'z':
      if (zones)
	parse_zone(p, rest, zones), zones = NULL;
      else
	parsing_error(p, "zone field without time field");
      break;

    case 'k':
      parse_key(p, rest, &sdp->sdp_key);
      break;

    case 'a':
      parse_session_attr(p, rest, attributes);
      if (*attributes)
	attributes = &(*attributes)->a_next;
      break;

    case 'm':
      parse_descs(p, record, message, &sdp->sdp_media);
      post_session(p, sdp);
      return;

    default:
      parsing_error(p, "unknown field \"%s\"", record);
      return;
    }
  }

  post_session(p, sdp);
}

int sdp_connection_is_inaddr_any(sdp_connection_t const *c)
{
  return 
    c && 
    c->c_nettype == sdp_net_in && 
    ((c->c_addrtype == sdp_addr_ip4 && strcmp(c->c_address, "0.0.0.0")) ||
     (c->c_addrtype == sdp_addr_ip6 && strcmp(c->c_address, "::")));
}

/**Postprocess session description.
 *
 * The function post_session() postprocesses the session description. The
 * postprocessing includes setting the session backpointer for each media.
 */
static void post_session(sdp_parser_t *p, sdp_session_t *sdp)
{
  sdp_media_t *m;
  sdp_connection_t const *c;

  if (!p->pr_ok)
    return;

  /* Set session back-pointer */
  for (m = sdp->sdp_media; m; m = m->m_next) {
    m->m_session = sdp;
  }

  if (p->pr_config) {
    if (sdp->sdp_version[0] != 0)
      parsing_error(p, "Incorrect version");
    return;
  }
  
  /* Go through all media and set mode */
  for (m = sdp->sdp_media; m; m = m->m_next) {
    if (m->m_port == 0) {
      m->m_mode = sdp_inactive;
      m->m_rejected = 1;
      continue;
    }

    c = sdp_media_connections(m);

    if (p->pr_mode_0000 && c) {
      if (c->c_nettype == sdp_net_in &&
	  c->c_addrtype == sdp_addr_ip4 &&
	  strcmp(c->c_address, "0.0.0.0") == 0)
	/* Reset recvonly flag */
	m->m_mode &= ~sdp_recvonly;
    }
  }

502 503 504
  if (p->pr_insane)
    return;

Pekka Pessi's avatar
Pekka Pessi committed
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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 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 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  /* Verify that all mandatory fields are present */
  if (sdp_sanity_check(p) < 0)
    return;
}

/** Validates that all mandatory fields exist 
 *
 * Checks that all necessary fields (v=, o=) exists in the given sdp. If
 * strict, check that all mandatory fields (c=, o=, s=, t=) are present. 
 * This function also goes through all media, marks rejected media as such,
 * and updates the mode accordingly.
 * 
 * @retval 0 if parsed SDP description is valid
 * @retval -1 if some SDP line is missing
 * @retval -2 if c= line is missing
 */
int sdp_sanity_check(sdp_parser_t *p)
{
  sdp_session_t *sdp = p->pr_session;
  sdp_media_t *m;

  if (!p || !p->pr_ok)
    ;
  else if (sdp->sdp_version[0] != 0)
    parsing_error(p, "Incorrect version");
  else if (!sdp->sdp_origin)
    parsing_error(p, "No o= present");
  else if (p->pr_strict && !sdp->sdp_subject)
    parsing_error(p, "No s= present");
  else if (p->pr_strict && !sdp->sdp_time)
    parsing_error(p, "No t= present");

  if (!p->pr_ok)
    return -1;

  /* If there is no session level c= check that one exists for all media */
  /* c= line may be missing if this is a RTSP description */
  if (!p->pr_c_missing && !sdp->sdp_connection) {
    for (m = sdp->sdp_media ; m ; m = m->m_next) {
      if (!m->m_connections && !m->m_rejected) {
	parsing_error(p, "No c= on either session level or all mediums");
	return -2;
      }
    }
  }

  return 0;
}

#if 0
/**
 * Parse a "v=" field
 *
 * The function parser_version() parses the SDP version field.
 *
 * @param p      pointer to SDP parser object
 * @param r      pointer to record data
 * @param result pointer to which parsed record is assigned
 */
static void parse_version(sdp_parser_t *p, char *r, sdp_version_t *result)
{
  /*
   proto-version =       "v=" 1*DIGIT CRLF
                         ;[RFC2327] describes version 0
   */
  if (parse_ul(p, &r, result, 0))
    parsing_error(p, "version \"%s\" is invalid", r);
  else if (*result > 0)
    parsing_error(p, "unknown version v=%s", r);
}
#endif

/* -------------------------------------------------------------------------
 * Function parse_origin() - parse an "o=" field
 *
 * Description:
 *   This function parses an SDP origin field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_origin(sdp_parser_t *p, char *r, sdp_origin_t **result)
{
  /*
   origin-field =        "o=" username space
                         sess-id space sess-version space
                         nettype space addrtype space
                         addr CRLF

   username =            safe
                         ;pretty wide definition, but doesn't include space

   sess-id =             1*(DIGIT)
                         ;should be unique for this originating username/host

   sess-version =        1*(DIGIT)
                         ;0 is a new session

			 
   */
  PARSE_ALLOC(p, sdp_origin_t, o);

  *result = o;

  o->o_username = token(&r, SPACE TAB, NULL, SPACE TAB);
  if (!o->o_username) {
    parsing_error(p, "invalid username");
    return;
  }
  if (parse_ull(p, &r, &o->o_id, 0)) {
    parsing_error(p, "invalid session id");
    return;
  }

  if (parse_ull(p, &r, &o->o_version, 0)) {
    parsing_error(p, "invalid session version");
    return;
  }

  parse_connection(p, r, &o->o_address);
}

/* -------------------------------------------------------------------------
 * Function parse_subject() - parse an "s=" field
 *
 * Description:
 *   This function parses an SDP subject field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_subject(sdp_parser_t *p, char *r, sdp_text_t **result)
{
  /*
   session-name-field =  "s=" text CRLF
   text =                byte-string
   */
  *result = r;
}

/* -------------------------------------------------------------------------
 * Function parse_information() - parse an "i=" field
 *
 * Description:
 *   This function parses an SDP information field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_information(sdp_parser_t *p, char *r, sdp_text_t **result)
{
  /*
   information-field =   ["i=" text CRLF]
   */
  *result = r;
}

/* -------------------------------------------------------------------------
 * Function parse_uri() - parse an "u=" field
 *
 * Description:
 *   This function parses an SDP URI field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_uri(sdp_parser_t *p, char *r, sdp_text_t **result)
{
  /*
    uri-field =           ["u=" uri CRLF]

    uri=                  ;defined in RFC1630
  */
  /* XXX - no syntax checking here */
  *result = r;
}

/* -------------------------------------------------------------------------
 * Function parse_email() - parse an "e=" field
 *
 * Description:
 *   This function parses an SDP email field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_email(sdp_parser_t *p, char *r, sdp_list_t **result)
{
  /*
   email-fields =        *("e=" email-address CRLF)

   email-address =       email | email "(" email-safe ")" |
                         email-safe "<" email ">"

   email =               ;defined in RFC822  */
  parse_text_list(p, r, result);
}

/* -------------------------------------------------------------------------
 * Function parse_phone() - parse an "p=" field
 *
 * Description:
 *   This function parses an SDP phone field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_phone(sdp_parser_t *p, char *r, sdp_list_t **result)
{
  /*
   phone-fields =        *("p=" phone-number CRLF)

   phone-number =        phone | phone "(" email-safe ")" |
                         email-safe "<" phone ">"

   phone =               "+" POS-DIGIT 1*(space | "-" | DIGIT)
                         ;there must be a space or hyphen between the
                         ;international code and the rest of the number.
  */
  parse_text_list(p, r, result);
}

/* -------------------------------------------------------------------------
 * Function parse_connection() - parse an "c=" field
 *
 * Description:
 *   This function parses an SDP connection field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_connection(sdp_parser_t *p, char *r, sdp_connection_t **result)
{
  /*
   connection-field =    ["c=" nettype space addrtype space
                         connection-address CRLF]
                         ;a connection field must be present
                         ;in every media description or at the
                         ;session-level

   nettype =             "IN"
                         ;list to be extended

   addrtype =            "IP4" | "IP6"
                         ;list to be extended

   connection-address =  multicast-address
                         | addr

   multicast-address =   3*(decimal-uchar ".") decimal-uchar "/" ttl
                         [ "/" integer ]
                         ;multicast addresses may be in the range
                         ;224.0.0.0 to 239.255.255.255

   ttl =                 decimal-uchar

   addr =                FQDN | unicast-address

   FQDN =                4*(alpha-numeric|"-"|".")
                         ;fully qualified domain name as specified in RFC1035

   unicast-address =     IP4-address | IP6-address

   IP4-address =         b1 "." decimal-uchar "." decimal-uchar "." b4
   b1 =                  decimal-uchar
                         ;less than "224"; not "0" or "127"
   b4 =                  decimal-uchar
                         ;not "0"

   IP6-address =         ;to be defined
   */
  PARSE_ALLOC(p, sdp_connection_t, c);

  *result = c;

  if (strncasecmp(r, "IN", 2) == 0) {
    char *s;

    /* nettype is internet */
    c->c_nettype = sdp_net_in;
    s = token(&r, SPACE TAB, NULL, NULL);

    /* addrtype */
    s = token(&r, SPACE TAB, NULL, NULL);
    if (s && strcasecmp(s, "IP4") == 0)
      c->c_addrtype = sdp_addr_ip4;
    else if (s && strcasecmp(s, "IP6") == 0)
      c->c_addrtype = sdp_addr_ip6;
    else {
      parsing_error(p, "unknown IN address type: %s", s);
      return;
    }

    /* address */
    s = next(&r, SPACE TAB, SPACE TAB);
    c->c_address = s;
    if (!s || !*s) {
      parsing_error(p, "invalid address");
      return;
    }

    /* ttl */
    s = strchr(s, '/');
    if (s) {
      unsigned long value;
      *s++ = 0;
      if (parse_ul(p, &s, &value, 256) ||
	  (*s && *s != '/')) {
	parsing_error(p, "invalid ttl");
	return;
      }
      c->c_ttl = value;
      c->c_mcast = 1;

      /* multiple groups */
      value = 1;
      if (*s++ == '/')
	if (parse_ul(p, &s, &value, 0) || *s) {
	  parsing_error(p, "invalid number of multicast groups");
	  return;
	}
      c->c_groups = value;
    }
    else
      c->c_groups = 1;
  }
  else if (p->pr_anynet) {
    c->c_nettype = sdp_net_x;
    c->c_addrtype = sdp_addr_x;
    c->c_address = r;
    c->c_ttl = 0;
    c->c_groups = 1;
  }
  else 
    parsing_error(p, "invalid address");
}

/* -------------------------------------------------------------------------
 * Function parse_bandwidth() - parse an "b=" field
 *
 * Description:
 *   This function parses an SDP bandwidth field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_bandwidth(sdp_parser_t *p, char *r, sdp_bandwidth_t **result)
{
  /*
   bandwidth-fields =    *("b=" bwtype ":" bandwidth CRLF)

   bwtype =              1*(alpha-numeric)
                         ; CT or AS
   bandwidth =           1*(DIGIT)
   */
  /* NOTE: bwtype can also be like X-barf */
  sdp_bandwidth_e modifier;
  char *name;
  unsigned long value;

  name = token(&r, ":", ALPHA DIGIT "-", SPACE TAB);

  if (name == NULL || parse_ul(p, &r, &value, 0)) {
    parsing_error(p, "invalid bandwidth");
    return;
  }

  if (strcmp(name, "CT") == 0)
    modifier = sdp_bw_ct, name = NULL;
  else if (strcmp(name, "AS") == 0)
    modifier = sdp_bw_as, name = NULL;
  else
    modifier = sdp_bw_x;

  if (STRICT(p))
    PARSE_CHECK_REST(p, r, "b");
  
  {
    PARSE_ALLOC(p, sdp_bandwidth_t, b);
    *result = b;
    b->b_modifier = modifier;
    b->b_modifier_name = name;
    b->b_value = value;
  }
}

/* -------------------------------------------------------------------------
 * Function parse_time() - parse an "t=" field
 *
 * Description:
 *   This function parses an SDP time field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_time(sdp_parser_t *p, char *r, sdp_time_t **result)
{
  /*
   time-fields =         1*( "t=" start-time space stop-time
                         *(CRLF repeat-fields) CRLF)
                         [zone-adjustments CRLF]

   start-time =          time | "0"

   stop-time =           time | "0"

   time =                POS-DIGIT 9*(DIGIT)
                         ;sufficient for 2 more centuries
   */
  PARSE_ALLOC(p, sdp_time_t, t);
  *result = t;
  if (parse_ul(p, &r, &t->t_start, 0) ||
      parse_ul(p, &r, &t->t_stop, 0))
    parsing_error(p, "invalid time");
  else if (STRICT(p)) {
    PARSE_CHECK_REST(p, r, "t");
  }
}

/**
 * Parse an "r=" field
 *
 * The function parse_repeat() parses an SDP repeat field.
 *
 * @param p      pointer to SDP parser object
 * @param r      pointer to record data
 * @param result pointer to which parsed record is assigned
 * 
 */
static void parse_repeat(sdp_parser_t *p, char *d, sdp_repeat_t **result)
{
  /*
   repeat-fields =       "r=" repeat-interval space typed-time
                         1*(space typed-time)

   repeat-interval =     typed-time

   typed-time =          1*(DIGIT) [fixed-len-time-unit]

   fixed-len-time-unit = "d" | "h" | "m" | "s"
   */

  unsigned long tt, *interval;
  int i, n, N;
  char *s;
  sdp_repeat_t *r;

  /** Count number of intervals */
  for (N = 0, s = d; *s; ) {
    if (!(is_posdigit(*s) || (!STRICT(p) && (*s) == '0')))
      break;
    do { s++; } while (is_digit(*s));
    if (*s && strchr("dhms", *s))
      s++;
    N++;
    if (!(i = STRICT(p) ? is_space(*s) : strspn(s, SPACE TAB)))
      break;
    s += i;
  }

  PARSE_CHECK_REST(p, s, "r");
  if (N < 2) {
    parsing_error(p, "invalid repeat");
    return;
  }
  if (!(r = su_salloc(p->pr_home, offsetof(sdp_repeat_t, r_offsets[N - 1])))) {
    parse_alloc_error(p, "sdp_repeat_t"); 
    return;
  }

  r->r_number_of_offsets = N - 2;
  r->r_offsets[N - 2] = 0;

  for (n = 0, interval = &r->r_interval; n < N; n++) {
    tt = strtoul(d, &d, 10);

    switch (*d) {
    case 'd': tt *= 24;
    case 'h': tt *= 60;
    case 'm': tt *= 60;
    case 's': d++;
      break;
    }

    interval[n] = tt;

    while (is_space(*d))
      d++;
  } 

  *result = r;
}

/* -------------------------------------------------------------------------
 * Function parse_zone() - parse an "z=" field
 *
 * Description:
 *   This function parses an SDP time zone field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 *
 * Note:
 *   This is unimplemented!
 */
static void parse_zone(sdp_parser_t *p, char *r, sdp_zone_t **result)
{
  char *s;
  int i, n, N;
  sdp_zone_t *z;

  /*
   zone-adjustments =    time space ["-"] typed-time
                         *(space time space ["-"] typed-time)
   */

  /** Count number of timezones, check syntax */
  for (N = 0, s = r; *s;) {
    if (!(is_posdigit(*s) || (!STRICT(p) && (*s) == '0')))
      break;
    do { s++; } while (is_digit(*s));
    if (!(i = STRICT(p) ? is_space(*s) : strspn(s, SPACE TAB)))
      break;
    s += i;
    if (!(*s == '-' || is_posdigit(*s) || (!STRICT(p) && (*s) == '0')))
      break;
    do { s++; } while (is_digit(*s));
    if (*s && strchr("dhms", *s))
      s++;
    N++;
    if (!(i = STRICT(p) ? is_space(*s) : strspn(s, SPACE TAB)))
      break;
    s += i;
  }

  PARSE_CHECK_REST(p, s, "z");

  if (N < 1) {
    parsing_error(p, "invalid timezone");
    return;
  }
  if (!(z = su_salloc(p->pr_home, offsetof(sdp_zone_t, z_adjustments[N])))) {
    parse_alloc_error(p, "sdp_zone_t"); 
    return;
  }

  z->z_number_of_adjustments = N;

  for (n = 0; n < N; n++) {
    unsigned long at = strtoul(r, &r, 10);
    long offset = strtol(r, &r, 10);
    switch (*r) {
    case 'd': offset *= 24;
    case 'h': offset *= 60;
    case 'm': offset *= 60;
    case 's': r++;
      break;
    }

    z->z_adjustments[n].z_at = at;
    z->z_adjustments[n].z_offset = offset;
  } 

  *result = z;
}

/* -------------------------------------------------------------------------
 * Function parse_key() - parse an "k=" field
 *
 * Description:
 *   This function parses an SDP key field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 *
 * Note:
 *   This function is unimplemented!
 */
static void parse_key(sdp_parser_t *p, char *r, sdp_key_t **result)
{
  char *s;
  /*
   key-field =           ["k=" key-type CRLF]


   key-type =            "prompt" |
                         "clear:" key-data |
                         "base64:" key-data |
                         "uri:" uri


   key-data =            email-safe | "~" | "
   */

  s = token(&r, ":", ALPHA DIGIT "-", SPACE TAB);
  if (!s) {
    parsing_error(p, "invalid key method");
    return;
  }

  {
    PARSE_ALLOC(p, sdp_key_t, k);
    *result = k;

    if (strcasecmp(s, "clear") == 0)
      k->k_method = sdp_key_clear, k->k_method_name = "clear";
    else if (strcasecmp(s, "base64") == 0)
      k->k_method = sdp_key_base64, k->k_method_name = "base64";
    else if (strcasecmp(s, "uri") == 0)
      k->k_method = sdp_key_uri, k->k_method_name = "uri";
    else if (strcasecmp(s, "prompt") == 0)
      k->k_method = sdp_key_prompt, k->k_method_name = "prompt";
    else
      k->k_method = sdp_key_x, k->k_method_name = s;

    k->k_material = r;
  }
}

/* -------------------------------------------------------------------------
 * Function parse_session_attr() - parse a session "a=" field
 *
 * Description:
 *   This function parses an SDP attribute field regarding whole session.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_session_attr(sdp_parser_t *p, char *r, sdp_attribute_t **result)
{
  /*
   attribute-fields =    *("a=" attribute CRLF)

   attribute =           (att-field ":" att-value) | att-field

   att-field =           1*(alpha-numeric)

   att-value =           byte-string
   */

  char *name = NULL, *value = NULL;

  if (!(name = token(&r, ":", ALPHA DIGIT "-", SPACE TAB))) {
    parsing_error(p,"invalid attribute name");
    return;
  }

  if (*r)
    value = r;
  else 
    PARSE_CHECK_REST(p, r, "a");

  if (strcasecmp(name, "charset") == 0) {
    p->pr_session->sdp_charset = value;
1183
    return;
Pekka Pessi's avatar
Pekka Pessi committed
1184 1185
  }

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
  if (p->pr_mode_manual) 
    ;
  else if (strcasecmp(name, "inactive") == 0)
    p->pr_session_mode = sdp_inactive;
  else if (strcasecmp(name, "sendonly") == 0)
    p->pr_session_mode = sdp_sendonly;
  else if (strcasecmp(name, "recvonly") == 0)
    p->pr_session_mode = sdp_recvonly;
  else if (strcasecmp(name, "sendrecv") == 0)
    p->pr_session_mode = sdp_sendrecv;

  PARSE_ALLOC(p, sdp_attribute_t, a);
  *result = a;

  a->a_name  = name;
  a->a_value = value;
Pekka Pessi's avatar
Pekka Pessi committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
}

/* -------------------------------------------------------------------------
 * Function parse_media() - parse an "m=" field
 *
 * Description:
 *   This function parses an SDP media field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_media(sdp_parser_t *p, char *r, sdp_media_t **result)
{
  /*
   media-descriptions =  *( media-field
                         information-field
                         *(connection-field)
                         bandwidth-fields
                         key-field
                         attribute-fields )

   media-field =         "m=" media space port ["/" integer]
                         space proto 1*(space fmt) CRLF

   media =               1*(alpha-numeric)
                         ;typically "audio", "video", "application"
                         ;or "data"

   fmt =                 1*(alpha-numeric)
                         ;typically an RTP payload type for audio
                         ;and video media

   proto =               1*(alpha-numeric)
                         ;typically "RTP/AVP" or "udp" for IP4

   port =                1*(DIGIT)
                         ;should in the range "1024" to "65535" inclusive
   */
  char *s;
  unsigned long value;
  PARSE_ALLOC(p, sdp_media_t, m);

  *result = m;

  m->m_mode = sdp_sendrecv;

  s = token(&r, SPACE, ALPHA DIGIT, NULL);
  if (s == NULL && p->pr_config)
    s = token(&r, SPACE, "*", NULL);
  if (!s) {
    parsing_error(p, "m= invalid media field");
    return;
  }

  sdp_media_type(m, s);

  /* Accept m=* in configuration file */
  if (p->pr_config && m->m_type == sdp_media_any) {
    r += strspn(r, SPACE TAB);
    if (r[0] == '\0') {
      m->m_proto = sdp_proto_any, m->m_proto_name = "*";
      return;
    }
  }

  if (parse_ul(p, &r, &value, 0)) {
    parsing_error(p, "m= invalid port number");
    return;
  }
  m->m_port = value;

  if (*r == '/') {
    r++;
    if (parse_ul(p, &r, &value, 0)) {
      parsing_error(p, "m= invalid port specification");
      return;
    }
    m->m_number_of_ports = value;
  }

  /* alpha-numeric and "/" */
  s = token(&r, SPACE, ALPHA DIGIT "/", SPACE);
  if (s == NULL && p->pr_config) 
    s = token(&r, SPACE, "*", SPACE);
  if (s == NULL) {
    parsing_error(p, "m= missing protocol");
    return;
  }
  
  if (!STRICT(p) && strcasecmp(s, "RTP") == 0)
    m->m_proto = sdp_proto_rtp, m->m_proto_name = "RTP/AVP";
  else 
    sdp_media_transport(m, s);

  /* RTP format list */
  if (*r && sdp_media_has_rtp(m)) {
    parse_payload(p, r, &m->m_rtpmaps);
    return;
  }

  /* "normal" format list */
  if (*r) {
    sdp_list_t **fmt = &m->m_format;
    
    while (r && *r) {
      PARSE_ALLOC(p, sdp_list_t, l);
      *fmt = l;
      l->l_text = next(&r, SPACE TAB, SPACE TAB);
      fmt = &l->l_next;
    }
  }
}

/** Set media type */
void sdp_media_type(sdp_media_t *m, char const *s)
{
  if (strcmp(s, "*") == 0)
    m->m_type = sdp_media_any, m->m_type_name = "*";
  else if (strcasecmp(s, "audio") == 0)
    m->m_type = sdp_media_audio, m->m_type_name = "audio";
  else if (strcasecmp(s, "video") == 0)
    m->m_type = sdp_media_video, m->m_type_name = "video";
  else if (strcasecmp(s, "application") == 0)
    m->m_type = sdp_media_application, m->m_type_name = "application";
  else if (strcasecmp(s, "data") == 0)
    m->m_type = sdp_media_data, m->m_type_name = "data";
  else if (strcasecmp(s, "control") == 0)
    m->m_type = sdp_media_control, m->m_type_name = "control";
  else if (strcasecmp(s, "message") == 0)
    m->m_type = sdp_media_message, m->m_type_name = "message";
  // Addition for JPIP - START
  else if (strcasecmp(s, "image") == 0)
    m->m_type = sdp_media_image, m->m_type_name = "image";
  // Addition for JPIP - END
  else
    m->m_type = sdp_media_x, m->m_type_name = s;
}

void sdp_media_transport(sdp_media_t *m, char const *s)
{
  if (strcasecmp(s, "*") == 0)
    m->m_proto = sdp_proto_any, m->m_proto_name = "*";
  else if (strcasecmp(s, "RTP/AVP") == 0)
    m->m_proto = sdp_proto_rtp, m->m_proto_name = "RTP/AVP";
  else if (strcasecmp(s, "RTP/SAVP") == 0)
    m->m_proto = sdp_proto_srtp, m->m_proto_name = "RTP/SAVP";
  else if (strcasecmp(s, "UDP") == 0)
    m->m_proto = sdp_proto_udp, m->m_proto_name = "UDP";
  else if (strcasecmp(s, "TCP") == 0)
    m->m_proto = sdp_proto_tcp, m->m_proto_name = "TCP";
  else if (strcasecmp(s, "TLS") == 0)
    m->m_proto = sdp_proto_tls, m->m_proto_name = "TLS";
  else 
    m->m_proto = sdp_proto_x, m->m_proto_name = s;
}

/** Check if media uses RTP as its transport protocol.  */
int sdp_media_has_rtp(sdp_media_t const *m)
{
  return m && (m->m_proto == sdp_proto_rtp || m->m_proto == sdp_proto_srtp);
}

/**
 * The function parse_payload() parses an RTP payload type list, and
 * creates an rtpmap structure for each payload type.
 *
 * @param p       pointer to SDP parser object
 * @param r       pointer to record data
 * @param result  pointer to which parsed record is assigned
 */
static void parse_payload(sdp_parser_t *p, char *r, sdp_rtpmap_t **result)
{
#define SETMAP(rm, type, rate) (rm->rm_encoding = type, rm->rm_rate = rate)

  while (*r) {
    unsigned long value;

    if (parse_ul(p, &r, &value, 128) == 0) {
      PARSE_ALLOC(p, sdp_rtpmap_t, rm);

      *result = rm; result = &rm->rm_next;

      rm->rm_predef = 1;
      rm->rm_pt = value;

      switch (value) {
	/* Payload type numbers as per RFC3551 */
      case 0:  SETMAP(rm, "PCMU", 8000); break;
      case 3:  SETMAP(rm, "GSM",  8000); break;
      case 4:  SETMAP(rm, "G723", 8000); break;
      case 5:  SETMAP(rm, "DVI4", 8000); break;
      case 6:  SETMAP(rm, "DVI4", 16000); break;
      case 7:  SETMAP(rm, "LPC",  8000); break;
      case 8:  SETMAP(rm, "PCMA", 8000); break;
      case 9:  SETMAP(rm, "G722", 16000); break;
      /* in stereo, where available! */
      case 10:  SETMAP(rm, "L16", 44100); rm->rm_params = "2"; break;
      case 11:  SETMAP(rm, "L16", 44100); break;
      case 12:  SETMAP(rm, "QCELP", 8000); break;
      case 13:  SETMAP(rm, "CN", 8000); break;
      case 14:  SETMAP(rm, "MPA", 90000); break;
      case 15:  SETMAP(rm, "G728", 8000); break;
      case 16:  SETMAP(rm, "DVI4", 11025); break;
      case 17:  SETMAP(rm, "DVI4", 22050); break;
      case 18:  SETMAP(rm, "G729", 8000); break;

      case 25:  SETMAP(rm, "CelB", 90000); break;
      case 26:  SETMAP(rm, "JPEG", 90000); break;
      case 28:  SETMAP(rm, "nv",   90000); break;
      case 31:  SETMAP(rm, "H261", 90000); break;
      case 32:  SETMAP(rm, "MPV",  90000); break;
      case 33:  SETMAP(rm, "MP2T", 90000); break;
      case 34:  SETMAP(rm, "H263", 90000); break;

      default:  SETMAP(rm, "", 0); break;
      }
    }
    else if (p->pr_config && r[0] == '*' && (r[1] == ' ' || r[1] == '\0')) {
      PARSE_ALLOC(p, sdp_rtpmap_t, rm);

      *result = rm; result = &rm->rm_next;

      rm->rm_predef = 1;
      rm->rm_any = 1;

      SETMAP(rm, "*", 0); break;
    }
    else {
      parsing_error(p, "m= invalid format for RTP/AVT");
      break;
    }
  }
}

/* -------------------------------------------------------------------------
 * Function parse_media_attr() - parse a media-specific "a=" field
 *
 * Description:
 *   This function parses a media-specific attribute field.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   r      - pointer to record data
 *   result - pointer to which parsed record is assigned
 */
static void parse_media_attr(sdp_parser_t *p, char *r, sdp_media_t *m,
			     sdp_attribute_t **result)
{
  /*
   attribute-fields =    *("a=" attribute CRLF)

   attribute =           (att-field ":" att-value) | att-field

   att-field =           1*(alpha-numeric) 

   att-value =           byte-string

   a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters>]
   a=fmtp:<payload type> <parameters>
   */
  int rtp = sdp_media_has_rtp(m);
  char *name = NULL, *value = NULL;
  int n;

  if (!(name = token(&r, ":", ALPHA DIGIT "-", SPACE TAB))) {
    parsing_error(p,"invalid attribute name");
    return;
  }

  if (*r)
    value = r;
  else 
    PARSE_CHECK_REST(p, r, "a");

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
  if (p->pr_mode_manual) 
    ;
  else if (strcasecmp(name, "inactive") == 0) {
    m->m_mode = sdp_inactive;
    return;
  }
  else if (strcasecmp(name, "sendonly") == 0) {
    m->m_mode = sdp_sendonly;
    return;
  }
  else if (strcasecmp(name, "recvonly") == 0) {
    m->m_mode = sdp_recvonly;
    return;
  }
  else if (strcasecmp(name, "sendrecv") == 0) {
    m->m_mode = sdp_sendrecv;
    return;
  }

Pekka Pessi's avatar
Pekka Pessi committed
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
  if (rtp && strcasecmp(name, "rtpmap") == 0) {
    if ((n = parse_rtpmap(p, r, m)) == 0 || n < -1)
      return;
  }
  else if (rtp && strcasecmp(name, "fmtp") == 0) {
    if ((n = parse_fmtp(p, r, m)) == 0 || n < -1)
      return;
  }
  else {
    PARSE_ALLOC(p, sdp_attribute_t, a);
    *result = a;

    a->a_name  = name;
    a->a_value = value;
  }
}

/** Parse rtpmap attribute.
 *
 * a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters>]
 */
static int parse_rtpmap(sdp_parser_t *p, char *r, sdp_media_t *m)
{
  unsigned long pt, rate;
  char *encoding, *params;
  sdp_rtpmap_t *rm;

  int strict = STRICT(p);

  if (parse_ul(p, &r, &pt, 128)) {
    if (strict)
      parsing_error(p, "a=rtpmap: invalid payload type");
    return -1;
  }

  for (rm = m->m_rtpmaps; rm; rm = rm->rm_next)
    if (rm->rm_pt == pt)
      break;

  if (!rm) {
    if (strict)
      parsing_error(p, "a=rtpmap:%lu: unknown payload type", pt);
    return -1;
  }

  encoding = token(&r, "/", ALPHA DIGIT "-", NULL);
  if (!r) {
    parsing_error(p, "a=rtpmap:%lu: missing <clock rate>", pt);
    return -2;
  }
  if (parse_ul(p, &r, &rate, 0)) {
    parsing_error(p, "a=rtpmap:%lu %s: invalid <clock rate>", pt, encoding);
    return -2;
  }

  if (*r == '/') 
    params = ++r;
  else
    params = 0;
    
  rm->rm_predef = 0;
  rm->rm_encoding = encoding;
  rm->rm_rate = rate;
  rm->rm_params = params;

  return 0;
}

/** Parse fmtp attribute.
 *
 * a=fmtp:<payload type> <parameters>
 */
static int parse_fmtp(sdp_parser_t *p, char *r, sdp_media_t *m)
{
  unsigned long pt;
  sdp_rtpmap_t *rm;

  int strict = STRICT(p);

  if (parse_ul(p, &r, &pt, 128)) {
    if (strict)
      parsing_error(p, "a=rtpmap: invalid payload type");
    return -1;
  }

  for (rm = m->m_rtpmaps; rm; rm = rm->rm_next)
    if (rm->rm_pt == pt)
      break;

  if (!rm) {
    if (strict)
      parsing_error(p, "a=fmtp:%lu: unknown payload type", pt);
    return -1;
  }

  rm->rm_fmtp = r;
  return 0;
}

/* -------------------------------------------------------------------------
 * Function parse_descs() - parse media descriptors
 *
 * Description:
 *   This function parses media descriptors at the end of SDP message.
 *
 * Parameters:
 *   p      - pointer to SDP parser object
 *   record - pointer to first media field
 *   message - pointer to rest 
 *   medias - pointer to which parsed media structures are assigned
 */
static void parse_descs(sdp_parser_t *p, 
			char *record, 
			char *message, 
			sdp_media_t **medias)
{
  char *rest;
  const char *strip;
  sdp_media_t *m = NULL;
  sdp_connection_t **connections = NULL;
  sdp_bandwidth_t **bandwidths = NULL;
  sdp_attribute_t **attributes = NULL;

  if (!STRICT(p))
    strip = SPACE TAB;		/* skip initial whitespace */
  else
    strip = NULL;
  
  for (;
       record && p->pr_ok;
       record = next(&message, CRLF, strip)) {
    char field = record[0];

    rest = record + 2; rest += strspn(rest, strip);

    if (record[1] == '=') switch (field) {
    case 'c':
      assert(connections);
      parse_connection(p, rest, connections);
      connections = &(*connections)->c_next;
      break;

    case 'b':
      assert(bandwidths);
      parse_bandwidth(p, rest, bandwidths);
      bandwidths = &(*bandwidths)->b_next;
      break;

    case 'k':
      parse_key(p, rest, &m->m_key);
      break;

    case 'a':
      assert(attributes);
      parse_media_attr(p, rest, m, attributes);
      if (*attributes)
	attributes = &(*attributes)->a_next;
      break;

    case 'm':
      parse_media(p, rest, medias);
      m = *medias;
      if (m) {
1660
	m->m_mode = p->pr_session_mode;
Pekka Pessi's avatar
Pekka Pessi committed
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
	medias = &m->m_next;
	connections = &m->m_connections;
	bandwidths = &m->m_bandwidths;
	attributes = &m->m_attributes;
      }
    }
  }
}

static void parse_text_list(sdp_parser_t *p, char *r, sdp_list_t **result)
{
  PARSE_ALLOC(p, sdp_list_t, l);

  *result = l;

  l->l_text = r;
}

/*
 * parse_ul: parse an unsigned long
 */
static int parse_ul(sdp_parser_t *p, char **r, 
		    unsigned long *result, unsigned long max)
{
  char *ul = *r; 

  ul += strspn(ul, SPACE TAB);

  *result = strtoul(ul, r, 10);
  if (ul != *r && !(max && max <= *result)) {
    *r += strspn(*r, SPACE TAB);
    return 0;
  }

  return -1;
}

/*
1699
 * parse_ull: parse an unsigned long long
Pekka Pessi's avatar
Pekka Pessi committed
1700 1701 1702 1703
 */
static int parse_ull(sdp_parser_t *p, char **r, 
		     uint64_t *result, uint64_t max)
{
1704
  unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

  char *s = *r; 

  s += strspn(s, SPACE TAB);

  ull = strtoull(s, r, 10);

  if (s != *r && !(max && max <= ull)) {
    *result = (uint64_t)ull;
    *r += strspn(*r, SPACE TAB);
    return 0;
  }

  return -1;
}

static char *token(char **message, 
		   const char *sep, 
		   const char *legal, 
		   const char *strip)
{
  size_t n;
  char *retval = *message;

  if (strip)
    retval += strspn(retval, strip);

  if (legal)
    n = strspn(retval, legal);
  else 
    n = strcspn(retval, sep);

  if (n == 0)
    return NULL;

  if (retval[n]) {
    retval[n++] = '\0';
    n += strspn(retval + n, sep);
  }

  *message = retval + n;

  if (*retval == '\0')
    return NULL;

  return retval;
}

static char *next(char **message, const char *sep, const char *strip)
{
  size_t n;
  char *retval = *message;

  if (strip)
    retval += strspn(retval, strip);

  n = strcspn(retval, sep);

  if (n == 0)
    return NULL;

  if (retval[n]) {
    retval[n++] = '\0';
    n += strspn(retval + n, sep);
  }

  *message = retval + n;

  if (*retval == '\0')
    return NULL;

  return retval;
}

static void parsing_error(sdp_parser_t *p, char const *fmt, ...)
{
  int n;
  va_list ap;
  va_start(ap, fmt); 
  
  memset(p->pr_error, 0, sizeof(p->pr_error));
  n = vsnprintf(p->pr_error, sizeof(p->pr_error), fmt, ap);
  va_end(ap);

  p->pr_ok = 0;
}

static void parse_alloc_error(sdp_parser_t *p, const char *typename)
{
  parsing_error(p, "memory exhausted (while allocating memory for %s)", 
		typename);
}