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 188
 <INITIAL>{FORK}		{ count(); yylval.strval=yytext; return FORK; }
188 189
 <INITIAL>{LOGSTDERROR}	{ yylval.strval=yytext; return LOGSTDERROR; }
189 190
 <INITIAL>{LISTEN}	{ count(); yylval.strval=yytext; return LISTEN; }
191
+<INITIAL>{ALIAS}	{ count(); yylval.strval=yytext; return ALIAS; }
190 192
 <INITIAL>{DNS}	{ count(); yylval.strval=yytext; return DNS; }
191 193
 <INITIAL>{REV_DNS}	{ count(); yylval.strval=yytext; return REV_DNS; }
192 194
 <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 89
 %token FORK
89 90
 %token LOGSTDERROR
90 91
 %token LISTEN
92
+%token ALIAS
91 93
 %token DNS
92 94
 %token REV_DNS
93 95
 %token PORT
... ...
@@ -272,8 +274,33 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
272 274
 												"(max. %d).\n", MAX_LISTEN);
273 275
 								}
274 276
 							  }
277
+		| LISTEN EQUAL  host {
278
+								if (sock_no < MAX_LISTEN){
279
+									sock_info[sock_no].name.s=
280
+										(char*)malloc(strlen($3)+1);
281
+									if (sock_info[sock_no].name.s==0){
282
+										LOG(L_CRIT, "ERROR: cfg. parser:"
283
+													" out of memory.\n");
284
+									}else{
285
+										strncpy(sock_info[sock_no].name.s, $3,
286
+												strlen($3)+1);
287
+										sock_info[sock_no].name.len=strlen($3);
288
+										sock_info[sock_no].port_no=port_no;
289
+										sock_no++;
290
+									}
291
+								}else{
292
+									LOG(L_CRIT, "ERROR: cfg. parser: "
293
+												"too many listen addresses"
294
+												"(max. %d).\n", MAX_LISTEN);
295
+								}
296
+							  }
297
+		
275 298
 		| LISTEN EQUAL  error { yyerror("ip address or hostname"
276 299
 						"expected"); }
300
+		| ALIAS  EQUAL STRING { add_alias($3, strlen($3)); }
301
+		| ALIAS  EQUAL ID     { add_alias($3, strlen($3)); }
302
+		| ALIAS  EQUAL host   { add_alias($3, strlen($3)); }
303
+		| ALIAS  EQUAL error  { yyerror(" hostname expected"); }
277 304
 		| error EQUAL { yyerror("unknown config variable"); }
278 305
 	;
279 306
 
... ...
@@ -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 102
 			break;
102 103
 	}
103 104
 	if (r==sock_no){
104
-		DBG("check_self: host != me\n");
105
-		return 0;
105
+		/* try to look into the aliases*/
106
+		if (grep_aliases(host->s, host->len)==0){
107
+			DBG("check_self: host != me\n");
108
+			return 0;
109
+		}
106 110
 	}
107 111
 	return 1;
108 112
 }
109 113
 
110 114
 
115
+
111 116
 int forward_request( struct sip_msg* msg, struct proxy_l * p)
112 117
 {
113 118
 	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 234
 
234 235
 unsigned short port_no=0; /* default port*/
235 236
 
237
+struct host_alias* aliases=0; /* name aliases list */
238
+
236 239
 /* ipc related globals */
237 240
 int process_no = 0;
238 241
 process_bm_t process_bit = 0;
... ...
@@ -659,6 +662,8 @@ int main(int argc, char** argv)
659 662
 	struct hostent* he;
660 663
 	int c,r;
661 664
 	char *tmp;
665
+	char** h;
666
+	struct host_alias* a;
662 667
 	struct utsname myname;
663 668
 	char *options;
664 669
 	char port_no_str[MAX_PORT_LEN];
... ...
@@ -933,9 +938,9 @@ int main(int argc, char** argv)
933 938
 			fprintf(stderr, "Out of memory.\n");
934 939
 			goto error;
935 940
 		}
936
-		strncpy(sock_info[sock_no].name.s, myname.nodename,
937
-				strlen(myname.nodename)+1);
938 941
 		sock_info[sock_no].name.len=strlen(myname.nodename);
942
+		strncpy(sock_info[sock_no].name.s, myname.nodename,
943
+				sock_info[sock_no].name.len+1);
939 944
 		sock_no++;
940 945
 	}
941 946
 
... ...
@@ -947,6 +952,27 @@ int main(int argc, char** argv)
947 952
 			DPrint("ERROR: could not resolve %s\n", sock_info[r].name.s);
948 953
 			goto error;
949 954
 		}
955
+		/* check if we got the official name */
956
+		if (strcasecmp(he->h_name, sock_info[r].name.s)!=0){
957
+			if (add_alias(sock_info[r].name.s, sock_info[r].name.len)<0){
958
+				LOG(L_ERR, "ERROR: main: add_alias failed\n");
959
+			}
960
+			/* change the oficial name */
961
+			free(sock_info[r].name.s);
962
+			sock_info[r].name.s=(char*)malloc(strlen(he->h_name)+1);
963
+			if (sock_info[r].name.s==0){
964
+				fprintf(stderr, "Out of memory.\n");
965
+				goto error;
966
+			}
967
+			sock_info[r].name.len=strlen(he->h_name);
968
+			strncpy(sock_info[r].name.s, he->h_name, sock_info[r].name.len+1);
969
+		}
970
+		/* add the aliases*/
971
+		for(h=he->h_aliases; h && *h; h++)
972
+			if (add_alias(*h, strlen(*h))<0){
973
+				LOG(L_ERR, "ERROR: main: add_alias failed\n");
974
+			}
975
+		
950 976
 		hostent2ip_addr(&sock_info[r].address, he, 0); /*convert to ip_addr 
951 977
 														 format*/
952 978
 		tmp=ip_addr2a(&sock_info[r].address);
... ...
@@ -987,6 +1013,9 @@ int main(int argc, char** argv)
987 1013
 		printf("%s [%s]:%s\n",sock_info[r].name.s, sock_info[r].address_str.s,
988 1014
 				sock_info[r].port_no_str.s);
989 1015
 	}
1016
+	printf("Aliases: ");
1017
+	for(a=aliases; a; a=a->next) printf("%.*s ", a->alias.len, a->alias.s);
1018
+	printf("\n");
990 1019
 
991 1020
 
992 1021
 	
... ...
@@ -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 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ */
5
+
6
+
7
+#include "str.h"
8
+#include "dprint.h"
9
+
10
+
11
+
12
+struct host_alias{
13
+	str alias;
14
+	struct host_alias* next;
15
+};
16
+
17
+
18
+extern struct host_alias* aliases;
19
+
20
+
21
+
22
+/* returns 1 if  name is in the alias list*/
23
+static inline int grep_aliases(char* name, int len)
24
+{
25
+	struct  host_alias* a;
26
+	
27
+	for(a=aliases;a;a=a->next)
28
+		if ((a->alias.len==len) && (strncasecmp(a->alias.s, name, len)==0))
29
+			return 1;
30
+	return 0;
31
+}
32
+
33
+
34
+
35
+/* adds an alias to the list (only if it isn't already there)
36
+ * returns 1 if a new alias was added, 0 if the alias was already on the list
37
+ * and  -1 on error */
38
+static inline int add_alias(char* name, int len)
39
+{
40
+	struct host_alias* a;
41
+	
42
+	if (grep_aliases(name,len)) return 0;
43
+	a=0;
44
+	a=(struct host_alias*)malloc(sizeof(struct host_alias));
45
+	if(a==0) goto error;
46
+	a->alias.s=(char*)malloc(len+1);
47
+	if (a->alias.s==0) goto error;
48
+	a->alias.len=len;
49
+	memcpy(a->alias.s, name, len);
50
+	a->alias.s[len]=0; /* null terminate for easier printing*/
51
+	a->next=aliases;
52
+	aliases=a;
53
+	return 1;
54
+error:
55
+	LOG(L_ERR, "ERROR: add_alias: memory allocation error\n");
56
+	if (a) free(a);
57
+	return -1;
58
+}
59
+
60
+
61
+
... ...
@@ -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 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ */
5
+
6
+#include <stdio.h>
7
+#include <errno.h>
8
+#include <unistd.h>
9
+#include <netdb.h>
10
+
11
+
12
+
13
+
14
+static char* id="$Id$";
15
+static char* version="gethostbyaddr 0.1";
16
+static char* help_msg="\
17
+Usage: gethostbyaddr   [-hV] -n host\n\
18
+Options:\n\
19
+    -n host       host name\n\
20
+    -V            version number\n\
21
+    -h            this help message\n\
22
+";
23
+
24
+
25
+int main(int argc, char** argv)
26
+{
27
+	char c;
28
+	char* name;
29
+	struct hostent* he;
30
+	unsigned char** h;
31
+
32
+	name=0;
33
+	
34
+	opterr=0;
35
+	while ((c=getopt(argc, argv, "n:hV"))!=-1){
36
+		switch(c){
37
+			case 'n':
38
+				name=optarg;
39
+				break;
40
+			case 'V':
41
+				printf("version: %s\n", version);
42
+				printf("%s\n", id);
43
+				exit(0);
44
+				break;
45
+			case 'h':
46
+				printf("version: %s\n", version);
47
+				printf("%s", help_msg);
48
+				exit(0);
49
+				break;
50
+			case '?':
51
+				if (isprint(optopt))
52
+					fprintf(stderr, "Unknown option `-%c�\n", optopt);
53
+				else
54
+					fprintf(stderr, "Unknown character `\\x%x�\n", optopt);
55
+				goto error;
56
+			case ':':
57
+				fprintf(stderr, "Option `-%c� requires an argument.\n",
58
+						optopt);
59
+				goto error;
60
+				break;
61
+			default:
62
+				abort();
63
+		}
64
+	}
65
+	
66
+	if (name==0){
67
+		fprintf(stderr, "Missing domain name (-n name)\n");
68
+		goto error;
69
+	}
70
+	
71
+	he=gethostbyname(name);
72
+	if (he==0){
73
+			printf("bad adddress <%s>\n", name);
74
+			goto error;
75
+	}
76
+	he=gethostbyaddr(he->h_addr_list[0], he->h_length, he->h_addrtype); 
77
+	if (he==0) printf("no answer\n");
78
+	else{
79
+		printf("h_name=%s\n", he->h_name);
80
+		for(h=he->h_aliases;*h;h++)
81
+			printf("   alias=%s\n", *h);
82
+		for(h=he->h_addr_list;*h;h++)
83
+			printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
84
+	}
85
+	printf("done\n");
86
+	exit(0);
87
+error:
88
+	exit(-1);
89
+}
0 90
new file mode 100644
... ...
@@ -0,0 +1,84 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ */
5
+
6
+#include <stdio.h>
7
+#include <errno.h>
8
+#include <unistd.h>
9
+#include <netdb.h>
10
+
11
+
12
+
13
+
14
+static char* id="$Id$";
15
+static char* version="gethostbyname 0.1";
16
+static char* help_msg="\
17
+Usage: gethostbyname   [-hV] -n host\n\
18
+Options:\n\
19
+    -n host       host name\n\
20
+    -V            version number\n\
21
+    -h            this help message\n\
22
+";
23
+
24
+
25
+int main(int argc, char** argv)
26
+{
27
+	char c;
28
+	char* name;
29
+	struct hostent* he;
30
+	unsigned char** h;
31
+
32
+	name=0;
33
+	
34
+	opterr=0;
35
+	while ((c=getopt(argc, argv, "n:hV"))!=-1){
36
+		switch(c){
37
+			case 'n':
38
+				name=optarg;
39
+				break;
40
+			case 'V':
41
+				printf("version: %s\n", version);
42
+				printf("%s\n", id);
43
+				exit(0);
44
+				break;
45
+			case 'h':
46
+				printf("version: %s\n", version);
47
+				printf("%s", help_msg);
48
+				exit(0);
49
+				break;
50
+			case '?':
51
+				if (isprint(optopt))
52
+					fprintf(stderr, "Unknown option `-%c�\n", optopt);
53
+				else
54
+					fprintf(stderr, "Unknown character `\\x%x�\n", optopt);
55
+				goto error;
56
+			case ':':
57
+				fprintf(stderr, "Option `-%c� requires an argument.\n",
58
+						optopt);
59
+				goto error;
60
+				break;
61
+			default:
62
+				abort();
63
+		}
64
+	}
65
+	
66
+	if (name==0){
67
+		fprintf(stderr, "Missing domain name (-n name)\n");
68
+		goto error;
69
+	}
70
+	
71
+	he=gethostbyname(name);
72
+	if (he==0) printf("no answer\n");
73
+	else{
74
+		printf("h_name=%s\n", he->h_name);
75
+		for(h=he->h_aliases;*h;h++)
76
+			printf("   alias=%s\n", *h);
77
+		for(h=he->h_addr_list;*h;h++)
78
+			printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
79
+	}
80
+	printf("done\n");
81
+	exit(0);
82
+error:
83
+	exit(-1);
84
+}
... ...
@@ -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