nua_stack.c 69.5 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
  dnh->nh_valid = nua_valid_handle_cookie;
153
  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;

254 255 256
  if (nh == nua->nua_dhandle)
    nh = NULL;

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

    if (status == 0)
262
      SU_DEBUG_5(("nua(%p): event %s %s\n", (void *)nh, name, p));
Pekka Pessi's avatar
Pekka Pessi committed
263
    else
264
      SU_DEBUG_5(("nua(%p): event %s %u %s\n", (void *)nh, name, status, p));
Pekka Pessi's avatar
Pekka Pessi committed
265 266 267
  }

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

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

285 286 287 288 289 290 291 292
  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
293 294
  p_len = phrase ? strlen(phrase) + 1 : 1;

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

300 301 302
    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
303

304 305 306 307 308
      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
309 310

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

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

  return event;
}

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


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

347 348
  assert(tags);

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

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

  if (nua_log->log_level >= 5) {
    char const *name = nua_event_name(e->e_event);
    if (e->e_status == 0)
367
      SU_DEBUG_5(("nua(%p): signal %s\n", (void *)nh, name + 4));
Pekka Pessi's avatar
Pekka Pessi committed
368
    else
369
      SU_DEBUG_5(("nua(%p): signal %s %u %s\n",
370 371
		  (void *)nh, name + 4,
		  e->e_status, e->e_phrase ? e->e_phrase : ""));
Pekka Pessi's avatar
Pekka Pessi committed
372 373
  }

374 375
  su_msg_save(nua->nua_signal, msg);

376 377
  event = e->e_event;

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

462
  if (error < 0) {
463
    nua_stack_event(nh->nh_nua, nh, NULL, event, NUA_INTERNAL_ERROR, NULL);
464 465
  }

466 467 468
  if (su_msg_is_non_null(nua->nua_signal))
    su_msg_destroy(nua->nua_signal);

469
  if (nh != nua->nua_dhandle)
470
    nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
471 472 473 474 475 476
}

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

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

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

488
  su_timer_set(t, nua_stack_timer, a);
Pekka Pessi's avatar
Pekka Pessi committed
489 490

  if (nua->nua_shutdown) {
491
    nua_stack_shutdown(nua);
Pekka Pessi's avatar
Pekka Pessi committed
492 493 494 495 496 497
    return;
  }

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


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

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

  if (du == NULL)
    return 0;

520
  nua_handle_ref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
521 522 523 524

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

525
    nua_dialog_usage_refresh(nh, ds, du, now);
Pekka Pessi's avatar
Pekka Pessi committed
526 527 528 529 530 531 532 533 534

    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) {
535 536 537
      if (now == 0)
	break;
      if (du->du_refresh && du->du_refresh < next)
Pekka Pessi's avatar
Pekka Pessi committed
538 539 540 541
	break;
    }
  }

542
  nua_handle_unref(nh);
Pekka Pessi's avatar
Pekka Pessi committed
543 544 545 546 547

  return 1;
}


548

Pekka Pessi's avatar
Pekka Pessi committed
549 550
/* ====================================================================== */

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
/**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
 */

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

Pekka Pessi's avatar
Pekka Pessi committed
613 614
    nh_next = nh->nh_next;

615
    for (sr = ds->ds_sr; sr; sr = sr_next) {
616
      sr_next = sr->sr_next;
Pekka Pessi's avatar
Pekka Pessi committed
617

618 619 620 621
      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);
622
      }
Pekka Pessi's avatar
Pekka Pessi committed
623 624 625 626
    }

    busy += nh_call_pending(nh, 0);

Pekka Pessi's avatar
Pekka Pessi committed
627 628
    if (nh->nh_soa) {
      soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
629 630
    }

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

647
  if (status >= 200) {
648 649 650 651 652 653
    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);
      }
    }
654 655 656 657
    su_timer_destroy(nua->nua_timer), nua->nua_timer = NULL;
    nta_agent_destroy(nua->nua_nta), nua->nua_nta = NULL;
  }

658
  nua_stack_event(nua, NULL, NULL, nua_r_shutdown, status, phrase, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
659 660 661 662
}

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

663
/** @internal Create a handle */
Pekka Pessi's avatar
Pekka Pessi committed
664 665 666 667 668 669 670 671 672 673
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);
674

Pekka Pessi's avatar
Pekka Pessi committed
675 676 677 678 679 680 681 682
  if (nh) {
    nh->nh_ref_by_stack = 1;
    nh_append(nua, nh);
  }

  return nh;
}

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

704
void nua_stack_destroy_handle(nua_t *nua, nua_handle_t *nh, tagi_t const *tags)
705 706
{
  if (nh->nh_notifier)
707
    nua_stack_terminate(nua, nh, 0, NULL);
708

709 710
  nua_dialog_shutdown(nh, nh->nh_ds);

711 712 713 714 715 716 717 718
  if (nh->nh_ref_by_user) {
    nh->nh_ref_by_user = 0;
    nua_handle_unref(nh);
  }

  nh_destroy(nua, nh);
}

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

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

738

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

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

746
  nua_dialog_deinit(nh, nh->nh_ds);
Pekka Pessi's avatar
Pekka Pessi committed
747

748 749 750 751 752 753
  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
754 755
  if (nh->nh_soa)
    soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
756 757 758 759

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

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

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

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

777 778 779
  if (nh == NULL)
    return -1;

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

  ta_start(ta, tag, value);

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

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

  ta_end(ta);

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

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

  nh->nh_init = 1;
800 801

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
802 803
}

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

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

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

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

841
    nua_dialog_store_peer_info(nh, ds, sip);
Pekka Pessi's avatar
Pekka Pessi committed
842

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

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

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

  return nh;
}


861 862 863 864 865
/** Set flags and special event on handle.
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
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 892 893
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;
}

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


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

  ta_end(ta);

  return retval;
}

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

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

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

966 967 968 969
  if (server < 0 || proxy < 0)
    return -1;

  return server + proxy;
Pekka Pessi's avatar
Pekka Pessi committed
970 971
}

972 973
static inline
int can_redirect(sip_contact_t const *m, sip_method_t method)
Pekka Pessi's avatar
Pekka Pessi committed
974
{
975 976 977 978 979 980 981 982 983
  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
984
  }
985
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
986 987
}

988 989 990 991
/* ======================================================================== */
/* Authentication */

/** @NUA_EVENT nua_r_authenticate
992
 *
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
 * 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
1018 1019
 */

1020 1021 1022 1023 1024
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));
1025

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

1029 1030 1031 1032 1033 1034
    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",
1035
		      NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1036 1037
    }
  }
1038
  else if (status < 0) {
1039
    nua_stack_event(nua, nh, NULL, e, 900, "Cannot add credentials", NULL);
1040 1041
  }
  else {
1042
    nua_stack_event(nua, nh, NULL, e, 904, "No matching challenge", NULL);
1043
  }
Pekka Pessi's avatar
Pekka Pessi committed
1044 1045
}

1046

1047 1048 1049 1050
/* ======================================================================== */
/*
 * Process incoming requests
 */
1051

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
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
};


1073 1074 1075 1076 1077 1078 1079
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;
1080 1081 1082 1083 1084
  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;

1085 1086 1087
  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);
1088

1089
  enter;
Pekka Pessi's avatar
Pekka Pessi committed
1090

1091
  nta_incoming_tag(irq, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1092

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

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

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

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

1129 1130 1131 1132
  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
1133

1134
  initial = nh == nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
1135

1136
  if (sm == NULL) {
1137 1138
    SU_DEBUG_1(("nua(%p): strange %s from <" URL_PRINT_FORMAT ">\n",
		(void *)nh, sip->sip_request->rq_method_name,
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
		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
1156

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

1167
  sr = memset(sr0, 0, (sizeof sr0));
1168

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

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

1176
  sr->sr_irq = irq;
Pekka Pessi's avatar
Pekka Pessi committed
1177

1178
  SR_STATUS1(sr, SIP_100_TRYING);
Pekka Pessi's avatar
Pekka Pessi committed
1179

1180 1181 1182
  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
1183

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

1187 1188 1189 1190 1191 1192
  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
1193
  }
1194
  /* Create handle if request does not fail */
1195 1196 1197 1198
  else if (initial && sr->sr_status < 300) {
    if ((nh = nua_stack_incoming_handle(nua, irq, sip, create_dialog)))
      sr->sr_owner = nh;
    else
1199
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
1200
  }
1201 1202 1203 1204

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

1207 1208 1209 1210 1211
  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);
  }
1212

1213
  if (sr->sr_status == 100 && method != sip_method_unknown &&
1214 1215 1216 1217 1218 1219
      !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);
  }
1220 1221

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

    if (sr) {
1226 1227
      *sr = *sr0;

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

1240 1241 1242 1243 1244 1245 1246
  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());
    }
1247 1248
  }
  else {
1249 1250
    /* Note that this may change the sr->sr_status */
    nua_server_respond(sr, NULL);
1251 1252
  }

1253 1254
  if (nua_server_report(sr) == 0)
    return 0;
1255

1256 1257
  return 501;
}		 
1258

1259 1260
#undef nua_base_server_init
#undef nua_base_server_preprocess
1261

1262 1263
int nua_base_server_init(nua_server_request_t *sr)
{
1264 1265 1266
  return 0;
}

1267
int nua_base_server_preprocess(nua_server_request_t *sr)
1268
{
1269
  return 0;
1270 1271
}

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

1277 1278
  if (sr->sr_irq)
    nta_incoming_destroy(sr->sr_irq), sr->sr_irq = NULL;
1279

1280 1281
  if (sr->sr_request.msg)
    msg_destroy(sr->sr_request.msg), sr->sr_request.msg = NULL;
1282

1283 1284
  if (sr->sr_response.msg)
    msg_destroy(sr->sr_response.msg), sr->sr_response.msg = NULL;
1285

1286 1287 1288 1289 1290 1291
  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);
  }
1292 1293 1294
}

/** Respond to a request with given status. 
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
 *
 * 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.
 *
1307
 * In order to simplify the simple applications, most requests are responded
1308 1309 1310
 * 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
1311 1312 1313 1314
 * 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>.
1315 1316
 *
 * Note that certain methods are rejected outside a SIP session (created
1317 1318 1319
 * 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.
1320 1321 1322 1323 1324 1325 1326 1327