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

PJSIP logging with Homer. More...

#include "asterisk.h"
#include <pjsip.h>
#include <pjsip_ua.h>
#include <pjlib.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_session.h"
#include "asterisk/res_hep.h"
#include "asterisk/module.h"
#include "asterisk/netsock2.h"
Include dependency graph for res_hep_pjsip.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static char * assign_uuid (const pj_str_t *call_id, const pj_str_t *local_tag, const pj_str_t *remote_tag)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static pj_bool_t logging_on_rx_msg (pjsip_rx_data *rdata)
 
static pj_status_t logging_on_tx_msg (pjsip_tx_data *tdata)
 
static int transport_to_protocol_id (pjsip_transport *tp)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "PJSIP 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_pjsip,res_pjsip_session,res_hep", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static pjsip_module logging_module
 

Detailed Description

PJSIP logging with Homer.

Author
Matt Jordan mjord[email protected][email protected][email protected]igium[email protected].com

Definition in file res_hep_pjsip.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 255 of file res_hep_pjsip.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 255 of file res_hep_pjsip.c.

◆ assign_uuid()

static char* assign_uuid ( const pj_str_t *  call_id,
const pj_str_t *  local_tag,
const pj_str_t *  remote_tag 
)
static

Definition at line 47 of file res_hep_pjsip.c.

References ao2_cleanup, ast_channel_name(), ast_copy_pj_str(), ast_malloc, ast_sip_dialog_get_session(), ast_strdup, HEP_UUID_TYPE_CHANNEL, hepv3_get_uuid_type(), NULL, RAII_VAR, session, and hepv3_capture_info::uuid.

Referenced by logging_on_rx_msg(), and logging_on_tx_msg().

48 {
50  pjsip_dialog *dlg;
51  char *uuid = NULL;
52  enum hep_uuid_type uuid_type = hepv3_get_uuid_type();
53 
54  if ((uuid_type == HEP_UUID_TYPE_CHANNEL)
55  && (dlg = pjsip_ua_find_dialog(call_id, local_tag, remote_tag, PJ_FALSE))
57  && (session->channel)) {
58 
59  uuid = ast_strdup(ast_channel_name(session->channel));
60  }
61 
62  /* If we couldn't get the channel or we never wanted it, default to the call-id */
63  if (!uuid) {
64 
65  uuid = ast_malloc(pj_strlen(call_id) + 1);
66  if (uuid) {
67  ast_copy_pj_str(uuid, call_id, pj_strlen(call_id) + 1);
68  }
69  }
70 
71  return uuid;
72 }
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:5240
#define NULL
Definition: resample.c:96
A structure describing a SIP session.
#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
static struct ast_mansession session
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
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
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_sip_session * ast_sip_dialog_get_session(pjsip_dialog *dlg)
Retrieves a session from a dialog.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 255 of file res_hep_pjsip.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 233 of file res_hep_pjsip.c.

References ast_log, AST_LOG_WARNING, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_sip_register_service(), hepv3_is_loaded(), and logging_module.

Referenced by unload_module().

234 {
235  if (!hepv3_is_loaded()) {
236  ast_log(AST_LOG_WARNING, "res_hep is disabled; declining module load\n");
238  }
239 
242 }
#define AST_LOG_WARNING
Definition: logger.h:279
#define ast_log
Definition: astobj2.c:42
static pjsip_module logging_module
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition: res_pjsip.c:3315
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&#39;re currently loaded and active.
Definition: res_hep.c:421

◆ logging_on_rx_msg()

static pj_bool_t logging_on_rx_msg ( pjsip_rx_data *  rdata)
static

Definition at line 156 of file res_hep_pjsip.c.

References ao2_ref, assign_uuid(), ast_sip_get_pjsip_endpoint(), ast_sockaddr_parse(), hepv3_capture_info::capture_time, hepv3_capture_info::capture_type, hepv3_capture_info::dst_addr, HEPV3_CAPTURE_TYPE_SIP, hepv3_create_capture_info(), hepv3_send_packet(), PARSE_PORT_REQUIRE, hepv3_capture_info::protocol_id, hepv3_capture_info::src_addr, transport_to_protocol_id(), hepv3_capture_info::uuid, and hepv3_capture_info::zipped.

157 {
158  char local_buf[256];
159  char remote_buf[256];
160  char *uuid;
161  struct hepv3_capture_info *capture_info;
162 
163  capture_info = hepv3_create_capture_info(&rdata->pkt_info.packet, rdata->pkt_info.len);
164  if (!capture_info) {
165  return PJ_SUCCESS;
166  }
167 
168  if (!rdata->pkt_info.src_addr_len) {
169  return PJ_SUCCESS;
170  }
171  pj_sockaddr_print(&rdata->pkt_info.src_addr, remote_buf, sizeof(remote_buf), 3);
172 
173  if (!(rdata->tp_info.transport->flag & PJSIP_TRANSPORT_RELIABLE)) {
174  pjsip_tpmgr_fla2_param prm;
175 
176  /* Attempt to determine what IP address we probably received this packet on */
177  pjsip_tpmgr_fla2_param_default(&prm);
178  prm.tp_type = rdata->tp_info.transport->key.type;
179  pj_strset2(&prm.dst_host, rdata->pkt_info.src_name);
180  prm.local_if = PJ_TRUE;
181 
182  /* If we can't get the local address use what we have already */
183  if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), rdata->tp_info.pool, &prm) != PJ_SUCCESS) {
184  pj_sockaddr_print(&rdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
185  } else {
186  if (prm.tp_type & PJSIP_TRANSPORT_IPV6) {
187  snprintf(local_buf, sizeof(local_buf), "[%.*s]:%hu",
188  (int)pj_strlen(&prm.ret_addr),
189  pj_strbuf(&prm.ret_addr),
190  prm.ret_port);
191  } else {
192  snprintf(local_buf, sizeof(local_buf), "%.*s:%hu",
193  (int)pj_strlen(&prm.ret_addr),
194  pj_strbuf(&prm.ret_addr),
195  prm.ret_port);
196  }
197  }
198  } else {
199  pj_sockaddr_print(&rdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
200  }
201 
202  uuid = assign_uuid(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag);
203  if (!uuid) {
204  ao2_ref(capture_info, -1);
205  return PJ_SUCCESS;
206  }
207 
208  ast_sockaddr_parse(&capture_info->src_addr, remote_buf, PARSE_PORT_REQUIRE);
209  ast_sockaddr_parse(&capture_info->dst_addr, local_buf, PARSE_PORT_REQUIRE);
210 
211  capture_info->protocol_id = transport_to_protocol_id(rdata->tp_info.transport);
212  capture_info->capture_time.tv_sec = rdata->pkt_info.timestamp.sec;
213  capture_info->capture_time.tv_usec = rdata->pkt_info.timestamp.msec * 1000;
214  capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
215  capture_info->uuid = uuid;
216  capture_info->zipped = 0;
217 
218  hepv3_send_packet(capture_info);
219 
220  return PJ_FALSE;
221 }
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
enum hepv3_capture_type capture_type
Definition: res_hep.h:70
struct ast_sockaddr dst_addr
Definition: res_hep.h:62
unsigned int zipped
Definition: res_hep.h:74
HEPv3 Capture Info.
Definition: res_hep.h:58
static int transport_to_protocol_id(pjsip_transport *tp)
Definition: res_hep_pjsip.c:74
#define ao2_ref(o, delta)
Definition: astobj2.h:464
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
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
struct timeval capture_time
Definition: res_hep.h:64
int hepv3_send_packet(struct hepv3_capture_info *capture_info)
Send a generic packet capture to HEPv3.
Definition: res_hep.c:581
static char * assign_uuid(const pj_str_t *call_id, const pj_str_t *local_tag, const pj_str_t *remote_tag)
Definition: res_hep_pjsip.c:47

◆ logging_on_tx_msg()

static pj_status_t logging_on_tx_msg ( pjsip_tx_data *  tdata)
static

Definition at line 83 of file res_hep_pjsip.c.

References ao2_ref, assign_uuid(), ast_sip_get_pjsip_endpoint(), ast_sockaddr_parse(), ast_tvnow(), hepv3_capture_info::capture_time, hepv3_capture_info::capture_type, hepv3_capture_info::dst_addr, HEPV3_CAPTURE_TYPE_SIP, hepv3_create_capture_info(), hepv3_send_packet(), PARSE_PORT_REQUIRE, hepv3_capture_info::protocol_id, hepv3_capture_info::src_addr, transport_to_protocol_id(), hepv3_capture_info::uuid, and hepv3_capture_info::zipped.

84 {
85  char local_buf[256];
86  char remote_buf[256];
87  char *uuid;
88  struct hepv3_capture_info *capture_info;
89  pjsip_cid_hdr *cid_hdr;
90  pjsip_from_hdr *from_hdr;
91  pjsip_to_hdr *to_hdr;
92 
93  capture_info = hepv3_create_capture_info(tdata->buf.start, (size_t)(tdata->buf.cur - tdata->buf.start));
94  if (!capture_info) {
95  return PJ_SUCCESS;
96  }
97 
98  if (!(tdata->tp_info.transport->flag & PJSIP_TRANSPORT_RELIABLE)) {
99  pjsip_tpmgr_fla2_param prm;
100 
101  /* Attempt to determine what IP address will we send this packet out of */
102  pjsip_tpmgr_fla2_param_default(&prm);
103  prm.tp_type = tdata->tp_info.transport->key.type;
104  pj_strset2(&prm.dst_host, tdata->tp_info.dst_name);
105  prm.local_if = PJ_TRUE;
106 
107  /* If we can't get the local address use what we have already */
108  if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), tdata->pool, &prm) != PJ_SUCCESS) {
109  pj_sockaddr_print(&tdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
110  } else {
111  if (prm.tp_type & PJSIP_TRANSPORT_IPV6) {
112  snprintf(local_buf, sizeof(local_buf), "[%.*s]:%hu",
113  (int)pj_strlen(&prm.ret_addr),
114  pj_strbuf(&prm.ret_addr),
115  prm.ret_port);
116  } else {
117  snprintf(local_buf, sizeof(local_buf), "%.*s:%hu",
118  (int)pj_strlen(&prm.ret_addr),
119  pj_strbuf(&prm.ret_addr),
120  prm.ret_port);
121  }
122  }
123  } else {
124  /* For reliable transports they can only ever come from the transport
125  * local address.
126  */
127  pj_sockaddr_print(&tdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
128  }
129 
130  pj_sockaddr_print(&tdata->tp_info.dst_addr, remote_buf, sizeof(remote_buf), 3);
131 
132  cid_hdr = PJSIP_MSG_CID_HDR(tdata->msg);
133  from_hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
134  to_hdr = PJSIP_MSG_TO_HDR(tdata->msg);
135 
136  uuid = assign_uuid(&cid_hdr->id, &to_hdr->tag, &from_hdr->tag);
137  if (!uuid) {
138  ao2_ref(capture_info, -1);
139  return PJ_SUCCESS;
140  }
141 
142  ast_sockaddr_parse(&capture_info->src_addr, local_buf, PARSE_PORT_REQUIRE);
143  ast_sockaddr_parse(&capture_info->dst_addr, remote_buf, PARSE_PORT_REQUIRE);
144 
145  capture_info->protocol_id = transport_to_protocol_id(tdata->tp_info.transport);
146  capture_info->capture_time = ast_tvnow();
147  capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
148  capture_info->uuid = uuid;
149  capture_info->zipped = 0;
150 
151  hepv3_send_packet(capture_info);
152 
153  return PJ_SUCCESS;
154 }
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
enum hepv3_capture_type capture_type
Definition: res_hep.h:70
struct ast_sockaddr dst_addr
Definition: res_hep.h:62
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
unsigned int zipped
Definition: res_hep.h:74
HEPv3 Capture Info.
Definition: res_hep.h:58
static int transport_to_protocol_id(pjsip_transport *tp)
Definition: res_hep_pjsip.c:74
#define ao2_ref(o, delta)
Definition: astobj2.h:464
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
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
struct timeval capture_time
Definition: res_hep.h:64
int hepv3_send_packet(struct hepv3_capture_info *capture_info)
Send a generic packet capture to HEPv3.
Definition: res_hep.c:581
static char * assign_uuid(const pj_str_t *call_id, const pj_str_t *local_tag, const pj_str_t *remote_tag)
Definition: res_hep_pjsip.c:47

◆ transport_to_protocol_id()

static int transport_to_protocol_id ( pjsip_transport *  tp)
static

Definition at line 74 of file res_hep_pjsip.c.

Referenced by logging_on_rx_msg(), and logging_on_tx_msg().

75 {
76  /* XXX If we ever add SCTP support, we'll need to revisit */
77  if (tp->flag & PJSIP_TRANSPORT_RELIABLE) {
78  return IPPROTO_TCP;
79  }
80  return IPPROTO_UDP;
81 }

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 244 of file res_hep_pjsip.c.

References AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_SUPPORT_EXTENDED, ast_sip_unregister_service(), ASTERISK_GPL_KEY, load_module(), and logging_module.

245 {
247  return 0;
248 }
static pjsip_module logging_module
void ast_sip_unregister_service(pjsip_module *module)
Definition: res_pjsip.c:3331

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "PJSIP 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_pjsip,res_pjsip_session,res_hep", }
static

Definition at line 255 of file res_hep_pjsip.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 255 of file res_hep_pjsip.c.

◆ logging_module

pjsip_module logging_module
static

Definition at line 223 of file res_hep_pjsip.c.

Referenced by load_module(), and unload_module().