Browse code

- basic tel uri support (it's parsed, SIP_URI_T, SIPS_URI_T and TEL_URI_T added)

Andrei Pelinescu-Onciul authored on 25/02/2005 14:19:57
Showing 6 changed files
... ...
@@ -53,7 +53,7 @@ MAIN_NAME=ser
53 53
 VERSION = 0
54 54
 PATCHLEVEL = 10
55 55
 SUBLEVEL =   99
56
-EXTRAVERSION = -dev1
56
+EXTRAVERSION = -dev2
57 57
 
58 58
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
59 59
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
... ...
@@ -174,10 +174,11 @@ int do_action(struct action* a, struct sip_msg* msg)
174 174
 							goto error_fwd_uri;
175 175
 					}
176 176
 #ifdef USE_TLS
177
-					if (u->secure){
177
+					if (u->type==SIPS_URI_T){
178 178
 						if (u->proto==PROTO_UDP){
179 179
 							LOG(L_ERR, "ERROR: do_action: forward: secure uri"
180
-									" incompatible with transport %d\n", u->proto);
180
+									" incompatible with transport %d\n",
181
+									u->proto);
181 182
 							ret=E_BAD_PROTO;
182 183
 							goto error_fwd_uri;
183 184
 						}
... ...
@@ -108,7 +108,7 @@ inline static struct proxy_l *uri2proxy( str *uri, int proto )
108 108
 		return 0;
109 109
 	}
110 110
 	
111
-	if (parsed_uri.secure){
111
+	if (parsed_uri.type==SIPS_URI_T){
112 112
 		if ((parsed_uri.proto!=PROTO_TCP) && (parsed_uri.proto!=PROTO_NONE)){
113 113
 			LOG(L_ERR, "ERROR: uri2proxy: bad transport  for sips uri: %d\n",
114 114
 					parsed_uri.proto);
... ...
@@ -37,6 +37,7 @@
37 37
  *  2003-04-12  added msg_flags to sip_msg (andrei)
38 38
  *  2003-11-02  added diversion header field to sip_msg (jh)
39 39
  *  2004-11-08  added force_send_socket (andrei)
40
+ *  2005-02-25  uri types added (sip, sips & tel)  (andrei)
40 41
  */
41 42
 
42 43
 
... ...
@@ -121,6 +122,9 @@ struct sip_uri {
121 122
 };
122 123
 #endif
123 124
 
125
+enum _uri_type{ERROR_URI_T=0, SIP_URI_T, SIPS_URI_T, TEL_URI_T};
126
+typedef enum _uri_type uri_type;
127
+
124 128
 struct sip_uri {
125 129
 	str user;     /* Username */
126 130
 	str passwd;   /* Password */
... ...
@@ -130,7 +134,7 @@ struct sip_uri {
130 134
 	str headers;  
131 135
 	unsigned short port_no;
132 136
 	unsigned short proto; /* from transport */
133
-	int secure; /* 1 if is a sips, 0 otherwise */
137
+	uri_type type; /* uri scheme */
134 138
 	/* parameters */
135 139
 	str transport;
136 140
 	str ttl;
... ...
@@ -33,6 +33,7 @@
33 33
  * 2003-04-11  ser_error is now set in parse_uri (andrei)
34 34
  * 2003-04-26  ZSW (jiri)
35 35
  * 2003-07-03  sips:, r2, lr=on support added (andrei)
36
+ * 2005-02-25  preliminary tel uri support (andrei)
36 37
  */
37 38
 
38 39
 
... ...
@@ -96,6 +97,11 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
96 97
 	char* pass;
97 98
 	int found_user;
98 99
 	int error_headers;
100
+	unsigned int scheme;
101
+	
102
+#define SIP_SCH		0x3a706973
103
+#define SIPS_SCH	0x73706973
104
+#define TEL_SCH		0x3a6c6574
99 105
 	
100 106
 #define case_port( ch, var) \
101 107
 	case ch: \
... ...
@@ -315,15 +321,18 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
315 321
 	port_no=0;
316 322
 	state=URI_INIT;
317 323
 	memset(uri, 0, sizeof(struct sip_uri)); /* zero it all, just to be sure*/
318
-	/*look for sip:*/
324
+	/*look for sip:, sips: or tel:*/
319 325
 	if (len<5) goto error_too_short;
320
-	if (! ( ((buf[0]|0x20)=='s')&&((buf[1]|0x20)=='i')&&((buf[2]|0x20)=='p')))
321
-		goto error_bad_uri;
322
-	if (buf[3]!=':'){
323
-		/* parse also sips: */
324
-		if  (((buf[3]|0x20)=='s')&&(buf[4]==':')) {p++; uri->secure=1;}
326
+	scheme=buf[0]+(buf[1]<<8)+(buf[2]<<16)+(buf[3]<<24);
327
+	scheme|=0x20202020;
328
+	if (scheme==SIP_SCH){
329
+		uri->type=SIP_URI_T;
330
+	}else if(scheme==SIPS_SCH){
331
+		if(buf[4]==':'){ p++; uri->type=SIPS_URI_T;}
325 332
 		else goto error_bad_uri;
326
-	}
333
+	}else if (scheme==TEL_SCH){
334
+		uri->type=TEL_URI_T;
335
+	}else goto error_bad_uri;
327 336
 	
328 337
 	s=p;
329 338
 	for(;p<end; p++){
... ...
@@ -942,11 +951,18 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
942 951
 		default:
943 952
 			goto error_bug;
944 953
 	}
945
-	
946
-#if EXTRA_DEBUG
954
+	if (uri->type==TEL_URI_T){
955
+		/* fix tel uris, move the number in uri and empty the host */
956
+		uri->user=uri->host;
957
+		uri->host.s="";
958
+		uri->host.len=0;
959
+	}
960
+#ifdef EXTRA_DEBUG
947 961
 	/* do stuff */
948
-	DBG("parsed uri:\n user=<%.*s>(%d)\n passwd=<%.*s>(%d)\n host=<%.*s>(%d)\n"
949
-			" port=<%.*s>(%d): %d\n params=<%.*s>(%d)\n headers=<%.*s>(%d)\n",
962
+	DBG("parsed uri:\n type=%d user=<%.*s>(%d)\n passwd=<%.*s>(%d)\n"
963
+			" host=<%.*s>(%d)\n port=<%.*s>(%d): %d\n params=<%.*s>(%d)\n"
964
+			" headers=<%.*s>(%d)\n",
965
+			uri->type,
950 966
 			uri->user.len, ZSW(uri->user.s), uri->user.len,
951 967
 			uri->passwd.len, ZSW(uri->passwd.s), uri->passwd.len,
952 968
 			uri->host.len, ZSW(uri->host.s), uri->host.len,
... ...
@@ -3,11 +3,23 @@
3 3
 #include <string.h>
4 4
 #include "../str.h"
5 5
 
6
+/* ser compat defs */
7
+
6 8
 #define DBG printf
7 9
 #define LOG(lev, fmt, args...) printf(fmt, ## args)
8 10
 
11
+#define EXTRA_DEBUG
12
+#define ZSW(s)	((s)?(s):"")
13
+#define E_BAD_URI -100
14
+
15
+int ser_error=0;
16
+
17
+
9 18
 enum {PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP };
10 19
 
20
+enum _uri_type{ERROR_URI_T=0, SIP_URI_T, SIPS_URI_T, TEL_URI_T};
21
+typedef enum _uri_type uri_type;
22
+
11 23
 struct sip_uri {
12 24
 	str user;     /* Username */
13 25
 	str passwd;   /* Password */
... ...
@@ -17,6 +29,7 @@ struct sip_uri {
17 29
 	str headers;  
18 30
 	unsigned short port_no;
19 31
 	unsigned short proto; /* from transport */
32
+	uri_type type; /* uri scheme */
20 33
 	/* parameters */
21 34
 	str transport;
22 35
 	str ttl;
... ...
@@ -24,16 +37,20 @@ struct sip_uri {
24 37
 	str maddr;
25 38
 	str method;
26 39
 	str lr;
40
+	str r2; /* ser specific rr parameter */
27 41
 	/* values */
28 42
 	str transport_val;
29 43
 	str ttl_val;
30 44
 	str user_param_val;
31 45
 	str maddr_val;
32 46
 	str method_val;
47
+	str lr_val; /* lr value placeholder for lr=on a.s.o*/
48
+	str r2_val;
33 49
 };
34 50
 
35 51
 
36 52
 
53
+
37 54
 int parse_uri(char* buf, int len, struct sip_uri* uri)
38 55
 {
39 56
 	enum states  {	URI_INIT, URI_USER, URI_PASSWORD, URI_PASSWORD_ALPHA,
... ...
@@ -53,7 +70,10 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
53 70
 					/* maddr */
54 71
 					      PMA_A, PMA_D, PMA_D2, PMA_R, PMA_eq,
55 72
 					/* lr */
56
-					PLR_L, PLR_R_FIN,
73
+					PLR_L, PLR_R_FIN, PLR_eq,
74
+					/* r2 */
75
+					PR2_R, PR2_2_FIN, PR2_eq,
76
+					
57 77
 					/* transport values */
58 78
 					/* udp */
59 79
 					VU_U, VU_D, VU_P_FIN,
... ...
@@ -78,6 +98,11 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
78 98
 	char* pass;
79 99
 	int found_user;
80 100
 	int error_headers;
101
+	unsigned int scheme;
102
+	
103
+#define SIP_SCH		0x3a706973
104
+#define SIPS_SCH	0x73706973
105
+#define TEL_SCH		0x3a6c6574
81 106
 	
82 107
 #define case_port( ch, var) \
83 108
 	case ch: \
... ...
@@ -297,14 +322,22 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
297 322
 	port_no=0;
298 323
 	state=URI_INIT;
299 324
 	memset(uri, 0, sizeof(struct sip_uri)); /* zero it all, just to be sure*/
300
-	/*look for sip:*/
301
-	if (len<4) goto error_too_short;
302
-	if (! ( ((buf[0]|0x20)=='s')&&((buf[1]|0x20)=='i')&&((buf[2]|0x20)=='p')&&
303
-		     (buf[3]==':') ) ) goto error_bad_uri;
325
+	/*look for sip:, sips: or tel:*/
326
+	if (len<5) goto error_too_short;
327
+	scheme=buf[0]+(buf[1]<<8)+(buf[2]<<16)+(buf[3]<<24);
328
+	scheme|=0x20202020;
329
+	if (scheme==SIP_SCH){
330
+		uri->type=SIP_URI_T;
331
+	}else if(scheme==SIPS_SCH){
332
+		if(buf[4]==':'){ p++; uri->type=SIPS_URI_T;}
333
+		else goto error_bad_uri;
334
+	}else if (scheme==TEL_SCH){
335
+		uri->type=TEL_URI_T;
336
+	}else goto error_bad_uri;
304 337
 	
305 338
 	s=p;
306 339
 	for(;p<end; p++){
307
-		switch(state){
340
+		switch((unsigned char)state){
308 341
 			case URI_INIT:
309 342
 				switch(*p){
310 343
 					case '[':
... ...
@@ -535,13 +568,17 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
535 568
 						b=p;
536 569
 						state=PLR_L;
537 570
 						break;
571
+					case 'r':
572
+					case 'R':
573
+						b=p;
574
+						state=PR2_R;
538 575
 					default:
539 576
 						state=URI_PARAM_P;
540 577
 				}
541 578
 				break;
542 579
 			case URI_PARAM_P: /* ignore current param */
543 580
 				/* supported params:
544
-				 *  maddr, transport, ttl, lr, user, method  */
581
+				 *  maddr, transport, ttl, lr, user, method, r2  */
545 582
 				switch(*p){
546 583
 					param_common_cases;
547 584
 				};
... ...
@@ -677,6 +714,9 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
677 714
 					case '@':
678 715
 						still_at_user; 
679 716
 						break;
717
+					case '=':
718
+						state=PLR_eq;
719
+						break;
680 720
 					semicolon_case; 
681 721
 						uri->lr.s=b;
682 722
 						uri->lr.len=(p-b);
... ...
@@ -691,7 +731,52 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
691 731
 						state=URI_PARAM_P;
692 732
 				}
693 733
 				break;
694
-						
734
+				/* handle lr=something case */
735
+			case PLR_eq:
736
+				param=&uri->lr;
737
+				param_val=&uri->lr_val;
738
+				switch(*p){
739
+					param_common_cases;
740
+					default:
741
+						v=p;
742
+						state=URI_VAL_P;
743
+				}
744
+				break;
745
+			/* r2 */
746
+			param_switch1(PR2_R,  '2', PR2_2_FIN);
747
+			case PR2_2_FIN:
748
+				switch(*p){
749
+					case '@':
750
+						still_at_user; 
751
+						break;
752
+					case '=':
753
+						state=PR2_eq;
754
+						break;
755
+					semicolon_case; 
756
+						uri->r2.s=b;
757
+						uri->r2.len=(p-b);
758
+						break;
759
+					question_case; 
760
+						uri->r2.s=b;
761
+						uri->r2.len=(p-b);
762
+						break;
763
+					colon_case;
764
+						break;
765
+					default:
766
+						state=URI_PARAM_P;
767
+				}
768
+				break;
769
+				/* handle lr=something case */
770
+			case PR2_eq:
771
+				param=&uri->r2;
772
+				param_val=&uri->r2_val;
773
+				switch(*p){
774
+					param_common_cases;
775
+					default:
776
+						v=p;
777
+						state=URI_VAL_P;
778
+				}
779
+				break;
695 780
 				
696 781
 				
697 782
 			case URI_HEADERS:
... ...
@@ -800,16 +885,25 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
800 885
 		case PM_D:
801 886
 		case PM_eq:
802 887
 		case PLR_L: /* lr */
888
+		case PR2_R:  /* r2 */
803 889
 			uri->params.s=s;
804 890
 			uri->params.len=p-s;
805 891
 			break;
806 892
 		/* fin param states */
807 893
 		case PLR_R_FIN:
894
+		case PLR_eq:
808 895
 			uri->params.s=s;
809 896
 			uri->params.len=p-s;
810 897
 			uri->lr.s=b;
811 898
 			uri->lr.len=p-b;
812 899
 			break;
900
+		case PR2_2_FIN:
901
+		case PR2_eq:
902
+			uri->params.s=s;
903
+			uri->params.len=p-s;
904
+			uri->r2.s=b;
905
+			uri->r2.len=p-b;
906
+			break;
813 907
 		case URI_VAL_P:
814 908
 		/* intermediate value states */
815 909
 		case VU_U:
... ...
@@ -858,74 +952,88 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
858 952
 		default:
859 953
 			goto error_bug;
860 954
 	}
861
-	
955
+	if (uri->type==TEL_URI_T){
956
+		/* fix tel uris, move the number in uri and empty the host */
957
+		uri->user=uri->host;
958
+		uri->host.s="";
959
+		uri->host.len=0;
960
+	}
961
+#ifdef EXTRA_DEBUG
862 962
 	/* do stuff */
863
-	DBG("parsed uri:\n user=<%.*s>(%d)\n passwd=<%.*s>(%d)\n host=<%.*s>(%d)\n"
864
-			" port=<%.*s>(%d): %d\n params=<%.*s>(%d)\n headers=<%.*s>(%d)\n",
865
-			uri->user.len, uri->user.s, uri->user.len,
866
-			uri->passwd.len, uri->passwd.s, uri->passwd.len,
867
-			uri->host.len, uri->host.s, uri->host.len,
868
-			uri->port.len, uri->port.s, uri->port.len, uri->port_no,
869
-			uri->params.len, uri->params.s, uri->params.len,
870
-			uri->headers.len, uri->headers.s, uri->headers.len
963
+	DBG("parsed uri:\n type=%d user=<%.*s>(%d)\n passwd=<%.*s>(%d)\n"
964
+			" host=<%.*s>(%d)\n port=<%.*s>(%d): %d\n params=<%.*s>(%d)\n"
965
+			" headers=<%.*s>(%d)\n",
966
+			uri->type,
967
+			uri->user.len, ZSW(uri->user.s), uri->user.len,
968
+			uri->passwd.len, ZSW(uri->passwd.s), uri->passwd.len,
969
+			uri->host.len, ZSW(uri->host.s), uri->host.len,
970
+			uri->port.len, ZSW(uri->port.s), uri->port.len, uri->port_no,
971
+			uri->params.len, ZSW(uri->params.s), uri->params.len,
972
+			uri->headers.len, ZSW(uri->headers.s), uri->headers.len
871 973
 		);
872 974
 	DBG(" uri params:\n   transport=<%.*s>, val=<%.*s>, proto=%d\n",
873
-			uri->transport.len, uri->transport.s, uri->transport_val.len,
874
-			uri->transport_val.s, uri->proto);
975
+			uri->transport.len, ZSW(uri->transport.s), uri->transport_val.len,
976
+			ZSW(uri->transport_val.s), uri->proto);
875 977
 	DBG("   user-param=<%.*s>, val=<%.*s>\n",
876
-			uri->user_param.len, uri->user_param.s, uri->user_param_val.len,
877
-			uri->user_param_val.s);
978
+			uri->user_param.len, ZSW(uri->user_param.s), 
979
+			uri->user_param_val.len, ZSW(uri->user_param_val.s));
878 980
 	DBG("   method=<%.*s>, val=<%.*s>\n",
879
-			uri->method.len, uri->method.s, uri->method_val.len,
880
-			uri->method_val.s);
981
+			uri->method.len, ZSW(uri->method.s), 
982
+			uri->method_val.len, ZSW(uri->method_val.s));
881 983
 	DBG("   ttl=<%.*s>, val=<%.*s>\n",
882
-			uri->ttl.len, uri->ttl.s, uri->ttl_val.len,
883
-			uri->ttl_val.s);
984
+			uri->ttl.len, ZSW(uri->ttl.s), 
985
+			uri->ttl_val.len, ZSW(uri->ttl_val.s));
884 986
 	DBG("   maddr=<%.*s>, val=<%.*s>\n",
885
-			uri->maddr.len, uri->maddr.s, uri->maddr_val.len,
886
-			uri->maddr_val.s);
887
-	DBG("   lr=<%.*s>\n", uri->lr.len, uri->lr.s); 
987
+			uri->maddr.len, ZSW(uri->maddr.s), 
988
+			uri->maddr_val.len, ZSW(uri->maddr_val.s));
989
+	DBG("   lr=<%.*s>\n", uri->lr.len, ZSW(uri->lr.s)); 
990
+#endif
888 991
 	return 0;
889 992
 	
890 993
 error_too_short:
891 994
 	LOG(L_ERR, "ERROR: parse_uri: uri too short: <%.*s> (%d)\n",
892
-			len, buf, len);
893
-	return -1;
995
+			len, ZSW(buf), len);
996
+	ser_error=E_BAD_URI;
997
+	return E_BAD_URI;
894 998
 error_bad_char:
895 999
 	LOG(L_ERR, "ERROR: parse_uri: bad char '%c' in state %d"
896 1000
 			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
897
-			*p, state, (p-buf), buf, (p-buf), len, buf, len);
898
-	return -1;
1001
+			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1002
+	return E_BAD_URI;
899 1003
 error_bad_host:
900 1004
 	LOG(L_ERR, "ERROR: parse_uri: bad host in uri (error at char %c in"
901 1005
 			" state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
902
-			*p, state, (p-buf), buf, (p-buf), len, buf, len);
903
-	return -1;
1006
+			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1007
+	ser_error=E_BAD_URI;
1008
+	return E_BAD_URI;
904 1009
 error_bad_port:
905 1010
 	LOG(L_ERR, "ERROR: parse_uri: bad port in uri (error at char %c in"
906 1011
 			" state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
907
-			*p, state, (p-buf), buf, (p-buf), len, buf, len);
908
-	return -1;
1012
+			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1013
+	ser_error=E_BAD_URI;
1014
+	return E_BAD_URI;
909 1015
 error_bad_uri:
910 1016
 	LOG(L_ERR, "ERROR: parse_uri: bad uri,  state %d"
911 1017
 			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
912
-			 state, (p-buf), buf, (p-buf), len, buf, len);
913
-	return -1;
1018
+			 state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1019
+	ser_error=E_BAD_URI;
1020
+	return E_BAD_URI;
914 1021
 error_headers:
915 1022
 	LOG(L_ERR, "ERROR: parse_uri: bad uri headers: <%.*s>(%d)"
916 1023
 			" / <%.*s>(%d)\n",
917
-			uri->headers.len, uri->headers.s, uri->headers.len,
918
-			len, buf, len);
919
-	return -1;
1024
+			uri->headers.len, ZSW(uri->headers.s), uri->headers.len,
1025
+			len, ZSW(buf), len);
1026
+	ser_error=E_BAD_URI;
1027
+	return E_BAD_URI;
920 1028
 error_bug:
921 1029
 	LOG(L_CRIT, "BUG: parse_uri: bad  state %d"
922 1030
 			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
923
-			 state, (p-buf), buf, (p-buf), len, buf, len);
924
-	return -1;
1031
+			 state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1032
+	ser_error=E_BAD_URI;
1033
+	return E_BAD_URI;
925 1034
 }
926 1035
 
927 1036
 
928
-
929 1037
 int main (int argc, char** argv)
930 1038
 {
931 1039