Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
res_hep_rtcp.c File Reference

RTCP logging with Homer. More...

#include "asterisk.h"
#include "asterisk/res_hep.h"
#include "asterisk/module.h"
#include "asterisk/netsock2.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/stasis.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/json.h"
#include "asterisk/config.h"
Include dependency graph for res_hep_rtcp.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static char * assign_uuid (struct ast_json *json_channel)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static void rtcp_message_handler (struct stasis_message *message)
 
static void rtp_topic_handler (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RTCP HEPv3 Logger" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_hep", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct stasis_subscriptionstasis_rtp_subscription
 

Detailed Description

RTCP logging with Homer.

Author
Matt Jordan mjord.nosp@m.an@d.nosp@m.igium.nosp@m..com

Definition in file res_hep_rtcp.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 191 of file res_hep_rtcp.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 191 of file res_hep_rtcp.c.

◆ assign_uuid()

static char* assign_uuid ( struct ast_json json_channel)
static

Definition at line 46 of file res_hep_rtcp.c.

References ast_begins_with(), ast_channel_cleanup, ast_channel_get_by_name(), ast_func_read(), ast_json_object_get(), ast_json_string_get(), ast_strdup, buf, HEP_UUID_TYPE_CALL_ID, hepv3_get_uuid_type(), and NULL.

Referenced by rtcp_message_handler().

47 {
48  const char *channel_name = ast_json_string_get(ast_json_object_get(json_channel, "name"));
49  enum hep_uuid_type uuid_type = hepv3_get_uuid_type();
50  char *uuid = NULL;
51 
52  if (!channel_name) {
53  return NULL;
54  }
55 
56  if (uuid_type == HEP_UUID_TYPE_CALL_ID) {
57  struct ast_channel *chan = NULL;
58  char buf[128];
59 
60  if (ast_begins_with(channel_name, "PJSIP")) {
61  chan = ast_channel_get_by_name(channel_name);
62 
63  if (chan && !ast_func_read(chan, "CHANNEL(pjsip,call-id)", buf, sizeof(buf))) {
64  uuid = ast_strdup(buf);
65  }
66  } else if (ast_begins_with(channel_name, "SIP")) {
67  chan = ast_channel_get_by_name(channel_name);
68 
69  if (chan && !ast_func_read(chan, "SIP_HEADER(call-id)", buf, sizeof(buf))) {
70  uuid = ast_strdup(buf);
71  }
72  }
73 
74  ast_channel_cleanup(chan);
75  }
76 
77  /* If we couldn't get the call-id or didn't want it, just use the channel name */
78  if (!uuid) {
79  uuid = ast_strdup(channel_name);
80  }
81 
82  return uuid;
83 }
int ast_func_read(struct ast_channel *chan, const char *function, char *workspace, size_t len)
executes a read operation on a function
Main Channel structure associated with a channel.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
#define ast_channel_cleanup(c)
Cleanup a channel reference.
Definition: channel.h:2992
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
enum hep_uuid_type hepv3_get_uuid_type(void)
Get the preferred UUID type.
Definition: res_hep.c:409
hep_uuid_type
Definition: res_hep.h:52
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Definition: strings.h:94
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 191 of file res_hep_rtcp.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 158 of file res_hep_rtcp.c.

References ast_log, AST_LOG_WARNING, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_rtp_rtcp_received_type(), ast_rtp_rtcp_sent_type(), ast_rtp_topic(), hepv3_is_loaded(), NULL, rtp_topic_handler(), stasis_subscribe, stasis_subscription_accept_message_type(), STASIS_SUBSCRIPTION_FILTER_SELECTIVE, and stasis_subscription_set_filter().

Referenced by unload_module().

159 {
160  if (!hepv3_is_loaded()) {
161  ast_log(AST_LOG_WARNING, "res_hep is disabled; declining module load\n");
163  }
164 
169  }
173 
175 }
struct stasis_message_type * ast_rtp_rtcp_sent_type(void)
Message type for an RTCP message sent from this Asterisk instance.
#define AST_LOG_WARNING
Definition: logger.h:279
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
Definition: stasis.c:1079
#define NULL
Definition: resample.c:96
static struct stasis_subscription * stasis_rtp_subscription
Definition: res_hep_rtcp.c:44
struct stasis_message_type * ast_rtp_rtcp_received_type(void)
Message type for an RTCP message received from some external source.
#define ast_log
Definition: astobj2.c:42
#define stasis_subscribe(topic, callback, data)
Definition: stasis.h:652
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int hepv3_is_loaded(void)
Return whether or not we're currently loaded and active.
Definition: res_hep.c:421
static void rtp_topic_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: res_hep_rtcp.c:148
struct stasis_topic * ast_rtp_topic(void)
Stasis Message Bus API topic for RTP and RTCP related messages
Definition: rtp_engine.c:3531
int stasis_subscription_accept_message_type(struct stasis_subscription *subscription, const struct stasis_message_type *type)
Indicate to a subscription that we are interested in a message type.
Definition: stasis.c:1025

◆ rtcp_message_handler()

static void rtcp_message_handler ( struct stasis_message message)
static

Definition at line 85 of file res_hep_rtcp.c.

References ao2_ref, assign_uuid(), ast_json_dump_string, ast_json_free(), ast_json_object_get(), ast_json_string_get(), ast_json_unref(), ast_sockaddr_parse(), ast_strlen_zero, ast_tvnow(), hepv3_capture_info::capture_time, hepv3_capture_info::capture_type, hepv3_capture_info::dst_addr, HEPV3_CAPTURE_TYPE_RTCP, hepv3_create_capture_info(), hepv3_send_packet(), NULL, PARSE_PORT_REQUIRE, RAII_VAR, hepv3_capture_info::src_addr, stasis_message_to_json(), hepv3_capture_info::uuid, and hepv3_capture_info::zipped.

Referenced by rtp_topic_handler().

86 {
87 
88  RAII_VAR(struct ast_json *, json_payload, NULL, ast_json_unref);
89  RAII_VAR(char *, payload, NULL, ast_json_free);
90  struct ast_json *json_blob;
91  struct ast_json *json_channel;
92  struct ast_json *json_rtcp;
93  struct hepv3_capture_info *capture_info;
94  struct ast_json *from;
95  struct ast_json *to;
96  struct timeval current_time = ast_tvnow();
97 
98  json_payload = stasis_message_to_json(message, NULL);
99  if (!json_payload) {
100  return;
101  }
102 
103  json_blob = ast_json_object_get(json_payload, "blob");
104  if (!json_blob) {
105  return;
106  }
107 
108  json_channel = ast_json_object_get(json_payload, "channel");
109  if (!json_channel) {
110  return;
111  }
112 
113  json_rtcp = ast_json_object_get(json_payload, "rtcp_report");
114  if (!json_rtcp) {
115  return;
116  }
117 
118  from = ast_json_object_get(json_blob, "from");
119  to = ast_json_object_get(json_blob, "to");
120  if (!from || !to) {
121  return;
122  }
123 
124  payload = ast_json_dump_string(json_rtcp);
125  if (ast_strlen_zero(payload)) {
126  return;
127  }
128 
129  capture_info = hepv3_create_capture_info(payload, strlen(payload));
130  if (!capture_info) {
131  return;
132  }
135 
136  capture_info->uuid = assign_uuid(json_channel);
137  if (!capture_info->uuid) {
138  ao2_ref(capture_info, -1);
139  return;
140  }
141  capture_info->capture_time = current_time;
142  capture_info->capture_type = HEPV3_CAPTURE_TYPE_RTCP;
143  capture_info->zipped = 0;
144 
145  hepv3_send_packet(capture_info);
146 }
struct ast_sockaddr src_addr
Definition: res_hep.h:60
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
enum hepv3_capture_type capture_type
Definition: res_hep.h:70
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
struct ast_sockaddr dst_addr
Definition: res_hep.h:62
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition: json.h:763
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define NULL
Definition: resample.c:96
struct ast_json * stasis_message_to_json(struct stasis_message *msg, struct stasis_message_sanitizer *sanitize)
Build the JSON representation of the message.
unsigned int zipped
Definition: res_hep.h:74
#define ast_strlen_zero(foo)
Definition: strings.h:52
HEPv3 Capture Info.
Definition: res_hep.h:58
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static char * assign_uuid(struct ast_json *json_channel)
Definition: res_hep_rtcp.c:46
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
struct hepv3_capture_info * hepv3_create_capture_info(const void *payload, size_t len)
Create a hepv3_capture_info object.
Definition: res_hep.c:428
struct timeval capture_time
Definition: res_hep.h:64
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
Abstract JSON element (object, array, string, int, ...).
int hepv3_send_packet(struct hepv3_capture_info *capture_info)
Send a generic packet capture to HEPv3.
Definition: res_hep.c:581

◆ rtp_topic_handler()

static void rtp_topic_handler ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Definition at line 148 of file res_hep_rtcp.c.

References ast_rtp_rtcp_received_type(), ast_rtp_rtcp_sent_type(), rtcp_message_handler(), and stasis_message_type().

Referenced by load_module().

149 {
150  struct stasis_message_type *message_type = stasis_message_type(message);
151 
152  if ((message_type == ast_rtp_rtcp_sent_type()) ||
153  (message_type == ast_rtp_rtcp_received_type())) {
154  rtcp_message_handler(message);
155  }
156 }
struct stasis_message_type * ast_rtp_rtcp_sent_type(void)
Message type for an RTCP message sent from this Asterisk instance.
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
struct stasis_message_type * ast_rtp_rtcp_received_type(void)
Message type for an RTCP message received from some external source.
static void rtcp_message_handler(struct stasis_message *message)
Definition: res_hep_rtcp.c:85

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 177 of file res_hep_rtcp.c.

References AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_SUPPORT_EXTENDED, ASTERISK_GPL_KEY, load_module(), and stasis_unsubscribe_and_join().

178 {
181  }
182 
183  return 0;
184 }
static struct stasis_subscription * stasis_rtp_subscription
Definition: res_hep_rtcp.c:44
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1136

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RTCP HEPv3 Logger" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_hep", }
static

Definition at line 191 of file res_hep_rtcp.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 191 of file res_hep_rtcp.c.

◆ stasis_rtp_subscription

struct stasis_subscription* stasis_rtp_subscription
static

Definition at line 44 of file res_hep_rtcp.c.