nua_stack.c 68.4 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE nua_stack.c
26
 * @brief Sofia-SIP User Agent Engine implementation
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Kai Vehmanen <Kai.Vehmanen@nokia.com>
30
 * @author Martti Mela <Martti Mela@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
31 32 33 34 35 36 37 38
 * @author Remeres Jacobs <Remeres.Jacobs@nokia.com>
 * @author Tat Chan <Tat.Chan@nokia.com>
 *
 * @date Created: Wed Feb 14 18:32:58 2001 ppessi
 */

#include "config.h"

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

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

#define SU_ROOT_MAGIC_T   struct nua_s
#define SU_MSG_ARG_T      struct event_s

50 51
#define NUA_SAVED_EVENT_T su_msg_t *

Pekka Pessi's avatar
Pekka Pessi committed
52 53
#define NTA_AGENT_MAGIC_T    struct nua_s
#define NTA_LEG_MAGIC_T      struct nua_handle_s
54
#define NTA_OUTGOING_MAGIC_T struct nua_client_request
Pekka Pessi's avatar
Pekka Pessi committed
55

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

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

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

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

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

78
#include <assert.h>
79

Pekka Pessi's avatar
Pekka Pessi committed
80 81 82 83 84 85 86 87 88 89
/* ========================================================================
 *
 *                       Protocol stack side
 *
 * ======================================================================== */

nua_handle_t *nh_create(nua_t *nua, tag_type_t t, tag_value_t v, ...);
static void nh_append(nua_t *nua, nua_handle_t *nh);
static void nh_remove(nua_t *nua, nua_handle_t *nh);

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

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

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

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

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

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

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

#define NUA_STACK_TIMER_INTERVAL (1000)

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

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

Pekka Pessi's avatar
Pekka Pessi committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  static int initialized_logs = 0;

  enter;

  if (!initialized_logs) {
    extern su_log_t tport_log[];
    extern su_log_t nta_log[];
    extern su_log_t nea_log[];
    extern su_log_t iptsec_log[];

    su_log_init(tport_log);
    su_log_init(nta_log);
    su_log_init(nea_log);
    su_log_init(iptsec_log);

    initialized_logs = 1;
  }

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

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

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

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

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

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

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

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

179 180 181 182 183 184 185 186 187 188
  dnh->nh_ds->ds_leg = nta_leg_tcreate(nua->nua_nta,
				       nua_stack_process_request, dnh,
				       NTATAG_NO_DIALOG(1),
				       TAG_END());

  if (nua->nua_nta == NULL ||
      dnh->nh_ds->ds_leg == NULL || 
      nta_agent_set_params(nua->nua_nta, NTATAG_UA(1), TAG_END()) < 0 ||
      nua_stack_init_transport(nua, nua->nua_args) < 0) {
    SU_DEBUG_1(("nua: initializing SIP stack failed\n"));
189
    return -1;
190
  }
191 192

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

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

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

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

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

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

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

215
static void nua_stack_shutdown(nua_t *);
216

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

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

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

231 232 233 234 235 236 237 238 239 240 241 242
int nua_stack_tevent(nua_t *nua, nua_handle_t *nh, msg_t *msg,
		     nua_event_t event, int status, char const *phrase,
		     tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  int retval;
  ta_start(ta, tag, value);
  retval = nua_stack_event(nua, nh, msg, event, status, phrase, ta_args(ta));
  ta_end(ta);
  return retval;
}

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

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

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

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

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

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

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

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

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

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

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

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

  return event;
}

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


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

344 345
  assert(tags);

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

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

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

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

372 373
  event = e->e_event;

Pekka Pessi's avatar
Pekka Pessi committed
374 375
  if (nua->nua_shutdown && !e->e_always) {
    /* Shutting down */
376
    nua_stack_event(nua, nh, NULL, event,
377
		    901, "Stack is going down",
378
		    NULL);
Pekka Pessi's avatar
Pekka Pessi committed
379
  }
380

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

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

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

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

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

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

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

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

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

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


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

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

  if (du == NULL)
    return 0;

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

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

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

    if (du_next == NULL)
      break;

    for (du = nh->nh_ds->ds_usage; du; du = du->du_next)
      if (du == du_next)
	break;

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

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

  return 1;
}


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

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 617 618 619 620 621
      if (sr->sr_respond) {
	SR_STATUS1(sr, SIP_410_GONE);
	sr->sr_usage = NULL;
	sr->sr_respond(sr, NULL);
	sr->sr_respond = NULL;
	busy++;
      }
	
      nua_server_request_destroy(sr);
Pekka Pessi's avatar
Pekka Pessi committed
622 623 624 625
    }

    busy += nh_call_pending(nh, 0);

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

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

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

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

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

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

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

  return nh;
}

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

703
void nua_stack_destroy_handle(nua_t *nua, nua_handle_t *nh, tagi_t const *tags)
704 705 706 707
{
  nh_call_pending(nh, 0);	/* Call pending operations with 0 */

  if (nh->nh_notifier)
708
    nua_stack_terminate(nua, nh, 0, NULL);
709 710 711 712 713 714 715 716 717 718 719

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

  nh_destroy(nua, nh);
}

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

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

739

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

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

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

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

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

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

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

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

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

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

  ta_start(ta, tag, value);

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

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

  ta_end(ta);

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

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

  nh->nh_init = 1;
801 802

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

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

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

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

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

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

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

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

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

  return nh;
}


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

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


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

  ta_end(ta);

  return retval;
}

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

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

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

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

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

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

989 990
/* ---------------------------------------------------------------------- */

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
/**@internal
 * Get remote contact header for @a irq */
static inline
sip_contact_t const *incoming_contact(nta_incoming_t *irq)
{
  sip_contact_t const *retval = NULL;
  msg_t *request;
  sip_t *sip;

  request = nta_incoming_getrequest(irq);
  sip = sip_object(request);
  if (sip)
    retval = sip->sip_contact;
  msg_destroy(request);

  return retval;
}

1009 1010
/**@internal
 * Create a response message.
Pekka Pessi's avatar
Pekka Pessi committed
1011
 */
1012 1013
msg_t *nh_make_response(nua_t *nua,
			nua_handle_t *nh,
Pekka Pessi's avatar
Pekka Pessi committed
1014 1015 1016 1017 1018 1019 1020
			nta_incoming_t *irq,
			int status, char const *phrase,
			tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  msg_t *msg = nta_msg_create(nua->nua_nta, 0);
  sip_t *sip = sip_object(msg);
1021
  msg_t *retval = NULL;
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

  sip_method_t method = nta_incoming_method(irq);

  int contact;

  if (method == sip_method_message)
    contact = NUA_PGET(nua, nh, win_messenger_enable);
  else
    contact = 
      /*
       * Insert Contact to everything but 
       * ACK, CANCEL, BYE, REGISTER, INFO, PRACK, or PUBLISH
       */
      method <= sip_method_invite ||
      method == sip_method_options ||
      (method >= sip_method_update && method != sip_method_prack);
Pekka Pessi's avatar
Pekka Pessi committed
1038 1039 1040 1041

  ta_start(ta, tag, value);

  if (!msg)
1042
    /* retval is NULL */;
1043 1044 1045
  else if (nta_incoming_complete_response(irq, msg,
					  status, phrase,
					  ta_tags(ta)) < 0)
1046
    msg_destroy(msg);
1047 1048
  else if (!sip->sip_supported && NH_PGET(nh, supported) &&
	   sip_add_dup(msg, sip, (sip_header_t *)NH_PGET(nh, supported)) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
1049
    msg_destroy(msg);
1050
  else if (!sip->sip_user_agent && NH_PGET(nh, user_agent) &&
1051 1052
	   sip_add_make(msg, sip, sip_user_agent_class, 
			NH_PGET(nh, user_agent)) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
1053
    msg_destroy(msg);
1054 1055
  else if (!sip->sip_organization && NH_PGET(nh, organization) &&
	   sip_add_dup(msg, sip, (sip_header_t *)NH_PGET(nh, organization)) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
1056
    msg_destroy(msg);
1057 1058
  else if (!sip->sip_allow && NH_PGET(nh, allow) &&
	   sip_add_dup(msg, sip, (sip_header_t*)NH_PGET(nh, allow)) < 0)
Pekka Pessi's avatar
Pekka Pessi committed
1059
    msg_destroy(msg);
1060
  else if (!sip->sip_allow_events && 
1061
	   (method == sip_method_publish || method == sip_method_subscribe) &&
1062 1063 1064
	   NH_PGET(nh, allow_events) &&
	   sip_add_dup(msg, sip, (sip_header_t*)NH_PGET(nh, allow_events)) < 0)
    msg_destroy(msg);
1065
  else if (!sip->sip_contact && status < 300 && contact &&
1066 1067
	   nua_registration_add_contact_to_response(nh, msg, sip, NULL, 
						    incoming_contact(irq)) < 0)
1068
    msg_destroy(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1069
  else
1070
    retval = msg;
1071

1072 1073 1074
  ta_end(ta);

  return retval;
Pekka Pessi's avatar
Pekka Pessi committed
1075 1076 1077
}


1078 1079
/* ======================================================================== */
/* Generic processing */
Pekka Pessi's avatar
Pekka Pessi committed
1080

1081 1082 1083 1084
int nua_stack_process_unknown(nua_t *nua,
			      nua_handle_t *nh,
			      nta_incoming_t *irq,
			      sip_t const *sip)
Pekka Pessi's avatar
Pekka Pessi committed
1085
{
1086
  return 501;
Pekka Pessi's avatar
Pekka Pessi committed
1087
}
Pekka Pessi's avatar
Pekka Pessi committed
1088

1089 1090 1091 1092
/* ======================================================================== */
/* Authentication */

/** @NUA_EVENT nua_r_authenticate
1093
 *
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
 * 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
1119 1120
 */

1121 1122 1123 1124 1125
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));
1126

1127 1128
  if (status > 0) {
    nua_client_request_t *cr = nh->nh_ds->ds_cr;
1129

1130 1131 1132 1133 1134 1135
    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",
1136
		      NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1137 1138
    }
  }
1139
  else if (status < 0) {
1140
    nua_stack_event(nua, nh, NULL, e, 900, "Cannot add credentials", NULL);
1141 1142
  }
  else {
1143
    nua_stack_event(nua, nh, NULL, e, 904, "No matching challenge", NULL);
1144
  }
Pekka Pessi's avatar
Pekka Pessi committed
1145 1146
}

1147

1148 1149 1150 1151
/* ======================================================================== */
/*
 * Process incoming requests
 */
1152

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
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;
  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);
1163 1164
  int status;

1165
  enter;
Pekka Pessi's avatar
Pekka Pessi committed
1166

1167
  nta_incoming_tag(irq, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1168

1169 1170
  if (nta_check_method(irq, sip, allow,
		       SIPTAG_SUPPORTED(supported),
1171
		       SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1172 1173 1174 1175 1176 1177 1178
		       TAG_END()))
    return 405;

  switch (sip->sip_request->rq_url->url_type) {
  case url_sip:
  case url_sips:
  case url_im:
1179 1180
  case url_pres:
  case url_tel:
Pekka Pessi's avatar
Pekka Pessi committed
1181 1182
    break;
  default:
1183
    nta_incoming_treply(irq, SIP_416_UNSUPPORTED_URI,
1184
			SIPTAG_ALLOW(allow),
1185
			SIPTAG_SUPPORTED(supported),
1186
			SIPTAG_USER_AGENT_STR(user_agent),
1187
			TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
1188 1189
  }

1190
  if (nta_check_required(irq, sip, supported,
1191
			 SIPTAG_ALLOW(allow),
1192
			 SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1193 1194 1195
			 TAG_END()))
    return 420;

1196
  if (nh == nua->nua_dhandle) {
Pekka Pessi's avatar
Pekka Pessi committed
1197 1198
    if (!sip->sip_to->a_tag)
      ;
1199
    else if (method == sip_method_message && NH_PGET(nh, win_messenger_enable))
Pekka Pessi's avatar
Pekka Pessi committed
1200 1201
      ;
    else {
1202
      nta_incoming_treply(irq, 481, "Initial transaction with a To tag",
1203
			  TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
      return 481;
    }
    nh = NULL;
  }

  if (sip->sip_timestamp)
    nta_incoming_treply(irq, SIP_100_TRYING, TAG_END());

  switch (method) {
  case sip_method_invite:
1214
    return nua_stack_process_invite(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1215 1216

  case sip_method_info:
1217
    if (nh) return nua_stack_process_info(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1218 1219 1220
    /*FALLTHROUGH*/

  case sip_method_update:
1221
    if (nh) return nua_stack_process_update(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1222 1223 1224
    /*FALLTHROUGH*/

  case sip_method_bye:
1225
    if (nh) return nua_stack_process_bye(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1226

1227 1228
    nta_incoming_treply(irq,
			481, "Call Does Not Exist",
1229
			SIPTAG_ALLOW(allow),
1230
			SIPTAG_SUPPORTED(supported),
1231
			SIPTAG_USER_AGENT_STR(user_agent),
Pekka Pessi's avatar
Pekka Pessi committed
1232 1233 1234 1235
			TAG_END());
    return 481;

  case sip_method_message:
1236
    return nua_stack_process_message(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1237 1238

  case sip_method_notify:
1239
    return nua_stack_process_notify(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1240

1241
  case sip_method_subscribe:
1242
    return nua_stack_process_subscribe(nua, nh, irq, sip);
1243

1244 1245 1246
  case sip_method_register:
    return nua_stack_process_register(nua, nh, irq, sip);

Pekka Pessi's avatar
Pekka Pessi committed
1247
  case sip_method_options:
1248 1249 1250 1251 1252
    /* Hook to outbound */
    status = nua_registration_process_request(nua->nua_registrations, irq, sip);
    if (status)
      return status;

1253
    return nua_stack_process_options(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1254 1255

  case sip_method_refer:
1256
    return nua_stack_process_refer(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1257 1258

  case sip_method_publish:
1259
    return nua_stack_process_publish(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1260 1261 1262 1263 1264 1265 1266 1267 1268

  case sip_method_ack:
  case sip_method_cancel:
    SU_DEBUG_1(("nua(%p): strange %s from <" URL_PRINT_FORMAT ">\n", nh,
		sip->sip_request->rq_method_name,
		URL_PRINT_ARGS(sip->sip_from->a_url)));
    /* Send nua_i_error ? */
    return 481;

1269 1270 1271
  case sip_method_unknown:
    return nua_stack_process_method(nua, nh, irq, sip);

Pekka Pessi's avatar
Pekka Pessi committed
1272
  default:
1273
    return nua_stack_process_unknown(nua, nh, irq, sip);
Pekka Pessi's avatar
Pekka Pessi committed
1274 1275 1276
  }
}

1277 1278 1279 1280 1281 1282 1283 1284
nua_server_request_t *nua_server_request(nua_t *nua,
					 nua_handle_t *nh,
					 nta_incoming_t *irq,
					 sip_t const *sip,
					 nua_server_request_t *sr,
					 size_t size,
					 nua_server_respond_f *respond,
					 int create_dialog)
Pekka Pessi's avatar
Pekka Pessi committed
1285
{
1286 1287 1288
  int initial = 1, final = 300;
  sip_method_t method;
  char const *name;
1289 1290 1291 1292
  assert(nua && irq && sip && sr);

  initial = nh == NULL || nh == nua->nua_dhandle;

1293 1294
  method = sip->sip_request->rq_method;
  name = sip->sip_request->rq_method_name;
1295 1296

  /* Create handle if request does not fail */
1297 1298 1299
  if (sr->sr_status >= 300)
    ;
  else if (initial) {
1300 1301
    if (!(nh = nua_stack_incoming_handle(nua, irq, sip, create_dialog)))
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
1302 1303 1304 1305 1306
  }
  else if (create_dialog) {
    nua_dialog_store_peer_info(nh, nh->nh_ds, sip);
    nua_dialog_uas_route(nh, nh->nh_ds, sip, 1);
  }
1307 1308 1309 1310

  if (nh == NULL)
    nh = nua->nua_dhandle;

1311 1312 1313 1314 1315 1316 1317
  if (sr->sr_status == 100 && method != sip_method_unknown &&
      !sip_is_allowed(NH_PGET(nh, appl_method), method, name))
    SR_STATUS1(sr, SIP_200_OK);

  /* INVITE server request is not finalized after 2XX response */
  final = method == sip_method_invite ? 300 : 200;

1318 1319 1320 1321 1322 1323 1324 1325 1326
  if (sr->sr_status < final) {
    nua_server_request_t *sr0 = sr;

    if (size < (sizeof *sr))
      size = sizeof *sr;

    sr = su_zalloc(nh->nh_home, size);

    if (sr) {
1327
      if ((sr->sr_next = nh->nh_ds->ds_sr))
1328 1329 1330
	*(sr->sr_prev = sr->sr_next->sr_prev) = sr,
	  sr->sr_next->sr_prev = &sr->sr_next;
      else
1331
	*(sr->sr_prev = &nh->nh_ds->ds_sr) = sr;
1332 1333 1334 1335 1336 1337
      SR_STATUS(sr, sr0->sr_status, sr0->sr_phrase);
    }
    else {
      sr = sr0;
      SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
    }
Pekka Pessi's avatar
Pekka Pessi committed
1338
  }
1339

1340
  sr->sr_owner = nh;
1341
  sr->sr_method = sip->sip_request->rq_method;
1342 1343
  sr->sr_respond = respond ? respond : nua_default_respond;
  sr->sr_request = nta_incoming_getrequest(irq);
1344