Browse code

pv: new transformation class - urialias

- encode/decode uri alias values (addr~port~proto)

Daniel-Constantin Mierla authored on 30/03/2021 09:29:26
Showing 3 changed files
... ...
@@ -59,6 +59,8 @@ static tr_export_t mod_trans[] = {
59 59
 		tr_parse_tobody },
60 60
 	{ {"line", sizeof("line")-1}, /* line class */
61 61
 		tr_parse_line },
62
+	{ {"urialias", sizeof("urialias")-1}, /* uri alias class */
63
+		tr_parse_urialias },
62 64
 
63 65
 	{ { 0, 0 }, 0 }
64 66
 };
... ...
@@ -2327,6 +2327,59 @@ done:
2327 2327
 	return 0;
2328 2328
 }
2329 2329
 
2330
+/*!
2331
+ * \brief Evaluate urialias transformations
2332
+ * \param msg SIP message
2333
+ * \param tp transformation
2334
+ * \param subtype transformation type
2335
+ * \param val pseudo-variable
2336
+ * \return 0 on success, -1 on error
2337
+ */
2338
+int tr_eval_urialias(struct sip_msg *msg, tr_param_t *tp, int subtype,
2339
+		pv_value_t *val)
2340
+{
2341
+	str sv;
2342
+
2343
+	if(val==NULL || (!(val->flags&PV_VAL_STR)) || val->rs.len<=0)
2344
+		return -1;
2345
+
2346
+	switch(subtype)
2347
+	{
2348
+		case TR_URIALIAS_ENCODE:
2349
+			tr_set_crt_buffer();
2350
+			sv.s = _tr_buffer;
2351
+			sv.len = TR_BUFFER_SIZE;
2352
+			if(ksr_uri_alias_encode(&val->rs, &sv)<0) {
2353
+				LM_WARN("error converting uri to alias [%.*s]\n",
2354
+						val->rs.len, val->rs.s);
2355
+				val->rs = _tr_empty;
2356
+				break;
2357
+			}
2358
+			val->rs = sv;
2359
+			break;
2360
+		case TR_URIALIAS_DECODE:
2361
+			tr_set_crt_buffer();
2362
+			sv.s = _tr_buffer;
2363
+			sv.len = TR_BUFFER_SIZE;
2364
+			if(ksr_uri_alias_decode(&val->rs, &sv)<0) {
2365
+				LM_WARN("error converting uri to alias [%.*s]\n",
2366
+						val->rs.len, val->rs.s);
2367
+				val->rs = _tr_empty;
2368
+				break;
2369
+			}
2370
+			val->rs = sv;
2371
+			break;
2372
+
2373
+		default:
2374
+			LM_ERR("unknown subtype %d\n",
2375
+					subtype);
2376
+			return -1;
2377
+	}
2378
+
2379
+	val->flags = PV_VAL_STR;
2380
+	return 0;
2381
+}
2382
+
2330 2383
 
2331 2384
 #define _tr_parse_nparam(_p, _p0, _tp, _spec, _n, _sign, _in, _s) \
2332 2385
 	while(is_in_str(_p, _in) && (*_p==' ' || *_p=='\t' || *_p=='\n')) _p++; \
... ...
@@ -3446,3 +3499,52 @@ done:
3446 3499
 	t->name = name;
3447 3500
 	return p;
3448 3501
 }
3502
+
3503
+/*!
3504
+ * \brief Helper fuction to parse urialias transformation
3505
+ * \param in parsed string
3506
+ * \param t transformation
3507
+ * \return pointer to the end of the transformation in the string - '}', null on error
3508
+ */
3509
+char* tr_parse_urialias(str* in, trans_t *t)
3510
+{
3511
+	char *p;
3512
+	str name;
3513
+
3514
+
3515
+	if(in==NULL || t==NULL)
3516
+		return NULL;
3517
+
3518
+	p = in->s;
3519
+	name.s = in->s;
3520
+	t->type = TR_URIALIAS;
3521
+	t->trf = tr_eval_urialias;
3522
+
3523
+	/* find next token */
3524
+	while(is_in_str(p, in) && *p!=TR_PARAM_MARKER && *p!=TR_RBRACKET) p++;
3525
+	if(*p=='\0') {
3526
+		LM_ERR("invalid transformation: %.*s\n",
3527
+				in->len, in->s);
3528
+		goto error;
3529
+	}
3530
+	name.len = p - name.s;
3531
+	trim(&name);
3532
+
3533
+	if(name.len==6 && strncasecmp(name.s, "encode", 6)==0) {
3534
+		t->subtype = TR_URIALIAS_ENCODE;
3535
+		goto done;
3536
+	} else if(name.len==6 && strncasecmp(name.s, "decode", 6)==0) {
3537
+		t->subtype = TR_URIALIAS_DECODE;
3538
+		goto done;
3539
+	}
3540
+
3541
+
3542
+	LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s,
3543
+			name.len, name.s, name.len);
3544
+error:
3545
+	return NULL;
3546
+
3547
+done:
3548
+	t->name = name;
3549
+	return p;
3550
+}
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 
33 33
 enum _tr_type { TR_NONE=0, TR_STRING, TR_URI, TR_PARAMLIST, TR_NAMEADDR,
34
-				TR_TOBODY, TR_LINE };
34
+				TR_TOBODY, TR_LINE, TR_URIALIAS};
35 35
 enum _tr_s_subtype {
36 36
 	TR_S_NONE=0, TR_S_LEN, TR_S_INT, TR_S_MD5, TR_S_SHA256, TR_S_SHA384,
37 37
 	TR_S_SHA512, TR_S_SUBSTR, TR_S_SELECT, TR_S_ENCODEHEXA, TR_S_DECODEHEXA,
... ...
@@ -67,6 +67,9 @@ enum _tr_tobody_subtype {
67 67
 enum _tr_line_subtype {
68 68
 	TR_LINE_NONE=0, TR_LINE_COUNT, TR_LINE_AT, TR_LINE_SW
69 69
 };
70
+enum _tr_urialias_subtype {
71
+	TR_URIALIAS_NONE=0, TR_URIALIAS_ENCODE, TR_URIALIAS_DECODE
72
+};
70 73
 
71 74
 
72 75
 char* tr_parse_string(str *in, trans_t *tr);
... ...
@@ -75,6 +78,7 @@ char* tr_parse_paramlist(str *in, trans_t *tr);
75 78
 char* tr_parse_nameaddr(str *in, trans_t *tr);
76 79
 char* tr_parse_tobody(str* in, trans_t *t);
77 80
 char* tr_parse_line(str* in, trans_t *t);
81
+char* tr_parse_urialias(str* in, trans_t *t);
78 82
 
79 83
 int tr_init_buffers(void);
80 84