nua_stack.c 54.1 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
/*
 * 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
26
 * @brief Sofia-SIP User Agent Engine implementation
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Kai Vehmanen <Kai.Vehmanen@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
30
 * @author Martti Mela <Martti Mela@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
31 32 33 34 35 36 37 38
 * @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"

39 40 41
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tagarg.h>
42
#include <sofia-sip/su_strlst.h>
43
#include <sofia-sip/su_uniqueid.h>
Pekka Pessi's avatar
Pekka Pessi committed
44

45
#include <sofia-sip/su_tag_io.h>
Pekka Pessi's avatar
Pekka Pessi committed
46 47 48 49

#define SU_ROOT_MAGIC_T   struct nua_s
#define SU_MSG_ARG_T      struct event_s

50 51
#define NUA_SAVED_EVENT_T su_msg_t *

Pekka Pessi's avatar
Pekka Pessi committed
52 53 54 55 56
#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

57 58 59 60
#include <sofia-sip/sip.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sip_util.h>
Pekka Pessi's avatar
Pekka Pessi committed
61

62
#include <sofia-sip/tport_tag.h>
63
#include <sofia-sip/nta.h>
64
#include <sofia-sip/nta_tport.h>
65
#include <sofia-sip/auth_client.h>
Pekka Pessi's avatar
Pekka Pessi committed
66

67
#include <sofia-sip/soa.h>
Pekka Pessi's avatar
Pekka Pessi committed
68

69 70
#include "sofia-sip/nua.h"
#include "sofia-sip/nua_tag.h"
Pekka Pessi's avatar
Pekka Pessi committed
71 72
#include "nua_stack.h"

73 74 75 76 77
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
78

79
#include <assert.h>
80

Pekka Pessi's avatar
Pekka Pessi committed
81 82 83 84 85 86 87 88 89 90
/* ========================================================================
 *
 *                       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);

91
static int nh_authorize(nua_handle_t *nh,
Pekka Pessi's avatar
Pekka Pessi committed
92 93 94 95
			tag_type_t tag, tag_value_t value, ...);

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

96
static void nua_stack_timer(nua_t *nua, su_timer_t *t, su_timer_arg_t *a);
Pekka Pessi's avatar
Pekka Pessi committed
97

98 99
/* ---------------------------------------------------------------------- */
/* Constant data */
Pekka Pessi's avatar
Pekka Pessi committed
100

101
/**@internal Default internal error. */
102
char const nua_internal_error[] = "Internal NUA Error";
Pekka Pessi's avatar
Pekka Pessi committed
103

104 105 106 107
char const nua_application_sdp[] = "application/sdp";

#define NUA_STACK_TIMER_INTERVAL (1000)

Pekka Pessi's avatar
Pekka Pessi committed
108 109 110 111
/* ----------------------------------------------------------------------
 * Initialization & deinitialization
 */

112
int nua_stack_init(su_root_t *root, nua_t *nua)
Pekka Pessi's avatar
Pekka Pessi committed
113 114
{
  su_home_t *home;
115
  nua_handle_t *dnh;
116

Pekka Pessi's avatar
Pekka Pessi committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  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;
  }

135
  nua->nua_root = root;
136 137 138 139
  nua->nua_timer = su_timer_create(su_root_task(root),
				   NUA_STACK_TIMER_INTERVAL);
  if (!nua->nua_timer)
    return -1;
140

Pekka Pessi's avatar
Pekka Pessi committed
141
  home = nua->nua_home;
142 143
  nua->nua_handles_tail = &nua->nua_handles;
  sip_from_init(nua->nua_from);
Pekka Pessi's avatar
Pekka Pessi committed
144

145
  dnh = su_home_clone(nua->nua_home, sizeof (*dnh) + sizeof(*dnh->nh_prefs));
146 147
  if (!dnh)
    return -1;
148

149
  dnh->nh_prefs = (void *)(dnh + 1);
150 151
  dnh->nh_valid = nua_handle;
  dnh->nh_nua = nua;
152 153 154
  nua_handle_ref(dnh); dnh->nh_ref_by_stack = 1; 
  nua_handle_ref(dnh); dnh->nh_ref_by_user = 1;
  nh_append(nua, dnh);
155
  dnh->nh_identity = dnh;
156
  dnh->nh_ds->ds_local = nua->nua_from;
157
  dnh->nh_ds->ds_remote = nua->nua_from;
Pekka Pessi's avatar
Pekka Pessi committed
158

159 160 161
  if (nua_stack_set_defaults(dnh, dnh->nh_prefs) < 0)
    return -1;

162
  if (nua_stack_set_params(nua, dnh, nua_i_none, nua->nua_args) < 0)
163 164 165 166
    return -1;

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

167
  nua->nua_nta = nta_agent_create(root, NONE, NULL, NULL,
168 169 170 171 172 173
				  NTATAG_MERGE_482(1),
				  NTATAG_CLIENT_RPORT(1),
				  NTATAG_UA(1),
#if HAVE_SOFIA_SMIME
				  NTATAG_SMIME(nua->sm),
#endif
Pekka Pessi's avatar
Pekka Pessi committed
174
				  TPTAG_STUN_SERVER(1),
Pekka Pessi's avatar
Pekka Pessi committed
175
				  TAG_NEXT(nua->nua_args));
Pekka Pessi's avatar
Pekka Pessi committed
176

Pekka Pessi's avatar
Pekka Pessi committed
177 178 179 180 181 182 183 184 185 186
  dnh->nh_ds->ds_leg = nta_leg_tcreate(nua->nua_nta,
				       nua_stack_process_request, dnh,
				       NTATAG_NO_DIALOG(1),
				       TAG_END());

  if (nua->nua_nta == NULL ||
      dnh->nh_ds->ds_leg == NULL || 
      nta_agent_set_params(nua->nua_nta, NTATAG_UA(1), TAG_END()) < 0 ||
      nua_stack_init_transport(nua, nua->nua_args) < 0) {
    SU_DEBUG_1(("nua: initializing SIP stack failed\n"));
187
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
188
  }
Pekka Pessi's avatar
Pekka Pessi committed
189 190

  if (nua_stack_set_from(nua, 1, nua->nua_args) < 0)
191
    return -1;
192

193 194
  if (NHP_ISSET(dnh->nh_prefs, detect_network_updates))
    nua_stack_launch_network_change_detector(nua);
Pekka Pessi's avatar
Pekka Pessi committed
195

196
  nua_stack_timer(nua, nua->nua_timer, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
197

Pekka Pessi's avatar
Pekka Pessi committed
198 199 200
  return 0;
}

201
void nua_stack_deinit(su_root_t *root, nua_t *nua)
Pekka Pessi's avatar
Pekka Pessi committed
202 203 204 205 206 207 208 209 210 211 212
{
  enter;

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

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

213
static void nua_stack_shutdown(nua_t *);
214

215 216 217 218
void
  nua_stack_authenticate(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  nua_stack_respond(nua_t *, nua_handle_t *, int , char const *, tagi_t const *),
  nua_stack_destroy_handle(nua_t *, nua_handle_t *, tagi_t const *);
Pekka Pessi's avatar
Pekka Pessi committed
219

220 221 222 223 224
/* Notifier */
void
  nua_stack_authorize(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  nua_stack_notifier(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *),
  nua_stack_terminate(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *);
Pekka Pessi's avatar
Pekka Pessi committed
225

226 227
int nh_notifier_shutdown(nua_handle_t *nh, nea_event_t *ev,
			 tag_type_t t, tag_value_t v, ...);
Pekka Pessi's avatar
Pekka Pessi committed
228

Pekka Pessi's avatar
Pekka Pessi committed
229
/** @internal Send an event to the application. */
230 231 232
int nua_stack_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, ...)
Pekka Pessi's avatar
Pekka Pessi committed
233 234 235 236
{
  su_msg_r sumsg = SU_MSG_R_INIT;

  ta_list ta;
Pekka Pessi's avatar
Pekka Pessi committed
237
  size_t e_len, len, xtra, p_len;
Pekka Pessi's avatar
Pekka Pessi committed
238

239
  if (event == nua_r_ack || event == nua_i_none)
240 241
    return event;

Pekka Pessi's avatar
Pekka Pessi committed
242 243 244 245 246 247 248 249 250 251 252 253 254
  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) {
255
    if (msg)
Pekka Pessi's avatar
Pekka Pessi committed
256 257 258 259 260 261 262
      msg_destroy(msg);
    if (status >= 200) {
      nh_destroy(nua, nh);
    }
    return event;
  }

263 264
  if ((event > nua_r_authenticate && event <= nua_r_ack)
      || event < nua_i_error
265 266
      || (nh && !nh->nh_valid)
      || (nua->nua_shutdown && event != nua_r_shutdown)) {
267
    if (msg)
Pekka Pessi's avatar
Pekka Pessi committed
268 269 270 271 272 273 274 275 276 277 278
      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;

279
  if (su_msg_create(sumsg, nua->nua_client, su_task_null,
Pekka Pessi's avatar
Pekka Pessi committed
280 281 282 283 284 285 286 287 288 289
		    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;
290
    e->e_nh = nh ? nua_handle_ref(nh) : nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
291 292
    e->e_status = status;
    e->e_phrase = strcpy(end, phrase ? phrase : "");
293 294
    if (msg)
      e->e_msg = msg, su_home_threadsafe(msg_home(msg));
Pekka Pessi's avatar
Pekka Pessi committed
295 296

    if (su_msg_send(sumsg) != 0)
297
      nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
298 299 300 301 302 303 304 305
  }

  ta_end(ta);

  return event;
}

/* ----------------------------------------------------------------------
306
 * Post signal to stack itself
Pekka Pessi's avatar
Pekka Pessi committed
307
 */
308
void nua_stack_post_signal(nua_handle_t *nh, nua_event_t event,
309
			   tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
310 311 312
{
  ta_list ta;
  ta_start(ta, tag, value);
313
  nua_signal((nh)->nh_nua, nh, NULL, 1, event, 0, NULL, ta_tags(ta));
Pekka Pessi's avatar
Pekka Pessi committed
314 315 316 317 318 319 320
  ta_end(ta);
}


/* ----------------------------------------------------------------------
 * Receiving events from client
 */
321
void nua_stack_signal(nua_t *nua, su_msg_r msg, nua_event_data_t *e)
Pekka Pessi's avatar
Pekka Pessi committed
322 323 324 325
{
  nua_handle_t *nh = e->e_nh;
  tagi_t *tags = e->e_tags;

326 327
  assert(tags);

Pekka Pessi's avatar
Pekka Pessi committed
328 329 330 331
  if (nh) {
    if (!nh->nh_prev)
      nh_append(nua, nh);
    if (!nh->nh_ref_by_stack) {
332
      /* Mark handle as used by stack */
Pekka Pessi's avatar
Pekka Pessi committed
333
      nh->nh_ref_by_stack = 1;
334
      nua_handle_ref(nh);
335
    }
Pekka Pessi's avatar
Pekka Pessi committed
336 337 338 339 340 341 342
  }

  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
343
      SU_DEBUG_5(("nua(%p): signal %s %u %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
344 345 346
		  nh, name + 4, e->e_status, e->e_phrase ? e->e_phrase : ""));
  }

347 348
  su_msg_save(nua->nua_signal, msg);

Pekka Pessi's avatar
Pekka Pessi committed
349 350
  if (nua->nua_shutdown && !e->e_always) {
    /* Shutting down */
351 352 353
    nua_stack_event(nua, nh, NULL, e->e_event,
		    901, "Stack is going down",
		    TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
354
  }
355

Pekka Pessi's avatar
Pekka Pessi committed
356 357
  else switch (e->e_event) {
  case nua_r_get_params:
358
    nua_stack_get_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
359 360
    break;
  case nua_r_set_params:
361
    nua_stack_set_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
362 363
    break;
  case nua_r_shutdown:
364
    nua_stack_shutdown(nua);
Pekka Pessi's avatar
Pekka Pessi committed
365 366 367
    break;
  case nua_r_register:
  case nua_r_unregister:
368
    nua_stack_register(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
369 370
    break;
  case nua_r_invite:
371
    nua_stack_invite(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
372 373
    break;
  case nua_r_cancel:
374
    nua_stack_cancel(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
375 376
    break;
  case nua_r_bye:
377
    nua_stack_bye(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
378 379
    break;
  case nua_r_options:
380
    nua_stack_options(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
381 382
    break;
  case nua_r_refer:
383
    nua_stack_refer(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
384 385
    break;
  case nua_r_publish:
Pekka Pessi's avatar
Pekka Pessi committed
386
  case nua_r_unpublish:
387
    nua_stack_publish(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
388 389
    break;
  case nua_r_info:
390
    nua_stack_info(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
391 392
    break;
  case nua_r_update:
393
    nua_stack_update(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
394 395
    break;
  case nua_r_message:
396
    nua_stack_message(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
397 398 399
    break;
  case nua_r_subscribe:
  case nua_r_unsubscribe:
400
    nua_stack_subscribe(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
401 402
    break;
  case nua_r_notify:
403
    nua_stack_notify(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
404 405
    break;
  case nua_r_notifier:
406
    nua_stack_notifier(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
407 408
    break;
  case nua_r_terminate:
409
    nua_stack_terminate(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
410 411
    break;
  case nua_r_method:
412
    nua_stack_method(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
413 414
    break;
  case nua_r_authenticate:
415
    nua_stack_authenticate(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
416
    break;
417
  case nua_r_authorize:
418
    nua_stack_authorize(nua, nh, e->e_event, tags);
419
    break;
Pekka Pessi's avatar
Pekka Pessi committed
420
  case nua_r_ack:
421
    nua_stack_ack(nua, nh, e->e_event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
422 423
    break;
  case nua_r_respond:
424
    nua_stack_respond(nua, nh, e->e_status, e->e_phrase, tags);
Pekka Pessi's avatar
Pekka Pessi committed
425 426
    break;
  case nua_r_destroy:
427
    nua_stack_destroy_handle(nua, nh, tags);
Pekka Pessi's avatar
Pekka Pessi committed
428 429 430 431 432
    break;
  default:
    break;
  }

433 434 435
  if (su_msg_is_non_null(nua->nua_signal))
    su_msg_destroy(nua->nua_signal);

436
  if (nh != nua->nua_dhandle)
437
    nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
438 439 440 441 442 443
}

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

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

Pekka Pessi's avatar
Pekka Pessi committed
444 445
/**@internal
 * Timer routine.
Pekka Pessi's avatar
Pekka Pessi committed
446
 *
447
 * Go through all active handles and execute pending tasks
Pekka Pessi's avatar
Pekka Pessi committed
448
 */
449
void nua_stack_timer(nua_t *nua, su_timer_t *t, su_timer_arg_t *a)
Pekka Pessi's avatar
Pekka Pessi committed
450 451 452
{
  nua_handle_t *nh, *nh_next;
  sip_time_t now = sip_now();
453
  su_root_t *root = su_timer_root(t);
Pekka Pessi's avatar
Pekka Pessi committed
454

455
  su_timer_set(t, nua_stack_timer, a);
Pekka Pessi's avatar
Pekka Pessi committed
456 457

  if (nua->nua_shutdown) {
458
    nua_stack_shutdown(nua);
Pekka Pessi's avatar
Pekka Pessi committed
459 460 461 462 463 464
    return;
  }

  for (nh = nua->nua_handles; nh; nh = nh_next) {
    nh_next = nh->nh_next;
    nh_call_pending(nh, now);
465
    su_root_yield(root);	/* Handle received packets */
Pekka Pessi's avatar
Pekka Pessi committed
466 467 468 469 470 471 472
  }
}


static
int nh_call_pending(nua_handle_t *nh, sip_time_t now)
{
473
  nua_dialog_state_t *ds = nh->nh_ds;
Pekka Pessi's avatar
Pekka Pessi committed
474
  nua_dialog_usage_t *du;
Pekka Pessi's avatar
Pekka Pessi committed
475
  sip_time_t next = now + NUA_STACK_TIMER_INTERVAL / 1000;
Pekka Pessi's avatar
Pekka Pessi committed
476

477
  for (du = ds->ds_usage; du; du = du->du_next) {
478 479 480
    if (now == 0)
      break;
    if (du->du_refresh && du->du_refresh < next)
Pekka Pessi's avatar
Pekka Pessi committed
481 482 483 484 485 486
      break;
  }

  if (du == NULL)
    return 0;

487
  nua_handle_ref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
488 489 490 491

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

492
    nua_dialog_usage_refresh(nh, ds, du, now);
Pekka Pessi's avatar
Pekka Pessi committed
493 494 495 496 497 498 499 500 501

    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) {
502 503 504
      if (now == 0)
	break;
      if (du->du_refresh && du->du_refresh < next)
Pekka Pessi's avatar
Pekka Pessi committed
505 506 507 508
	break;
    }
  }

509
  nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
510 511 512 513 514 515 516

  return 1;
}


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

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
/**Shutdown a @nua stack.
 *
 * When the @nua stack is shutdown, ongoing calls are released,
 * registrations unregistered, publications un-PUBLISHed and subscriptions
 * terminated. If the stack cannot terminate everything within 30 seconds,
 * it sends the #nua_r_shutdown event with status 500.
 *
 * @param nua         Pointer to @nua stack object
 *
 * @return
 *     nothing
 *
 * @par Related tags:
 *     none
 *
 * @par Events:
 *     #nua_r_shutdown
 *
 * @sa #nua_r_shutdown, nua_destroy(), nua_create(), nua_bye(),
 * nua_unregister(), nua_unpublish(), nua_unsubscribe(), nua_notify(),
 * nua_handle_destroy(), nua_handle_unref()
 */

/** @NUA_EVENT nua_r_shutdown
 *
 * Answer to nua_shutdown().
 *
 * Status codes
 * - 100 shutdown started
 * - 101 shutdown in progress (sent when shutdown has been progressed)
 * - 200 shutdown was successful
 * - 500 shutdown timeout after 30 sec
 *
 * @param status shutdown status code
 * @param nh     NULL
 * @param hmagic NULL
 * @param sip    NULL
 * @param tags   empty
 *
 * @sa nua_shutdown(), nua_destroy()
 *
 * @END_NUA_EVENT
 */

Pekka Pessi's avatar
Pekka Pessi committed
561
/** @internal Shut down stack. */
562
void nua_stack_shutdown(nua_t *nua)
Pekka Pessi's avatar
Pekka Pessi committed
563 564 565 566 567 568 569 570 571 572 573 574 575
{
  nua_handle_t *nh, *nh_next;
  int busy = 0;
  sip_time_t now = sip_now();
  int status;
  char const *phrase;

  enter;

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

  for (nh = nua->nua_handles; nh; nh = nh_next) {
576
    nua_dialog_state_t *ds = nh->nh_ds;
577 578
    nua_server_request_t *sr, *sr_next;

Pekka Pessi's avatar
Pekka Pessi committed
579 580
    nh_next = nh->nh_next;

581
    for (sr = ds->ds_sr; sr; sr = sr_next) {
582
      sr_next = sr->sr_next;
Pekka Pessi's avatar
Pekka Pessi committed
583

584 585 586 587 588 589 590 591 592
      if (sr->sr_respond) {
	SR_STATUS1(sr, SIP_410_GONE);
	sr->sr_usage = NULL;
	sr->sr_respond(sr, NULL);
	sr->sr_respond = NULL;
	busy++;
      }
	
      nua_server_request_destroy(sr);
Pekka Pessi's avatar
Pekka Pessi committed
593 594 595 596
    }

    busy += nh_call_pending(nh, 0);

Pekka Pessi's avatar
Pekka Pessi committed
597 598
    if (nh->nh_soa) {
      soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
599 600
    }

601
    if (nua_client_request_pending(ds->ds_cr))
Pekka Pessi's avatar
Pekka Pessi committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
      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");

617
  if (status >= 200) {
618 619 620 621 622 623
    for (nh = nua->nua_handles; nh; nh = nh_next) {
      nh_next = nh->nh_next;
      while (nh->nh_ds && nh->nh_ds->ds_usage) {
	nua_dialog_usage_remove(nh, nh->nh_ds, nh->nh_ds->ds_usage);
      }
    }
624 625 626 627
    su_timer_destroy(nua->nua_timer), nua->nua_timer = NULL;
    nta_agent_destroy(nua->nua_nta), nua->nua_nta = NULL;
  }

628
  nua_stack_event(nua, NULL, NULL, nua_r_shutdown, status, phrase, TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
629 630 631 632
}

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

Pekka Pessi's avatar
Pekka Pessi committed
633
/** @internal Create a handle */
Pekka Pessi's avatar
Pekka Pessi committed
634 635 636 637 638 639 640 641 642 643
nua_handle_t *nh_create(nua_t *nua, tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  nua_handle_t *nh;

  enter;

  ta_start(ta, tag, value);
  nh = nh_create_handle(nua, NULL, ta_args(ta));
  ta_end(ta);
644

Pekka Pessi's avatar
Pekka Pessi committed
645 646 647 648 649 650 651 652
  if (nh) {
    nh->nh_ref_by_stack = 1;
    nh_append(nua, nh);
  }

  return nh;
}

Pekka Pessi's avatar
Pekka Pessi committed
653
/** @internal Append an handle to the list of handles */
Pekka Pessi's avatar
Pekka Pessi committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
void nh_append(nua_t *nua, nua_handle_t *nh)
{
  nh->nh_next = NULL;
  nh->nh_prev = nua->nua_handles_tail;
  *nua->nua_handles_tail = nh;
  nua->nua_handles_tail = &nh->nh_next;
}

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

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

  return NULL;
}

674
void nua_stack_destroy_handle(nua_t *nua, nua_handle_t *nh, tagi_t const *tags)
675 676 677 678
{
  nh_call_pending(nh, 0);	/* Call pending operations with 0 */

  if (nh->nh_notifier)
679
    nua_stack_terminate(nua, nh, 0, NULL);
680 681 682 683 684 685 686 687 688 689 690

#if 0
  if (nh->nh_ref_by_user) {
    nh->nh_ref_by_user = 0;
    nua_handle_unref(nh);
  }
#endif

  nh_destroy(nua, nh);
}

Pekka Pessi's avatar
Pekka Pessi committed
691 692
#define nh_is_inserted(nh) ((nh)->nh_prev != NULL)

Pekka Pessi's avatar
Pekka Pessi committed
693
/** @internal Remove a handle from list of handles */
Pekka Pessi's avatar
Pekka Pessi committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
static
void nh_remove(nua_t *nua, nua_handle_t *nh)
{
  assert(nh_is_inserted(nh)); assert(*nh->nh_prev == nh);

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

  *nh->nh_prev = nh->nh_next;

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

710

Pekka Pessi's avatar
Pekka Pessi committed
711 712
void nh_destroy(nua_t *nua, nua_handle_t *nh)
{
713
  assert(nh); assert(nh != nua->nua_dhandle);
Pekka Pessi's avatar
Pekka Pessi committed
714 715 716 717 718 719

  nh_enter;

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

720
  nua_creq_deinit(nh->nh_ds->ds_cr, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
721

722
  nua_dialog_deinit(nh, nh->nh_ds);
Pekka Pessi's avatar
Pekka Pessi committed
723

Pekka Pessi's avatar
Pekka Pessi committed
724 725
  if (nh->nh_soa)
    soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
726 727 728 729

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

730
  nua_handle_unref(nh);		/* Remove stack reference */
Pekka Pessi's avatar
Pekka Pessi committed
731 732 733 734
}

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

Pekka Pessi's avatar
Pekka Pessi committed
735
/**@internal
736
 * Initialize handle Allow and authentication info, save parameters.
737 738 739 740
 *
 * @retval -1 upon an error
 * @retval 0 when successful
 */
741
int nua_stack_init_handle(nua_t *nua, nua_handle_t *nh,
742
			  tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
743 744
{
  ta_list ta;
745
  int retval = 0;
Pekka Pessi's avatar
Pekka Pessi committed
746

747 748 749
  if (nh == NULL)
    return -1;

750
  assert(nh != nua->nua_dhandle);
Pekka Pessi's avatar
Pekka Pessi committed
751 752 753

  ta_start(ta, tag, value);

754
  if (nua_stack_set_params(nua, nh, nua_i_error, ta_args(ta)) < 0)
755
    retval = -1;
756

757 758 759
  if (!retval && nh->nh_soa)
    if (soa_set_params(nh->nh_soa, ta_tags(ta)) < 0)
      retval = -1;
760 761 762

  ta_end(ta);

763 764
  if (retval || nh->nh_init) /* Already initialized? */
    return retval;
Pekka Pessi's avatar
Pekka Pessi committed
765 766 767 768 769

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

  nh->nh_init = 1;
770 771

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
772 773
}

Pekka Pessi's avatar
Pekka Pessi committed
774
/** @internal Create a handle for processing incoming request */
775
nua_handle_t *nua_stack_incoming_handle(nua_t *nua,
776 777 778
					nta_incoming_t *irq,
					sip_t const *sip,
					int create_dialog)
Pekka Pessi's avatar
Pekka Pessi committed
779 780 781 782 783 784 785 786 787 788 789 790 791
{
  nua_handle_t *nh;
  url_t const *url;
  sip_to_t to[1];
  sip_from_t from[1];

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

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

792
  /* Strip away parameters */
Pekka Pessi's avatar
Pekka Pessi committed
793 794 795 796 797 798 799
  sip_from_init(from)->a_display = sip->sip_to->a_display;
  *from->a_url = *sip->sip_to->a_url;

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

  nh = nh_create(nua,
800 801 802
		 NUTAG_URL((url_string_t *)url), /* Remote target */
		 SIPTAG_TO(to), /* Local AoR */
		 SIPTAG_FROM(from), /* Remote AoR */
Pekka Pessi's avatar
Pekka Pessi committed
803 804
		 TAG_END());

805
  if (nua_stack_init_handle(nh->nh_nua, nh, TAG_END()) < 0)
806
    nh_destroy(nua, nh), nh = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
807

808 809
  if (nh && create_dialog) {
    struct nua_dialog_state *ds = nh->nh_ds;
Pekka Pessi's avatar
Pekka Pessi committed
810

811
    nua_dialog_store_peer_info(nh, ds, sip);
Pekka Pessi's avatar
Pekka Pessi committed
812

813
    ds->ds_leg = nta_leg_tcreate(nua->nua_nta, nua_stack_process_request, nh,
814 815 816 817 818
				 SIPTAG_CALL_ID(sip->sip_call_id),
				 SIPTAG_FROM(sip->sip_to),
				 SIPTAG_TO(sip->sip_from),
				 NTATAG_REMOTE_CSEQ(sip->sip_cseq->cs_seq),
				 TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
819

820
    if (!ds->ds_leg || !nta_leg_tag(ds->ds_leg, nta_incoming_tag(irq, NULL)))
821
      nh_destroy(nua, nh), nh = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
822 823 824
  }

  if (nh)
825
    nua_dialog_uas_route(nh, nh->nh_ds, sip, 1);
Pekka Pessi's avatar
Pekka Pessi committed
826 827 828 829 830

  return nh;
}


831 832 833 834 835
/** Set flags and special event on handle.
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
int nua_stack_set_handle_special(nua_handle_t *nh,
				 enum nh_kind kind,
				 nua_event_t special)
{
  if (nh == NULL)
    return -1;

  if (special && nh->nh_special && nh->nh_special != special)
    return -1;

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

    if (special)
      nh->nh_special = special;
  }

  return 0;
}

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
sip_replaces_t *nua_stack_handle_make_replaces(nua_handle_t *nh, 
					       su_home_t *home,
					       int early_only)
{
  if (nh && nh->nh_ds && nh->nh_ds->ds_leg)
    return nta_leg_make_replaces(nh->nh_ds->ds_leg, home, early_only);
  else
    return NULL;
}

nua_handle_t *nua_stack_handle_by_replaces(nua_t *nua,
					   sip_replaces_t const *r)
{
  if (nua) {
    nta_leg_t *leg = nta_leg_by_replaces(nua->nua_nta, r);
    if (leg)
      return nta_leg_magic(leg, nua_stack_process_request);
  }
  return NULL;
}


Pekka Pessi's avatar
Pekka Pessi committed
886
/** @internal Add authorization data */
Pekka Pessi's avatar
Pekka Pessi committed
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
int nh_authorize(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...)
{
  int retval = 0;
  tagi_t const *ti;
  ta_list ta;

  ta_start(ta, tag, value);

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

      if (rv > 0) {
	retval = 1;
902
      }
Pekka Pessi's avatar
Pekka Pessi committed
903
      else if (rv < 0) {
904
	retval = -1;
Pekka Pessi's avatar
Pekka Pessi committed
905 906 907 908 909 910 911 912 913 914
	break;
      }
    }
  }

  ta_end(ta);

  return retval;
}

Pekka Pessi's avatar
Pekka Pessi committed
915 916
/**@internal
 * Collect challenges from response.
Pekka Pessi's avatar
Pekka Pessi committed
917 918 919 920
 *
 * @return Number of updated challenges, 0 if no updates found.
 * @retval -1 upon error.
 */
Pekka Pessi's avatar
Pekka Pessi committed
921 922 923
static
int nh_challenge(nua_handle_t *nh, sip_t const *sip)
{
Pekka Pessi's avatar
Pekka Pessi committed
924 925
  int server = 0, proxy = 0;

Pekka Pessi's avatar
Pekka Pessi committed
926
  if (sip->sip_www_authenticate)
927
    server = auc_challenge(&nh->nh_auth, nh->nh_home,
928
			   sip->sip_www_authenticate,
Pekka Pessi's avatar
Pekka Pessi committed
929
			   sip_authorization_class);
Pekka Pessi's avatar
Pekka Pessi committed
930 931

  if (sip->sip_proxy_authenticate)
932
    proxy = auc_challenge(&nh->nh_auth, nh->nh_home,
933
			  sip->sip_proxy_authenticate,
Pekka Pessi's avatar
Pekka Pessi committed
934
			  sip_proxy_authorization_class);
Pekka Pessi's avatar
Pekka Pessi committed
935

Pekka Pessi's avatar
Pekka Pessi committed
936 937 938 939
  if (server < 0 || proxy < 0)
    return -1;

  return server + proxy;
Pekka Pessi's avatar
Pekka Pessi committed
940 941
}

942 943 944 945 946 947 948 949 950 951 952 953
#include <sofia-sip/su_tag_inline.h>

/** Check if tag list has contact */
int nua_tagis_have_contact_tag(tagi_t const *t)
{
  for (; t && t->t_tag; t = t_next(t))
    if (t->t_tag == siptag_contact ||
	t->t_tag == siptag_contact_str)
      return 1;
  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
954 955
/**@internal
 * Create a request message.
Pekka Pessi's avatar
Pekka Pessi committed
956
 *
957
 * @param nua
Pekka Pessi's avatar
Pekka Pessi committed
958
 * @param nh
Pekka Pessi's avatar
Pekka Pessi committed
959 960
 * @param cr
 * @param restart
Pekka Pessi's avatar
Pekka Pessi committed
961 962
 * @param method
 * @param name
Pekka Pessi's avatar
Pekka Pessi committed
963
 * @param tag, value, ... list of tag-value pairs
Pekka Pessi's avatar
Pekka Pessi committed
964
 */
965 966
msg_t *nua_creq_msg(nua_t *nua,
		    nua_handle_t *nh,
967
		    nua_client_request_t *cr,
Pekka Pessi's avatar
Pekka Pessi committed
968 969 970
		    int restart,
		    sip_method_t method, char const *name,
		    tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
971 972
{
  struct nua_dialog_state *ds = nh->nh_ds;
973
  msg_t *msg = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
974
  sip_t *sip;
975
  ta_list ta;
Pekka Pessi's avatar
Pekka Pessi committed
976 977
  url_string_t const *url = NULL;
  long seq = -1;
978
  int copy = 1;
Pekka Pessi's avatar
Pekka Pessi committed
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995

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

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

    /* Remove CSeq */
    if (sip && sip->sip_cseq)
      sip_header_remove(msg, sip, (sip_header_t *)sip->sip_cseq);
    if (sip && sip->sip_request)
996
      method = sip->sip_request->rq_method,
Pekka Pessi's avatar
Pekka Pessi committed
997 998 999 1000 1001 1002 1003 1004 1005 1006
	name = sip->sip_request->rq_method_name;
  }

  if (!restart) {
    if (cr->cr_msg) {
      /* If method is ACK or CANCEL, use existing CSeq */
      if (method == sip_method_ack || method == sip_method_cancel) {
	sip_t *nh_sip = sip_object(cr->cr_msg);
	if (nh_sip && nh_sip->sip_cseq)
	  seq = nh_sip->sip_cseq->cs_seq;
1007
	/* ACK/CANCEL cannot be restarted so we do not copy message */
Pekka Pessi's avatar
Pekka Pessi committed
1008 1009 1010
	copy = 0;
      }
      else
1011
	msg_destroy(cr->cr_msg), cr->cr_msg = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1012 1013
    }
    msg = nta_msg_create(nua->nua_nta, 0);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

    /**@par Populating SIP Request Message with Tagged Arguments
     *
     * The tagged arguments can be used to pass values for any SIP headers
     * to the stack. When the INVITE message (or any other SIP message) is
     * created, the tagged values saved with nua_handle() are used first,
     * next the tagged values given with the operation (nua_invite()) are
     * added.
     *
     * 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 in a SIP message, the latest non-NULL value is used, e.g.,
     * @Subject. However, if the SIP header can consist of multiple lines or
     * header fields separated by comma, e.g., @Accept, all the tagged
     * values are concatenated.
     *
     * However, if a tag value is #SIP_NONE (-1 casted as a void pointer),
     * the values from previous tags are ignored.
     */
Pekka Pessi's avatar
Pekka Pessi committed
1033 1034 1035 1036
    tl_gets(nh->nh_tags, NUTAG_URL_REF(url), TAG_END());
    sip_add_tl(msg, sip_object(msg), TAG_NEXT(nh->nh_tags));
  }

1037
  ta_start(ta, tag, value);
Pekka Pessi's avatar
Pekka Pessi committed
1038

1039 1040 1041
  sip = sip_object(msg);
  if (!sip)
    goto error;
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  if (sip_add_tl(msg, sip, ta_tags(ta)) < 0)
    goto error;

  if (method != sip_method_ack) {
    /**
     * Next, values previously set with nua_set_params() or nua_set_hparams()
     * are used: @Allow, @Supported, @Organization, and @UserAgent headers are
     * added to the request if they are not already set. 
     */
    if (!sip->sip_allow && !ds->ds_remote_tag)
      sip_add_dup(msg, sip, (sip_header_t*)NH_PGET(nh, allow));

    if (!sip->sip_supported && NH_PGET(nh, supported))
      sip_add_dup(msg, sip, (sip_header_t *)NH_PGET(nh, supported));
    
    if (method == sip_method_register && NH_PGET(nh, path_enable) &&
	!sip_has_feature(sip->sip_supported, "path") &&
	!sip_has_feature(sip->sip_require, "path"))
      sip_add_make(msg, sip, sip_supported_class, "path");

    if (!sip->sip_organization && NH_PGET(nh, organization))
      sip_add_dup(msg, sip, (sip_header_t *)NH_PGET(nh, organization));
Pekka Pessi's avatar
Pekka Pessi committed
1064

1065 1066 1067 1068
    if (!sip->sip_user_agent && NH_PGET(nh, user_agent))
      sip_add_make(msg, sip, sip_user_agent_class, NH_PGET(nh, user_agent));
  }
	  
1069
  {
1070 1071 1072 1073 1074 1075 1076 1077 1078
    int add_contact = 0, use_dialog = 0, add_service_route, has_contact = 0;
    tagi_t const *t;

    for (t = ta_args(ta); t; t = t_next(t)) {
      if (t->t_tag == siptag_contact ||
	  t->t_tag == siptag_contact_str)
	has_contact = 1;
      else if (t->t_tag == nutag_url)
	url = (url_string_t const *)t->t_value;