Browse code

- tel uri fixes: - fix ';' in user resetting uri->type; - tel uris parameters are moved into uri->params even for sip:...;user=phone type of uris

Andrei Pelinescu-Onciul authored on 03/03/2005 10:47:38
Showing 2 changed files
... ...
@@ -34,6 +34,7 @@
34 34
  * 2003-04-26  ZSW (jiri)
35 35
  * 2003-07-03  sips:, r2, lr=on support added (andrei)
36 36
  * 2005-02-25  preliminary tel uri support (andrei)
37
+ * 2005-03-03  more tel uri fixes (andrei)
37 38
  */
38 39
 
39 40
 
... ...
@@ -98,6 +99,7 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
98 99
 	int found_user;
99 100
 	int error_headers;
100 101
 	unsigned int scheme;
102
+	uri_type backup;
101 103
 	
102 104
 #define SIP_SCH		0x3a706973
103 105
 #define SIPS_SCH	0x73706973
... ...
@@ -118,9 +120,12 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
118 120
 							}else{ \
119 121
 								user.len=p-user.s; \
120 122
 							}\
123
+							/* save the uri type/scheme */ \
124
+							backup=uri->type; \
121 125
 							/* everything else is 0 */ \
122 126
 							memset(uri, 0, sizeof(struct sip_uri)); \
123
-							/* copy user & pass */ \
127
+							/* restore the scheme, copy user & pass */ \
128
+							uri->type=backup; \
124 129
 							uri->user=user; \
125 130
 							if (pass)	uri->passwd=password;  \
126 131
 							s=p+1; \
... ...
@@ -956,12 +961,25 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
956 961
 			if ((uri->user_param_val.len == 5) &&
957 962
 				(strncmp(uri->user_param_val.s, "phone", 5) == 0)) {
958 963
 				uri->type = TEL_URI_T;
964
+				/* move params from user into uri->params */
965
+				p=q_memchr(uri->user.s, ';', uri->user.len);
966
+				if (p){
967
+					uri->params.s=p+1;
968
+					uri->params.len=uri->user.s+uri->user.len-uri->params.s;
969
+					uri->user.len=p-uri->user.s;
970
+				}
959 971
 			}
960 972
 			break;
961 973
 		case SIPS_URI_T:
962 974
 			if ((uri->user_param_val.len == 5) &&
963 975
 				(strncmp(uri->user_param_val.s, "phone", 5) == 0)) {
964 976
 				uri->type = TELS_URI_T;
977
+				p=q_memchr(uri->user.s, ';', uri->user.len);
978
+				if (p){
979
+					uri->params.s=p+1;
980
+					uri->params.len=uri->user.s+uri->user.len-uri->params.s;
981
+					uri->user.len=p-uri->user.s;
982
+				}
965 983
 			}
966 984
 			break;
967 985
 		case TEL_URI_T:
... ...
@@ -972,6 +990,8 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
972 990
 			uri->host.len=0;
973 991
 			break;
974 992
 		case ERROR_URI_T:
993
+			LOG(L_ERR, "ERROR: parse_uri unexpected error (BUG?)\n"); 
994
+			goto error_bad_uri;
975 995
 			break; /* do nothing, avoids a compilation warning */
976 996
 	}
977 997
 #ifdef EXTRA_DEBUG
... ...
@@ -1009,43 +1029,44 @@ int parse_uri(char* buf, int len, struct sip_uri* uri)
1009 1029
 error_too_short:
1010 1030
 	LOG(L_ERR, "ERROR: parse_uri: uri too short: <%.*s> (%d)\n",
1011 1031
 			len, ZSW(buf), len);
1012
-	ser_error=E_BAD_URI;
1013
-	return E_BAD_URI;
1032
+	goto error_exit;
1014 1033
 error_bad_char:
1015 1034
 	LOG(L_ERR, "ERROR: parse_uri: bad char '%c' in state %d"
1016 1035
 			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
1017
-			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1018
-	return E_BAD_URI;
1036
+			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf),
1037
+			len, ZSW(buf), len);
1038
+	goto error_exit;
1019 1039
 error_bad_host:
1020 1040
 	LOG(L_ERR, "ERROR: parse_uri: bad host in uri (error at char %c in"
1021 1041
 			" state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
1022
-			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1023
-	ser_error=E_BAD_URI;
1024
-	return E_BAD_URI;
1042
+			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf),
1043
+			len, ZSW(buf), len);
1044
+	goto error_exit;
1025 1045
 error_bad_port:
1026 1046
 	LOG(L_ERR, "ERROR: parse_uri: bad port in uri (error at char %c in"
1027 1047
 			" state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
1028
-			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1029
-	ser_error=E_BAD_URI;
1030
-	return E_BAD_URI;
1048
+			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf),
1049
+			len, ZSW(buf), len);
1050
+	goto error_exit;
1031 1051
 error_bad_uri:
1032 1052
 	LOG(L_ERR, "ERROR: parse_uri: bad uri,  state %d"
1033 1053
 			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
1034
-			 state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1035
-	ser_error=E_BAD_URI;
1036
-	return E_BAD_URI;
1054
+			 state, (int)(p-buf), ZSW(buf), (int)(p-buf), len,
1055
+			 ZSW(buf), len);
1056
+	goto error_exit;
1037 1057
 error_headers:
1038 1058
 	LOG(L_ERR, "ERROR: parse_uri: bad uri headers: <%.*s>(%d)"
1039 1059
 			" / <%.*s>(%d)\n",
1040 1060
 			uri->headers.len, ZSW(uri->headers.s), uri->headers.len,
1041 1061
 			len, ZSW(buf), len);
1042
-	ser_error=E_BAD_URI;
1043
-	return E_BAD_URI;
1062
+	goto error_exit;
1044 1063
 error_bug:
1045 1064
 	LOG(L_CRIT, "BUG: parse_uri: bad  state %d"
1046 1065
 			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
1047 1066
 			 state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1067
+error_exit:
1048 1068
 	ser_error=E_BAD_URI;
1069
+	uri->type=ERROR_URI_T;
1049 1070
 	return E_BAD_URI;
1050 1071
 }
1051 1072
 
... ...
@@ -1,1037 +1,22 @@
1 1
 
2 2
 #include <stdio.h>
3
+#include <stdlib.h> /* exit() */
3 4
 #include <string.h>
4 5
 #include "../str.h"
5 6
 
6 7
 /* ser compat defs */
7
-
8
-#define DBG printf
9
-#define LOG(lev, fmt, args...) printf(fmt, ## args)
10
-
11 8
 #define EXTRA_DEBUG
12
-#define ZSW(s)	((s)?(s):"")
13
-#define E_BAD_URI -100
14
-
15
-int ser_error=0;
16
-
17
-
18
-enum {PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP };
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
-
23
-struct sip_uri {
24
-	str user;     /* Username */
25
-	str passwd;   /* Password */
26
-	str host;     /* Host name */
27
-	str port;     /* Port number */
28
-	str params;   /* Parameters */
29
-	str headers;  
30
-	unsigned short port_no;
31
-	unsigned short proto; /* from transport */
32
-	uri_type type; /* uri scheme */
33
-	/* parameters */
34
-	str transport;
35
-	str ttl;
36
-	str user_param;
37
-	str maddr;
38
-	str method;
39
-	str lr;
40
-	str r2; /* ser specific rr parameter */
41
-	/* values */
42
-	str transport_val;
43
-	str ttl_val;
44
-	str user_param_val;
45
-	str maddr_val;
46
-	str method_val;
47
-	str lr_val; /* lr value placeholder for lr=on a.s.o*/
48
-	str r2_val;
49
-};
50
-
51
-
52
-
53
-
54
-int parse_uri(char* buf, int len, struct sip_uri* uri)
55
-{
56
-	enum states  {	URI_INIT, URI_USER, URI_PASSWORD, URI_PASSWORD_ALPHA,
57
-					URI_HOST, URI_HOST_P,
58
-					URI_HOST6_P, URI_HOST6_END, URI_PORT, 
59
-					URI_PARAM, URI_PARAM_P, URI_VAL_P, URI_HEADERS,
60
-					/* param states */
61
-					/* transport */
62
-					PT_T, PT_R, PT_A, PT_N, PT_S, PT_P, PT_O, PT_R2, PT_T2,
63
-					PT_eq,
64
-					/* ttl */
65
-					      PTTL_T2, PTTL_L, PTTL_eq,
66
-					/* user */
67
-					PU_U, PU_S, PU_E, PU_R, PU_eq,
68
-					/* method */
69
-					PM_M, PM_E, PM_T, PM_H, PM_O, PM_D, PM_eq,
70
-					/* maddr */
71
-					      PMA_A, PMA_D, PMA_D2, PMA_R, PMA_eq,
72
-					/* lr */
73
-					PLR_L, PLR_R_FIN, PLR_eq,
74
-					/* r2 */
75
-					PR2_R, PR2_2_FIN, PR2_eq,
76
-					
77
-					/* transport values */
78
-					/* udp */
79
-					VU_U, VU_D, VU_P_FIN,
80
-					/* tcp */
81
-					VT_T, VT_C, VT_P_FIN,
82
-					/* tls */
83
-					      VTLS_L, VTLS_S_FIN,
84
-					/* sctp */
85
-					VS_S, VS_C, VS_T, VS_P_FIN
86
-	};
87
-	register enum states state;
88
-	char* s;
89
-	char* b; /* param start */
90
-	char *v; /* value start */
91
-	str* param; /* current param */
92
-	str* param_val; /* current param val */
93
-	str user;
94
-	str password;
95
-	int port_no;
96
-	register char* p;
97
-	char* end;
98
-	char* pass;
99
-	int found_user;
100
-	int error_headers;
101
-	unsigned int scheme;
102
-	
103
-#define SIP_SCH		0x3a706973
104
-#define SIPS_SCH	0x73706973
105
-#define TEL_SCH		0x3a6c6574
106
-	
107
-#define case_port( ch, var) \
108
-	case ch: \
109
-			 (var)=(var)*10+ch-'0'; \
110
-			 break
111
-			 
112
-#define still_at_user  \
113
-						if (found_user==0){ \
114
-							user.s=uri->host.s; \
115
-							if (pass){\
116
-								user.len=pass-user.s; \
117
-								password.s=pass+1; \
118
-								password.len=p-password.s; \
119
-							}else{ \
120
-								user.len=p-user.s; \
121
-							}\
122
-							/* everything else is 0 */ \
123
-							memset(uri, 0, sizeof(struct sip_uri)); \
124
-							/* copy user & pass */ \
125
-							uri->user=user; \
126
-							if (pass)	uri->passwd=password;  \
127
-							s=p+1; \
128
-							found_user=1;\
129
-							error_headers=0; \
130
-							state=URI_HOST; \
131
-						}else goto error_bad_char 
132
-
133
-#define check_host_end \
134
-					case ':': \
135
-						/* found the host */ \
136
-						uri->host.s=s; \
137
-						uri->host.len=p-s; \
138
-						state=URI_PORT; \
139
-						s=p+1; \
140
-						break; \
141
-					case ';': \
142
-						uri->host.s=s; \
143
-						uri->host.len=p-s; \
144
-						state=URI_PARAM; \
145
-						s=p+1; \
146
-						break; \
147
-					case '?': \
148
-						uri->host.s=s; \
149
-						uri->host.len=p-s; \
150
-						state=URI_HEADERS; \
151
-						s=p+1; \
152
-						break; \
153
-					case '&': \
154
-					case '@': \
155
-						goto error_bad_char 
156
-
9
+#include "../parser/parse_uri.c"
10
+#include "../dprint.c"
157 11
 
158
-#define param_set(t_start, v_start) \
159
-					param->s=(t_start);\
160
-					param->len=(p-(t_start));\
161
-					param_val->s=(v_start); \
162
-					param_val->len=(p-(v_start)) 
163 12
 
164
-#define semicolon_case \
165
-					case';': \
166
-						if (pass){ \
167
-							found_user=1;/* no user, pass cannot contain ';'*/ \
168
-							pass=0; \
169
-						} \
170
-						state=URI_PARAM   /* new param */ 
171
-
172
-#define question_case \
173
-					case '?': \
174
-						uri->params.s=s; \
175
-						uri->params.len=p-s; \
176
-						state=URI_HEADERS; \
177
-						s=p+1; \
178
-						if (pass){ \
179
-							found_user=1;/* no user, pass cannot contain '?'*/ \
180
-							pass=0; \
181
-						}
182
-
183
-#define colon_case \
184
-					case ':': \
185
-						if (found_user==0){ \
186
-							/*might be pass but only if user not found yet*/ \
187
-							if (pass){ \
188
-								found_user=1; /* no user */ \
189
-								pass=0; \
190
-							}else{ \
191
-								pass=p; \
192
-							} \
193
-						} \
194
-						state=URI_PARAM_P /* generic param */
195
-
196
-#define param_common_cases \
197
-					case '@': \
198
-						/* ughhh, this is still the user */ \
199
-						still_at_user; \
200
-						break; \
201
-					semicolon_case; \
202
-						break; \
203
-					question_case; \
204
-						break; \
205
-					colon_case; \
206
-						break
207
-
208
-#define value_common_cases \
209
-					case '@': \
210
-						/* ughhh, this is still the user */ \
211
-						still_at_user; \
212
-						break; \
213
-					semicolon_case; \
214
-						param_set(b, v); \
215
-						break; \
216
-					question_case; \
217
-						param_set(b, v); \
218
-						break; \
219
-					colon_case; \
220
-						state=URI_VAL_P; \
221
-						break
222
-
223
-#define param_switch(old_state, c1, c2, new_state) \
224
-			case old_state: \
225
-				switch(*p){ \
226
-					case c1: \
227
-					case c2: \
228
-						state=(new_state); \
229
-						break; \
230
-					param_common_cases; \
231
-					default: \
232
-						state=URI_PARAM_P; \
233
-				} \
234
-				break
235
-#define param_switch1(old_state, c1, new_state) \
236
-			case old_state: \
237
-				switch(*p){ \
238
-					case c1: \
239
-						state=(new_state); \
240
-						break; \
241
-					param_common_cases; \
242
-					default: \
243
-						state=URI_PARAM_P; \
244
-				} \
245
-				break
246
-#define param_switch_big(old_state, c1, c2, d1, d2, new_state_c, new_state_d) \
247
-			case old_state : \
248
-				switch(*p){ \
249
-					case c1: \
250
-					case c2: \
251
-						state=(new_state_c); \
252
-						break; \
253
-					case d1: \
254
-					case d2: \
255
-						state=(new_state_d); \
256
-						break; \
257
-					param_common_cases; \
258
-					default: \
259
-						state=URI_PARAM_P; \
260
-				} \
261
-				break
262
-#define value_switch(old_state, c1, c2, new_state) \
263
-			case old_state: \
264
-				switch(*p){ \
265
-					case c1: \
266
-					case c2: \
267
-						state=(new_state); \
268
-						break; \
269
-					value_common_cases; \
270
-					default: \
271
-						state=URI_VAL_P; \
272
-				} \
273
-				break
274
-#define value_switch_big(old_state, c1, c2, d1, d2, new_state_c, new_state_d) \
275
-			case old_state: \
276
-				switch(*p){ \
277
-					case c1: \
278
-					case c2: \
279
-						state=(new_state_c); \
280
-						break; \
281
-					case d1: \
282
-					case d2: \
283
-						state=(new_state_d); \
284
-						break; \
285
-					value_common_cases; \
286
-					default: \
287
-						state=URI_VAL_P; \
288
-				} \
289
-				break
290
-
291
-#define transport_fin(c_state, proto_no) \
292
-			case c_state: \
293
-				switch(*p){ \
294
-					case '@': \
295
-						still_at_user; \
296
-						break; \
297
-					semicolon_case; \
298
-						param_set(b, v); \
299
-						uri->proto=(proto_no); \
300
-						break; \
301
-					question_case; \
302
-						param_set(b, v); \
303
-						uri->proto=(proto_no); \
304
-						break; \
305
-					colon_case;  \
306
-					default: \
307
-						state=URI_VAL_P; \
308
-						break; \
309
-				} \
310
-				break
311
-			
312
-	
313
-
314
-	/* init */
315
-	end=buf+len;
316
-	p=buf+4;
317
-	found_user=0;
318
-	error_headers=0;
319
-	b=v=0;
320
-	param=param_val=0;
321
-	pass=0;
322
-	port_no=0;
323
-	state=URI_INIT;
324
-	memset(uri, 0, sizeof(struct sip_uri)); /* zero it all, just to be sure*/
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;
337
-	
338
-	s=p;
339
-	for(;p<end; p++){
340
-		switch((unsigned char)state){
341
-			case URI_INIT:
342
-				switch(*p){
343
-					case '[':
344
-						/* uri =  [ipv6address]... */
345
-						state=URI_HOST6_P;
346
-						s=p;
347
-						break;
348
-					case ']':
349
-						/* invalid, no uri can start with ']' */
350
-					case ':':
351
-						/* the same as above for ':' */
352
-						goto error_bad_char;
353
-					case '@': /* error no user part, or
354
-								 be forgiving and accept it ? */
355
-					default:
356
-						state=URI_USER;
357
-				}
358
-				break; 
359
-			case URI_USER:
360
-				switch(*p){
361
-					case '@':
362
-						/* found the user*/
363
-						uri->user.s=s;
364
-						uri->user.len=p-s;
365
-						state=URI_HOST;
366
-						found_user=1;
367
-						s=p+1; /* skip '@' */
368
-						break;
369
-					case ':':
370
-						/* found the user, or the host? */
371
-						uri->user.s=s;
372
-						uri->user.len=p-s;
373
-						state=URI_PASSWORD;
374
-						s=p+1; /* skip ':' */
375
-						break;
376
-					case ';':
377
-						/* this could be still the user or
378
-						 * params?*/
379
-						uri->host.s=s;
380
-						uri->host.len=p-s;
381
-						state=URI_PARAM;
382
-						s=p+1;
383
-						break;
384
-					case '?': /* still user or headers? */
385
-						uri->host.s=s;
386
-						uri->host.len=p-s;
387
-						state=URI_HEADERS;
388
-						s=p+1;
389
-						break;
390
-						/* almost anything permitted in the user part */
391
-					case '[':
392
-					case ']': /* the user part cannot contain "[]" */
393
-						goto error_bad_char;
394
-				}
395
-				break;
396
-			case URI_PASSWORD: /* this can also be the port (missing user)*/
397
-				switch(*p){
398
-					case '@':
399
-						/* found the password*/
400
-						uri->passwd.s=s;
401
-						uri->passwd.len=p-s;
402
-						port_no=0;
403
-						state=URI_HOST;
404
-						found_user=1;
405
-						s=p+1; /* skip '@' */
406
-						break;
407
-					case ';':
408
-						/* upps this is the port */
409
-						uri->port.s=s;
410
-						uri->port.len=p-s;
411
-						uri->port_no=port_no;
412
-						/* user contains in fact the host */
413
-						uri->host.s=uri->user.s;
414
-						uri->host.len=uri->user.len;
415
-						uri->user.s=0;
416
-						uri->user.len=0;
417
-						state=URI_PARAM;
418
-						found_user=1; /*  there is no user part */
419
-						s=p+1;
420
-						break;
421
-					case '?':
422
-						/* upps this is the port */
423
-						uri->port.s=s;
424
-						uri->port.len=p-s;
425
-						uri->port_no=port_no;
426
-						/* user contains in fact the host */
427
-						uri->host.s=uri->user.s;
428
-						uri->host.len=uri->user.len;
429
-						uri->user.s=0;
430
-						uri->user.len=0;
431
-						state=URI_HEADERS;
432
-						found_user=1; /*  there is no user part */
433
-						s=p+1;
434
-						break;
435
-					case_port('0', port_no);
436
-					case_port('1', port_no);
437
-					case_port('2', port_no);
438
-					case_port('3', port_no);
439
-					case_port('4', port_no);
440
-					case_port('5', port_no);
441
-					case_port('6', port_no);
442
-					case_port('7', port_no);
443
-					case_port('8', port_no);
444
-					case_port('9', port_no);
445
-					case '[':
446
-					case ']':
447
-					case ':':
448
-						goto error_bad_char;
449
-					default:
450
-						/* it can't be the port, non number found */
451
-						port_no=0;
452
-						state=URI_PASSWORD_ALPHA;
453
-				}
454
-				break;
455
-			case URI_PASSWORD_ALPHA:
456
-				switch(*p){
457
-					case '@':
458
-						/* found the password*/
459
-						uri->passwd.s=s;
460
-						uri->passwd.len=p-s;
461
-						state=URI_HOST;
462
-						found_user=1;
463
-						s=p+1; /* skip '@' */
464
-						break;
465
-					case ';': /* contains non-numbers => cannot be port no*/
466
-					case '?':
467
-						goto error_bad_port;
468
-					case '[':
469
-					case ']':
470
-					case ':':
471
-						goto error_bad_char;
472
-				}
473
-				break;
474
-			case URI_HOST:
475
-				switch(*p){
476
-					case '[':
477
-						state=URI_HOST6_P;
478
-						break;
479
-					case ':': 
480
-					case ';':
481
-					case '?': /* null host name ->invalid */
482
-					case '&':
483
-					case '@': /*chars not allowed in hosts names */
484
-						goto error_bad_host;
485
-					default:
486
-						state=URI_HOST_P;
487
-				}
488
-				break;
489
-			case URI_HOST_P:
490
-				switch(*p){
491
-					check_host_end;
492
-				}
493
-				break;
494
-			case URI_HOST6_END:
495
-				switch(*p){
496
-					check_host_end;
497
-					default: /*no chars allowed after [ipv6] */
498
-						goto error_bad_host;
499
-				}
500
-				break;
501
-			case URI_HOST6_P:
502
-				switch(*p){
503
-					case ']':
504
-						state=URI_HOST6_END;
505
-						break;
506
-					case '[':
507
-					case '&':
508
-					case '@':
509
-					case ';':
510
-					case '?':
511
-						goto error_bad_host;
512
-				}
513
-				break;
514
-			case URI_PORT:
515
-				switch(*p){
516
-					case ';':
517
-						uri->port.s=s;
518
-						uri->port.len=p-s;
519
-						uri->port_no=port_no;
520
-						state=URI_PARAM;
521
-						s=p+1;
522
-						break;
523
-					case '?':
524
-						uri->port.s=s;
525
-						uri->port.len=p-s;
526
-						uri->port_no=port_no;
527
-						state=URI_HEADERS;
528
-						s=p+1;
529
-						break;
530
-					case_port('0', port_no);
531
-					case_port('1', port_no);
532
-					case_port('2', port_no);
533
-					case_port('3', port_no);
534
-					case_port('4', port_no);
535
-					case_port('5', port_no);
536
-					case_port('6', port_no);
537
-					case_port('7', port_no);
538
-					case_port('8', port_no);
539
-					case_port('9', port_no);
540
-					case '&':
541
-					case '@':
542
-					case ':':
543
-					default:
544
-						goto error_bad_port;
545
-				}
546
-				break;
547
-			case URI_PARAM: /* beginning of a new param */
548
-				switch(*p){
549
-					param_common_cases;
550
-					/* recognized params */
551
-					case 't':
552
-					case 'T':
553
-						b=p;
554
-						state=PT_T;
555
-						break;
556
-					case 'u':
557
-					case 'U':
558
-						b=p;
559
-						state=PU_U;
560
-						break;
561
-					case 'm':
562
-					case 'M':
563
-						b=p;
564
-						state=PM_M;
565
-						break;
566
-					case 'l':
567
-					case 'L':
568
-						b=p;
569
-						state=PLR_L;
570
-						break;
571
-					case 'r':
572
-					case 'R':
573
-						b=p;
574
-						state=PR2_R;
575
-					default:
576
-						state=URI_PARAM_P;
577
-				}
578
-				break;
579
-			case URI_PARAM_P: /* ignore current param */
580
-				/* supported params:
581
-				 *  maddr, transport, ttl, lr, user, method, r2  */
582
-				switch(*p){
583
-					param_common_cases;
584
-				};
585
-				break;
586
-			/* ugly but fast param names parsing */
587
-			/*transport */
588
-			param_switch_big(PT_T,  'r', 'R', 't', 'T', PT_R, PTTL_T2);
589
-			param_switch(PT_R,  'a', 'A', PT_A);
590
-			param_switch(PT_A,  'n', 'N', PT_N);
591
-			param_switch(PT_N,  's', 'S', PT_S);
592
-			param_switch(PT_S,  'p', 'P', PT_P);
593
-			param_switch(PT_P,  'o', 'O', PT_O);
594
-			param_switch(PT_O,  'r', 'R', PT_R2);
595
-			param_switch(PT_R2, 't', 'T', PT_T2);
596
-			param_switch1(PT_T2, '=',  PT_eq);
597
-			/* value parsing */
598
-			case PT_eq:
599
-				param=&uri->transport;
600
-				param_val=&uri->transport_val;
601
-				switch (*p){
602
-					param_common_cases;
603
-					case 'u':
604
-					case 'U':
605
-						v=p;
606
-						state=VU_U;
607
-						break;
608
-					case 't':
609
-					case 'T':
610
-						v=p;
611
-						state=VT_T;
612
-						break;
613
-					case 's':
614
-					case 'S':
615
-						v=p;
616
-						state=VS_S;
617
-						break;
618
-					default:
619
-						v=p;
620
-						state=URI_VAL_P;
621
-				}
622
-				break;
623
-				/* generic value */
624
-			case URI_VAL_P:
625
-				switch(*p){
626
-					value_common_cases;
627
-				}
628
-				break;
629
-			/* udp */
630
-			value_switch(VU_U,  'd', 'D', VU_D);
631
-			value_switch(VU_D,  'p', 'P', VU_P_FIN);
632
-			transport_fin(VU_P_FIN, PROTO_UDP);
633
-			/* tcp */
634
-			value_switch_big(VT_T,  'c', 'C', 'l', 'L', VT_C, VTLS_L);
635
-			value_switch(VT_C,  'p', 'P', VT_P_FIN);
636
-			transport_fin(VT_P_FIN, PROTO_TCP);
637
-			/* tls */
638
-			value_switch(VTLS_L, 's', 'S', VTLS_S_FIN);
639
-			transport_fin(VTLS_S_FIN, PROTO_TLS);
640
-			/* sctp */
641
-			value_switch(VS_S, 'c', 'C', VS_C);
642
-			value_switch(VS_C, 't', 'T', VS_T);
643
-			value_switch(VS_T, 'p', 'P', VS_P_FIN);
644
-			transport_fin(VS_P_FIN, PROTO_SCTP);
645
-			
646
-			/* ttl */
647
-			param_switch(PTTL_T2,  'l', 'L', PTTL_L);
648
-			param_switch1(PTTL_L,  '=', PTTL_eq);
649
-			case PTTL_eq:
650
-				param=&uri->ttl;
651
-				param_val=&uri->ttl_val;
652
-				switch(*p){
653
-					param_common_cases;
654
-					default:
655
-						v=p;
656
-						state=URI_VAL_P;
657
-				}
658
-				break;
659
-			
660
-			/* user param */
661
-			param_switch(PU_U, 's', 'S', PU_S);
662
-			param_switch(PU_S, 'e', 'E', PU_E);
663
-			param_switch(PU_E, 'r', 'R', PU_R);
664
-			param_switch1(PU_R, '=', PU_eq);
665
-			case PU_eq:
666
-				param=&uri->user_param;
667
-				param_val=&uri->user_param_val;
668
-				switch(*p){
669
-					param_common_cases;
670
-					default:
671
-						v=p;
672
-						state=URI_VAL_P;
673
-				}
674
-				break;
675
-			
676
-			/* method*/
677
-			param_switch_big(PM_M, 'e', 'E', 'a', 'A', PM_E, PMA_A);
678
-			param_switch(PM_E, 't', 'T', PM_T);
679
-			param_switch(PM_T, 'h', 'H', PM_H);
680
-			param_switch(PM_H, 'o', 'O', PM_O);
681
-			param_switch(PM_O, 'd', 'D', PM_D);
682
-			param_switch1(PM_D, '=', PM_eq);
683
-			case PM_eq:
684
-				param=&uri->method;
685
-				param_val=&uri->method_val;
686
-				switch(*p){
687
-					param_common_cases;
688
-					default:
689
-						v=p;
690
-						state=URI_VAL_P;
691
-				}
692
-				break;
13
+int ser_error=0;
14
+int debug=L_DBG;
15
+int log_stderr=1;
16
+int log_facility=LOG_DAEMON;
17
+int process_no=0;
18
+struct process_table* pt=0;
693 19
 
694
-			/*maddr*/
695
-			param_switch(PMA_A,  'd', 'D', PMA_D);
696
-			param_switch(PMA_D,  'd', 'D', PMA_D2);
697
-			param_switch(PMA_D2, 'r', 'R', PMA_R);
698
-			param_switch1(PMA_R, '=', PMA_eq);
699
-			case PMA_eq:
700
-				param=&uri->maddr;
701
-				param_val=&uri->maddr_val;
702
-				switch(*p){
703
-					param_common_cases;
704
-					default:
705
-						v=p;
706
-						state=URI_VAL_P;
707
-				}
708
-				break;
709
-			
710
-			/* lr */
711
-			param_switch(PLR_L,  'r', 'R', PLR_R_FIN);
712
-			case PLR_R_FIN:
713
-				switch(*p){
714
-					case '@':
715
-						still_at_user; 
716
-						break;
717
-					case '=':
718
-						state=PLR_eq;
719
-						break;
720
-					semicolon_case; 
721
-						uri->lr.s=b;
722
-						uri->lr.len=(p-b);
723
-						break;
724
-					question_case; 
725
-						uri->lr.s=b;
726
-						uri->lr.len=(p-b);
727
-						break;
728
-					colon_case;
729
-						break;
730
-					default:
731
-						state=URI_PARAM_P;
732
-				}
733
-				break;
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;
780
-				
781
-				
782
-			case URI_HEADERS:
783
-				/* for now nobody needs them so we completely ignore the 
784
-				 * headers (they are not allowed in request uri) --andrei */
785
-				switch(*p){
786
-					case '@':
787
-						/* yak, we are still at user */
788
-						still_at_user;
789
-						break;
790
-					case ';':
791
-						/* we might be still parsing user, try it */
792
-						if (found_user) goto error_bad_char;
793
-						error_headers=1; /* if this is not the user
794
-											we have an error */
795
-						/* if pass is set => it cannot be user:pass
796
-						 * => error (';') is illegal in a header */
797
-						if (pass) goto error_headers;
798
-						break;
799
-					case ':':
800
-						if (found_user==0){
801
-							/*might be pass but only if user not found yet*/
802
-							if (pass){
803
-								found_user=1; /* no user */
804
-								pass=0;
805
-							}else{
806
-								pass=p;
807
-							}
808
-						}
809
-						break;
810
-					case '?':
811
-						if (pass){
812
-							found_user=1; /* no user, pass cannot contain '?'*/
813
-							pass=0;
814
-						}
815
-						break;
816
-				}
817
-				break;
818
-			default:
819
-				goto error_bug;
820
-		}
821
-	}
822
-	/*end of uri */
823
-	switch (state){
824
-		case URI_INIT: /* error empty uri */
825
-			goto error_too_short;
826
-		case URI_USER:
827
-			/* this is the host, it can't be the user */
828
-			if (found_user) goto error_bad_uri;
829
-			uri->host.s=s;
830
-			uri->host.len=p-s;
831
-			state=URI_HOST;
832
-			break;
833
-		case URI_PASSWORD:
834
-			/* this is the port, it can't be the passwd */
835
-			if (found_user) goto error_bad_port;
836
-			uri->port.s=s;
837
-			uri->port.len=p-s;
838
-			uri->port_no=port_no;
839
-			uri->host=uri->user;
840
-			uri->user.s=0;
841
-			uri->user.len=0;
842
-			break;
843
-		case URI_PASSWORD_ALPHA:
844
-			/* this is the port, it can't be the passwd */
845
-			goto error_bad_port;
846
-		case URI_HOST_P:
847
-		case URI_HOST6_END:
848
-			uri->host.s=s;
849
-			uri->host.len=p-s;
850
-			break;
851
-		case URI_HOST: /* error: null host */
852
-		case URI_HOST6_P: /* error: unterminated ipv6 reference*/
853
-			goto error_bad_host;
854
-		case URI_PORT:
855
-			uri->port.s=s;
856
-			uri->port.len=p-s;
857
-			uri->port_no=port_no;
858
-			break;
859
-		case URI_PARAM:
860
-		case URI_PARAM_P:
861
-		/* intermediate param states */
862
-		case PT_T: /* transport */
863
-		case PT_R:
864
-		case PT_A:
865
-		case PT_N:
866
-		case PT_S:
867
-		case PT_P:
868
-		case PT_O:
869
-		case PT_R2:
870
-		case PT_T2:
871
-		case PT_eq: /* ignore empty transport params */
872
-		case PTTL_T2: /* ttl */
873
-		case PTTL_L:
874
-		case PTTL_eq:
875
-		case PU_U:  /* user */
876
-		case PU_S:
877
-		case PU_E:
878
-		case PU_R:
879
-		case PU_eq:
880
-		case PM_M: /* method */
881
-		case PM_E:
882
-		case PM_T:
883
-		case PM_H:
884
-		case PM_O:
885
-		case PM_D:
886
-		case PM_eq:
887
-		case PLR_L: /* lr */
888
-		case PR2_R:  /* r2 */
889
-			uri->params.s=s;
890
-			uri->params.len=p-s;
891
-			break;
892
-		/* fin param states */
893
-		case PLR_R_FIN:
894
-		case PLR_eq:
895
-			uri->params.s=s;
896
-			uri->params.len=p-s;
897
-			uri->lr.s=b;
898
-			uri->lr.len=p-b;
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;
907
-		case URI_VAL_P:
908
-		/* intermediate value states */
909
-		case VU_U:
910
-		case VU_D:
911
-		case VT_T:
912
-		case VT_C:
913
-		case VTLS_L:
914
-		case VS_S:
915
-		case VS_C:
916
-		case VS_T:
917
-			uri->params.s=s;
918
-			uri->params.len=p-s;
919
-			param_set(b, v);
920
-			break;
921
-		/* fin value states */
922
-		case VU_P_FIN:
923
-			uri->params.s=s;
924
-			uri->params.len=p-s;
925
-			param_set(b, v);
926
-			uri->proto=PROTO_UDP;
927
-			break;
928
-		case VT_P_FIN:
929
-			uri->params.s=s;
930
-			uri->params.len=p-s;
931
-			param_set(b, v);
932
-			uri->proto=PROTO_TCP;
933
-			break;
934
-		case VTLS_S_FIN:
935
-			uri->params.s=s;
936
-			uri->params.len=p-s;
937
-			param_set(b, v);
938
-			uri->proto=PROTO_TLS;
939
-			break;
940
-		case VS_P_FIN:
941
-			uri->params.s=s;
942
-			uri->params.len=p-s;
943
-			param_set(b, v);
944
-			uri->proto=PROTO_SCTP;
945
-			break;
946
-		/* headers */
947
-		case URI_HEADERS:
948
-			uri->headers.s=s;
949
-			uri->headers.len=p-s;
950
-			if (error_headers) goto error_headers;
951
-			break;
952
-		default:
953
-			goto error_bug;
954
-	}
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
962
-	/* do stuff */
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
973
-		);
974
-	DBG(" uri params:\n   transport=<%.*s>, val=<%.*s>, proto=%d\n",
975
-			uri->transport.len, ZSW(uri->transport.s), uri->transport_val.len,
976
-			ZSW(uri->transport_val.s), uri->proto);
977
-	DBG("   user-param=<%.*s>, val=<%.*s>\n",
978
-			uri->user_param.len, ZSW(uri->user_param.s), 
979
-			uri->user_param_val.len, ZSW(uri->user_param_val.s));
980
-	DBG("   method=<%.*s>, val=<%.*s>\n",
981
-			uri->method.len, ZSW(uri->method.s), 
982
-			uri->method_val.len, ZSW(uri->method_val.s));
983
-	DBG("   ttl=<%.*s>, val=<%.*s>\n",
984
-			uri->ttl.len, ZSW(uri->ttl.s), 
985
-			uri->ttl_val.len, ZSW(uri->ttl_val.s));
986
-	DBG("   maddr=<%.*s>, val=<%.*s>\n",
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
991
-	return 0;
992
-	
993
-error_too_short:
994
-	LOG(L_ERR, "ERROR: parse_uri: uri too short: <%.*s> (%d)\n",
995
-			len, ZSW(buf), len);
996
-	ser_error=E_BAD_URI;
997
-	return E_BAD_URI;
998
-error_bad_char:
999
-	LOG(L_ERR, "ERROR: parse_uri: bad char '%c' in state %d"
1000
-			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
1001
-			*p, state, (int)(p-buf), ZSW(buf), (int)(p-buf), len, ZSW(buf), len);
1002
-	return E_BAD_URI;
1003
-error_bad_host:
1004
-	LOG(L_ERR, "ERROR: parse_uri: bad host in uri (error at char %c in"
1005
-			" state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
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;
1009
-error_bad_port:
1010
-	LOG(L_ERR, "ERROR: parse_uri: bad port in uri (error at char %c in"
1011
-			" state %d) parsed: <%.*s>(%d) /<%.*s> (%d)\n",
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;
1015
-error_bad_uri:
1016
-	LOG(L_ERR, "ERROR: parse_uri: bad uri,  state %d"
1017
-			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
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;
1021
-error_headers:
1022
-	LOG(L_ERR, "ERROR: parse_uri: bad uri headers: <%.*s>(%d)"
1023
-			" / <%.*s>(%d)\n",
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;
1028
-error_bug:
1029
-	LOG(L_CRIT, "BUG: parse_uri: bad  state %d"
1030
-			" parsed: <%.*s> (%d) / <%.*s> (%d)\n",
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;
1034
-}
1035 20
 
1036 21
 
1037 22
 int main (int argc, char** argv)