Asterisk - The Open Source Telephony Project  18.5.0
res_hep_pjsip.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2014, Digium, Inc.
5  *
6  * Matt Jordan <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  * \brief PJSIP logging with Homer
22  *
23  * \author Matt Jordan <[email protected]>
24  *
25  */
26 
27 /*** MODULEINFO
28  <depend>pjproject</depend>
29  <depend>res_pjsip</depend>
30  <depend>res_pjsip_session</depend>
31  <depend>res_hep</depend>
32  <support_level>extended</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 #include <pjsip.h>
38 #include <pjsip_ua.h>
39 #include <pjlib.h>
40 
41 #include "asterisk/res_pjsip.h"
43 #include "asterisk/res_hep.h"
44 #include "asterisk/module.h"
45 #include "asterisk/netsock2.h"
46 
47 static char *assign_uuid(const pj_str_t *call_id, const pj_str_t *local_tag, const pj_str_t *remote_tag)
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 }
73 
74 static int transport_to_protocol_id(pjsip_transport *tp)
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 }
82 
83 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
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 }
155 
156 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
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 }
222 
223 static pjsip_module logging_module = {
224  .name = { "HEPv3 Logging Module", 20 },
225  .priority = 0,
226  .on_rx_request = logging_on_rx_msg,
227  .on_rx_response = logging_on_rx_msg,
228  .on_tx_request = logging_on_tx_msg,
229  .on_tx_response = logging_on_tx_msg,
230 };
231 
232 
233 static int load_module(void)
234 {
235  if (!hepv3_is_loaded()) {
236  ast_log(AST_LOG_WARNING, "res_hep is disabled; declining module load\n");
238  }
239 
242 }
243 
244 static int unload_module(void)
245 {
247  return 0;
248 }
249 
251  .support_level = AST_MODULE_SUPPORT_EXTENDED,
252  .load = load_module,
253  .unload = unload_module,
254  .requires = "res_pjsip,res_pjsip_session,res_hep",
255 );
static int unload_module(void)
struct ast_sockaddr src_addr
Definition: res_hep.h:60
Asterisk main include file. File version handling, generic pbx functions.
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
#define AST_LOG_WARNING
Definition: logger.h:279
Routines for integration with Homer using HEPv3.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#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.
unsigned int zipped
Definition: res_hep.h:74
HEPv3 Capture Info.
Definition: res_hep.h:58
#define ast_log
Definition: astobj2.c:42
static int transport_to_protocol_id(pjsip_transport *tp)
Definition: res_hep_pjsip.c:74
static pjsip_module logging_module
#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 ao2_ref(o, delta)
Definition: astobj2.h:464
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition: res_pjsip.c:3315
Network socket handling.
static int load_module(void)
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
static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
enum hep_uuid_type hepv3_get_uuid_type(void)
Get the preferred UUID type.
Definition: res_hep.c:409
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
hep_uuid_type
Definition: res_hep.h:52
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
int hepv3_is_loaded(void)
Return whether or not we&#39;re currently loaded and active.
Definition: res_hep.c:421
struct timeval capture_time
Definition: res_hep.h:64
static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
Definition: res_hep_pjsip.c:83
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
const char * ast_channel_name(const struct ast_channel *chan)
void ast_sip_unregister_service(pjsip_module *module)
Definition: res_pjsip.c:3331
int hepv3_send_packet(struct hepv3_capture_info *capture_info)
Send a generic packet capture to HEPv3.
Definition: res_hep.c:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
struct ast_sip_session * ast_sip_dialog_get_session(pjsip_dialog *dlg)
Retrieves a session from a dialog.
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