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

Pekka Pessi's avatar
Pekka Pessi committed
25
/**@internal @file nea_server.c
Pekka Pessi's avatar
Pekka Pessi committed
26 27 28 29 30 31 32 33 34 35
 * @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
#define NONE ((void *)(intptr_t)-1)
Pekka Pessi's avatar
Pekka Pessi committed
45 46 47 48 49 50 51 52 53

#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
#define NEA_VIEW_MAX (8)

Pekka Pessi's avatar
Pekka Pessi committed
68
/** @internal Server object, created for every notifier.
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72 73
 */
struct nea_server_s {
  su_home_t                 nes_home[1];
  su_root_t                *nes_root;
  su_timer_t               *nes_timer;
74

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

109 110
  /** Events.
   * Each subscriber will be added to one of these. */
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114
  nea_event_t              *nes_events;
};


Pekka Pessi's avatar
Pekka Pessi committed
115
/** @internal Supported events and their subscribers  */
Pekka Pessi's avatar
Pekka Pessi committed
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
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;

Pekka Pessi's avatar
Pekka Pessi committed
144
/** @internal Object representing particular view of event */
Pekka Pessi's avatar
Pekka Pessi committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158
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;

Pekka Pessi's avatar
Pekka Pessi committed
159
  /** @internal Queued notification */
Pekka Pessi's avatar
Pekka Pessi committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  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


Pekka Pessi's avatar
Pekka Pessi committed
176
/** @internal Subscription object.
Pekka Pessi's avatar
Pekka Pessi committed
177 178 179 180
 */
struct nea_sub_s {
  nea_sub_t        *s_next;
  nea_sub_t       **s_prev;
181

Pekka Pessi's avatar
Pekka Pessi committed
182 183 184
  nta_leg_t        *s_leg;
  nta_incoming_t   *s_irq;
  nta_outgoing_t   *s_oreq;
185

Pekka Pessi's avatar
Pekka Pessi committed
186 187 188 189 190 191
  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
  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. */

204 205
  unsigned          s_reported :1 ; /**< Made watcher report upon un-SUBSCRIBE */

Pekka Pessi's avatar
Pekka Pessi committed
206
  unsigned          s_processing : 1;
207
  unsigned          s_rejected : 1;
Pekka Pessi's avatar
Pekka Pessi committed
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
  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,
245
			nta_incoming_t *irq,
Pekka Pessi's avatar
Pekka Pessi committed
246 247 248 249 250 251 252 253 254 255 256 257
			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);

258 259 260
static int nea_sub_notify(nea_server_t *nes,
			  nea_sub_t *s,
			  sip_time_t now,
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263 264 265 266 267 268 269 270
			  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);

271 272
static int nea_view_queue(nea_server_t *nes,
			  nea_event_view_t *evv,
Pekka Pessi's avatar
Pekka Pessi committed
273 274 275
			  nea_event_queue_t *evq);

/** Assign an event view to subscriber. */
276
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
277 278 279 280 281 282 283 284 285
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;
}

286
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
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
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).
315
 *
Pekka Pessi's avatar
Pekka Pessi committed
316 317 318 319
 * @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
320
 * @param callback    authorization function,
Pekka Pessi's avatar
Pekka Pessi committed
321 322 323 324 325
 *                    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
326
 * The function nea_server_create() takes the following tag values as its
Pekka Pessi's avatar
Pekka Pessi committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
 * 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.
342
 *
Pekka Pessi's avatar
Pekka Pessi committed
343 344
 * <dt>NEATAG_THROTTLE()
 * <dd>Default value for event throttle (by default, 5 seconds).
345 346 347
 * Throttle determines the minimum interval betweeen notifications. Note
 * that the notification indicating that the subscription has terminated
 * will be sent regardless of throttle.
348
 *
349 350
 * The default throttle value is used if the subscriber does not include
 * a throttle parameter in @ref sip_event "Event" header of SUBSCRIBE request.
351
 *
Pekka Pessi's avatar
Pekka Pessi committed
352 353
 * <dt>NEATAG_MINTHROTTLE()
 * <dd>Minimum allowed throttle value (by default, 5 seconds).
354
 *
Pekka Pessi's avatar
Pekka Pessi committed
355 356 357 358 359 360
 * <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.
361
 *
Pekka Pessi's avatar
Pekka Pessi committed
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
 * <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;
387 388
  unsigned
    min_expires = 15 * 60,
Pekka Pessi's avatar
Pekka Pessi committed
389 390 391 392 393 394 395 396 397 398 399
    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);

400
    tl_gets(ta_args(ta),
Pekka Pessi's avatar
Pekka Pessi committed
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
	    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;
  }
431

432
  nes = su_home_new(sizeof(nea_server_t));
Pekka Pessi's avatar
Pekka Pessi committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

  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, "");

454 455
    nes->nes_allow_methods = sip_allow_make(home, "SUBSCRIBE");

456 457
    nes->nes_server =
      su_sprintf(home, "%s%snea/" NEA_VERSION_STR " %s",
Pekka Pessi's avatar
Pekka Pessi committed
458
		 server_str ? server_str : "",
459
		 server_str ? " " : "",
Pekka Pessi's avatar
Pekka Pessi committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473
		 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 {
474 475 476
      nes->nes_leg = nta_leg_tcreate(agent,
				     nea_server_callback,
				     (nea_sub_t*)nes,
Pekka Pessi's avatar
Pekka Pessi committed
477 478 479 480 481
				     NTATAG_NO_DIALOG(1),
				     NTATAG_METHOD("SUBSCRIBE"),
				     URLTAG_URL(url),
				     TAG_END());
    }
482

Pekka Pessi's avatar
Pekka Pessi committed
483 484 485 486 487 488 489 490 491
    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);

492
    nes->nes_timer = su_timer_create(su_root_task(nes->nes_root),
Pekka Pessi's avatar
Pekka Pessi committed
493
				     nes->nes_min_throttle
494
				     ? 500L * nes->nes_min_throttle
Pekka Pessi's avatar
Pekka Pessi committed
495 496 497 498 499 500
				     : 500L);

    if (nes->nes_allow_events &&
	nes->nes_eventity_uri &&
	(nes->nes_leg || leg == NULL) &&
	nes->nes_timer) {
501
      SU_DEBUG_5(("nea_server_create(%p): success\n", (void *)nes));
Pekka Pessi's avatar
Pekka Pessi committed
502 503 504 505 506 507
      su_timer_set(nes->nes_timer, nes_event_timer, nes);

      nes->nes_callback = callback;
      nes->nes_context = context;
    }
    else {
508
      SU_DEBUG_5(("nea_server_create(%p): failed\n", (void *)nes));
Pekka Pessi's avatar
Pekka Pessi committed
509 510 511 512 513 514 515 516 517
      nea_server_destroy(nes), nes = NULL;
    }
  }

  return nes;
}

/** Invoke the new event callback.
 *
518 519
 * The function nes_event_callback() calls the callback provided by the
 * application using the notifier object.
Pekka Pessi's avatar
Pekka Pessi committed
520 521 522 523 524 525 526 527 528 529 530
 *
 * @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
531 532 533
int nes_new_event_callback(nea_server_t *nes,
			   nea_event_t **ev_p,
			   nea_event_view_t **view_p,
Pekka Pessi's avatar
Pekka Pessi committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
			   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) {
556
    SU_DEBUG_5(("nea_server_shutdown(%p) while in callback\n", (void *)nes));
Pekka Pessi's avatar
Pekka Pessi committed
557 558
    return 100;
  }
559

560
  SU_DEBUG_5(("nea_server_shutdown(%p)\n", (void *)nes));
Pekka Pessi's avatar
Pekka Pessi committed
561 562 563 564 565 566 567 568 569

  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)
570
      nea_sub_auth(s, nea_terminated,
Pekka Pessi's avatar
Pekka Pessi committed
571 572 573 574 575 576
		   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;
577
  }
Pekka Pessi's avatar
Pekka Pessi committed
578

579
  nes->nes_in_callback = in_callback;
Pekka Pessi's avatar
Pekka Pessi committed
580 581 582 583 584 585 586 587 588 589

  return 200;
}

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

  if (nes->nes_in_callback) {
590
    SU_DEBUG_5(("nea_server_destroy(%p) while in callback\n", (void *)nes));
Pekka Pessi's avatar
Pekka Pessi committed
591 592 593
    nes->nes_pending_destroy = 1;
    return;
  }
594

595
  SU_DEBUG_5(("nea_server_destroy(%p)\n", (void *)nes));
596

Pekka Pessi's avatar
Pekka Pessi committed
597
  nta_leg_destroy(nes->nes_leg), nes->nes_leg = NULL;
598

Pekka Pessi's avatar
Pekka Pessi committed
599 600
  while (nes->nes_subscribers)
    nea_sub_destroy(nes->nes_subscribers);
601

Pekka Pessi's avatar
Pekka Pessi committed
602
  su_timer_destroy(nes->nes_timer), nes->nes_timer = NULL;
603

604
  su_home_unref(nes->nes_home);
Pekka Pessi's avatar
Pekka Pessi committed
605 606 607 608
}

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

609 610 611 612 613
/**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
614
 * delivered to politely blocked subscribers.
615 616
 *
 * In addition to primary views, there can be secondary views, views
617
 * assigned to a single subscriber only.
618 619 620 621 622 623 624 625 626 627 628 629 630 631
 *
 * @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.
 *
632
 * <dt>NEATAG_VIEW(view)
633 634 635 636 637 638 639 640 641 642 643 644
 * <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.
 *
645 646
 * <dt>NEATAG_RELIABLE(reliable)
 * <dd>The @a reliable flag determines how overlapping updates are handled.
647 648 649 650 651 652 653
 * 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.
654
 *
655 656
 * The default throttle value is used if the subscriber does not include
 * a throttle parameter in @ref sip_event "Event" header of SUBSCRIBE request.
657
 *
658 659 660 661
 * <dt>NEATAG_MINTHROTTLE()
 * <dd>Minimum allowed throttle value for updated event view.
 *
 * </dl>
Pekka Pessi's avatar
Pekka Pessi committed
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
 *
 * @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());
687

Pekka Pessi's avatar
Pekka Pessi committed
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
  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());
736

Pekka Pessi's avatar
Pekka Pessi committed
737 738
  ta_end(ta);

739 740 741
  if (min_throttle < throttle)
    min_throttle = throttle;

Pekka Pessi's avatar
Pekka Pessi committed
742 743 744 745 746 747 748 749 750 751 752 753 754
  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++)
755
      if (su_casematch(cts, evv->evv_content_type->c_type))
Pekka Pessi's avatar
Pekka Pessi committed
756
	break;
757

Pekka Pessi's avatar
Pekka Pessi committed
758 759 760 761 762 763 764
    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;
765

Pekka Pessi's avatar
Pekka Pessi committed
766
    /* Search for fakeness/eventlist/private view */
767
    if (evv && (private || evv->evv_private || evv->evv_fake != (unsigned)fake)) {
Pekka Pessi's avatar
Pekka Pessi committed
768 769 770
      for (eevv = &evv->evv_next; (evv = *eevv); eevv = &evv->evv_next) {
	if (private || evv->evv_private)
	  continue;
771
	if (evv->evv_fake == (unsigned)fake)
Pekka Pessi's avatar
Pekka Pessi committed
772 773 774 775
	  break;
      }
    }
  }
776

Pekka Pessi's avatar
Pekka Pessi committed
777 778 779 780 781 782 783
  /* 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;
784

Pekka Pessi's avatar
Pekka Pessi committed
785 786
    new_pl = pl ? sip_payload_dup(home, pl)
      : sip_payload_make(home, pls);
787 788

    new_ct = ct ? sip_content_type_dup(home, ct)
Pekka Pessi's avatar
Pekka Pessi committed
789
      : sip_content_type_make(home, cts);
790

Pekka Pessi's avatar
Pekka Pessi committed
791 792 793 794 795 796
    if ((!new_pl && pl) || !new_ct) {
      su_free(home, evv); su_free(home, new_pl);
      return -1;
    }

    *evvp = *eevv = evv;
797

Pekka Pessi's avatar
Pekka Pessi committed
798 799 800 801 802 803 804 805 806 807 808 809 810
    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 {
811 812
    if (pl &&
	evv->evv_payload &&
Pekka Pessi's avatar
Pekka Pessi committed
813 814 815
	evv->evv_payload->pl_len == pl->pl_len &&
	memcmp(evv->evv_payload->pl_data, pl->pl_data, pl->pl_len) == 0)
      return 0;
816 817
    if (!pl && pls && evv->evv_payload &&
	evv->evv_payload->pl_len == strlen(pls) &&
Pekka Pessi's avatar
Pekka Pessi committed
818 819 820 821 822 823 824 825 826 827 828
	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;
829

Pekka Pessi's avatar
Pekka Pessi committed
830 831 832 833 834 835 836 837 838 839 840 841
    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);

842 843
  SU_DEBUG_7(("nea_server_update(%p): %s (%s)\n", (void *)nes,
	      ev->ev_event->o_type, evv->evv_content_type->c_type));
Pekka Pessi's avatar
Pekka Pessi committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863

  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));
864

Pekka Pessi's avatar
Pekka Pessi committed
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
  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++)
919
    if (su_casematch(content_type, ev->ev_views[i]->evv_content_type->c_type))
Pekka Pessi's avatar
Pekka Pessi committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
      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
938 939
int nea_view_queue(nea_server_t *nes,
		   nea_event_view_t *evv,
Pekka Pessi's avatar
Pekka Pessi committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
		   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  */
    }
956

Pekka Pessi's avatar
Pekka Pessi committed
957 958 959 960 961 962 963 964 965
  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  */
966
    assert(evq->evq_next == evv->evv_head->evq_next);
Pekka Pessi's avatar
Pekka Pessi committed
967 968 969 970 971 972 973 974 975 976 977 978 979

    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
980
int nea_view_dequeue(nea_server_t *nes,
Pekka Pessi's avatar
Pekka Pessi committed
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 1009 1010
		     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;
}

/* ----------------------------------------------------------------- */
1011

Pekka Pessi's avatar
Pekka Pessi committed
1012 1013
/** Notify watchers.
 *
1014
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
1015 1016 1017 1018 1019 1020 1021 1022 1023
 * 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;

1024 1025
  SU_DEBUG_7(("nea_server_notify(%p): %s\n", (void *)nes,
	      ev ? ev->ev_event->o_type: ""));
Pekka Pessi's avatar
Pekka Pessi committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

  ++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);
1048
	SU_DEBUG_3(("nea_server(): notified %u, throttling at %u\n",
Pekka Pessi's avatar
Pekka Pessi committed
1049 1050 1051
		    notified, ev->ev_throttling));
      }
    else {
1052
      SU_DEBUG_3(("nea_server(): notified %u, throttling at %u\n",
Pekka Pessi's avatar
Pekka Pessi committed
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
		  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;
      }
1090
    }
Pekka Pessi's avatar
Pekka Pessi committed
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
    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 */
1130
    s->s_throttle = nes->nes_throttle;
Pekka Pessi's avatar
Pekka Pessi committed
1131 1132 1133 1134 1135
  }

  return s;
}

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
/* ----------------------------------------------------------------- */
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
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

/* ----------------------------------------------------------------- */
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;

1183
    if (del->s_oreq)
Pekka Pessi's avatar
Pekka Pessi committed
1184
      nta_outgoing_destroy(del->s_oreq), del->s_oreq = NULL;
1185
    if (del->s_leg)
Pekka Pessi's avatar
Pekka Pessi committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
      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,
1201
			      char const *name,
Pekka Pessi's avatar
Pekka Pessi committed
1202 1203 1204 1205
			      char const *subname,
			      char const *default_content_type,
			      char const *accept)
{
1206 1207 1208
  return nea_event_tcreate(nes, callback, context,
			   name, subname,
			   SIPTAG_CONTENT_TYPE_STR(default_content_type),
Pekka Pessi's avatar
Pekka Pessi committed
1209 1210 1211 1212 1213 1214 1215 1216
			   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,
1217
			       char const *name,
Pekka Pessi's avatar
Pekka Pessi committed
1218 1219 1220 1221 1222 1223
			       char const *subname,
			       tag_type_t tag, tag_value_t value, ...)
{
  nea_event_t *ev, **pev;
  ta_list ta;

1224 1225 1226
  if (nes == NULL || callback == NULL || name == NULL)
    return NULL;

Pekka Pessi's avatar
Pekka Pessi committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
  /* 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 {
1237 1238 1239 1240 1241
    size_t len = strlen(name);

    if (len == 0)
      return NULL;

Pekka Pessi's avatar
Pekka Pessi committed
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
    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;
1263

Pekka Pessi's avatar
Pekka Pessi committed
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
    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;
1284 1285 1286
    ev->ev_event = sip_event_format(nes->nes_home, "%s%s%s",
				    name,
				    subname ? "." : "",
Pekka Pessi's avatar
Pekka Pessi committed
1287
				    subname ? subname : "");
1288

Pekka Pessi's avatar
Pekka Pessi committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    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)
1304
      if (!sip_allow_events_find(nes->nes_allow_events,
Pekka Pessi's avatar
Pekka Pessi committed
1305
				 ev->ev_event->o_type))
1306
	sip_allow_events_add(nes->nes_home, nes->nes_allow_events,
Pekka Pessi's avatar
Pekka Pessi committed
1307 1308 1309 1310 1311
			     ev->ev_event->o_type);
    }

    if (ct)
      ev->ev_default = sip_accept_make(nes->nes_home, ct->c_type);
1312
    else
Pekka Pessi's avatar
Pekka Pessi committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
      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;
1329
    *pev = ev;
Pekka Pessi's avatar
Pekka Pessi committed
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 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
  }

  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;
}

1370 1371 1372 1373
/* ----------------------------------------------------------------- */
nta_incoming_t *nea_sub_get_request(nea_sub_t *sub)
{
  assert(sub);
Pekka Pessi's avatar
Pekka Pessi committed
1374

1375 1376
  return sub->s_irq;
}
Pekka Pessi's avatar
Pekka Pessi committed
1377 1378 1379

/** Invoke the event callback.
 *
1380 1381
 * The function nes_watcher_callback() calls the callback provided by the
 * application using the notifier object.
Pekka Pessi's avatar
Pekka Pessi committed
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
 *
 * @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
1393 1394 1395
int nes_watcher_callback(nea_server_t *nes,
			 nea_event_t *ev,
			 nea_sub_t *s,
1396 1397
			 sip_t const *sip,
			 sip_time_t now)
Pekka Pessi's avatar
Pekka Pessi committed
1398 1399 1400
{
  if (!nes->nes_in_callback) {
    nes->nes_in_callback = 1;
1401
    if (ev->ev_callback && !s->s_reported) {
Pekka Pessi's avatar
Pekka Pessi committed
1402 1403
      nea_subnode_t sn[1];

1404
      nea_subnode_init(sn, s, now);
1405

1406 1407
      if (sn->sn_expires == 0  || sn->sn_state == nea_terminated)
	s->s_reported = 1;
Pekka Pessi's avatar
Pekka Pessi committed
1408 1409 1410 1411 1412 1413 1414

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

    if (nes->nes_in_list)
      return 0;
1415

Pekka Pessi's avatar
Pekka Pessi committed
1416 1417 1418 1419
    if (nes->nes_pending_destroy) {
      nea_server_destroy(nes);
      return -2;
    }
1420

Pekka Pessi's avatar
Pekka Pessi committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
    if (sip == NULL && nes->nes_pending_flush) {
      int flushed = s->s_pending_flush;
      nea_server_pending_flush(nes);
      if (flushed)
	return -1;
    }
  }

  return 0;
}

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

1434
#if 0
Pekka Pessi's avatar
Pekka Pessi committed
1435 1436 1437 1438
/** Process incoming SUBSCRIBE message.
 *
 * The function nea_server_add() is called when the notifier receives a
 * SUBSCRIBE request without existing event dialog.
1439
 *
Pekka Pessi's avatar
Pekka Pessi committed
1440
 * @param nes pointer to notifier
1441
 * @param local_target optional contact header
Pekka Pessi's avatar
Pekka Pessi committed
1442 1443 1444
 * @param msg pointer to request message
 * @param sip pointer to SIP view to request message
 *
1445
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
1446 1447
 * The function nea_server_add() returns 0 if successful, -1 upon an
 * error.
1448
 *
Pekka Pessi's avatar
Pekka Pessi committed
1449
 */
1450
int nea_server_add(nea_server_t *nes,
Pekka Pessi's avatar
Pekka Pessi committed
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
		   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;
  }
}
1485
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1486 1487 1488 1489

static
int nea_server_callback(nea_sub_t *nes_as_sub,
			nta_leg_t *leg,
1490
			nta_incoming_t *irq,
Pekka Pessi's avatar
Pekka Pessi committed
1491 1492 1493 1494 1495 1496 1497 1498 1499
			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,
1500
		       nta_incoming_t *irq,
Pekka Pessi's avatar
Pekka Pessi committed
1501 1502
		       sip_t const *sip)
{
1503 1504 1505
  nea_sub_t *s = nea_sub_create(nes);
  if (s == NULL)
    return 500;
Pekka Pessi's avatar
Pekka Pessi committed
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

  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];
1516

Pekka Pessi's avatar
Pekka Pessi committed
1517 1518
    *target = *local_target->m_url;

1519
    s->s_leg = nta_leg_tcreate(nes->nes_agent, nea_sub_process_incoming, s,
Pekka Pessi's avatar
Pekka Pessi committed
1520 1521 1522 1523 1524 1525 1526 1527 1528
			       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);
  }
1529

Pekka Pessi's avatar
Pekka Pessi committed
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
  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:
1578
    nta_incoming_treply(irq,
Pekka Pessi's avatar
Pekka Pessi committed
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
			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;
1589

Pekka Pessi's avatar
Pekka Pessi committed
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
  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;
1628
  sip_accept_t accept_default[1];
Pekka Pessi's avatar
Pekka Pessi committed
1629 1630 1631 1632 1633
  unsigned proposed_throttle;
  char const *type, *throttle;
  int once, what, supported_eventlist, require_eventlist;

  if (sip->sip_payload && !sip->sip_content_type) {
1634
    nta_incoming_treply(irq, 400, "Missing Content-Type",
Pekka Pessi's avatar
Pekka Pessi committed
1635
			SIPTAG_SERVER_STR(nes->nes_server),
1636 1637
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1638 1639 1640 1641
			TAG_NULL());
    return 0;
  }

1642 1643
  if (sip->sip_expires &&
      sip->sip_expires->ex_delta > 0 &&
Pekka Pessi's avatar
Pekka Pessi committed
1644
      sip->sip_expires->ex_delta < nes->nes_min_expires) {
1645
    sip_min_expires_t me[1];
Pekka Pessi's avatar
Pekka Pessi committed
1646 1647 1648 1649 1650

    sip_min_expires_init(me);

    me->me_delta = nes->nes_min_expires;

1651
    nta_incoming_treply(irq, 423, "Subscription Interval Too Small",
Pekka Pessi's avatar
Pekka Pessi committed
1652 1653 1654
			SIPTAG_ACCEPT(accept),
			SIPTAG_MIN_EXPIRES(me),
			SIPTAG_SERVER_STR(nes->nes_server),
1655 1656
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1657 1658 1659 1660 1661 1662
			TAG_NULL());
    return 0;
  }

  /* Check features */
  if (nes->nes_require) {
1663
    unsupported = sip_has_unsupported2(nes->nes_home,
Pekka Pessi's avatar
Pekka Pessi committed
1664 1665 1666 1667 1668
				       sip->sip_supported,
				       sip->sip_require,
				       nes->nes_require);

    if (unsupported) {
1669
      nta_incoming_treply(irq, SIP_421_EXTENSION_REQUIRED,
Pekka Pessi's avatar
Pekka Pessi committed
1670 1671 1672
			  SIPTAG_REQUIRE(nes->nes_require),
			  SIPTAG_UNSUPPORTED(unsupported),
			  SIPTAG_SERVER_STR(nes->nes_server),
1673 1674
			  SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			  SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
			  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;

1686
  if (s->s_id && (!sip->sip_event ||
Pekka Pessi's avatar
Pekka Pessi committed
1687 1688 1689
		  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. */
1690 1691
    return nta_incoming_treply(irq, 501,
			       "Multiple subscriptions not implemented",
Pekka Pessi's avatar
Pekka Pessi committed
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
			       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;
1719

Pekka Pessi's avatar
Pekka Pessi committed
1720 1721 1722 1723
    if (ev || once)
      break;

    /* Ask the application either to
1724 1725
       1) add a new event or assing us an event/payload (0),
       2) take care of transaction (positive), or
Pekka Pessi's avatar
Pekka Pessi committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
       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) {
1737
    nta_incoming_treply(irq, SIP_489_BAD_EVENT,
Pekka Pessi's avatar
Pekka Pessi committed
1738
			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
			NULL);
    return 0;
  } else if (ev == NULL) {
    ev = ev_maybe;

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

1749
    nta_incoming_treply(irq, SIP_420_BAD_EXTENSION,
Pekka Pessi's avatar
Pekka Pessi committed
1750 1751 1752 1753
			SIPTAG_UNSUPPORTED(unsupported),
			SIPTAG_REQUIRE(ev->ev_require),
			SIPTAG_SUPPORTED(ev->ev_supported),
			SIPTAG_SERVER_STR(nes->nes_server),
1754 1755
			SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
			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 */
1767 1768 1769
    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
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784

    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;
1785 1786 1787

	    for (aa = (sip_accept_t **)&accept;
		 *aa && sip_q_value((*aa)->ac_q) >= sip_q_value(a->ac_q);
Pekka Pessi's avatar
Pekka Pessi committed
1788 1789
		 aa = &(*aa)->ac_next)
	      ;
1790

Pekka Pessi's avatar
Pekka Pessi committed
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
	    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;
1805

Pekka Pessi's avatar
Pekka Pessi committed
1806 1807 1808
      /* 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);
1809

Pekka Pessi's avatar
Pekka Pessi committed
1810 1811 1812 1813 1814
	if (strcmp(ac->ac_type, "*/*") == 0)
	  break;

	type = evv->evv_content_type->c_type;

1815 1816 1817 1818
	if ((su_casematch(ac->ac_type, type)) ||
	    (su_casematch(ac->ac_subtype, "*") &&
	     su_casenmatch(ac->ac_type, type,
			 ac->ac_subtype - ac->ac_type))) {
Pekka Pessi's avatar
Pekka Pessi committed
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
	  if (evv_maybe == NULL)
	    evv_maybe = evv;
	}
      }

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

    /* Free the sorted Accept list */
    for (a = a0; a; a = a_next)
1830
      a_next = a->ac_next, su_free(home, a);
Pekka Pessi's avatar
Pekka Pessi committed
1831 1832 1833

    if (!evv)
      evv = evv_maybe;
1834

Pekka Pessi's avatar
Pekka Pessi committed
1835 1836 1837 1838
    if (evv || once)
      break;

    /* Ask the application either to
1839 1840
       1) add a new event view or assign us an event view (0),
       2) take care of transaction (positive), or
Pekka Pessi's avatar
Pekka Pessi committed
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
       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 */
1856
    return nta_incoming_treply(irq, SIP_406_NOT_ACCEPTABLE,
Pekka Pessi's avatar
Pekka Pessi committed
1857 1858
			       SIPTAG_ACCEPT(ev->ev_accept),
			       SIPTAG_SERVER_STR(nes->nes_server),
1859 1860
			       SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			       SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1861 1862 1863 1864 1865 1866 1867 1868
			       TAG_NULL());
  }

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

  /* Set throttle */
1869
  if (sip->sip_event &&
Pekka Pessi's avatar
Pekka Pessi committed
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
      (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
1903
  /* s->s_accept = sip_accept_dup(home, accept); */
1904 1905
  if (s->s_id == NULL)
    s->s_id = sip_event_dup(home, sip->sip_event);
Pekka Pessi's avatar
Pekka Pessi committed
1906 1907 1908 1909 1910 1911
  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) {
1912
    nta_incoming_treply(irq, SIP_202_ACCEPTED,
1913 1914 1915
			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
1916 1917 1918 1919 1920 1921 1922 1923 1924
			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 */
1925
  if (nes_watcher_callback(nes, ev, s, sip, now) < 0) {
Pekka Pessi's avatar
Pekka Pessi committed
1926 1927 1928 1929 1930 1931
    if (irq) {
      nta_incoming_treply(irq, SIP_503_SERVICE_UNAVAILABLE, TAG_END());
      nta_incoming_destroy(irq);
    }
    return -1;
  }
1932

1933 1934


Pekka Pessi's avatar
Pekka Pessi committed
1935 1936 1937 1938 1939
  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());

1940
  if (s->s_updated != evv->evv_updated && !(irq && s->s_rejected))
Pekka Pessi's avatar
Pekka Pessi committed
1941 1942 1943 1944
    nea_sub_notify(nes, s, now, TAG_END());

  if (irq) {
    if (s->s_rejected)
1945
      nta_incoming_treply(irq, SIP_403_FORBIDDEN,
Pekka Pessi's avatar
Pekka Pessi committed
1946 1947 1948
			  SIPTAG_SERVER_STR(nes->nes_server),
			  TAG_END());
    else if (s->s_state == nea_active)
1949
      nta_incoming_treply(irq, SIP_200_OK,
Pekka Pessi's avatar
Pekka Pessi committed
1950 1951 1952 1953 1954
			  SIPTAG_REQUIRE(ev->ev_require),
			  SIPTAG_SUPPORTED(ev->ev_supported),
			  SIPTAG_EXPIRES(expires),
			  SIPTAG_SERVER_STR(nes->nes_server),
			  SIPTAG_CONTACT(s->s_local),
1955 1956
			  SIPTAG_ALLOW_EVENTS(nes->nes_allow_events),
			  SIPTAG_ALLOW(nes->nes_allow_methods),
Pekka Pessi's avatar
Pekka Pessi committed
1957 1958
			  TAG_END());
    else
1959
      nta_incoming_treply(irq, SIP_202_ACCEPTED,