Browse code

core: use a list of static ip address variables for internal str convertion functions

- safer to work with up to 6 functions that use them internally

Daniel-Constantin Mierla authored on 03/03/2021 07:29:07
Showing 1 changed files
... ...
@@ -57,6 +57,20 @@
57 57
 #include "dns_cache.h"
58 58
 #endif
59 59
 
60
+#define KSR_IPADDR_LIST_SIZE 6
61
+static ip_addr_t _ksr_ipaddr_list[KSR_IPADDR_LIST_SIZE];
62
+static int _ksr_ipaddr_list_idx = 0;
63
+
64
+static ip_addr_t* get_next_ipaddr_buf(void)
65
+{
66
+	ip_addr_t *ipb;
67
+
68
+	ipb = &_ksr_ipaddr_list[_ksr_ipaddr_list_idx];
69
+	_ksr_ipaddr_list_idx = (_ksr_ipaddr_list_idx + 1) % KSR_IPADDR_LIST_SIZE;
70
+
71
+	return ipb;
72
+}
73
+
60 74
 /* counters framework */
61 75
 struct dns_counters_h dns_cnts_h;
62 76
 counter_def_t dns_cnt_defs[] =  {
... ...
@@ -1780,13 +1794,14 @@ error_dots:
1780 1794
    Warning: the result is a pointer to a statically allocated structure */
1781 1795
 ip_addr_t* str2ip(str* st)
1782 1796
 {
1783
-	static ip_addr_t ip;
1797
+	ip_addr_t *ipb;
1784 1798
 
1785
-	if(str2ipbuf(st, &ip)<0) {
1799
+	ipb = get_next_ipaddr_buf();
1800
+	if(str2ipbuf(st, ipb)<0) {
1786 1801
 		return NULL;
1787 1802
 	}
1788 1803
 
1789
-	return &ip;
1804
+	return ipb;
1790 1805
 }
1791 1806
 
1792 1807
 /* converts a str to an ipv6 address struct stored in ipb
... ...
@@ -1895,13 +1910,14 @@ error_char:
1895 1910
  * the ip_addr struct is static, so subsequent calls will destroy its content*/
1896 1911
 ip_addr_t* str2ip6(str* st)
1897 1912
 {
1898
-	static ip_addr_t ip;
1913
+	ip_addr_t *ipb;
1899 1914
 
1900
-	if(str2ip6buf(st, &ip)<0) {
1915
+	ipb = get_next_ipaddr_buf();
1916
+	if(str2ip6buf(st, ipb)<0) {
1901 1917
 		return NULL;
1902 1918
 	}
1903 1919
 
1904
-	return &ip;
1920
+	return ipb;
1905 1921
 }
1906 1922
 
1907 1923
 /* converts a str to an ipvv/6 address struct stored in ipb
... ...
@@ -1922,13 +1938,14 @@ int str2ipxbuf(str* st, ip_addr_t* ipb)
1922 1938
  * the ip_addr struct is static, so subsequent calls will destroy its content*/
1923 1939
 struct ip_addr* str2ipx(str* st)
1924 1940
 {
1925
-	static ip_addr_t ip;
1941
+	ip_addr_t *ipb;
1926 1942
 
1927
-	if(str2ipbuf(st, &ip)<0) {
1928
-		if(str2ip6buf(st, &ip)<0) {
1943
+	ipb = get_next_ipaddr_buf();
1944
+	if(str2ipbuf(st, ipb)<0) {
1945
+		if(str2ip6buf(st, ipb)<0) {
1929 1946
 			return NULL;
1930 1947
 		}
1931 1948
 	}
1932 1949
 
1933
-	return &ip;
1950
+	return ipb;
1934 1951
 }