Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
event.c File Reference

Internal generic event system. More...

#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/taskprocessor.h"
#include "asterisk/astobj2.h"
#include "asterisk/cli.h"
Include dependency graph for event.c:

Go to the source code of this file.

Data Structures

struct  ast_ev_check_list
 Subscription event check list. More...
 
struct  ast_event
 An event. More...
 
struct  ast_event_ie
 An event information element. More...
 
struct  ast_event_ie_str_payload
 The payload for a string information element. More...
 
struct  ast_event_ie_val
 
struct  ie_map
 IE payload types and names. More...
 

Functions

int ast_event_append_eid (struct ast_event **event)
 Append the global EID IE. More...
 
int ast_event_append_ie_bitflags (struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t flags)
 Append an information element that has a bitflags payload. More...
 
int ast_event_append_ie_raw (struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
 Append an information element that has a raw payload. More...
 
int ast_event_append_ie_str (struct ast_event **event, enum ast_event_ie_type ie_type, const char *str)
 Append an information element that has a string payload. More...
 
int ast_event_append_ie_uint (struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t data)
 Append an information element that has an integer payload. More...
 
void ast_event_destroy (struct ast_event *event)
 Destroy an event. More...
 
enum ast_event_ie_pltype ast_event_get_ie_pltype (enum ast_event_ie_type ie_type)
 Get the payload type for a given information element type. More...
 
const void * ast_event_get_ie_raw (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the value of an information element that has a raw payload. More...
 
uint16_t ast_event_get_ie_raw_payload_len (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the length of the raw payload for a particular IE. More...
 
const char * ast_event_get_ie_str (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the value of an information element that has a string payload. More...
 
const char * ast_event_get_ie_type_name (enum ast_event_ie_type ie_type)
 Get the string representation of an information element type. More...
 
uint32_t ast_event_get_ie_uint (const struct ast_event *event, enum ast_event_ie_type ie_type)
 Get the value of an information element that has an integer payload. More...
 
size_t ast_event_get_size (const struct ast_event *event)
 Get the size of an event. More...
 
enum ast_event_type ast_event_get_type (const struct ast_event *event)
 Get the type for an event. More...
 
const char * ast_event_get_type_name (const struct ast_event *event)
 Get the string representation of the type of the given event. More...
 
const char * ast_event_iterator_get_ie_str (struct ast_event_iterator *iterator)
 Get the value of the current IE in the iterator as a string payload. More...
 
enum ast_event_ie_type ast_event_iterator_get_ie_type (struct ast_event_iterator *iterator)
 Get the type of the current IE in the iterator instance. More...
 
uint32_t ast_event_iterator_get_ie_uint (struct ast_event_iterator *iterator)
 Get the value of the current IE in the iterator as an integer payload. More...
 
int ast_event_iterator_init (struct ast_event_iterator *iterator, const struct ast_event *event)
 Initialize an event iterator instance. More...
 
int ast_event_iterator_next (struct ast_event_iterator *iterator)
 Move iterator instance to next IE. More...
 
size_t ast_event_minimum_length (void)
 Get the minimum length of an ast_event. More...
 
struct ast_eventast_event_new (enum ast_event_type type,...)
 Create a new event. More...
 
static void * event_iterator_get_ie_raw (struct ast_event_iterator *iterator)
 
static uint16_t event_iterator_get_ie_raw_payload_len (struct ast_event_iterator *iterator)
 

Variables

static const char *const event_names [AST_EVENT_TOTAL]
 Event Names. More...
 
static const struct ie_map ie_maps [AST_EVENT_IE_TOTAL]
 

Detailed Description

Internal generic event system.

Author
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com

Definition in file event.c.

Function Documentation

◆ ast_event_append_eid()

int ast_event_append_eid ( struct ast_event **  event)

Append the global EID IE.

Parameters
eventthe event to append IE to
Note
For ast_event_new() that includes IEs, this is done automatically for you.
Return values
0success
-1failure

Definition at line 518 of file event.c.

References ast_eid_default, ast_event_append_ie_raw(), and AST_EVENT_IE_EID.

Referenced by ast_event_new().

519 {
521  &ast_eid_default, sizeof(ast_eid_default));
522 }
Entity ID Used by All events Payload type: RAW This IE indicates which server the event originated fr...
Definition: event_defs.h:272
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
Append an information element that has a raw payload.
Definition: event.c:374
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93

◆ ast_event_append_ie_bitflags()

int ast_event_append_ie_bitflags ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
uint32_t  bitflags 
)

Append an information element that has a bitflags payload.

Parameters
eventthe event that the IE will be appended to
ie_typethe type of IE to append
bitflagsthe flags that are the payload of the IE
Return values
0success
-1failure
Since
1.8

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 367 of file event.c.

References ast_event_append_ie_raw().

Referenced by ast_event_new().

369 {
370  flags = htonl(flags);
371  return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
372 }
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
Append an information element that has a raw payload.
Definition: event.c:374

◆ ast_event_append_ie_raw()

int ast_event_append_ie_raw ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
const void *  data,
size_t  data_len 
)

Append an information element that has a raw payload.

Parameters
eventthe event that the IE will be appended to
ie_typethe type of IE to append
dataA pointer to the raw data for the payload of the IE
data_lenThe amount of data to copy into the payload
Return values
0success
-1failure

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 374 of file event.c.

References ast_free, ast_realloc, ast_event::event_len, ast_event_ie::ie_payload, ast_event_ie::ie_payload_len, and ast_event_ie::ie_type.

Referenced by ast_event_append_eid(), ast_event_append_ie_bitflags(), ast_event_append_ie_str(), ast_event_append_ie_uint(), and ast_event_new().

376 {
377  struct ast_event_ie *ie;
378  struct ast_event *old_event;
379  unsigned int extra_len;
380  uint16_t event_len;
381 
382  event_len = ntohs((*event)->event_len);
383  extra_len = sizeof(*ie) + data_len;
384 
385  old_event = *event;
386  *event = ast_realloc(*event, event_len + extra_len);
387  if (!*event) {
388  ast_free(old_event);
389  return -1;
390  }
391 
392  ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
393  ie->ie_type = htons(ie_type);
394  ie->ie_payload_len = htons(data_len);
395  memcpy(ie->ie_payload, data, data_len);
396 
397  (*event)->event_len = htons(event_len + extra_len);
398 
399  return 0;
400 }
enum ast_event_ie_type ie_type
Definition: event.c:54
An event.
Definition: event.c:81
#define ast_realloc(p, len)
A wrapper for realloc()
Definition: astmm.h:228
uint16_t ie_payload_len
Definition: event.c:56
An event information element.
Definition: event.c:53
#define ast_free(a)
Definition: astmm.h:182
uint16_t event_len
Definition: event.c:85
unsigned char ie_payload[0]
Definition: event.c:57

◆ ast_event_append_ie_str()

int ast_event_append_ie_str ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
const char *  str 
)

Append an information element that has a string payload.

Parameters
eventthe event that the IE will be appended to
ie_typethe type of IE to append
strThe string for the payload of the IE
Return values
0success
-1failure

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 345 of file event.c.

References ast_alloca, ast_event_append_ie_raw(), ast_str_hash(), ast_event_ie_str_payload::hash, and ast_event_ie_str_payload::str.

Referenced by ast_event_new(), and AST_TEST_DEFINE().

347 {
348  struct ast_event_ie_str_payload *str_payload;
349  size_t payload_len;
350 
351  payload_len = sizeof(*str_payload) + strlen(str);
352  str_payload = ast_alloca(payload_len);
353 
354  strcpy(str_payload->str, str);
355  str_payload->hash = ast_str_hash(str);
356 
357  return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
358 }
uint32_t hash
A hash calculated with ast_str_hash(), to speed up comparisons.
Definition: event.c:65
const char * str
Definition: app_jack.c:147
char str[1]
The actual string, null terminated.
Definition: event.c:67
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
Append an information element that has a raw payload.
Definition: event.c:374
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
The payload for a string information element.
Definition: event.c:63
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1206

◆ ast_event_append_ie_uint()

int ast_event_append_ie_uint ( struct ast_event **  event,
enum ast_event_ie_type  ie_type,
uint32_t  data 
)

Append an information element that has an integer payload.

Parameters
eventthe event that the IE will be appended to
ie_typethe type of IE to append
dataThe integer for the payload of the IE
Return values
0success
-1failure

The pointer to the event will get updated with the new location for the event that now contains the appended information element. If the re-allocation of the memory for this event fails, it will be set to NULL.

Definition at line 360 of file event.c.

References ast_event_append_ie_raw().

Referenced by ast_event_new(), and AST_TEST_DEFINE().

362 {
363  data = htonl(data);
364  return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
365 }
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
Append an information element that has a raw payload.
Definition: event.c:374

◆ ast_event_destroy()

void ast_event_destroy ( struct ast_event event)

Destroy an event.

Parameters
eventthe event to destroy
Returns
Nothing

Definition at line 524 of file event.c.

References ast_free.

Referenced by AST_TEST_DEFINE(), cel_report_event(), corosync_ping(), publish_to_corosync(), and send_cluster_notify().

525 {
526  ast_free(event);
527 }
#define ast_free(a)
Definition: astmm.h:182

◆ ast_event_get_ie_pltype()

enum ast_event_ie_pltype ast_event_get_ie_pltype ( enum ast_event_ie_type  ie_type)

Get the payload type for a given information element type.

Parameters
ie_typethe information element type to get the payload type of
Returns
the payload type for the provided IE type
Since
1.6.1

Definition at line 218 of file event.c.

References ARRAY_LEN, AST_EVENT_IE_PLTYPE_UNKNOWN, ast_log, ie_maps, ie_map::ie_pltype, ast_event_ie::ie_type, and LOG_ERROR.

Referenced by dump_event(), and match_ie_val().

219 {
220  if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
221  ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
223  }
224 
225  return ie_maps[ie_type].ie_pltype;
226 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
enum ast_event_ie_pltype ie_pltype
Definition: event.c:129
static const struct ie_map ie_maps[AST_EVENT_IE_TOTAL]

◆ ast_event_get_ie_raw()

const void* ast_event_get_ie_raw ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the value of an information element that has a raw payload.

Parameters
eventThe event to get the IE from
ie_typethe type of information element to retrieve
Returns
This returns the payload of the information element with the given type. If the information element isn't found, NULL will be returned.

Definition at line 311 of file event.c.

References ast_event_iterator_get_ie_type(), ast_event_iterator_init(), ast_event_iterator_next(), event_iterator_get_ie_raw(), ast_event_ie::ie_type, and NULL.

Referenced by ast_event_get_ie_str(), ast_event_get_ie_uint(), ast_event_new(), corosync_node_alloc(), corosync_ping_to_event(), cpg_deliver_cb(), publish_cluster_discovery_to_stasis(), publish_corosync_ping_to_stasis(), publish_device_state_to_stasis(), publish_mwi_to_stasis(), and publish_to_corosync().

312 {
313  struct ast_event_iterator iterator;
314  int res;
315 
316  for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
317  if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
318  return event_iterator_get_ie_raw(&iterator);
319  }
320  }
321 
322  return NULL;
323 }
supposed to be an opaque type
Definition: event_defs.h:356
#define NULL
Definition: resample.c:96
static void * event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
Definition: event.c:283
int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
Initialize an event iterator instance.
Definition: event.c:242
int ast_event_iterator_next(struct ast_event_iterator *iterator)
Move iterator instance to next IE.
Definition: event.c:258
enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
Get the type of the current IE in the iterator instance.
Definition: event.c:264

◆ ast_event_get_ie_raw_payload_len()

uint16_t ast_event_get_ie_raw_payload_len ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the length of the raw payload for a particular IE.

Parameters
eventThe event to get the IE payload length from
ie_typethe type of information element to get the length of
Returns
If an IE of type ie_type is found, its payload length is returned. Otherwise, 0 is returned.

Definition at line 330 of file event.c.

References ast_event_iterator_get_ie_type(), ast_event_iterator_init(), ast_event_iterator_next(), event_iterator_get_ie_raw_payload_len(), and ast_event_ie::ie_type.

331 {
332  struct ast_event_iterator iterator;
333  int res;
334 
335  for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
336  if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
337  return event_iterator_get_ie_raw_payload_len(&iterator);
338  }
339  }
340 
341  return 0;
342 }
static uint16_t event_iterator_get_ie_raw_payload_len(struct ast_event_iterator *iterator)
Definition: event.c:325
supposed to be an opaque type
Definition: event_defs.h:356
int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
Initialize an event iterator instance.
Definition: event.c:242
int ast_event_iterator_next(struct ast_event_iterator *iterator)
Move iterator instance to next IE.
Definition: event.c:258
enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
Get the type of the current IE in the iterator instance.
Definition: event.c:264

◆ ast_event_get_ie_str()

const char* ast_event_get_ie_str ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the value of an information element that has a string payload.

Parameters
eventThe event to get the IE from
ie_typethe type of information element to retrieve
Returns
This returns the payload of the information element with the given type. If the information element isn't found, NULL will be returned.

Definition at line 302 of file event.c.

References ast_event_get_ie_raw(), NULL, and ast_event_ie_str_payload::str.

Referenced by ast_cel_fill_record(), check_event(), corosync_node_alloc(), match_ie_val(), publish_device_state_to_stasis(), publish_mwi_to_stasis(), and test_sub().

303 {
304  const struct ast_event_ie_str_payload *str_payload;
305 
306  str_payload = ast_event_get_ie_raw(event, ie_type);
307 
308  return str_payload ? str_payload->str : NULL;
309 }
char str[1]
The actual string, null terminated.
Definition: event.c:67
#define NULL
Definition: resample.c:96
const void * ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a raw payload.
Definition: event.c:311
The payload for a string information element.
Definition: event.c:63

◆ ast_event_get_ie_type_name()

const char* ast_event_get_ie_type_name ( enum ast_event_ie_type  ie_type)

Get the string representation of an information element type.

Parameters
ie_typethe information element type to get the string representation of
Returns
the string representation of the information element type
Since
1.6.1

Definition at line 208 of file event.c.

References ARRAY_LEN, ast_log, ie_maps, ast_event_ie::ie_type, LOG_ERROR, and ie_map::name.

Referenced by add_ip_json_object(), add_json_object(), alloc_security_event_json_object(), append_event_str_single(), append_json_single(), dump_event(), and events_are_equal().

209 {
210  if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
211  ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
212  return "";
213  }
214 
215  return ie_maps[ie_type].name;
216 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_log
Definition: astobj2.c:42
const char * name
Definition: event.c:130
#define LOG_ERROR
Definition: logger.h:285
static const struct ie_map ie_maps[AST_EVENT_IE_TOTAL]

◆ ast_event_get_ie_uint()

uint32_t ast_event_get_ie_uint ( const struct ast_event event,
enum ast_event_ie_type  ie_type 
)

Get the value of an information element that has an integer payload.

Parameters
eventThe event to get the IE from
ie_typethe type of information element to retrieve
Returns
This returns the payload of the information element with the given type. However, an IE with a payload of 0, and the case where no IE is found yield the same return value.

Definition at line 293 of file event.c.

References ast_event_get_ie_raw(), and get_unaligned_uint32().

Referenced by ast_cel_fill_record(), check_event(), corosync_node_alloc(), dump_event(), match_ie_val(), publish_cluster_discovery_to_stasis(), publish_device_state_to_stasis(), and publish_mwi_to_stasis().

294 {
295  const uint32_t *ie_val;
296 
297  ie_val = ast_event_get_ie_raw(event, ie_type);
298 
299  return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
300 }
const void * ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a raw payload.
Definition: event.c:311
static unsigned int get_unaligned_uint32(const void *p)
Definition: unaligned.h:38

◆ ast_event_get_size()

size_t ast_event_get_size ( const struct ast_event event)

Get the size of an event.

Parameters
eventthe event to get the size of
Returns
the number of bytes contained in the event
Since
1.6.1

Definition at line 228 of file event.c.

References ast_event::event_len.

Referenced by ao2_dup_event(), ast_event_iterator_init(), AST_TEST_DEFINE(), and publish_event_to_corosync().

229 {
230  size_t res;
231 
232  res = ntohs(event->event_len);
233 
234  return res;
235 }
uint16_t event_len
Definition: event.c:85

◆ ast_event_get_type()

enum ast_event_type ast_event_get_type ( const struct ast_event event)

Get the type for an event.

Parameters
eventthe event to get the type for
Returns
the event type as represented by one of the values in the ast_event_type enum

Definition at line 288 of file event.c.

References ast_event::type.

Referenced by ast_event_get_type_name(), check_event(), check_events(), cpg_deliver_cb(), events_are_equal(), publish_cluster_discovery_to_stasis(), publish_corosync_ping_to_stasis(), publish_device_state_to_stasis(), publish_event_to_corosync(), publish_mwi_to_stasis(), and publish_to_corosync().

289 {
290  return ntohs(event->type);
291 }
enum ast_event_type type
Definition: event.c:83

◆ ast_event_get_type_name()

const char* ast_event_get_type_name ( const struct ast_event event)

Get the string representation of the type of the given event.

  • event the event to get the type of
Returns
the string representation of the event type of the provided event
Since
1.6.1

Definition at line 194 of file event.c.

References ARRAY_LEN, ast_event_get_type(), ast_log, LOG_ERROR, and type.

Referenced by cpg_deliver_cb(), and publish_event_to_corosync().

195 {
196  enum ast_event_type type;
197 
198  type = ast_event_get_type(event);
199 
200  if ((unsigned int)type >= ARRAY_LEN(event_names)) {
201  ast_log(LOG_ERROR, "Invalid event type - '%u'\n", type);
202  return "";
203  }
204 
205  return event_names[type];
206 }
static const char type[]
Definition: chan_ooh323.c:109
enum ast_event_type ast_event_get_type(const struct ast_event *event)
Get the type for an event.
Definition: event.c:288
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static const char *const event_names[AST_EVENT_TOTAL]
Event Names.
Definition: event.c:109
#define ast_log
Definition: astobj2.c:42
ast_event_type
Definition: event_defs.h:28
#define LOG_ERROR
Definition: logger.h:285

◆ ast_event_iterator_get_ie_str()

const char* ast_event_iterator_get_ie_str ( struct ast_event_iterator iterator)

Get the value of the current IE in the iterator as a string payload.

Parameters
iteratorThe iterator instance
Returns
This returns the payload of the information element as a string.

Definition at line 274 of file event.c.

References ast_event_iterator::ie, ast_event_ie::ie_payload, NULL, and ast_event_ie_str_payload::str.

Referenced by dump_event().

275 {
276  const struct ast_event_ie_str_payload *str_payload;
277 
278  str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
279 
280  return str_payload ? str_payload->str : NULL;
281 }
struct ast_event_ie * ie
Definition: event_defs.h:359
char str[1]
The actual string, null terminated.
Definition: event.c:67
#define NULL
Definition: resample.c:96
unsigned char ie_payload[0]
Definition: event.c:57
The payload for a string information element.
Definition: event.c:63

◆ ast_event_iterator_get_ie_type()

enum ast_event_ie_type ast_event_iterator_get_ie_type ( struct ast_event_iterator iterator)

Get the type of the current IE in the iterator instance.

Parameters
iteratorThe iterator instance
Returns
the ie type as represented by one of the value sin the ast_event_ie_type enum

Definition at line 264 of file event.c.

References ast_event_iterator::ie, and ast_event_ie::ie_type.

Referenced by ast_event_get_ie_raw(), ast_event_get_ie_raw_payload_len(), dump_event(), and events_are_equal().

265 {
266  return ntohs(iterator->ie->ie_type);
267 }
enum ast_event_ie_type ie_type
Definition: event.c:54
struct ast_event_ie * ie
Definition: event_defs.h:359

◆ ast_event_iterator_get_ie_uint()

uint32_t ast_event_iterator_get_ie_uint ( struct ast_event_iterator iterator)

Get the value of the current IE in the iterator as an integer payload.

Parameters
iteratorThe iterator instance
Returns
This returns the payload of the information element as a uint.

Definition at line 269 of file event.c.

References get_unaligned_uint32(), ast_event_iterator::ie, and ast_event_ie::ie_payload.

Referenced by dump_event().

270 {
271  return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
272 }
struct ast_event_ie * ie
Definition: event_defs.h:359
static unsigned int get_unaligned_uint32(const void *p)
Definition: unaligned.h:38
unsigned char ie_payload[0]
Definition: event.c:57

◆ ast_event_iterator_init()

int ast_event_iterator_init ( struct ast_event_iterator iterator,
const struct ast_event event 
)

Initialize an event iterator instance.

Parameters
iteratorThe iterator instance to initialize
eventThe event that will be iterated through
Return values
0Success, there are IEs available to iterate
-1Failure, there are no IEs in the event to iterate

Definition at line 242 of file event.c.

References ast_event_get_size(), ast_event_iterator::event, ast_event_iterator::event_len, ast_event_iterator::ie, and NULL.

Referenced by ast_event_get_ie_raw(), ast_event_get_ie_raw_payload_len(), dump_event(), and events_are_equal().

243 {
244  int res = 0;
245 
246  iterator->event_len = ast_event_get_size(event);
247  iterator->event = event;
248  if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
249  iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
250  } else {
251  iterator->ie = NULL;
252  res = -1;
253  }
254 
255  return res;
256 }
struct ast_event_ie * ie
Definition: event_defs.h:359
#define NULL
Definition: resample.c:96
An event information element.
Definition: event.c:53
const struct ast_event * event
Definition: event_defs.h:358
size_t ast_event_get_size(const struct ast_event *event)
Get the size of an event.
Definition: event.c:228
uint16_t event_len
Definition: event_defs.h:357

◆ ast_event_iterator_next()

int ast_event_iterator_next ( struct ast_event_iterator iterator)

Move iterator instance to next IE.

Parameters
iteratorThe iterator instance
Return values
0on success
-1if end is reached

Definition at line 258 of file event.c.

References ast_event_iterator::event, ast_event_iterator::event_len, ast_event_iterator::ie, and ast_event_ie::ie_payload_len.

Referenced by ast_event_get_ie_raw(), ast_event_get_ie_raw_payload_len(), dump_event(), and events_are_equal().

259 {
260  iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
261  return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
262 }
struct ast_event_ie * ie
Definition: event_defs.h:359
uint16_t ie_payload_len
Definition: event.c:56
An event information element.
Definition: event.c:53
const struct ast_event * event
Definition: event_defs.h:358
uint16_t event_len
Definition: event_defs.h:357

◆ ast_event_minimum_length()

size_t ast_event_minimum_length ( void  )

Get the minimum length of an ast_event.

Returns
minimum amount of memory that will be consumed by any ast_event.

Definition at line 529 of file event.c.

Referenced by cpg_deliver_cb().

530 {
531  return sizeof(struct ast_event);
532 }
An event.
Definition: event.c:81

◆ ast_event_new()

struct ast_event* ast_event_new ( enum ast_event_type  event_type,
  ... 
)

Create a new event.

Parameters
event_typeThe type of event to create

The rest of the arguments to this function specify information elements to add to the event. They are specified in the form:

and must end with AST_EVENT_IE_END.

If the ie_type specified is not AST_EVENT_IE_END, then it must be followed by a valid IE payload type. A payload must also be specified after the IE payload type.

Note
The EID IE will be appended automatically when this function is used with at least one IE specified.
Returns
This returns the event that has been created. If there is an error creating the event, NULL will be returned.

Example usage:

This creates a MWI event with 3 information elements, a mailbox which is a string, and the number of new and old messages, specified as integers.

Definition at line 402 of file event.c.

References ast_calloc, ast_event_append_eid(), ast_event_append_ie_bitflags(), ast_event_append_ie_raw(), ast_event_append_ie_str(), ast_event_append_ie_uint(), ast_event_get_ie_raw(), AST_EVENT_IE_EID, AST_EVENT_IE_END, AST_EVENT_IE_PLTYPE_BITFLAGS, AST_EVENT_IE_PLTYPE_EXISTS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_IE_PLTYPE_UNKNOWN, AST_EVENT_TOTAL, ast_free, AST_LIST_HEAD_NOLOCK_STATIC, AST_LIST_INSERT_TAIL, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_log, ast_malloc, cleanup(), ast_event_ie_val::ie_pltype, ast_event_ie_val::ie_type, ast_event_ie::ie_type, LOG_WARNING, NULL, ast_event_ie_val::payload, ast_event_ie_val::raw, ast_event_ie_val::raw_datalen, ast_event_ie_val::str, and ast_event_ie_val::uint.

Referenced by append_event(), ast_cel_create_event_with_time(), AST_TEST_DEFINE(), corosync_ping(), corosync_ping_to_event(), devstate_to_event(), fake_event(), mwi_to_event(), publish_corosync_ping_to_stasis(), and send_cluster_notify().

403 {
404  va_list ap;
405  struct ast_event *event;
406  enum ast_event_ie_type ie_type;
407  struct ast_event_ie_val *ie_val;
409 
410  /* Invalid type */
411  if (type >= AST_EVENT_TOTAL) {
412  ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
413  "type '%u'!\n", type);
414  return NULL;
415  }
416 
417  va_start(ap, type);
418  for (ie_type = va_arg(ap, enum ast_event_ie_type);
419  ie_type != AST_EVENT_IE_END;
420  ie_type = va_arg(ap, enum ast_event_ie_type))
421  {
422  struct ast_event_ie_val *ie_value = ast_malloc(sizeof(*ie_value));
423  int insert = 0;
424 
425  memset(ie_value, 0, sizeof(*ie_value));
426  ie_value->ie_type = ie_type;
427  ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
428 
429  switch (ie_value->ie_pltype) {
431  ie_value->payload.uint = va_arg(ap, uint32_t);
432  insert = 1;
433  break;
435  ie_value->payload.uint = va_arg(ap, uint32_t);
436  insert = 1;
437  break;
439  ie_value->payload.str = va_arg(ap, const char *);
440  insert = 1;
441  break;
443  {
444  void *data = va_arg(ap, void *);
445  size_t datalen = va_arg(ap, size_t);
446  ie_value->payload.raw = ast_malloc(datalen);
447  memcpy(ie_value->payload.raw, data, datalen);
448  ie_value->raw_datalen = datalen;
449  insert = 1;
450  break;
451  }
454  break;
455  }
456 
457  if (insert) {
458  AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
459  } else {
460  ast_log(LOG_WARNING, "Unsupported PLTYPE(%d)\n", ie_value->ie_pltype);
461  }
462  }
463  va_end(ap);
464 
465  if (!(event = ast_calloc(1, sizeof(*event)))) {
466  return NULL;
467  }
468 
469  event->type = htons(type);
470  event->event_len = htons(sizeof(*event));
471 
472  AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
473  switch (ie_val->ie_pltype) {
475  ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
476  break;
478  ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
479  break;
481  ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
482  break;
484  ast_event_append_ie_raw(&event, ie_val->ie_type,
485  ie_val->payload.raw, ie_val->raw_datalen);
486  break;
489  break;
490  }
491 
492  /* realloc inside one of the append functions failed */
493  if (!event) {
494  goto cleanup;
495  }
496  }
497 
498  if (!ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
499  /* If the event is originating on this server, add the server's
500  * entity ID to the event. */
501  ast_event_append_eid(&event);
502  }
503 
504 cleanup:
505  AST_LIST_TRAVERSE_SAFE_BEGIN(&ie_vals, ie_val, entry) {
507 
508  if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
509  ast_free(ie_val->payload.raw);
510  }
511  ast_free(ie_val);
512  }
514 
515  return event;
516 }
static const char type[]
Definition: chan_ooh323.c:109
int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t data)
Append an information element that has an integer payload.
Definition: event.c:360
An event.
Definition: event.c:81
ast_event_ie_pltype
Payload types for event information elements.
Definition: event_defs.h:321
enum ast_event_ie_pltype ie_pltype
Definition: event.c:94
#define LOG_WARNING
Definition: logger.h:274
Definition: astman.c:222
size_t raw_datalen
Definition: event.c:103
#define NULL
Definition: resample.c:96
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:614
Entity ID Used by All events Payload type: RAW This IE indicates which server the event originated fr...
Definition: event_defs.h:272
const char * str
Definition: event.c:99
int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type, const char *str)
Append an information element that has a string payload.
Definition: event.c:345
#define ast_log
Definition: astobj2.c:42
const void * ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a raw payload.
Definition: event.c:311
ast_event_ie_type
Event Information Element types.
Definition: event_defs.h:68
int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type, uint32_t flags)
Append an information element that has a bitflags payload.
Definition: event.c:367
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:556
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type, const void *data, size_t data_len)
Append an information element that has a raw payload.
Definition: event.c:374
#define AST_LIST_HEAD_NOLOCK_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:345
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:730
enum ast_event_ie_type ie_type
Definition: event.c:93
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
void * raw
Definition: event.c:101
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
union ast_event_ie_val::@385 payload
Definition: search.h:40
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
int ast_event_append_eid(struct ast_event **event)
Append the global EID IE.
Definition: event.c:518
uint32_t uint
Definition: event.c:96

◆ event_iterator_get_ie_raw()

static void* event_iterator_get_ie_raw ( struct ast_event_iterator iterator)
static

Definition at line 283 of file event.c.

References ast_event_iterator::ie, and ast_event_ie::ie_payload.

Referenced by ast_event_get_ie_raw().

284 {
285  return iterator->ie->ie_payload;
286 }
struct ast_event_ie * ie
Definition: event_defs.h:359
unsigned char ie_payload[0]
Definition: event.c:57

◆ event_iterator_get_ie_raw_payload_len()

static uint16_t event_iterator_get_ie_raw_payload_len ( struct ast_event_iterator iterator)
static

Definition at line 325 of file event.c.

References ast_event_iterator::ie, and ast_event_ie::ie_payload_len.

Referenced by ast_event_get_ie_raw_payload_len().

326 {
327  return ntohs(iterator->ie->ie_payload_len);
328 }
struct ast_event_ie * ie
Definition: event_defs.h:359
uint16_t ie_payload_len
Definition: event.c:56

Variable Documentation

◆ event_names

const char* const event_names[AST_EVENT_TOTAL]
static

Event Names.

Definition at line 109 of file event.c.

◆ ie_maps

const struct ie_map ie_maps[AST_EVENT_IE_TOTAL]
static