Browse Source

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

Andrei Pelinescu-Onciul 24 years ago
parent
commit
104316b6bc
7 changed files with 95 additions and 24 deletions
  1. 4 2
      Makefile
  2. 7 2
      action.c
  3. 28 13
      forward.c
  4. 3 0
      main.c
  5. 6 6
      msg_parser.c
  6. 46 0
      proxy.c
  7. 1 1
      receive.c

+ 4 - 2
Makefile

@@ -27,7 +27,9 @@ NAME=ser
 # NO_LOG completely turns of all the logging (and DBG(...))
 # DEBUG compiles in some extra debugging code
 # OLD_PARSER uses the old and stable parser (from ser 8.3.2)
-DEFS=-DNOCR -DMACROEATER -DSTATS -DOLD_PARSER #-DNO_DEBUG #-DNO_LOG
+# DNS_IP_HACK faster ip address resolver for ip strings (e.g "127.0.0.1")
+DEFS=-DNOCR -DMACROEATER -DSTATS -DOLD_PARSER -DDNS_IP_HACK #-DNO_DEBUG 
+#-DNO_LOG
 
 PROFILE=  # -pg #set this if you want profiling
 
@@ -38,7 +40,7 @@ ARCH = $(shell uname -s)
 #common
 CC=gcc
 LD=gcc
-CFLAGS=-O2 -Wcast-align $(PROFILE)#-Wmissing-prototypes 
+CFLAGS=-O2 -Wcast-align $(PROFILE) -Winline#-Wmissing-prototypes 
 LDFLAGS=-Wl,-O2 -Wl,-E $(PROFILE)
 LEX=flex
 YACC=bison

+ 7 - 2
action.c

@@ -13,6 +13,7 @@
 #include "udp_server.h"
 #include "route.h"
 #include "msg_parser.h"
+#include "ut.h"
 #include "sr_module.h"
 
 #include <sys/types.h>
@@ -40,6 +41,7 @@ int do_action(struct action* a, struct sip_msg* msg)
 	char *new_uri, *end, *crt;
 	int len;
 	int user;
+	int err;
 	struct sip_uri uri;
 	unsigned short port;
 
@@ -67,8 +69,11 @@ int do_action(struct action* a, struct sip_msg* msg)
 				switch (a->p2_type){
 					case URIPORT_ST:
 									if (uri.port.s){
-										port=strtol(uri.port.s,&end,10);
-										if ((end)&&(*end)){
+									 /*port=strtol(uri.port.s,&end,10);*/
+										port=str2s(uri.port.s, uri.port.len,
+													&err);
+										/*if ((end)&&(*end)){*/
+										if (err){
 											LOG(L_ERR, "ERROR: do_action: "
 													"forward: bad port in "
 													"uri: <%s>\n", uri.port);

+ 28 - 13
forward.c

@@ -20,6 +20,7 @@
 #include "udp_server.h"
 #include "globals.h"
 #include "data_lump.h"
+#include "ut.h"
 
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
@@ -33,10 +34,10 @@
 #endif
 
 
-static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
 /* faster than inet_ntoa */
-__inline char* q_inet_itoa(unsigned long ip)
+static inline char* q_inet_itoa(unsigned long ip)
 {
+	static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
 	unsigned char* p;
 	unsigned char a,b,c;  /* abc.def.ghi.jkl */
 	int offset;
@@ -472,6 +473,9 @@ int forward_reply(struct sip_msg* msg)
 	char* orig;
 	char* buf;
 	unsigned int len;
+#ifdef DNS_IP_HACK
+	int err;
+#endif
 	
 
 	orig=msg->orig;
@@ -529,19 +533,30 @@ int forward_reply(struct sip_msg* msg)
 			msg->via2.host.s, 
 			(unsigned short)msg->via2.port,
 			new_buf);
-	/* fork? gethostbyname will probably block... */
-	he=gethostbyname(msg->via2.host.s);
-	if (he==0){
-		LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
-				msg->via2.host.s);
-		goto error;
-	}
-	to->sin_family = AF_INET;
-	to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
-	to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
+
+#ifdef DNS_IP_HACK
+	to->sin_addr.s_addr=str2ip(msg->via2.host.s, msg->via2.host.len, &err);
+	if (err==0){
+		to->sin_family = AF_INET;
+		to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
+	}else{
+#endif
+		/* fork? gethostbyname will probably block... */
+		he=gethostbyname(msg->via2.host.s);
+		if (he==0){
+			LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
+					msg->via2.host.s);
+			goto error;
+		}
+		to->sin_family = AF_INET;
+		to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
+		to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
 
 #ifdef STATS
-	stats.total_tx++;
+		stats.total_tx++;
+#endif
+#ifdef DNS_IP_HACK
+	}
 #endif
 	if (udp_send(new_buf,new_len, (struct sockaddr*) to, 
 					sizeof(struct sockaddr_in))==-1)

+ 3 - 0
main.c

@@ -65,6 +65,9 @@ static char flags[]="NOCR:"
 #ifdef OLD_PARSER
 ", OLD_PARSER"
 #endif
+#ifdef DNS_IP_HACK
+", DNS_IP_HACK"
+#endif
 ;
 
 static char help_msg[]= "\

+ 6 - 6
msg_parser.c

@@ -10,6 +10,7 @@
 
 #include "msg_parser.h"
 #include "parser_f.h"
+#include "ut.h"
 #include "error.h"
 #include "dprint.h"
 
@@ -290,7 +291,7 @@ error:
 char* parse_hostport(char* buf, str* host, short int* port)
 {
 	char *tmp;
-	char *invalid;
+	int err;
 	
 	host->s=buf;
 	for(tmp=buf;(*tmp)&&(*tmp!=':');tmp++);
@@ -299,12 +300,11 @@ char* parse_hostport(char* buf, str* host, short int* port)
 		*port=0;
 	}else{
 		*tmp=0;
-		invalid=0;
-		*port=strtol(tmp+1, &invalid, 10);
-		if ((invalid!=0)&&(*invalid)){
+		*port=str2s(tmp+1, strlen(tmp+1), &err);
+		if (err ){
 			LOG(L_INFO, 
-					"ERROR: hostport: trailing chars in port number: %s(%x)\n",
-					invalid, invalid);
+					"ERROR: hostport: trailing chars in port number: %s\n",
+					tmp+1);
 			/* report error? */
 		}
 	}

+ 46 - 0
proxy.c

@@ -13,6 +13,10 @@
 #include <string.h>
 #include <stdlib.h>
 
+#ifdef DNS_IP_HACK
+#include "ut.h"
+#endif
+
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
 #endif
@@ -154,6 +158,11 @@ struct proxy_l* mk_proxy(char* name, unsigned short port)
 {
 	struct proxy_l* p;
 	struct hostent* he;
+#ifdef DNS_IP_HACK
+	int err;
+	unsigned int ip;
+	int len;
+#endif
 	
 	p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
 	if (p==0){
@@ -163,6 +172,43 @@ struct proxy_l* mk_proxy(char* name, unsigned short port)
 	memset(p,0,sizeof(struct proxy_l));
 	p->name=name;
 	p->port=port;
+#ifdef DNS_IP_HACK
+	len=strlen(name);
+	ip=str2ip(name, len, &err);
+	if (err==0){
+		p->host.h_name=malloc(len+1);
+		if (p->host.h_name==0) goto error;
+		memcpy(p->host.h_name, name, len);
+		p->host.h_aliases=malloc(sizeof(char*));
+		if (p->host.h_aliases==0) {
+			free(p->host.h_name);
+			goto error;
+		}
+		p->host.h_aliases[0]=0;
+		p->host.h_addrtype=AF_INET;
+		p->host.h_length=4;
+		p->host.h_addr_list=malloc(2*sizeof(char*));
+		if (p->host.h_addr_list==0){
+			free(p->host.h_name);
+			free(p->host.h_aliases);
+			goto error;
+		}
+		p->host.h_addr_list[1]=0;
+		p->host.h_addr_list[0]=malloc(5);
+		if (p->host.h_addr_list[0]==0){
+			free(p->host.h_name);
+			free(p->host.h_aliases);
+			free(p->host.h_addr_list);
+			goto error;
+		}
+		memcpy(p->host.h_addr_list[0], (char*)&ip, 4);
+		p->host.h_addr_list[0][4]=0;
+		
+		return p;
+	}
+#endif
+	/* fail over to normal lookup */
+	
 	he=gethostbyname(name);
 	if (he==0){
 		LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"

+ 1 - 1
receive.c

@@ -89,7 +89,7 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
 		/* send the msg */
 		if (forward_reply(&msg)==0){
 			DBG(" reply forwarded to %s:%d\n", 
-						msg.via2.host,
+						msg.via2.host.s,
 						(unsigned short) msg.via2.port);
 		}
 	}