Browse Source

tm: rpc commands with hexa-encoded body

Daniel-Constantin Mierla 2 months ago
parent
commit
c7e04ab790
3 changed files with 151 additions and 13 deletions
  1. 86 10
      src/modules/tm/rpc_uac.c
  2. 6 0
      src/modules/tm/rpc_uac.h
  3. 59 3
      src/modules/tm/tm.c

+ 86 - 10
src/modules/tm/rpc_uac.c

@@ -631,11 +631,16 @@ static void rpc_uac_block_callback(
  *                     for the transaction arrives, if 0 immediately send
  *                     an rpc reply (see above). If 2 blocking wait until
  *                     final response for the transaction arrives.
+ * @param cbflags - uac req callback flags
+ * @param rpflags - rpc parameters flags
  */
-static void rpc_t_uac(rpc_t *rpc, void *c, int reply_wait, int cbflags)
+static void rpc_t_uac(
+		rpc_t *rpc, void *c, int reply_wait, int cbflags, int rpflags)
 {
 	/* rpc params */
-	str method, ruri, nexthop, send_socket, headers, body;
+	str method, ruri, nexthop, send_socket, headers;
+	str body = STR_NULL;
+	str sraw = STR_NULL;
 	/* other internal vars.*/
 	str hfb, callid;
 	struct sip_uri p_uri, pnexthop;
@@ -654,8 +659,6 @@ static void rpc_t_uac(rpc_t *rpc, void *c, int reply_wait, int cbflags)
 	int rcount = 0;
 	void *th = NULL;
 
-	body.s = 0;
-	body.len = 0;
 	dctx = 0;
 	if(reply_wait == 1
 			&& (rpc->capabilities == 0
@@ -680,6 +683,13 @@ static void rpc_t_uac(rpc_t *rpc, void *c, int reply_wait, int cbflags)
 		rpc->fault(c, 400, "Invalid request uri \"%s\"", ruri.s);
 		return;
 	}
+	if(body.len > 0 && (rpflags & 1)) {
+		if(ksr_hex_decode_ws(&body, &sraw) < 0) {
+			rpc->fault(c, 400, "Invalid hexa body");
+			return;
+		}
+		body = sraw;
+	}
 	/* old fifo & unixsock backwards compatibility for nexthop: '.' is still
 	   allowed */
 	if(nexthop.len == 1 && nexthop.s[0] == '.') {
@@ -689,6 +699,9 @@ static void rpc_t_uac(rpc_t *rpc, void *c, int reply_wait, int cbflags)
 	} else if(nexthop.len == 0) {
 		nexthop.s = 0;
 	} else if(parse_uri(nexthop.s, nexthop.len, &pnexthop) < 0) {
+		if(sraw.s != NULL) {
+			pkg_free(sraw.s);
+		}
 		rpc->fault(c, 400, "Invalid next-hop uri \"%s\"", nexthop.s);
 		return;
 	}
@@ -707,10 +720,16 @@ static void rpc_t_uac(rpc_t *rpc, void *c, int reply_wait, int cbflags)
 					  ||
 					  /* check also if it's not a MH addr. */
 					  saddr.len == 0 || saddr.s[0] == '(')) {
+		if(sraw.s != NULL) {
+			pkg_free(sraw.s);
+		}
 		rpc->fault(c, 400, "Invalid send socket \"%s\"", send_socket.s);
 		return;
 	} else if(saddr.len
 			  && (ssock = grep_sock_info(&saddr, sport, sproto)) == 0) {
+		if(sraw.s != NULL) {
+			pkg_free(sraw.s);
+		}
 		rpc->fault(c, 400, "No local socket for \"%s\"", send_socket.s);
 		return;
 	}
@@ -719,6 +738,9 @@ static void rpc_t_uac(rpc_t *rpc, void *c, int reply_wait, int cbflags)
 	faked_msg.len = headers.len;
 	faked_msg.buf = faked_msg.unparsed = headers.s;
 	if(parse_headers(&faked_msg, HDR_EOH_F, 0) == -1) {
+		if(sraw.s != NULL) {
+			pkg_free(sraw.s);
+		}
 		rpc->fault(c, 400, "Invalid headers");
 		return;
 	}
@@ -832,6 +854,9 @@ static void rpc_t_uac(rpc_t *rpc, void *c, int reply_wait, int cbflags)
 						   &ritem->rtext)
 						< 0) {
 					rpc->fault(c, 500, "Fields error");
+					if(sraw.s != NULL) {
+						pkg_free(sraw.s);
+					}
 					return;
 				}
 			}
@@ -845,6 +870,9 @@ error01:
 error:
 	if(faked_msg.headers)
 		free_hdr_field_lst(faked_msg.headers);
+	if(sraw.s != NULL) {
+		pkg_free(sraw.s);
+	}
 }
 
 
@@ -853,7 +881,15 @@ error:
  */
 void rpc_t_uac_start(rpc_t *rpc, void *c)
 {
-	rpc_t_uac(rpc, c, 0, 0);
+	rpc_t_uac(rpc, c, 0, 0, 0);
+}
+
+/** t_uac with no reply waiting and hex body.
+ * @see rpc_t_uac.
+ */
+void rpc_t_uac_start_hex(rpc_t *rpc, void *c)
+{
+	rpc_t_uac(rpc, c, 0, 0, 1);
 }
 
 /** t_uac with reply waiting.
@@ -861,7 +897,15 @@ void rpc_t_uac_start(rpc_t *rpc, void *c)
  */
 void rpc_t_uac_wait(rpc_t *rpc, void *c)
 {
-	rpc_t_uac(rpc, c, 1, 0);
+	rpc_t_uac(rpc, c, 1, 0, 0);
+}
+
+/** t_uac with reply waiting and hex body.
+ * @see rpc_t_uac.
+ */
+void rpc_t_uac_wait_hex(rpc_t *rpc, void *c)
+{
+	rpc_t_uac(rpc, c, 1, 0, 1);
 }
 
 /** t_uac with blocking for reply waiting.
@@ -869,7 +913,15 @@ void rpc_t_uac_wait(rpc_t *rpc, void *c)
  */
 void rpc_t_uac_wait_block(rpc_t *rpc, void *c)
 {
-	rpc_t_uac(rpc, c, 2, 0);
+	rpc_t_uac(rpc, c, 2, 0, 0);
+}
+
+/** t_uac with blocking for reply waiting and hex body.
+ * @see rpc_t_uac.
+ */
+void rpc_t_uac_wait_block_hex(rpc_t *rpc, void *c)
+{
+	rpc_t_uac(rpc, c, 2, 0, 1);
 }
 
 /** t_uac with no reply waiting and no ack.
@@ -877,7 +929,15 @@ void rpc_t_uac_wait_block(rpc_t *rpc, void *c)
  */
 void rpc_t_uac_start_noack(rpc_t *rpc, void *c)
 {
-	rpc_t_uac(rpc, c, 0, TMCB_DONT_ACK);
+	rpc_t_uac(rpc, c, 0, TMCB_DONT_ACK, 0);
+}
+
+/** t_uac with no reply waiting, no ack and hex body.
+ * @see rpc_t_uac.
+ */
+void rpc_t_uac_start_noack_hex(rpc_t *rpc, void *c)
+{
+	rpc_t_uac(rpc, c, 0, TMCB_DONT_ACK, 1);
 }
 
 /** t_uac with reply waiting and no ack.
@@ -885,7 +945,15 @@ void rpc_t_uac_start_noack(rpc_t *rpc, void *c)
  */
 void rpc_t_uac_wait_noack(rpc_t *rpc, void *c)
 {
-	rpc_t_uac(rpc, c, 1, TMCB_DONT_ACK);
+	rpc_t_uac(rpc, c, 1, TMCB_DONT_ACK, 0);
+}
+
+/** t_uac with reply waiting, no ack and hex body.
+ * @see rpc_t_uac.
+ */
+void rpc_t_uac_wait_noack_hex(rpc_t *rpc, void *c)
+{
+	rpc_t_uac(rpc, c, 1, TMCB_DONT_ACK, 1);
 }
 
 /** t_uac with blocking for reply waiting and no ack.
@@ -893,7 +961,15 @@ void rpc_t_uac_wait_noack(rpc_t *rpc, void *c)
  */
 void rpc_t_uac_wait_block_noack(rpc_t *rpc, void *c)
 {
-	rpc_t_uac(rpc, c, 2, TMCB_DONT_ACK);
+	rpc_t_uac(rpc, c, 2, TMCB_DONT_ACK, 0);
+}
+
+/** t_uac with blocking for reply waiting, no ack and hex body.
+ * @see rpc_t_uac.
+ */
+void rpc_t_uac_wait_block_noack_hex(rpc_t *rpc, void *c)
+{
+	rpc_t_uac(rpc, c, 2, TMCB_DONT_ACK, 1);
 }
 
 

+ 6 - 0
src/modules/tm/rpc_uac.h

@@ -25,12 +25,18 @@ int tm_rpc_response_list_init(void);
 int tm_rpc_response_list_destroy(void);
 
 void rpc_t_uac_start(rpc_t *rpc, void *c);
+void rpc_t_uac_start_hex(rpc_t *rpc, void *c);
 void rpc_t_uac_wait(rpc_t *rpc, void *c);
+void rpc_t_uac_wait_hex(rpc_t *rpc, void *c);
 void rpc_t_uac_wait_block(rpc_t *rpc, void *c);
+void rpc_t_uac_wait_block_hex(rpc_t *rpc, void *c);
 
 void rpc_t_uac_start_noack(rpc_t *rpc, void *c);
+void rpc_t_uac_start_noack_hex(rpc_t *rpc, void *c);
 void rpc_t_uac_wait_noack(rpc_t *rpc, void *c);
+void rpc_t_uac_wait_noack_hex(rpc_t *rpc, void *c);
 void rpc_t_uac_wait_block_noack(rpc_t *rpc, void *c);
+void rpc_t_uac_wait_block_noack_hex(rpc_t *rpc, void *c);
 
 int t_uac_send(str *method, str *ruri, str *nexthop, str *send_socket,
 		str *headers, str *body);

+ 59 - 3
src/modules/tm/tm.c

@@ -2899,6 +2899,12 @@ static const char *rpc_t_uac_start_doc[2] = {
 	0
 };
 
+static const char *rpc_t_uac_start_hex_doc[2] = {
+	"starts a tm uac using  a list of string parameters: method, ruri, "
+	"dst_uri, send_sock, headers (CRLF separated) and hexa body (optional)",
+	0
+};
+
 static const char *rpc_t_uac_wait_doc[2] = {
 	"starts a tm uac and waits for the final reply, using a list of string "
 	"parameters: method, ruri, dst_uri send_sock, headers (CRLF separated)"
@@ -2906,6 +2912,13 @@ static const char *rpc_t_uac_wait_doc[2] = {
 	0
 };
 
+static const char *rpc_t_uac_wait_hex_doc[2] = {
+	"starts a tm uac and waits for the final reply, using a list of string "
+	"parameters: method, ruri, dst_uri send_sock, headers (CRLF separated)"
+	" and hexa body (optional)",
+	0
+};
+
 static const char *rpc_t_uac_wait_block_doc[2] = {
 	"starts a tm uac and waits for the final reply in blocking mode, using a"
 	" list of string parameters: method, ruri, dst_uri send_sock, headers"
@@ -2913,6 +2926,13 @@ static const char *rpc_t_uac_wait_block_doc[2] = {
 	0
 };
 
+static const char *rpc_t_uac_wait_block_hex_doc[2] = {
+	"starts a tm uac and waits for the final reply in blocking mode, using a"
+	" list of string parameters: method, ruri, dst_uri send_sock, headers"
+	" (CRLF separated) and hexa body (optional)",
+	0
+};
+
 static const char *rpc_t_uac_start_noack_doc[2] = {
 	"starts a tm uac using a list of string parameters: method, ruri, "
 	"dst_uri, send_sock, headers (CRLF separated) and body (optional), "
@@ -2920,6 +2940,13 @@ static const char *rpc_t_uac_start_noack_doc[2] = {
 	0
 };
 
+static const char *rpc_t_uac_start_noack_hex_doc[2] = {
+	"starts a tm uac using a list of string parameters: method, ruri, "
+	"dst_uri, send_sock, headers (CRLF separated) and hexa body (optional), "
+	"without sending ACK for local INVITE",
+	0
+};
+
 static const char *rpc_t_uac_wait_noack_doc[2] = {
 	"starts a tm uac and waits for the final reply, using a list of string "
 	"parameters: method, ruri, dst_uri send_sock, headers (CRLF separated)"
@@ -2927,6 +2954,13 @@ static const char *rpc_t_uac_wait_noack_doc[2] = {
 	0
 };
 
+static const char *rpc_t_uac_wait_noack_hex_doc[2] = {
+	"starts a tm uac and waits for the final reply, using a list of string "
+	"parameters: method, ruri, dst_uri send_sock, headers (CRLF separated)"
+	" and hexa body (optional), without sending ACK for local INVITE",
+	0
+};
+
 static const char *rpc_t_uac_wait_block_noack_doc[2] = {
 	"starts a tm uac and waits for the final reply in blocking mode, using a"
 	" list of string parameters: method, ruri, dst_uri send_sock, headers "
@@ -2934,6 +2968,13 @@ static const char *rpc_t_uac_wait_block_noack_doc[2] = {
 	0
 };
 
+static const char *rpc_t_uac_wait_block_noack_hex_doc[2] = {
+	"starts a tm uac and waits for the final reply in blocking mode, using a"
+	" list of string parameters: method, ruri, dst_uri send_sock, headers "
+	"(CRLF separated) and hexa body (optional), without sending ACK for local INVITE",
+	0
+};
+
 static const char *tm_rpc_list_doc[2] = {
 	"List transactions.",
 	0
@@ -2955,15 +2996,30 @@ static rpc_export_t tm_rpc[] = {
 	{"tm.reply_callid", rpc_reply_callid, rpc_reply_callid_doc, 0},
 	{"tm.stats", tm_rpc_stats, tm_rpc_stats_doc, 0},
 	{"tm.hash_stats", tm_rpc_hash_stats, tm_rpc_hash_stats_doc, 0},
-	{"tm.t_uac_start", rpc_t_uac_start, rpc_t_uac_start_doc, 0},
-	{"tm.t_uac_wait", rpc_t_uac_wait, rpc_t_uac_wait_doc, RET_ARRAY},
-	{"tm.t_uac_wait_block", rpc_t_uac_wait_block, rpc_t_uac_wait_block_doc, 0},
+	{"tm.t_uac_start", rpc_t_uac_start,
+		rpc_t_uac_start_doc, 0},
+	{"tm.t_uac_start_hex", rpc_t_uac_start_hex,
+		rpc_t_uac_start_hex_doc, 0},
+	{"tm.t_uac_wait", rpc_t_uac_wait,
+		rpc_t_uac_wait_doc, RET_ARRAY},
+	{"tm.t_uac_wait_hex", rpc_t_uac_wait_hex,
+		rpc_t_uac_wait_hex_doc, RET_ARRAY},
+	{"tm.t_uac_wait_block", rpc_t_uac_wait_block,
+		rpc_t_uac_wait_block_doc, 0},
+	{"tm.t_uac_wait_block_hex", rpc_t_uac_wait_block_hex,
+		rpc_t_uac_wait_block_hex_doc, 0},
 	{"tm.t_uac_start_noack", rpc_t_uac_start_noack,
 		rpc_t_uac_start_noack_doc, 0},
+	{"tm.t_uac_start_noack_hex", rpc_t_uac_start_noack_hex,
+		rpc_t_uac_start_noack_hex_doc, 0},
 	{"tm.t_uac_wait_noack", rpc_t_uac_wait_noack,
 		rpc_t_uac_wait_noack_doc, RET_ARRAY},
+	{"tm.t_uac_wait_noack_hex", rpc_t_uac_wait_noack_hex,
+		rpc_t_uac_wait_noack_hex_doc, RET_ARRAY},
 	{"tm.t_uac_wait_block_noack", rpc_t_uac_wait_block_noack,
 		rpc_t_uac_wait_block_noack_doc, 0},
+	{"tm.t_uac_wait_block_noack_hex", rpc_t_uac_wait_block_noack_hex,
+		rpc_t_uac_wait_block_noack_hex_doc, 0},
 	{"tm.list", tm_rpc_list, tm_rpc_list_doc, RET_ARRAY},
 	{"tm.clean", tm_rpc_clean, tm_rpc_clean_doc, 0},
 	{0, 0, 0, 0}