nea.c 15 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 55 56 57 58 59 60 61 62 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 333 334 335 336 337 338 339 340 341 342 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 374 375 376 377 378 379 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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE 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
 * $Date: 2005/07/20 20:35:27 $
 */

#include "config.h"

const char _nea_c_id[] =
"$Id: nea.c,v 1.1.1.1 2005/07/20 20:35:27 kaiv Exp $";

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

#include <su_tagarg.h>

#include <sip.h>
#include <sip_header.h>
#include <sip_util.h>
#include <sip_status.h>

#include <sl_utils.h>

#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

#include <su_wait.h>

#include "nea.h"

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 */
   
  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,
			       nta_incoming_t *ireq, 
			       sip_t const *sip);

static int handle_notify(nta_leg_magic_t *lmagic, 
			 nta_leg_t *leg,
			 nta_incoming_t *ireq, 
			 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);

  have_to = 
    tl_find(ta_args(ta), siptag_to) || tl_find(ta_args(ta), siptag_to_str);
  have_from = 
    tl_find(ta_args(ta), siptag_from) || tl_find(ta_args(ta), siptag_from_str);
  have_contact =
    tl_find(ta_args(ta), siptag_contact) || 
    tl_find(ta_args(ta), siptag_contact_str);    

  if (have_to && (nea = su_home_clone(NULL, sizeof(nea_t)))) {
    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;

    if (!have_from) 
      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 */
    tl_gets(nea->nea_args, 
	    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);

    tl_tremove(nea->nea_args, 
	       SIPTAG_EXPIRES(0), 
	       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 */
      nea->nea_leg = nta_leg_tcreate(nea->nea_agent, 
				     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 ||
	!nea->nea_timer) 
      nea_destroy(nea), nea = NULL;
  }

  ta_end(ta);
  return nea;
}


int nea_update(nea_t *nea, 
	       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;
    
  ta_start(ta, tag, value);
  
  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());
  
  if (!pl || (!ct && !cts)) {
    ta_end(ta);
    return -1;
  }

  tl_tremove(nea->nea_args, 
	     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,
				       NULL, 
				       SIPTAG_TO(nea->nea_to),
				       SIPTAG_PAYLOAD(pl),
				       TAG_IF(ct, SIPTAG_CONTENT_TYPE(ct)), 
				       TAG_IF(cts, SIPTAG_CONTENT_TYPE_STR(cts)),
				       SIPTAG_EXPIRES(nea->nea_expires),
				       TAG_NEXT(nea->nea_args));

  ta_end(ta);

  if (!nea->nea_oreq) 
    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 */
int process_nea_request(nea_t *nea, 
			nta_leg_t *leg,
			nta_incoming_t *ireq, 
			sip_t const *sip)
{

  switch (sip->sip_request->rq_method) {
  case sip_method_notify:
    return handle_notify(nea, leg, ireq, sip);
  case sip_method_ack:  
    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 +
	sip_contact_expires(NULL, sip->sip_expires, sip->sip_date, 
			    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);
	nta_leg_client_route(nea->nea_leg, 
			     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;
    su_timer_set_at(nea->nea_timer, 
		    nea_expires_renew, 
		    nea,
		    now);
  }
  else
    nea->nea_callback(nea, nea->nea_context, NULL);
    
  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;
  if (status >= 300) 
    nea->nea_callback(nea, nea->nea_context, NULL);

  return 0;
}

/** handle notifications */
int handle_notify(nea_t *nea, 
		  nta_leg_t *leg,
		  nta_incoming_t *irq, 
		  sip_t const *sip)
{
  sip_subscription_state_t *ss = sip->sip_subscription_state;

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

    if (ss == NULL)
      phrase = "NOTIFY Has No Subscription-State Header";
    else if (sip->sip_event == NULL) 
      phrase = "Event Header Missing";
      
    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 */
    sip_subscription_state_t ss0[1];
    unsigned long delta = 3600;
    char expires[32];

    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);
    nta_leg_server_route(nea->nea_leg, 
			 sip->sip_record_route, sip->sip_contact);
  }

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

  if (strcasecmp(ss->ss_substate, "terminated") == 0) {
    nta_leg_destroy(nea->nea_leg), nea->nea_leg = NULL;
    nea->nea_state = nea_terminated;

    if (str0casecmp(ss->ss_reason, "deactivated") == 0) {
      nea->nea_state = nea_embryonic;
      nea->nea_deadline = sip_now();
    } else if (str0casecmp(ss->ss_reason, "probation") == 0) {
      char const *retry_after;
      sip_time_t retry = sip_now() + NEA_TIMER_DELTA;

      retry_after = msg_params_find(ss->ss_params, "retry-after=");
      if (retry_after)
	retry += strtoul(retry_after, NULL, 10);
      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;
    }
  }
  else if (strcasecmp(ss->ss_substate, "pending") == 0) 
    nea->nea_state = nea_pending;
  else if (strcasecmp(ss->ss_substate, "active") == 0) 
    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;
    su_timer_set_at(nea->nea_timer, 
		    nea_expires_renew, 
		    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 ||
      nea->nea_deadline == 0 || 
      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));
    
  return;
}