nua_stack.c 69.3 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>
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
#include <sofia-sip/su_tag_class.h>
40
#include <sofia-sip/su_tag_inline.h>
41
#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
#define NTA_AGENT_MAGIC_T    struct nua_s
#define NTA_LEG_MAGIC_T      struct nua_handle_s
54
#define NTA_OUTGOING_MAGIC_T struct nua_client_request
Pekka Pessi's avatar
Pekka Pessi committed
55

56 57 58 59
#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
60

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

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

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

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

78
#include <assert.h>
79

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

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

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

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

97 98 99
inline int nua_client_request_queue(nua_client_request_t *cr);
inline nua_client_request_t *nua_client_request_remove(nua_client_request_t *cr);

100 101
/* ---------------------------------------------------------------------- */
/* Constant data */
Pekka Pessi's avatar
Pekka Pessi committed
102

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

106 107 108 109
char const nua_application_sdp[] = "application/sdp";

#define NUA_STACK_TIMER_INTERVAL (1000)

Pekka Pessi's avatar
Pekka Pessi committed
110 111 112 113
/* ----------------------------------------------------------------------
 * Initialization & deinitialization
 */

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

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

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

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

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

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

161 162 163
  if (nua_stack_set_defaults(dnh, dnh->nh_prefs) < 0)
    return -1;

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

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

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

179 180 181 182 183 184 185 186 187 188
  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"));
189
    return -1;
190
  }
191 192

  if (nua_stack_set_from(nua, 1, nua->nua_args) < 0)
193
    return -1;
194

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

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

Pekka Pessi's avatar
Pekka Pessi committed
200 201 202
  return 0;
}

203
void nua_stack_deinit(su_root_t *root, nua_t *nua)
Pekka Pessi's avatar
Pekka Pessi committed
204 205 206 207 208 209 210 211 212 213 214
{
  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
 */

215
static void nua_stack_shutdown(nua_t *);
216

217 218 219 220
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
221

222 223 224 225 226
/* 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
227

228 229
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
230

231 232 233 234 235 236 237 238 239 240 241 242
int nua_stack_tevent(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, ...)
{
  ta_list ta;
  int retval;
  ta_start(ta, tag, value);
  retval = nua_stack_event(nua, nh, msg, event, status, phrase, ta_args(ta));
  ta_end(ta);
  return retval;
}

243
/** @internal Send an event to the application. */
244 245
int nua_stack_event(nua_t *nua, nua_handle_t *nh, msg_t *msg,
		    nua_event_t event, int status, char const *phrase,
246
		    tagi_t const *tags)
Pekka Pessi's avatar
Pekka Pessi committed
247 248
{
  su_msg_r sumsg = SU_MSG_R_INIT;
249
  size_t e_len, len, xtra, p_len;
Pekka Pessi's avatar
Pekka Pessi committed
250

251
  if (event == nua_r_ack || event == nua_i_none)
252 253
    return event;

Pekka Pessi's avatar
Pekka Pessi committed
254 255 256 257 258
  if (nua_log->log_level >= 5) {
    char const *name = nua_event_name(event) + 4;
    char const *p = phrase ? phrase : "";

    if (status == 0)
259
      SU_DEBUG_5(("nua(%p): event %s %s\n", nh, name, p));
Pekka Pessi's avatar
Pekka Pessi committed
260
    else
261
      SU_DEBUG_5(("nua(%p): event %s %u %s\n", nh, name, status, p));
Pekka Pessi's avatar
Pekka Pessi committed
262 263 264
  }

  if (event == nua_r_destroy) {
265
    if (msg)
Pekka Pessi's avatar
Pekka Pessi committed
266 267 268 269 270 271 272
      msg_destroy(msg);
    if (status >= 200) {
      nh_destroy(nua, nh);
    }
    return event;
  }

273 274
  if ((event > nua_r_authenticate && event <= nua_r_ack)
      || event < nua_i_error
275 276
      || (nh && !nh->nh_valid)
      || (nua->nua_shutdown && event != nua_r_shutdown)) {
277
    if (msg)
Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281
      msg_destroy(msg);
    return event;
  }

282 283 284 285 286 287 288 289
  if (tags) {
    e_len = offsetof(event_t, e_tags);
    len = tl_len(tags);
    xtra = tl_xtra(tags, len);
  }
  else {
    e_len = sizeof(event_t), len = 0, xtra = 0;
  }
Pekka Pessi's avatar
Pekka Pessi committed
290 291
  p_len = phrase ? strlen(phrase) + 1 : 1;

292
  if (su_msg_create(sumsg, nua->nua_client, su_task_null,
Pekka Pessi's avatar
Pekka Pessi committed
293 294
		    nua_event, e_len + len + xtra + p_len) == 0) {
    event_t *e = su_msg_data(sumsg);
295
    void *p;
Pekka Pessi's avatar
Pekka Pessi committed
296

297 298 299
    if (tags) {
      tagi_t *t = e->e_tags, *t_end = (tagi_t *)((char *)t + len);
      void *b = t_end, *end = (char *)b + xtra;
Pekka Pessi's avatar
Pekka Pessi committed
300

301 302 303 304 305
      t = tl_dup(t, tags, &b); p = b;
      assert(t == t_end); assert(b == end);
    }
    else
      p = e + 1;
Pekka Pessi's avatar
Pekka Pessi committed
306 307

    e->e_event = event;
308
    e->e_nh = nh ? nua_handle_ref(nh) : nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
309
    e->e_status = status;
310
    e->e_phrase = strcpy(p, phrase ? phrase : "");
311 312
    if (msg)
      e->e_msg = msg, su_home_threadsafe(msg_home(msg));
Pekka Pessi's avatar
Pekka Pessi committed
313

314
    if (su_msg_send(sumsg) != 0 && nh)
315
      nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
316 317 318 319 320 321
  }

  return event;
}

/* ----------------------------------------------------------------------
322
 * Post signal to stack itself
Pekka Pessi's avatar
Pekka Pessi committed
323
 */
324
void nua_stack_post_signal(nua_handle_t *nh, nua_event_t event,
325
			   tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
326 327 328
{
  ta_list ta;
  ta_start(ta, tag, value);
329
  nua_signal((nh)->nh_nua, nh, NULL, 1, event, 0, NULL, ta_tags(ta));
Pekka Pessi's avatar
Pekka Pessi committed
330 331 332 333 334 335 336
  ta_end(ta);
}


/* ----------------------------------------------------------------------
 * Receiving events from client
 */
337
void nua_stack_signal(nua_t *nua, su_msg_r msg, nua_event_data_t *e)
Pekka Pessi's avatar
Pekka Pessi committed
338 339 340
{
  nua_handle_t *nh = e->e_nh;
  tagi_t *tags = e->e_tags;
341 342
  nua_event_t event;
  int error = 0;
Pekka Pessi's avatar
Pekka Pessi committed
343

344 345
  assert(tags);

346 347 348 349 350
  if (nua_log->log_level >= 7) {
    char const *name = nua_event_name(e->e_event) + 4;
    SU_DEBUG_7(("nua(%p): recv %s\n", nh, name));
  }

Pekka Pessi's avatar
Pekka Pessi committed
351 352 353 354
  if (nh) {
    if (!nh->nh_prev)
      nh_append(nua, nh);
    if (!nh->nh_ref_by_stack) {
355
      /* Mark handle as used by stack */
Pekka Pessi's avatar
Pekka Pessi committed
356
      nh->nh_ref_by_stack = 1;
357
      nua_handle_ref(nh);
358
    }
Pekka Pessi's avatar
Pekka Pessi committed
359 360 361 362 363 364 365
  }

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

370 371
  su_msg_save(nua->nua_signal, msg);

372 373
  event = e->e_event;

Pekka Pessi's avatar
Pekka Pessi committed
374 375
  if (nua->nua_shutdown && !e->e_always) {
    /* Shutting down */
376
    nua_stack_event(nua, nh, NULL, event,
377
		    901, "Stack is going down",
378
		    NULL);
Pekka Pessi's avatar
Pekka Pessi committed
379
  }
380
  else switch (event) {
Pekka Pessi's avatar
Pekka Pessi committed
381
  case nua_r_get_params:
382
    nua_stack_get_params(nua, nh ? nh : nua->nua_dhandle, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
383 384
    break;
  case nua_r_set_params:
385
    nua_stack_set_params(nua, nh ? nh : nua->nua_dhandle, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
386 387
    break;
  case nua_r_shutdown:
388
    nua_stack_shutdown(nua);
Pekka Pessi's avatar
Pekka Pessi committed
389 390 391
    break;
  case nua_r_register:
  case nua_r_unregister:
392
    nua_stack_register(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
393 394
    break;
  case nua_r_invite:
395
    error = nua_stack_invite(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
396 397
    break;
  case nua_r_cancel:
398
    error = nua_stack_cancel(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
399 400
    break;
  case nua_r_bye:
401
    error = nua_stack_bye(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
402 403
    break;
  case nua_r_options:
404
    error = nua_stack_options(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
405 406
    break;
  case nua_r_refer:
407
    error = nua_stack_refer(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
408 409
    break;
  case nua_r_publish:
Pekka Pessi's avatar
Pekka Pessi committed
410
  case nua_r_unpublish:
411
    error = nua_stack_publish(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
412 413
    break;
  case nua_r_info:
414
    error = nua_stack_info(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
415 416
    break;
  case nua_r_update:
417
    error = nua_stack_update(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
418 419
    break;
  case nua_r_message:
420
    error = nua_stack_message(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
421 422 423
    break;
  case nua_r_subscribe:
  case nua_r_unsubscribe:
424
    error = nua_stack_subscribe(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
425 426
    break;
  case nua_r_notify:
427
    error = nua_stack_notify(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
428 429
    break;
  case nua_r_notifier:
430
    nua_stack_notifier(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
431 432
    break;
  case nua_r_terminate:
433
    nua_stack_terminate(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
434 435
    break;
  case nua_r_method:
436
    error = nua_stack_method(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
437 438
    break;
  case nua_r_authenticate:
439
    nua_stack_authenticate(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
440
    break;
441
  case nua_r_authorize:
442
    nua_stack_authorize(nua, nh, event, tags);
443
    break;
Pekka Pessi's avatar
Pekka Pessi committed
444
  case nua_r_ack:
445
    error = nua_stack_ack(nua, nh, event, tags);
Pekka Pessi's avatar
Pekka Pessi committed
446 447
    break;
  case nua_r_respond:
448
    nua_stack_respond(nua, nh, e->e_status, e->e_phrase, tags);
Pekka Pessi's avatar
Pekka Pessi committed
449
    break;
450
  case nua_r_destroy: 
451
    nua_stack_destroy_handle(nua, nh, tags);
452 453
    su_msg_destroy(nua->nua_signal);
    return;
Pekka Pessi's avatar
Pekka Pessi committed
454 455 456 457
  default:
    break;
  }

458
  if (error < 0) {
459
    nua_stack_event(nh->nh_nua, nh, NULL, event, NUA_INTERNAL_ERROR, NULL);
460 461
  }

462 463 464
  if (su_msg_is_non_null(nua->nua_signal))
    su_msg_destroy(nua->nua_signal);

465
  if (nh != nua->nua_dhandle)
466
    nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
467 468 469 470 471 472
}

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

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

473 474
/**@internal
 * Timer routine.
Pekka Pessi's avatar
Pekka Pessi committed
475
 *
476
 * Go through all active handles and execute pending tasks
Pekka Pessi's avatar
Pekka Pessi committed
477
 */
478
void nua_stack_timer(nua_t *nua, su_timer_t *t, su_timer_arg_t *a)
Pekka Pessi's avatar
Pekka Pessi committed
479 480 481
{
  nua_handle_t *nh, *nh_next;
  sip_time_t now = sip_now();
482
  su_root_t *root = su_timer_root(t);
Pekka Pessi's avatar
Pekka Pessi committed
483

484
  su_timer_set(t, nua_stack_timer, a);
Pekka Pessi's avatar
Pekka Pessi committed
485 486

  if (nua->nua_shutdown) {
487
    nua_stack_shutdown(nua);
Pekka Pessi's avatar
Pekka Pessi committed
488 489 490 491 492 493
    return;
  }

  for (nh = nua->nua_handles; nh; nh = nh_next) {
    nh_next = nh->nh_next;
    nh_call_pending(nh, now);
494
    su_root_yield(root);	/* Handle received packets */
Pekka Pessi's avatar
Pekka Pessi committed
495 496 497 498 499 500 501
  }
}


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

506
  for (du = ds->ds_usage; du; du = du->du_next) {
507 508 509
    if (now == 0)
      break;
    if (du->du_refresh && du->du_refresh < next)
Pekka Pessi's avatar
Pekka Pessi committed
510 511 512 513 514 515
      break;
  }

  if (du == NULL)
    return 0;

516
  nua_handle_ref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
517 518 519 520

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

521
    nua_dialog_usage_refresh(nh, ds, du, now);
Pekka Pessi's avatar
Pekka Pessi committed
522 523 524 525 526 527 528 529 530

    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) {
531 532 533
      if (now == 0)
	break;
      if (du->du_refresh && du->du_refresh < next)
Pekka Pessi's avatar
Pekka Pessi committed
534 535 536 537
	break;
    }
  }

538
  nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
539 540 541 542 543

  return 1;
}


544

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

591
/** @internal Shut down stack. */
592
void nua_stack_shutdown(nua_t *nua)
Pekka Pessi's avatar
Pekka Pessi committed
593 594 595 596 597 598 599 600 601 602 603 604 605
{
  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) {
606
    nua_dialog_state_t *ds = nh->nh_ds;
607 608
    nua_server_request_t *sr, *sr_next;

Pekka Pessi's avatar
Pekka Pessi committed
609 610
    nh_next = nh->nh_next;

611
    for (sr = ds->ds_sr; sr; sr = sr_next) {
612
      sr_next = sr->sr_next;
Pekka Pessi's avatar
Pekka Pessi committed
613

614 615 616 617
      if (nua_server_request_is_pending(sr)) {
	SR_STATUS1(sr, SIP_410_GONE); /* 410 terminates dialog */
	nua_server_respond(sr, NULL);
	nua_server_report(sr);
618
      }
Pekka Pessi's avatar
Pekka Pessi committed
619 620 621 622
    }

    busy += nh_call_pending(nh, 0);

Pekka Pessi's avatar
Pekka Pessi committed
623 624
    if (nh->nh_soa) {
      soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
625 626
    }

627
    if (nua_client_request_pending(ds->ds_cr))
Pekka Pessi's avatar
Pekka Pessi committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
      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");

643
  if (status >= 200) {
644 645 646 647 648 649
    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);
      }
    }
650 651 652 653
    su_timer_destroy(nua->nua_timer), nua->nua_timer = NULL;
    nta_agent_destroy(nua->nua_nta), nua->nua_nta = NULL;
  }

654
  nua_stack_event(nua, NULL, NULL, nua_r_shutdown, status, phrase, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
655 656 657 658
}

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

659
/** @internal Create a handle */
Pekka Pessi's avatar
Pekka Pessi committed
660 661 662 663 664 665 666 667 668 669
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);
670

Pekka Pessi's avatar
Pekka Pessi committed
671 672 673 674 675 676 677 678
  if (nh) {
    nh->nh_ref_by_stack = 1;
    nh_append(nua, nh);
  }

  return nh;
}

679
/** @internal Append a handle to the list of handles */
Pekka Pessi's avatar
Pekka Pessi committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
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;
}

700
void nua_stack_destroy_handle(nua_t *nua, nua_handle_t *nh, tagi_t const *tags)
701 702
{
  if (nh->nh_notifier)
703
    nua_stack_terminate(nua, nh, 0, NULL);
704

705 706
  nua_dialog_shutdown(nh, nh->nh_ds);

707 708 709 710 711 712 713 714 715 716
#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
717 718
#define nh_is_inserted(nh) ((nh)->nh_prev != NULL)

719
/** @internal Remove a handle from list of handles */
Pekka Pessi's avatar
Pekka Pessi committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
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;
}

736

Pekka Pessi's avatar
Pekka Pessi committed
737 738
void nh_destroy(nua_t *nua, nua_handle_t *nh)
{
739
  assert(nh); assert(nh != nua->nua_dhandle);
Pekka Pessi's avatar
Pekka Pessi committed
740 741 742 743

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

744
  nua_dialog_deinit(nh, nh->nh_ds);
Pekka Pessi's avatar
Pekka Pessi committed
745

746 747 748 749 750 751
  while (nh->nh_ds->ds_cr)
    nua_client_request_destroy(nh->nh_ds->ds_cr);

  while (nh->nh_ds->ds_sr)
    nua_server_request_destroy(nh->nh_ds->ds_sr);

Pekka Pessi's avatar
Pekka Pessi committed
752 753
  if (nh->nh_soa)
    soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
754 755 756 757

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

758
  nua_handle_unref(nh);		/* Remove stack reference */
Pekka Pessi's avatar
Pekka Pessi committed
759 760 761 762
}

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

763
/**@internal
764
 * Save handle parameters and initial authentication info.
765 766 767 768
 *
 * @retval -1 upon an error
 * @retval 0 when successful
 */
769
int nua_stack_init_handle(nua_t *nua, nua_handle_t *nh,
770
			  tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
771 772
{
  ta_list ta;
773
  int retval = 0;
Pekka Pessi's avatar
Pekka Pessi committed
774

775 776 777
  if (nh == NULL)
    return -1;

778
  assert(nh != nua->nua_dhandle);
Pekka Pessi's avatar
Pekka Pessi committed
779 780 781

  ta_start(ta, tag, value);

782
  if (nua_stack_set_params(nua, nh, nua_i_error, ta_args(ta)) < 0)
783
    retval = -1;
784

785 786 787
  if (!retval && nh->nh_soa)
    if (soa_set_params(nh->nh_soa, ta_tags(ta)) < 0)
      retval = -1;
788 789 790

  ta_end(ta);

791 792
  if (retval || nh->nh_init) /* Already initialized? */
    return retval;
Pekka Pessi's avatar
Pekka Pessi committed
793 794 795 796 797

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

  nh->nh_init = 1;
798 799

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
800 801
}

802
/** @internal Create a handle for processing incoming request */
803
nua_handle_t *nua_stack_incoming_handle(nua_t *nua,
804 805 806
					nta_incoming_t *irq,
					sip_t const *sip,
					int create_dialog)
Pekka Pessi's avatar
Pekka Pessi committed
807 808 809 810 811 812 813 814 815 816 817 818 819
{
  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;

820
  /* Strip away parameters */
Pekka Pessi's avatar
Pekka Pessi committed
821 822 823 824 825 826 827
  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,
828 829 830
		 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
831 832
		 TAG_END());

833
  if (nua_stack_init_handle(nh->nh_nua, nh, TAG_END()) < 0)
834
    nh_destroy(nua, nh), nh = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
835

836 837
  if (nh && create_dialog) {
    struct nua_dialog_state *ds = nh->nh_ds;
Pekka Pessi's avatar
Pekka Pessi committed
838

839
    nua_dialog_store_peer_info(nh, ds, sip);
Pekka Pessi's avatar
Pekka Pessi committed
840

841
    ds->ds_leg = nta_leg_tcreate(nua->nua_nta, nua_stack_process_request, nh,
842 843 844 845 846
				 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
847

848
    if (!ds->ds_leg || !nta_leg_tag(ds->ds_leg, nta_incoming_tag(irq, NULL)))
849
      nh_destroy(nua, nh), nh = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
850 851 852
  }

  if (nh)
853
    nua_dialog_uas_route(nh, nh->nh_ds, sip, 1);
Pekka Pessi's avatar
Pekka Pessi committed
854 855 856 857 858

  return nh;
}


859 860 861 862 863
/** Set flags and special event on handle.
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
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;
}

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
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;
}


914
/** @internal Add authorization data */
Pekka Pessi's avatar
Pekka Pessi committed
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
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;
930
      }
Pekka Pessi's avatar
Pekka Pessi committed
931
      else if (rv < 0) {
932
	retval = -1;
Pekka Pessi's avatar
Pekka Pessi committed
933 934 935 936 937 938 939 940 941 942
	break;
      }
    }
  }

  ta_end(ta);

  return retval;
}

943 944
/**@internal
 * Collect challenges from response.
945 946 947 948
 *
 * @return Number of updated challenges, 0 if no updates found.
 * @retval -1 upon error.
 */
Pekka Pessi's avatar
Pekka Pessi committed
949 950 951
static
int nh_challenge(nua_handle_t *nh, sip_t const *sip)
{
952 953
  int server = 0, proxy = 0;

Pekka Pessi's avatar
Pekka Pessi committed
954
  if (sip->sip_www_authenticate)
955
    server = auc_challenge(&nh->nh_auth, nh->nh_home,
956
			   sip->sip_www_authenticate,
957
			   sip_authorization_class);
Pekka Pessi's avatar
Pekka Pessi committed
958 959

  if (sip->sip_proxy_authenticate)
960
    proxy = auc_challenge(&nh->nh_auth, nh->nh_home,
961
			  sip->sip_proxy_authenticate,
962
			  sip_proxy_authorization_class);
Pekka Pessi's avatar
Pekka Pessi committed
963

964 965 966 967
  if (server < 0 || proxy < 0)
    return -1;

  return server + proxy;
Pekka Pessi's avatar
Pekka Pessi committed
968 969
}

970 971
static inline
int can_redirect(sip_contact_t const *m, sip_method_t method)
Pekka Pessi's avatar
Pekka Pessi committed
972
{
973 974 975 976 977 978 979 980 981
  if (m && m->m_url->url_host) {
    enum url_type_e type = m->m_url->url_type;
    return
      type == url_sip ||
      type == url_sips ||
      (type == url_tel &&
       (method == sip_method_invite || method == sip_method_message)) ||
      (type == url_im && method == sip_method_message) ||
      (type == url_pres && method == sip_method_subscribe);
Pekka Pessi's avatar
Pekka Pessi committed
982
  }
983
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
984 985
}

986 987 988 989
/* ======================================================================== */
/* Authentication */

/** @NUA_EVENT nua_r_authenticate
990
 *
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
 * Response to nua_authenticate(). Under normal operation, this event is
 * never sent but rather the unauthenticated operation is completed. 
 * However, if there is no operation to authentication or if there is an
 * authentication error the #nua_r_authenticate event is sent to the
 * application with the status code as follows:
 * - <i>202 No operation to restart</i>:\n
 *   The authenticator associated with the handle was updated, but there was
 *   no operation to retry with the new credentials.
 * - <i>900 Cannot add credentials</i>:\n
 *   There was internal problem updating authenticator.
 * - <i>904 No matching challenge</i>:\n
 *   There was no challenge matching with the credentials provided by
 *   nua_authenticate(), e.g., their realm did not match with the one 
 *   received with the challenge.
 * 
 * @param status status code from authentication 
 * @param phrase a short textual description of @a status code
 * @param nh     operation handle authenticated
 * @param hmagic application context associated with the handle
 * @param sip    NULL
 * @param tags   empty
 * 
 * @sa nua_terminate(), nua_handle_destroy()
 *
 * @END_NUA_EVENT
Pekka Pessi's avatar
Pekka Pessi committed
1016 1017
 */

1018 1019 1020 1021 1022
void
nua_stack_authenticate(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		       tagi_t const *tags)
{
  int status = nh_authorize(nh, TAG_NEXT(tags));
1023

1024 1025
  if (status > 0) {
    nua_client_request_t *cr = nh->nh_ds->ds_cr;
1026

1027 1028 1029 1030 1031 1032
    if (cr && cr->cr_challenged) {
      nua_client_resend_request(cr, cr->cr_terminating, tags);
    }
    else {
      nua_stack_event(nua, nh, NULL, e, 
		      202, "No operation to restart",
1033
		      NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1034 1035
    }
  }
1036
  else if (status < 0) {
1037
    nua_stack_event(nua, nh, NULL, e, 900, "Cannot add credentials", NULL);
1038 1039
  }
  else {
1040
    nua_stack_event(nua, nh, NULL, e, 904, "No matching challenge", NULL);
1041
  }
Pekka Pessi's avatar
Pekka Pessi committed
1042 1043
}

1044

1045 1046 1047 1048
/* ======================================================================== */
/*
 * Process incoming requests
 */
1049

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
nua_server_methods_t const *nua_server_methods[] = {
  /* These must be in same order as in sip_method_t */
  &nua_extension_server_methods,
  &nua_invite_server_methods,	/**< INVITE */
  NULL,				/**< ACK */
  NULL,				/**< CANCEL */
  &nua_bye_server_methods,	/**< BYE */
  &nua_options_server_methods,	/**< OPTIONS */
  &nua_register_server_methods,	/**< REGISTER */
  &nua_info_server_methods,	/**< INFO */
  &nua_prack_server_methods,	/**< PRACK */
  &nua_update_server_methods,	/**< UPDATE */
  &nua_message_server_methods,	/**< MESSAGE */
  &nua_subscribe_server_methods,/**< SUBSCRIBE */
  &nua_notify_server_methods,	/**< NOTIFY */
  &nua_refer_server_methods,	/**< REFER */
  &nua_publish_server_methods,	/**< PUBLISH */
  NULL
};


1071 1072 1073 1074 1075 1076 1077
int nua_stack_process_request(nua_handle_t *nh,
			      nta_leg_t *leg,
			      nta_incoming_t *irq,
			      sip_t const *sip)
{
  nua_t *nua = nh->nh_nua;
  sip_method_t method = sip->sip_request->rq_method;
1078 1079 1080 1081 1082
  char const *name = sip->sip_request->rq_method_name;
  nua_server_methods_t const *sm;
  nua_server_request_t *sr, sr0[1];
  int status, initial = 1;

1083 1084 1085
  char const *user_agent = NH_PGET(nh, user_agent);
  sip_supported_t const *supported = NH_PGET(nh, supported);
  sip_allow_t const *allow = NH_PGET(nh, allow);
1086

1087
  enter;
Pekka Pessi's avatar
Pekka Pessi committed
1088

1089
  nta_incoming_tag(irq, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1090

1091 1092 1093 1094 1095 1096 1097 1098
  /* Hook to outbound */
  if (method == sip_method_options) {
    status = nua_registration_process_request(nua->nua_registrations,
					      irq, sip);
    if (status)
      return status;
  }

1099 1100
  if (nta_check_method(irq, sip, allow,
		       SIPTAG_SUPPORTED(supported),
1101
		       SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1102 1103 1104 1105 1106 1107 1108
		       TAG_END()))
    return 405;

  switch (sip->sip_request->rq_url->url_type) {
  case url_sip:
  case url_sips:
  case url_im:
1109 1110
  case url_pres:
  case url_tel:
Pekka Pessi's avatar
Pekka Pessi committed
1111 1112
    break;
  default:
1113
    nta_incoming_treply(irq, status = SIP_416_UNSUPPORTED_URI,
1114
			SIPTAG_ALLOW(allow),
1115
			SIPTAG_SUPPORTED(supported),
1116
			SIPTAG_USER_AGENT_STR(user_agent),
1117
			TAG_END());
1118
    return status;
Pekka Pessi's avatar
Pekka Pessi committed
1119 1120
  }

1121
  if (nta_check_required(irq, sip, supported,
1122
			 SIPTAG_ALLOW(allow),
1123
			 SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1124 1125 1126
			 TAG_END()))
    return 420;

1127 1128 1129 1130
  if (method > sip_method_unknown && method <= sip_method_publish)
    sm = nua_server_methods[method];
  else
    sm = nua_server_methods[0];
Pekka Pessi's avatar
Pekka Pessi committed
1131

1132
  initial = nh == nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
1133

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  if (sm == NULL) {
    SU_DEBUG_1(("nua(%p): strange %s from <" URL_PRINT_FORMAT ">\n", nh,
		sip->sip_request->rq_method_name,
		URL_PRINT_ARGS(sip->sip_from->a_url)));
  }
  else if (initial && sm->sm_flags.in_dialog) {
    /* These must be in-dialog */
    sm = NULL;
  }
  else if (initial && sip->sip_to->a_tag) {
    /* RFC 3261 section 12.2.2:
       
       If the UAS wishes to reject the request because it does not wish to
       recreate the dialog, it MUST respond to the request with a 481
       (Call/Transaction Does Not Exist) status code and pass that to the
       server transaction.
    */
    if (method != sip_method_message || !NH_PGET(nh, win_messenger_enable))
      sm = NULL;
  }
Pekka Pessi's avatar
Pekka Pessi committed
1154

1155
  if (!sm) {
1156
    nta_incoming_treply(irq,
1157
			status = 481, "Call Does Not Exist",
1158
			SIPTAG_ALLOW(allow),
1159
			SIPTAG_SUPPORTED(supported),
1160
			SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1161 1162
			TAG_END());
    return 481;
1163
  }
Pekka Pessi's avatar
Pekka Pessi committed
1164

1165
  sr = memset(sr0, 0, (sizeof sr0));
1166

1167 1168 1169
  sr->sr_methods = sm;
  sr->sr_method = method = sip->sip_request->rq_method;
  sr->sr_add_contact = sm->sm_flags.add_contact;
1170

1171 1172
  sr->sr_owner = nh;
  sr->sr_initial = initial;
Pekka Pessi's avatar
Pekka Pessi committed
1173

1174
  sr->sr_irq = irq;
Pekka Pessi's avatar
Pekka Pessi committed
1175

1176
  SR_STATUS1(sr, SIP_100_TRYING);
Pekka Pessi's avatar
Pekka Pessi committed
1177

1178 1179 1180
  sr->sr_request.msg = nta_incoming_getrequest(irq);
  sr->sr_request.sip = sip;
  assert(sr->sr_request.msg);
Pekka Pessi's avatar
Pekka Pessi committed
1181

1182 1183
  sr->sr_response.msg = nta_incoming_create_response(irq, 0, NULL);
  sr->sr_response.sip = sip_object(sr->sr_response.msg);
1184

1185 1186 1187 1188 1189 1190
  if (sr->sr_response.msg == NULL) {
    SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
  }
  else if (sm->sm_init && sm->sm_init(sr)) {
    if (sr->sr_status < 200)    /* Init may have set response status */
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
Pekka Pessi's avatar
Pekka Pessi committed
1191
  }
1192
  /* Create handle if request does not fail */
1193 1194 1195 1196
  else if (initial && sr->sr_status < 300) {
    if ((nh = nua_stack_incoming_handle(nua, irq, sip, create_dialog)))
      sr->sr_owner = nh;
    else
1197
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
1198
  }
1199 1200 1201 1202

  if (sr->sr_status < 300 && sm->sm_preprocess && sm->sm_preprocess(sr)) {
    if (sr->sr_status < 200)    /* Preprocess may have set response status */
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
1203
  }
1204

1205 1206 1207 1208 1209
  if (sr->sr_status < 300) {
    if (sm->sm_flags.target_refresh)
      nua_dialog_uas_route(nh, nh->nh_ds, sip, 1); /* Set route and tags */
    nua_dialog_store_peer_info(nh, nh->nh_ds, sip);
  }
1210

1211
  if (sr->sr_status == 100 && method != sip_method_unknown &&
1212 1213 1214 1215 1216 1217
      !sip_is_allowed(NH_PGET(sr->sr_owner, appl_method), method, name)) {
    if (method == sip_method_refer || method == sip_method_subscribe)
      SR_STATUS1(sr, SIP_202_ACCEPTED);
    else
      SR_STATUS1(sr, SIP_200_OK);
  }
1218 1219

  /* INVITE server request is not finalized after 2XX response */
1220 1221
  if (sr->sr_status < (method == sip_method_invite ? 300 : 200)) {
    sr = su_alloc(nh->nh_home, (sizeof *sr));
1222 1223

    if (sr) {
1224 1225
      *sr = *sr0;

1226
      if ((sr->sr_next = nh->nh_ds->ds_sr))
1227 1228 1229
	*(sr->sr_prev = sr->sr_next->sr_prev) = sr,
	  sr->sr_next->sr_prev = &sr->sr_next;
      else
1230
	*(sr->sr_prev = &nh->nh_ds->ds_sr) = sr;
1231 1232 1233 1234 1235
    }
    else {
      sr = sr0;
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
    }
Pekka Pessi's avatar
Pekka Pessi committed
1236
  }
1237

1238 1239 1240 1241 1242 1243 1244
  if (sr->sr_status <= 100) {
    SR_STATUS1(sr, SIP_100_TRYING);
    if (method == sip_method_invite || sip->sip_timestamp) {
      nta_incoming_treply(irq, SIP_100_TRYING,
			  SIPTAG_USER_AGENT_STR(user_agent),
			  TAG_END());
    }
1245 1246
  }
  else {
1247 1248
    /* Note that this may change the sr->sr_status */
    nua_server_respond(sr, NULL);
1249 1250
  }

1251 1252
  if (nua_server_report(sr) == 0)
    return 0;
1253

1254 1255
  return 501;
}		 
1256

1257 1258
#undef nua_base_server_init
#undef nua_base_server_preprocess
1259

1260 1261
int nua_base_server_init(nua_server_request_t *sr)
{
1262 1263 1264
  return 0;
}

1265
int nua_base_server_preprocess(nua_server_request_t *sr)
1266
{
1267
  return 0;
1268 1269
}

1270
void nua_server_request_destroy(nua_server_request_t *sr)
1271
{
1272 1273
  if (sr == NULL)
    return;
1274

1275 1276
  if (sr->sr_irq)
    nta_incoming_destroy(sr->sr_irq), sr->sr_irq = NULL;
1277

1278 1279
  if (sr->sr_request.msg)
    msg_destroy(sr->sr_request.msg), sr->sr_request.msg = NULL;
1280

1281 1282
  if (sr->sr_response.msg)
    msg_destroy(sr->sr_response.msg), sr->sr_response.msg = NULL;
1283

1284 1285 1286 1287 1288 1289
  if (sr->sr_prev) {
    /* Allocated from heap */
    if ((*sr->sr_prev = sr->sr_next))
      sr->sr_next->sr_prev = sr->sr_prev;
    su_free(sr->sr_owner->nh_home, sr);
  }
1290 1291 1292
}

/** Respond to a request with given status. 
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
 *
 * When nua protocol engine receives an incoming SIP request, it can either
 * respond to the request automatically or let it up to application to
 * respond to the request. The automatic answer is sent if the request fails
 * because of method, SIP extension or, in some times, MIME content
 * negotiation fails.
 *
 * When responding to an incoming INVITE request, the nua_respond() can be
 * called without NUTAG_WITH() (or NUTAG_WITH_CURRENT() or
 * NUTAG_WITH_SAVED()). Otherwise, NUTAG_WITH() will contain an indication
 * of the request being responded.
 *
1305
 * In order to simplify the simple applications, most requests are responded
1306 1307 1308
 * automatically. The set of requests always responded by the stack include
 * BYE, CANCEL and NOTIFY. The application can add methods that it likes to
 * handle by itself with NUTAG_APPL_METHOD(). The default set of
1309 1310 1311 1312
 * NUTAG_APPL_METHOD() includes INVITE, PUBLISH, REGISTER and SUBSCRIBE. 
 * Note that unless the method is also included in the set of allowed
 * methods with NUTAG_ALLOW(), the stack will respond to the incoming
 * methods with <i>405 Not Allowed</i>.
1313 1314
 *
 * Note that certain methods are rejected outside a SIP session (created
1315 1316 1317
 * with INVITE transaction). They include BYE, UPDATE, PRACK and INFO. Also
 * the auxiliary methods ACK and CANCEL are rejected by stack if there is no
 * ongoing INVITE transaction corresponding to them.
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
 *
 * @param nh              Pointer to operation handle
 * @param status          SIP response status (see RFCs of SIP)
 * @param phrase          free text (default response phrase used if NULL)
 * @param tag, value, ... List of tagged parameters
 *
 * @return 
 *    nothing
 *
 * @par Related Tags:
 *    NUTAG_WITH(), NUTAG_WITH_CURRENT(), NUTAG_WITH_SAVED() \n
 *    NUTAG_EARLY_ANSWER() \n
 *    SOATAG_ADDRESS() \n
 *    SOATAG_AF() \n
 *    SOATAG_HOLD() \n
 *    Tags in <sip_tag.h>.
 *
 * @par Events:
 *    #nua_i_state \n
 *    #nua_i_media_error \n
 *    #nua_i_error \n
 *    #nua_i_active \n
 *    #nua_i_terminated \n
 *
 * @sa #nua_i_invite, #nua_i_register, #nua_i_subscribe, #nua_i_publish
 */
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
void
nua_stack_respond(nua_t *nua, nua_handle_t *nh,
		  int status, char const *phrase, tagi_t const *tags)
{
  nua_server_request_t *sr;
  tagi_t const *t;
  msg_t const *request = NULL;

  t = tl_find_last(tags, nutag_with);

  if (t)
    request = (msg_t const *)t->t_value;

1357
  <