Module: kamailio
Branch: master
Commit: ac22a5c174d4652ae09b779a5384182211700f20
URL: 
https://github.com/kamailio/kamailio/commit/ac22a5c174d4652ae09b779a5384182211700f20

Author: Daniel-Constantin Mierla <mico...@gmail.com>
Committer: Daniel-Constantin Mierla <mico...@gmail.com>
Date: 2024-04-05T13:00:32+02:00

textopsx: added msg_changed_route(rname)

- execute a route block using the changed message, without efectivelly
  changing the current message structure (it stays without changes
  applied)

---

Modified: src/modules/textopsx/textopsx.c

---

Diff:  
https://github.com/kamailio/kamailio/commit/ac22a5c174d4652ae09b779a5384182211700f20.diff
Patch: 
https://github.com/kamailio/kamailio/commit/ac22a5c174d4652ae09b779a5384182211700f20.patch

---

diff --git a/src/modules/textopsx/textopsx.c b/src/modules/textopsx/textopsx.c
index 5710cfeb864..8982924296a 100644
--- a/src/modules/textopsx/textopsx.c
+++ b/src/modules/textopsx/textopsx.c
@@ -46,6 +46,7 @@
 MODULE_VERSION
 
 static int msg_apply_changes_f(sip_msg_t *msg, char *str1, char *str2);
+static int msg_changed_route_f(sip_msg_t *msg, char *str1, char *str2);
 static int msg_set_buffer_f(sip_msg_t *msg, char *p1data, char *p2);
 
 static int change_reply_status_f(sip_msg_t *, char *, char *);
@@ -122,6 +123,8 @@ static pv_export_t mod_pvs[] = {
 static cmd_export_t cmds[] = {
        {"msg_apply_changes", (cmd_function)msg_apply_changes_f, 0, 0, 0,
                        REQUEST_ROUTE | ONREPLY_ROUTE},
+       {"msg_changed_route", (cmd_function)msg_changed_route_f, 1, 
fixup_spve_null,
+                       fixup_free_spve_null, ANY_ROUTE},
        {"msg_set_buffer", (cmd_function)msg_set_buffer_f, 1, fixup_spve_null,
                        fixup_free_spve_null, REQUEST_ROUTE | ONREPLY_ROUTE},
        {"change_reply_status", change_reply_status_f, 2,
@@ -276,6 +279,92 @@ static int msg_apply_changes_f(sip_msg_t *msg, char *str1, 
char *str2)
        return sip_msg_apply_changes(msg);
 }
 
+/**
+ *
+ */
+static int ki_msg_changed_route(sip_msg_t *msg, str *rname)
+{
+       str obuf = STR_NULL;
+       int ridx = -1;
+       sip_msg_t lmsg;
+       sr_kemi_eng_t *keng = NULL;
+       run_act_ctx_t ctx;
+       str evname = str_init("textopsx:msg-changed-route");
+
+       keng = sr_kemi_eng_get();
+       if(keng == NULL) {
+               ridx = route_lookup(&main_rt, rname->s);
+               if(ridx < 0) {
+                       LM_ERR("route block [%.*s] not found\n", rname->len, 
rname->s);
+                       return -1;
+               }
+       }
+
+       if(sip_msg_eval_changes(msg, &obuf) < 0 || obuf.s == NULL) {
+               LM_ERR("failed to evaluate msg changes\n");
+               return -1;
+       }
+
+       memset(&lmsg, 0, sizeof(sip_msg_t));
+       lmsg.buf = obuf.s;
+       lmsg.len = obuf.len;
+       lmsg.id = msg->id;
+       lmsg.pid = msg->pid;
+       lmsg.rcv = msg->rcv;
+       lmsg.set_global_address = msg->set_global_address;
+       lmsg.set_global_port = msg->set_global_port;
+       lmsg.flags = msg->flags;
+       lmsg.msg_flags = msg->msg_flags;
+       memcpy(lmsg.xflags, msg->xflags, KSR_XFLAGS_SIZE * sizeof(flag_t));
+       lmsg.hash_index = msg->hash_index;
+       lmsg.force_send_socket = msg->force_send_socket;
+
+       /* parse the local message */
+       LM_DBG("SIP message content updated - parsing\n");
+       if(parse_msg(lmsg.buf, lmsg.len, &lmsg) != 0) {
+               LM_ERR("parsing new sip message failed [[%.*s]]\n", lmsg.len, 
lmsg.buf);
+               return -1;
+       }
+       if(parse_headers(&lmsg, HDR_FROM_F | HDR_TO_F | HDR_CALLID_F | 
HDR_CSEQ_F, 0)
+                       < 0) {
+               LM_ERR("parsing main headers of new sip message failed 
[[%.*s]]\n",
+                               lmsg.len, lmsg.buf);
+               return -1;
+       }
+       init_run_actions_ctx(&ctx);
+
+       if(keng == NULL) {
+               run_top_route(main_rt.rlist[ridx], &lmsg, &ctx);
+       } else {
+               if(sr_kemi_ctx_route(keng, &ctx, &lmsg, get_route_type(),
+                                  rname, &evname)
+                               < 0) {
+                       LM_ERR("error running route kemi callback\n");
+               }
+       }
+
+       free_sip_msg(&lmsg);
+
+       pkg_free(obuf.s);
+
+       return 1;
+}
+
+/**
+ *
+ */
+static int msg_changed_route_f(sip_msg_t *msg, char *prname, char *str2)
+{
+       str rname = STR_NULL;
+
+       if(fixup_get_svalue(msg, (gparam_t *)prname, &rname) < 0) {
+               LM_ERR("could not get route name param value\n");
+               return -1;
+       }
+
+       return ki_msg_changed_route(msg, &rname);
+}
+
 /**
  *
  */

_______________________________________________
Kamailio (SER) - Development Mailing List
To unsubscribe send an email to sr-dev-le...@lists.kamailio.org

Reply via email to