nea.c 14.8 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * 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 nea.c  Nokia Event Client API agent implementation.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Feb 14 18:32:58 2001 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

41
#include <sofia-sip/su_tagarg.h>
42
#include <sofia-sip/su_string.h>
Pekka Pessi's avatar
Pekka Pessi committed
43

44 45 46 47
#include <sofia-sip/sip.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/sip_status.h>
Pekka Pessi's avatar
Pekka Pessi committed
48 49 50 51 52 53 54 55

#define SU_TIMER_ARG_T       struct nea_s
#define NTA_LEG_MAGIC_T      struct nea_s
#define NTA_OUTGOING_MAGIC_T struct nea_s

#define NEA_TIMER_DELTA 2 /* time to resubscribe without expiration */
#define EXPIRES_DEFAULT       3600

56
#include <sofia-sip/su_wait.h>
Pekka Pessi's avatar
Pekka Pessi committed
57

58
#include "sofia-sip/nea.h"
Pekka Pessi's avatar
Pekka Pessi committed
59 60 61 62 63 64 65 66 67 68 69

struct nea_s {
  su_home_t         nea_home[1];
  su_timer_t       *nea_timer;

  nta_agent_t      *nea_agent;
  nta_leg_t        *nea_leg;
  nta_outgoing_t   *nea_oreq;		/**< Outstanding request */
  sip_to_t         *nea_to;		/**< The other end of subscription :) */
  nea_notify_f      nea_callback;	/**< Notify callback  */
  nea_magic_t      *nea_context;	/**< Application context */
70

Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  sip_contact_t    *nea_contact;	/**< */
  sip_expires_t    *nea_expires;	/**< Proposed expiration time */

  nea_state_t       nea_state;	        /**< State of our subscription */
  sip_time_t        nea_deadline;	/**< When our subscription expires */
  tagi_t           *nea_args;

  unsigned          nea_dialog : 1;     /**< Dialog has been established */
  unsigned          nea_notify_received : 1;
  unsigned          nea_terminating : 1;
  unsigned          nea_strict_3265 : 1;       /**< Strict mode */
};

int details = 0;

static int process_nea_request(nea_t *nea,
			       nta_leg_t *leg,
88
			       nta_incoming_t *ireq,
Pekka Pessi's avatar
Pekka Pessi committed
89 90
			       sip_t const *sip);

91
static int handle_notify(nta_leg_magic_t *lmagic,
Pekka Pessi's avatar
Pekka Pessi committed
92
			 nta_leg_t *leg,
93
			 nta_incoming_t *ireq,
Pekka Pessi's avatar
Pekka Pessi committed
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
			 sip_t const *sip);

static int response_to_subscribe(nea_t *nea,
				 nta_outgoing_t *req,
				 sip_t const *sip);

static int response_to_unsubscribe(nea_t *nea,
				   nta_outgoing_t *req,
				   sip_t const *sip);

static void nea_expires_renew(su_root_magic_t *magic,
		       su_timer_t *timer,
		       nea_t *nea);

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

/** Create a event watcher object.
 *
 */
nea_t *nea_create(nta_agent_t *agent,
		  su_root_t *root,
		  nea_notify_f no_callback,
		  nea_magic_t *context,
		  tag_type_t tag, tag_value_t value, ...)
{
  nea_t *nea = NULL;
  ta_list ta;
  int have_from, have_to, have_contact;
  sip_expires_t const *expires = NULL;
  char const *expires_str = NULL;
  sip_method_t method = sip_method_subscribe;
  char const *SUBSCRIBE = "SUBSCRIBE";
  char const *method_name = SUBSCRIBE;

  ta_start(ta, tag, value);

130
  have_to =
Pekka Pessi's avatar
Pekka Pessi committed
131
    tl_find(ta_args(ta), siptag_to) || tl_find(ta_args(ta), siptag_to_str);
132
  have_from =
Pekka Pessi's avatar
Pekka Pessi committed
133 134
    tl_find(ta_args(ta), siptag_from) || tl_find(ta_args(ta), siptag_from_str);
  have_contact =
135 136
    tl_find(ta_args(ta), siptag_contact) ||
    tl_find(ta_args(ta), siptag_contact_str);
Pekka Pessi's avatar
Pekka Pessi committed
137

138
  if (have_to && (nea = su_home_new(sizeof(nea_t)))) {
Pekka Pessi's avatar
Pekka Pessi committed
139 140 141 142 143 144 145 146 147 148
    su_home_t      *home = nea->nea_home;
    sip_contact_t  *m = nta_agent_contact(agent);
    sip_from_t     *from;
    sip_to_t const *to;
    int strict = 0;

    nea->nea_agent = agent;
    nea->nea_callback = no_callback;
    nea->nea_context = context;

149
    if (!have_from)
Pekka Pessi's avatar
Pekka Pessi committed
150 151 152 153 154 155 156 157 158
      from = sip_from_create(home, (url_string_t*)m->m_url);
    else
      from = NULL;

    nea->nea_args = tl_tlist(home,
			     TAG_IF(!have_contact, SIPTAG_CONTACT(m)),
			     ta_tags(ta));

    /* Get and remove Expires header from tag list */
159
    tl_gets(nea->nea_args,
Pekka Pessi's avatar
Pekka Pessi committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	    SIPTAG_EXPIRES_REF(expires),
	    SIPTAG_EXPIRES_STR_REF(expires_str),
	    SIPTAG_TO_REF(to),
	    NEATAG_STRICT_3265_REF(strict),
	    NTATAG_METHOD_REF(method_name),
	    TAG_END());

    nea->nea_strict_3265 = strict;

    if (to)
      nea->nea_to = sip_to_dup(home, to);

    if (expires)
      nea->nea_expires = sip_expires_dup(home, expires);
    else if (expires_str)
      nea->nea_expires = sip_expires_make(home, expires_str);
    else
      nea->nea_expires = sip_expires_create(home, EXPIRES_DEFAULT);

179 180
    tl_tremove(nea->nea_args,
	       SIPTAG_EXPIRES(0),
Pekka Pessi's avatar
Pekka Pessi committed
181 182 183 184 185 186 187 188 189 190 191 192
	       SIPTAG_EXPIRES_STR(0),
	       TAG_END());

    if (method_name != SUBSCRIBE)
      method = sip_method_code(method_name);

    if (method != sip_method_invalid)
      /* Create the timer object */
      nea->nea_timer = su_timer_create(su_root_task(root), 0L);

    if (nea->nea_timer) {
      /* Create leg for NOTIFY requests */
193
      nea->nea_leg = nta_leg_tcreate(nea->nea_agent,
Pekka Pessi's avatar
Pekka Pessi committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
				     process_nea_request, nea,
				     TAG_IF(!have_from, SIPTAG_FROM(from)),
				     TAG_NEXT(nea->nea_args));

      if (nea->nea_leg) {
	nta_leg_tag(nea->nea_leg, NULL);
	nea->nea_oreq = nta_outgoing_tcreate(nea->nea_leg,
					     response_to_subscribe, nea,
					     NULL,
					     method, method_name,
					     NULL,
					     SIPTAG_EXPIRES(nea->nea_expires),
					     TAG_NEXT(nea->nea_args));
      }
    }

    if (!nea->nea_leg ||
	!nea->nea_oreq ||
212
	!nea->nea_timer)
Pekka Pessi's avatar
Pekka Pessi committed
213 214 215 216 217 218 219 220
      nea_destroy(nea), nea = NULL;
  }

  ta_end(ta);
  return nea;
}


221
int nea_update(nea_t *nea,
Pekka Pessi's avatar
Pekka Pessi committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	       tag_type_t tag,
	       tag_value_t value,
	       ...)
{
  ta_list ta;
  sip_expires_t const *expires = NULL;
  sip_payload_t const *pl = NULL;
  sip_content_type_t const *ct = NULL;
  char const *cts = NULL;

  /* char const *expires_str = NULL; */
  su_home_t *home = nea->nea_home;

  /* XXX - hack, previous request still waiting for response */
  if (!nea->nea_leg || nea->nea_oreq)
    return -1;
238

Pekka Pessi's avatar
Pekka Pessi committed
239
  ta_start(ta, tag, value);
240

Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244 245 246
  tl_gets(ta_args(ta),
	  SIPTAG_CONTENT_TYPE_REF(ct),
	  SIPTAG_CONTENT_TYPE_STR_REF(cts),
	  SIPTAG_PAYLOAD_REF(pl),
	  SIPTAG_EXPIRES_REF(expires),
	  TAG_NULL());
247

Pekka Pessi's avatar
Pekka Pessi committed
248 249 250 251 252
  if (!pl || (!ct && !cts)) {
    ta_end(ta);
    return -1;
  }

253
  tl_tremove(nea->nea_args,
Pekka Pessi's avatar
Pekka Pessi committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	     SIPTAG_CONTENT_TYPE(0),
	     SIPTAG_CONTENT_TYPE_STR(0),
	     SIPTAG_PAYLOAD(0),
	     SIPTAG_PAYLOAD_STR(0),
	     TAG_END());

  su_free(home, nea->nea_expires);

  if (expires)
    nea->nea_expires = sip_expires_dup(home, expires);
  else
    nea->nea_expires = sip_expires_create(home, EXPIRES_DEFAULT);

  /* nta_leg_tag(nea->nea_leg, NULL); */
  nea->nea_oreq = nta_outgoing_tcreate(nea->nea_leg,
				       response_to_subscribe, nea,
				       NULL,
				       SIP_METHOD_SUBSCRIBE,
272
				       NULL,
Pekka Pessi's avatar
Pekka Pessi committed
273 274
				       SIPTAG_TO(nea->nea_to),
				       SIPTAG_PAYLOAD(pl),
275
				       TAG_IF(ct, SIPTAG_CONTENT_TYPE(ct)),
Pekka Pessi's avatar
Pekka Pessi committed
276 277 278 279 280 281
				       TAG_IF(cts, SIPTAG_CONTENT_TYPE_STR(cts)),
				       SIPTAG_EXPIRES(nea->nea_expires),
				       TAG_NEXT(nea->nea_args));

  ta_end(ta);

282
  if (!nea->nea_oreq)
Pekka Pessi's avatar
Pekka Pessi committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    return -1;

  return 0;
}


/** Unsubscribe the agent. */
void nea_end(nea_t *nea)
{
  if (nea == NULL)
    return;

  nea->nea_terminating = 1;

  su_timer_destroy(nea->nea_timer), nea->nea_timer = NULL;

  if (nea->nea_leg && nea->nea_deadline) {
    nea->nea_oreq =
      nta_outgoing_tcreate(nea->nea_leg,
			   response_to_unsubscribe,
			   nea,
			   NULL,
			   SIP_METHOD_SUBSCRIBE,
			   NULL,
			   SIPTAG_EXPIRES_STR("0"),
			   TAG_NEXT(nea->nea_args));
  }
}

void nea_destroy(nea_t *nea)
{
  if (nea == NULL)
    return;

  if (nea->nea_oreq)
    nta_outgoing_destroy(nea->nea_oreq), nea->nea_oreq = NULL;

  if (nea->nea_leg)
    nta_leg_destroy(nea->nea_leg), nea->nea_leg = NULL;

  if (nea->nea_timer) {
    su_timer_reset(nea->nea_timer);
    su_timer_destroy(nea->nea_timer), nea->nea_timer = NULL;
  }

  su_free(NULL, nea);
}


/* Function called by NTA to handle incoming requests belonging to the leg */
333
int process_nea_request(nea_t *nea,
Pekka Pessi's avatar
Pekka Pessi committed
334
			nta_leg_t *leg,
335
			nta_incoming_t *ireq,
Pekka Pessi's avatar
Pekka Pessi committed
336 337 338 339 340 341
			sip_t const *sip)
{

  switch (sip->sip_request->rq_method) {
  case sip_method_notify:
    return handle_notify(nea, leg, ireq, sip);
342
  case sip_method_ack:
Pekka Pessi's avatar
Pekka Pessi committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    return 400;
  default:
    nta_incoming_treply(ireq, SIP_405_METHOD_NOT_ALLOWED,
			SIPTAG_ALLOW_STR("NOTIFY"), TAG_END());
    return 405;
  }
}


/* Callback function to handle subscription requests */
int response_to_subscribe(nea_t *nea,
			  nta_outgoing_t *oreq,
			  sip_t const *sip)
{
  int status = sip->sip_status->st_status;
  int error = status >= 300;

  if (status >= 200 && oreq == nea->nea_oreq)
    nea->nea_oreq = NULL;

  nea->nea_callback(nea, nea->nea_context, sip);

  if (status < 200)
    return 0;

  nea->nea_oreq = NULL;

  if (status < 300) {
    sip_time_t now = sip_now();
    if (!nea->nea_notify_received) {
      nea->nea_deadline = now +
374
	sip_contact_expires(NULL, sip->sip_expires, sip->sip_date,
Pekka Pessi's avatar
Pekka Pessi committed
375 376 377 378
			    EXPIRES_DEFAULT, now);
      if (sip->sip_to->a_tag && !nea->nea_dialog) {
	nea->nea_dialog = 1;
	nta_leg_rtag(nea->nea_leg, sip->sip_to->a_tag);
379
	nta_leg_client_route(nea->nea_leg,
Pekka Pessi's avatar
Pekka Pessi committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 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
			     sip->sip_record_route, sip->sip_contact);
      }
    }
  }
  else {
    nea->nea_deadline = 0;
    nea->nea_state = nea_terminated;
    if (status == 301 || status == 302 || status == 305) {
      sip_contact_t *m;

      for (m = sip->sip_contact; m; m = m->m_next) {
	if (m->m_url->url_type == url_sip ||
	    m->m_url->url_type == url_sips)
	  break;
      }

      if (m) {
	url_string_t const *proxy, *url;
	if (status == 305)
	  url = NULL, proxy = (url_string_t *)m->m_url;
	else
	  url = (url_string_t *)m->m_url, proxy = NULL;

	nea->nea_oreq =
	  nta_outgoing_tcreate(nea->nea_leg,
			       response_to_subscribe,
			       nea,
			       proxy,
			       SIP_METHOD_SUBSCRIBE,
			       url,
			       SIPTAG_EXPIRES(nea->nea_expires),
			       TAG_NEXT(nea->nea_args));
      }
    } else if (status == 423 && sip->sip_min_expires) {
      unsigned value = sip->sip_min_expires->me_delta;
      su_free(nea->nea_home, nea->nea_expires);
      nea->nea_expires = sip_expires_format(nea->nea_home, "%u", value);

      nea->nea_oreq =
	nta_outgoing_tcreate(nea->nea_leg,
			     response_to_subscribe,
			     nea,
			     NULL,
			     SIP_METHOD_SUBSCRIBE,
			     NULL,
			     SIPTAG_EXPIRES(nea->nea_expires),
			     TAG_NEXT(nea->nea_args));
    }
  }

  if (status >= 200)
    nta_outgoing_destroy(oreq);

  if (nea->nea_oreq || !error) {
    su_time_t now = su_now();
    now.tv_sec = nea->nea_deadline;
436 437
    su_timer_set_at(nea->nea_timer,
		    nea_expires_renew,
Pekka Pessi's avatar
Pekka Pessi committed
438 439 440 441 442
		    nea,
		    now);
  }
  else
    nea->nea_callback(nea, nea->nea_context, NULL);
443

Pekka Pessi's avatar
Pekka Pessi committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457
  return 0;
}


int response_to_unsubscribe(nea_t *nea,
			    nta_outgoing_t *orq,
			    sip_t const *sip)
{
  int status = sip->sip_status->st_status;

  nea->nea_callback(nea, nea->nea_context, sip);

  if (status >= 200)
    nta_outgoing_destroy(orq), nea->nea_oreq = NULL;
458
  if (status >= 300)
Pekka Pessi's avatar
Pekka Pessi committed
459 460 461 462 463 464
    nea->nea_callback(nea, nea->nea_context, NULL);

  return 0;
}

/** handle notifications */
465
int handle_notify(nea_t *nea,
Pekka Pessi's avatar
Pekka Pessi committed
466
		  nta_leg_t *leg,
467
		  nta_incoming_t *irq,
Pekka Pessi's avatar
Pekka Pessi committed
468 469 470
		  sip_t const *sip)
{
  sip_subscription_state_t *ss = sip->sip_subscription_state;
471 472
  sip_subscription_state_t ss0[1];
  char expires[32];
Pekka Pessi's avatar
Pekka Pessi committed
473 474 475 476 477 478

  if (nea->nea_strict_3265) {
    char const *phrase = NULL;

    if (ss == NULL)
      phrase = "NOTIFY Has No Subscription-State Header";
479
    else if (sip->sip_event == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
480
      phrase = "Event Header Missing";
481

Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    if (phrase) {
      nta_incoming_treply(irq, 400, phrase, TAG_END());
      nta_incoming_destroy(irq);
      nta_leg_destroy(nea->nea_leg), nea->nea_leg = NULL;
      nea->nea_state = nea_terminated;
      nea->nea_callback(nea, nea->nea_context, NULL);
      return 0;
    }
  }

  if (ss == NULL) {
    /* Do some compatibility stuff here */
    unsigned long delta = 3600;

    sip_subscription_state_init(ss = ss0);

    if (sip->sip_expires)
      delta = sip->sip_expires->ex_delta;

    if (delta == 0)
      ss->ss_substate = "terminated";
    else
      ss->ss_substate = "active";

    if (delta > 0) {
      snprintf(expires, sizeof expires, "%lu", delta);
      ss->ss_expires = expires;
    }
  }

  if (!nea->nea_dialog) {
    nea->nea_dialog = 1;
    nta_leg_rtag(nea->nea_leg, sip->sip_from->a_tag);
515
    nta_leg_server_route(nea->nea_leg,
Pekka Pessi's avatar
Pekka Pessi committed
516 517 518 519 520 521
			 sip->sip_record_route, sip->sip_contact);
  }

  nea->nea_notify_received = 1;
  nea->nea_callback(nea, nea->nea_context, sip);

522
  if (su_casematch(ss->ss_substate, "terminated")) {
Pekka Pessi's avatar
Pekka Pessi committed
523 524 525
    nta_leg_destroy(nea->nea_leg), nea->nea_leg = NULL;
    nea->nea_state = nea_terminated;

526
    if (su_casematch(ss->ss_reason, "deactivated")) {
Pekka Pessi's avatar
Pekka Pessi committed
527 528
      nea->nea_state = nea_embryonic;
      nea->nea_deadline = sip_now();
529
    } else if (su_casematch(ss->ss_reason, "probation")) {
Pekka Pessi's avatar
Pekka Pessi committed
530 531
      sip_time_t retry = sip_now() + NEA_TIMER_DELTA;

532 533
      if (ss->ss_retry_after)
	retry += strtoul(ss->ss_retry_after, NULL, 10);
Pekka Pessi's avatar
Pekka Pessi committed
534 535 536 537 538 539 540 541 542 543 544
      else
	retry += NEA_TIMER_DELTA;

      nea->nea_state = nea_embryonic;
      nea->nea_deadline = retry;
    } else {
      nea->nea_deadline = 0;
      nea->nea_callback(nea, nea->nea_context, NULL);
      return 200;
    }
  }
545
  else if (su_casematch(ss->ss_substate, "pending"))
Pekka Pessi's avatar
Pekka Pessi committed
546
    nea->nea_state = nea_pending;
547
  else if (su_casematch(ss->ss_substate, "active"))
Pekka Pessi's avatar
Pekka Pessi committed
548 549 550 551 552 553 554 555 556 557 558 559 560
    nea->nea_state = nea_active;
  else
    nea->nea_state = nea_extended;

  if (nea->nea_state != nea_embryonic && ss->ss_expires) {
    unsigned retry = strtoul(ss->ss_expires, NULL, 10);
    if (retry > 60) retry -= 30; else retry /= 2;
    nea->nea_deadline = sip_now() + retry;
  }

  {
    su_time_t now = su_now();
    now.tv_sec = nea->nea_deadline;
561 562
    su_timer_set_at(nea->nea_timer,
		    nea_expires_renew,
Pekka Pessi's avatar
Pekka Pessi committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
		    nea,
		    now);
  }

  return 200;
}

void nea_expires_renew(su_root_magic_t *magic,
		       su_timer_t *timer,
		       nea_t *nea)
{
  sip_time_t now = sip_now();

  /* re-subscribe if expires soon */
  if (nea->nea_state == nea_terminated ||
578
      nea->nea_deadline == 0 ||
Pekka Pessi's avatar
Pekka Pessi committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
      nea->nea_deadline > now + NEA_TIMER_DELTA)
    return;

  if (!nea->nea_notify_received)	/* Hmph. */
    return;

  nea->nea_notify_received = 0;

  nea->nea_oreq =
    nta_outgoing_tcreate(nea->nea_leg,
			 response_to_subscribe,
			 nea,
			 NULL,
			 SIP_METHOD_SUBSCRIBE,
			 NULL,
			 SIPTAG_EXPIRES(nea->nea_expires),
			 TAG_NEXT(nea->nea_args));
596

Pekka Pessi's avatar
Pekka Pessi committed
597 598
  return;
}