sdp_parse.c 48.9 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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
26
 * @CFILE sdp_parse.c
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30 31 32 33 34 35 36
 * @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
 */

#include "config.h"

37 38 39 40
#include <sofia-sip/su_alloc.h>

#include "sofia-sip/sdp.h"

Pekka Pessi's avatar
Pekka Pessi committed
41 42 43 44 45
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
46
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
47 48
#include <assert.h>

49
/** @typedef struct sdp_parser_s sdp_parser_t;
50 51 52 53 54 55 56 57 58 59 60
 *
 * SDP parser handle.
 *
 * The SDP parser handle is returned by sdp_parse(). It contains either
 * successfully parse SDP session #sdp_session_t or an error message.
 * If sdp_session() returns non-NULL, parsing was successful.
 *
 * @sa #sdp_session_t, sdp_parse(), sdp_session(), sdp_parsing_error(),
 * sdp_sanity_check(), sdp_parser_home(), sdp_parser_free().
 */

Pekka Pessi's avatar
Pekka Pessi committed
61 62 63 64 65 66 67
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;
68 69 70

  sdp_mode_t pr_session_mode;

Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74 75
  unsigned   pr_ok : 1;

  unsigned   pr_strict : 1;
  unsigned   pr_anynet : 1;
  unsigned   pr_mode_0000 : 1;
76
  unsigned   pr_mode_manual : 1;
Pekka Pessi's avatar
Pekka Pessi committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  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);
101
static int parsing_error(sdp_parser_t *p, char const *fmt, ...);
Pekka Pessi's avatar
Pekka Pessi committed
102 103 104 105 106

/** 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
107
 * may not contain a NUL.
108
 *
109
 * The parsing result is stored to an #sdp_session_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
110 111 112 113 114 115
 *
 * @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.
 *
116
 * The following flags are used by parser:
Pekka Pessi's avatar
Pekka Pessi committed
117
 *
118
 * @li #sdp_f_strict Parser should accept only messages conforming strictly
119 120 121 122 123 124 125 126
 *                   to the specification.
 * @li #sdp_f_anynet Parser accepts unknown network or address types.
 * @li #sdp_f_insane Do not run sanity check.
 * @li #sdp_f_c_missing  Sanity check does not require c= for each m= line
 * @li #sdp_f_mode_0000 Parser regards "c=IN IP4 0.0.0.0" as "a=inactive"
 *                      (likewise with c=IN IP6 ::)
 * @li #sdp_f_mode_manual Do not generate or parse SDP mode
 * @li #sdp_f_config   Parse config files (any line can be missing)
Pekka Pessi's avatar
Pekka Pessi committed
127 128
 *
 * @return
129 130 131
 * Always a valid parser handle.
 *
 * @todo Parser accepts some non-conforming SDP even with #sdp_f_strict.
Pekka Pessi's avatar
Pekka Pessi committed
132 133
 */
sdp_parser_t *
134
sdp_parse(su_home_t *home, char const msg[], issize_t msgsize, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
135
{
136
  sdp_parser_t *p;
137
  char *b;
138
  size_t len;
139

140
  if (msgsize == -1 || msg == NULL) {
141
    p = su_home_clone(home, sizeof(*p));
142
    if (p)
143 144 145
      parsing_error(p, "invalid input message");
    else
      p = (sdp_parser_t*)&no_mem_error;
146 147 148
    return p;
  }

149 150 151 152 153 154 155 156
  if (msgsize == -1 && msg)
    len = strlen(msg);
  else
    len = msgsize;

  if (len > ISSIZE_MAX)
    len = ISSIZE_MAX;

157
  p = su_home_clone(home, sizeof(*p) + len + 1);
Pekka Pessi's avatar
Pekka Pessi committed
158

159
  if (p) {
160
    b = strncpy((void *)(p + 1), msg, len);
161
    b[len] = 0;
162

Pekka Pessi's avatar
Pekka Pessi committed
163 164 165 166 167 168 169 170
    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;
171 172 173
    p->pr_mode_manual = (flags & sdp_f_mode_manual) != 0;
    p->pr_session_mode = sdp_sendrecv;

Pekka Pessi's avatar
Pekka Pessi committed
174
    parse_message(p);
175

Pekka Pessi's avatar
Pekka Pessi committed
176 177 178 179 180 181 182 183 184
    return p;
  }

  if (p)
    sdp_parser_free(p);

  return (sdp_parser_t*)&no_mem_error;
}

Pekka Pessi's avatar
Pekka Pessi committed
185 186 187 188 189 190 191 192 193 194

/** 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
195 196 197 198
/** 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
199
 * data in the structure are valid until sdp_parser_free() is called.
Pekka Pessi's avatar
Pekka Pessi committed
200 201 202 203 204
 *
 * @param p SDP parser
 *
 * @return
 *   The function sdp_session() returns a pointer to an parsed SDP message
205
 *   or NULL, if an error has occurred.  */
Pekka Pessi's avatar
Pekka Pessi committed
206 207 208 209 210 211 212 213 214 215 216 217 218
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
 *
219
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
220
 * The function sdp_parsing_error() returns a C string describing parsing
221
 * error, or NULL if no error occurred.
Pekka Pessi's avatar
Pekka Pessi committed
222 223 224 225 226 227 228 229 230 231 232
 */
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.
 *
233
 * @param p pointer to the SDP parser to be freed
Pekka Pessi's avatar
Pekka Pessi committed
234 235 236 237
 */
void sdp_parser_free(sdp_parser_t *p)
{
  if (p && p != &no_mem_error)
238
    su_home_unref(p->pr_home);
Pekka Pessi's avatar
Pekka Pessi committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252
}

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

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

/* Parsing tokens */
#define SPACE " "
#define TAB   "\011"
#define CRLF  "\015\012"
#define ALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define DIGIT "0123456789"
253
#define TOKEN ALPHA DIGIT "-!#$%&'*+.^_`{|}~"
Pekka Pessi's avatar
Pekka Pessi committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

/* ========================================================================= */
/* 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);
274
static void parse_media_attr(sdp_parser_t *p, char *r, sdp_media_t *m,
Pekka Pessi's avatar
Pekka Pessi committed
275 276 277 278 279 280 281
			     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);

282
static int parse_ul(sdp_parser_t *p, char **r, unsigned long *result,
Pekka Pessi's avatar
Pekka Pessi committed
283
		    unsigned long max_value);
284
static int parse_ull(sdp_parser_t *p, char **r, uint64_t *result,
Pekka Pessi's avatar
Pekka Pessi committed
285 286 287
		     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);
288
static char *token(char **message, const char *sep, const char *legal,
Pekka Pessi's avatar
Pekka Pessi committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
		   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)); \
304
 if (!v && (parse_alloc_error(p, #t), 1)) return;
Pekka Pessi's avatar
Pekka Pessi committed
305 306 307 308 309 310 311 312 313 314 315 316

/* -------------------------------------------------------------------------
 * 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

/* -------------------------------------------------------------------------
317
 * Function parse_message() - parse an SDP message
Pekka Pessi's avatar
Pekka Pessi committed
318 319
 *
 * Description:
320
 *   This function parses an SDP message, which is copied into the
Pekka Pessi's avatar
Pekka Pessi committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
 *   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
361
    strip = "";
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364 365 366 367

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

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

Pekka Pessi's avatar
Pekka Pessi committed
369
  if (!record || strcmp(record, "v=0")) {
370 371 372 373 374 375 376
    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
377 378
  }

379 380 381 382
  /*
    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
383

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

    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)
{
479 480 481
  return
    c &&
    c->c_nettype == sdp_net_in &&
Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    ((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;
  }
509

Pekka Pessi's avatar
Pekka Pessi committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  /* 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;
    }
  }

529 530 531
  if (p->pr_insane)
    return;

Pekka Pessi's avatar
Pekka Pessi committed
532 533 534 535 536
  /* Verify that all mandatory fields are present */
  if (sdp_sanity_check(p) < 0)
    return;
}

537
/** Validates that all mandatory fields exist
Pekka Pessi's avatar
Pekka Pessi committed
538
 *
539
 * Checks that all necessary fields (v=, o=) exists in the parsed sdp. If
540
 * strict, check that all mandatory fields (c=, o=, s=, t=) are present.
Pekka Pessi's avatar
Pekka Pessi committed
541 542
 * This function also goes through all media, marks rejected media as such,
 * and updates the mode accordingly.
543
 *
Pekka Pessi's avatar
Pekka Pessi committed
544 545 546 547 548 549 550 551 552 553
 * @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)
554
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
555
  else if (sdp->sdp_version[0] != 0)
556
    return parsing_error(p, "Incorrect version");
Pekka Pessi's avatar
Pekka Pessi committed
557
  else if (!sdp->sdp_origin)
558
    return parsing_error(p, "No o= present");
Pekka Pessi's avatar
Pekka Pessi committed
559
  else if (p->pr_strict && !sdp->sdp_subject)
560
    return parsing_error(p, "No s= present");
Pekka Pessi's avatar
Pekka Pessi committed
561
  else if (p->pr_strict && !sdp->sdp_time)
562
    return parsing_error(p, "No t= present");
Pekka Pessi's avatar
Pekka Pessi committed
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

  /* 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

629

Pekka Pessi's avatar
Pekka Pessi committed
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
   */
  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;
  }
876
  else
Pekka Pessi's avatar
Pekka Pessi committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    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)
895
   bwtype =              token
Pekka Pessi's avatar
Pekka Pessi committed
896 897 898 899 900 901 902
   bandwidth =           1*(DIGIT)
   */
  /* NOTE: bwtype can also be like X-barf */
  sdp_bandwidth_e modifier;
  char *name;
  unsigned long value;

903
  name = token(&r, ":", TOKEN, SPACE TAB);
Pekka Pessi's avatar
Pekka Pessi committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918

  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");
919

Pekka Pessi's avatar
Pekka Pessi committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
  {
    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)
{
  /*
943
   time-fields =         1*( "t=" start-time SP stop-time
Pekka Pessi's avatar
Pekka Pessi committed
944 945 946
                         *(CRLF repeat-fields) CRLF)
                         [zone-adjustments CRLF]

947
   start-time =          time / "0"
Pekka Pessi's avatar
Pekka Pessi committed
948

949
   stop-time =           time / "0"
Pekka Pessi's avatar
Pekka Pessi committed
950

951 952 953 954 955 956 957
   time =                POS-DIGIT 9*DIGIT
                         ; Decimal representation of NTP time in
                         ; seconds since 1900.  The representation
                         ; of NTP time is an unbounded length field
                         ; containing at least 10 digits.  Unlike the
                         ; 64-bit representation used elsewhere, time
                         ; in SDP does not wrap in the year 2036.
Pekka Pessi's avatar
Pekka Pessi committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
   */
  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
977
 *
Pekka Pessi's avatar
Pekka Pessi committed
978 979 980 981
 */
static void parse_repeat(sdp_parser_t *p, char *d, sdp_repeat_t **result)
{
  /*
982
   repeat-fields =       %x72 "=" repeat-interval 2*(SP typed-time)
Pekka Pessi's avatar
Pekka Pessi committed
983

984
   repeat-interval =     POS-DIGIT *DIGIT [fixed-len-time-unit]
Pekka Pessi's avatar
Pekka Pessi committed
985

986
   typed-time =          1*DIGIT [fixed-len-time-unit]
Pekka Pessi's avatar
Pekka Pessi committed
987

988
   fixed-len-time-unit = %x64 / %x68 / %x6d / %x73 ; "d" | "h" | "m" | "s"
Pekka Pessi's avatar
Pekka Pessi committed
989 990 991
   */

  unsigned long tt, *interval;
Michael Jerris's avatar
Michael Jerris committed
992 993
  size_t i;
  int n, N;
Pekka Pessi's avatar
Pekka Pessi committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  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])))) {
1016
    parse_alloc_error(p, "sdp_repeat_t");
Pekka Pessi's avatar
Pekka Pessi committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    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++;
1038
  }
Pekka Pessi's avatar
Pekka Pessi committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

  *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
 *
 */
static void parse_zone(sdp_parser_t *p, char *r, sdp_zone_t **result)
{
  char *s;
Michael Jerris's avatar
Michael Jerris committed
1058 1059
  size_t i;
  int n, N;
Pekka Pessi's avatar
Pekka Pessi committed
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
  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])))) {
1093
    parse_alloc_error(p, "sdp_zone_t");
Pekka Pessi's avatar
Pekka Pessi committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    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;
1112
  }
Pekka Pessi's avatar
Pekka Pessi committed
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

  *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
 *
 */
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 | "~" | "
   */

1143
  s = token(&r, ":", TOKEN, SPACE TAB);
Pekka Pessi's avatar
Pekka Pessi committed
1144 1145 1146 1147 1148 1149 1150 1151 1152
  if (!s) {
    parsing_error(p, "invalid key method");
    return;
  }

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

1153 1154 1155 1156 1157
    /* These are defined as key-sensitive in RFC 4566 */
#define MATCH(s, tok) \
    (STRICT(p) ? strcmp((s), (tok)) == 0 : strcasecmp((s), (tok)) == 0)

    if (MATCH(s, "clear"))
Pekka Pessi's avatar
Pekka Pessi committed
1158
      k->k_method = sdp_key_clear, k->k_method_name = "clear";
1159
    else if (MATCH(s, "base64"))
Pekka Pessi's avatar
Pekka Pessi committed
1160
      k->k_method = sdp_key_base64, k->k_method_name = "base64";
1161
    else if (MATCH(s, "uri"))
Pekka Pessi's avatar
Pekka Pessi committed
1162
      k->k_method = sdp_key_uri, k->k_method_name = "uri";
1163
    else if (MATCH(s, "prompt"))
Pekka Pessi's avatar
Pekka Pessi committed
1164
      k->k_method = sdp_key_prompt, k->k_method_name = "prompt";
1165
    else if (!STRICT(p))
Pekka Pessi's avatar
Pekka Pessi committed
1166
      k->k_method = sdp_key_x, k->k_method_name = s;
1167 1168 1169 1170
    else {
      parsing_error(p, "invalid key method");
      return;
    }
Pekka Pessi's avatar
Pekka Pessi committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

    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)

1192
   attribute =           (att-field ":" att-value) / att-field
Pekka Pessi's avatar
Pekka Pessi committed
1193

1194
   att-field =           token
Pekka Pessi's avatar
Pekka Pessi committed
1195 1196 1197 1198 1199 1200

   att-value =           byte-string
   */

  char *name = NULL, *value = NULL;

1201
  if (!(name = token(&r, ":", TOKEN, SPACE TAB))) {
Pekka Pessi's avatar
Pekka Pessi committed
1202 1203 1204 1205 1206 1207
    parsing_error(p,"invalid attribute name");
    return;
  }

  if (*r)
    value = r;
1208
  else
Pekka Pessi's avatar
Pekka Pessi committed
1209 1210 1211 1212
    PARSE_CHECK_REST(p, r, "a");

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

1216
  if (p->pr_mode_manual)
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    ;
  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;

1227 1228 1229
  {
    PARSE_ALLOC(p, sdp_attribute_t, a);
    *result = a;
1230

1231 1232 1233
    a->a_name  = name;
    a->a_value = value;
  }
Pekka Pessi's avatar
Pekka Pessi committed
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
}

/* -------------------------------------------------------------------------
 * 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

1260
   media =               token
Pekka Pessi's avatar
Pekka Pessi committed
1261 1262 1263
                         ;typically "audio", "video", "application"
                         ;or "data"

1264
   fmt =                 token
Pekka Pessi's avatar
Pekka Pessi committed
1265 1266 1267
                         ;typically an RTP payload type for audio
                         ;and video media

1268
   proto =               token *("/" token)
Pekka Pessi's avatar
Pekka Pessi committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
                         ;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;

1282
  s = token(&r, SPACE, TOKEN, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
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
  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;
  }

1314
  s = token(&r, SPACE, "/" TOKEN, SPACE);
Pekka Pessi's avatar
Pekka Pessi committed
1315 1316 1317 1318
  if (s == NULL) {
    parsing_error(p, "m= missing protocol");
    return;
  }
1319

Pekka Pessi's avatar
Pekka Pessi committed
1320 1321
  if (!STRICT(p) && strcasecmp(s, "RTP") == 0)
    m->m_proto = sdp_proto_rtp, m->m_proto_name = "RTP/AVP";
1322
  else
Pekka Pessi's avatar
Pekka Pessi committed
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    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;
1334

Pekka Pessi's avatar
Pekka Pessi committed
1335 1336 1337
    while (r && *r) {
      PARSE_ALLOC(p, sdp_list_t, l);
      *fmt = l;
1338
      l->l_text = token(&r, SPACE TAB, TOKEN, SPACE TAB);
Pekka Pessi's avatar
Pekka Pessi committed
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
      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";
  else if (strcasecmp(s, "image") == 0)
    m->m_type = sdp_media_image, m->m_type_name = "image";
1363 1364
  else if (strcasecmp(s, "red") == 0)
    m->m_type = sdp_media_red, m->m_type_name = "red";
Pekka Pessi's avatar
Pekka Pessi committed
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
  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";
1377 1378 1379
  else if (strcasecmp(s, "udptl") == 0)
    /* Lower case - be compatible with people living by T.38 examples */
    m->m_proto = sdp_proto_udptl, m->m_proto_name = "udptl";
Pekka Pessi's avatar
Pekka Pessi committed
1380 1381 1382 1383 1384 1385
  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";
1386
  else
Pekka Pessi's avatar
Pekka Pessi committed
1387 1388 1389 1390 1391 1392 1393 1394 1395
    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);
}

1396 1397
#define RTPMAP(pt, encoding, rate, params) \
  { sizeof(sdp_rtpmap_t), NULL, encoding, rate, (char *)params, NULL, 1, pt, 0 }
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

/* rtpmaps for well-known codecs */
static sdp_rtpmap_t const
  sdp_rtpmap_pcmu = RTPMAP(0, "PCMU", 8000, 0),
  sdp_rtpmap_1016 = RTPMAP(1, "1016", 8000, 0),
  sdp_rtpmap_g721 = RTPMAP(2, "G721", 8000, 0),
  sdp_rtpmap_gsm =  RTPMAP(3, "GSM",  8000, 0),
  sdp_rtpmap_g723 = RTPMAP(4, "G723", 8000, 0),
  sdp_rtpmap_dvi4_8000 = RTPMAP(5, "DVI4", 8000, 0),
  sdp_rtpmap_dvi4_16000 = RTPMAP(6, "DVI4", 16000, 0),
  sdp_rtpmap_lpc = RTPMAP(7, "LPC",  8000, 0),
  sdp_rtpmap_pcma = RTPMAP(8, "PCMA", 8000, 0),
  sdp_rtpmap_g722 = RTPMAP(9, "G722", 8000, 0),
  sdp_rtpmap_l16_2 = RTPMAP(10, "L16", 44100, "2"),
  sdp_rtpmap_l16 = RTPMAP(11, "L16", 44100, 0),
  sdp_rtpmap_qcelp = RTPMAP(12, "QCELP", 8000, 0),
  sdp_rtpmap_cn = RTPMAP(13, "CN", 8000, 0),
  sdp_rtpmap_mpa = RTPMAP(14, "MPA", 90000, 0),
  sdp_rtpmap_g728 = RTPMAP(15, "G728", 8000, 0),
  sdp_rtpmap_dvi4_11025 = RTPMAP(16, "DVI4", 11025, 0),
  sdp_rtpmap_dvi4_22050 = RTPMAP(17, "DVI4", 22050, 0),
  sdp_rtpmap_g729 = RTPMAP(18, "G729", 8000, 0),
  sdp_rtpmap_reserved_cn = RTPMAP(19, "CN", 8000, 0),
  /* video codecs */
  sdp_rtpmap_celb = RTPMAP(25, "CelB", 90000, 0),
  sdp_rtpmap_jpeg = RTPMAP(26, "JPEG", 90000, 0),
  sdp_rtpmap_nv = RTPMAP(28, "nv",   90000, 0),
  sdp_rtpmap_h261 = RTPMAP(31, "H261", 90000, 0),
  sdp_rtpmap_mpv = RTPMAP(32, "MPV",  90000, 0),
  sdp_rtpmap_mp2t = RTPMAP(33, "MP2T", 90000, 0),
  sdp_rtpmap_h263 = RTPMAP(34, "H263", 90000, 0);

/** Table of rtpmap structures by payload type numbers.
 *
 * The table of reserved payload numbers is constructed from @RFC3551
1433
 * and @RFC1890. Note the clock rate of G722.
1434 1435
 *
 * Use sdp_rtpmap_dup() to copy these structures.
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
 */
sdp_rtpmap_t const * const sdp_rtpmap_well_known[128] =
{
  &sdp_rtpmap_pcmu,		/* 0 */
  &sdp_rtpmap_1016,		/* 1 */
  &sdp_rtpmap_g721,		/* 2 */
  &sdp_rtpmap_gsm,		/* 3 */
  &sdp_rtpmap_g723,		/* 4 */
  &sdp_rtpmap_dvi4_8000,	/* 5 */
  &sdp_rtpmap_dvi4_16000,	/* 6 */
  &sdp_rtpmap_lpc,		/* 7 */
  &sdp_rtpmap_pcma,		/* 8 */
  &sdp_rtpmap_g722,		/* 9 */
  &sdp_rtpmap_l16_2,		/* 10 */
  &sdp_rtpmap_l16,		/* 11 */
  &sdp_rtpmap_qcelp,		/* 12 */
  &sdp_rtpmap_cn,		/* 13 */
  &sdp_rtpmap_mpa,		/* 14 */
  &sdp_rtpmap_g728,		/* 15 */
  &sdp_rtpmap_dvi4_11025,	/* 16 */
  &sdp_rtpmap_dvi4_22050,	/* 17 */
  &sdp_rtpmap_g729,		/* 18 */
  &sdp_rtpmap_reserved_cn,	/* 19 */
  NULL,				/* 20 */
  NULL,				/* 21 */
  NULL,				/* 22 */
  NULL,				/* 23 */
  NULL,				/* 24 */
  &sdp_rtpmap_celb,		/* 25 */
  &sdp_rtpmap_jpeg,		/* 26 */
  NULL,				/* 27 */
  &sdp_rtpmap_nv,		/* 28 */
  NULL,				/* 29 */
  NULL,				/* 30 */
  &sdp_rtpmap_h261,		/* 31 */
  &sdp_rtpmap_mpv,		/* 32 */
  &sdp_rtpmap_mp2t,		/* 33 */
  &sdp_rtpmap_h263,		/* 34 */
  NULL,
};

Pekka Pessi's avatar
Pekka Pessi committed
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
/**
 * 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)
{
  while (*r) {
    unsigned long value;

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

1493 1494
      assert(0 <= value && value < 128);

Pekka Pessi's avatar
Pekka Pessi committed
1495 1496
      *result = rm; result = &rm->rm_next;

1497 1498 1499 1500 1501 1502 1503 1504
      if (sdp_rtpmap_well_known[value]) {
	*rm = *sdp_rtpmap_well_known[value];
      }
      else {
	rm->rm_predef = 1;
	rm->rm_pt = value;
	rm->rm_encoding = "";
	rm->rm_rate = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1505 1506 1507 1508 1509 1510 1511 1512 1513
      }
    }
    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;
1514 1515
      rm->rm_encoding = "*";
      rm->rm_rate = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1516

1517
      return;
Pekka Pessi's avatar
Pekka Pessi committed
1518 1519 1520
    }
    else {
      parsing_error(p, "m= invalid format for RTP/AVT");
1521 1522

      return;
Pekka Pessi's avatar
Pekka Pessi committed
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
    }
  }
}

/* -------------------------------------------------------------------------
 * 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)

1544
   attribute =           (att-field ":" att-value) / att-field
Pekka Pessi's avatar
Pekka Pessi committed
1545

1546
   att-field =           token
Pekka Pessi's avatar
Pekka Pessi committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556

   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;

1557
  if (!(name = token(&r, ":", TOKEN, SPACE TAB))) {
Pekka Pessi's avatar
Pekka Pessi committed
1558 1559 1560 1561 1562 1563
    parsing_error(p,"invalid attribute name");
    return;
  }

  if (*r)
    value = r;
1564
  else
Pekka Pessi's avatar
Pekka Pessi committed
1565 1566
    PARSE_CHECK_REST(p, r, "a");

1567
  if (p->pr_mode_manual)
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
    ;
  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
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
  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;
  }

1631
  encoding = token(&r, "/", TOKEN, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1632 1633 1634 1635 1636 1637 1638 1639 1640
  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;
  }

1641
  if (*r == '/')
Pekka Pessi's avatar
Pekka Pessi committed
1642 1643 1644
    params = ++r;
  else
    params = 0;
1645

Pekka Pessi's avatar
Pekka Pessi committed
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 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
  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
1694
 *   message - pointer to rest
Pekka Pessi's avatar
Pekka Pessi committed
1695 1696
 *   medias - pointer to which parsed media structures are assigned
 */
1697 1698 1699
static void parse_descs(sdp_parser_t *p,
			char *record,
			char *message,
Pekka Pessi's avatar
Pekka Pessi committed
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
			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
1712
    strip = "";
1713

Pekka Pessi's avatar
Pekka Pessi committed
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
  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) {
1749
	m->m_mode = p->pr_session_mode;
Pekka Pessi's avatar
Pekka Pessi committed
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
	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
 */
1771
static int parse_ul(sdp_parser_t *p, char **r,
Pekka Pessi's avatar
Pekka Pessi committed
1772 1773
		    unsigned long *result, unsigned long max)
{
1774
  char *ul = *r;
Pekka Pessi's avatar
Pekka Pessi committed
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786

  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;
}

1787 1788 1789 1790
#if !HAVE_STRTOULL
unsigned longlong strtoull(char const *string, char **return_end, int base);
#endif

Pekka Pessi's avatar
Pekka Pessi committed
1791
/*
1792
 * parse_ull: parse an unsigned long long
Pekka Pessi's avatar
Pekka Pessi committed
1793
 */
1794
static int parse_ull(sdp_parser_t *p, char **r,
Pekka Pessi's avatar
Pekka Pessi committed
1795 1796
		     uint64_t *result, uint64_t max)
{
1797
  unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
1798

1799
  char *s = *r;
Pekka Pessi's avatar
Pekka Pessi committed
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813

  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;
}

1814 1815 1816
static char *token(char **message,
		   const char *sep,
		   const char *legal,
Pekka Pessi's avatar
Pekka Pessi committed
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
		   const char *strip)
{
  size_t n;
  char *retval = *message;

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

  if (legal)
    n = strspn(retval, legal);
1827
  else
Pekka Pessi's avatar
Pekka Pessi committed
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
    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;

1851
  if (strip[0])
Pekka Pessi's avatar
Pekka Pessi committed
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
    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;
}

1872
static int parsing_error(sdp_parser_t *p, char const *fmt, ...)
Pekka Pessi's avatar
Pekka Pessi committed
1873 1874 1875
{
  int n;
  va_list ap;
1876 1877
  va_start(ap, fmt);

Pekka Pessi's avatar
Pekka Pessi committed
1878 1879 1880 1881 1882
  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;
1883 1884

  return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1885 1886 1887 1888
}

static void parse_alloc_error(sdp_parser_t *p, const char *typename)
{
1889
  parsing_error(p, "memory exhausted (while allocating memory for %s)",
Pekka Pessi's avatar
Pekka Pessi committed
1890 1891
		typename);
}