nua_stack.c 169 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
/*
 * 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 nua_stack.c
 * @brief Nokia User Agent (NUA) implementation
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Kai Vehmanen <Kai.Vehmanen@nokia.com>
 * @author Remeres Jacobs <Remeres.Jacobs@nokia.com>
 * @author Tat Chan <Tat.Chan@nokia.com>
 *
 * @date Created: Wed Feb 14 18:32:58 2001 ppessi
34
 * $Date: 2005/09/09 10:56:31 $
Pekka Pessi's avatar
Pekka Pessi committed
35 36 37 38 39
 */

#include "config.h"

const char _nua_stack_c_id[] =
40
"$Id: nua_stack.c,v 1.4 2005/09/09 10:56:31 ppessi Exp $";
Pekka Pessi's avatar
Pekka Pessi committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

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

#include <assert.h>

#include <su_tag_class.h>
#include <su_tag_class.h>
#include <su_tagarg.h>

#include <stdio.h>

#include <su_tag_io.h>

#define SU_LOG (nua_log)
#include <su_debug.h>

#define SU_ROOT_MAGIC_T   struct nua_s
#define SU_MSG_ARG_T      struct event_s

#include <su_wait.h>

#include <su_strlst.h>

#include "nua.h"
#include "nua_tag.h"

#if HAVE_HERBIE
#include "nua_herbie.h"
#endif

#define NTA_AGENT_MAGIC_T    struct nua_s
#define NTA_LEG_MAGIC_T      struct nua_handle_s
#define NTA_OUTGOING_MAGIC_T struct nua_handle_s
#define NTA_INCOMING_MAGIC_T struct nua_handle_s
#define NTA_RELIABLE_MAGIC_T struct nua_handle_s

#define NEA_SMAGIC_T         nua_handle_t
#define NEA_EMAGIC_T         nua_handle_t

#include <sip.h>
#include <sip_header.h>
#include <sip_status.h>
#include <sip_util.h>

#include <nta.h>
#include <nea.h>

#include <tport_tag.h>
#include <auth_client.h>

Pekka Pessi's avatar
Pekka Pessi committed
93
#if HAVE_SOFIA_SMIME 
Pekka Pessi's avatar
Pekka Pessi committed
94
#include "smimec.h"
Pekka Pessi's avatar
Pekka Pessi committed
95
#endif                  
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100 101 102 103

#include <sl_utils.h>

#if HAVE_SIGCOMP
#include <sigcomp.h>
#include <nta_tport.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
104 105
#include "soa.h"

Pekka Pessi's avatar
Pekka Pessi committed
106 107
#include "nua_stack.h"

108
typedef unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

/* ========================================================================
 *
 *                       Protocol stack side
 *
 * ======================================================================== */

nua_handle_t *nh_create(nua_t *nua, tag_type_t t, tag_value_t v, ...);
static void nh_append(nua_t *nua, nua_handle_t *nh);
static void nh_remove(nua_t *nua, nua_handle_t *nh);
static void nh_destroy(nua_t *nua, nua_handle_t *nh);

static int nh_authorize(nua_handle_t *nh, 
			tag_type_t tag, tag_value_t value, ...);

static int nh_challenge(nua_handle_t *nh, sip_t const *sip);

static void dialog_uac_route(nua_handle_t *nh, sip_t const *sip, int rtag);
static void dialog_uas_route(nua_handle_t *nh, sip_t const *sip, int rtag);
static void dialog_get_peer_info(nua_handle_t *nh, sip_t const *sip);

static
int nh_notifier_shutdown(nua_handle_t *nh, nea_event_t *ev,
			 tag_type_t t, tag_value_t v, ...);

static void ua_timer(nua_t *nua, su_timer_t *t, su_timer_arg_t *a);

static void ua_set_from(nua_t *nua, sip_from_t const *f);

static void ua_init_contact(nua_t *nua);

static int process_request(nua_handle_t *nh,
			   nta_leg_t *leg,
			   nta_incoming_t *irq,
			   sip_t const *sip);

static int process_response(nua_handle_t *nh,
			    struct nua_client_request *cr,
			    nta_outgoing_t *orq,
			    sip_t const *sip,
			    tag_type_t tag, tag_value_t value, ...);

#define SIP_METHOD_UNKNOWN sip_method_unknown, NULL

#define UA_INTERVAL 5

/* Private tags */
#define NUTAG_ADD_CONTACT(v) _nutag_add_contact, tag_bool_v(v)
extern tag_typedef_t _nutag_add_contact;

#define NUTAG_ADD_CONTACT_REF(v) _nutag_add_contact_ref, tag_bool_vr(&v)
extern tag_typedef_t _nutag_add_contact_ref;

#define NUTAG_COPY(v) _nutag_copy, tag_bool_v(v)
extern tag_typedef_t _nutag_copy;

#define NUTAG_COPY_REF(v) _nutag_copy_ref, tag_bool_vr(&v)
extern tag_typedef_t _nutag_copy_ref;

/** Methods allowed by default. */ 
static char const nua_allow_str[] =
"INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, INFO, "
"MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE";

Pekka Pessi's avatar
Pekka Pessi committed
173 174 175
/** Default internal error */
char const nua_500_error[] = "Internal NUA Error";

Pekka Pessi's avatar
Pekka Pessi committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/* ----------------------------------------------------------------------
 * Initialization & deinitialization
 */

int ua_init(su_root_t *root, nua_t *nua)
{
  su_home_t *home;
  void *sip_parser = NULL;
  url_string_t const *contact = NULL;
  url_string_t const *sips_contact = NULL;

  char const *certificate_dir = NULL;
  char const *uicc_name = "default";

  nua_handle_t *nh;
  int media_enable = 1;
Pekka Pessi's avatar
Pekka Pessi committed
192
  soa_session_t *soa = NULL;
193
  char const *soa_name = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

  static int initialized_logs = 0;

  enter;

  if (!initialized_logs) {
    extern su_log_t tport_log[];
    extern su_log_t nta_log[];
    extern su_log_t nea_log[];
    extern su_log_t iptsec_log[];

    su_log_init(tport_log);
    su_log_init(nta_log);
    su_log_init(nea_log);
    su_log_init(iptsec_log);

    initialized_logs = 1;
  }

  home = nua->nua_home;

  nua->nua_root = root;

  nua->nua_handles_tail = &nua->nua_handles;
  
  if (!(nua->nua_handles = nh = nh_create(nua, TAG_END())))
    return -1;

  /* Set some defaults */
  nua->nua_handles->nh_auto_ack = 1;
  nua->nua_path_enable = 1;
  nua->nua_service_route_enable = 1;

  /* Set initial parameters */

  tl_gets(nua->nua_args,
	  NUTAG_URL_REF(contact),
	  NUTAG_SIPS_URL_REF(sips_contact),
	  NUTAG_CERTIFICATE_DIR_REF(certificate_dir),
	  NUTAG_SIP_PARSER_REF(sip_parser),
	  NUTAG_UICC_REF(uicc_name),
Pekka Pessi's avatar
Pekka Pessi committed
235 236
	  NUTAG_MEDIA_ENABLE_REF(media_enable),
	  NUTAG_SOA_SESSION_REF(soa),
237
	  NUTAG_SOA_NAME_REF(soa_name),
Pekka Pessi's avatar
Pekka Pessi committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	  TAG_NULL());

#if HAVE_UICC_H
  if (uicc_name)
    nua->nua_uicc = uicc_create(root, uicc_name);
#endif

  if (!contact && sips_contact)
    contact = sips_contact, sips_contact = NULL;
  
  nua->nua_nta = nta_agent_create(root, contact, NULL, NULL, 
				  TPTAG_CERTIFICATE(certificate_dir),
				  NTATAG_TAG_3261(0),
				  TAG_END());

  if (!nua->nua_nta && !contact) {
    contact = (url_string_t const*)"sip:*:*";
    nua->nua_nta = nta_agent_create(root, contact, NULL, NULL, 
				    TPTAG_CERTIFICATE(certificate_dir),
				    NTATAG_TAG_3261(0),
				    TAG_END());
  }

  if (!nua->nua_nta)
    return -1;

  if (sips_contact) {
    nta_agent_add_tport(nua->nua_nta, sips_contact, 
			TPTAG_CERTIFICATE(certificate_dir),
			TAG_END());
  }

  nua->nua_invite_timer = 120;
  nua->nua_session_timer = 1800;
  nua->nua_min_se = 120;
  nua->nua_retry_count = 3;
  nua->nua_max_subscriptions = 20;
  nua->nua_media_enable = media_enable;
  nua->nua_messageRespond = getenv("PIMIW_HACK") != 0;

  nta_agent_set_params(nua->nua_nta,
		       NTATAG_UA(1),
		       NTATAG_MERGE_482(1),
		       NTATAG_RPORT(1),	/* XXX */
Pekka Pessi's avatar
Pekka Pessi committed
282
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
283 284 285 286 287 288 289
		       NTATAG_SMIME(nua->sm),
#endif
		       TAG_NEXT(nua->nua_args));

  nua->nua_sdp_content = sip_content_type_make(home, SDP_MIME_TYPE);
  nua->nua_invite_accept = sip_accept_make(home, SDP_MIME_TYPE);

290 291 292
  if (media_enable) {
    if (soa == NULL)
      soa = soa_create(soa_name, nua->nua_root, nua->nua_default);
Pekka Pessi's avatar
Pekka Pessi committed
293 294
    nh->nh_soa = soa;
    soa_set_params(soa, TAG_NEXT(nua->nua_args));
Pekka Pessi's avatar
Pekka Pessi committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308
  }

  nh->nh_ds->ds_leg = nta_leg_tcreate(nua->nua_nta, process_request, nh,
				      NTATAG_NO_DIALOG(1), 
				      TAG_END());

  if (!nua->nua_allow)
    nua->nua_allow = sip_allow_make(home, nua_allow_str);
  if (!nua->nua_supported)
    nua->nua_supported = sip_supported_make(home, "timer, 100rel");

  ua_init_contact(nua);
  ua_set_from(nua, NULL);

Pekka Pessi's avatar
Pekka Pessi committed
309 310
  nua->nua_timer = su_timer_create(su_root_task(root), UA_INTERVAL * 1000);

Pekka Pessi's avatar
Pekka Pessi committed
311 312 313 314 315 316 317 318 319 320 321 322 323
  if (!(nh->nh_ds->ds_leg &&
	nua->nua_allow &&
	nua->nua_supported &&
	(nua->nua_contact || nua->nua_sips_contact) &&
	nua->nua_from &&
	nua->nua_sdp_content &&
	nua->nua_timer))
    return -1;

#if HAVE_HERBIE
  /* Enable polyphonic ringing tones */
  nua->nua_herbie = nua_herbie_create(home, getenv("NUA_HERBIE_TONE"));
  if (!nua->nua_herbie);
Pekka Pessi's avatar
Pekka Pessi committed
324
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
325 326
#endif

Pekka Pessi's avatar
Pekka Pessi committed
327 328
  ua_timer(nua, nua->nua_timer, NULL);

Pekka Pessi's avatar
Pekka Pessi committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  nua->nua_args = NULL;

  return 0;
}

void ua_deinit(su_root_t *root, nua_t *nua)
{
  enter;

#if HAVE_HERBIE
  if (nua->nua_herbie)
    nua_herbie_free(nua->nua_herbie), nua->nua_herbie = NULL;
#endif

  su_timer_destroy(nua->nua_timer), nua->nua_timer = NULL;
  nta_agent_destroy(nua->nua_nta), nua->nua_nta = NULL;
/* XXX: */
#if HAVE_MSS
  /* XXX: clean up media subsystem resources (nua->nua_mss) */
#endif
}

/** Set the default from field */
void ua_set_from(nua_t *nua, sip_from_t const *f)
{
  sip_param_t params[2] = { NULL, NULL };
  sip_from_t from[1] = { SIP_FROM_INIT() }, *f0 = NULL;

#if HAVE_UICC_H
  /* XXX: add */
#endif

  if (f) {
    from->a_display = f->a_display;
    *from->a_url = *f->a_url;
    if (f->a_params)
      from->a_params = f->a_params;
    else
      from->a_params = params;
  } 
  else {
    sip_contact_t *m;
    m = nua->nua_contact ? nua->nua_contact : nua->nua_sips_contact;
    from->a_display = m->m_display;
    *from->a_url = *m->m_url;
    from->a_params = params;
  }

  params[0] = nta_agent_newtag(NULL, "tag=%s", nua->nua_nta);

  nua->nua_from = sip_from_dup(nua->nua_home, from);

  su_free(NULL, (void *) params[0]);
  su_free(NULL, f0);
}


static 
void ua_init_a_contact(nua_t *nua, su_home_t *home, sip_contact_t *m);

/** Initialize our contacts. */
void ua_init_contact(nua_t *nua)
{
  su_home_t home[1] = { SU_HOME_INIT(home) };
  sip_via_t *v;
  int sip = 0, sips = 0;
  sip_contact_t *m;

  for (v = nta_agent_via(nua->nua_nta); v; v = v->v_next) {
    if (strcasecmp(v->v_protocol, sip_transport_tls) != 0) {
      if (!sip) {
	m = sip_contact_create_from_via(home, v, NULL);
	if (m) {
	  ua_init_a_contact(nua, home, m);
	  sip = 1;
	}
      }
    } 
    else if (!sips) {
      m = sip_contact_create_from_via(home, v, NULL);
      if (m) {
	ua_init_a_contact(nua, home, m);
	sips = 1;
      }
    }

    if (sip && sips)
      break;
  }

  su_home_deinit(home);
}

Pekka Pessi's avatar
Pekka Pessi committed
422 423


Pekka Pessi's avatar
Pekka Pessi committed
424 425 426
static 
void ua_init_a_contact(nua_t *nua, su_home_t *home, sip_contact_t *m)
{
Pekka Pessi's avatar
Pekka Pessi committed
427
  char const ***m_params = (msg_param_t **)&m->m_params;
Pekka Pessi's avatar
Pekka Pessi committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441
  su_strlst_t *l = su_strlst_create(home);
  nua_handle_t *nh = nua->nua_default;
  int i;

  if (nh->nh_callee_caps) {
    if (nh->nh_allow) {
      char *methods;
      if (nh->nh_allow->k_items)
	for (i = 0; nh->nh_allow->k_items[i]; i++)
	  su_strlst_append(l, nh->nh_allow->k_items[i]);
      methods = su_strlst_join(l, home, ",");
      methods = su_sprintf(home, "methods=\"%s\"", methods);
      msg_params_replace(home, m_params, methods);
    }
Pekka Pessi's avatar
Pekka Pessi committed
442 443 444 445 446 447 448 449 450

    if (nh->nh_soa) {
      char **media = soa_media_features(nh->nh_soa, 0, home);

      while (*media) {
	msg_params_replace(home, m_params, *media);
	media++;
      }
    }
Pekka Pessi's avatar
Pekka Pessi committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  }

  m = sip_contact_dup(nua->nua_home, m);

  if (m) {
    if (m->m_url->url_type == url_sip)
      su_free(nua->nua_home, nua->nua_contact), 
	nua->nua_contact = m;
    else
      su_free(nua->nua_home, nua->nua_sips_contact), 
	nua->nua_sips_contact = m;
  }

}


/* ----------------------------------------------------------------------
 * Sending events to client application
 */

static void 
  ua_shutdown(nua_t *),
  ua_set_params(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
Pekka Pessi's avatar
Pekka Pessi committed
474 475 476 477
  ua_get_params(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *);

#if 0
static void
Pekka Pessi's avatar
Pekka Pessi committed
478 479 480 481 482
  ua_set_media_param(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_get_media_param(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_media_setup(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_media_describe(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_media_event(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *);
Pekka Pessi's avatar
Pekka Pessi committed
483
#endif
Pekka Pessi's avatar
Pekka Pessi committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

static int
  ua_register(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_invite(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_ack(nua_t *, nua_handle_t *, tagi_t const *),
  ua_cancel(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_bye(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_options(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_publish(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_info(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_update(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_message(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_subscribe(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_notify(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_refer(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_method(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *);

static void
  ua_authenticate(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_notifier(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_terminate(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_respond(nua_t *, nua_handle_t *, int , char const *, tagi_t const *),
  ua_destroy(nua_t *, nua_handle_t *, tagi_t const *);

#define UA_EVENT1(e, statusphrase) \
  ua_event(nua, nh, NULL, e, statusphrase, TAG_END())

#define UA_EVENT2(e, status, phrase)			\
  ua_event(nua, nh, NULL, e, status, phrase, TAG_END())

#define UA_EVENT3(e, status, phrase, tag)			\
  ua_event(nua, nh, NULL, e, status, phrase, tag, TAG_END())

/** Send an event to the application. */
int ua_event(nua_t *nua, nua_handle_t *nh, msg_t *msg,
	     nua_event_t event, int status, char const *phrase,
	     tag_type_t tag, tag_value_t value, ...)
{
  su_msg_r sumsg = SU_MSG_R_INIT;

  ta_list ta;
  int e_len, len, xtra, p_len;

  enter;

  if (nua_log->log_level >= 5) {
    char const *name = nua_event_name(event) + 4;
    char const *p = phrase ? phrase : "";

    if (status == 0)
      SU_DEBUG_5(("nua(%p): %s %s\n", nh, name, p));
    else
      SU_DEBUG_5(("nua(%p): %s %u %s\n", nh, name, status, p));
  }

  if (event == nua_r_destroy) {
    if (msg) 
      msg_destroy(msg);
    if (status >= 200) {
      nh_destroy(nua, nh);
    }
    return event;
  }

  if (event > nua_r_method || (nh && !nh->nh_valid) ||
      (nua->nua_shutdown && event != nua_r_shutdown)) {
    if (msg) 
      msg_destroy(msg);
    return event;
  }

  ta_start(ta, tag, value);

  e_len = offsetof(event_t, e_tags);
  len = tl_len(ta_args(ta));
  xtra = tl_xtra(ta_args(ta), len);
  p_len = phrase ? strlen(phrase) + 1 : 1;

  if (su_msg_create(sumsg, nua->nua_client, nua->nua_server,
		    nua_event, e_len + len + xtra + p_len) == 0) {
    event_t *e = su_msg_data(sumsg);

    tagi_t *t = e->e_tags, *t_end = (tagi_t *)((char *)t + len);
    void *b = t_end, *end = (char *)b + xtra;

    t = tl_dup(t, ta_args(ta), &b);
    assert(t == t_end); assert(b == end);

    e->e_event = event;
    e->e_nh = nh ? nh_incref(nh) : nua->nua_default;
    e->e_status = status;
    e->e_phrase = strcpy(end, phrase ? phrase : "");
    e->e_msg = msg;

    if (su_msg_send(sumsg) != 0)
      nh_decref(nh);
  }

  ta_end(ta);

  return event;
}

/** Get name for NUA event. */
char const *nua_event_name(nua_event_t event)
{
  switch (event) {
  case nua_i_error: return "nua_i_error";
  case nua_i_media_error: return "nua_i_media_error";
  case nua_i_invite: return "nua_i_invite";
  case nua_i_fork: return "nua_i_fork";
  case nua_i_active: return "nua_i_active";
  case nua_i_terminate: return "nua_i_terminate";
  case nua_i_cancel: return "nua_i_cancel";
  case nua_i_bye: return "nua_i_bye";
  case nua_i_options: return "nua_i_options";
  case nua_i_refer: return "nua_i_refer";
  case nua_i_publish: return "nua_i_publish";
  case nua_i_prack: return "nua_i_prack";
  case nua_i_info: return "nua_i_info";
  case nua_i_update: return "nua_i_update";
  case nua_i_message: return "nua_i_message";
  case nua_i_chat: return "nua_i_chat";
  case nua_i_subscribe: return "nua_i_subscribe";
  case nua_i_notify: return "nua_i_notify";
  case nua_i_method: return "nua_i_method";
  case nua_i_media_event: return "nua_i_media_event";
  case nua_i_terminated: return "nua_i_terminated";

  case nua_i_announce: return "nua_i_announce (rtsp)";
  case nua_i_describe: return "nua_i_describe (rtsp)";
  case nua_i_get_parameter: return "nua_i_get_parameter (rtsp)";
  case nua_i_pause: return "nua_i_pause (rtsp)";
  case nua_i_options2: return "nua_i_options (rtsp)";
  case nua_i_play: return "nua_i_play (rtsp)";
  case nua_i_record: return "nua_i_record (rtsp)";
  case nua_i_set_parameter: return "nua_i_set_parameter (rtsp)";
  case nua_i_setup: return "nua_i_setup (rtsp)";
  case nua_i_teardown: return "nua_i_teardown (rtsp)";

  /* Responses */
  case nua_r_get_params: return "nua_r_get_params";
  case nua_r_shutdown: return "nua_r_shutdown";
  case nua_r_set_media_param: return "nua_r_set_media_param";
  case nua_r_get_media_param: return "nua_r_get_media_param";
  case nua_r_media_setup: return "nua_r_media_setup";
  case nua_r_media_describe: return "nua_r_media_describe";
  case nua_r_media_event: return "nua_r_media_event";
  case nua_r_notifier: return "nua_r_notifier";
  case nua_r_terminate: return "nua_r_terminate";

  case nua_r_register: return "nua_r_register";
  case nua_r_unregister: return "nua_r_unregister";
  case nua_r_invite: return "nua_r_invite";
  case nua_r_bye: return "nua_r_bye";
  case nua_r_options: return "nua_r_options";
  case nua_r_refer: return "nua_r_refer";
  case nua_r_publish: return "nua_r_publish";
  case nua_r_info: return "nua_r_info";
  case nua_r_update: return "nua_r_update";
  case nua_r_message: return "nua_r_message";
  case nua_r_chat: return "nua_r_chat";
  case nua_r_subscribe: return "nua_r_subscribe";
  case nua_r_unsubscribe: return "nua_r_unsubscribe";
  case nua_r_notify: return "nua_r_notify";

  case nua_r_setup: return "nua_r_setup (rtsp)";
  case nua_r_play: return "nua_r_play (rtsp)";
  case nua_r_record: return "nua_r_record (rtsp)";
  case nua_r_pause: return "nua_r_pause (rtsp)";
  case nua_r_describe: return "nua_r_describe (rtsp)";
  case nua_r_teardown: return "nua_r_teardown (rtsp)";
  case nua_r_options2: return "nua_r_options2 (rtsp)";
  case nua_r_announce: return "nua_r_announce (rtsp)";
  case nua_r_get_parameter: return "nua_r_get_parameter (rtsp)";
  case nua_r_set_parameter: return "nua_r_set_parameter (rtsp)";

  case nua_r_method: return "nua_r_method";

  case nua_r_cancel: return "nua_r_cancel";
  case nua_r_authenticate: return "nua_r_authenticate";
  case nua_r_redirect: return "nua_r_redirect";
  case nua_r_destroy: return "nua_r_destroy";
  case nua_r_respond: return "nua_r_respond";
  case nua_r_set_params: return "nua_r_set_params";
  case nua_r_ack: return "nua_r_ack";
  default: return "NUA_UNKNOWN";
  }
}


/* ----------------------------------------------------------------------
 * Post signal to stack client
 */
static
void stack_signal(nua_handle_t *nh, nua_event_t event, 
		  tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  ta_start(ta, tag, value);
  nua_signal((nh)->nh_nua, nh, NULL, 1, event, 0, NULL, ta_tags(ta)); 
  ta_end(ta);
}


/* ----------------------------------------------------------------------
 * Receiving events from client
 */
void ua_signal(nua_t *nua, su_msg_r msg, event_t *e)
{
  nua_handle_t *nh = e->e_nh;
  tagi_t *tags = e->e_tags;

  if (nh) {
    if (!nh->nh_prev)
      nh_append(nua, nh);
    if (!nh->nh_ref_by_stack) {
      nh->nh_ref_by_stack = 1;
      nh_incref(nh);
    }      
  }

  if (nua_log->log_level >= 5) {
    char const *name = nua_event_name(e->e_event);
    if (e->e_status == 0)
      SU_DEBUG_5(("nua(%p): signal %s\n", nh, name + 4));
    else
      SU_DEBUG_5(("nua(%p): signal %s %u %s\n", 
		  nh, name + 4, e->e_status, e->e_phrase ? e->e_phrase : ""));
  }

  if (nua->nua_shutdown && !e->e_always) {
    /* Shutting down */
    ua_event(nua, nh, NULL, e->e_event, 500, "Stack is going down", TAG_END());
  }
  else switch (e->e_event) {
  case nua_r_get_params:
    ua_get_params(nua, nh, e->e_event, tags);
    break;
  case nua_r_set_params:
    ua_set_params(nua, nh, e->e_event, tags);
    break;
  case nua_r_shutdown:
    ua_shutdown(nua);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
729
#if 0
Pekka Pessi's avatar
Pekka Pessi committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  case nua_r_set_media_param:
    ua_set_media_param(nua, nh, e->e_event, tags);
    break;
  case nua_r_get_media_param:
    ua_get_media_param(nua, nh, e->e_event, tags);
    break;
  case nua_r_media_setup:
    ua_media_setup(nua, nh, e->e_event, tags);
    break;
  case nua_r_media_describe:
    ua_media_describe(nua, nh, e->e_event, tags);
    break;
  case nua_r_media_event:
    ua_media_event(nua, nh, e->e_event, tags);
    break;
Pekka Pessi's avatar
Pekka Pessi committed
745
#endif
Pekka Pessi's avatar
Pekka Pessi committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
  case nua_r_register:
  case nua_r_unregister:
    ua_register(nua, nh, e->e_event, tags);
    break;
  case nua_r_invite:
    ua_invite(nua, nh, e->e_event, tags);
    break;
  case nua_r_cancel:
    ua_cancel(nua, nh, e->e_event, tags);
    break;
  case nua_r_bye:
    ua_bye(nua, nh, e->e_event, tags);
    break;
  case nua_r_options:
    ua_options(nua, nh, e->e_event, tags);
    break;
  case nua_r_refer:
    ua_refer(nua, nh, e->e_event, tags);
    break;
  case nua_r_publish:
    ua_publish(nua, nh, e->e_event, tags);
    break;
  case nua_r_info:
    ua_info(nua, nh, e->e_event, tags);
    break;
  case nua_r_update:
    ua_update(nua, nh, e->e_event, tags);
    break;
  case nua_r_message:
    ua_message(nua, nh, e->e_event, tags);
    break;
  case nua_r_subscribe:
  case nua_r_unsubscribe:
    ua_subscribe(nua, nh, e->e_event, tags);
    break;
  case nua_r_notify:
    ua_notify(nua, nh, e->e_event, tags);
    break;
  case nua_r_notifier:
    ua_notifier(nua, nh, e->e_event, tags);
    break;
  case nua_r_terminate:
    ua_terminate(nua, nh, e->e_event, tags);
    break;
  case nua_r_method:
    ua_method(nua, nh, e->e_event, tags);
    break;
  case nua_r_authenticate:
    ua_authenticate(nua, nh, e->e_event, tags);
    break;
  case nua_r_ack:
    ua_ack(nua, nh, tags);
    break;
  case nua_r_respond:
    ua_respond(nua, nh, e->e_status, e->e_phrase, tags);
    break;
  case nua_r_destroy:
    ua_destroy(nua, nh, tags);
    break;
  default:
    break;
  }

  if (nh != nua->nua_default)
    nh_decref(nh);
}

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

static int nh_call_pending(nua_handle_t *nh, sip_time_t time);

/** Timer routine.
 *
 * Go through all handles and execute pending tasks
 */
void ua_timer(nua_t *nua, su_timer_t *t, su_timer_arg_t *a)
{
  nua_handle_t *nh, *nh_next;
  sip_time_t now = sip_now();

  su_timer_set(t, ua_timer, a);

  if (nua->nua_shutdown) {
    ua_shutdown(nua);
    return;
  }

  for (nh = nua->nua_handles; nh; nh = nh_next) {
    nh_next = nh->nh_next;
    nh_call_pending(nh, now);
  }
}


static
int nh_call_pending(nua_handle_t *nh, sip_time_t now)
{
  nua_dialog_usage_t *du;
  sip_time_t next = now + UA_INTERVAL;

  for (du = nh->nh_ds->ds_usage; du; du = du->du_next) {
    if (!du->du_pending)
      continue;
    if (now == 0 || (du->du_refresh && du->du_refresh < next))
      break;
  }

  if (du == NULL)
    return 0;

  nh_incref(nh);

  while (du) {
    nh_pending_f *pending = du->du_pending;
    nua_dialog_usage_t *du_next = du->du_next;

    du->du_pending = NULL;

    pending(nh, du, now);

    if (du_next == NULL)
      break;

    for (du = nh->nh_ds->ds_usage; du; du = du->du_next)
      if (du == du_next)
	break;

    for (; du; du = du->du_next) {
      if (!du->du_pending)
	continue;
      if (now == 0 || (du->du_refresh && du->du_refresh < next))
	break;
    }
  }

  nh_decref(nh);

  return 1;
}


/** Set refresh value suitably */
static 
void dialog_usage_set_refresh(nua_dialog_usage_t *du, unsigned delta)
{
  sip_time_t target = sip_now();

  if (delta > 60 && delta < 5 * 60)
    /* refresh 30 seconds before deadline */
    delta -= 30;
  else 
    /* refresh at half time before deadline */
    delta /= 2;

  if (target + delta >= target)
    target = target + delta;
  else
    target = SIP_TIME_MAX;

  du->du_refresh = target;
}

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

/** Shut down stack. */
void ua_shutdown(nua_t *nua)
{
  nua_handle_t *nh, *nh_next;
  int busy = 0;
  sip_time_t now = sip_now();
  int status;
  char const *phrase;
  struct nua_session_state *ss;

  enter;

  if (!nua->nua_shutdown)
    nua->nua_shutdown = now;

  for (nh = nua->nua_handles; nh; nh = nh_next) {
    nh_next = nh->nh_next;

    ss = nh->nh_ss;

Pekka Pessi's avatar
Pekka Pessi committed
930 931
    if (ss->ss_srequest->sr_respond) {
      ss->ss_srequest->sr_respond(nua, nh, SIP_410_GONE, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
932 933 934 935 936
      busy++;
    }

    busy += nh_call_pending(nh, 0);

Pekka Pessi's avatar
Pekka Pessi committed
937 938
    if (nh->nh_soa) {
      soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
      busy++;
    }

    if (nh->nh_cr->cr_orq || nh->nh_ss->ss_crequest->cr_orq)
      busy++;

    if (nh_notifier_shutdown(nh, NULL, NEATAG_REASON("noresource"), TAG_END()))
      busy++;
  }


  if (!busy)
    SET_STATUS(200, "Shutdown successful");
  else if (now == nua->nua_shutdown)
    SET_STATUS(100, "Shutdown started");
  else if (now - nua->nua_shutdown < 30)
    SET_STATUS(101, "Shutdown in progress");
  else
    SET_STATUS(500, "Shutdown timeout");

  ua_event(nua, NULL, NULL, nua_r_shutdown, status, phrase, TAG_END());
}

/* ----------------------------------------------------------------------
 * Parameters
 */
void ua_set_params(nua_t *nua, nua_handle_t *nh, nua_event_t e, 
		   tagi_t const *tags)
{
  su_home_t     tmphome[1] = { SU_HOME_INIT(tmphome) }; 

  int           autoAlert = nua->nua_autoAlert;
  int           autoAnswer = nua->nua_autoAnswer;
  int           autoACK = nua->nua_default->nh_auto_ack;
  int           enableInvite = nua->nua_enableInvite;
  int           enableMessage = nua->nua_enableMessage;
  int           enableMessenger = nua->nua_enableMessenger;
  int           early_media = nua->nua_default->nh_early_media;
Pekka Pessi's avatar
Pekka Pessi committed
977 978

#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
979 980 981 982 983 984 985 986
  int           smime_enable = nua->sm->sm_enable;
  int           smime_opt = nua->sm->sm_opt;
  int           smime_protection_mode = nua->sm->sm_protection_mode;
  char const   *smime_message_digest = NONE;
  char const   *smime_signature = NONE;
  char const   *smime_key_encryption = NONE;
  char const   *smime_message_encryption = NONE;
  char const   *smime_path = NONE;
Pekka Pessi's avatar
Pekka Pessi committed
987 988 989 990 991
#endif			

  int           invite_timer = nua->nua_invite_timer;
  int           session_timer = nua->nua_session_timer;
  int           min_se = nua->nua_min_se;
Pekka Pessi's avatar
Pekka Pessi committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  int           refresher = nua->nua_refresher;
  int           update_refresh = nua->nua_default->nh_update_refresh;
  int           media_features = nua->nua_default->nh_media_features;
  int           callee_caps = nua->nua_default->nh_callee_caps;
  int           path_enable = nua->nua_path_enable;
  int           service_route_enable = nua->nua_service_route_enable;
  url_string_t const *registrar = NONE;
  sip_from_t const *from = NONE;
  char const   *fromstr = NONE;
  char const   *allow_str = NONE, *supported_str = NONE;
  sip_allow_t const *allow = NONE;
  sip_supported_t const *supported = NONE;
  sip_organization_t const *organization = NONE;
  char const   *organization_str = NONE;
  char const   *user_agent = nua->nua_ua_name;

  enter;

  nta_agent_set_params(nua->nua_nta, TAG_NEXT(tags));
Pekka Pessi's avatar
Pekka Pessi committed
1011 1012
  
  soa_set_params(nua->nua_default->nh_soa, TAG_NEXT(tags));
Pekka Pessi's avatar
Pekka Pessi committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

  if (!tl_gets(tags,
	       NUTAG_INVITE_TIMER_REF(invite_timer),
	       NUTAG_SESSION_TIMER_REF(session_timer),
	       NUTAG_MIN_SE_REF(min_se),
	       NUTAG_SESSION_REFRESHER_REF(refresher),
	       NUTAG_UPDATE_REFRESH_REF(update_refresh),
	       NUTAG_EARLY_MEDIA_REF(early_media),
	       NUTAG_AUTOALERT_REF(autoAlert),
	       NUTAG_AUTOANSWER_REF(autoAnswer),
	       NUTAG_AUTOACK_REF(autoACK),
	       NUTAG_ENABLEINVITE_REF(enableInvite),
	       NUTAG_ENABLEMESSAGE_REF(enableMessage),
	       NUTAG_ENABLEMESSENGER_REF(enableMessenger),
	       NUTAG_CALLEE_CAPS_REF(callee_caps),
	       NUTAG_PATH_ENABLE_REF(path_enable),
	       NUTAG_SERVICE_ROUTE_ENABLE_REF(service_route_enable),
Pekka Pessi's avatar
Pekka Pessi committed
1030
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 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 1097 1098
	       NUTAG_SMIME_ENABLE_REF(smime_enable),
	       NUTAG_SMIME_OPT_REF(smime_opt),
	       NUTAG_SMIME_PROTECTION_MODE_REF(smime_protection_mode),
	       NUTAG_SMIME_MESSAGE_DIGEST_REF(smime_message_digest),
	       NUTAG_SMIME_SIGNATURE_REF(smime_signature),
	       NUTAG_SMIME_KEY_ENCRYPTION_REF(smime_key_encryption),
	       NUTAG_SMIME_MESSAGE_ENCRYPTION_REF(smime_message_encryption),
	       NUTAG_CERTIFICATE_DIR_REF(smime_path),
#endif
#if HAVE_SRTP
	       NUTAG_SRTP_ENABLE_REF(nua->srtp->srtp_enable),
	       NUTAG_SRTP_INTEGRITY_PROTECTION_REF(nua->srtp->srtp_integrity_protection),
	       NUTAG_SRTP_CONFIDENTIALITY_REF(nua->srtp->srtp_confidentiality),
#endif
	       NUTAG_REGISTRAR_REF(registrar),
	       SIPTAG_FROM_REF(from),
	       SIPTAG_FROM_STR_REF(fromstr),
	       SIPTAG_ORGANIZATION_REF(organization),
	       SIPTAG_ORGANIZATION_STR_REF(organization_str),
	       SIPTAG_SUPPORTED_REF(supported),
	       SIPTAG_SUPPORTED_STR_REF(supported_str),
	       SIPTAG_ALLOW_REF(allow),
	       SIPTAG_ALLOW_STR_REF(allow_str),
	       NUTAG_USER_AGENT_REF(user_agent),
	       NUTAG_MEDIA_FEATURES_REF(media_features),
	       TAG_NULL()))
    return;

#if 0
  reinit_contact = 
    nua->nua_default->nh_callee_caps != callee_caps ||
    media_path != NONE ||
    allow != NONE || allow_str != NONE;
#endif

  if (invite_timer > 0 && invite_timer < 30)
    invite_timer = 30;
  nua->nua_invite_timer = invite_timer;
  nua->nua_session_timer = session_timer;
  if (min_se > 0 && min_se < 30)
    min_se = 30;
  nua->nua_min_se = min_se;
  if (session_timer > 0) {
    if (session_timer < 30)
      session_timer = 30;
    if (session_timer < min_se)
      session_timer = min_se;
  }
  if (refresher >= nua_remote_refresher)
    nua->nua_refresher = nua_remote_refresher;
  else if (refresher <= nua_no_refresher)
    nua->nua_refresher = nua_no_refresher;
  else
    nua->nua_refresher = refresher;

  nua->nua_default->nh_update_refresh = update_refresh != 0;
  nua->nua_autoAlert = autoAlert != 0;
  nua->nua_autoAnswer = autoAnswer != 0;
  nua->nua_enableInvite = enableInvite != 0;
  nua->nua_enableMessage = enableMessage != 0;
  nua->nua_enableMessenger = enableMessenger != 0;
  nua->nua_default->nh_auto_ack = autoACK != 0;
  nua->nua_default->nh_early_media = early_media != 0;
  nua->nua_default->nh_media_features = media_features != 0;
  nua->nua_default->nh_callee_caps = callee_caps != 0;
  nua->nua_path_enable = path_enable != 0;
  nua->nua_service_route_enable = service_route_enable != 0;
  
Pekka Pessi's avatar
Pekka Pessi committed
1099
#if HAVE_SOFIA_SMIME 
Pekka Pessi's avatar
Pekka Pessi committed
1100 1101
  /* XXX - all S/MIME other parameters? */
  sm_set_params(nua->sm, smime_enable, smime_opt, smime_protection_mode, smime_path);
Pekka Pessi's avatar
Pekka Pessi committed
1102
#endif                  
Pekka Pessi's avatar
Pekka Pessi committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 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 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

#define update_header(nua, name, header, str) \
  if (header != NONE || str != NONE) { \
    sip_##name##_t *new_header; \
    if (header != NONE) \
      new_header = sip_##name##_dup(nua->nua_home, header); \
    else  \
      new_header = sip_##name##_make(nua->nua_home, str); \
    if (new_header != NULL || (header == NULL || str == NULL)) { \
      if (nua->nua_##name != NONE)  \
	su_free(nua->nua_home, nua->nua_##name); \
      nua->nua_##name = new_header; \
    } \
  }

  update_header(nua, supported, supported, supported_str);
  update_header(nua, allow, allow, allow_str);

  if (organization == NULL) {
    su_free(nua->nua_home, (void *)nua->nua_organization);
    nua->nua_organization = NULL;
  }
  else if (organization != NONE &&
	   str0cmp(organization->g_string, nua->nua_organization)) {
    su_free(nua->nua_home, (void *)nua->nua_organization);
    nua->nua_organization = su_strdup(nua->nua_home, organization->g_string);
  }
  else if (organization_str == NULL) {
    su_free(nua->nua_home, (void *)nua->nua_organization);
    nua->nua_organization = NULL;
  }
  else if (organization_str != NONE &&
	   str0cmp(organization_str, nua->nua_organization)) {
    su_free(nua->nua_home, (void *)nua->nua_organization);
    nua->nua_organization = su_strdup(nua->nua_home, organization_str);
  } 

  if (registrar != NONE) {
    if (registrar &&
	(url_string_p(registrar) ? 
	 strcmp(registrar->us_str, "*") == 0 :
	 registrar->us_url->url_type == url_any))
      registrar = NULL;
    su_free(nua->nua_home, nua->nua_registrar);
    nua->nua_registrar = url_hdup(nua->nua_home, registrar->us_url);
  }

  if (from != NONE) {
    /* XXX We leak here: su_free(nua->nua_home, nua->nua_from); */
    ua_set_from(nua, sip_from_dup(nua->nua_home, from));
  }
  else if (fromstr != NONE) {
    /* XXX su_free(nua->nua_home, nua->nua_from); */
    if (fromstr && fromstr[0])
      ua_set_from(nua, sip_from_make(nua->nua_home, fromstr));
    else
      ua_set_from(nua, NULL);
  }

  if (user_agent != nua->nua_ua_name) {
    char *me = su_strdup(nua->nua_home, user_agent);

    su_free(nua->nua_home, (void *)nua->nua_ua_name);
    nua->nua_ua_name = me;

    su_free(nua->nua_home, (void *)nua->nua_user_agent);
    nua->nua_user_agent = 
      sip_user_agent_format(nua->nua_home, "%s%snua/" NUA_VERSION " %s",
			    me ? me : "", me ? " " : "", 
			    nta_agent_version(nua->nua_nta));
  }

  ua_init_contact(nua);

  su_home_deinit(tmphome);
}

void
ua_get_params(nua_t *nua, nua_handle_t *nh, nua_event_t e, tagi_t const *tags)
{
  tagi_t *lst, *params;
  sip_from_t from[1];

  unsigned udp_mtu = 0, sip_t1 = 0, sip_t2 = 0, sip_t4 = 0, sip_t1x64 = 0;
  unsigned debug_drop_prob = 0;
  url_string_t const *proxy = NULL;
  sip_contact_t const *aliases = NULL;
  unsigned flags = 0;
  sip_organization_t organization[1];
Pekka Pessi's avatar
Pekka Pessi committed
1192

1193
  tagi_t *media_params = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1194 1195

  su_home_t home[1] = { SU_HOME_INIT(home) };
Pekka Pessi's avatar
Pekka Pessi committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214

  enter;

  nta_agent_get_params(nua->nua_nta,
		       NTATAG_UDP_MTU_REF(udp_mtu),
		       NTATAG_SIP_T1_REF(sip_t1),
		       NTATAG_SIP_T2_REF(sip_t2),
		       NTATAG_SIP_T4_REF(sip_t4),
		       NTATAG_SIP_T1X64_REF(sip_t1x64),
		       NTATAG_DEBUG_DROP_PROB_REF(debug_drop_prob),
		       NTATAG_DEFAULT_PROXY_REF(proxy),
		       NTATAG_ALIASES_REF(aliases),
		       NTATAG_SIPFLAGS_REF(flags),
		       TAG_END());

  *from = *nua->nua_from; from->a_params = NULL;

  sip_organization_init(organization)->g_string = nua->nua_organization;

1215 1216
  if (nh)
    media_params = soa_get_paramlist(nh->nh_soa);
Pekka Pessi's avatar
Pekka Pessi committed
1217

Pekka Pessi's avatar
Pekka Pessi committed
1218
  params = tl_list(NUTAG_MEDIA_ENABLE(nua->nua_media_enable),
Pekka Pessi's avatar
Pekka Pessi committed
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
		   NUTAG_EARLY_MEDIA(nua->nua_default->nh_early_media),
		   NUTAG_INVITE_TIMER(nua->nua_invite_timer),
		   NUTAG_SESSION_TIMER(nua->nua_session_timer),
		   NUTAG_MIN_SE(nua->nua_min_se),
		   NUTAG_SESSION_REFRESHER(nua->nua_default->nh_ss->ss_refresher),
		   NUTAG_UPDATE_REFRESH(nua->nua_default->nh_update_refresh),
		   NUTAG_AUTOALERT(nua->nua_autoAlert),
		   NUTAG_AUTOANSWER(nua->nua_autoAnswer),
		   NUTAG_AUTOACK(nua->nua_default->nh_auto_ack),
		   NUTAG_ENABLEINVITE(nua->nua_enableInvite),
		   NUTAG_ENABLEMESSAGE(nua->nua_enableMessage),
		   NUTAG_ENABLEMESSENGER(nua->nua_enableMessenger),
		   NUTAG_CALLEE_CAPS(nua->nua_default->nh_callee_caps),
		   NUTAG_PATH_ENABLE(nua->nua_path_enable),
		   NUTAG_SERVICE_ROUTE_ENABLE(nua->nua_service_route_enable),
Pekka Pessi's avatar
Pekka Pessi committed
1234
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
1235 1236 1237 1238 1239 1240 1241
		   NUTAG_SMIME_ENABLE(nua->sm->sm_enable),
		   NUTAG_SMIME_OPT(nua->sm->sm_opt),
		   NUTAG_SMIME_PROTECTION_MODE(nua->sm->sm_protection_mode),
		   NUTAG_SMIME_MESSAGE_DIGEST(nua->sm->sm_message_digest),
		   NUTAG_SMIME_SIGNATURE(nua->sm->sm_signature),
		   NUTAG_SMIME_KEY_ENCRYPTION(nua->sm->sm_key_encryption),
		   NUTAG_SMIME_MESSAGE_ENCRYPTION(nua->sm->sm_message_encryption),
Pekka Pessi's avatar
Pekka Pessi committed
1242
#endif                  
Pekka Pessi's avatar
Pekka Pessi committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
#if HAVE_SRTP
		   NUTAG_SRTP_ENABLE(nua->srtp->srtp_enable),
		   NUTAG_SRTP_CONFIDENTIALITY(nua->srtp->srtp_confidentiality),
		   NUTAG_SRTP_INTEGRITY_PROTECTION(nua->srtp->srtp_integrity_protection),
#endif
		   NUTAG_REGISTRAR(nua->nua_registrar),
		   NTATAG_CONTACT(nua->nua_contact ? nua->nua_contact :
				  nua->nua_sips_contact),
		   SIPTAG_FROM(from),
		   SIPTAG_ALLOW(nua->nua_allow),
		   SIPTAG_SUPPORTED(nua->nua_supported),
		   SIPTAG_ORGANIZATION(nua->nua_organization ? 
				       organization : NULL),
		   SIPTAG_ORGANIZATION_STR(nua->nua_organization),
		   NTATAG_UDP_MTU(udp_mtu),
		   NTATAG_SIP_T1(sip_t1),
		   NTATAG_SIP_T2(sip_t2),
		   NTATAG_SIP_T4(sip_t4),
		   NTATAG_SIP_T1X64(sip_t1x64),
		   NTATAG_DEBUG_DROP_PROB(debug_drop_prob),
		   NTATAG_DEFAULT_PROXY(proxy),
		   NTATAG_ALIASES(aliases),
		   NTATAG_SIPFLAGS(flags),
Pekka Pessi's avatar
Pekka Pessi committed
1266 1267 1268
		   TAG_NEXT(media_params));

  lst = tl_afilter(home, tags, params);
Pekka Pessi's avatar
Pekka Pessi committed
1269 1270

  ua_event(nua, nh, NULL, nua_r_get_params, SIP_200_OK, TAG_NEXT(lst));
Pekka Pessi's avatar
Pekka Pessi committed
1271 1272 1273 1274

  su_home_deinit(home);

  tl_vfree(media_params);
Pekka Pessi's avatar
Pekka Pessi committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
  tl_vfree(params);
}

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

static void crequest_deinit(struct nua_client_request *cr, 
			    nta_outgoing_t *orq);

/** Create a handle */
nua_handle_t *nh_create(nua_t *nua, tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  nua_handle_t *nh;

  enter;

  ta_start(ta, tag, value);
  nh = nh_create_handle(nua, NULL, ta_args(ta));
  ta_end(ta);
	
  if (nh) {
    nh->nh_ref_by_stack = 1;
    nh_append(nua, nh);
  }

  return nh;
}

/** Append an handle to the list of handles */
void nh_append(nua_t *nua, nua_handle_t *nh)
{
  nh->nh_next = NULL;
  nh->nh_prev = nua->nua_handles_tail;
  *nua->nua_handles_tail = nh;
  nua->nua_handles_tail = &nh->nh_next;
}

int nh_is_special(nua_handle_t *nh)
{
  return nh == NULL || nh->nh_special;
}

static inline
int nh_has_session(nua_handle_t const *nh)
{
  return nh != NULL && 
    nh->nh_ss->ss_state > init_session &&
    nh->nh_ss->ss_state < terminated_session;
}

static inline
nua_handle_t *nh_validate(nua_t *nua, nua_handle_t *maybe)
{
  nua_handle_t *nh;

  if (maybe)
    for (nh = nua->nua_handles; nh; nh = nh->nh_next)
      if (nh == maybe)
	return nh;

  return NULL;
}

#define nh_is_inserted(nh) ((nh)->nh_prev != NULL)

/** Remove a handle from list of handles */
static
void nh_remove(nua_t *nua, nua_handle_t *nh)
{
  assert(nh_is_inserted(nh)); assert(*nh->nh_prev == nh);

  if (nh->nh_next)
    nh->nh_next->nh_prev = nh->nh_prev;
  else
    nua->nua_handles_tail = nh->nh_prev;

  *nh->nh_prev = nh->nh_next;

  nh->nh_prev = NULL;
  nh->nh_next = NULL;
}

static
void nh_destroy(nua_t *nua, nua_handle_t *nh)
{
  assert(nh); assert(nh != nua->nua_default);

  nh_enter;

  if (nh->nh_notifier)
    nea_server_destroy(nh->nh_notifier), nh->nh_notifier = NULL;

  crequest_deinit(nh->nh_cr, NULL);
  if (nh->nh_ss)
    crequest_deinit(nh->nh_ss->ss_crequest, NULL);

  if (nh->nh_ds->ds_leg) {
    nta_leg_destroy(nh->nh_ds->ds_leg), nh->nh_ds->ds_leg = NULL;
  }

Pekka Pessi's avatar
Pekka Pessi committed
1375 1376 1377
  if (nh->nh_ss->ss_srequest->sr_irq) {
    nta_incoming_destroy(nh->nh_ss->ss_srequest->sr_irq);
    nh->nh_ss->ss_srequest->sr_irq = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1378 1379
  }

Pekka Pessi's avatar
Pekka Pessi committed
1380 1381
  if (nh->nh_soa)
    soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

  if (nh_is_inserted(nh))
    nh_remove(nua, nh);

  nh_decref(nh);
}

static
void crequest_deinit(struct nua_client_request *cr, nta_outgoing_t *orq)
{
  if (orq == NULL || orq == cr->cr_orq) {
    cr->cr_retry_count = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1394
    cr->cr_offer_sent = cr->cr_answer_recv = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

    if (cr->cr_msg)
      msg_destroy(cr->cr_msg);
    cr->cr_msg = NULL;

    if (cr->cr_orq)
      nta_outgoing_destroy(cr->cr_orq);
    cr->cr_orq = NULL;
  }
  else {
    nta_outgoing_destroy(orq);
  }
}

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

/** Initialize handle Allow and authentication info */
static
void nh_init(nua_t *nua, nua_handle_t *nh, 
	     enum nh_kind kind,
	     char const *default_allow,
	     tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;

  nua_handle_t *nh_default = nua->nua_default;

  int media_enable = nua->nua_media_enable;
  int media_features = nh_default->nh_media_features;
  int autoACK = nh_default->nh_auto_ack;
  int early_media = nh_default->nh_early_media;
  int update_refresh = nh_default->nh_update_refresh;

  sip_allow_t const *allow = NULL;
  char const *allowstr = NULL;

  if (kind && !nh_is_special(nh) && !nh->nh_has_invite) {
    switch (kind) {
    case nh_has_invite:    nh->nh_has_invite = 1;    break;
    case nh_has_subscribe: nh->nh_has_subscribe = 1; break;
    case nh_has_notify:    nh->nh_has_notify = 1;    break;
    case nh_has_register:  nh->nh_has_register = 1;  break;
    case nh_has_streaming: nh->nh_has_streaming = 1; break;
    case nh_has_nothing: 
    default:
      break;
    }
  }

  if (nh->nh_init)		  /* Already initialized? */
    return;

  ta_start(ta, tag, value);

  tl_gets(nh->nh_tags,
	  SIPTAG_ALLOW_REF(allow),
	  SIPTAG_ALLOW_STR_REF(allowstr),
	  NUTAG_ALLOW_REF(default_allow),
	  NUTAG_MEDIA_ENABLE_REF(media_enable),
	  NUTAG_MEDIA_FEATURES_REF(media_features),
	  NUTAG_AUTOACK_REF(autoACK),
	  NUTAG_EARLY_MEDIA_REF(early_media),
	  NUTAG_UPDATE_REFRESH_REF(update_refresh),
	  TAG_END());

#if HAVE_UICC_H
  if (nh->nh_has_register && nua->nua_uicc)
    auc_with_uicc(&nh->nh_auth, nh->nh_home, nua->nua_uicc);
#endif

  if (nh->nh_tags)
    nh_authorize(nh, TAG_NEXT(nh->nh_tags));

  if (allow)
    nh->nh_allow = sip_allow_dup(nh->nh_home, allow);
  else if (allowstr)
    nh->nh_allow = sip_allow_make(nh->nh_home, allowstr);
  else if (default_allow)
    nh->nh_allow = sip_allow_make(nh->nh_home, default_allow);

  if (nh->nh_allow == NULL)
    nh->nh_allow = nua->nua_allow;

  nh->nh_ss->ss_min_se = nua->nua_min_se;
  nh->nh_ss->ss_session_timer = nua->nua_session_timer;
  nh->nh_ss->ss_refresher = nua->nua_refresher;

  nh->nh_early_media = early_media;
  nh->nh_media_features = media_features;
  nh->nh_update_refresh = update_refresh;
  nh->nh_auto_ack = autoACK != 0;

1487 1488 1489 1490 1491 1492 1493
  if (media_enable && nua->nua_default->nh_soa) {
    soa_session_t *soa;

    soa = soa_clone(nua->nua_default->nh_soa, nua->nua_root, nh);

    if (soa)
      nh->nh_soa = soa;
Pekka Pessi's avatar
Pekka Pessi committed
1494 1495

    if (nh->nh_tags)
Pekka Pessi's avatar
Pekka Pessi committed
1496 1497
      soa_set_params(soa, TAG_NEXT(nh->nh_tags));
    soa_set_params(soa, ta_tags(ta));
Pekka Pessi's avatar
Pekka Pessi committed
1498 1499 1500 1501 1502 1503 1504 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 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
  }

  ta_end(ta);

  nh->nh_init = 1;
}

/** Create a handle for processing incoming request */
static
nua_handle_t *nh_create_from_incoming(nua_t *nua, 
				      nta_incoming_t *irq,
				      sip_t const *sip,
				      enum nh_kind kind,
				      int create_dialog)
{
  nua_handle_t *nh;
  url_t const *url;
  char const *default_allow = NULL; /* XXX - should be argument? */
  sip_to_t to[1];
  sip_from_t from[1];

  assert(sip && sip->sip_from && sip->sip_to);

  if (sip->sip_contact)
    url = sip->sip_contact->m_url;
  else
    url = sip->sip_from->a_url;

  sip_from_init(from)->a_display = sip->sip_to->a_display;
  *from->a_url = *sip->sip_to->a_url;

  sip_to_init(to)->a_display = sip->sip_from->a_display;
  *to->a_url = *sip->sip_from->a_url;

  nh = nh_create(nua,
		 NUTAG_URL((url_string_t *)url),
		 SIPTAG_TO(to), /* Local address */
		 SIPTAG_FROM(from), /* Remote address */
		 TAG_END());

  if (nh) {
    nh_init(nh->nh_nua, nh, kind, default_allow, TAG_END());

    if (create_dialog) {
      struct nua_dialog_state *ds = nh->nh_ds;

      dialog_get_peer_info(nh, sip);

      ds->ds_leg = nta_leg_tcreate(nua->nua_nta, process_request, nh,
				   SIPTAG_CALL_ID(sip->sip_call_id),
				   SIPTAG_FROM(sip->sip_to),
				   SIPTAG_TO(sip->sip_from),
				   NTATAG_REMOTE_CSEQ(sip->sip_cseq->cs_seq),
				   TAG_END());

      nta_leg_tag(ds->ds_leg, nta_incoming_tag_3261(irq, NULL));

      if (!ds->ds_leg)
	nh_destroy(nua, nh), nh = NULL;
    }
  }

  if (nh)
    dialog_uas_route(nh, sip, 1);

  return nh;
}


/** Add authorization data */
int nh_authorize(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...)
{
  int retval = 0;
  tagi_t const *ti;
  ta_list ta;

  ta_start(ta, tag, value);

  for (ti = ta_args(ta); ti; ti = tl_next(ti)) {
    if (ti->t_tag == nutag_auth && ti->t_value) {
      char *data = (char *)ti->t_value;
      int rv = auc_credentials(&nh->nh_auth, nh->nh_home, data);

      if (rv > 0) {
	retval = 1;
      } 
      else if (rv < 0) {
	retval = -1; 
	break;
      }
    }
  }

  ta_end(ta);

  return retval;
}

/** Collect challenges from response */
static
int nh_challenge(nua_handle_t *nh, sip_t const *sip)
{
  if (sip->sip_www_authenticate)
    auc_challenge(&nh->nh_auth, nh->nh_home, sip->sip_www_authenticate,
		  sip_authorization_class);

  if (sip->sip_proxy_authenticate)
    auc_challenge(&nh->nh_auth, nh->nh_home, sip->sip_proxy_authenticate,
		      sip_proxy_authorization_class);

  return 0;
}

/** Create request message.
 *
 * @param nua  
 * @param nh
 * @param method
 * @param name
 * @param tag @a value list of tag-value pairs
 */
static
msg_t *crequest_message(nua_t *nua, nua_handle_t *nh,
			struct nua_client_request *cr,
			int restart, 
			sip_method_t method, char const *name,
			tag_type_t tag, tag_value_t value, ...)
{
  struct nua_dialog_state *ds = nh->nh_ds;
  msg_t *msg;
  sip_t *sip;
  url_string_t const *url = NULL;
  long seq = -1;
  int copy = 1;

  /* If restarting, use existing message */
  if (restart) {
    msg = cr->cr_msg; sip = sip_object(msg);

    /* Trying to restart different method? */
    if (sip && method && sip->sip_request->rq_method != method) {
      SU_DEBUG_3(("nua(%p): trying to %s "
		  "but there is already %s waiting to restart\n",
		  nh, name, sip->sip_request->rq_method_name));
      restart = 0, msg = NULL; sip = NULL;
    }

    /* Remove CSeq */
    if (sip && sip->sip_cseq)
      sip_header_remove(msg, sip, (sip_header_t *)sip->sip_cseq);
    if (sip && sip->sip_request)
      method = sip->sip_request->rq_method, 
	name = sip->sip_request->rq_method_name;
  }

  if (!restart) {
    if (cr->cr_msg) {
      /* If method is ACK or CANCEL, use existing CSeq */
      if (method == sip_method_ack || method == sip_method_cancel) {
	sip_t *nh_sip = sip_object(cr->cr_msg);
	if (nh_sip && nh_sip->sip_cseq)
	  seq = nh_sip->sip_cseq->cs_seq;
	copy = 0;
      }
      else
	nta_msg_discard(nua->nua_nta, cr->cr_msg), cr->cr_msg = NULL;
    }
    msg = nta_msg_create(nua->nua_nta, 0);
    tl_gets(nh->nh_tags, NUTAG_URL_REF(url), TAG_END());
    sip_add_tl(msg, sip_object(msg), TAG_NEXT(nh->nh_tags));
  }

  if (msg) {
    ta_list ta;
    int use_leg = 0, add_contact = 0;

    sip = sip_object(msg);

    ta_start(ta, tag, value);

    tl_gets(ta_args(ta),
	    NUTAG_URL_REF(url),
	    NUTAG_USE_LEG_REF(use_leg),
	    /* NUTAG_COPY_REF(copy), */
	    NUTAG_ADD_CONTACT_REF(add_contact),
	    TAG_END());

    if (method == sip_method_register && url == NULL) {
      tl_gets(ta_args(ta), NUTAG_REGISTRAR_REF(url), TAG_END());
      if (url == NULL)
	tl_gets(nh->nh_tags, NUTAG_REGISTRAR_REF(url), TAG_END());
      if (url == NULL)
	url = (url_string_t *)nua->nua_registrar;
    }

    if (seq != -1) {
      sip_cseq_t *cseq = 
	sip_cseq_create(msg_home(msg), seq, method, name);
      sip_header_insert(msg, sip, (sip_header_t *)cseq);
    }

    if (ds->ds_leg) {
      /* If leg has established route, use it, not original URL */
      if (ds->ds_route) 
	url = NULL;

      if (sip_add_tl(msg, sip, ta_tags(ta)) < 0 ||
	  nta_msg_request_complete(msg, ds->ds_leg, method, name, url) < 0)
	nta_msg_discard(nua->nua_nta, msg), msg = NULL;
    }
    else {
      // tl_gets(ta_args(ta), TAG_END());

      if ((sip_add_tl(msg, sip, 
		      TAG_IF(method != sip_method_register,
			     SIPTAG_ROUTE(nua->nua_service_route)),
		      ta_tags(ta)) < 0)
	  || (ds->ds_remote_tag && 
	      sip_to_tag(nh->nh_home, sip->sip_to, ds->ds_remote_tag) < 0) 
	  || nta_msg_request_complete(msg, nua->nua_default->nh_ds->ds_leg,
				      method, name, url) < 0 
	  || (sip->sip_from == NULL &&
	      sip_add_dup(msg, sip, (sip_header_t *)nua->nua_from) < 0))
	nta_msg_discard(nua->nua_nta, msg), msg = NULL;

      if (use_leg && msg) {
	sip_route_t *route = sip->sip_route;

	if (method == sip_method_invite ||
	    method == sip_method_subscribe ||
	    method == sip_method_notify)
	  route = NULL;

	ds->ds_leg = nta_leg_tcreate(nua->nua_nta, process_request, nh,
				     SIPTAG_CALL_ID(sip->sip_call_id),
				     SIPTAG_FROM(sip->sip_from),
				     SIPTAG_TO(sip->sip_to),
				     SIPTAG_ROUTE(route),
				     SIPTAG_CSEQ(sip->sip_cseq),
				     TAG_END());
	if (!sip->sip_from->a_tag) {
	  nta_leg_tag(ds->ds_leg, NULL);
	  sip_from_tag(msg_home(msg), 
		       sip->sip_from, 
		       nta_leg_get_tag(ds->ds_leg));
	}
	/* XXX - check error */
      }
    }

    if (add_contact && msg && !sip->sip_contact) {
      /* We are missing contact */

      /* If application did not specify an empty contact, use ours */
      if (!tl_find(nh->nh_tags, siptag_contact) &&
	  !tl_find(nh->nh_tags, siptag_contact_str) &&
	  !tl_find(ta_args(ta), siptag_contact) &&
	  !tl_find(ta_args(ta), siptag_contact_str)) {
	if (sip->sip_request->rq_url->url_type == url_sips &&
	    nua->nua_sips_contact)
	  sip_add_dup(msg, sip, (sip_header_t *)nua->nua_sips_contact);
	else
	  sip_add_dup(msg, sip, (sip_header_t *)nua->nua_contact);
      }
    }

    if (!sip->sip_user_agent && nua->nua_user_agent) 
      sip_add_dup(msg, sip, (sip_header_t *)nua->nua_user_agent);      
    
    if (method != sip_method_ack) {
      if (!sip->sip_allow && !ds->ds_remote_tag) 
	sip_add_dup(msg, sip, (sip_header_t*)nua->nua_allow);

      if (!sip->sip_supported && nua->nua_supported)
	sip_add_dup(msg, sip, (sip_header_t *)nua->nua_supported);      

      if (method == sip_method_register && nua->nua_path_enable &&
	  !sip_has_feature(sip->sip_supported, "path") &&
	  !sip_has_feature(sip->sip_require, "path"))
	sip_add_make(msg, sip, sip_supported_class, "path");

      if (!sip->sip_organization && nua->nua_organization) 
	sip_add_make(msg, sip, sip_organization_class, nua->nua_organization);

      if (nh->nh_auth) {
	nh_authorize(nh, ta_tags(ta));
      
	if (method != sip_method_invite && 
	    method != sip_method_update &&
	    /* auc_authorize() removes existing authentication headers */
	    auc_authorize(&nh->nh_auth, msg, sip) < 0)
	  msg_destroy(msg), msg = NULL;
      }
    } else /* ACK */ {
      while (sip->sip_allow)
	sip_header_remove(msg, sip, (sip_header_t*)sip->sip_allow);
      while (sip->sip_priority)
	sip_header_remove(msg, sip, (sip_header_t*)sip->sip_priority);
      while (sip->sip_proxy_require)
	sip_header_remove(msg, sip, (sip_header_t*)sip->sip_proxy_require);
      while (sip->sip_require)
	sip_header_remove(msg, sip, (sip_header_t*)sip->sip_require);
      while (sip->sip_subject)
	sip_header_remove(msg, sip, (sip_header_t*)sip->sip_subject);
      while (sip->sip_supported)
	sip_header_remove(msg, sip, (sip_header_t*)sip->sip_supported);
    }

    ta_end(ta);

    if (!ds->ds_remote)
      ds->ds_remote = sip_to_dup(nh->nh_home, sip->sip_to);
    if (!ds->ds_local)
      ds->ds_local = sip_from_dup(nh->nh_home, sip->sip_from);

    if (copy) {
      cr->cr_msg = msg;
      msg = msg_copy(msg);
    }
  }

  return msg;
}

/** Create response message.
 *
 * @param nua  
 * @param nh
 * @param irq
 * @param status
 * @param phrase
 * @param tag, @a value, ... list of tag-value pairs
 */
static
msg_t *nh_make_response(nua_t *nua, nua_handle_t *nh, 
			nta_incoming_t *irq,
			int status, char const *phrase,
			tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  msg_t *msg = nta_msg_create(nua->nua_nta, 0);
  sip_t *sip = sip_object(msg);
  sip_header_t *m;
  int add_contact = 0;

  if (nta_incoming_url(irq)->url_type == url_sips && nua->nua_sips_contact)
    m = (sip_header_t*)nua->nua_sips_contact;
  else
    m = (sip_header_t*)nua->nua_contact;

  ta_start(ta, tag, value);

  tl_gets(ta_args(ta),
	  NUTAG_ADD_CONTACT_REF(add_contact),
	  TAG_END());

  if (!msg)
    return NULL;
  else if (nta_msg_response_complete(msg, irq, status, phrase) < 0) 
    msg_destroy(msg);      
  else if (sip_add_tl(msg, sip, ta_tags(ta)) < 0)
    msg_destroy(msg);
  else if (sip_message_complete(msg) < 0)
    msg_destroy(msg);      
  else if (add_contact && !sip->sip_contact && sip_add_dup(msg, sip, m) < 0)
    msg_destroy(msg);
  else if (!sip->sip_supported && nua->nua_supported &&
	   sip_add_dup(msg, sip, (sip_header_t *)nua->nua_supported) < 0)
    msg_destroy(msg);
  else if (!sip->sip_user_agent && nua->nua_user_agent &&
	   sip_add_dup(msg, sip, (sip_header_t *)nua->nua_user_agent) < 0)
    msg_destroy(msg);
  else if (!sip->sip_organization && nua->nua_organization &&
	   sip_add_make(msg, sip, sip_organization_class, 
			nua->nua_organization) < 0)
    msg_destroy(msg);
  else if (!sip->sip_allow && nua->nua_allow &&
	   sip_add_dup(msg, sip, (sip_header_t *)nua->nua_allow) < 0)
    msg_destroy(msg);
  else
    return msg;
  
  return NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
1883
/* ======================================================================== */
Pekka Pessi's avatar
Pekka Pessi committed
1884

Pekka Pessi's avatar
Pekka Pessi committed
1885
static char const application_sdp[] = "application/sdp";
Pekka Pessi's avatar
Pekka Pessi committed
1886

Pekka Pessi's avatar
Pekka Pessi committed
1887 1888 1889 1890 1891 1892
/** Get SDP from a SIP message */
static
int session_get_description(msg_t *msg, 
			    sip_t const *sip,
			    char const **return_sdp, 
			    size_t *return_len)
Pekka Pessi's avatar
Pekka Pessi committed
1893
{
Pekka Pessi's avatar
Pekka Pessi committed
1894 1895 1896
  sip_payload_t const *pl = sip->sip_payload;
  sip_content_type_t const *ct = sip->sip_content_type;
  int matching_content_type = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1897

Pekka Pessi's avatar
Pekka Pessi committed
1898
  if (pl == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
1899
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1900
  else if (pl->pl_len == 0 || pl->pl_data == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
1901
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1902 1903 1904 1905 1906 1907 1908 1909 1910
  else if (ct == NULL)
    /* Be bug-compatible with our old gateways */
    SU_DEBUG_3(("nua: no %s, assuming %s\n", 
		"Content-Type", application_sdp));
  else if (ct->c_type == NULL)
    SU_DEBUG_3(("nua: empty %s, assuming %s\n", 
		"Content-Type", application_sdp));
  else if (strcasecmp(ct->c_type, SDP_MIME_TYPE)) {
    SU_DEBUG_5(("nua: unknown %s: %s\n", "Content-Type", ct->c_type));
Pekka Pessi's avatar
Pekka Pessi committed
1911
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1912 1913 1914
  }
  else
    matching_content_type = 1;
Pekka Pessi's avatar
Pekka Pessi committed
1915

Pekka Pessi's avatar
Pekka Pessi committed
1916
  if (pl == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
1917 1918
    return 0;

Pekka Pessi's avatar
Pekka Pessi committed
1919 1920 1921 1922
  if (!matching_content_type) {
    /* Make sure we got SDP */
    if (pl->pl_len < 3 || strncasecmp(pl->pl_data, "v=0", 3))
      return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1923 1924
  }

Pekka Pessi's avatar
Pekka Pessi committed
1925 1926
  *return_sdp = pl->pl_data;
  *return_len = pl->pl_len;
Pekka Pessi's avatar
Pekka Pessi committed
1927

Pekka Pessi's avatar
Pekka Pessi committed
1928
  return 1;
Pekka Pessi's avatar
Pekka Pessi committed
1929 1930
}

Pekka Pessi's avatar
Pekka Pessi committed
1931 1932 1933 1934
/** Insert SDP into SIP message */ 
int session_include_description(nua_handle_t *nh,
				msg_t *msg, 
				sip_t *sip)
Pekka Pessi's avatar
Pekka Pessi committed
1935
{
Pekka Pessi's avatar
Pekka Pessi committed
1936
  su_home_t *home = msg_home(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1937

Pekka Pessi's avatar
Pekka Pessi committed
1938 1939
  char const *sdp;
  int len;
Pekka Pessi's avatar
Pekka Pessi committed
1940

Pekka Pessi's avatar
Pekka Pessi committed
1941 1942 1943 1944 1945
  sip_content_disposition_t *cd;
  sip_content_type_t *ct;
  sip_payload_t *pl;
  
  if (!nh->nh_soa)
Pekka Pessi's avatar
Pekka Pessi committed
1946 1947
    return 0;

Pekka Pessi's avatar
Pekka Pessi committed
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
  if (soa_get_local_sdp(nh->nh_soa, &sdp, &len) < 0)
    return -1;
    
  pl = sip_payload_create(home, sdp, len);
  ct = sip_content_type_make(home, application_sdp);
  cd = sip_content_disposition_make(home, "session");

  if (pl == NULL || ct == NULL || cd == NULL ||
      sip_header_insert(msg, sip, (sip_header_t *)cd) < 0 ||
      sip_header_insert(msg, sip, (sip_header_t *)ct) < 0 ||
      sip_header_insert(msg, sip, (sip_header_t *)pl) < 0)
    return -1;

  return 0;
}
Pekka Pessi's avatar
Pekka Pessi committed
1963

Pekka Pessi's avatar
Pekka Pessi committed
1964 1965 1966 1967 1968 1969 1970 1971 1972
/** Generate SDP headers */ 
int session_make_description(nua_handle_t *nh,
			     su_home_t *home,
			     sip_content_disposition_t **return_cd,
			     sip_content_type_t **return_ct,
			     sip_payload_t **return_pl)
{
  char const *sdp;
  int len;
Pekka Pessi's avatar
Pekka Pessi committed
1973

Pekka Pessi's avatar
Pekka Pessi committed
1974 1975
  if (!nh->nh_soa)
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1976

Pekka Pessi's avatar
Pekka Pessi committed
1977 1978 1979 1980 1981 1982
  if (soa_get_local_sdp(nh->nh_soa, &sdp, &len) < 0)
    return -1;
    
  *return_pl = sip_payload_create(home, sdp, len);
  *return_ct = sip_content_type_make(home, application_sdp);
  *return_cd = sip_content_disposition_make(home, "session");
Pekka Pessi's avatar
Pekka Pessi committed
1983

Pekka Pessi's avatar
Pekka Pessi committed
1984 1985
  return 0;
}
Pekka Pessi's avatar
Pekka Pessi committed
1986 1987 1988



Pekka Pessi's avatar
Pekka Pessi committed
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
/** Store SDP from incoming response. 
 * 
 * @retval 1 if there was SDP to process
 */
static
int session_process_response(nua_handle_t *nh,
			     struct nua_client_request *cr,
			     nta_outgoing_t *orq,
			     sip_t const *sip)
{
  char const *method = nta_outgoing_method_name(orq);
  msg_t *msg = nta_outgoing_getresponse_ref(orq);
  int retval = 0;
  char const *sdp = NULL;
  int len;

  if (nh->nh_soa == NULL)
    /* Xyzzy */;
  else if (!session_get_description(msg, sip, &sdp, &len)) 
    /* No SDP */;
  else if (cr->cr_answer_recv) {
    /* Ignore spurious answers after completing O/A */
    SU_DEBUG_3(("nua(%p): %s: ignoring duplicate SDP in %u %s\n", 
		nh, method,
		sip->sip_status->st_status, sip->sip_status->st_phrase));
    sdp = NULL;
  }
  else if (!cr->cr_offer_sent && 
	   nta_outgoing_method(orq) != sip_method_invite) {
    /* If non-invite request did not have offer, ignore SDP in response */
    SU_DEBUG_3(("nua(%p): %s: ignoring extra SDP in %u %s\n", 
		nh, method,
		sip->sip_status->st_status, sip->sip_status->st_phrase));
    sdp = NULL;
  }
  else {
    if (cr->cr_offer_sent)
      cr->cr_answer_recv = sip->sip_status->st_status;
    else 
      cr->cr_offer_recv = 1, cr->cr_answer_sent = 0;

    if (soa_set_remote_sdp(nh->nh_soa, sdp, len) < 0) {
      SU_DEBUG_5(("nua(%p): %s: error parsing SDP in %u %s\n", 
		  nh, method,
		  sip->sip_status->st_status, 
		  sip->sip_status->st_phrase));
      retval = -1;
      sdp = NULL;
    }
    else if (cr->cr_offer_recv) {
      SU_DEBUG_5(("nua(%p): %s: get SDP %s in %u %s\n", 
		  nh, method, "offer",
		  sip->sip_status->st_status, 
		  sip->sip_status->st_phrase));
      retval = 1;
    }
    else if (soa_process_answer(nh->nh_soa, NULL) < 0) {
      SU_DEBUG_5(("nua(%p): %s: error processing SDP answer in %u %s\n", 
		  nh, method,
		  sip->sip_status->st_status, 
		  sip->sip_status->st_phrase));
      sdp = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
2051 2052
    }
    else {
Pekka Pessi's avatar
Pekka Pessi committed
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
      if (soa_activate(nh->nh_soa, NULL) < 0) {
	SU_DEBUG_5(("nua(%p): %s: error activating media after %u %s\n", 
		    nh, method,
		    sip->sip_status->st_status, 
		    sip->sip_status->st_phrase));
	/* XXX */
      }
      else {
	SU_DEBUG_5(("nua(%p): %s: processed SDP answer in %u %s\n", 
		    nh, method, 
		    sip->sip_status->st_status, 
		    sip->sip_status->st_phrase));
      }
Pekka Pessi's avatar
Pekka Pessi committed
2066 2067 2068
    }
  }

Pekka Pessi's avatar
Pekka Pessi committed
2069 2070 2071 2072 2073 2074 2075
  msg_destroy(msg);		/* unref */
   
  process_response(nh, cr, orq, sip, 
		   NH_REMOTE_MEDIA_TAGS(sdp != NULL, nh->nh_soa),
		   TAG_END());

  return retval;
Pekka Pessi's avatar
Pekka Pessi committed
2076 2077
}

Pekka Pessi's avatar
Pekka Pessi committed
2078 2079
#if 0
/** Parse and store SDP from incoming request */
Pekka Pessi's avatar
Pekka Pessi committed
2080
static
Pekka Pessi's avatar
Pekka Pessi committed
2081 2082 2083
int session_process_request(nua_handle_t *nh,
			    nta_incoming_t *irq,
			    sip_t const *sip)
Pekka Pessi's avatar
Pekka Pessi committed
2084
{
Pekka Pessi's avatar
Pekka Pessi committed
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
  char const *sdp = NULL;
  int len;
    
  if (nh->nh_soa) {
    msg_t *msg = nta_outgoing_getresponse_ref(irq);

    if (session_get_description(msg, sip, &sdp, &len)) {
      if (soa_is_complete(nh->nh_soa)) {
	/* Ignore spurious answers after completing O/A */
	SU_DEBUG_5(("nua: ignoring duplicate SDP in %u %s\n", 
		    sip->sip_status->st_status, sip->sip_status->st_phrase));
	sdp = NULL;
      }
      else if (soa_parse_sdp(nh->nh_soa, sdp, len) < 0) {
	SU_DEBUG_5(("nua: error parsing SDP in %u %s\n", 
		    sip->sip_status->st_status, 
		    sip->sip_status->st_phrase));
	sdp = NULL;
      }
    }
Pekka Pessi's avatar
Pekka Pessi committed
2105

Pekka Pessi's avatar
Pekka Pessi committed
2106
    msg_destroy(msg);
Pekka Pessi's avatar
Pekka Pessi committed
2107
  }
Pekka Pessi's avatar
Pekka Pessi committed
2108 2109 2110 2111
   
  return process_response(nh, cr, orq, sip, 
			  NH_REMOTE_MEDIA_TAGS(sdp != NULL, nh->nh_soa),
			  TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
2112
}
Pekka Pessi's avatar
Pekka Pessi committed
2113
#endif
Pekka Pessi's avatar
Pekka Pessi committed
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541

/* ======================================================================== */
/* Dialog handling */

static void 
dialog_usage_remove_at(nua_handle_t*, nua_dialog_state_t*, 
		       nua_dialog_usage_t**),
  dialog_log_usage(nua_handle_t *, nua_dialog_state_t *);

static inline
int dialog_is_established(struct nua_dialog_state const *ds)
{
  return ds->ds_remote_tag != NULL;
}

/** Refer usage */
static sip_event_t const first_refer_usage[1] = 
  {{{ 0, 0, sip_event_class, 0, 0 }, 0, "refer" }};

/** UAS tag and route */
static
void dialog_uas_route(nua_handle_t *nh, sip_t const *sip, int rtag)
{
  struct nua_dialog_state *ds = nh->nh_ds;
  int established = dialog_is_established(ds);

  if (!established && sip->sip_from->a_tag)
    ds->ds_remote_tag = su_strdup(nh->nh_home, sip->sip_from->a_tag);

  if (ds->ds_leg == NULL)
    return;

  nta_leg_server_route(ds->ds_leg, sip->sip_record_route, sip->sip_contact);
  ds->ds_route = ds->ds_route || sip->sip_record_route || sip->sip_contact;

  if (rtag && !established && sip->sip_from->a_tag)
    nta_leg_rtag(ds->ds_leg, sip->sip_from->a_tag);
}

/** UAC tag and route.
 *
 * Update dialog tags and route on the UAC side.
 *
 * @param nh   NUA handle
 * @param sip  SIP message containing response used to update dialog
 * @param rtag if true, set remote tag within the leg
 */
static
void dialog_uac_route(nua_handle_t *nh, sip_t const *sip, int rtag)
{
  struct nua_dialog_state *ds = nh->nh_ds;
  int established = dialog_is_established(ds);

  if (!established && sip->sip_to->a_tag)
    ds->ds_remote_tag = su_strdup(nh->nh_home, sip->sip_to->a_tag);

  if (ds->ds_leg == NULL)
    return;

  nta_leg_client_route(ds->ds_leg, sip->sip_record_route, sip->sip_contact);
  ds->ds_route = ds->ds_route || sip->sip_record_route || sip->sip_contact;

  if (rtag && !established && sip->sip_to->a_tag)
    nta_leg_rtag(ds->ds_leg, sip->sip_to->a_tag);
}

/** Store information from remote endpoint. */
static void 
dialog_get_peer_info(nua_handle_t *nh, sip_t const *sip)
{
  nua_remote_t *nr = nh->nh_ds->ds_remote_ua, old[1];

  *old = *nr;

  if (sip->sip_allow) {
    nr->nr_allow = sip_allow_dup(nh->nh_home, sip->sip_allow);
    su_free(nh->nh_home, old->nr_allow);
  }

  if (sip->sip_accept) {
    nr->nr_accept = sip_accept_dup(nh->nh_home, sip->sip_accept);
    su_free(nh->nh_home, old->nr_accept);
  }

  if (sip->sip_require) {
    nr->nr_require = sip_require_dup(nh->nh_home, sip->sip_require);
    su_free(nh->nh_home, old->nr_require);
  }

  if (sip->sip_supported) {
    nr->nr_supported = sip_supported_dup(nh->nh_home, sip->sip_supported);
    su_free(nh->nh_home, old->nr_supported);
  }

  if (sip->sip_user_agent) {
    nr->nr_user_agent = sip_user_agent_dup(nh->nh_home, sip->sip_user_agent);
    su_free(nh->nh_home, old->nr_user_agent);
  }
}

/** Get dialog usage slot */
nua_dialog_usage_t **
dialog_usage_at(nua_dialog_state_t const *ds, 
		enum nua_dialog_usage_e kind,
		sip_event_t const *event)
{
  static nua_dialog_usage_t *none = NULL;

  if (ds) {
    nua_dialog_usage_t *du, * const * prev;
    sip_event_t const *o;

    for (prev = &ds->ds_usage; (du = *prev); prev = &du->du_next) {
      if (du->du_kind != kind)
	continue;

      if (event == NONE || 
	  /* Subscriber usage requires event */
	  (event == NULL && kind != nua_subscriber_usage))
	return (nua_dialog_usage_t **)prev;

      o = du->du_event;

      if (event == o)
	return (nua_dialog_usage_t **)prev;

      if (event == NULL || o == NULL)
	continue;
      if (strcmp(event->o_type, o->o_type))
	continue;
      if (event == first_refer_usage)
	return (nua_dialog_usage_t **)prev;
      if (str0casecmp(event->o_id, o->o_id))
	continue;

      return (nua_dialog_usage_t **)prev;
    }
  }

  return &none;
}

/** Get a dialog usage */ 
static
nua_dialog_usage_t *
dialog_usage_get(nua_dialog_state_t const *ds, 
		 enum nua_dialog_usage_e kind,
		 sip_event_t const *event)
{
  return *dialog_usage_at(ds, kind, event);
}

/** Get dialog usage name */
static
char const *
dialog_usage_name(nua_dialog_usage_t const *du)
{
  switch (du->du_kind) {
  case nua_session_usage:  
    return "session";

  case nua_notifier_usage:
    return "notifier";

  case nua_subscriber_usage:
    return "subscription";

  case nua_register_usage:
    return "register";

  case nua_publish_usage:
    return "publish";

  case nua_transaction_usage:
    return "transaction";

  default:
    return "unknown";
  }
} 

/** Add dialog usage */
static nua_dialog_usage_t *
dialog_usage_add(nua_handle_t *nh, 
		 struct nua_dialog_state *ds, 
		 enum nua_dialog_usage_e kind,
		 sip_event_t const *event)
{
  if (ds) {
    sip_event_t *o;
    nua_dialog_usage_t *du, **prev_du;

    prev_du = dialog_usage_at(ds, kind, event);
    du = *prev_du;
    if (du) {		/* Already exists */
      SU_DEBUG_5(("nta(%p): adding already existing %s usage%s%s\n",
		  nh, dialog_usage_name(du), 
		  event ? " with event " : "", event ? event->o_type : ""));
      
      if (prev_du != &ds->ds_usage) {
	/* Move as a first usage in the list */
	*prev_du = du->du_next;
	du->du_next = ds->ds_usage;
	ds->ds_usage = du;
      }
      return du;
    }

    o = event ? sip_event_dup(nh->nh_home, event) : NULL;

    if (o != NULL || event == NULL)
      du = su_zalloc(nh->nh_home, sizeof *du);

    if (du) {
      du->du_kind = kind;
      if (o)
	du->du_event = o;

      switch (kind) {
      case nua_session_usage:  
	ds->ds_has_session = 1; 
	break;
      case nua_notifier_usage:
	ds->ds_has_events = 1;
	ds->ds_has_notifier = 1;
	break;
      case nua_subscriber_usage:
	ds->ds_has_events = 1;
	ds->ds_has_subscription = 1;
	break;
      case nua_register_usage:
	ds->ds_has_register = 1;
	break;
      case nua_publish_usage:
	ds->ds_has_publish = 1;
	break;
      case nua_transaction_usage:
      default:
	break;
      }

      du->du_next = ds->ds_usage;

      SU_DEBUG_5(("nta(%p): adding %s usage%s%s\n",
		  nh, dialog_usage_name(du), 
		  event ? "with event " : "", event ? event->o_type :""));

      nh_incref(nh);

      return ds->ds_usage = du;
    }

    su_free(nh->nh_home, o);
  }

  return NULL;
}

/** Remove dialog usage. */
void
dialog_usage_remove(nua_handle_t *nh, 
		    nua_dialog_state_t *ds,
		    nua_dialog_usage_t *du)
{
  nua_dialog_usage_t **at;

  assert(nh); assert(ds); assert(du);

  for (at = &ds->ds_usage; *at; at = &(*at)->du_next)
    if (du == *at)
      break;

  assert(*at);

  dialog_usage_remove_at(nh, ds, at);
}

/** Remove dialog usage.
 *
 * Zap dialog state (leg, tag and route) if no usages remain. 
*/
static void
dialog_usage_remove_at(nua_handle_t *nh, 
		       nua_dialog_state_t *ds,
		       nua_dialog_usage_t **at)
{
  if (*at) {
    nua_dialog_usage_t *du = *at;
    enum nua_dialog_usage_e kind = du->du_kind;
    sip_event_t const *o = NULL;

    *at = du->du_next;

    if (kind == nua_notifier_usage || kind == nua_subscriber_usage)
      o = du->du_event;

    SU_DEBUG_5(("nta(%p): removing %s usage%s%s\n",
		nh, dialog_usage_name(du), 
		o ? "with event " : "", o ? o->o_type :""));

    switch (kind) {
    case nua_session_usage:  
      ds->ds_has_session = 0;
      break;

    case nua_notifier_usage:
      su_free(nh->nh_home, (void *)du->du_event);
      ds->ds_has_notifier = NULL != *dialog_usage_at(ds, kind, NONE);
      ds->ds_has_events = ds->ds_has_notifier || ds->ds_has_subscription;
      break;

    case nua_subscriber_usage:
      su_free(nh->nh_home, (void *)du->du_event);
      ds->ds_has_subscription = NULL != *dialog_usage_at(ds, kind, NONE);
      ds->ds_has_events = ds->ds_has_subscription || ds->ds_has_notifier;
      msg_destroy(du->du_subscriber->de_msg);
      break;

    case nua_register_usage:
      ds->ds_has_register = 0;
#if HAVE_SIGCOMP
      if (du->du_register->ru_compartment)
	sigcomp_compartment_unref(du->du_register->ru_compartment);
      du->du_register->ru_compartment = NULL;
#endif      
      msg_destroy(du->du_register->ru_msg);
      break;

    case nua_publish_usage:
      ds->ds_has_publish = 0;
      su_free(nh->nh_home, du->du_publisher->pu_etag);
      msg_destroy(du->du_publisher->pu_msg);
      break;

    case nua_transaction_usage:
    default:
      break;
    }

    nh_decref(nh);

    su_free(nh->nh_home, du);
  }

  /* Zap dialog if there is no more usages */
  if (ds->ds_usage == NULL) {
    nta_leg_destroy(ds->ds_leg), ds->ds_leg = NULL;
    su_free(nh->nh_home, (void *)ds->ds_remote_tag), ds->ds_remote_tag = NULL;
    ds->ds_route = 0;
    ds->ds_has_events = 0;
    ds->ds_terminated = 0;
    return;
  }
  else if (!ds->ds_terminated) {
    dialog_log_usage(nh, ds);
  }
}

static void
dialog_log_usage(nua_handle_t *nh, nua_dialog_state_t *ds)
{
  nua_dialog_usage_t *du;
  int has_session = 0;

  for (du = ds->ds_usage; du; du = du->du_next) {
    if (du->du_kind == nua_session_usage)
      has_session = 1;
    else if (du->du_kind == nua_notifier_usage ||
	     du->du_kind == nua_subscriber_usage)
      ds->ds_has_events = 1;
  }

  if (nua_log->log_level >= 3) {
    char buffer[160];
    int l = 0, n, N = sizeof buffer, has_session = 0;
    
    buffer[0] = '\0';

    for (du = ds->ds_usage; du; du = du->du_next) {
      msg_header_t const *h;

      if (du->du_kind != nua_notifier_usage &&
	  du->du_kind != nua_subscriber_usage)
	continue;

      h = (msg_header_t const *)du->du_event;
      n = sip_event_e(buffer + l, N - l, h, 0);
      if (n == -1)
	break;
      l += n;
      if (du->du_next && l + 2 < sizeof(buffer)) {
	strcpy(buffer + l, ", ");
	l += 2;
      }
    }
    
    SU_DEBUG_3(("nua(%p): still has %s%s%s\n", nh,
		has_session ? "session and " : "", 
		ds->ds_has_events ? "events " : "",
		buffer));
  }
}

/** Dialog has been terminated. */
static
void
dialog_terminated(nua_handle_t *nh,
		  struct nua_dialog_state *ds,
		  int status,
		  char const *phrase)
{
  int call = 0;

  ds->ds_terminated = 1;

  while (ds->ds_usage) {
    if (ds->ds_usage->du_kind == nua_session_usage)
      call = 1;			/* Delay sending the event */
    else
      /* XXX */;
    dialog_usage_remove_at(nh, ds, &ds->ds_usage);
  }

  if (call)
    ua_event(nh->nh_nua, nh, NULL, nua_i_terminated, 
	     status, phrase, TAG_END());
}

Pekka Pessi's avatar
Pekka Pessi committed
2542 2543 2544 2545 2546
char const *convert_soa_error_to_sip_reason(soa_session_t *soa)
{

  return "SIP;cause=500;text=\"Internal media error\"";
}
Pekka Pessi's avatar
Pekka Pessi committed
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600