Pārlūkot izejas kodu

- 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 23 gadi atpakaļ
vecāks
revīzija
e278821b00
12 mainītis faili ar 308 papildinājumiem un 8 dzēšanām
  1. 2 2
      Makefile.defs
  2. 2 0
      cfg.lex
  3. 27 0
      cfg.y
  4. 7 2
      forward.c
  5. 31 2
      main.c
  6. 1 1
      mem/vq_malloc.c
  7. 61 0
      name_alias.h
  8. 1 1
      parser/parse_uri.c
  9. 89 0
      test/gethostbyaddr.c
  10. 84 0
      test/gethostbyname.c
  11. 1 0
      test/inv_srv.sip
  12. 2 0
      test/stateless.cfg

+ 2 - 2
Makefile.defs

@@ -8,7 +8,7 @@
 VERSION = 0
 PATCHLEVEL = 8
 SUBLEVEL = 7
-EXTRAVERSION = -9-self
+EXTRAVERSION = -10-alias
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s)
@@ -99,7 +99,7 @@ DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
 	 -DADAPTIVE_WAIT -DADAPTIVE_WAIT_LOOPS=1024 \
 	 -DUSE_IPV6 \
 	 -DEXTRA_DEBUG \
-	 -DVQ_MALLOC  -DDBG_QM_MALLOC \
+	 -DDBG_QM_MALLOC \
 	 #-DF_MALLOC \
 	 #-DVQ_MALLOC  
 	 #-DCONTACT_BUG

+ 2 - 0
cfg.lex

@@ -91,6 +91,7 @@ DEBUG	debug
 FORK	fork
 LOGSTDERROR	log_stderror
 LISTEN		listen
+ALIAS		alias
 DNS		 dns
 REV_DNS	 rev_dns
 PORT	port
@@ -187,6 +188,7 @@ EAT_ABLE	[\ \t\b\r]
 <INITIAL>{FORK}		{ count(); yylval.strval=yytext; return FORK; }
 <INITIAL>{LOGSTDERROR}	{ yylval.strval=yytext; return LOGSTDERROR; }
 <INITIAL>{LISTEN}	{ count(); yylval.strval=yytext; return LISTEN; }
+<INITIAL>{ALIAS}	{ count(); yylval.strval=yytext; return ALIAS; }
 <INITIAL>{DNS}	{ count(); yylval.strval=yytext; return DNS; }
 <INITIAL>{REV_DNS}	{ count(); yylval.strval=yytext; return REV_DNS; }
 <INITIAL>{PORT}	{ count(); yylval.strval=yytext; return PORT; }

+ 27 - 0
cfg.y

@@ -21,6 +21,7 @@
 #include "sr_module.h"
 #include "modparam.h"
 #include "ip_addr.h"
+#include "name_alias.h"
 
 #include "config.h"
 
@@ -88,6 +89,7 @@ void* f_tmp;
 %token FORK
 %token LOGSTDERROR
 %token LISTEN
+%token ALIAS
 %token DNS
 %token REV_DNS
 %token PORT
@@ -272,8 +274,33 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
 												"(max. %d).\n", MAX_LISTEN);
 								}
 							  }
+		| LISTEN EQUAL  host {
+								if (sock_no < MAX_LISTEN){
+									sock_info[sock_no].name.s=
+										(char*)malloc(strlen($3)+1);
+									if (sock_info[sock_no].name.s==0){
+										LOG(L_CRIT, "ERROR: cfg. parser:"
+													" out of memory.\n");
+									}else{
+										strncpy(sock_info[sock_no].name.s, $3,
+												strlen($3)+1);
+										sock_info[sock_no].name.len=strlen($3);
+										sock_info[sock_no].port_no=port_no;
+										sock_no++;
+									}
+								}else{
+									LOG(L_CRIT, "ERROR: cfg. parser: "
+												"too many listen addresses"
+												"(max. %d).\n", MAX_LISTEN);
+								}
+							  }
+		
 		| LISTEN EQUAL  error { yyerror("ip address or hostname"
 						"expected"); }
+		| ALIAS  EQUAL STRING { add_alias($3, strlen($3)); }
+		| ALIAS  EQUAL ID     { add_alias($3, strlen($3)); }
+		| ALIAS  EQUAL host   { add_alias($3, strlen($3)); }
+		| ALIAS  EQUAL error  { yyerror(" hostname expected"); }
 		| error EQUAL { yyerror("unknown config variable"); }
 	;
 

+ 7 - 2
forward.c

@@ -28,6 +28,7 @@
 #include "stats.h"
 #include "ip_addr.h"
 #include "resolve.h"
+#include "name_alias.h"
 
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
@@ -101,13 +102,17 @@ int check_self(str* host)
 			break;
 	}
 	if (r==sock_no){
-		DBG("check_self: host != me\n");
-		return 0;
+		/* try to look into the aliases*/
+		if (grep_aliases(host->s, host->len)==0){
+			DBG("check_self: host != me\n");
+			return 0;
+		}
 	}
 	return 1;
 }
 
 
+
 int forward_request( struct sip_msg* msg, struct proxy_l * p)
 {
 	unsigned int len;

+ 31 - 2
main.c

@@ -37,6 +37,7 @@
 #include "parser/parse_hname2.h"
 #include "parser/digest/digest_parser.h"
 #include "fifo_server.h"
+#include "name_alias.h"
 
 
 #include "stats.h"
@@ -233,6 +234,8 @@ struct socket_info* sendipv6; /* same as above for ipv6 */
 
 unsigned short port_no=0; /* default port*/
 
+struct host_alias* aliases=0; /* name aliases list */
+
 /* ipc related globals */
 int process_no = 0;
 process_bm_t process_bit = 0;
@@ -659,6 +662,8 @@ int main(int argc, char** argv)
 	struct hostent* he;
 	int c,r;
 	char *tmp;
+	char** h;
+	struct host_alias* a;
 	struct utsname myname;
 	char *options;
 	char port_no_str[MAX_PORT_LEN];
@@ -933,9 +938,9 @@ int main(int argc, char** argv)
 			fprintf(stderr, "Out of memory.\n");
 			goto error;
 		}
-		strncpy(sock_info[sock_no].name.s, myname.nodename,
-				strlen(myname.nodename)+1);
 		sock_info[sock_no].name.len=strlen(myname.nodename);
+		strncpy(sock_info[sock_no].name.s, myname.nodename,
+				sock_info[sock_no].name.len+1);
 		sock_no++;
 	}
 
@@ -947,6 +952,27 @@ int main(int argc, char** argv)
 			DPrint("ERROR: could not resolve %s\n", sock_info[r].name.s);
 			goto error;
 		}
+		/* check if we got the official name */
+		if (strcasecmp(he->h_name, sock_info[r].name.s)!=0){
+			if (add_alias(sock_info[r].name.s, sock_info[r].name.len)<0){
+				LOG(L_ERR, "ERROR: main: add_alias failed\n");
+			}
+			/* change the oficial name */
+			free(sock_info[r].name.s);
+			sock_info[r].name.s=(char*)malloc(strlen(he->h_name)+1);
+			if (sock_info[r].name.s==0){
+				fprintf(stderr, "Out of memory.\n");
+				goto error;
+			}
+			sock_info[r].name.len=strlen(he->h_name);
+			strncpy(sock_info[r].name.s, he->h_name, sock_info[r].name.len+1);
+		}
+		/* add the aliases*/
+		for(h=he->h_aliases; h && *h; h++)
+			if (add_alias(*h, strlen(*h))<0){
+				LOG(L_ERR, "ERROR: main: add_alias failed\n");
+			}
+		
 		hostent2ip_addr(&sock_info[r].address, he, 0); /*convert to ip_addr 
 														 format*/
 		tmp=ip_addr2a(&sock_info[r].address);
@@ -987,6 +1013,9 @@ int main(int argc, char** argv)
 		printf("%s [%s]:%s\n",sock_info[r].name.s, sock_info[r].address_str.s,
 				sock_info[r].port_no_str.s);
 	}
+	printf("Aliases: ");
+	for(a=aliases; a; a=a->next) printf("%.*s ", a->alias.len, a->alias.s);
+	printf("\n");
 
 
 	

+ 1 - 1
mem/vq_malloc.c

@@ -98,7 +98,7 @@ void vqm_debug_frag(struct vqm_block* qm, struct vqm_frag* f)
 		abort();
 	};
 	if (memcmp(f->end_check, END_CHECK_PATTERN, END_CHECK_PATTERN_LEN)!=0) {
-		LOG(L_CRIT, "BUG: vqm_*: fragm. %p end overwritten(%*s)!\n",
+		LOG(L_CRIT, "BUG: vqm_*: fragm. %p end overwritten(%.*s)!\n",
 				f, END_CHECK_PATTERN_LEN, f->end_check );
 		vqm_status(qm);
 		abort();

+ 61 - 0
name_alias.h

@@ -0,0 +1,61 @@
+/*
+ * $Id$
+ *
+ */
+
+
+#include "str.h"
+#include "dprint.h"
+
+
+
+struct host_alias{
+	str alias;
+	struct host_alias* next;
+};
+
+
+extern struct host_alias* aliases;
+
+
+
+/* returns 1 if  name is in the alias list*/
+static inline int grep_aliases(char* name, int len)
+{
+	struct  host_alias* a;
+	
+	for(a=aliases;a;a=a->next)
+		if ((a->alias.len==len) && (strncasecmp(a->alias.s, name, len)==0))
+			return 1;
+	return 0;
+}
+
+
+
+/* adds an alias to the list (only if it isn't already there)
+ * returns 1 if a new alias was added, 0 if the alias was already on the list
+ * and  -1 on error */
+static inline int add_alias(char* name, int len)
+{
+	struct host_alias* a;
+	
+	if (grep_aliases(name,len)) return 0;
+	a=0;
+	a=(struct host_alias*)malloc(sizeof(struct host_alias));
+	if(a==0) goto error;
+	a->alias.s=(char*)malloc(len+1);
+	if (a->alias.s==0) goto error;
+	a->alias.len=len;
+	memcpy(a->alias.s, name, len);
+	a->alias.s[len]=0; /* null terminate for easier printing*/
+	a->next=aliases;
+	aliases=a;
+	return 1;
+error:
+	LOG(L_ERR, "ERROR: add_alias: memory allocation error\n");
+	if (a) free(a);
+	return -1;
+}
+
+
+

+ 1 - 1
parser/parse_uri.c

@@ -207,7 +207,7 @@ int parse_sip_msg_uri(struct sip_msg* msg)
 			tmp_len=msg->first_line.u.request.uri.len;
 		}
 		if (parse_uri(tmp, tmp_len, &msg->parsed_uri)<0){
-			LOG(L_ERR, "ERROR: parse_sip_msg_uri: bad uri <%*s>\n",
+			LOG(L_ERR, "ERROR: parse_sip_msg_uri: bad uri <%.*s>\n",
 						tmp_len, tmp);
 			msg->parsed_uri_ok=0;
 			return -1;

+ 89 - 0
test/gethostbyaddr.c

@@ -0,0 +1,89 @@
+/*
+ * $Id$
+ *
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <netdb.h>
+
+
+
+
+static char* id="$Id$";
+static char* version="gethostbyaddr 0.1";
+static char* help_msg="\
+Usage: gethostbyaddr   [-hV] -n host\n\
+Options:\n\
+    -n host       host name\n\
+    -V            version number\n\
+    -h            this help message\n\
+";
+
+
+int main(int argc, char** argv)
+{
+	char c;
+	char* name;
+	struct hostent* he;
+	unsigned char** h;
+
+	name=0;
+	
+	opterr=0;
+	while ((c=getopt(argc, argv, "n:hV"))!=-1){
+		switch(c){
+			case 'n':
+				name=optarg;
+				break;
+			case 'V':
+				printf("version: %s\n", version);
+				printf("%s\n", id);
+				exit(0);
+				break;
+			case 'h':
+				printf("version: %s\n", version);
+				printf("%s", help_msg);
+				exit(0);
+				break;
+			case '?':
+				if (isprint(optopt))
+					fprintf(stderr, "Unknown option `-%c´\n", optopt);
+				else
+					fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
+				goto error;
+			case ':':
+				fprintf(stderr, "Option `-%c´ requires an argument.\n",
+						optopt);
+				goto error;
+				break;
+			default:
+				abort();
+		}
+	}
+	
+	if (name==0){
+		fprintf(stderr, "Missing domain name (-n name)\n");
+		goto error;
+	}
+	
+	he=gethostbyname(name);
+	if (he==0){
+			printf("bad adddress <%s>\n", name);
+			goto error;
+	}
+	he=gethostbyaddr(he->h_addr_list[0], he->h_length, he->h_addrtype); 
+	if (he==0) printf("no answer\n");
+	else{
+		printf("h_name=%s\n", he->h_name);
+		for(h=he->h_aliases;*h;h++)
+			printf("   alias=%s\n", *h);
+		for(h=he->h_addr_list;*h;h++)
+			printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
+	}
+	printf("done\n");
+	exit(0);
+error:
+	exit(-1);
+}

+ 84 - 0
test/gethostbyname.c

@@ -0,0 +1,84 @@
+/*
+ * $Id$
+ *
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <netdb.h>
+
+
+
+
+static char* id="$Id$";
+static char* version="gethostbyname 0.1";
+static char* help_msg="\
+Usage: gethostbyname   [-hV] -n host\n\
+Options:\n\
+    -n host       host name\n\
+    -V            version number\n\
+    -h            this help message\n\
+";
+
+
+int main(int argc, char** argv)
+{
+	char c;
+	char* name;
+	struct hostent* he;
+	unsigned char** h;
+
+	name=0;
+	
+	opterr=0;
+	while ((c=getopt(argc, argv, "n:hV"))!=-1){
+		switch(c){
+			case 'n':
+				name=optarg;
+				break;
+			case 'V':
+				printf("version: %s\n", version);
+				printf("%s\n", id);
+				exit(0);
+				break;
+			case 'h':
+				printf("version: %s\n", version);
+				printf("%s", help_msg);
+				exit(0);
+				break;
+			case '?':
+				if (isprint(optopt))
+					fprintf(stderr, "Unknown option `-%c´\n", optopt);
+				else
+					fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
+				goto error;
+			case ':':
+				fprintf(stderr, "Option `-%c´ requires an argument.\n",
+						optopt);
+				goto error;
+				break;
+			default:
+				abort();
+		}
+	}
+	
+	if (name==0){
+		fprintf(stderr, "Missing domain name (-n name)\n");
+		goto error;
+	}
+	
+	he=gethostbyname(name);
+	if (he==0) printf("no answer\n");
+	else{
+		printf("h_name=%s\n", he->h_name);
+		for(h=he->h_aliases;*h;h++)
+			printf("   alias=%s\n", *h);
+		for(h=he->h_addr_list;*h;h++)
+			printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
+	}
+	printf("done\n");
+	exit(0);
+error:
+	exit(-1);
+}

+ 1 - 0
test/inv_srv.sip

@@ -1,4 +1,5 @@
 INVITE sip:[email protected] SIP/2.0
+:q
 Via: SIP/2.0/UDP dorian.fokus.gmd.de 
 From: "GMD FOKUS iptlab" <sip:[email protected]>;tag=b96b0300ed30f1286-2f5d
 Call-ID: [email protected]

+ 2 - 0
test/stateless.cfg

@@ -24,6 +24,8 @@ rev_dns=off      # (cmd. line: -R)
 #listen=192.168.57.72
 #loop_checks=0
 # for more info: sip_router -h
+alias=iptel.org
+alias="foo.bar"
 
 #modules