Browse code

core: Fix typos

Bastian Triller authored on 05/05/2022 16:21:23 • Henning Westerholt committed on 08/05/2022 15:30:14
Showing 1 changed files
... ...
@@ -1388,9 +1388,9 @@ end:
1388 1388
  *         tried      - bitmap used to keep track of the already tried records
1389 1389
  *                      (no more then sizeof(tried)*8 valid records are
1390 1390
  *                      ever walked
1391
- *         srv_name   - if succesfull, it will be set to the selected record
1391
+ *         srv_name   - if successful, it will be set to the selected record
1392 1392
  *                      srv name (naptr repl.)
1393
- *         proto      - if succesfull it will be set to the selected record
1393
+ *         proto      - if successful it will be set to the selected record
1394 1394
  *                      protocol
1395 1395
  * returns  0 if no more records found or a pointer to the selected record
1396 1396
  *  and sets  protocol and srv_name
Browse code

core: resolve - use macro value for comparison

Daniel-Constantin Mierla authored on 16/03/2022 07:35:24
Showing 1 changed files
... ...
@@ -841,7 +841,7 @@ again:
841 841
 #endif
842 842
 		p+=skip;
843 843
 		rec_name_len=strlen(rec_name);
844
-		if (unlikely(rec_name_len>255)){
844
+		if (unlikely(rec_name_len>MAX_DNS_NAME-2)){
845 845
 			LM_ERR("dn_expand(rec_name): name too long (%d)\n",
846 846
 					rec_name_len);
847 847
 			goto error;
Browse code

core: resolve - safety check for setting port

Daniel-Constantin Mierla authored on 15/04/2021 06:32:38
Showing 1 changed files
... ...
@@ -1225,7 +1225,7 @@ change:
1225 1225
  *   and *proto!=0 or port==0 && proto==0)
1226 1226
  * when performing SRV lookup (*port==0) it will use *proto to look for
1227 1227
  * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
1228
- * If zt is set, name will be assumed to be 0 terminated and some copy 
1228
+ * If zt is set, name will be assumed to be 0 terminated and some copy
1229 1229
  * operations will be avoided.
1230 1230
  * If is_srv is set it will assume name has the srv prefixes for sip already
1231 1231
  *  appended and it's already 0-term'ed; if not it will append them internally.
... ...
@@ -1313,7 +1313,7 @@ struct hostent* srv_sip_resolvehost(str* name, int zt, unsigned short* port,
1313 1313
 do_srv:
1314 1314
 			/* try to find the SRV records inside previous ARs  first*/
1315 1315
 			for (l=ars; l; l=l->next){
1316
-				if (l->type!=T_SRV) continue; 
1316
+				if (l->type!=T_SRV) continue;
1317 1317
 				srv=(struct srv_rdata*) l->rdata;
1318 1318
 				if (srv==0){
1319 1319
 					LM_CRIT("null rdata\n");
... ...
@@ -1325,7 +1325,7 @@ do_srv:
1325 1325
 					/* we found it*/
1326 1326
 					LM_DBG("found SRV(%s) = %s:%d in AR\n",
1327 1327
 							srv_target, srv->name, srv->port);
1328
-					*port=srv->port;
1328
+					if(port) *port=srv->port;
1329 1329
 					/* cleanup on exit */
1330 1330
 					goto end;
1331 1331
 				}
... ...
@@ -1344,7 +1344,7 @@ do_srv:
1344 1344
 					/* we found it*/
1345 1345
 					LM_DBG("SRV(%s) = %s:%d\n",
1346 1346
 							srv_target, srv->name, srv->port);
1347
-					*port=srv->port;
1347
+					if(port) *port=srv->port;
1348 1348
 					/* cleanup on exit */
1349 1349
 					goto end;
1350 1350
 				}
... ...
@@ -1356,7 +1356,7 @@ do_srv:
1356 1356
 				goto end;
1357 1357
 			}
1358 1358
 			/* cleanup on exit */
1359
-			LM_DBG("no SRV record found for %.*s," 
1359
+			LM_DBG("no SRV record found for %.*s,"
1360 1360
 					" trying 'normal' lookup...\n", name->len, name->s);
1361 1361
 		}
1362 1362
 	}
Browse code

core: dns - safety checks for port and proto

Daniel-Constantin Mierla authored on 11/04/2021 07:09:24
Showing 1 changed files
... ...
@@ -1261,7 +1261,7 @@ struct hostent* srv_sip_resolvehost(str* name, int zt, unsigned short* port,
1261 1261
 	if (is_srv){
1262 1262
 		/* skip directly to srv resolving */
1263 1263
 		srv_proto=(proto)?*proto:0;
1264
-		*port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT;
1264
+		if(port) *port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT;
1265 1265
 		if (zt){
1266 1266
 			srv_target=name->s; /* name.s must be 0 terminated in
1267 1267
 								  this case */
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
 }
Browse code

core: dns - use all NAPTR records

- enable using of all NAPTR records instead of the first one ordered by
priority
- GH #2290

Semen Darienko authored on 03/05/2020 10:26:45 • Daniel-Constantin Mierla committed on 03/05/2020 10:26:45
Showing 1 changed files
... ...
@@ -407,6 +407,7 @@ struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
407 407
 		PKG_MEM_ERROR;
408 408
 		goto error;
409 409
 	}
410
+	naptr->skip_record = 0;
410 411
 	naptr->order=ntohs(order);
411 412
 	naptr->pref=ntohs(pref);
412 413
 	
Browse code

core: bug fix in str2ipbuf, return an error for invalid input parameter

Henning Westerholt authored on 12/11/2019 11:18:06
Showing 1 changed files
... ...
@@ -1738,7 +1738,7 @@ int str2ipbuf(str* st, ip_addr_t* ipb)
1738 1738
 	/* just in case that e.g. the VIA parser get confused */
1739 1739
 	if(unlikely(!st->s || st->len <= 0)) {
1740 1740
 		LM_ERR("invalid name, no conversion to IP address possible\n");
1741
-		return 0;
1741
+		return -1;
1742 1742
 	}
1743 1743
 	s=(unsigned char*)st->s;
1744 1744
 
Browse code

core: workaround related to T_OPT for alpine linux musl C library (GH #2095)

Henning Westerholt authored on 07/10/2019 13:38:22
Showing 1 changed files
... ...
@@ -32,8 +32,12 @@
32 32
 #include <resolv.h>
33 33
 #include <string.h>
34 34
 
35
-/* older glibc < 2.25 does not include T_OPT in nameser_compat.h yet */
36
-#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 25
35
+/*
36
+ * Older glibc < 2.25 does not include T_OPT in nameser_compat.h yet.
37
+ * On alpine linux musl library it is also not defined. There is no
38
+ * musl feature test macro, so we look for glibc instead.
39
+ */
40
+#if (defined __GLIBC__ &&  __GLIBC__ == 2 && __GLIBC_MINOR__ < 25) || !defined __GLIBC__
37 41
 #ifndef T_OPT
38 42
 #define T_OPT ns_t_opt
39 43
 #endif
Browse code

core: fix compile error related to missing T_OPT in older (<2.25) glibc (GH #2087)

Henning Westerholt authored on 01/10/2019 16:00:13
Showing 1 changed files
... ...
@@ -32,6 +32,13 @@
32 32
 #include <resolv.h>
33 33
 #include <string.h>
34 34
 
35
+/* older glibc < 2.25 does not include T_OPT in nameser_compat.h yet */
36
+#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 25
37
+#ifndef T_OPT
38
+#define T_OPT ns_t_opt
39
+#endif
40
+#endif
41
+
35 42
 #include "resolve.h"
36 43
 #include "compiler_opt.h"
37 44
 #include "dprint.h"
Browse code

core: skip get_record for DNS extensions e.g. for EDNS0 (GH #2087)

Henning Westerholt authored on 01/10/2019 14:38:20
Showing 1 changed files
... ...
@@ -951,6 +951,12 @@ again:
951 951
 				*last=rd;
952 952
 				last=&(rd->next);
953 953
 				break;
954
+			case T_OPT:
955
+				/* skip DNS extensions, e.g. EDNS0 */
956
+				rd->rdata=0;
957
+				*last=rd;
958
+				last=&(rd->next);
959
+				break;
954 960
 			default:
955 961
 				LM_ERR("unknown type %d\n", rtype);
956 962
 				rd->rdata=0;
Browse code

core: added a few variants for parsing IPv4/6 addresses

- store parsed structure directly in a provided buffer
- wrapper for parsing either IPv4 or IPv6

Daniel-Constantin Mierla authored on 22/05/2019 10:26:09
Showing 1 changed files
... ...
@@ -1254,7 +1254,7 @@ struct hostent* srv_sip_resolvehost(str* name, int zt, unsigned short* port,
1254 1254
 														  don't find another */
1255 1255
 		/* check if it's an ip address */
1256 1256
 		if (((ip=str2ip(name))!=0)
1257
-			  || ((ip=str2ip6(name))!=0) 
1257
+			  || ((ip=str2ip6(name))!=0)
1258 1258
 			 ){
1259 1259
 			/* we are lucky, this is an ip address */
1260 1260
 			he=ip_addr2he(name, ip);
... ...
@@ -1264,7 +1264,7 @@ struct hostent* srv_sip_resolvehost(str* name, int zt, unsigned short* port,
1264 1264
 			LM_WARN("domain name too long (%d), unable to perform SRV lookup\n",
1265 1265
 						name->len);
1266 1266
 		}else{
1267
-			
1267
+
1268 1268
 			switch(srv_proto){
1269 1269
 				case PROTO_UDP:
1270 1270
 				case PROTO_TCP:
... ...
@@ -1691,7 +1691,7 @@ int sip_hostport2su(union sockaddr_union* su, str* name, unsigned short port,
1691 1691
 						char* proto)
1692 1692
 {
1693 1693
 	struct hostent* he;
1694
-	
1694
+
1695 1695
 	he=sip_resolvehost(name, &port, proto);
1696 1696
 	if (he==0){
1697 1697
 		ser_error=E_BAD_ADDRESS;
... ...
@@ -1708,3 +1708,209 @@ int sip_hostport2su(union sockaddr_union* su, str* name, unsigned short port,
1708 1708
 error:
1709 1709
 	return -1;
1710 1710
 }
1711
+
1712
+/* converts a str to an ipv4 address struct stored in ipb
1713
+ * - ipb must be already allocated
1714
+ * - return 0 on success; <0 on failure */
1715
+int str2ipbuf(str* st, ip_addr_t* ipb)
1716
+{
1717
+	int i;
1718
+	unsigned char *limit;
1719
+	unsigned char* s;
1720
+
1721
+	/* just in case that e.g. the VIA parser get confused */
1722
+	if(unlikely(!st->s || st->len <= 0)) {
1723
+		LM_ERR("invalid name, no conversion to IP address possible\n");
1724
+		return 0;
1725
+	}
1726
+	s=(unsigned char*)st->s;
1727
+
1728
+	/*init*/
1729
+	ipb->u.addr32[0]=0;
1730
+	i=0;
1731
+	limit=(unsigned char*)(st->s + st->len);
1732
+
1733
+	for(;s<limit ;s++){
1734
+		if (*s=='.'){
1735
+				i++;
1736
+				if (i>3) goto error_dots;
1737
+		}else if ( (*s <= '9' ) && (*s >= '0') ){
1738
+				ipb->u.addr[i]=ipb->u.addr[i]*10+*s-'0';
1739
+		}else{
1740
+				//error unknown char
1741
+				goto error_char;
1742
+		}
1743
+	}
1744
+	if (i<3) goto error_dots;
1745
+	ipb->af=AF_INET;
1746
+	ipb->len=4;
1747
+
1748
+	return 0;
1749
+
1750
+error_dots:
1751
+	DBG("error - too %s dots in [%.*s]\n", (i>3)?"many":"few",
1752
+			st->len, st->s);
1753
+	return -1;
1754
+ error_char:
1755
+	/*
1756
+	DBG("warning - unexpected char %c in [%.*s]\n", *s, st->len, st->s);
1757
+	*/
1758
+	return -2;
1759
+}
1760
+
1761
+/* converts a str to an ipv4 address, returns the address or 0 on error
1762
+   Warning: the result is a pointer to a statically allocated structure */
1763
+ip_addr_t* str2ip(str* st)
1764
+{
1765
+	static ip_addr_t ip;
1766
+
1767
+	if(str2ipbuf(st, &ip)<0) {
1768
+		return NULL;
1769
+	}
1770
+
1771
+	return &ip;
1772
+}
1773
+
1774
+/* converts a str to an ipv6 address struct stored in ipb
1775
+ * - ipb must be already allocated
1776
+ * - return 0 on success; <0 on failure */
1777
+int str2ip6buf(str* st, ip_addr_t* ipb)
1778
+{
1779
+	int i, idx1, rest;
1780
+	int no_colons;
1781
+	int double_colon;
1782
+	int hex;
1783
+	unsigned short* addr_start;
1784
+	unsigned short addr_end[8];
1785
+	unsigned short* addr;
1786
+	unsigned char* limit;
1787
+	unsigned char* s;
1788
+
1789
+	/* just in case that e.g. the VIA parser get confused */
1790
+	if(unlikely(!st->s || st->len <= 0)) {
1791
+		LM_ERR("invalid name, no conversion to IP address possible\n");
1792
+		return -1;
1793
+	}
1794
+	/* init */
1795
+	if ((st->len) && (st->s[0]=='[')){
1796
+		/* skip over [ ] */
1797
+		if (st->s[st->len-1]!=']') goto error_char;
1798
+		s=(unsigned char*)(st->s+1);
1799
+		limit=(unsigned char*)(st->s+st->len-1);
1800
+	}else{
1801
+		s=(unsigned char*)st->s;
1802
+		limit=(unsigned char*)(st->s+st->len);
1803
+	}
1804
+	i=idx1=rest=0;
1805
+	double_colon=0;
1806
+	no_colons=0;
1807
+	ipb->af=AF_INET6;
1808
+	ipb->len=16;
1809
+	addr_start=ipb->u.addr16;
1810
+	addr=addr_start;
1811
+	memset(addr_start, 0 , 8*sizeof(unsigned short));
1812
+	memset(addr_end, 0 , 8*sizeof(unsigned short));
1813
+	for (; s<limit; s++){
1814
+		if (*s==':'){
1815
+			no_colons++;
1816
+			if (no_colons>7) goto error_too_many_colons;
1817
+			if (double_colon){
1818
+				idx1=i;
1819
+				i=0;
1820
+				if (addr==addr_end) goto error_colons;
1821
+				addr=addr_end;
1822
+			}else{
1823
+				double_colon=1;
1824
+				addr[i]=htons(addr[i]);
1825
+				i++;
1826
+			}
1827
+		}else if ((hex=HEX2I(*s))>=0){
1828
+				addr[i]=addr[i]*16+hex;
1829
+				double_colon=0;
1830
+		}else{
1831
+			/* error, unknown char */
1832
+			goto error_char;
1833
+		}
1834
+	}
1835
+	if (!double_colon){ /* not ending in ':' */
1836
+		addr[i]=htons(addr[i]);
1837
+		i++;
1838
+	}
1839
+	/* if address contained '::' fix it */
1840
+	if (addr==addr_end){
1841
+		rest=8-i-idx1;
1842
+		memcpy(addr_start+idx1+rest, addr_end, i*sizeof(unsigned short));
1843
+	}else{
1844
+		/* no double colons inside */
1845
+		if (no_colons<7) goto error_too_few_colons;
1846
+	}
1847
+/*
1848
+	DBG("idx1=%d, rest=%d, no_colons=%d, hex=%x\n",
1849
+			idx1, rest, no_colons, hex);
1850
+	DBG("address %x:%x:%x:%x:%x:%x:%x:%x\n",
1851
+			addr_start[0], addr_start[1], addr_start[2],
1852
+			addr_start[3], addr_start[4], addr_start[5],
1853
+			addr_start[6], addr_start[7] );
1854
+*/
1855
+	return 0;
1856
+
1857
+error_too_many_colons:
1858
+	DBG("error - too many colons in [%.*s]\n", st->len, st->s);
1859
+	return -1;
1860
+
1861
+error_too_few_colons:
1862
+	DBG("error - too few colons in [%.*s]\n", st->len, st->s);
1863
+	return -2;
1864
+
1865
+error_colons:
1866
+	DBG("error - too many double colons in [%.*s]\n", st->len, st->s);
1867
+	return -3;
1868
+
1869
+error_char:
1870
+	/*
1871
+	DBG("warning - unexpected char %c in  [%.*s]\n", *s, st->len,
1872
+			st->s);*/
1873
+	return -4;
1874
+}
1875
+
1876
+/* returns an ip_addr struct.; on error returns 0
1877
+ * the ip_addr struct is static, so subsequent calls will destroy its content*/
1878
+ip_addr_t* str2ip6(str* st)
1879
+{
1880
+	static ip_addr_t ip;
1881
+
1882
+	if(str2ip6buf(st, &ip)<0) {
1883
+		return NULL;
1884
+	}
1885
+
1886
+	return &ip;
1887
+}
1888
+
1889
+/* converts a str to an ipvv/6 address struct stored in ipb
1890
+ * - ipb must be already allocated
1891
+ * - return 0 on success; <0 on failure */
1892
+int str2ipxbuf(str* st, ip_addr_t* ipb)
1893
+{
1894
+	if (str2ipbuf(st, ipb)<0) {
1895
+		if(str2ip6buf(st, ipb) < 0) {
1896
+			return -1;
1897
+		}
1898
+	}
1899
+
1900
+	return 0;
1901
+}
1902
+
1903
+/* returns an ip_addr struct converted from ipv4/6 str; on error returns 0
1904
+ * the ip_addr struct is static, so subsequent calls will destroy its content*/
1905
+struct ip_addr* str2ipx(str* st)
1906
+{
1907
+	static ip_addr_t ip;
1908
+
1909
+	if(str2ipbuf(st, &ip)<0) {
1910
+		if(str2ip6buf(st, &ip)<0) {
1911
+			return NULL;
1912
+		}
1913
+	}
1914
+
1915
+	return &ip;
1916
+}
Browse code

core: dns slow queries reporting

Julien Chavanton authored on 29/01/2019 18:08:18
Showing 1 changed files
... ...
@@ -51,6 +51,8 @@ struct dns_counters_h dns_cnts_h;
51 51
 counter_def_t dns_cnt_defs[] =  {
52 52
 	{&dns_cnts_h.failed_dns_req, "failed_dns_request", 0, 0, 0,
53 53
 		"incremented each time a DNS request has failed."},
54
+	{&dns_cnts_h.slow_dns_req, "slow_dns_request", 0, 0, 0,
55
+		"incremented each time a DNS request took longer than dns_slow_query_ms."},
54 56
 	{0, 0, 0, 0, 0, 0 }
55 57
 };
56 58
 
... ...
@@ -714,6 +716,8 @@ struct rdata* get_record(char* name, int type, int flags)
714 716
 	int name_len;
715 717
 	struct rdata* fullname_rd;
716 718
 	char c;
719
+	struct timeval start, stop;
720
+	int slow_query_ms = cfg_get(core, core_cfg, dns_slow_query_ms);
717 721
 
718 722
 	name_len=strlen(name);
719 723
 
... ...
@@ -735,8 +739,21 @@ struct rdata* get_record(char* name, int type, int flags)
735 739
 	}
736 740
 	fullname_rd=0;
737 741
 
742
+	if (slow_query_ms > 0)
743
+		gettimeofday(&start, NULL);
744
+
738 745
 	size=dns_func.sr_res_search(name, C_IN, type, buff.buff, sizeof(buff));
739 746
 
747
+	if (slow_query_ms > 0) {
748
+		gettimeofday(&stop, NULL);
749
+		int latency_ms = (stop.tv_sec - start.tv_sec)*1000
750
+                + (stop.tv_usec - start.tv_usec)/1000;
751
+		if (slow_query_ms < latency_ms) {
752
+			LOG(cfg_get(core, core_cfg, latency_log), "res_search[%d][%s]elapsed[%dms]\n", type, name, latency_ms);
753
+			counter_inc(dns_cnts_h.slow_dns_req);
754
+		}
755
+	}
756
+
740 757
 	if (unlikely(size<0)) {
741 758
 		LM_DBG("lookup(%s, %d) failed\n", name, type);
742 759
 		goto not_found;
Browse code

core: cleanup, remove local_malloc/local_free #define in core

- remove the unused local_malloc and local_free #define in the core
- they were introduced in 2002 and not touched in the repository since this time
- they pointed to pkg_malloc and pkg_free

Henning Westerholt authored on 23/12/2018 21:50:43
Showing 1 changed files
... ...
@@ -54,9 +54,6 @@ counter_def_t dns_cnt_defs[] =  {
54 54
 	{0, 0, 0, 0, 0, 0 }
55 55
 };
56 56
 
57
-/* mallocs for local stuff */
58
-#define local_malloc pkg_malloc
59
-#define local_free   pkg_free
60 57
 
61 58
 #ifdef USE_NAPTR
62 59
 static int naptr_proto_pref[PROTO_LAST+1];
... ...
@@ -305,7 +302,7 @@ struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
305 302
 	if (len>255)
306 303
 		goto error;
307 304
 	/* alloc enought space for the struct + null terminated name */
308
-	srv=local_malloc(sizeof(struct srv_rdata)-1+len+1);
305
+	srv=pkg_malloc(sizeof(struct srv_rdata)-1+len+1);
309 306
 	if (srv==0){
310 307
 		PKG_MEM_ERROR;
311 308
 		goto error;
... ...
@@ -319,7 +316,7 @@ struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
319 316
 	
320 317
 	return srv;
321 318
 error:
322
-	if (srv) local_free(srv);
319
+	if (srv) pkg_free(srv);
323 320
 	return 0;
324 321
 }
325 322
 
... ...
@@ -391,7 +388,7 @@ struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
391 388
 	len=strlen(repl);
392 389
 	if (len>255)
393 390
 		goto error;
394
-	naptr=local_malloc(sizeof(struct naptr_rdata)+flags_len+services_len+
391
+	naptr=pkg_malloc(sizeof(struct naptr_rdata)+flags_len+services_len+
395 392
 						regexp_len+len+1-1);
396 393
 	if (naptr == 0){
397 394
 		PKG_MEM_ERROR;
... ...
@@ -416,7 +413,7 @@ struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
416 413
 	
417 414
 	return naptr;
418 415
 error:
419
-	if (naptr) local_free(naptr);
416
+	if (naptr) pkg_free(naptr);
420 417
 	return 0;
421 418
 }
422 419
 
... ...
@@ -437,7 +434,7 @@ struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
437 434
 	if (len>255)
438 435
 		goto error;
439 436
 	/* alloc sizeof struct + space for the null terminated name */
440
-	cname=local_malloc(sizeof(struct cname_rdata)-1+len+1);
437
+	cname=pkg_malloc(sizeof(struct cname_rdata)-1+len+1);
441 438
 	if(cname==0){
442 439
 		PKG_MEM_ERROR;
443 440
 		goto error;
... ...
@@ -447,7 +444,7 @@ struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
447 444
 	cname->name[cname->name_len]=0;
448 445
 	return cname;
449 446
 error:
450
-	if (cname) local_free(cname);
447
+	if (cname) pkg_free(cname);
451 448
 	return 0;
452 449
 }
453 450
 
... ...
@@ -461,7 +458,7 @@ struct a_rdata* dns_a_parser(unsigned char* rdata, unsigned char* eor)
461 458
 	struct a_rdata* a;
462 459
 	
463 460
 	if (rdata+4>eor) goto error;
464
-	a=(struct a_rdata*)local_malloc(sizeof(struct a_rdata));
461
+	a=(struct a_rdata*)pkg_malloc(sizeof(struct a_rdata));
465 462
 	if (a==0){
466 463
 		PKG_MEM_ERROR;
467 464
 		goto error;
... ...
@@ -481,7 +478,7 @@ struct aaaa_rdata* dns_aaaa_parser(unsigned char* rdata, unsigned char* eor)
481 478
 	struct aaaa_rdata* aaaa;
482 479
 	
483 480
 	if (rdata+16>eor) goto error;
484
-	aaaa=(struct aaaa_rdata*)local_malloc(sizeof(struct aaaa_rdata));
481
+	aaaa=(struct aaaa_rdata*)pkg_malloc(sizeof(struct aaaa_rdata));
485 482
 	if (aaaa==0){
486 483
 		PKG_MEM_ERROR;
487 484
 		goto error;
... ...
@@ -531,7 +528,7 @@ static struct txt_rdata* dns_txt_parser(unsigned char* msg, unsigned char* end,
531 528
 	}while(p<end);
532 529
 	/* alloc sizeof struct + space for the dns_cstr array + space for
533 530
 	   the strings */
534
-	txt=local_malloc(sizeof(struct txt_rdata) +(n-1)*sizeof(struct dns_cstr)+
531
+	txt=pkg_malloc(sizeof(struct txt_rdata) +(n-1)*sizeof(struct dns_cstr)+
535 532
 						str_size);
536 533
 	if(unlikely(txt==0)){
537 534
 		PKG_MEM_ERROR;
... ...
@@ -555,7 +552,7 @@ static struct txt_rdata* dns_txt_parser(unsigned char* msg, unsigned char* end,
555 552
 	}
556 553
 	return txt;
557 554
 error:
558
-	if (txt) local_free(txt);
555
+	if (txt) pkg_free(txt);
559 556
 	return 0;
560 557
 }
561 558
 
... ...
@@ -602,7 +599,7 @@ static struct ebl_rdata* dns_ebl_parser(unsigned char* msg, unsigned char* end,
602 599
 		goto error;
603 600
 	apex_len=strlen(apex);
604 601
 	/* alloc sizeof struct + space for the 2 null-terminated strings */
605
-	ebl=local_malloc(sizeof(struct ebl_rdata)-1+sep_len+1+apex_len+1);
602
+	ebl=pkg_malloc(sizeof(struct ebl_rdata)-1+sep_len+1+apex_len+1);
606 603
 	if (ebl==0){
607 604
 		PKG_MEM_ERROR;
608 605
 		goto error;
... ...
@@ -619,7 +616,7 @@ static struct ebl_rdata* dns_ebl_parser(unsigned char* msg, unsigned char* end,
619 616
 	
620 617
 	return ebl;
621 618
 error:
622
-	if (ebl) local_free(ebl);
619
+	if (ebl) pkg_free(ebl);
623 620
 	return 0;
624 621
 }
625 622
 
... ...
@@ -640,7 +637,7 @@ struct ptr_rdata* dns_ptr_parser( unsigned char* msg, unsigned char* end,
640 637
 	if (len>255)
641 638
 		goto error;
642 639
 	/* alloc sizeof struct + space for the null terminated name */
643
-	pname=local_malloc(sizeof(struct ptr_rdata)-1+len+1);
640
+	pname=pkg_malloc(sizeof(struct ptr_rdata)-1+len+1);
644 641
 	if(pname==0){
645 642
 		PKG_MEM_ERROR;
646 643
 		goto error;
... ...
@@ -650,7 +647,7 @@ struct ptr_rdata* dns_ptr_parser( unsigned char* msg, unsigned char* end,
650 647
 	pname->ptrdname[pname->ptrdname_len]=0;
651 648
 	return pname;
652 649
 error:
653
-	if (pname) local_free(pname);
650
+	if (pname) pkg_free(pname);
654 651
 	return 0;
655 652
 }
656 653
 
... ...
@@ -665,8 +662,8 @@ void free_rdata_list(struct rdata* head)
665 662
 	while (l != 0) {
666 663
 		next_l = l->next;
667 664
 		/* free the parsed rdata*/
668
-		if (l->rdata) local_free(l->rdata);
669
-		local_free(l);
665
+		if (l->rdata) pkg_free(l->rdata);
666
+		pkg_free(l);
670 667
 		l = next_l;
671 668
 	}
672 669
 }
... ...
@@ -833,7 +830,7 @@ again:
833 830
 		}
834 831
 		/* expand the "type" record  (rdata)*/
835 832
 
836
-		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+
833
+		rd=(struct rdata*) pkg_malloc(sizeof(struct rdata)+rec_name_len+
837 834
 										1-1);
838 835
 		if (rd==0){
839 836
 			PKG_MEM_ERROR;
... ...
@@ -972,7 +969,7 @@ again:
972 969
 	 * (queried) to long name (answered)
973 970
 	 */
974 971
 	if ((search_list_used==1)&&(fullname_rd!=0)) {
975
-		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+name_len+1-1);
972
+		rd=(struct rdata*) pkg_malloc(sizeof(struct rdata)+name_len+1-1);
976 973
 		if (unlikely(rd==0)){
977 974
 			PKG_MEM_ERROR;
978 975
 			goto error;
... ...
@@ -985,7 +982,7 @@ again:
985 982
 		rd->name[name_len]=0;
986 983
 		rd->name_len=name_len;
987 984
 		/* alloc sizeof struct + space for the null terminated name */
988
-		rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+
985
+		rd->rdata=(void*)pkg_malloc(sizeof(struct cname_rdata)-1+
989 986
 										head->name_len+1);
990 987
 		if(unlikely(rd->rdata==0)){
991 988
 			PKG_MEM_ERROR;
... ...
@@ -1009,7 +1006,7 @@ error_parse:
1009 1006
 				name, type,
1010 1007
 				p, end, rtype, class, ttl, rdlength);
1011 1008
 error_rd:
1012
-		if (rd) local_free(rd); /* rd->rdata=0 & rd is not linked yet into
1009
+		if (rd) pkg_free(rd); /* rd->rdata=0 & rd is not linked yet into
1013 1010
 								   the list */
1014 1011
 error:
1015 1012
 		LM_ERR("get_record\n");
Browse code

core: use generic PKG_MEM_ERROR, SHM_ERROR and _CRITICAL helper defines in core

- refactoring of the core to use generic PKG_MEM_ERROR, SHM_ERROR,
PKG_MEM_CRITICAL, SHM_MEM_CRITICAL and SYS_MEM_ERROR helper defines
- unify many different error messages in different spellings
- add a few missing error handler for allocation errors after (found
with a complete review of all memory allocation functions in core)
- no other functional changes, change has been reviewed two times

Henning Westerholt authored on 23/12/2018 21:31:03
Showing 1 changed files
... ...
@@ -307,7 +307,7 @@ struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
307 307
 	/* alloc enought space for the struct + null terminated name */
308 308
 	srv=local_malloc(sizeof(struct srv_rdata)-1+len+1);
309 309
 	if (srv==0){
310
-		LM_ERR("out of memory\n");
310
+		PKG_MEM_ERROR;
311 311
 		goto error;
312 312
 	}
313 313
 	srv->priority=ntohs(priority);
... ...
@@ -394,7 +394,7 @@ struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
394 394
 	naptr=local_malloc(sizeof(struct naptr_rdata)+flags_len+services_len+
395 395
 						regexp_len+len+1-1);
396 396
 	if (naptr == 0){
397
-		LM_ERR("out of memory\n");
397
+		PKG_MEM_ERROR;
398 398
 		goto error;
399 399
 	}
400 400
 	naptr->order=ntohs(order);
... ...
@@ -439,7 +439,7 @@ struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
439 439
 	/* alloc sizeof struct + space for the null terminated name */
440 440
 	cname=local_malloc(sizeof(struct cname_rdata)-1+len+1);
441 441
 	if(cname==0){
442
-		LM_ERR("out of memory\n");
442
+		PKG_MEM_ERROR;
443 443
 		goto error;
444 444
 	}
445 445
 	cname->name_len=len;
... ...
@@ -463,7 +463,7 @@ struct a_rdata* dns_a_parser(unsigned char* rdata, unsigned char* eor)
463 463
 	if (rdata+4>eor) goto error;
464 464
 	a=(struct a_rdata*)local_malloc(sizeof(struct a_rdata));
465 465
 	if (a==0){
466
-		LM_ERR("out of memory\n");
466
+		PKG_MEM_ERROR;
467 467
 		goto error;
468 468
 	}
469 469
 	memcpy(a->ip, rdata, 4);
... ...
@@ -483,7 +483,7 @@ struct aaaa_rdata* dns_aaaa_parser(unsigned char* rdata, unsigned char* eor)
483 483
 	if (rdata+16>eor) goto error;
484 484
 	aaaa=(struct aaaa_rdata*)local_malloc(sizeof(struct aaaa_rdata));
485 485
 	if (aaaa==0){
486
-		LM_ERR("out of memory\n");
486
+		PKG_MEM_ERROR;
487 487
 		goto error;
488 488
 	}
489 489
 	memcpy(aaaa->ip6, rdata, 16);
... ...
@@ -534,7 +534,7 @@ static struct txt_rdata* dns_txt_parser(unsigned char* msg, unsigned char* end,
534 534
 	txt=local_malloc(sizeof(struct txt_rdata) +(n-1)*sizeof(struct dns_cstr)+
535 535
 						str_size);
536 536
 	if(unlikely(txt==0)){
537
-		LM_ERR("out of memory\n");
537
+		PKG_MEM_ERROR;
538 538
 		goto error;
539 539
 	}
540 540
 	/* string table */
... ...
@@ -604,7 +604,7 @@ static struct ebl_rdata* dns_ebl_parser(unsigned char* msg, unsigned char* end,
604 604
 	/* alloc sizeof struct + space for the 2 null-terminated strings */
605 605
 	ebl=local_malloc(sizeof(struct ebl_rdata)-1+sep_len+1+apex_len+1);
606 606
 	if (ebl==0){
607
-		LM_ERR("out of memory\n");
607
+		PKG_MEM_ERROR;
608 608
 		goto error;
609 609
 	}
610 610
 	ebl->position=rdata[0];
... ...
@@ -642,7 +642,7 @@ struct ptr_rdata* dns_ptr_parser( unsigned char* msg, unsigned char* end,
642 642
 	/* alloc sizeof struct + space for the null terminated name */
643 643
 	pname=local_malloc(sizeof(struct ptr_rdata)-1+len+1);
644 644
 	if(pname==0){
645
-		LM_ERR("out of memory\n");
645
+		PKG_MEM_ERROR;
646 646
 		goto error;
647 647
 	}
648 648
 	pname->ptrdname_len=len;
... ...
@@ -836,7 +836,7 @@ again:
836 836
 		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+
837 837
 										1-1);
838 838
 		if (rd==0){
839
-			LM_ERR("out of memory\n");
839
+			PKG_MEM_ERROR;
840 840
 			goto error;
841 841
 		}
842 842
 		rd->type=rtype;
... ...
@@ -974,7 +974,7 @@ again:
974 974
 	if ((search_list_used==1)&&(fullname_rd!=0)) {
975 975
 		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+name_len+1-1);
976 976
 		if (unlikely(rd==0)){
977
-			LM_ERR("out of memory\n");
977
+			PKG_MEM_ERROR;
978 978
 			goto error;
979 979
 		}
980 980
 		rd->type=T_CNAME;
... ...
@@ -988,7 +988,7 @@ again:
988 988
 		rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+
989 989
 										head->name_len+1);
990 990
 		if(unlikely(rd->rdata==0)){
991
-			LM_ERR("out of memory\n");
991
+			PKG_MEM_ERROR;
992 992
 			goto error_rd;
993 993
 		}
994 994
 		((struct cname_rdata*)(rd->rdata))->name_len=fullname_rd->name_len;
Browse code

core: cleanup resolver defines, no function change

cleanup core resolver defines, they were set since 2007 for a bit additional debugging,
no change in functionality

Henning Westerholt authored on 08/06/2018 21:52:29
Showing 1 changed files
... ...
@@ -950,9 +950,7 @@ again:
950 950
 	if (flags & RES_AR){
951 951
 		flags&=~RES_AR;
952 952
 		answers_no=ntohs((unsigned short)buff.hdr.nscount);
953
-#ifdef RESOLVE_DBG
954 953
 		LM_DBG("skipping %d NS (p=%p, end=%p)\n", answers_no, p, end);
955
-#endif
956 954
 		for (r=0; (r<answers_no) && (p<end); r++){
957 955
 			/* skip over the ns records */
958 956
 			if ((p=dns_skipname(p, end))==0) {
... ...
@@ -965,9 +963,7 @@ again:
965 963
 			p+=2+2+4+2+ntohs(rdlength);
966 964
 		}
967 965
 		answers_no=ntohs((unsigned short)buff.hdr.arcount);
968
-#ifdef RESOLVE_DBG
969 966
 		LM_DBG("parsing %d ARs (p=%p, end=%p)\n", answers_no, p, end);
970
-#endif
971 967
 		goto again; /* add also the additional records */
972 968
 	}
973 969
 
... ...
@@ -1152,12 +1148,10 @@ int naptr_proto_preferred(char new_proto, char old_proto)
1152 1148
 int naptr_choose (struct naptr_rdata** crt, char* crt_proto,
1153 1149
 									struct naptr_rdata* n , char n_proto)
1154 1150
 {
1155
-#ifdef NAPTR_DBG
1156 1151
 	LM_DBG("o:%d w:%d p:%d, o:%d w:%d p:%d\n",
1157 1152
 			*crt?(int)(*crt)->order:-1, *crt?(int)(*crt)->pref:-1,
1158 1153
 			(int)*crt_proto,
1159 1154
 			(int)n->order, (int)n->pref, (int)n_proto);
1160
-#endif
1161 1155
 	if ((*crt==0) || ((*crt_proto!=n_proto) && 
1162 1156
 						( naptr_proto_preferred(n_proto, *crt_proto))) )
1163 1157
 			goto change;
... ...
@@ -1166,14 +1160,10 @@ int naptr_choose (struct naptr_rdata** crt, char* crt_proto,
1166 1160
 								(n->pref < (*crt)->pref)))){
1167 1161
 			goto change;
1168 1162
 	}
1169
-#ifdef NAPTR_DBG
1170 1163
 	LM_DBG("no change\n");
1171
-#endif
1172 1164
 	return 0;
1173 1165
 change:
1174
-#ifdef NAPTR_DBG
1175 1166
 	LM_DBG("changed\n");
1176
-#endif
1177 1167
 	*crt_proto=n_proto;
1178 1168
 	*crt=n;
1179 1169
 	return 1;
... ...
@@ -1220,10 +1210,8 @@ struct hostent* srv_sip_resolvehost(str* name, int zt, unsigned short* port,
1220 1210
 		he=0;
1221 1211
 		goto end;
1222 1212
 	}
1223
-#ifdef RESOLVE_DBG
1224 1213
 	LM_DBG("%.*s:%d proto=%d\n", name->len, name->s,
1225 1214
 			port?(int)*port:-1, proto?(int)*proto:-1);
1226
-#endif
1227 1215
 	if (is_srv){
1228 1216
 		/* skip directly to srv resolving */
1229 1217
 		srv_proto=(proto)?*proto:0;
... ...
@@ -1289,10 +1277,8 @@ do_srv:
1289 1277
 				he=resolvehost(srv->name);
1290 1278
 				if (he!=0){
1291 1279
 					/* we found it*/
1292
-#ifdef RESOLVE_DBG
1293 1280
 					LM_DBG("found SRV(%s) = %s:%d in AR\n",
1294 1281
 							srv_target, srv->name, srv->port);
1295
-#endif
1296 1282
 					*port=srv->port;
1297 1283
 					/* cleanup on exit */
1298 1284
 					goto end;
... ...
@@ -1310,10 +1296,8 @@ do_srv:
1310 1296
 				he=resolvehost(srv->name);
1311 1297
 				if (he!=0){
1312 1298
 					/* we found it*/
1313
-#ifdef RESOLVE_DBG
1314 1299
 					LM_DBG("SRV(%s) = %s:%d\n",
1315 1300
 							srv_target, srv->name, srv->port);
1316
-#endif
1317 1301
 					*port=srv->port;
1318 1302
 					/* cleanup on exit */
1319 1303
 					goto end;
... ...
@@ -1326,10 +1310,8 @@ do_srv:
1326 1310
 				goto end;
1327 1311
 			}
1328 1312
 			/* cleanup on exit */
1329
-#ifdef RESOLVE_DBG
1330 1313
 			LM_DBG("no SRV record found for %.*s," 
1331 1314
 					" trying 'normal' lookup...\n", name->len, name->s);
1332
-#endif
1333 1315
 		}
1334 1316
 	}
1335 1317
 	if (likely(!zt)){
... ...
@@ -1340,11 +1322,9 @@ do_srv:
1340 1322
 		he=resolvehost(name->s);
1341 1323
 	}
1342 1324
 end:
1343
-#ifdef RESOLVE_DBG
1344 1325
 	LM_DBG("returning %p (%.*s:%d proto=%d)\n",
1345 1326
 			he, name->len, name->s,
1346 1327
 			port?(int)*port:-1, proto?(int)*proto:-1);
1347
-#endif
1348 1328
 	if (srv_head)
1349 1329
 		free_rdata_list(srv_head);
1350 1330
 	return he;
... ...
@@ -1402,10 +1382,8 @@ struct rdata* naptr_sip_iterate(struct rdata* naptr_head,
1402 1382
 			i++;
1403 1383
 			continue; /* already tried */
1404 1384
 		}
1405
-#ifdef NAPTR_DBG
1406 1385
 		LM_DBG("found a valid sip NAPTR rr %.*s, proto %d\n",
1407 1386
 					naptr->repl_len, naptr->repl, (int)naptr_proto);
1408
-#endif
1409 1387
 		if ((naptr_proto_supported(naptr_proto))){
1410 1388
 			if (naptr_choose(&naptr_saved, &saved_proto,
1411 1389
 								naptr, naptr_proto)) {
... ...
@@ -1417,11 +1395,9 @@ struct rdata* naptr_sip_iterate(struct rdata* naptr_head,
1417 1395
 	}
1418 1396
 	if (naptr_saved){
1419 1397
 		/* found something */
1420
-#ifdef NAPTR_DBG
1421 1398
 		LM_DBG("choosed NAPTR rr %.*s, proto %d tried: 0x%x\n",
1422 1399
 					naptr_saved->repl_len,
1423 1400
 					naptr_saved->repl, (int)saved_proto, *tried);
1424
-#endif
1425 1401
 		*tried|=1<<idx;
1426 1402
 		*proto=saved_proto;
1427 1403
 		srv_name->s=naptr_saved->repl;
... ...
@@ -1645,10 +1621,8 @@ struct hostent* naptr_sip_resolvehost(str* name,  unsigned short* port,
1645 1621
 			}
1646 1622
 		}
1647 1623
 		/*clean up on exit*/
1648
-#ifdef RESOLVE_DBG
1649 1624
 		LM_DBG("no NAPTR record found for %.*s, trying SRV lookup...\n",
1650 1625
 					name->len, name->s);
1651
-#endif
1652 1626
 	}
1653 1627
 	/* fallback to srv lookup */
1654 1628
 	if(proto) *proto = origproto;
Browse code

core: resolve - early safety checks to deal faster with broken dns responses

Daniel-Constantin Mierla authored on 30/12/2017 15:57:42
Showing 1 changed files
... ...
@@ -679,13 +679,16 @@ void free_rdata_list(struct rdata* head)
679 679
 int match_search_list(const struct __res_state* res, char* name) {