Browse code

siputils: added optional mode parameter to is_first_hop()

- if set to 1, skip checking for loops
- exported to kemi as is_first_hop_mode(int mode)

Daniel-Constantin Mierla authored on 11/05/2021 13:16:36
Showing 3 changed files
... ...
@@ -140,12 +140,12 @@ int w_is_supported(sip_msg_t *msg, char *_option, char *p2)
140 140
 }
141 141
 
142 142
 
143
-int is_first_hop(sip_msg_t *msg)
143
+int is_first_hop_mode(sip_msg_t *msg, int mode)
144 144
 {
145 145
 	int ret;
146 146
 	rr_t* r = NULL;
147 147
 	sip_uri_t puri;
148
-	struct ip_addr *ip;
148
+	struct ip_addr *ip = NULL;
149 149
 
150 150
 	if(msg==NULL)
151 151
 		return -1;
... ...
@@ -180,10 +180,12 @@ int is_first_hop(sip_msg_t *msg)
180 180
 			LM_DBG("failed to parse uri in first record-route header\n");
181 181
 			return -1;
182 182
 		}
183
-		if (((ip = str2ip(&(puri.host))) == NULL)
184
-				&& ((ip = str2ip6(&(puri.host))) == NULL)) {
185
-			LM_DBG("uri host is not an ip address\n");
186
-			return -1;
183
+		if(mode==0) {
184
+			if (((ip = str2ip(&(puri.host))) == NULL)
185
+					&& ((ip = str2ip6(&(puri.host))) == NULL)) {
186
+				LM_DBG("uri host is not an ip address\n");
187
+				return -1;
188
+			}
187 189
 		}
188 190
 		ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
189 191
 				(puri.transport_val.s)?puri.proto:0);
... ...
@@ -191,13 +193,15 @@ int is_first_hop(sip_msg_t *msg)
191 193
 			LM_DBG("top record route uri is not myself\n");
192 194
 			return -1;
193 195
 		}
194
-		if (ip_addr_cmp(ip, &(msg->rcv.src_ip))
195
-				&& ((msg->rcv.src_port == puri.port_no)
196
-					|| ((puri.port.len == 0) && (msg->rcv.src_port == 5060)))
197
-				&& (puri.proto==msg->rcv.proto
198
-					|| (puri.proto==0 && msg->rcv.proto==PROTO_UDP)) ) {
199
-			LM_DBG("source address matches top record route uri - loop\n");
200
-			return -1;
196
+		if(mode==0) {
197
+			if (ip_addr_cmp(ip, &(msg->rcv.src_ip))
198
+					&& ((msg->rcv.src_port == puri.port_no)
199
+						|| ((puri.port.len == 0) && (msg->rcv.src_port == 5060)))
200
+					&& (puri.proto==msg->rcv.proto
201
+						|| (puri.proto==0 && msg->rcv.proto==PROTO_UDP)) ) {
202
+				LM_DBG("source address matches top record route uri - loop\n");
203
+				return -1;
204
+			}
201 205
 		}
202 206
 		/* todo - check spirals */
203 207
 		return 1;
... ...
@@ -206,6 +210,23 @@ int is_first_hop(sip_msg_t *msg)
206 210
 	}
207 211
 }
208 212
 
213
+int w_is_first_hop_mode(sip_msg_t *msg, char *p1mode, char *p2)
214
+{
215
+	int mode = 0;
216
+
217
+	if(fixup_get_ivalue(msg, (gparam_t*)p1mode, &mode)<0) {
218
+		LM_ERR("failed to get mode parameter\n");
219
+		return -1;
220
+	}
221
+
222
+	return is_first_hop_mode(msg, mode);
223
+}
224
+
225
+int is_first_hop(sip_msg_t *msg)
226
+{
227
+	return is_first_hop_mode(msg, 0);
228
+}
229
+
209 230
 int w_is_first_hop(sip_msg_t *msg, char *p1, char *p2)
210 231
 {
211 232
 	return is_first_hop(msg);
... ...
@@ -40,5 +40,7 @@ int w_is_gruu(sip_msg_t *msg, char *uri1, char *p2);
40 40
 int w_is_supported(sip_msg_t *msg, char *_option, char *p2);
41 41
 int w_is_first_hop(sip_msg_t *msg, char *p1, char *p2);
42 42
 int is_first_hop(sip_msg_t *msg);
43
+int w_is_first_hop_mode(sip_msg_t *msg, char *p1mode, char *p2);
44
+int is_first_hop_mode(sip_msg_t *msg, int mode);
43 45
 
44 46
 #endif
... ...
@@ -185,6 +185,8 @@ static cmd_export_t cmds[]={
185 185
 		0, ANY_ROUTE},
186 186
 	{"is_first_hop",  (cmd_function)w_is_first_hop,          0, 0,
187 187
 		0, ANY_ROUTE},
188
+	{"is_first_hop",  (cmd_function)w_is_first_hop,          1, fixup_igp_null,
189
+		fixup_free_igp_null, ANY_ROUTE},
188 190
 	{"is_tel_number", (cmd_function)is_tel_number,           1, fixup_spve_null,
189 191
 		0, ANY_ROUTE},
190 192
 	{"is_numeric", (cmd_function)is_numeric,                 1, fixup_spve_null,
... ...
@@ -571,6 +573,11 @@ static sr_kemi_t sr_kemi_siputils_exports[] = {
571 573
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
572 574
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
573 575
 	},
576
+	{ str_init("siputils"), str_init("is_first_hop_mode"),
577
+		SR_KEMIP_INT, is_first_hop_mode,
578
+		{ SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
579
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
580
+	},
574 581
 	{ str_init("siputils"), str_init("is_uri"),
575 582
 		SR_KEMIP_INT, ki_is_uri,
576 583
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,