Browse code

- ipv6 support (-DUSE_IPV6) - changed all the sockaddr/ip addr structures - added gethostbyname/addr wrappers (resolve.h)

Andrei Pelinescu-Onciul authored on 26/05/2002 13:50:48
Showing 28 changed files
... ...
@@ -7,8 +7,8 @@
7 7
 #version number
8 8
 VERSION = 0
9 9
 PATCHLEVEL = 8
10
-SUBLEVEL = 7
11
-EXTRAVERSION = -7-ipaq-viadbg
10
+SUBLEVEL = 8
11
+EXTRAVERSION = -1-ipv6
12 12
 
13 13
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
14 14
 OS = $(shell uname -s)
... ...
@@ -74,9 +74,11 @@ ARCH = $(shell uname -m |sed -e s/i.86/i386/ -e s/sun4u/sparc64/ )
74 74
 # -DNEW_HNAME
75 75
 #		32-bit header name parsing; turn off for lower speed ;-) or debugging;
76 76
 #		to become non-optional if fast and stable
77
-# -SILENT_FR
77
+# -DSILENT_FR
78 78
 #		if defined, when FR timer hits (in tm) cancel is sent only if forking
79 79
 #		if used; otherwise, just delete the transaction without doing anything
80
+# -DUSE_IPV6
81
+#		compiles ipv6 support
80 82
 
81 83
 DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
82 84
 	 -DOS='"$(OS)"' -DCOMPILER='"$(CC_VER)"'\
... ...
@@ -85,10 +87,9 @@ DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
85 85
 	 -DSHM_MEM  -DSHM_MMAP \
86 86
 	 -DADAPTIVE_WAIT -DADAPTIVE_WAIT_LOOPS=1024 \
87 87
 	 -DWAIT -DNEW_HNAME \
88
-	 -DNOISY_REPLIES \
89
-	 -DVERY_NOISY_REPLIES\
90
-	 -DSILENT_FR \
91
- 	 #-DUSE_SYNONIM\
88
+	 -DUSE_IPV6 \
89
+	 #-DVERY_NOISY_REPLIES\
90
+	 #-DSILENT_FR \
92 91
 	 #-DNO_DEBUG \
93 92
 	 #-DADAPTIVE_WAIT -DADAPTIVE_WAIT_LOOPS=0 \
94 93
 	 #-DNOSMP \
... ...
@@ -102,8 +103,8 @@ DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
102 102
 
103 103
 
104 104
 #PROFILE=  -pg #set this if you want profiling
105
-mode = debug
106
-#mode = release
105
+#mode = debug
106
+mode = release
107 107
 
108 108
 # platform dependent settings
109 109
 
... ...
@@ -37,7 +37,7 @@ int do_action(struct action* a, struct sip_msg* msg)
37 37
 {
38 38
 	int ret;
39 39
 	int v;
40
-	struct sockaddr_in* to;
40
+	union sockaddr_union* to;
41 41
 	struct proxy_l* p;
42 42
 	char* tmp;
43 43
 	char *new_uri, *end, *crt;
... ...
@@ -77,8 +77,8 @@ int do_action(struct action* a, struct sip_msg* msg)
77 77
 										/*if ((end)&&(*end)){*/
78 78
 										if (err){
79 79
 											LOG(L_ERR, "ERROR: do_action: "
80
-													"forward: bad port in "
81
-													"uri: <%s>\n", uri.port.s);
80
+												"forward: bad port in "
81
+												"uri: <%s>\n", uri.port.s);
82 82
 											ret=E_UNSPEC;
83 83
 											goto error_fwd_uri;
84 84
 										}
... ...
@@ -116,24 +116,22 @@ int do_action(struct action* a, struct sip_msg* msg)
116 116
 			}
117 117
 			break;
118 118
 		case SEND_T:
119
-			to=(struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
120
-			if (to==0){
121
-				LOG(L_ERR, "ERROR: do_action: "
122
-							"memory allocation failure\n");
123
-				ret=E_OUT_OF_MEM;
124
-				break;
125
-			}
126 119
 			if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
127 120
 				LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
128 121
 						a->p1_type, a->p2_type);
129 122
 				ret=E_BUG;
130 123
 				break;
131 124
 			}
125
+			to=(union sockaddr_union*) malloc(sizeof(union sockaddr_union));
126
+			if (to==0){
127
+				LOG(L_ERR, "ERROR: do_action: "
128
+							"memory allocation failure\n");
129
+				ret=E_OUT_OF_MEM;
130
+				break;
131
+			}
132 132
 			
133 133
 			p=(struct proxy_l*)a->p1.data;
134 134
 			
135
-			to->sin_family = AF_INET;
136
-			to->sin_port=(p->port)?htons(p->port):htons(SIP_PORT);
137 135
 			if (p->ok==0){
138 136
 				if (p->host.h_addr_list[p->addr_idx+1])
139 137
 					p->addr_idx++;
... ...
@@ -141,15 +139,14 @@ int do_action(struct action* a, struct sip_msg* msg)
141 141
 					p->addr_idx=0;
142 142
 				p->ok=1;
143 143
 			}
144
-			memcpy(&(to->sin_addr.s_addr), p->host.h_addr_list[p->addr_idx],
145
-					sizeof(to->sin_addr.s_addr));
146
-			/*
147
-			to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);
148
-			*/
149
-			p->tx++;
150
-			p->tx_bytes+=msg->len;
151
-			ret=udp_send(msg->orig, msg->len, (struct sockaddr*)to,
152
-					sizeof(struct sockaddr_in));
144
+			ret=hostent2su(	to, &p->host, p->addr_idx,
145
+						(p->port)?htons(p->port):htons(SIP_PORT) );
146
+			if (ret==0){
147
+				p->tx++;
148
+				p->tx_bytes+=msg->len;
149
+				ret=udp_send(msg->orig, msg->len, to,
150
+								sizeof(union sockaddr_union));
151
+			}
153 152
 			free(to);
154 153
 			if (ret<0){
155 154
 				p->errors++;
... ...
@@ -8,6 +8,7 @@
8 8
 %{
9 9
 	#include "cfg.tab.h"
10 10
 	#include "dprint.h"
11
+	#include "globals.h"
11 12
 	#include <string.h>
12 13
 	#include <stdlib.h>
13 14
 
... ...
@@ -107,6 +108,9 @@ DIGIT		[0-9]
107 107
 ALPHANUM	{LETTER}|{DIGIT}|[_]
108 108
 NUMBER		{DIGIT}+
109 109
 ID			{LETTER}{ALPHANUM}*
110
+HEX			[0-9a-fA-F]
111
+HEX4		{HEX}{1,4}
112
+IPV6ADDR	({HEX4}":"){7}{HEX4}|({HEX4}":"){1,7}(":"{HEX4}){1,7}|":"(":"{HEX4}){1,7}|({HEX4}":"){1,7}":"|"::"
110 113
 QUOTES		\"
111 114
 TICK		\'
112 115
 SLASH		"/"
... ...
@@ -188,8 +192,10 @@ EAT_ABLE	[\ \t\b\r]
188 188
 <INITIAL>{AND}		{ count(); return AND; }
189 189
 <INITIAL>{OR}		{ count(); return OR;  }
190 190
 
191
-<INITIAL>{NUMBER}		{ count(); yylval.intval=atoi(yytext);
192
-							return NUMBER; }
191
+
192
+
193
+<INITIAL>{IPV6ADDR}		{ count(); yylval.strval=yytext; return IPV6ADDR; }
194
+<INITIAL>{NUMBER}		{ count(); yylval.intval=atoi(yytext);return NUMBER; }
193 195
 <INITIAL>{YES}			{ count(); yylval.intval=1; return NUMBER; }
194 196
 <INITIAL>{NO}			{ count(); yylval.intval=0; return NUMBER; }
195 197
 
... ...
@@ -13,12 +13,14 @@
13 13
 #include <netinet/in.h>
14 14
 #include <arpa/inet.h>
15 15
 #include <string.h>
16
+#include <errno.h>
16 17
 #include "route_struct.h"
17 18
 #include "globals.h"
18 19
 #include "route.h"
19 20
 #include "dprint.h"
20 21
 #include "sr_module.h"
21 22
 #include "modparam.h"
23
+#include "ip_addr.h"
22 24
 
23 25
 #include "config.h"
24 26
 
... ...
@@ -40,7 +42,8 @@ void* f_tmp;
40 40
 	char* strval;
41 41
 	struct expr* expr;
42 42
 	struct action* action;
43
-	struct net* net;
43
+	struct net* ipnet;
44
+	struct ip_addr* ipaddr;
44 45
 }
45 46
 
46 47
 /* terminals */
... ...
@@ -106,6 +109,7 @@ void* f_tmp;
106 106
 %token <intval> NUMBER
107 107
 %token <strval> ID
108 108
 %token <strval> STRING
109
+%token <strval> IPV6ADDR
109 110
 
110 111
 /* other */
111 112
 %token COMMA
... ...
@@ -124,8 +128,8 @@ void* f_tmp;
124 124
 /*non-terminals */
125 125
 %type <expr> exp, exp_elem /*, condition*/
126 126
 %type <action> action, actions, cmd, if_cmd, stm
127
-%type <uval> ipv4
128
-%type <net> net4
127
+%type <ipaddr> ipv4, ipv6, ip
128
+%type <ipnet> ipnet
129 129
 %type <strval> host
130 130
 /*%type <route_el> rules;
131 131
   %type <route_el> rule;
... ...
@@ -175,18 +179,25 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
175 175
 		| CHECK_VIA EQUAL error { yyerror("boolean value expected"); }
176 176
 		| LOOP_CHECKS EQUAL NUMBER { loop_checks=$3; }
177 177
 		| LOOP_CHECKS EQUAL error { yyerror("boolean value expected"); }
178
-		| LISTEN EQUAL ipv4  {
178
+		| LISTEN EQUAL ip  {
179 179
 								if (addresses_no < MAX_LISTEN){
180
-									tmp=inet_ntoa(*(struct in_addr*)&$3);
181
-									names[addresses_no]=
182
-												(char*)malloc(strlen(tmp)+1);
183
-									if (names[addresses_no]==0){
180
+									tmp=ip_addr2a($3);
181
+								/*	tmp=inet_ntoa(*(struct in_addr*)&$3);*/
182
+									if (tmp==0){
184 183
 										LOG(L_CRIT, "ERROR: cfg. parser: "
185
-														"out of memory.\n");
184
+											" bad ip address: %s\n",
185
+											strerror(errno));
186 186
 									}else{
187
-										strncpy(names[addresses_no], tmp,
188
-												strlen(tmp)+1);
189
-										addresses_no++;
187
+										names[addresses_no]=
188
+												(char*)malloc(strlen(tmp)+1);
189
+										if (names[addresses_no]==0){
190
+											LOG(L_CRIT, "ERROR: cfg. parser: "
191
+														"out of memory.\n");
192
+										}else{
193
+											strncpy(names[addresses_no], tmp,
194
+													strlen(tmp)+1);
195
+											addresses_no++;
196
+										}
190 197
 									}
191 198
 								}else{
192 199
 									LOG(L_CRIT, "ERROR: cfg. parser:"
... ...
@@ -255,21 +266,63 @@ module_stm:	LOADMODULE STRING	{ DBG("loading module %s\n", $2);
255 255
 		 ;
256 256
 
257 257
 
258
+ip:		 ipv4  { $$=$1; }
259
+		|ipv6  { $$=$1; }
260
+		;
261
+
258 262
 ipv4:	NUMBER DOT NUMBER DOT NUMBER DOT NUMBER { 
259
-											if (($1>255) || ($1<0) ||
260
-												($3>255) || ($3<0) ||
261
-												($5>255) || ($5<0) ||
262
-												($7>255) || ($7<0)){
263
-												yyerror("invalid ipv4"
264
-														"address");
265
-												$$=0;
263
+											$$=malloc(sizeof(struct ip_addr));
264
+											if ($$==0){
265
+												LOG(L_CRIT, "ERROR: cfg. "
266
+													"parser: out of memory.\n"
267
+													);
266 268
 											}else{
267
-												$$=htonl( ($1<<24)|
269
+												memset($$, 0, 
270
+													sizeof(struct ip_addr));
271
+												$$->af=AF_INET;
272
+												$$->len=4;
273
+												if (($1>255) || ($1<0) ||
274
+													($3>255) || ($3<0) ||
275
+													($5>255) || ($5<0) ||
276
+													($7>255) || ($7<0)){
277
+													yyerror("invalid ipv4"
278
+															"address");
279
+													$$->u.addr32[0]=0;
280
+													/* $$=0; */
281
+												}else{
282
+													$$->u.addr[0]=$1;
283
+													$$->u.addr[1]=$3;
284
+													$$->u.addr[2]=$5;
285
+													$$->u.addr[3]=$7;
286
+													/*
287
+													$$=htonl( ($1<<24)|
268 288
 													($3<<16)| ($5<<8)|$7 );
289
+													*/
290
+												}
269 291
 											}
270 292
 												}
271 293
 	;
272 294
 
295
+ipv6:	IPV6ADDR {
296
+					$$=malloc(sizeof(struct ip_addr));
297
+					if ($$==0){
298
+						LOG(L_CRIT, "ERROR: cfg. parser: out of memory.\n");
299
+					}else{
300
+						memset($$, 0, sizeof(struct ip_addr));
301
+						$$->af=AF_INET6;
302
+						$$->len=16;
303
+					#ifndef USE_IPV6
304
+						yyerror("ipv6 address & no ipv6 support compiled in");
305
+						YYABORT;
306
+					#endif
307
+						if (inet_pton(AF_INET6, $1, $$->u.addr)<=0){
308
+							yyerror("bad ipv6 address");
309
+						}
310
+					}
311
+				}
312
+	;
313
+
314
+
273 315
 route_stm:	ROUTE LBRACE actions RBRACE { push($3, &rlist[DEFAULT_RT]); }
274 316
 
275 317
 		| ROUTE LBRACK NUMBER RBRACK LBRACE actions RBRACE { 
... ...
@@ -343,7 +396,7 @@ exp_elem:	METHOD EQUAL_T STRING	{$$= mk_elem(	EQUAL_OP, STRING_ST,
343 343
 		| URI error	{ $$=0; yyerror("invalid operator,"
344 344
 				  					" == or =~ expected");
345 345
 					}
346
-		| SRCIP EQUAL_T net4	{ $$=mk_elem(	EQUAL_OP, NET_ST,
346
+		| SRCIP EQUAL_T ipnet	{ $$=mk_elem(	EQUAL_OP, NET_ST,
347 347
 												SRCIP_O, $3);
348 348
 								}
349 349
 		| SRCIP EQUAL_T STRING	{ $$=mk_elem(	EQUAL_OP, STRING_ST,
... ...
@@ -363,7 +416,7 @@ exp_elem:	METHOD EQUAL_T STRING	{$$= mk_elem(	EQUAL_OP, STRING_ST,
363 363
 		| SRCIP MATCH error  { $$=0; yyerror( "hostname expected"); }
364 364
 		| SRCIP error  { $$=0; 
365 365
 						 yyerror("invalid operator, == or =~ expected");}
366
-		| DSTIP EQUAL_T net4	{ $$=mk_elem(	EQUAL_OP, NET_ST,
366
+		| DSTIP EQUAL_T ipnet	{ $$=mk_elem(	EQUAL_OP, NET_ST,
367 367
 												DSTIP_O, $3);
368 368
 								}
369 369
 		| DSTIP EQUAL_T STRING	{ $$=mk_elem(	EQUAL_OP, STRING_ST,
... ...
@@ -387,18 +440,22 @@ exp_elem:	METHOD EQUAL_T STRING	{$$= mk_elem(	EQUAL_OP, STRING_ST,
387 387
 		| NUMBER		{$$=mk_elem( NO_OP, NUMBER_ST, NUMBER_O, (void*)$1 ); }
388 388
 	;
389 389
 
390
-net4:	ipv4 SLASH ipv4	{ $$=mk_net($1, $3); } 
391
-	| ipv4 SLASH NUMBER {	if (($3>32)|($3<0)){
390
+ipnet:	ip SLASH ip	{ $$=mk_net($1, $3); } 
391
+	| ip SLASH NUMBER 	{	if (($3<0) || ($3>$1->len*8)){
392 392
 								yyerror("invalid bit number in netmask");
393 393
 								$$=0;
394 394
 							}else{
395
+								$$=mk_net_bitlen($1, $3);
396
+							/*
395 397
 								$$=mk_net($1, 
396 398
 										htonl( ($3)?~( (1<<(32-$3))-1 ):0 ) );
399
+							*/
397 400
 							}
398 401
 						}
399
-	| ipv4				{ $$=mk_net($1, 0xffffffff); }
400
-	| ipv4 SLASH error { $$=0;
401
-						 yyerror("netmask (eg:255.0.0.0 or 8) expected");}
402
+	| ip				{ $$=mk_net_bitlen($1, $1->len*8); }
403
+	| ip SLASH error	{ $$=0;
404
+						 yyerror("netmask (eg:255.0.0.0 or 8) expected");
405
+						}
402 406
 	;
403 407
 
404 408
 host:	ID				{ $$=$1; }
... ...
@@ -462,7 +519,7 @@ cmd:		FORWARD LPAREN host RPAREN	{ $$=mk_action(	FORWARD_T,
462 462
 														$3,
463 463
 														0);
464 464
 										}
465
-		| FORWARD LPAREN ipv4 RPAREN	{ $$=mk_action(	FORWARD_T,
465
+		| FORWARD LPAREN ip RPAREN	{ $$=mk_action(	FORWARD_T,
466 466
 														IP_ST,
467 467
 														NUMBER_ST,
468 468
 														(void*)$3,
... ...
@@ -480,7 +537,7 @@ cmd:		FORWARD LPAREN host RPAREN	{ $$=mk_action(	FORWARD_T,
480 480
 																$3,
481 481
 																(void*)$5);
482 482
 													}
483
-		| FORWARD LPAREN ipv4 COMMA NUMBER RPAREN { $$=mk_action(FORWARD_T,
483
+		| FORWARD LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(FORWARD_T,
484 484
 																 IP_ST,
485 485
 																 NUMBER_ST,
486 486
 																 (void*)$3,
... ...
@@ -524,7 +581,7 @@ cmd:		FORWARD LPAREN host RPAREN	{ $$=mk_action(	FORWARD_T,
524 524
 													$3,
525 525
 													0);
526 526
 									}
527
-		| SEND LPAREN ipv4 RPAREN	{ $$=mk_action(	SEND_T,
527
+		| SEND LPAREN ip RPAREN		{ $$=mk_action(	SEND_T,
528 528
 													IP_ST,
529 529
 													NUMBER_ST,
530 530
 													(void*)$3,
... ...
@@ -542,7 +599,7 @@ cmd:		FORWARD LPAREN host RPAREN	{ $$=mk_action(	FORWARD_T,
542 542
 																$3,
543 543
 																(void*)$5);
544 544
 												}
545
-		| SEND LPAREN ipv4 COMMA NUMBER RPAREN { $$=mk_action(	SEND_T,
545
+		| SEND LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(	SEND_T,
546 546
 																IP_ST,
547 547
 																NUMBER_ST,
548 548
 																(void*)$3,
... ...
@@ -8,7 +8,6 @@
8 8
 
9 9
 #include <syslog.h>
10 10
 
11
-#include "globals.h"
12 11
 
13 12
 #define L_ALERT -3
14 13
 #define L_CRIT  -2
... ...
@@ -18,6 +17,10 @@
18 18
 #define L_INFO   3
19 19
 #define L_DBG    4
20 20
 
21
+/* vars:*/
22
+
23
+extern int debug;
24
+extern int log_stderr;
21 25
 
22 26
 
23 27
 #define DPRINT_LEV	1
... ...
@@ -9,7 +9,7 @@
9 9
 enum { FL_WHITE=1, FL_YELLOW, FL_GREEN, FL_RED, FL_BLUE, FL_MAGENTA,
10 10
 	   FL_BROWN, FL_BLACK, FL_ACC, FL_MAX };
11 11
 
12
-typedef unsigned long flag_t;
12
+typedef unsigned int flag_t;
13 13
 
14 14
 #define MAX_FLAG  ( sizeof(flag_t) * CHAR_BIT - 1 )
15 15
 
... ...
@@ -25,6 +25,8 @@
25 25
 #include "msg_translator.h"
26 26
 #include "sr_module.h"
27 27
 #include "stats.h"
28
+#include "ip_addr.h"
29
+#include "resolve.h"
28 30
 
29 31
 #ifdef DEBUG_DMALLOC
30 32
 #include <dmalloc.h>
... ...
@@ -36,7 +38,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
36 36
 {
37 37
 	unsigned int len;
38 38
 	char* buf;
39
-	struct sockaddr_in* to;
39
+	union sockaddr_union* to;
40 40
 
41 41
 	to=0;
42 42
 	buf = build_req_buf_from_sip_req( msg, &len);
... ...
@@ -45,7 +47,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
45 45
 		goto error;
46 46
 	}
47 47
 
48
-	to=(struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
48
+	to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
49 49
 	if (to==0){
50 50
 		LOG(L_ERR, "ERROR: forward_request: out of memory\n");
51 51
 		goto error;
... ...
@@ -55,26 +57,20 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
55 55
 	DBG("Sending:\n%s.\n", buf);
56 56
 	DBG("orig. len=%d, new_len=%d\n", msg->len, len );
57 57
 
58
-	to->sin_family = AF_INET;
59
-	to->sin_port = (p->port)?htons(p->port):htons(SIP_PORT);
60 58
 	/* if error try next ip address if possible */
61 59
 	if (p->ok==0){
62 60
 		if (p->host.h_addr_list[p->addr_idx+1])
63 61
 			p->addr_idx++;
62
+		else p->addr_idx=0;
64 63
 		p->ok=1;
65 64
 	}
66
-	
67
-	memcpy(&(to->sin_addr.s_addr), p->host.h_addr_list[p->addr_idx],
68
-			sizeof(to->sin_addr.s_addr));
69
-	/* 
70
-	to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);
71
-	*/
72 65
 
66
+	hostent2su(to, &p->host, p->addr_idx, 
67
+				(p->port)?htons(p->port):htons(SIP_PORT));
73 68
 	p->tx++;
74 69
 	p->tx_bytes+=len;
75 70
 
76
-	if (udp_send( buf, len, (struct sockaddr*) to,
77
-				sizeof(struct sockaddr_in))==-1){
71
+	if (udp_send( buf, len,  to, sizeof(union sockaddr_union))==-1){
78 72
 			p->errors++;
79 73
 			p->ok=0;
80 74
 			STATS_TX_DROPS;
... ...
@@ -93,43 +89,50 @@ error:
93 93
 }
94 94
 
95 95
 
96
-int update_sock_struct_from_via( struct sockaddr_in* to,  struct via_body* via )
96
+int update_sock_struct_from_via( union sockaddr_union* to,  
97
+								 struct via_body* via )
97 98
 {
98 99
 	int err;
99 100
 	struct hostent* he;
101
+	unsigned int ip;
100 102
 	char *host_copy;
101 103
 
102
-	to->sin_family = AF_INET;
103
-	to->sin_port = (via->port)?htons(via->port): htons(SIP_PORT);
104 104
 
105 105
 #ifdef DNS_IP_HACK
106
-	to->sin_addr.s_addr=str2ip((unsigned char*)via->host.s,via->host.len,&err);
107
-	if (err)
106
+	ip=str2ip((unsigned char*)via->host.s,via->host.len,&err);
107
+	if (err==0){
108
+		to->sin.sin_family=AF_INET;
109
+		to->sin.sin_port=(via->port)?htons(via->port): htons(SIP_PORT);
110
+		memcpy(&to->sin.sin_addr, (char*)&ip, 4);
111
+	}else
108 112
 #endif
109 113
 	{
110
-		/* fork? gethostbyname will probably block... */
111 114
 		/* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
112
-		   strings; -jiri */
113
-		if (!(host_copy=pkg_malloc( via->host.len+1 ))) {
114
-			LOG(L_NOTICE, "ERROR: update_sock_struct_from_via: not enough memory\n");
115
-			return -1;
115
+		   strings; -jiri 
116
+		   but only if host is not null terminated
117
+		   (host.s[len] will always be ok for a via)
118
+           BTW: when is via->host.s non null terminated? tm copy?
119
+		   - andrei 
120
+		*/
121
+		if (via->host.s[via->host.len]){
122
+			if (!(host_copy=pkg_malloc( via->host.len+1 ))) {
123
+				LOG(L_NOTICE, "ERROR: update_sock_struct_from_via: not enough memory\n");
124
+				return -1;
125
+			}
126
+			memcpy(host_copy, via->host.s, via->host.len );
127
+			host_copy[via->host.len]=0;
128
+			he=resolvehost(host_copy);
129
+			pkg_free( host_copy );
130
+		}else{
131
+			he=resolvehost(via->host.s);
116 132
 		}
117
-		memcpy(host_copy, via->host.s, via->host.len );
118
-		host_copy[via->host.len]=0;
119
-		he=gethostbyname(host_copy);
120
-		/* he=gethostbyname(via->host.s); */
121
-		pkg_free( host_copy );
122 133
 
123 134
 		if (he==0){
124 135
 			LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
125 136
 					via->host.s);
126 137
 			return -1;
127 138
 		}
128
-		memcpy(&(to->sin_addr.s_addr), he->h_addr_list[0], 
129
-				sizeof(to->sin_addr.s_addr));
130
-		/*
131
-		to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
132
-		*/
139
+		hostent2su(to, he, 0, (via->port)?htons(via->port): htons(SIP_PORT));
133 140
 	}
134 141
 	return 1;
135 142
 }
... ...
@@ -140,7 +143,7 @@ int forward_reply(struct sip_msg* msg)
140 140
 {
141 141
 	int  r;
142 142
 	char* new_buf;
143
-	struct sockaddr_in* to;
143
+	union sockaddr_union* to;
144 144
 	unsigned int new_len;
145 145
 	struct sr_module *mod;
146 146
 	
... ...
@@ -158,7 +161,6 @@ int forward_reply(struct sip_msg* msg)
158 158
 		}
159 159
 	}
160 160
 
161
-	/* here will be called the T Module !!!!!!  */
162 161
 	/* quick hack, slower for mutliple modules*/
163 162
 	for (mod=modules;mod;mod=mod->next){
164 163
 		if ((mod->exports) && (mod->exports->response_f)){
... ...
@@ -176,7 +178,7 @@ int forward_reply(struct sip_msg* msg)
176 176
 		goto error;
177 177
 	}
178 178
 
179
-	to=(struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
179
+	to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
180 180
 	if (to==0){
181 181
 		LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
182 182
 		goto error;
... ...
@@ -190,8 +192,8 @@ int forward_reply(struct sip_msg* msg)
190 190
 
191 191
 	if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
192 192
 
193
-	if (udp_send(new_buf,new_len, (struct sockaddr*) to,
194
-					sizeof(struct sockaddr_in))==-1)
193
+	if (udp_send(new_buf,new_len,  to,
194
+				sizeof(union sockaddr_union))==-1)
195 195
 	{
196 196
 		STATS_TX_DROPS;
197 197
 		goto error;
... ...
@@ -12,7 +12,8 @@
12 12
 
13 13
 
14 14
 int forward_request( struct sip_msg* msg,  struct proxy_l* p);
15
-int update_sock_struct_from_via( struct sockaddr_in* to,  struct via_body* via );
15
+int update_sock_struct_from_via( union sockaddr_union* to,
16
+								struct via_body* via );
16 17
 int forward_reply( struct sip_msg* msg);
17 18
 
18 19
 #endif
... ...
@@ -10,6 +10,7 @@
10 10
 #define globals_h
11 11
 
12 12
 #include "types.h"
13
+#include "ip_addr.h"
13 14
 
14 15
 #define NO_DNS     0
15 16
 #define DO_DNS     1
... ...
@@ -24,17 +25,17 @@ extern int port_no_str_len;
24 24
 extern unsigned int maxbuffer;
25 25
 extern char * names[];
26 26
 extern int names_len[];
27
-extern unsigned int addresses[];
27
+extern struct ip_addr addresses[];
28 28
 extern int addresses_no;
29
-extern unsigned int bind_address;
29
+extern struct ip_addr* bind_address;
30 30
 extern int children_no;
31
-extern int debug;
32 31
 extern int dont_fork;
33
-extern int log_stderr;
34 32
 extern int check_via;
35 33
 extern int received_dns;
36 34
 extern int loop_checks;
37 35
 extern int process_no;
36
+/*
37
+ * debug & log_stderr moved to dprint.h*/
38 38
 
39 39
 extern process_bm_t process_bit;
40 40
 extern int *pids;
41 41
new file mode 100644
... ...
@@ -0,0 +1,126 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ *
4
+ * ip address & address family related functions
5
+ */
6
+
7
+#include <stdio.h>
8
+
9
+#include "ip_addr.h"
10
+#include "dprint.h"
11
+
12
+
13
+struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask)
14
+{
15
+	struct net* n;
16
+	
17
+	if ((ip->af != mask->af) || (ip->len != mask->len)){
18
+		LOG(L_CRIT, "ERROR: mk_net: trying to use a different mask family"
19
+				" (eg. ipv4/ipv6mask or ipv6/ipv4mask)\n");
20
+		goto error;
21
+	}
22
+	n=(struct net*)malloc(sizeof(struct net));
23
+	if (n==0){ 
24
+		LOG(L_CRIT, "ERROR: mk_net: memory allocation failure\n");
25
+		goto error;
26
+	}
27
+	n->ip=*ip;
28
+	n->mask=*mask;
29
+	return n;
30
+error:
31
+	return 0;
32
+}
33
+
34
+
35
+
36
+struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
37
+{
38
+	struct net* n;
39
+	int r;
40
+	
41
+	if (bitlen>ip->len*8){
42
+		LOG(L_CRIT, "ERROR: mk_net_bitlen: bad bitlen number %d\n", bitlen);
43
+		goto error;
44
+	}
45
+	n=(struct net*)malloc(sizeof(struct net));
46
+	if (n==0){
47
+		LOG(L_CRIT, "ERROR: mk_net_bitlen: memory allocation failure\n"); 
48
+		goto error;
49
+	}
50
+	memset(n,0, sizeof(struct net));
51
+	n->ip=*ip;
52
+	for (r=0;r<bitlen/8;r++) n->mask.u.addr[r]=0xff;
53
+	if (bitlen%8) n->mask.u.addr[r]=  ~((1<<(8-(bitlen%8)))-1);
54
+	n->mask.af=ip->af;
55
+	n->mask.len=ip->len;
56
+	
57
+	return n;
58
+error:
59
+	return 0;
60
+}
61
+
62
+
63
+
64
+void print_ip(struct ip_addr* ip)
65
+{
66
+	switch(ip->af){
67
+		case AF_INET:
68
+			DBG("%d.%d.%d.%d",	ip->u.addr[0],
69
+								ip->u.addr[1],
70
+								ip->u.addr[2],
71
+								ip->u.addr[3]);
72
+			break;
73
+		case AF_INET6:
74
+			DBG("%x:%x:%x:%x:%x:%x:%x:%x",	htons(ip->u.addr16[0]),
75
+											htons(ip->u.addr16[1]),
76
+											htons(ip->u.addr16[2]),
77
+											htons(ip->u.addr16[3]),
78
+											htons(ip->u.addr16[4]),
79
+											htons(ip->u.addr16[5]),
80
+											htons(ip->u.addr16[6]),
81
+											htons(ip->u.addr16[7])
82
+				);
83
+			break;
84
+		default:
85
+			DBG("print_ip: warning unknown adress family %d\n", ip->af);
86
+	}
87
+}
88
+
89
+
90
+
91
+void stdout_print_ip(struct ip_addr* ip)
92
+{
93
+	switch(ip->af){
94
+		case AF_INET:
95
+			printf("%d.%d.%d.%d",	ip->u.addr[0],
96
+								ip->u.addr[1],
97
+								ip->u.addr[2],
98
+								ip->u.addr[3]);
99
+			break;
100
+		case AF_INET6:
101
+			printf("%x:%x:%x:%x:%x:%x:%x:%x",	htons(ip->u.addr16[0]),
102
+											htons(ip->u.addr16[1]),
103
+											htons(ip->u.addr16[2]),
104
+											htons(ip->u.addr16[3]),
105
+											htons(ip->u.addr16[4]),
106
+											htons(ip->u.addr16[5]),
107
+											htons(ip->u.addr16[6]),
108
+											htons(ip->u.addr16[7])
109
+				);
110
+			break;
111
+		default:
112
+			DBG("print_ip: warning unknown adress family %d\n", ip->af);
113
+	}
114
+}
115
+
116
+
117
+
118
+void print_net(struct net* net)
119
+{
120
+	if (net==0){
121
+		LOG(L_WARN, "ERROR: print net: null pointer\n");
122
+		return;
123
+	}
124
+	print_ip(&net->ip); DBG("/"); print_ip(&net->mask);
125
+}
0 126
new file mode 100644
... ...
@@ -0,0 +1,336 @@
0
+/* $Id$
1
+ *
2
+ * ip address family realted structures
3
+ */
4
+
5
+#ifndef ip_addr_h
6
+#define ip_addr_h
7
+
8
+#include <string.h>
9
+#include <netinet/in.h>
10
+#include <netdb.h>
11
+
12
+#ifdef USE_IPV6
13
+	#ifdef FreeBSD			/* freebsd is brain damaged and needs a different
14
+							   include */
15
+#include <netinet6/in6.h>
16
+	#endif
17
+#endif
18
+
19
+#include "dprint.h"
20
+
21
+
22
+
23
+struct ip_addr{
24
+	unsigned int af; /* address family: AF_INET6 or AF_INET */
25
+	unsigned int len;    /* address len, 16 or 4 */
26
+	
27
+	/* 64 bits alligned address */
28
+	union {
29
+		unsigned int   addr32[4];
30
+		unsigned short addr16[8];
31
+		unsigned char  addr[16];
32
+	}u;
33
+};
34
+
35
+
36
+
37
+struct net{
38
+	struct ip_addr ip;
39
+	struct ip_addr mask;
40
+};
41
+
42
+union sockaddr_union{
43
+		struct sockaddr     s;
44
+		struct sockaddr_in  sin;
45
+	#ifdef USE_IPV6
46
+		struct sockaddr_in6 sin6;
47
+	#endif
48
+};
49
+
50
+
51
+
52
+
53
+/* inits an ip_addr with the addr. info from a hostent structure
54
+ * ip = struct ip_addr*
55
+ * he= struct hostent*
56
+ */
57
+#define hostent2ip_addr(ip, he, addr_no) \
58
+	do{ \
59
+		(ip)->af=(he)->h_addrtype; \
60
+		(ip)->len=(he)->h_length;  \
61
+		memcpy((ip)->u.addr, (he)->h_addr_list[(addr_no)], (ip)->len); \
62
+	}while(0)
63
+	
64
+
65
+
66
+
67
+/* gets the protocol family corresponding to a specific address family
68
+ * ( PF_INET - AF_INET, PF_INET6 - AF_INET6, af for others)
69
+ */
70
+#ifdef USE_IPV6
71
+#define AF2PF(af)   (((af)==AF_INET)?PF_INET:((af)==AF_INET6)?PF_INET6:(af))
72
+#else
73
+#define AF2PF(af)   (((af)==AF_INET)?PF_INET:(af))
74
+#endif
75
+
76
+
77
+
78
+
79
+struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask);
80
+struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen);
81
+
82
+void print_ip(struct ip_addr* ip);
83
+void stdout_print_ip(struct ip_addr* ip);
84
+void print_net(struct net* net);
85
+
86
+
87
+
88
+
89
+/* returns 1 if ip & net.mask == net.ip ; 0 otherwise & -1 on error 
90
+	[ diff. adress fams ]) */
91
+inline static int matchnet(struct ip_addr* ip, struct net* net)
92
+{
93
+	int r;
94
+	int ret;
95
+	
96
+	ret=-1;
97
+	if (ip->af == net->ip.af){
98
+		for(r=0; r<ip->len/4; r++){ /* ipv4 & ipv6 addresses are
99
+									   all multiple of 4*/
100
+			if ((ip->u.addr32[r]&net->mask.u.addr32[r])!=
101
+														 net->ip.u.addr32[r]){
102
+				return 0;
103
+			}
104
+		}
105
+		return 1;
106
+	};
107
+	return -1;
108
+}
109
+
110
+
111
+
112
+/* inits an ip_addr pointer from a sockaddr_union ip address */
113
+static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
114
+{
115
+	switch(su->s.sa_family){
116
+	case AF_INET: 
117
+					ip->af=AF_INET;
118
+					ip->len=4;
119
+					memcpy(ip->u.addr, &su->sin.sin_addr, 4);
120
+					break;
121
+#ifdef USE_IPV6
122
+	case AF_INET6:
123
+					ip->af=AF_INET6;
124
+					ip->len=16;
125
+					memcpy(ip->u.addr, &su->sin6.sin6_addr, 16);
126
+					break;
127
+#endif
128
+	default:
129
+					LOG(L_CRIT,"su2ip_addr: BUG: unknown address family %d\n",
130
+							su->s.sa_family);
131
+	}
132
+}
133
+
134
+
135
+
136
+/* inits a struct sockaddr_union from a struct ip_addr and a port no 
137
+ * returns 0 if ok, -1 on error (unknown address family) */
138
+static inline int init_su( union sockaddr_union* su,
139
+							struct ip_addr* ip,
140
+							unsigned short   port ) 
141
+{
142
+	su->s.sa_family=ip->af;
143
+	switch(ip->af){
144
+#ifdef USE_IPV6
145
+	case	AF_INET6:
146
+		memcpy(&su->sin6.sin6_addr, ip->u.addr, ip->len); 
147
+		#ifdef FreeBSD
148
+			su->sin6.sin6_len=sizeof(struct sockaddr_in6);
149
+		#endif
150
+		su->sin6.sin6_port=port;
151
+		break;
152
+#endif
153
+	case AF_INET:
154
+		memcpy(&su->sin.sin_addr, ip->u.addr, ip->len);
155
+		#ifdef FreeBSD
156
+			su->sin.sin_len=sizeof(struct sockaddr_in);
157
+		#endif
158
+		su->sin.sin_port=port;
159
+		break;
160
+	default:
161
+		LOG(L_CRIT, "init_ss: BUG: unknown address family %d\n", ip->af);
162
+		return -1;
163
+	}
164
+	return 0;
165
+}
166
+
167
+
168
+
169
+/* inits a struct sockaddr_union from a struct hostent, an address index int
170
+ * the hostent structure and a port no.
171
+ * WARNING: no index overflow  checks!
172
+ * returns 0 if ok, -1 on error (unknown address family) */
173
+static inline int hostent2su( union sockaddr_union* su,
174
+								struct hostent* he,
175
+								unsigned int idx,
176
+								unsigned short   port ) 
177
+{
178
+	su->s.sa_family=he->h_addrtype;
179
+	switch(he->h_addrtype){
180
+#ifdef USE_IPV6
181
+	case	AF_INET6:
182
+		memcpy(&su->sin6.sin6_addr, he->h_addr_list[idx], he->h_length);
183
+		#ifdef FreeBSD
184
+			su->sin6.sin6_len=sizeof(struct sockaddr_in6);
185
+		#endif
186
+		su->sin6.sin6_port=port;
187
+		break;
188
+#endif
189
+	case AF_INET:
190
+		memcpy(&su->sin.sin_addr, he->h_addr_list[idx], he->h_length);
191
+		#ifdef FreeBSD
192
+			su->sin.sin_len=sizeof(struct sockaddr_in);
193
+		#endif
194
+		su->sin.sin_port=port;
195
+		break;
196
+	default:
197
+		LOG(L_CRIT, "hostent2su: BUG: unknown address family %d\n", 
198
+				he->h_addrtype);
199
+		return -1;
200
+	}
201
+	return 0;
202
+}
203
+
204
+
205
+
206
+/* fast ip_addr -> string convertor;
207
+ * it uses an internal buffer
208
+ */
209
+static inline char* ip_addr2a(struct ip_addr* ip)
210
+{
211
+
212
+	static char buff[40];/* 1234:5678:9012:3456:7890:1234:5678:9012\0 */
213
+	int offset;
214
+	register unsigned char a,b,c;
215
+#ifdef USE_IPV6
216
+	register unsigned char d;
217
+#endif
218
+	int r;
219
+	#define HEXDIG(x) (((x)>=10)?(x)-10+'A':(x)+'0')
220
+	
221
+	
222
+	offset=0;
223
+	switch(ip->af){
224
+	#ifdef USE_IPV6
225
+		case AF_INET6:
226
+			for(r=0;r<7;r++){
227
+				a=ip->u.addr16[r]>>12;
228
+				b=(ip->u.addr16[r]>>8)&0xf;
229
+				c=(ip->u.addr16[r]>>4)&0xf;
230
+				d=ip->u.addr16[r]&0xf;
231
+				if (a){
232
+					buff[offset]=HEXDIG(a);
233
+					buff[offset+1]=HEXDIG(b);
234
+					buff[offset+2]=HEXDIG(c);
235
+					buff[offset+3]=HEXDIG(d);
236
+					buff[offset+4]=':';
237
+					offset+=5;
238
+				}else if(b){
239
+					buff[offset]=HEXDIG(b);
240
+					buff[offset+1]=HEXDIG(c);
241
+					buff[offset+2]=HEXDIG(d);
242
+					buff[offset+3]=':';
243
+					offset+=4;
244
+				}else if(c){
245
+					buff[offset]=HEXDIG(c);
246
+					buff[offset+1]=HEXDIG(d);
247
+					buff[offset+2]=':';
248
+					offset+=3;
249
+				}else{
250
+					buff[offset]=HEXDIG(d);
251
+					buff[offset+1]=':';
252
+					offset+=2;
253
+				}
254
+			}
255
+			/* last int16*/
256
+			a=ip->u.addr16[r]>>12;
257
+			b=(ip->u.addr16[r]>>8)&0xf;
258
+			c=(ip->u.addr16[r]>>4)&0xf;
259
+			d=ip->u.addr16[r]&0xf;
260
+			if (a){
261
+				buff[offset]=HEXDIG(a);
262
+				buff[offset+1]=HEXDIG(b);
263
+				buff[offset+2]=HEXDIG(c);
264
+				buff[offset+3]=HEXDIG(d);
265
+				buff[offset+4]=0;
266
+			}else if(b){
267
+				buff[offset]=HEXDIG(b);
268
+				buff[offset+1]=HEXDIG(c);
269
+				buff[offset+2]=HEXDIG(d);
270
+				buff[offset+3]=0;
271
+			}else if(c){
272
+				buff[offset]=HEXDIG(c);
273
+				buff[offset+1]=HEXDIG(d);
274
+				buff[offset+2]=0;
275
+			}else{
276
+				buff[offset]=HEXDIG(d);
277
+				buff[offset+1]=0;
278
+			}
279
+			break;
280
+	#endif
281
+		case AF_INET:
282
+			for(r=0;r<3;r++){
283
+				a=ip->u.addr[r]/100;
284
+				c=ip->u.addr[r]%10;
285
+				b=ip->u.addr[r]%100/10;
286
+				if (a){
287
+					buff[offset]=a+'0';
288
+					buff[offset+1]=b+'0';
289
+					buff[offset+2]=c+'0';
290
+					buff[offset+3]='.';
291
+					offset+=4;
292
+				}else if (b){
293
+					buff[offset]=b+'0';
294
+					buff[offset+1]=c+'0';
295
+					buff[offset+2]='.';
296
+					offset+=3;
297
+				}else{
298
+					buff[offset]=c+'0';
299
+					buff[offset+1]='.';
300
+					offset+=2;
301
+				}
302
+			}
303
+			/* last number */
304
+			a=ip->u.addr[r]/100;
305
+			c=ip->u.addr[r]%10;
306
+			b=ip->u.addr[r]%100/10;
307
+			if (a){
308
+				buff[offset]=a+'0';
309
+				buff[offset+1]=b+'0';
310
+				buff[offset+2]=c+'0';
311
+				buff[offset+3]=0;
312
+			}else if (b){
313
+				buff[offset]=b+'0';
314
+				buff[offset+1]=c+'0';
315
+				buff[offset+2]=0;
316
+			}else{
317
+				buff[offset]=c+'0';
318
+				buff[offset+1]=0;
319
+			}
320
+			break;
321
+		
322
+		default:
323
+			LOG(L_CRIT, "BUG: ip_addr2a: unknown address family %d\n",
324
+					ip->af);
325
+			return 0;
326
+	}
327
+	
328
+	return buff;
329
+}
330
+
331
+
332
+
333
+
334
+
335
+#endif
... ...
@@ -18,6 +18,7 @@
18 18
 #include <sys/fcntl.h>
19 19
 #include <sys/time.h>
20 20
 #include <sys/wait.h>
21
+#include <signal.h>
21 22
 
22 23
 #include "config.h"
23 24
 #include "dprint.h"
... ...
@@ -31,9 +32,10 @@
31 31
 #include "sr_module.h"
32 32
 #include "timer.h"
33 33
 #include "parser/msg_parser.h"
34
+#include "ip_addr.h"
35
+#include "resolve.h"
34 36
 
35 37
 
36
-#include <signal.h>
37 38
 
38 39
 #include "stats.h"
39 40
 
... ...
@@ -51,6 +53,9 @@ static char flags[]=
51 51
 #else
52 52
 "Off"
53 53
 #endif
54
+#ifdef USE_IPV6
55
+", USE_IPV6"
56
+#endif
54 57
 #ifdef NO_DEBUG
55 58
 ", NO_DEBUG"
56 59
 #endif
... ...
@@ -142,7 +147,7 @@ Options:\n\
142 142
     -h           This help message\n\
143 143
     -b nr        Maximum receive buffer size which will not be exceeded by\n\
144 144
                  auto-probing procedure even if  OS allows\n\
145
-	-m nr        Size of shared memory allocated in Megabytes\n\
145
+    -m nr        Size of shared memory allocated in Megabytes\n\
146 146
     -w  dir      change the working directory to \"dir\" (default \"/\")\n\
147 147
     -t  dir      chroot to \"dir\"\n\
148 148
     -u uid       change uid \n\
... ...
@@ -193,12 +198,13 @@ char* cfg_file = 0;
193 193
 unsigned short port_no = 0; /* port on which we listen */
194 194
 char port_no_str[MAX_PORT_LEN];
195 195
 int port_no_str_len=0;
196
-unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do not want to exceed
197
-				      		durig the auto-probing procedure; may be
198
-				      		re-configured */
199
-int children_no = 0;           /* number of children processing requests */
200
-int *pids=0;		       /*array with childrens pids, 0= main proc,
201
-				alloc'ed in shared mem if possible*/
196
+unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
197
+												  not want to exceed durig the
198
+												  auto-probing procedure; may 
199
+												  be re-configured */
200
+int children_no = 0;			/* number of children processing requests */
201
+int *pids=0;					/*array with childrens pids, 0= main proc,
202
+									alloc'ed in shared mem if possible*/
202 203
 int debug = 0;
203 204
 int dont_fork = 0;
204 205
 int log_stderr = 0;
... ...
@@ -213,9 +219,9 @@ int gid = 0;
213 213
 
214 214
 char* names[MAX_LISTEN];              /* our names */
215 215
 int names_len[MAX_LISTEN];            /* lengths of the names*/
216
-unsigned int addresses[MAX_LISTEN];   /* our ips */
216
+struct ip_addr addresses[MAX_LISTEN]; /* our ips */
217 217
 int addresses_no=0;                   /* number of names/ips */
218
-unsigned int bind_address=0;          /* listen address of the crt. process */
218
+struct ip_addr* bind_address;        /* listen address of the crt. process */
219 219
 
220 220
 /* ipc related globals */
221 221
 int process_no = 0;
... ...
@@ -373,7 +379,7 @@ int main_loop()
373 373
 		setstats( 0 );
374 374
 #endif
375 375
 		/* only one address */
376
-		if (udp_init(addresses[0],port_no)==-1) goto error;
376
+		if (udp_init(&addresses[0],port_no)==-1) goto error;
377 377
 
378 378
 		/* we need another process to act as the timer*/
379 379
 		if (timer_list){
... ...
@@ -414,7 +420,7 @@ int main_loop()
414 414
 	}else{
415 415
 		for(r=0;r<addresses_no;r++){
416 416
 			/* create the listening socket (for each address)*/
417
-			if (udp_init(addresses[r], port_no)==-1) goto error;
417
+			if (udp_init(&addresses[r], port_no)==-1) goto error;
418 418
 			for(i=0;i<children_no;i++){
419 419
 				if ((pid=fork())<0){
420 420
 					LOG(L_CRIT,  "main_loop: Cannot fork\n");
... ...
@@ -784,8 +790,8 @@ int main(int argc, char** argv)
784 784
 		fprintf(stderr, "ERROR: bad port number: %d\n", port_no);
785 785
 		goto error;
786 786
 	}
787
-	/* on some system snprintf return really strange things if it does not have
788
-	 * enough space */
787
+	/* on some system snprintf return really strange things if it does not 
788
+	   have  enough space */
789 789
 	port_no_str_len=
790 790
 				(port_no_str_len<MAX_PORT_LEN)?port_no_str_len:MAX_PORT_LEN;
791 791
 
... ...
@@ -836,16 +842,17 @@ int main(int argc, char** argv)
836 836
 	/* get ips */
837 837
 	printf("Listening on ");
838 838
 	for (r=0; r<addresses_no;r++){
839
-		he=gethostbyname(names[r]);
839
+		he=resolvehost(names[r]);
840 840
 		if (he==0){
841 841
 			DPrint("ERROR: could not resolve %s\n", names[r]);
842 842
 			goto error;
843 843
 		}
844
-		memcpy(&addresses[r], he->h_addr_list[0], sizeof(int));
844
+		hostent2ip_addr(&addresses[r], he, 0); /*convert to ip_addr format*/
845
+		/*memcpy(&addresses[r], he->h_addr_list[0], sizeof(int));*/
845 846
 		/*addresses[r]=*((long*)he->h_addr_list[0]);*/
846
-		printf("%s [%s] : %d\n",names[r],
847
-				inet_ntoa(*(struct in_addr*)&addresses[r]),
848
-				(unsigned short)port_no);
847
+		printf("%s [",names[r]);
848
+		stdout_print_ip(&addresses[r]);
849
+		printf("]:%d\n", (unsigned short)port_no);
849 850
 	}
850 851
 
851 852
 #ifdef STATS
... ...
@@ -7,6 +7,7 @@
7 7
 
8 8
 #include "shm_mem.h"
9 9
 #include "../config.h"
10
+#include "../globals.h"
10 11
 
11 12
 #ifdef  SHM_MMAP
12 13
 
... ...
@@ -23,6 +24,8 @@
23 23
 #endif
24 24
 
25 25
 
26
+
27
+
26 28
 /* define semun */
27 29
 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
28 30
 	/* union semun is defined by including <sys/sem.h> */
... ...
@@ -9,11 +9,14 @@
9 9
 #include <stdio.h>
10 10
 
11 11
 #include "msg_translator.h"
12
+#include "globals.h"
12 13
 #include "mem/mem.h"
13 14
 #include "dprint.h"
14 15
 #include "config.h"
15 16
 #include "md5utils.h"
16 17
 #include "data_lump_rpl.h"
18
+#include "ip_addr.h"
19
+#include "resolve.h"
17 20
 
18 21
 
19 22
 
... ...
@@ -40,88 +43,35 @@
40 40
 extern char version[];
41 41
 extern int version_len;
42 42
 
43
-/* faster than inet_ntoa */
44
-static inline char* q_inet_itoa(unsigned long ip)
45
-{
46
-	static char q_inet_itoa_buf[16]; /* 123.567.901.345\0 */
47
-	unsigned char* p;
48
-	unsigned char a,b,c;  /* abc.def.ghi.jkl */
49
-	int offset;
50
-	int r;
51
-	p=(unsigned char*)&ip;
52
-
53
-	offset=0;
54
-	/* unrolled loops (faster)*/
55
-	for(r=0;r<3;r++){
56
-		a=p[r]/100;
57
-		c=p[r]%10;
58
-		b=p[r]%100/10;
59
-		if (a){
60
-			q_inet_itoa_buf[offset]=a+'0';
61
-			q_inet_itoa_buf[offset+1]=b+'0';
62
-			q_inet_itoa_buf[offset+2]=c+'0';
63
-			q_inet_itoa_buf[offset+3]='.';
64
-			offset+=4;
65
-		}else if (b){
66
-			q_inet_itoa_buf[offset]=b+'0';
67
-			q_inet_itoa_buf[offset+1]=c+'0';
68
-			q_inet_itoa_buf[offset+2]='.';
69
-			offset+=3;
70
-		}else{
71
-			q_inet_itoa_buf[offset]=c+'0';
72
-			q_inet_itoa_buf[offset+1]='.';
73
-			offset+=2;
74
-		}
75
-	}
76
-	/* last number */
77
-	a=p[r]/100;
78
-	c=p[r]%10;
79
-	b=p[r]%100/10;
80
-	if (a){
81
-		q_inet_itoa_buf[offset]=a+'0';
82
-		q_inet_itoa_buf[offset+1]=b+'0';
83
-		q_inet_itoa_buf[offset+2]=c+'0';
84
-		q_inet_itoa_buf[offset+3]=0;
85
-	}else if (b){
86
-		q_inet_itoa_buf[offset]=b+'0';
87
-		q_inet_itoa_buf[offset+1]=c+'0';
88
-		q_inet_itoa_buf[offset+2]=0;
89
-	}else{
90
-		q_inet_itoa_buf[offset]=c+'0';
91
-		q_inet_itoa_buf[offset+1]=0;
92
-	}
93
-
94
-	return q_inet_itoa_buf;
95
-}
96
-
97
-
98 43
 
99 44
 
100 45
 /* checks if ip is in host(name) and ?host(ip)=name?
101 46
  * ip must be in network byte order!
102 47
  *  resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
103 48
  * return 0 if equal */
104
-int check_address(unsigned long ip, char *name, int resolver)
49
+int check_address(struct ip_addr* ip, char *name, int resolver)
105 50
 {
106 51
 	struct hostent* he;
107 52
 	int i;
108 53
 
109 54
 	/* maybe we are lucky and name it's an ip */
110
-	if (strcmp(name, q_inet_itoa(ip))==0)
55
+	if (strcmp(name, ip_addr2a(ip))==0)
111 56
 		return 0;
112 57
 	if (resolver&DO_DNS){
113 58
 		DBG("check_address: doing dns lookup\n");
114 59
 		/* try all names ips */
115
-		he=gethostbyname(name);
116
-		for(i=0;he && he->h_addr_list[i];i++){
117
-			if (*(unsigned long*)he->h_addr_list[i]==ip)
118
-				return 0;
60
+		he=resolvehost(name);
61
+		if (ip->af==he->h_addrtype){
62
+			for(i=0;he && he->h_addr_list[i];i++){
63
+				if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
64
+					return 0;
65
+			}
119 66
 		}
120 67
 	}
121 68
 	if (resolver&DO_REV_DNS){
122 69
 		DBG("check_address: doing rev. dns lookup\n");
123 70
 		/* try reverse dns */
124
-		he=gethostbyaddr((char*)&ip, sizeof(ip), AF_INET);
71
+		he=rev_resolvehost(ip);
125 72
 		if (he && (strcmp(he->h_name, name)==0))
126 73
 			return 0;
127 74
 		for (i=0; he && he->h_aliases[i];i++){
... ...
@@ -241,7 +191,7 @@ char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
241 241
 	/*adding src_ip*/
242 242
 	if (p-buf+26+2>=MAX_WARNING_LEN)
243 243
 		goto done;
244
-	p += sprintf(p,"req_src_ip=%s",q_inet_itoa(msg->src_ip));
244
+	p += sprintf(p,"req_src_ip=%s",ip_addr2a(&msg->src_ip));
245 245
 	*(p++)=' ';
246 246
 
247 247
 	/*adding in_uri*/
... ...
@@ -274,7 +224,7 @@ done:
274 274
 char * build_req_buf_from_sip_req( struct sip_msg* msg,
275 275
 								unsigned int *returned_len)
276 276
 {
277
-	unsigned int len, new_len, received_len, uri_len, via_len;
277
+	unsigned int len, new_len, received_len, uri_len, via_len, extra_len;
278 278
 	char* line_buf;
279 279
 	char* received_buf;
280 280
 	char* tmp;
... ...
@@ -284,7 +234,7 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
284 284
 	char* buf;
285 285
 	char  backup;
286 286
 	unsigned int offset, s_offset, size;
287
-	unsigned long source_ip;
287
+	struct ip_addr* source_ip;
288 288
 	struct lump *t,*r;
289 289
 	struct lump* anchor;
290 290
 
... ...
@@ -292,10 +242,11 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
292 292
 	orig=msg->orig;
293 293
 	buf=msg->buf;
294 294
 	len=msg->len;
295
-	source_ip=msg->src_ip;
295
+	source_ip=&msg->src_ip;
296 296
 	received_len=0;
297 297
 	new_buf=0;
298 298
 	received_buf=0;
299
+	extra_len=0;
299 300
 
300 301
 
301 302
 	line_buf = via_builder( msg, &via_len );
... ...
@@ -318,10 +269,17 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
318 318
 								inet_ntoa(*(struct in_addr *)&source_ip));
319 319
 		*/
320 320
 		memcpy(received_buf, RECEIVED, RECEIVED_LEN);
321
-		tmp=q_inet_itoa( /* *(struct in_addr *)& */source_ip);
321
+		tmp=ip_addr2a(source_ip);
322 322
 		tmp_len=strlen(tmp);
323 323
 		received_len=RECEIVED_LEN+tmp_len;
324
-		memcpy(received_buf+RECEIVED_LEN, tmp, tmp_len);
324
+		if(source_ip->af==AF_INET6){
325
+			received_len+=2;
326
+			received_buf[RECEIVED_LEN]='[';
327
+			received_buf[RECEIVED_LEN+tmp_len+1]=']';
328
+			extra_len=1;
329
+		}
330
+		
331
+		memcpy(received_buf+RECEIVED_LEN+extra_len, tmp, tmp_len);
325 332
 		received_buf[received_len]=0; /*null terminate it */
326 333
 	}
327 334
 	msg->via1->host.s[msg->via1->host.len] = backup;
... ...
@@ -777,9 +777,7 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
777 777
 	
778 778
 error:
779 779
 	/* more debugging, msg->orig is/should be null terminated*/
780
-	LOG(L_ERR, "ERROR: parse_msg: ip source=%x dest=%x; message=<%s>\n",
781
-			msg->src_ip, msg->dst_ip,
782
-			msg->orig);
780
+	LOG(L_ERR, "ERROR: parse_msg: message=<%s>\n", msg->orig);
783 781
 	return -1;
784 782
 }
785 783
 
... ...
@@ -8,6 +8,7 @@
8 8
 #include "../str.h"
9 9
 #include "../data_lump.h"
10 10
 #include "../flags.h"
11
+#include "../ip_addr.h"
11 12
 
12 13
 #define SIP_REQUEST 1
13 14
 #define SIP_REPLY   2
... ...
@@ -209,8 +210,8 @@ struct sip_msg{
209 209
 	char* eoh; /* pointer to the end of header (if found) or null */
210 210
 	char* unparsed; /* here we stopped parsing*/
211 211
 
212
-	unsigned int src_ip;
213
-	unsigned int dst_ip;
212
+	struct ip_addr src_ip;
213
+	struct ip_addr dst_ip;
214 214
 	char* orig; /* original message copy */
215 215
 	char* buf;  /* scratch pad, holds a modfied message,
216 216
 				   via, etc. point into it */
... ...
@@ -1284,8 +1284,7 @@ main_via:
1284