Browse code

-functional test full via parser Not tested: cr/lf behavaviour Todo: param parsing

Andrei Pelinescu-Onciul authored on 06/11/2001 18:55:46
Showing 1 changed files
... ...
@@ -17,6 +17,7 @@
17 17
 
18 18
 #include <stdio.h>
19 19
 
20
+/* main via states (uri:port ...) */
20 21
 enum{	         F_HOST,    P_HOST,
21 22
 		L_PORT,  F_PORT,    P_PORT,
22 23
 		L_PARAM, F_PARAM,   P_PARAM,
... ...
@@ -24,9 +25,16 @@ enum{	         F_HOST,    P_HOST,
24 25
 		         F_COMMENT, P_COMMENT,
25 26
 				 F_IP6HOST, P_IP6HOST,
26 27
 				 F_CRLF,
27
-				 F_LF
28
+				 F_LF,
29
+				 F_CR
28 30
 	};
29
-enum{
31
+
32
+/* first via part state */
33
+enum{	         F_SIP=100,
34
+		SIP1, SIP2, FIN_SIP,
35
+		L_VER, F_VER,
36
+		VER1, VER2, FIN_VER,
37
+		L_PROTO, F_PROTO, P_PROTO
30 38
 	};
31 39
 
32 40
 #define LOG(lev, fmt, args...) fprintf(stderr, fmt, ## args)
... ...
@@ -45,8 +53,9 @@ int main(int argc, char** argv)
45 53
 	char* param;
46 54
 	char* comment;
47 55
 	char* next_via;
56
+	char *proto; /* in fact transport*/
48 57
 
49
-	host=port_str=param=comment=next_via=0;
58
+	host=port_str=param=comment=next_via=proto=0;
50 59
 
51 60
 	printf(" %s (%d)\n", argv[0], argc);
52 61
 	if (argc<2){
... ...
@@ -61,22 +70,27 @@ int main(int argc, char** argv)
61 70
 			case ' ':
62 71
 			case'\t':
63 72
 				switch(state){
64
-					case L_SIP: /*eat space*/
65
-					case L_SLASH1:
66
-					case L_SLASH2:
67
-					case L_VER:
73
+					case L_VER: /* eat space */
68 74
 					case L_PROTO:
69 75
 					case F_SIP:
70 76
 					case F_VER:
71 77
 					case F_PROTO:
72 78
 						break;
73 79
 					case P_PROTO:
80
+						*tmp=0;  /* finished proto parsing */
81
+						state=F_HOST; /* start looking for host*/
82
+						goto main_via;
83
+					case FIN_SIP:
74 84
 						*tmp=0;
75
-						state=F_HOST;
85
+						state=L_VER;
86
+						break;
87
+					case FIN_VER:
88
+						*tmp=0;
89
+						state=L_PROTO;
76 90
 						break;
77 91
 					case F_LF:
78 92
 					case F_CRLF:
79
-					case F_CR:
93
+					case F_CR: /* header continues on this line */
80 94
 						state=saved_state;
81 95
 						break;
82 96
 					default:
... ...
@@ -87,20 +101,28 @@ int main(int argc, char** argv)
87 101
 				break;
88 102
 			case '\n':
89 103
 				switch(state){
90
-					case L_SIP:
91
-					case L_SLASH1:
92
-					case L_SLASH2:
93 104
 					case L_VER:
94 105
 					case F_SIP:
95 106
 					case F_VER:
96 107
 					case F_PROTO:
108
+					case L_PROTO:
97 109
 						saved_state=state;
98 110
 						state=F_LF;
99 111
 						break;
100 112
 					case P_PROTO:
101 113
 						*tmp=0;
102 114
 						state=F_LF;
103
-						saved_state=;
115
+						saved_state=F_HOST; /* start looking for host*/
116
+						goto main_via;
117
+					case FIN_SIP:
118
+						*tmp=0;
119
+						state=F_LF;
120
+						saved_state=L_VER;
121
+						break;
122
+					case FIN_VER:
123
+						*tmp=0;
124
+						state=F_LF;
125
+						saved_state=L_PROTO;
104 126
 						break;
105 127
 					case F_CR:
106 128
 						state=F_CRLF;
... ...
@@ -117,20 +139,28 @@ int main(int argc, char** argv)
117 139
 				break;
118 140
 			case '\r':
119 141
 				switch(state){
120
-					case L_SIP:
121
-					case L_SLASH1:
122
-					case L_SLASH2:
123 142
 					case L_VER:
124 143
 					case F_SIP:
125 144
 					case F_VER:
126 145
 					case F_PROTO:
146
+					case L_PROTO:
127 147
 						saved_state=state;
128 148
 						state=F_CR;
129 149
 						break;
130 150
 					case P_PROTO:
131 151
 						*tmp=0;
132 152
 						state=F_CR;
133
-						saved_state=;
153
+						saved_state=F_HOST;
154
+						goto main_via;
155
+					case FIN_SIP:
156
+						*tmp=0;
157
+						state=F_CR;
158
+						saved_state=L_VER;
159
+						break;
160
+					case FIN_VER:
161
+						*tmp=0;
162
+						state=F_CR;
163
+						saved_state=L_PROTO;
134 164
 						break;
135 165
 					case F_LF: /*end of line ?next header?*/
136 166
 					case F_CR:
... ...
@@ -144,29 +174,169 @@ int main(int argc, char** argv)
144 174
 				}
145 175
 				break;
146 176
 			
147
-			case '\\':
177
+			case '/':
148 178
 				switch(state){
149
-					case L_VER:
179
+					case FIN_SIP:
150 180
 						*tmp=0;
151 181
 						state=F_VER;
152 182
 						break;
153
-					case L_PROTO:
183
+					case FIN_VER:
154 184
 						*tmp=0;
155 185
 						state=F_PROTO;
156 186
 						break;
157
-					case L_
158
-
159
-
160
-						
161
-				
162
-								
163
-								
164
-
187
+					case L_VER:
188
+						state=F_VER;
189
+						break;
190
+					case L_PROTO:
191
+						state=F_PROTO;
192
+						break;
193
+					default:
194
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
195
+								" state %d\n", *tmp, state);
196
+						goto error;
197
+				}
198
+				break;
199
+				/* match SIP*/
200
+			case 'S':
201
+			case 's':
202
+				switch(state){
203
+					case F_SIP:
204
+						state=SIP1;
205
+						break;
206
+					/* allow S in PROTO */
207
+					case F_PROTO:
208
+						proto=tmp;
209
+						state=P_PROTO;
210
+						break;
211
+					case P_PROTO:
212
+						break;
213
+					default:
214
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
215
+								" state %d\n", *tmp, state);
216
+						goto error;
217
+				}
218
+				break;
219
+			case 'I':
220
+			case 'i':
221
+				switch(state){
222
+					case SIP1:
223
+						state=SIP2;
224
+						break;
225
+					/* allow i in PROTO */
226
+					case F_PROTO:
227
+						proto=tmp;
228
+						state=P_PROTO;
229
+						break;
230
+					case P_PROTO:
231
+						break;
232
+					default:
233
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
234
+								" state %d\n", *tmp, state);
235
+						goto error;
236
+				}
237
+				break;
238
+			case 'p':
239
+			case 'P':
240
+				switch(state){
241
+					case SIP2:
242
+						state=FIN_SIP;
243
+						break;
244
+					/* allow p in PROTO */
245
+					case F_PROTO:
246
+						proto=tmp;
247
+						state=P_PROTO;
248
+						break;
249
+					case P_PROTO:
250
+						break;
251
+					default:
252
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
253
+								" state %d\n", *tmp, state);
254
+						goto error;
255
+				}
256
+				break;
257
+			/*match 2.0*/
258
+			case '2':
259
+				switch(state){
260
+					case F_VER:
261
+						state=VER1;
262
+						break;
263
+					/* allow 2 in PROTO*/
264
+					case F_PROTO:
265
+						proto=tmp;
266
+						state=P_PROTO;
267
+						break;
268
+					case P_PROTO:
269
+						break;
270
+					default:
271
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
272
+								" state %d\n", *tmp, state);
273
+						goto error;
274
+				}
275
+				break;
276
+			case '.':
277
+				switch(state){
278
+					case VER1:
279
+						state=VER2;
280
+						break;
281
+					/* allow . in PROTO */
282
+					case F_PROTO:
283
+						proto=tmp;
284
+						state=P_PROTO;
285
+						break;
286
+					case P_PROTO:
287
+						break;
288
+					default:
289
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
290
+								" state %d\n", *tmp, state);
291
+						goto error;
292
+				}
293
+				 break;
294
+			case '0':
295
+				switch(state){
296
+					case VER2:
297
+						state=FIN_VER;
298
+						break;
299
+					/* allow 0 in PROTO*/
300
+					case F_PROTO:
301
+						proto=tmp;
302
+						state=P_PROTO;
303
+						break;
304
+					case P_PROTO:
305
+						break;
306
+					default:
307
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
308
+								" state %d\n", *tmp, state);
309
+						goto error;
310
+				}
311
+				break;
165 312
 			
166
-	}
313
+			default:
314
+				switch(state){
315
+					case F_PROTO:
316
+						proto=tmp;
317
+						state=P_PROTO;
318
+						break;
319
+					case P_PROTO:
320
+						break;
321
+					default:
322
+						LOG(L_ERR, "ERROR: parse_via: bad char <%c> on"
323
+								" state %d\n", *tmp, state);
324
+						goto error;
325
+				}
326
+				break;
327
+		}
328
+	} /* for tmp*/
167 329
 
330
+/* we should not be here! if everything is ok > main_via*/
331
+	LOG(L_ERR, "ERROR: parse_via: bad via: end of packet on state=%d\n",
332
+			state);
333
+	goto error;
334
+
335
+main_via:
336
+/* inc tmp to point to the next char*/
337
+	tmp++;
168 338
 	c_nest=0;
169
-	state=F_HOST;
339
+	/*state should always be F_HOST here*/;
170 340
 	for(;*tmp;tmp++){
171 341
 		switch(*tmp){
172 342
 			case ' ':
... ...
@@ -327,8 +497,6 @@ int main(int argc, char** argv)
327 497
 					case L_PARAM:
328 498
 					case F_PARAM:
329 499
 					case P_PARAM:
330
-					case F_COMMENT:
331
-					case P_COMMENT:
332 500
 						LOG(L_ERR, "ERROR:parse_via:"
333 501
 						" bad char <%c> in state %d\n",
334 502
 							*tmp,state);
... ...
@@ -343,7 +511,10 @@ int main(int argc, char** argv)
343 511
 					case F_CR:
344 512
 						/*previous=crlf and now !=' '*/
345 513
 						goto endofheader;
346
-					case F_COMMENT:
514
+					case F_COMMENT:/*everything is allowed in a comment*/
515
+						comment=tmp;
516
+						state=P_COMMENT;
517
+						break;
347 518
 					case P_COMMENT: /*everything is allowed in a comment*/
348 519
 						break;
349 520
 					default:
... ...
@@ -371,8 +542,6 @@ int main(int argc, char** argv)
371 542
 						state=F_PARAM;
372 543
 						break;
373 544
 					case F_PORT:
374
-					case F_COMMENT:
375
-					case P_COMMENT:
376 545
 						LOG(L_ERR, "ERROR:parse_via:"
377 546
 						" bad char <%c> in state %d\n",
378 547
 							*tmp,state);
... ...
@@ -396,7 +565,10 @@ int main(int argc, char** argv)
396 565
 					case F_CR:
397 566
 						/*previous=crlf and now !=' '*/
398 567
 						goto endofheader;
399
-					case F_COMMENT:
568
+					case F_COMMENT:/*everything is allowed in a comment*/
569
+						comment=tmp;
570
+						state=P_COMMENT;
571
+						break;
400 572
 					case P_COMMENT: /*everything is allowed in a comment*/
401 573
 						break;
402 574
 					
... ...
@@ -428,8 +600,6 @@ int main(int argc, char** argv)
428 600
 						break;
429 601
 					case F_PORT:
430 602
 					case F_PARAM:
431
-					case F_COMMENT:
432
-					case P_COMMENT:
433 603
 						LOG(L_ERR, "ERROR:parse_via:"
434 604
 						" invalid char <%c> in state"
435 605
 						" %d\n", *tmp,state);
... ...
@@ -442,7 +612,10 @@ int main(int argc, char** argv)
442 612
 					case F_CR:
443 613
 						/*previous=crlf and now !=' '*/
444 614
 						goto endofheader;
445
-					case F_COMMENT:
615
+					case F_COMMENT:/*everything is allowed in a comment*/
616
+						comment=tmp;
617
+						state=P_COMMENT;
618
+						break;
446 619
 					case P_COMMENT: /*everything is allowed in a comment*/
447 620
 						break;
448 621
 					default:
... ...
@@ -547,7 +720,10 @@ int main(int argc, char** argv)
547 720
 					case F_HOST:
548 721
 						state=F_IP6HOST;
549 722
 						break;
550
-					case F_COMMENT:
723
+					case F_COMMENT:/*everything is allowed in a comment*/
724
+						comment=tmp;
725
+						state=P_COMMENT;
726
+						break;
551 727
 					case P_COMMENT:
552 728
 						break;
553 729
 					case F_CRLF:
... ...
@@ -573,6 +749,12 @@ int main(int argc, char** argv)
573 749
 					case F_CR:
574 750
 						/*previous=crlf and now !=' '*/
575 751
 						goto endofheader;
752
+					case F_COMMENT:/*everything is allowed in a comment*/
753
+						comment=tmp;
754
+						state=P_COMMENT;
755
+						break;
756
+					case P_COMMENT:
757
+						break;
576 758
 					default:
577 759
 						LOG(L_ERR,"ERROR:parse_via"
578 760
 							" on <%c> state %d\n",
... ...
@@ -605,7 +787,7 @@ int main(int argc, char** argv)
605 787
 					case F_VIA:
606 788
 						next_via=tmp;
607 789
 						printf("found new via on <%c>\n", *tmp);
608
-						goto skip;
790
+						goto nextvia;
609 791
 					case L_PORT:
610 792
 					case L_PARAM:
611 793
 					case L_VIA:
... ...
@@ -642,21 +824,42 @@ int main(int argc, char** argv)
642 824
 					
643 825
 		}			
644 826
 	}
645
-goto endofheader;
646
-error:
647
-	fprintf(stderr, "error\n");
827
+
828
+	printf("end of packet reached, state=%d\n", state);
829
+	goto endofpacket; /*end of packet, probably should be goto error*/
648 830
 	
649
-skip:
650
-	printf("skipping\n");
651 831
 endofheader:
832
+	state=saved_state;
652 833
 	printf("end of header reached, state=%d\n", state);
653
-	if (host) printf("host=%s\n", host);
654
-	if (port) printf("port=%s\n", port_str);
655
-	if (param) printf("params=%s\n", param);
656
-	if (comment) printf("comment=%s\n", comment);
657
-	if(next_via) printf("next_via=%s\n", next_via);
834
+endofpacket:
835
+	/* check if error*/
836
+	switch(state){
837
+		case P_HOST:
838
+		case L_PORT:
839
+		case P_PORT:
840
+		case L_PARAM:
841
+		case P_PARAM:
842
+		case L_VIA:
843
+			break;
844
+		default:
845
+			LOG(L_ERR, "ERROR: parse_via: invalid via - end of header in"
846
+					" state %d\n", state);
847
+			goto error;
848
+	}
849
+		
850
+nextvia:
851
+	if (proto) printf("<SIP/2.0/%s<\n", proto);
852
+	if (host) printf("host= <%s>\n", host);
853
+	if (port_str) printf("port= <%s>\n", port_str);
854
+	if (param) printf("params= <%s>\n", param);
855
+	if (comment) printf("comment= <%s>\n", comment);
856
+	if(next_via) printf("next_via= <%s>\n", next_via);
658 857
 	printf("rest=<%s>\n", tmp);
659 858
 	
660 859
 	exit(0);
860
+
861
+error:
862
+	fprintf(stderr, "via parse error\n");
863
+	exit(-1);
661 864
 }
662 865