Browse code

posops: functions to get the position of a string in message buffer

Daniel-Constantin Mierla authored on 21/09/2021 13:50:33
Showing 2 changed files
... ...
@@ -232,6 +232,61 @@ $var(pos) = pos_body_start();
232 232
 				<programlisting format="linespecific">
233 233
 ...
234 234
 $var(pos) = pos_body_end();
235
+...
236
+				</programlisting>
237
+			</example>
238
+		</section>
239
+		<section id="posops.f.pos_find_str">
240
+			<title>
241
+				<function moreinfo="none">pos_find_str(idx, val)</function>
242
+			</title>
243
+			<para>
244
+			Return the position of the val in message buffer starting at idx. In
245
+			case of not finding it or error, the return code is negative. If
246
+			val is at index 0, it returns the value specified by modparam idx0.
247
+			</para>
248
+			<para>
249
+			The idx can be an integer value or a variable holding an integer.
250
+			</para>
251
+			<para>
252
+			The val can be a static string or variables.
253
+			</para>
254
+			<para>
255
+			This function can be used from ANY_ROUTE.
256
+			</para>
257
+			<example>
258
+				<title><function>pos_find_str()</function> usage</title>
259
+				<programlisting format="linespecific">
260
+...
261
+$var(idx) = pos_find_str("100", "kamailio");
262
+...
263
+				</programlisting>
264
+			</example>
265
+		</section>
266
+		<section id="posops.f.pos_find_str">
267
+			<title>
268
+				<function moreinfo="none">pos_findi_str(idx, val)</function>
269
+			</title>
270
+			<para>
271
+			Return the position of the val (matching case insensitive) in message
272
+			buffer starting at idx. In case of not finding it or error, the return
273
+			code is negative. If val is at index 0, it returns the value specified
274
+			by modparam idx0.
275
+			</para>
276
+			<para>
277
+			The idx can be an integer value or a variable holding an integer.
278
+			</para>
279
+			<para>
280
+			The val can be a static string or variables.
281
+			</para>
282
+			<para>
283
+			This function can be used from ANY_ROUTE.
284
+			</para>
285
+			<example>
286
+				<title><function>pos_findi_str()</function> usage</title>
287
+				<programlisting format="linespecific">
288
+...
289
+$var(idx) = pos_findi_str("100", "kamailio");
235 290
 ...
236 291
 				</programlisting>
237 292
 			</example>
... ...
@@ -45,6 +45,8 @@ static int w_posops_pos_headers_start(sip_msg_t* msg, char* p1, char* p2);
45 45
 static int w_posops_pos_headers_end(sip_msg_t* msg, char* p1, char* p2);
46 46
 static int w_posops_pos_body_start(sip_msg_t* msg, char* p1, char* p2);
47 47
 static int w_posops_pos_body_end(sip_msg_t* msg, char* p1, char* p2);
48
+static int w_posops_pos_find_str(sip_msg_t* msg, char* p1idx, char* p2val);
49
+static int w_posops_pos_findi_str(sip_msg_t* msg, char* p1idx, char* p2val);
48 50
 
49 51
 typedef struct posops_data {
50 52
 	int ret;
... ...
@@ -84,6 +86,10 @@ static cmd_export_t cmds[]={
84 86
 		0, ANY_ROUTE},
85 87
 	{"pos_body_end", (cmd_function)w_posops_pos_body_end, 0, 0,
86 88
 		0, ANY_ROUTE},
89
+	{"pos_find_str", (cmd_function)w_posops_pos_find_str, 2, fixup_igp_spve,
90
+		fixup_free_igp_spve, ANY_ROUTE},
91
+	{"pos_findi_str", (cmd_function)w_posops_pos_findi_str, 2, fixup_igp_spve,
92
+		fixup_free_igp_spve, ANY_ROUTE},
87 93
 
88 94
 	{0, 0, 0, 0, 0, 0}
89 95
 };
... ...
@@ -413,6 +419,104 @@ static int w_posops_pos_body_end(sip_msg_t* msg, char* p1, char* p2)
413 419
 	return  ki_posops_pos_body_end(msg);
414 420
 }
415 421
 
422
+/**
423
+ *
424
+ */
425
+static int ki_posops_pos_find_str(sip_msg_t *msg, int idx, str *val)
426
+{
427
+	char *p;
428
+	str text;
429
+
430
+	if(idx<0 || val==NULL || val->s==NULL || val->len<=0) {
431
+		return -1;
432
+	}
433
+	if(idx > msg->len - val->len) {
434
+		return -1;
435
+	}
436
+
437
+	text.s = msg->buf + idx;
438
+	text.len = msg->len - idx;
439
+	p = str_search(&text, val);
440
+	if(p==NULL) {
441
+		return -1;
442
+	}
443
+
444
+	_posops_data.idx = (int)(p - msg->buf);
445
+	_posops_data.ret = (_posops_data.idx==0)?posops_idx0:_posops_data.idx;
446
+
447
+	return _posops_data.ret;
448
+}
449
+
450
+/**
451
+ *
452
+ */
453
+static int w_posops_pos_find_str(sip_msg_t* msg, char* p1idx, char* p2val)
454
+{
455
+	int idx = 0;
456
+	str val = STR_NULL;
457
+
458
+	if(fixup_get_ivalue(msg, (gparam_t*)p1idx, &idx)!=0) {
459
+		LM_ERR("unable to get idx parameter\n");
460
+		return -1;
461
+	}
462
+
463
+	if(fixup_get_svalue(msg, (gparam_t*)p2val, &val)!=0) {
464
+		LM_ERR("unable to get val parameter\n");
465
+		return -1;
466
+	}
467
+
468
+	return ki_posops_pos_find_str(msg, idx, &val);
469
+}
470
+
471
+/**
472
+ *
473
+ */
474
+static int ki_posops_pos_findi_str(sip_msg_t *msg, int idx, str *val)
475
+{
476
+	char *p;
477
+	str text;
478
+
479
+	if(idx<0 || val==NULL || val->s==NULL || val->len<=0) {
480
+		return -1;
481
+	}
482
+	if(idx > msg->len - val->len) {
483
+		return -1;
484
+	}
485
+
486
+	text.s = msg->buf + idx;
487
+	text.len = msg->len - idx;
488
+	p = str_casesearch(&text, val);
489
+	if(p==NULL) {
490
+		return -1;
491
+	}
492
+
493
+	_posops_data.idx = (int)(p - msg->buf);
494
+	_posops_data.ret = (_posops_data.idx==0)?posops_idx0:_posops_data.idx;
495
+
496
+	return _posops_data.ret;
497
+}
498
+
499
+/**
500
+ *
501
+ */
502
+static int w_posops_pos_findi_str(sip_msg_t* msg, char* p1idx, char* p2val)
503
+{
504
+	int idx = 0;
505
+	str val = STR_NULL;
506
+
507
+	if(fixup_get_ivalue(msg, (gparam_t*)p1idx, &idx)!=0) {
508
+		LM_ERR("unable to get idx parameter\n");
509
+		return -1;
510
+	}
511
+
512
+	if(fixup_get_svalue(msg, (gparam_t*)p2val, &val)!=0) {
513
+		LM_ERR("unable to get val parameter\n");
514
+		return -1;
515
+	}
516
+
517
+	return ki_posops_pos_findi_str(msg, idx, &val);
518
+}
519
+
416 520
 /**
417 521
  *
418 522
  */
... ...
@@ -495,6 +599,16 @@ static sr_kemi_t sr_kemi_posops_exports[] = {
495 599
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
496 600
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
497 601
 	},
602
+	{ str_init("posops"), str_init("pos_find_str"),
603
+		SR_KEMIP_INT, ki_posops_pos_find_str,
604
+		{ SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_NONE,
605
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
606
+	},
607
+	{ str_init("posops"), str_init("pos_findi_str"),
608
+		SR_KEMIP_INT, ki_posops_pos_findi_str,
609
+		{ SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_NONE,
610
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
611
+	},
498 612
 
499 613
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
500 614
 };