nua_params.c 52.8 KB
Newer Older
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
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2006 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_register.c
 * @brief REGISTER and registrations
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Mar  8 11:48:49 EET 2006 ppessi
 */

#include "config.h"

35
#include <sofia-sip/su_string.h>
36 37 38
#include <sofia-sip/su_strlst.h>
#include <sofia-sip/token64.h>
#include <sofia-sip/su_tagarg.h>
39
#include <sofia-sip/su_tag_inline.h>
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

#include <sofia-sip/bnf.h>

#include <sofia-sip/sip_protos.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/msg_parser.h>

#include "nua_stack.h"

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

#include <assert.h>

57 58 59 60 61 62 63 64 65
/* ====================================================================== */
/* Helper macros and functions for handling #nua_handle_preferences_t. */

#define NHP_IS_ANY_SET(nhp) nhp_is_any_set((nhp))

/** Check if any preference is set in @a nhp. */
su_inline int nhp_is_any_set(nua_handle_preferences_t const *nhp)
{
  char nhp_zero[sizeof nhp->nhp_set] = { 0 };
66
  return memcmp(&nhp->nhp_set, nhp_zero, sizeof nhp->nhp_set) != 0;
67 68
}

69 70 71 72
/** Copy set parameters from @a b to @a a.
 *
 * If preference is set in @a b, mark it set also in @a a.
 */
73 74 75
su_inline void nhp_or_set(nua_handle_preferences_t *a,
			  nua_handle_preferences_t const *b)
{
76 77
  memcpy(a, b, offsetof(nua_handle_preferences_t, nhp_set));

78 79 80
  /* Bitwise or of bitfields, casted to unsigned */
  a->nhp_set_.set_unsigned[0] |= b->nhp_set_.set_unsigned[0];
  a->nhp_set_.set_unsigned[1] |= b->nhp_set_.set_unsigned[1];
81 82
}

83
static int nhp_set_tags(su_home_t *home,
84
			nua_handle_preferences_t *nhp,
85
			nua_global_preferences_t *ngp,
86
			tagi_t const *tags);
87

88 89 90 91 92 93 94 95
static int nhp_merge_lists(su_home_t *home,
			   msg_hclass_t *hc,
			   msg_list_t **return_new_list,
			   msg_list_t const *old_list,
			   int already_set,
			   int already_parsed,
			   int always_merge,
			   tag_value_t value);
96

97
static int nhp_save_params(nua_handle_t *nh,
98 99 100
			   su_home_t *tmphome,
			   nua_global_preferences_t *gsrc,
			   nua_handle_preferences_t *src);
101

102 103 104 105 106
/* ====================================================================== */
/* Magical NUTAG_USER_AGENT() - add NHP_USER_AGENT there if it is not there */

#define NHP_USER_AGENT PACKAGE_NAME "/" PACKAGE_VERSION

107
static int already_contains_package_name(char const *s)
108 109 110
{
  char const pn[] = " " PACKAGE_NAME "/";
  size_t pnlen = strlen(pn + 1);
111

112
  return su_casenmatch(s, pn + 1, pnlen) || su_strcasestr(s, pn);
113
}
114

115 116 117 118 119 120 121 122 123 124 125
/* ====================================================================== */
/* Stack and handle parameters */

static int nua_stack_set_smime_params(nua_t *nua, tagi_t const *tags);

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

/** @internal Set default parameters */
126
int nua_stack_set_defaults(nua_handle_t *nh,
127 128 129 130 131 132 133 134
			   nua_handle_preferences_t *nhp)
{
  su_home_t *home = (su_home_t *)nh;

  /* Set some defaults */
  NHP_SET(nhp, retry_count, 3);
  NHP_SET(nhp, max_subscriptions, 20);

135
  NHP_SET(nhp, media_enable, 1);
136 137 138
  NHP_SET(nhp, invite_enable, 1);
  NHP_SET(nhp, auto_alert, 0);
  NHP_SET(nhp, early_media, 0);
139
  NHP_SET(nhp, only183_100rel, 0);
140 141 142 143
  NHP_SET(nhp, auto_answer, 0);
  NHP_SET(nhp, auto_ack, 1);
  NHP_SET(nhp, invite_timeout, 120);

144 145 146
  nhp->nhp_session_timer = 1800;
  nhp->nhp_refresher = nua_no_refresher;

147 148 149 150 151
  NHP_SET(nhp, min_se, 120);
  NHP_SET(nhp, update_refresh, 0);

  NHP_SET(nhp, message_enable, 1);
  NHP_SET(nhp, win_messenger_enable, 0);
152 153
  if (getenv("PIMIW_HACK") != 0)
    NHP_SET(nhp, message_auto_respond, 1);
154 155 156 157 158 159 160

  NHP_SET(nhp, media_features,  0);
  NHP_SET(nhp, callee_caps, 0);
  NHP_SET(nhp, service_route_enable, 1);
  NHP_SET(nhp, path_enable, 1);

  NHP_SET(nhp, refer_expires, 300);
161
  NHP_SET(nhp, refer_with_id, 1);
162

163 164 165
  NHP_SET(nhp, auto302, 1);
  NHP_SET(nhp, auto305, 1);

166
  NHP_SET(nhp, substate, nua_substate_active);
167
  NHP_SET(nhp, sub_expires, 3600);
168 169 170

  NHP_SET(nhp, allow, sip_allow_make(home, nua_allow_str));
  NHP_SET(nhp, supported, sip_supported_make(home, "timer, 100rel"));
171
  NHP_SET(nhp, user_agent, su_strdup(home, NHP_USER_AGENT));
172

173
  NHP_SET(nhp, outbound, su_strdup(home, "natify"));
174

175
  NHP_SET(nhp, keepalive, 120000);
176

177 178 179
  NHP_SET(nhp, appl_method,
	  sip_allow_make(home, "INVITE, REGISTER, PUBLISH, SUBSCRIBE"));

180 181 182 183 184 185 186 187 188 189
  if (!nhp->nhp_allow ||
      !nhp->nhp_supported ||
      !nhp->nhp_user_agent ||
      !nhp->nhp_outbound)
    return -1;

  return 0;
}

/** @internal Set the default from field */
190
int nua_stack_set_from(nua_t *nua, int initial, tagi_t const *tags)
191 192 193 194
{
  sip_from_t const *from = NONE;
  char const *str = NONE;
  sip_from_t *f = NULL,  f0[1];
195
  int set;
196 197 198 199 200 201 202 203

  tl_gets(tags,
	  /* By nua_stack_set_from() */
	  SIPTAG_FROM_REF(from),
	  SIPTAG_FROM_STR_REF(str),
	  TAG_END());

  if (!initial && from == NONE && str == NONE)
204
    return 0;
205 206 207 208 209 210 211

  sip_from_init(f0);

  if (from && from != NONE) {
    f0->a_display = from->a_display;
    *f0->a_url = *from->a_url;
    f = sip_from_dup(nua->nua_home, f0);
212
    set = 1;
213 214 215 216 217
  }
  else if (str && str != NONE) {
    f = sip_from_make(nua->nua_home, str);
    if (f)
      *f0 = *f, f = f0, f->a_params = NULL;
218
    set = 1;
219 220
  }
  else {
221 222 223
    sip_contact_t const *m;

    m = nua_stack_get_contact(nua->nua_registrations);
224

225 226 227 228 229
    if (m) {
      f0->a_display = m->m_display;
      *f0->a_url = *m->m_url;
      f = sip_from_dup(nua->nua_home, f0);
    }
230
    set = 0;
231 232
  }

233 234
  if (!f)
    return -1;
235

236
  nua->nua_from_is_set = set;
237 238
  *nua->nua_from = *f;
  return 0;
239 240 241 242 243 244 245 246 247 248 249
}

/** @internal Initialize instance ID. */
int nua_stack_init_instance(nua_handle_t *nh, tagi_t const *tags)
{
  nua_handle_preferences_t *nhp = nh->nh_prefs;

  char const *instance = NONE;

  tl_gets(tags, NUTAG_INSTANCE_REF(instance), TAG_END());

250
  if (instance != NONE) {
251
    NHP_SET(nhp, instance, su_strdup(nh->nh_home, instance));
252
    if (instance && !nhp->nhp_instance)
253
      return -1;
254 255 256 257 258 259 260
  }

  return 0;
}

/**@fn void nua_set_params(nua_t *nua, tag_type_t tag, tag_value_t value, ...)
 *
261
 * Set @nua parameters, shared by all handles.
262 263 264 265 266 267 268 269
 *
 * @param nua             Pointer to NUA stack object
 * @param tag, value, ... List of tagged parameters
 *
 * @return
 *     nothing
 *
 * @par Related tags:
270
 *   NUTAG_ALLOW(), SIPTAG_ALLOW(), and SIPTAG_ALLOW_STR() \n
271
 *   NUTAG_ALLOW_EVENTS(), SIPTAG_ALLOW_EVENTS(), and
272
 *                         SIPTAG_ALLOW_EVENTS_STR() \n
273 274 275 276
 *   NUTAG_AUTOACK() \n
 *   NUTAG_AUTOALERT() \n
 *   NUTAG_AUTOANSWER() \n
 *   NUTAG_CALLEE_CAPS() \n
277
 *   NUTAG_DETECT_NETWORK_UPDATES() \n
278
 *   NUTAG_EARLY_ANSWER() \n
279 280 281 282
 *   NUTAG_EARLY_MEDIA() \n
 *   NUTAG_ENABLEINVITE() \n
 *   NUTAG_ENABLEMESSAGE() \n
 *   NUTAG_ENABLEMESSENGER() \n
283 284
 *   NUTAG_INITIAL_ROUTE() \n
 *   NUTAG_INITIAL_ROUTE_STR() \n
285 286 287 288 289
 *   NUTAG_INSTANCE() \n
 *   NUTAG_INVITE_TIMER() \n
 *   NUTAG_KEEPALIVE() \n
 *   NUTAG_KEEPALIVE_STREAM() \n
 *   NUTAG_MAX_SUBSCRIPTIONS() \n
290
 *   NUTAG_MEDIA_ENABLE() \n
291 292
 *   NUTAG_MEDIA_FEATURES() \n
 *   NUTAG_MIN_SE() \n
293 294 295 296 297
 *   NUTAG_M_DISPLAY() \n
 *   NUTAG_M_FEATURES() \n
 *   NUTAG_M_PARAMS() \n
 *   NUTAG_M_USERNAME() \n
 *   NUTAG_ONLY183_100REL() \n
298 299
 *   NUTAG_OUTBOUND() \n
 *   NUTAG_PATH_ENABLE() \n
300
 *   NUTAG_PROXY() (aka NTATAG_DEFAULT_PROXY()) \n
301 302
 *   NUTAG_REFER_EXPIRES() \n
 *   NUTAG_REFER_WITH_ID() \n
303
 *   NUTAG_REFRESH_WITHOUT_SDP() \n
304
 *   NUTAG_REGISTRAR() \n
305
 *   NUTAG_RETRY_COUNT() \n
306
 *   NUTAG_SERVICE_ROUTE_ENABLE() \n
307
 *   NUTAG_SESSION_REFRESHER() \n
308 309 310 311 312 313 314 315
 *   NUTAG_SESSION_TIMER() \n
 *   NUTAG_SMIME_ENABLE() \n
 *   NUTAG_SMIME_KEY_ENCRYPTION() \n
 *   NUTAG_SMIME_MESSAGE_DIGEST() \n
 *   NUTAG_SMIME_MESSAGE_ENCRYPTION() \n
 *   NUTAG_SMIME_OPT() \n
 *   NUTAG_SMIME_PROTECTION_MODE() \n
 *   NUTAG_SMIME_SIGNATURE() \n
316
 *   NUTAG_SOA_NAME() \n
317
 *   NUTAG_SUBSTATE() \n
318
 *   NUTAG_SUB_EXPIRES() \n
319
 *   NUTAG_SUPPORTED(), SIPTAG_SUPPORTED(), and SIPTAG_SUPPORTED_STR() \n
320
 *   NUTAG_UPDATE_REFRESH() \n
321 322
 *   NUTAG_USER_AGENT(), SIPTAG_USER_AGENT() and SIPTAG_USER_AGENT_STR() \n
 *   SIPTAG_ORGANIZATION() and SIPTAG_ORGANIZATION_STR() \n
323 324 325
 *
 * nua_set_params() also accepts any soa tags, defined in
 * <sofia-sip/soa_tag.h>, and nta tags, defined in <sofia-sip/nta_tag.h>.
326
 *
327
 * @par Events:
328
 *     #nua_r_set_params
329 330 331 332 333 334 335 336 337
 *
 * @par SIP Header as NUA Parameters
 * The @nua parameters include SIP headers @Allow, @Supported, @Organization,
 * @UserAgent and @From. They are included in most of the SIP messages sent
 * by @nua. They are set in the same way as the tagged arguments are
 * used to populate a SIP message.
 * @par
 * When multiple tags for the same header are specified, the behaviour
 * depends on the header type. If only a single header field can be included
338
 * in a SIP message, the latest non-NULL value is used, e.g., @Organization.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
 * However, if the SIP header can consist of multiple lines or header fields
 * separated by comma, in this case, @Allow and @Supported, all the tagged
 * values are concatenated.
 * @par
 * However, if the tag value is #SIP_NONE (-1 casted as a void pointer), the
 * values from previous tags are ignored.
 *
 * For example, the nua_set_params() call like this:
 * @code
 * nua_set_params(nua,
 *                SIPTAG_USER_AGENT_STR("tester/1.0"),
 *                SIPTAG_ALLOW_STR("INVITE,CANCEL,BYE,ACK"),
 *                SIPTAG_ORGANIZATION(NULL),
 *                SIPTAG_USER_AGENT(NULL),
 *                SIPTAG_ALLOW(SIP_NONE),
 *                TAG_END());
 * @endcode
 * will leave @Allow and @Organization headers empty. The @UserAgent header
 * will contain value "tester/1.0".
 * @code
 * nua_set_params(nua,
 *                SIPTAG_ORGANIZATION_STR("Malevolent Microwavers"),
 *                SIPTAG_ALLOW_STR("OPTIONS"),
 *                SIPTAG_ALLOW(SIP_NONE),
 *                SIPTAG_ORGANIZATION_STR("The Phone Company"),
 *                SIPTAG_ALLOW_STR("SUBSCRIBE"),
 *                SIPTAG_ALLOW(NULL),
 *                SIPTAG_ORGANIZATION_STR(NULL),
 *                TAG_END());
 * @endcode
 * sets the header @Allow with value <code>SUBSCRIBE</code> and the
 * header @Organization will have value <code>The Phone Company</code>.
 *
372 373 374
 */

/**@fn void nua_set_hparams(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...);
375 376 377 378 379 380 381 382 383
 *
 * Set the handle-specific parameters.
 *
 * The handle-specific parameters override default or global parameters set
 * by nua_set_params(). The handle-specific parameters are set by several
 * other operations: nua_invite(), nua_respond(), nua_ack(),
 * nua_prack(), nua_update(), nua_info(), nua_bye(), nua_options(),
 * nua_message(), nua_register(), nua_publish(), nua_refer(),
 * nua_subscribe(), nua_notify(), nua_refer(), and nua_notifier().
384 385 386 387 388 389 390
 *
 * @param nh              Pointer to a NUA handle
 * @param tag, value, ... List of tagged parameters
 *
 * @return
 *     nothing
 *
391
 * @par Tags Used to Set Handle-Specific Parameters:
392
 *   NUTAG_ALLOW(), SIPTAG_ALLOW(), and SIPTAG_ALLOW_STR() \n
393
 *   NUTAG_ALLOW_EVENTS(), SIPTAG_ALLOW_EVENTS(), and
394
 *                         SIPTAG_ALLOW_EVENTS_STR() \n
395
 *   NUTAG_AUTH_CACHE() \n
396 397 398 399
 *   NUTAG_AUTOACK() \n
 *   NUTAG_AUTOALERT() \n
 *   NUTAG_AUTOANSWER() \n
 *   NUTAG_CALLEE_CAPS() \n
400
 *   NUTAG_EARLY_ANSWER() \n
401 402 403 404
 *   NUTAG_EARLY_MEDIA() \n
 *   NUTAG_ENABLEINVITE() \n
 *   NUTAG_ENABLEMESSAGE() \n
 *   NUTAG_ENABLEMESSENGER() \n
405 406
 *   NUTAG_INITIAL_ROUTE() \n
 *   NUTAG_INITIAL_ROUTE_STR() \n
407
 *   NUTAG_INSTANCE() \n
408 409 410 411
 *   NUTAG_INVITE_TIMER() \n
 *   NUTAG_KEEPALIVE() \n
 *   NUTAG_KEEPALIVE_STREAM() \n
 *   NUTAG_MAX_SUBSCRIPTIONS() \n
412
 *   NUTAG_MEDIA_ENABLE() \n
413 414
 *   NUTAG_MEDIA_FEATURES() \n
 *   NUTAG_MIN_SE() \n
415 416 417 418 419 420
 *   NUTAG_M_DISPLAY() \n
 *   NUTAG_M_FEATURES() \n
 *   NUTAG_M_PARAMS() \n
 *   NUTAG_M_USERNAME() \n
 *   NUTAG_ONLY183_100REL() \n
 *   NUTAG_OUTBOUND() \n
421
 *   NUTAG_PATH_ENABLE() \n
422
 *   NUTAG_PROXY() (aka NTATAG_DEFAULT_PROXY()) \n
423 424
 *   NUTAG_REFER_EXPIRES() \n
 *   NUTAG_REFER_WITH_ID() \n
425
 *   NUTAG_REFRESH_WITHOUT_SDP() \n
426
 *   NUTAG_REGISTRAR() \n
427 428
 *   NUTAG_RETRY_COUNT() \n
 *   NUTAG_SERVICE_ROUTE_ENABLE() \n
429
 *   NUTAG_SESSION_REFRESHER() \n
430
 *   NUTAG_SESSION_TIMER() \n
431
 *   NUTAG_SOA_NAME() \n
432
 *   NUTAG_SUBSTATE() \n
433
 *   NUTAG_SUB_EXPIRES() \n
434
 *   NUTAG_SUPPORTED(), SIPTAG_SUPPORTED(), and SIPTAG_SUPPORTED_STR() \n
435
 *   NUTAG_UPDATE_REFRESH() \n
436 437
 *   NUTAG_USER_AGENT(), SIPTAG_USER_AGENT() and SIPTAG_USER_AGENT_STR() \n
 *   SIPTAG_ORGANIZATION() and SIPTAG_ORGANIZATION_STR() \n
438 439
 * Any soa tags are also considered as handle-specific parameters. They are
 * defined in <sofia-sip/soa_tag.h>.
440
 *
441
 * The global parameters that can not be set by nua_set_hparams() include
442
 * NUTAG_DETECT_NETWORK_UPDATES(), NUTAG_SMIME_* tags, and all NTA tags.
443
 *
444
 * @par Events:
445
 *     #nua_r_set_params
446 447
 */

448 449 450 451 452 453 454 455 456
/** @NUA_EVENT nua_r_set_params
 *
 * Response to nua_set_params() or nua_set_hparams().
 *
 * @param status 200 when successful, error code otherwise
 * @param phrase a short textual description of @a status code
 * @param nh     NULL when responding to nua_set_params(),
 *               operation handle when responding to nua_set_hparams()
 * @param hmagic NULL when responding to nua_set_params(),
457
 *               application contact associated with the operation handle
458 459 460 461
 *               when responding to nua_set_hparams()
 * @param sip    NULL
 * @param tags   None
 *
462
 * @sa nua_set_params(), nua_set_hparams(),
463 464 465 466 467
 * #nua_r_get_params, nua_get_params(), nua_get_hparams()
 *
 * @END_NUA_EVENT
 */

468 469 470 471
int nua_stack_set_params(nua_t *nua, nua_handle_t *nh, nua_event_t e,
			 tagi_t const *tags)
{
  nua_handle_t *dnh = nua->nua_dhandle;
472

473 474
  int status;
  char const *phrase;
475

476 477 478
  nua_handle_preferences_t tmp[1];
  int any_changes = 0;

479
  enter;
480

481 482 483 484 485 486 487 488 489
  {
    su_home_t tmphome[1] = { SU_HOME_INIT(tmphome) };
    nua_handle_preferences_t *nhp = nh->nh_prefs;
    nua_handle_preferences_t const *dnhp = dnh->nh_prefs;
    nua_global_preferences_t gtmp[1], *ngp = NULL;

    *tmp = *nhp; NHP_UNSET_ALL(tmp);

    /*
490 491
     * Supported features, allowed methods and events are merged
     * with previous or default settings.
492 493 494 495 496 497 498
     *
     * Here we just copy pointers from default settings. However when saving
     * settings we have to be extra careful so that we
     * 1) zero the pointers if the setting has not been modified
     * 2) do not free pointer if the setting has been modified
     * See NHP_ZAP_OVERRIDEN() below for gorier details.
     */
499
    if (!NHP_ISSET(nhp, supported))
500
      tmp->nhp_supported = dnhp->nhp_supported;
501
    if (!NHP_ISSET(nhp, allow))
502
      tmp->nhp_allow = dnhp->nhp_allow;
503
    if (!NHP_ISSET(nhp, allow_events))
504
      tmp->nhp_allow_events = dnhp->nhp_allow_events;
505
    if (!NHP_ISSET(nhp, appl_method))
506
      tmp->nhp_appl_method = dnhp->nhp_appl_method;
507

508 509 510 511
    if (nh == dnh) /* nua_set_params() call, save stack-wide params, too */
      ngp = gtmp, *gtmp = *nua->nua_prefs;

    /* Set and save parameters to tmp */
512
    if (!nh->nh_used_ptags &&
513 514 515 516
	nhp_set_tags(tmphome, tmp, NULL, nh->nh_ptags) < 0)
      status = 900, phrase = "Error storing default handle parameters";
    else if (nhp_set_tags(tmphome, tmp, ngp, tags) < 0)
      status = 900, phrase = "Error storing parameters";
517
    else if ((any_changes = nhp_save_params(nh, tmphome, ngp, tmp)) < 0)
518 519 520
      status = 900, phrase = su_strerror(ENOMEM);
    else
      status = 200, phrase = "OK", nh->nh_used_ptags = 1;
521

522
    su_home_deinit(tmphome);
523 524
  }

525 526 527 528 529 530 531 532 533 534
  if (status == 200) {
    nua_handle_preferences_t const *nhp = nh->nh_prefs;
    nua_handle_preferences_t const *dnhp = dnh->nh_prefs;

    if (!nh->nh_soa && NHP_GET(nhp, dnhp, media_enable)) {
      /* Create soa when needed */
      char const *soa_name = NHP_GET(nhp, dnhp, soa_name);

      if (dnh->nh_soa)
	nh->nh_soa = soa_clone(dnh->nh_soa, nua->nua_root, nh);
535
      else
536
	nh->nh_soa = soa_create(soa_name, nua->nua_root, nh);
537

538 539 540 541 542 543 544 545 546
      if (!nh->nh_soa)
	status = 900, phrase = "Error Creating SOA Object";
      else if (soa_set_params(nh->nh_soa, TAG_NEXT(nh->nh_ptags)) < 0)
	status = 900, phrase = "Error Setting SOA Parameters";
    }
    else if (nh->nh_soa && !NHP_GET(nhp, dnhp, media_enable)) {
      /* ... destroy soa when not needed */
      soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
    }
547

548 549
    if (status == 200 && tags && nh->nh_soa &&
	soa_set_params(nh->nh_soa, TAG_NEXT(tags)) < 0)
550
      status = 900, phrase = "Error Setting SOA Parameters";
551 552
  }

553 554 555 556 557
  if (status == 200 && nh == dnh) {
    /* Set stack-specific things below */
    if (nua_stack_set_smime_params(nua, tags) < 0) {
      status = 900, phrase = "Error setting S/MIME parameters";
    }
558 559
    else if (nua->nua_nta &&
	     nta_agent_set_params(nua->nua_nta, TAG_NEXT(tags)) < 0) {
560 561 562 563
      status = 900, phrase = "Error setting NTA parameters";
    }
    else {
      nua_stack_set_from(nua, 0, tags);
564

565 566 567
      if (nua->nua_prefs->ngp_detect_network_updates)
	nua_stack_launch_network_change_detector(nua);
    }
568 569
  }

570
  if (status != 200) {
571 572 573 574
    if (e == nua_i_none)
      SU_DEBUG_1(("nua_set_params(): failed: %s\n", phrase));
    return UA_EVENT2(e, status, phrase), -1;
  }
575 576 577
  else {
    if (e == nua_r_set_params)
      UA_EVENT2(e, status, phrase);
578 579 580 581 582 583 584 585 586 587 588 589

    if (any_changes) {
      nua_handle_preferences_t changed[1];

      *changed = *nh->nh_prefs;
      memcpy(&changed->nhp_set_, &tmp->nhp_set_, sizeof changed->nhp_set_);

      nua_dialog_update_params(nh->nh_ds,
			       changed,
			       nh->nh_prefs,
			       dnh->nh_prefs);
    }
590 591
    return 0;
  }
592 593
}

594 595

/** Parse parameters from tags to @a nhp or @a ngp.
596 597 598
 *
 * @param home allocate new values from @a home
 * @param nhp  structure to store handle preferences
599
 * @param ngp  structure to store global preferences
600 601
 * @param tags list of tags to parse
 */
602
static int nhp_set_tags(su_home_t *home,
603
			nua_handle_preferences_t *nhp,
604
			nua_global_preferences_t *ngp,
605
			tagi_t const *tags)
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
{

/* Set copy of string to handle pref structure */
#define NHP_SET_STR(nhp, name, v)				 \
  if ((v) != (tag_value_t)0) {					 \
    char const *_value = (char const *)v;			 \
    char *_new = _value ? su_strdup(home, _value) : NULL;	 \
    if (NHP_ISSET(nhp, name))					 \
      su_free(home, (void *)nhp->nhp_##name);			 \
    NHP_SET(nhp, name, _new);					 \
    if (_new == NULL && _value != NULL)				 \
      return -1;						 \
  }

/* Set copy of string from url to handle pref structure */
621
#define NHP_SET_STR_BY_URL(nhp, type, name, v)			 \
622 623
  if ((v) != (tag_value_t)-1) {					 \
    url_t const *_value = (url_t const *)(v);			 \
624
    type *_new = (type *)url_as_string(home, (void *)_value);	 \
625 626 627 628 629 630 631 632
    if (NHP_ISSET(nhp, name))					 \
      su_free(home, (void *)nhp->nhp_##name);			 \
    NHP_SET(nhp, name, _new);					 \
    if (_new == NULL && _value != NULL)				 \
      return -1;						 \
  }

/* Set copy of header to handle pref structure */
633
#define NHP_SET_HEADER(nhp, name, hdr, v)			 \
634
  if ((v) != 0) {						 \
635 636
    sip_##hdr##_t const *_value = (sip_##hdr##_t const *)(v);	 \
    sip_##hdr##_t *_new = NULL;				 \
637 638 639 640 641 642 643 644 645 646
    if (_value != SIP_NONE)					 \
      _new = sip_##name##_dup(home, _value);			 \
    if (NHP_ISSET(nhp, name))					 \
      msg_header_free_all(home, (void *)nhp->nhp_##name);	 \
    NHP_SET(nhp, name, _new);					 \
    if (_new == NULL && _value != SIP_NONE)			 \
      return -1;						 \
  }

/* Set header made of string to handle pref structure */
647
#define NHP_SET_HEADER_STR(nhp, name, hdr, v)			 \
648 649
  if ((v) != 0) {						 \
    char const *_value = (char const *)(v);			 \
650
    sip_##hdr##_t *_new = NULL;				 \
651 652 653 654 655 656 657 658 659
    if (_value != SIP_NONE)					 \
      _new = sip_##name##_make(home, _value);			 \
    if (NHP_ISSET(nhp, name))					 \
      msg_header_free_all(home, (void *)nhp->nhp_##name);	 \
    NHP_SET(nhp, name, _new);					 \
    if (_new == NULL && _value != SIP_NONE)			 \
      return -1;						 \
  }

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
/* Append copy of header to handle pref structure */
#define NHP_APPEND_HEADER(nhp, name, hdr, is_str, next, v)	 \
  {								 \
    sip_##hdr##_t const *_value = (sip_##hdr##_t const *)(v);	 \
    char const *_str = (char const *)(v);			 \
    sip_##hdr##_t *_new = NULL;					 \
    sip_##hdr##_t **_end = &nhp->nhp_##name;			 \
    if (_value != SIP_NONE && _value != NULL) {			 \
      _new = (is_str)						 \
	? sip_##hdr##_make(home, _str)				 \
	: sip_##hdr##_dup(home, _value);			 \
      if (_new == NULL) return -1;				 \
    }								 \
    if (NHP_ISSET(nhp, name))					 \
      while(*_end)						 \
	_end = next(*_end);					 \
    nhp->nhp_set.nhb_##name = 1;				 \
    *_end = _new;						 \
  }

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
/* Set copy of string from header to handle pref structure */
#define NHP_SET_STR_BY_HEADER(nhp, name, v)			 \
  if ((v) != 0) {					 \
    sip_##name##_t const *_value = (sip_##name##_t const *)(v);	 \
    char *_new = NULL;						 \
    if (_value != SIP_NONE)					 \
      _new = sip_header_as_string(home, (void *)_value);	 \
    if (NHP_ISSET(nhp, name))					 \
      su_free(home, (void *)nhp->nhp_##name);			 \
    NHP_SET(nhp, name, _new);					 \
    if (_new == NULL && _value != SIP_NONE)			 \
      return -1;						 \
  }


  tagi_t const *t;
696 697

  for (t = tags; t; t = tl_next(t)) {
698 699 700 701
    tag_type_t tag = t->t_tag;
    tag_value_t value = t->t_value;

    if (tag == NULL)
702 703
      break;
    /* NUTAG_RETRY_COUNT(retry_count) */
704 705
    else if (tag == nutag_retry_count) {
      NHP_SET(nhp, retry_count, (unsigned)value);
706 707
    }
    /* NUTAG_MAX_SUBSCRIPTIONS(max_subscriptions) */
708 709 710 711 712 713 714 715 716 717
    else if (tag == nutag_max_subscriptions) {
      NHP_SET(nhp, max_subscriptions, (unsigned)value);
    }
    /* NUTAG_SOA_NAME(soa_name) */
    else if (tag == nutag_soa_name) {
      NHP_SET_STR(nhp, soa_name, value);
    }
    /* NUTAG_MEDIA_ENABLE(media_enable) */
    else if (tag == nutag_media_enable) {
      NHP_SET(nhp, media_enable, value != 0);
718 719
    }
    /* NUTAG_ENABLEINVITE(invite_enable) */
720 721
    else if (tag == nutag_enableinvite) {
      NHP_SET(nhp, invite_enable, value != 0);
722 723
    }
    /* NUTAG_AUTOALERT(auto_alert) */
724 725
    else if (tag == nutag_autoalert) {
      NHP_SET(nhp, auto_alert, value != 0);
726
    }
727 728 729 730
    /* NUTAG_EARLY_ANSWER(early_answer) */
    else if (tag == nutag_early_answer) {
      NHP_SET(nhp, early_answer, value != 0);
    }
731
    /* NUTAG_EARLY_MEDIA(early_media) */
732 733
    else if (tag == nutag_early_media) {
      NHP_SET(nhp, early_media, value != 0);
734
    }
735
    /* NUTAG_ONLY183_100REL(only183_100rel) */
736 737
    else if (tag == nutag_only183_100rel) {
      NHP_SET(nhp, only183_100rel, value != 0);
738
    }
739
    /* NUTAG_AUTOANSWER(auto_answer) */
740 741
    else if (tag == nutag_autoanswer) {
      NHP_SET(nhp, auto_answer, value != 0);
742 743
    }
    /* NUTAG_AUTOACK(auto_ack) */
744 745
    else if (tag == nutag_autoack) {
      NHP_SET(nhp, auto_ack, value != 0);
746
    }
747 748 749 750 751 752 753 754
    /* NUTAG_AUTO302(auto302) */
    else if (tag == nutag_auto302) {
      NHP_SET(nhp, auto302, value != 0);
    }
    /* NUTAG_AUTO305(auto305) */
    else if (tag == nutag_auto305) {
      NHP_SET(nhp, auto305, value != 0);
    }
755
    /* NUTAG_INVITE_TIMER(invite_timeout) */
756 757
    else if (tag == nutag_invite_timer) {
      NHP_SET(nhp, invite_timeout, (unsigned)value);
758 759
    }
    /* NUTAG_SESSION_TIMER(session_timer) */
760 761
    else if (tag == nutag_session_timer) {
      NHP_SET(nhp, session_timer, (unsigned)value);
762 763
    }
    /* NUTAG_MIN_SE(min_se) */
764 765
    else if (tag == nutag_min_se) {
      NHP_SET(nhp, min_se, (unsigned)value);
766 767
    }
    /* NUTAG_SESSION_REFRESHER(refresher) */
768 769 770 771 772 773 774 775
    else if (tag == nutag_session_refresher) {
      int refresher = value;

      if (refresher >= nua_remote_refresher)
	refresher = nua_remote_refresher;
      else if (refresher <= nua_no_refresher)
	refresher = nua_no_refresher;

Kai Samposalo's avatar
Kai Samposalo committed
776
      NHP_SET(nhp, refresher, (enum nua_session_refresher)refresher);
777 778
    }
    /* NUTAG_UPDATE_REFRESH(update_refresh) */
779 780
    else if (tag == nutag_update_refresh) {
      NHP_SET(nhp, update_refresh, value != 0);
781
    }
782 783 784 785
    /* NUTAG_REFRESH_WITHOUT_SDP(refresh_without_sdp) */
    else if (tag == nutag_refresh_without_sdp) {
      NHP_SET(nhp, refresh_without_sdp, value != 0);
    }
786
    /* NUTAG_ENABLEMESSAGE(message_enable) */
787 788
    else if (tag == nutag_enablemessage) {
      NHP_SET(nhp, message_enable, value != 0);
789 790
    }
    /* NUTAG_ENABLEMESSENGER(win_messenger_enable) */
791 792
    else if (tag == nutag_enablemessenger) {
      NHP_SET(nhp, win_messenger_enable, value != 0);
793 794
    }
    /* NUTAG_CALLEE_CAPS(callee_caps) */
795 796
    else if (tag == nutag_callee_caps) {
      NHP_SET(nhp, callee_caps, value != 0);
797 798
    }
    /* NUTAG_MEDIA_FEATURES(media_features) */
799 800
    else if (tag == nutag_media_features) {
      NHP_SET(nhp, media_features, value != 0);
801 802
    }
    /* NUTAG_SERVICE_ROUTE_ENABLE(service_route_enable) */
803 804
    else if (tag == nutag_service_route_enable) {
      NHP_SET(nhp, service_route_enable, value != 0);
805 806
    }
    /* NUTAG_PATH_ENABLE(path_enable) */
807 808
    else if (tag == nutag_path_enable) {
      NHP_SET(nhp, path_enable, value != 0);
809
    }
810 811 812 813 814
    /* NUTAG_AUTH_CACHE(auth_cache) */
    else if (tag == nutag_auth_cache) {
      if (value >= 0 && value < (tag_value_t)_nua_auth_cache_invalid)
	NHP_SET(nhp, auth_cache, (int)value);
    }
815
    /* NUTAG_REFER_EXPIRES(refer_expires) */
816 817
    else if (tag == nutag_refer_expires) {
      NHP_SET(nhp, refer_expires, value);
818 819
    }
    /* NUTAG_REFER_WITH_ID(refer_with_id) */
820 821
    else if (tag == nutag_refer_with_id) {
      NHP_SET(nhp, refer_with_id, value != 0);
822
    }
823
    /* NUTAG_SUBSTATE(substate) */
824 825
    else if (tag == nutag_substate) {
      NHP_SET(nhp, substate, (int)value);
826
    }
827 828 829 830
    /* NUTAG_SUB_EXPIRES(sub_expires) */
    else if (tag == nutag_sub_expires) {
      NHP_SET(nhp, sub_expires, value);
    }
831
    /* NUTAG_KEEPALIVE(keepalive) */
832 833
    else if (tag == nutag_keepalive) {
      NHP_SET(nhp, keepalive, (unsigned)value);
834 835
    }
    /* NUTAG_KEEPALIVE_STREAM(keepalive_stream) */
836 837
    else if (tag == nutag_keepalive_stream) {
      NHP_SET(nhp, keepalive_stream, (unsigned)value);
838 839
    }

840 841 842 843
    /* NUTAG_SUPPORTED(feature) */
    /* SIPTAG_SUPPORTED_STR(supported_str) */
    /* SIPTAG_SUPPORTED(supported) */
    else if (tag == nutag_supported ||
844
	     tag == siptag_supported ||
845 846 847 848
	     tag == siptag_supported_str) {
      int ok;
      sip_supported_t *supported = NULL;

849
      ok = nhp_merge_lists(home,
850 851 852 853 854 855 856 857 858
			   sip_supported_class, &supported, nhp->nhp_supported,
			   NHP_ISSET(nhp, supported), /* already set by tags */
			   tag == siptag_supported, /* dup it, don't make */
			   tag == nutag_supported, /* merge with old value */
			   t->t_value);
      if (ok < 0)
	return -1;
      else if (ok)
	NHP_SET(nhp, supported, supported);
859
    }
860 861 862 863 864 865 866
    /* NUTAG_ALLOW(allowing) */
    /* SIPTAG_ALLOW_STR(allow_str) */
    /* SIPTAG_ALLOW(allow) */
    else if (tag == nutag_allow ||
	     tag == siptag_allow_str ||
	     tag == siptag_allow) {
      int ok;
867
      msg_list_t *allow = NULL;
868

869
      ok = nhp_merge_lists(home,
870
			   sip_allow_class,
871
			   &allow,
872
			   (msg_list_t const *)nhp->nhp_allow,
873 874 875 876 877 878 879
			   NHP_ISSET(nhp, allow), /* already set by tags */
			   tag == siptag_allow, /* dup it, don't make */
			   tag == nutag_allow, /* merge with old value */
			   t->t_value);
      if (ok < 0)
	return -1;
      else if (ok)
880
	NHP_SET(nhp, allow, (sip_allow_t *)allow);
881
    }
882 883 884 885 886 887 888 889 890
    /* NUTAG_ALLOW_EVENTS(allow_events) */
    /* SIPTAG_ALLOW_EVENTS_STR(allow_events) */
    /* SIPTAG_ALLOW_EVENTS(allow_events) */
    else if (tag == nutag_allow_events ||
	     tag == siptag_allow_events_str ||
	     tag == siptag_allow_events) {
      int ok;
      sip_allow_events_t *allow_events = NULL;

891
      ok = nhp_merge_lists(home,
892
			   sip_allow_events_class,
893
			   &allow_events,
894 895 896 897 898 899 900 901 902 903
			   nhp->nhp_allow_events,
			   NHP_ISSET(nhp, allow_events), /* already set */
			   tag == siptag_allow_events, /* dup it, don't make */
			   tag == nutag_allow_events, /* merge with old value */
			   t->t_value);
      if (ok < 0)
	return -1;
      else if (ok)
	NHP_SET(nhp, allow_events, allow_events);
    }
904 905 906 907 908 909 910 911 912 913 914 915 916
    /* NUTAG_APPL_METHOD(appl_method) */
    else if (tag == nutag_appl_method) {
      if (t->t_value == 0) {
	NHP_SET(nhp, appl_method, NULL);
      }
      else {
	int ok;
	msg_list_t *appl_method = NULL;

	ok = nhp_merge_lists(home,
			     sip_allow_class,
			     &appl_method,
			     (msg_list_t const *)nhp->nhp_appl_method,
917
			     /* already set by tags? */
918
			     NHP_ISSET(nhp, appl_method),
919 920 921 922 923 924 925 926 927
			     0, /* dup it, don't make */
			     1, /* merge with old value */
			     t->t_value);
	if (ok < 0)
	  return -1;
	else if (ok)
	  NHP_SET(nhp, appl_method, (sip_allow_t *)appl_method);
      }
    }
928 929 930 931 932 933 934 935 936
    else if (tag == nutag_initial_route ||
	     tag == nutag_initial_route_str) {
#define next_route(r) (&(r)->r_next)
      NHP_APPEND_HEADER(nhp, initial_route, route,
			(tag == nutag_initial_route_str),
			next_route,
			t->t_value);
      sip_route_fix(nhp->nhp_initial_route);
    }
937 938 939
    /* SIPTAG_USER_AGENT(user_agent) */
    else if (tag == siptag_user_agent) {
      NHP_SET_STR_BY_HEADER(nhp, user_agent, value);
940
    }
941 942 943 944 945
    /* SIPTAG_USER_AGENT_STR(user_agent_str) */
    else if (tag == siptag_user_agent_str && value != 0) {
      if (value == -1)
	value = 0;
      NHP_SET_STR(nhp, user_agent, value);
946
    }
947 948 949
    /* NUTAG_USER_AGENT(ua_name) */
    else if (tag == nutag_user_agent) {
      /* Add contents of NUTAG_USER_AGENT() to our distribution name */
950
      char const *str = (void *)value, *ua;
951

952 953 954 955
      if (str && !already_contains_package_name(str))
	ua = su_sprintf(home, "%s %s", str, NHP_USER_AGENT);
      else if (str)
	ua = su_strdup(home, str);
956 957 958 959
      else
	ua = su_strdup(home, NHP_USER_AGENT);

      NHP_SET(nhp, user_agent, ua);
960
    }
961 962 963
    /* SIPTAG_ORGANIZATION(organization) */
    else if (tag == siptag_organization) {
      NHP_SET_STR_BY_HEADER(nhp, organization, value);
964
    }
965 966 967 968 969
    /* SIPTAG_ORGANIZATION_STR(organization_str) */
    else if (tag == siptag_organization_str) {
      if (value == -1)
	value = 0;
      NHP_SET_STR(nhp, organization, value);
970
    }
971 972
    /* NUTAG_REGISTRAR(registrar) */
    else if (tag == nutag_registrar) {
973
      NHP_SET_STR_BY_URL(nhp, char, registrar, value);
974
      if (NHP_ISSET(nhp, registrar) && su_strmatch(nhp->nhp_registrar, "*"))
975
	NHP_SET_STR(nhp, registrar, 0);
976
    }
977 978 979
    /* NUTAG_INSTANCE(instance) */
    else if (tag == nutag_instance) {
      NHP_SET_STR(nhp, instance, value);
980
    }
981 982 983
    /* NUTAG_M_DISPLAY(m_display) */
    else if (tag == nutag_m_display) {
      NHP_SET_STR(nhp, m_display, value);
984
    }
985 986 987
    /* NUTAG_M_USERNAME(m_username) */
    else if (tag == nutag_m_username) {
      NHP_SET_STR(nhp, m_username, value);
988
    }
989 990 991
    /* NUTAG_M_PARAMS(m_params) */
    else if (tag == nutag_m_params) {
      NHP_SET_STR(nhp, m_params, value);
992
    }
993 994 995
    /* NUTAG_M_FEATURES(m_features) */
    else if (tag == nutag_m_features) {
      NHP_SET_STR(nhp, m_features, value);
996
    }
997 998 999
    /* NUTAG_OUTBOUND(outbound) */
    else if (tag == nutag_outbound) {
      NHP_SET_STR(nhp, outbound, value);
1000
    }
1001 1002 1003 1004
    /* NUTAG_PROXY() (aka NTATAG_DEFAULT_PROXY()) */
    else if (tag == ntatag_default_proxy) {
      NHP_SET_STR_BY_URL(nhp, url_string_t, proxy, value);
    }
1005
    /* NUTAG_DETECT_NETWORK_UPDATES(detect_network_updates) */
1006
    else if (ngp && tag == nutag_detect_network_updates) {
1007 1008 1009 1010 1011 1012 1013
      int detector = (int)value;

      if (detector < NUA_NW_DETECT_NOTHING)
	detector = NUA_NW_DETECT_NOTHING;
      else if (detector > NUA_NW_DETECT_TRY_FULL)
	detector = NUA_NW_DETECT_TRY_FULL;

1014 1015 1016 1017 1018 1019 1020
      ngp->ngp_detect_network_updates = detector;
      ngp->ngp_set.ngp_detect_network_updates = 1;
    }
    /* NUTAG_SHUTDOWN_EVENTS() */
    else if (ngp && tag == nutag_shutdown_events) {
      ngp->ngp_shutdown_events = value != 0;
      ngp->ngp_set.ngp_shutdown_events = 1;
1021
    }
1022 1023
  }

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
  return 0;
}

/** Merge (when needed) new values with old values. */
static int nhp_merge_lists(su_home_t *home,
			   msg_hclass_t *hc,
			   msg_list_t **return_new_list,
			   msg_list_t const *old_list,
			   int already_set,
			   int already_parsed,
			   int always_merge,
			   tag_value_t value)
{
  msg_list_t *list, *elems;

  if (value == -1) {
    *return_new_list = NULL;
    return 1;
1042 1043
  }

1044 1045 1046 1047 1048 1049
  if (value == 0) {
    if (!already_set && !always_merge) {
      *return_new_list = NULL;
      return 1;
    }
    return 0;
1050 1051
  }

1052
  if (already_parsed)
1053 1054 1055
    elems = (void *)msg_header_dup_as(home, hc, (msg_header_t *)value);
  else
    elems = (void *)msg_header_make(home, hc, (char const *)value);
1056

1057 1058 1059 1060
  if (!elems)
    return -1;

  list = (msg_list_t *)old_list;
1061

1062 1063 1064 1065 1066 1067 1068 1069 1070
  if (!already_set) {
    if (always_merge && list) {
      list = (void *)msg_header_dup_as(home, hc, (void *)old_list);
      if (!list)
	return -1;
    }
    else
      list = NULL;
  }
1071

1072 1073 1074
  if (!list) {
    *return_new_list = elems;
    return 1;
1075 1076
  }

1077 1078 1079 1080
  /* Add contents to the new list to the old list */
  if (msg_params_join(home, (msg_param_t **)&list->k_items, elems->k_items,
		      2 /* prune */, 0 /* don't dup */) < 0)
    return -1;
1081 1082

  *return_new_list =
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
    (msg_list_t *)msg_header_dup_as(home, hc, (msg_header_t *)list);
  if (!*return_new_list)
    return -1;

  msg_header_free(home, (msg_header_t *)list);
  msg_header_free(home, (msg_header_t *)elems);

  return 1;
}

1093
/** Save parameters in @a gtmp and @a tmp.
1094 1095 1096 1097
 *
 * @retval 1 - parameters were changed
 * @retval 0 - no changes in parameters
 * @retval -1 - an error
1098
 */
1099
static
1100
int nhp_save_params(nua_handle_t *nh,
1101
		    su_home_t *tmphome,
1102 1103
		    nua_global_preferences_t *gsrc,
		    nua_handle_preferences_t *src)
1104
{
1105 1106 1107 1108
  su_home_t *home = nh->nh_home;
  nua_t *nua = nh->nh_nua;
  nua_handle_t *dnh = nua->nua_dhandle;
  nua_handle_preferences_t *dst = nh->nh_prefs, old[1];
1109

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
  if (gsrc) {
    *nua->nua_prefs = *gsrc;	/* No pointers this far */
  }

  if (!NHP_IS_ANY_SET(src))
    return 0;

  if (nh == dnh || nh->nh_prefs != dnh->nh_prefs) {
    dst = nh->nh_prefs, *old = *dst;
  }
  else {
    dst = su_zalloc(home, sizeof *dst), memset(old, 0, sizeof *old);
    if (!dst)
      return -1;
  }
1125

1126 1127
  /* Move allocations from tmphome to home */
  su_home_move(nh->nh_home, tmphome);
1128

1129
  /* Copy parameters that are set from src to dst */
1130 1131 1132
  nhp_or_set(dst, src);

  /* Handle pointer items. Free changed ones and zap unset ones. */
1133
  /* Note that pointer items where !NHP_ISSET(old, pref) are not freed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
     (because they were just on loan from the default preference set) */
#define NHP_ZAP_OVERRIDEN(old, dst, free, pref)				\
  (((NHP_ISSET(old, pref) &&						\
     (old)->nhp_##pref && (old)->nhp_##pref != (dst)->nhp_##pref)	\
    ? (free)(home, (void *)(old)->nhp_##pref) : (void)0),		\
   (void)(!(dst)->nhp_set.nhb_##pref ? (dst)->nhp_##pref = NULL : NULL))

  NHP_ZAP_OVERRIDEN(old, dst, su_free, soa_name);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, registrar);
  NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, allow);
  NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, supported);
  NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, allow_events);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, user_agent);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, organization);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, m_display);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, m_username);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, m_params);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, m_features);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, instance);
  NHP_ZAP_OVERRIDEN(old, dst, su_free, outbound);
  NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, appl_method);
  NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, initial_route);

  nh->nh_prefs = dst;
1158

1159
  return memcmp(dst, old, sizeof *dst) != 0;
1160 1161 1162 1163 1164 1165
}

static int nua_handle_tags_filter(tagi_t const *f, tagi_t const *t);
static int nua_handle_param_filter(tagi_t const *f, tagi_t const *t);

/** Save taglist to a handle */
1166
int nua_handle_save_tags(nua_handle_t *nh, tagi_t *tags)
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
{
  /* Initialization parameters */
  url_string_t const *url = NULL;
  sip_to_t const *p_to = NULL;
  char const *to_str = NULL;
  sip_from_t const *p_from = NULL;
  char const *from_str = NULL;
  nua_handle_t *identity = NULL;

  tagi_t const *t;

  su_home_t tmphome[SU_HOME_AUTO_SIZE(1024)];

  int error;

1182
#if HAVE_OPEN_C
1183 1184 1185 1186
  /* Nice. An old symbian compiler */
  tagi_t tagfilter[2];
  tagi_t paramfilter[2];

1187 1188 1189 1190 1191 1192 1193 1194 1195
  tagfilter[0].t_tag = tag_filter;
  tagfilter[0].t_value = tag_filter_v(nua_handle_tags_filter);
  tagfilter[1].t_tag = (tag_type_t)0;
  tagfilter[1].t_value = (tag_value_t)0;

  paramfilter[0].t_tag = tag_filter;
  paramfilter[0].t_value = tag_filter_v(nua_handle_param_filter);
  paramfilter[1].t_tag = (tag_type_t)0;
  paramfilter[1].t_value = (tag_value_t)0;
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

#else
  tagi_t const tagfilter[] = {
    { TAG_FILTER(nua_handle_tags_filter) },
    { TAG_NULL() }
  };
  tagi_t const paramfilter[] = {
    { TAG_FILTER(nua_handle_param_filter) },
    { TAG_NULL() }
  };
1206 1207
#endif

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
  for (t = tags; t; t = tl_next(t)) {
    if (t->t_tag == NULL)
      break;
    /* SIPTAG_FROM_REF(p_from) */
    else if (t->t_tag == siptag_from) {
      p_from = (sip_from_t *)t->t_value, from_str = NULL;
    }
    /* SIPTAG_FROM_STR_REF(from_str) */
    else if (t->t_tag == siptag_from_str) {
      from_str = (char const *)t->t_value, p_from = NULL;
    }
    /* SIPTAG_TO_REF(p_to) */
    else if (t->t_tag == siptag_to) {
      p_to = (sip_to_t *)t->t_value, to_str = NULL;
    }
    /* SIPTAG_TO_STR_REF(to_str) */
    else if (t->t_tag == siptag_to_str) {
      to_str = (char const *)t->t_value, p_to = NULL;
    }
    /* NUTAG_IDENTITY_REF(identity) */
    else if (t->t_tag == nutag_identity) {
      identity = (nua_handle_t *)t->t_value;
    }
    /* NUTAG_URL_REF(url) */
    else if (t->t_tag == nutag_url) {
      url = (url_string_t *)t->t_value;
    }
    /* NUTAG_SIPS_URL_REF(url) */
1236
    else if (t->t_tag == nutag_sips_url) {
1237
      url = (url_string_t *)t->t_value;
1238 1239 1240
    }
  }

1241 1242 1243 1244 1245 1246 1247
  su_home_auto(tmphome, sizeof tmphome);

  if (p_from)
    ;
  else if (from_str)
    p_from = sip_from_make(tmphome, from_str);
  else
1248
    p_from = SIP_NONE;
1249 1250 1251 1252 1253

  if (p_to)
    ;
  else if (to_str)
    p_to = sip_to_make(tmphome, to_str);
1254
  else if (url)
1255
    p_to = sip_to_create(tmphome, url),
1256
      p_to ? sip_aor_strip((url_t*)p_to->a_url) : 0;
1257 1258
  else
    p_to = SIP_NONE;
1259

1260 1261 1262 1263 1264
  if (p_to == NULL || p_from == NULL) {
    su_home_deinit(tmphome);
    return -1;
  }

1265
  nh->nh_tags =
1266
    tl_filtered_tlist(nh->nh_home, tagfilter,
1267 1268 1269 1270
		      TAG_IF(p_from != SIP_NONE, SIPTAG_FROM(p_from)),
		      TAG_IF(p_from != SIP_NONE, TAG_FILTER(nua_handle_tags_filter)),
		      TAG_IF(p_to != SIP_NONE, SIPTAG_TO(p_to)),
		      TAG_IF(p_to != SIP_NONE, TAG_FILTER(nua_handle_tags_filter)),
1271 1272
		      TAG_NEXT(tags));

1273
  nh->nh_ptags =
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    tl_filtered_tlist(nh->nh_home, paramfilter, TAG_NEXT(tags));

  error = nh->nh_tags == NULL || nh->nh_ptags == NULL;

  if (!error)
    tl_gets(nh->nh_tags,	/* These does not change while nh lives */
	    SIPTAG_FROM_REF(nh->nh_ds->ds_local),
	    SIPTAG_TO_REF(nh->nh_ds->ds_remote),
	    TAG_END());

  if (nh->nh_ptags && nh->nh_ptags->t_tag == NULL)
    su_free(nh->nh_home, nh->nh_ptags), nh->nh_ptags = NULL;
1286

1287 1288 1289
  if (identity)
    nh->nh_identity = nua_handle_ref(identity);

1290 1291
  su_home_deinit(tmphome);

1292 1293 1294 1295 1296 1297 1298
  return -error;
}

/** Filter tags used for settings. */
static int nua_handle_param_filter(tagi_t const *f, tagi_t const *t)
{
  char const *ns;
1299

1300 1301
  if (!t || !t->t_tag)
    return 0;
1302

1303
  if (t->t_tag == nutag_url ||
1304 1305 1306 1307
      t->t_tag == nutag_sips_url ||
      t->t_tag == nutag_identity)
    return 0;

1308
  ns = t->t_tag->tt_ns;
1309 1310
  if (!ns)
    return 0;
1311

1312
  return strcmp(ns, "nua") == 0 || strcmp(ns, "soa") == 0;
1313
}
1314

1315 1316 1317 1318
/** Filter tags stored permanently as taglist. */
static int nua_handle_tags_filter(tagi_t const *f, tagi_t const *t)
{
  tag_type_t tag;
1319

1320 1321
  if (!t || !t->t_tag)
    return 0;
1322

1323
  tag = t->t_tag;
1324

1325 1326
  if (tag == tag_filter)
    return 0;
1327

1328 1329 1330 1331
  /* Accept @From or @To only when they are followed by
     TAG_FILTER(nua_handle_tags_filter) */
  if (tag == siptag_from || tag == siptag_to) {
    t = tl_next(t);
1332
    return t && t->t_tag == tag_filter &&
1333 1334
      t->t_value == (tag_value_t)nua_handle_tags_filter;
  }
1335

1336 1337 1338 1339 1340 1341
  if (tag == nutag_identity)
    return 0;
  if (tag == siptag_from_str)
    return 0;
  if (tag == siptag_to_str)
    return 0;
1342

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
  /** Ignore @CSeq, @RSeq, @RAck, @Timestamp, and @ContentLength */
  if (tag == siptag_cseq || tag == siptag_cseq_str)
    return 0;
  if (tag == siptag_rseq || tag == siptag_rseq_str)
    return 0;
  if (tag == siptag_rack || tag == siptag_rack_str)
    return 0;
  if (tag == siptag_timestamp || tag == siptag_timestamp_str)
    return 0;
  if (tag == siptag_content_length || tag == siptag_content_length_str)
    return 0;
1354

1355
  return ! nua_handle_param_filter(f, t);
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
}

static
int nua_stack_set_smime_params(nua_t *nua, tagi_t const *tags)
{
#if HAVE_SOFIA_SMIME
  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;

  int n;

  n = tl_gets(tags,
	      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),
	      TAG_NULL());
  if (n <= 0)
    return n;

  /* XXX - all other S/MIME parameters? */
1387
  return sm_set_params(nua->sm, smime_enable, smime_opt,
1388 1389 1390 1391 1392 1393 1394 1395
		       smime_protection_mode, smime_path);
#endif

  return 0;
}

/**@fn void nua_get_params(nua_t *nua, tag_type_t tag, tag_value_t value, ...)
 *
1396 1397
 * Get NUA parameters matching with the given filter.
 * The values of NUA parameters is returned in #nua_r_get_params event.
1398 1399 1400 1401 1402 1403 1404 1405
 *
 * @param nua             Pointer to NUA stack object
 * @param tag, value, ... List of tagged parameters
 *
 * @return
 *     nothing
 *
 * @par Related tags:
1406 1407
 *     TAG_ANY() \n
 *     otherwise same tags as nua_set_params()
1408 1409 1410
 *
 * @par Events:
 *     #nua_r_get_params
1411 1412 1413 1414 1415 1416
 *
 * @par Examples
 * Find out default values of all parameters:
 * @code
 *    nua_get_params(nua, TAG_ANY(), TAG_END());
 * @endcode
1417 1418 1419
 */

/**@fn void nua_get_hparams(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...)
1420
 *
1421
 * Get values of handle-specific parameters in #nua_r_get_params event.
1422 1423 1424
 *
 * Application will specify either expilicit list of tags it is interested
 * in, or a filter (at the moment, TAG_ANY()). The values are returned as a
1425
 * list of tags in the #nua_r_get_params event.
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436