nea_server.c 63.4 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
 * 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
 *
 */

/**@file nea_server.c
 * @brief Nokia Event API - event notifier implementation.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Martti Mela <Martti.Mela@nokia.com>
 *
 * @date Created: Wed Feb 14 18:37:04 EET 2001 ppessi
 */

#include "config.h"

36 37 38 39 40
#include <sofia-sip/sip.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
41

42
#include "nea_debug.h"
Pekka Pessi's avatar
Pekka Pessi committed
43 44 45 46 47 48 49 50 51 52 53

#define NONE ((void *)- 1)

#define SU_ROOT_MAGIC_T      struct nea_server_s
#define SU_MSG_ARG_T         tagi_t

#define NTA_AGENT_MAGIC_T    struct nea_server_s
#define NTA_LEG_MAGIC_T      struct nea_sub_s
#define NTA_INCOMING_MAGIC_T struct nea_sub_s
#define NTA_OUTGOING_MAGIC_T struct nea_sub_s

54 55 56 57 58 59 60 61 62 63
#include <sofia-sip/nea.h>
#include <sofia-sip/htable.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
64

65
/** Number of primary views (with different MIME type or content) */
Pekka Pessi's avatar
Pekka Pessi committed
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
#define NEA_VIEW_MAX (8)

/** Server object, created for every notifier.
 */
struct nea_server_s {
  su_home_t                 nes_home[1];
  su_root_t                *nes_root;
  su_timer_t               *nes_timer;
  
  nta_agent_t              *nes_agent;
  nta_leg_t                *nes_leg;

  nea_sub_t                *nes_subscribers;

  sip_require_t            *nes_require;

  sip_time_t                nes_min_expires;
  sip_time_t                nes_expires;
  sip_time_t                nes_max_expires;

  int                       nes_max_subs;
  unsigned                  nes_throttle; /**< Default throttle */
  unsigned                  nes_min_throttle; /**< Minimum throttle */
  unsigned                  nes_eventlist:1; /**< Eventlist only */
  unsigned                  nes_in_callback : 1;
  unsigned                  nes_pending_destroy : 1;
  unsigned                  nes_pending_flush : 1;
  unsigned                  nes_202_before_notify:1;

  unsigned                  nes_in_list;

  unsigned                  nes_throttled; /**< Throttled notifications? */

  char const               *nes_server;

  sip_contact_t            *nes_eventity_uri;
  sip_allow_events_t       *nes_allow_events;

104 105
  sip_allow_t              *nes_allow_methods;

Pekka Pessi's avatar
Pekka Pessi committed
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
  nea_new_event_f          *nes_callback;
  nea_smagic_t             *nes_context;

  /** Events.  
   * Each subscriber will be added to one of these. */ 
  nea_event_t              *nes_events;
};


/** Supported events and their subscribers  */
struct nea_event_s {
  nea_event_t              *ev_next;
  nea_event_t             **ev_prev;

  nea_watcher_f            *ev_callback;
  nea_emagic_t             *ev_magic;

  unsigned                  ev_throttle; /**< Default throttle */
  unsigned                  ev_min_throttle; /**< Minimum throttle */
  unsigned                  ev_eventlist:1; /**< Eventlist is supported */
  unsigned                  ev_reliable:1;  /**< Keep all notifications */
  unsigned                  :0;

  /** Sequence number of first unsent update */
  unsigned                  ev_throttling;
  unsigned                  ev_updated;	/**< Sequence number for updates */
  sip_require_t            *ev_require; /**< Required features */
  sip_supported_t          *ev_supported; /**< Supported features */

  sip_event_t              *ev_event;
  sip_accept_t const       *ev_default;/**< Default content type */
  sip_accept_t const       *ev_accept; /**< Supported content types */

  nea_event_view_t         *ev_views[NEA_VIEW_MAX + 1];
};

typedef struct nea_event_queue_s nea_event_queue_t;

/** Object representing particular view of event */
struct nea_event_view_s
{
  nea_event_view_t *evv_next;
  nea_event_view_t *evv_primary; 	/**< Backpointer to the primary view */

  nea_evmagic_t    *evv_magic;

  unsigned          evv_throttle; /**< Default throttle */
  unsigned          evv_min_throttle; /**< Minimum throttle */
  unsigned          evv_fake:1;		/**< This is "fake" (ie. default) view */
  unsigned          evv_private:1;	/**< This is private view */
  unsigned          evv_reliable:1;     /**< Keep all notifications */
  unsigned:0;

  /** Queued notification */
  struct nea_event_queue_s
  {
    nea_event_queue_t  *evq_next;
    unsigned            evq_updated;
    unsigned            evq_version;
    sip_content_type_t *evq_content_type;
    sip_payload_t      *evq_payload;
  } evv_head[1];
};

#define evv_version evv_head->evq_version
#define evv_updated evv_head->evq_updated
#define evv_content_type evv_head->evq_content_type
#define evv_payload evv_head->evq_payload


/** Subscription object 
 */
struct nea_sub_s {
  nea_sub_t        *s_next;
  nea_sub_t       **s_prev;
  
  nta_leg_t        *s_leg;
  nta_incoming_t   *s_irq;
  nta_outgoing_t   *s_oreq;
  
  nea_server_t     *s_nes;

  sip_contact_t    *s_local;	/**< Local contact */

  sip_from_t       *s_from;
  sip_contact_t    *s_remote;	/**< Remote contact  */
Pekka Pessi's avatar
Pekka Pessi committed
192
  /* sip_accept_t  *s_accept; */
Pekka Pessi's avatar
Pekka Pessi committed
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
  sip_event_t      *s_id;

  nea_event_t      *s_event;
  nea_event_view_t *s_view;
  nea_state_t       s_state;

  char const       *s_extended;

  sip_content_type_t *s_content_type; /** Content-Type of SUBSCRIBE body. */
  sip_payload_t    *s_payload;      /**< Body of SUBSCRIBE. */

  unsigned          s_processing : 1;
  unsigned          s_rejected : 1; 
  unsigned          s_pending_flush : 1;
  unsigned          s_garbage : 1;
  unsigned          s_fake : 1; /**< Do not send real information to user */
  unsigned          s_eventlist : 1; /**< Subscriber supported eventlist */

  sip_time_t        s_subscribed; /**< When first SUBSCRIBE was recv */
  sip_time_t        s_notified; /**< When last notification was sent */
  sip_time_t        s_expires;  /**< Expiration time. */

  unsigned          s_version;	/**< Version number set by application */

  unsigned          s_latest;	/**< External version of latest payload */
  unsigned          s_updated;	/**< Internal version of latest payload */
  unsigned          s_throttle;	/**< Minimum time between notifications */
};

/* Prototypes */

static void nea_server_pending_flush(nea_server_t *nes);

static int nea_view_update(nea_server_t *nes,
			   nea_event_t *ev,
			   nea_event_view_t **evvp,
			   int private,
			   int fake,
			   tag_type_t tag,
			   tag_value_t value,
			   ...);

static nea_sub_t *nea_sub_create(nea_server_t *nes);
static int nea_sub_is_removed(nea_sub_t const *s);
static void nea_sub_remove(nea_sub_t *s);
static void nea_sub_destroy(nea_sub_t *s);

static
int nea_server_callback(nea_sub_t *nes_as_sub,
			nta_leg_t *leg,
			nta_incoming_t *irq, 
			sip_t const *sip);

static int nea_sub_process_incoming(nea_sub_t *s,
				    nta_leg_t *leg,
				    nta_incoming_t *irq,
				    sip_t const *sip);

static int nea_sub_process_subscribe(nea_sub_t *s,
				     nta_leg_t *leg,
				     nta_incoming_t *irq,
				     sip_t const *sip);

static int nea_sub_notify(nea_server_t *nes, 
			  nea_sub_t *s, 
			  sip_time_t now, 
			  tag_type_t tag, tag_value_t value, ...);

static int response_to_notify(nea_sub_t *s,
			      nta_outgoing_t *oreq,
			      sip_t const *sip);

static void nes_event_timer(nea_server_t *nes,
			    su_timer_t *timer,
			    su_timer_arg_t *arg);

static int nea_view_queue(nea_server_t *nes, 
			  nea_event_view_t *evv, 
			  nea_event_queue_t *evq);

/** Assign an event view to subscriber. */
static inline
void nea_sub_assign_view(nea_sub_t *s, nea_event_view_t *evv)
{
  if (s->s_view != evv)
    /* Make sure we send a notification */
    s->s_updated = evv->evv_updated - 1;
  s->s_view = evv;
  s->s_throttle = evv->evv_throttle;
}

static inline 
void nea_subnode_init(nea_subnode_t *sn, nea_sub_t *s, sip_time_t now)
{
  sn->sn_state = s->s_state;
  sn->sn_fake = s->s_fake;
  sn->sn_subscriber = s;
  sn->sn_event = s->s_event;
  sn->sn_remote = s->s_from;
  sn->sn_contact = s->s_remote;
  sn->sn_content_type = s->s_content_type;
  sn->sn_payload = s->s_payload;
  if (s->s_expires != 0 && (int)(s->s_expires - now) > 0)
    sn->sn_expires = s->s_expires - now;
  else
    sn->sn_expires = 0;
  sn->sn_latest = s->s_latest;
  sn->sn_throttle = s->s_throttle;
  sn->sn_eventlist = s->s_eventlist;
  sn->sn_version = s->s_version;
  sn->sn_subscribed = now - s->s_subscribed;
  sn->sn_notified = s->s_notified;
  sn->sn_view = s->s_view;
}

/** Create an event server.
 *
 * The function nea_server_create() initializes an event server object and
 * registers it with @b nta. An event server object takes care of all events
 * for a particular URI (@em eventity).
 * 
 * @param agent       pointer to an @b nta agent object
 * @param root        pointer to an @b root object
 *@param url         url of the server to be created
 * @param max_subs    maximum number of subscriptions
 * @param callback    authorization function, 
 *                    or @c NULL if no authorization is required
 * @param context     server context (pointer to application data)
 * @param tag, value, ... optional list of tag parameters
 *
 * @TAGS
 * The function nea_server_create() takes the following tag values as its 
 * arguments:
 * <dl>
 *
 * <dt>SIPTAG_CONTACT() or SIPTAG_CONTACT_STR()
 * <dd>The target address of the event server.
 *
 * <dt>SIPTAG_ALLOW_EVENTS()
 * <dd>The initial list of events supported by eventity. This list is
 * extended whenever a new event is created with nea_event_tcreate().
 *
 * <dt>SIPTAG_SERVER_STR()
 * <dd>The @b Server header for the event server.
 *
 * <dt>NEATAG_MINSUB()
 * <dd>Minimum duration of a subscription.
 * 
 * <dt>NEATAG_THROTTLE()
 * <dd>Default value for event throttle (by default, 5 seconds).
343 344 345 346 347 348
 * Throttle determines the minimum interval betweeen notifications. Note
 * that the notification indicating that the subscription has terminated
 * will be sent regardless of throttle.
 * 
 * The default throttle value is used if the subscriber does not include
 * a throttle parameter in @ref sip_event "Event" header of SUBSCRIBE request.
Pekka Pessi's avatar
Pekka Pessi committed
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
 * 
 * <dt>NEATAG_MINTHROTTLE()
 * <dd>Minimum allowed throttle value (by default, 5 seconds).
 * 
 * <dt>NEATAG_EVENTLIST()
 * <dd>If true, the subscribers must support eventlists. If SIPTAG_REQUIRE()
 * is given, it must contain the "eventlist" feature.
 *
 * <dt>NEATAG_DIALOG()
 * <dd>Give an optional NTA destination leg to event server.
 * 
 * <dt>SIPTAG_REQUIRE()/SIPTAG_REQUIRE_STR()
 * <dd>The @b Require header for the event server. The subscribers must
 * indicate support the specified features.
 *
 * </dl>
 *
 * @return
 * The function nea_server_create() returns a pointer to an event server
 * object, or @c NULL upon an error.
 */
nea_server_t *nea_server_create(nta_agent_t *agent,
				su_root_t *root,
				url_t const *url,
				int max_subs,
				nea_new_event_f *callback,
				nea_smagic_t *context,
				tag_type_t tag, tag_value_t value, ...)
{
  nea_server_t *nes = NULL;
  sip_contact_t const *contact = NULL;
  sip_allow_events_t const *allow_events = NULL;
  sip_require_t const *rq = NULL;
  char const *contact_str = NULL;
  char const *server_str = NULL;
  char const *rq_str = NULL;
  unsigned 
    min_expires = 15 * 60, 
    expires = NEA_DEFAULT_EXPIRES,
    max_expires = 24 * 60 * 60;
  nta_leg_t *leg = NONE;
  unsigned throttle = 5, min_throttle = throttle;
  int eventlist = 0;

  {
    ta_list ta;

    ta_start(ta, tag, value);

    tl_gets(ta_args(ta), 
	    SIPTAG_CONTACT_REF(contact),
	    SIPTAG_CONTACT_STR_REF(contact_str),
	    SIPTAG_ALLOW_EVENTS_REF(allow_events),
	    SIPTAG_SERVER_STR_REF(server_str),
	    SIPTAG_REQUIRE_REF(rq),
	    SIPTAG_REQUIRE_STR_REF(rq_str),
	    NEATAG_MIN_EXPIRES_REF(min_expires),
	    NEATAG_EXPIRES_REF(expires),
	    NEATAG_MAX_EXPIRES_REF(max_expires),
	    NEATAG_DIALOG_REF(leg),
	    NEATAG_THROTTLE_REF(throttle),
	    NEATAG_MINTHROTTLE_REF(min_throttle),
	    NEATAG_EVENTLIST_REF(eventlist),
	    TAG_NULL());

    ta_end(ta);
  }

  if (throttle < min_throttle)
    throttle = min_throttle;

  if (!url) {
    SU_DEBUG_5(("nea_server_create(): invalid url\n"));
    return NULL;
  }

  if (min_expires > expires || expires > max_expires) {
    SU_DEBUG_5(("nea_server_create(): invalid expiration range\n"));
    return NULL;
  }
  
430
  nes = su_home_new(sizeof(nea_server_t));
Pekka Pessi's avatar
Pekka Pessi committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

  if (nes) {
    su_home_t *home = nes->nes_home;

    nes->nes_root = root;
    nes->nes_agent = agent;

    nes->nes_max_subs = max_subs;

    nes->nes_min_expires = min_expires;
    nes->nes_expires = expires;
    nes->nes_max_expires = max_expires;

    nes->nes_throttle = throttle;
    nes->nes_min_throttle = min_throttle;

    if (allow_events)
      nes->nes_allow_events = sip_allow_events_dup(home, allow_events);
    else
      nes->nes_allow_events = sip_allow_events_make(home, "");

452 453
    nes->nes_allow_methods = sip_allow_make(home, "SUBSCRIBE");

Pekka Pessi's avatar
Pekka Pessi committed
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
    nes->nes_server = 
      su_sprintf(home, "%s%snea/" NEA_VERSION_STR " %s", 
		 server_str ? server_str : "",
		 server_str ? " " : "", 
		 nta_agent_version(agent));

    if (contact)
      nes->nes_eventity_uri = sip_contact_dup(home, contact);
    else if (contact_str)
      nes->nes_eventity_uri = sip_contact_make(home, contact_str);
    else
      nes->nes_eventity_uri = sip_contact_create(home, (url_string_t *)url, NULL);

    if (leg != NONE) {
      nes->nes_leg = leg;
      if (leg != NULL)
	nta_leg_bind(leg, nea_server_callback, (nea_sub_t*)nes);
    } else {
      nes->nes_leg = nta_leg_tcreate(agent, 
				     nea_server_callback, 
				     (nea_sub_t*)nes, 
				     NTATAG_NO_DIALOG(1),
				     NTATAG_METHOD("SUBSCRIBE"),
				     URLTAG_URL(url),
				     TAG_END());
    }
				     
    nes->nes_eventlist = eventlist; /* Every event is a list */
    if (eventlist && rq == NULL && rq_str == NULL)
      rq_str = "eventlist";

    if (rq)
      nes->nes_require = sip_require_dup(nes->nes_home, rq);
    else if (rq_str)
      nes->nes_require = sip_require_make(nes->nes_home, rq_str);

    nes->nes_timer = su_timer_create(su_root_task(nes->nes_root), 
				     nes->nes_min_throttle
				     ? 500L * nes->nes_min_throttle 
				     : 500L);

    if (nes->nes_allow_events &&
	nes->nes_eventity_uri &&
	(nes->nes_leg || leg == NULL) &&
	nes->nes_timer) {
      SU_DEBUG_5(("nea_server_create(%p): success\n", nes));
      su_timer_set(nes->nes_timer, nes_event_timer, nes);

      nes->nes_callback = callback;
      nes->nes_context = context;
    }
    else {
      SU_DEBUG_5(("nea_server_create(%p): failed\n", nes));
      nea_server_destroy(nes), nes = NULL;
    }
  }

  return nes;
}

/** Invoke the new event callback.
 *
 * The function nes_event_callback() calls the callback provided by the 
 * application using the notifier object. 
 *
 * @param nes pointer to notifier object
 * @param ev  pointer to event view
 * @param s   pointer to subscription object
 * @param sip pointer to subscribe request
 *
 * @return
 * The function nes_event_callback() returns -1 if the notifier object
 * has been destroyed by the callback function, 0 otherwise.
 */
static
int nes_new_event_callback(nea_server_t *nes, 
			   nea_event_t **ev_p, 
			   nea_event_view_t **view_p, 
			   nta_incoming_t *irq,
			   sip_t const *sip)
{
  if (nes->nes_callback)
    return nes->nes_callback(nes->nes_context, nes, ev_p, view_p, irq, sip);
  else
    return -1;
}

/** Shutdown event server.
 */
int nea_server_shutdown(nea_server_t *nes,
			int retry_after)
{
  nea_sub_t *s;
  int status = 200;
  int in_callback;

  if (nes == NULL)
    return 500;

  if (nes->nes_in_callback) {
    SU_DEBUG_5(("nea_server_shutdown(%p) while in callback\n", nes));
    return 100;
  }
  
  SU_DEBUG_5(("nea_server_shutdown(%p)\n", nes));

  in_callback = nes->nes_in_callback; nes->nes_in_callback = 1;

  for (s = nes->nes_subscribers; s; s = s->s_next) {
    if (s->s_state == nea_terminated)
      continue;
    if (s->s_pending_flush)
      continue;
    if (s->s_oreq == NULL)
      nea_sub_auth(s, nea_terminated, 
		   TAG_IF(retry_after, NEATAG_REASON("probation")),
		   TAG_IF(!retry_after, NEATAG_REASON("deactivated")),
		   TAG_IF(retry_after, NEATAG_RETRY_AFTER(retry_after)),
		   TAG_END());
    else
      status = 180;
  }    

  nes->nes_in_callback = in_callback;   

  return 200;
}

void nea_server_destroy(nea_server_t *nes)
{
  if (nes == NULL)
    return;

  if (nes->nes_in_callback) {
    SU_DEBUG_5(("nea_server_destroy(%p) while in callback\n", nes));
    nes->nes_pending_destroy = 1;
    return;
  }
  
  SU_DEBUG_5(("nea_server_destroy(%p)\n", nes));
  
  nta_leg_destroy(nes->nes_leg), nes->nes_leg = NULL;
  
  while (nes->nes_subscribers)
    nea_sub_destroy(nes->nes_subscribers);
  
  su_timer_destroy(nes->nes_timer), nes->nes_timer = NULL;
  
602
  su_home_unref(nes->nes_home);
Pekka Pessi's avatar
Pekka Pessi committed
603 604 605 606
}

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

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/**Update server payload.
 *
 * A nea event server has typed content that is delivered to the
 * subscribers. Different content types are each assigned a separate primary
 * view. There can be also primary views with "fake" content, content
 * delivered to politely blocked subscribers. 
 *
 * In addition to primary views, there can be secondary views, views
 * assigned to a single subscriber only. 
 *
 * @TAGS
 * The following tagged arguments are accepted:
 * <dl>
 *
 * <dt>SIPTAG_PAYLOAD() or SIPTAG_PAYLOAD_STR()
 * <dd>Updated event content.
 *
 * <dt>SIPTAG_CONTENT_TYPE() or SIPTAG_CONTENT_TYPE_STR().
 * <dd>MIME type of the content.
 *
 * <dt>NEATAG_FAKE(fak)
 * <dd>If @a fake is true, 'fake' view is updated.
 *
 * <dt>NEATAG_VIEW(view) 
 * <dd>If included in tagged arguments, @a view is * updated. Used when
 * updating secondary view.
 *
 * <dt>NEATAG_VERSION(version)
 * <dd>The application-provided @a version for
 * event content. After updated content has been sent to subscriber, @a
 * version is copied to subscriber information structure.
 *
 * <dt>NEATAG_EVMAGIC(context)
 * <dd>Application-provided @a context pointer.
 * The @a context pointer is returned by nea_view_magic() function.
 *
 * <dt>NEATAG_RELIABLE(reliable) 
 * <dd>The @a reliable flag determines how overlapping updates are handled. 
 * If @a reliable is true, all updates are delivered to the subscribers.
 *
 * <dt>NEATAG_THROTTLE(throttl)
 * <dd>Default value for event throttle for updated event view. Throttle
 * determines the minimum interval in seconds betweeen notifications. Note
 * that the notification indicating that the subscription has terminated
 * will be sent regardless of throttle.
 * 
 * The default throttle value is used if the subscriber does not include
 * a throttle parameter in @ref sip_event "Event" header of SUBSCRIBE request.
 * 
 * <dt>NEATAG_MINTHROTTLE()
 * <dd>Minimum allowed throttle value for updated event view.
 *
 * </dl>
Pekka Pessi's avatar
Pekka Pessi committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
 *
 * @retval -1 upon an error.
 * @retval 0  if event document was not updated.
 * @retval 1  if event document was updated.
 */
int nea_server_update(nea_server_t *nes,
		      nea_event_t *ev,
		      tag_type_t tag,
		      tag_value_t value,
		      ...)
{
  nea_event_view_t *evv = NULL;
  int fake = 0, updated;

  ta_list ta;

  if (ev == NULL)
    ev = nes->nes_events;

  ta_start(ta, tag, value);

  tl_gets(ta_args(ta),
	  NEATAG_FAKE_REF(fake),
	  NEATAG_VIEW_REF(evv),
	  TAG_NULL());
  
  updated = nea_view_update(nes, ev, &evv, 0, fake, ta_tags(ta));

  ta_end(ta);

  return updated;
}

static
int nea_view_update(nea_server_t *nes,
		    nea_event_t *ev,
		    nea_event_view_t **evvp,
		    int private,
		    int fake,
		    tag_type_t tag,
		    tag_value_t value,
		    ...)
{
  ta_list ta;

  su_home_t *home = nes->nes_home;

  sip_content_type_t const *ct = NULL;
  char const *cts = NULL, *pls = NULL;
  sip_payload_t const *pl = NULL;
  sip_payload_t *new_pl;
  nea_event_view_t *evv, **eevv = &evv;
  nea_event_view_t *primary = NULL, **primary_p = &primary;
  unsigned version = UINT_MAX;
  nea_evmagic_t *evmagic = NULL;
  int reliable = ev->ev_reliable;
  unsigned throttle = ev->ev_throttle;
  unsigned min_throttle = ev->ev_min_throttle;

  nea_event_queue_t evq[1] = {{ NULL }};

  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_PAYLOAD_STR_REF(pls),
	  NEATAG_VERSION_REF(version),
	  NEATAG_EVMAGIC_REF(evmagic),
	  NEATAG_RELIABLE_REF(reliable),
	  NEATAG_THROTTLE_REF(throttle),
	  NEATAG_MINTHROTTLE_REF(min_throttle),
	  TAG_NULL());
  
  ta_end(ta);

737 738 739
  if (min_throttle < throttle)
    min_throttle = throttle;

Pekka Pessi's avatar
Pekka Pessi committed
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 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 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 1008
  if (ct == NULL && cts == NULL)
    return -1;

  if (ct)
    cts = ct->c_type;

  evv = *evvp;

  if (!evv) {
    int i;

    /* Check if the payload type already exists */
    for (i = 0; (evv = ev->ev_views[i]); i++)
      if (str0casecmp(cts, evv->evv_content_type->c_type) == 0)
	break;
    
    if (private && evv == NULL) /* No private view without primary view. */
      return -1;

    if (i == NEA_VIEW_MAX)	/* Too many primary views. */
      return -1;

    primary_p = eevv = ev->ev_views + i;
    
    /* Search for fakeness/eventlist/private view */
    if (evv && (private || evv->evv_private || evv->evv_fake != fake)) {
      for (eevv = &evv->evv_next; (evv = *eevv); eevv = &evv->evv_next) {
	if (private || evv->evv_private)
	  continue;
	if (evv->evv_fake == fake)
	  break;
      }
    }
  }
   
  /* New event view, allocate and link to chain */
  if (!evv) {
    sip_content_type_t *new_ct;

    evv = su_zalloc(home, sizeof (*evv));
    if (!evv)
      return -1;
    
    new_pl = pl ? sip_payload_dup(home, pl)
      : sip_payload_make(home, pls);
    
    new_ct = ct ? sip_content_type_dup(home, ct) 
      : sip_content_type_make(home, cts);
    
    if ((!new_pl && pl) || !new_ct) {
      su_free(home, evv); su_free(home, new_pl);
      return -1;
    }

    *evvp = *eevv = evv;
      
    evv->evv_primary = *primary_p;
    evv->evv_private = private != 0;
    evv->evv_fake = fake != 0;
    evv->evv_reliable = reliable != 0;
    evv->evv_magic = evmagic;
    evv->evv_content_type = new_ct;
    evv->evv_payload = new_pl;
    evv->evv_throttle = throttle;
    evv->evv_min_throttle = min_throttle;

    assert(evv->evv_content_type);
  }
  else {
    if (pl && 
	evv->evv_payload && 
	evv->evv_payload->pl_len == pl->pl_len &&
	memcmp(evv->evv_payload->pl_data, pl->pl_data, pl->pl_len) == 0)
      return 0;
    if (!pl && pls && evv->evv_payload && 
	evv->evv_payload->pl_len == strlen(pls) && 
	memcmp(evv->evv_payload->pl_data, pls, evv->evv_payload->pl_len) == 0)
      return 0;
    if (!pl && !pls && !evv->evv_payload)
      return 0;

    *evq = *evv->evv_head;

    new_pl = pl ? sip_payload_dup(home, pl) : sip_payload_make(home, pls);

    if (!new_pl && (pl || pls))
      return -1;
    
    evv->evv_payload = new_pl;
  }

  if (version != UINT_MAX)
    evv->evv_version = version;

  if (!fake)
    evv->evv_updated = ++ev->ev_updated;

  if (evq->evq_content_type)
    nea_view_queue(nes, evv, evq);

  SU_DEBUG_7(("nea_server_update(%p): %s (%s)\n", 
	      nes, ev->ev_event->o_type, evv->evv_content_type->c_type));

  return 1;
}

nea_event_view_t *nea_view_create(nea_server_t *nes,
				  nea_event_t *ev,
				  nea_evmagic_t *magic,
				  tag_type_t tag,
				  tag_value_t value,
				  ...)
{
  nea_event_view_t *evv = NULL;
  ta_list ta;

  if (ev == NULL)
    return NULL;

  ta_start(ta, tag, value);

  nea_view_update(nes, ev, &evv, 1, 0, ta_tags(ta));
			 
  ta_end(ta);

  return evv;
}

void nea_view_destroy(nea_server_t *nes, nea_event_view_t *evv)
{
  nea_event_view_t **evvp;
  nea_sub_t *s;

  if (nes == NULL || evv == NULL || !evv->evv_private)
    return;

  assert(evv->evv_primary && evv != evv->evv_primary);

  for (evvp = &evv->evv_primary->evv_next; *evvp; evvp = &(*evvp)->evv_next)
    if (*evvp == evv) {
      *evvp = evv->evv_next;
      break;
    }

  for (s = nes->nes_subscribers; s; s = s->s_next)
    if (s->s_view == evv)
      nea_sub_assign_view(s, evv->evv_primary);

  su_free(nes->nes_home, evv->evv_content_type);
  su_free(nes->nes_home, evv->evv_payload);
  su_free(nes->nes_home, evv);
}

nea_evmagic_t *nea_view_magic(nea_event_view_t const *evv)
{
  return evv ? evv->evv_magic : NULL;
}

void nea_view_set_magic(nea_event_view_t *evv, nea_evmagic_t *magic)
{
  if (evv)
    evv->evv_magic = magic;
}

unsigned nea_view_version(nea_event_view_t const *evv)
{
  return evv ? evv->evv_version : 0;
}

/** Get primary, non-fake event view for given content type  */
nea_event_view_t *nea_event_view(nea_event_t *ev, char const *content_type)
{
  int i;
  nea_event_view_t *evv;

  /* Check if the payload type already exists */
  for (i = 0; ev->ev_views[i]; i++)
    if (str0casecmp(content_type, ev->ev_views[i]->evv_content_type->c_type) == 0)
      break;

  for (evv = ev->ev_views[i]; evv; evv = evv->evv_next)
    if (!evv->evv_fake)
      return evv;

  return ev->ev_views[i];
}

/** Get the content type for event view */
sip_content_type_t const *nea_view_content_type(nea_event_view_t const *evv)
{
  return evv ? evv->evv_content_type : NULL;
}


/** Queue an old notification if needed. */
static
int nea_view_queue(nea_server_t *nes, 
		   nea_event_view_t *evv, 
		   nea_event_queue_t *evq)
{
  nea_sub_t *s = NULL;

  assert(nes && evv && evq);

  if (evv->evv_reliable)
    for (s = nes->nes_subscribers; s; s = s->s_next) {
      if (s->s_view != evv)
	continue;
      if (s->s_updated > evq->evq_updated)
	continue;
      if (s->s_updated == evq->evq_updated && s->s_oreq == NULL)
	continue;
      break;			/* This  */
    }
  
  if (s) {
    nea_event_queue_t *evq0 = su_alloc(nes->nes_home, sizeof *evq);

    if (evq0 == NULL)
      return -1;

    *evq0 = *evq, evq = evq0;

    /* evq should be copy of old head but with changed payload  */
    assert(evq->evq_next == evv->evv_head->evq_next); 

    evv->evv_head->evq_next = evq;     /* insert to the queue */

    return 0;
  }

  su_free(nes->nes_home, (void *)evq->evq_payload);

  return 0;
}

/** Remove old unneeded notifications. */
static
int nea_view_dequeue(nea_server_t *nes, 
		     nea_event_t *ev)
{
  int i;
  nea_event_view_t *evv;
  nea_event_queue_t **prev, *evq;;

  assert(nes && ev);

  for (i = 0; ev->ev_views[i]; i++) {
    for (evv = ev->ev_views[i]; evv; evv = evv->evv_next) {
      if (!evv->evv_reliable)
	continue;

      for (prev = &evv->evv_head->evq_next; *prev; prev = &(*prev)->evq_next)
	if (ev->ev_throttling >= (*prev)->evq_updated)
	  break;

      /* Free from evq onwards */
      for (evq = *prev; evq; evq = *prev) {
	*prev = evq->evq_next;
	su_free(nes->nes_home, evq->evq_payload);
	su_free(nes->nes_home, evq);
      }
    }
  }

  return 0;
}

/* ----------------------------------------------------------------- */
1009

Pekka Pessi's avatar
Pekka Pessi committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
/** Notify watchers.
 *
 * @return 
 * The function nea_server_notify() returns number of subscribers that the
 * notification could be sent, or -1 upon an error.
 */
int nea_server_notify(nea_server_t *nes, nea_event_t *ev)
{
  sip_time_t now = sip_now();
  nea_sub_t *s;
  int notified = 0, throttled = nes->nes_throttled;

  SU_DEBUG_7(("nea_server_notify(%p): %s\n", nes, ev ? ev->ev_event->o_type: ""));

  ++nes->nes_in_list;

  nes->nes_throttled = 0;

  if (ev == NULL)
    for (ev = nes->nes_events; ev; ev = ev->ev_next)
      ev->ev_throttling = UINT_MAX;
  else
    ev->ev_throttling = UINT_MAX;

  for (s = nes->nes_subscribers; s; s = s->s_next) {
    if ((ev == NULL || ev == s->s_event) && s->s_state != nea_terminated) {
      notified += nea_sub_notify(nes, s, now, TAG_END());
    }
  }

  if (throttled) {
    /* Dequeue throttled updates */
    if (ev == NULL)
      for (ev = nes->nes_events; ev; ev = ev->ev_next) {
	nea_view_dequeue(nes, ev);
	SU_DEBUG_3(("nea_server(): notified %u, throttling at %u\n", 
		    notified, ev->ev_throttling));
      }
    else {
      SU_DEBUG_3(("nea_server(): notified %u, throttling at %u\n", 
		  notified, ev->ev_throttling));
      nea_view_dequeue(nes, ev);
    }
  }

  if (--nes->nes_in_list == 0 && nes->nes_pending_flush)
    nea_server_pending_flush(nes);

  return notified;
}


/* ----------------------------------------------------------------- */
void nea_server_flush(nea_server_t *nes, nea_event_t *event)
{
  nea_sub_t *s, **ss;
  sip_time_t now;

  if (nes == NULL)
    return;

  now = sip_now();

  for (ss = &nes->nes_subscribers; (s = *ss);) {
    if ((event == NULL || s->s_event == event) &&
	(s->s_state == nea_terminated || s->s_expires < now)) {
      /** On first flush, mark as garbage, remove on second flush */
      if (!s->s_garbage)
	s->s_garbage = 1;
      else if (nes->nes_in_callback || nes->nes_in_list) {
	nes->nes_pending_flush = 1;
	(*ss)->s_pending_flush = 1;
      }
      else {
	nea_sub_destroy(*ss);
	continue;
      }
    } 
    ss = &((*ss)->s_next);
  }
}


/* ----------------------------------------------------------------- */
static
void nea_server_pending_flush(nea_server_t *nes)
{
  nea_sub_t **ss;

  for (ss = &nes->nes_subscribers; *ss;) {
    if ((*ss)->s_pending_flush && !(*ss)->s_processing) {
      nea_sub_destroy(*ss);
    } else {
      ss = &((*ss)->s_next);
    }
  }

  nes->nes_pending_flush = 0;
}

/* ----------------------------------------------------------------- */
nea_sub_t *nea_sub_create(nea_server_t *nes)
{
  nea_sub_t *s;

  assert(nes);

  s = su_zalloc(nes->nes_home, sizeof (*s));

  if (s) {
    s->s_nes = nes;
    if ((s->s_next = nes->nes_subscribers))
      s->s_next->s_prev = &s->s_next;
    s->s_prev = &nes->nes_subscribers;
    nes->nes_subscribers = s;

    /* Copy default values */
    s->s_throttle = nes->nes_throttle;	
  }

  return s;
}

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
/* ----------------------------------------------------------------- */
nta_incoming_t *nea_subnode_get_incoming(nea_subnode_t *sn)
{
  assert(sn);

  if (sn->sn_subscriber) {
    return sn->sn_subscriber->s_irq;
  }
  return NULL;
}
Pekka Pessi's avatar
Pekka Pessi committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 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 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

/* ----------------------------------------------------------------- */
void nea_sub_remove(nea_sub_t *s)
{
  if (s) {
    assert(s->s_prev);

    if ((*s->s_prev = s->s_next))
      s->s_next->s_prev = s->s_prev;

    s->s_prev = NULL;
    s->s_next = NULL;
  }
}

/* ----------------------------------------------------------------- */
/**Check if subscriber has been removed from list */
static int nea_sub_is_removed(nea_sub_t const *s)
{
  return s->s_prev == NULL;
}

/* ----------------------------------------------------------------- */
void nea_sub_destroy(nea_sub_t *s)
{
  if (s) {
    nea_sub_t *del =  s;
    su_home_t *home = del->s_nes->nes_home;

    if (!nea_sub_is_removed(del))
      nea_sub_remove(del);

    del->s_event = NULL;

    su_free(home, del->s_local), del->s_local = NULL;
    su_free(home, del->s_remote), del->s_remote = NULL;

    if (del->s_oreq) 
      nta_outgoing_destroy(del->s_oreq), del->s_oreq = NULL;
    if (del->s_leg) 
      nta_leg_destroy(del->s_leg), del->s_leg = NULL;
    if (del->s_from)
      su_free(home, del->s_from), del->s_from = NULL;

    su_free(home, del);
  }
}

/** Create a new event.
 *
 * The function nea_event_create() creates a new event for the event server.
 */
nea_event_t *nea_event_create(nea_server_t *nes,
			      nea_watcher_f *callback,
			      nea_emagic_t *context,
			      char const *name, 
			      char const *subname,
			      char const *default_content_type,
			      char const *accept)
{
  return nea_event_tcreate(nes, callback, context, 
			   name, subname, 
			   SIPTAG_CONTENT_TYPE_STR(default_content_type), 
			   SIPTAG_ACCEPT_STR(accept),
			   TAG_END());
}

/** Create a new event (or subevent) with tags */
nea_event_t *nea_event_tcreate(nea_server_t *nes,
			       nea_watcher_f *callback,
			       nea_emagic_t *context,
			       char const *name, 
			       char const *subname,
			       tag_type_t tag, tag_value_t value, ...)
{
  nea_event_t *ev, **pev;
  int len = strlen(name);
  ta_list ta;

  /* Find a matching event */
  if (subname == NULL) {
    for (pev = &nes->nes_events; (ev = *pev); pev = &(*pev)->ev_next) {
      if (strcmp(ev->ev_event->o_type, name) != 0)
	continue;
      SU_DEBUG_5(("nea_event_create(): already event %s\n", name));
      return NULL;
    }
  }
  else {
    for (pev = &nes->nes_events; (ev = *pev); pev = &(*pev)->ev_next) {
      if (strncmp(ev->ev_event->o_type, name, len) != 0 ||
	  ev->ev_event->o_type[len] != '.' ||
	  strcmp(subname, ev->ev_event->o_type + len + 1) != 0)
	continue;
      SU_DEBUG_5(("nea_event_create(): already event %s.%s\n", name, subname));
      return NULL;
    }
  }

  ta_start(ta, tag, value);

  ev = su_zalloc(nes->nes_home, sizeof (*ev));

  if (ev) {
    int reliable = 0;
    sip_content_type_t const *ct = NULL;
    sip_accept_t const *ac = NULL;
    sip_supported_t const *k = NULL;
    sip_require_t const *rq = NULL;
    char const *ct_str = NULL, *ac_str = NULL, *k_str = NULL, *rq_str = NULL;
    
    unsigned throttle = nes->nes_throttle, min_throttle = nes->nes_min_throttle;
    int eventlist = nes->nes_eventlist;

    tl_gets(ta_args(ta),
	    NEATAG_RELIABLE_REF(reliable),
	    NEATAG_THROTTLE_REF(throttle),
	    NEATAG_MINTHROTTLE_REF(min_throttle),
	    NEATAG_EVENTLIST_REF(eventlist),
	    SIPTAG_CONTENT_TYPE_REF(ct),
	    SIPTAG_CONTENT_TYPE_STR_REF(ct_str),
	    SIPTAG_ACCEPT_REF(ac),
	    SIPTAG_ACCEPT_STR_REF(ac_str),
	    SIPTAG_SUPPORTED_REF(k),
	    SIPTAG_SUPPORTED_STR_REF(k_str),
	    SIPTAG_REQUIRE_REF(rq),
	    SIPTAG_REQUIRE_STR_REF(rq_str),
	    TAG_END());

    ev->ev_callback = callback;
    ev->ev_magic = context;
    ev->ev_event = sip_event_format(nes->nes_home, "%s%s%s", 
				    name, 
				    subname ? "." : "", 
				    subname ? subname : "");
 
    ev->ev_reliable = reliable != 0;
    ev->ev_throttle = throttle;
    ev->ev_min_throttle = min_throttle;
    ev->ev_eventlist = eventlist;

    if (eventlist && rq == NULL && rq_str == NULL)
      rq_str = "eventlist";

    if (rq)
      ev->ev_require = sip_require_dup(nes->nes_home, rq);
    else if (rq_str)
      ev->ev_require = sip_require_make(nes->nes_home, rq_str);

    if (ev->ev_event) {
#define sip_allow_events_find(k, i) sip_params_find(k->k_items, i)
      if (!sip_allow_events_find(nes->nes_allow_events, 
				 ev->ev_event->o_type))
	sip_allow_events_add(nes->nes_home, nes->nes_allow_events, 
			     ev->ev_event->o_type);
    }

    if (ct)
      ev->ev_default = sip_accept_make(nes->nes_home, ct->c_type);
    else 
      ev->ev_default = sip_accept_make(nes->nes_home, ct_str);

    if (ac == NULL && ac_str == NULL)
      ac_str = ct ? ct->c_type : ct_str;

    if (ac)
      ev->ev_accept = sip_accept_dup(nes->nes_home, ac);
    else
      ev->ev_accept = sip_accept_make(nes->nes_home, ac_str ? ac_str : "");

    if (k)
      ev->ev_supported = sip_supported_dup(nes->nes_home, k);
    else if (k_str)
      ev->ev_supported = sip_supported_make(nes->nes_home, k_str);

    ev->ev_prev = pev;
    *pev = ev; 
  }

  ta_end(ta);

  return ev;
}


/* ----------------------------------------------------------------- */
/** Return magic context bound to nea_event.
 *
 * The function returns the magic context bound to the event.
 *
 * @param ev pointer to event object
 *
 * @return
 * The function nea_emagic_get() returns the magic context
 * bound to the event.
 */
nea_emagic_t *nea_emagic_get(nea_event_t *ev)
{
  assert(ev);

  return ev->ev_magic;
}


/* ----------------------------------------------------------------- */
/** Get named event */
nea_event_t *nea_event_get(nea_server_t const *nes, char const *e)
{
  nea_event_t *ev = NULL;

  for (ev = nes->nes_events; ev; ev = ev->ev_next)
    if (e == NULL || strcmp(ev->ev_event->o_type, e) == 0)
      break;

  return ev;
}

1360 1361 1362 1363
/* ----------------------------------------------------------------- */
nta_incoming_t *nea_sub_get_request(nea_sub_t *sub)
{
  assert(sub);
Pekka Pessi's avatar
Pekka Pessi committed
1364

1365 1366
  return sub->s_irq;
}
Pekka Pessi's avatar
Pekka Pessi committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419

/** Invoke the event callback.
 *
 * The function nes_watcher_callback() calls the callback provided by the 
 * application using the notifier object. 
 *
 * @param nes pointer to notifier object
 * @param ev  pointer to event view
 * @param s   pointer to subscription object
 * @param sip pointer to subscribe request
 *
 * @return
 * The function nes_watcher_callback() returns -1 if the notifier object
 * has been destroyed by the callback function, 0 otherwise.
 */
static
int nes_watcher_callback(nea_server_t *nes, 
			 nea_event_t *ev, 
			 nea_sub_t *s, 
			 sip_t const *sip)
{
  if (!nes->nes_in_callback) {
    nes->nes_in_callback = 1;
    if (ev->ev_callback) {
      nea_subnode_t sn[1];

      nea_subnode_init(sn, s, sip_now());

      ev->ev_callback(nes, ev->ev_magic, ev, sn, sip);
    }
    nes->nes_in_callback = 0;

    if (nes->nes_in_list)
      return 0;
    
    if (nes->nes_pending_destroy) {
      nea_server_destroy(nes);
      return -2;
    }
    
    if (sip == NULL && nes->nes_pending_flush) {
      int flushed = s->s_pending_flush;
      nea_server_pending_flush(nes);
      if (flushed)
	return -1;
    }
  }

  return 0;
}

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

1420
#if 0
Pekka Pessi's avatar
Pekka Pessi committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
/** Process incoming SUBSCRIBE message.
 *
 * The function nea_server_add() is called when the notifier receives a
 * SUBSCRIBE request without existing event dialog.
 * 
 * @param nes pointer to notifier
 * @param local_target optional contact header 
 * @param msg pointer to request message
 * @param sip pointer to SIP view to request message
 *
 * @return 
 * The function nea_server_add() returns 0 if successful, -1 upon an
 * error.
 *  
 */
int nea_server_add(nea_server_t *nes, 
		   sip_contact_t const *local_target,
		   msg_t *msg, sip_t *sip)
{
  su_home_t *home = nes->nes_home;
  nea_sub_t *s = NULL;
  url_t target[1];

  s = nea_sub_create(nes);

  s->s_from = sip_from_dup(home, sip->sip_from);

  if (local_target == NULL)
    local_target = nes->nes_eventity_uri;

  s->s_local = sip_contact_dup(nes->nes_home, local_target);

  *target = *local_target->m_url;

  s->s_leg = nta_leg_tcreate(nes->nes_agent, nea_sub_process_incoming, s,
			     SIPTAG_CALL_ID(sip->sip_call_id),
			     SIPTAG_FROM(sip->sip_to), /* local address */
			     SIPTAG_TO(sip->sip_from), /* remote address */
			     URLTAG_URL(target),
			     TAG_END());

  if (s->s_local && s->s_leg) {
    nta_leg_tag(s->s_leg, NULL);
    return 0;
  }
  else {
    nea_sub_destroy(s);
    return -1;
  }
}
1471
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612

static
int nea_server_callback(nea_sub_t *nes_as_sub,
			nta_leg_t *leg,
			nta_incoming_t *irq, 
			sip_t const *sip)
{
  return nea_server_add_irq((nea_server_t *)nes_as_sub, leg, NULL, irq, sip);
}

/** Process incoming request */
int nea_server_add_irq(nea_server_t *nes,
		       nta_leg_t *leg,
		       sip_contact_t const *local_target,
		       nta_incoming_t *irq, 
		       sip_t const *sip)
{
  nea_sub_t *s = NULL;
  s = nea_sub_create(nes);

  s->s_from = sip_from_dup(nes->nes_home, sip->sip_from);

  if (local_target == NULL)
    local_target = nes->nes_eventity_uri;

  s->s_local = sip_contact_dup(nes->nes_home, local_target);

  if (leg == NULL || leg == nes->nes_leg) {
    url_t target[1];
    
    *target = *local_target->m_url;

    s->s_leg = nta_leg_tcreate(nes->nes_agent, nea_sub_process_incoming, s, 
			       SIPTAG_FROM(sip->sip_to),
			       SIPTAG_TO(sip->sip_from),
			       SIPTAG_CALL_ID(sip->sip_call_id),
			       URLTAG_URL((url_string_t *)target),
			       TAG_NULL());
  }
  else {
    nta_leg_bind(s->s_leg = leg, nea_sub_process_incoming, s);
  }
  
  if (s->s_leg) {
    if (sip->sip_to->a_tag == NULL) {
      nta_leg_tag(s->s_leg, NULL);
      nta_incoming_tag(irq, nta_leg_get_tag(s->s_leg));
    }
    nta_leg_server_route(s->s_leg, sip->sip_record_route, sip->sip_contact);

    return nea_sub_process_incoming(s, s->s_leg, irq, sip);
  }
  else {
    nea_sub_destroy(s);
    return 500;
  }
}


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

/**Process incoming transactions for event dialog.
 *
 * The nea_sub_process_incoming() processes the transactions for event
 * dialog. Currently, no other methods allowed beside SUBSCRIBE. The
 * SUBSCRIBE is processed by nea_sub_process_subscribe().
 *
 * @param s   pointer to subscriber object
 * @param leg pointer to NTA dialog object
 * @param irq pointer to NTA server transaction
 * @param sip pointer to structure containing SIP headers of the request
 *
 * The nea_sub_process_incoming() returns 0 if successful, SIP error code
 * otherwise.
 */
int nea_sub_process_incoming(nea_sub_t *s,
			     nta_leg_t *leg,
			     nta_incoming_t *irq,
			     sip_t const *sip)
{
  int retval;

  s->s_processing = 1;
  s->s_irq = irq;

  switch(sip->sip_request->rq_method) {
  case sip_method_subscribe:
    retval = nea_sub_process_subscribe(s, leg, irq, sip);
    break;

  default:
    nta_incoming_treply(irq, 
			retval = SIP_405_METHOD_NOT_ALLOWED,
			SIPTAG_ALLOW_STR("SUBSCRIBE"),
			TAG_END());
    retval = 405;
  }

  s->s_processing = 0;

  if (s->s_irq)
    nta_incoming_destroy(irq), s->s_irq = NULL;
  
  if (s->s_pending_flush || s->s_state == nea_embryonic)
    nea_sub_destroy(s);

  return retval;
}


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

/**Process incoming SUBSCRIBE transactions for event dialog.
 *
 * The function nea_sub_process_subscribe() processes the SUBSCRIBE
 * transactions for (possible) event dialog.
 *
 * @param s   pointer to subscriber object
 * @param leg pointer to NTA dialog object
 * @param irq pointer to NTA server transaction
 * @param sip pointer to structure containing SIP headers of the request
 *
 * @return
 * The function nea_sub_process_subscribe() returns 0 if successful, and a
 * SIP error code otherwise.
 */
int nea_sub_process_subscribe(nea_sub_t *s,
			      nta_leg_t *leg,
			      nta_incoming_t *irq,
			      sip_t const *sip)
{
  nea_server_t *nes = s->s_nes;
  su_home_t *home = nes->nes_home;
  nea_event_t *ev = NULL, *ev_maybe = NULL;
  nea_event_view_t *evv = NULL, *evv_maybe = NULL;
  sip_time_t delta = 0, now = sip_now();
  sip_expires_t expires[1] = { SIP_EXPIRES_INIT() };
  sip_unsupported_t *unsupported;
  sip_event_t const *o;
  sip_accept_t const *ac = NULL, *accept = NULL;
  sip_accept_t *a0 = NULL, *a, *a_next, **aa;
1613
  sip_accept_t accept_default[1];
Pekka Pessi's avatar
Pekka Pessi committed
1614 1615 1616 1617 1618 1619 1620
  unsigned proposed_throttle;
  char const *type, *throttle;
  int once, what, supported_eventlist, require_eventlist;

  if (sip->sip_payload && !sip->sip_content_type) {
    nta_incoming_treply(irq, 400, "Missing Content-Type", 
			SIPTAG_SERVER_STR(nes->nes_server),
1621 1622
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
			TAG_NULL());
    return 0;
  }

  if (sip->sip_expires && 
      sip->sip_expires->ex_delta > 0 && 
      sip->sip_expires->ex_delta < nes->nes_min_expires) {
    sip_min_expires_t me[1]; 

    sip_min_expires_init(me);

    me->me_delta = nes->nes_min_expires;

    nta_incoming_treply(irq, 423, "Subscription Interval Too Small", 
			SIPTAG_ACCEPT(accept),
			SIPTAG_MIN_EXPIRES(me),
			SIPTAG_SERVER_STR(nes->nes_server),
1640 1641
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
			TAG_NULL());
    return 0;
  }

  /* Check features */
  if (nes->nes_require) {
    unsupported = sip_has_unsupported2(nes->nes_home, 
				       sip->sip_supported,
				       sip->sip_require,
				       nes->nes_require);

    if (unsupported) {
      nta_incoming_treply(irq, SIP_421_EXTENSION_REQUIRED, 
			  SIPTAG_REQUIRE(nes->nes_require),
			  SIPTAG_UNSUPPORTED(unsupported),
			  SIPTAG_SERVER_STR(nes->nes_server),
1658 1659
			  SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			  SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
			  TAG_NULL());
      su_free(nes->nes_home, unsupported);

      return 0;
    }
  }

  supported_eventlist = sip_has_feature(sip->sip_supported, "eventlist");
  require_eventlist = sip_has_feature(sip->sip_require, "eventlist");
  supported_eventlist = supported_eventlist || require_eventlist;

  if (s->s_id && (!sip->sip_event || 
		  str0cmp(s->s_id->o_type, sip->sip_event->o_type) != 0 ||
		  str0cmp(s->s_id->o_id, sip->sip_event->o_id))) {
    /* Multiple subscriptions per dialog are not supported. */
    return nta_incoming_treply(irq, 501, 
			       "Multiple subscriptions not implemented", 
			       SIPTAG_SERVER_STR(nes->nes_server),
			       TAG_NULL());
  }

  /* Check that subscriber asks for a supported event  */
  for (once = 0; ev == NULL ;once++) {
    o = sip->sip_event;

    /* Check that we have a matching event */
    if (o && o->o_type) {
      for (ev = nes->nes_events; ev; ev = ev->ev_next) {
	if (strcmp(o->o_type, ev->ev_event->o_type) == 0) {
	  ev_maybe = ev;

	  if (ev->ev_eventlist) {
	    if (supported_eventlist)
	      break;
	  } else {
	    if (!supported_eventlist)
	      break;
	  }
	}
      }
    }

    if (!ev && !require_eventlist)
      ev = ev_maybe;
          
    if (ev || once)
      break;

    /* Ask the application either to
       1) add a new event or assing us an event/payload (0), 
       2) take care of transaction (positive), or 
       3) drop request (negative).
    */
    if ((what = nes_new_event_callback(nes, &ev, &evv, irq, sip)) < 0)
      break;
    if (what > 0) {
      s->s_irq = NULL;
      return 0;
    }
  }

  if (ev_maybe == NULL && ev == NULL) {
    nta_incoming_treply(irq, SIP_489_BAD_EVENT, 
			SIPTAG_SERVER_STR(nes->nes_server),
1724 1725
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
			NULL);
    return 0;
  } else if (ev == NULL) {
    ev = ev_maybe;

    unsupported = sip_has_unsupported(nes->nes_home, ev->ev_supported,
				      sip->sip_require);

    nta_incoming_treply(irq, SIP_420_BAD_EXTENSION, 
			SIPTAG_UNSUPPORTED(unsupported),
			SIPTAG_REQUIRE(ev->ev_require),
			SIPTAG_SUPPORTED(ev->ev_supported),
			SIPTAG_SERVER_STR(nes->nes_server),
1739 1740
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
			TAG_NULL());

    su_free(nes->nes_home, unsupported);

    return 0;
  }

  if (sip->sip_accept)
    accept = sip->sip_accept;
  else if (evv && evv->evv_content_type) {
    /* Generate accept header from event view specified by application */
1752 1753 1754
    sip_accept_init(accept_default);
    accept_default->ac_type = evv->evv_content_type->c_type;
    accept_default->ac_subtype = evv->evv_content_type->c_subtype;
Pekka Pessi's avatar
Pekka Pessi committed
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843

    accept = a0;
  }
  else
    accept = ev->ev_default;

  for (once = 0; evv == NULL ;once++) {
    /* If there are multiple accept values with different Q values,
       insertion sort by Q value */
    for (ac = accept->ac_next; ac; ac = ac->ac_next) {
      if (ac->ac_q != accept->ac_q) {
	if ((a0 = sip_accept_dup(home, accept))) {
	  /* Sort the accept list by Q values */
	  for (a = a0, accept = NULL; a; a = a_next) {
	    a_next = a->ac_next;
	    
	    for (aa = (sip_accept_t **)&accept; 
		 *aa && sip_q_value((*aa)->ac_q) >= sip_q_value(a->ac_q); 
		 aa = &(*aa)->ac_next)
	      ;
	    
	    a->ac_next = *aa; *aa = a; 	/* Insert */
	  }
	}

	break;
      }
    }

    /* Check that subscriber asks for a supported content type */
    for (ac = accept; ac; ac = ac->ac_next) {
      int i;

      if (ac->ac_type == NULL || ac->ac_subtype == NULL)
	continue;
      
      /* Check all supported content types v. accept */
      for (i = 0; (evv = ev->ev_views[i]); i++) {
	assert(evv->evv_content_type && evv->evv_content_type->c_type);
	
	if (strcmp(ac->ac_type, "*/*") == 0)
	  break;

	type = evv->evv_content_type->c_type;

	if ((strcasecmp(ac->ac_type, type) == 0) ||
	    (strcasecmp(ac->ac_subtype, "*") == 0 &&
	     strncasecmp(ac->ac_type, type, 
			 ac->ac_subtype - ac->ac_type) == 0)) {
	  if (evv_maybe == NULL)
	    evv_maybe = evv;
	}
      }

      if (evv)			/* Found */
	break;
    }

    /* Free the sorted Accept list */
    for (a = a0; a; a = a_next)
      a_next = a->ac_next, su_free(home, a); 

    if (!evv)
      evv = evv_maybe;
  
    if (evv || once)
      break;

    /* Ask the application either to
       1) add a new event view or assign us an event view (0), 
       2) take care of transaction (positive), or 
       3) drop request (negative).
    */
    if ((what = nes_new_event_callback(nes, &ev, &evv, irq, sip)) < 0)
      break;
    if (what > 0) {
      s->s_irq = NULL;
      return 0;
    }
  }

  if (evv == NULL) {
    SU_DEBUG_3(("nea_server: event %s rejected %u %s\n",
		ev->ev_event->o_type, SIP_406_NOT_ACCEPTABLE));

    /* There is no media acceptable to watcher */
    return nta_incoming_treply(irq, SIP_406_NOT_ACCEPTABLE, 
			       SIPTAG_ACCEPT(ev->ev_accept),
			       SIPTAG_SERVER_STR(nes->nes_server),
1844 1845
			       SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			       SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
			       TAG_NULL());
  }

  /* Do not change private view */
  if (s->s_view && s->s_view->evv_primary == evv)
    evv = s->s_view;

  /* Set throttle */
  if (sip->sip_event && 
      (throttle = sip_params_find(sip->sip_event->o_params, "throttle="))) {
    proposed_throttle = strtoul(throttle, NULL, 10);

    if (proposed_throttle < evv->evv_min_throttle)
      proposed_throttle = evv->evv_min_throttle;
  } else
    proposed_throttle = evv->evv_throttle;

  s->s_throttle = proposed_throttle;

  /* Update route, store remote contact */
  nta_leg_server_route(leg, sip->sip_record_route, sip->sip_contact);
  su_free(home, s->s_remote);
  s->s_remote = sip_contact_dup(home, sip->sip_contact);

  /* Store content-type and body */
  if (sip->sip_content_type) {
    su_free(home, s->s_content_type);
    s->s_content_type = sip_content_type_dup(home, sip->sip_content_type);
    su_free(home, s->s_payload);
    s->s_payload = sip_payload_dup(home, sip->sip_payload);
  }

  /* Calculate expiration time for subscription */
  delta = sip_contact_expires(NULL, sip->sip_expires, sip->sip_date,
			      nes->nes_expires, now);
  if (delta > nes->nes_max_expires)
    delta = nes->nes_max_expires;
  expires->ex_delta = delta;

  if (s->s_subscribed == 0)
    s->s_subscribed = now;
  s->s_expires = now + delta;
Pekka Pessi's avatar
Pekka Pessi committed
1888
  /* s->s_accept = sip_accept_dup(home, accept); */
1889 1890
  if (s->s_id == NULL)
    s->s_id = sip_event_dup(home, sip->sip_event);
Pekka Pessi's avatar
Pekka Pessi committed
1891 1892 1893 1894 1895 1896 1897
  s->s_event = ev;
  s->s_eventlist = supported_eventlist;
  nea_sub_assign_view(s, evv);
  s->s_updated = evv->evv_updated - 1;  /* Force notify */

  if (nes->nes_202_before_notify) {
    nta_incoming_treply(irq, SIP_202_ACCEPTED, 
1898 1899 1900
			SIPTAG_SERVER_STR(nes->nes_server),
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
			SIPTAG_REQUIRE(ev->ev_require),
			SIPTAG_SUPPORTED(ev->ev_supported),
			SIPTAG_EXPIRES(expires),
			SIPTAG_CONTACT(s->s_local),
			TAG_END());
    nta_incoming_destroy(irq), s->s_irq = irq = NULL;
  }

  /* Callback for checking subscriber authorization */
  if (nes_watcher_callback(nes, ev, s, sip) < 0) {
    if (irq) {
      nta_incoming_treply(irq, SIP_503_SERVICE_UNAVAILABLE, TAG_END());
      nta_incoming_destroy(irq);
    }
    return -1;
  }
  
  evv = s->s_view;  /* Callback can change event view */

  if (s->s_state == nea_embryonic)
    nea_sub_auth(s, nea_pending, NEATAG_FAKE(1), TAG_END());

1923
  if (s->s_updated != evv->evv_updated && !(irq && s->s_rejected))
Pekka Pessi's avatar
Pekka Pessi committed
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
    nea_sub_notify(nes, s, now, TAG_END());

  if (irq) {
    if (s->s_rejected)
      nta_incoming_treply(irq, SIP_403_FORBIDDEN, 
			  SIPTAG_SERVER_STR(nes->nes_server),
			  TAG_END());
    else if (s->s_state == nea_active)
      nta_incoming_treply(irq, SIP_200_OK, 
			  SIPTAG_REQUIRE(ev->ev_require),
			  SIPTAG_SUPPORTED(ev->ev_supported),
			  SIPTAG_EXPIRES(expires),
			  SIPTAG_SERVER_STR(nes->nes_server),
			  SIPTAG_CONTACT(s->s_local),
1938 1939
			  SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			  SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1940 1941 1942 1943 1944 1945 1946
			  TAG_END());
    else
      nta_incoming_treply(irq, SIP_202_ACCEPTED, 
			  SIPTAG_REQUIRE(ev->ev_require),
			  SIPTAG_SUPPORTED(ev->ev_supported),
			  SIPTAG_EXPIRES(expires),
			  SIPTAG_SERVER_STR(nes->nes_server),
1947 1948
			  SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			  SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
			  SIPTAG_CONTACT(s->s_local),
			  TAG_END());
  }

  return 0;
}

/* ----------------------------------------------------------------- */
/**Notify subscriber 
 *
 * The function nea_sub_notify() sends a notification to the subscriber. The
 * event type is specified by subscriber event, payload type and payload in
 * the event view. The responses to the NOTIFY transaction are
 * processed by response_to_notify().
 *
 * @param nes pointer to the notifier object
 * @param s   pointer to the subscription object
 * @param now current SIP time (if 0, no body is sent, 
 *            but updated Subscription-State header only
 * @param tag,value,... tag list
 *
 */
int nea_sub_notify(nea_server_t *nes, nea_sub_t *s, 
		   sip_time_t now,
		   tag_type_t tag, tag_value_t value, ...)
{
  int notified = 0;
  ta_list ta;
  int subscription_state_change = now == 0;
  nea_event_t *ev = s->s_event;
  nea_state_t substate = s->s_state;

1981
  if (s->s_pending_flush || (s->s_oreq && substate != nea_terminated)) {
Pekka Pessi's avatar
Pekka Pessi committed
1982 1983 1984 1985 1986
    if (ev && ev->ev_throttling > s->s_updated)
      ev->ev_throttling = s->s_updated;
    return 0;
  }

1987 1988 1989
  if (s->s_oreq)
    nta_outgoing_destroy(s->s_oreq), s->s_oreq = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
  assert(s->s_view); assert(ev);

  if (!subscription_state_change && s->s_view->evv_updated == s->s_updated) 
    return 0;

  if (now == 0)
    now = sip_now();

  if (s->s_notified + s->s_throttle > now &&
      /* Do not throttle state termination notification */
      substate != nea_terminated && 
      (long)(s->s_expires - now) > 0) {
    if (ev->ev_throttling > s->s_updated && !s->s_fake)
      ev->ev_throttling = s->s_updated;
    nes->nes_throttled++;
    return 0;
  }

  ta_start(ta, tag, value);
  {
    sip_subscription_state_t ss[1];
    char expires[32];
    sip_param_t params[] = { NULL, NULL, NULL };
    char const *reason = NULL;
    int fake = 0;
    char reason_buf[64];
    unsigned retry_after = (unsigned)-1;
    char retry_after_buf[64];
    int i = 0;
    nta_response_f *callback;
    nea_event_view_t *evv = s->s_view;
    nea_event_queue_t *evq, *n_evq;

    assert(ev);

    sip_subscription_state_init(ss);

    tl_gets(ta_args(ta), 
	    NEATAG_REASON_REF(reason),
	    NEATAG_FAKE_REF(fake), /* XXX - semantics??? */
	    NEATAG_RETRY_AFTER_REF(retry_after),
	    TAG_END());

    if (substate == nea_terminated) {
      if (reason)
	snprintf(reason_buf, sizeof(reason_buf), 
		 "reason=%s", reason), params[i++] = reason_buf;
      if (retry_after != (unsigned)-1)
	snprintf(retry_after_buf, sizeof(retry_after_buf), 
		 "retry-after=%u", retry_after), params[i++] = retry_after_buf;
    }
    else if ((long)(s->s_expires - now) <= 0) {
      substate = nea_terminated;
      params[i++] = "reason=timeout";
    }
    else {
      snprintf(expires, sizeof(expires),
	       "expires=%lu", (unsigned long)(s->s_expires - now));
      params[i++] = expires;
    }

    ss->ss_params = params;

    switch (substate) {
    case nea_extended: ss->ss_substate = s->s_extended; break;
    case nea_pending:  ss->ss_substate = "pending"; break;
    case nea_active:   ss->ss_substate = "active"; break;
    case nea_terminated: ss->ss_substate = "terminated"; break;
      /* Do not send notifys for embryonic subscriptions */      
    case nea_embryonic:
      ta_end(ta);
      return 0; 
    }

    callback = substate != nea_terminated ? response_to_notify : NULL;

    for (evq = evv->evv_head; evq->evq_next; evq = evq->evq_next) {
      if (evq->evq_next->evq_updated <= s->s_updated)
	break;
    }

    subscription_state_change = (s->s_view->evv_updated == s->s_updated);

    n_evq = evq->evq_payload ? evq : evv->evv_primary->evv_head;

    s->s_oreq = 
      nta_outgoing_tcreate(s->s_leg, 
			   callback, s, NULL, 
			   SIP_METHOD_NOTIFY, NULL,
			   SIPTAG_SUBSCRIPTION_STATE(ss),
			   SIPTAG_REQUIRE(ev->ev_require),
			   SIPTAG_SUPPORTED(ev->ev_supported),
			   SIPTAG_USER_AGENT_STR(nes->nes_server),
			   SIPTAG_CONTACT(s->s_local),
			   SIPTAG_EVENT(s->s_id),
			   TAG_IF(!subscription_state_change, 
				  SIPTAG_CONTENT_TYPE(n_evq->evq_content_type)),
			   TAG_IF(!subscription_state_change,
				  SIPTAG_PAYLOAD(n_evq->evq_payload)),
			   ta_tags(ta)); 


    notified = s->s_oreq != 0;

    if (notified) {
      s->s_notified = now;
      s->s_state = substate; /* XXX - we need state for "waiting" */
      s->s_latest = evq->evq_version;  /* Application version */
      s->s_updated = evq->evq_updated; /* Internal version */
      if (ev->ev_throttling > s->s_updated)
	ev->ev_throttling = s->s_updated;
    }

    if (callback == NULL) {
      nta_outgoing_destroy(s->s_oreq), s->s_oreq = NULL;
      /* Inform the application of a subscriber leaving the subscription. */
      nes_watcher_callback(nes, ev, s, NULL);
    }
  }
  ta_end(ta);

  return notified;
}

/* ----------------------------------------------------------------- */
/**Process responses to the NOTIFY.
 * 
 * The response_to_notify() processes the responses to the NOTIFY request. 
 * If there was an error with delivering the NOTIFY, the subscription is
 * considered terminated.
 *
 * @param s   pointer to subscription object
 */
int response_to_notify(nea_sub_t *s,
		       nta_outgoing_t *oreq,
		       sip_t const *sip)
{
  nea_server_t *nes = s->s_nes;
  int status = sip->sip_status->st_status;

  if (status < 200)
    return 0;

  nta_outgoing_destroy(s->s_oreq), s->s_oreq = NULL;

  if (status < 300) {
    if (s->s_view->evv_updated != s->s_updated) {
      sip_time_t now = sip_now();

      if (s->s_notified + s->s_throttle <= now)
	nea_sub_notify(nes, s, now, TAG_END());
      else
	nes->nes_throttled++;
    }
  }

  if (s->s_state == nea_terminated || status >= 300) {
    SU_DEBUG_5(("nea_server: removing subscriber " URL_PRINT_FORMAT "\n",
		URL_PRINT_ARGS(s->s_from->a_url)));
    /* Inform the application of a subscriber leaving the subscription. */
    nes_watcher_callback(nes, s->s_event, s, NULL);
  }

  return 0;
}

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

/** Get number of active subscribers.
 *
 * The function nea_server_active() counts the number of active subscribers
 * watching the specified view. If the view is not specified (@a ev is @c
 * NULL), it counts the number of all subscribers.
 *
 * @param nes notifier
 * @param ev  event
 * 
 * The function nea_server_active() returns number of active subscribers.
 */
int nea_server_active(nea_server_t *nes, nea_event_t const *ev)
{
  int n = 0;
  nea_sub_t *s = NULL;

  /* Count the number of subscribers watching this event */
  for (s = nes->nes_subscribers; s ; s = s->s_next)
    if (!s->s_pending_flush && s->s_state == nea_active 
	&& (ev == NULL || ev == s->s_event))
      n++;

  return n;
}

/** Get number of non-embryonic subscribers.
 *
 * The function nea_server_non_embryonic() counts the number of pending,
 * active or terminated subscribers watching the specified view. If the view
 * is not specified (@a ev is @c NULL), it counts the number of all
 * subscribers.
 *
 * @param nes notifier
 * @param ev  event view
 * 
 * The function nea_server_active() returns number of active subscribers.
 */
int nea_server_non_embryonic(nea_server_t *nes, nea_event_t const *ev)
{
  int n = 0;
  nea_sub_t *s = NULL;

  /* Count the number of subscribers watching this event */
  for (s = nes->nes_subscribers; s ; s = s->s_next)
    if (!s->s_pending_flush && s->s_state != nea_embryonic 
	&& (ev == NULL || ev == s->s_event))
      n++;

  return n;
}

/** Set application version number */
int nea_sub_version(nea_sub_t *s, unsigned version)
{
  if (s)
    return s->s_version = version;
  return 0;
}

2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
/** Authorize a subscription.
 *
 * Application can modify the subscription state and authorize the user.
 * The subscription state has following simple state diagram:
 *
 * @code
 *               +---------------+ +------------------+
 *               |	         | |       	      |
 * +-----------+ |  +---------+  V |  +------------+  V  +------------+
 * | embryonic |-+->| pending |--+-+->| authorized |--+->| terminated |
 * +-----------+    +---------+       +------------+     +------------+
 *
 * @endcode
 *
 * @TAGS
 * IF NEATAG_VIEW(view) is included in tagged arguments, @a view is assigned
 * to the subscriber and the content from the view is delivered to the
 * subscriber.
 *
 * If NEATAG_FAKE(1) is included in tags, content marked as 'fake' is
 * delivered to the subscriber.
 *
 * @retval 0 if successful
 * @retval -1 upon an error
 */
Pekka Pessi's avatar
Pekka Pessi committed
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
int nea_sub_auth(nea_sub_t *s, 
		 nea_state_t state,
		 tag_type_t tag, tag_value_t value, ...)
{

  ta_list ta;
  int retval, embryonic, rejected = 0;
  int fake = 0;
  char const *reason = NULL;
  nea_event_view_t *evv = NULL;

  if (s == NULL)
    return -1;
  if (state == nea_embryonic)
    return -1;
2257 2258
  if (state < s->s_state)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

  ta_start(ta, tag, value);

  embryonic = s->s_state == nea_embryonic;

  s->s_state = state;

  if (tl_gets(ta_args(ta), NEATAG_VIEW_REF(evv), TAG_END()) && evv) {
    nea_sub_assign_view(s, evv);
  }
  else {
    if (tl_gets(ta_args(ta), NEATAG_FAKE_REF(fake), TAG_END()))
      s->s_fake = fake;

    if (s->s_view && s->s_view->evv_fake != s->s_fake) {
      for (evv = s->s_view->evv_primary; evv; evv = evv->evv_next)
	if (!evv->evv_private && evv->evv_fake == s->s_fake) {
	  nea_sub_assign_view(s, evv);
	  break;
	}
    }
  }

  tl_gets(ta_args(ta), NEATAG_REASON_REF(reason), TAG_END());
  
  rejected = reason && strcasecmp(reason, "rejected") == 0;
  
  if (state == nea_terminated && embryonic && rejected && s->s_irq)
    retval = 0, s->s_rejected = 1;
  else
    retval = nea_sub_notify(s->s_nes, s, 0, ta_tags(ta));

  ta_end(ta);

  return retval;
}

/** Obtain a list of subscribers */
nea_subnode_t const **nea_server_get_subscribers(nea_server_t *nes, 
						 nea_event_t const *ev)
{
  nea_sub_t *s;
  nea_subnode_t **sn_list, *sn;
  int i, n;
  sip_time_t now = sip_now();

  n = nea_server_non_embryonic(nes, ev);
  if (n == 0)
    return NULL;

  sn_list = su_zalloc(nes->nes_home, 
		      (n + 1) * sizeof(sn) + n * sizeof(*sn));
  if (sn_list) {
    sn = (nea_subnode_t *)(sn_list + n + 1);

    for (i = 0, s = nes->nes_subscribers; s; s = s->s_next) {
      if (!s->s_pending_flush && s->s_state != nea_embryonic 
	  && (ev == NULL || ev == s->s_event)) {
	assert(i < n);
	nea_subnode_init(sn, s, now);
	sn_list[i++] = sn++;
      }
    }

2323 2324
    nes->nes_in_list++;

Pekka Pessi's avatar
Pekka Pessi committed
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
    sn_list[i] = NULL;
  }

  return (nea_subnode_t const **)sn_list;
}

/** Free a list of subscriptions. */
void nea_server_free_subscribers(nea_server_t *nes, 
				 nea_subnode_t const **sn_list)
{
2335
  if (sn_list) {
Pekka Pessi's avatar
Pekka Pessi committed
2336
    su_free(nes->nes_home, (void *)sn_list);
2337 2338 2339
    if (--nes->nes_in_list == 0 && nes->nes_pending_flush)
      nea_server_pending_flush(nes);
  }
Pekka Pessi's avatar
Pekka Pessi committed
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
}

/* ----------------------------------------------------------------- */
void nes_event_timer(nea_server_t *srvr,
		     su_timer_t *timer,
		     su_timer_arg_t *arg)
{
  nea_server_t *nes = (nea_server_t *) arg;
  sip_time_t now = sip_now();
  nea_sub_t *s = NULL, *s_next = NULL;
2350
  su_root_t *root = su_timer_root(timer);
Pekka Pessi's avatar
Pekka Pessi committed
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360

  su_timer_set(timer, nes_event_timer, nes);

  nes->nes_in_list++;

  /* Notify and terminate expired subscriptions */
  for (s = nes->nes_subscribers; s; s = s_next) {
    s_next = s->s_next;
    if (s->s_state == nea_terminated)
      continue;
2361 2362 2363 2364 2365
    if ((int)(now - s->s_expires) >= 0) {
      nea_sub_notify(nes, s, now, TAG_END());
      /* Yield so we can handle received packets */
      su_root_yield(root);
    }
Pekka Pessi's avatar
Pekka Pessi committed
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
  }

  if (--nes->nes_in_list == 0 && nes->nes_pending_flush)
    nea_server_pending_flush(nes);

  if (nes->nes_throttled)
    nea_server_notify(nes, NULL);

  return;
}