Skip to content

Commit

Permalink
Implement tracing api for xlog messages
Browse files Browse the repository at this point in the history
	Xlog messages can now be traced using siptrace module.
Sip_trace script function will provide the desired functionality.
The scope used for sip_trace function(message/transaction/dialog)
will be the same the scope for the traced xlog messages.
  • Loading branch information
ionutrazvanionita committed Sep 16, 2016
1 parent 5cad545 commit cd678d5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
7 changes: 7 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#include "script_cb.h"
#include "dset.h"
#include "blacklists.h"
#include "xlog.h"

#include "pt.h"
#include "ut.h"
Expand Down Expand Up @@ -1198,6 +1199,12 @@ int main(int argc, char** argv)
goto error;
}

/* init xlog */
if (init_xlog() < 0) {
LM_ERR("error while initializing xlog\n");
goto error;
}

/* register route timers */
if(register_route_timers() < 0) {
LM_ERR("Failed to register timer\n");
Expand Down
1 change: 1 addition & 0 deletions modules/proto_hep/hep.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

#define HEP_PROTO_TYPE_SIP 0x01
#define HEP_PROTO_TYPE_REST 0x055
#define HEP_PROTO_TYPE_XLOG 0x056

enum hep_generic_chunks { HEP_PROTO_FAMILY=0x0001, HEP_PROTO_ID=0x0002,
HEP_IPV4_SRC=0x0003, HEP_IPV4_DST=0x0004, HEP_IPV6_SRC=0x0005,
Expand Down
110 changes: 104 additions & 6 deletions xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@

#include "pvar.h"

#include "modules/siptrace/siptrace.h"


char *log_buf = NULL;

int xlog_buf_size = 4096;
int xlog_force_color = 0;
int xlog_default_level = L_ERR;

/* tracing api */
siptrace_api_t siptrace_api;
/* id with which xlog will be identified by siptrace module */
trace_type_id_t xlog_type_id;
/* xlog string identifier */
static const char* xlog_id_s="xlog";

static int buf_init(void)
{
LM_DBG("initializing...\n");
Expand All @@ -55,15 +64,104 @@ static int buf_init(void)
return 0;
}

int xl_print_log(struct sip_msg* msg, pv_elem_p list, int *len)
int init_xlog(void)
{
if (log_buf == NULL)
if (buf_init())
{
LM_ERR("Cannot print message\n");
if (log_buf == NULL) {
if (buf_init()) {
LM_ERR("Cannot print message!\n");
return -1;
}
}

memset(&siptrace_api, 0, sizeof(siptrace_api_t));

/* try loading siptrace api */
if (load_siptrace_api(&siptrace_api) == 0) {
/* tracing module loaded; try registering string identifier */
xlog_type_id = siptrace_api.register_type((char *)xlog_id_s);
}

return 0;
}

static inline int trace_xlog(struct sip_msg* msg, char* buf, int len)
{
str x_msg = {buf, len};

int siptrace_id_hash=0;
union sockaddr_union to_su, from_su;

const int proto = IPPROTO_TCP;

trace_dest send_dest, old_dest=NULL;
trace_message trace_msg;

if (msg == NULL || buf == NULL) {
LM_ERR("bad input!\n");
return -1;
}

/* api not loaded; no need to continue */
if (siptrace_api.trace_api == NULL)
return 0;

/* xlog not traced; exit... */
if ((siptrace_id_hash=siptrace_api.is_id_traced(xlog_type_id)) == 0)
return 0;


/*
* Source and destination will be set to localhost(127.0.0.1) port 0
*/
from_su.sin.sin_addr.s_addr = TRACE_INADDR_LOOPBACK;
from_su.sin.sin_port = 0;
from_su.sin.sin_family = AF_INET;

to_su.sin.sin_addr.s_addr = TRACE_INADDR_LOOPBACK;
to_su.sin.sin_port = 0;
to_su.sin.sin_family = AF_INET;


while((send_dest=siptrace_api.get_next_destination(old_dest, siptrace_id_hash))) {
trace_msg = siptrace_api.trace_api->create_trace_message(&from_su, &to_su,
proto, &x_msg, HEP_PROTO_TYPE_XLOG, send_dest);
if (trace_msg == NULL) {
LM_ERR("failed to create trace message!\n");
return -1;
}
return pv_printf(msg, list, log_buf, len);

if (siptrace_api.trace_api->add_trace_data(trace_msg, msg->callid->body.s,
msg->callid->body.len, TRACE_TYPE_STR, 0x0011/* correlation id*/, 0) < 0) {
LM_ERR("failed to add correlation id to the packet!\n");
return -1;
}

if (siptrace_api.trace_api->send_message(trace_msg, send_dest, NULL) < 0) {
LM_ERR("failed to send trace message!\n");
return -1;
}

siptrace_api.trace_api->free_message(trace_msg);

old_dest=send_dest;
}

return 0;
}

int xl_print_log(struct sip_msg* msg, pv_elem_p list, int *len)
{
if (pv_printf(msg, list, log_buf, len) < 0) {
LM_ERR("failed to resolve xlog variables!\n");
return -1;
}

if (trace_xlog(msg, log_buf, *len) < 0) {
LM_ERR("failed to trace xlog message!\n");
return -1;
}

return 0;
}


Expand Down
2 changes: 2 additions & 0 deletions xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ int pv_parse_color_name(pv_spec_p sp, str *in);
int pv_get_color(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res);

int init_xlog(void);

#endif

0 comments on commit cd678d5

Please sign in to comment.