soa.c 37.3 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
/*
 * 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
31
 * $Date: 2005/09/29 18:35:22 $
Pekka Pessi's avatar
Pekka Pessi committed
32 33 34 35 36
 */

#include "config.h"

const char soa_c_id[] =
37
"$Id: soa.c,v 1.7 2005/09/29 18:35:22 ppessi Exp $";
Pekka Pessi's avatar
Pekka Pessi committed
38 39 40 41 42 43 44 45

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#include <su_tag_class.h>
Pekka Pessi's avatar
Pekka Pessi committed
46
#include <su_wait.h>
Pekka Pessi's avatar
Pekka Pessi committed
47 48 49 50 51 52 53 54 55 56

#include "soa.h"
const char soa_h_id[] = SOA_H;
#include "sdp.h"
#include "soa_session.h"
const char soa_session_h_id[] = SOA_SESSION_H;
#include "soa_add.h"
const char soa_add_h_id[] = SOA_ADD_H;

#include <su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
57
#include <su_localinfo.h>
58 59 60
#include <su_uniqueid.h>

#include <string0.h>
Pekka Pessi's avatar
Pekka Pessi committed
61 62 63

#define NONE ((void *)-1)
#define XXX assert(!"implemented")
64 65

typedef unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
66 67 68 69

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

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

static inline int soa_media_is_ready(soa_session_t const *ss);

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

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

Pekka Pessi's avatar
Pekka Pessi committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/* ======================================================================== */

#define SOA_VALID_ACTIONS(a)					\
  ((a)->sizeof_soa_session_actions >= sizeof (*actions) &&	\
   (a)->sizeof_soa_session >= sizeof(soa_session_t) &&		\
   (a)->soa_init != NULL &&					\
   (a)->soa_set_params != NULL &&				\
   (a)->soa_get_params != NULL &&				\
   (a)->soa_generate_offer != NULL &&				\
   (a)->soa_generate_answer != NULL &&				\
   (a)->soa_activate_session != NULL &&				\
   (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
108
 *
Pekka Pessi's avatar
Pekka Pessi committed
109 110 111 112 113 114 115 116
 * @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
117 118
/**Debug log for @b soa module.
 *
Pekka Pessi's avatar
Pekka Pessi committed
119 120 121 122 123 124 125 126 127
 * 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) };

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

/* API Functions */

Pekka Pessi's avatar
Pekka Pessi committed
128
struct soa_namenode
Pekka Pessi's avatar
Pekka Pessi committed
129 130 131 132 133 134 135 136
{
  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
137
static struct soa_namenode const soa_default_node =
Pekka Pessi's avatar
Pekka Pessi committed
138 139 140 141 142 143 144
  {
    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
145 146
int soa_add(char const *name,
	    struct soa_session_actions const *actions)
Pekka Pessi's avatar
Pekka Pessi committed
147 148 149 150 151 152 153 154 155
{
  struct soa_namenode const *n;
  struct soa_namenode *e;

  if (name == NULL || actions == NULL)
    return (errno = EFAULT), -1;

  if (!SOA_VALID_ACTIONS(actions))
    return (errno = EINVAL), -1;
Pekka Pessi's avatar
Pekka Pessi committed
156

Pekka Pessi's avatar
Pekka Pessi committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/** Search for a named backend */
struct soa_session_actions const *soa_find(char const *name)
{
  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)
      return (void)(errno = ENOENT), NULL;

    return n->actions;
  }

  return NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
194 195
/* ======================================================================== */

Pekka Pessi's avatar
Pekka Pessi committed
196 197 198
soa_session_t *soa_create(char const *name,
			  su_root_t *root,
			  soa_magic_t *magic)
Pekka Pessi's avatar
Pekka Pessi committed
199 200 201 202
{
  struct soa_session_actions const *actions = &soa_default_actions;

  soa_session_t *ss;
203
  size_t namelen;
Pekka Pessi's avatar
Pekka Pessi committed
204

205
  if (name && name[0]) {
Pekka Pessi's avatar
Pekka Pessi committed
206 207 208 209 210 211 212 213 214 215 216 217
    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)
      return (void)(errno = ENOENT), NULL;

    actions = n->actions; assert(actions);
  }
218 219
  else
    name = "default";
Pekka Pessi's avatar
Pekka Pessi committed
220 221 222 223 224 225

  assert(SOA_VALID_ACTIONS(actions));

  if (root == NULL)
    return (void)(errno = EFAULT), NULL;

226 227 228
  namelen = strlen(name) + 1;
  
  ss = su_home_new(actions->sizeof_soa_session + namelen);
Pekka Pessi's avatar
Pekka Pessi committed
229 230
  if (ss) {
    ss->ss_root = root;
Pekka Pessi's avatar
Pekka Pessi committed
231
    ss->ss_magic = magic;
Pekka Pessi's avatar
Pekka Pessi committed
232
    ss->ss_actions = actions;
233
    ss->ss_name = strcpy((char *)ss + actions->sizeof_soa_session, name);
Pekka Pessi's avatar
Pekka Pessi committed
234 235 236

    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
237 238 239 240 241
  }

  return ss;
}

Pekka Pessi's avatar
Pekka Pessi committed
242 243 244
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
245
{
Pekka Pessi's avatar
Pekka Pessi committed
246
  soa_session_t *ss;
247
  size_t namelen;
Pekka Pessi's avatar
Pekka Pessi committed
248

Pekka Pessi's avatar
Pekka Pessi committed
249
  if (parent_ss == NULL || root == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
250 251
    return (void)(errno = EFAULT), NULL;

252 253 254
  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
255 256 257 258
  if (ss) {
    ss->ss_root = root;
    ss->ss_magic = magic;
    ss->ss_actions = parent_ss->ss_actions;
259 260
    ss->ss_name = strcpy((char *)ss + ss->ss_actions->sizeof_soa_session,
			 parent_ss->ss_name);
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263

    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
264
  }
Pekka Pessi's avatar
Pekka Pessi committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278

  return ss;
}

/** Increase reference count */
soa_session_t *soa_session_ref(soa_session_t *ss)
{
  return su_home_ref(ss->ss_home);
}

/** Decrease reference count */
void soa_session_unref(soa_session_t *ss)
{
  su_home_unref(ss->ss_home);
Pekka Pessi's avatar
Pekka Pessi committed
279 280 281
}

/* Initialize session */
Pekka Pessi's avatar
Pekka Pessi committed
282 283
int soa_base_init(char const *name,
		     soa_session_t *ss,
Pekka Pessi's avatar
Pekka Pessi committed
284 285 286 287
		     soa_session_t *parent)
{
  if (parent) {
#define DUP(d, dup, s) if ((s) && !((d) = dup(ss->ss_home, (s)))) return -1
288
    su_home_t *h = ss->ss_home;
Pekka Pessi's avatar
Pekka Pessi committed
289

290 291
    if (soa_description_dup(h, ss->ss_caps, parent->ss_caps) < 0)
      return -1;
292 293
    if (soa_description_dup(h, ss->ss_user, parent->ss_user) < 0)
      return -1;
294 295 296 297
    if (soa_description_dup(h, ss->ss_local, parent->ss_local) < 0)
      return -1;
    if (soa_description_dup(h, ss->ss_remote, parent->ss_remote) < 0)
      return -1;
Pekka Pessi's avatar
Pekka Pessi committed
298 299 300

    DUP(ss->ss_address, su_strdup, parent->ss_address);
    ss->ss_af = parent->ss_af;
301
    DUP(ss->ss_hold, su_strdup, parent->ss_hold);
Pekka Pessi's avatar
Pekka Pessi committed
302 303

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

Pekka Pessi's avatar
Pekka Pessi committed
305 306 307 308 309 310 311 312 313 314 315 316
    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)
{
  if (ss) {
Pekka Pessi's avatar
Pekka Pessi committed
317 318
    ss->ss_active = 0;
    ss->ss_terminated++;
Pekka Pessi's avatar
Pekka Pessi committed
319
    ss->ss_actions->soa_deinit(ss);
Pekka Pessi's avatar
Pekka Pessi committed
320
    su_home_unref(ss->ss_home);
Pekka Pessi's avatar
Pekka Pessi committed
321 322 323
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
324
void soa_base_deinit(soa_session_t *ss)
Pekka Pessi's avatar
Pekka Pessi committed
325 326 327 328 329 330 331 332 333
{
  (void)ss;
}

/** Set tagged parameters */
int soa_set_params(soa_session_t *ss, tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
334

Pekka Pessi's avatar
Pekka Pessi committed
335 336 337 338 339 340 341 342 343 344 345 346
  if (ss == NULL)
    return (errno = EFAULT), -1;

  ta_start(ta, tag, value);

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

  ta_end(ta);

  return n;
}

Pekka Pessi's avatar
Pekka Pessi committed
347
int soa_base_set_params(soa_session_t *ss, tagi_t const *tags)
Pekka Pessi's avatar
Pekka Pessi committed
348
{
349
  int n, change_session = 0;
Pekka Pessi's avatar
Pekka Pessi committed
350 351 352

  int af;

353
  sdp_session_t const *caps_sdp, *user_sdp;
354
  char const *caps_sdp_str, *user_sdp_str;
355

356
  char const *media_address, *hold;
Pekka Pessi's avatar
Pekka Pessi committed
357 358 359 360
  int srtp_enable, srtp_confidentiality, srtp_integrity;

  af = ss->ss_af;

361
  hold = ss->ss_hold;
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364 365 366 367
  media_address = ss->ss_address;

  srtp_enable = ss->ss_srtp_enable;
  srtp_confidentiality = ss->ss_srtp_confidentiality;
  srtp_integrity = ss->ss_srtp_integrity;

368 369 370
  caps_sdp = user_sdp = NONE;
  caps_sdp_str = user_sdp_str = NONE;

Pekka Pessi's avatar
Pekka Pessi committed
371
  n = tl_gets(tags,
372 373

	      SOATAG_CAPS_SDP_REF(caps_sdp),
Pekka Pessi's avatar
Pekka Pessi committed
374 375
	      SOATAG_CAPS_SDP_STR_REF(caps_sdp_str),

376 377
	      SOATAG_USER_SDP_REF(user_sdp),
	      SOATAG_USER_SDP_STR_REF(user_sdp_str),
378

Pekka Pessi's avatar
Pekka Pessi committed
379 380
	      SOATAG_AF_REF(af),
	      SOATAG_ADDRESS_REF(media_address),
381
	      SOATAG_HOLD_REF(hold),
Pekka Pessi's avatar
Pekka Pessi committed
382 383 384 385

	      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
386

Pekka Pessi's avatar
Pekka Pessi committed
387 388 389 390 391
	      TAG_END());

  if (n <= 0)
    return n;

392 393 394 395 396
  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) {
397
      if (soa_set_capability_sdp(ss, caps_sdp, caps_sdp_str, -1) < 0) {
398
	return -1;
Pekka Pessi's avatar
Pekka Pessi committed
399 400
      }
    }
401 402 403
    else {
      soa_description_free(ss, ss->ss_caps);
    }
Pekka Pessi's avatar
Pekka Pessi committed
404 405
  }

406 407 408
  if (user_sdp != NONE || user_sdp_str != NONE) {
    if (user_sdp == NONE) user_sdp = NULL;
    if (user_sdp_str == NONE) user_sdp_str = NULL;
409

410 411
    if (user_sdp || user_sdp_str) {
      if (soa_set_user_sdp(ss, user_sdp, user_sdp_str, -1) < 0) {
412 413
	return -1;
      }
414 415
      if (ss->ss_caps->ssd_str == NULL)
	soa_set_capability_sdp(ss, user_sdp, user_sdp_str, -1);
416 417
    }
    else {
418
      soa_description_free(ss, ss->ss_user);
419 420 421
    }
  }

Pekka Pessi's avatar
Pekka Pessi committed
422
  if (af != ss->ss_af &&
423
      af >= SOA_AF_ANY && af <= SOA_AF_IP6_IP4) {
Pekka Pessi's avatar
Pekka Pessi committed
424
    ss->ss_af = af;
425 426
    change_session = 1;
  }
Pekka Pessi's avatar
Pekka Pessi committed
427 428 429 430

  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);
431
    change_session = 1;
Pekka Pessi's avatar
Pekka Pessi committed
432 433
  }

434 435
  if (hold == (char const *)1)
    hold = "*";
Pekka Pessi's avatar
Pekka Pessi committed
436

437 438 439
  if (str0casecmp(hold, ss->ss_hold)) {
    su_free(ss->ss_home, (void *)ss->ss_hold);
    ss->ss_hold = su_strdup(ss->ss_home, hold);
440
    change_session = 1;
Pekka Pessi's avatar
Pekka Pessi committed
441 442
  }

443 444 445
  if (change_session)
    ss->ss_user_version++;

Pekka Pessi's avatar
Pekka Pessi committed
446 447 448 449
  return n;
}

/** Get tagged parameters */
Pekka Pessi's avatar
Pekka Pessi committed
450
int soa_get_params(soa_session_t const *ss,
Pekka Pessi's avatar
Pekka Pessi committed
451 452 453 454
		   tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
455

Pekka Pessi's avatar
Pekka Pessi committed
456 457 458 459 460 461 462 463 464 465 466 467
  if (ss == NULL)
    return (errno = EFAULT), -1;

  ta_start(ta, tag, value);

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

  ta_end(ta);

  return n;
}

Pekka Pessi's avatar
Pekka Pessi committed
468
int soa_base_get_params(soa_session_t const *ss, tagi_t *tags)
Pekka Pessi's avatar
Pekka Pessi committed
469 470 471 472
{
  int n;

  n = tl_tgets(tags,
473 474
	       SOATAG_CAPS_SDP(ss->ss_caps->ssd_sdp),
	       SOATAG_CAPS_SDP_STR(ss->ss_caps->ssd_str),
Pekka Pessi's avatar
Pekka Pessi committed
475

476 477
	       SOATAG_USER_SDP(ss->ss_user->ssd_sdp),
	       SOATAG_USER_SDP_STR(ss->ss_user->ssd_str),
478

479 480 481 482 483 484
	       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),
	       SOATAG_REMOTE_SDP_STR(ss->ss_remote->ssd_str),

Pekka Pessi's avatar
Pekka Pessi committed
485 486
	       SOATAG_AF(ss->ss_af),
	       SOATAG_ADDRESS(ss->ss_address),
487
	       SOATAG_HOLD(ss->ss_hold),
Pekka Pessi's avatar
Pekka Pessi committed
488

Pekka Pessi's avatar
Pekka Pessi committed
489 490 491 492 493 494 495 496 497 498
	       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;
}

/** Return a list of parameters */
499 500
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
501
{
502 503
  ta_list ta;
  tagi_t *params = NULL;
504

505
  if (ss) {
506 507 508 509 510
    ta_start(ta, tag, value);
    params = ss->ss_actions->soa_get_paramlist(ss, ta_tags(ta));
    ta_end(ta);
  }

511
  return params;
Pekka Pessi's avatar
Pekka Pessi committed
512 513 514
}


515 516 517
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
518
{
519 520
  ta_list ta;
  tagi_t *params;
Pekka Pessi's avatar
Pekka Pessi committed
521 522 523

  if (ss == NULL)
    return NULL;
524 525
  
  ta_start(ta, tag, value);
Pekka Pessi's avatar
Pekka Pessi committed
526

527
  params = tl_list(
528 529 530 531
		   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
532

533 534 535 536 537 538 539 540 541
		   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
542

543 544 545 546 547
		   TAG_IF(ss->ss_remote->ssd_sdp,
			  SOATAG_REMOTE_SDP(ss->ss_remote->ssd_sdp)),
		   TAG_IF(ss->ss_remote->ssd_str,
			  SOATAG_REMOTE_SDP_STR(ss->ss_remote->ssd_str)),

Pekka Pessi's avatar
Pekka Pessi committed
548
		   SOATAG_AF(ss->ss_af),
549 550
		   TAG_IF(ss->ss_address, 
			  SOATAG_ADDRESS(ss->ss_address)),
Pekka Pessi's avatar
Pekka Pessi committed
551

Pekka Pessi's avatar
Pekka Pessi committed
552 553 554
		   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
555

556
		   ta_tags(ta));
Pekka Pessi's avatar
Pekka Pessi committed
557

558
  ta_end(ta);
Pekka Pessi's avatar
Pekka Pessi committed
559 560 561 562 563 564

  return params;
}

#include <sip_status.h>

Pekka Pessi's avatar
Pekka Pessi committed
565
int soa_error_as_sip_response(soa_session_t *ss,
Pekka Pessi's avatar
Pekka Pessi committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
			      char const **return_phrase)
{
  if (ss == NULL) {
    if (return_phrase)
      *return_phrase = sip_500_Internal_server_error;
    return 500;
  }

  if (return_phrase)
    *return_phrase = sip_501_Not_implemented;

  return 501;
}

char const *soa_error_as_sip_reason(soa_session_t *ss)
{
  return "SIP;cause=501;text=\"Unimplemented media\"";
}

585 586 587 588 589 590 591

/** Return SDP description of capabilities.
 *
 * @retval 0 if there is no description to return
 * @retval 1 if description is returned
 * @retval -1 upon an error
 */
592
int soa_get_capability_sdp(soa_session_t const *ss,
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
			   char const **return_sdp,
			   int *return_len)
{
  char const *sdp;

  if (ss == NULL)
    return (void)(errno = EFAULT), -1;

  sdp = ss->ss_caps->ssd_str;

  if (sdp == NULL)
    return 0;
  if (return_sdp)
    *return_sdp = sdp;
  if (return_len)
    *return_len = strlen(sdp);

  return 1;
Pekka Pessi's avatar
Pekka Pessi committed
611 612
}

613 614

int soa_set_capability_sdp(soa_session_t *ss, 
615
			   sdp_session_t const *sdp,
616
			   char const *str, int len)
Pekka Pessi's avatar
Pekka Pessi committed
617
{
618
  return soa_set_sdp(ss, soa_capability_sdp_kind, sdp, str, len);
619 620
}

621 622 623 624
/** Set capabilities */
int 
soa_base_set_capability_sdp(soa_session_t *ss, 
			    sdp_session_t *sdp, char const *str0, int len0)
625
{
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
  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
662 663 664 665
}

/** Return SDP description of the session.
 *
Pekka Pessi's avatar
Pekka Pessi committed
666 667 668
 * @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
669
 */
670
int soa_get_user_sdp(soa_session_t const *ss,
671 672
		      char const **return_sdp,
		      int *return_len)
Pekka Pessi's avatar
Pekka Pessi committed
673
{
674
  char const *sdp_str;
Pekka Pessi's avatar
Pekka Pessi committed
675

676 677 678
  if (ss == NULL)
    return (void)(errno = EFAULT), -1;

679 680 681 682 683
  /* return modified user SDP if available */
  if (ss->ss_local->ssd_str)
    sdp_str = ss->ss_local->ssd_str;
  else
    sdp_str = ss->ss_user->ssd_str;
Pekka Pessi's avatar
Pekka Pessi committed
684

685
  if (sdp_str == NULL)
686 687
    return 0;
  if (return_sdp)
688
    *return_sdp = sdp_str;
689
  if (return_len)
690
    *return_len = strlen(sdp_str);
691 692 693

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

695
/** 
696
 * Returns the version number of user session
697 698 699
 * description. The version numbering starts from
 * zero and is incremented for each modification.
 */
700
int soa_get_user_version(soa_session_t const *ss)
701 702
{
  assert(ss != NULL);
703
  return ss->ss_user_version;
704 705
} 

706 707 708
int soa_set_user_sdp(soa_session_t *ss, 
		     sdp_session_t const *sdp,
		     char const *str, int len)
709
{
710
  return soa_set_sdp(ss, soa_user_sdp_kind, sdp, str, len);
711
}
Pekka Pessi's avatar
Pekka Pessi committed
712

713 714 715
/** Set user SDP (base version). */
int soa_base_set_user_sdp(soa_session_t *ss, 
			   sdp_session_t *sdp, char const *str0, int len0)
716
{
717 718
  ++ss->ss_user_version;
  return soa_description_set(ss, ss->ss_user, sdp, str0, len0);
719
}
Pekka Pessi's avatar
Pekka Pessi committed
720

721 722 723 724 725 726
/** Return remote SDP description of the session.
 *
 * @retval 0 if there is no description to return
 * @retval 1 if description is returned
 * @retval -1 upon an error
 */
727
int soa_get_remote_sdp(soa_session_t const *ss,
728 729 730 731
		       char const **return_sdp,
		       int *return_len)
{
  char const *sdp;
Pekka Pessi's avatar
Pekka Pessi committed
732

733 734
  if (ss == NULL)
    return (void)(errno = EFAULT), -1;
Pekka Pessi's avatar
Pekka Pessi committed
735

736
  sdp = ss->ss_remote->ssd_str;
Pekka Pessi's avatar
Pekka Pessi committed
737

738 739 740 741 742 743 744 745 746 747
  if (sdp == NULL)
    return 0;
  if (return_sdp)
    *return_sdp = sdp;
  if (return_len)
    *return_len = strlen(sdp);

  return 1;
}

748 749 750 751 752 753 754 755
/** 
 * Returns the version number of remote session
 * description. The version numbering starts from
 * zero and is incremented for each modification.
 */
int soa_get_remote_version(soa_session_t const *ss)
{
  assert(ss != NULL);
756
  return ss->ss_remote_version;
757
} 
758 759

int soa_set_remote_sdp(soa_session_t *ss, 
760
		       sdp_session_t const *sdp,
761 762
		       char const *str, int len)
{
763
  return soa_set_sdp(ss, soa_remote_sdp_kind, sdp, str, len);
764 765
}

766 767 768 769
/** Set remote SDP (base version). */
int soa_base_set_remote_sdp(soa_session_t *ss,
			    int new_version,
			    sdp_session_t *sdp, char const *str0, int len0)
770
{
771 772 773 774 775 776 777 778 779 780 781
  /* 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
782 783
}

784 785 786 787 788 789 790 791 792 793
int soa_clear_remote_sdp(soa_session_t *ss)
{
  if (!ss)
    return (void)(errno = EFAULT), -1;

  ss->ss_unprocessed_remote = 0;

  return 0;
}

794 795 796 797 798 799 800 801
int soa_get_local_sdp(soa_session_t const *ss,
		      char const **return_sdp,
		      int *return_len)
{
  char const *sdp;

  if (ss == NULL)
    return (void)(errno = EFAULT), -1;
802

803 804 805 806 807 808 809 810 811 812 813
  sdp = ss->ss_local->ssd_str;

  if (sdp == NULL)
    return 0;
  if (return_sdp)
    *return_sdp = sdp;
  if (return_len)
    *return_len = strlen(sdp);

  return 1;
}
Pekka Pessi's avatar
Pekka Pessi committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830

/** Initialize offer/answer state machine */
int soa_init_offer_answer(soa_session_t *ss)
{
  int complete;

  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;

831 832
  ss->ss_unprocessed_remote = 0;

Pekka Pessi's avatar
Pekka Pessi committed
833 834 835 836 837
  return complete;
}

char **soa_media_features(soa_session_t *ss, int live, su_home_t *home)
{
Pekka Pessi's avatar
Pekka Pessi committed
838
  if (ss)
Pekka Pessi's avatar
Pekka Pessi committed
839 840 841 842 843
    return ss->ss_actions->soa_media_features(ss, live, home);
  else
    return (void)(errno = EFAULT), NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
844
char **soa_base_media_features(soa_session_t *ss, int live, su_home_t *home)
Pekka Pessi's avatar
Pekka Pessi committed
845 846 847 848 849 850 851 852
{
  return su_zalloc(home, 8 * sizeof (char **));
}

char const * const * soa_sip_required(soa_session_t const *ss)
{
  if (ss)
    return ss->ss_actions->soa_sip_required(ss);
Pekka Pessi's avatar
Pekka Pessi committed
853
  else
Pekka Pessi's avatar
Pekka Pessi committed
854 855 856
    return (void)(errno = EFAULT), NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
857
char const * const * soa_base_sip_required(soa_session_t const *ss)
Pekka Pessi's avatar
Pekka Pessi committed
858 859 860 861 862 863 864 865 866
{
  static char const *null = NULL;
  return &null;
}

char const * const * soa_sip_support(soa_session_t const *ss)
{
  if (ss)
    return ss->ss_actions->soa_sip_support(ss);
Pekka Pessi's avatar
Pekka Pessi committed
867
  else
Pekka Pessi's avatar
Pekka Pessi committed
868 869 870
    return (void)(errno = EFAULT), NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
871
char const * const * soa_base_sip_support(soa_session_t const *ss)
Pekka Pessi's avatar
Pekka Pessi committed
872 873 874 875 876
{
  static char const *null = NULL;
  return &null;
}

Pekka Pessi's avatar
Pekka Pessi committed
877
int soa_remote_sip_features(soa_session_t *ss,
Pekka Pessi's avatar
Pekka Pessi committed
878 879 880 881 882
			    char const * const * support,
			    char const * const * required)
{
  if (ss)
    return ss->ss_actions->soa_remote_sip_features(ss, support, required);
Pekka Pessi's avatar
Pekka Pessi committed
883
  else
Pekka Pessi's avatar
Pekka Pessi committed
884 885 886
    return (void)(errno = EFAULT), -1;
}

Pekka Pessi's avatar
Pekka Pessi committed
887
int soa_base_remote_sip_features(soa_session_t *ss,
Pekka Pessi's avatar
Pekka Pessi committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
				    char const * const * support,
				    char const * const * required)
{
  return 0;
}


/** Run Offer step.
 *
 * @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
 *                  operation is completed
 *
 * @retval 1 when operation is successful
 * @retval 0 when operation is not needed
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
905
 *
Pekka Pessi's avatar
Pekka Pessi committed
906 907
 * @ERRORS
 */
908 909 910
int soa_generate_offer(soa_session_t *ss,
		       int always,
		       soa_callback_f *completed)
Pekka Pessi's avatar
Pekka Pessi committed
911
{
912 913
  /** @ERROR EFAULT Bad address. */
  if (ss == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
914 915 916 917 918 919 920 921 922 923 924
    return (errno = EFAULT), -1;

  /** @ERROR An operation is already in progress */
  if (ss->ss_in_progress)
    return (errno = EALREADY), -1;

  /** @ERROR EPROTO We have received offer, now we should send answer */
  if (ss->ss_offer_recv && !ss->ss_answer_sent)
    return (errno = EPROTO), -1;

  /** @ERROR EPROTO We have received SDP, but it has not been processed */
925
  if (soa_has_received_sdp(ss))
Pekka Pessi's avatar
Pekka Pessi committed
926 927 928 929 930 931
    return (errno = EPROTO), -1;

  /** @ERROR EPROTO We have sent an offer, but have received no answer */
  if (ss->ss_offer_sent && !ss->ss_answer_recv)
    return (errno = EPROTO), -1;

932 933 934 935 936 937
  /** @ERROR EPROTO We have received offer. */
  if (ss->ss_unprocessed_remote)
    return (errno = EPROTO), -1;

  /* We should avoid actual operation unless always is true */
  (void)always;  /* We always regenerate offer */
Pekka Pessi's avatar
Pekka Pessi committed
938 939 940 941

  return ss->ss_actions->soa_generate_offer(ss, completed);
}

942 943
int soa_base_generate_offer(soa_session_t *ss,
			    soa_callback_f *completed)
Pekka Pessi's avatar
Pekka Pessi committed
944
{
945
  sdp_session_t const *sdp = ss->ss_user->ssd_sdp;
946 947 948 949 950 951 952 953 954

  (void)completed;

  if (!sdp)
    return -1;

  soa_set_activity(ss, sdp->sdp_media, 0);

  ss->ss_offer_sent = 1;
955
  ss->ss_answer_recv = 0;
956 957 958 959 960 961 962 963 964 965

  return 0;
}

/* Generate answer */
int soa_generate_answer(soa_session_t *ss,
			soa_callback_f *completed)
{
  /** @ERROR EFAULT Bad address as @a ss. */
  if (ss == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
966 967 968 969 970 971 972 973 974 975 976
    return (errno = EFAULT), -1;

  /** @ERROR An operation is already in progress. */
  if (ss->ss_in_progress)
    return (errno = EALREADY), -1;

  /** @ERROR EPROTO We have sent an offer, but have received no answer. */
  if (ss->ss_offer_sent && !ss->ss_answer_recv)
    return (errno = EPROTO), -1;

  /** @ERROR EPROTO We have not received offer. */
977
  if (!ss->ss_unprocessed_remote)
Pekka Pessi's avatar
Pekka Pessi committed
978 979 980 981 982
    return (errno = EPROTO), -1;

  return ss->ss_actions->soa_generate_answer(ss, completed);
}

983 984
int soa_base_generate_answer(soa_session_t *ss,
			     soa_callback_f *completed)
Pekka Pessi's avatar
Pekka Pessi committed
985
{
986
  sdp_session_t const *l_sdp = ss->ss_user->ssd_sdp;
987 988
  sdp_session_t const *r_sdp = ss->ss_remote->ssd_sdp;
  sdp_session_t *rsession;
989

Pekka Pessi's avatar
Pekka Pessi committed
990
  (void)completed;
991

992
  if (!l_sdp || !r_sdp)
993
    return -1;
994 995 996
  rsession = sdp_session_dup(ss->ss_home, r_sdp);
  if (!rsession)
    return -1;
997

998 999 1000 1001 1002 1003
  if (ss->ss_rsession)
    su_free(ss->ss_home, ss->ss_rsession);
  ss->ss_rsession = rsession;

  soa_set_activity(ss, l_sdp->sdp_media, 0);
  soa_set_activity(ss, r_sdp->sdp_media, 1);
1004 1005 1006 1007

  ss->ss_offer_recv = 1;
  ss->ss_answer_sent = 1;
  ss->ss_complete = 1;
1008
  ss->ss_unprocessed_remote = 0;
1009 1010

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1011 1012
}

1013
/** Complete offer-answer after receiving answer */
1014
int soa_process_answer(soa_session_t *ss,
1015
		       soa_callback_f *completed)
Pekka Pessi's avatar
Pekka Pessi committed
1016
{
1017 1018
  /** @ERROR EFAULT Bad address as @a ss. */
  if (ss == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
1019 1020 1021 1022 1023 1024
    return (errno = EFAULT), -1;

  /** @ERROR An operation is already in progress. */
  if (ss->ss_in_progress)
    return (errno = EALREADY), -1;

1025 1026 1027
  /** @ERROR EPROTO We have not sent an offer 
      or already have received answer. */
  if (!ss->ss_offer_sent || ss->ss_answer_recv)
Pekka Pessi's avatar
Pekka Pessi committed
1028 1029
    return (errno = EPROTO), -1;

1030 1031 1032 1033 1034
  /** @ERROR EPROTO We have not received answer. */
  if (!ss->ss_unprocessed_remote)
    return (errno = EPROTO), -1;

  return ss->ss_actions->soa_process_answer(ss, completed);
Pekka Pessi's avatar
Pekka Pessi committed
1035 1036
}

1037 1038
/** 
 * Processes answer from remote end.
1039
 */
1040 1041
int soa_base_process_answer(soa_session_t *ss,
			    soa_callback_f *completed)
Pekka Pessi's avatar
Pekka Pessi committed
1042
{
1043 1044 1045
  sdp_session_t const *l_sdp = ss->ss_local->ssd_sdp;
  sdp_session_t const *r_sdp = ss->ss_remote->ssd_sdp;
  sdp_session_t *rsession;
1046

Pekka Pessi's avatar
Pekka Pessi committed
1047
  (void)completed;
1048

1049
  if (!l_sdp || !r_sdp)
1050
    return -1;
1051 1052 1053
  rsession = sdp_session_dup(ss->ss_home, r_sdp);
  if (!rsession)
    return -1;
1054

1055 1056 1057 1058 1059 1060
  if (ss->ss_rsession)
    su_free(ss->ss_home, ss->ss_rsession);
  ss->ss_rsession = rsession;

  soa_set_activity(ss, l_sdp->sdp_media, 0);
  soa_set_activity(ss, r_sdp->sdp_media, 1);
1061 1062 1063

  ss->ss_answer_recv = 1;
  ss->ss_complete = 1;
1064
  ss->ss_unprocessed_remote = 0;
1065 1066

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1067 1068
}

1069 1070 1071
/** Process rejection of offer */
int soa_process_reject(soa_session_t *ss,
		       soa_callback_f *completed)
Pekka Pessi's avatar
Pekka Pessi committed
1072
{
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  /** @ERROR EFAULT Bad address as @a ss. */
  if (ss == NULL)
    return (errno = EFAULT), -1;

  /** @ERROR An operation is already in progress. */
  if (ss->ss_in_progress)
    return (errno = EALREADY), -1;

  /** @ERROR EPROTO We have not sent an offer 
      or already have received answer. */
  if (!ss->ss_offer_sent || ss->ss_answer_recv)
    return (errno = EPROTO), -1;

  return ss->ss_actions->soa_process_reject(ss, completed);
}

/** 
 * Process reject from remote end.
 */
int soa_base_process_reject(soa_session_t *ss,
			    soa_callback_f *completed)
{
  sdp_session_t const *l_sdp = ss->ss_local->ssd_sdp;

Pekka Pessi's avatar
Pekka Pessi committed
1097
  (void)completed;
1098 1099 1100 1101 1102 1103 1104 1105 1106

  if (!l_sdp)
    return -1;

  soa_set_activity(ss, l_sdp->sdp_media, 0);

  ss->ss_offer_sent = 0;

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1107 1108
}

Pekka Pessi's avatar
Pekka Pessi committed
1109
/** Activate session */
1110
int soa_activate(soa_session_t *ss, char const *option)
Pekka Pessi's avatar
Pekka Pessi committed
1111 1112 1113
{
  /** @ERROR EFAULT Bad address as @a ss. */
  if (ss == NULL)
1114
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1115 1116 1117

  ss->ss_active = 1;

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
  return ss->ss_actions->soa_activate_session(ss, option);
}

int soa_base_activate(soa_session_t *ss, char const *option)
{
  (void)ss;
  (void)option;
  return 0;
}

/** Deactivate session */
int soa_deactivate(soa_session_t *ss, char const *option)
{
  /** @ERROR EFAULT Bad address as @a ss. */
  if (ss == NULL)
    return -1;

  ss->ss_active = 0;

  return ss->ss_actions->soa_deactivate_session(ss, option);
Pekka Pessi's avatar
Pekka Pessi committed
1138 1139
}

1140
int soa_base_deactivate(soa_session_t *ss, char const *option)
Pekka Pessi's avatar
Pekka Pessi committed
1141 1142 1143
{
  (void)ss;
  (void)option;
1144
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
}

/** Terminate session */
void soa_terminate(soa_session_t *ss, char const *option)
{
  /** @ERROR EFAULT Bad address as @a ss. */
  if (ss == NULL)
    return;

  ss->ss_active = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1155
  ss->ss_terminated++;
Pekka Pessi's avatar
Pekka Pessi committed
1156 1157 1158 1159

  ss->ss_actions->soa_terminate_session(ss, option);
}

Pekka Pessi's avatar
Pekka Pessi committed
1160
void soa_base_terminate(soa_session_t *ss, char const *option)
Pekka Pessi's avatar
Pekka Pessi committed
1161 1162
{
  (void)option;
Pekka Pessi's avatar
Pekka Pessi committed
1163 1164 1165 1166

  soa_init_offer_answer(ss);
  ss->ss_oa_rounds = 0;

1167
  soa_description_free(ss, ss->ss_remote);
Pekka Pessi's avatar
Pekka Pessi committed
1168 1169
  soa_set_activity(ss, NULL, 0);
  soa_set_activity(ss, NULL, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1170 1171 1172 1173
}

int soa_has_received_sdp(soa_session_t const *ss)
{
1174
  return ss && ss->ss_unprocessed_remote;
Pekka Pessi's avatar
Pekka Pessi committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
}

int soa_is_complete(soa_session_t const *ss)
{
  return ss && ss->ss_complete;
}

int soa_is_audio_active(soa_session_t const *ss)
{
  return ss ? ss->ss_local_activity->ma_audio : SOA_ACTIVE_DISABLED;
}

int soa_is_video_active(soa_session_t const *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
1189
  return ss ? ss->ss_local_activity->ma_video : SOA_ACTIVE_DISABLED;
Pekka Pessi's avatar
Pekka Pessi committed
1190 1191 1192 1193
}

int soa_is_image_active(soa_session_t const *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
1194
  return ss ? ss->ss_local_activity->ma_image : SOA_ACTIVE_DISABLED;
Pekka Pessi's avatar
Pekka Pessi committed
1195 1196 1197 1198
}

int soa_is_chat_active(soa_session_t const *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
1199
  return ss ? ss->ss_local_activity->ma_chat : SOA_ACTIVE_DISABLED;
Pekka Pessi's avatar
Pekka Pessi committed
1200 1201 1202 1203 1204 1205 1206 1207 1208
}

int soa_is_remote_audio_active(soa_session_t const *ss)
{
  return ss ? ss->ss_remote_activity->ma_audio : SOA_ACTIVE_DISABLED;
}

int soa_is_remote_video_active(soa_session_t const *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
1209
  return ss ? ss->ss_remote_activity->ma_video : SOA_ACTIVE_DISABLED;
Pekka Pessi's avatar
Pekka Pessi committed
1210 1211 1212 1213
}

int soa_is_remote_image_active(soa_session_t const *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
1214
  return ss ? ss->ss_remote_activity->ma_image : SOA_ACTIVE_DISABLED;
Pekka Pessi's avatar
Pekka Pessi committed
1215 1216 1217 1218
}

int soa_is_remote_chat_active(soa_session_t const *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
1219
  return ss ? ss->ss_remote_activity->ma_chat : SOA_ACTIVE_DISABLED;
Pekka Pessi's avatar
Pekka Pessi committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
}

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

int soa_set_status(soa_session_t *ss, int status, char const *phrase)
{
  if (ss)
    ss->ss_status = status, ss->ss_phrase = phrase;
  return -1;
}

Pekka Pessi's avatar
Pekka Pessi committed
1231
static inline
Pekka Pessi's avatar
Pekka Pessi committed
1232 1233
int soa_media_is_ready(soa_session_t const *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
1234
  XXX;
Pekka Pessi's avatar
Pekka Pessi committed
1235 1236 1237 1238
  return 0;
  /* return ss && ss->ss_session != NULL; */
}

Pekka Pessi's avatar
Pekka Pessi committed
1239 1240 1241
void soa_set_activity(soa_session_t *ss,
		      sdp_media_t const *m,
		      int remote)
Pekka Pessi's avatar
Pekka Pessi committed
1242 1243 1244 1245
{
  struct soa_media_activity *ma;
  sdp_connection_t const *c;
  int mode;
1246 1247 1248 1249 1250
  int ma_audio = SOA_ACTIVE_DISABLED;
  int ma_video = SOA_ACTIVE_DISABLED;
  int ma_chat = SOA_ACTIVE_DISABLED;
  int ma_image = SOA_ACTIVE_DISABLED;
  int *p;
Pekka Pessi's avatar
Pekka Pessi committed
1251 1252 1253 1254 1255 1256

  remote = !!remote;

  ma = remote ? ss->ss_remote_activity : ss->ss_local_activity;

  for (; m; m = m->m_next) {
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    if (m->m_type == sdp_media_audio)
      p = &ma_audio;
    else if (m->m_type == sdp_media_video)
      p = &ma_video;
    else if (m->m_type == sdp_media_image)
      p = &ma_image;
    else if (strcasecmp(m->m_type_name, "message") == 0)
      p = &ma_chat;
    else
      continue;

    if (m->m_rejected) {
      if (*p < 0)
	*p = SOA_ACTIVE_REJECTED;
Pekka Pessi's avatar
Pekka Pessi committed
1271
      continue;
1272
    }
Pekka Pessi's avatar
Pekka Pessi committed
1273 1274 1275 1276 1277 1278 1279 1280

    mode = m->m_mode;

    c = sdp_media_connections((sdp_media_t *)m);

    if (remote != (c && c->c_mcast))
      mode = ((mode << 1) & 2) | ((mode >> 1) & 1);

1281 1282 1283 1284
    if (*p < 0)
      *p = mode;
    else
      *p |= mode;
Pekka Pessi's avatar
Pekka Pessi committed
1285
  }
Pekka Pessi's avatar
Pekka Pessi committed
1286

1287 1288 1289 1290
  ma->ma_audio = ma_audio;
  ma->ma_video = ma_video;
  ma->ma_image = ma_image;
  ma->ma_chat = ma_chat;
Pekka Pessi's avatar
Pekka Pessi committed
1291
}
Pekka Pessi's avatar
Pekka Pessi committed
1292

1293 1294 1295 1296
/* ----------------------------------------------------------------------*/
/* Handle SDP */


1297 1298 1299 1300 1301 1302 1303 1304 1305
/**
 * Parses and stores session description
 * 
 * @param ss instance pointer
 * @param what caps, local or remote
 * @param sdp0 new sdp (parsed)
 * @param sdp_str new sdp (unparsed)
 * @param str_len length on unparsed data
 **/
Pekka Pessi's avatar
Pekka Pessi committed
1306
static
1307 1308 1309 1310
int soa_set_sdp(soa_session_t *ss, 
		enum soa_sdp_kind what,
		sdp_session_t const *sdp0,
		char const *sdp_str, int str_len)
Pekka Pessi's avatar
Pekka Pessi committed
1311
{
1312 1313 1314 1315
  struct soa_description *ssd;
  int flags, new_version;
  sdp_parser_t *parser = NULL;
  sdp_session_t sdp[1];
Pekka Pessi's avatar
Pekka Pessi committed
1316

1317 1318 1319 1320 1321
  switch (what) {
  case soa_capability_sdp_kind:
    ssd = ss->ss_caps;
    flags = sdp_f_config;
    break;
1322 1323
  case soa_user_sdp_kind:
    ssd = ss->ss_user;
1324 1325 1326 1327 1328 1329 1330
    flags = sdp_f_config;
    break;
  case soa_remote_sdp_kind:
    ssd = ss->ss_remote;
    flags = sdp_f_mode_0000;
    break;
  default:
1331
    return -1;
1332
  }
Pekka Pessi's avatar
Pekka Pessi committed
1333

1334
  if (sdp0)
1335
    new_version = sdp_session_cmp(sdp0, ssd->ssd_sdp) != 0;
1336
  else if (sdp_str)
1337
    new_version = str0cmp(sdp_str, ssd->ssd_unparsed) != 0;
1338 1339
  else
    return (void)(errno = EINVAL), -1;
Pekka Pessi's avatar
Pekka Pessi committed
1340

1341 1342
  if (sdp_str && str_len == -1)
    str_len = strlen(sdp_str);
Pekka Pessi's avatar
Pekka Pessi committed
1343

1344 1345 1346 1347 1348 1349 1350 1351
  if (!new_version) {
    if (what == soa_remote_sdp_kind) {
      *sdp = *ssd->ssd_sdp;
      /* XXX - should check changes by soa_set_remote_sdp */
      return ss->ss_actions->soa_set_remote_sdp(ss, new_version, 
						sdp, sdp_str, str_len);
    }
    return 0;
1352
  } 
Pekka Pessi's avatar
Pekka Pessi committed
1353

1354
  if (sdp0) {
1355
    /* note: case 1 - src in parsed form */
1356 1357 1358
    *sdp = *sdp0;
  } 
  else /* if (sdp_str) */ {
1359
    /* note: case 2 - src in unparsed form */
1360
    parser = sdp_parse(ss->ss_home, sdp_str, str_len, flags | sdp_f_anynet);
1361 1362 1363 1364 1365 1366 1367 1368

    if (sdp_parsing_error(parser)) {
      sdp_parser_free(parser);
      return soa_set_status(ss, 400, "Bad Session Description");
    }

    *sdp = *sdp_session(parser);
  }
1369
 
1370 1371 1372
  switch (what) {
  case soa_capability_sdp_kind:
    return ss->ss_actions->soa_set_capability_sdp(ss, sdp, sdp_str, str_len);
1373 1374
  case soa_user_sdp_kind:
    return ss->ss_actions->soa_set_user_sdp(ss, sdp, sdp_str, str_len);
1375 1376 1377 1378 1379
  case soa_remote_sdp_kind:
    return ss->ss_actions->soa_set_remote_sdp(ss, 1, sdp, sdp_str, str_len);
  default:
    return -1;
  }
Pekka Pessi's avatar
Pekka Pessi committed
1380 1381
}

1382

1383 1384 1385 1386 1387 1388
/** Set session descriptions. */
int soa_description_set(soa_session_t *ss,
			struct soa_description *ssd,
			sdp_session_t *sdp,
			char const *sdp_str,
			int str_len)
Pekka Pessi's avatar
Pekka Pessi committed
1389
{
1390
  int retval = -1;
Pekka Pessi's avatar
Pekka Pessi committed
1391

1392 1393 1394 1395
  sdp_printer_t *printer = NULL;
  sdp_session_t *sdp_new;
  char *sdp_str_new;
  char *sdp_str0_new;
Pekka Pessi's avatar
Pekka Pessi committed
1396

1397
  void *tbf1, *tbf2, *tbf3, *tbf4;
Pekka Pessi's avatar
Pekka Pessi committed
1398

1399 1400 1401 1402 1403 1404 1405
  /* Store description in three forms: unparsed, parsed and reprinted */

  sdp_new = sdp_session_dup(ss->ss_home, sdp);
  printer = sdp_print(ss->ss_home, sdp, NULL, 0, 0);
  sdp_str_new = (char *)sdp_message(printer);
  if (sdp_str)
    sdp_str0_new = su_strndup(ss->ss_home, sdp_str, str_len);
Pekka Pessi's avatar
Pekka Pessi committed
1406
  else
1407 1408 1409 1410 1411
    sdp_str0_new = sdp_str_new;
  
  if (sdp_new && printer && sdp_str_new && sdp_str0_new) {
    tbf1 = ssd->ssd_sdp, tbf2 = ssd->ssd_printer;
    tbf3 = (void *)ssd->ssd_str, tbf4 = (void *)ssd->ssd_unparsed;
Pekka Pessi's avatar
Pekka Pessi committed
1412

1413 1414 1415 1416
    ssd->ssd_sdp = sdp_new;
    ssd->ssd_printer = printer;
    ssd->ssd_str = sdp_str_new;
    ssd->ssd_unparsed = sdp_str0_new;      
Pekka Pessi's avatar
Pekka Pessi committed
1417

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    retval = 1;
  }
  else {
    tbf1 = sdp_new, tbf2 = printer, tbf3 = sdp_str_new, tbf4 = sdp_str0_new;
  }
  
  su_free(ss->ss_home, tbf1);
  sdp_printer_free(tbf2);
  if (tbf3 != tbf4)
    su_free(ss->ss_home, tbf4);
Pekka Pessi's avatar
Pekka Pessi committed
1428

1429 1430
  return retval;
}
Pekka Pessi's avatar
Pekka Pessi committed
1431

1432

1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
/** Duplicate a session descriptions. */
int soa_description_dup(su_home_t *home, 
			struct soa_description *ssd,
			struct soa_description const *ssd0)
{
  if (ssd0->ssd_sdp) {
    int len = ssd0->ssd_str ? strlen(ssd0->ssd_str) + 1 : 0;

    ssd->ssd_sdp = sdp_session_dup(home, ssd0->ssd_sdp);
    ssd->ssd_printer = sdp_print(home, ssd->ssd_sdp, NULL, len, 0);
    ssd->ssd_str = (char *)sdp_message(ssd->ssd_printer);
    if (ssd0->ssd_str != ssd0->ssd_unparsed)
      ssd->ssd_unparsed = su_strdup(home, ssd0->ssd_unparsed);
    else
      ssd->ssd_unparsed = ssd->ssd_str;
  }
Pekka Pessi's avatar
Pekka Pessi committed
1449

1450 1451
  return 0;
}
Pekka Pessi's avatar
Pekka Pessi committed
1452

1453 1454 1455 1456 1457
/** Free session descriptions. */
void soa_description_free(soa_session_t *ss, 
			  struct soa_description *ssd)
{
  void *tbf1, *tbf2, *tbf3, *tbf4;
Pekka Pessi's avatar
Pekka Pessi committed
1458

1459 1460
  tbf1 = ssd->ssd_sdp, tbf2 = ssd->ssd_printer;
  tbf3 = (void *)ssd->ssd_str, tbf4 = (void *)ssd->ssd_unparsed;
Pekka Pessi's avatar
Pekka Pessi committed
1461

1462 1463 1464 1465 1466 1467
  memset(ssd, 0, sizeof *ssd);

  su_free(ss->ss_home, tbf1);
  sdp_printer_free(tbf2);
  if (tbf3 != tbf4)
    su_free(ss->ss_home, tbf4);
Pekka Pessi's avatar
Pekka Pessi committed
1468 1469
}

1470 1471

/** Initialize SDP o= line */
Pekka Pessi's avatar
Pekka Pessi committed
1472
int
1473
soa_init_sdp_origin(soa_session_t *ss, sdp_origin_t *o, char buffer[64])
Pekka Pessi's avatar
Pekka Pessi committed
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
{
  sdp_connection_t *c;

  if (ss == NULL || o == NULL)
    return (errno = EFAULT), -1;

  assert(o->o_address);

  if (!o->o_username)
    o->o_username = "-";

  if (o->o_id == 0)
    su_randmem(&o->o_id, sizeof o->o_id);
  o->o_id &= 0x7fffffffffffffffULL;

  if (o->o_version == 0)
    su_randmem(&o->o_version, sizeof o->o_version);
  o->o_version &= 0x7fffffffffffffffULL;

  c = o->o_address;

  if (!c->c_nettype ||
      !c->c_address ||
      strcmp(c->c_address, "") == 0 ||
      strcasecmp(c->c_address, "localhost") == 0 ||
      strcasecmp(c->c_address, "localhost.localdomain") == 0 ||
      strcmp(c->c_address, "0.0.0.0") == 0 ||
      strcmp(c->c_address, "127.0.0.1") == 0 ||
      strcmp(c->c_address, "::") == 0 ||
      strcmp(c->c_address, "::1") == 0) {
1504
    return soa_init_sdp_connection(ss, c, buffer);
Pekka Pessi's avatar
Pekka Pessi committed
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
  }

  return 0;
}

/** Search for an local address item from string provided by user */
static
su_localinfo_t *li_in_list(su_localinfo_t *li0, char const **llist)
{
  char const *list = *llist;
  int n;

  if (!list)
    return NULL;

  while ((n = strcspn(list, ", "))) {
    su_localinfo_t *li;

    for (li = li0; li; li = li->li_next) {
      if (strncasecmp(li->li_canonname, list, n) == 0 &&
	  li->li_canonname[n] == '\0')
	break;
    }

    list += n; while (list[0] == ' ' || list[0] == ',') list++;
    *llist = list;

    if (li)
      return li;
  }

  return NULL;
}


/** Obtain a local address for SDP connection structure */
int
soa_init_sdp_connection(soa_session_t *ss,
1543 1544
			sdp_connection_t *c,
			char buffer[64])