Browse code

- added aliases (names that ser will think are his own) - now the offical name will be put in sock_info[r] and the aliases in the. host_alias list. - added new script config. parameter: alias=

Andrei Pelinescu-Onciul authored on 20/08/2002 09:34:12
Showing 12 changed files
... ...
@@ -8,7 +8,7 @@
8 8
 VERSION = 0
9 9
 PATCHLEVEL = 8
10 10
 SUBLEVEL = 7
11
-EXTRAVERSION = -9-self
11
+EXTRAVERSION = -10-alias
12 12
 
13 13
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
14 14
 OS = $(shell uname -s)
... ...
@@ -99,7 +99,7 @@ DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
99 99
 	 -DADAPTIVE_WAIT -DADAPTIVE_WAIT_LOOPS=1024 \
100 100
 	 -DUSE_IPV6 \
101 101
 	 -DEXTRA_DEBUG \
102
-	 -DVQ_MALLOC  -DDBG_QM_MALLOC \
102
+	 -DDBG_QM_MALLOC \
103 103
 	 #-DF_MALLOC \
104 104
 	 #-DVQ_MALLOC  
105 105
 	 #-DCONTACT_BUG
... ...
@@ -91,6 +91,7 @@ DEBUG	debug
91 91
 FORK	fork
92 92
 LOGSTDERROR	log_stderror
93 93
 LISTEN		listen
94
+ALIAS		alias
94 95
 DNS		 dns
95 96
 REV_DNS	 rev_dns
96 97
 PORT	port
... ...
@@ -187,6 +188,7 @@ EAT_ABLE	[\ \t\b\r]
187 187
 <INITIAL>{FORK}		{ count(); yylval.strval=yytext; return FORK; }
188 188
 <INITIAL>{LOGSTDERROR}	{ yylval.strval=yytext; return LOGSTDERROR; }
189 189
 <INITIAL>{LISTEN}	{ count(); yylval.strval=yytext; return LISTEN; }
190
+<INITIAL>{ALIAS}	{ count(); yylval.strval=yytext; return ALIAS; }
190 191
 <INITIAL>{DNS}	{ count(); yylval.strval=yytext; return DNS; }
191 192
 <INITIAL>{REV_DNS}	{ count(); yylval.strval=yytext; return REV_DNS; }
192 193
 <INITIAL>{PORT}	{ count(); yylval.strval=yytext; return PORT; }
... ...
@@ -21,6 +21,7 @@
21 21
 #include "sr_module.h"
22 22
 #include "modparam.h"
23 23
 #include "ip_addr.h"
24
+#include "name_alias.h"
24 25
 
25 26
 #include "config.h"
26 27
 
... ...
@@ -88,6 +89,7 @@ void* f_tmp;
88 88
 %token FORK
89 89
 %token LOGSTDERROR
90 90
 %token LISTEN
91
+%token ALIAS
91 92
 %token DNS
92 93
 %token REV_DNS
93 94
 %token PORT
... ...
@@ -272,8 +274,33 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
272 272
 												"(max. %d).\n", MAX_LISTEN);
273 273
 								}
274 274
 							  }
275
+		| LISTEN EQUAL  host {
276
+								if (sock_no < MAX_LISTEN){
277
+									sock_info[sock_no].name.s=
278
+										(char*)malloc(strlen($3)+1);
279
+									if (sock_info[sock_no].name.s==0){
280
+										LOG(L_CRIT, "ERROR: cfg. parser:"
281
+													" out of memory.\n");
282
+									}else{
283
+										strncpy(sock_info[sock_no].name.s, $3,
284
+												strlen($3)+1);
285
+										sock_info[sock_no].name.len=strlen($3);
286
+										sock_info[sock_no].port_no=port_no;
287
+										sock_no++;
288
+									}
289
+								}else{
290
+									LOG(L_CRIT, "ERROR: cfg. parser: "
291
+												"too many listen addresses"
292
+												"(max. %d).\n", MAX_LISTEN);
293
+								}
294
+							  }
295
+		
275 296
 		| LISTEN EQUAL  error { yyerror("ip address or hostname"
276 297
 						"expected"); }
298
+		| ALIAS  EQUAL STRING { add_alias($3, strlen($3)); }
299
+		| ALIAS  EQUAL ID     { add_alias($3, strlen($3)); }
300
+		| ALIAS  EQUAL host   { add_alias($3, strlen($3)); }
301
+		| ALIAS  EQUAL error  { yyerror(" hostname expected"); }
277 302
 		| error EQUAL { yyerror("unknown config variable"); }
278 303
 	;
279 304
 
... ...
@@ -28,6 +28,7 @@
28 28
 #include "stats.h"
29 29
 #include "ip_addr.h"
30 30
 #include "resolve.h"
31
+#include "name_alias.h"
31 32
 
32 33
 #ifdef DEBUG_DMALLOC
33 34
 #include <dmalloc.h>
... ...
@@ -101,13 +102,17 @@ int check_self(str* host)
101 101
 			break;
102 102
 	}
103 103
 	if (r==sock_no){
104
-		DBG("check_self: host != me\n");
105
-		return 0;
104
+		/* try to look into the aliases*/
105
+		if (grep_aliases(host->s, host->len)==0){
106
+			DBG("check_self: host != me\n");
107
+			return 0;
108
+		}
106 109
 	}
107 110
 	return 1;
108 111
 }
109 112
 
110 113
 
114
+
111 115
 int forward_request( struct sip_msg* msg, struct proxy_l * p)
112 116
 {
113 117
 	unsigned int len;
... ...
@@ -37,6 +37,7 @@
37 37
 #include "parser/parse_hname2.h"
38 38
 #include "parser/digest/digest_parser.h"
39 39
 #include "fifo_server.h"
40
+#include "name_alias.h"
40 41
 
41 42
 
42 43
 #include "stats.h"
... ...
@@ -233,6 +234,8 @@ struct socket_info* sendipv6; /* same as above for ipv6 */
233 233
 
234 234
 unsigned short port_no=0; /* default port*/
235 235
 
236
+struct host_alias* aliases=0; /* name aliases list */
237
+
236 238
 /* ipc related globals */
237 239
 int process_no = 0;
238 240
 process_bm_t process_bit = 0;
... ...
@@ -659,6 +662,8 @@ int main(int argc, char** argv)
659 659
 	struct hostent* he;
660 660
 	int c,r;
661 661
 	char *tmp;
662
+	char** h;
663
+	struct host_alias* a;
662 664
 	struct utsname myname;
663 665
 	char *options;
664 666
 	char port_no_str[MAX_PORT_LEN];
... ...
@@ -933,9 +938,9 @@ int main(int argc, char** argv)
933 933
 			fprintf(stderr, "Out of memory.\n");
934 934
 			goto error;
935 935
 		}
936
-		strncpy(sock_info[sock_no].name.s, myname.nodename,
937
-				strlen(myname.nodename)+1);
938 936
 		sock_info[sock_no].name.len=strlen(myname.nodename);
937
+		strncpy(sock_info[sock_no].name.s, myname.nodename,
938
+				sock_info[sock_no].name.len+1);
939 939
 		sock_no++;
940 940
 	}
941 941
 
... ...
@@ -947,6 +952,27 @@ int main(int argc, char** argv)
947 947
 			DPrint("ERROR: could not resolve %s\n", sock_info[r].name.s);
948 948
 			goto error;
949 949
 		}
950
+		/* check if we got the official name */
951
+		if (strcasecmp(he->h_name, sock_info[r].name.s)!=0){
952
+			if (add_alias(sock_info[r].name.s, sock_info[r].name.len)<0){
953
+				LOG(L_ERR, "ERROR: main: add_alias failed\n");
954
+			}
955
+			/* change the oficial name */
956
+			free(sock_info[r].name.s);
957
+			sock_info[r].name.s=(char*)malloc(strlen(he->h_name)+1);
958
+			if (sock_info[r].name.s==0){
959
+				fprintf(stderr, "Out of memory.\n");
960
+				goto error;
961
+			}
962
+			sock_info[r].name.len=strlen(he->h_name);
963
+			strncpy(sock_info[r].name.s, he->h_name, sock_info[r].name.len+1);
964
+		}
965
+		/* add the aliases*/
966
+		for(h=he->h_aliases; h && *h; h++)
967
+			if (add_alias(*h, strlen(*h))<0){
968
+				LOG(L_ERR, "ERROR: main: add_alias failed\n");
969
+			}
970
+		
950 971
 		hostent2ip_addr(&sock_info[r].address, he, 0); /*convert to ip_addr 
951 972
 														 format*/
952 973
 		tmp=ip_addr2a(&sock_info[r].address);
... ...
@@ -987,6 +1013,9 @@ int main(int argc, char** argv)
987 987
 		printf("%s [%s]:%s\n",sock_info[r].name.s, sock_info[r].address_str.s,
988 988
 				sock_info[r].port_no_str.s);
989 989
 	}
990
+	printf("Aliases: ");
991
+	for(a=aliases; a; a=a->next) printf("%.*s ", a->alias.len, a->alias.s);
992
+	printf("\n");
990 993
 
991 994
 
992 995
 	
... ...
@@ -98,7 +98,7 @@ void vqm_debug_frag(struct vqm_block* qm, struct vqm_frag* f)
98 98
 		abort();
99 99
 	};
100 100
 	if (memcmp(f->end_check, END_CHECK_PATTERN, END_CHECK_PATTERN_LEN)!=0) {
101
-		LOG(L_CRIT, "BUG: vqm_*: fragm. %p end overwritten(%*s)!\n",
101
+		LOG(L_CRIT, "BUG: vqm_*: fragm. %p end overwritten(%.*s)!\n",
102 102
 				f, END_CHECK_PATTERN_LEN, f->end_check );
103 103
 		vqm_status(qm);
104 104
 		abort();
105 105
new file mode 100644
... ...
@@ -0,0 +1,61 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ */
4
+
5
+
6
+#include "str.h"
7
+#include "dprint.h"
8
+
9
+
10
+
11
+struct host_alias{
12
+	str alias;
13
+	struct host_alias* next;
14
+};
15
+
16
+
17
+extern struct host_alias* aliases;
18
+
19
+
20
+
21
+/* returns 1 if  name is in the alias list*/
22
+static inline int grep_aliases(char* name, int len)
23
+{
24
+	struct  host_alias* a;
25
+	
26
+	for(a=aliases;a;a=a->next)
27
+		if ((a->alias.len==len) && (strncasecmp(a->alias.s, name, len)==0))
28
+			return 1;
29
+	return 0;
30
+}
31
+
32
+
33
+
34
+/* adds an alias to the list (only if it isn't already there)
35
+ * returns 1 if a new alias was added, 0 if the alias was already on the list
36
+ * and  -1 on error */
37
+static inline int add_alias(char* name, int len)
38
+{
39
+	struct host_alias* a;
40
+	
41
+	if (grep_aliases(name,len)) return 0;
42
+	a=0;
43
+	a=(struct host_alias*)malloc(sizeof(struct host_alias));
44
+	if(a==0) goto error;
45
+	a->alias.s=(char*)malloc(len+1);
46
+	if (a->alias.s==0) goto error;
47
+	a->alias.len=len;
48
+	memcpy(a->alias.s, name, len);
49
+	a->alias.s[len]=0; /* null terminate for easier printing*/
50
+	a->next=aliases;
51
+	aliases=a;
52
+	return 1;
53
+error:
54
+	LOG(L_ERR, "ERROR: add_alias: memory allocation error\n");
55
+	if (a) free(a);
56
+	return -1;
57
+}
58
+
59
+
60
+
... ...
@@ -207,7 +207,7 @@ int parse_sip_msg_uri(struct sip_msg* msg)
207 207
 			tmp_len=msg->first_line.u.request.uri.len;
208 208
 		}
209 209
 		if (parse_uri(tmp, tmp_len, &msg->parsed_uri)<0){
210
-			LOG(L_ERR, "ERROR: parse_sip_msg_uri: bad uri <%*s>\n",
210
+			LOG(L_ERR, "ERROR: parse_sip_msg_uri: bad uri <%.*s>\n",
211 211
 						tmp_len, tmp);
212 212
 			msg->parsed_uri_ok=0;
213 213
 			return -1;
214 214
new file mode 100644
... ...
@@ -0,0 +1,89 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ */
4
+
5
+#include <stdio.h>
6
+#include <errno.h>
7
+#include <unistd.h>
8
+#include <netdb.h>
9
+
10
+
11
+
12
+
13
+static char* id="$Id$";
14
+static char* version="gethostbyaddr 0.1";
15
+static char* help_msg="\
16
+Usage: gethostbyaddr   [-hV] -n host\n\
17
+Options:\n\
18
+    -n host       host name\n\
19
+    -V            version number\n\
20
+    -h            this help message\n\
21
+";
22
+
23
+
24
+int main(int argc, char** argv)
25
+{
26
+	char c;
27
+	char* name;
28
+	struct hostent* he;
29
+	unsigned char** h;
30
+
31
+	name=0;
32
+	
33
+	opterr=0;
34
+	while ((c=getopt(argc, argv, "n:hV"))!=-1){
35
+		switch(c){
36
+			case 'n':
37
+				name=optarg;
38
+				break;
39
+			case 'V':
40
+				printf("version: %s\n", version);
41
+				printf("%s\n", id);
42
+				exit(0);
43
+				break;
44
+			case 'h':
45
+				printf("version: %s\n", version);
46
+				printf("%s", help_msg);
47
+				exit(0);
48
+				break;
49
+			case '?':
50
+				if (isprint(optopt))
51
+					fprintf(stderr, "Unknown option `-%c�\n", optopt);
52
+				else
53
+					fprintf(stderr, "Unknown character `\\x%x�\n", optopt);
54
+				goto error;
55
+			case ':':
56
+				fprintf(stderr, "Option `-%c� requires an argument.\n",
57
+						optopt);
58
+				goto error;
59
+				break;
60
+			default:
61
+				abort();
62
+		}
63
+	}
64
+	
65
+	if (name==0){
66
+		fprintf(stderr, "Missing domain name (-n name)\n");
67
+		goto error;
68
+	}
69
+	
70
+	he=gethostbyname(name);
71
+	if (he==0){
72
+			printf("bad adddress <%s>\n", name);
73
+			goto error;
74
+	}
75
+	he=gethostbyaddr(he->h_addr_list[0], he->h_length, he->h_addrtype); 
76
+	if (he==0) printf("no answer\n");
77
+	else{
78
+		printf("h_name=%s\n", he->h_name);
79
+		for(h=he->h_aliases;*h;h++)
80
+			printf("   alias=%s\n", *h);
81
+		for(h=he->h_addr_list;*h;h++)
82
+			printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
83
+	}
84
+	printf("done\n");
85
+	exit(0);
86
+error:
87
+	exit(-1);
88
+}
0 89
new file mode 100644
... ...
@@ -0,0 +1,84 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ */
4
+
5
+#include <stdio.h>
6
+#include <errno.h>
7
+#include <unistd.h>
8
+#include <netdb.h>
9
+
10
+
11
+
12
+
13
+static char* id="$Id$";
14
+static char* version="gethostbyname 0.1";
15
+static char* help_msg="\
16
+Usage: gethostbyname   [-hV] -n host\n\
17
+Options:\n\
18
+    -n host       host name\n\
19
+    -V            version number\n\
20
+    -h            this help message\n\
21
+";
22
+
23
+
24
+int main(int argc, char** argv)
25
+{
26
+	char c;
27
+	char* name;
28
+	struct hostent* he;
29
+	unsigned char** h;
30
+
31
+	name=0;
32
+	
33
+	opterr=0;
34
+	while ((c=getopt(argc, argv, "n:hV"))!=-1){
35
+		switch(c){
36
+			case 'n':
37
+				name=optarg;
38
+				break;
39
+			case 'V':
40
+				printf("version: %s\n", version);
41
+				printf("%s\n", id);
42
+				exit(0);
43
+				break;
44
+			case 'h':
45
+				printf("version: %s\n", version);
46
+				printf("%s", help_msg);
47
+				exit(0);
48
+				break;
49
+			case '?':
50
+				if (isprint(optopt))
51
+					fprintf(stderr, "Unknown option `-%c�\n", optopt);
52
+				else
53
+					fprintf(stderr, "Unknown character `\\x%x�\n", optopt);
54
+				goto error;
55
+			case ':':
56
+				fprintf(stderr, "Option `-%c� requires an argument.\n",
57
+						optopt);
58
+				goto error;
59
+				break;
60
+			default:
61
+				abort();
62
+		}
63
+	}
64
+	
65
+	if (name==0){
66
+		fprintf(stderr, "Missing domain name (-n name)\n");
67
+		goto error;
68
+	}
69
+	
70
+	he=gethostbyname(name);
71
+	if (he==0) printf("no answer\n");
72
+	else{
73
+		printf("h_name=%s\n", he->h_name);
74
+		for(h=he->h_aliases;*h;h++)
75
+			printf("   alias=%s\n", *h);
76
+		for(h=he->h_addr_list;*h;h++)
77
+			printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
78
+	}
79
+	printf("done\n");
80
+	exit(0);
81
+error:
82
+	exit(-1);
83
+}
... ...
@@ -1,4 +1,5 @@
1 1
 INVITE sip:andrei@iptel.org SIP/2.0
2
+:q
2 3
 Via: SIP/2.0/UDP dorian.fokus.gmd.de 
3 4
 From: "GMD FOKUS iptlab" <sip:jiri@iptel.org>;tag=b96b0300ed30f1286-2f5d
4 5
 Call-ID: b96b0300-88d30f-66da-63aa@195.37.78.190
... ...
@@ -24,6 +24,8 @@ rev_dns=off      # (cmd. line: -R)
24 24
 #listen=192.168.57.72
25 25
 #loop_checks=0
26 26
 # for more info: sip_router -h
27
+alias=iptel.org
28
+alias="foo.bar"
27 29
 
28 30
 #modules
29 31