sdp.c 47.7 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
  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
Pekka Pessi's avatar
Pekka Pessi committed
177
 * description @a o allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
178 179 180 181
 *
 * @param h     Memory home
 * @param o     SDP origin description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
182 183 184
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
185
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
186 187 188 189
 * 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)
190
{
Pekka Pessi's avatar
Pekka Pessi committed
191 192 193 194 195
  SDP_DUP(origin, o);
}

/**Duplicate an SDP connection description.
 *
Pekka Pessi's avatar
Pekka Pessi committed
196 197
 * The function sdp_connection_dup() duplicates (deeply copies) a list of
 * SDP connection description @a c allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
198 199 200 201
 *
 * @param h     Memory home
 * @param c     SDP connection description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
202 203 204
 * @note The duplicated list is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
205
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
206 207 208 209
 * 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)
210
{
Pekka Pessi's avatar
Pekka Pessi committed
211 212 213 214 215
  SDP_LIST_DUP(connection, c);
}

/**Duplicate an SDP bandwidth description.
 *
Pekka Pessi's avatar
Pekka Pessi committed
216 217
 * The function sdp_bandwidth_dup() duplicates (deeply copies) a list of SDP
 * bandwidth descriptions @a b allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
218 219 220 221
 *
 * @param h     Memory home
 * @param b     SDP bandwidth description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
222 223 224
 * @note The duplicated list is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
225
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
226 227 228 229
 * 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)
230
{
Pekka Pessi's avatar
Pekka Pessi committed
231 232 233 234 235
  SDP_LIST_DUP(bandwidth, b);
}

/**Duplicate an SDP time description.
 *
Pekka Pessi's avatar
Pekka Pessi committed
236 237
 * The function sdp_time_dup() duplicates (deeply copies) a list of SDP time
 * descriptions @a t allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
238 239 240 241
 *
 * @param h  Memory home
 * @param t  SDP time description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
242 243 244
 * @note The duplicated list is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
245
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
246 247 248 249
 * 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)
250
{
Pekka Pessi's avatar
Pekka Pessi committed
251 252 253 254 255 256
  SDP_LIST_DUP(time, t);
}

/**Duplicate an SDP repeat description.
 *
 * The function sdp_repeat_dup() duplicates (deeply copies) an SDP repeat
Pekka Pessi's avatar
Pekka Pessi committed
257
 * description @a r allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
258 259 260 261
 *
 * @param h  Memory home
 * @param r  SDP repeat description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
262 263 264
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
265
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
266 267 268 269
 * 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)
270
{
Pekka Pessi's avatar
Pekka Pessi committed
271 272 273 274 275 276
  SDP_DUP(repeat, r);
}

/**Duplicate an SDP zone description.
 *
 * The function sdp_zone_dup() duplicates (deeply copies) an SDP zone
Pekka Pessi's avatar
Pekka Pessi committed
277
 * description @a z allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281
 *
 * @param h  Memory home
 * @param z  SDP zone description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
282 283 284
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
285
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
286 287 288 289
 * 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)
290
{
Pekka Pessi's avatar
Pekka Pessi committed
291 292 293 294 295 296
  SDP_DUP(zone, z);
}

/**Duplicate an SDP key description.
 *
 * The function sdp_key_dup() duplicates (deeply copies) an SDP key
Pekka Pessi's avatar
Pekka Pessi committed
297
 * description @a k allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
298 299 300 301
 *
 * @param h  Memory home
 * @param k  SDP key description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
302 303 304
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
305
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
306 307 308 309
 * 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)
310
{
Pekka Pessi's avatar
Pekka Pessi committed
311 312 313 314 315 316 317 318 319 320 321
  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
 *
Pekka Pessi's avatar
Pekka Pessi committed
322 323 324
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
325
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
326 327 328 329
 * 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)
330
{
Pekka Pessi's avatar
Pekka Pessi committed
331 332 333 334 335 336 337 338 339 340 341
  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
 *
Pekka Pessi's avatar
Pekka Pessi committed
342 343 344
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
345
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
346 347 348 349
 * 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)
350
{
Pekka Pessi's avatar
Pekka Pessi committed
351 352 353 354 355 356 357 358 359 360 361
  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
 *
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
365
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
366 367 368 369
 * 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)
370
{
Pekka Pessi's avatar
Pekka Pessi committed
371 372 373 374 375 376
  SDP_LIST_DUP(rtpmap, rm);
}

/**Duplicate an SDP media description.
 *
 * The function sdp_media_dup() duplicates (deeply copies) an SDP media
Pekka Pessi's avatar
Pekka Pessi committed
377
 * description @a m allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
378 379 380 381 382 383
 *
 * @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
 *
Pekka Pessi's avatar
Pekka Pessi committed
384 385 386
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
387
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
388 389 390
 * If successful, a pointer to newly allocated sdp_media_t structure is
 * returned, otherwise NULL is returned.
 */
391
sdp_media_t *sdp_media_dup(su_home_t *h, sdp_media_t const *m,
Pekka Pessi's avatar
Pekka Pessi committed
392 393
			   sdp_session_t *sdp)
{
394
  sdp_media_t *rv; size_t size; char *p, *end;
Pekka Pessi's avatar
Pekka Pessi committed
395 396 397 398 399 400 401 402 403 404
  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
Pekka Pessi's avatar
Pekka Pessi committed
405
 * media descriptions @a m allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
406 407 408 409 410 411
 *
 * @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
 *
Pekka Pessi's avatar
Pekka Pessi committed
412 413 414
 * @note The duplicated list is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
415
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
416 417 418
 * If successful, a pointer to a newly allocated list of sdp_media_t
 * structures is returned, otherwise NULL is returned.
 */
419
sdp_media_t *sdp_media_dup_all(su_home_t *h, sdp_media_t const *m,
Pekka Pessi's avatar
Pekka Pessi committed
420 421
			       sdp_session_t *sdp)
{
422
  sdp_media_t *rv; size_t size; char *p, *end;
Pekka Pessi's avatar
Pekka Pessi committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  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

444
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
445 446 447 448 449 450
 * 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.
 */
451
sdp_media_t *sdp_media_dup_ex(su_home_t *home,
Pekka Pessi's avatar
Pekka Pessi committed
452 453 454 455 456
			      sdp_media_t const *src,
			      sdp_session_t *sdp,
			      sdp_connection_t *dst_c,
			      sdp_connection_t const *src_c)
{
457
  sdp_media_t *rv; size_t size; char *p, *end;
Pekka Pessi's avatar
Pekka Pessi committed
458 459 460 461 462 463 464 465 466 467
  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

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

468
static size_t origin_xtra(sdp_origin_t const *o)
Pekka Pessi's avatar
Pekka Pessi committed
469
{
470
  size_t rv = sizeof(*o);
Pekka Pessi's avatar
Pekka Pessi committed
471 472 473 474 475 476 477 478 479 480 481
  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;

482
  p = *pp;
Pekka Pessi's avatar
Pekka Pessi committed
483 484 485 486
  STRUCT_DUP(p, o, src);
  STR_DUP(p, o, src, o_username);
  PTR_DUP(p, o, src, o_address, connection_dup);

487
  assert((size_t)(p - *pp) == origin_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
488 489 490 491
  *pp = p;
  return o;
}

492
static size_t connection_xtra(sdp_connection_t const *c)
Pekka Pessi's avatar
Pekka Pessi committed
493
{
494
  size_t rv = sizeof(*c);
Pekka Pessi's avatar
Pekka Pessi committed
495 496 497 498 499 500 501 502 503 504
  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;

505 506
  p = *pp;
  STRUCT_DUP(p, c, src);
Pekka Pessi's avatar
Pekka Pessi committed
507 508 509
  c->c_next = NULL;
  STR_DUP(p, c, src, c_address);

510
  assert((size_t)(p - *pp) == connection_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
511 512 513 514
  *pp = p;
  return c;
}

515
static size_t bandwidth_xtra(sdp_bandwidth_t const *b)
Pekka Pessi's avatar
Pekka Pessi committed
516
{
517
  size_t rv = sizeof(*b);
Pekka Pessi's avatar
Pekka Pessi committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  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);

533
  assert((size_t)(p - *pp) == bandwidth_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
534 535 536 537 538
  *pp = p;
  return b;
}


539
static size_t time_xtra(sdp_time_t const *t)
Pekka Pessi's avatar
Pekka Pessi committed
540
{
541
  size_t rv = sizeof(*t);
Pekka Pessi's avatar
Pekka Pessi committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  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);

559
  assert((size_t)(p - *pp) == time_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
560 561 562 563 564
  *pp = p;
  return t;
}


565
static size_t repeat_xtra(sdp_repeat_t const *r)
Pekka Pessi's avatar
Pekka Pessi committed
566
{
567
  return (size_t)r->r_size;
Pekka Pessi's avatar
Pekka Pessi committed
568 569 570 571 572 573 574 575 576 577 578
}

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

579
  assert((size_t)(p - *pp) == repeat_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
580 581 582 583 584
  *pp = p;
  return r;
}


585
static size_t zone_xtra(sdp_zone_t const *z)
Pekka Pessi's avatar
Pekka Pessi committed
586 587 588 589 590 591 592 593 594 595 596 597 598
{
  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);

599
  assert((size_t)(p - *pp) == zone_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
600 601 602 603 604
  *pp = p;
  return z;
}


605
static size_t key_xtra(sdp_key_t const *k)
Pekka Pessi's avatar
Pekka Pessi committed
606
{
607
  size_t rv = sizeof(*k);
Pekka Pessi's avatar
Pekka Pessi committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  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);

624
  assert((size_t)(p - *pp) == key_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
625 626 627 628 629
  *pp = p;
  return k;
}


630
static size_t attribute_xtra(sdp_attribute_t const *a)
Pekka Pessi's avatar
Pekka Pessi committed
631
{
632
  size_t rv = sizeof(*a);
Pekka Pessi's avatar
Pekka Pessi committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  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);

650
  assert((size_t)(p - *pp) == attribute_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
651 652 653 654 655
  *pp = p;
  return a;
}


656
static size_t media_xtra(sdp_media_t const *m)
Pekka Pessi's avatar
Pekka Pessi committed
657
{
658
  size_t rv = sizeof(*m);
Pekka Pessi's avatar
Pekka Pessi committed
659 660 661 662 663 664 665 666 667 668

  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);
669

Pekka Pessi's avatar
Pekka Pessi committed
670 671 672 673
  return rv;
}

static
674
sdp_media_t *media_dup(char **pp,
Pekka Pessi's avatar
Pekka Pessi committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		       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);

695
  /* note! we must not implicitly use 'src->m_session' as it
Pekka Pessi's avatar
Pekka Pessi committed
696 697
           might point to a temporary session */
  m->m_session = sdp;
698 699

  m->m_rejected = src->m_rejected;
Pekka Pessi's avatar
Pekka Pessi committed
700 701
  m->m_mode = src->m_mode;

702
  assert((size_t)(p - *pp) == media_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
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
  *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
733
sdp_media_t *media_dup_ex(char **pp,
Pekka Pessi's avatar
Pekka Pessi committed
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
			  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);
762 763

    /* note! we must not implicitly use 'src->m_session' as it
Pekka Pessi's avatar
Pekka Pessi committed
764 765
       might point to a temporary session */
    m->m_session = sdp;
766 767

    m->m_rejected = src->m_rejected;
Pekka Pessi's avatar
Pekka Pessi committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
    m->m_mode = src->m_mode;

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

  assert(p - *pp == xtra);


  *pp = p;

  return retval;
}
#endif

783
static size_t media_xtra_all(sdp_media_t const *m)
Pekka Pessi's avatar
Pekka Pessi committed
784
{
785
  size_t rv = 0;
Pekka Pessi's avatar
Pekka Pessi committed
786 787 788 789 790 791 792 793 794 795

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

  return rv;
}

static
796
sdp_media_t *media_dup_all(char **pp,
Pekka Pessi's avatar
Pekka Pessi committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
			   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;
}

817
static size_t list_xtra(sdp_list_t const *l)
Pekka Pessi's avatar
Pekka Pessi committed
818
{
819
  size_t rv = sizeof(*l);
Pekka Pessi's avatar
Pekka Pessi committed
820 821 822 823 824 825 826 827 828 829
  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;

830
  p = *pp;
Pekka Pessi's avatar
Pekka Pessi committed
831 832 833 834
  STRUCT_DUP(p, l, src);
  l->l_next = NULL;
  STR_DUP(p, l, src, l_text);

835
  assert((size_t)(p - *pp) == list_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
836 837 838 839
  *pp = p;
  return l;
}

840 841

static size_t rtpmap_xtra(sdp_rtpmap_t const *rm)
Pekka Pessi's avatar
Pekka Pessi committed
842
{
843
  size_t rv = sizeof(*rm);
Pekka Pessi's avatar
Pekka Pessi committed
844 845 846 847 848 849 850 851 852 853 854 855
  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;

856
  p = *pp;
Pekka Pessi's avatar
Pekka Pessi committed
857 858 859 860 861 862
  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);

863
  assert((size_t)(p - *pp) == rtpmap_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
864 865 866 867 868
  *pp = p;
  return rm;
}

/** Return total size of a list, including size of all nodes */
869
static size_t list_xtra_all(xtra_f *xtra, void const *v)
Pekka Pessi's avatar
Pekka Pessi committed
870
{
871
  size_t rv = 0;
Pekka Pessi's avatar
Pekka Pessi committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
  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
904
static size_t XXX_xtra(sdp_XXX_t const *YYY)
Pekka Pessi's avatar
Pekka Pessi committed
905
{
906
  size_t rv = sizeof(*YYY);
Pekka Pessi's avatar
Pekka Pessi committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
  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

930
static size_t session_xtra(sdp_session_t const *sdp)
Pekka Pessi's avatar
Pekka Pessi committed
931
{
932
  size_t rv = sizeof(*sdp);
Pekka Pessi's avatar
Pekka Pessi committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956

  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;

957
  p = *pp;
Pekka Pessi's avatar
Pekka Pessi committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
  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);

975
  assert((size_t)(p - *pp) == session_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
976 977 978 979 980 981 982
  *pp = p;
  return sdp;
}

/**Duplicate an SDP session description.
 *
 * The function sdp_session_dup() duplicates (deeply copies) an SDP
Pekka Pessi's avatar
Pekka Pessi committed
983
 * session description @a sdp allocating memory using memory @a home.
Pekka Pessi's avatar
Pekka Pessi committed
984 985 986 987
 *
 * @param h   Memory home
 * @param sdp SDP session description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
988 989 990
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
991
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
992 993 994 995 996
 * 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)
997
{
Pekka Pessi's avatar
Pekka Pessi committed
998 999 1000 1001 1002
  SDP_DUP(session, sdp);
}

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

1003
static size_t session_without_media_xtra(sdp_session_t const *sdp)
Pekka Pessi's avatar
Pekka Pessi committed
1004
{
1005
  size_t rv = sizeof(*sdp);
Pekka Pessi's avatar
Pekka Pessi committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

  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;

1029
  p = *pp;
Pekka Pessi's avatar
Pekka Pessi committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
  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;
1047

1048
  assert((size_t)(p - *pp) == session_without_media_xtra(src));
Pekka Pessi's avatar
Pekka Pessi committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  *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
Pekka Pessi's avatar
Pekka Pessi committed
1059
 * description @a sdp allocating memory using memory @a home. It does not
Pekka Pessi's avatar
Pekka Pessi committed
1060 1061 1062 1063 1064
 * copy the media descriptions, however.
 *
 * @param h     memory h
 * @param sdp   SDP session description to be duplicated
 *
Pekka Pessi's avatar
Pekka Pessi committed
1065 1066 1067
 * @note The duplicated structure is allocated using a single call to
 * su_alloc() and it can be freed with su_free().
 *
1068
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
1069 1070 1071 1072
 * If successful, a pointer to newly allocated sdp_session_t structure is
 * returned, otherwise NULL is returned.
 */

1073
sdp_session_t *sdp_session_dup_without_media(su_home_t *h,
Pekka Pessi's avatar
Pekka Pessi committed
1074
					     sdp_session_t const *sdp)
1075
{
Pekka Pessi's avatar
Pekka Pessi committed
1076 1077 1078 1079 1080 1081
  SDP_DUP(session_without_media, sdp);
}

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

1082
#include <sofia-sip/su_tag_class.h>
Pekka Pessi's avatar
Pekka Pessi committed
1083 1084 1085 1086 1087

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

1088
  if (sdp)
Pekka Pessi's avatar
Pekka Pessi committed
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    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;

1100
  assert(src); assert(*bb);
Pekka Pessi's avatar
Pekka Pessi committed
1101 1102 1103 1104 1105

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

1106
  sdp = srcsdp ? session_dup(&b, srcsdp) : NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

  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;
1120
  size_t retval;
Pekka Pessi's avatar
Pekka Pessi committed
1121 1122 1123

  assert(t);

1124 1125 1126
  if (!t || !t->t_value) {
    if (size && b) b[0] = 0;
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1127 1128 1129 1130 1131 1132 1133
  }

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

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

  retval = sdp_message_size(print);
1134

Pekka Pessi's avatar
Pekka Pessi committed
1135 1136
  sdp_printer_free(print);

1137
  return (int)retval;
Pekka Pessi's avatar
Pekka Pessi committed
1138 1139 1140
}

/** Tag class for SDP tags. @HIDE */
1141
tag_class_t sdptag_session_class[1] =
Pekka Pessi's avatar
Pekka Pessi committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  {{
    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,
  }};


/* ---------------------------------------------------------------------- */
1158 1159

/* Compare two string pointers */
1160
su_inline
1161 1162 1163 1164 1165 1166 1167
int str0cmp(char const *a, char const *b)
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strcmp(a, b);
}

1168
/* Compare two string pointers ignoring case. */
1169
su_inline
1170 1171 1172 1173 1174 1175 1176
int str0casecmp(char const *a, char const *b)
{
  if (a == NULL) a = "";
  if (b == NULL) b = "";
  return strcasecmp(a, b);
}

1177
/** Compare two session descriptions
1178 1179 1180
 */
int sdp_session_cmp(sdp_session_t const *a, sdp_session_t const *b)
{
1181
  int rv;
1182 1183 1184
  sdp_bandwidth_t const *ab, *bb;
  sdp_attribute_t const *aa, *ba;
  sdp_media_t const *am, *bm;
1185 1186

  if ((rv = (a != NULL) - (b != NULL)))
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    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;

1207 1208
  for (ab = a->sdp_bandwidths, bb = b->sdp_bandwidths;
       ab || bb;
1209 1210 1211 1212 1213 1214 1215 1216 1217
       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;

1218 1219
  for (aa = a->sdp_attributes, ba = b->sdp_attributes;
       aa || bb;
1220 1221 1222 1223
       aa = aa->a_next, ba = ba->a_next)
    if ((rv = sdp_attribute_cmp(aa, ba)))
      return rv;

1224 1225
  for (am = a->sdp_media, bm = b->sdp_media;
       am || bm;
1226 1227 1228 1229 1230 1231 1232
       am = am->m_next, bm = bm->m_next)
    if ((rv = sdp_media_cmp(am, bm)))
      return rv;

  return 0;
}

1233
/** Compare two origin fields
Pekka Pessi's avatar
Pekka Pessi committed
1234 1235 1236
 */
int sdp_origin_cmp(sdp_origin_t const *a, sdp_origin_t const *b)
{
1237
  int rv;
Pekka Pessi's avatar
Pekka Pessi committed
1238

1239
  if ((rv = (a != NULL) - (b != NULL)))
Pekka Pessi's avatar
Pekka Pessi committed
1240 1241 1242
    return rv;
  if (a == b)
    return 0;
1243 1244 1245 1246
  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
1247 1248 1249 1250 1251 1252 1253 1254
  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;
}

1255
/** Compare two connection fields
1256 1257 1258 1259 1260
 */
int sdp_connection_cmp(sdp_connection_t const *a, sdp_connection_t const *b)
{
  if (a == b)
    return 0;
1261 1262
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;
1263

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
  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);
1274 1275 1276 1277 1278
}

/** Compare two bandwidth (b=) fields */
int sdp_bandwidth_cmp(sdp_bandwidth_t const *a, sdp_bandwidth_t const *b)
{
1279
  int rv;
1280 1281 1282

  if (a == b)
    return 0;
1283 1284 1285 1286 1287
  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;
1288
  if (a->b_modifier == sdp_bw_x &&
1289
      (rv = strcmp(a->b_modifier_name, b->b_modifier_name)))
1290
    return rv;
1291 1292 1293 1294 1295

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

  return 0;
1296 1297
}

Pekka Pessi's avatar
Pekka Pessi committed
1298 1299 1300
/** Compare two time fields */
int sdp_time_cmp(sdp_time_t const *a, sdp_time_t const *b)
{
1301
  int rv;
Pekka Pessi's avatar
Pekka Pessi committed
1302

1303
  if ((rv = (a != NULL) - (b != NULL)))
Pekka Pessi's avatar
Pekka Pessi committed
1304 1305 1306
    return rv;
  if (a == b)
    return 0;
1307 1308 1309 1310
  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
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
  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)
{
1321
  int i, n;
1322

Pekka Pessi's avatar
Pekka Pessi committed
1323 1324
  if (a == b)
    return 0;
1325 1326 1327 1328 1329 1330 1331
  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;
1332
  n = a->r_number_of_offsets < b->r_number_of_offsets
Pekka Pessi's avatar
Pekka Pessi committed
1333 1334
    ? a->r_number_of_offsets : b->r_number_of_offsets;
  for (i = 0; i < n; i++)
1335 1336
    if (a->r_offsets[i] != b->r_offsets[i])
      return a->r_offsets[i] < b->r_offsets[i] ? -1 : 1;
1337

1338 1339
  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
1340 1341 1342 1343 1344 1345 1346

  return 0;
 }

/** Compare two zone (z=) fields */
int sdp_zone_cmp(sdp_zone_t const *a, sdp_zone_t const *b)
{
1347
  int i, n;
1348

Pekka Pessi's avatar
Pekka Pessi committed
1349 1350
  if (a == b)
    return 0;
1351 1352
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;
Pekka Pessi's avatar
Pekka Pessi committed
1353 1354 1355 1356

  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++) {
1357 1358 1359 1360 1361
    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
1362 1363
  }

1364 1365
  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
1366 1367 1368 1369

  return 0;
}

1370 1371 1372
/** Compare two key (k=) fields */
int sdp_key_cmp(sdp_key_t const *a, sdp_key_t const *b)
{
1373
  int rv;
1374 1375 1376

  if (a == b)
    return 0;
1377 1378 1379 1380 1381
  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;
1382
  if (a->k_method == sdp_key_x &&
1383 1384 1385 1386 1387 1388 1389 1390
      (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)
{
1391
  int rv;
1392 1393 1394

  if (a == b)
    return 0;
1395 1396 1397
  if ((a != NULL) != (b != NULL))
    return (a != NULL) < (b != NULL) ? -1 : 1;

1398 1399 1400 1401 1402
  if ((rv = str0cmp(a->a_name, b->a_name)))
    return rv;
  return str0cmp(a->a_value, b->a_value);
}

1403 1404 1405 1406 1407 1408 1409
/** 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;
1410 1411 1412 1413 1414
  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;
1415 1416 1417 1418 1419

  /* Case insensitive encoding */
  if ((rv = str0cmp(a->rm_encoding, b->rm_encoding)))
    return rv;
  /* Rate */
1420 1421
  if (a->rm_rate != b->rm_rate)
    return a->rm_rate < b->rm_rate ? -1 : 1;
1422 1423 1424 1425 1426 1427 1428 1429

  {
    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;
1430

1431 1432 1433 1434 1435 1436
    rv = strcasecmp(a_param, b_param);

    if (rv)
      return rv;
  }

1437
  return str0casecmp(a->rm_fmtp, b->rm_fmtp);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
}

/** 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;
1448 1449
    if ((a != NULL) != (b != NULL))
      return (a != NULL) < (b != NULL) ? -1 : 1;
1450 1451 1452 1453 1454 1455 1456
    if ((rv = str0cmp(a->l_text, b->l_text)))
      return rv;
  }

  return 0;
}

1457 1458 1459
/** Compare two media (m=) fields */
int sdp_media_cmp(sdp_media_t const *a, sdp_media_t const *b)
{
1460
  int rv;
1461 1462 1463

  sdp_connection_t const *ac, *bc;
  sdp_bandwidth_t const *ab, *bb;
1464
  sdp_rtpmap_t const *arm, *brm;
1465 1466 1467 1468
  sdp_attribute_t const *aa, *ba;

  if (a == b)
    return 0;
1469
  if ((rv = (a != NULL) - (b != NULL)))
1470
    return rv;
1471 1472 1473

  if (a->m_type != b->m_type)
    return a->m_type < b->m_type ? -1 : 1;
1474 1475 1476
  if (a->m_type == sdp_media_x)
    if ((rv = str0cmp(a->m_type_name, b->m_type_name)))
      return rv;
1477 1478
  if (a->m_port != b->m_port)
    return a->m_port < b->m_port ? -1 : 1;
1479 1480 1481 1482 1483

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

1484 1485 1486 1487 1488
  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;
1489 1490 1491
  if (a->m_proto == sdp_media_x)
    if ((rv = str0cmp(a->m_proto_name, b->m_proto_name)))
      return rv;
1492

1493 1494
  if (a->m_mode != b->m_mode)
    return a->m_mode < b->m_mode ? -1 : 1;
1495

1496 1497
  for (arm = a->m_rtpmaps, brm = b->m_rtpmaps;
       arm || brm;
1498 1499 1500 1501 1502 1503
       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;
1504

Pekka Pessi's avatar
Pekka Pessi committed
1505 1506 1507
  if ((rv = str0cmp(a->m_information, b->m_information)))
    return rv;

1508 1509
  for (ac = a->m_connections, bc = b->m_connections;
       ac || bc;
1510 1511 1512 1513
       ac = ac->c_next, bc = bc->c_next)
  if ((rv = sdp_connection_cmp(ac, bc)))
    return rv;

1514 1515
  for (ab = a->m_bandwidths, bb = b->m_bandwidths;
       ab || bb;
1516 1517 1518 1519 1520 1521 1522
       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;

1523 1524
  for (aa = a->m_attributes, ba = b->m_attributes;
       aa || bb;
1525 1526 1527 1528 1529 1530 1531
       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
1532 1533
/* ---------------------------------------------------------------------- */

1534
sdp_connection_t *sdp_media_connections(sdp_media_t const *m)
Pekka Pessi's avatar
Pekka Pessi committed
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
{
  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. */
1548
sdp_attribute_t *sdp_attribute_find(sdp_attribute_t const *a, char const *name)
Pekka Pessi's avatar
Pekka Pessi committed
1549 1550 1551 1552 1553 1554
{
  for (; a; a = a->a_next) {
    if (strcasecmp(a->a_name, name) == 0)
      break;
  }

1555
  return (sdp_attribute_t *)a;
Pekka Pessi's avatar
Pekka Pessi committed
1556 1557 1558
}

/** Find named attribute from given lists (a or a2). */
1559 1560
sdp_attribute_t *sdp_attribute_find2(sdp_attribute_t const *a,
				     sdp_attribute_t const *a2,
Pekka Pessi's avatar
Pekka Pessi committed
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
				     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;
    }

1574
  return (sdp_attribute_t *)a;
Pekka Pessi's avatar
Pekka Pessi committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
}

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

1594
/** Convert session mode as #sdp_attribute_t structure. */
Pekka Pessi's avatar
Pekka Pessi committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
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;
1610

Pekka Pessi's avatar
Pekka Pessi committed
1611 1612 1613 1614 1615 1616 1617
  a = su_salloc(home, sizeof(*a));
  if (a)
    a->a_name = name;

  return a;
}

1618
/** Find a mapped attribute.
Pekka Pessi's avatar
Pekka Pessi committed
1619
 *
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
 * 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
 *
1630
 * @return Pointer to a matching attribute structure, or NULL.
Pekka Pessi's avatar
Pekka Pessi committed
1631
 *
1632 1633
 * 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
1634
 */
1635
sdp_attribute_t *sdp_attribute_mapped_find(sdp_attribute_t const *a,
Pekka Pessi's avatar
Pekka Pessi committed
1636
					   char const *name,
1637
					   int pt, char **return_result)
Pekka Pessi's avatar
Pekka Pessi committed
1638
{
1639 1640 1641 1642 1643