soa.c 60.9 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE soa.c
 * @brief Sofia SDP Offer/Answer Engine interface
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Aug  3 20:27:15 EEST 2005
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
38
#include <errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
39 40 41

#include <assert.h>

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

45 46 47 48
#include "sofia-sip/soa.h"
#include "sofia-sip/sdp.h"
#include "sofia-sip/soa_session.h"
#include "sofia-sip/soa_add.h"
Pekka Pessi's avatar
Pekka Pessi committed
49

50
#include <sofia-sip/hostdomain.h>
51 52 53
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_localinfo.h>
#include <sofia-sip/su_uniqueid.h>
54

55
#include <sofia-sip/string0.h>
56
#include <sofia-sip/su_errno.h>
57

Pekka Pessi's avatar
Pekka Pessi committed
58 59
#define NONE ((void *)-1)
#define XXX assert(!"implemented")
60 61

typedef unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
62

Pekka Pessi's avatar
Pekka Pessi committed
63 64 65 66 67 68 69
#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
static char const __func__[] = "soa";
#endif

Pekka Pessi's avatar
Pekka Pessi committed
70 71 72
/* ======================================================================== */

/* Internal prototypes */
Pekka Pessi's avatar
Pekka Pessi committed
73 74 75
void soa_set_activity(soa_session_t *ss,
		      sdp_media_t const *,
		      int remote);
Pekka Pessi's avatar
Pekka Pessi committed
76 77 78

static inline int soa_media_is_ready(soa_session_t const *ss);

79 80
enum soa_sdp_kind { 
  soa_capability_sdp_kind,
81
  soa_user_sdp_kind,
82 83 84 85 86 87
  soa_remote_sdp_kind
};

static int soa_set_sdp(soa_session_t *ss, 
		       enum soa_sdp_kind what,
		       sdp_session_t const *sdp0,
88
		       char const *sdp_str, issize_t str_len);
89

Pekka Pessi's avatar
Pekka Pessi committed
90 91 92
/* ======================================================================== */

#define SOA_VALID_ACTIONS(a)					\
93 94
  ((a)->sizeof_soa_session_actions >= (int)sizeof (*actions) && \
   (a)->sizeof_soa_session >= (int)sizeof(soa_session_t) &&     \
Pekka Pessi's avatar
Pekka Pessi committed
95
   (a)->soa_name != NULL &&					\
Pekka Pessi's avatar
Pekka Pessi committed
96
   (a)->soa_init != NULL &&					\
97
   (a)->soa_deinit != NULL &&					\
Pekka Pessi's avatar
Pekka Pessi committed
98 99
   (a)->soa_set_params != NULL &&				\
   (a)->soa_get_params != NULL &&				\
100 101 102 103 104 105 106 107
   (a)->soa_get_paramlist != NULL &&				\
   (a)->soa_media_features != NULL &&				\
   (a)->soa_sip_require != NULL &&				\
   (a)->soa_sip_supported != NULL &&				\
   (a)->soa_remote_sip_features != NULL &&			\
   (a)->soa_set_capability_sdp != NULL &&			\
   (a)->soa_set_remote_sdp != NULL &&				\
   (a)->soa_set_user_sdp != NULL &&				\
Pekka Pessi's avatar
Pekka Pessi committed
108 109
   (a)->soa_generate_offer != NULL &&				\
   (a)->soa_generate_answer != NULL &&				\
110 111
   (a)->soa_process_answer != NULL &&				\
   (a)->soa_process_reject != NULL &&				\
Pekka Pessi's avatar
Pekka Pessi committed
112
   (a)->soa_activate_session != NULL &&				\
113
   (a)->soa_deactivate_session != NULL &&			\
Pekka Pessi's avatar
Pekka Pessi committed
114 115 116 117 118 119 120 121 122 123
   (a)->soa_terminate_session != NULL)

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

/**@var SOA_DEBUG
 *
 * Environment variable determining the default debug log level.
 *
 * The SOA_DEBUG environment variable is used to determine the default
 * debug logging level. The normal level is 3.
Pekka Pessi's avatar
Pekka Pessi committed
124
 *
Pekka Pessi's avatar
Pekka Pessi committed
125 126 127 128 129 130 131 132
 * @sa <su_debug.h>, su_log_global, SOFIA_DEBUG
 */
extern char const SOA_DEBUG[];

#ifndef SU_DEBUG
#define SU_DEBUG 3
#endif

Pekka Pessi's avatar
Pekka Pessi committed
133 134
/**Debug log for @b soa module.
 *
Pekka Pessi's avatar
Pekka Pessi committed
135 136 137 138 139
 * The soa_log is the log object used by @b soa module. The level of
 * #soa_log is set using #SOA_DEBUG environment variable.
 */
su_log_t soa_log[] = { SU_LOG_INIT("soa", "SOA_DEBUG", SU_DEBUG) };

140 141 142
/* Add " around string */
#define NICE(s) s ? "\"" : "", s ? s : "(nil)", s ? "\"" : ""

Pekka Pessi's avatar
Pekka Pessi committed
143 144 145 146
/* ======================================================================== */

/* API Functions */

Pekka Pessi's avatar
Pekka Pessi committed
147
struct soa_namenode
Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152 153 154 155
{
  struct soa_namenode const *next;
  char const *basename;
  struct soa_session_actions const *actions;
};

#define SOA_NAMELISTLEN (16)

Pekka Pessi's avatar
Pekka Pessi committed
156
static struct soa_namenode const soa_default_node =
Pekka Pessi's avatar
Pekka Pessi committed
157 158 159 160 161 162 163
  {
    NULL, "default", &soa_default_actions
  };

static struct soa_namenode const *soa_namelist = &soa_default_node;

/** Add a named soa backend */
Pekka Pessi's avatar
Pekka Pessi committed
164 165
int soa_add(char const *name,
	    struct soa_session_actions const *actions)
Pekka Pessi's avatar
Pekka Pessi committed
166 167 168 169
{
  struct soa_namenode const *n;
  struct soa_namenode *e;

170 171
  SU_DEBUG_9(("soa_add(%s%s%s, %p) called\n", NICE(name), actions));

Pekka Pessi's avatar
Pekka Pessi committed
172
  if (name == NULL || actions == NULL)
173
    return su_seterrno(EFAULT);
Pekka Pessi's avatar
Pekka Pessi committed
174 175

  if (!SOA_VALID_ACTIONS(actions))
176
    return su_seterrno(EINVAL);
Pekka Pessi's avatar
Pekka Pessi committed
177

Pekka Pessi's avatar
Pekka Pessi committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  for (n = soa_namelist; n; n = n->next) {
    if (strcasecmp(name, n->basename) == 0)
      return 0;
  }

  e = malloc(sizeof *e); if (!e) return -1;

  e->next = soa_namelist;
  e->basename = name;
  e->actions = actions;

  soa_namelist = e;

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
194 195 196
/** Search for a named backend */
struct soa_session_actions const *soa_find(char const *name)
{
197 198
  SU_DEBUG_9(("soa_find(%s%s%s) called\n", NICE(name)));

Pekka Pessi's avatar
Pekka Pessi committed
199 200 201 202 203 204 205 206 207 208
  if (name) {
    struct soa_namenode const *n;
    size_t baselen = strcspn(name, ":/");

    for (n = soa_namelist; n; n = n->next) {
      if (strncasecmp(name, n->basename, baselen) == 0)
	break;
    }

    if (n == NULL)
209
      return (void)su_seterrno(ENOENT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
210 211 212 213 214 215 216

    return n->actions;
  }

  return NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
217 218
/* ======================================================================== */

Pekka Pessi's avatar
Pekka Pessi committed
219
/** Create a soa session. */
Pekka Pessi's avatar
Pekka Pessi committed
220 221 222
soa_session_t *soa_create(char const *name,
			  su_root_t *root,
			  soa_magic_t *magic)
Pekka Pessi's avatar
Pekka Pessi committed
223 224 225 226
{
  struct soa_session_actions const *actions = &soa_default_actions;

  soa_session_t *ss;
227
  size_t namelen;
Pekka Pessi's avatar
Pekka Pessi committed
228

229 230 231
  SU_DEBUG_9(("soa_create(\"%s\", %p, %p) called\n",
	      name ? name : "default", root, magic));

232
  if (name && name[0]) {
Pekka Pessi's avatar
Pekka Pessi committed
233 234 235 236 237 238 239 240
    struct soa_namenode const *n;
    size_t baselen = strcspn(name, ":/");

    for (n = soa_namelist; n; n = n->next) {
      if (strncasecmp(name, n->basename, baselen) == 0)
	break;
    }
    if (n == NULL)
241
      return (void)su_seterrno(ENOENT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
242 243 244

    actions = n->actions; assert(actions);
  }
245 246
  else
    name = "default";
Pekka Pessi's avatar
Pekka Pessi committed
247 248 249 250

  assert(SOA_VALID_ACTIONS(actions));

  if (root == NULL)
251
    return (void)su_seterrno(EFAULT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
252

253 254 255
  namelen = strlen(name) + 1;
  
  ss = su_home_new(actions->sizeof_soa_session + namelen);
Pekka Pessi's avatar
Pekka Pessi committed
256 257
  if (ss) {
    ss->ss_root = root;
Pekka Pessi's avatar
Pekka Pessi committed
258
    ss->ss_magic = magic;
Pekka Pessi's avatar
Pekka Pessi committed
259
    ss->ss_actions = actions;
260
    ss->ss_name = strcpy((char *)ss + actions->sizeof_soa_session, name);
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263

    if (ss->ss_actions->soa_init(name, ss, NULL) < 0)
      ss->ss_actions->soa_deinit(ss), ss = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
264 265 266 267 268
  }

  return ss;
}

269
/** Create a copy of a @soa session object. */
Pekka Pessi's avatar
Pekka Pessi committed
270 271 272
soa_session_t *soa_clone(soa_session_t *parent_ss,
			 su_root_t *root,
			 soa_magic_t *magic)
Pekka Pessi's avatar
Pekka Pessi committed
273
{
Pekka Pessi's avatar
Pekka Pessi committed
274
  soa_session_t *ss;
275
  size_t namelen;
Pekka Pessi's avatar
Pekka Pessi committed
276

277 278 279 280
  SU_DEBUG_9(("soa_clone(%s::%p, %p, %p) called\n",
	      parent_ss ? parent_ss->ss_actions->soa_name : "",
	      parent_ss, root, magic));

Pekka Pessi's avatar
Pekka Pessi committed
281
  if (parent_ss == NULL || root == NULL)
282
    return (void)su_seterrno(EFAULT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
283

284 285 286
  namelen = strlen(parent_ss->ss_name) + 1;

  ss = su_home_new(parent_ss->ss_actions->sizeof_soa_session + namelen);
Pekka Pessi's avatar
Pekka Pessi committed
287 288 289 290
  if (ss) {
    ss->ss_root = root;
    ss->ss_magic = magic;
    ss->ss_actions = parent_ss->ss_actions;
291 292
    ss->ss_name = strcpy((char *)ss + ss->ss_actions->sizeof_soa_session,
			 parent_ss->ss_name);
Pekka Pessi's avatar
Pekka Pessi committed
293 294 295

    if (ss->ss_actions->soa_init(NULL, ss, parent_ss) < 0)
      ss->ss_actions->soa_deinit(ss), ss = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
296
  }
Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301 302 303

  return ss;
}

/** Increase reference count */
soa_session_t *soa_session_ref(soa_session_t *ss)
{
304 305
  SU_DEBUG_9(("soa_session_ref(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));
Pekka Pessi's avatar
Pekka Pessi committed
306 307 308 309 310 311
  return su_home_ref(ss->ss_home);
}

/** Decrease reference count */
void soa_session_unref(soa_session_t *ss)
{
312 313
  SU_DEBUG_9(("soa_session_unref(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));
Pekka Pessi's avatar
Pekka Pessi committed
314
  su_home_unref(ss->ss_home);
Pekka Pessi's avatar
Pekka Pessi committed
315 316 317
}

/* Initialize session */
Pekka Pessi's avatar
Pekka Pessi committed
318 319
int soa_base_init(char const *name,
		     soa_session_t *ss,
Pekka Pessi's avatar
Pekka Pessi committed
320 321 322 323
		     soa_session_t *parent)
{
  if (parent) {
#define DUP(d, dup, s) if ((s) && !((d) = dup(ss->ss_home, (s)))) return -1
324
    su_home_t *home = ss->ss_home;
Pekka Pessi's avatar
Pekka Pessi committed
325

326
    if (soa_description_dup(home, ss->ss_caps, parent->ss_caps) < 0)
327
      return -1;
328
    if (soa_description_dup(home, ss->ss_user, parent->ss_user) < 0)
329
      return -1;
330
    if (soa_description_dup(home, ss->ss_local, parent->ss_local) < 0)
331
      return -1;
332
    if (soa_description_dup(home, ss->ss_remote, parent->ss_remote) < 0)
333
      return -1;
Pekka Pessi's avatar
Pekka Pessi committed
334 335 336

    DUP(ss->ss_address, su_strdup, parent->ss_address);
    ss->ss_af = parent->ss_af;
337
    DUP(ss->ss_hold, su_strdup, parent->ss_hold);
Pekka Pessi's avatar
Pekka Pessi committed
338 339

    DUP(ss->ss_cname, su_strdup, parent->ss_cname);
Pekka Pessi's avatar
Pekka Pessi committed
340

Pekka Pessi's avatar
Pekka Pessi committed
341 342 343 344 345 346 347 348 349 350 351
    ss->ss_srtp_enable = parent->ss_srtp_enable;
    ss->ss_srtp_confidentiality = parent->ss_srtp_confidentiality;
    ss->ss_srtp_integrity = parent->ss_srtp_integrity;
  }

  return 0;
}

/** Destroy a session. */
void soa_destroy(soa_session_t *ss)
{
352 353 354
  SU_DEBUG_9(("soa_destroy(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

Pekka Pessi's avatar
Pekka Pessi committed
355
  if (ss) {
Pekka Pessi's avatar
Pekka Pessi committed
356 357
    ss->ss_active = 0;
    ss->ss_terminated++;
Pekka Pessi's avatar
Pekka Pessi committed
358
    ss->ss_actions->soa_deinit(ss);
Pekka Pessi's avatar
Pekka Pessi committed
359
    su_home_unref(ss->ss_home);
Pekka Pessi's avatar
Pekka Pessi committed
360 361 362
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
363
void soa_base_deinit(soa_session_t *ss)
Pekka Pessi's avatar
Pekka Pessi committed
364 365 366 367
{
  (void)ss;
}

368 369 370 371 372 373
/** Set parameters.
 *
 * @param ss soa session object
 * @param tag, value, ... tagged parameter list
 *
 * @return Number of parameters set, or -1 upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
 *
 * @TAGS
 * SOATAG_CAPS_SDP(),
 * SOATAG_CAPS_SDP_STR(),
 * SOATAG_USER_SDP(),
 * SOATAG_USER_SDP_STR(),
 * SOATAG_REMOTE_SDP(),
 * SOATAG_REMOTE_SDP_STR(),
 * SOATAG_AF(),
 * SOATAG_ADDRESS(),
 * SOATAG_AUDIO_AUX() (currently for "default" only),
 * SOATAG_HOLD(),
 * SOATAG_RTP_SELECT(),
 * SOATAG_RTP_SORT(),
 * SOATAG_RTP_MISMATCH(),
 * SOATAG_SRTP_ENABLE(),
 * SOATAG_SRTP_CONFIDENTIALITY(), and
 * SOATAG_SRTP_INTEGRITY().
392
 */
393
int soa_set_params(soa_session_t *ss, tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
394 395
{
  ta_list ta;
396
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
397

398 399 400
  SU_DEBUG_9(("soa_set_params(%s::%p, ...) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

Pekka Pessi's avatar
Pekka Pessi committed
401
  if (ss == NULL)
402
    return su_seterrno(EFAULT), -1;
Pekka Pessi's avatar
Pekka Pessi committed
403 404 405 406 407 408 409

  ta_start(ta, tag, value);

  n = ss->ss_actions->soa_set_params(ss, ta_args(ta));

  ta_end(ta);

410
  return n;
Pekka Pessi's avatar
Pekka Pessi committed
411 412
}

413 414 415 416 417 418
/**Base method for setting parameters.
 *
 * @param ss   soa session object
 * @param tags  tag item list
 *
 * @return Number of parameters set, or -1 upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
 *
 * @TAGS
 * SOATAG_CAPS_SDP(),
 * SOATAG_CAPS_SDP_STR(),
 * SOATAG_USER_SDP(),
 * SOATAG_USER_SDP_STR(),
 * SOATAG_REMOTE_SDP(),
 * SOATAG_REMOTE_SDP_STR(),
 * SOATAG_AF(),
 * SOATAG_ADDRESS(),
 * SOATAG_HOLD(),
 * SOATAG_RTP_SELECT(),
 * SOATAG_RTP_SORT(),
 * SOATAG_RTP_MISMATCH(),
 * SOATAG_SRTP_ENABLE(),
 * SOATAG_SRTP_CONFIDENTIALITY(), and
 * SOATAG_SRTP_INTEGRITY().
436
 */
437
int soa_base_set_params(soa_session_t *ss, tagi_t const *tags)
Pekka Pessi's avatar
Pekka Pessi committed
438
{
439
  int n, change_session = 0;
Pekka Pessi's avatar
Pekka Pessi committed
440

441
  sdp_session_t const *caps_sdp, *user_sdp;
442
  char const *caps_sdp_str, *user_sdp_str;
443

444
  int af;
445
  char const *media_address, *hold;
446
  int rtp_select, rtp_sort;
447
  int rtp_mismatch;
Pekka Pessi's avatar
Pekka Pessi committed
448 449 450 451
  int srtp_enable, srtp_confidentiality, srtp_integrity;

  af = ss->ss_af;

452
  hold = ss->ss_hold;
Pekka Pessi's avatar
Pekka Pessi committed
453 454
  media_address = ss->ss_address;

455 456
  rtp_select = (int)ss->ss_rtp_select;
  rtp_sort = (int)ss->ss_rtp_sort;
457 458
  rtp_mismatch = ss->ss_rtp_mismatch;

Pekka Pessi's avatar
Pekka Pessi committed
459 460 461 462
  srtp_enable = ss->ss_srtp_enable;
  srtp_confidentiality = ss->ss_srtp_confidentiality;
  srtp_integrity = ss->ss_srtp_integrity;

463 464 465
  caps_sdp = user_sdp = NONE;
  caps_sdp_str = user_sdp_str = NONE;

Pekka Pessi's avatar
Pekka Pessi committed
466
  n = tl_gets(tags,
467 468

	      SOATAG_CAPS_SDP_REF(caps_sdp),
Pekka Pessi's avatar
Pekka Pessi committed
469 470
	      SOATAG_CAPS_SDP_STR_REF(caps_sdp_str),

471 472
	      SOATAG_USER_SDP_REF(user_sdp),
	      SOATAG_USER_SDP_STR_REF(user_sdp_str),
473

Pekka Pessi's avatar
Pekka Pessi committed
474 475
	      SOATAG_AF_REF(af),
	      SOATAG_ADDRESS_REF(media_address),
476
	      SOATAG_HOLD_REF(hold),
Pekka Pessi's avatar
Pekka Pessi committed
477

478 479 480 481
	      SOATAG_RTP_SELECT_REF(rtp_select),
	      SOATAG_RTP_SORT_REF(rtp_sort),
	      SOATAG_RTP_MISMATCH_REF(rtp_mismatch),

Pekka Pessi's avatar
Pekka Pessi committed
482 483 484
	      SOATAG_SRTP_ENABLE_REF(srtp_enable),
	      SOATAG_SRTP_CONFIDENTIALITY_REF(srtp_confidentiality),
	      SOATAG_SRTP_INTEGRITY_REF(srtp_integrity),
Pekka Pessi's avatar
Pekka Pessi committed
485

Pekka Pessi's avatar
Pekka Pessi committed
486 487 488 489 490
	      TAG_END());

  if (n <= 0)
    return n;

491 492 493 494 495
  if (caps_sdp != NONE || caps_sdp_str != NONE) {
    if (caps_sdp == NONE) caps_sdp = NULL;
    if (caps_sdp_str == NONE) caps_sdp_str = NULL;

    if (caps_sdp || caps_sdp_str) {
496
      if (soa_set_capability_sdp(ss, caps_sdp, caps_sdp_str, -1) < 0) {
497
	return -1;
Pekka Pessi's avatar
Pekka Pessi committed
498 499
      }
    }
500 501 502
    else {
      soa_description_free(ss, ss->ss_caps);
    }
Pekka Pessi's avatar
Pekka Pessi committed
503 504
  }

505 506 507
  if (user_sdp != NONE || user_sdp_str != NONE) {
    if (user_sdp == NONE) user_sdp = NULL;
    if (user_sdp_str == NONE) user_sdp_str = NULL;
508

509 510
    if (user_sdp || user_sdp_str) {
      if (soa_set_user_sdp(ss, user_sdp, user_sdp_str, -1) < 0) {
511 512
	return -1;
      }
513 514
      if (ss->ss_caps->ssd_str == NULL)
	soa_set_capability_sdp(ss, user_sdp, user_sdp_str, -1);
515 516
    }
    else {
517
      soa_description_free(ss, ss->ss_user);
518 519 520
    }
  }

521 522 523 524
  if (af < SOA_AF_ANY || af > SOA_AF_IP6_IP4) 
    af = ss->ss_af;

  if (rtp_select < SOA_RTP_SELECT_SINGLE || rtp_select > SOA_RTP_SELECT_ALL)
525
    rtp_select = (int)ss->ss_rtp_select;
526
  if (rtp_sort < SOA_RTP_SORT_DEFAULT || rtp_sort > SOA_RTP_SORT_REMOTE)
527
    rtp_sort = (int)ss->ss_rtp_sort;
528 529 530 531 532 533
  rtp_mismatch = rtp_mismatch != 0;

  srtp_enable = srtp_enable != 0;
  srtp_confidentiality = srtp_confidentiality != 0;
  srtp_integrity = srtp_integrity != 0;

534
  change_session 
535
    =  af != (int)ss->ss_af
536 537
    || rtp_select != (int)ss->ss_rtp_select
    || rtp_sort != (int)ss->ss_rtp_sort
538 539 540 541 542
    || rtp_mismatch != (int)ss->ss_rtp_mismatch
    || srtp_enable != (int)ss->ss_srtp_enable
    || srtp_confidentiality != (int)ss->ss_srtp_confidentiality
    || srtp_integrity != (int)ss->ss_srtp_integrity
    ;
543 544 545 546 547 548 549 550 551 552

  ss->ss_af = af;

  ss->ss_rtp_select = rtp_select;
  ss->ss_rtp_sort = rtp_sort;
  ss->ss_rtp_mismatch = rtp_mismatch;

  ss->ss_srtp_enable = srtp_enable;
  ss->ss_srtp_confidentiality = srtp_confidentiality;
  ss->ss_srtp_integrity = srtp_integrity;
Pekka Pessi's avatar
Pekka Pessi committed
553 554 555 556

  if (str0casecmp(media_address, ss->ss_address)) {
    su_free(ss->ss_home, (void *)ss->ss_address);
    ss->ss_address = su_strdup(ss->ss_home, media_address);
557
    change_session = 1;
Pekka Pessi's avatar
Pekka Pessi committed
558 559
  }

560 561
  if (hold == (char const *)1)
    hold = "*";
Pekka Pessi's avatar
Pekka Pessi committed
562

563 564 565
  if (str0casecmp(hold, ss->ss_hold)) {
    su_free(ss->ss_home, (void *)ss->ss_hold);
    ss->ss_hold = su_strdup(ss->ss_home, hold);
566
    change_session = 1;
Pekka Pessi's avatar
Pekka Pessi committed
567 568
  }

569 570 571
  if (change_session)
    ss->ss_user_version++;

572
  return n;
Pekka Pessi's avatar
Pekka Pessi committed
573 574
}

575 576 577 578 579 580
/** Get tagged parameters.
 *
 * @param ss soa session object
 * @param tag, value, ... tagged parameter list
 *
 * @return Number of parameters get, or -1 upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
 *
 * @TAGS
 * SOATAG_CAPS_SDP(),
 * SOATAG_CAPS_SDP_STR(),
 * SOATAG_USER_SDP(),
 * SOATAG_USER_SDP_STR(),
 * SOATAG_LOCAL_SDP(),
 * SOATAG_LOCAL_SDP_STR(),
 * SOATAG_REMOTE_SDP(),
 * SOATAG_REMOTE_SDP_STR(),
 * SOATAG_AF(),
 * SOATAG_ADDRESS(),
 * SOATAG_AUDIO_AUX() (currently for "default" only),
 * SOATAG_HOLD(),
 * SOATAG_RTP_SELECT(),
 * SOATAG_RTP_SORT(),
 * SOATAG_RTP_MISMATCH(),
 * SOATAG_SRTP_ENABLE(),
 * SOATAG_SRTP_CONFIDENTIALITY(), and
 * SOATAG_SRTP_INTEGRITY().
601
 */
602 603
int soa_get_params(soa_session_t const *ss,
		   tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
604 605
{
  ta_list ta;
606
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
607

608 609 610
  SU_DEBUG_9(("soa_get_params(%s::%p, ...) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

Pekka Pessi's avatar
Pekka Pessi committed
611
  if (ss == NULL)
612
    return su_seterrno(EFAULT), -1;
Pekka Pessi's avatar
Pekka Pessi committed
613 614 615 616 617 618 619

  ta_start(ta, tag, value);

  n = ss->ss_actions->soa_get_params(ss, ta_args(ta));

  ta_end(ta);

620
  return n;
Pekka Pessi's avatar
Pekka Pessi committed
621 622
}

623 624 625 626 627 628
/**Base method for getting tagged parameters.
 *
 * @param ss soa session object
 * @param tags   tag item list
 *
 * @return Number of parameters get, or -1 upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
 *
 * @TAGS
 * SOATAG_CAPS_SDP(),
 * SOATAG_CAPS_SDP_STR(),
 * SOATAG_USER_SDP(),
 * SOATAG_USER_SDP_STR(),
 * SOATAG_LOCAL_SDP(),
 * SOATAG_LOCAL_SDP_STR(),
 * SOATAG_REMOTE_SDP(),
 * SOATAG_REMOTE_SDP_STR(),
 * SOATAG_AF(),
 * SOATAG_ADDRESS(),
 * SOATAG_HOLD(),
 * SOATAG_RTP_SELECT(),
 * SOATAG_RTP_SORT(),
 * SOATAG_RTP_MISMATCH(),
 * SOATAG_SRTP_ENABLE(),
 * SOATAG_SRTP_CONFIDENTIALITY(), and
 * SOATAG_SRTP_INTEGRITY().
648
 */
649
int soa_base_get_params(soa_session_t const *ss, tagi_t *tags)
Pekka Pessi's avatar
Pekka Pessi committed
650
{
651
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
652 653

  n = tl_tgets(tags,
654 655
	       SOATAG_CAPS_SDP(ss->ss_caps->ssd_sdp),
	       SOATAG_CAPS_SDP_STR(ss->ss_caps->ssd_str),
Pekka Pessi's avatar
Pekka Pessi committed
656

657 658
	       SOATAG_USER_SDP(ss->ss_user->ssd_sdp),
	       SOATAG_USER_SDP_STR(ss->ss_user->ssd_str),
659

660 661 662 663
	       SOATAG_LOCAL_SDP(ss->ss_local->ssd_sdp),
	       SOATAG_LOCAL_SDP_STR(ss->ss_local->ssd_str),

	       SOATAG_REMOTE_SDP(ss->ss_remote->ssd_sdp),
664
	       SOATAG_REMOTE_SDP_STR(ss->ss_remote->ssd_unparsed),
665

Pekka Pessi's avatar
Pekka Pessi committed
666 667
	       SOATAG_AF(ss->ss_af),
	       SOATAG_ADDRESS(ss->ss_address),
668
	       SOATAG_HOLD(ss->ss_hold),
Pekka Pessi's avatar
Pekka Pessi committed
669

670 671
	       SOATAG_RTP_SELECT((int)ss->ss_rtp_select),
	       SOATAG_RTP_SORT((int)ss->ss_rtp_sort),
672 673
	       SOATAG_RTP_MISMATCH(ss->ss_rtp_mismatch),

Pekka Pessi's avatar
Pekka Pessi committed
674 675 676 677 678 679 680 681 682
	       SOATAG_SRTP_ENABLE(ss->ss_srtp_enable),
	       SOATAG_SRTP_CONFIDENTIALITY(ss->ss_srtp_confidentiality),
	       SOATAG_SRTP_INTEGRITY(ss->ss_srtp_integrity),

	       TAG_END());

  return n;
}

683
/** Return a list of parameters. */
684 685
tagi_t *soa_get_paramlist(soa_session_t const *ss,
			  tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
686
{
687 688
  ta_list ta;
  tagi_t *params = NULL;
689

690 691 692
  SU_DEBUG_9(("soa_get_paramlist(%s::%p, ...) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

693
  if (ss) {
694 695 696 697 698
    ta_start(ta, tag, value);
    params = ss->ss_actions->soa_get_paramlist(ss, ta_tags(ta));
    ta_end(ta);
  }

699
  return params;
Pekka Pessi's avatar
Pekka Pessi committed
700 701 702
}


703
/** Base bethod for getting list of parameters. */
704 705 706
tagi_t *soa_base_get_paramlist(soa_session_t const *ss, 
			       tag_type_t tag, tag_value_t value, 
			       ...)
Pekka Pessi's avatar
Pekka Pessi committed
707
{
708 709
  ta_list ta;
  tagi_t *params;
Pekka Pessi's avatar
Pekka Pessi committed
710 711 712

  if (ss == NULL)
    return NULL;
713 714
  
  ta_start(ta, tag, value);
Pekka Pessi's avatar
Pekka Pessi committed
715

716
  params = tl_llist(
717 718 719 720
		   TAG_IF(ss->ss_caps->ssd_sdp,
			  SOATAG_CAPS_SDP(ss->ss_caps->ssd_sdp)),
		   TAG_IF(ss->ss_caps->ssd_str,
			  SOATAG_CAPS_SDP_STR(ss->ss_caps->ssd_str)),
Pekka Pessi's avatar
Pekka Pessi committed
721

722 723 724 725 726 727 728 729 730
		   TAG_IF(ss->ss_user->ssd_sdp,
			  SOATAG_USER_SDP(ss->ss_user->ssd_sdp)),
		   TAG_IF(ss->ss_user->ssd_str,
			  SOATAG_USER_SDP_STR(ss->ss_user->ssd_str)),

		   TAG_IF(ss->ss_local->ssd_sdp,
			  SOATAG_LOCAL_SDP(ss->ss_local->ssd_sdp)),
		   TAG_IF(ss->ss_user->ssd_str,
			  SOATAG_LOCAL_SDP_STR(ss->ss_local->ssd_str)),
Pekka Pessi's avatar
Pekka Pessi committed
731

732 733 734
		   TAG_IF(ss->ss_remote->ssd_sdp,
			  SOATAG_REMOTE_SDP(ss->ss_remote->ssd_sdp)),
		   TAG_IF(ss->ss_remote->ssd_str,
735
			  SOATAG_REMOTE_SDP_STR(ss->ss_remote->ssd_unparsed)),
736

Pekka Pessi's avatar
Pekka Pessi committed
737
		   SOATAG_AF(ss->ss_af),
738 739
		   TAG_IF(ss->ss_address, 
			  SOATAG_ADDRESS(ss->ss_address)),
Pekka Pessi's avatar
Pekka Pessi committed
740

Pekka Pessi's avatar
Pekka Pessi committed
741 742 743
		   SOATAG_SRTP_ENABLE(ss->ss_srtp_enable),
		   SOATAG_SRTP_CONFIDENTIALITY(ss->ss_srtp_confidentiality),
		   SOATAG_SRTP_INTEGRITY(ss->ss_srtp_integrity),
Pekka Pessi's avatar
Pekka Pessi committed
744

745
		   ta_tags(ta));
Pekka Pessi's avatar
Pekka Pessi committed
746

747
  ta_end(ta);
Pekka Pessi's avatar
Pekka Pessi committed
748 749 750 751

  return params;
}

752
#include <sofia-sip/sip_status.h>
Pekka Pessi's avatar
Pekka Pessi committed
753

754
/** Convert @soa error to a SIP response code and phrase. */ 
Pekka Pessi's avatar
Pekka Pessi committed
755
int soa_error_as_sip_response(soa_session_t *ss,
Pekka Pessi's avatar
Pekka Pessi committed
756 757
			      char const **return_phrase)
{
758 759 760
  SU_DEBUG_9(("soa_error_as_sip_response(%s::%p, ...) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

761
  if (ss == NULL || ss->ss_status < 400 || ss->ss_status >= 700) {
Pekka Pessi's avatar
Pekka Pessi committed
762 763 764 765 766 767
    if (return_phrase)
      *return_phrase = sip_500_Internal_server_error;
    return 500;
  }

  if (return_phrase)
768 769
    *return_phrase = ss->ss_phrase;
  return ss->ss_status;
Pekka Pessi's avatar
Pekka Pessi committed
770 771
}

772
/** Convert @soa error to a SIP @Reason header. */ 
Pekka Pessi's avatar
Pekka Pessi committed
773 774
char const *soa_error_as_sip_reason(soa_session_t *ss)
{
775 776 777 778
  char const *phrase;
  int status;
  char *reason;

779 780 781
  SU_DEBUG_9(("soa_error_as_sip_reason(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

782 783 784 785 786 787 788 789 790 791 792
  if (ss == NULL)
    return "SIP;cause=500;text=\"Internal Server Error\"";

  status = soa_error_as_sip_response(ss, &phrase);

  reason = su_sprintf(ss->ss_home, "SIP;cause=%u;text=\"%s\"", status, phrase);
  
  if (ss->ss_reason)
    su_free(ss->ss_home, reason);

  return ss->ss_reason = reason;
Pekka Pessi's avatar
Pekka Pessi committed
793 794
}

795

796
/** Return SIP @Warning code and text. */
797 798 799 800 801 802 803 804 805 806 807 808 809 810
int soa_get_warning(soa_session_t *ss, char const **return_text)
{
  if (!ss)
    return 0;

  if (!ss->ss_wcode)
    return 0;

  if (return_text)
    *return_text = ss->ss_warning;

  return ss->ss_wcode;
}

811
/** Return SDP description of capabilities.
812 813 814 815 816
 *
 * @param ss  pointer to @soa session
 * @param return_sdp      return value for capability SDP structure 
 * @param return_sdp_str  return value for capability SDP string
 * @param return_len  return value for length of capability SDP string
817 818 819 820
 *
 * @retval 0 if there is no description to return
 * @retval 1 if description is returned
 * @retval -1 upon an error
821 822 823 824
 *
 * @sa @RFC3261 section 11, soa_set_capability_sdp(), 
 * SOATAG_CAPS_SDP(), SOATAG_CAPS_SDP_STR(), 
 * nua_options(), #nua_i_options()
825
 */
826
int soa_get_capability_sdp(soa_session_t const *ss,
827 828
			   sdp_session_t const **return_sdp,
			   char const **return_sdp_str,
829
			   isize_t *return_len)
830
{
831 832
  sdp_session_t const *sdp;
  char const *sdp_str;
833

834 835 836
  SU_DEBUG_9(("soa_get_capability_sdp(%s::%p, [%p], [%p], [%p]) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss,
	      return_sdp, return_sdp_str, return_len));
837

838
  if (ss == NULL)
839
    return (void)su_seterrno(EFAULT), -1;
840

841 842
  sdp = ss->ss_caps->ssd_sdp;
  sdp_str = ss->ss_caps->ssd_str;
843 844 845 846 847

  if (sdp == NULL)
    return 0;
  if (return_sdp)
    *return_sdp = sdp;
848 849
  if (return_sdp_str)
    *return_sdp_str = sdp_str;
850
  if (return_len)
851
    *return_len = strlen(sdp_str);
852 853

  return 1;
Pekka Pessi's avatar
Pekka Pessi committed
854 855
}

856

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
/** Set capability SDP. 
 *
 * Capability SDP is used instead of user SDP when generating OPTIONS
 * responses describing media capabilities. 
 *
 * @param ss  pointer to @soa session
 * @param sdp pointer to SDP session structure
 * @param str pointer to string containing SDP session description
 * @param len lenght of string @a str
 *
 * @retval 1 when SDP is stored and it differs from previously stored
 * @retval 0 when SDP is identical to previously stored one (and user version
 *           returned by soa_get_user_version() is not incremented)
 * @retval -1 upon an error
 *
 * @sa @RFC3261 section 11, soa_get_capability_sdp(),
 * SOATAG_CAPS_SDP(), SOATAG_CAPS_SDP_STR(), 
 * nua_options(), #nua_i_options()
 */
876
int soa_set_capability_sdp(soa_session_t *ss, 
877
			   sdp_session_t const *sdp,
878
			   char const *str, issize_t len)
Pekka Pessi's avatar
Pekka Pessi committed
879
{
880
  SU_DEBUG_9(("soa_set_capability_sdp(%s::%p, %p, %p, "MOD_ZD") called\n",
881
	      ss ? ss->ss_actions->soa_name : "", ss, sdp, str, (ssize_t)len));
882

883
  return soa_set_sdp(ss, soa_capability_sdp_kind, sdp, str, len);
884 885
}

886 887 888
/** Set capabilities */
int 
soa_base_set_capability_sdp(soa_session_t *ss, 
889
			    sdp_session_t *sdp, char const *str0, isize_t len0)
890
{
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  sdp_origin_t o[1] = {{ sizeof(o) }};
  sdp_connection_t *c, c0[1] = {{ sizeof(c0) }};
  char c_address[64];
  sdp_time_t t[1] = {{ sizeof(t) }};
  sdp_media_t *m;

  if (sdp->sdp_origin)
    *o = *sdp->sdp_origin;
  else
    o->o_address = c0;

  sdp->sdp_origin = o;

  if (soa_init_sdp_origin(ss, o, c_address) < 0)
    return -1;

  if (!sdp->sdp_subject)
    sdp->sdp_subject = "-";

  sdp->sdp_time = t;

  /* Set port to zero - or should we check that port is already zero? */
  for (m = sdp->sdp_media; m; m = m->m_next)
    m->m_port = 0;

  c = sdp->sdp_origin->o_address;

  if (sdp->sdp_connection == NULL) {
    for (m = sdp->sdp_media; m; m = m->m_next)
      if (m->m_connections == NULL)
	break;
    if (m)
      sdp->sdp_connection = c;
  }

  return soa_description_set(ss, ss->ss_caps, sdp, str0, len0);
Pekka Pessi's avatar
Pekka Pessi committed
927 928
}

Pekka Pessi's avatar
Pekka Pessi committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
/**Return user SDP description.
 *
 * <i>User SDP</i> is used as basis for SDP Offer/Answer negotiation. It can
 * be very minimal template, consisting just m= line containing media name,
 * transport protocol port number and list of supported codecs.
 *
 * The SDP used as an offer or answer (generated by soa_generate_answer() or
 * soa_generate_offer()) is known as <i>local SDP</i> and it is available
 * with soa_get_local_sdp() or SOATAG_LOCAL_SDP()/SOATAG_LOCAL_SDP_STR()
 * tags.
 *
 * @param ss  pointer to @soa session
 * @param return_sdp SDP  session structure return value
 * @param return_sdp_str  return value for pointer to string 
 *                        containing the user SDP session description
944
 * @param return_len  return value for user SDP session description string
Pekka Pessi's avatar
Pekka Pessi committed
945 946 947 948
 *                    length 
 *
 * Any of the parameters @a return_sdp, @a return_sdp_str, or @a return_len 
 * may be NULL.
Pekka Pessi's avatar
Pekka Pessi committed
949
 *
Pekka Pessi's avatar
Pekka Pessi committed
950 951 952
 * @retval 0 if there is no description to return
 * @retval 1 if description is returned
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
953 954 955 956
 *
 * @sa soa_get_local_sdp(), soa_set_user_sdp(), soa_get_user_version(),
 * SOATAG_USER_SDP(), SOATAG_USER_SDP_STR(), soa_get_remote_sdp(),
 * soa_get_capability_sdp()
Pekka Pessi's avatar
Pekka Pessi committed
957
 */
958
int soa_get_user_sdp(soa_session_t const *ss,
959 960
		     sdp_session_t const **return_sdp,
		     char const **return_sdp_str,
961
		     isize_t *return_len)
Pekka Pessi's avatar
Pekka Pessi committed
962
{
963
  sdp_session_t const *sdp;
964
  char const *sdp_str;
Pekka Pessi's avatar
Pekka Pessi committed
965

966 967 968
  SU_DEBUG_9(("soa_get_user_sdp(%s::%p, [%p], [%p], [%p]) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss,
	      return_sdp, return_sdp_str, return_len));
969

970
  if (ss == NULL)
971
    return (void)su_seterrno(EFAULT), -1;
972

973 974
  sdp = ss->ss_user->ssd_sdp;
  sdp_str = ss->ss_user->ssd_str;
Pekka Pessi's avatar
Pekka Pessi committed
975

976
  if (sdp == NULL)
977 978
    return 0;
  if (return_sdp)
979 980 981
    *return_sdp = sdp;
  if (return_sdp_str)
    *return_sdp_str = sdp_str;
982
  if (return_len)
983
    *return_len = strlen(sdp_str);
984 985 986

  return 1;
}
Pekka Pessi's avatar
Pekka Pessi committed
987

Pekka Pessi's avatar
Pekka Pessi committed
988 989 990 991 992 993 994 995 996 997
/**Returns the version number of user session description. The version
 * numbering starts from zero and it is incremented each time
 * soa_set_user_sdp() or soa_set_params() modifies user SDP.
 *
 * @param ss  pointer to @soa session

 * @return Sequence number of user SDP.
 *
 * @sa soa_set_user_sdp(), soa_get_user_sdp(), soa_set_params(),
 * SOATAG_USER_SDP(), SOATAG_USER_SDP_STR()
998
 */
999
int soa_get_user_version(soa_session_t const *ss)
1000 1001
{
  assert(ss != NULL);
1002
  return ss ? (int)ss->ss_user_version : -1;
1003 1004
} 

Pekka Pessi's avatar
Pekka Pessi committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
/**Store user SDP to soa session. 
 *
 * User SDP is used as basis for SDP Offer/Answer negotiation. It can be
 * very minimal, consisting just m= line containing media name, transport
 * protocol port number and list of supported codecs.
 *
 * The SDP used as an offer or answer (generated by soa_generate_answer() or
 * soa_generate_offer()) is known as <i>local SDP</i> and it is available
 * with soa_get_local_sdp() or SOATAG_LOCAL_SDP()/SOATAG_LOCAL_SDP_STR()
 * tags.
 *
 * @param ss  pointer to @soa session
 * @param sdp pointer to SDP session structure
 * @param str pointer to string containing SDP session description
 * @param len lenght of string @a str
 *
 * Either @a sdp or @a str must be non-NULL. If @a len is -1, length of
 * string @a str is calculated using strlen().
 *
 * @retval 1 when SDP is stored and it differs from previously stored
 * @retval 0 when SDP is identical to previously stored one (and user version
 *           returned by soa_get_user_version() is not incremented)
 * @retval -1 upon an error
 *
 * @sa soa_get_user_sdp(), soa_get_user_version(), soa_set_params(),
 * SOATAG_USER_SDP(), SOATAG_USER_SDP_STR(), soa_generate_offer(),
 * soa_generate_answer(), soa_get_local_sdp(), soa_set_capability_sdp(),
 * soa_set_remote_sdp()
 */
1034 1035
int soa_set_user_sdp(soa_session_t *ss, 
		     sdp_session_t const *sdp,
1036
		     char const *str, issize_t len)
1037
{
1038
  SU_DEBUG_9(("soa_set_user_sdp(%s::%p, %p, %p, "MOD_ZD") called\n",
1039 1040
	      ss ? ss->ss_actions->soa_name : "", ss, sdp, str, (ssize_t)len));

1041
  return soa_set_sdp(ss, soa_user_sdp_kind, sdp, str, len);
1042
}
Pekka Pessi's avatar
Pekka Pessi committed
1043

1044 1045
/** Set user SDP (base version). */
int soa_base_set_user_sdp(soa_session_t *ss, 
1046
			  sdp_session_t *sdp, char const *str0, isize_t len0)
1047
{
1048 1049
  ++ss->ss_user_version;
  return soa_description_set(ss, ss->ss_user, sdp, str0, len0);
1050
}
Pekka Pessi's avatar
Pekka Pessi committed
1051

Pekka Pessi's avatar
Pekka Pessi committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/**Return remote SDP description of the session.
 *
 * <i>Remote SDP</i> is used, together with <i>User SDP</i> as basis for SDP
 * Offer/Answer negotiation.
 *
 * @param ss  pointer to @soa session
 * @param return_sdp SDP  session structure return value
 * @param return_sdp_str  return value for pointer to string 
 *                        containing the user SDP session description
 * @param return_len  return value for user SDP session descrioption string
 *                    length 
 *
 * Any of the parameters @a return_sdp, @a return_sdp_str, or @a return_len 
 * may be NULL.
1066 1067 1068 1069
 *
 * @retval 0 if there is no description to return
 * @retval 1 if description is returned
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
1070 1071 1072 1073
 *
 * @sa soa_set_remote_sdp(), soa_get_remote_version(), soa_get_params(),
 * soa_get_paramlist(), SOATAG_REMOTE_SDP(), SOATAG_REMOTE_SDP_STR(),
 * soa_get_local_sdp(), soa_get_user_sdp(), soa_get_capability_sdp().
1074
 */
1075
int soa_get_remote_sdp(soa_session_t const *ss,
1076 1077
		       sdp_session_t const **return_sdp,
		       char const **return_sdp_str,
1078
		       isize_t *return_len)
1079
{
1080 1081
  sdp_session_t const *sdp;
  char const *sdp_str;
Pekka Pessi's avatar
Pekka Pessi committed
1082

1083 1084 1085
  SU_DEBUG_9(("soa_get_remote_sdp(%s::%p, [%p], [%p], [%p]) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss,
	      return_sdp, return_sdp_str, return_len));
1086

1087
  if (ss == NULL)
1088
    return (void)su_seterrno(EFAULT), -1;
Pekka Pessi's avatar
Pekka Pessi committed
1089

1090 1091
  sdp = ss->ss_remote->ssd_sdp;
  sdp_str = ss->ss_remote->ssd_str;
Pekka Pessi's avatar
Pekka Pessi committed
1092

1093 1094 1095 1096
  if (sdp == NULL)
    return 0;
  if (return_sdp)
    *return_sdp = sdp;
1097 1098
  if (return_sdp_str)
    *return_sdp_str = sdp_str;
1099
  if (return_len)
1100
    *return_len = strlen(sdp_str);
1101 1102 1103 1104

  return 1;
}

Pekka Pessi's avatar
Pekka Pessi committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
/**Returns the version number of remote session description. The version
 * numbering starts from zero and it is incremented each time
 * soa_set_remote_sdp() or soa_set_params() modifies remote SDP.
 *
 * @param ss  pointer to @soa session

 * @return Sequence number of remote SDP.
 *
 * @sa soa_set_remote_sdp(), soa_get_remote_sdp(), soa_set_params(),
 * SOATAG_REMOTE_SDP(), SOATAG_REMOTE_SDP_STR()
1115 1116 1117 1118
 */
int soa_get_remote_version(soa_session_t const *ss)
{
  assert(ss != NULL);
1119
  return ss->ss_remote_version;
1120
} 
1121

Pekka Pessi's avatar
Pekka Pessi committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
/** Set remote SDP (offer or answer).
 *
 * <i>Remote SDP</i> is an SDP offer or answer received from the remote end. 
 * It is used together with <i>User SDP</i> as basis for SDP Offer/Answer
 * negotiation in soa_generate_answer() or soa_process_answer(). Remote SDP
 * can be set using soa_set_params() and SOATAG_REMOTE_SDP() or
 * SOATAG_REMOTE_SDP_STR() tags, too.
 *
 * If the SDP Offer/Answer negotiation step cannot be completed and the
 * received remote offer or answer should be ignored, call
 * soa_clear_remote_sdp().
 *
 * @param ss  pointer to @soa session
 * @param sdp pointer to SDP session structure
 * @param str pointer to string containing SDP session description
 * @param len lenght of string @a str
 *
 * Either @a sdp or @a str must be non-NULL. If @a len is -1, length of
 * string @a str is calculated using strlen().
 *
 * @retval 1 when SDP is stored and it differs from previously stored
 * @retval 0 when SDP is identical to previously stored one (and remote version
 *           returned by soa_get_remote_version() is not incremented)
 * @retval -1 upon an error
 *
 * @sa soa_has_received_sdp(), soa_get_remote_sdp(),
 * soa_get_remote_version(), soa_set_params(), SOATAG_REMOTE_SDP(),
 * SOATAG_REMOTE_SDP_STR(), soa_generate_answer(), soa_process_answer(),
 * soa_clear_remote_sdp(), soa_init_offer_answer(), soa_get_local_sdp(),
 * soa_set_user_sdp(), soa_set_capability_sdp().
 */
1153
int soa_set_remote_sdp(soa_session_t *ss, 
1154
		       sdp_session_t const *sdp,
1155
		       char const *str, issize_t len)
1156
{
1157
  SU_DEBUG_9(("soa_set_remote_sdp(%s::%p, %p, %p, "MOD_ZD") called\n",
1158 1159
	      ss ? ss->ss_actions->soa_name : "", ss, sdp, str, (ssize_t)len));

1160
  return soa_set_sdp(ss, soa_remote_sdp_kind, sdp, str, len);
1161 1162
}

Pekka Pessi's avatar
Pekka Pessi committed
1163
/** Base method for setting the remote SDP (offer or answer). */
1164 1165
int soa_base_set_remote_sdp(soa_session_t *ss,
			    int new_version,
1166
			    sdp_session_t *sdp, char const *str0, isize_t len0)
1167
{
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
  /* This is cleared in soa_generate_answer() or soa_process_answer(). */
  ss->ss_unprocessed_remote = 1;

  if (!new_version)
    return 0;
    
  soa_set_activity(ss, sdp->sdp_media, 1);

  ss->ss_remote_version++;
  
  return soa_description_set(ss, ss->ss_remote, sdp, str0, len0);
Pekka Pessi's avatar
Pekka Pessi committed
1179 1180
}

Pekka Pessi's avatar
Pekka Pessi committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
/** Clear remote SDP.
 *
 * Remote SDP (offer or answer) should be cleared after a it has been stored
 * in the SDP session object using soa_set_remote_sdp() or soa_set_params(),
 * but the SDP Offer/Answer negotiation step (soa_generate_answer() or
 * soa_process_answer()) cannot be completed.
 *
 * @param ss  pointer to @soa session
 *
 * @retval 0  when successful
 * @retval -1 upon an error
 *
 * @sa soa_init_offer_answer(), soa_set_remote_sdp(),
 * soa_has_received_sdp(), soa_set_params(), SOATAG_REMOTE_SDP(),
 * SOATAG_REMOTE_SDP_STR(), soa_generate_answer(), soa_process_answer(),
 * soa_process_reject().
 */
1198 1199
int soa_clear_remote_sdp(soa_session_t *ss)
{
1200 1201 1202
  SU_DEBUG_9(("soa_clear_remote_sdp(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

1203
  if (!ss)
1204
    return (void)su_seterrno(EFAULT), -1;
1205 1206 1207 1208 1209 1210

  ss->ss_unprocessed_remote = 0;

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
/** Check if remote SDP has been saved but it has not been processed.
 *
 * @sa soa_init_offer_answer(), soa_set_remote_sdp(), soa_generate_answer(),
 * soa_process_answer(), soa_clear_remote_sdp().
 */
int soa_has_received_sdp(soa_session_t const *ss)
{
  return ss && ss->ss_unprocessed_remote;
}


/**Get local SDP.
 *
 * The <i>local SDP</i> is used as an offer or answer and it is generated by
 * soa_generate_offer() or soa_generate_answer(), respectively. It can be
 * retrieved using soa_get_params() or soa_get_paramlist() with
 * SOATAG_LOCAL_SDP() or SOATAG_LOCAL_SDP_STR() tags.
 *
 * @param ss  pointer to @soa session
 * @param return_sdp SDP  session structure return value
 * @param return_sdp_str  return value for pointer to string 
 *                        containing the user SDP session description
 * @param return_len  return value for user SDP session descrioption string
 *                    length 
 *
 * Any of the parameters @a return_sdp, @a return_sdp_str, or @a return_len 
 * may be NULL.
 *
 * @retval 0 if there is no description to return
 * @retval 1 if description is returned
 * @retval -1 upon an error
1242
 *
Pekka Pessi's avatar
Pekka Pessi committed
1243 1244 1245
 * @sa soa_generate_offer(), soa_generate_answer(), soa_get_params(),
 * soa_get_paramlist(), SOATAG_LOCAL_SDP(), SOATAG_LOCAL_SDP_STR(),
 * soa_get_user_sdp(), soa_get_remote_sdp(), soa_get_capability_sdp().
1246
 */
1247
int soa_get_local_sdp(soa_session_t const *ss,
1248 1249
		      sdp_session_t const **return_sdp,
		      char const **return_sdp_str,
1250
		      isize_t *return_len)
1251
{
1252 1253
  sdp_session_t const *sdp;
  char const *sdp_str;
1254

1255 1256 1257
  SU_DEBUG_9(("soa_get_local_sdp(%s::%p, [%p], [%p], [%p]) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss,
	      return_sdp, return_sdp_str, return_len));
1258

1259
  if (ss == NULL)
1260
    return (void)su_seterrno(EFAULT), -1;
1261

1262 1263
  sdp = ss->ss_local->ssd_sdp;
  sdp_str = ss->ss_local->ssd_str;
1264 1265 1266 1267 1268

  if (sdp == NULL)
    return 0;
  if (return_sdp)
    *return_sdp = sdp;
1269 1270
  if (return_sdp_str)
    *return_sdp_str = sdp_str;
1271
  if (return_len)
1272
    *return_len = strlen(sdp_str);
1273 1274 1275

  return 1;
}
Pekka Pessi's avatar
Pekka Pessi committed
1276

Pekka Pessi's avatar
Pekka Pessi committed
1277 1278 1279 1280 1281 1282 1283
/**Initialize the offer/answer state machine.
 *
 * @param ss  pointer to @soa session
 *
 * @retval 0  when successful
 * @retval -1 upon an error
 */
Pekka Pessi's avatar
Pekka Pessi committed
1284 1285 1286 1287
int soa_init_offer_answer(soa_session_t *ss)
{
  int complete;

1288 1289 1290
  SU_DEBUG_9(("soa_init_offer_answer(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

Pekka Pessi's avatar
Pekka Pessi committed
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
  if (!ss)
    return 0;

  complete = ss->ss_complete;

  ss->ss_complete = 0;
  ss->ss_offer_sent = 0;
  ss->ss_offer_recv = 0;
  ss->ss_answer_sent = 0;
  ss->ss_answer_recv = 0;

1302 1303
  ss->ss_unprocessed_remote = 0;

Pekka Pessi's avatar
Pekka Pessi committed
1304 1305 1306
  return complete;
}

Pekka Pessi's avatar
Pekka Pessi committed
1307
/** Return list of media fetures. */
Pekka Pessi's avatar
Pekka Pessi committed
1308 1309
char **soa_media_features(soa_session_t *ss, int live, su_home_t *home)
{
1310 1311 1312
  SU_DEBUG_9(("soa_media_features(%s::%p, %u, %p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss, live, home));

Pekka Pessi's avatar
Pekka Pessi committed
1313
  if (ss)
Pekka Pessi's avatar
Pekka Pessi committed
1314 1315
    return ss->ss_actions->soa_media_features(ss, live, home);
  else
1316
    return (void)su_seterrno(EFAULT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1317 1318
}

Pekka Pessi's avatar
Pekka Pessi committed
1319
char **soa_base_media_features(soa_session_t *ss, int live, su_home_t *home)
Pekka Pessi's avatar
Pekka Pessi committed
1320 1321 1322 1323
{
  return su_zalloc(home, 8 * sizeof (char **));
}

1324
char const * const * soa_sip_require(soa_session_t const *ss)
Pekka Pessi's avatar
Pekka Pessi committed
1325
{
1326 1327 1328
  SU_DEBUG_9(("soa_sip_require(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

Pekka Pessi's avatar
Pekka Pessi committed
1329
  if (ss)
1330
    return ss->ss_actions->soa_sip_require(ss);
Pekka Pessi's avatar
Pekka Pessi committed
1331
  else
1332
    return (void)su_seterrno(EFAULT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1333 1334
}

1335
char const * const * soa_base_sip_require(soa_session_t const *ss)
Pekka Pessi's avatar
Pekka Pessi committed
1336 1337 1338 1339 1340
{
  static char const *null = NULL;
  return &null;
}

1341
char const * const * soa_sip_supported(soa_session_t const *ss)
Pekka Pessi's avatar
Pekka Pessi committed
1342
{
1343 1344 1345
  SU_DEBUG_9(("soa_sip_supported(%s::%p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss));

Pekka Pessi's avatar
Pekka Pessi committed
1346
  if (ss)
1347
    return ss->ss_actions->soa_sip_supported(ss);
Pekka Pessi's avatar
Pekka Pessi committed
1348
  else
1349
    return (void)su_seterrno(EFAULT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1350 1351
}

1352
char const * const * soa_base_sip_supported(soa_session_t const *ss)
Pekka Pessi's avatar
Pekka Pessi committed
1353 1354 1355 1356 1357
{
  static char const *null = NULL;
  return &null;
}

Pekka Pessi's avatar
Pekka Pessi committed
1358
int soa_remote_sip_features(soa_session_t *ss,
1359 1360
			    char const * const * supported,
			    char const * const * require)
Pekka Pessi's avatar
Pekka Pessi committed
1361
{
1362 1363 1364
  SU_DEBUG_9(("soa_remote_sip_features(%s::%p, %p, %p) called\n",
	      ss ? ss->ss_actions->soa_name : "", ss, supported, require));

Pekka Pessi's avatar
Pekka Pessi committed
1365
  if (ss)
1366
    return ss->ss_actions->soa_remote_sip_features(ss, supported, require);
Pekka Pessi's avatar
Pekka Pessi committed
1367
  else
1368
    return (void)su_seterrno(EFAULT), -1;
Pekka Pessi's avatar
Pekka Pessi committed
1369 1370
}

Pekka Pessi's avatar
Pekka Pessi committed
1371
int soa_base_remote_sip_features(soa_session_t *ss,
1372 1373
				    char const * const * supported,
				    char const * const * require)
Pekka Pessi's avatar
Pekka Pessi committed
1374 1375 1376 1377 1378
{
  return 0;
}


Pekka Pessi's avatar
Pekka Pessi committed
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
/**Generate offer.
 *
 * When generating the offer the user SDP is augmented with the required SDP
 * lines (v=, o=, t=, c=, a=rtpmap, etc.).
 *
 * The resulting SDP is also known as <i>local SDP</i>. It is available with
 * soa_get_local_sdp() or with soa_get_params() and soa_get_paramlist() tags
 * SOATAG_LOCAL_SDP() or SOATAG_LOCAL_SDP_STR().
 *
 * The user SDP has been stored to the soa session with soa_set_user_sdp()
 * or with soa_set_params() tags SOATAG_USER_SDP() or SOATAG_USER_SDP_STR(). 
 * There are various other parameters directing the generation of offer, set
 * by soa_set_params().
Pekka Pessi's avatar
Pekka Pessi committed
1392 1393 1394 1395
 *
 * @param ss pointer to session object
 * @param always always send offer (even if offer/answer has been completed)
 * @param completed pointer to callback function which is invoked when
Pekka Pessi's avatar
Pekka Pessi committed
1396
 *                  operation is completed (currently not in use)
Pekka Pessi's avatar
Pekka Pessi committed
1397 1398
 *
 * @retval 1 when operation is successful
Pekka Pessi's avatar
Pekka Pessi committed
1399
 * @retval 0 when operation was not needed
Pekka Pessi's avatar
Pekka Pessi committed
1400
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
1401
 *
Pekka Pessi's avatar
Pekka Pessi committed
1402 1403
 * @ERRORS
 */
1404 1405 1406
int soa_generate_offer(soa_session_t *ss,
		       int always,
		       soa_callback_f *completed)
Pekka Pessi's avatar
Pekka Pessi committed
1407
{
Pekka Pessi's avatar