Hi All, I'm trying to add an event route to be execute for each successfully delivered message from msilo module, but I'm still not getting running the event route I have defined for it in the script.
If someone can have a look at the patch and give me some hint would be really great. Thanks in advance. Roberto Fichera.
diff --git a/modules/msilo/msilo.c b/modules/msilo/msilo.c index 903b3cf..3d55c3a 100644 --- a/modules/msilo/msilo.c +++ b/modules/msilo/msilo.c @@ -62,9 +62,13 @@ #include "../../parser/contact/parse_contact.h" #include "../../parser/parse_allow.h" #include "../../parser/parse_methods.h" +#include "../../parser/msg_parser.h" #include "../../resolve.h" #include "../../usr_avp.h" #include "../../mod_fix.h" +#include "../../dprint.h" +#include "../../globals.h" +#include "../../dset.h" #include "../../modules/tm/tm_load.h" @@ -73,7 +77,7 @@ #include "api.h" #define MAX_DEL_KEYS 1 -#define NR_KEYS 11 +#define NR_KEYS 12 static str sc_mid = str_init("id"); /* 0 */ static str sc_from = str_init("src_addr"); /* 1 */ @@ -86,6 +90,7 @@ static str sc_exp_time = str_init("exp_time"); /* 7 */ static str sc_inc_time = str_init("inc_time"); /* 8 */ static str sc_snd_time = str_init("snd_time"); /* 9 */ static str sc_stored_hdrs = str_init("extra_hdrs"); /* 10 */ +static str sc_callid = str_init("callid"); /* 11 */ #define SET_STR_VAL(_str, _res, _r, _c) \ if (RES_ROWS(_res)[_r].values[_c].nul == 0) \ @@ -185,6 +190,7 @@ int check_message_support(struct sip_msg* msg); /** TM callback function */ static void m_tm_callback( struct cell *t, int type, struct tmcb_params *ps); +static void m_tm_callback_on_delivered_event( struct cell *t, int type, struct tmcb_params *ps); static cmd_export_t cmds[]={ {"m_store", (cmd_function)m_store_2, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE}, @@ -224,6 +230,7 @@ static param_export_t params[]={ { "sc_inc_time", STR_PARAM, &sc_inc_time.s }, { "sc_snd_time", STR_PARAM, &sc_snd_time.s }, { "sc_stored_hdrs", STR_PARAM, &sc_stored_hdrs.s }, + { "sc_callid", STR_PARAM, &sc_callid.s }, { "snd_time_avp", STR_PARAM, &ms_snd_time_avp_param.s }, { "extra_hdrs_avp", STR_PARAM, &ms_extra_hdrs_avp_param.s }, { "add_date", INT_PARAM, &ms_add_date }, @@ -281,6 +288,17 @@ static int bind_msilo(msilo_api_t* api) return 0; } +/* where to go for the local request route ("msilo:on-delivered") */ +static int msilo_event_on_delivered=-1; /* default disabled */ + +typedef struct msilo_notification_event +{ + struct sip_msg *req; + int mid; +} msilo_notification_event_t; + +static msilo_notification_event_t *msilo_make_request(str *ruri, str *to, str *from, str *hdr, str *callid, int mid); + /** * init module function */ @@ -480,6 +498,11 @@ static int mod_init(void) if(ms_outbound_proxy.s!=NULL) ms_outbound_proxy.len = strlen(ms_outbound_proxy.s); + /* check if the on-delivered route event exists or not */ + msilo_event_on_delivered=route_lookup(&event_rt, "msilo:on-delivered"); + if (msilo_event_on_delivered>=0 && event_rt.rlist[msilo_event_on_delivered]==0) + msilo_event_on_delivered=-1; /* disable */ + return 0; } @@ -583,8 +606,8 @@ static int m_store(struct sip_msg* msg, str *owner_s) #define EXTRA_HDRS_BUF_LEN 1024 static char extra_hdrs_buf[EXTRA_HDRS_BUF_LEN]; str extra_hdrs; - db_key_t db_keys[NR_KEYS-1]; - db_val_t db_vals[NR_KEYS-1]; + db_key_t db_keys[NR_KEYS]; + db_val_t db_vals[NR_KEYS]; db_key_t db_cols[1]; db1_res_t* res = NULL; uac_req_t uac_r; @@ -839,6 +862,16 @@ static int m_store(struct sip_msg* msg, str *owner_s) db_vals[nr_keys].val.blob_val.len = extra_hdrs.len; nr_keys++; + + /* add callid */ + db_keys[nr_keys] = &sc_callid; + + db_vals[nr_keys].type = DB1_STR; + db_vals[nr_keys].nul = 0; + db_vals[nr_keys].val.str_val.s = msg->callid->body.s; + db_vals[nr_keys].val.str_val.len = msg->callid->body.len; + + nr_keys++; if(msilo_dbf.insert(db_con, db_keys, db_vals, nr_keys) < 0) { @@ -962,16 +995,16 @@ static int m_dump(struct sip_msg* msg, str* owner_s) db_key_t ob_key; db_op_t db_ops[3]; db_val_t db_vals[3]; - db_key_t db_cols[7]; + db_key_t db_cols[8]; db1_res_t* db_res = NULL; - int i, db_no_cols = 7, db_no_keys = 3, mid, n; + int i, db_no_cols = 8, db_no_keys = 3, mid, n; static char hdr_buf[1024]; static char body_buf[1024]; struct sip_uri puri; uac_req_t uac_r; - str str_vals[5], hdr_str, body_str, extra_hdrs_str, tmp_extra_hdrs; + str str_vals[6], hdr_str, body_str, extra_hdrs_str, tmp_extra_hdrs; time_t rtime; - + /* init */ ob_key = &sc_mid; @@ -989,6 +1022,7 @@ static int m_dump(struct sip_msg* msg, str* owner_s) db_cols[4]=&sc_ctype; db_cols[5]=&sc_inc_time; db_cols[6]=&sc_stored_hdrs; + db_cols[7]=&sc_callid; LM_DBG("------------ start ------------\n"); @@ -1096,8 +1130,8 @@ static int m_dump(struct sip_msg* msg, str* owner_s) SET_STR_VAL(str_vals[2], db_res, i, 3); /* body */ SET_STR_VAL(str_vals[3], db_res, i, 4); /* ctype */ SET_STR_VAL(str_vals[4], db_res, i, 6); /* stored hdrs */ - rtime = - (time_t)RES_ROWS(db_res)[i].values[5/*inc time*/].val.int_val; + SET_STR_VAL(str_vals[5], db_res, i, 7); /* callid */ + rtime = (time_t)RES_ROWS(db_res)[i].values[5/*inc time*/].val.int_val; if (ms_extra_hdrs != NULL) { if (fixup_get_svalue(msg, (gparam_p)*ms_extra_hdrs_sp, @@ -1145,7 +1179,7 @@ static int m_dump(struct sip_msg* msg, str* owner_s) if(tmp_extra_hdrs.len>0) pkg_free(tmp_extra_hdrs.s); - LM_DBG("msg [%d-%d] for: %.*s\n", i+1, mid, pto->uri.len, pto->uri.s); + LM_DBG("msg [%d-%d] for: %.*s\n", i+1, mid, pto->uri.len, pto->uri.s); /** sending using TM function: t_uac */ body_str.len = 1024; @@ -1166,14 +1200,20 @@ static int m_dump(struct sip_msg* msg, str* owner_s) uac_r.headers = &hdr_str; uac_r.body = (n<0)?&str_vals[2]:&body_str; uac_r.cb_flags = TMCB_LOCAL_COMPLETED; - uac_r.cb = m_tm_callback; - uac_r.cbp = (void*)(long)mid; - - tmb.t_request(&uac_r, /* UAC Req */ - &str_vals[1], /* Request-URI */ - &str_vals[1], /* To */ - &str_vals[0], /* From */ - (ms_outbound_proxy.s)?&ms_outbound_proxy:0 /* ob uri */ + uac_r.cbp = (void*)msilo_make_request(&str_vals[1] /* ruri */, + &str_vals[1] /* to */, + &str_vals[0] /* from */, + &hdr_str, + &str_vals[5] /* callid */, + mid); + + uac_r.cb = m_tm_callback_on_delivered_event; + + tmb.t_request(&uac_r, /* UAC Req */ + &str_vals[1], /* Request-URI */ + &str_vals[1], /* To */ + &str_vals[0], /* From */ + (ms_outbound_proxy.s)?&ms_outbound_proxy:0 /* ob uri */ ); } @@ -1334,6 +1374,69 @@ done: return; } +/** + * TM callback function - delete message from database if was sent OK and execute + * the msilo:on-delivered event route + */ +void m_tm_callback_on_delivered_event(struct cell *t, int type, struct tmcb_params *ps) +{ + msilo_notification_event_t *event; + + if(ps->param==NULL || *ps->param == NULL) + { + LM_DBG("message id not received\n"); + goto done; + } + + event = (msilo_notification_event_t*)*ps->param; + + LM_DBG(">>>>>>> msilo_notification_event %p\n", event); + + LM_DBG("completed with status %d [mid: %d]\n", ps->code, event->mid); + if(!db_con) + { + LM_ERR("db_con is NULL\n"); + goto done; + } + if(ps->code >= 300) + { + LM_DBG("message <%d> was not sent successfully\n", event->mid); + msg_list_set_flag(ml, event->mid, MS_MSG_ERRO); + goto done; + } + + LM_DBG("message <%d> was sent successfully\n", event->mid); + msg_list_set_flag(ml, event->mid, MS_MSG_DONE); + + /* execute the msilo:on-delivered event route */ + if(unlikely(msilo_event_on_delivered>=0)) + { + int sflag_bk; + int backup_route_type; + struct run_act_ctx ctx; + + LM_DBG("executing event_route[msilo:on-delivered] (%d)\n", msilo_event_on_delivered); + + sflag_bk = getsflags(); + backup_route_type = get_route_type(); + + set_route_type(EVENT_ROUTE); + init_run_actions_ctx(&ctx); + LM_DBG("event_route[msilo:on-delivered] ps->req=%p)\n", event->req); + run_top_route(event_rt.rlist[msilo_event_on_delivered], event->req, &ctx); + + set_route_type(backup_route_type); + setsflagsval(sflag_bk); + +/* pkg_free(event->req->buf); + pkg_free(event->req); + pkg_free(event); */ + } + +done: + return; +} + void m_send_ontimer(unsigned int ticks, void *param) { db_key_t db_keys[2]; @@ -1620,3 +1723,95 @@ int check_message_support(struct sip_msg* msg) return -1; } +#define FAKED_SIP_MSG "MESSAGE %.*s SIP/2.0\r\nVia: SIP/2.0/UDP 127.0.0.1\r\nFrom: <%.*s>;tag=123\r\nTo: <%.*s>\r\nCall-ID: %.*s\r\n%.*s\r\nCSeq: 1 MESSAGE\r\nContent-Length: 0\r\n\r\n" +#define FAKED_SIP_MSG_LEN (sizeof(FAKED_SIP_MSG)-1) +static unsigned int _faked_msg_no = 1; + +static msilo_notification_event_t *msilo_make_request(str *ruri, str *to, str *from, str *hdr, str *callid, int mid) +{ + msilo_notification_event_t *ret = NULL; + struct sip_msg *msg = NULL; + char *buff = NULL; + int buff_len = 0; + + if ((ret = pkg_malloc(sizeof(msilo_notification_event_t))) == NULL) + { + LM_ERR("Out of pkg memory"); + goto out; + } + + memset(ret, 0, sizeof(msilo_notification_event_t)); + + if ((msg = pkg_malloc(sizeof(struct sip_msg))) == NULL) + { + LM_ERR("Out of pkg memory"); + pkg_free(ret); + ret = NULL; + goto out; + } + + LM_DBG(">>>>>>> msilo_sip_msg at %p\n", msg); + + memset(msg, 0, sizeof(struct sip_msg)); + + buff_len = FAKED_SIP_MSG_LEN + ruri->len + to->len + from->len + hdr->len + callid->len + 1; + + if ((buff = pkg_malloc(buff_len)) == NULL) + { + LM_ERR("Out of pkg memory"); + pkg_free(msg); + pkg_free(ret); + ret = NULL; + goto out; + } + + memset(buff, 0, buff_len); + + msg->id = 1 + _faked_msg_no++; + msg->pid = my_pid(); + memset(&msg->tval, 0, sizeof(struct timeval)); + clear_branches(); + + msg->buf = buff; + msg->len = buff_len-1; + + snprintf( msg->buf, buff_len-1, FAKED_SIP_MSG, ruri->len, ruri->s, + from->len, from->s, + to->len, to->s, + callid->len, callid->s, + hdr->len, hdr->s ); + msg->buf[buff_len-1] = '\0'; + + LM_DBG(">>>>>>> msilo_event_msg[%d]: %s <<<<<<<<", msg->len, msg->buf); + + msg->set_global_address = default_global_address; + msg->set_global_port = default_global_port; + + if (parse_msg(msg->buf, msg->len, msg) != 0) + { + LM_ERR("parse_msg failed\n"); + pkg_free(buff); + pkg_free(msg); + pkg_free(ret); + ret = NULL; + goto out; + } + + msg->rcv.proto = PROTO_UDP; + msg->rcv.src_port = 5060; + msg->rcv.src_ip.u.addr32[0] = 0x7f000001; + msg->rcv.src_ip.af = AF_INET; + msg->rcv.src_ip.len = 4; + msg->rcv.dst_port = 5060; + msg->rcv.dst_ip.u.addr32[0] = 0x7f000001; + msg->rcv.dst_ip.af = AF_INET; + msg->rcv.dst_ip.len = 4; + + ret->req = msg; + ret->mid = mid; + +out: + LM_DBG(">>>>>>> msilo_notification_event %p\n", ret); + return ret; +} +
_______________________________________________ SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list sr-users@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users