Browse code

registrar: kemi functions to get $ulc(...) attributes

Daniel-Constantin Mierla authored on 12/05/2022 09:21:46
Showing 3 changed files
... ...
@@ -1005,6 +1005,16 @@ static sr_kemi_t sr_kemi_registrar_exports[] = {
1005 1005
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1006 1006
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1007 1007
 	},
1008
+	{ str_init("registrar"), str_init("ulc_rget"),
1009
+		SR_KEMIP_XVAL, ki_reg_ulc_rget,
1010
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1011
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1012
+	},
1013
+	{ str_init("registrar"), str_init("ulc_cget"),
1014
+		SR_KEMIP_XVAL, ki_reg_ulc_cget,
1015
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1016
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1017
+	},
1008 1018
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1009 1019
 };
1010 1020
 /* clang-format on */
... ...
@@ -430,6 +430,113 @@ error:
430 430
 	return -1;
431 431
 }
432 432
 
433
+/**
434
+ *
435
+ */
436
+static sr_kemi_xval_t _sr_kemi_reg_ulc_xval = {0};
437
+
438
+sr_kemi_xval_t* ki_reg_ulc_rget(sip_msg_t* msg, str* rid, str* attr)
439
+{
440
+	regpv_profile_t *rpp = NULL;
441
+
442
+	memset(&_sr_kemi_reg_ulc_xval, 0, sizeof(sr_kemi_xval_t));
443
+	if(rid==NULL || rid->s==NULL || attr==NULL || attr->s==NULL) {
444
+		LM_WARN("invalid parameters - return value 0\n");
445
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
446
+		_sr_kemi_reg_ulc_xval.v.n = 0;
447
+		return &_sr_kemi_reg_ulc_xval;
448
+	}
449
+
450
+	rpp = regpv_get_profile(rid);
451
+	if(rpp==0) {
452
+		LM_WARN("result [%.*s] is not defined - return value 0\n",
453
+				rid->len, rid->s);
454
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
455
+		_sr_kemi_reg_ulc_xval.v.n = 0;
456
+		return &_sr_kemi_reg_ulc_xval;
457
+	}
458
+
459
+	if(attr->len==5 && strncmp(attr->s, "count", 5)==0) {
460
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
461
+		_sr_kemi_reg_ulc_xval.v.n = rpp->nrc;
462
+		return &_sr_kemi_reg_ulc_xval;
463
+	} else if(attr->len==3 && strncmp(attr->s, "aor", 3)==0) {
464
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_STR;
465
+		_sr_kemi_reg_ulc_xval.v.s = rpp->aor;
466
+		return &_sr_kemi_reg_ulc_xval;
467
+	}
468
+
469
+	LM_WARN("attribute [%.*s] is not defined - return value 0\n",
470
+			attr->len, attr->s);
471
+	_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
472
+	_sr_kemi_reg_ulc_xval.v.n = 0;
473
+	return &_sr_kemi_reg_ulc_xval;
474
+}
475
+
476
+sr_kemi_xval_t* ki_reg_ulc_cget(sip_msg_t* msg, str* rid, str* attr, int idx)
477
+{
478
+	regpv_profile_t *rpp = NULL;
479
+	ucontact_t *c = NULL;
480
+	int i = 0;
481
+
482
+
483
+	memset(&_sr_kemi_reg_ulc_xval, 0, sizeof(sr_kemi_xval_t));
484
+	if(rid==NULL || rid->s==NULL || attr==NULL || attr->s==NULL) {
485
+		LM_WARN("invalid parameters - return value 0\n");
486
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
487
+		_sr_kemi_reg_ulc_xval.v.n = 0;
488
+		return &_sr_kemi_reg_ulc_xval;
489
+	}
490
+
491
+	rpp = regpv_get_profile(rid);
492
+	if(rpp==0) {
493
+		LM_WARN("result [%.*s] is not defined - return value 0\n",
494
+				rid->len, rid->s);
495
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
496
+		_sr_kemi_reg_ulc_xval.v.n = 0;
497
+		return &_sr_kemi_reg_ulc_xval;
498
+	}
499
+
500
+	/* work only with positive indexes by now */
501
+	if(idx<0) {
502
+		idx = 0;
503
+	}
504
+	/* get contact by index */
505
+	i = 0;
506
+	for(c = rpp->contacts; c!=NULL; c = c->next) {
507
+		if(i == idx) {
508
+			break;
509
+		}
510
+		i++;
511
+	}
512
+	if(c == NULL) {
513
+		LM_WARN("contact at index [%d] is not found - return value 0\n", idx);
514
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
515
+		_sr_kemi_reg_ulc_xval.v.n = 0;
516
+		return &_sr_kemi_reg_ulc_xval;
517
+	}
518
+
519
+	if(attr->len==4 && strncmp(attr->s, "addr", 4)==0) {
520
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_STR;
521
+		_sr_kemi_reg_ulc_xval.v.s = c->c;
522
+		return &_sr_kemi_reg_ulc_xval;
523
+	} else if(attr->len==6 && strncmp(attr->s, "socket", 6)==0) {
524
+		if(c->sock==NULL) {
525
+			sr_kemi_xval_null(&_sr_kemi_reg_ulc_xval, SR_KEMI_XVAL_NULL_EMPTY);
526
+			return &_sr_kemi_reg_ulc_xval;
527
+		}
528
+		_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_STR;
529
+		_sr_kemi_reg_ulc_xval.v.s = c->sock->sock_str;
530
+		return &_sr_kemi_reg_ulc_xval;
531
+	}
532
+
533
+	LM_WARN("attribute [%.*s] is not defined - return value 0\n",
534
+			attr->len, attr->s);
535
+	_sr_kemi_reg_ulc_xval.vtype = SR_KEMIP_INT;
536
+	_sr_kemi_reg_ulc_xval.v.n = 0;
537
+	return &_sr_kemi_reg_ulc_xval;
538
+}
539
+
433 540
 int pv_fetch_contacts_helper(sip_msg_t* msg, udomain_t* dt, str* uri,
434 541
 		str* profile)
435 542
 {
... ...
@@ -31,6 +31,7 @@
31 31
 #define _REGPV_H_
32 32
 
33 33
 #include "../../core/pvar.h"
34
+#include "../../core/kemi.h"
34 35
 #include "../../modules/usrloc/usrloc.h"
35 36
 
36 37
 int pv_get_ulc(struct sip_msg *msg,  pv_param_t *param,
... ...
@@ -49,4 +50,7 @@ void regpv_free_profiles(void);
49 50
 
50 51
 void reg_ul_expired_contact(ucontact_t* c, int type, void* param);
51 52
 
53
+sr_kemi_xval_t* ki_reg_ulc_rget(sip_msg_t* msg, str* rid, str* attr);
54
+sr_kemi_xval_t* ki_reg_ulc_cget(sip_msg_t* msg, str* rid, str* attr, int idx);
55
+
52 56
 #endif