sdp.c 46.1 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * 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
 *
 */

/**@CFILE sdp.c Simple SDP 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"

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

42 43
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su_types.h>
Pekka Pessi's avatar
Pekka Pessi committed
44

45
#include "sofia-sip/sdp.h"
Pekka Pessi's avatar
Pekka Pessi committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

struct align { void  *_a; char _b; };

#define ALIGN(v, n) ((n - (intptr_t)(v)) & (n - 1))
#define STRUCT_ALIGN_  (sizeof(struct align) - offsetof(struct align, _b))
#define STRUCT_ALIGN(v) ALIGN(v, sizeof(void *))
#define ASSERT_STRUCT_ALIGN(p) \
  (STRUCT_ALIGN(p) ? (void)assert(!"STRUCT_ALIGNED(" #p ")") : (void)0)

const unsigned sdp_struct_align_ = sizeof(void *) - STRUCT_ALIGN_;

#define STR_XTRA(rv, s)    ((s) ? rv += strlen((s)) + 1 : 0)
#define PTR_XTRA(rv, p, f) \
  ((p) ? (rv += STRUCT_ALIGN(rv) + f(p)) : 0)
#define LST_XTRA(rv, l, f) \
  ((l) ? (rv += STRUCT_ALIGN(rv) + list_xtra_all((xtra_f*)f, l)) : 0)


#define STRUCT_DUP(p, dst, src) \
65 66 67 68
  ASSERT_STRUCT_ALIGN(p); assert(*(int*)(src) >= (int)sizeof(*src));	\
  ((*(int*)(src) >= (int)sizeof(*src)					\
    ? (dst = memcpy((p), (src), sizeof(*src)))				\
    : (dst = memcpy((p), (src), *(int*)(src))),				\
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72
    memset((p)+*(int*)(src), 0, sizeof(*src) - *(int*)(src))), \
  ((p) += sizeof(*src)))

#define STRUCT_DUP2(p, dst, src) \
73
  ASSERT_STRUCT_ALIGN(p); assert(*(int*)(src) >= (int)sizeof(*src));	\
Pekka Pessi's avatar
Pekka Pessi committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  (dst = memcpy((p), (src), *(int*)(src)), ((p) += *(int*)(src)))

#define STR_DUP(p, dst, src, m) \
 ((src->m) ? ((dst->m) = strcpy((p), (src->m)), (p) += strlen((p)) + 1) \
           : ((dst->m) = 0))
#define PTR_DUP(p, dst, src, m, dup) \
 ((dst->m) = (src->m)?((p += STRUCT_ALIGN(p)), ((dup)(&(p), (src->m)))): 0)
#define LST_DUP(p, dst, src, m, dup) \
 ((dst->m) = (src->m)?((p += STRUCT_ALIGN(p)),\
                       list_dup_all((dup_f*)(dup), &(p), src->m)) : 0)
#define MED_XTRA_EX(rv, l, c) \
  ((l) ? (rv += STRUCT_ALIGN(rv) + media_xtra_ex(l, c)) : 0)
#define MED_DUP_EX(p, dst, src, m, dst_c, src_c) \
 ((dst->m) = (src->m)?((p += STRUCT_ALIGN(p)),\
                       media_dup_all(&(p), src->m, dst, dst_c, src_c)) : 0)

#define MED_XTRA_ALL(rv, m) \
  ((m) ? (rv += STRUCT_ALIGN(rv) + media_xtra_all(m)) : 0)
#define MED_DUP_ALL(p, dst, src, m) \
 ((dst->m) = (src->m)?((p += STRUCT_ALIGN(p)),\
                       media_dup_all(&(p), src->m, dst)) : 0)

96
typedef size_t xtra_f(void const *);
Pekka Pessi's avatar
Pekka Pessi committed
97 98
typedef void *dup_f(char **bb, void const *src);

99
static size_t list_xtra_all(xtra_f *xtra, void const *v);
Pekka Pessi's avatar
Pekka Pessi committed
100 101
static void *list_dup_all(dup_f *dup, char **bb, void const *vsrc);

102
static size_t session_xtra(sdp_session_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
103 104
static sdp_session_t *session_dup(char **pp, sdp_session_t const *o);

105
static size_t origin_xtra(sdp_origin_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
106 107
static sdp_origin_t *origin_dup(char **pp, sdp_origin_t const *o);

108
static size_t connection_xtra(sdp_connection_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
109 110
static sdp_connection_t *connection_dup(char **pp, sdp_connection_t const *o);

111
static size_t bandwidth_xtra(sdp_bandwidth_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
112 113
static sdp_bandwidth_t *bandwidth_dup(char **pp, sdp_bandwidth_t const *o);

114
static size_t time_xtra(sdp_time_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
115 116
static sdp_time_t *time_dup(char **pp, sdp_time_t const *o);

117
static size_t repeat_xtra(sdp_repeat_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
118 119
static sdp_repeat_t *repeat_dup(char **pp, sdp_repeat_t const *o);

120
static size_t zone_xtra(sdp_zone_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
121 122
static sdp_zone_t *zone_dup(char **pp, sdp_zone_t const *o);

123
static size_t key_xtra(sdp_key_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
124 125
static sdp_key_t *key_dup(char **pp, sdp_key_t const *o);

126
static size_t attribute_xtra(sdp_attribute_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
127 128
static sdp_attribute_t *attribute_dup(char **pp, sdp_attribute_t const *o);

129
static size_t list_xtra(sdp_list_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
130 131
static sdp_list_t *list_dup(char **pp, sdp_list_t const *o);

132
static size_t rtpmap_xtra(sdp_rtpmap_t const *o); 
Pekka Pessi's avatar
Pekka Pessi committed
133 134
static sdp_rtpmap_t *rtpmap_dup(char **pp, sdp_rtpmap_t const *o);

135
static size_t media_xtra(sdp_media_t const *o);
Pekka Pessi's avatar
Pekka Pessi committed
136 137 138 139
static sdp_media_t *media_dup(char **pp,
			      sdp_media_t const *o,
			      sdp_session_t *sdp);
#ifdef nomore
140
static size_t media_xtra_ex(sdp_media_t const *o,
Pekka Pessi's avatar
Pekka Pessi committed
141 142 143 144 145 146 147
			  sdp_connection_t const *c);
static sdp_media_t *media_dup_ex(char **pp,
				  sdp_media_t const *o,
				  sdp_session_t *sdp,
				  sdp_connection_t *dst_c,
				  sdp_connection_t const *src_c);
#endif
148
static size_t media_xtra_all(sdp_media_t const *o);
Pekka Pessi's avatar
Pekka Pessi committed
149 150 151 152 153 154 155
static sdp_media_t *media_dup_all(char **pp,
				  sdp_media_t const *o,
				  sdp_session_t *sdp);


/** Define a function body duplicating an SDP structure. */
#define SDP_DUP(type, name) \
156
  sdp_##type##_t *rv; size_t size; char *p, *end; \
Pekka Pessi's avatar
Pekka Pessi committed
157 158 159 160 161 162 163 164 165
  if (!name) return NULL; \
  size = type##_xtra(name); \
  p = su_alloc(h, size); end = p + size; \
  rv = type##_dup(&p, name); \
  assert(p == end); \
  return rv;

/** Define a function body duplicating a list of SDP structures. */
#define SDP_LIST_DUP(type, name) \
166
  sdp_##type##_t *rv; size_t size; char *p, *end; \
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  if (!name) return NULL; \
  size = list_xtra_all((xtra_f*)type##_xtra, name); \
  rv = su_alloc(h, size); p = (char *)rv; end = p + size; \
  list_dup_all((dup_f*)type##_dup, &p, name); \
  assert(p == end); \
  return rv;

/**Duplicate an SDP origin description.
 *
 * The function sdp_origin_dup() duplicates (deeply copies) an SDP origin
 * description@a o allocating memory using memory @a home.
 *
 * @param h     Memory home
 * @param o     SDP origin description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_origin_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_origin_t *sdp_origin_dup(su_home_t *h, sdp_origin_t const *o)
{ 
  SDP_DUP(origin, o);
}

/**Duplicate an SDP connection description.
 *
 * The function sdp_connection_dup() duplicates (deeply copies) an SDP
 * connection description@a c allocating memory using memory @a home.
 *
 * @param h     Memory home
 * @param c     SDP connection description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_connection_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_connection_t *sdp_connection_dup(su_home_t *h, sdp_connection_t const *c)
{ 
  SDP_LIST_DUP(connection, c);
}

/**Duplicate an SDP bandwidth description.
 *
 * The function sdp_bandwidth_dup() duplicates (deeply copies) an SDP
 * bandwidth description@a b allocating memory using memory @a home.
 *
 * @param h     Memory home
 * @param b     SDP bandwidth description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_bandwidth_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_bandwidth_t *sdp_bandwidth_dup(su_home_t *h, sdp_bandwidth_t const *b)
{ 
  SDP_LIST_DUP(bandwidth, b);
}

/**Duplicate an SDP time description.
 *
 * The function sdp_time_dup() duplicates (deeply copies) an SDP time
 * description@a t allocating memory using memory @a home.
 *
 * @param h  Memory home
 * @param t  SDP time description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_time_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_time_t *sdp_time_dup(su_home_t *h, sdp_time_t const *t)
{ 
  SDP_LIST_DUP(time, t);
}

/**Duplicate an SDP repeat description.
 *
 * The function sdp_repeat_dup() duplicates (deeply copies) an SDP repeat
 * description@a r allocating memory using memory @a home.
 *
 * @param h  Memory home
 * @param r  SDP repeat description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_repeat_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_repeat_t *sdp_repeat_dup(su_home_t *h, sdp_repeat_t const *r)
{ 
  SDP_DUP(repeat, r);
}

/**Duplicate an SDP zone description.
 *
 * The function sdp_zone_dup() duplicates (deeply copies) an SDP zone
 * description@a z allocating memory using memory @a home.
 *
 * @param h  Memory home
 * @param z  SDP zone description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_zone_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_zone_t *sdp_zone_dup(su_home_t *h, sdp_zone_t const *z)
{ 
  SDP_DUP(zone, z);
}

/**Duplicate an SDP key description.
 *
 * The function sdp_key_dup() duplicates (deeply copies) an SDP key
 * description@a k allocating memory using memory @a home.
 *
 * @param h  Memory home
 * @param k  SDP key description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_key_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_key_t *sdp_key_dup(su_home_t *h, sdp_key_t const *k)
{ 
  SDP_DUP(key, k);
}

/**Duplicate an SDP attribute list.
 *
 * The function sdp_attribute_dup() duplicates (deeply copies) an SDP
 * attribute list @a a allocating memory using memory @a home.
 *
 * @param h  Memory home
 * @param a  SDP attribute description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_attribute_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_attribute_t *sdp_attribute_dup(su_home_t *h, sdp_attribute_t const *a)
{ 
  SDP_LIST_DUP(attribute, a);
}

/**Duplicate an SDP list of text.
 *
 * The function sdp_list_dup() duplicates (deeply copies) an SDP text
 * list @a l allocating memory using memory @a home.
 *
 * @param h  Memory home
 * @param l  SDP list description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_list_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_list_t *sdp_list_dup(su_home_t *h, sdp_list_t const *l)
{ 
  SDP_LIST_DUP(list, l);
}

/**Duplicate an SDP rtpmap list.
 *
 * The function sdp_rtpmap_dup() duplicates (deeply copies) an SDP rtpmap
 * list @a rm allocating memory using memory @a home.
 *
 * @param h  Memory home
 * @param rm SDP rtpmap description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_rtpmap_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_rtpmap_t *sdp_rtpmap_dup(su_home_t *h, sdp_rtpmap_t const *rm)
{ 
  SDP_LIST_DUP(rtpmap, rm);
}

/**Duplicate an SDP media description.
 *
 * The function sdp_media_dup() duplicates (deeply copies) an SDP media
 * description@a m allocating memory using memory @a home.
 *
 * @param h   Memory home
 * @param m   SDP media description to be duplicated
 * @param sdp SDP session description to which the newly allocated
 *            media description is linked
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_media_t structure is
 * returned, otherwise NULL is returned.
 */
sdp_media_t *sdp_media_dup(su_home_t *h, sdp_media_t const *m, 
			   sdp_session_t *sdp)
{
361
  sdp_media_t *rv; size_t size; char *p, *end;
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  size = media_xtra(m);
  p = su_alloc(h, size); end = p + size;
  rv = media_dup(&p, m, sdp);
  assert(p == end);
  return rv;
}

/**Duplicate an SDP media description.
 *
 * The function sdp_media_dup_all() duplicates (deeply copies) a list of SDP
 * media descriptions@a m allocating memory using memory @a home.
 *
 * @param h     Memory home
 * @param m     list of SDP media descriptions to be duplicated
 * @param sdp   SDP session description to which the newly allocated
 *              media descriptions are linked
 *
 * @return 
 * If successful, a pointer to a newly allocated list of sdp_media_t
 * structures is returned, otherwise NULL is returned.
 */
sdp_media_t *sdp_media_dup_all(su_home_t *h, sdp_media_t const *m, 
			       sdp_session_t *sdp)
{
386
  sdp_media_t *rv; size_t size; char *p, *end;
Pekka Pessi's avatar
Pekka Pessi committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  size = media_xtra_all(m);
  p = su_alloc(h, size); end = p + size;
  rv = media_dup_all(&p, m, sdp);
  assert(p == end);
  return rv;
}

#ifdef nomore			/* really deprecated */
/**Duplicate media description with common address.
 *
 * This function is provided in order to avoid duplicate @c c= lines.  If
 * the @c c= line in media description equals to @a src_c, it is not
 * duplicated but replaced with @a dst_c instead.
 *
 * @param home  Memory home
 * @param src   SDP media description to be duplicated
 * @param sdp   SDP session description to which the newly allocated
 *              media description is linked
 * @param dst_c Connection description used instead of duplicate of @a src_c.
 * @param src_c Connection description not to be duplicated

 * @return 
 * If successful, a pointer to newly allocated sdp_media_t structure is
 * returned, otherwise NULL is returned.
 *
 * @deprecated
 * This function is deprecated. Use sdp_media_dup() instead.
 */
sdp_media_t *sdp_media_dup_ex(su_home_t *home, 
			      sdp_media_t const *src,
			      sdp_session_t *sdp,
			      sdp_connection_t *dst_c,
			      sdp_connection_t const *src_c)
{
421
  sdp_media_t *rv; size_t size; char *p, *end;
Pekka Pessi's avatar
Pekka Pessi committed
422 423 424 425 426 427 428 429 430 431
  size = media_xtra_all(src, src_c);
  p = su_alloc(home, size); end = p + size;
  rv = media_dup_all(&p, src, sdp, dst_c, src_c);
  assert(p == end);
  return rv;
}
#endif

/* ---------------------------------------------------------------------- */

432
static size_t origin_xtra(sdp_origin_t const *o)
Pekka Pessi's avatar
Pekka Pessi committed
433
{
434
  size_t rv = sizeof(*o);
Pekka Pessi's avatar
Pekka Pessi committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
  STR_XTRA(rv, o->o_username);
  PTR_XTRA(rv, o->o_address, connection_xtra);
  return rv;
}

static
sdp_origin_t *origin_dup(char **pp, sdp_origin_t const *src)
{
  char *p;
  sdp_origin_t *o;

  p = *pp; 
  STRUCT_DUP(p, o, src);
  STR_DUP(p, o, src, o_username);
  PTR_DUP(p, o, src, o_address, connection_dup);

451
  assert((size_t)(p - *pp) == origin_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
452 453 454 455
  *pp = p;
  return o;
}

456
static size_t connection_xtra(sdp_connection_t const *c)
Pekka Pessi's avatar
Pekka Pessi committed
457
{
458
  size_t rv = sizeof(*c);
Pekka Pessi's avatar
Pekka Pessi committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  STR_XTRA(rv, c->c_address);
  return rv;
}

static
sdp_connection_t *connection_dup(char **pp, sdp_connection_t const *src)
{
  char *p;
  sdp_connection_t *c;

  p = *pp; 
  STRUCT_DUP(p, c, src); 
  c->c_next = NULL;
  STR_DUP(p, c, src, c_address);

474
  assert((size_t)(p - *pp) == connection_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
475 476 477 478
  *pp = p;
  return c;
}

479
static size_t bandwidth_xtra(sdp_bandwidth_t const *b)
Pekka Pessi's avatar
Pekka Pessi committed
480
{
481
  size_t rv = sizeof(*b);
Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  STR_XTRA(rv, b->b_modifier_name);
  return rv;
}

static
sdp_bandwidth_t *bandwidth_dup(char **pp, sdp_bandwidth_t const *src)
{
  char *p;
  sdp_bandwidth_t *b;

  p = *pp;
  STRUCT_DUP(p, b, src);
  b->b_next = NULL;
  STR_DUP(p, b, src, b_modifier_name);

497
  assert((size_t)(p - *pp) == bandwidth_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
498 499 500 501 502
  *pp = p;
  return b;
}


503
static size_t time_xtra(sdp_time_t const *t)
Pekka Pessi's avatar
Pekka Pessi committed
504
{
505
  size_t rv = sizeof(*t);
Pekka Pessi's avatar
Pekka Pessi committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  PTR_XTRA(rv, t->t_repeat, repeat_xtra);
  PTR_XTRA(rv, t->t_zone, zone_xtra);
  return rv;
}

static
sdp_time_t *time_dup(char **pp, sdp_time_t const *src)
{
  char *p;
  sdp_time_t *t;

  p = *pp;
  STRUCT_DUP(p, t, src);
  t->t_next = NULL;
  PTR_DUP(p, t, src, t_repeat, repeat_dup);
  PTR_DUP(p, t, src, t_zone, zone_dup);

523
  assert((size_t)(p - *pp) == time_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
524 525 526 527 528
  *pp = p;
  return t;
}


529
static size_t repeat_xtra(sdp_repeat_t const *r)
Pekka Pessi's avatar
Pekka Pessi committed
530
{
531
  return (size_t)r->r_size;
Pekka Pessi's avatar
Pekka Pessi committed
532 533 534 535 536 537 538 539 540 541 542
}

static
sdp_repeat_t *repeat_dup(char **pp, sdp_repeat_t const *src)
{
  char *p;
  sdp_repeat_t *r;

  p = *pp;
  STRUCT_DUP2(p, r, src);

543
  assert((size_t)(p - *pp) == repeat_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
544 545 546 547 548
  *pp = p;
  return r;
}


549
static size_t zone_xtra(sdp_zone_t const *z)
Pekka Pessi's avatar
Pekka Pessi committed
550 551 552 553 554 555 556 557 558 559 560 561 562
{
  return z->z_size;
}

static
sdp_zone_t *zone_dup(char **pp, sdp_zone_t const *src)
{
  char *p;
  sdp_zone_t *z;

  p = *pp;
  STRUCT_DUP2(p, z, src);

563
  assert((size_t)(p - *pp) == zone_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
564 565 566 567 568
  *pp = p;
  return z;
}


569
static size_t key_xtra(sdp_key_t const *k)
Pekka Pessi's avatar
Pekka Pessi committed
570
{
571
  size_t rv = sizeof(*k);
Pekka Pessi's avatar
Pekka Pessi committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  STR_XTRA(rv, k->k_method_name);
  STR_XTRA(rv, k->k_material);
  return rv;
}

static
sdp_key_t *key_dup(char **pp, sdp_key_t const *src)
{
  char *p;
  sdp_key_t *k;

  p = *pp;
  STRUCT_DUP(p, k, src);
  STR_DUP(p, k, src, k_method_name);
  STR_DUP(p, k, src, k_material);

588
  assert((size_t)(p - *pp) == key_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
589 590 591 592 593
  *pp = p;
  return k;
}


594
static size_t attribute_xtra(sdp_attribute_t const *a)
Pekka Pessi's avatar
Pekka Pessi committed
595
{
596
  size_t rv = sizeof(*a);
Pekka Pessi's avatar
Pekka Pessi committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
  STR_XTRA(rv, a->a_name);
  STR_XTRA(rv, a->a_value);
  return rv;
}

static
sdp_attribute_t *attribute_dup(char **pp, sdp_attribute_t const *src)
{
  char *p;
  sdp_attribute_t *a;

  p = *pp;
  STRUCT_DUP(p, a, src);
  a->a_next = NULL;
  STR_DUP(p, a, src, a_name);
  STR_DUP(p, a, src, a_value);

614
  assert((size_t)(p - *pp) == attribute_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
615 616 617 618 619
  *pp = p;
  return a;
}


620
static size_t media_xtra(sdp_media_t const *m)
Pekka Pessi's avatar
Pekka Pessi committed
621
{
622
  size_t rv = sizeof(*m);
Pekka Pessi's avatar
Pekka Pessi committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

  STR_XTRA(rv, m->m_type_name);
  STR_XTRA(rv, m->m_proto_name);
  LST_XTRA(rv, m->m_format, list_xtra);
  LST_XTRA(rv, m->m_rtpmaps, rtpmap_xtra);
  STR_XTRA(rv, m->m_information);
  LST_XTRA(rv, m->m_connections, connection_xtra);
  LST_XTRA(rv, m->m_bandwidths, bandwidth_xtra);
  PTR_XTRA(rv, m->m_key, key_xtra);
  LST_XTRA(rv, m->m_attributes, attribute_xtra);
  
  return rv;
}

static
sdp_media_t *media_dup(char **pp, 
		       sdp_media_t const *src,
		       sdp_session_t *sdp)
{
  char *p;
  sdp_media_t *m;

  p = *pp;
  STRUCT_DUP(p, m, src);
  m->m_next = NULL;

  STR_DUP(p, m, src, m_type_name);
  STR_DUP(p, m, src, m_proto_name);
  LST_DUP(p, m, src, m_format, list_dup);
  LST_DUP(p, m, src, m_rtpmaps, rtpmap_dup);
  STR_DUP(p, m, src, m_information);
  LST_DUP(p, m, src, m_connections, connection_dup);
  LST_DUP(p, m, src, m_bandwidths, bandwidth_dup);
  PTR_DUP(p, m, src, m_key, key_dup);
  LST_DUP(p, m, src, m_attributes, attribute_dup);

  /* note! we must not implicitly use 'src->m_session' as it 
           might point to a temporary session */
  m->m_session = sdp;
    
  m->m_rejected = src->m_rejected; 
  m->m_mode = src->m_mode;

666
  assert((size_t)(p - *pp) == media_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
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
  *pp = p;
  return m;
}

#ifdef nomore
static
int media_xtra_ex(sdp_media_t const *m, sdp_connection_t const *c)
{
  int rv = 0;

  for (; m; m = m->m_next) {
    rv += STRUCT_ALIGN(rv);
    rv += sizeof(*m);

    STR_XTRA(rv, m->m_type_name);
    STR_XTRA(rv, m->m_proto_name);
    LST_XTRA(rv, m->m_format, list_xtra);
    LST_XTRA(rv, m->m_rtpmaps, rtpmap_xtra);
    STR_XTRA(rv, m->m_information);
    if (c != m->m_connections)
      LST_XTRA(rv, m->m_connections, connection_xtra);
    LST_XTRA(rv, m->m_bandwidths, bandwidth_xtra);
    PTR_XTRA(rv, m->m_key, key_xtra);
    LST_XTRA(rv, m->m_attributes, attribute_xtra);
  }

  return rv;
}

static
sdp_media_t *media_dup_ex(char **pp, 
			  sdp_media_t const *src,
			  sdp_session_t *sdp,
			  sdp_connection_t *dst_c,
			  sdp_connection_t const *src_c)
{
  char *p;
  sdp_media_t *retval = NULL, *m, **mm = &retval;
  int xtra = media_xtra_ex(src, src_c);

  p = *pp;

  for (; src; src = src->m_next) {
    p += STRUCT_ALIGN(p);
    STRUCT_DUP(p, m, src);
    m->m_next = NULL;

    STR_DUP(p, m, src, m_type_name);
    STR_DUP(p, m, src, m_proto_name);
    LST_DUP(p, m, src, m_format, list_dup);
    LST_DUP(p, m, src, m_rtpmaps, rtpmap_dup);
    STR_DUP(p, m, src, m_information);
    if (src_c != src->m_connections)
      LST_DUP(p, m, src, m_connections, connection_dup);
    else
      m->m_connections = dst_c;
    LST_DUP(p, m, src, m_bandwidths, bandwidth_dup);
    PTR_DUP(p, m, src, m_key, key_dup);
    LST_DUP(p, m, src, m_attributes, attribute_dup);
    
    /* note! we must not implicitly use 'src->m_session' as it 
       might point to a temporary session */
    m->m_session = sdp;
    
    m->m_rejected = src->m_rejected; 
    m->m_mode = src->m_mode;

    assert(m);
    *mm = m; mm = &m->m_next;
  }

  assert(p - *pp == xtra);


  *pp = p;

  return retval;
}
#endif

747
static size_t media_xtra_all(sdp_media_t const *m)
Pekka Pessi's avatar
Pekka Pessi committed
748
{
749
  size_t rv = 0;
Pekka Pessi's avatar
Pekka Pessi committed
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

  for (; m; m = m->m_next) {
    rv += STRUCT_ALIGN(rv);
    rv += media_xtra(m);
  }

  return rv;
}

static
sdp_media_t *media_dup_all(char **pp, 
			   sdp_media_t const *src,
			   sdp_session_t *sdp)
{
  char *p;
  sdp_media_t *retval = NULL, *m, **mm = &retval;

  p = *pp;

  for (; src; src = src->m_next) {
    p += STRUCT_ALIGN(p);
    m = media_dup(&p, src, sdp);
    assert(m);
    *mm = m; mm = &m->m_next;
  }

  *pp = p;

  return retval;
}

781
static size_t list_xtra(sdp_list_t const *l)
Pekka Pessi's avatar
Pekka Pessi committed
782
{
783
  size_t rv = sizeof(*l);
Pekka Pessi's avatar
Pekka Pessi committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
  rv += strlen(l->l_text) + 1;
  return rv;
}

static
sdp_list_t *list_dup(char **pp, sdp_list_t const *src)
{
  char *p;
  sdp_list_t *l;

  p = *pp; 
  STRUCT_DUP(p, l, src);
  l->l_next = NULL;
  STR_DUP(p, l, src, l_text);

799
  assert((size_t)(p - *pp) == list_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
800 801 802 803
  *pp = p;
  return l;
}

804 805

static size_t rtpmap_xtra(sdp_rtpmap_t const *rm)
Pekka Pessi's avatar
Pekka Pessi committed
806
{
807
  size_t rv = sizeof(*rm);
Pekka Pessi's avatar
Pekka Pessi committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
  STR_XTRA(rv, rm->rm_encoding);
  STR_XTRA(rv, rm->rm_params);
  STR_XTRA(rv, rm->rm_fmtp);
  return rv;
}

static
sdp_rtpmap_t *rtpmap_dup(char **pp, sdp_rtpmap_t const *src)
{
  char *p;
  sdp_rtpmap_t *rm;

  p = *pp; 
  STRUCT_DUP(p, rm, src);
  rm->rm_next = NULL;
  STR_DUP(p, rm, src, rm_encoding);
  STR_DUP(p, rm, src, rm_params);
  STR_DUP(p, rm, src, rm_fmtp);

827
  assert((size_t)(p - *pp) == rtpmap_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
828 829 830 831 832
  *pp = p;
  return rm;
}

/** Return total size of a list, including size of all nodes */
833
static size_t list_xtra_all(xtra_f *xtra, void const *v)
Pekka Pessi's avatar
Pekka Pessi committed
834
{
835
  size_t rv = 0;
Pekka Pessi's avatar
Pekka Pessi committed
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
  sdp_list_t const *l;

  for (l = v; l; l = l->l_next) {
    rv += STRUCT_ALIGN(rv);
    rv += xtra(l);
  }

  return rv;
}

static
void *list_dup_all(dup_f *dup, char **pp, void const *vsrc)
{
  char *p;
  sdp_list_t const *src;
  sdp_list_t *retval = NULL, *l, **ll = &retval;

  p = *pp;

  for (src = vsrc; src; src = src->l_next) {
    p += STRUCT_ALIGN(p);
    l = dup(&p, src);
    assert(l);
    *ll = l; ll = &l->l_next;
  }

  *pp = p;

  return retval;
}

#if 0
868
static size_t XXX_xtra(sdp_XXX_t const *YYY)
Pekka Pessi's avatar
Pekka Pessi committed
869
{
870
  size_t rv = sizeof(*YYY);
Pekka Pessi's avatar
Pekka Pessi committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
  rv += strlen(YYY->YYY_encoding) + 1;
  if (YYY->YYY_params);
    rv += strlen(YYY->YYY_params) + 1;
  return rv;
}

static
sdp_XXX_t *XXX_dup(char **pp, sdp_XXX_t const *src)
{
  char *p;
  sdp_XXX_t *YYY;

  p = *pp; ASSERT_STRUCT_ALIGN(p);
  YYY = memcpy(p, src, src->YYY_size);
  p += src->YYY_size;
  YYY->YYY_next = NULL;
  ZZZ
  *pp = p;
  return YYY;
}

#endif

894
static size_t session_xtra(sdp_session_t const *sdp)
Pekka Pessi's avatar
Pekka Pessi committed
895
{
896
  size_t rv = sizeof(*sdp);
Pekka Pessi's avatar
Pekka Pessi committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

  PTR_XTRA(rv, sdp->sdp_origin, origin_xtra);
  STR_XTRA(rv, sdp->sdp_subject);
  STR_XTRA(rv, sdp->sdp_information);
  STR_XTRA(rv, sdp->sdp_uri);
  LST_XTRA(rv, sdp->sdp_emails, list_xtra);
  LST_XTRA(rv, sdp->sdp_phones, list_xtra);
  LST_XTRA(rv, sdp->sdp_connection, connection_xtra);
  LST_XTRA(rv, sdp->sdp_bandwidths, bandwidth_xtra);
  LST_XTRA(rv, sdp->sdp_time, time_xtra);
  PTR_XTRA(rv, sdp->sdp_key, key_xtra);
  LST_XTRA(rv, sdp->sdp_attributes, attribute_xtra);
  STR_XTRA(rv, sdp->sdp_charset);
  MED_XTRA_ALL(rv, sdp->sdp_media);

  return rv;
}

static
sdp_session_t *session_dup(char **pp, sdp_session_t const *src)
{
  char *p;
  sdp_session_t *sdp;

  p = *pp; 
  STRUCT_DUP(p, sdp, src);
  sdp->sdp_next = NULL;

  PTR_DUP(p, sdp, src, sdp_origin, origin_dup);
  STR_DUP(p, sdp, src, sdp_subject);
  STR_DUP(p, sdp, src, sdp_information);
  STR_DUP(p, sdp, src, sdp_uri);
  LST_DUP(p, sdp, src, sdp_emails, list_dup);
  LST_DUP(p, sdp, src, sdp_phones, list_dup);
  LST_DUP(p, sdp, src, sdp_connection, connection_dup);
  LST_DUP(p, sdp, src, sdp_bandwidths, bandwidth_dup);
  LST_DUP(p, sdp, src, sdp_time, time_dup);
  PTR_DUP(p, sdp, src, sdp_key, key_dup);
  LST_DUP(p, sdp, src, sdp_attributes, attribute_dup);
  STR_DUP(p, sdp, src, sdp_charset);
  MED_DUP_ALL(p, sdp, src, sdp_media);

939
  assert((size_t)(p - *pp) == session_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
  *pp = p;
  return sdp;
}

/**Duplicate an SDP session description.
 *
 * The function sdp_session_dup() duplicates (deeply copies) an SDP
 * session description@a sdp allocating memory using memory @a home.
 *
 * @param h   Memory home
 * @param sdp SDP session description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_session_t structure is
 * returned, otherwise NULL is returned.
 */

sdp_session_t *sdp_session_dup(su_home_t *h, sdp_session_t const *sdp)
{ 
  SDP_DUP(session, sdp);
}

/* ---------------------------------------------------------------------- */

964
static size_t session_without_media_xtra(sdp_session_t const *sdp)
Pekka Pessi's avatar
Pekka Pessi committed
965
{
966
  size_t rv = sizeof(*sdp);
Pekka Pessi's avatar
Pekka Pessi committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

  PTR_XTRA(rv, sdp->sdp_origin, origin_xtra);
  STR_XTRA(rv, sdp->sdp_subject);
  STR_XTRA(rv, sdp->sdp_information);
  STR_XTRA(rv, sdp->sdp_uri);
  LST_XTRA(rv, sdp->sdp_emails, list_xtra);
  LST_XTRA(rv, sdp->sdp_phones, list_xtra);
  LST_XTRA(rv, sdp->sdp_connection, connection_xtra);
  LST_XTRA(rv, sdp->sdp_bandwidths, bandwidth_xtra);
  LST_XTRA(rv, sdp->sdp_time, time_xtra);
  PTR_XTRA(rv, sdp->sdp_key, key_xtra);
  LST_XTRA(rv, sdp->sdp_attributes, attribute_xtra);
  STR_XTRA(rv, sdp->sdp_charset);

  return rv;
}

static
sdp_session_t *session_without_media_dup(char **pp, sdp_session_t const *src)
{
  char *p;
  sdp_session_t *sdp;

  p = *pp; 
  STRUCT_DUP(p, sdp, src);
  sdp->sdp_next = NULL;

  PTR_DUP(p, sdp, src, sdp_origin, origin_dup);
  STR_DUP(p, sdp, src, sdp_subject);
  STR_DUP(p, sdp, src, sdp_information);
  STR_DUP(p, sdp, src, sdp_uri);
  LST_DUP(p, sdp, src, sdp_emails, list_dup);
  LST_DUP(p, sdp, src, sdp_phones, list_dup);
  LST_DUP(p, sdp, src, sdp_connection, connection_dup);
  LST_DUP(p, sdp, src, sdp_bandwidths, bandwidth_dup);
  LST_DUP(p, sdp, src, sdp_time, time_dup);
  PTR_DUP(p, sdp, src, sdp_key, key_dup);
  LST_DUP(p, sdp, src, sdp_attributes, attribute_dup);
  STR_DUP(p, sdp, src, sdp_charset);

  sdp->sdp_media = NULL;
  
1009
  assert((size_t)(p - *pp) == session_without_media_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  *pp = p;
  return sdp;
}

/* SDP_DUP macro requires this */
typedef sdp_session_t sdp_session_without_media_t;

/**Duplicate an SDP session description without media descriptions.
 *
 * The function sdp_session_dup() duplicates (deeply copies) an SDP session
 * description@a sdp allocating memory using memory @a home. It does not
 * copy the media descriptions, however.
 *
 * @param h     memory h
 * @param sdp   SDP session description to be duplicated
 *
 * @return 
 * If successful, a pointer to newly allocated sdp_session_t structure is
 * returned, otherwise NULL is returned.
 */

sdp_session_t *sdp_session_dup_without_media(su_home_t *h, 
					     sdp_session_t const *sdp)
{ 
  SDP_DUP(session_without_media, sdp);
}

/* ---------------------------------------------------------------------- */
/* SDP Tag classes */

1040
#include <sofia-sip/su_tag_class.h>
Pekka Pessi's avatar
Pekka Pessi committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

size_t sdptag_session_xtra(tagi_t const *t, size_t offset)
{
  sdp_session_t const *sdp = (sdp_session_t *)t->t_value;

  if (sdp) 
    return STRUCT_ALIGN(offset) + session_xtra(sdp);
  else
    return 0;
}

tagi_t *sdptag_session_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  sdp_session_t *sdp;
  sdp_session_t const *srcsdp;
  char *b;

  assert(src); assert(*bb); 

  b = *bb;
  b += STRUCT_ALIGN(b);
  srcsdp = (sdp_session_t *)src->t_value;

1064
  sdp = srcsdp ? session_dup(&b, srcsdp) : NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

  dst->t_tag = src->t_tag;
  dst->t_value = (tag_value_t)sdp;

  *bb = b;

  return dst + 1;
}

int sdptag_session_snprintf(tagi_t const *t, char b[], size_t size)
{
  sdp_session_t const *sdp;
  sdp_printer_t *print;
1078
  size_t retval;
Pekka Pessi's avatar
Pekka Pessi committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094

  assert(t);

  if (!t || !t->t_value) { 
    if (size && b) b[0] = 0; 
    return 0; 
  }

  sdp = (sdp_session_t const *)t->t_value;

  print = sdp_print(NULL, sdp, b, size, 0);

  retval = sdp_message_size(print);
  
  sdp_printer_free(print);

1095
  return (int)retval;
Pekka Pessi's avatar
Pekka Pessi committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
}

/** Tag class for SDP tags. @HIDE */
tag_class_t sdptag_session_class[1] = 
  {{
    sizeof(sdptag_session_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     sdptag_session_xtra,
    /* tc_dup */      sdptag_session_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ sdptag_session_snprintf,
    /* tc_filter */   NULL /* msgtag_str_filter */,
    /* tc_ref_set */  t_ptr_ref_set,
  }};


/* ---------------------------------------------------------------------- */
1116 1117

/* Compare two string pointers */
1118
su_inline 
1119 1120 1121 1122 1123 1124 1125
int str0cmp(char const *a, char const *b)
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strcmp(a, b);
}

1126
/* Compare two string pointers ignoring case. */
1127
su_inline 
1128 1129 1130 1131 1132 1133 1134
int str0casecmp(char const *a, char const *b)
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strcasecmp(a, b);
}

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
/** Compare two session descriptions 
 */
int sdp_session_cmp(sdp_session_t const *a, sdp_session_t const *b)
{
  int rv; 
  sdp_bandwidth_t const *ab, *bb;
  sdp_attribute_t const *aa, *ba;
  sdp_media_t const *am, *bm;
 
  if ((rv = (a != NULL) - (b != NULL))) 
    return rv;
  if (a == b)
    return 0;
  if ((rv = (a->sdp_version[0] - b->sdp_version[0])))
    return rv;
  if ((rv = sdp_origin_cmp(a->sdp_origin, b->sdp_origin)))
    return rv;
  if ((rv = str0cmp(a->sdp_subject, b->sdp_subject)))
    return rv;
  if ((rv = str0cmp(a->sdp_information, b->sdp_information)))
    return rv;
  if ((rv = str0cmp(a->sdp_uri, b->sdp_uri)))
    return rv;
  if ((rv = sdp_list_cmp(a->sdp_emails, b->sdp_emails)))
    return rv;
  if ((rv = sdp_list_cmp(a->sdp_phones, b->sdp_phones)))
    return rv;
  if ((rv = sdp_connection_cmp(a->sdp_connection, b->sdp_connection)))
    return rv;

  for (ab = a->sdp_bandwidths, bb = b->sdp_bandwidths; 
       ab || bb; 
       ab = ab->b_next, bb = bb->b_next)
    if ((rv = sdp_bandwidth_cmp(a->sdp_bandwidths, b->sdp_bandwidths)))
      return rv;

  if ((rv = sdp_time_cmp(a->sdp_time, b->sdp_time)))
    return rv;
  if ((rv = sdp_key_cmp(a->sdp_key, b->sdp_key)))
    return rv;

  for (aa = a->sdp_attributes, ba = b->sdp_attributes; 
       aa || bb; 
       aa = aa->a_next, ba = ba->a_next)
    if ((rv = sdp_attribute_cmp(aa, ba)))
      return rv;

  for (am = a->sdp_media, bm = b->sdp_media; 
1183
       am || bm; 
1184 1185 1186 1187 1188 1189 1190
       am = am->m_next, bm = bm->m_next)
    if ((rv = sdp_media_cmp(am, bm)))
      return rv;

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
1191 1192 1193 1194
/** Compare two origin fields 
 */
int sdp_origin_cmp(sdp_origin_t const *a, sdp_origin_t const *b)
{
1195
  int rv;
Pekka Pessi's avatar
Pekka Pessi committed
1196 1197 1198 1199 1200

  if ((rv = (a != NULL) - (b != NULL))) 
    return rv;
  if (a == b)
    return 0;
1201 1202 1203 1204
  if (a->o_version != b->o_version)
    return a->o_version < b->o_version ? -1 : 1;
  if (a->o_id != b->o_id)
    return a->o_id < b->o_id ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1205 1206 1207 1208 1209 1210 1211 1212
  if ((rv = strcasecmp(a->o_username, b->o_username)))
    return rv;
  if ((rv = strcasecmp(a->o_address->c_address, b->o_address->c_address)))
    return rv;

  return 0;
}

1213 1214 1215 1216 1217 1218
/** Compare two connection fields 
 */
int sdp_connection_cmp(sdp_connection_t const *a, sdp_connection_t const *b)
{
  if (a == b)
    return 0;
1219 1220
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;
1221

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
  if (a->c_nettype != b->c_nettype)
    return a->c_nettype < b->c_nettype ? -1 : 1;
  if (a->c_addrtype != b->c_addrtype)
    return a->c_addrtype < b->c_addrtype ? -1 : 1;
  if (a->c_ttl != b->c_ttl)
    return a->c_ttl < b->c_ttl ? -1 : 1;
  if (a->c_groups != b->c_groups)
    return a->c_groups < b->c_groups ? -1 : 1;

  return strcmp(a->c_address, b->c_address);
1232 1233 1234 1235 1236
}

/** Compare two bandwidth (b=) fields */
int sdp_bandwidth_cmp(sdp_bandwidth_t const *a, sdp_bandwidth_t const *b)
{
1237
  int rv;
1238 1239 1240

  if (a == b)
    return 0;
1241 1242 1243 1244 1245
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;

  if (a->b_modifier != b->b_modifier)
    return a->b_modifier < b->b_modifier ? -1 : 1;
1246
  if (a->b_modifier == sdp_bw_x && 
1247
      (rv = strcmp(a->b_modifier_name, b->b_modifier_name)))
1248
    return rv;
1249 1250 1251 1252 1253

  if (a->b_value != b->b_value)
    return a->b_value < b->b_value ? -1 : 1;

  return 0;
1254 1255
}

Pekka Pessi's avatar
Pekka Pessi committed
1256 1257 1258 1259 1260 1261 1262 1263 1264
/** Compare two time fields */
int sdp_time_cmp(sdp_time_t const *a, sdp_time_t const *b)
{
  int rv; 

  if ((rv = (a != NULL) - (b != NULL))) 
    return rv;
  if (a == b)
    return 0;
1265 1266 1267 1268
  if (a->t_start != b->t_start)
    return a->t_start < b->t_start ? -1 : 1;
  if (a->t_stop != b->t_stop)
    return a->t_stop < b->t_stop ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  if ((rv = sdp_zone_cmp(a->t_zone, b->t_zone)))
    return rv;
  if ((rv = sdp_repeat_cmp(a->t_repeat, b->t_repeat)))
    return rv;
  return 0;
}

/** Compare two repeat (r=) fields */
int sdp_repeat_cmp(sdp_repeat_t const *a, sdp_repeat_t const *b)
{
1279
  int i, n;
Pekka Pessi's avatar
Pekka Pessi committed
1280 1281 1282
  
  if (a == b)
    return 0;
1283 1284 1285 1286 1287 1288 1289
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;

  if (a->r_interval != b->r_interval)
    return a->r_interval < b->r_interval ? -1 : 1;
  if (a->r_duration != b->r_duration)
    return a->r_duration < b->r_duration ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1290 1291 1292
  n = a->r_number_of_offsets < b->r_number_of_offsets 
    ? a->r_number_of_offsets : b->r_number_of_offsets;
  for (i = 0; i < n; i++)
1293 1294
    if (a->r_offsets[i] != b->r_offsets[i])
      return a->r_offsets[i] < b->r_offsets[i] ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1295
  
1296 1297
  if (a->r_number_of_offsets != b->r_number_of_offsets)
    return a->r_number_of_offsets < b->r_number_of_offsets ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1298 1299 1300 1301 1302 1303 1304

  return 0;
 }

/** Compare two zone (z=) fields */
int sdp_zone_cmp(sdp_zone_t const *a, sdp_zone_t const *b)
{
1305
  int i, n;
Pekka Pessi's avatar
Pekka Pessi committed
1306 1307 1308
  
  if (a == b)
    return 0;
1309 1310
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1311 1312 1313 1314

  n = a->z_number_of_adjustments < b->z_number_of_adjustments
    ? a->z_number_of_adjustments : b->z_number_of_adjustments;
  for (i = 0; i < n; i++) {
1315 1316 1317 1318 1319
    if (a->z_adjustments[i].z_at != b->z_adjustments[i].z_at)
      return a->z_adjustments[i].z_at < b->z_adjustments[i].z_at ? -1 : 1;
    if (a->z_adjustments[i].z_offset != b->z_adjustments[i].z_offset)
      return a->z_adjustments[i].z_offset < b->z_adjustments[i].z_offset
	? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1320 1321
  }

1322 1323
  if (a->z_number_of_adjustments != b->z_number_of_adjustments)
    return a->z_number_of_adjustments < b->z_number_of_adjustments ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1324 1325 1326 1327

  return 0;
}

1328 1329 1330
/** Compare two key (k=) fields */
int sdp_key_cmp(sdp_key_t const *a, sdp_key_t const *b)
{
1331
  int rv;
1332 1333 1334

  if (a == b)
    return 0;
1335 1336 1337 1338 1339
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;

  if (a->k_method != b->k_method)
    return a->k_method < b->k_method ? -1 : 1;
1340 1341 1342 1343 1344 1345 1346 1347 1348
  if (a->k_method == sdp_key_x && 
      (rv = str0cmp(a->k_method_name, b->k_method_name)))
    return rv;
  return str0cmp(a->k_material, b->k_material);
}

/** Compare two attribute (a=) fields */
int sdp_attribute_cmp(sdp_attribute_t const *a, sdp_attribute_t const *b)
{
1349
  int rv;
1350 1351 1352

  if (a == b)
    return 0;
1353 1354 1355
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;

1356 1357 1358 1359 1360
  if ((rv = str0cmp(a->a_name, b->a_name)))
    return rv;
  return str0cmp(a->a_value, b->a_value);
}

1361 1362 1363 1364 1365 1366 1367
/** Compare two rtpmap structures. */
int sdp_rtpmap_cmp(sdp_rtpmap_t const *a, sdp_rtpmap_t const *b)
{
  int rv;

  if (a == b)
    return 0;
1368 1369 1370 1371 1372
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;

  if (a->rm_pt != b->rm_pt)
    return a->rm_pt < b->rm_pt ? -1 : 1;
1373 1374 1375 1376 1377

  /* Case insensitive encoding */
  if ((rv = str0cmp(a->rm_encoding, b->rm_encoding)))
    return rv;
  /* Rate */
1378 1379
  if (a->rm_rate != b->rm_rate)
    return a->rm_rate < b->rm_rate ? -1 : 1;
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

  {
    char const *a_param = "1", *b_param = "1";

    if (a->rm_params)
      a_param = a->rm_params;
    if (b->rm_params)
      b_param = b->rm_params;
    
    rv = strcasecmp(a_param, b_param);

    if (rv)
      return rv;
  }

1395
  return str0casecmp(a->rm_fmtp, b->rm_fmtp);
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
}

/** Compare two lists. */
int sdp_list_cmp(sdp_list_t const *a, sdp_list_t const *b)
{
  int rv;

  for (;a || b; a = a->l_next, b = b->l_next) {
    if (a == b)
      return 0;
1406 1407
    if ((a != NULL) != (b != NULL))
      return (a != NULL) < (b != NULL) ? -1 : 1;
1408 1409 1410 1411 1412 1413 1414
    if ((rv = str0cmp(a->l_text, b->l_text)))
      return rv;
  }

  return 0;
}

1415 1416 1417 1418 1419 1420 1421
/** Compare two media (m=) fields */
int sdp_media_cmp(sdp_media_t const *a, sdp_media_t const *b)
{
  int rv; 

  sdp_connection_t const *ac, *bc;
  sdp_bandwidth_t const *ab, *bb;
1422
  sdp_rtpmap_t const *arm, *brm;
1423 1424 1425 1426
  sdp_attribute_t const *aa, *ba;

  if (a == b)
    return 0;
1427
  if ((rv = (a != NULL) - (b != NULL))) 
1428
    return rv;
1429 1430 1431

  if (a->m_type != b->m_type)
    return a->m_type < b->m_type ? -1 : 1;
1432 1433 1434
  if (a->m_type == sdp_media_x)
    if ((rv = str0cmp(a->m_type_name, b->m_type_name)))
      return rv;
1435 1436
  if (a->m_port != b->m_port)
    return a->m_port < b->m_port ? -1 : 1;
1437 1438 1439 1440 1441

  if (a->m_port == 0 /* && b->m_port == 0 */)
    /* Ignore transport protocol and media list if media has been rejected */
    return 0;

1442 1443 1444 1445 1446
  if (a->m_number_of_ports != b->m_number_of_ports)
    return a->m_number_of_ports < b->m_number_of_ports ? -1 : 1;

  if (a->m_proto != b->m_proto)
    return a->m_proto < b->m_proto ? -1 : 1;
1447 1448 1449
  if (a->m_proto == sdp_media_x)
    if ((rv = str0cmp(a->m_proto_name, b->m_proto_name)))
      return rv;
1450

1451 1452
  if (a->m_mode != b->m_mode)
    return a->m_mode < b->m_mode ? -1 : 1;
1453

1454 1455 1456 1457 1458 1459 1460 1461
  for (arm = a->m_rtpmaps, brm = b->m_rtpmaps; 
       arm || brm; 
       arm = arm->rm_next, brm = brm->rm_next)
    if ((rv = sdp_rtpmap_cmp(arm, brm)))
      return rv;

  if ((rv = sdp_list_cmp(a->m_format, b->m_format)))
    return rv;
1462

Pekka Pessi's avatar
Pekka Pessi committed
1463 1464 1465
  if ((rv = str0cmp(a->m_information, b->m_information)))
    return rv;

1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
  for (ac = a->m_connections, bc = b->m_connections; 
       ac || bc; 
       ac = ac->c_next, bc = bc->c_next)
  if ((rv = sdp_connection_cmp(ac, bc)))
    return rv;

  for (ab = a->m_bandwidths, bb = b->m_bandwidths; 
       ab || bb; 
       ab = ab->b_next, bb = bb->b_next)
    if ((rv = sdp_bandwidth_cmp(a->m_bandwidths, b->m_bandwidths)))
      return rv;

  if ((rv = sdp_key_cmp(a->m_key, b->m_key)))
    return rv;

  for (aa = a->m_attributes, ba = b->m_attributes; 
       aa || bb; 
       aa = aa->a_next, ba = ba->a_next)
    if ((rv = sdp_attribute_cmp(aa, ba)))
      return rv;

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
1490 1491
/* ---------------------------------------------------------------------- */

1492
sdp_connection_t *sdp_media_connections(sdp_media_t const *m)
Pekka Pessi's avatar
Pekka Pessi committed
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
{
  if (m) {
    if (m->m_connections)
      return m->m_connections;
    if (m->m_session)
      return m->m_session->sdp_connection;
  }
  return NULL;
}

/* ---------------------------------------------------------------------- */

/** Find named attribute from given list. */
1506
sdp_attribute_t *sdp_attribute_find(sdp_attribute_t const *a, char const *name)
Pekka Pessi's avatar
Pekka Pessi committed
1507 1508 1509 1510 1511 1512
{
  for (; a; a = a->a_next) {
    if (strcasecmp(a->a_name, name) == 0)
      break;
  }

1513
  return (sdp_attribute_t *)a;
Pekka Pessi's avatar
Pekka Pessi committed
1514 1515 1516
}

/** Find named attribute from given lists (a or a2). */
1517 1518
sdp_attribute_t *sdp_attribute_find2(sdp_attribute_t const *a, 
				     sdp_attribute_t const *a2, 
Pekka Pessi's avatar
Pekka Pessi committed
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
				     char const *name)
{
  for (; a; a = a->a_next) {
    if (strcasecmp(a->a_name, name) == 0)
      break;
  }

  if (a == 0)
    for (a = a2; a; a = a->a_next) {
      if (strcasecmp(a->a_name, name) == 0)
	break;
    }

1532
  return (sdp_attribute_t *)a;
Pekka Pessi's avatar
Pekka Pessi committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
}

/** Get session mode from attribute list. */
sdp_mode_t sdp_attribute_mode(sdp_attribute_t const *a, sdp_mode_t defmode)
{
  for (; a; a = a->a_next) {
    if (strcasecmp(a->a_name, "sendrecv") == 0)
      return sdp_sendrecv;
    if (strcasecmp(a->a_name, "inactive") == 0)
      return sdp_inactive;
    if (strcasecmp(a->a_name, "recvonly") == 0)
      return sdp_recvonly;
    if (strcasecmp(a->a_name, "sendonly") == 0)
      return sdp_sendonly;
  }

  return defmode;
}

1552
/** Convert session mode as #sdp_attribute_t structure. */
Pekka Pessi's avatar
Pekka Pessi committed
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
sdp_attribute_t *sdp_attribute_by_mode(su_home_t *home, sdp_mode_t mode)
{
  sdp_attribute_t *a;
  char const *name;

  if (mode == sdp_inactive)
    name = "inactive";
  else if (mode == sdp_sendonly)
    name = "sendonly";
  else if (mode == sdp_recvonly)
    name = "recvonly";
  else if (mode == sdp_sendrecv)
    name = "sendrecv";
  else
    return NULL;
  
  a = su_salloc(home, sizeof(*a));
  if (a)
    a->a_name = name;

  return a;
}

/** Find a mapped attribute. 
 *
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
 * A mapped attribute has form 'a=<name>:<pt> <value>' where pt is a RTP
 * payload type, integer in range 0..127. For example, "a=atmmap" [@RFC3108]
 * is a mapped attribute. Note that common mapped attributes, "a=rtpmap" and
 * "a=fmtp" are already parsed as list of #sdp_rtpmap_t in #sdp_media_t.
 *
 * @param a pointer to first attribute in the list
 * @param name name of the attribute
 * @param pt payload type number (must be 0..127)
 * @param return_result return value parameter for mapped attribute value
 *
 * @return Pointer to a matching attribute structure, or NULL. 
Pekka Pessi's avatar
Pekka Pessi committed
1589
 *
1590 1591
 * If a matching attribute is found, @a return_result will point to part of
 * the attribute after the payload type and whitespace.
Pekka Pessi's avatar
Pekka Pessi committed
1592
 */
1593
sdp_attribute_t *sdp_attribute_mapped_find(sdp_attribute_t const *a,
Pekka Pessi's avatar
Pekka Pessi committed
1594
					   char const *name,
1595
					   int pt, char **return_result)
Pekka Pessi's avatar
Pekka Pessi committed
1596
{
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
  char pt_value[4];
  size_t pt_len;

  if (return_result)
    *return_result = NULL;

  if (0 > pt || pt > 127)
    return NULL;

  snprintf(pt_value, sizeof(pt_value), "%u", (unsigned)pt);
  pt_len = strlen(pt_value);
Pekka Pessi's avatar
Pekka Pessi committed
1608 1609

  for (; (a = sdp_attribute_find(a, name)); a = a->a_next) {
1610 1611
    char const *value = a->a_value;
    size_t wlen;
Pekka Pessi's avatar
Pekka Pessi committed
1612

1613
    if (strncmp(value, pt_value, pt_len))
Pekka Pessi's avatar
Pekka Pessi committed
1614 1615
      continue;

1616
    wlen = strspn(value + pt_len, " \t");
Pekka Pessi's avatar
Pekka Pessi committed
1617

1618
    if (wlen == 0 || value[pt_len + wlen] == '\0')
Pekka Pessi's avatar
Pekka Pessi committed
1619 1620
      continue;

1621 1622
    if (return_result)
      *return_result = (char *)value + pt_len + wlen;
Pekka Pessi's avatar
Pekka Pessi committed
1623

1624
    return (sdp_attribute_t *)a;
Pekka Pessi's avatar
Pekka Pessi committed
1625 1626 1627 1628
  }

  return NULL;
}
1629

1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 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 1706 1707 1708 1709 1710 1711 1712
/** Append a (list of) attribute(s) to a list of attributes. */
void sdp_attribute_append(sdp_attribute_t **list, 
			  sdp_attribute_t const *a)
{
  assert(list);

  if (list == NULL || a == NULL)
    return;

  for (;*list; list = &(*list)->a_next)
    ;

  *list = (sdp_attribute_t *)a;
}

/**Replace or append a attribute within a list of attributes. 
 *
 * @retval 1 if replaced existing attribute
 * @retval 0 if attribute was appended
 * @retval -1 upon an error
 */
int sdp_attribute_replace(sdp_attribute_t **list, 
			  sdp_attribute_t *a,
			  sdp_attribute_t **return_replaced)
{
  sdp_attribute_t *replaced;

  assert(list);

  if (return_replaced)
    *return_replaced = NULL;

  if (list == NULL || a == NULL)
    return -1;

  assert(a->a_name != NULL); assert(a->a_next == NULL);

  for (; *list; list = &(*list)->a_next) {
    if (strcasecmp((*list)->a_name, a->a_name) == 0)
      break;
  }

  replaced = *list, *list = a;
  
  if (replaced) {
    a->a_next = replaced->a_next;
    replaced->a_next = NULL;

    if (return_replaced)
      *return_replaced = replaced;

    return 1;
  }

  return 0;
}

/** Remove a named attribute from a list of attributes. */
sdp_attribute_t *sdp_attribute_remove(sdp_attribute_t **list, 
				      char const *name)
{
  sdp_attribute_t *a;

  assert(list);

  if (list == NULL)
    return NULL;
  if (name == NULL)
    return NULL;

  for (a = *list; a; list = &a->a_next, a = *list) {
    if (strcasecmp(name, a->a_name) == 0)
      break;
  }

  if (a) {
    *list = a->a_next;
    a->a_next = NULL;
  }

  return a;
}

1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
/* Return 1 if m= line struct matches with given type and name */
unsigned sdp_media_match(sdp_media_t const *m,
			 sdp_media_e type,
			 sdp_text_t *type_name,
			 sdp_proto_e proto,
			 sdp_text_t *proto_name)
{
  if (m == NULL)
    return 0;

  if (type == sdp_media_any || m->m_type == sdp_media_any)
    return 1;

  if (type_name == NULL)
    type_name = "";

  if (type != m->m_type ||
      (type == sdp_media_x && strcasecmp(m->m_type_name, type_name)))
    return 0;

  if (proto == sdp_proto_any || m->m_proto == sdp_proto_any)
    return 1;

  if (proto_name == NULL)
    proto_name = "";

  if (proto != m->m_proto ||
      (proto == sdp_proto_x && strcasecmp(m->m_proto_name, proto_name)))
    return 0;

  return 1;
}

/* Return 1 if media type and protocol of m= line structs matches */
unsigned sdp_media_match_with(sdp_media_t const *a,
			      sdp_media_t const *b)
{
  if (a == NULL || b == NULL)
    return a == b;

  if (a->m_type == sdp_media_any || b->m_type == sdp_media_any)
    return 1;

  if (a->m_type != b->m_type ||
      (a->m_type == sdp_media_x &&
       strcasecmp(b->m_type_name, a->m_type_name)))
    return 0;

  if (a->m_proto == sdp_proto_any || b->m_proto == sdp_proto_any)
    return 1;

  if (a->m_proto != b->m_proto ||
      (a->m_proto == sdp_proto_x &&
       strcasecmp(b->m_proto_name, a->m_proto_name)))
    return 0;

  return 1;
}


/** Count matching media lines in SDP. */
unsigned sdp_media_count(sdp_session_t const *sdp,
			 sdp_media_e type,
			 sdp_text_t *type_name,
			 sdp_proto_e proto,
			 sdp_text_t *proto_name)
{
  unsigned count = 0;
  sdp_media_t const *m;

  if (sdp != NULL)
    for (m = sdp->sdp_media; m; m = m->m_next)
      count += sdp_media_match(m, type, type_name, proto, proto_name);

  return count;
}

/** Count matching media lines in SDP. */
unsigned sdp_media_count_with(sdp_session_t const *sdp,
			      sdp_media_t const *m0)
{
  unsigned count = 0;
  sdp_media_t const *m;

  if (sdp != NULL)
    for (m = sdp->sdp_media; m; m = m->m_next)
      count += sdp_media_match_with(m, m0);

  return count;
}
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860

/** Return true if media uses RTP */
int sdp_media_uses_rtp(sdp_media_t const *m)
{
  return m &&
    (m->m_proto == sdp_proto_rtp ||
     m->m_proto == sdp_proto_srtp ||
     (m->m_proto == sdp_proto_x && m->m_proto_name &&
      strncasecmp(m->m_proto_name, "RTP/", 4) == 0));
}

/** Check if payload type, rtp rate and parameters match in rtpmaps*/
int sdp_rtpmap_match(sdp_rtpmap_t const *a, sdp_rtpmap_t const *b)
{
  char const *aparam, *bparam;

  if (a == b)
    return 1;

  if (a == 0 || b == 0)
    return 0;

  if (a->rm_rate != b->rm_rate)
    return 0;

  if (strcasecmp(a->rm_encoding, b->rm_encoding))
    return 0;

  aparam = a->rm_params; bparam = b->rm_params;

  if (aparam == bparam)
    return 1;

  if (!aparam) aparam = "1"; if (!bparam) bparam = "1";
  
  if (strcasecmp(aparam, bparam))
    return 0;

  return 1;
}

/** Search for matching rtpmap from list.
 *
 * @note
 * The a=fmtp: for the codecs are not compared.
 */
sdp_rtpmap_t *sdp_rtpmap_find_matching(sdp_rtpmap_t const *list,
				       sdp_rtpmap_t const *rm)
{
  char const *lparam, *rparam;

  if (rm == NULL)
    return NULL;

  for (; list; list = list->rm_next) {
    if (rm->rm_rate != list->rm_rate)
      continue;

1861
    if (strcasecmp(rm->rm_encoding, list->rm_encoding) != 0)
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
      continue;

    lparam = rm->rm_params; rparam = list->rm_params;

    if (lparam == rparam)
      break;

    if (!lparam) lparam = "1"; if (!rparam) rparam = "1";
    if (strcasecmp(lparam, rparam))
      continue;

    break;
  }

  return (sdp_rtpmap_t *)list;
}