Browse code

registrar: implemented lookup filter mode 2

- use only contacts that have associated TCP/TLS/WSS connection active

Daniel-Constantin Mierla authored on 10/09/2021 11:20:12
Showing 2 changed files
... ...
@@ -1109,6 +1109,14 @@ end
1109 1109
 				rlf_bflags - e.g., $xavp(reg=>rlf_bflags).
1110 1110
 			</para>
1111 1111
 		</listitem>
1112
+		<listitem>
1113
+			<para>
1114
+				<emphasis>2</emphasis> - apply the active tcp connection filter
1115
+				- return only contact records that have the associated TCP/TLS/WSS
1116
+				connection active. UDP and SCTP contacts are not filtered, all
1117
+				are returned.
1118
+			</para>
1119
+		</listitem>
1112 1120
 	</itemizedlist>
1113 1121
 		<para>
1114 1122
 		<emphasis>
... ...
@@ -45,6 +45,8 @@
45 45
 #include "lookup.h"
46 46
 #include "config.h"
47 47
 
48
+#define REG_LOOKUP_FILTER_BFLAG        (1<<0)
49
+#define REG_LOOKUP_FILTER_TCPCON       (1<<1)
48 50
 
49 51
 extern int reg_lookup_filter_mode;
50 52
 
... ...
@@ -60,12 +62,13 @@ static void reg_lookup_filter_init(void)
60 62
 	str filter_bflags = str_init("rlf_bflags");
61 63
 	sr_xavp_t *vavp = NULL;
62 64
 
63
-	if(reg_lookup_filter_mode==0 || reg_xavp_cfg.s==NULL) {
65
+	if(reg_lookup_filter_mode==0) {
64 66
 		return;
65 67
 	}
66 68
 	memset(&_reg_lookup_filter, 0, sizeof(reg_lookup_filter_t));
67 69
 
68
-	if((reg_lookup_filter_mode & 1)
70
+	if((reg_lookup_filter_mode & REG_LOOKUP_FILTER_BFLAG)
71
+			&& (reg_xavp_cfg.s != NULL && reg_xavp_cfg.len > 0)
69 72
 			&& (vavp = xavp_get_child_with_ival(&reg_xavp_cfg,
70 73
 					&filter_bflags)) != NULL) {
71 74
 		if(vavp->val.v.i != 0) {
... ...
@@ -73,24 +76,45 @@ static void reg_lookup_filter_init(void)
73 76
 			_reg_lookup_filter.factive = 1;
74 77
 		}
75 78
 	}
79
+
80
+	if(reg_lookup_filter_mode & REG_LOOKUP_FILTER_TCPCON) {
81
+		_reg_lookup_filter.factive = 1;
82
+	}
76 83
 	return;
77 84
 }
78 85
 
79 86
 static int reg_lookup_filter_match(ucontact_t* ptr)
80 87
 {
81
-	if(reg_lookup_filter_mode==0 || reg_xavp_cfg.s==NULL) {
82
-		return 1;
83
-	}
84
-	if(_reg_lookup_filter.factive==0) {
88
+	tcp_connection_t *con = NULL;
89
+
90
+	if(reg_lookup_filter_mode==0 || _reg_lookup_filter.factive==0) {
85 91
 		return 1;
86 92
 	}
87
-	if(_reg_lookup_filter.bflags!=0) {
93
+
94
+	if((reg_lookup_filter_mode & REG_LOOKUP_FILTER_BFLAG)
95
+			&& _reg_lookup_filter.bflags!=0) {
88 96
 		if((_reg_lookup_filter.bflags & ptr->cflags)==0) {
89 97
 			return 0;
90 98
 		}
91 99
 	}
92
-	return 1;
93 100
 
101
+	if(reg_lookup_filter_mode & REG_LOOKUP_FILTER_TCPCON) {
102
+		if(ptr->tcpconn_id > 0) {
103
+			con = tcpconn_get(ptr->tcpconn_id, 0, 0, 0, 0);
104
+			if (unlikely(con == NULL)) {
105
+				LM_DBG("connection id %d does not exist\n", ptr->tcpconn_id);
106
+				return 0;
107
+			}
108
+			if (con->state != S_CONN_OK) {
109
+				LM_DBG("connection id %d not in state ok\n", ptr->tcpconn_id);
110
+				tcpconn_put(con);
111
+				return 0;
112
+			}
113
+			tcpconn_put(con);
114
+		}
115
+	}
116
+
117
+	return 1;
94 118
 }
95 119
 
96 120
 static int has_to_tag(struct sip_msg* msg)
... ...
@@ -485,7 +509,9 @@ int lookup_helper(struct sip_msg* _m, udomain_t* _d, str* _uri, int _mode)
485 509
 	if (!cfg_get(registrar, registrar_cfg, append_branches)) goto done;
486 510
 
487 511
 	for( ; ptr ; ptr = ptr->next ) {
488
-		if ((VALID_CONTACT(ptr, act_time) || cfg_get(registrar,registrar_cfg,use_expired_contacts)) && allowed_method(_m, ptr)
512
+		if ((VALID_CONTACT(ptr, act_time)
513
+					|| cfg_get(registrar,registrar_cfg,use_expired_contacts))
514
+				&& allowed_method(_m, ptr)
489 515
 				&& reg_lookup_filter_match(ptr)) {
490 516
 			path_dst.len = 0;
491 517
 			if(ptr->path.s && ptr->path.len) {
... ...
@@ -500,7 +526,8 @@ int lookup_helper(struct sip_msg* _m, udomain_t* _d, str* _uri, int _mode)
500 526
 						continue;
501 527
 					}
502 528
 					if (check_self(&(path_uri.host), 0, 0)) {
503
-						/* first hop in path vector is local - check for additional hops and if present, point to next one */
529
+						/* first hop in path vector is local
530
+						 * - check for additional hops and if present, point to next one */
504 531
 						if (path_str.len > (path_dst.len + 3)) {
505 532
 							path_str.s = path_str.s + path_dst.len + 3;
506 533
 							path_str.len = path_str.len - path_dst.len - 3;