Browse code

Merge aa8d3ed4fe20efbd22db3b0b01a655789afa8818 into 6208d8a0e31fa03ca24bc2fd63093607d235dbbd

zenichev authored on 19/09/2021 07:14:03 • GitHub committed on 19/09/2021 07:14:03
Showing 1 changed files
... ...
@@ -921,6 +921,30 @@ static int comp_gws(const void *_g1, const void *_g2)
921 921
 	return memcmp(g1->ip_addr.u.addr, g2->ip_addr.u.addr, g1->ip_addr.len);
922 922
 }
923 923
 
924
+/*
925
+ * Compare gateways based on their IP address and port
926
+ */
927
+static int comp_gws_include_port(const void *_g1, const void *_g2)
928
+{
929
+	struct gw_info *g1 = (struct gw_info *)_g1;
930
+	struct gw_info *g2 = (struct gw_info *)_g2;
931
+
932
+	/* first address family comparison */
933
+	if(g1->ip_addr.af < g2->ip_addr.af)
934
+		return -1;
935
+	if(g1->ip_addr.af > g2->ip_addr.af)
936
+		return 1;
937
+	if(g1->ip_addr.len < g2->ip_addr.len)
938
+		return -1;
939
+	if(g1->ip_addr.len > g2->ip_addr.len)
940
+		return 1;
941
+
942
+	/* secondly ports comparison */
943
+	if(g1->port != g2->port)
944
+		return -1;
945
+
946
+	return memcmp(g1->ip_addr.u.addr, g2->ip_addr.u.addr, g1->ip_addr.len);
947
+}
924 948
 
925 949
 /* 
926 950
  * Insert gw info into index i or gws table
... ...
@@ -2997,15 +3021,21 @@ static int do_from_gw(struct sip_msg *_m, unsigned int lcr_id,
2997 3021
 		return -1;
2998 3022
 	}
2999 3023
 
3000
-	/* Search for gw ip address */
3001 3024
 	gw.ip_addr = *src_addr;
3002
-	res = (struct gw_info *)bsearch(&gw, &(gws[1]), gws[0].ip_addr.u.addr32[0],
3003
-			sizeof(struct gw_info), comp_gws);
3025
+	if (src_port != 0) {
3026
+		/* Search for gw based on its ip address and port */
3027
+		gw.port = src_port;
3028
+		res = (struct gw_info *)bsearch(&gw, &(gws[1]), gws[0].ip_addr.u.addr32[0],
3029
+				sizeof(struct gw_info), comp_gws_include_port);
3030
+	} else {
3031
+		/* Search for gw based on its ip address */
3032
+		res = (struct gw_info *)bsearch(&gw, &(gws[1]), gws[0].ip_addr.u.addr32[0],
3033
+				sizeof(struct gw_info), comp_gws);
3034
+	}
3004 3035
 
3005 3036
 	/* Store tag and flags and return result */
3006 3037
 	if((res != NULL)
3007
-					&& ((transport == PROTO_NONE) || (res->transport_code == transport))
3008
-					&& ((src_port == 0) || (res->port == src_port))) {
3038
+					&& ((transport == PROTO_NONE) || (res->transport_code == transport))) {
3009 3039
 		LM_DBG("request came from gw\n");
3010 3040
 		if(tag_avp_param) {
3011 3041
 			val.s.s = res->tag;