nua_session.c 77.8 KB
Newer Older
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) 2006 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_session.c
26
 * @brief SIP session handling
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Mar  8 16:17:27 EET 2006 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <assert.h>

#include <sofia-sip/string0.h>
#include <sofia-sip/sip_protos.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sip_util.h>

#define NTA_LEG_MAGIC_T      struct nua_handle_s
#define NTA_OUTGOING_MAGIC_T struct nua_handle_s
#define NTA_INCOMING_MAGIC_T struct nua_handle_s
#define NTA_RELIABLE_MAGIC_T struct nua_handle_s

#include "nua_stack.h"
#include <sofia-sip/soa.h>

55 56 57 58
#if !defined(random) && defined(_WIN32)
#define random rand
#endif

59 60 61 62
#ifndef SDP_H
typedef struct sdp_session_s sdp_session_t;
#endif

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/* ---------------------------------------------------------------------- */
/*  */
/** @enum nua_callstate

The states for SIP session established with INVITE.

Initially the call states follow the state of the INVITE transaction. If the
initial INVITE transaction fails, the call is terminated. The status codes
401 and 407 are an exception: if the client (on the left side in the diagram
below) receives them, it enters in #nua_callstate_authenticating state.

If a re-INVITE transaction fails, the result depends on the status code in
failure. The call can return to the ready state, be terminated immediately,
or be terminated gracefully. The proper action to take is determined with
sip_response_terminates_dialog().			   
							   
@par Session State Diagram				   
							   
@code							   
  			 +----------+			   
  			 |          |---------------------+
  			 |   Init   |                     |
  			 |          |----------+          |
  			 +----------+          |          |
  			  |        |           |          |
                 --/INVITE|        |INVITE/100 |          |
                          V        V           |          |
     		+----------+      +----------+ |          |
       +--------|          |      |          | |          |
       |  18X +-| Calling  |      | Received | |INVITE/   |
       |   /- | |          |      |          | |  /18X    |
       |      V +----------+      +----------+ V          |
       |   +----------+ |           |     |  +----------+ |
       |---|          | |2XX     -/ |  -/ |  |          | |
       |   | Proceed- | | /-     2XX|  18X|  |  Early   | |INVITE/
       |   |   ing    | |           |     +->|          | |  /200
       |   +----------+ V           V        +----------+ |
       |     |  +----------+      +----------+   | -/     |
       |  2XX|  |          |      |          |<--+ 2XX    |
       |   /-|  | Complet- |      | Complete |<-----------+
       |     +->|   ing    |      |          |------+
       |        +----------+      +----------+      |
       |                  |        |      |         |
       |401,407/     -/ACK|        |ACK/- |timeout/ |
       | /ACK             V        V      | /BYE    |
       |                 +----------+     |         |
       |                 |          |     |         |
       |              +--|  Ready   |     |         |
       |              |  |          |     |         |
       |              |  +----------+     |         |
       |              |       |           |         |
       |         BYE/ |       |-/BYE      |         |BYE/
       V         /200 |       V           |         |/200
  +----------+        |  +----------+     |         |
  |          |        |  |          |     |         |
  |Authentic-|        |  | Terminat-|<----+         |
  |  ating   |        |  |   ing    |               |
  +----------+        |  +----------+               |
                      |       |                     |
                      |       |[23456]XX/-          |
                      |       V                     |
                      |  +----------+               |
                      |  |          |               |
                      +->|Terminated|<--------------+
                         |          |
                         +----------+
                              | 
                              V
                         +----------+
        		 |          |
                         |   Init   |
			 |          |
@endcode		 +----------+
			      
*/			      
			      
139 140 141 142 143 144
/* ---------------------------------------------------------------------- */
/* Session event usage */

struct session_usage;

static char const *nua_session_usage_name(nua_dialog_usage_t const *du);
145
static int nua_session_usage_add(nua_handle_t *nh,
146 147
				 nua_dialog_state_t *ds,
				 nua_dialog_usage_t *du);
148
static void nua_session_usage_remove(nua_handle_t *nh,
149 150
				     nua_dialog_state_t *ds,
				     nua_dialog_usage_t *du);
151 152 153 154 155
static void nua_session_usage_refresh(nua_owner_t *,
				      nua_dialog_usage_t *,
				      sip_time_t now);
static int nua_session_usage_shutdown(nua_owner_t *,
				       nua_dialog_usage_t *);
156 157 158

static nua_usage_class const nua_session_usage[1] = {
  {
Pekka Pessi's avatar
Pekka Pessi committed
159
    0 /* sizeof (struct session_usage) */,
160 161 162 163
    sizeof nua_session_usage,
    nua_session_usage_add,
    nua_session_usage_remove,
    nua_session_usage_name,
164 165 166
    NULL,
    nua_session_usage_refresh,
    nua_session_usage_shutdown
167 168 169 170 171 172 173
  }};

static char const *nua_session_usage_name(nua_dialog_usage_t const *du)
{
  return "session";
}

174 175
static
int nua_session_usage_add(nua_handle_t *nh,
176 177 178 179 180 181 182 183 184
			   nua_dialog_state_t *ds,
			   nua_dialog_usage_t *du)
{
  if (ds->ds_has_session)
    return -1;
  ds->ds_has_session = 1;
  return 0;
}

185 186
static
void nua_session_usage_remove(nua_handle_t *nh,
187 188 189 190 191 192 193 194 195
			       nua_dialog_state_t *ds,
			       nua_dialog_usage_t *du)
{
  ds->ds_has_session = 0;
}

/* ======================================================================== */
/* INVITE and call (session) processing */

196 197
static int nua_stack_invite2(nua_t *, nua_handle_t *, nua_event_t e,
			     int restarted, tagi_t const *tags);
198 199 200
static int process_response_to_invite(nua_handle_t *nh,
				      nta_outgoing_t *orq,
				      sip_t const *sip);
201
static void
202 203 204 205
  session_timeout(nua_handle_t *nh, nua_dialog_usage_t *du, sip_time_t now);

static void restart_invite(nua_handle_t *nh, tagi_t *tags);

206 207 208 209 210 211 212
static int process_100rel(nua_handle_t *nh,
			  nta_outgoing_t *orq,
			  sip_t const *sip);

int nua_stack_prack(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		    tagi_t const *tags);

213 214 215 216 217 218
static int process_response_to_prack(nua_handle_t *nh,
				     nta_outgoing_t *orq,
				     sip_t const *sip);

static void nsession_destroy(nua_handle_t *nh);

219 220
static int  use_session_timer(nua_handle_t *nh, int uas, msg_t *msg, sip_t *);
static int  init_session_timer(nua_handle_t *nh, sip_t const *);
221 222
static void set_session_timer(nua_handle_t *nh);

223 224 225
static int nh_referral_check(nua_handle_t *nh, tagi_t const *tags);
static void nh_referral_respond(nua_handle_t *,
				int status, char const *phrase);
226

227
static void signal_call_state_change(nua_handle_t *nh,
228 229 230 231 232 233
				     int status, char const *phrase,
				     enum nua_callstate next_state,
				     char const *oa_recv,
				     char const *oa_sent);

static
234
int session_get_description(msg_t *msg,
235
			    sip_t const *sip,
236
			    char const **return_sdp,
237 238 239
			    size_t *return_len);

static
240 241
int session_include_description(soa_session_t *soa,
				msg_t *msg,
242 243 244
				sip_t *sip);

static
245 246
int session_make_description(su_home_t *home,
			     soa_session_t *soa,
247 248 249 250 251 252 253 254 255 256 257
			     sip_content_disposition_t **return_cd,
			     sip_content_type_t **return_ct,
			     sip_payload_t **return_pl);

static
int session_process_response(nua_handle_t *nh,
			     struct nua_client_request *cr,
			     nta_outgoing_t *orq,
			     sip_t const *sip,
			     char const **return_received);

258 259 260
int
nua_stack_invite(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		 tagi_t const *tags)
261 262 263 264 265 266 267 268 269 270
{
  nua_session_state_t *ss = nh->nh_ss;
  struct nua_client_request *cr = ss->ss_crequest;
  char const *what;

  if (nh_is_special(nh))
    what = "Invalid handle for INVITE";
  else if (cr->cr_orq) {
    what = "INVITE request already in progress";
  }
271
  else if (nh_referral_check(nh, tags) < 0) {
272 273
    what = "Invalid referral";
  }
274
  else if (nua_stack_init_handle(nua, nh, nh_has_invite, NULL,
275 276 277 278
				 TAG_NEXT(tags)) < 0) {
    what = "Handle initialization failed";
  }
  else
279
    return nua_stack_invite2(nua, nh, e, 0, tags);
280

281
  UA_EVENT2(e, 900, what);
282

283
  signal_call_state_change(nh, 900, what, nua_callstate_init, 0, 0);
284 285 286 287 288

  return e;
}

static int
289 290 291
nua_stack_invite2(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		  int restarted,
		  tagi_t const *tags)
292 293 294 295 296 297 298
{
  nua_session_state_t *ss = nh->nh_ss;
  struct nua_client_request *cr = ss->ss_crequest;
  nua_dialog_usage_t *du;
  int offer_sent = 0;

  msg_t *msg = NULL;
299
  sip_t *sip = NULL;
300 301 302 303 304 305 306

  char const *what;

  if (ss->ss_state == nua_callstate_terminated)
    ss->ss_state = nua_callstate_init;

  du = nua_dialog_usage_add(nh, nh->nh_ds, nua_session_usage, NULL);
307
  what = nua_internal_error;		/* Internal error */
308

309 310 311 312 313 314 315 316 317 318 319 320 321
  if (du) {
    if (restarted && !cr->cr_msg)
      cr->cr_msg = msg_dup(du->du_msg);

    msg = nua_creq_msg(nua, nh, cr, restarted,
		       SIP_METHOD_INVITE,
		       NUTAG_USE_DIALOG(1),
		       NUTAG_ADD_CONTACT(1),
		       TAG_NEXT(tags));
    if (!restarted) 
      du->du_msg = msg_dup(msg);
  }

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  sip = sip_object(msg);

  if (du && sip && nh->nh_soa) {
    soa_init_offer_answer(nh->nh_soa);

    if (sip->sip_payload)
      offer_sent = 0;
    else if (soa_generate_offer(nh->nh_soa, 0, NULL) < 0)
      offer_sent = -1;
    else
      offer_sent = 1;
  }

  assert(cr->cr_orq == NULL);

  if (du && sip && offer_sent >= 0) {
338
    sip_time_t invite_timeout = NH_PGET(nh, invite_timeout);
339
    if (invite_timeout == 0)
340
      invite_timeout = UINT_MAX;
341 342 343
    /* Cancel if we don't get response within timeout*/
    nua_dialog_usage_set_expires(du, invite_timeout);
    nua_dialog_usage_set_refresh(du, 0);
344 345

    /* Add session timer headers */
346
    use_session_timer(nh, 0, msg, sip);
347 348 349 350 351 352 353 354

    ss->ss_100rel = NH_PGET(nh, early_media);
    ss->ss_precondition = sip_has_feature(sip->sip_require, "precondition");

    if (ss->ss_precondition)
      ss->ss_update_needed = ss->ss_100rel = 1;

    if (offer_sent > 0 &&
355
	session_include_description(nh->nh_soa, msg, sip) < 0)
356
      sip = NULL, what = "Internal media error";
357

358
    if (sip && nh->nh_soa &&
359
	NH_PGET(nh, media_features) && !nua_dialog_is_established(nh->nh_ds) &&
360 361 362 363 364 365
	!sip->sip_accept_contact && !sip->sip_reject_contact) {
      sip_accept_contact_t ac[1];
      sip_accept_contact_init(ac);

      ac->cp_params = (msg_param_t *)
	soa_media_features(nh->nh_soa, 1, msg_home(msg));
366

367 368 369 370 371 372
      if (ac->cp_params) {
	msg_header_replace_param(msg_home(msg), ac->cp_common, "explicit");
	sip_add_dup(msg, sip, (sip_header_t *)ac);
      }
    }

373 374 375 376
    if (sip && nh->nh_auth) {
      if (auc_authorize(&nh->nh_auth, msg, sip) < 0)
	sip = NULL, what = "Internal authentication error";
    }
377 378 379 380 381 382 383 384 385 386
    if (sip)
      cr->cr_orq = nta_outgoing_mcreate(nua->nua_nta,
					process_response_to_invite, nh, NULL,
					msg,
					NTATAG_REL100(ss->ss_100rel),
					SIPTAG_END(), TAG_NEXT(tags));

    if (cr->cr_orq) {
      cr->cr_offer_sent = offer_sent;
      cr->cr_usage = du;
Pekka Pessi's avatar
Pekka Pessi committed
387
      du->du_refresh = 0;
388 389 390 391 392 393 394 395
      signal_call_state_change(nh, 0, "INVITE sent",
			       nua_callstate_calling, 0,
			       offer_sent ? "offer" : 0);
      return cr->cr_event = e;
    }
  }

  msg_destroy(msg);
396
  if (du && !du->du_ready)
397 398
    nua_dialog_usage_remove(nh, nh->nh_ds, du);

399 400
  UA_EVENT2(e, 900, what);
  signal_call_state_change(nh, 900, what, nua_callstate_init, 0, 0);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

  return e;
}

static int process_response_to_invite(nua_handle_t *nh,
				      nta_outgoing_t *orq,
				      sip_t const *sip)
{
  nua_t *nua = nh->nh_nua;
  nua_session_state_t *ss = nh->nh_ss;
  nua_client_request_t *cr = ss->ss_crequest;
  nua_dialog_usage_t *du = cr->cr_usage;
  int status = sip->sip_status->st_status;
  char const *phrase = sip->sip_status->st_phrase;
  int terminated = 0;
  int gracefully = 1;
  char const *received = NULL;

  assert(du);

#if HAVE_SOFIA_SMIME
  if (status < 300) {
    int sm_status;
    msg_t *response;

    /* decrypt sdp payload if it's S/MIME */
    /* XXX msg had a problem!!?? */
    response = nta_outgoing_getresponse(orq);

    sm_status = sm_decode_message(nua->sm, response, sip);

    switch (sm_status) {
    case SM_SMIME_DISABLED:
    case SM_ERROR:
      status = 493, phrase = "Undecipherable";
      break;
    case SM_SUCCESS:
      break;
    default:
      break;
    }
  }
#endif

445 446 447
  if (status < 300 && !ss->ss_usage) /* Usage is now established */
    ss->ss_usage = du;

448 449 450 451
  if (status >= 300) {
    if (sip->sip_retry_after)
      gracefully = 0;

452
    terminated = sip_response_terminates_dialog(status, sip_method_invite,
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
						&gracefully);

    if (!terminated) {
      if (nua_creq_check_restart(nh, cr, orq, sip, restart_invite))
	return 0;

      if (nh->nh_ss->ss_state < nua_callstate_ready)
	terminated = 1;
    }
  }
  else if (status >= 200) {

    du->du_ready = 1;
    if (!ss->ss_usage)
      ss->ss_usage = du;
    cr->cr_usage = NULL;
469

470 471
    /* XXX - check remote tag, handle forks */
    /* Set route, contact, nh_ds->ds_remote_tag */
472
    nua_dialog_uac_route(nh, nh->nh_ds, sip, 1);
473
    nua_dialog_store_peer_info(nh, nh->nh_ds, sip);
474

475
    init_session_timer(nh, sip);
476 477 478 479 480 481 482

    set_session_timer(nh);

    /* signal_call_state_change */
    if (session_process_response(nh, cr, orq, sip, &received) >= 0) {
      ss->ss_ack_needed = received ? received : "";

483
      if (NH_PGET(nh, auto_ack) ||
484
	  /* Auto-ACK response to re-INVITE unless auto_ack is set to 0 */
485
	  (ss->ss_state == nua_callstate_ready &&
486 487 488
	   !NH_PISSET(nh, auto_ack)))
	nua_stack_ack(nua, nh, nua_r_ack, NULL);
      else
489
	signal_call_state_change(nh, status, phrase,
490 491 492 493 494
				 nua_callstate_completing, received, 0);
      nh_referral_respond(nh, SIP_200_OK);
      return 0;
    }

495
    status = 500, phrase = "Malformed Session in Response";
496 497 498 499 500 501 502 503 504 505 506 507 508 509

    nua_stack_ack(nua, nh, nua_r_ack, NULL);
    gracefully = 1;
  }
  else if (sip->sip_rseq) {
    /* Reliable provisional response */
    nh_referral_respond(nh, status, phrase);

    return process_100rel(nh, orq, sip); /* signal_call_state_change */
  }
  else {
    /* Provisional response */
    nh_referral_respond(nh, status, phrase);
    session_process_response(nh, cr, orq, sip, &received);
510
    signal_call_state_change(nh, status, phrase,
511 512 513 514 515 516 517 518 519 520
			     nua_callstate_proceeding, received, 0);
    return 0;
  }

  cr->cr_usage = NULL;

  nh_referral_respond(nh, status, phrase);
  nua_stack_process_response(nh, cr, orq, sip, TAG_END());

  if (terminated)
521
    signal_call_state_change(nh, status, phrase,
522 523 524 525 526 527 528 529 530
			     nua_callstate_terminated, 0, 0);

  if (terminated < 0) {
    nua_dialog_terminated(nh, nh->nh_ds, status, phrase);
  }
  else if (terminated > 0) {
    nua_dialog_usage_remove(nh, nh->nh_ds, du);
  }
  else if (gracefully) {
531
    char *reason =
532 533
      su_sprintf(NULL, "SIP;cause=%u;text=\"%s\"", 
		 status > 699 ? 500 : status, phrase);
534

535
    signal_call_state_change(nh, status, phrase,
536 537
			     nua_callstate_terminating, 0, 0);

538 539 540
    nua_stack_post_signal(nh, nua_r_bye,
			  SIPTAG_REASON_STR(reason),
			  TAG_END());
541 542 543 544 545 546 547

    su_free(NULL, reason);
  }

  return 0;
}

548 549
int nua_stack_ack(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		  tagi_t const *tags)
550 551 552 553 554 555 556 557 558 559 560
{
  nua_session_state_t *ss = nh->nh_ss;
  struct nua_client_request *cr = ss->ss_crequest;
  nta_outgoing_t *ack = NULL;
  msg_t *msg;
  sip_t *sip;
  int status = 200;
  char const *phrase = "OK", *reason = NULL, *sent = NULL;
  char const *received = ss->ss_ack_needed;

  if (!ss->ss_ack_needed)
561
    return UA_EVENT2(nua_i_error, 900, "No response to ACK");
562 563 564 565 566 567 568 569 570 571

  ss->ss_ack_needed = 0;

  if (!received[0])
    received = NULL;

  if (tags) {
    nua_stack_set_params(nua, nh, nua_r_ack, tags);
  }

572
  msg = nua_creq_msg(nua, nh, cr, 0,
573 574 575
		     SIP_METHOD_ACK,
		     /* NUTAG_COPY(0), */
		     TAG_NEXT(tags));
576 577 578 579 580 581 582 583
  sip = sip_object(msg);

  if (sip && nh->nh_soa) {
    if (tags)
      soa_set_params(nh->nh_soa, TAG_NEXT(tags));

    if (cr->cr_offer_recv && !cr->cr_answer_sent) {
      if (soa_generate_answer(nh->nh_soa, NULL) < 0 ||
584
	  session_include_description(nh->nh_soa, msg, sip) < 0) {
585
	reason = soa_error_as_sip_reason(nh->nh_soa);
586
	status = 900, phrase = "Internal media error";
587 588 589 590 591 592 593 594 595 596 597
	reason = "SIP;cause=500;text=\"Internal media error\"";
      }
      else {
	cr->cr_answer_sent = 1;
	soa_activate(nh->nh_soa, NULL);

	/* signal that O/A round is complete */
	sent = "answer";
      }
    }

598
    if (!reason &&
599 600 601
	/* ss->ss_offer_sent && !ss->ss_answer_recv */
	!soa_is_complete(nh->nh_soa)) {
      /* No SDP answer in 2XX response -> terminate call */
602 603
      status = 988, phrase = "Incomplete offer/answer";
      reason = "SIP;cause=488;text=\"Incomplete offer/answer\"";
604 605 606 607
    }
  }

  if (sip)
608
    ack = nta_outgoing_mcreate(nua->nua_nta, NULL, NULL, NULL, msg,
609 610 611 612
			       SIPTAG_END(), TAG_NEXT(tags));

  if (!ack) {
    if (!reason) {
613
      status = 900, phrase = "Cannot send ACK";
614 615 616 617 618 619 620 621 622
      reason = "SIP;cause=500;text=\"Internal Error\"";
    }
    msg_destroy(msg);
  }

  nua_creq_deinit(cr, NULL);	/* Destroy INVITE transaction */
  nta_outgoing_destroy(ack);	/* TR engine keeps this around for T2 */

  if (status < 300) {
623
    signal_call_state_change(nh, status, phrase, nua_callstate_ready,
624 625 626
			     received, sent);
  }
  else {
627 628 629 630 631
    signal_call_state_change(nh, status, phrase, nua_callstate_terminating,
			     0, 0);
    nua_stack_post_signal(nh, nua_r_bye,
			  SIPTAG_REASON_STR(reason),
			  TAG_END());
632 633 634 635 636
  }

  return 0;
}

637
/* Process reliable provisional response */
638
static int
639 640 641 642 643 644 645
process_100rel(nua_handle_t *nh,
	       nta_outgoing_t *orq,
	       sip_t const *sip)
{
  struct nua_session_state *ss = nh->nh_ss;
  struct nua_client_request *cr_invite = ss->ss_crequest;
  struct nua_client_request *cr_prack = nh->nh_cr;
646 647 648 649
  
  sip_rseq_t *rseq;
  char const *recv = NULL;
  int status; char const *phrase;
650 651

  if (cr_prack->cr_orq) {
652
    /* XXX - better luck next time */
653
    SU_DEBUG_3(("nua(%p): cannot send PRACK because %s is pending\n", nh,
654
		nta_outgoing_method_name(cr_prack->cr_orq)));
655
    return 0; /* Wait until this response is re-transmitted */
656 657 658
  }

  if (!nua_dialog_is_established(nh->nh_ds)) {
659
    /* Establish early dialog */
660 661
    nua_dialog_uac_route(nh, nh->nh_ds, sip, 1);
    nua_dialog_store_peer_info(nh, nh->nh_ds, sip);
662 663
    
    /* Tag the INVITE request */
664
    cr_invite->cr_orq =
665 666 667 668 669
      nta_outgoing_tagged(orq, process_response_to_invite, nh,
			  sip->sip_to->a_tag, sip->sip_rseq);
    nta_outgoing_destroy(orq);
    orq = cr_invite->cr_orq;
  }
670 671 672 673 674
  
  assert(sip);

  status = sip->sip_status->st_status, phrase = sip->sip_status->st_phrase;
  rseq = sip->sip_rseq;
675

676 677
  if (!rseq) {
    SU_DEBUG_5(("nua(%p): 100rel missing RSeq\n", nh));
678
  }
679 680 681 682 683 684
  else if (rseq->rs_response <= nta_outgoing_rseq(orq)) {
    SU_DEBUG_5(("nua(%p): 100rel bad RSeq %u (got %u)\n", nh, 
		(unsigned)rseq->rs_response,
		nta_outgoing_rseq(orq)));
    /* XXX - send nua_r_invite event or not? */
    return 0;
685
  }
686 687 688
  else if (nta_outgoing_setrseq(orq, rseq->rs_response) < 0) {
    SU_DEBUG_1(("nua(%p): cannot set RSeq %u\n", nh, 
		(unsigned)rseq->rs_response));
689
  }
690 691 692 693 694
  else if (session_process_response(nh, cr_invite, orq, sip, &recv) < 0) {
    assert(nh->nh_soa);
    status = soa_error_as_sip_response(nh->nh_soa, &phrase);
    nua_stack_event(nh->nh_nua, nh, NULL,
		    nua_i_media_error, status, phrase, TAG_END());
695
  }
696
  /* Here we could let application PRACK and just send state event */
697
  else {
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    sip_rack_t rack[1];
    tagi_t tags[] = {
      { TAG_SKIP(nua_stack_prack) }, /* this is autoprack */
      { NUTAG_STATUS(status), },
      { NUTAG_PHRASE(phrase), },
      { NUTAG_PHRASE(recv), },
      { SIPTAG_RACK(rack) }, 
      { TAG_END() }
    };

    sip_rack_init(rack);

    rack->ra_response    = sip->sip_rseq->rs_response;
    rack->ra_cseq        = sip->sip_cseq->cs_seq;
    rack->ra_method      = sip->sip_cseq->cs_method;
    rack->ra_method_name = sip->sip_cseq->cs_method_name;

    nua_stack_prack(nh->nh_nua, nh, nua_r_prack, tags);

    return 0;
718 719
  }

720 721 722
  /* XXX - CANCEL INVITE or BYE this session? */
  /* Because we don't do forking very well we just cancel INVITE */
  nua_stack_cancel(nh->nh_nua, nh, nua_r_cancel, NULL);
723 724 725 726

  return 0;
}

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
int nua_stack_prack(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		    tagi_t const *tags)
{
  struct nua_session_state *ss = nh->nh_ss;
  struct nua_client_request *cr = nh->nh_cr;
  struct nua_client_request *cri = ss->ss_crequest;
  msg_t *msg;
  sip_t *sip;
  int offer_sent_in_prack = 0, answer_sent_in_prack = 0;

  int autoprack =		/* XXX - should have common indication */
    tags && tags->t_tag == tag_skip && 
    tags->t_value == (tag_value_t)nua_stack_prack;
  int status = 0; char const *phrase = "PRACK sent";
  char const *recv = NULL, *sent = NULL;

  if (autoprack) {
    status = (int)tags[1].t_value; 
    phrase = (char const *)tags[2].t_value;
    recv = (char const *)tags[3].t_value;
    tags += 4;
  }

  if (!nh_has_session(nh) || !cri->cr_orq || !nta_outgoing_rseq(cri->cr_orq))
    return UA_EVENT2(e, 900, "Nothing to PRACK");
  else if (cr->cr_orq)
    return UA_EVENT2(e, 900, "Request already in progress");

  nua_stack_init_handle(nua, nh, nh_has_nothing, NULL, TAG_NEXT(tags));

  msg = nua_creq_msg(nua, nh, cr, cr->cr_retry_count,
		     SIP_METHOD_PRACK,
		     NUTAG_USE_DIALOG(1),
		     NUTAG_ADD_CONTACT(1),
		     TAG_NEXT(tags));

  sip = sip_object(msg);

  if (sip) {
    if (nh->nh_soa == NULL)
      /* It is up to application to handle SDP */;
    else if (sip->sip_payload)
      /* XXX - we should just do MIME in session_include_description() */;
    else if (cri->cr_offer_recv && !cri->cr_answer_sent) {

      if (soa_generate_answer(nh->nh_soa, NULL) < 0 ||
	  session_include_description(nh->nh_soa, msg, sip) < 0) {

	status = soa_error_as_sip_response(nh->nh_soa, &phrase);
	SU_DEBUG_3(("nua(%p): PRACK answer: %d %s\n", nh, status, phrase));
	nua_stack_event(nh->nh_nua, nh, NULL,
			nua_i_media_error, status, phrase, TAG_END());

	goto error;
      }
      else {
	answer_sent_in_prack = 1, sent = "answer";
	soa_activate(nh->nh_soa, NULL);
      }
    }
    /* When 100rel response status was 183 fake support for preconditions */
    else if (autoprack && status == 183 && ss->ss_precondition) {

      if (soa_generate_offer(nh->nh_soa, 0, NULL) < 0 ||
	  session_include_description(nh->nh_soa, msg, sip) < 0) {

	status = soa_error_as_sip_response(nh->nh_soa, &phrase);
	SU_DEBUG_3(("nua(%p): PRACK offer: %d %s\n", nh, status, phrase));
	nua_stack_event(nh->nh_nua, nh, NULL,
			nua_i_media_error, status, phrase, TAG_END());
	goto error;
      }
      else {
	offer_sent_in_prack = 1, sent = "offer";
      }
    }

    if (nh->nh_auth) {
      if (auc_authorize(&nh->nh_auth, msg, sip) < 0)
	/* xyzzy */;
    }

    cr->cr_orq = nta_outgoing_mcreate(nua->nua_nta,
				      process_response_to_prack, nh, NULL,
				      msg,
				      SIPTAG_END(), TAG_NEXT(tags));
    if (cr->cr_orq) {
      cr->cr_event = nua_r_prack;

      if (answer_sent_in_prack)
	cri->cr_answer_sent = 1;
      else if (offer_sent_in_prack)
	cr->cr_offer_sent = 1;

      if (autoprack) 
	signal_call_state_change(nh, status, phrase,
				 nua_callstate_proceeding, recv, sent);
      else
	signal_call_state_change(nh, 0, "PRACK sent",
				 nua_callstate_proceeding, NULL, sent);
	

      return cr->cr_event = e;
    }
  }

 error:
  msg_destroy(msg);
  return UA_EVENT1(e, NUA_INTERNAL_ERROR);
}

void restart_prack(nua_handle_t *nh, tagi_t *tags)
{
  nua_creq_restart(nh, nh->nh_cr, process_response_to_prack, tags);
}


844
static int
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
process_response_to_prack(nua_handle_t *nh,
			  nta_outgoing_t *orq,
			  sip_t const *sip)
{
  struct nua_client_request *cr = nh->nh_cr;
  int status;
  char const *phrase = "OK", *reason = NULL, *recv = NULL;

  if (sip)
    status = sip->sip_status->st_status, phrase = sip->sip_status->st_phrase;
  else
    status = 408, phrase = sip_408_Request_timeout;

  SU_DEBUG_5(("nua: process_response_to_prack: %u %s\n", status, phrase));

  if (nua_creq_check_restart(nh, cr, orq, sip, restart_prack))
    return 0;

  if (status < 200)
    return 0;
865

866 867
  if (status < 300) {
    if (session_process_response(nh, cr, orq, sip, &recv) < 0) {
868
      status = 900, phrase = "Malformed Session in Response";
869 870 871 872 873 874
      reason = "SIP;status=400;phrase=\"Malformed Session in Response\"";
    }
  }
  else
    nua_stack_process_response(nh, cr, orq, sip, TAG_END());

875 876 877
  if (recv)
    signal_call_state_change(nh, status, phrase,
			     nua_callstate_proceeding, recv, NULL);
878 879 880 881 882 883 884

  if (status < 300 && nh->nh_ss->ss_update_needed)
    nua_stack_update(nh->nh_nua, nh, nua_r_update, NULL);

  return 0;
}

885 886 887 888
/** Refresh session usage */
static void nua_session_usage_refresh(nua_handle_t *nh,
				      nua_dialog_usage_t *du,
				      sip_time_t now)
889
{
890 891 892
  tagi_t const timer_tags[2] = {
    { SIPTAG_SUBJECT_STR("Session refresh") }, 
    { TAG_END() }
893
  };
894 895 896 897 898 899 900
  tagi_t const refresh_tags[2] = {
    { SIPTAG_SUBJECT_STR("Dialog refresh") }, 
    { TAG_END() }
  };
  nua_session_state_t const *ss = nh->nh_ss;
  nua_client_request_t const *cri = ss->ss_crequest, *cro = nh->nh_cr;
  nua_server_request_t const *sri = ss->ss_srequest;
901

902 903 904 905
  if (cri->cr_msg)	/* INVITE in progress or being authenticated */
    return;
  else if (ss->ss_state >= nua_callstate_terminating)
    return;
906

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
  if (!ss->ss_refresher) {
    if (now >= du->du_expires)
      session_timeout(nh, du, now);
    else
      /* Refreshing contact & route set */
      nua_stack_invite2(nh->nh_nua, nh, nua_r_invite, 1, refresh_tags);
  }
  else if (NH_PGET(nh, update_refresh)) {
    if (!cro->cr_orq)
      nua_stack_update(nh->nh_nua, nh, nua_r_update, timer_tags);
    else
      nua_dialog_usage_refresh_range(du, 5, 15);
  }
  else {
    if (!cri->cr_orq && !sri->sr_irq)
      nua_stack_invite2(nh->nh_nua, nh, nua_r_invite, 1, timer_tags);
  }
924 925
}

926 927
static
char const reason_timeout[] = "SIP;cause=408;text=\"Session timeout\"";
928

929
static void
930 931 932 933 934
session_timeout(nua_handle_t *nh, nua_dialog_usage_t *du, sip_time_t now)
{
  if (now > 1) {
    signal_call_state_change(nh, 408, "Session Timeout",
			     nua_callstate_terminating, NULL, NULL);
935 936 937
    nua_stack_post_signal(nh, nua_r_bye,
			  SIPTAG_REASON_STR(reason_timeout),
			  TAG_END());
938 939 940
  }
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 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 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
/** Terminate usage/dialog/handle/agent gracefully */
static int nua_session_usage_shutdown(nua_handle_t *nh,
				       nua_dialog_usage_t *du)
{
  nua_session_state_t *ss = nh->nh_ss;
  nua_client_request_t *cr;
  nua_server_request_t *sr;
  int status;

  /* Zap client-side invite transaction */
  if (ss->ss_crequest->cr_orq) {
    cr = ss->ss_crequest;
    status = nta_outgoing_status(cr->cr_orq);

    if (status < 200) 
      nta_outgoing_tcancel(cr->cr_orq, NULL, NULL, TAG_END());

    if (ss->ss_ack_needed) {
      msg_t *ack = nua_creq_msg(nh->nh_nua, nh, cr, 0,
				SIP_METHOD_ACK,
				TAG_END());
      nta_outgoing_mcreate(nh->nh_nua->nua_nta, NULL, NULL, NULL, 
			   ack, TAG_END());
    }

    nua_creq_deinit(cr, NULL);
  }

  sr = ss->ss_srequest;

  /* Zap server-side invite transaction */
  if (sr->sr_irq) {
    status = nta_incoming_status(sr->sr_irq);
    if (status < 200)
      nta_incoming_treply(sr->sr_irq,
			  SIP_480_TEMPORARILY_UNAVAILABLE,
			  TAG_END());
    nta_incoming_destroy(sr->sr_irq);
    memset(sr, 0, sizeof *sr);
  }

  switch (ss->ss_state) {
  case nua_callstate_completing:
  case nua_callstate_ready:
  case nua_callstate_completed:
    {
      msg_t *bye;

      cr = nh->nh_cr;
      nua_creq_deinit(cr, NULL);
      bye = nua_creq_msg(nh->nh_nua, nh, nh->nh_cr, 0, 
			 SIP_METHOD_BYE,
			 TAG_END());
      cr->cr_orq = nta_outgoing_mcreate(nh->nh_nua->nua_nta,
					NULL, NULL, NULL,
					bye,
					TAG_END());
      if (cr->cr_orq)		/* Wait for BYE to complete? */
	nua_creq_deinit(cr, NULL);
    }
  }

  nua_dialog_usage_remove(nh, nh->nh_ds, du);

  return 0;
}

1008
/** Restart invite (e.g., after 302 or 407) */
1009
void
1010 1011
restart_invite(nua_handle_t *nh, tagi_t *tags)
{
1012
  nua_stack_invite2(nh->nh_nua, nh, nua_r_invite, 1, tags);
1013 1014 1015 1016 1017 1018 1019 1020
}

static int process_response_to_cancel(nua_handle_t *nh,
				      nta_outgoing_t *orq,
				      sip_t const *sip);

/* CANCEL */
int
1021 1022
nua_stack_cancel(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		 tagi_t const *tags)
1023 1024 1025 1026
{
  nua_session_state_t *ss = nh->nh_ss;
  nua_client_request_t *cri = ss->ss_crequest;
  nua_client_request_t *crc = nh->nh_cr;
1027

1028 1029 1030
  if (tags)
    nua_stack_set_params(nua, nh, nua_r_cancel, tags);

1031
  if (nh && cri->cr_orq && cri->cr_usage) {
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    nta_outgoing_t *orq;

    /* nh_referral_respond(nh, SIP_487_REQUEST_TERMINATED); */

    if (e)
      orq = nta_outgoing_tcancel(cri->cr_orq, process_response_to_cancel, nh,
				 TAG_NEXT(tags));
    else
      orq = nta_outgoing_tcancel(cri->cr_orq, NULL, NULL, TAG_NEXT(tags));

    if (orq == NULL)
1043 1044
      return nua_stack_event(nua, nh, NULL, e, 400, "Internal error",
			     TAG_END());
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

    if (e && crc->cr_orq == NULL)
      crc->cr_orq = orq, crc->cr_event = e;

    return 0;
  }

  return UA_EVENT2(e, 481, "No transaction to CANCEL");
}

static int process_response_to_cancel(nua_handle_t *nh,
				      nta_outgoing_t *orq,
				      sip_t const *sip)
{
  return nua_stack_process_response(nh, nh->nh_cr, orq, sip, TAG_END());
}

1062 1063 1064
/* ---------------------------------------------------------------------- */
/* UAS side of INVITE */

1065
static void respond_to_invite(nua_t *nua, nua_handle_t *nh,
1066
			      int status, char const *phrase,
1067 1068
			      tagi_t const *tags);

1069
static int
1070 1071
  process_invite1(nua_t *, nua_handle_t**, nta_incoming_t *, msg_t *, sip_t *),
  process_invite2(nua_t *, nua_handle_t *, nta_incoming_t *, sip_t *),
1072
  process_prack(nua_handle_t *, nta_reliable_t *, nta_incoming_t *,
1073 1074 1075 1076 1077
		sip_t const *),
  process_ack_or_cancel(nua_handle_t *, nta_incoming_t *, sip_t const *),
  process_ack(nua_handle_t *, nta_incoming_t *, sip_t const *),
  process_cancel(nua_handle_t *, nta_incoming_t *, sip_t const *),
  process_timeout(nua_handle_t *, nta_incoming_t *);
1078

Pekka Pessi's avatar
Pekka Pessi committed
1079
/** @internal Process incoming INVITE. */
1080 1081 1082 1083 1084 1085
int nua_stack_process_invite(nua_t *nua,
			     nua_handle_t *nh0,
			     nta_incoming_t *irq,
			     sip_t const *sip)
{
  nua_handle_t *nh = nh0;
1086
  msg_t *msg = nta_incoming_getrequest(irq);
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  int status;

  status = process_invite1(nua, &nh, irq, msg, (sip_t *)sip);

  if (status) {
    msg_destroy(msg);
    if (nh != nh0)
      nh_destroy(nua, nh);
    return status;
  }

  return process_invite2(nua, nh, irq, (sip_t *)sip);
}

Pekka Pessi's avatar
Pekka Pessi committed
1101
/** @internal Preprocess incoming invite - sure we have a valid request. */
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
static
int process_invite1(nua_t *nua,
		    nua_handle_t **return_nh,
		    nta_incoming_t *irq,
		    msg_t *msg,
		    sip_t *sip)
{
  nua_handle_t *nh = *return_nh;
  nua_handle_t *dnh = nua->nua_dhandle, *nh0 = nh ? nh : dnh;
  nua_server_request_t *sr;
  int have_sdp;
  char const *sdp;
  size_t len;
  sip_user_agent_t const *user_agent = NH_PGET(nh0, user_agent);

1117
#if HAVE_SOFIA_SMIME
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
  int sm_status;

  sm_status = sm_decode_message(nua->sm, msg, sip);
  switch(sm_status) {
  case SM_SMIME_DISABLED:
  case SM_ERROR:
    nta_incoming_treply(irq, 493, "Undecipherable", TAG_END());
    return 493;

  case SM_SUCCESS:
    break;
  default:
    break;
  }
#endif

  if (nh0->nh_soa) {
    /* Make sure caller uses application/sdp without compression */
    if (nta_check_session_content(irq, sip,
				  nua->nua_invite_accept,
				  SIPTAG_USER_AGENT(user_agent),
				  SIPTAG_ACCEPT_ENCODING_STR(""),
				  TAG_END()))
      return 415;

    /* Make sure caller accepts application/sdp */
1144
    if (nta_check_accept(irq, sip,
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
			 nua->nua_invite_accept,
			 NULL,
			 SIPTAG_USER_AGENT(user_agent),
			 SIPTAG_ACCEPT_ENCODING_STR(""),
			 TAG_END()))
      return 406;
  }

  if (sip->sip_session_expires) {
    unsigned min_se = nh ? nh->nh_ss->ss_min_se : DNH_PGET(dnh, min_se);
1155
    if (nta_check_session_expires(irq, sip,
1156 1157 1158
				  min_se,
				  SIPTAG_USER_AGENT(user_agent),
				  TAG_END()))
1159
      return 500; /* respond with 500 Internal Server Error  */
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
  }

  if (!nh) {
    if (!DNH_PGET(dnh, invite_enable))
      return 403;

    if (!(nh = nua_stack_incoming_handle(nua, irq, sip, nh_has_invite, 1)))
      return 500;
  }

  have_sdp = session_get_description(msg, sip, &sdp, &len);

  if (nh->nh_ss->ss_srequest->sr_irq
      /* XXX || (have_sdp && nh->nh_sr->sr_offer_recv) XXX */) {
    /* Overlapping invites - RFC 3261 14.2 */
    sip_retry_after_t af[1];

    /* Random delay of 0..10 seconds */
    sip_retry_after_init(af)->af_delta = (unsigned)random() % 11U;
    af->af_comment = "Overlapping INVITE Request";

    nta_incoming_treply(irq, 500, af->af_comment,
			SIPTAG_RETRY_AFTER(af),
			TAG_END());
    return 500;
  }

  if (nh->nh_ss->ss_crequest->cr_orq ||
      (have_sdp && nh->nh_cr->cr_orq && nh->nh_cr->cr_offer_sent)) {
    /* Glare - RFC 3261 14.2 and RFC 3311 section 5.2 */
    nta_incoming_treply(irq, SIP_491_REQUEST_PENDING, TAG_END());
    return 491;
  }

  *return_nh = nh;

  sr = nh->nh_ss->ss_srequest; memset(sr, 0, sizeof *sr);

  if (nh->nh_soa) {
    soa_init_offer_answer(nh->nh_soa);

    if (have_sdp) {
      if (soa_set_remote_sdp(nh->nh_soa, NULL, sdp, len) < 0) {
	SU_DEBUG_5(("nua(%p): error parsing SDP in INVITE\n", nh));
	nta_incoming_treply(irq, 400, "Bad Session Description", TAG_END());
	return 400;
      }
      sr->sr_offer_recv = 1;
    }
  }

  /** Add a dialog usage */
1212 1213
  if (!nh->nh_ss->ss_usage)
    nh->nh_ss->ss_usage =
1214 1215 1216 1217 1218 1219 1220 1221
      nua_dialog_usage_add(nh, nh->nh_ds, nua_session_usage, NULL);
  if (!nh->nh_ss->ss_usage) {
    nta_incoming_treply(irq, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
    return 500;
  }

  sr->sr_msg = msg;
  sr->sr_irq = irq;
1222

1223 1224 1225
  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
1226
/** @internal Process incoming invite - initiate media, etc. */
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
static
int process_invite2(nua_t *nua,
		    nua_handle_t *nh,
		    nta_incoming_t *irq,
		    sip_t *sip)
{
  nua_session_state_t *ss = nh->nh_ss;
  nua_server_request_t *sr = ss->ss_srequest;

  ss->ss_100rel = NH_PGET(nh, early_media);
  ss->ss_precondition = sip_has_feature(sip->sip_require, "precondition");
  if (ss->ss_precondition)
    ss->ss_100rel = 1;

  /* Session Timer negotiation */
1242
  init_session_timer(nh, sip);
1243

1244
  nua_dialog_uas_route(nh, nh->nh_ds, sip, 1);	/* Set route and tags */
1245 1246 1247 1248 1249 1250 1251

  nta_incoming_bind(irq, process_ack_or_cancel, nh);

  assert(ss->ss_state >= nua_callstate_ready ||
	 ss->ss_state == nua_callstate_init);

  /* Magical value indicating autoanswer within respond_to_invite() */
1252
#define AUTOANSWER ((void*)-1)
1253 1254

  if (NH_PGET(nh, auto_answer) ||
1255
      /* Auto-answer to re-INVITE unless auto_answer is set to 0 on handle */
1256
      (ss->ss_state == nua_callstate_ready &&
1257 1258 1259 1260
       /* Auto-answer requires enabled media (soa). 
	* XXX - if the re-INVITE modifies the media we should not auto-answer.
	*/
       nh->nh_soa &&
1261 1262 1263 1264 1265 1266 1267 1268
       !NH_PISSET(nh, auto_answer))) {
    respond_to_invite(nua, nh, SIP_200_OK, AUTOANSWER);
    return 0;
  }

  ss->ss_srequest->sr_respond = respond_to_invite;

  if (NH_PGET(nh, auto_alert)) {
1269
    if (ss->ss_100rel &&
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	(sip_has_feature(nh->nh_ds->ds_remote_ua->nr_supported, "100rel") ||
	 sip_has_feature(nh->nh_ds->ds_remote_ua->nr_require, "100rel"))) {
      respond_to_invite(nua, nh, SIP_183_SESSION_PROGRESS, AUTOANSWER);
    }
    else {
      respond_to_invite(nua, nh, SIP_180_RINGING, AUTOANSWER);
    }
  }
  else {
    nta_incoming_treply(irq, SIP_100_TRYING, TAG_END());

    nua_stack_event(nh->nh_nua, nh, sr->sr_msg,
	     nua_i_invite, SIP_100_TRYING,
	     NH_ACTIVE_MEDIA_TAGS(1, nh->nh_soa),
	     TAG_END());
    sr->sr_msg = NULL;

1287 1288
    signal_call_state_change(nh, SIP_100_TRYING,
			     nua_callstate_received,