nua_stack.c 73.7 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
			tag_type_t tag, tag_value_t value, ...);

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

95 96
/* ---------------------------------------------------------------------- */
/* Constant data */
Pekka Pessi's avatar
Pekka Pessi committed
97

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

101 102 103 104
char const nua_application_sdp[] = "application/sdp";

#define NUA_STACK_TIMER_INTERVAL (1000)

Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108
/* ----------------------------------------------------------------------
 * Initialization & deinitialization
 */

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

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

132
  nua->nua_root = root;
133 134 135 136
  nua->nua_timer = su_timer_create(su_root_task(root),
				   NUA_STACK_TIMER_INTERVAL);
  if (!nua->nua_timer)
    return -1;
137

Pekka Pessi's avatar
Pekka Pessi committed
138
  home = nua->nua_home;
139 140
  nua->nua_handles_tail = &nua->nua_handles;
  sip_from_init(nua->nua_from);
Pekka Pessi's avatar
Pekka Pessi committed
141

142
  dnh = su_home_clone(nua->nua_home, sizeof (*dnh) + sizeof(*dnh->nh_prefs));
143 144
  if (!dnh)
    return -1;
145

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

156 157 158
  if (nua_stack_set_defaults(dnh, dnh->nh_prefs) < 0)
    return -1;

159
  if (nua_stack_set_params(nua, dnh, nua_i_none, nua->nua_args) < 0)
160 161 162 163
    return -1;

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

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

174 175 176 177 178 179 180 181 182 183
  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"));
184
    return -1;
185
  }
186 187

  if (nua_stack_set_from(nua, 1, nua->nua_args) < 0)
188
    return -1;
189

190 191
  if (NHP_ISSET(dnh->nh_prefs, detect_network_updates))
    nua_stack_launch_network_change_detector(nua);
Pekka Pessi's avatar
Pekka Pessi committed
192

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

Pekka Pessi's avatar
Pekka Pessi committed
195 196 197
  return 0;
}

198
void nua_stack_deinit(su_root_t *root, nua_t *nua)
Pekka Pessi's avatar
Pekka Pessi committed
199 200 201 202 203 204 205 206 207 208 209
{
  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
 */

210
static void nua_stack_shutdown(nua_t *);
211

212 213 214 215
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
216

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

223 224
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
225

226 227 228 229 230 231 232 233 234 235 236 237
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;
}

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

246
  if (event == nua_r_ack || event == nua_i_none)
247 248
    return event;

249 250 251
  if (nh == nua->nua_dhandle)
    nh = NULL;

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

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

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

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

280 281 282 283 284 285 286 287
  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
288 289
  p_len = phrase ? strlen(phrase) + 1 : 1;

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

295 296 297
    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
298

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

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

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

  return event;
}

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


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

342 343
  assert(tags);

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

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

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

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

371 372
  event = e->e_event;

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

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

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

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

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

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

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

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

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

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


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

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

  if (du == NULL)
    return 0;

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

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

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

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

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

  return 1;
}


543

Pekka Pessi's avatar
Pekka Pessi committed
544 545
/* ====================================================================== */

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

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

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

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

613 614 615 616
      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);
617
      }
Pekka Pessi's avatar
Pekka Pessi committed
618 619 620 621
    }

    busy += nh_call_pending(nh, 0);

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

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

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

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

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

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

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

  return nh;
}

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

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

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

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

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

733

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

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

741 742 743 744 745 746
  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);

747 748
  nua_dialog_deinit(nh, nh->nh_ds);

Pekka Pessi's avatar
Pekka Pessi committed
749 750
  if (nh->nh_soa)
    soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
751 752 753 754

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

755
  nua_handle_unref(nh);		/* Remove stack reference */
Pekka Pessi's avatar
Pekka Pessi committed
756 757 758 759
}

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

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

772 773 774
  if (nh == NULL)
    return -1;

775
  assert(nh != nua->nua_dhandle);
Pekka Pessi's avatar
Pekka Pessi committed
776 777 778

  ta_start(ta, tag, value);

779
  if (nua_stack_set_params(nua, nh, nua_i_error, ta_args(ta)) < 0)
780
    retval = -1;
781

782 783 784
  if (!retval && nh->nh_soa)
    if (soa_set_params(nh->nh_soa, ta_tags(ta)) < 0)
      retval = -1;
785 786 787

  ta_end(ta);

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

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

  nh->nh_init = 1;
795 796

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
797 798
}

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

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

830
  if (nua_stack_init_handle(nh->nh_nua, nh, TAG_END()) < 0)
831
    nh_destroy(nua, nh), nh = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
832

833 834
  if (nh && create_dialog) {
    struct nua_dialog_state *ds = nh->nh_ds;
Pekka Pessi's avatar
Pekka Pessi committed
835

836
    nua_dialog_store_peer_info(nh, ds, sip);
Pekka Pessi's avatar
Pekka Pessi committed
837

838
    ds->ds_leg = nta_leg_tcreate(nua->nua_nta, nua_stack_process_request, nh,
839 840 841 842 843
				 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
844

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

  if (nh)
850
    nua_dialog_uas_route(nh, nh->nh_ds, sip, 1);
Pekka Pessi's avatar
Pekka Pessi committed
851 852 853 854 855

  return nh;
}


856 857 858 859 860
/** Set flags and special event on handle.
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
861 862 863 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
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;
}

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


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

  ta_end(ta);

  return retval;
}

940
su_inline
941
int can_redirect(sip_contact_t const *m, sip_method_t method)
Pekka Pessi's avatar
Pekka Pessi committed
942
{
943 944 945 946 947 948 949 950 951
  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
952
  }
953
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
954 955
}

956 957 958 959
/* ======================================================================== */
/* Authentication */

/** @NUA_EVENT nua_r_authenticate
960
 *
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
 * 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
986 987
 */

988 989 990 991
void
nua_stack_authenticate(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		       tagi_t const *tags)
{
992
  nua_client_request_t *cr = nh->nh_ds->ds_cr;
993
  int status = nh_authorize(nh, TAG_NEXT(tags));
994

995
  if (status > 0) {
996
    if (cr && cr->cr_wait_for_cred) {
997
      nua_client_restart_request(cr, cr->cr_terminating, tags);
998 999 1000 1001
    }
    else {
      nua_stack_event(nua, nh, NULL, e, 
		      202, "No operation to restart",
1002
		      NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1003 1004
    }
  }
1005 1006 1007 1008 1009 1010 1011 1012
  else if (cr && cr->cr_wait_for_cred) {
    cr->cr_wait_for_cred = 0;

    if (status < 0)
      nua_client_response(cr, 900, "Cannot add credentials", NULL);
    else
      nua_client_response(cr, 904, "No matching challenge", NULL);
  }
1013
  else if (status < 0) {
1014
    nua_stack_event(nua, nh, NULL, e, 900, "Cannot add credentials", NULL);
1015 1016
  }
  else {
1017
    nua_stack_event(nua, nh, NULL, e, 904, "No matching challenge", NULL);
1018
  }
Pekka Pessi's avatar
Pekka Pessi committed
1019 1020
}

1021

1022 1023 1024 1025
/* ======================================================================== */
/*
 * Process incoming requests
 */
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
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
};


1048 1049 1050 1051 1052 1053 1054
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;
1055 1056 1057 1058
  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;
1059
  int create_dialog;
1060

1061 1062 1063
  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);
1064

1065
  enter;
Pekka Pessi's avatar
Pekka Pessi committed
1066

1067
  nta_incoming_tag(irq, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1068

1069 1070 1071
  if (method == sip_method_cancel)
    return 481;

1072 1073 1074 1075 1076 1077 1078 1079
  /* Hook to outbound */
  if (method == sip_method_options) {
    status = nua_registration_process_request(nua->nua_registrations,
					      irq, sip);
    if (status)
      return status;
  }

1080 1081
  if (nta_check_method(irq, sip, allow,
		       SIPTAG_SUPPORTED(supported),
1082
		       SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1083 1084 1085 1086 1087 1088 1089
		       TAG_END()))
    return 405;

  switch (sip->sip_request->rq_url->url_type) {
  case url_sip:
  case url_sips:
  case url_im:
1090 1091
  case url_pres:
  case url_tel:
Pekka Pessi's avatar
Pekka Pessi committed
1092 1093
    break;
  default:
1094
    nta_incoming_treply(irq, status = SIP_416_UNSUPPORTED_URI,
1095
			SIPTAG_ALLOW(allow),
1096
			SIPTAG_SUPPORTED(supported),
1097
			SIPTAG_USER_AGENT_STR(user_agent),
1098
			TAG_END());
1099
    return status;
Pekka Pessi's avatar
Pekka Pessi committed
1100 1101
  }

1102
  if (nta_check_required(irq, sip, supported,
1103
			 SIPTAG_ALLOW(allow),
1104
			 SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1105 1106 1107
			 TAG_END()))
    return 420;

1108 1109 1110 1111
  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
1112

1113
  initial = nh == nua->nua_dhandle;
Pekka Pessi's avatar
Pekka Pessi committed
1114

1115
  if (sm == NULL) {
1116 1117
    SU_DEBUG_1(("nua(%p): strange %s from <" URL_PRINT_FORMAT ">\n",
		(void *)nh, sip->sip_request->rq_method_name,
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
		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
1135

1136
  if (!sm) {
1137
    nta_incoming_treply(irq,
1138
			status = 481, "Call Does Not Exist",
1139
			SIPTAG_ALLOW(allow),
1140
			SIPTAG_SUPPORTED(supported),
1141
			SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1142 1143
			TAG_END());
    return 481;
1144
  }
Pekka Pessi's avatar
Pekka Pessi committed
1145

1146 1147 1148
  create_dialog = sm->sm_flags.create_dialog;
  if (method == sip_method_message && NH_PGET(nh, win_messenger_enable))
    create_dialog = 1;
1149
  sr = memset(sr0, 0, (sizeof sr0));
1150

1151 1152 1153
  sr->sr_methods = sm;
  sr->sr_method = method = sip->sip_request->rq_method;
  sr->sr_add_contact = sm->sm_flags.add_contact;
1154
  sr->sr_target_refresh = sm->sm_flags.target_refresh;
1155

1156 1157
  sr->sr_owner = nh;
  sr->sr_initial = initial;
Pekka Pessi's avatar
Pekka Pessi committed
1158

1159
  sr->sr_irq = irq;
Pekka Pessi's avatar
Pekka Pessi committed
1160

1161
  SR_STATUS1(sr, SIP_100_TRYING);
Pekka Pessi's avatar
Pekka Pessi committed
1162

1163 1164 1165
  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
1166

1167 1168
  sr->sr_response.msg = nta_incoming_create_response(irq, 0, NULL);
  sr->sr_response.sip = sip_object(sr->sr_response.msg);
1169

1170 1171 1172 1173 1174 1175
  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
1176
  }
1177
  /* Create handle if request does not fail */
1178 1179 1180 1181
  else if (initial && sr->sr_status < 300) {
    if ((nh = nua_stack_incoming_handle(nua, irq, sip, create_dialog)))
      sr->sr_owner = nh;
    else
1182
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
1183
  }
1184 1185

  if (sr->sr_status < 300 && sm->sm_preprocess && sm->sm_preprocess(sr)) {
1186
    if (sr->sr_status < 200)    /* Set response status if preprocess did not */
1187
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
1188
  }
1189

1190
  if (sr->sr_status < 300) {
1191
    if (sr->sr_target_refresh)
1192 1193 1194
      nua_dialog_uas_route(nh, nh->nh_ds, sip, 1); /* Set route and tags */
    nua_dialog_store_peer_info(nh, nh->nh_ds, sip);
  }
1195

1196
  if (sr->sr_status == 100 && method != sip_method_unknown &&
1197 1198 1199 1200 1201 1202
      !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);
  }
1203 1204

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

    if (sr) {
1209 1210
      *sr = *sr0;

1211
      if ((sr->sr_next = nh->nh_ds->ds_sr))
1212 1213 1214
	*(sr->sr_prev = sr->sr_next->sr_prev) = sr,
	  sr->sr_next->sr_prev = &sr->sr_next;
      else
1215
	*(sr->sr_prev = &nh->nh_ds->ds_sr) = sr;
1216 1217 1218 1219 1220
    }
    else {
      sr = sr0;
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
    }
Pekka Pessi's avatar
Pekka Pessi committed
1221
  }
1222

1223 1224 1225 1226 1227 1228 1229
  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());
    }
1230 1231
  }
  else {
1232 1233
    /* Note that this may change the sr->sr_status */
    nua_server_respond(sr, NULL);
1234 1235
  }

1236 1237
  if (nua_server_report(sr) == 0)
    return 0;
1238

1239 1240
  return 501;
}		 
1241

1242 1243
#undef nua_base_server_init
#undef nua_base_server_preprocess
1244

1245 1246
int nua_base_server_init(nua_server_request_t *sr)
{
1247 1248 1249
  return 0;
}

1250
int nua_base_server_preprocess(nua_server_request_t *sr)
1251
{
1252
  return 0;
1253 1254
}

1255
void nua_server_request_destroy(nua_server_request_t *sr)
1256
{
1257 1258
  if (sr == NULL)
    return;
1259

1260 1261
  if (sr->sr_irq)
    nta_incoming_destroy(sr->sr_irq), sr->sr_irq = NULL;
1262

1263 1264
  su_msg_destroy(sr->sr_signal);

1265 1266
  if (sr->sr_request.msg)
    msg_destroy(sr->sr_request.msg), sr->sr_request.msg = NULL;
1267

1268 1269
  if (sr->sr_response.msg)
    msg_destroy(sr->sr_response.msg), sr->sr_response.msg = NULL;
1270

1271 1272 1273 1274 1275 1276
  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);
  }
1277 1278
}

1279 1280 1281
/**@fn void nua_respond(nua_handle_t *nh, int status, char const *phrase, tag_type_t tag, tag_value_t value, ...);
 * 
 * Respond to a request with given status code and phrase.
1282
 *
1283 1284 1285 1286 1287 1288
 * The stack returns a SIP response message with given status code and
 * phrase to the client. The tagged parameter list can specify extra headers
 * to include with the response message and other stack parameters. The SIP
 * session or other protocol state associated with the handle is updated
 * accordingly (for instance, if an initial INVITE is responded with 200, a
 * SIP session is established.)
1289 1290
 *
 * When responding to an incoming INVITE request, the nua_respond() can be
1291
 * called without NUTAG_WITH() (or NUTAG_WITH_CURRENT() or
1292 1293 1294
 * NUTAG_WITH_SAVED()). Otherwise, NUTAG_WITH() will contain an indication
 * of the request being responded.
 *
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
 * @param nh              Pointer to operation handle
 * @param status          SIP response status code (see RFCs of SIP)
 * @param phrase          free text (default response phrase is used if NULL)
 * @param tag, value, ... List of tagged parameters
 *
 * @return 
 *    nothing
 *
 * @par Responses by Protocol Engine
 *
 * When nua protocol engine receives an incoming SIP request, it can either
 * respond to the request automatically or let application to respond to the
 * request. The automatic response is returned to the client if the request
 * fails syntax check, or the method, SIP extension or content negotiation
 * fails.
 *
 * When the @ref nua_handlingevents "request event" is delivered to the
 * application, the application should examine the @a status parameter. The
 * @a status parameter is 200 or greater if the request has been already
 * responded automatically by the stack.
Pekka Pessi's avatar
Pekka Pessi committed
1315 1316 1317 1318 1319 <