nua_stack.c 171 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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
/*
 * 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
 */

#include "config.h"

#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"

#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
85
#if HAVE_SOFIA_SMIME 
Pekka Pessi's avatar
Pekka Pessi committed
86
#include "smimec.h"
Pekka Pessi's avatar
Pekka Pessi committed
87
#endif                  
Pekka Pessi's avatar
Pekka Pessi committed
88 89 90 91 92 93 94 95

#include <sl_utils.h>

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

Pekka Pessi's avatar
Pekka Pessi committed
96 97
#include "soa.h"

Pekka Pessi's avatar
Pekka Pessi committed
98 99
#include "nua_stack.h"

100 101 102 103 104 105 106 107 108
#ifndef SDP_MIME_TYPE
static char const nua_application_sdp[] = "application/sdp";
#define SDP_MIME_TYPE (nua_application_sdp)
#endif

#ifndef SDP_H
typedef struct sdp_session_s sdp_session_t;
#endif

109
typedef unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
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

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

137
static void ua_set_from(nua_t *nua, sip_from_t const *f, char const *fromstr);
Pekka Pessi's avatar
Pekka Pessi committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151

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

152 153 154 155 156
static void signal_call_state_change(nua_handle_t *nh, 
				     int status, char const *phrase, 
				     enum nua_callstate tr_event,
				     char const *sdp_recv, 
				     char const *sdp_sent);
157

Pekka Pessi's avatar
Pekka Pessi committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#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
180 181 182
/** Default internal error */
char const nua_500_error[] = "Internal NUA Error";

Pekka Pessi's avatar
Pekka Pessi committed
183 184 185 186 187 188 189 190 191 192
/* ----------------------------------------------------------------------
 * 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;
193 194
  sip_from_t const *from = NULL;
  char const *from_str = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
195 196 197 198

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

199 200
  nua_handle_t *dnh;
  nua_handle_preferences_t *dnhp;
Pekka Pessi's avatar
Pekka Pessi committed
201
  int media_enable = 1;
Pekka Pessi's avatar
Pekka Pessi committed
202
  soa_session_t *soa = NULL;
203
  char const *soa_name = NULL;
204
  
Pekka Pessi's avatar
Pekka Pessi committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  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;

225 226 227
  dnh = su_home_clone(home, sizeof (*dnh) + sizeof(*dnhp));
  if (!dnh)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
228

229
  nua->nua_root = root;
Pekka Pessi's avatar
Pekka Pessi committed
230
  nua->nua_handles_tail = &nua->nua_handles;
231
  nh_append(nua, dnh);  
Pekka Pessi's avatar
Pekka Pessi committed
232
  
233 234 235 236 237 238 239
#if SU_HAVE_PTHREADS
  pthread_rwlock_init(dnh->nh_refcount, NULL);
#endif
  dnh->nh_valid = nua_handle;
  dnh->nh_nua = nua;
  dnh->nh_ds->ds_local = sip_from_init(nua->nua_from);
  dnh->nh_ds->ds_remote = nua->nua_from;
Pekka Pessi's avatar
Pekka Pessi committed
240

241 242
  nh_incref(dnh); dnh->nh_ref_by_stack = 1;
  nh_incref(dnh); dnh->nh_ref_by_user = 1;
Pekka Pessi's avatar
Pekka Pessi committed
243

244
  dnh->nh_prefs = dnhp = (void *)(dnh + 1);
Pekka Pessi's avatar
Pekka Pessi committed
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
  /* Set some defaults */
  DNHP_SET(dnhp, retry_count, 3);
  DNHP_SET(dnhp, max_subscriptions, 20);

  DNHP_SET(dnhp, invite_enable, 1);
  DNHP_SET(dnhp, auto_alert, 0);
  DNHP_SET(dnhp, early_media, 0);
  DNHP_SET(dnhp, auto_answer, 0);
  DNHP_SET(dnhp, auto_ack, 1);
  DNHP_SET(dnhp, invite_timeout, 120);

  DNHP_SET(dnhp, session_timer, 1800);
  DNHP_SET(dnhp, min_se, 120);
  DNHP_SET(dnhp, refresher, nua_no_refresher);
  DNHP_SET(dnhp, update_refresh, 0);

  DNHP_SET(dnhp, message_enable, 1);
  DNHP_SET(dnhp, win_messenger_enable, 0); 
  if (getenv("PIMIW_HACK") != 0)
    DNHP_SET(dnhp, message_auto_respond, 1);

  DNHP_SET(dnhp, media_features,  0);
  DNHP_SET(dnhp, callee_caps, 0);
  DNHP_SET(dnhp, service_route_enable, 1);
  DNHP_SET(dnhp, path_enable, 1);

  DNHP_SET(dnhp, allow, sip_allow_make(dnh->nh_home, nua_allow_str));
  DNHP_SET(dnhp, supported, sip_supported_make(dnh->nh_home, "timer, 100rel"));
  DNHP_SET(dnhp, user_agent, 
	   sip_user_agent_make(dnh->nh_home, PACKAGE_NAME "/" PACKAGE_VERSION));

  /* Set initial nta parameters */
Pekka Pessi's avatar
Pekka Pessi committed
278 279
  tl_gets(nua->nua_args,
	  NUTAG_URL_REF(contact),
280 281
	  SIPTAG_FROM_REF(from),
	  SIPTAG_FROM_STR_REF(from_str),
Pekka Pessi's avatar
Pekka Pessi committed
282 283 284 285
	  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
286
	  NUTAG_MEDIA_ENABLE_REF(media_enable),
Pekka Pessi's avatar
Pekka Pessi committed
287
	  /* NUTAG_SOA_SESSION_REF(soa), */
288
	  NUTAG_SOA_NAME_REF(soa_name),
Pekka Pessi's avatar
Pekka Pessi committed
289 290 291 292 293 294 295
	  TAG_NULL());

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

296
  nua->nua_nta = nta_agent_create(root, NONE, NULL, NULL, 
Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301 302
				  TPTAG_CERTIFICATE(certificate_dir),
				  NTATAG_TAG_3261(0),
				  TAG_END());
  if (!nua->nua_nta)
    return -1;

303 304
  if (!contact && !sips_contact)
    contact = URL_STRING_MAKE("sip:*:*");
Pekka Pessi's avatar
Pekka Pessi committed
305

306 307 308 309 310 311 312
  if ((!contact ||
       nta_agent_add_tport(nua->nua_nta, contact, TAG_END()) < 0) &&
      (!sips_contact ||
       nta_agent_add_tport(nua->nua_nta, sips_contact, TAG_END()) < 0)) {
    return -1;
  }
    
Pekka Pessi's avatar
Pekka Pessi committed
313 314 315 316 317 318
  nua->nua_media_enable = media_enable;

  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
319
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
320 321 322 323 324 325 326
		       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);

327 328
  if (media_enable) {
    if (soa == NULL)
329 330
      soa = soa_create(soa_name, nua->nua_root, nua->nua_dhandle);
    dnh->nh_soa = soa;
Pekka Pessi's avatar
Pekka Pessi committed
331
    soa_set_params(soa, TAG_NEXT(nua->nua_args));
Pekka Pessi's avatar
Pekka Pessi committed
332 333
  }

334
  dnh->nh_ds->ds_leg = nta_leg_tcreate(nua->nua_nta, process_request, dnh,
Pekka Pessi's avatar
Pekka Pessi committed
335 336 337 338
				      NTATAG_NO_DIALOG(1), 
				      TAG_END());

  ua_init_contact(nua);
339
  ua_set_from(nua, from, from_str);
Pekka Pessi's avatar
Pekka Pessi committed
340

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

343 344 345
  if (!(dnh->nh_ds->ds_leg &&
	dnhp->nhp_allow && 
	dnhp->nhp_supported &&
Pekka Pessi's avatar
Pekka Pessi committed
346 347 348 349 350 351
	(nua->nua_contact || nua->nua_sips_contact) &&
	nua->nua_from &&
	nua->nua_sdp_content &&
	nua->nua_timer))
    return -1;

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

Pekka Pessi's avatar
Pekka Pessi committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367
  nua->nua_args = NULL;

  return 0;
}

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

  su_timer_destroy(nua->nua_timer), nua->nua_timer = NULL;
  nta_agent_destroy(nua->nua_nta), nua->nua_nta = NULL;
}

/** Set the default from field */
368
void ua_set_from(nua_t *nua, sip_from_t const *f, char const *str)
Pekka Pessi's avatar
Pekka Pessi committed
369
{
370
  sip_from_t from[1], *f0;
Pekka Pessi's avatar
Pekka Pessi committed
371 372 373 374 375

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

376 377
  sip_from_init(from);

Pekka Pessi's avatar
Pekka Pessi committed
378 379 380
  if (f) {
    from->a_display = f->a_display;
    *from->a_url = *f->a_url;
381 382 383 384 385 386 387
    f0 = sip_from_dup(nua->nua_home, from);
  }
  else if (str) {
    f0 = sip_from_make(nua->nua_home, str);
    if (f0) 
      *from = *f0, f0 = from, f0->a_params = NULL;
  }
Pekka Pessi's avatar
Pekka Pessi committed
388 389 390 391 392
  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;
393
    f0 = sip_from_dup(nua->nua_home, from);
Pekka Pessi's avatar
Pekka Pessi committed
394 395
  }

396 397
  if (f0)
    *nua->nua_from = *f0;
Pekka Pessi's avatar
Pekka Pessi committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
}


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

static 
void ua_init_a_contact(nua_t *nua, su_home_t *home, sip_contact_t *m)
{
Pekka Pessi's avatar
Pekka Pessi committed
440
  char const ***m_params = (msg_param_t **)&m->m_params;
Pekka Pessi's avatar
Pekka Pessi committed
441
  su_strlst_t *l = su_strlst_create(home);
442
  nua_handle_t *dnh = nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
443 444
  int i;

445 446 447 448
  if (DNH_PGET(dnh, callee_caps)) {
    sip_allow_t const *allow = DNH_PGET(dnh, allow);

    if (allow) {
Pekka Pessi's avatar
Pekka Pessi committed
449
      char *methods;
450 451 452
      if (allow->k_items)
	for (i = 0; allow->k_items[i]; i++)
	  su_strlst_append(l, allow->k_items[i]);
Pekka Pessi's avatar
Pekka Pessi committed
453 454 455 456
      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
457

458 459
    if (dnh->nh_soa) {
      char **media = soa_media_features(dnh->nh_soa, 0, home);
Pekka Pessi's avatar
Pekka Pessi committed
460 461 462 463 464 465

      while (*media) {
	msg_params_replace(home, m_params, *media);
	media++;
      }
    }
Pekka Pessi's avatar
Pekka Pessi committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  }

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

486
static void ua_shutdown(nua_t *);
Pekka Pessi's avatar
Pekka Pessi committed
487

Pekka Pessi's avatar
Pekka Pessi committed
488
static int
489 490
  ua_set_params(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  ua_get_params(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
Pekka Pessi's avatar
Pekka Pessi committed
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
  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;
578
    e->e_nh = nh ? nh_incref(nh) : nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
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
    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;
}

/* ----------------------------------------------------------------------
 * 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:
638
    ua_get_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
639 640
    break;
  case nua_r_set_params:
641
    ua_set_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    break;
  case nua_r_shutdown:
    ua_shutdown(nua);
    break;
  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:
Pekka Pessi's avatar
Pekka Pessi committed
666
  case nua_r_unpublish:
Pekka Pessi's avatar
Pekka Pessi committed
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
    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;
  }

710
  if (nh != nua->nua_dhandle)
Pekka Pessi's avatar
Pekka Pessi committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 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
    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;
}

Pekka Pessi's avatar
Pekka Pessi committed
809

Pekka Pessi's avatar
Pekka Pessi committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
/* ====================================================================== */

/** 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
832 833
    if (ss->ss_srequest->sr_respond) {
      ss->ss_srequest->sr_respond(nua, nh, SIP_410_GONE, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
834 835 836 837 838
      busy++;
    }

    busy += nh_call_pending(nh, 0);

Pekka Pessi's avatar
Pekka Pessi committed
839 840
    if (nh->nh_soa) {
      soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    }

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

860 861 862 863 864
  if (status >= 200) {
    su_timer_destroy(nua->nua_timer), nua->nua_timer = NULL;
    nta_agent_destroy(nua->nua_nta), nua->nua_nta = NULL;
  }

Pekka Pessi's avatar
Pekka Pessi committed
865 866 867 868 869 870
  ua_event(nua, NULL, NULL, nua_r_shutdown, status, phrase, TAG_END());
}

/* ----------------------------------------------------------------------
 * Parameters
 */
Pekka Pessi's avatar
Pekka Pessi committed
871

872 873
int ua_set_params(nua_t *nua, nua_handle_t *nh, nua_event_t e, 
		  tagi_t const *tags)
Pekka Pessi's avatar
Pekka Pessi committed
874
{
875 876 877 878 879 880
  nua_handle_t *dnh = nua->nua_dhandle;
  nua_handle_preferences_t nhp[1], *ohp = nh->nh_prefs;
  nua_handle_preferences_t const *dnhp = dnh->nh_prefs;

  su_home_t tmphome[1] = { SU_HOME_INIT(tmphome) };

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
  int retry_count = NHP_GET(ohp, dnhp, retry_count);
  int max_subscriptions = NHP_GET(ohp, dnhp, max_subscriptions);

  int invite_enable = NHP_GET(ohp, dnhp, invite_enable);
  int auto_alert = NHP_GET(ohp, dnhp, auto_alert);
  int early_media = NHP_GET(ohp, dnhp, early_media);
  int auto_answer = NHP_GET(ohp, dnhp, auto_answer);
  int auto_ack = NHP_GET(ohp, dnhp, auto_ack);
  int invite_timeout = NHP_GET(ohp, dnhp, invite_timeout);

  int session_timer = NHP_GET(ohp, dnhp, session_timer);
  int min_se = NHP_GET(ohp, dnhp, min_se);
  int refresher = NHP_GET(ohp, dnhp, refresher);
  int update_refresh = NHP_GET(ohp, dnhp, update_refresh);

  int message_enable = NHP_GET(ohp, dnhp, message_enable);
  int win_messenger_enable = NHP_GET(ohp, dnhp, win_messenger_enable);
  int message_auto_respond = NHP_GET(ohp, dnhp, message_auto_respond);

  int callee_caps = NHP_GET(ohp, dnhp, callee_caps);
  int media_features = NHP_GET(ohp, dnhp, media_features);
  int service_route_enable = NHP_GET(ohp, dnhp, service_route_enable);
  int path_enable = NHP_GET(ohp, dnhp, path_enable);
904 905 906 907 908

  sip_allow_t const *allow = NONE;
  char const   *allow_str = NONE;
  sip_supported_t const *supported = NONE;
  char const *supported_str = NONE;
909
  sip_user_agent_t const *user_agent = NONE;
910 911 912
  char const *user_agent_str = NONE, *ua_name = NONE;
  sip_organization_t const *organization = NONE;
  char const *organization_str = NONE;
Pekka Pessi's avatar
Pekka Pessi committed
913

914 915 916
  url_string_t const *registrar = NONE;
  sip_from_t const *from = NONE;
  char const *from_str = NONE;
Pekka Pessi's avatar
Pekka Pessi committed
917 918

#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
919 920 921 922 923 924 925 926
  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
927 928
#endif			

929
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
930 931 932

  enter;

933 934
  if (nh == dnh) 
    if (nta_agent_set_params(nua->nua_nta, TAG_NEXT(tags)) < 0)
935
      return UA_EVENT2(e, 400, "Error setting NTA parameters"), -1;
936

937
  if (nh->nh_soa && soa_set_params(nh->nh_soa, TAG_NEXT(tags)) < 0)
938
    return UA_EVENT2(e, 400, "Error setting SOA parameters"), -1;
939 940

  n =  tl_gets(tags,
941 942
	       NUTAG_RETRY_COUNT_REF(retry_count),
	       NUTAG_MAX_SUBSCRIPTIONS_REF(max_subscriptions),
943 944 945 946 947 948 949

	       NUTAG_ENABLEINVITE_REF(invite_enable),
	       NUTAG_AUTOALERT_REF(auto_alert),
	       NUTAG_EARLY_MEDIA_REF(early_media),
	       NUTAG_AUTOANSWER_REF(auto_answer),
	       NUTAG_AUTOACK_REF(auto_ack),
	       NUTAG_INVITE_TIMER_REF(invite_timeout),
Pekka Pessi's avatar
Pekka Pessi committed
950 951 952 953 954

	       NUTAG_SESSION_TIMER_REF(session_timer),
	       NUTAG_MIN_SE_REF(min_se),
	       NUTAG_SESSION_REFRESHER_REF(refresher),
	       NUTAG_UPDATE_REFRESH_REF(update_refresh),
955 956 957 958 959

	       NUTAG_ENABLEMESSAGE_REF(message_enable),
	       NUTAG_ENABLEMESSENGER_REF(win_messenger_enable),
	       /* NUTAG_MESSAGE_AUTOANSWER(message_auto_respond), */

Pekka Pessi's avatar
Pekka Pessi committed
960
	       NUTAG_CALLEE_CAPS_REF(callee_caps),
961
	       NUTAG_MEDIA_FEATURES_REF(media_features),
Pekka Pessi's avatar
Pekka Pessi committed
962
	       NUTAG_SERVICE_ROUTE_ENABLE_REF(service_route_enable),
963 964 965 966 967 968
	       NUTAG_PATH_ENABLE_REF(path_enable),

	       SIPTAG_SUPPORTED_REF(supported),
	       SIPTAG_SUPPORTED_STR_REF(supported_str),
	       SIPTAG_ALLOW_REF(allow),
	       SIPTAG_ALLOW_STR_REF(allow_str),
969
	       SIPTAG_USER_AGENT_REF(user_agent),
970 971 972 973 974 975 976 977 978 979 980 981
	       SIPTAG_USER_AGENT_STR_REF(user_agent_str),
	       NUTAG_USER_AGENT_REF(ua_name),

	       SIPTAG_ORGANIZATION_REF(organization),
	       SIPTAG_ORGANIZATION_STR_REF(organization_str),

	       TAG_IF(nh != dnh, TAG_END()),

	       NUTAG_REGISTRAR_REF(registrar),
	       SIPTAG_FROM_REF(from),
	       SIPTAG_FROM_STR_REF(from_str),

Pekka Pessi's avatar
Pekka Pessi committed
982
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
983 984 985 986 987 988 989 990 991
	       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
992 993
	       TAG_NULL());
  if (n < 0)
994
    return UA_EVENT2(e, 400, "Error obtaining NUA parameters"), -1;
995
  
996
  *nhp = *ohp; NHP_UNSET_ALL(nhp);
Pekka Pessi's avatar
Pekka Pessi committed
997 998 999

#if 0
  reinit_contact = 
1000
    nua->nua_dhandle->nh_callee_caps != callee_caps ||
Pekka Pessi's avatar
Pekka Pessi committed
1001 1002 1003 1004
    media_path != NONE ||
    allow != NONE || allow_str != NONE;
#endif

1005 1006 1007
  if (invite_timeout > 0 && invite_timeout < 30)
    invite_timeout = 30;

Pekka Pessi's avatar
Pekka Pessi committed
1008 1009 1010 1011 1012 1013 1014 1015 1016
  if (min_se > 0 && min_se < 30)
    min_se = 30;
  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)
1017
    refresher = nua_remote_refresher;
Pekka Pessi's avatar
Pekka Pessi committed
1018
  else if (refresher <= nua_no_refresher)
1019 1020
    refresher = nua_no_refresher;

1021
  /* Set int in handle pref structure */
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
#define NHP_SET(nhp, pref, value)			   \
  (((nhp)->nhp_set.set_bits.nhp_##pref = (value) != (nhp)->nhp_##pref), \
   (nhp)->nhp_##pref = value)

  NHP_SET(nhp, retry_count, retry_count);
  NHP_SET(nhp, max_subscriptions, max_subscriptions);

  NHP_SET(nhp, invite_enable, invite_enable);
  NHP_SET(nhp, auto_alert, auto_alert != 0);
  NHP_SET(nhp, early_media, early_media != 0);
  NHP_SET(nhp, auto_answer, auto_answer != 0);
  NHP_SET(nhp, auto_ack, auto_ack != 0);
  NHP_SET(nhp, invite_timeout, invite_timeout);

  NHP_SET(nhp, session_timer, session_timer);
  NHP_SET(nhp, min_se, min_se);
  NHP_SET(nhp, refresher, refresher);
  NHP_SET(nhp, update_refresh, update_refresh != 0);

  NHP_SET(nhp, message_enable, message_enable);
  NHP_SET(nhp, win_messenger_enable, win_messenger_enable);
  NHP_SET(nhp, message_auto_respond, message_auto_respond);

  NHP_SET(nhp, media_features, media_features != 0);
  NHP_SET(nhp, callee_caps, callee_caps != 0);
  NHP_SET(nhp, service_route_enable, service_route_enable != 0);
  NHP_SET(nhp, path_enable, path_enable != 0);

1050
  /* Set string in handle pref structure */
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
#define NHP_SET_STR(nhp, name, str)				 \
  if (str != NONE && str0cmp(str, nhp->nhp_##name)) {		 \
    char *new_str = su_strdup(tmphome, str);			 \
    if (new_str != NULL || str == NULL) {			 \
      NHP_SET(nhp, name, new_str);				 \
    }								 \
    else {							 \
      n = -1;							 \
    }								 \
  }

1062
  /* Set header in handle pref structure */
1063 1064 1065 1066 1067 1068 1069
#define NHP_SET_HEADER(nhp, name, header, str)			 \
  if (header != NONE || str != NONE) {				 \
    sip_##name##_t *new_header;					 \
    if (header != NONE)						 \
      new_header = sip_##name##_dup(tmphome, header);		 \
    else							 \
      new_header = sip_##name##_make(tmphome, str);		 \
Pekka Pessi's avatar
Pekka Pessi committed
1070
    if (new_header != NULL || (header == NULL || str == NULL)) { \
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
      NHP_SET(nhp, name, new_header);				 \
    }								 \
    else {							 \
      n = -1;							 \
    }								 \
  }

  NHP_SET_HEADER(nhp, supported, supported, supported_str);
  NHP_SET_HEADER(nhp, allow, allow, allow_str);
  if (ua_name != NONE && user_agent_str == NONE && user_agent == NONE)
    user_agent_str = ua_name 
      ? su_sprintf(tmphome, "%s %s", ua_name, PACKAGE_NAME "/" PACKAGE_VERSION)
      : PACKAGE_NAME "/" PACKAGE_VERSION;
  NHP_SET_HEADER(nhp, user_agent, user_agent, user_agent_str);
  NHP_SET_STR(nhp, ua_name, ua_name);
  NHP_SET_HEADER(nhp, organization, organization, organization_str);

  if (n > 0 && NHP_IS_ANY_SET(nhp)) {
    /* Move allocations from tmphome to handle's home */
    if (nh != dnh && nh->nh_prefs == dnh->nh_prefs) {
      /* We have made changes to handle-specific settings
       * but we don't have a prefs structure owned by handle yet */
      nua_handle_preferences_t *ahp = su_alloc(nh->nh_home, sizeof *ahp);
1094
      if (ahp && su_home_move(nh->nh_home, tmphome) >= 0) {
1095
	memcpy(ahp, nhp, sizeof *ahp);
1096

1097 1098 1099 1100 1101 1102 1103 1104 1105
	/* Zap pointers which are not set */
#define NHP_ZAP_UNSET_PTR(nhp, pref) \
	(!(nhp)->nhp_set.set_bits.nhp_##pref ? (nhp)->nhp_##pref = NULL : NULL)

	NHP_ZAP_UNSET_PTR(ahp, supported);
	NHP_ZAP_UNSET_PTR(ahp, allow);
	NHP_ZAP_UNSET_PTR(ahp, user_agent);
	NHP_ZAP_UNSET_PTR(ahp, ua_name);
	NHP_ZAP_UNSET_PTR(ahp, organization);
1106 1107 1108 1109 1110

	nh->nh_prefs = ahp;
      }
      else {
	n = -1;
1111 1112 1113 1114 1115 1116 1117
      }
    }
    else if (su_home_move(nh->nh_home, tmphome) >= 0) {
      /* Update prefs structure */
      nua_handle_preferences_t tbf[1];
      nhp->nhp_set.set_any |= ohp->nhp_set.set_any;
      *tbf = *ohp; *ohp = *nhp; 
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

      /* Free changed items */
#define NHP_ZAP_OVERRIDEN(tbf, nhp, pref)			\
      ((tbf)->nhp_set.set_bits.nhp_##pref			\
       && (tbf)->nhp_##pref != (nhp)->nhp_##pref		\
       ? su_free(nh->nh_home, (void *)(tbf)->nhp_##pref) : (void)0)

      NHP_ZAP_OVERRIDEN(tbf, nhp, supported);
      NHP_ZAP_OVERRIDEN(tbf, nhp, allow);
      NHP_ZAP_OVERRIDEN(tbf, nhp, user_agent);
      NHP_ZAP_OVERRIDEN(tbf, nhp, ua_name);
      NHP_ZAP_OVERRIDEN(tbf, nhp, organization);
1130 1131 1132 1133
    }
    else
      /* Fail miserably with ENOMEM */
      n = -1;
Pekka Pessi's avatar
Pekka Pessi committed
1134 1135
  }

1136
  su_home_deinit(tmphome);
Pekka Pessi's avatar
Pekka Pessi committed
1137

1138
  if (n < 0)
1139
    return UA_EVENT2(e, 500, "Error storing parameters"), -1;
1140 1141

  if (nh != dnh)
Pekka Pessi's avatar
Pekka Pessi committed
1142
    return e == nua_r_set_params ? UA_EVENT2(e, 200, "OK") : 0;
Pekka Pessi's avatar
Pekka Pessi committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

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

1154 1155 1156 1157
  if (from != NONE || from_str != NONE) {
    if (from == NONE) from = NULL;
    if (from_str == NONE) from_str = NULL;
    ua_set_from(nua, from, from_str);
Pekka Pessi's avatar
Pekka Pessi committed
1158 1159 1160 1161
  }

  ua_init_contact(nua);

1162 1163 1164 1165 1166
#if HAVE_SOFIA_SMIME 
  /* XXX - all S/MIME other parameters? */
  sm_set_params(nua->sm, smime_enable, smime_opt, 
		smime_protection_mode, smime_path);
#endif                  
Pekka Pessi's avatar
Pekka Pessi committed
1167
  return e == nua_r_set_params ? UA_EVENT2(e, 200, "OK") : 0;
Pekka Pessi's avatar
Pekka Pessi committed
1168 1169
}

1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
/** Get NUA parameters.
 *
 * The ua_get_params() sends a list of parameters to the application.  It
 * gets invoked when application calls either nua_get_params() or
 * nua_get_hparams(). 
 *
 * The parameter tag list will initially contain all the relevant parameter
 * tags, and it will be filtered down to parameters asked by application.
 *
 * The handle-specific parameters will contain only the parameters actually
 * modified by application, either by nua_set_hparams() or some other
 * handle-specific call. NTA parameters are returned only when application
 * asks for user-agent-level parameters using nua_get_params().
 *
 */
int
Pekka Pessi's avatar
Pekka Pessi committed
1186 1187
ua_get_params(nua_t *nua, nua_handle_t *nh, nua_event_t e, tagi_t const *tags)
{
1188 1189 1190
  nua_handle_t *dnh = nua->nua_dhandle;
  nua_handle_preferences_t const *nhp = nh->nh_prefs;

Pekka Pessi's avatar
Pekka Pessi committed
1191
  tagi_t *lst;
1192 1193

  int has_from;
Pekka Pessi's avatar
Pekka Pessi committed
1194 1195
  sip_from_t from[1];

1196
  /* nta */
Pekka Pessi's avatar
Pekka Pessi committed
1197 1198 1199 1200 1201
  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;
1202 1203
  
  /* soa */
1204
  tagi_t *media_params = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1205

Pekka Pessi's avatar
Pekka Pessi committed
1206
  su_home_t tmphome[SU_HOME_AUTO_SIZE(16536)];
Pekka Pessi's avatar
Pekka Pessi committed
1207 1208 1209

  enter;

Pekka Pessi's avatar
Pekka Pessi committed
1210 1211
  su_home_auto(tmphome, sizeof(tmphome));

Pekka Pessi's avatar
Pekka Pessi committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
  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());

1224 1225 1226 1227 1228 1229 1230
  if (nh->nh_ds->ds_local)
    has_from = 1, *from = *nh->nh_ds->ds_local, from->a_params = NULL;
  else 
    has_from = 0;

  media_params = soa_get_paramlist(nh->nh_soa, TAG_END());

1231 1232
  /* Include tag in list returned to user
   * if it has been earlier set (by user) */
1233 1234 1235
#define TIF(TAG, pref) \
  TAG_IF(nhp->nhp_set.set_bits.nhp_##pref, TAG(nhp->nhp_##pref))

1236 1237
  /* Include string tag made out of SIP header
   * if it has been earlier set (by user) */
1238 1239 1240 1241
#define TIF_STR(TAG, pref)						\
  TAG_IF(nhp->nhp_set.set_bits.nhp_##pref,				\
	 TAG(nhp->nhp_set.set_bits.nhp_##pref && nhp->nhp_##pref	\
	     ? sip_header_as_string(tmphome, (void *)nhp->nhp_##pref) : NULL))
Pekka Pessi's avatar
Pekka Pessi committed
1242

Pekka Pessi's avatar
Pekka Pessi committed
1243 1244
  lst = tl_filtered_tlist
    (tmphome, tags, 
1245
     TAG_IF(has_from, SIPTAG_FROM(from)),
1246 1247 1248 1249
     TAG_IF(has_from,
	    SIPTAG_FROM_STR(has_from
			    ? sip_header_as_string(tmphome, (void *)from)
			    : NULL)),
1250

1251 1252
     TIF(NUTAG_RETRY_COUNT, retry_count),
     TIF(NUTAG_MAX_SUBSCRIPTIONS, max_subscriptions),
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

     TIF(NUTAG_ENABLEINVITE, invite_enable),
     TIF(NUTAG_AUTOALERT, auto_alert),
     TIF(NUTAG_EARLY_MEDIA, early_media),
     TIF(NUTAG_AUTOANSWER, auto_answer),
     TIF(NUTAG_AUTOACK, auto_ack),
     TIF(NUTAG_INVITE_TIMER, invite_timeout),

     TIF(NUTAG_SESSION_TIMER, session_timer),
     TIF(NUTAG_MIN_SE, min_se),
     TIF(NUTAG_SESSION_REFRESHER, refresher),
     TIF(NUTAG_UPDATE_REFRESH, update_refresh),

     TIF(NUTAG_ENABLEMESSAGE, message_enable),
     TIF(NUTAG_ENABLEMESSENGER, win_messenger_enable),
     /* TIF(NUTAG_MESSAGE_AUTOANSWER, message_auto_respond), */

     TIF(NUTAG_CALLEE_CAPS, callee_caps),
     TIF(NUTAG_MEDIA_FEATURES, media_features),
     TIF(NUTAG_SERVICE_ROUTE_ENABLE, service_route_enable),
     TIF(NUTAG_PATH_ENABLE, path_enable),

     TIF(SIPTAG_SUPPORTED, supported),
     TIF_STR(SIPTAG_SUPPORTED_STR, supported),
     TIF(SIPTAG_ALLOW, allow),
     TIF_STR(SIPTAG_ALLOW_STR, allow),
     TIF(SIPTAG_USER_AGENT, user_agent),
     TIF_STR(SIPTAG_USER_AGENT_STR, user_agent),
     TIF(NUTAG_USER_AGENT, ua_name),

     TIF(SIPTAG_ORGANIZATION, organization),
     TIF_STR(SIPTAG_ORGANIZATION_STR, organization),

     /* Skip user-agent-level parameters if parameters are for handle only */
     TAG_IF(nh != dnh, TAG_NEXT(media_params)),
     
Pekka Pessi's avatar
Pekka Pessi committed
1289
     NUTAG_MEDIA_ENABLE(nua->nua_media_enable),
1290 1291 1292 1293
     NUTAG_REGISTRAR(nua->nua_registrar),
     NTATAG_CONTACT(nua->nua_contact
		    ? nua->nua_contact : nua->nua_sips_contact),

Pekka Pessi's avatar
Pekka Pessi committed
1294
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
1295 1296 1297 1298 1299 1300 1301
     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
1302
#endif                  
1303

Pekka Pessi's avatar
Pekka Pessi committed
1304 1305 1306 1307 1308 1309 1310 1311 1312
     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),
1313 1314

     TAG_NEXT(media_params));
Pekka Pessi's avatar
Pekka Pessi committed
1315 1316

  ua_event(nua, nh, NULL, nua_r_get_params, SIP_200_OK, TAG_NEXT(lst));
Pekka Pessi's avatar
Pekka Pessi committed
1317

Pekka Pessi's avatar
Pekka Pessi committed
1318
  su_home_deinit(tmphome);
Pekka Pessi's avatar
Pekka Pessi committed
1319 1320

  tl_vfree(media_params);
1321 1322

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
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
}

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

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 && 
1368 1369
    nh->nh_ss->ss_state > nua_callstate_init &&
    nh->nh_ss->ss_state < nua_callstate_terminated;
Pekka Pessi's avatar
Pekka Pessi committed
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
}

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)
{
1407
  assert(nh); assert(nh != nua->nua_dhandle);
Pekka Pessi's avatar
Pekka Pessi committed
1408