sdp_parse.c 49.2 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
 * @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
33 34
 *
 * @sa @RFC4566, @RFC2327.
Pekka Pessi's avatar
Pekka Pessi committed
35 36 37 38
 */

#include "config.h"

39
#include <sofia-sip/su_alloc.h>
40
#include <sofia-sip/su_string.h>
41 42 43

#include "sofia-sip/sdp.h"

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

52
/** @typedef struct sdp_parser_s sdp_parser_t;
53 54 55
 *
 * SDP parser handle.
 *
56 57
 * The SDP parser handle returned by sdp_parse() contains either
 * a successfully parsed SDP session #sdp_session_t or an error message.
58 59 60
 * If sdp_session() returns non-NULL, parsing was successful.
 *
 * @sa #sdp_session_t, sdp_parse(), sdp_session(), sdp_parsing_error(),
61 62
 * sdp_sanity_check(), sdp_parser_home(), sdp_parser_free(), @RFC4566,
 * @RFC2327.
63 64
 */

Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71
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;
72 73 74

  sdp_mode_t pr_session_mode;

Pekka Pessi's avatar
Pekka Pessi committed
75 76 77 78 79
  unsigned   pr_ok : 1;

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

/** 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
111
 * may not contain a NUL.
112
 *
113
 * The parsing result is stored to an #sdp_session_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
114 115 116 117 118 119
 *
 * @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.
 *
120
 * The following flags are used by parser:
Pekka Pessi's avatar
Pekka Pessi committed
121
 *
122
 * @li #sdp_f_strict Parser should accept only messages conforming strictly
123 124 125 126 127 128 129 130
 *                   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
131 132
 *
 * @return
133 134 135
 * Always a valid parser handle.
 *
 * @todo Parser accepts some non-conforming SDP even with #sdp_f_strict.
136 137 138
 *
 * @sa sdp_session(), sdp_parsing_error(), sdp_sanity_check(),
 * sdp_parser_home(), sdp_parser_free(), @RFC4566, @RFC2327.
Pekka Pessi's avatar
Pekka Pessi committed
139 140
 */
sdp_parser_t *
141
sdp_parse(su_home_t *home, char const msg[], issize_t msgsize, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
142
{
143
  sdp_parser_t *p;
144
  char *b;
145
  size_t len;
146

147
  if (msgsize == -1 || msg == NULL) {
148
    p = su_home_clone(home, sizeof(*p));
149
    if (p)
150 151 152
      parsing_error(p, "invalid input message");
    else
      p = (sdp_parser_t*)&no_mem_error;
153 154 155
    return p;
  }

156 157 158 159 160 161 162 163
  if (msgsize == -1 && msg)
    len = strlen(msg);
  else
    len = msgsize;

  if (len > ISSIZE_MAX)
    len = ISSIZE_MAX;

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

166
  if (p) {
167
    b = strncpy((void *)(p + 1), msg, len);
168
    b[len] = 0;
169

Pekka Pessi's avatar
Pekka Pessi committed
170 171 172 173 174 175 176 177
    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;
178 179 180
    p->pr_mode_manual = (flags & sdp_f_mode_manual) != 0;
    p->pr_session_mode = sdp_sendrecv;

Pekka Pessi's avatar
Pekka Pessi committed
181
    parse_message(p);
182

Pekka Pessi's avatar
Pekka Pessi committed
183 184 185 186 187 188 189 190 191
    return p;
  }

  if (p)
    sdp_parser_free(p);

  return (sdp_parser_t*)&no_mem_error;
}

Pekka Pessi's avatar
Pekka Pessi committed
192 193 194 195 196 197 198 199 200 201

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

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

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

/* Parsing tokens */
#define SPACE " "
#define TAB   "\011"
#define CRLF  "\015\012"
#define ALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define DIGIT "0123456789"
260
#define TOKEN ALPHA DIGIT "-!#$%&'*+.^_`{|}~"
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

/* ========================================================================= */
/* 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);
281
static void parse_media_attr(sdp_parser_t *p, char *r, sdp_media_t *m,
Pekka Pessi's avatar
Pekka Pessi committed
282 283 284 285 286 287 288
			     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);

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

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

/* -------------------------------------------------------------------------
324
 * Function parse_message() - parse an SDP message
Pekka Pessi's avatar
Pekka Pessi committed
325 326
 *
 * Description:
327
 *   This function parses an SDP message, which is copied into the
Pekka Pessi's avatar
Pekka Pessi committed
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 361 362 363 364 365 366 367
 *   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
368
    strip = "";
Pekka Pessi's avatar
Pekka Pessi committed
369 370 371 372 373 374

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

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

376
  if (!su_strmatch(record, "v=0")) {
377 378 379 380 381 382 383
    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
384 385
  }

386 387 388 389
  /*
    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
390

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

    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)
{
486 487 488
  return
    c &&
    c->c_nettype == sdp_net_in &&
489 490
    ((c->c_addrtype == sdp_addr_ip4 && su_strmatch(c->c_address, "0.0.0.0")) ||
     (c->c_addrtype == sdp_addr_ip6 && su_strmatch(c->c_address, "::")));
Pekka Pessi's avatar
Pekka Pessi committed
491 492 493 494
}

/**Postprocess session description.
 *
495 496
 * Postprocessing includes setting the session backpointer for each media,
 * doing sanity checks and setting rejected and mode flags.
Pekka Pessi's avatar
Pekka Pessi committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
 */
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;
  }
516

Pekka Pessi's avatar
Pekka Pessi committed
517 518 519 520 521 522 523 524 525 526
  /* 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);

527 528 529
    if (p->pr_mode_0000 && sdp_connection_is_inaddr_any(c)) {
      /* Reset recvonly flag */
      m->m_mode &= ~sdp_recvonly;
Pekka Pessi's avatar
Pekka Pessi committed
530 531 532
    }
  }

533 534 535
  if (p->pr_insane)
    return;

Pekka Pessi's avatar
Pekka Pessi committed
536 537 538 539 540
  /* Verify that all mandatory fields are present */
  if (sdp_sanity_check(p) < 0)
    return;
}

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

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

633

Pekka Pessi's avatar
Pekka Pessi committed
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
   */
  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;

822
  if (su_casenmatch(r, "IN", 2)) {
Pekka Pessi's avatar
Pekka Pessi committed
823 824 825 826 827 828 829 830
    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);
831
    if (su_casematch(s, "IP4"))
Pekka Pessi's avatar
Pekka Pessi committed
832
      c->c_addrtype = sdp_addr_ip4;
833
    else if (su_casematch(s, "IP6"))
Pekka Pessi's avatar
Pekka Pessi committed
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
      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;
  }
880
  else
Pekka Pessi's avatar
Pekka Pessi committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    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)
899
   bwtype =              token
Pekka Pessi's avatar
Pekka Pessi committed
900 901 902 903 904 905 906
   bandwidth =           1*(DIGIT)
   */
  /* NOTE: bwtype can also be like X-barf */
  sdp_bandwidth_e modifier;
  char *name;
  unsigned long value;

907
  name = token(&r, ":", TOKEN, SPACE TAB);
Pekka Pessi's avatar
Pekka Pessi committed
908 909 910 911 912 913

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

914
  if (su_casematch(name, "CT"))
Pekka Pessi's avatar
Pekka Pessi committed
915
    modifier = sdp_bw_ct, name = NULL;
916
  else if (su_casematch(name, "AS") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
917 918 919 920 921 922
    modifier = sdp_bw_as, name = NULL;
  else
    modifier = sdp_bw_x;

  if (STRICT(p))
    PARSE_CHECK_REST(p, r, "b");
923

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

951
   start-time =          time / "0"
Pekka Pessi's avatar
Pekka Pessi committed
952

953
   stop-time =           time / "0"
Pekka Pessi's avatar
Pekka Pessi committed
954

955 956 957 958 959 960 961
   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
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
   */
  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
981
 *
Pekka Pessi's avatar
Pekka Pessi committed
982 983 984 985
 */
static void parse_repeat(sdp_parser_t *p, char *d, sdp_repeat_t **result)
{
  /*
986
   repeat-fields =       %x72 "=" repeat-interval 2*(SP typed-time)
Pekka Pessi's avatar
Pekka Pessi committed
987

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

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

992
   fixed-len-time-unit = %x64 / %x68 / %x6d / %x73 ; "d" | "h" | "m" | "s"
Pekka Pessi's avatar
Pekka Pessi committed
993 994 995
   */

  unsigned long tt, *interval;
Michael Jerris's avatar
Michael Jerris committed
996 997
  size_t i;
  int n, N;
Pekka Pessi's avatar
Pekka Pessi committed
998 999
  char *s;
  sdp_repeat_t *r;
1000
  int strict = STRICT(p);
Pekka Pessi's avatar
Pekka Pessi committed
1001 1002 1003

  /** Count number of intervals */
  for (N = 0, s = d; *s; ) {
1004
    if (!(is_posdigit(*s) || (!strict && (*s) == '0')))
Pekka Pessi's avatar
Pekka Pessi committed
1005 1006
      break;
    do { s++; } while (is_digit(*s));
1007
    if (*s && strchr(strict ? "dhms" : "dhmsDHMS", *s))
Pekka Pessi's avatar
Pekka Pessi committed
1008 1009
      s++;
    N++;
1010
    if (!(i = strict ? is_space(*s) : strspn(s, SPACE TAB)))
Pekka Pessi's avatar
Pekka Pessi committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
      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])))) {
1021
    parse_alloc_error(p, "sdp_repeat_t");
Pekka Pessi's avatar
Pekka Pessi committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    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) {
1032 1033 1034 1035
    case 'd': case 'D': tt *= 24;
    case 'h': case 'H': tt *= 60;
    case 'm': case 'M': tt *= 60;
    case 's': case 'S': d++;
Pekka Pessi's avatar
Pekka Pessi committed
1036 1037 1038 1039 1040 1041 1042
      break;
    }

    interval[n] = tt;

    while (is_space(*d))
      d++;
1043
  }
Pekka Pessi's avatar
Pekka Pessi committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

  *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
1063 1064
  size_t i;
  int n, N;
Pekka Pessi's avatar
Pekka Pessi committed
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
  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])))) {
1098
    parse_alloc_error(p, "sdp_zone_t");
Pekka Pessi's avatar
Pekka Pessi committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    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;
1117
  }
Pekka Pessi's avatar
Pekka Pessi committed
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

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

1148
  s = token(&r, ":", TOKEN, SPACE TAB);
Pekka Pessi's avatar
Pekka Pessi committed
1149 1150 1151 1152 1153 1154 1155 1156 1157
  if (!s) {
    parsing_error(p, "invalid key method");
    return;
  }

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

1158 1159
    /* These are defined as key-sensitive in RFC 4566 */
#define MATCH(s, tok) \
1160
    (STRICT(p) ? su_strmatch((s), (tok)) : su_casematch((s), (tok)))
1161 1162

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

    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)

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

1199
   att-field =           token
Pekka Pessi's avatar
Pekka Pessi committed
1200 1201 1202 1203 1204 1205

   att-value =           byte-string
   */

  char *name = NULL, *value = NULL;

1206
  if (!(name = token(&r, ":", TOKEN, SPACE TAB))) {
Pekka Pessi's avatar
Pekka Pessi committed
1207 1208 1209 1210 1211 1212
    parsing_error(p,"invalid attribute name");
    return;
  }

  if (*r)
    value = r;
1213
  else
Pekka Pessi's avatar
Pekka Pessi committed
1214 1215
    PARSE_CHECK_REST(p, r, "a");

1216
  if (su_casematch(name, "charset")) {
Pekka Pessi's avatar
Pekka Pessi committed
1217
    p->pr_session->sdp_charset = value;
1218
    return;
Pekka Pessi's avatar
Pekka Pessi committed
1219 1220
  }

1221
  if (p->pr_mode_manual)
1222
    ;
1223
  else if (su_casematch(name, "inactive"))
1224
    p->pr_session_mode = sdp_inactive;
1225
  else if (su_casematch(name, "sendonly"))
1226
    p->pr_session_mode = sdp_sendonly;
1227
  else if (su_casematch(name, "recvonly"))
1228
    p->pr_session_mode = sdp_recvonly;
1229
  else if (su_casematch(name, "sendrecv"))
1230 1231
    p->pr_session_mode = sdp_sendrecv;

1232 1233 1234
  {
    PARSE_ALLOC(p, sdp_attribute_t, a);
    *result = a;
1235

1236 1237 1238
    a->a_name  = name;
    a->a_value = value;
  }
Pekka Pessi's avatar
Pekka Pessi committed
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
}

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

1265
   media =               token
Pekka Pessi's avatar
Pekka Pessi committed
1266 1267 1268
                         ;typically "audio", "video", "application"
                         ;or "data"

1269
   fmt =                 token
Pekka Pessi's avatar
Pekka Pessi committed
1270 1271 1272
                         ;typically an RTP payload type for audio
                         ;and video media

1273
   proto =               token *("/" token)
Pekka Pessi's avatar
Pekka Pessi committed
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
                         ;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;

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

1319
  s = token(&r, SPACE, "/" TOKEN, SPACE);
Pekka Pessi's avatar
Pekka Pessi committed
1320 1321 1322 1323
  if (s == NULL) {
    parsing_error(p, "m= missing protocol");
    return;
  }
1324

1325
  if (!STRICT(p) && su_casematch(s, "RTP"))
Pekka Pessi's avatar
Pekka Pessi committed
1326
    m->m_proto = sdp_proto_rtp, m->m_proto_name = "RTP/AVP";
1327
  else
Pekka Pessi's avatar
Pekka Pessi committed
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    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;
1339

Pekka Pessi's avatar
Pekka Pessi committed
1340 1341 1342
    while (r && *r) {
      PARSE_ALLOC(p, sdp_list_t, l);
      *fmt = l;
1343
      l->l_text = token(&r, SPACE TAB, TOKEN, SPACE TAB);
Pekka Pessi's avatar
Pekka Pessi committed
1344 1345 1346 1347 1348 1349 1350 1351
      fmt = &l->l_next;
    }
  }
}

/** Set media type */
void sdp_media_type(sdp_media_t *m, char const *s)
{
1352
  if (su_strmatch(s, "*"))
Pekka Pessi's avatar
Pekka Pessi committed
1353
    m->m_type = sdp_media_any, m->m_type_name = "*";
1354
  else if (su_casematch(s, "audio"))
Pekka Pessi's avatar
Pekka Pessi committed
1355
    m->m_type = sdp_media_audio, m->m_type_name = "audio";
1356
  else if (su_casematch(s, "video"))
Pekka Pessi's avatar
Pekka Pessi committed
1357
    m->m_type = sdp_media_video, m->m_type_name = "video";
1358
  else if (su_casematch(s, "application"))
Pekka Pessi's avatar
Pekka Pessi committed
1359
    m->m_type = sdp_media_application, m->m_type_name = "application";
1360
  else if (su_casematch(s, "data"))
Pekka Pessi's avatar
Pekka Pessi committed
1361
    m->m_type = sdp_media_data, m->m_type_name = "data";
1362
  else if (su_casematch(s, "control"))
Pekka Pessi's avatar
Pekka Pessi committed
1363
    m->m_type = sdp_media_control, m->m_type_name = "control";
1364
  else if (su_casematch(s, "message"))
Pekka Pessi's avatar
Pekka Pessi committed
1365
    m->m_type = sdp_media_message, m->m_type_name = "message";
1366
  else if (su_casematch(s, "image"))
Pekka Pessi's avatar
Pekka Pessi committed
1367
    m->m_type = sdp_media_image, m->m_type_name = "image";
1368
  else if (su_casematch(s, "red"))
1369
    m->m_type = sdp_media_red, m->m_type_name = "red";
Pekka Pessi's avatar
Pekka Pessi committed
1370 1371 1372 1373
  else
    m->m_type = sdp_media_x, m->m_type_name = s;
}

1374 1375 1376 1377 1378
/** Set transport protocol.
 *
 * Set the @m->m_proto to a well-known protocol type as
 * well as canonize case of @a m_proto_name.
 */
Pekka Pessi's avatar
Pekka Pessi committed
1379 1380
void sdp_media_transport(sdp_media_t *m, char const *s)
{
1381 1382 1383
  if (m == NULL || s == NULL)
    ;
  else if (su_strmatch(s, "*"))
Pekka Pessi's avatar
Pekka Pessi committed
1384
    m->m_proto = sdp_proto_any, m->m_proto_name = "*";
1385
  else if (su_casematch(s, "RTP/AVP"))
Pekka Pessi's avatar
Pekka Pessi committed
1386
    m->m_proto = sdp_proto_rtp, m->m_proto_name = "RTP/AVP";
1387
  else if (su_casematch(s, "RTP/SAVP"))
Pekka Pessi's avatar
Pekka Pessi committed
1388
    m->m_proto = sdp_proto_srtp, m->m_proto_name = "RTP/SAVP";
1389
  else if (su_casematch(s, "udptl"))
1390 1391
    /* Lower case - be compatible with people living by T.38 examples */
    m->m_proto = sdp_proto_udptl, m->m_proto_name = "udptl";
1392
  else if (su_casematch(s, "UDP"))
Pekka Pessi's avatar
Pekka Pessi committed
1393
    m->m_proto = sdp_proto_udp, m->m_proto_name = "UDP";
1394
  else if (su_casematch(s, "TCP"))
Pekka Pessi's avatar
Pekka Pessi committed
1395
    m->m_proto = sdp_proto_tcp, m->m_proto_name = "TCP";
1396
  else if (su_casematch(s, "TLS"))
Pekka Pessi's avatar
Pekka Pessi committed
1397
    m->m_proto = sdp_proto_tls, m->m_proto_name = "TLS";
1398
  else
Pekka Pessi's avatar
Pekka Pessi committed
1399 1400 1401 1402 1403 1404 1405 1406 1407
    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);
}

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

/* 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
1445
 * and @RFC1890. Note the clock rate of G722.
1446 1447
 *
 * Use sdp_rtpmap_dup() to copy these structures.
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 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
 */
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
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
/**
 * 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);

1505 1506
      assert(0 <= value && value < 128);

Pekka Pessi's avatar
Pekka Pessi committed
1507 1508
      *result = rm; result = &rm->rm_next;

1509 1510 1511 1512 1513 1514 1515 1516
      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
1517 1518 1519 1520 1521 1522 1523 1524 1525
      }
    }
    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;
1526 1527
      rm->rm_encoding = "*";
      rm->rm_rate = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1528

1529
      return;
Pekka Pessi's avatar
Pekka Pessi committed
1530 1531 1532
    }
    else {
      parsing_error(p, "m= invalid format for RTP/AVT");
1533 1534

      return;
Pekka Pessi's avatar
Pekka Pessi committed
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
    }
  }
}

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

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

1558
   att-field =           token
Pekka Pessi's avatar
Pekka Pessi committed
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568

   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;

1569
  if (!(name = token(&r, ":", TOKEN, SPACE TAB))) {
Pekka Pessi's avatar
Pekka Pessi committed
1570 1571 1572 1573 1574 1575
    parsing_error(p,"invalid attribute name");
    return;
  }

  if (*r)
    value = r;
1576
  else
Pekka Pessi's avatar
Pekka Pessi committed
1577 1578
    PARSE_CHECK_REST(p, r, "a");

1579
  if (p->pr_mode_manual)
1580
    ;
1581
  else if (su_casematch(name, "inactive")) {
1582 1583 1584
    m->m_mode = sdp_inactive;
    return;
  }
1585
  else if (su_casematch(name, "sendonly")) {
1586 1587 1588
    m->m_mode = sdp_sendonly;
    return;
  }
1589
  else if (su_casematch(name, "recvonly")) {
1590 1591 1592
    m->m_mode = sdp_recvonly;
    return;
  }
1593
  else if (su_casematch(name, "sendrecv")) {
1594 1595 1596 1597
    m->m_mode = sdp_sendrecv;
    return;
  }

1598
  if (rtp && su_casematch(name, "rtpmap")) {
Pekka Pessi's avatar
Pekka Pessi committed
1599 1600 1601
    if ((n = parse_rtpmap(p, r, m)) == 0 || n < -1)
      return;
  }
1602
  else if (rtp && su_casematch(name, "fmtp")) {
Pekka Pessi's avatar
Pekka Pessi committed
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
    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;
  }

1643
  encoding = token(&r, "/", TOKEN, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1644 1645 1646 1647 1648 1649 1650 1651 1652
  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;
  }

1653
  if (*r == '/')
Pekka Pessi's avatar
Pekka Pessi committed
1654 1655 1656
    params = ++r;
  else
    params = 0;
1657

Pekka Pessi's avatar
Pekka Pessi committed
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 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
  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
1706
 *   message - pointer to rest
Pekka Pessi's avatar
Pekka Pessi committed
1707 1708
 *   medias - pointer to which parsed media structures are assigned
 */
1709 1710 1711
static void parse_descs(sdp_parser_t *p,
			char *record,
			char *message,
Pekka Pessi's avatar
Pekka Pessi committed
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
			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
1724
    strip = "";
1725

Pekka Pessi's avatar
Pekka Pessi committed
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
  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) {
1761
	m->m_mode = p->pr_session_mode;
Pekka Pessi's avatar
Pekka Pessi committed
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
	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
 */
1783
static int parse_ul(sdp_parser_t *p, char **r,
Pekka Pessi's avatar
Pekka Pessi committed
1784 1785
		    unsigned long *result, unsigned long max)
{
1786
  char *ul = *r;
Pekka Pessi's avatar
Pekka Pessi committed
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798

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

1799 1800 1801 1802
#if !HAVE_STRTOULL
unsigned longlong strtoull(char const *string, char **return_end, int base);
#endif

Pekka Pessi's avatar
Pekka Pessi committed
1803
/*
1804
 * parse_ull: parse an unsigned long long
Pekka Pessi's avatar
Pekka Pessi committed
1805
 */
1806
static int parse_ull(sdp_parser_t *p, char **r,
Pekka Pessi's avatar
Pekka Pessi committed
1807 1808
		     uint64_t *result, uint64_t max)
{
1809
  unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
1810

1811
  char *s = *r;
Pekka Pessi's avatar
Pekka Pessi committed
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825

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

1826 1827 1828
static char *token(char **message,
		   const char *sep,
		   const char *legal,
Pekka Pessi's avatar
Pekka Pessi committed
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
		   const char *strip)
{
  size_t n;
  char *retval = *message;

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

  if (legal)
    n = strspn(retval, legal);
1839
  else
Pekka Pessi's avatar
Pekka Pessi committed
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
    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;

1863
  if (strip[0])
Pekka Pessi's avatar
Pekka Pessi committed
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
    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;
}

1884
static int parsing_error(sdp_parser_t *p, char const *fmt, ...)
Pekka Pessi's avatar
Pekka Pessi committed
1885 1886 1887
{
  int n;
  va_list ap;
1888 1889
  va_start(ap, fmt);

Pekka Pessi's avatar
Pekka Pessi committed
1890 1891 1892 1893 1894
  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;
1895 1896

  return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1897 1898 1899 1900
}

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