Browse code

- removed -p port && extended -l to support the same syntax as listen= : -l [proto:]addr[:port] where proto=udp|tcp (default all), and addr=ip_addr|host|interface_name. E.g.: -l localhost, -l tcp:127.0.0.1 -l udp:[::1]:5062, -l eth0, -l tcp:xl1:5090 - mysql: fixed minor warning

Andrei Pelinescu-Onciul authored on 02/12/2004 15:55:25
Showing 3 changed files
... ...
@@ -50,7 +50,7 @@ MAIN_NAME=ser
50 50
 VERSION = 0
51 51
 PATCHLEVEL = 8
52 52
 SUBLEVEL =   99
53
-EXTRAVERSION = -dev21
53
+EXTRAVERSION = -dev22
54 54
 
55 55
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
56 56
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
... ...
@@ -41,8 +41,13 @@ core:
41 41
  - new parts:
42 42
        UNIX domain socket server implemented
43 43
  - changes: 
44
+    - command line: removed -p port and extended -l:
45
+       -l [proto:]addr[:port] , where proto=udp|tcp and 
46
+       addr= host|ip_address|interface_name. The format is the same
47
+       as for listen in the config file. ipv6 addresses must be enclosed in
48
+       [].
44 49
     - added from_uri & to_uri: behave exactly like uri but use the
45
-	   "From:"/"To:" uris
50
+      "From:"/"To:" uris
46 51
        (e.g.: if (from_uri==myself) ..., if (to_uri=~"^sip:test@")... )
47 52
     - config: better escape support in strings (e.g. \", \<cr>, \x0a, \012)
48 53
     - bad network addresses are now automatically fixed
... ...
@@ -54,6 +54,8 @@
54 54
  *                => kill all or abort)  (andrei)
55 55
  *              force a shm_unlock before cleaning-up, in case we have a
56 56
  *               crashed childvwhich still holds the lock  (andrei)
57
+ *  2004-12-02  removed -p, extended -l to support [proto:]address[:port],
58
+ *               added parse_phostport, parse_proto (andrei)
57 59
  */
58 60
 
59 61
 
... ...
@@ -135,12 +137,12 @@ Usage: " NAME " -l address [-p port] [-l address [-p port]...] [options]\n\
135 135
 Options:\n\
136 136
     -f file      Configuration file (default " CFG_FILE ")\n\
137 137
     -c           Check configuration file for errors\n\
138
-    -p port      Listen on the specified port (default: 5060)\n\
139
-                  applies to the last address in -l and to all \n\
140
-                  following that do not have a corresponding -p\n\
141 138
     -l address   Listen on the specified address/interface (multiple -l\n\
142
-                  mean listening on more addresses). The default behavior\n\
143
-                  is to listen on all the interfaces\n\
139
+                  mean listening on more addresses).  The address format is\n\
140
+                  [proto:]addr[:port], where proto=udp|tcp and \n\
141
+                  addr= host|ip_address|interface_name. E.g: -l locahost, \n\
142
+                  -l udp:127.0.0.1:5080, -l eth0:5062 The default behavior\n\
143
+                  is to listen on all the interfaces.\n\
144 144
     -n processes Number of child processes to fork per interface\n\
145 145
                   (default: 8)\n\
146 146
     -r           Use dns to check if is necessary to add a \"received=\"\n\
... ...
@@ -673,6 +675,126 @@ error:
673 673
 
674 674
 
675 675
 
676
+/* returns -1 on error, 0 on success
677
+ * sets proto */
678
+static int parse_proto(unsigned char* s, long len, int* proto)
679
+{
680
+#define PROTO2UINT(a, b, c) ((	(((unsigned int)(a))<<16)+ \
681
+								(((unsigned int)(b))<<8)+  \
682
+								((unsigned int)(c)) ) | 0x20202020)
683
+	unsigned int i;
684
+	if (len!=3) return -1;
685
+	i=PROTO2UINT(s[0], s[1], s[2]);
686
+	switch(i){
687
+		case PROTO2UINT('u', 'd', 'p'):
688
+			*proto=PROTO_UDP;
689
+			break;
690
+#ifdef USE_TCP
691
+		case PROTO2UINT('t', 'c', 'p'):
692
+			*proto=PROTO_TCP;
693
+			break;
694
+#ifdef USE_TLS
695
+		case PROTO2UINT('t', 'l', 's'):
696
+			*proto=PROTO_TLS;
697
+			break;
698
+#endif
699
+#endif
700
+		default:
701
+			return -1;
702
+	}
703
+	return 0;
704
+}
705
+
706
+
707
+
708
+/*
709
+ * parses [proto:]host[:port]
710
+ * where proto= udp|tcp|tls
711
+ * returns 0 on success and -1 on failure
712
+ */
713
+static int parse_phostport(char* s, char** host, int* hlen, int* port,
714
+							int* proto)
715
+{
716
+	char* first; /* first ':' occurrence */
717
+	char* second; /* second ':' occurrence */
718
+	char* p;
719
+	int bracket;
720
+	char* tmp;
721
+	
722
+	first=second=0;
723
+	bracket=0;
724
+	
725
+	/* find the first 2 ':', ignoring possible ipv6 addresses
726
+	 * (substrings between [])
727
+	 */
728
+	for(p=s; *p; p++){
729
+		switch(*p){
730
+			case '[':
731
+				bracket++;
732
+				if (bracket>1) goto error_brackets;
733
+				break;
734
+			case ']':
735
+				bracket--;
736
+				if (bracket<0) goto error_brackets;
737
+				break;
738
+			case ':':
739
+				if (bracket==0){
740
+					if (first==0) first=p;
741
+					else if( second==0) second=p;
742
+					else goto error_colons;
743
+				}
744
+				break;
745
+		}
746
+	}
747
+	if (p==s) return -1;
748
+	if (*(p-1)==':') goto error_colons;
749
+	
750
+	if (first==0){ /* no ':' => only host */
751
+		*host=s;
752
+		*hlen=(int)(p-s);
753
+		*port=0;
754
+		*proto=0;
755
+		return 0;
756
+	}
757
+	if (second){ /* 2 ':' found => check if valid */
758
+		if (parse_proto(s, first-s, proto)<0) goto error_proto;
759
+		*port=strtol(second+1, &tmp, 10);
760
+		if ((tmp==0)||(*tmp)||(tmp==second+1)) goto error_port;
761
+		*host=first+1;
762
+		*hlen=(int)(second-*host);
763
+		return 0;
764
+	}
765
+	/* only 1 ':' found => it's either proto:host or host:port */
766
+	*port=strtol(first+1, &tmp, 10);
767
+	if ((tmp==0)||(*tmp)||(tmp==first+1)){
768
+		/* invalid port => it's proto:host */
769
+		if (parse_proto(s, first-s, proto)<0) goto error_proto;
770
+		*port=0;
771
+		*host=first+1;
772
+		*hlen=(int)(p-*host);
773
+	}else{
774
+		/* valid port => its host:port */
775
+		*proto=0;
776
+		*host=s;
777
+		*hlen=(int)(first-*host);
778
+	}
779
+	return 0;
780
+error_brackets:
781
+	LOG(L_ERR, "ERROR: parse_phostport: too many brackets in %s\n", s);
782
+	return -1;
783
+error_colons:
784
+	LOG(L_ERR, "ERROR: parse_phostport: too many colons in %s\n", s);
785
+	return -1;
786
+error_proto:
787
+	LOG(L_ERR, "ERROR: parse_phostport: bad protocol in %s\n", s);
788
+	return -1;
789
+error_port:
790
+	LOG(L_ERR, "ERROR: parse_phostport: bad port number in %s\n", s);
791
+	return -1;
792
+}
793
+
794
+
795
+
676 796
 /* main loop */
677 797
 int main_loop()
678 798
 {
... ...
@@ -1064,6 +1186,9 @@ int main(int argc, char** argv)
1064 1064
 	FILE* cfg_stream;
1065 1065
 	int c,r;
1066 1066
 	char *tmp;
1067
+	int tmp_len;
1068
+	int port;
1069
+	int proto;
1067 1070
 	char *options;
1068 1071
 	int ret;
1069 1072
 	unsigned int seed;
... ...
@@ -1090,7 +1215,7 @@ int main(int argc, char** argv)
1090 1090
 #ifdef STATS
1091 1091
 	"s:"
1092 1092
 #endif
1093
-	"f:cp:m:b:l:n:N:rRvdDETVhw:t:u:g:P:G:i:x:";
1093
+	"f:cm:b:l:n:N:rRvdDETVhw:t:u:g:P:G:i:x:";
1094 1094
 	
1095 1095
 	while((c=getopt(argc,argv,options))!=-1){
1096 1096
 		switch(c){
... ...
@@ -1106,13 +1231,6 @@ int main(int argc, char** argv)
1106 1106
 					stat_file=optarg;
1107 1107
 				#endif
1108 1108
 					break;
1109
-			case 'p':
1110
-					port_no=strtol(optarg, &tmp, 10);
1111
-					if (tmp &&(*tmp)){
1112
-						fprintf(stderr, "bad port number: -p %s\n", optarg);
1113
-						goto error;
1114
-					}
1115
-					break;
1116 1109
 			case 'm':
1117 1110
 					shm_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
1118 1111
 					if (tmp &&(*tmp)){
... ...
@@ -1133,8 +1251,15 @@ int main(int argc, char** argv)
1133 1133
 					}
1134 1134
 					break;
1135 1135
 			case 'l':
1136
+					if (parse_phostport(optarg, &tmp, &tmp_len,
1137
+											&port, &proto)<0){
1138
+						fprintf(stderr, "bad -l address specifier: %s\n",
1139
+										optarg);
1140
+						goto error;
1141
+					}
1142
+					tmp[tmp_len]=0; /* null terminate the host */
1136 1143
 					/* add a new addr. to our address list */
1137
-					if (add_listen_iface(optarg, 0, 0, 0)!=0){
1144
+					if (add_listen_iface(tmp, port, proto, 0)!=0){
1138 1145
 						fprintf(stderr, "failed to add new listen address\n");
1139 1146
 						goto error;
1140 1147
 					}