soa.c 62.5 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 */
73
su_inline int soa_media_is_ready(soa_session_t const *ss);
Pekka Pessi's avatar
Pekka Pessi committed
74

75 76
enum soa_sdp_kind { 
  soa_capability_sdp_kind,
77
  soa_user_sdp_kind,
78 79 80 81 82 83
  soa_remote_sdp_kind
};

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

Pekka Pessi's avatar
Pekka Pessi committed
86 87 88
/* ======================================================================== */

#define SOA_VALID_ACTIONS(a)					\
89 90
  ((a)->sizeof_soa_session_actions >= (int)sizeof (*actions) && \
   (a)->sizeof_soa_session >= (int)sizeof(soa_session_t) &&     \
Pekka Pessi's avatar
Pekka Pessi committed
91
   (a)->soa_name != NULL &&					\
Pekka Pessi's avatar
Pekka Pessi committed
92
   (a)->soa_init != NULL &&					\
93
   (a)->soa_deinit != NULL &&					\
Pekka Pessi's avatar
Pekka Pessi committed
94 95
   (a)->soa_set_params != NULL &&				\
   (a)->soa_get_params != NULL &&				\
96 97 98 99 100 101 102 103
   (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
104 105
   (a)->soa_generate_offer != NULL &&				\
   (a)->soa_generate_answer != NULL &&				\
106 107
   (a)->soa_process_answer != NULL &&				\
   (a)->soa_process_reject != NULL &&				\
Pekka Pessi's avatar
Pekka Pessi committed
108
   (a)->soa_activate_session != NULL &&				\
109
   (a)->soa_deactivate_session != NULL &&			\
Pekka Pessi's avatar
Pekka Pessi committed
110 111 112 113 114 115 116 117 118 119
   (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
120
 *
121
 * @sa <sofia-sip/su_debug.h>, su_log_global, SOFIA_DEBUG
Pekka Pessi's avatar
Pekka Pessi committed
122 123 124 125 126 127 128
 */
extern char const SOA_DEBUG[];

#ifndef SU_DEBUG
#define SU_DEBUG 3
#endif

Pekka Pessi's avatar
Pekka Pessi committed
129 130
/**Debug log for @b soa module.
 *
Pekka Pessi's avatar
Pekka Pessi committed
131 132 133 134 135
 * 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) };

136 137 138
/* Add " around string */
#define NICE(s) s ? "\"" : "", s ? s : "(nil)", s ? "\"" : ""

Pekka Pessi's avatar
Pekka Pessi committed
139 140 141 142
/* ======================================================================== */

/* API Functions */

Pekka Pessi's avatar
Pekka Pessi committed
143
struct soa_namenode
Pekka Pessi's avatar
Pekka Pessi committed
144 145 146 147 148 149 150 151
{
  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
152
static struct soa_namenode const soa_default_node =
Pekka Pessi's avatar
Pekka Pessi committed
153 154 155 156 157 158 159
  {
    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
160 161
int soa_add(char const *name,
	    struct soa_session_actions const *actions)
Pekka Pessi's avatar
Pekka Pessi committed
162 163 164 165
{
  struct soa_namenode const *n;
  struct soa_namenode *e;

166
  SU_DEBUG_9(("soa_add(%s%s%s, %p) called\n", NICE(name), (void *)actions));
167

Pekka Pessi's avatar
Pekka Pessi committed
168
  if (name == NULL || actions == NULL)
169
    return su_seterrno(EFAULT);
Pekka Pessi's avatar
Pekka Pessi committed
170 171

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

Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  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
190 191 192
/** Search for a named backend */
struct soa_session_actions const *soa_find(char const *name)
{
193 194
  SU_DEBUG_9(("soa_find(%s%s%s) called\n", NICE(name)));

Pekka Pessi's avatar
Pekka Pessi committed
195 196 197 198 199 200 201 202 203 204
  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)
205
      return (void)su_seterrno(ENOENT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
206 207 208 209 210 211 212

    return n->actions;
  }

  return NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
213 214
/* ======================================================================== */

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

  soa_session_t *ss;
223
  size_t namelen;
Pekka Pessi's avatar
Pekka Pessi committed
224

225
  SU_DEBUG_9(("soa_create(\"%s\", %p, %p) called\n",
226
	      name ? name : "default", (void *)root, (void *)magic));
227

228
  if (name && name[0]) {
Pekka Pessi's avatar
Pekka Pessi committed
229 230 231 232 233 234 235 236
    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)
237
      return (void)su_seterrno(ENOENT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
238 239 240

    actions = n->actions; assert(actions);
  }
241 242
  else
    name = "default";
Pekka Pessi's avatar
Pekka Pessi committed
243 244 245 246

  assert(SOA_VALID_ACTIONS(actions));

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

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

    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
260 261 262 263 264
  }

  return ss;
}

265
/** Create a copy of a @soa session object. */
Pekka Pessi's avatar
Pekka Pessi committed
266 267 268
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
269
{
Pekka Pessi's avatar
Pekka Pessi committed
270
  soa_session_t *ss;
271
  size_t namelen;
Pekka Pessi's avatar
Pekka Pessi committed
272

273 274
  SU_DEBUG_9(("soa_clone(%s::%p, %p, %p) called\n",
	      parent_ss ? parent_ss->ss_actions->soa_name : "",
275
	      (void *)parent_ss, (void *)root, (void *)magic));
276

Pekka Pessi's avatar
Pekka Pessi committed
277
  if (parent_ss == NULL || root == NULL)
278
    return (void)su_seterrno(EFAULT), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
279

280 281 282
  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
283 284 285 286
  if (ss) {
    ss->ss_root = root;
    ss->ss_magic = magic;
    ss->ss_actions = parent_ss->ss_actions;
287 288
    ss->ss_name = strcpy((char *)ss + ss->ss_actions->sizeof_soa_session,
			 parent_ss->ss_name);
Pekka Pessi's avatar
Pekka Pessi committed
289 290 291

    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
292
  }
Pekka Pessi's avatar
Pekka Pessi committed
293 294 295 296 297 298 299

  return ss;
}

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

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

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

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

    DUP(ss->ss_address, su_strdup, parent->ss_address);
    ss->ss_af = parent->ss_af;
333
    DUP(ss->ss_hold, su_strdup, parent->ss_hold);
Pekka Pessi's avatar
Pekka Pessi committed
334 335

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

Pekka Pessi's avatar
Pekka Pessi committed
337 338 339 340 341 342 343 344 345 346 347
    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)
{
348
  SU_DEBUG_9(("soa_destroy(%s::%p) called\n",
349
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));
350

Pekka Pessi's avatar
Pekka Pessi committed
351
  if (ss) {
Pekka Pessi's avatar
Pekka Pessi committed
352 353
    ss->ss_active = 0;
    ss->ss_terminated++;
Pekka Pessi's avatar
Pekka Pessi committed
354
    ss->ss_actions->soa_deinit(ss);
Pekka Pessi's avatar
Pekka Pessi committed
355
    su_home_unref(ss->ss_home);
Pekka Pessi's avatar
Pekka Pessi committed
356 357 358
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
359
void soa_base_deinit(soa_session_t *ss)
Pekka Pessi's avatar
Pekka Pessi committed
360 361 362 363
{
  (void)ss;
}

364 365 366 367 368 369
/** 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
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
 *
 * @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().
388
 */
389
int soa_set_params(soa_session_t *ss, tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
390 391
{
  ta_list ta;
392
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
393

394
  SU_DEBUG_9(("soa_set_params(%s::%p, ...) called\n",
395
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));
396

Pekka Pessi's avatar
Pekka Pessi committed
397
  if (ss == NULL)
398
    return su_seterrno(EFAULT), -1;
Pekka Pessi's avatar
Pekka Pessi committed
399 400 401 402 403 404 405

  ta_start(ta, tag, value);

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

  ta_end(ta);

406
  return n;
Pekka Pessi's avatar
Pekka Pessi committed
407 408
}

409 410 411 412 413 414
/**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
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
 *
 * @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().
432
 */
433
int soa_base_set_params(soa_session_t *ss, tagi_t const *tags)
Pekka Pessi's avatar
Pekka Pessi committed
434
{
435
  int n, change_session = 0;
Pekka Pessi's avatar
Pekka Pessi committed
436

437
  sdp_session_t const *caps_sdp, *user_sdp;
438
  char const *caps_sdp_str, *user_sdp_str;
439

440
  int af;
441
  char const *media_address, *hold;
442
  int rtp_select, rtp_sort;
443
  int rtp_mismatch;
Pekka Pessi's avatar
Pekka Pessi committed
444 445 446 447
  int srtp_enable, srtp_confidentiality, srtp_integrity;

  af = ss->ss_af;

448
  hold = ss->ss_hold;
Pekka Pessi's avatar
Pekka Pessi committed
449 450
  media_address = ss->ss_address;

451 452
  rtp_select = (int)ss->ss_rtp_select;
  rtp_sort = (int)ss->ss_rtp_sort;
453 454
  rtp_mismatch = ss->ss_rtp_mismatch;

Pekka Pessi's avatar
Pekka Pessi committed
455 456 457 458
  srtp_enable = ss->ss_srtp_enable;
  srtp_confidentiality = ss->ss_srtp_confidentiality;
  srtp_integrity = ss->ss_srtp_integrity;

459 460 461
  caps_sdp = user_sdp = NONE;
  caps_sdp_str = user_sdp_str = NONE;

Pekka Pessi's avatar
Pekka Pessi committed
462
  n = tl_gets(tags,
463 464

	      SOATAG_CAPS_SDP_REF(caps_sdp),
Pekka Pessi's avatar
Pekka Pessi committed
465 466
	      SOATAG_CAPS_SDP_STR_REF(caps_sdp_str),

467 468
	      SOATAG_USER_SDP_REF(user_sdp),
	      SOATAG_USER_SDP_STR_REF(user_sdp_str),
469

Pekka Pessi's avatar
Pekka Pessi committed
470 471
	      SOATAG_AF_REF(af),
	      SOATAG_ADDRESS_REF(media_address),
472
	      SOATAG_HOLD_REF(hold),
Pekka Pessi's avatar
Pekka Pessi committed
473

474 475 476 477
	      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
478 479 480
	      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
481

Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486
	      TAG_END());

  if (n <= 0)
    return n;

487 488 489 490 491
  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) {
492
      if (soa_set_capability_sdp(ss, caps_sdp, caps_sdp_str, -1) < 0) {
493
	return -1;
Pekka Pessi's avatar
Pekka Pessi committed
494 495
      }
    }
496 497 498
    else {
      soa_description_free(ss, ss->ss_caps);
    }
Pekka Pessi's avatar
Pekka Pessi committed
499 500
  }

501 502 503
  if (user_sdp != NONE || user_sdp_str != NONE) {
    if (user_sdp == NONE) user_sdp = NULL;
    if (user_sdp_str == NONE) user_sdp_str = NULL;
504

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

517 518 519 520
  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)
521
    rtp_select = (int)ss->ss_rtp_select;
522
  if (rtp_sort < SOA_RTP_SORT_DEFAULT || rtp_sort > SOA_RTP_SORT_REMOTE)
523
    rtp_sort = (int)ss->ss_rtp_sort;
524 525 526 527 528 529
  rtp_mismatch = rtp_mismatch != 0;

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

530
  change_session 
531
    =  af != (int)ss->ss_af
532 533
    || rtp_select != (int)ss->ss_rtp_select
    || rtp_sort != (int)ss->ss_rtp_sort
534 535 536 537 538
    || 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
    ;
539 540 541 542 543 544 545 546 547 548

  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
549 550 551 552

  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);
553
    change_session = 1;
Pekka Pessi's avatar
Pekka Pessi committed
554 555
  }

556 557
  if (hold == (char const *)1)
    hold = "*";
Pekka Pessi's avatar
Pekka Pessi committed
558

559 560 561
  if (str0casecmp(hold, ss->ss_hold)) {
    su_free(ss->ss_home, (void *)ss->ss_hold);
    ss->ss_hold = su_strdup(ss->ss_home, hold);
562
    change_session = 1;
Pekka Pessi's avatar
Pekka Pessi committed
563 564
  }

565 566 567
  if (change_session)
    ss->ss_user_version++;

568
  return n;
Pekka Pessi's avatar
Pekka Pessi committed
569 570
}

571 572 573 574 575 576
/** 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
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
 *
 * @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().
597
 */
598 599
int soa_get_params(soa_session_t const *ss,
		   tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
600 601
{
  ta_list ta;
602
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
603

604
  SU_DEBUG_9(("soa_get_params(%s::%p, ...) called\n",
605
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));
606

Pekka Pessi's avatar
Pekka Pessi committed
607
  if (ss == NULL)
608
    return su_seterrno(EFAULT), -1;
Pekka Pessi's avatar
Pekka Pessi committed
609 610 611 612 613 614 615

  ta_start(ta, tag, value);

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

  ta_end(ta);

616
  return n;
Pekka Pessi's avatar
Pekka Pessi committed
617 618
}

619 620 621 622 623 624
/**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
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
 *
 * @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().
644
 */
645
int soa_base_get_params(soa_session_t const *ss, tagi_t *tags)
Pekka Pessi's avatar
Pekka Pessi committed
646
{
647
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
648 649

  n = tl_tgets(tags,
650 651
	       SOATAG_CAPS_SDP(ss->ss_caps->ssd_sdp),
	       SOATAG_CAPS_SDP_STR(ss->ss_caps->ssd_str),
Pekka Pessi's avatar
Pekka Pessi committed
652

653 654
	       SOATAG_USER_SDP(ss->ss_user->ssd_sdp),
	       SOATAG_USER_SDP_STR(ss->ss_user->ssd_str),
655

656 657 658 659
	       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),
660
	       SOATAG_REMOTE_SDP_STR(ss->ss_remote->ssd_unparsed),
661

Pekka Pessi's avatar
Pekka Pessi committed
662 663
	       SOATAG_AF(ss->ss_af),
	       SOATAG_ADDRESS(ss->ss_address),
664
	       SOATAG_HOLD(ss->ss_hold),
Pekka Pessi's avatar
Pekka Pessi committed
665

666 667
	       SOATAG_RTP_SELECT((int)ss->ss_rtp_select),
	       SOATAG_RTP_SORT((int)ss->ss_rtp_sort),
668 669
	       SOATAG_RTP_MISMATCH(ss->ss_rtp_mismatch),

Pekka Pessi's avatar
Pekka Pessi committed
670 671 672 673 674 675 676 677 678
	       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;
}

679
/** Return a list of parameters. */
680 681
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
682
{
683 684
  ta_list ta;
  tagi_t *params = NULL;
685

686
  SU_DEBUG_9(("soa_get_paramlist(%s::%p, ...) called\n",
687
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));
688

689
  if (ss) {
690 691 692 693 694
    ta_start(ta, tag, value);
    params = ss->ss_actions->soa_get_paramlist(ss, ta_tags(ta));
    ta_end(ta);
  }

695
  return params;
Pekka Pessi's avatar
Pekka Pessi committed
696 697 698
}


699
/** Base bethod for getting list of parameters. */
700 701 702
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
703
{
704 705
  ta_list ta;
  tagi_t *params;
Pekka Pessi's avatar
Pekka Pessi committed
706 707 708

  if (ss == NULL)
    return NULL;
709 710
  
  ta_start(ta, tag, value);
Pekka Pessi's avatar
Pekka Pessi committed
711

712
  params = tl_llist(
713 714 715 716
		   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
717

718 719 720 721 722 723 724 725 726
		   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
727

728 729 730
		   TAG_IF(ss->ss_remote->ssd_sdp,
			  SOATAG_REMOTE_SDP(ss->ss_remote->ssd_sdp)),
		   TAG_IF(ss->ss_remote->ssd_str,
731
			  SOATAG_REMOTE_SDP_STR(ss->ss_remote->ssd_unparsed)),
732

Pekka Pessi's avatar
Pekka Pessi committed
733
		   SOATAG_AF(ss->ss_af),
734 735
		   TAG_IF(ss->ss_address, 
			  SOATAG_ADDRESS(ss->ss_address)),
Pekka Pessi's avatar
Pekka Pessi committed
736

Pekka Pessi's avatar
Pekka Pessi committed
737 738 739
		   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
740

741
		   ta_tags(ta));
Pekka Pessi's avatar
Pekka Pessi committed
742

743
  ta_end(ta);
Pekka Pessi's avatar
Pekka Pessi committed
744 745 746 747

  return params;
}

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

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

757
  if (ss == NULL || ss->ss_status < 400 || ss->ss_status >= 700) {
Pekka Pessi's avatar
Pekka Pessi committed
758 759 760 761 762 763
    if (return_phrase)
      *return_phrase = sip_500_Internal_server_error;
    return 500;
  }

  if (return_phrase)
764 765
    *return_phrase = ss->ss_phrase;
  return ss->ss_status;
Pekka Pessi's avatar
Pekka Pessi committed
766 767
}

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

775
  SU_DEBUG_9(("soa_error_as_sip_reason(%s::%p) called\n",
776
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));
777

778 779 780 781 782 783 784 785 786 787 788
  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
789 790
}

791

792
/** Return SIP @Warning code and text. */
793 794 795 796 797 798 799 800 801 802 803 804 805 806
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;
}

807
/** Return SDP description of capabilities.
808 809 810 811 812
 *
 * @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
813 814 815 816
 *
 * @retval 0 if there is no description to return
 * @retval 1 if description is returned
 * @retval -1 upon an error
817 818 819
 *
 * @sa @RFC3261 section 11, soa_set_capability_sdp(), 
 * SOATAG_CAPS_SDP(), SOATAG_CAPS_SDP_STR(), 
820
 * nua_options(), #nua_i_options
821
 */
822
int soa_get_capability_sdp(soa_session_t const *ss,
Pekka Pessi's avatar
Pekka Pessi committed
823
			   struct sdp_session_s const **return_sdp,
824
			   char const **return_sdp_str,
825
			   isize_t *return_len)
826
{
827 828
  sdp_session_t const *sdp;
  char const *sdp_str;
829

830
  SU_DEBUG_9(("soa_get_capability_sdp(%s::%p, [%p], [%p], [%p]) called\n",
831 832
	      ss ? ss->ss_actions->soa_name : "", (void *)ss,
	      (void *)return_sdp, (void *)return_sdp_str, (void *)return_len));
833

834
  if (ss == NULL)
835
    return (void)su_seterrno(EFAULT), -1;
836

837 838
  sdp = ss->ss_caps->ssd_sdp;
  sdp_str = ss->ss_caps->ssd_str;
839 840 841 842 843

  if (sdp == NULL)
    return 0;
  if (return_sdp)
    *return_sdp = sdp;
844 845
  if (return_sdp_str)
    *return_sdp_str = sdp_str;
846
  if (return_len)
847
    *return_len = strlen(sdp_str);
848 849

  return 1;
Pekka Pessi's avatar
Pekka Pessi committed
850 851
}

852

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/** 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(), 
870
 * nua_options(), #nua_i_options
871
 */
872
int soa_set_capability_sdp(soa_session_t *ss, 
Pekka Pessi's avatar
Pekka Pessi committed
873
			   struct sdp_session_s const *sdp,
874
			   char const *str, issize_t len)
Pekka Pessi's avatar
Pekka Pessi committed
875
{
876
  SU_DEBUG_9(("soa_set_capability_sdp(%s::%p, %p, %p, "MOD_ZD") called\n",
877
	      ss ? ss->ss_actions->soa_name : "", (void *)ss, (void *)sdp, (void *)str, (ssize_t)len));
878

879
  return soa_set_sdp(ss, soa_capability_sdp_kind, sdp, str, len);
880 881
}

882 883
/** Set capabilities */
int 
884 885 886
soa_base_set_capability_sdp(soa_session_t *ss,
			    sdp_session_t *_sdp,
			    char const *str0, isize_t len0)
887
{
888
  sdp_session_t sdp[1];
889 890 891 892 893 894
  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;

895 896
  *sdp = *_sdp;

897 898 899 900 901 902 903 904
  if (sdp->sdp_origin)
    *o = *sdp->sdp_origin;
  else
    o->o_address = c0;

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

905 906
  sdp->sdp_origin = o;

907
  if (!sdp->sdp_subject)
908
    sdp->sdp_subject = "-";	/* s=- */
909

910
  sdp->sdp_time = t;		/* t=0 0 */
911 912 913 914 915 916

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

  if (sdp->sdp_connection == NULL) {
917 918
    c = sdp->sdp_origin->o_address;

919 920 921 922 923 924 925 926
    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,
Pekka Pessi's avatar
Pekka Pessi committed
959
		     struct sdp_session_s const **return_sdp,
960
		     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
  SU_DEBUG_9(("soa_get_user_sdp(%s::%p, [%p], [%p], [%p]) called\n",
967 968
	      ss ? ss->ss_actions->soa_name : "", (void *)ss,
			  (void *)return_sdp, (void *)return_sdp_str, (void *)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
int soa_set_user_sdp(soa_session_t *ss, 
Pekka Pessi's avatar
Pekka Pessi committed
1035
		     struct sdp_session_s 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
	      ss ? ss->ss_actions->soa_name : "", (void *)ss, (void *)sdp, (void *)str, (ssize_t)len));
1040

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,
Pekka Pessi's avatar
Pekka Pessi committed
1076
		       struct sdp_session_s const **return_sdp,
1077
		       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
  SU_DEBUG_9(("soa_get_remote_sdp(%s::%p, [%p], [%p], [%p]) called\n",
1084 1085
	      ss ? ss->ss_actions->soa_name : "", (void *)ss,
			  (void *)return_sdp, (void *)return_sdp_str, (void *)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, 
Pekka Pessi's avatar
Pekka Pessi committed
1154
		       struct sdp_session_s 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
	      ss ? ss->ss_actions->soa_name : "", (void *)ss, (void *)sdp, (void *)str, (ssize_t)len));
1159

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
  /* This is cleared in soa_generate_answer() or soa_process_answer(). */
  ss->ss_unprocessed_remote = 1;

  if (!new_version)
    return 0;
    
1174
  soa_set_activity(ss, sdp->sdp_media, soa_activity_remote);
1175 1176 1177 1178

  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
  SU_DEBUG_9(("soa_clear_remote_sdp(%s::%p) called\n",
1201
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));
1202

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,
Pekka Pessi's avatar
Pekka Pessi committed
1248
		      struct sdp_session_s const **return_sdp,
1249
		      char const **return_sdp_str,
1250
		      isize_t *return_len)
1251
{
1252 1253
  sdp_session_t const *sdp;
  char const *sdp_str;
1254

1255
  SU_DEBUG_9(("soa_get_local_sdp(%s::%p, [%p], [%p], [%p]) called\n",
1256 1257
	      ss ? ss->ss_actions->soa_name : "", (void *)ss,
			  (void *)return_sdp, (void *)return_sdp_str, (void *)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
  SU_DEBUG_9(("soa_init_offer_answer(%s::%p) called\n",
1289
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));
1290

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
  SU_DEBUG_9(("soa_media_features(%s::%p, %u, %p) called\n",
1311
	      ss ? ss->ss_actions->soa_name : "", (void *)ss, live, (void *)home));
1312

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
  SU_DEBUG_9(("soa_sip_require(%s::%p) called\n",
1327
	      ss ? ss->ss_actions->soa_name : "", (void *)ss));