loader: Process dependencies for built-in modules.
[asterisk/asterisk.git] / main / event.c
index 4855e43..1e623a1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007, Digium, Inc.
+ * Copyright (C) 2007 - 2008, Digium, Inc.
  *
  * Russell Bryant <russell@digium.com>
  *
  * \author Russell Bryant <russell@digium.com>
  */
 
-#include "asterisk.h"
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk.h"
 
 #include "asterisk/_private.h"
+
 #include "asterisk/event.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/dlinkedlists.h"
 #include "asterisk/lock.h"
 #include "asterisk/utils.h"
 #include "asterisk/unaligned.h"
+#include "asterisk/utils.h"
 #include "asterisk/taskprocessor.h"
-
-struct ast_taskprocessor *event_dispatcher;
+#include "asterisk/astobj2.h"
+#include "asterisk/cli.h"
 
 /*!
  * \brief An event information element
@@ -51,7 +55,17 @@ struct ast_event_ie {
        /*! Total length of the IE payload */
        uint16_t ie_payload_len;
        unsigned char ie_payload[0];
-} __attribute__ ((packed));
+} __attribute__((packed));
+
+/*!
+ * \brief The payload for a string information element
+ */
+struct ast_event_ie_str_payload {
+       /*! \brief A hash calculated with ast_str_hash(), to speed up comparisons */
+       uint32_t hash;
+       /*! \brief The actual string, null terminated */
+       char str[1];
+} __attribute__((packed));
 
 /*!
  * \brief An event
@@ -71,18 +85,8 @@ struct ast_event {
        uint16_t event_len:16;
        /*! The data payload of the event, made up of information elements */
        unsigned char payload[0];
-} __attribute__ ((packed));
+} __attribute__((packed));
 
-struct ast_event_ref {
-       struct ast_event *event;
-       AST_LIST_ENTRY(ast_event_ref) entry;
-};
-
-struct ast_event_iterator {
-       uint16_t event_len;
-       const struct ast_event *event;
-       struct ast_event_ie *ie;
-};
 
 struct ast_event_ie_val {
        AST_LIST_ENTRY(ast_event_ie_val) entry;
@@ -90,286 +94,165 @@ struct ast_event_ie_val {
        enum ast_event_ie_pltype ie_pltype;
        union {
                uint32_t uint;
-               const char *str;
+               struct {
+                       uint32_t hash;
+                       const char *str;
+               };
+               void *raw;
        } payload;
+       size_t raw_datalen;
 };
 
-/*! \brief Event subscription */
-struct ast_event_sub {
-       enum ast_event_type type;
-       ast_event_cb_t cb;
-       void *userdata;
-       uint32_t uniqueid;
-       AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
-       AST_RWDLLIST_ENTRY(ast_event_sub) entry;
+/*!
+ * \brief Event Names
+ */
+static const char * const event_names[AST_EVENT_TOTAL] = {
+       [AST_EVENT_ALL]                 = "All",
+       [AST_EVENT_CUSTOM]              = "Custom",
+       [AST_EVENT_MWI]                 = "MWI",
+       [AST_EVENT_SUB]                 = "Subscription",
+       [AST_EVENT_UNSUB]               = "Unsubscription",
+       [AST_EVENT_DEVICE_STATE]        = "DeviceState",
+       [AST_EVENT_DEVICE_STATE_CHANGE] = "DeviceStateChange",
+       [AST_EVENT_CEL]                 = "CEL",
+       [AST_EVENT_SECURITY]            = "Security",
+       [AST_EVENT_NETWORK_CHANGE]      = "NetworkChange",
+       [AST_EVENT_PRESENCE_STATE]      = "PresenceState",
+       [AST_EVENT_ACL_CHANGE]          = "ACLChange",
+       [AST_EVENT_PING]                = "Ping",
 };
 
-static uint32_t sub_uniqueid;
-
-/*! \brief Event subscriptions
- * The event subscribers are indexed by which event they are subscribed to */
-static AST_RWDLLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
-
-/*! \brief Cached events
- * The event cache is indexed on the event type.  The purpose of this is 
- * for events that express some sort of state.  So, when someone first
- * needs to know this state, it can get the last known state from the cache. */
-static AST_RWLIST_HEAD(ast_event_ref_list, ast_event_ref) ast_event_cache[AST_EVENT_TOTAL];
-
-static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
-{
-       if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
-               ast_free((void *) ie_val->payload.str);
-
-       ast_free(ie_val);
-}
+/*!
+ * \brief IE payload types and names
+ */
+static const struct ie_map {
+       enum ast_event_ie_pltype ie_pltype;
+       const char *name;
+} ie_maps[AST_EVENT_IE_TOTAL] = {
+       [AST_EVENT_IE_NEWMSGS]             = { AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
+       [AST_EVENT_IE_OLDMSGS]             = { AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
+       [AST_EVENT_IE_MAILBOX]             = { AST_EVENT_IE_PLTYPE_STR,  "Mailbox" },
+       [AST_EVENT_IE_UNIQUEID]            = { AST_EVENT_IE_PLTYPE_UINT, "UniqueID" },
+       [AST_EVENT_IE_EVENTTYPE]           = { AST_EVENT_IE_PLTYPE_UINT, "EventType" },
+       [AST_EVENT_IE_EXISTS]              = { AST_EVENT_IE_PLTYPE_UINT, "Exists" },
+       [AST_EVENT_IE_DEVICE]              = { AST_EVENT_IE_PLTYPE_STR,  "Device" },
+       [AST_EVENT_IE_STATE]               = { AST_EVENT_IE_PLTYPE_UINT, "State" },
+       [AST_EVENT_IE_CONTEXT]             = { AST_EVENT_IE_PLTYPE_STR,  "Context" },
+       [AST_EVENT_IE_EID]                 = { AST_EVENT_IE_PLTYPE_RAW,  "EntityID" },
+       [AST_EVENT_IE_CEL_EVENT_TYPE]      = { AST_EVENT_IE_PLTYPE_UINT, "CELEventType" },
+       [AST_EVENT_IE_CEL_EVENT_TIME]      = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTime" },
+       [AST_EVENT_IE_CEL_EVENT_TIME_USEC] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTimeUSec" },
+       [AST_EVENT_IE_CEL_USEREVENT_NAME]  = { AST_EVENT_IE_PLTYPE_STR,  "CELUserEventName" },
+       [AST_EVENT_IE_CEL_CIDNAME]         = { AST_EVENT_IE_PLTYPE_STR,  "CELCIDName" },
+       [AST_EVENT_IE_CEL_CIDNUM]          = { AST_EVENT_IE_PLTYPE_STR,  "CELCIDNum" },
+       [AST_EVENT_IE_CEL_EXTEN]           = { AST_EVENT_IE_PLTYPE_STR,  "CELExten" },
+       [AST_EVENT_IE_CEL_CONTEXT]         = { AST_EVENT_IE_PLTYPE_STR,  "CELContext" },
+       [AST_EVENT_IE_CEL_CHANNAME]        = { AST_EVENT_IE_PLTYPE_STR,  "CELChanName" },
+       [AST_EVENT_IE_CEL_APPNAME]         = { AST_EVENT_IE_PLTYPE_STR,  "CELAppName" },
+       [AST_EVENT_IE_CEL_APPDATA]         = { AST_EVENT_IE_PLTYPE_STR,  "CELAppData" },
+       [AST_EVENT_IE_CEL_AMAFLAGS]        = { AST_EVENT_IE_PLTYPE_UINT, "CELAMAFlags" },
+       [AST_EVENT_IE_CEL_ACCTCODE]        = { AST_EVENT_IE_PLTYPE_STR,  "CELAcctCode" },
+       [AST_EVENT_IE_CEL_UNIQUEID]        = { AST_EVENT_IE_PLTYPE_STR,  "CELUniqueID" },
+       [AST_EVENT_IE_CEL_USERFIELD]       = { AST_EVENT_IE_PLTYPE_STR,  "CELUserField" },
+       [AST_EVENT_IE_CEL_CIDANI]          = { AST_EVENT_IE_PLTYPE_STR,  "CELCIDani" },
+       [AST_EVENT_IE_CEL_CIDRDNIS]        = { AST_EVENT_IE_PLTYPE_STR,  "CELCIDrdnis" },
+       [AST_EVENT_IE_CEL_CIDDNID]         = { AST_EVENT_IE_PLTYPE_STR,  "CELCIDdnid" },
+       [AST_EVENT_IE_CEL_PEER]            = { AST_EVENT_IE_PLTYPE_STR,  "CELPeer" },
+       [AST_EVENT_IE_CEL_LINKEDID]        = { AST_EVENT_IE_PLTYPE_STR,  "CELLinkedID" },
+       [AST_EVENT_IE_CEL_PEERACCT]        = { AST_EVENT_IE_PLTYPE_STR,  "CELPeerAcct" },
+       [AST_EVENT_IE_CEL_EXTRA]           = { AST_EVENT_IE_PLTYPE_STR,  "CELExtra" },
+       [AST_EVENT_IE_SECURITY_EVENT]      = { AST_EVENT_IE_PLTYPE_UINT, "SecurityEvent" },
+       [AST_EVENT_IE_EVENT_VERSION]       = { AST_EVENT_IE_PLTYPE_UINT, "EventVersion" },
+       [AST_EVENT_IE_SERVICE]             = { AST_EVENT_IE_PLTYPE_STR,  "Service" },
+       [AST_EVENT_IE_MODULE]              = { AST_EVENT_IE_PLTYPE_STR,  "Module" },
+       [AST_EVENT_IE_ACCOUNT_ID]          = { AST_EVENT_IE_PLTYPE_STR,  "AccountID" },
+       [AST_EVENT_IE_SESSION_ID]          = { AST_EVENT_IE_PLTYPE_STR,  "SessionID" },
+       [AST_EVENT_IE_SESSION_TV]          = { AST_EVENT_IE_PLTYPE_STR,  "SessionTV" },
+       [AST_EVENT_IE_ACL_NAME]            = { AST_EVENT_IE_PLTYPE_STR,  "ACLName" },
+       [AST_EVENT_IE_LOCAL_ADDR]          = { AST_EVENT_IE_PLTYPE_STR,  "LocalAddress" },
+       [AST_EVENT_IE_REMOTE_ADDR]         = { AST_EVENT_IE_PLTYPE_STR,  "RemoteAddress" },
+       [AST_EVENT_IE_EVENT_TV]            = { AST_EVENT_IE_PLTYPE_STR,  "EventTV" },
+       [AST_EVENT_IE_REQUEST_TYPE]        = { AST_EVENT_IE_PLTYPE_STR,  "RequestType" },
+       [AST_EVENT_IE_REQUEST_PARAMS]      = { AST_EVENT_IE_PLTYPE_STR,  "RequestParams" },
+       [AST_EVENT_IE_AUTH_METHOD]         = { AST_EVENT_IE_PLTYPE_STR,  "AuthMethod" },
+       [AST_EVENT_IE_SEVERITY]            = { AST_EVENT_IE_PLTYPE_STR,  "Severity" },
+       [AST_EVENT_IE_EXPECTED_ADDR]       = { AST_EVENT_IE_PLTYPE_STR,  "ExpectedAddress" },
+       [AST_EVENT_IE_CHALLENGE]           = { AST_EVENT_IE_PLTYPE_STR,  "Challenge" },
+       [AST_EVENT_IE_RESPONSE]            = { AST_EVENT_IE_PLTYPE_STR,  "Response" },
+       [AST_EVENT_IE_EXPECTED_RESPONSE]   = { AST_EVENT_IE_PLTYPE_STR,  "ExpectedResponse" },
+       [AST_EVENT_IE_RECEIVED_CHALLENGE]  = { AST_EVENT_IE_PLTYPE_STR,  "ReceivedChallenge" },
+       [AST_EVENT_IE_RECEIVED_HASH]       = { AST_EVENT_IE_PLTYPE_STR,  "ReceivedHash" },
+       [AST_EVENT_IE_USING_PASSWORD]      = { AST_EVENT_IE_PLTYPE_UINT, "UsingPassword" },
+       [AST_EVENT_IE_ATTEMPTED_TRANSPORT] = { AST_EVENT_IE_PLTYPE_STR,  "AttemptedTransport" },
+       [AST_EVENT_IE_CACHABLE]            = { AST_EVENT_IE_PLTYPE_UINT, "Cachable" },
+       [AST_EVENT_IE_PRESENCE_PROVIDER]   = { AST_EVENT_IE_PLTYPE_STR,  "PresenceProvider" },
+       [AST_EVENT_IE_PRESENCE_STATE]      = { AST_EVENT_IE_PLTYPE_UINT, "PresenceState" },
+       [AST_EVENT_IE_PRESENCE_SUBTYPE]    = { AST_EVENT_IE_PLTYPE_STR,  "PresenceSubtype" },
+       [AST_EVENT_IE_PRESENCE_MESSAGE]    = { AST_EVENT_IE_PLTYPE_STR,  "PresenceMessage" },
+};
 
-enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
+const char *ast_event_get_type_name(const struct ast_event *event)
 {
-       va_list ap;
-       enum ast_event_ie_type ie_type;
-       enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
-       struct ast_event_ie_val *ie_val, *sub_ie_val;
-       struct ast_event_sub *sub;
-       AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
-
-       if (type >= AST_EVENT_TOTAL) {
-               ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
-               return res;
-       }
+       enum ast_event_type type;
 
-       va_start(ap, type);
-       for (ie_type = va_arg(ap, enum ast_event_type);
-               ie_type != AST_EVENT_IE_END;
-               ie_type = va_arg(ap, enum ast_event_type))
-       {
-               struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
-               memset(ie_val, 0, sizeof(*ie_val));
-               ie_val->ie_type = ie_type;
-               ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
-               if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
-                       ie_val->payload.uint = va_arg(ap, uint32_t);
-               else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
-                       ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
-               AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
-       }
-       va_end(ap);
+       type = ast_event_get_type(event);
 
-       AST_RWDLLIST_RDLOCK(&ast_event_subs[type]);
-       AST_RWDLLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
-               AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
-                       AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
-                               if (sub_ie_val->ie_type == ie_val->ie_type)
-                                       break;
-                       }
-                       if (!sub_ie_val) {
-                               if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
-                                       break;
-                               continue;
-                       }
-                       /* The subscriber doesn't actually care what the value is */
-                       if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
-                               continue;
-                       if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
-                               ie_val->payload.uint != sub_ie_val->payload.uint)
-                               break;
-                       if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
-                               strcmp(ie_val->payload.str, sub_ie_val->payload.str))
-                               break;
-               }
-               if (!ie_val)
-                       break;
+       if ((unsigned int)type >= ARRAY_LEN(event_names)) {
+               ast_log(LOG_ERROR, "Invalid event type - '%u'\n", type);
+               return "";
        }
-       AST_RWDLLIST_UNLOCK(&ast_event_subs[type]);
 
-       if (sub) /* All parameters were matched */
-               return AST_EVENT_SUB_EXISTS;
-
-       AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
-       if (!AST_DLLIST_EMPTY(&ast_event_subs[AST_EVENT_ALL]))
-               res = AST_EVENT_SUB_EXISTS;
-       AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
-
-       return res;
+       return event_names[type];
 }
 
-/*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
-void ast_event_report_subs(const struct ast_event_sub *event_sub)
+const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
 {
-       struct ast_event *event;
-       struct ast_event_sub *sub;
-       enum ast_event_type event_type = -1;
-       struct ast_event_ie_val *ie_val;
-
-       if (event_sub->type != AST_EVENT_SUB)
-               return;
-
-       AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
-               if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
-                       event_type = ie_val->payload.uint;
-                       break;
-               }
+       if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
+               ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
+               return "";
        }
 
-       if (event_type == -1)
-               return;
-
-       AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
-       AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
-               if (event_sub == sub)
-                       continue;
-
-               event = ast_event_new(AST_EVENT_SUB,
-                       AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
-                       AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
-                       AST_EVENT_IE_END);
-
-               AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
-                       switch (ie_val->ie_pltype) {
-                       case AST_EVENT_IE_PLTYPE_EXISTS:
-                               ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
-                               break;
-                       case AST_EVENT_IE_PLTYPE_UINT:
-                               ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
-                               break;
-                       case AST_EVENT_IE_PLTYPE_STR:
-                               ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
-                               break;
-                       }
-                       if (!event)
-                               break;
-               }
-
-               if (!event)
-                       continue;
-
-               event_sub->cb(event, event_sub->userdata);
-
-               ast_event_destroy(event);
-       }
-       AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
+       return ie_maps[ie_type].name;
 }
 
-struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb, 
-       void *userdata, ...)
+enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
 {
-       va_list ap;
-       enum ast_event_ie_type ie_type;
-       struct ast_event_sub *sub;
-       struct ast_event *event;
-
-       if (type >= AST_EVENT_TOTAL) {
-               ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
-               return NULL;
-       }
-
-       if (!(sub = ast_calloc(1, sizeof(*sub))))
-               return NULL;
-
-       va_start(ap, userdata);
-       for (ie_type = va_arg(ap, enum ast_event_type);
-               ie_type != AST_EVENT_IE_END;
-               ie_type = va_arg(ap, enum ast_event_type))
-       {
-               struct ast_event_ie_val *ie_val;
-               if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
-                       continue;
-               ie_val->ie_type = ie_type;
-               ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
-               if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
-                       ie_val->payload.uint = va_arg(ap, uint32_t);
-               else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
-                       if (!(ie_val->payload.str = ast_strdup(va_arg(ap, const char *)))) {
-                               ast_free(ie_val);
-                               continue;
-                       }
-               }
-               AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
-       }
-       va_end(ap);
-
-       sub->type = type;
-       sub->cb = cb;
-       sub->userdata = userdata;
-       sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
-
-       if (ast_event_check_subscriber(AST_EVENT_SUB,
-               AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, type,
-               AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
-               struct ast_event_ie_val *ie_val;
-
-               event = ast_event_new(AST_EVENT_SUB,
-                       AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
-                       AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
-                       AST_EVENT_IE_END);
-
-               AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
-                       switch (ie_val->ie_pltype) {
-                       case AST_EVENT_IE_PLTYPE_EXISTS:
-                               ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
-                               break;
-                       case AST_EVENT_IE_PLTYPE_UINT:
-                               ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
-                               break;
-                       case AST_EVENT_IE_PLTYPE_STR:
-                               ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
-                               break;
-                       }
-                       if (!event)
-                               break;
-               }
-
-               if (event)
-                       ast_event_queue(event);
+       if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
+               ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
+               return AST_EVENT_IE_PLTYPE_UNKNOWN;
        }
 
-       AST_RWDLLIST_WRLOCK(&ast_event_subs[type]);
-       AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[type], sub, entry);
-       AST_RWDLLIST_UNLOCK(&ast_event_subs[type]);
-
-       return sub;
+       return ie_maps[ie_type].ie_pltype;
 }
 
-static void ast_event_sub_destroy(struct ast_event_sub *sub)
+size_t ast_event_get_size(const struct ast_event *event)
 {
-       struct ast_event_ie_val *ie_val;
+       size_t res;
 
-       while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry)))
-               ast_event_ie_val_destroy(ie_val);
+       res = ntohs(event->event_len);
 
-       ast_free(sub);
+       return res;
 }
 
-void ast_event_unsubscribe(struct ast_event_sub *sub)
+/*! \brief Subscription event check list. */
+struct ast_ev_check_list {
+       AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
+};
+
+int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
 {
-       struct ast_event *event;
+       int res = 0;
 
-       AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
-       AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
-       AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
-
-       if (ast_event_check_subscriber(AST_EVENT_UNSUB,
-               AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
-               AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
-               
-               event = ast_event_new(AST_EVENT_UNSUB,
-                       AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
-                       AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
-                       AST_EVENT_IE_END);
-
-               if (event)
-                       ast_event_queue(event);
+       iterator->event_len = ast_event_get_size(event);
+       iterator->event = event;
+       if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
+               iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
+       } else {
+               iterator->ie = NULL;
+               res = -1;
        }
 
-       ast_event_sub_destroy(sub);
-}
-
-void ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
-{
-       iterator->event_len = ntohs(event->event_len);
-       iterator->event = event;
-       iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
-       return;
+       return res;
 }
 
 int ast_event_iterator_next(struct ast_event_iterator *iterator)
@@ -390,10 +273,14 @@ uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
 
 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
 {
-       return (const char*)iterator->ie->ie_payload;
+       const struct ast_event_ie_str_payload *str_payload;
+
+       str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
+
+       return str_payload ? str_payload->str : NULL;
 }
 
-void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
+static void *event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
 {
        return iterator->ie->ie_payload;
 }
@@ -414,26 +301,60 @@ uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_
 
 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
 {
-       return ast_event_get_ie_raw(event, ie_type);
+       const struct ast_event_ie_str_payload *str_payload;
+
+       str_payload = ast_event_get_ie_raw(event, ie_type);
+
+       return str_payload ? str_payload->str : NULL;
 }
 
 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
 {
        struct ast_event_iterator iterator;
-       int res = 0;
+       int res;
 
-       for (ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
-               if (ast_event_iterator_get_ie_type(&iterator) == ie_type)
-                       return ast_event_iterator_get_ie_raw(&iterator);
+       for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
+               if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
+                       return event_iterator_get_ie_raw(&iterator);
+               }
        }
 
        return NULL;
 }
 
+static uint16_t event_iterator_get_ie_raw_payload_len(struct ast_event_iterator *iterator)
+{
+       return ntohs(iterator->ie->ie_payload_len);
+}
+
+uint16_t ast_event_get_ie_raw_payload_len(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+       struct ast_event_iterator iterator;
+       int res;
+
+       for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
+               if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
+                       return event_iterator_get_ie_raw_payload_len(&iterator);
+               }
+       }
+
+       return 0;
+}
+
+
 int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
        const char *str)
 {
-       return ast_event_append_ie_raw(event, ie_type, str, strlen(str) + 1);
+       struct ast_event_ie_str_payload *str_payload;
+       size_t payload_len;
+
+       payload_len = sizeof(*str_payload) + strlen(str);
+       str_payload = ast_alloca(payload_len);
+
+       strcpy(str_payload->str, str);
+       str_payload->hash = ast_str_hash(str);
+
+       return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
 }
 
 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
@@ -443,18 +364,30 @@ int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie
        return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
 }
 
+int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
+       uint32_t flags)
+{
+       flags = htonl(flags);
+       return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
+}
+
 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
        const void *data, size_t data_len)
 {
        struct ast_event_ie *ie;
+       struct ast_event *old_event;
        unsigned int extra_len;
        uint16_t event_len;
 
        event_len = ntohs((*event)->event_len);
        extra_len = sizeof(*ie) + data_len;
 
-       if (!(*event = ast_realloc(*event, event_len + extra_len)))
+       old_event = *event;
+       *event = ast_realloc(*event, event_len + extra_len);
+       if (!*event) {
+               ast_free(old_event);
                return -1;
+       }
 
        ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
        ie->ie_type = htons(ie_type);
@@ -470,326 +403,119 @@ struct ast_event *ast_event_new(enum ast_event_type type, ...)
 {
        va_list ap;
        struct ast_event *event;
-       enum ast_event_type ie_type;
+       enum ast_event_ie_type ie_type;
        struct ast_event_ie_val *ie_val;
        AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
 
        /* Invalid type */
        if (type >= AST_EVENT_TOTAL) {
                ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
-                       "type '%d'!\n", type);
+                       "type '%u'!\n", type);
                return NULL;
        }
 
        va_start(ap, type);
-       for (ie_type = va_arg(ap, enum ast_event_type);
+       for (ie_type = va_arg(ap, enum ast_event_ie_type);
                ie_type != AST_EVENT_IE_END;
-               ie_type = va_arg(ap, enum ast_event_type))
+               ie_type = va_arg(ap, enum ast_event_ie_type))
        {
-               struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
-               memset(ie_val, 0, sizeof(*ie_val));
-               ie_val->ie_type = ie_type;
-               ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
-               if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
-                       ie_val->payload.uint = va_arg(ap, uint32_t);
-               else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
-                       ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
-               AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
+               struct ast_event_ie_val *ie_value = ast_alloca(sizeof(*ie_value));
+               int insert = 0;
+
+               memset(ie_value, 0, sizeof(*ie_value));
+               ie_value->ie_type = ie_type;
+               ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
+
+               switch (ie_value->ie_pltype) {
+               case AST_EVENT_IE_PLTYPE_UINT:
+                       ie_value->payload.uint = va_arg(ap, uint32_t);
+                       insert = 1;
+                       break;
+               case AST_EVENT_IE_PLTYPE_BITFLAGS:
+                       ie_value->payload.uint = va_arg(ap, uint32_t);
+                       insert = 1;
+                       break;
+               case AST_EVENT_IE_PLTYPE_STR:
+                       ie_value->payload.str = va_arg(ap, const char *);
+                       insert = 1;
+                       break;
+               case AST_EVENT_IE_PLTYPE_RAW:
+               {
+                       void *data = va_arg(ap, void *);
+                       size_t datalen = va_arg(ap, size_t);
+                       ie_value->payload.raw = ast_alloca(datalen);
+                       memcpy(ie_value->payload.raw, data, datalen);
+                       ie_value->raw_datalen = datalen;
+                       insert = 1;
+                       break;
+               }
+               case AST_EVENT_IE_PLTYPE_UNKNOWN:
+               case AST_EVENT_IE_PLTYPE_EXISTS:
+                       break;
+               }
+
+               if (insert) {
+                       AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
+               } else {
+                       ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
+               }
        }
        va_end(ap);
 
-       if (!(event = ast_calloc(1, sizeof(*event))))
+       if (!(event = ast_calloc(1, sizeof(*event)))) {
                return NULL;
+       }
 
        event->type = htons(type);
        event->event_len = htons(sizeof(*event));
 
        AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
-               if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
+               switch (ie_val->ie_pltype) {
+               case AST_EVENT_IE_PLTYPE_STR:
                        ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
-               else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
+                       break;
+               case AST_EVENT_IE_PLTYPE_UINT:
                        ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
-
-               if (!event)
                        break;
-       }
-
-       return event;
-}
-
-void ast_event_destroy(struct ast_event *event)
-{
-       ast_free(event);
-}
-
-static void ast_event_ref_destroy(struct ast_event_ref *event_ref)
-{
-       ast_event_destroy(event_ref->event);
-       ast_free(event_ref);
-}
-
-static struct ast_event *ast_event_dup(const struct ast_event *event)
-{
-       struct ast_event *dup_event;
-       uint16_t event_len;
-
-       event_len = ntohs(event->event_len);
-
-       if (!(dup_event = ast_calloc(1, event_len)))
-               return NULL;
-       
-       memcpy(dup_event, event, event_len);
-
-       return dup_event;
-}
-
-struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
-{
-       va_list ap;
-       enum ast_event_ie_type ie_type;
-       struct ast_event *dup_event = NULL;
-       struct ast_event_ref *event_ref;
-       struct cache_arg {
-               AST_LIST_ENTRY(cache_arg) entry;
-               enum ast_event_ie_type ie_type;
-               enum ast_event_ie_pltype ie_pltype;
-               union {
-                       uint32_t uint;
-                       const char *str;
-               } payload;
-       } *cache_arg;
-       AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
-
-       if (type >= AST_EVENT_TOTAL) {
-               ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
-               return NULL;
-       }
-
-       va_start(ap, type);
-       for (ie_type = va_arg(ap, enum ast_event_type);
-               ie_type != AST_EVENT_IE_END;
-               ie_type = va_arg(ap, enum ast_event_type))
-       {
-               cache_arg = alloca(sizeof(*cache_arg));
-               memset(cache_arg, 0, sizeof(*cache_arg));
-               cache_arg->ie_type = ie_type;
-               cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
-               if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
-                       cache_arg->payload.uint = va_arg(ap, uint32_t);
-               else if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
-                       cache_arg->payload.str = ast_strdupa(va_arg(ap, const char *));
-               AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
-       }
-       va_end(ap);
-
-       if (AST_LIST_EMPTY(&cache_args)) {
-               ast_log(LOG_ERROR, "Events can not be retrieved from the cache without "
-                       "specifying at least one IE type!\n");
-               return NULL;
-       }
-
-       AST_RWLIST_RDLOCK(&ast_event_cache[type]);
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[type], event_ref, entry) {
-               AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
-                       if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
-                          (cache_arg->payload.uint ==
-                           ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
-
-                          (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
-                          (!strcmp(cache_arg->payload.str,
-                            ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
-
-                          (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
-                           ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) ) 
-                       {
-                               break;  
-                       }
-               }
-               if (!cache_arg) {
-                       /* All parameters were matched on this cache entry, so return it */
-                       dup_event = ast_event_dup(event_ref->event);
+               case AST_EVENT_IE_PLTYPE_BITFLAGS:
+                       ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
+                       break;
+               case AST_EVENT_IE_PLTYPE_RAW:
+                       ast_event_append_ie_raw(&event, ie_val->ie_type,
+                                       ie_val->payload.raw, ie_val->raw_datalen);
+                       break;
+               case AST_EVENT_IE_PLTYPE_EXISTS:
+               case AST_EVENT_IE_PLTYPE_UNKNOWN:
                        break;
                }
-       }
-       AST_RWLIST_TRAVERSE_SAFE_END
-       AST_RWLIST_UNLOCK(&ast_event_cache[type]);
-
-       return dup_event;
-}
-
-/*! \brief Duplicate an event and add it to the cache
- * \note This assumes this index in to the cache is locked */
-static int ast_event_dup_and_cache(const struct ast_event *event)
-{
-       struct ast_event *dup_event;
-       struct ast_event_ref *event_ref;
-
-       if (!(dup_event = ast_event_dup(event)))
-               return -1;
-       if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
-               return -1;
-       
-       event_ref->event = dup_event;
-
-       AST_LIST_INSERT_TAIL(&ast_event_cache[ntohs(event->type)], event_ref, entry);
-
-       return 0;
-}
-
-int ast_event_queue_and_cache(struct ast_event *event, ...)
-{
-       va_list ap;
-       enum ast_event_type ie_type;
-       uint16_t host_event_type;
-       struct ast_event_ref *event_ref;
-       int res;
-       struct cache_arg {
-               AST_LIST_ENTRY(cache_arg) entry;
-               enum ast_event_ie_type ie_type;
-               enum ast_event_ie_pltype ie_pltype;
-       } *cache_arg;
-       AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
-
-       host_event_type = ntohs(event->type);
 
-       /* Invalid type */
-       if (host_event_type >= AST_EVENT_TOTAL) {
-               ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
-                       "type '%d'!\n", host_event_type);
-               return -1;
-       }
-
-       va_start(ap, event);
-       for (ie_type = va_arg(ap, enum ast_event_type);
-               ie_type != AST_EVENT_IE_END;
-               ie_type = va_arg(ap, enum ast_event_type))
-       {
-               cache_arg = alloca(sizeof(*cache_arg));
-               memset(cache_arg, 0, sizeof(*cache_arg));
-               cache_arg->ie_type = ie_type;
-               cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
-               AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
+               /* realloc inside one of the append functions failed */
+               if (!event) {
+                       return NULL;
+               }
        }
-       va_end(ap);
 
-       if (AST_LIST_EMPTY(&cache_args)) {
-               ast_log(LOG_ERROR, "Events can not be cached without specifying at "
-                       "least one IE type!\n");
-               return ast_event_queue(event);
-       }
-       AST_RWLIST_WRLOCK(&ast_event_cache[host_event_type]);
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[host_event_type], event_ref, entry) {
-               AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
-                       if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
-                          (ast_event_get_ie_uint(event, cache_arg->ie_type) ==
-                           ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
-
-                          (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
-                          (!strcmp(ast_event_get_ie_str(event, cache_arg->ie_type),
-                            ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
-
-                          (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
-                           ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) )
-                       {
-                               break;  
-                       }
-               }
-               if (!cache_arg) {
-                       /* All parameters were matched on this cache entry, so remove it */
-                       AST_LIST_REMOVE_CURRENT(entry);
-                       ast_event_ref_destroy(event_ref);
-               }
+       if (!ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
+               /* If the event is originating on this server, add the server's
+                * entity ID to the event. */
+               ast_event_append_eid(&event);
        }
-       AST_RWLIST_TRAVERSE_SAFE_END;
-       res = ast_event_dup_and_cache(event);
-       AST_RWLIST_UNLOCK(&ast_event_cache[host_event_type]);
 
-       return (ast_event_queue(event) || res) ? -1 : 0;
+       return event;
 }
 
-static int handle_event(void *data)
+int ast_event_append_eid(struct ast_event **event)
 {
-       struct ast_event_ref *event_ref = data;
-       struct ast_event_sub *sub;
-       uint16_t host_event_type;
-
-       host_event_type = ntohs(event_ref->event->type);
-
-       /* Subscribers to this specific event first */
-       AST_RWDLLIST_RDLOCK(&ast_event_subs[host_event_type]);
-       AST_RWDLLIST_TRAVERSE(&ast_event_subs[host_event_type], sub, entry) {
-               struct ast_event_ie_val *ie_val;
-               AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
-                       if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
-                               ast_event_get_ie_raw(event_ref->event, ie_val->ie_type)) {
-                               continue;
-                       } else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
-                               ast_event_get_ie_uint(event_ref->event, ie_val->ie_type) 
-                               == ie_val->payload.uint) {
-                               continue;
-                       } else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
-                               !strcmp(ast_event_get_ie_str(event_ref->event, ie_val->ie_type),
-                                       ie_val->payload.str)) {
-                               continue;
-                       }
-                       break;
-               }
-               if (ie_val)
-                       continue;
-               sub->cb(event_ref->event, sub->userdata);
-       }
-       AST_RWDLLIST_UNLOCK(&ast_event_subs[host_event_type]);
-
-       /* Now to subscribers to all event types */
-       AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
-       AST_RWDLLIST_TRAVERSE(&ast_event_subs[AST_EVENT_ALL], sub, entry)
-               sub->cb(event_ref->event, sub->userdata);
-       AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
-
-       ast_event_ref_destroy(event_ref);
-
-       return 0;
+       return ast_event_append_ie_raw(event, AST_EVENT_IE_EID,
+                       &ast_eid_default, sizeof(ast_eid_default));
 }
 
-int ast_event_queue(struct ast_event *event)
+void ast_event_destroy(struct ast_event *event)
 {
-       struct ast_event_ref *event_ref;
-       uint16_t host_event_type;
-
-       host_event_type = ntohs(event->type);
-
-       /* Invalid type */
-       if (host_event_type >= AST_EVENT_TOTAL) {
-               ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
-                       "type '%d'!\n", host_event_type);
-               return -1;
-       }
-
-       /* If nobody has subscribed to this event type, throw it away now */
-       if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END) 
-               == AST_EVENT_SUB_NONE) {
-               ast_event_destroy(event);
-               return 0;
-       }
-
-       if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
-               return -1;
-
-       event_ref->event = event;
-
-       return ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
+       ast_free(event);
 }
 
-void ast_event_init(void)
+size_t ast_event_minimum_length(void)
 {
-       int i;
-
-       for (i = 0; i < AST_EVENT_TOTAL; i++)
-               AST_RWDLLIST_HEAD_INIT(&ast_event_subs[i]);
-
-       for (i = 0; i < AST_EVENT_TOTAL; i++)
-               AST_RWLIST_HEAD_INIT(&ast_event_cache[i]);
-
-       event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0);
+       return sizeof(struct ast_event);
 }