Browse Source

core: msg translator - function to parse via branch value

Daniel-Constantin Mierla 3 days ago
parent
commit
712d593406
2 changed files with 78 additions and 0 deletions
  1. 67 0
      src/core/msg_translator.c
  2. 11 0
      src/core/msg_translator.h

+ 67 - 0
src/core/msg_translator.c

@@ -102,6 +102,8 @@
 #include "cfg/cfg.h"
 #include "parser/parse_to.h"
 #include "parser/parse_param.h"
+#include "parser/parser_f.h"
+#include "hash_func.h"
 #include "forward.h"
 #include "str_list.h"
 #include "pvapi.h"
@@ -2843,6 +2845,71 @@ error00:
 }
 
 
+int via_branch_parser(str *vbranch, viabranch_t *vb)
+{
+	char *p;
+	char *n;
+	int scan_space;
+
+	/* we do RFC 3261 tid matching and want to see first if there is
+	 * magic cookie in branch */
+	if(vbranch->len <= MCOOKIE_LEN)
+		goto nomatch;
+	if(memcmp(vbranch->s, MCOOKIE, MCOOKIE_LEN) != 0)
+		goto nomatch;
+
+	vb->cookie.s = vbranch->s;
+	vb->cookie.len = MCOOKIE_LEN;
+
+	p = vbranch->s + MCOOKIE_LEN;
+	scan_space = vbranch->len - MCOOKIE_LEN;
+
+	/* hash_id */
+	n = eat_token2_end(p, p + scan_space, BRANCH_SEPARATOR);
+	vb->shashidx.len = n - p;
+	scan_space -= vb->shashidx.len;
+	if(!vb->shashidx.len || scan_space < 2 || *n != BRANCH_SEPARATOR)
+		goto nomatch;
+	vb->shashidx.s = p;
+	p = n + 1;
+	scan_space--;
+
+	/* md5 value */
+	n = eat_token2_end(p, p + scan_space, BRANCH_SEPARATOR);
+	vb->transid.len = n - p;
+	scan_space -= vb->transid.len;
+	if(n == p || scan_space < 2 || *n != BRANCH_SEPARATOR)
+		goto nomatch;
+	vb->transid.s = p;
+	p = n + 1;
+	scan_space--;
+
+	/* branch id  -  should exceed the scan_space */
+	n = eat_token_end(p, p + scan_space);
+	vb->sbranchidx.len = n - p;
+	if(!vb->sbranchidx.len)
+		goto nomatch;
+	vb->sbranchidx.s = p;
+
+	/* sanity check */
+	if(unlikely(reverse_hex2int(vb->shashidx.s, vb->shashidx.len, &vb->vhashidx)
+						< 0
+				|| vb->vhashidx >= TABLE_ENTRIES
+				|| reverse_hex2int(vb->sbranchidx.s, vb->sbranchidx.len,
+						   &vb->vbranchidx)
+						   < 0
+				|| vb->vbranchidx >= sr_dst_max_branches
+				|| vb->transid.len != MD5_LEN)) {
+		LM_DBG("poor reply ids - hashidx %d branchidx %d transid-len %d/%d\n",
+				vb->vhashidx, vb->vbranchidx, vb->transid.len, MD5_LEN);
+		goto nomatch;
+	}
+	return 0;
+
+nomatch:
+	return -1;
+}
+
 /* return number of chars printed or 0 if space exceeded;
    assumes buffer size of at least MAX_BRANCH_PARAM_LEN
  */

+ 11 - 0
src/core/msg_translator.h

@@ -70,6 +70,15 @@ struct hostport
 	str *port;
 };
 
+typedef struct viabranch
+{
+	str cookie;
+	str shashidx;
+	unsigned int vhashidx;
+	str transid;
+	str sbranchidx;
+	unsigned int vbranchidx;
+} viabranch_t;
 
 #define set_hostport(hp, msg)                                              \
 	do {                                                                   \
@@ -118,6 +127,8 @@ char *via_builder(unsigned int *len, sip_msg_t *msg,
 char *create_via_hf(unsigned int *len, struct sip_msg *msg,
 		struct dest_info *send_info /* where to send the reply */, str *branch);
 
+int via_branch_parser(str *vbranch, viabranch_t *vb);
+
 int branch_builder(unsigned int hash_index,
 		/* only either parameter useful */
 		unsigned int label, char *char_v, str *xval, int branch,