Browse code

- added DNS_IP_HACK - added inline functions: str2ip, str2s, q_inet_ntoa (faster then their library equivalents).

Andrei Pelinescu-Onciul authored on 15/11/2001 23:39:36
Showing 7 changed files
... ...
@@ -27,7 +27,9 @@ NAME=ser
27 27
 # NO_LOG completely turns of all the logging (and DBG(...))
28 28
 # DEBUG compiles in some extra debugging code
29 29
 # OLD_PARSER uses the old and stable parser (from ser 8.3.2)
30
-DEFS=-DNOCR -DMACROEATER -DSTATS -DOLD_PARSER #-DNO_DEBUG #-DNO_LOG
30
+# DNS_IP_HACK faster ip address resolver for ip strings (e.g "127.0.0.1")
31
+DEFS=-DNOCR -DMACROEATER -DSTATS -DOLD_PARSER -DDNS_IP_HACK #-DNO_DEBUG 
32
+#-DNO_LOG
31 33
 
32 34
 PROFILE=  # -pg #set this if you want profiling
33 35
 
... ...
@@ -38,7 +40,7 @@ ARCH = $(shell uname -s)
38 40
 #common
39 41
 CC=gcc
40 42
 LD=gcc
41
-CFLAGS=-O2 -Wcast-align $(PROFILE)#-Wmissing-prototypes 
43
+CFLAGS=-O2 -Wcast-align $(PROFILE) -Winline#-Wmissing-prototypes 
42 44
 LDFLAGS=-Wl,-O2 -Wl,-E $(PROFILE)
43 45
 LEX=flex
44 46
 YACC=bison
... ...
@@ -13,6 +13,7 @@
13 13
 #include "udp_server.h"
14 14
 #include "route.h"
15 15
 #include "msg_parser.h"
16
+#include "ut.h"
16 17
 #include "sr_module.h"
17 18
 
18 19
 #include <sys/types.h>
... ...
@@ -40,6 +41,7 @@ int do_action(struct action* a, struct sip_msg* msg)
40 41
 	char *new_uri, *end, *crt;
41 42
 	int len;
42 43
 	int user;
44
+	int err;
43 45
 	struct sip_uri uri;
44 46
 	unsigned short port;
45 47
 
... ...
@@ -67,8 +69,11 @@ int do_action(struct action* a, struct sip_msg* msg)
67 69
 				switch (a->p2_type){
68 70
 					case URIPORT_ST:
69 71
 									if (uri.port.s){
70
-										port=strtol(uri.port.s,&end,10);
71
-										if ((end)&&(*end)){
72
+									 /*port=strtol(uri.port.s,&end,10);*/
73
+										port=str2s(uri.port.s, uri.port.len,
74
+													&err);
75
+										/*if ((end)&&(*end)){*/
76
+										if (err){
72 77
 											LOG(L_ERR, "ERROR: do_action: "
73 78
 													"forward: bad port in "
74 79
 													"uri: <%s>\n", uri.port);
... ...
@@ -20,6 +20,7 @@
20 20
 #include "udp_server.h"
21 21
 #include "globals.h"
22 22
 #include "data_lump.h"
23
+#include "ut.h"
23 24
 
24 25
 #ifdef DEBUG_DMALLOC
25 26
 #include <dmalloc.h>
... ...
@@ -33,10 +34,10 @@
33 34
 #endif
34 35
 
35 36
 
36
-static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
37 37
 /* faster than inet_ntoa */
38
-__inline char* q_inet_itoa(unsigned long ip)
38
+static inline char* q_inet_itoa(unsigned long ip)
39 39
 {
40
+	static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
40 41
 	unsigned char* p;
41 42
 	unsigned char a,b,c;  /* abc.def.ghi.jkl */
42 43
 	int offset;
... ...
@@ -472,6 +473,9 @@ int forward_reply(struct sip_msg* msg)
472 473
 	char* orig;
473 474
 	char* buf;
474 475
 	unsigned int len;
476
+#ifdef DNS_IP_HACK
477
+	int err;
478
+#endif
475 479
 	
476 480
 
477 481
 	orig=msg->orig;
... ...
@@ -529,19 +533,30 @@ int forward_reply(struct sip_msg* msg)
529 533
 			msg->via2.host.s, 
530 534
 			(unsigned short)msg->via2.port,
531 535
 			new_buf);
532
-	/* fork? gethostbyname will probably block... */
533
-	he=gethostbyname(msg->via2.host.s);
534
-	if (he==0){
535
-		LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
536
-				msg->via2.host.s);
537
-		goto error;
538
-	}
539
-	to->sin_family = AF_INET;
540
-	to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
541
-	to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
536
+
537
+#ifdef DNS_IP_HACK
538
+	to->sin_addr.s_addr=str2ip(msg->via2.host.s, msg->via2.host.len, &err);
539
+	if (err==0){
540
+		to->sin_family = AF_INET;
541
+		to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
542
+	}else{
543
+#endif
544
+		/* fork? gethostbyname will probably block... */
545
+		he=gethostbyname(msg->via2.host.s);
546
+		if (he==0){
547
+			LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
548
+					msg->via2.host.s);
549
+			goto error;
550
+		}
551
+		to->sin_family = AF_INET;
552
+		to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
553
+		to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
542 554
 
543 555
 #ifdef STATS
544
-	stats.total_tx++;
556
+		stats.total_tx++;
557
+#endif
558
+#ifdef DNS_IP_HACK
559
+	}
545 560
 #endif
546 561
 	if (udp_send(new_buf,new_len, (struct sockaddr*) to, 
547 562
 					sizeof(struct sockaddr_in))==-1)
... ...
@@ -65,6 +65,9 @@ static char flags[]="NOCR:"
65 65
 #ifdef OLD_PARSER
66 66
 ", OLD_PARSER"
67 67
 #endif
68
+#ifdef DNS_IP_HACK
69
+", DNS_IP_HACK"
70
+#endif
68 71
 ;
69 72
 
70 73
 static char help_msg[]= "\
... ...
@@ -10,6 +10,7 @@
10 10
 
11 11
 #include "msg_parser.h"
12 12
 #include "parser_f.h"
13
+#include "ut.h"
13 14
 #include "error.h"
14 15
 #include "dprint.h"
15 16
 
... ...
@@ -290,7 +291,7 @@ error:
290 291
 char* parse_hostport(char* buf, str* host, short int* port)
291 292
 {
292 293
 	char *tmp;
293
-	char *invalid;
294
+	int err;
294 295
 	
295 296
 	host->s=buf;
296 297
 	for(tmp=buf;(*tmp)&&(*tmp!=':');tmp++);
... ...
@@ -299,12 +300,11 @@ char* parse_hostport(char* buf, str* host, short int* port)
299 300
 		*port=0;
300 301
 	}else{
301 302
 		*tmp=0;
302
-		invalid=0;
303
-		*port=strtol(tmp+1, &invalid, 10);
304
-		if ((invalid!=0)&&(*invalid)){
303
+		*port=str2s(tmp+1, strlen(tmp+1), &err);
304
+		if (err ){
305 305
 			LOG(L_INFO, 
306
-					"ERROR: hostport: trailing chars in port number: %s(%x)\n",
307
-					invalid, invalid);
306
+					"ERROR: hostport: trailing chars in port number: %s\n",
307
+					tmp+1);
308 308
 			/* report error? */
309 309
 		}
310 310
 	}
... ...
@@ -13,6 +13,10 @@
13 13
 #include <string.h>
14 14
 #include <stdlib.h>
15 15
 
16
+#ifdef DNS_IP_HACK
17
+#include "ut.h"
18
+#endif
19
+
16 20
 #ifdef DEBUG_DMALLOC
17 21
 #include <dmalloc.h>
18 22
 #endif
... ...
@@ -154,6 +158,11 @@ struct proxy_l* mk_proxy(char* name, unsigned short port)
154 158
 {
155 159
 	struct proxy_l* p;
156 160
 	struct hostent* he;
161
+#ifdef DNS_IP_HACK
162
+	int err;
163
+	unsigned int ip;
164
+	int len;
165
+#endif
157 166
 	
158 167
 	p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
159 168
 	if (p==0){
... ...
@@ -163,6 +172,43 @@ struct proxy_l* mk_proxy(char* name, unsigned short port)
163 172
 	memset(p,0,sizeof(struct proxy_l));
164 173
 	p->name=name;
165 174
 	p->port=port;
175
+#ifdef DNS_IP_HACK
176
+	len=strlen(name);
177
+	ip=str2ip(name, len, &err);
178
+	if (err==0){
179
+		p->host.h_name=malloc(len+1);
180
+		if (p->host.h_name==0) goto error;
181
+		memcpy(p->host.h_name, name, len);
182
+		p->host.h_aliases=malloc(sizeof(char*));
183
+		if (p->host.h_aliases==0) {
184
+			free(p->host.h_name);
185
+			goto error;
186
+		}
187
+		p->host.h_aliases[0]=0;
188
+		p->host.h_addrtype=AF_INET;
189
+		p->host.h_length=4;
190
+		p->host.h_addr_list=malloc(2*sizeof(char*));
191
+		if (p->host.h_addr_list==0){
192
+			free(p->host.h_name);
193
+			free(p->host.h_aliases);
194
+			goto error;
195
+		}
196
+		p->host.h_addr_list[1]=0;
197
+		p->host.h_addr_list[0]=malloc(5);
198
+		if (p->host.h_addr_list[0]==0){
199
+			free(p->host.h_name);
200
+			free(p->host.h_aliases);
201
+			free(p->host.h_addr_list);
202
+			goto error;
203
+		}
204
+		memcpy(p->host.h_addr_list[0], (char*)&ip, 4);
205
+		p->host.h_addr_list[0][4]=0;
206
+		
207
+		return p;
208
+	}
209
+#endif
210
+	/* fail over to normal lookup */
211
+	
166 212
 	he=gethostbyname(name);
167 213
 	if (he==0){
168 214
 		LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
... ...
@@ -89,7 +89,7 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
89 89
 		/* send the msg */
90 90
 		if (forward_reply(&msg)==0){
91 91
 			DBG(" reply forwarded to %s:%d\n", 
92
-						msg.via2.host,
92
+						msg.via2.host.s,
93 93
 						(unsigned short) msg.via2.port);
94 94
 		}
95 95
 	}