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 38
 #common
39 39
 CC=gcc
40 40
 LD=gcc
41
-CFLAGS=-O2 -Wcast-align $(PROFILE)#-Wmissing-prototypes 
41
+CFLAGS=-O2 -Wcast-align $(PROFILE) -Winline#-Wmissing-prototypes 
42 42
 LDFLAGS=-Wl,-O2 -Wl,-E $(PROFILE)
43 43
 LEX=flex
44 44
 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 40
 	char *new_uri, *end, *crt;
41 41
 	int len;
42 42
 	int user;
43
+	int err;
43 44
 	struct sip_uri uri;
44 45
 	unsigned short port;
45 46
 
... ...
@@ -67,8 +69,11 @@ int do_action(struct action* a, struct sip_msg* msg)
67 67
 				switch (a->p2_type){
68 68
 					case URIPORT_ST:
69 69
 									if (uri.port.s){
70
-										port=strtol(uri.port.s,&end,10);
71
-										if ((end)&&(*end)){
70
+									 /*port=strtol(uri.port.s,&end,10);*/
71
+										port=str2s(uri.port.s, uri.port.len,
72
+													&err);
73
+										/*if ((end)&&(*end)){*/
74
+										if (err){
72 75
 											LOG(L_ERR, "ERROR: do_action: "
73 76
 													"forward: bad port in "
74 77
 													"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 33
 #endif
34 34
 
35 35
 
36
-static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
37 36
 /* faster than inet_ntoa */
38
-__inline char* q_inet_itoa(unsigned long ip)
37
+static inline char* q_inet_itoa(unsigned long ip)
39 38
 {
39
+	static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
40 40
 	unsigned char* p;
41 41
 	unsigned char a,b,c;  /* abc.def.ghi.jkl */
42 42
 	int offset;
... ...
@@ -472,6 +473,9 @@ int forward_reply(struct sip_msg* msg)
472 472
 	char* orig;
473 473
 	char* buf;
474 474
 	unsigned int len;
475
+#ifdef DNS_IP_HACK
476
+	int err;
477
+#endif
475 478
 	
476 479
 
477 480
 	orig=msg->orig;
... ...
@@ -529,19 +533,30 @@ int forward_reply(struct sip_msg* msg)
529 529
 			msg->via2.host.s, 
530 530
 			(unsigned short)msg->via2.port,
531 531
 			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]);
532
+
533
+#ifdef DNS_IP_HACK
534
+	to->sin_addr.s_addr=str2ip(msg->via2.host.s, msg->via2.host.len, &err);
535
+	if (err==0){
536
+		to->sin_family = AF_INET;
537
+		to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
538
+	}else{
539
+#endif
540
+		/* fork? gethostbyname will probably block... */
541
+		he=gethostbyname(msg->via2.host.s);
542
+		if (he==0){
543
+			LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
544
+					msg->via2.host.s);
545
+			goto error;
546
+		}
547
+		to->sin_family = AF_INET;
548
+		to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
549
+		to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
542 550
 
543 551
 #ifdef STATS
544
-	stats.total_tx++;
552
+		stats.total_tx++;
553
+#endif
554
+#ifdef DNS_IP_HACK
555
+	}
545 556
 #endif
546 557
 	if (udp_send(new_buf,new_len, (struct sockaddr*) to, 
547 558
 					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 290
 char* parse_hostport(char* buf, str* host, short int* port)
291 291
 {
292 292
 	char *tmp;
293
-	char *invalid;
293
+	int err;
294 294
 	
295 295
 	host->s=buf;
296 296
 	for(tmp=buf;(*tmp)&&(*tmp!=':');tmp++);
... ...
@@ -299,12 +300,11 @@ char* parse_hostport(char* buf, str* host, short int* port)
299 299
 		*port=0;
300 300
 	}else{
301 301
 		*tmp=0;
302
-		invalid=0;
303
-		*port=strtol(tmp+1, &invalid, 10);
304
-		if ((invalid!=0)&&(*invalid)){
302
+		*port=str2s(tmp+1, strlen(tmp+1), &err);
303
+		if (err ){
305 304
 			LOG(L_INFO, 
306
-					"ERROR: hostport: trailing chars in port number: %s(%x)\n",
307
-					invalid, invalid);
305
+					"ERROR: hostport: trailing chars in port number: %s\n",
306
+					tmp+1);
308 307
 			/* report error? */
309 308
 		}
310 309
 	}
... ...
@@ -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 154
 {
155 155
 	struct proxy_l* p;
156 156
 	struct hostent* he;
157
+#ifdef DNS_IP_HACK
158
+	int err;
159
+	unsigned int ip;
160
+	int len;
161
+#endif
157 162
 	
158 163
 	p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
159 164
 	if (p==0){
... ...
@@ -163,6 +172,43 @@ struct proxy_l* mk_proxy(char* name, unsigned short port)
163 163
 	memset(p,0,sizeof(struct proxy_l));
164 164
 	p->name=name;
165 165
 	p->port=port;
166
+#ifdef DNS_IP_HACK
167
+	len=strlen(name);
168
+	ip=str2ip(name, len, &err);
169
+	if (err==0){
170
+		p->host.h_name=malloc(len+1);
171
+		if (p->host.h_name==0) goto error;
172
+		memcpy(p->host.h_name, name, len);
173
+		p->host.h_aliases=malloc(sizeof(char*));
174
+		if (p->host.h_aliases==0) {
175
+			free(p->host.h_name);
176
+			goto error;
177
+		}
178
+		p->host.h_aliases[0]=0;
179
+		p->host.h_addrtype=AF_INET;
180
+		p->host.h_length=4;
181
+		p->host.h_addr_list=malloc(2*sizeof(char*));
182
+		if (p->host.h_addr_list==0){
183
+			free(p->host.h_name);
184
+			free(p->host.h_aliases);
185
+			goto error;
186
+		}
187
+		p->host.h_addr_list[1]=0;
188
+		p->host.h_addr_list[0]=malloc(5);
189
+		if (p->host.h_addr_list[0]==0){
190
+			free(p->host.h_name);
191
+			free(p->host.h_aliases);
192
+			free(p->host.h_addr_list);
193
+			goto error;
194
+		}
195
+		memcpy(p->host.h_addr_list[0], (char*)&ip, 4);
196
+		p->host.h_addr_list[0][4]=0;
197
+		
198
+		return p;
199
+	}
200
+#endif
201
+	/* fail over to normal lookup */
202
+	
166 203
 	he=gethostbyname(name);
167 204
 	if (he==0){
168 205
 		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
 	}