nua_subnotref.c 26.9 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_subnotref.c
26 27
 * @brief Subscriber (event watcher)
 *
28 29 30 31
 * This file contains implementation SUBSCRIBE UAC, NOTIFY UAS, REFER UAC.
 * The implementation of SUBSCRIBE UAS, NOTIFY UAC and REFER UAS is in
 * nua_notifier.c.
 * Alternative implementation using nea is in nua_event_server.c.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Mar  8 15:10:08 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>
Pekka Pessi's avatar
Pekka Pessi committed
50
#include <sofia-sip/sip_extra.h>
51
#include <sofia-sip/sip_util.h>
52
#include <sofia-sip/su_uniqueid.h>
53

54
#include "nua_stack.h"
55 56

/* ---------------------------------------------------------------------- */
57
/* Subcriber event usage */
58 59 60

struct event_usage
{
Pekka Pessi's avatar
Pekka Pessi committed
61
  enum nua_substate eu_substate;	/**< Subscription state */
62 63
  unsigned eu_delta;	                /**< Proposed expiration */
  sip_time_t eu_expires;	        /**< Absolute expiration time */
64
  unsigned eu_notified;		        /**< Number of NOTIFYs received */
65
  unsigned eu_unsolicited:1;	        /**< Not SUBSCRIBEd or REFERed */
Pekka Pessi's avatar
Pekka Pessi committed
66
  unsigned eu_refer:1;		        /**< Implied subscription by refer */
67 68
  unsigned eu_final_wait:1;	        /**< Waiting for final NOTIFY */
  unsigned eu_no_id:1;		        /**< Do not use "id" (even if we have one) */
69 70 71
};

static char const *nua_subscribe_usage_name(nua_dialog_usage_t const *du);
72
static int nua_subscribe_usage_add(nua_handle_t *nh,
73 74
				   nua_dialog_state_t *ds,
				   nua_dialog_usage_t *du);
75
static void nua_subscribe_usage_remove(nua_handle_t *nh,
76
				       nua_dialog_state_t *ds,
77 78 79
				       nua_dialog_usage_t *du,
				       nua_client_request_t *cr,
				       nua_server_request_t *sr);
80
static void nua_subscribe_usage_refresh(nua_handle_t *,
81
					nua_dialog_state_t *,
82 83 84
					nua_dialog_usage_t *,
					sip_time_t);
static int nua_subscribe_usage_shutdown(nua_handle_t *,
85
					nua_dialog_state_t *,
86
					nua_dialog_usage_t *);
87 88 89 90 91 92 93

static nua_usage_class const nua_subscribe_usage[1] = {
  {
    sizeof (struct event_usage), (sizeof nua_subscribe_usage),
    nua_subscribe_usage_add,
    nua_subscribe_usage_remove,
    nua_subscribe_usage_name,
94
    nua_base_usage_update_params,
95 96 97
    NULL,
    nua_subscribe_usage_refresh,
    nua_subscribe_usage_shutdown
98 99 100 101 102 103 104
  }};

static char const *nua_subscribe_usage_name(nua_dialog_usage_t const *du)
{
  return "subscribe";
}

105 106
static
int nua_subscribe_usage_add(nua_handle_t *nh,
107 108 109 110 111
			   nua_dialog_state_t *ds,
			   nua_dialog_usage_t *du)
{
  ds->ds_has_events++;
  ds->ds_has_subscribes++;
Pekka Pessi's avatar
Pekka Pessi committed
112

113 114 115
  return 0;
}

116 117
static
void nua_subscribe_usage_remove(nua_handle_t *nh,
118
			       nua_dialog_state_t *ds,
119 120 121
			       nua_dialog_usage_t *du,
				nua_client_request_t *cr,
				nua_server_request_t *sr)
122
{
123 124
  ds->ds_has_events--;
  ds->ds_has_subscribes--;
125 126 127 128 129
}

/* ====================================================================== */
/* SUBSCRIBE */

130 131
/**@fn void nua_subscribe(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...);
 *
132
 *  Subscribe to a SIP event.
133
 *
134 135
 * Subscribe a SIP event using the SIP SUBSCRIBE request. If the
 * SUBSCRBE is successful a subscription state is established and
136 137 138 139 140 141
 * the subscription is refreshed regularly. The refresh requests will
 * generate #nua_r_subscribe events.
 *
 * @param nh              Pointer to operation handle
 * @param tag, value, ... List of tagged parameters
 *
142
 * @return
143 144 145 146
 *    nothing
 *
 * @par Related Tags:
 *    NUTAG_URL()
147
 *    Header tags defined in <sofia-sip/sip_tag.h>
148 149 150 151 152 153 154 155
 *
 * @par Events:
 *    #nua_r_subscribe \n
 *    #nua_i_notify
 *
 * @sa NUTAG_SUBSTATE(), @RFC3265
 */

156 157
/**@fn void nua_unsubscribe(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...);
 *
158
 * Unsubscribe an event.
159
 *
160 161 162
 * Unsubscribe an active or pending subscription with SUBSCRIBE request
 * containing Expires: header with value 0. The dialog associated with
 * subscription will be destroyed if there is no other subscriptions or
163 164 165 166 167
 * call using this dialog.
 *
 * @param nh              Pointer to operation handle
 * @param tag, value, ... List of tagged parameters
 *
168
 * @return
169 170 171 172
 *    nothing
 *
 * @par Related Tags:
 *    SIPTAG_EVENT() or SIPTAG_EVENT_STR() \n
173
 *    Header tags defined in <sofia-sip/sip_tag.h> except SIPTAG_EXPIRES() or SIPTAG_EXPIRES_STR()
174 175
 *
 * @par Events:
176
 *    #nua_r_unsubscribe
177 178 179 180
 *
 * @sa NUTAG_SUBSTATE(), @RFC3265
 */

181
static int nua_subscribe_client_init(nua_client_request_t *cr,
Pekka Pessi's avatar
Pekka Pessi committed
182 183 184 185 186 187 188
				     msg_t *, sip_t *,
				     tagi_t const *tags);
static int nua_subscribe_client_request(nua_client_request_t *cr,
					msg_t *, sip_t *,
					tagi_t const *tags);
static int nua_subscribe_client_response(nua_client_request_t *cr,
					 int status, char const *phrase,
189 190
					 sip_t const *sip);

191
static nua_client_methods_t const nua_subscribe_client_methods = {
192 193 194
  SIP_METHOD_SUBSCRIBE,		/* crm_method, crm_method_name */
  0,				/* crm_extra */
  {				/* crm_flags */
Pekka Pessi's avatar
Pekka Pessi committed
195 196 197 198
    /* create_dialog */ 1,
    /* in_dialog */ 1,
    /* target refresh */ 1
  },
199 200 201 202 203 204 205 206
  NULL,				/* crm_template */
  nua_subscribe_client_init,	/* crm_init */
  nua_subscribe_client_request,	/* crm_send */
  NULL,				/* crm_check_restart */
  nua_subscribe_client_response, /* crm_recv */
  NULL,				/* crm_preliminary */
  NULL,				/* crm_report */
  NULL,				/* crm_complete */
Pekka Pessi's avatar
Pekka Pessi committed
207
};
208

209
int
210 211
nua_stack_subscribe(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		    tagi_t const *tags)
212
{
Pekka Pessi's avatar
Pekka Pessi committed
213 214
  return nua_client_create(nh, e, &nua_subscribe_client_methods, tags);
}
215

Pekka Pessi's avatar
Pekka Pessi committed
216 217 218 219 220 221 222
static int nua_subscribe_client_init(nua_client_request_t *cr,
				     msg_t *msg, sip_t *sip,
				     tagi_t const *tags)
{
  nua_handle_t *nh = cr->cr_owner;
  nua_dialog_usage_t *du;
  sip_event_t *o = sip->sip_event;
223

Pekka Pessi's avatar
Pekka Pessi committed
224
  du = nua_dialog_usage_get(nh->nh_ds, nua_subscribe_usage, o);
225

Pekka Pessi's avatar
Pekka Pessi committed
226 227
  if (du == NULL && o == NULL)
    du = nua_dialog_usage_get(nh->nh_ds, nua_subscribe_usage, NONE);
228

Pekka Pessi's avatar
Pekka Pessi committed
229 230 231 232 233
  if (du) {
    if (du->du_event && o == NULL)
      /* Add Event header */
      sip_add_dup(msg, sip, (sip_header_t *)du->du_event);
  }
234
  else if (cr->cr_event == nua_r_subscribe) {
Pekka Pessi's avatar
Pekka Pessi committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248
    /* Create dialog usage */
    du = nua_dialog_usage_add(nh, nh->nh_ds, nua_subscribe_usage, o);
    /* Note that we allow SUBSCRIBE without event */
  }

  cr->cr_usage = du;

  return 0;
}

static int nua_subscribe_client_request(nua_client_request_t *cr,
					msg_t *msg, sip_t *sip,
					tagi_t const *tags)
{
249
  nua_dialog_usage_t *du = cr->cr_usage;
Pekka Pessi's avatar
Pekka Pessi committed
250 251
  sip_time_t expires = 0;

252
  if (cr->cr_event == nua_r_destroy || !du || du->du_shutdown)
253
    nua_client_set_terminating(cr, 1);
254

Pekka Pessi's avatar
Pekka Pessi committed
255 256 257 258 259 260 261 262 263 264
  if (du) {
    struct event_usage *eu = nua_dialog_usage_private(du);
    sip_event_t *o = sip->sip_event;

    if (nua_client_bind(cr, du) < 0)
      return -1;

    if (eu->eu_no_id && o && o->o_id) {
      /* Notifier does not handle id properly, remove it */
      msg_header_remove_param(o->o_common, "id");
265
    }
Pekka Pessi's avatar
Pekka Pessi committed
266 267 268 269

#if 0
    if (cr->cr_terminating) {
      /* Already terminated subscription? */
270 271
      if (eu->eu_substate == nua_substate_terminated ||
	  eu->eu_substate == nua_substate_embryonic) {
Pekka Pessi's avatar
Pekka Pessi committed
272
	return nua_client_return(cr, SIP_200_OK, msg);
273
      }
274
    }
Pekka Pessi's avatar
Pekka Pessi committed
275
#endif
276

Pekka Pessi's avatar
Pekka Pessi committed
277
    nua_dialog_usage_reset_refresh(du); /* during SUBSCRIBE transaction */
278

279
    if (cr->cr_terminating || cr->cr_event != nua_r_subscribe)
280
      expires = eu->eu_delta = 0;
Pekka Pessi's avatar
Pekka Pessi committed
281 282
    else if (sip->sip_expires)
      /* Use value specified by application or negotiated with Min-Expires */
283
      expires = eu->eu_delta = sip->sip_expires->ex_delta;
284
    else
285 286 287 288
    /* We just use common default value, but the default is actually
       package-specific according to the RFC 3265 section 4.4.4:
       [Event] packages MUST also define a
       default "Expires" value to be used if none is specified. */
289
      expires = eu->eu_delta = 3600;
290

Pekka Pessi's avatar
Pekka Pessi committed
291
    eu->eu_final_wait = 0;
292

Pekka Pessi's avatar
Pekka Pessi committed
293 294 295
    if (eu->eu_substate == nua_substate_terminated)
      eu->eu_substate = nua_substate_embryonic;
  }
296

Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301
  if (!sip->sip_expires || sip->sip_expires->ex_delta != expires) {
    sip_expires_t ex[1];
    sip_expires_init(ex)->ex_delta = expires;
    sip_add_dup(msg, sip, (sip_header_t *)ex);
  }
302

Pekka Pessi's avatar
Pekka Pessi committed
303
  return nua_base_client_request(cr, msg, sip, tags);
304 305
}

306
/** @NUA_EVENT nua_r_subscribe
307
 *
308
 * Response to an outgoing SUBSCRIBE request.
309 310 311 312
 *
 * The SUBSCRIBE request may have been sent explicitly by nua_subscribe() or
 * implicitly by NUA state machine.
 *
313 314 315 316 317 318 319
 * @param status response status code
 *               (if the request is retried, @a status is 100, the @a
 *               sip->sip_status->st_status contain the real status code
 *               from the response message, e.g., 302, 401, or 407)
 * @param phrase a short textual description of @a status code
 * @param nh     operation handle associated with the subscription
 * @param hmagic application context associated with the handle
320
 * @param sip    response to SUBSCRIBE request or NULL upon an error
321
 *               (status code is in @a status and
322
 *                descriptive message in @a phrase parameters)
323 324 325
 * @param tags   NUTAG_SUBSTATE()
 *
 * @sa nua_subscribe(), @RFC3265
326 327
 *
 * @END_NUA_EVENT
328 329
 */

330
/** @NUA_EVENT nua_r_unsubscribe
331 332 333
 *
 * Response to an outgoing un-SUBSCRIBE.
 *
334 335 336 337 338 339 340
 * @param status response status code
 *               (if the request is retried, @a status is 100, the @a
 *               sip->sip_status->st_status contain the real status code
 *               from the response message, e.g., 302, 401, or 407)
 * @param phrase a short textual description of @a status code
 * @param nh     operation handle associated with the subscription
 * @param hmagic application context associated with the handle
341
 * @param sip    response to SUBSCRIBE request or NULL upon an error
342
 *               (status code is in @a status and
343
 *                descriptive message in @a phrase parameters)
344 345 346
 * @param tags   NUTAG_SUBSTATE()
 *
 * @sa nua_unsubscribe(), @RFC3265
347 348
 *
 * @END_NUA_EVENT
349 350
 */

Pekka Pessi's avatar
Pekka Pessi committed
351 352
static int nua_subscribe_client_response(nua_client_request_t *cr,
					 int status, char const *phrase,
353 354
					 sip_t const *sip)
{
Pekka Pessi's avatar
Pekka Pessi committed
355
  nua_handle_t *nh = cr->cr_owner;
356
  nua_dialog_usage_t *du = cr->cr_usage;
357
  struct event_usage *eu = nua_dialog_usage_private(du);
Pekka Pessi's avatar
Pekka Pessi committed
358
  enum nua_substate substate;
359

Pekka Pessi's avatar
Pekka Pessi committed
360 361 362 363 364
  if (eu == NULL || cr->cr_terminated)
    substate = nua_substate_terminated;
  else if (status >= 300)
    substate = eu->eu_substate;
  else {
365 366 367 368
    int win_messenger_enable = NH_PGET(nh, win_messenger_enable);
    sip_time_t delta, now = sip_now();

    du->du_ready = 1;
Pekka Pessi's avatar
Pekka Pessi committed
369 370

    if (eu->eu_substate != nua_substate_terminated)
371
      /* If there is no @Expires header,
372
	 use default value stored in eu_delta */
373
      delta = sip_contact_expires(NULL, sip->sip_expires, sip->sip_date,
374
				  eu->eu_delta, now);
Pekka Pessi's avatar
Pekka Pessi committed
375 376
    else
      delta = 0;
377

378 379
    if (delta > eu->eu_delta)
      delta = eu->eu_delta;
380

381
    if (win_messenger_enable && !nua_dialog_is_established(nh->nh_ds)) {
382
      /* Notify from messanger does not match with dialog tag */
383 384 385
      nh->nh_ds->ds_remote_tag = su_strdup(nh->nh_home, "");
    }

386 387
    if (delta > 0) {
      nua_dialog_usage_set_refresh(du, delta);
388
      eu->eu_expires = du->du_refquested + delta;
389
    }
390 391 392 393 394
    else {
      if (eu->eu_substate == nua_substate_terminated) {
	if (!eu->eu_notified)
	  eu->eu_substate = nua_substate_embryonic;
      }
395

396 397 398
      if (eu->eu_substate != nua_substate_terminated) {
	/* Wait 32 seconds for NOTIFY. */
	delta = 64 * NTA_SIP_T1 / 1000;
399

400
	eu->eu_final_wait = 1;
401

402 403 404 405 406
	if (!eu->eu_notified && win_messenger_enable)
	  delta = 4 * 60; 	/* Wait 4 minutes for NOTIFY from Messenger */

	nua_dialog_usage_set_refresh_range(du, delta, delta);
      }
407
      else {
408
	nua_dialog_usage_reset_refresh(du);
409 410 411
      }

      eu->eu_expires = du->du_refquested;
412
    }
413 414 415

    substate = eu->eu_substate;

Pekka Pessi's avatar
Pekka Pessi committed
416 417
    if (substate == nua_substate_terminated)
      /* let nua_base_client_tresponse to remove usage */
418
      cr->cr_terminated = 1;
419
  }
420

421
  return nua_base_client_tresponse(cr, status, phrase, sip,
Pekka Pessi's avatar
Pekka Pessi committed
422
				   NUTAG_SUBSTATE(substate),
423
				   SIPTAG_EVENT(du ? du->du_event : NULL),
Pekka Pessi's avatar
Pekka Pessi committed
424
				   TAG_END());
425 426 427
}

/** Refresh subscription */
428
static void nua_subscribe_usage_refresh(nua_handle_t *nh,
429
					nua_dialog_state_t *ds,
430 431
					nua_dialog_usage_t *du,
					sip_time_t now)
432
{
Pekka Pessi's avatar
Pekka Pessi committed
433
  nua_client_request_t *cr = du->du_cr;
434
  struct event_usage *eu = nua_dialog_usage_private(du);
435

436
  assert(eu);
437

438
  if (eu->eu_final_wait) {
Pekka Pessi's avatar
Pekka Pessi committed
439
    /* Did not receive NOTIFY for fetch */
440 441 442
    sip_event_t const *o = du->du_event;
    char const *id = o ? o->o_id : NULL;

Pekka Pessi's avatar
Pekka Pessi committed
443
    SU_DEBUG_3(("nua(%p): event %s%s%s fetch timeouts\n",
444
		(void *)nh, o ? o->o_type : "(empty)",
445 446
		id ? "; id=" : "", id ? id : ""));

447
    nua_stack_tevent(nh->nh_nua, nh,  NULL,
448
		     nua_i_notify, 408, "Fetch Timeouts without NOTIFY",
449 450 451
		     NUTAG_SUBSTATE(nua_substate_terminated),
		     SIPTAG_EVENT(du->du_event),
		     TAG_END());
452
    nua_dialog_usage_remove(nh, ds, du, NULL, NULL);
453

454 455 456
    return;
  }

Pekka Pessi's avatar
Pekka Pessi committed
457
  if (cr) {
458
    if (nua_client_resend_request(cr, 0) >= 0)
Pekka Pessi's avatar
Pekka Pessi committed
459
      return;
460
  }
Pekka Pessi's avatar
Pekka Pessi committed
461 462 463 464 465
  else if (eu->eu_refer) {
    /*
     * XXX - If we have received a NOTIFY, we should try to terminate
     * subscription
     */
466 467
  }

468 469
  if (!eu->eu_unsolicited)
    nua_stack_tevent(nh->nh_nua, nh, NULL,
Pekka Pessi's avatar
Pekka Pessi committed
470
		     nua_i_notify, NUA_ERROR_AT(__FILE__, __LINE__),
471 472 473
		     NUTAG_SUBSTATE(nua_substate_terminated),
		     SIPTAG_EVENT(du->du_event),
		     TAG_END());
474

475
  nua_dialog_usage_remove(nh, ds, du, NULL, NULL);
476 477
}

Pekka Pessi's avatar
Pekka Pessi committed
478 479 480 481 482 483
/** Terminate subscription.
 *
 * @retval >0  shutdown done
 * @retval 0   shutdown in progress
 * @retval <0  try again later
 */
484
static int nua_subscribe_usage_shutdown(nua_handle_t *nh,
485
					nua_dialog_state_t *ds,
486
					nua_dialog_usage_t *du)
487
{
488
  struct event_usage *eu = nua_dialog_usage_private(du);
Pekka Pessi's avatar
Pekka Pessi committed
489
  nua_client_request_t *cr = du->du_cr;
490

491
  assert(eu); (void)eu;
492

Pekka Pessi's avatar
Pekka Pessi committed
493
  if (cr) {
494
    if (nua_client_resend_request(cr, 1) >= 0)
Pekka Pessi's avatar
Pekka Pessi committed
495 496
      return 0;
  }
497

498
  nua_dialog_usage_remove(nh, ds, du, NULL, NULL);
499
  return 200;
500 501 502
}

/* ======================================================================== */
503
/* NOTIFY server */
504

505
/** @NUA_EVENT nua_i_notify
506 507 508 509
 *
 * Event for incoming NOTIFY request.
 *
 * @param status statuscode of response sent automatically by stack
510 511 512
 * @param phrase a short textual description of @a status code
 * @param nh     operation handle associated with the subscription
 * @param hmagic application context associated with the handle
513 514 515 516
 * @param sip    incoming NOTIFY request
 * @param tags   NUTAG_SUBSTATE() indicating the subscription state
 *
 * @sa nua_subscribe(), nua_unsubscribe(), @RFC3265, #nua_i_subscribe
517
 *
518
 * @END_NUA_EVENT
519 520
 */

521 522 523 524
int nua_notify_server_init(nua_server_request_t *sr);
int nua_notify_server_preprocess(nua_server_request_t *sr);
int nua_notify_server_report(nua_server_request_t *, tagi_t const *);

525
nua_server_methods_t const nua_notify_server_methods =
526 527 528
  {
    SIP_METHOD_NOTIFY,
    nua_i_notify,		/* Event */
529
    {
530 531 532 533
      /* create_dialog: */ 1,	/* Do create dialog */
      /* in_dialog: */ 0,	/* Not always in-dialog request */
      /* target_refresh: */ 1,	/* Target refresh request  */
      /* add_contact: */ 1,	/* Add Contact to response */
534 535 536 537 538 539 540 541 542 543
    },
    nua_notify_server_init,
    nua_notify_server_preprocess,
    nua_base_server_params,
    nua_base_server_respond,
    nua_notify_server_report,
  };


int nua_notify_server_init(nua_server_request_t *sr)
544
{
545 546 547 548
  if (!sr->sr_initial) {
    nua_dialog_state_t *ds = sr->sr_owner->nh_ds;

    /* Check for forked subscription. */
549
    if (ds->ds_remote_tag && ds->ds_remote_tag[0] &&
550 551 552 553
	str0cmp(ds->ds_remote_tag, sr->sr_request.sip->sip_from->a_tag)) {
      sip_contact_t const *m = NULL;

      m = nua_stack_get_contact(sr->sr_owner->nh_nua->nua_registrations);
554

555 556
      if (m) {
	sip_warning_t w[1];
557

558 559 560 561
	sip_warning_init(w)->w_code = 399;
	w->w_host = m->m_url->url_host;
	w->w_port = m->m_url->url_port;
	w->w_text = "Forking SUBSCRIBEs are not supported";
562

563 564
	sip_add_dup(sr->sr_response.msg, NULL, (sip_header_t*)w);
      }
565

566
      return SR_STATUS(sr, 481, "Subscription Does Not Exist");
567
    }
568 569
  }

570 571
  return 0;
}
572

573 574 575 576 577 578 579 580 581 582
int nua_notify_server_preprocess(nua_server_request_t *sr)
{
  nua_dialog_state_t *ds = sr->sr_owner->nh_ds;
  nua_dialog_usage_t *du;
  struct event_usage *eu;
  sip_t const *sip = sr->sr_request.sip;
  sip_event_t *o = sip->sip_event;
  enum nua_substate substate = nua_substate_terminated;
  sip_subscription_state_t *subs = sip->sip_subscription_state;
  char const *what = "", *reason = NULL;
583
  int solicited = 1;
584 585

  du = nua_dialog_usage_get(ds, nua_subscribe_usage, o);
Pekka Pessi's avatar
Pekka Pessi committed
586 587 588 589

  if (du == NULL) {
    if (!sip_is_allowed(NH_PGET(sr->sr_owner, appl_method), SIP_METHOD_NOTIFY))
      return SR_STATUS(sr, 481, "Subscription Does Not Exist");
590 591 592 593 594

    solicited = 0;    /* Let application to handle unsolicited NOTIFY */
    du = nua_dialog_usage_add(sr->sr_owner, ds, nua_subscribe_usage, o);
    if (!du)
      return SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
Pekka Pessi's avatar
Pekka Pessi committed
595
  }
596

597
  sr->sr_usage = du;
598
  eu = nua_dialog_usage_private(du); assert(eu);
599
  eu->eu_notified++;
600
  if (!o || !o->o_id)
601
    eu->eu_no_id = 1;
602

603
  if (subs == NULL) {
604
    /* Compatibility */
605
    unsigned long delta = eu->eu_delta;
606
    if (sip->sip_expires)
607
      delta = sip->sip_expires->ex_delta;
608 609

    if (delta == 0)
610
      substate = nua_substate_terminated, what = "terminated";
611
    else
612
      substate = nua_substate_active, what = "active";
613
  }
614 615 616
  else if (strcasecmp(subs->ss_substate, what = "terminated") == 0) {
    substate = nua_substate_terminated;
    reason = subs->ss_reason;
617

618
    if (str0casecmp(reason, "deactivated") == 0 ||
619
	str0casecmp(reason, "probation") == 0)
620
      substate = nua_substate_embryonic;
621
  }
622 623 624 625
  else if (strcasecmp(subs->ss_substate, what = "pending") == 0) {
    substate = nua_substate_pending;
  }
  else /* if (strcasecmp(subs->ss_substate, what = "active") == 0) */ {
626
    /* Any extended state is considered as active */
627 628
    what = subs->ss_substate;
    substate = nua_substate_active;
629
  }
630

631
  eu->eu_substate = substate;
632 633
  if (!solicited)
    eu->eu_unsolicited = 1;
634

635
  SU_DEBUG_5(("nua(%p): %s: %s (%s)\n",
636
	      (void *)sr->sr_owner, "nua_notify_server_preprocess",
637
	      what, reason ? reason : ""));
638

639 640 641 642
  if (solicited)
    return SR_STATUS1(sr, SIP_200_OK);

  return 0;
643
}
644

645

646 647 648 649 650 651 652 653
int nua_notify_server_report(nua_server_request_t *sr, tagi_t const *tags)
{
  nua_handle_t *nh = sr->sr_owner;
  nua_dialog_usage_t *du = sr->sr_usage;
  struct event_usage *eu = nua_dialog_usage_private(du);
  sip_t const *sip = sr->sr_request.sip;
  enum nua_substate substate = nua_substate_terminated;
  sip_time_t delta = SIP_TIME_MAX;
654
  sip_event_t const *o = sip->sip_event;
655 656
  int retry = -1;
  int retval;
657

658 659
  if (eu) {
    sip_subscription_state_t *subs = sip->sip_subscription_state;
660

661
    substate = eu->eu_substate;
662

663
    if (substate == nua_substate_active || substate == nua_substate_pending) {
664 665 666 667 668 669
      if (subs && subs->ss_expires) {
	sip_time_t now = sip_now();
	sip_time_t delta0 = strtoul(subs->ss_expires, NULL, 10);
	if (now + delta0 <= eu->eu_expires)
	  delta = delta0;
      }
670 671 672 673 674
    }
    else if (substate == nua_substate_embryonic) {
      if (subs && subs->ss_reason) {
	if (str0casecmp(subs->ss_reason, "deactivated") == 0) {
	  retry = 0;		/* retry immediately */
675
	}
676 677 678 679 680 681 682 683 684 685 686
	else if (str0casecmp(subs->ss_reason, "probation") == 0) {
	  retry = 30;
	  if (subs->ss_retry_after)
	    retry = strtoul(subs->ss_retry_after, NULL, 10);
	  if (retry > 3600)
	    retry = 3600;
	}
      }
    }
    else if (substate == nua_substate_terminated) {
      sr->sr_terminating = 1;
687 688
    }
  }
689

690 691
  retval = nua_base_server_treport(sr, /* can destroy sr */
				   NUTAG_SUBSTATE(substate),
692
				   SIPTAG_EVENT(o),
693
				   TAG_NEXT(tags));
694

695
  if (retval != 1 || du == NULL)
696 697
    return retval;

698 699 700 701
  if (eu->eu_unsolicited) {
    /* Xyzzy */;
  }
  else if (retry >= 0) {		/* Try to subscribe again */
702 703
    /* XXX - this needs through testing */
    nua_dialog_remove(nh, nh->nh_ds, du); /* tear down */
704
    nua_dialog_usage_set_refresh_range(du, retry, retry + 5);
705 706
  }
  else {
707
    if (delta < SIP_TIME_MAX) {
708
      nua_dialog_usage_set_refresh(du, delta);
709 710
      eu->eu_expires = du->du_refquested + delta;
    }
711 712
  }

713
  return retval;
714 715
}

716

717 718 719
/* ======================================================================== */
/* REFER */

720 721
/**@fn void nua_refer(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...);
 *
722 723 724
 * Transfer a call.
 *
 * Send a REFER request asking the recipient to transfer the call.
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
 *
 * The REFER request also establishes an implied subscription to the "refer"
 * event. The "refer" event can have an "id" parameter, which has the value
 * of CSeq number in the REFER request. After initiating the REFER request,
 * the nua engine sends application a #nua_r_refer event with status 100 and
 * tag NUTAG_REFER_EVENT() containing a matching event header with id
 * parameter.
 *
 * Note that the @Event header in the locally generated #nua_r_refer event
 * contains the @a id parameter. The @a id parameter contains the @CSeq
 * number of the REFER request, and it may get incremented if the request is
 * retried because it got challenged or redirected. In that case, the
 * application gets a new #nua_r_refer event with status 100 and tag
 * NUTAG_REFER_EVENT(). Also the recipient of the REFER request may or may
 * not include the @a id parameter with the @Event header in the NOTIFY
 * requests messages which it sends to the sender of the REFER request.
 *
 * Therefore the application is not able to modify the state of the implied
 * subscription before receiving the first NOTIFY request.
744 745 746 747
 *
 * @param nh              Pointer to operation handle
 * @param tag, value, ... List of tagged parameters
 *
748
 * @return
749 750 751 752 753
 *    nothing
 *
 * @par Related Tags:
 *    NUTAG_URL() \n
 *    Tags of nua_set_hparams() \n
754
 *    Header tags defined in <sofia-sip/sip_tag.h>
755 756 757 758 759
 *
 * @par Events:
 *    #nua_r_refer \n
 *    #nua_i_notify
 *
760
 * @sa #nua_r_refer, NUTAG_SUBSTATE(), NUTAG_REFER_EVENT(),#nua_i_refer,
Pekka Pessi's avatar
Pekka Pessi committed
761 762 763 764
 * @RFC3515, @ReferTo, SIPTAG_REFER_TO(), SIPTAG_REFER_TO_STR(),
 * @RFC3892, @ReferredBy, SIPTAG_REFERRED_BY(), SIPTAG_REFERRED_BY_STR(),
 * @RFC3891, @Replaces, SIPTAG_REPLACES(), SIPTAG_REPLACES_STR(),
 * @RFC4488, @ReferSub, SIPTAG_REFER_SUB(), SIPTAG_REFER_SUB_STR()
765 766
 */

767
/**@NUA_EVENT nua_r_refer
768
 *
769
 * @brief Response to outgoing REFER.
770
 *
771 772 773 774 775 776 777
 * @param status response status code
 *               (if the request is retried, @a status is 100, the @a
 *               sip->sip_status->st_status contain the real status code
 *               from the response message, e.g., 302, 401, or 407)
 * @param phrase a short textual description of @a status code
 * @param nh     operation handle associated with the REFER request
 * @param hmagic application context associated with the handle
778
 * @param sip    response to REFER request or NULL upon an error
779
 *               (status code is in @a status and
780 781 782 783 784
 *                descriptive message in @a phrase parameters)
 * @param tags    NUTAG_REFER_EVENT() \n
 *                NUTAG_SUBSTATE()
 *
 * @sa nua_refer(), NUTAG_SUBSTATE(), #nua_i_refer,
Pekka Pessi's avatar
Pekka Pessi committed
785
 * @RFC3515, @RFC4488, @ReferSub
786 787
 *
 * @END_NUA_EVENT
788 789
 */

790
static int nua_refer_client_init(nua_client_request_t *cr,
Pekka Pessi's avatar
Pekka Pessi committed
791 792 793 794 795 796 797 798 799
				 msg_t *, sip_t *,
				 tagi_t const *tags);
static int nua_refer_client_request(nua_client_request_t *cr,
				    msg_t *, sip_t *,
				    tagi_t const *tags);
static int nua_refer_client_response(nua_client_request_t *cr,
				     int status, char const *phrase,
				     sip_t const *sip);

800
static nua_client_methods_t const nua_refer_client_methods = {
801 802 803
  SIP_METHOD_REFER,		/* crm_method, crm_method_name */
  0,				/* crm_extra */
  {				/* crm_flags */
Pekka Pessi's avatar
Pekka Pessi committed
804 805 806 807
    /* create_dialog */ 1,
    /* in_dialog */ 1,
    /* target refresh */ 1
  },
808 809 810 811 812 813 814 815
  NULL,				/* crm_template */
  nua_refer_client_init,	/* crm_init */
  nua_refer_client_request,	/* crm_send */
  NULL,				/* crm_check_restart */
  nua_refer_client_response,	/* crm_recv */
  nua_refer_client_response,	/* crm_preliminary */
  NULL,				/* crm_report */
  NULL,				/* crm_complete */
Pekka Pessi's avatar
Pekka Pessi committed
816 817 818 819 820
};

int
nua_stack_refer(nua_t *nua, nua_handle_t *nh, nua_event_t e,
		    tagi_t const *tags)
821
{
Pekka Pessi's avatar
Pekka Pessi committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
  return nua_client_create(nh, e, &nua_refer_client_methods, tags);
}

static int nua_refer_client_init(nua_client_request_t *cr,
				 msg_t *msg, sip_t *sip,
				 tagi_t const *tags)
{
  nua_handle_t *nh = cr->cr_owner;

  if (sip->sip_referred_by == NULL) {
    sip_from_t *a = sip->sip_from;
    sip_referred_by_t by[1];

    sip_referred_by_init(by);

    if (a == NULL)
      a = nh->nh_nua->nua_from;
    by->b_display = a->a_display;
    *by->b_url = *a->a_url;

    sip_add_dup(msg, sip, (sip_header_t *)by);
843
  }
Pekka Pessi's avatar
Pekka Pessi committed
844 845 846 847 848 849 850 851 852 853 854 855

  if (sip->sip_event)
    sip_header_remove(msg, sip, (sip_header_t *)sip->sip_event);

  return 0;
}

static int nua_refer_client_request(nua_client_request_t *cr,
				    msg_t *msg, sip_t *sip,
				    tagi_t const *tags)
{
  nua_handle_t *nh = cr->cr_owner;
856
  nua_dialog_usage_t *du, *du0 = cr->cr_usage;
Pekka Pessi's avatar
Pekka Pessi committed
857 858 859 860 861 862 863 864 865
  struct event_usage *eu;
  sip_event_t *event;
  int error;

  cr->cr_usage = NULL;

  event = sip_event_format(nh->nh_home, "refer;id=%u", sip->sip_cseq->cs_seq);
  if (!event)
    return -1;
866 867 868 869 870 871 872 873 874 875 876 877 878 879

  if (du0 == NULL ||
      du0->du_event == NULL ||
      du0->du_event->o_id == NULL ||
      strcmp(du0->du_event->o_id, event->o_id)) {
    du = nua_dialog_usage_add(nh, nh->nh_ds, nua_subscribe_usage, event);
    if (!du)
      return -1;
  }
  else {
    du = du0, du0 = NULL;
  }

  if (du0)
880
    nua_dialog_usage_remove(nh, nh->nh_ds, du0, NULL, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
881 882 883 884 885 886 887 888

  eu = nua_dialog_usage_private(cr->cr_usage = du);
  eu ->eu_refer = 1;

  error = nua_base_client_request(cr, msg, sip, tags);

  if (!error) {
    /* Give application an Event header for matching NOTIFYs with REFER */
889 890 891
    nua_stack_tevent(nh->nh_nua, nh, NULL,
		     cr->cr_event, SIP_100_TRYING,
		     NUTAG_REFER_EVENT(event),
892
		     SIPTAG_EVENT(event),
893
		     TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
894
    su_free(nh->nh_home, event);
895 896
  }

Pekka Pessi's avatar
Pekka Pessi committed
897 898 899 900 901 902 903
  return error;
}

static int nua_refer_client_response(nua_client_request_t *cr,
				     int status, char const *phrase,
				     sip_t const *sip)
{
904
  nua_dialog_usage_t *du = cr->cr_usage;
Pekka Pessi's avatar
Pekka Pessi committed
905 906 907 908 909 910
  enum nua_substate substate = nua_substate_terminated;

  if (du) {
    struct event_usage *eu = nua_dialog_usage_private(du);

    if (status < 200) {
911 912
      substate = eu->eu_substate;
    }
Pekka Pessi's avatar
Pekka Pessi committed
913 914 915 916 917 918 919 920 921 922
    else if (status < 300) {
      sip_refer_sub_t const *rs = sip_refer_sub(sip);

      if (rs && strcasecmp("false", rs->rs_value) == 0)
	cr->cr_terminated = 1;

      if (!cr->cr_terminated)
	substate = eu->eu_substate;
    }
  }
923 924

  return nua_base_client_tresponse(cr, status, phrase, sip,
Pekka Pessi's avatar
Pekka Pessi committed
925