Browse code

posops: added $pos(key) variable

- get attributes related to posops

Daniel-Constantin Mierla authored on 18/09/2021 13:24:39
Showing 1 changed files
... ...
@@ -49,12 +49,22 @@ static int w_posops_pos_body_end(sip_msg_t* msg, char* p1, char* p2);
49 49
 typedef struct posops_data {
50 50
 	int ret;
51 51
 	int idx;
52
-} pospos_data_t;
52
+} posops_data_t;
53
+
54
+static int pv_posops_get_pos(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
55
+static int pv_posops_parse_pos_name(pv_spec_t *sp, str *in);
56
+
57
+static pv_export_t mod_pvs[] = {
58
+	{ {"pos", (sizeof("pos")-1)}, PVT_OTHER, pv_posops_get_pos, 0,
59
+		pv_posops_parse_pos_name, 0, 0, 0 },
60
+
61
+	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
62
+};
53 63
 
54 64
 /**
55 65
  *
56 66
  */
57
-static pospos_data_t _pospos_data = {0};
67
+static posops_data_t _posops_data = {0};
58 68
 
59 69
 /* clang-format off */
60 70
 static cmd_export_t cmds[]={
... ...
@@ -82,16 +92,16 @@ static param_export_t params[]={
82 92
 };
83 93
 
84 94
 struct module_exports exports = {
85
-	"posops",
95
+	"posops",        /* module name */
86 96
 	DEFAULT_DLFLAGS, /* dlopen flags */
87
-	cmds,
88
-	params,
89
-	0,              /* exported RPC methods */
90
-	0,              /* exported pseudo-variables */
91
-	0,              /* response function */
92
-	mod_init,       /* module initialization function */
93
-	child_init,     /* per child init function */
94
-	mod_destroy    	/* destroy function */
97
+	cmds,            /* cmd (cfg function) exports */
98
+	params,          /* param exports */
99
+	0,               /* exported RPC methods */
100
+	mod_pvs,         /* exported pseudo-variables */
101
+	0,               /* response function */
102
+	mod_init,        /* module initialization function */
103
+	child_init,      /* per child init function */
104
+	mod_destroy      /* destroy function */
95 105
 };
96 106
 /* clang-format on */
97 107
 
... ...
@@ -292,18 +302,16 @@ static int w_posops_pos_rm(sip_msg_t* msg, char* p1idx, char* p2len)
292 302
  */
293 303
 static int ki_posops_pos_headers_start(sip_msg_t* msg)
294 304
 {
295
-	int ret = 0;
296
-
297
-	memset(&_pospos_data, 0, sizeof(pospos_data_t));
305
+	memset(&_posops_data, 0, sizeof(posops_data_t));
298 306
 	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
299 307
 		LM_ERR("failed to parse headers\n");
300 308
 		return -1;
301 309
 	}
302 310
 
303
-	ret = msg->first_line.len;
304
-	_pospos_data.idx = ret;
311
+	_posops_data.idx = msg->first_line.len;
312
+	_posops_data.ret = (_posops_data.idx==0)?-255:_posops_data.idx;
305 313
 
306
-	return (ret==0)?-255:ret;
314
+	return _posops_data.ret;
307 315
 }
308 316
 
309 317
 /**
... ...
@@ -319,18 +327,16 @@ static int w_posops_pos_headers_start(sip_msg_t* msg, char* p1, char* p2)
319 327
  */
320 328
 static int ki_posops_pos_headers_end(sip_msg_t* msg)
321 329
 {
322
-	int ret = 0;
323
-
324
-	memset(&_pospos_data, 0, sizeof(pospos_data_t));
330
+	memset(&_posops_data, 0, sizeof(posops_data_t));
325 331
 	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
326 332
 		LM_ERR("failed to parse headers\n");
327 333
 		return -1;
328 334
 	}
329 335
 
330
-	ret = msg->unparsed - msg->buf;
331
-	_pospos_data.idx = ret;
336
+	_posops_data.idx = msg->unparsed - msg->buf;
337
+	_posops_data.ret = (_posops_data.idx==0)?-255:_posops_data.idx;
332 338
 
333
-	return (ret==0)?-255:ret;
339
+	return _posops_data.ret;
334 340
 }
335 341
 
336 342
 /**
... ...
@@ -346,10 +352,9 @@ static int w_posops_pos_headers_end(sip_msg_t* msg, char* p1, char* p2)
346 352
  */
347 353
 static int ki_posops_pos_body_start(sip_msg_t* msg)
348 354
 {
349
-	int ret = 0;
350 355
 	char *body = 0;
351 356
 
352
-	memset(&_pospos_data, 0, sizeof(pospos_data_t));
357
+	memset(&_posops_data, 0, sizeof(posops_data_t));
353 358
 	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
354 359
 		LM_ERR("failed to parse headers\n");
355 360
 		return -1;
... ...
@@ -360,10 +365,11 @@ static int ki_posops_pos_body_start(sip_msg_t* msg)
360 365
 		LM_DBG("no body\n");
361 366
 		return -1;
362 367
 	}
363
-	ret = body - msg->buf;
364
-	_pospos_data.idx = ret;
368
+	_posops_data.idx = body - msg->buf;
369
+
370
+	_posops_data.ret = (_posops_data.idx==0)?-255:_posops_data.idx;
365 371
 
366
-	return (ret==0)?-255:ret;
372
+	return _posops_data.ret;
367 373
 }
368 374
 
369 375
 /**
... ...
@@ -379,9 +385,7 @@ static int w_posops_pos_body_start(sip_msg_t* msg, char* p1, char* p2)
379 385
  */
380 386
 static int ki_posops_pos_body_end(sip_msg_t* msg)
381 387
 {
382
-	int ret = 0;
383
-
384
-	memset(&_pospos_data, 0, sizeof(pospos_data_t));
388
+	memset(&_posops_data, 0, sizeof(posops_data_t));
385 389
 	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
386 390
 		LM_ERR("failed to parse headers\n");
387 391
 		return -1;
... ...
@@ -392,10 +396,10 @@ static int ki_posops_pos_body_end(sip_msg_t* msg)
392 396
 		return -1;
393 397
 	}
394 398
 
395
-	ret = msg->len;
396
-	_pospos_data.idx = ret;
399
+	_posops_data.idx = msg->len;
400
+	_posops_data.ret = (_posops_data.idx==0)?-255:_posops_data.idx;
397 401
 
398
-	return (ret==0)?-255:ret;
402
+	return _posops_data.ret;
399 403
 }
400 404
 
401 405
 /**
... ...
@@ -406,6 +410,47 @@ static int w_posops_pos_body_end(sip_msg_t* msg, char* p1, char* p2)
406 410
 	return  ki_posops_pos_body_end(msg);
407 411
 }
408 412
 
413
+/**
414
+ *
415
+ */
416
+static int pv_posops_get_pos(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
417
+{
418
+	switch(param->pvn.u.isname.name.n) {
419
+		case 0: /* idx */
420
+			return pv_get_sintval(msg, param, res, _posops_data.idx);
421
+		case 1: /* ret */
422
+			return pv_get_sintval(msg, param, res, _posops_data.ret);
423
+	}
424
+	return pv_get_null(msg, param, res);
425
+}
426
+
427
+/**
428
+ *
429
+ */
430
+static int pv_posops_parse_pos_name(pv_spec_t *sp, str *in)
431
+{
432
+	switch(in->len) {
433
+		case 3:
434
+			if(strncmp(in->s, "idx", 3)==0)
435
+				sp->pvp.pvn.u.isname.name.n = 0;
436
+			else if(strncmp(in->s, "ret", 3)==0)
437
+				sp->pvp.pvn.u.isname.name.n = 1;
438
+			else goto error;
439
+		break;
440
+
441
+		default:
442
+			goto error;
443
+	}
444
+	sp->pvp.pvn.type = PV_NAME_INTSTR;
445
+	sp->pvp.pvn.u.isname.type = 0;
446
+
447
+	return 0;
448
+
449
+error:
450
+	LM_ERR("unknown pv pos key: %.*s\n", in->len, in->s);
451
+	return -1;
452
+}
453
+
409 454
 
410 455
 /**
411 456
  *