8c934c5a04f970d91f7e34ca6a87e86d6fe05732
[sip-router] / parser / obsolete / parse_hname.c
1 /*
2  * $Id$
3  *
4  * header name parsing automaton:
5
6  * parsing:           compact form:
7  * Via:               v:
8  * From:              f:
9  * To:                t:
10  * Cseq:              n/a
11  * Call-ID:           i:
12  * Contact:           m:
13  * Max-Forwards:      n/a
14  * Route:             n/a
15  * Record-Route:      n/a
16  */
17
18
19 #include "msg_parser.h"
20 #include "../dprint.h"
21
22 enum { INITIAL=0,
23                 VIA1, VIA2,
24                 FROM1, FROM2, FROM3,
25                 TO1,
26                 C_START, CSEQ2, CSEQ3,
27                          CALLID2, CALLID3, CALLID4, CALLID5, CALLID6,
28                          CONTACT2, CONTACT3, CONTACT4, CONTACT5, CONTACT6, 
29                 M_FOUND,      MAXFORWARDS2, MAXFORWARDS3, MAXFORWARDS4, MAXFORWARDS5,
30                 MAXFORWARDS6, MAXFORWARDS7, MAXFORWARDS8, MAXFORWARDS9, MAXFORWARDS10,
31                 MAXFORWARDS11,
32                 ROUTE1, ROUTE2, ROUTE3, ROUTE4,
33                 RECROUTE1, RECROUTE2, RECROUTE3, RECROUTE4, RECROUTE5, 
34                 RECROUTE6, RECROUTE7, RECROUTE8, RECROUTE9, RECROUTE10,
35
36                 /* final states*/
37                 F_VIA=1000, F_FROM, F_TO, F_CSEQ, F_CALLID, F_CONTACT, F_MAXFORWARDS,
38                 F_ROUTE, F_RECROUTE,
39                 I_START,
40
41                 UNKNOWN_HEADER=200,
42                 BODY=220,
43                 LF=25000
44         };
45
46
47 /* returns end or pointer to next elem*/
48 char* parse_hname1(char* p, char* end, struct hdr_field* hdr)
49 {
50
51         char* t;
52         register int state;
53         int err;
54         err=0;
55
56         state=INITIAL;
57                 
58         for(t=p;t<end;t++){
59                 switch(*t){
60                         case 'V':
61                         case 'v':
62                                         switch(state){
63                                                 case INITIAL:
64                                                         state=VIA1;
65                                                         hdr->name.s=t;
66                                                         break;
67                                                 case UNKNOWN_HEADER: break;
68                                                 default:
69                                                         state=UNKNOWN_HEADER;
70                                         }
71                                         break;
72                         case 'I':
73                         case 'i':
74                                         switch(state){
75                                                 case VIA1:
76                                                         state=VIA2;
77                                                         break;
78                                                 case CALLID5:
79                                                         state=CALLID6;
80                                                         break;
81                                                 case INITIAL:
82                                                         state=I_START;
83                                                         hdr->name.s=t;
84                                                         break;
85                                                 case UNKNOWN_HEADER: break;
86                                                 default:
87                                                         state=UNKNOWN_HEADER;
88                                         }
89                                         break;
90                         case 'A':
91                         case 'a':
92                                         switch(state){
93                                                 case INITIAL:
94                                                                 state=UNKNOWN_HEADER;
95                                                                 hdr->name.s=t;
96                                                                 break;
97                                                 case VIA2:
98                                                                 state=F_VIA;
99                                                                 break;
100                                                 case C_START:  /*CALLID1*/
101                                                                 state=CALLID2;
102                                                                 break;
103                                                 case CONTACT4:
104                                                                 state=CONTACT5;
105                                                                 break;
106                                                 case M_FOUND:
107                                                                 state=MAXFORWARDS2;
108                                                                 break;
109                                                 case MAXFORWARDS8:
110                                                                 state=MAXFORWARDS9;
111                                                                 break;
112                                                 case UNKNOWN_HEADER: break;
113                                                 default:
114                                                                 state=UNKNOWN_HEADER;
115                                         }
116                                         break;
117                         case 'F':
118                         case 'f':
119                                         switch(state){
120                                                 case INITIAL:
121                                                                 state=FROM1;
122                                                                 hdr->name.s=t;
123                                                                 break;
124                                                 case MAXFORWARDS4:
125                                                                 state=MAXFORWARDS5;
126                                                                 break;
127                                                 case UNKNOWN_HEADER: break;
128                                                 default:
129                                                                 state=UNKNOWN_HEADER;
130                                         }
131                                         break;
132                         case 'R':
133                         case 'r':
134                                         switch(state){
135                                                 case INITIAL:
136                                                         state=ROUTE1;
137                                                         hdr->name.s=t;
138                                                         break;
139                                                 case FROM1:
140                                                         state=FROM2;
141                                                         break;
142                                                 case MAXFORWARDS6:
143                                                         state=MAXFORWARDS7;
144                                                         break;
145                                                 case MAXFORWARDS9:
146                                                         state=MAXFORWARDS10;
147                                                         break;
148                                                 case RECROUTE3:
149                                                         state=RECROUTE4;
150                                                         break;
151                                                 case RECROUTE6:
152                                                         state=RECROUTE7;
153                                                 case UNKNOWN_HEADER: break;
154                                                 default:
155                                                         state=UNKNOWN_HEADER;
156                                         }
157                                         break;
158                         case 'O':
159                         case 'o':
160                                         switch(state){
161                                                 case INITIAL:
162                                                                 state=UNKNOWN_HEADER;
163                                                                 hdr->name.s=t;
164                                                                 break;
165                                                 case FROM2:
166                                                         state=FROM3;
167                                                         break;
168                                                 case TO1:
169                                                         state=F_TO;
170                                                         break;
171                                                 case C_START: /*CONTACT1 */
172                                                         state=CONTACT2;
173                                                         break;
174                                                 case ROUTE1:
175                                                         state=ROUTE2;
176                                                         break;
177                                                 case MAXFORWARDS5:
178                                                         state=MAXFORWARDS6;
179                                                         break;
180                                                 case RECROUTE2:
181                                                         state=RECROUTE3;
182                                                         break;
183                                                 case RECROUTE7:
184                                                         state=RECROUTE8;
185                                                         break;
186                                                 case UNKNOWN_HEADER: break;
187                                                 default:
188                                                         state=UNKNOWN_HEADER;
189                                         }
190                                         break;
191                         case 'M':
192                         case 'm':
193                                                 switch(state){
194                                                         case INITIAL:
195                                                                 state=M_FOUND;
196                                                                 hdr->name.s=t;
197                                                                 break;
198                                                         case FROM3:
199                                                                 state=F_FROM;
200                                                                 break;
201                                                         case UNKNOWN_HEADER: break;
202                                                         default:
203                                                                 state=UNKNOWN_HEADER;
204                                                 }
205                                                 break;
206                         case 'T':
207                         case 't':
208                                                 switch(state){
209                                                         case INITIAL:
210                                                                 state=TO1;
211                                                                 hdr->name.s=t;
212                                                                 break;
213                                                         case CONTACT3:
214                                                                 state=CONTACT4;
215                                                                 break;
216                                                         case CONTACT6:
217                                                                 state=F_CONTACT;
218                                                                 break;
219                                                         case ROUTE3:
220                                                                 state=ROUTE4;
221                                                                 break;
222                                                         case RECROUTE9:
223                                                                 state=RECROUTE10;
224                                                                 break;
225                                                         case UNKNOWN_HEADER: break;
226                                                         default:
227                                                                 state=UNKNOWN_HEADER;
228                                                 }
229                                                 break;
230                         case 'C':
231                         case 'c':
232                                                 switch(state){
233                                                         case INITIAL:
234                                                                 state=C_START;
235                                                                 hdr->name.s=t;
236                                                                 break;
237                                                         case CONTACT5:
238                                                                 state=CONTACT6;
239                                                                 break;
240                                                         case RECROUTE1:
241                                                                 state=RECROUTE2;
242                                                                 break;
243                                                         case UNKNOWN_HEADER: break;
244                                                         default:
245                                                                 state=UNKNOWN_HEADER;
246                                                 }
247                                                 break;
248                         case 'S':
249                         case 's':
250                                                 switch(state){
251                                                         case INITIAL:
252                                                                 state=UNKNOWN_HEADER;
253                                                                 hdr->name.s=t;
254                                                                 break;
255                                                         case C_START:
256                                                                 state=CSEQ2;
257                                                                 break;
258                                                         case MAXFORWARDS11:
259                                                                 state=F_MAXFORWARDS;
260                                                                 break;
261                                                         case UNKNOWN_HEADER: break;
262                                                         default:
263                                                                 state=UNKNOWN_HEADER;
264                                                 }
265                                                 break;
266                         case 'E':
267                         case 'e':
268                                                 switch(state){
269                                                         case INITIAL:
270                                                                 state=UNKNOWN_HEADER;
271                                                                 hdr->name.s=t;
272                                                                 break;
273                                                         case CSEQ2:
274                                                                 state=CSEQ3;
275                                                                 break;
276                                                         case ROUTE4:
277                                                                 state=F_ROUTE;
278                                                                 break;
279                                                         case ROUTE1:
280                                                                 state=RECROUTE1;
281                                                                 break;
282                                                         case RECROUTE10:
283                                                                 state=F_RECROUTE;
284                                                                 break;
285                                                         case UNKNOWN_HEADER: break;
286                                                         default:
287                                                                 state=UNKNOWN_HEADER;
288                                                 }
289                                                 break;
290                         case 'Q':
291                         case 'q':
292                                                 switch(state){
293                                                         case INITIAL:
294                                                                 state=UNKNOWN_HEADER;
295                                                                 hdr->name.s=t;
296                                                                 break;
297                                                         case CSEQ3:
298                                                                 state=F_CSEQ;
299                                                                 break;
300                                                         case UNKNOWN_HEADER: break;
301                                                         default:
302                                                                 state=UNKNOWN_HEADER;
303                                                 }
304                                                 break;
305                         case 'L':
306                         case 'l':
307                                                 switch(state){
308                                                         case INITIAL:
309                                                                 state=UNKNOWN_HEADER;
310                                                                 hdr->name.s=t;
311                                                                 break;
312                                                         case CALLID2:
313                                                                 state=CALLID3;
314                                                                 break;
315                                                         case CALLID3:
316                                                                 state=CALLID4;
317                                                                 break;
318                                                         case UNKNOWN_HEADER: break;
319                                                         default:
320                                                                 state=UNKNOWN_HEADER;
321                                                 }
322                                                 break;
323                         case 'D':
324                         case 'd':
325                                                 switch(state){
326                                                         case INITIAL:
327                                                                 state=UNKNOWN_HEADER;
328                                                                 hdr->name.s=t;
329                                                                 break;
330                                                         case CALLID6:
331                                                                 state=F_CALLID;
332                                                                 break;
333                                                         case MAXFORWARDS10:
334                                                                 state=MAXFORWARDS11;
335                                                                 break;
336                                                         case RECROUTE4:
337                                                                 state=RECROUTE5;
338                                                                 break;
339                                                         case UNKNOWN_HEADER: break;
340                                                         default:
341                                                                 state=UNKNOWN_HEADER;
342                                                 }
343                                                 break;
344                         case 'N':
345                         case 'n':
346                                                 switch(state){
347                                                         case INITIAL:
348                                                                 state=UNKNOWN_HEADER;
349                                                                 hdr->name.s=t;
350                                                                 break;
351                                                         case CONTACT2:
352                                                                 state=CONTACT3;
353                                                                 break;
354                                                         case UNKNOWN_HEADER: break;
355                                                         default:
356                                                                 state=UNKNOWN_HEADER;
357                                                 }
358                                                 break;
359                         case 'X':
360                         case 'x':
361                                                 switch(state){
362                                                         case INITIAL:
363                                                                 state=UNKNOWN_HEADER;
364                                                                 hdr->name.s=t;
365                                                                 break;
366                                                         case MAXFORWARDS2:
367                                                                 state=MAXFORWARDS3;
368                                                                 break;
369                                                         case UNKNOWN_HEADER: break;
370                                                         default:
371                                                                 state=UNKNOWN_HEADER;
372                                                 }
373                                                 break;
374                         case '-':
375                                                 switch(state){
376                                                         case INITIAL:
377                                                                 state=UNKNOWN_HEADER;
378                                                                 hdr->name.s=t;
379                                                                 break;
380                                                         case CALLID4:
381                                                                 state=CALLID5;
382                                                                 break;
383                                                         case MAXFORWARDS3:
384                                                                 state=MAXFORWARDS4;
385                                                                 break;
386                                                         case RECROUTE5:
387                                                                 state=RECROUTE6;
388                                                                 break;
389                                                         case UNKNOWN_HEADER: break;
390                                                         default:
391                                                                 state=UNKNOWN_HEADER;
392                                                 }
393                                                 break;
394                         case 'W':
395                         case 'w':
396                                                 switch(state){
397                                                         case INITIAL:
398                                                                 state=UNKNOWN_HEADER;
399                                                                 hdr->name.s=t;
400                                                                 break;
401                                                         case MAXFORWARDS7:
402                                                                 state=MAXFORWARDS8;
403                                                                 break;
404                                                         case UNKNOWN_HEADER: break;
405                                                         default:
406                                                                 state=UNKNOWN_HEADER;
407                                                 }
408                                                 break;
409                         case 'U':
410                         case 'u':
411                                                 switch(state){
412                                                         case INITIAL:
413                                                                 state=UNKNOWN_HEADER;
414                                                                 hdr->name.s=t;
415                                                                 break;
416                                                         case ROUTE2:
417                                                                 state=ROUTE3;
418                                                                 break;
419                                                         case RECROUTE8:
420                                                                 state=RECROUTE9;
421                                                                 break;
422                                                         case UNKNOWN_HEADER: break;
423                                                         default:
424                                                                 state=UNKNOWN_HEADER;
425                                                 }
426                                                 break;
427                         case ' ':
428                                                 switch(state){
429                                                         case INITIAL:
430                                                                 break; /*eat space */
431                                                         case F_VIA:
432                                                         case F_FROM:
433                                                         case F_TO:
434                                                         case F_CSEQ:
435                                                         case F_CALLID:
436                                                         case F_CONTACT:
437                                                         case F_MAXFORWARDS:
438                                                         case F_ROUTE:
439                                                         case F_RECROUTE:
440                                                                 break; /* eat trailing space*/
441                                                         case VIA1:
442                                                                 /*compact form: v: */
443                                                                 state=F_VIA;
444                                                                 break;
445                                                         case FROM1:
446                                                                 /*compact form f:*/
447                                                                 state=F_FROM;
448                                                                 break;
449                                                         case TO1:
450                                                                 /*compact form t:*/
451                                                                 state=F_TO;
452                                                                 break;
453                                                         case I_START:
454                                                                 /*compact form i: (Call-ID)*/
455                                                                 state=F_CALLID;
456                                                                 break;
457                                                         case M_FOUND:
458                                                                 /*compact form m: (Contact)*/
459                                                                 state=F_CONTACT;
460                                                                 break;
461                                                         case UNKNOWN_HEADER: break;
462                                                         default:
463                                                                 state=UNKNOWN_HEADER;
464                                                 }
465                                                 break;
466                         case ':':
467                                                 switch(state){
468                                                         case F_VIA:
469                                                         case VIA1: /* compact form*/
470                                                                 *t=0;
471                                                                 hdr->name.len=t-hdr->name.s;
472                                                                 hdr->type=HDR_VIA;
473                                                                 goto skip;
474                                                         case F_FROM:
475                                                         case FROM1: /*compact form*/
476                                                                 *t=0;
477                                                                 hdr->name.len=t-hdr->name.s;
478                                                                 hdr->type=HDR_FROM;
479                                                                 goto skip;
480                                                         case F_TO:
481                                                         case TO1: /*compact form*/
482                                                                 *t=0;
483                                                                 hdr->name.len=t-hdr->name.s;
484                                                                 hdr->type=HDR_TO;
485                                                                 goto skip;
486                                                         case F_CSEQ:
487                                                                 *t=0;
488                                                                 hdr->name.len=t-hdr->name.s;
489                                                                 hdr->type=HDR_CSEQ;
490                                                                 goto skip;
491                                                         case F_CALLID:
492                                                         case I_START: /*compact form*/
493                                                                 *t=0;
494                                                                 hdr->name.len=t-hdr->name.s;
495                                                                 hdr->type=HDR_CALLID;
496                                                                 goto skip;
497                                                         case F_CONTACT:
498                                                         case M_FOUND: /*compact form*/
499                                                                 *t=0;
500                                                                 hdr->name.len=t-hdr->name.s;
501                                                                 hdr->type=HDR_CONTACT;
502                                                                 goto skip;
503                                                         case F_MAXFORWARDS:
504                                                                 *t=0;
505                                                                 hdr->name.len=t-hdr->name.s;
506                                                                 hdr->type=HDR_MAXFORWARDS;
507                                                                 goto skip;
508                                                         case F_ROUTE:
509                                                                 *t=0;
510                                                                 hdr->name.len=t-hdr->name.s;
511                                                                 hdr->type=HDR_ROUTE;
512                                                                 goto skip;
513                                                         case F_RECROUTE:
514                                                                 *t=0;
515                                                                 hdr->name.len=t-hdr->name.s;
516                                                                 hdr->type=HDR_RECORDROUTE;
517                                                                 goto skip;
518                                                         case UNKNOWN_HEADER:
519                                                                 *t=0;
520                                                                 hdr->name.len=t-hdr->name.s;
521                                                                 hdr->type=HDR_OTHER;
522                                                                 goto skip;
523                                                         default:
524                                                                 /* unknown header, e.g: "c:"*/
525                                                                 *t=0;
526                                                                 DBG("unknown header found, prev. state=%d\n",
527                                                                                 state);
528                                                                 state=UNKNOWN_HEADER;
529                                                                 hdr->name.len=t-hdr->name.s;
530                                                                 hdr->type=HDR_OTHER;
531                                                                 goto skip;
532                                                 }
533                                                 break;
534                                                 
535                         case '\n':
536                         case '\r': /*not allowed in hname*/
537                                                 goto error;
538                         
539                         default:
540                                         switch(state){
541                                                 case INITIAL:
542                                                         hdr->name.s=t;
543                                                         state=UNKNOWN_HEADER;
544                                                         break;
545                                                 case UNKNOWN_HEADER:
546                                                         break;
547                                                 default:
548                                                         state=UNKNOWN_HEADER;
549                                         }
550                 }
551         }
552 /* if we are here => we didn't find ':' => error*/
553         
554 error:
555         hdr->type=HDR_ERROR;
556         return t;
557
558 skip:
559         DBG("end of header name found, state=%d\n", state);
560         t++;
561         return t;
562
563 }
564