nua_stack.c 176 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
/*
 * 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>
41
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
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

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

#include <sl_utils.h>

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

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

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

101 102 103 104 105 106 107 108 109
#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

110
typedef unsigned longlong ull;
Pekka Pessi's avatar
Pekka Pessi committed
111

112 113 114 115 116 117 118 119 120
#define SET_STATUS(_status, _phrase) status = _status, phrase = _phrase

/* This is interesting macro: 
 * x expands to "num, str", num is assigned to status, str to phrase.
 * Macro expands to two comma-separated expressions 
 * usable as function arguments
 */
#define SET_STATUS1(x) ((status = x), status), (phrase = ((void)x))

Pekka Pessi's avatar
Pekka Pessi committed
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
/* ========================================================================
 *
 *                       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);

147
static void ua_set_from(nua_t *nua, sip_from_t const *f, char const *fromstr);
Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161

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

162 163 164 165 166
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);
167

Pekka Pessi's avatar
Pekka Pessi committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
#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[] =
187
"INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, "
Pekka Pessi's avatar
Pekka Pessi committed
188 189
"MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE";

Pekka Pessi's avatar
Pekka Pessi committed
190 191 192
/** Default internal error */
char const nua_500_error[] = "Internal NUA Error";

Pekka Pessi's avatar
Pekka Pessi committed
193 194 195 196 197 198 199 200 201 202
/* ----------------------------------------------------------------------
 * 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;
203 204
  sip_from_t const *from = NULL;
  char const *from_str = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
205 206 207 208

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

209 210
  nua_handle_t *dnh;
  nua_handle_preferences_t *dnhp;
Pekka Pessi's avatar
Pekka Pessi committed
211
  int media_enable = 1;
Pekka Pessi's avatar
Pekka Pessi committed
212
  soa_session_t *soa = NULL;
213
  char const *soa_name = NULL;
214
  
Pekka Pessi's avatar
Pekka Pessi committed
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;

235 236 237
  dnh = su_home_clone(home, sizeof (*dnh) + sizeof(*dnhp));
  if (!dnh)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
238

239
  nua->nua_root = root;
Pekka Pessi's avatar
Pekka Pessi committed
240
  nua->nua_handles_tail = &nua->nua_handles;
241
  nh_append(nua, dnh);  
Pekka Pessi's avatar
Pekka Pessi committed
242
  
243 244 245 246 247 248 249
#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
250

251 252
  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
253

254
  dnh->nh_prefs = dnhp = (void *)(dnh + 1);
Pekka Pessi's avatar
Pekka Pessi committed
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 282 283 284 285 286 287
  /* 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
288 289
  tl_gets(nua->nua_args,
	  NUTAG_URL_REF(contact),
290 291
	  SIPTAG_FROM_REF(from),
	  SIPTAG_FROM_STR_REF(from_str),
Pekka Pessi's avatar
Pekka Pessi committed
292 293 294 295
	  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
296
	  NUTAG_MEDIA_ENABLE_REF(media_enable),
Pekka Pessi's avatar
Pekka Pessi committed
297
	  /* NUTAG_SOA_SESSION_REF(soa), */
298
	  NUTAG_SOA_NAME_REF(soa_name),
Pekka Pessi's avatar
Pekka Pessi committed
299 300 301 302 303 304 305
	  TAG_NULL());

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

306
  nua->nua_nta = nta_agent_create(root, NONE, NULL, NULL, 
Pekka Pessi's avatar
Pekka Pessi committed
307 308
				  TPTAG_CERTIFICATE(certificate_dir),
				  NTATAG_TAG_3261(0),
Pekka Pessi's avatar
Pekka Pessi committed
309
				  TAG_NEXT(nua->nua_args));
Pekka Pessi's avatar
Pekka Pessi committed
310 311 312
  if (!nua->nua_nta)
    return -1;

Pekka Pessi's avatar
Pekka Pessi committed
313 314 315 316 317 318 319 320 321 322
  if (!contact && !sips_contact) {
    if (nta_agent_add_tport(nua->nua_nta, NULL,
			    TAG_NEXT(nua->nua_args)) < 0 &&
	nta_agent_add_tport(nua->nua_nta, URL_STRING_MAKE("sip:*:*"),
			    TAG_NEXT(nua->nua_args)) < 0)
      return -1;
  }
  else if ((!contact ||
       nta_agent_add_tport(nua->nua_nta, contact,
			   TAG_NEXT(nua->nua_args)) < 0) &&
323
      (!sips_contact ||
Pekka Pessi's avatar
Pekka Pessi committed
324 325
       nta_agent_add_tport(nua->nua_nta, sips_contact,
			   TAG_NEXT(nua->nua_args)) < 0)) {
326 327 328
    return -1;
  }
    
Pekka Pessi's avatar
Pekka Pessi committed
329 330 331 332 333 334
  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
335
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
336 337 338 339 340 341 342
		       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);

343 344
  if (media_enable) {
    if (soa == NULL)
345 346
      soa = soa_create(soa_name, nua->nua_root, nua->nua_dhandle);
    dnh->nh_soa = soa;
Pekka Pessi's avatar
Pekka Pessi committed
347
    soa_set_params(soa, TAG_NEXT(nua->nua_args));
Pekka Pessi's avatar
Pekka Pessi committed
348 349
  }

350
  dnh->nh_ds->ds_leg = nta_leg_tcreate(nua->nua_nta, process_request, dnh,
Pekka Pessi's avatar
Pekka Pessi committed
351 352 353 354
				      NTATAG_NO_DIALOG(1), 
				      TAG_END());

  ua_init_contact(nua);
355
  ua_set_from(nua, from, from_str);
Pekka Pessi's avatar
Pekka Pessi committed
356

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

359 360 361
  if (!(dnh->nh_ds->ds_leg &&
	dnhp->nhp_allow && 
	dnhp->nhp_supported &&
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364 365 366 367
	(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
368 369
  ua_timer(nua, nua->nua_timer, NULL);

Pekka Pessi's avatar
Pekka Pessi committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383
  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 */
384
void ua_set_from(nua_t *nua, sip_from_t const *f, char const *str)
Pekka Pessi's avatar
Pekka Pessi committed
385
{
386
  sip_from_t from[1], *f0;
Pekka Pessi's avatar
Pekka Pessi committed
387 388 389 390 391

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

392 393
  sip_from_init(from);

Pekka Pessi's avatar
Pekka Pessi committed
394 395 396
  if (f) {
    from->a_display = f->a_display;
    *from->a_url = *f->a_url;
397 398 399 400 401 402 403
    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
404 405 406 407 408
  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;
409
    f0 = sip_from_dup(nua->nua_home, from);
Pekka Pessi's avatar
Pekka Pessi committed
410 411
  }

412 413
  if (f0)
    *nua->nua_from = *f0;
Pekka Pessi's avatar
Pekka Pessi committed
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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
}


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
456
  char const ***m_params = (msg_param_t **)&m->m_params;
Pekka Pessi's avatar
Pekka Pessi committed
457
  su_strlst_t *l = su_strlst_create(home);
458
  nua_handle_t *dnh = nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
459 460
  int i;

461 462 463 464
  if (DNH_PGET(dnh, callee_caps)) {
    sip_allow_t const *allow = DNH_PGET(dnh, allow);

    if (allow) {
Pekka Pessi's avatar
Pekka Pessi committed
465
      char *methods;
466 467 468
      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
469 470 471 472
      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
473

474 475
    if (dnh->nh_soa) {
      char **media = soa_media_features(dnh->nh_soa, 0, home);
Pekka Pessi's avatar
Pekka Pessi committed
476 477 478 479 480 481

      while (*media) {
	msg_params_replace(home, m_params, *media);
	media++;
      }
    }
Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  }

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

502
static void ua_shutdown(nua_t *);
Pekka Pessi's avatar
Pekka Pessi committed
503

Pekka Pessi's avatar
Pekka Pessi committed
504
static int
505 506
  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
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
  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;
594
    e->e_nh = nh ? nh_incref(nh) : nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
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
    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:
654
    ua_get_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
655 656
    break;
  case nua_r_set_params:
657
    ua_set_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    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
682
  case nua_r_unpublish:
Pekka Pessi's avatar
Pekka Pessi committed
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
    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;
  }

726
  if (nh != nua->nua_dhandle)
Pekka Pessi's avatar
Pekka Pessi committed
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 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    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
825

Pekka Pessi's avatar
Pekka Pessi committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
/* ====================================================================== */

/** 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
848 849
    if (ss->ss_srequest->sr_respond) {
      ss->ss_srequest->sr_respond(nua, nh, SIP_410_GONE, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
850 851 852 853 854
      busy++;
    }

    busy += nh_call_pending(nh, 0);

Pekka Pessi's avatar
Pekka Pessi committed
855 856
    if (nh->nh_soa) {
      soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
    }

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

876 877 878 879 880
  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
881 882 883 884 885 886
  ua_event(nua, NULL, NULL, nua_r_shutdown, status, phrase, TAG_END());
}

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

888 889
#include <msg_parser.h>

890 891
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
892
{
893 894 895 896 897 898
  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) };

Pekka Pessi's avatar
Pekka Pessi committed
899 900
  unsigned retry_count = NHP_GET(ohp, dnhp, retry_count);
  unsigned max_subscriptions = NHP_GET(ohp, dnhp, max_subscriptions);
901 902 903 904 905 906

  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);
Pekka Pessi's avatar
Pekka Pessi committed
907
  unsigned invite_timeout = NHP_GET(ohp, dnhp, invite_timeout);
908

Pekka Pessi's avatar
Pekka Pessi committed
909 910
  unsigned session_timer = NHP_GET(ohp, dnhp, session_timer);
  unsigned min_se = NHP_GET(ohp, dnhp, min_se);
911 912 913 914 915 916 917 918 919 920 921
  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);
922 923 924

  sip_allow_t const *allow = NONE;
  char const   *allow_str = NONE;
925
  char const   *allowing = NULL;
926 927
  sip_supported_t const *supported = NONE;
  char const *supported_str = NONE;
928
  sip_user_agent_t const *user_agent = NONE;
929 930 931
  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
932

933 934 935
  url_string_t const *registrar = NONE;
  sip_from_t const *from = NONE;
  char const *from_str = NONE;
Pekka Pessi's avatar
Pekka Pessi committed
936 937

#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
938 939 940 941 942 943 944 945
  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
946 947
#endif			

948
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
949 950 951

  enter;

952 953
  if (nh == dnh) 
    if (nta_agent_set_params(nua->nua_nta, TAG_NEXT(tags)) < 0)
954
      return UA_EVENT2(e, 400, "Error setting NTA parameters"), -1;
955

956
  if (nh->nh_soa && soa_set_params(nh->nh_soa, TAG_NEXT(tags)) < 0)
957
    return UA_EVENT2(e, 400, "Error setting SOA parameters"), -1;
958 959

  n =  tl_gets(tags,
960 961
	       NUTAG_RETRY_COUNT_REF(retry_count),
	       NUTAG_MAX_SUBSCRIPTIONS_REF(max_subscriptions),
962 963 964 965 966 967 968

	       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
969 970 971 972 973

	       NUTAG_SESSION_TIMER_REF(session_timer),
	       NUTAG_MIN_SE_REF(min_se),
	       NUTAG_SESSION_REFRESHER_REF(refresher),
	       NUTAG_UPDATE_REFRESH_REF(update_refresh),
974 975 976 977 978

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

Pekka Pessi's avatar
Pekka Pessi committed
979
	       NUTAG_CALLEE_CAPS_REF(callee_caps),
980
	       NUTAG_MEDIA_FEATURES_REF(media_features),
Pekka Pessi's avatar
Pekka Pessi committed
981
	       NUTAG_SERVICE_ROUTE_ENABLE_REF(service_route_enable),
982 983 984 985
	       NUTAG_PATH_ENABLE_REF(path_enable),

	       SIPTAG_SUPPORTED_REF(supported),
	       SIPTAG_SUPPORTED_STR_REF(supported_str),
986

987 988
	       SIPTAG_ALLOW_REF(allow),
	       SIPTAG_ALLOW_STR_REF(allow_str),
989 990
	       NUTAG_ALLOW_REF(allowing),

991
	       SIPTAG_USER_AGENT_REF(user_agent),
992 993 994 995 996 997 998 999 1000 1001 1002 1003
	       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
1004
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
1005 1006 1007 1008 1009 1010 1011 1012 1013
	       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
1014 1015
	       TAG_NULL());
  if (n < 0)
1016
    return UA_EVENT2(e, 400, "Error obtaining NUA parameters"), -1;
1017
  
1018
  *nhp = *ohp; NHP_UNSET_ALL(nhp);
Pekka Pessi's avatar
Pekka Pessi committed
1019 1020 1021

#if 0
  reinit_contact = 
1022
    nua->nua_dhandle->nh_callee_caps != callee_caps ||
Pekka Pessi's avatar
Pekka Pessi committed
1023 1024 1025 1026
    media_path != NONE ||
    allow != NONE || allow_str != NONE;
#endif

1027 1028 1029
  if (invite_timeout > 0 && invite_timeout < 30)
    invite_timeout = 30;

Pekka Pessi's avatar
Pekka Pessi committed
1030 1031 1032 1033 1034 1035 1036 1037 1038
  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)
1039
    refresher = nua_remote_refresher;
Pekka Pessi's avatar
Pekka Pessi committed
1040
  else if (refresher <= nua_no_refresher)
1041 1042
    refresher = nua_no_refresher;

1043
  /* Set int in handle pref structure */
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
#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);

1072
  /* Set string in handle pref structure */
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
#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;							 \
    }								 \
  }

1084
  /* Set header in handle pref structure */
1085 1086 1087 1088 1089 1090 1091
#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
1092
    if (new_header != NULL || (header == NULL || str == NULL)) { \
1093 1094 1095 1096 1097 1098 1099
      NHP_SET(nhp, name, new_header);				 \
    }								 \
    else {							 \
      n = -1;							 \
    }								 \
  }

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
  /* Add contents of NUTAG_ALLOW() to list of currently allowed methods */
  if (allow == NONE && allow_str == NONE && allowing != NULL) {
    sip_allow_t *methods = sip_allow_make(tmphome, allowing);

    if (methods)
      allow = sip_allow_dup(tmphome, NHP_GET(ohp, dnhp, allow));

    if (allow == NULL)
      allow = NONE;

    if (allow != NONE)
      if (msg_params_join(tmphome, 
			  (msg_param_t **)&allow->k_items, methods->k_items,
			  1 /* prune */, 0 /* don't dup */) < 0)
	allow = NONE;
  }

1117 1118
  NHP_SET_HEADER(nhp, supported, supported, supported_str);
  NHP_SET_HEADER(nhp, allow, allow, allow_str);
1119
  /* Add contents of NUTAG_USER_AGENT() to our distribution name */
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
  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);
1134
      if (ahp && su_home_move(nh->nh_home, tmphome) >= 0) {
1135
	memcpy(ahp, nhp, sizeof *ahp);
1136

1137 1138 1139 1140 1141 1142 1143 1144 1145
	/* 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);
1146 1147 1148 1149 1150

	nh->nh_prefs = ahp;
      }
      else {
	n = -1;
1151 1152 1153 1154 1155 1156 1157
      }
    }
    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; 
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

      /* 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);
1170 1171 1172 1173
    }
    else
      /* Fail miserably with ENOMEM */
      n = -1;
Pekka Pessi's avatar
Pekka Pessi committed
1174 1175
  }

1176
  su_home_deinit(tmphome);
Pekka Pessi's avatar
Pekka Pessi committed
1177

1178
  if (n < 0)
1179
    return UA_EVENT2(e, 500, "Error storing parameters"), -1;
1180 1181

  if (nh != dnh)
Pekka Pessi's avatar
Pekka Pessi committed
1182
    return e == nua_r_set_params ? UA_EVENT2(e, 200, "OK") : 0;
Pekka Pessi's avatar
Pekka Pessi committed
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

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

1194 1195 1196 1197
  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
1198 1199 1200 1201
  }

  ua_init_contact(nua);

1202 1203 1204 1205 1206
#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
1207
  return e == nua_r_set_params ? UA_EVENT2(e, 200, "OK") : 0;
Pekka Pessi's avatar
Pekka Pessi committed
1208 1209
}

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/** 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
1226 1227
ua_get_params(nua_t *nua, nua_handle_t *nh, nua_event_t e, tagi_t const *tags)
{
1228 1229 1230
  nua_handle_t *dnh = nua->nua_dhandle;
  nua_handle_preferences_t const *nhp = nh->nh_prefs;

Pekka Pessi's avatar
Pekka Pessi committed
1231
  tagi_t *lst;
1232 1233

  int has_from;
Pekka Pessi's avatar
Pekka Pessi committed
1234 1235
  sip_from_t from[1];

1236
  /* nta */
Pekka Pessi's avatar
Pekka Pessi committed
1237 1238 1239 1240 1241
  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;
1242 1243
  
  /* soa */
1244
  tagi_t *media_params = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1245

Pekka Pessi's avatar
Pekka Pessi committed
1246
  su_home_t tmphome[SU_HOME_AUTO_SIZE(16536)];
Pekka Pessi's avatar
Pekka Pessi committed
1247 1248 1249

  enter;

Pekka Pessi's avatar
Pekka Pessi committed
1250 1251
  su_home_auto(tmphome, sizeof(tmphome));

Pekka Pessi's avatar
Pekka Pessi committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
  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());

1264 1265 1266 1267 1268 1269 1270
  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());

1271 1272
  /* Include tag in list returned to user
   * if it has been earlier set (by user) */
1273 1274 1275
#define TIF(TAG, pref) \
  TAG_IF(nhp->nhp_set.set_bits.nhp_##pref, TAG(nhp->nhp_##pref))

1276 1277
  /* Include string tag made out of SIP header
   * if it has been earlier set (by user) */
1278 1279 1280 1281
#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
1282

Pekka Pessi's avatar
Pekka Pessi committed
1283 1284
  lst = tl_filtered_tlist
    (tmphome, tags, 
1285
     TAG_IF(has_from, SIPTAG_FROM(from)),
1286 1287 1288 1289
     TAG_IF(has_from,
	    SIPTAG_FROM_STR(has_from
			    ? sip_header_as_string(tmphome, (void *)from)
			    : NULL)),
1290

1291 1292
     TIF(NUTAG_RETRY_COUNT, retry_count),
     TIF(NUTAG_MAX_SUBSCRIPTIONS, max_subscriptions),
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

     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
1329
     NUTAG_MEDIA_ENABLE(nua->nua_media_enable),
1330 1331 1332 1333
     NUTAG_REGISTRAR(nua->nua_registrar),
     NTATAG_CONTACT(nua->nua_contact
		    ? nua->nua_contact : nua->nua_sips_contact),

Pekka Pessi's avatar
Pekka Pessi committed
1334
#if HAVE_SOFIA_SMIME
Pekka Pessi's avatar
Pekka Pessi committed
1335 1336 1337 1338 1339 1340 1341
     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
1342
#endif                  
1343

Pekka Pessi's avatar
Pekka Pessi committed
1344 1345 1346 1347 1348 1349 1350 1351 1352
     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),
1353 1354

     TAG_NEXT(media_params));
Pekka Pessi's avatar
Pekka Pessi committed
1355 1356

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

Pekka Pessi's avatar
Pekka Pessi committed
1358
  su_home_deinit(tmphome);
Pekka Pessi's avatar
Pekka Pessi committed
1359 1360

  tl_vfree(media_params);
1361 1362

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 <