Coverage Report

Created: 2025-11-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_via.c
Line
Count
Source
1
/*
2
 * via parsing automaton
3
 *
4
 * Copyright (C) 2001-2003 FhG Fokus
5
 *
6
 * This file is part of opensips, a free SIP server.
7
 *
8
 * opensips is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version
12
 *
13
 * opensips is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
21
 */
22
23
24
25
/*
26
 *  2003-01-21  added rport parsing code, contributed by
27
 *               Maxim Sobolev  <sobomax@FreeBSD.org>
28
 *  2003-01-23  added extra via param parsing code (i=...), used
29
 *               by tcp to identify the sending socket, by andrei
30
 *  2003-01-23  fixed rport parsing code to accept rport w/o any value,
31
 *               by andrei
32
 *  2003-01-27  modified parse_via to set new via_param->start member and
33
 *               via->params.s (andrei)
34
 *  2003-01-28  zero-terminations replaced with VIA_ZT (jiri)
35
 *  2003-02-28  scratchpad compatibility abandoned (jiri)
36
 *  2003-04-26  ZSW (jiri)
37
 *  2003-06-23  fixed  parse_via_param [op].* param. parsing bug (andrei)
38
 *  2003-07-02  added support for TLS parsing in via (andrei)
39
 *  2003-10-27  added support for alias via param parsing [see
40
 *               draft-ietf-sip-connect-reuse-00.txt.]  (andrei)
41
 *  2004-03-31  fixed rport set instead of i bug (andrei)
42
 *  2005-03-02  if via has multiple bodies, and one of them is bad set
43
 *               also the first one as bad (andrei)
44
 */
45
46
47
48
#include <stdlib.h>
49
#include <string.h>
50
#include "../dprint.h"
51
#include "../ut.h"
52
#include "../ip_addr.h"
53
#include "../mem/mem.h"
54
#include "parse_via.h"
55
#include "parse_def.h"
56
57
58
59
/* main via states (uri:port ...) */
60
enum {
61
  F_HOST, P_HOST,
62
  L_PORT, F_PORT, P_PORT,
63
  L_PARAM, F_PARAM, P_PARAM,
64
  L_VIA, F_VIA,
65
  F_COMMENT, P_COMMENT,
66
  F_IP6HOST, P_IP6HOST,
67
  F_CRLF,
68
  F_LF,
69
  F_CR,
70
  END_OF_HEADER
71
};
72
73
74
/* first via part state */
75
enum {
76
  F_SIP = 100,
77
  SIP1, SIP2, FIN_SIP,
78
  L_VER, F_VER,
79
  VER1, VER2, FIN_VER,
80
  UDP1, UDP2, FIN_UDP,
81
  TCP_TLS1, TCP2, FIN_TCP,
82
  TLS2, FIN_TLS,
83
  SCTP1, SCTP2, SCTP3, FIN_SCTP,
84
  WS1, WS_WSS, FIN_WS, FIN_WSS,
85
  OTHER_PROTO,
86
  L_PROTO, F_PROTO
87
};
88
89
90
/* param related states
91
 * WARNING: keep in sync with parse_via.h, PARAM_HIDDEN, ...
92
 */
93
enum {
94
  L_VALUE = 200, F_VALUE, P_VALUE, P_STRING,
95
  HIDDEN1, HIDDEN2, HIDDEN3, HIDDEN4, HIDDEN5,
96
  TTL1, TTL2,
97
  BRANCH1, BRANCH2, BRANCH3, BRANCH4, BRANCH5,
98
  MADDR1, MADDR2, MADDR3, MADDR4,
99
  RECEIVED1, RECEIVED2, RECEIVED3, RECEIVED4, RECEIVED5, RECEIVED6,
100
  RECEIVED7,
101
  RPORT1, RPORT2, RPORT3,
102
  ALIAS1, ALIAS2, ALIAS3, ALIAS4,
103
       /* fin states (227-...)*/
104
  FIN_HIDDEN = 230, FIN_TTL, FIN_BRANCH,
105
  FIN_MADDR, FIN_RECEIVED, FIN_RPORT, FIN_I, FIN_ALIAS
106
       /*GEN_PARAM,
107
         PARAM_ERROR*/ /* declared in msg_parser.h*/
108
};
109
110
111
/* entry state must be F_PARAM, or saved_state=F_PARAM and
112
 * state=F_{LF,CR,CRLF}!
113
 * output state = L_PARAM or F_PARAM or END_OF_HEADER
114
 * (and saved_state= last state); everything else => error
115
 * WARNING: param->start must be filled before, it's used in param->size
116
 * computation.
117
 */
118
static /*inline*/ char* parse_via_param(char* p, char* end,
119
                    unsigned char* pstate,
120
                      unsigned char* psaved_state,
121
                    struct via_param* param)
122
140k
{
123
140k
  char* tmp;
124
140k
  register unsigned char state;
125
140k
  unsigned char saved_state;
126
127
140k
  state=*pstate;
128
129
140k
  saved_state=*psaved_state;
130
140k
  param->type=PARAM_ERROR;
131
132
1.74M
  for (tmp=p;tmp<end;tmp++){
133
1.74M
    switch(*tmp){
134
4.24k
      case ' ':
135
7.26k
      case '\t':
136
7.26k
        switch(state){
137
687
          case FIN_HIDDEN:
138
1.24k
          case FIN_ALIAS:
139
1.24k
            param->type=state;
140
1.24k
            param->name.len=tmp-param->name.s;
141
1.24k
            state=L_PARAM;
142
1.24k
            goto endofparam;
143
466
          case FIN_BRANCH:
144
1.00k
          case FIN_TTL:
145
1.42k
          case FIN_MADDR:
146
2.07k
          case FIN_RECEIVED:
147
2.96k
          case FIN_RPORT:
148
3.56k
          case FIN_I:
149
3.56k
            param->type=state;
150
3.56k
            param->name.len=tmp-param->name.s;
151
3.56k
            state=L_VALUE;
152
3.56k
            goto find_value;
153
0
          case F_PARAM:
154
0
            break;
155
0
          case F_LF:
156
0
          case F_CR:
157
0
          case F_CRLF:
158
0
            state=saved_state;
159
0
            break;
160
2.27k
          case GEN_PARAM:
161
2.45k
          default:
162
2.45k
            param->type=GEN_PARAM;
163
2.45k
            param->name.len=tmp-param->name.s;
164
2.45k
            state=L_VALUE;
165
2.45k
            goto find_value;
166
7.26k
        }
167
0
        break;
168
      /* \n and \r*/
169
17.5k
      case '\n':
170
17.5k
        switch(state){
171
408
          case FIN_HIDDEN:
172
797
          case FIN_ALIAS:
173
797
            param->type=state;
174
797
            param->name.len=tmp-param->name.s;
175
797
            param->size=tmp-param->start;
176
797
            saved_state=L_PARAM;
177
797
            state=F_LF;
178
797
            goto endofparam;
179
395
          case FIN_BRANCH:
180
785
          case FIN_TTL:
181
1.18k
          case FIN_MADDR:
182
1.69k
          case FIN_RECEIVED:
183
2.37k
          case FIN_I:
184
3.33k
          case FIN_RPORT:
185
3.33k
            param->type=state;
186
3.33k
            param->name.len=tmp-param->name.s;
187
3.33k
            param->size=tmp-param->start;
188
3.33k
            saved_state=L_VALUE;
189
3.33k
            state=F_LF;
190
3.33k
            goto find_value;
191
0
          case F_PARAM:
192
0
            saved_state=state;
193
0
            state=F_LF;
194
0
            break;
195
0
          case F_LF:
196
0
          case F_CRLF:
197
0
            state=END_OF_HEADER;
198
0
            goto end_via;
199
0
          case F_CR:
200
0
            state=F_CRLF;
201
0
            break;
202
10.7k
          case GEN_PARAM:
203
13.3k
          default:
204
13.3k
            param->type=GEN_PARAM;
205
13.3k
            saved_state=L_VALUE;
206
13.3k
            param->name.len=tmp-param->name.s;
207
13.3k
            param->size=tmp-param->start;
208
13.3k
            state=F_LF;
209
13.3k
            goto find_value;
210
17.5k
        }
211
0
        break;
212
10.3k
      case '\r':
213
10.3k
        switch(state){
214
485
          case FIN_HIDDEN:
215
873
          case FIN_ALIAS:
216
873
            param->type=state;
217
873
            param->name.len=tmp-param->name.s;
218
873
            param->size=tmp-param->start;
219
873
            saved_state=L_PARAM;
220
873
            state=F_CR;
221
873
            goto endofparam;
222
391
          case FIN_BRANCH:
223
781
          case FIN_TTL:
224
1.17k
          case FIN_MADDR:
225
1.57k
          case FIN_RECEIVED:
226
1.99k
          case FIN_I:
227
3.20k
          case FIN_RPORT:
228
3.20k
            param->type=state;
229
3.20k
            param->name.len=tmp-param->name.s;
230
3.20k
            param->size=tmp-param->start;
231
3.20k
            saved_state=L_VALUE;
232
3.20k
            state=F_CR;
233
3.20k
            goto find_value;
234
0
          case F_PARAM:
235
0
            saved_state=state;
236
0
            state=F_CR;
237
0
            break;
238
0
          case F_CR:
239
0
          case F_CRLF:
240
0
            state=END_OF_HEADER;
241
0
            goto end_via;
242
5.51k
          case GEN_PARAM:
243
6.23k
          default:
244
6.23k
            param->type=GEN_PARAM;
245
6.23k
            param->name.len=tmp-param->name.s;
246
6.23k
            param->size=tmp-param->start;
247
6.23k
            saved_state=L_VALUE;
248
6.23k
            state=F_CR;
249
6.23k
            goto find_value;
250
10.3k
        }
251
0
        break;
252
253
22.1k
      case '=':
254
22.1k
        switch(state){
255
742
          case FIN_BRANCH:
256
1.52k
          case FIN_TTL:
257
2.56k
          case FIN_MADDR:
258
7.05k
          case FIN_RECEIVED:
259
7.91k
          case FIN_RPORT:
260
8.79k
          case FIN_I:
261
8.79k
            param->type=state;
262
8.79k
            param->name.len=tmp-param->name.s;
263
8.79k
            state=F_VALUE;
264
8.79k
            goto find_value;
265
6
          case F_PARAM:
266
8
          case FIN_HIDDEN:
267
11
          case FIN_ALIAS:
268
11
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
269
11
            goto parse_error;
270
0
          case F_CR:
271
0
          case F_LF:
272
0
          case F_CRLF:
273
0
            state=END_OF_HEADER;
274
0
            goto end_via;
275
12.3k
          case GEN_PARAM:
276
13.3k
          default:
277
13.3k
            param->type=GEN_PARAM;
278
13.3k
            param->name.len=tmp-param->name.s;
279
13.3k
            state=F_VALUE;
280
13.3k
            goto find_value;
281
22.1k
        }
282
0
        break;
283
79.2k
      case ';':
284
79.2k
        switch(state){
285
12.6k
          case FIN_HIDDEN:
286
13.3k
          case FIN_RPORT: /* rport can appear w/o a value */
287
14.0k
          case FIN_ALIAS:
288
14.0k
            param->type=state;
289
14.0k
            param->name.len=tmp-param->name.s;
290
14.0k
            state=F_PARAM;
291
14.0k
            goto endofparam;
292
2
          case FIN_BRANCH:
293
4
          case FIN_MADDR:
294
6
          case FIN_TTL:
295
8
          case FIN_RECEIVED:
296
10
          case FIN_I:
297
10
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
298
10
            goto parse_error;
299
0
          case F_CR:
300
0
          case F_LF:
301
0
          case F_CRLF:
302
0
            state=END_OF_HEADER;
303
0
            goto end_via;
304
54.1k
          case GEN_PARAM:
305
65.2k
          default:
306
65.2k
            param->type=GEN_PARAM;
307
65.2k
            param->name.len=tmp-param->name.s;
308
65.2k
            state=F_PARAM;
309
65.2k
            goto endofparam;
310
79.2k
        }
311
0
        break;
312
2.72k
      case ',':
313
2.72k
        switch(state){
314
563
          case FIN_HIDDEN:
315
951
          case FIN_RPORT:
316
1.44k
          case FIN_ALIAS:
317
1.44k
            param->type=state;
318
1.44k
            param->name.len=tmp-param->name.s;
319
1.44k
            state=F_VIA;
320
1.44k
            goto endofvalue;
321
2
          case FIN_BRANCH:
322
4
          case FIN_MADDR:
323
6
          case FIN_TTL:
324
8
          case FIN_RECEIVED:
325
10
          case FIN_I:
326
10
            LM_ERR("new via found (',') when '=' expected"
327
10
                "(state %d=)\n", state);
328
10
            goto parse_error; /* or we could ignore this bad param*/
329
0
          case F_CR:
330
0
          case F_LF:
331
0
          case F_CRLF:
332
0
            state=END_OF_HEADER;
333
0
            goto end_via;
334
1.22k
          case GEN_PARAM:
335
1.26k
          default:
336
1.26k
            param->type=GEN_PARAM;
337
1.26k
            param->name.len=tmp-param->name.s;
338
1.26k
            state=F_VIA;
339
1.26k
            goto endofvalue;
340
2.72k
        }
341
0
        break;
342
343
        /* param names */
344
31.5k
      case 'h':
345
35.1k
      case 'H':
346
35.1k
        switch(state){
347
26.9k
          case F_PARAM:
348
26.9k
            state=HIDDEN1;
349
26.9k
            param->name.s=tmp;
350
26.9k
            break;
351
1.92k
          case BRANCH5:
352
1.92k
            state=FIN_BRANCH;
353
1.92k
            break;
354
5.22k
          case GEN_PARAM:
355
5.22k
            break;
356
0
          case F_CR:
357
0
          case F_LF:
358
0
          case F_CRLF:
359
0
            state=END_OF_HEADER;
360
0
            goto end_via;
361
1.10k
          default:
362
1.10k
            state=GEN_PARAM;
363
35.1k
        }
364
35.1k
        break;
365
56.2k
      case 'i':
366
67.0k
      case 'I':
367
67.0k
        switch(state){
368
3.94k
          case F_PARAM:
369
3.94k
            state=FIN_I;
370
3.94k
            param->name.s=tmp;
371
3.94k
            break;
372
24.7k
          case HIDDEN1:
373
24.7k
            state=HIDDEN2;
374
24.7k
            break;
375
9.29k
          case RECEIVED4:
376
9.29k
            state=RECEIVED5;
377
9.29k
            break;
378
3.78k
          case ALIAS2:
379
3.78k
            state=ALIAS3;
380
3.78k
            break;
381
23.3k
          case GEN_PARAM:
382
23.3k
            break;
383
0
          case F_CR:
384
0
          case F_LF:
385
0
          case F_CRLF:
386
0
            state=END_OF_HEADER;
387
0
            goto end_via;
388
2.03k
          default:
389
2.03k
            state=GEN_PARAM;
390
67.0k
        }
391
67.0k
        break;
392
85.2k
      case 'd':
393
91.3k
      case 'D':
394
91.3k
        switch(state){
395
3.15k
          case F_PARAM:
396
3.15k
            state=GEN_PARAM;
397
3.15k
            param->name.s=tmp;
398
3.15k
            break;
399
23.5k
          case HIDDEN2:
400
23.5k
            state=HIDDEN3;
401
23.5k
            break;
402
21.3k
          case HIDDEN3:
403
21.3k
            state=HIDDEN4;
404
21.3k
            break;
405
4.45k
          case MADDR2:
406
4.45k
            state=MADDR3;
407
4.45k
            break;
408
3.68k
          case MADDR3:
409
3.68k
            state=MADDR4;
410
3.68k
            break;
411
6.58k
          case RECEIVED7:
412
6.58k
            state=FIN_RECEIVED;
413
6.58k
            break;
414
27.3k
          case GEN_PARAM:
415
27.3k
            break;
416
0
          case F_CR:
417
0
          case F_LF:
418
0
          case F_CRLF:
419
0
            state=END_OF_HEADER;
420
0
            goto end_via;
421
1.11k
          default:
422
1.11k
            state=GEN_PARAM;
423
91.3k
        }
424
91.3k
        break;
425
91.3k
      case 'e':
426
83.3k
      case 'E':
427
83.3k
        switch(state){
428
4.11k
          case F_PARAM:
429
4.11k
            state=GEN_PARAM;
430
4.11k
            param->name.s=tmp;
431
4.11k
            break;
432
16.8k
          case HIDDEN4:
433
16.8k
            state=HIDDEN5;
434
16.8k
            break;
435
12.0k
          case RECEIVED1:
436
12.0k
            state=RECEIVED2;
437
12.0k
            break;
438
9.58k
          case RECEIVED3:
439
9.58k
            state=RECEIVED4;
440
9.58k
            break;
441
6.76k
          case RECEIVED6:
442
6.76k
            state=RECEIVED7;
443
6.76k
            break;
444
31.3k
          case GEN_PARAM:
445
31.3k
            break;
446
0
          case F_CR:
447
0
          case F_LF:
448
0
          case F_CRLF:
449
0
            state=END_OF_HEADER;
450
0
            goto end_via;
451
2.60k
          default:
452
2.60k
            state=GEN_PARAM;
453
83.3k
        }
454
83.3k
        break;
455
83.3k
      case 'n':
456
40.9k
      case 'N':
457
40.9k
        switch(state){
458
1.28k
          case F_PARAM:
459
1.28k
            state=GEN_PARAM;
460
1.28k
            param->name.s=tmp;
461
1.28k
            break;
462
13.1k
          case HIDDEN5:
463
13.1k
            state=FIN_HIDDEN;
464
13.1k
            break;
465
3.10k
          case BRANCH3:
466
3.10k
            state=BRANCH4;
467
3.10k
            break;
468
22.5k
          case GEN_PARAM:
469
22.5k
            break;
470
0
          case F_CR:
471
0
          case F_LF:
472
0
          case F_CRLF:
473
0
            state=END_OF_HEADER;
474
0
            goto end_via;
475
913
          default:
476
913
            state=GEN_PARAM;
477
40.9k
        }
478
40.9k
        break;
479
40.9k
      case 't':
480
33.8k
      case 'T':
481
33.8k
        switch(state){
482
8.83k
          case F_PARAM:
483
8.83k
            state=TTL1;
484
8.83k
            param->name.s=tmp;
485
8.83k
            break;
486
1.97k
          case TTL1:
487
1.97k
            state=TTL2;
488
1.97k
            break;
489
5.19k
          case RPORT3:
490
5.19k
            state=FIN_RPORT;
491
5.19k
            break;
492
15.3k
          case GEN_PARAM:
493
15.3k
            break;
494
0
          case F_CR:
495
0
          case F_LF:
496
0
          case F_CRLF:
497
0
            state=END_OF_HEADER;
498
0
            goto end_via;
499
2.52k
          default:
500
2.52k
            state=GEN_PARAM;
501
33.8k
        }
502
33.8k
        break;
503
33.8k
      case 'l':
504
35.0k
      case 'L':
505
35.0k
        switch(state){
506
3.08k
          case F_PARAM:
507
3.08k
            state=GEN_PARAM;
508
3.08k
            param->name.s=tmp;
509
3.08k
            break;
510
1.54k
          case TTL2:
511
1.54k
            state=FIN_TTL;
512
1.54k
            break;
513
5.40k
          case ALIAS1:
514
5.40k
            state=ALIAS2;
515
5.40k
            break;
516
23.9k
          case GEN_PARAM:
517
23.9k
            break;
518
0
          case F_CR:
519
0
          case F_LF:
520
0
          case F_CRLF:
521
0
            state=END_OF_HEADER;
522
0
            goto end_via;
523
1.10k
          default:
524
1.10k
            state=GEN_PARAM;
525
35.0k
        }
526
35.0k
        break;
527
35.0k
      case 'm':
528
14.9k
      case 'M':
529
14.9k
        switch(state){
530
7.72k
          case F_PARAM:
531
7.72k
            state=MADDR1;
532
7.72k
            param->name.s=tmp;
533
7.72k
            break;
534
6.63k
          case GEN_PARAM:
535
6.63k
            break;
536
0
          case F_CR:
537
0
          case F_LF:
538
0
          case F_CRLF:
539
0
            state=END_OF_HEADER;
540
0
            goto end_via;
541
628
          default:
542
628
            state=GEN_PARAM;
543
14.9k
        }
544
14.9k
        break;
545
296k
      case 'a':
546
312k
      case 'A':
547
312k
        switch(state){
548
12.1k
          case F_PARAM:
549
12.1k
            state=ALIAS1;
550
12.1k
            param->name.s=tmp;
551
12.1k
            break;
552
5.29k
          case MADDR1:
553
5.29k
            state=MADDR2;
554
5.29k
            break;
555
3.54k
          case BRANCH2:
556
3.54k
            state=BRANCH3;
557
3.54k
            break;
558
2.38k
          case ALIAS3:
559
2.38k
            state=ALIAS4;
560
2.38k
            break;
561
285k
          case GEN_PARAM:
562
285k
            break;
563
0
          case F_CR:
564
0
          case F_LF:
565
0
          case F_CRLF:
566
0
            state=END_OF_HEADER;
567
0
            goto end_via;
568
3.46k
          default:
569
3.46k
            state=GEN_PARAM;
570
312k
        }
571
312k
        break;
572
312k
      case 'r':
573
66.1k
      case 'R':
574
66.1k
        switch(state){
575
3.10k
          case MADDR4:
576
3.10k
            state=FIN_MADDR;
577
3.10k
            break;
578
30.4k
          case F_PARAM:
579
30.4k
            state=RECEIVED1;
580
30.4k
            param->name.s=tmp;
581
30.4k
            break;
582
4.03k
          case BRANCH1:
583
4.03k
            state=BRANCH2;
584
4.03k
            break;
585
5.57k
          case RPORT2:
586
5.57k
            state=RPORT3;
587
5.57k
            break;
588
20.8k
          case GEN_PARAM:
589
20.8k
            break;
590
0
          case F_CR:
591
0
          case F_LF:
592
0
          case F_CRLF:
593
0
            state=END_OF_HEADER;
594
0
            goto end_via;
595
2.14k
          default:
596
2.14k
            state=GEN_PARAM;
597
66.1k
        }
598
66.1k
        break;
599
66.1k
      case 'c':
600
35.2k
      case 'C':
601
35.2k
        switch(state){
602
1.32k
          case F_PARAM:
603
1.32k
            state=GEN_PARAM;
604
1.32k
            param->name.s=tmp;
605
1.32k
            break;
606
10.4k
          case RECEIVED2:
607
10.4k
            state=RECEIVED3;
608
10.4k
            break;
609
2.57k
          case BRANCH4:
610
2.57k
            state=BRANCH5;
611
2.57k
            break;
612
19.1k
          case GEN_PARAM:
613
19.1k
            break;
614
0
          case F_CR:
615
0
          case F_LF:
616
0
          case F_CRLF:
617
0
            state=END_OF_HEADER;
618
0
            goto end_via;
619
1.80k
          default:
620
1.80k
            state=GEN_PARAM;
621
35.2k
        }
622
35.2k
        break;
623
35.2k
      case 'v':
624
31.8k
      case 'V':
625
31.8k
        switch(state){
626
4.96k
          case F_PARAM:
627
4.96k
            state=GEN_PARAM;
628
4.96k
            param->name.s=tmp;
629
4.96k
            break;
630
8.52k
          case RECEIVED5:
631
8.52k
            state=RECEIVED6;
632
8.52k
            break;
633
16.8k
          case GEN_PARAM:
634
16.8k
            break;
635
0
          case F_CR:
636
0
          case F_LF:
637
0
          case F_CRLF:
638
0
            state=END_OF_HEADER;
639
0
            goto end_via;
640
1.50k
          default:
641
1.50k
            state=GEN_PARAM;
642
31.8k
        }
643
31.8k
        break;
644
31.8k
      case 'b':
645
17.1k
      case 'B':
646
17.1k
        switch(state){
647
6.04k
          case F_PARAM:
648
6.04k
            state=BRANCH1;
649
6.04k
            param->name.s=tmp;
650
6.04k
            break;
651
8.65k
          case GEN_PARAM:
652
8.65k
            break;
653
0
          case F_CR:
654
0
          case F_LF:
655
0
          case F_CRLF:
656
0
            state=END_OF_HEADER;
657
0
            goto end_via;
658
2.44k
          default:
659
2.44k
            state=GEN_PARAM;
660
17.1k
        }
661
17.1k
        break;
662
23.5k
      case 'p':
663
36.1k
      case 'P':
664
36.1k
        switch(state){
665
6.67k
          case F_PARAM:
666
6.67k
            state=GEN_PARAM;
667
6.67k
            param->name.s=tmp;
668
6.67k
            break;
669
9.17k
          case RECEIVED1:
670
9.17k
            state=RPORT1;
671
9.17k
            break;
672
0
          case F_CR:
673
0
          case F_LF:
674
0
          case F_CRLF:
675
0
            state=END_OF_HEADER;
676
0
            goto end_via;
677
20.2k
          default:
678
20.2k
            state=GEN_PARAM;
679
36.1k
        }
680
36.1k
        break;
681
43.4k
      case 'o':
682
52.9k
      case 'O':
683
52.9k
        switch(state){
684
1.02k
          case F_PARAM:
685
1.02k
            state=GEN_PARAM;
686
1.02k
            param->name.s=tmp;
687
1.02k
            break;
688
7.79k
          case RPORT1:
689
7.79k
            state=RPORT2;
690
7.79k
            break;
691
0
          case F_CR:
692
0
          case F_LF:
693
0
          case F_CRLF:
694
0
            state=END_OF_HEADER;
695
0
            goto end_via;
696
44.0k
          default:
697
44.0k
            state=GEN_PARAM;
698
52.9k
        }
699
52.9k
        break;
700
52.9k
      case 's':
701
19.7k
      case 'S':
702
19.7k
        switch(state){
703
1.79k
          case F_PARAM:
704
1.79k
            state=GEN_PARAM;
705
1.79k
            param->name.s=tmp;
706
1.79k
            break;
707
3.08k
          case ALIAS4:
708
3.08k
            state=FIN_ALIAS;
709
3.08k
            break;
710
0
          case F_CR:
711
0
          case F_LF:
712
0
          case F_CRLF:
713
0
            state=END_OF_HEADER;
714
0
            goto end_via;
715
14.8k
          default:
716
14.8k
            state=GEN_PARAM;
717
19.7k
        }
718
19.7k
        break;
719
632k
      default:
720
632k
        switch(state){
721
17.1k
          case F_PARAM:
722
17.1k
            state=GEN_PARAM;
723
17.1k
            param->name.s=tmp;
724
17.1k
            break;
725
602k
          case GEN_PARAM:
726
602k
            break;
727
0
          case F_CR:
728
0
          case F_LF:
729
0
          case F_CRLF:
730
0
            state=END_OF_HEADER;
731
0
            goto end_via;
732
13.0k
          default:
733
13.0k
            state=GEN_PARAM;
734
632k
        }
735
1.74M
    }
736
1.74M
  }/* for tmp*/
737
738
  /* end of packet? => error, no cr/lf,',' found!!!*/
739
1.40k
  saved_state=state;
740
1.40k
  state=END_OF_HEADER;
741
1.40k
  goto parse_error;
742
743
54.2k
 find_value:
744
54.2k
  tmp++;
745
612k
  for(;tmp<end;tmp++){
746
611k
    switch(*tmp){
747
10.5k
      case ' ':
748
27.0k
      case '\t':
749
27.0k
        switch(state){
750
2.41k
          case L_VALUE:
751
12.3k
          case F_VALUE: /*eat space*/
752
12.3k
            break;
753
1.46k
          case P_VALUE:
754
1.46k
            state=L_PARAM;
755
1.46k
            param->value.len=tmp-param->value.s;
756
1.46k
            goto endofvalue;
757
1.84k
          case P_STRING:
758
1.84k
            break;
759
4.26k
          case F_CR:
760
10.1k
          case F_LF:
761
11.3k
          case F_CRLF:
762
11.3k
            state=saved_state;
763
11.3k
            break;
764
0
          default:
765
0
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
766
0
            goto parse_error;
767
27.0k
        }
768
25.5k
        break;
769
25.5k
      case '\n':
770
7.83k
        switch(state){
771
1.69k
          case L_VALUE:
772
2.13k
          case F_VALUE: /*eat space*/
773
2.70k
          case P_STRING:
774
2.70k
            saved_state=state;
775
2.70k
            param->size=tmp-param->start;
776
2.70k
            state=F_LF;
777
2.70k
            break;
778
1.22k
          case P_VALUE:
779
1.22k
            saved_state=L_PARAM;
780
1.22k
            state=F_LF;
781
1.22k
            param->value.len=tmp-param->value.s;
782
1.22k
            goto endofvalue;
783
16
          case F_LF:
784
18
          case F_CRLF:
785
18
            state=END_OF_HEADER;
786
18
            goto end_via;
787
3.88k
          case F_CR:
788
3.88k
            state=F_CRLF;
789
3.88k
            break;
790
0
          default:
791
0
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
792
0
            goto parse_error;
793
7.83k
        }
794
6.59k
        break;
795
6.59k
      case '\r':
796
3.69k
        switch(state){
797
1.14k
          case L_VALUE:
798
2.51k
          case F_VALUE: /*eat space*/
799
2.91k
          case P_STRING:
800
2.91k
            saved_state=state;
801
2.91k
            param->size=tmp-param->start;
802
2.91k
            state=F_CR;
803
2.91k
            break;
804
753
          case P_VALUE:
805
753
            param->value.len=tmp-param->value.s;
806
753
            saved_state=L_PARAM;
807
753
            state=F_CR;
808
753
            goto endofvalue;
809
4
          case F_LF:
810
19
          case F_CR:
811
21
          case F_CRLF:
812
21
            state=END_OF_HEADER;
813
21
            goto end_via;
814
0
          default:
815
0
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
816
0
            goto parse_error;
817
3.69k
        }
818
2.91k
        break;
819
820
13.3k
      case '=':
821
13.3k
        switch(state){
822
10.7k
          case L_VALUE:
823
10.7k
            state=F_VALUE;
824
10.7k
            break;
825
781
          case P_STRING:
826
781
            break;
827
633
          case F_LF:
828
1.39k
          case F_CR:
829
1.81k
          case F_CRLF:
830
1.81k
            state=END_OF_HEADER;
831
1.81k
            goto end_via;
832
7
          default:
833
7
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
834
7
            goto parse_error;
835
13.3k
        }
836
11.5k
        break;
837
30.4k
      case ';':
838
30.4k
        switch(state){
839
12.9k
          case P_VALUE:
840
12.9k
            param->value.len=tmp-param->value.s;
841
12.9k
            state=F_PARAM;
842
12.9k
            goto endofvalue;
843
13.9k
          case F_VALUE:
844
13.9k
            param->value.len=0;
845
13.9k
            state=F_PARAM;
846
13.9k
            goto endofvalue;
847
692
          case P_STRING:
848
692
            break; /* what to do? */
849
519
          case F_LF:
850
996
          case F_CR:
851
1.45k
          case F_CRLF:
852
1.45k
            state=END_OF_HEADER;
853
1.45k
            goto end_via;
854
1.36k
          case L_VALUE:
855
1.36k
            if (param->type==FIN_RPORT){
856
1.35k
              param->value.len=0;
857
1.35k
              param->value.s=0; /* null value */
858
1.35k
              state=F_PARAM;
859
1.35k
              goto endofvalue;
860
1.35k
            };
861
            /* no break */
862
13
          default:
863
13
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
864
13
            goto parse_error;
865
30.4k
        }
866
692
        break;
867
2.88k
      case ',':
868
2.88k
        switch(state){
869
472
          case P_VALUE:
870
472
            param->value.len=tmp-param->value.s;
871
472
            state=F_VIA;
872
472
            goto endofvalue;
873
390
          case P_STRING:
874
1.10k
          case F_LF:
875
1.53k
          case F_CR:
876
2.02k
          case F_CRLF:
877
2.02k
            state=END_OF_HEADER;
878
2.02k
            goto end_via;
879
391
          case L_VALUE:
880
391
            if (param->type==FIN_RPORT){
881
389
              param->value.len=0;
882
389
              param->value.s=0; /* null value */
883
389
              state=F_VIA;
884
389
              goto endofvalue;
885
389
            };
886
            /* no break */
887
5
          default:
888
5
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
889
5
            goto parse_error;
890
2.88k
        }
891
0
        break; /* what to do? */
892
2.77k
      case '"':
893
2.77k
        switch(state){
894
1.08k
          case F_VALUE:
895
1.08k
            state=P_STRING;
896
1.08k
            param->value.s=tmp+1;
897
1.08k
            break;
898
399
          case P_STRING:
899
399
            state=L_PARAM;
900
399
            param->value.len=tmp-param->value.s;
901
399
            goto endofvalue;
902
446
          case F_LF:
903
896
          case F_CR:
904
1.29k
          case F_CRLF:
905
1.29k
            state=END_OF_HEADER;
906
1.29k
            goto end_via;
907
3
          default:
908
3
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
909
3
            goto parse_error;
910
2.77k
        }
911
1.08k
        break;
912
30.4k
      case '\0':
913
30.4k
        break;
914
915
493k
      default:
916
493k
        switch(state){
917
16.9k
          case F_VALUE:
918
16.9k
            state=P_VALUE;
919
16.9k
            param->value.s=tmp;
920
16.9k
            break;
921
453k
          case P_VALUE:
922
462k
          case P_STRING:
923
462k
            break;
924
11.0k
          case F_LF:
925
13.0k
          case F_CR:
926
13.9k
          case F_CRLF:
927
13.9k
            state=END_OF_HEADER;
928
13.9k
            goto end_via;
929
56
          default:
930
56
            LM_ERR("invalid char <%c> in state %d\n", *tmp, state);
931
56
            goto parse_error;
932
493k
        }
933
611k
    }
934
611k
  } /* for2 tmp*/
935
936
  /* end of buff and no CR/LF =>error*/
937
618
  saved_state=state;
938
618
  state=END_OF_HEADER;
939
618
  goto parse_error;
940
941
82.2k
 endofparam:
942
117k
 endofvalue:
943
117k
  param->size=tmp-param->start;
944
138k
normal_exit:
945
138k
  *pstate=state;
946
138k
  *psaved_state=saved_state;
947
138k
  LM_DBG("found param type %d, <%.*s> = <%.*s>; state=%d\n", param->type,
948
0
      param->name.len, ZSW(param->name.s),
949
0
      (param->value.len?param->value.len:3),
950
0
      (param->value.len?param->value.s:"n/a"), state);
951
138k
  return tmp;
952
953
20.6k
 end_via:
954
       /* if we are here we found an "unexpected" end of via
955
        *  (cr/lf). This is valid only if the param type is GEN_PARAM or
956
      *  RPORT (the only ones which can miss the value; HIDDEN is a
957
      *  special case )*/
958
20.6k
  if ((param->type==GEN_PARAM)||(param->type==PARAM_RPORT)){
959
20.5k
    saved_state=L_PARAM; /* change the saved_state, we have an unknown
960
                            param. w/o a value */
961
    /* param->size should be computed before */
962
20.5k
    goto normal_exit;
963
20.5k
  }
964
26
  *pstate=state;
965
26
  *psaved_state=saved_state;
966
26
  LM_DBG("error on  param type %d, <%.*s>, state=%d, saved_state=%d\n",
967
26
    param->type, param->name.len, ZSW(param->name.s), state, saved_state);
968
969
2.16k
parse_error:
970
2.16k
  LM_ERR("parse_via_param\n");
971
2.16k
  param->type=PARAM_ERROR;
972
2.16k
  *pstate=PARAM_ERROR;
973
2.16k
  *psaved_state=state;
974
2.16k
  return tmp;
975
26
}
976
977
978
979
/*
980
 * call it with a vb initialized to 0
981
 * returns: pointer after the parsed parts and sets vb->error
982
 * WARNING: don't forget to cleanup on error with free_via_list(vb)!
983
 */
984
char* parse_via(char* buffer, char* end, struct via_body *vbody)
985
143k
{
986
143k
  char* tmp;
987
143k
  char* param_start;
988
143k
  unsigned char state;
989
143k
  unsigned char saved_state;
990
143k
  int c_nest;
991
143k
  int err;
992
143k
  struct via_body* vb;
993
143k
  struct via_param* param;
994
995
143k
  vb=vbody; /* keep orignal vbody value, needed to set the error member
996
         in case of multiple via bodies in the same header */
997
164k
parse_again:
998
164k
  vb->error=PARSE_ERROR;
999
  /* parse start of via ( SIP/2.0/UDP    )*/
1000
164k
  state=F_SIP;
1001
164k
  saved_state=0; /*it should generate error if it's used without set*/
1002
164k
  param_start=0;
1003
3.13M
  for(tmp=buffer;tmp<end;tmp++){
1004
3.12M
    switch(*tmp){
1005
163k
      case ' ':
1006
181k
      case'\t':
1007
181k
        switch(state){
1008
1.25k
          case L_VER: /* eat space */
1009
2.02k
          case L_PROTO:
1010
2.91k
          case F_SIP:
1011
7.41k
          case F_VER:
1012
13.6k
          case F_PROTO:
1013
13.6k
            break;
1014
491
          case FIN_UDP:
1015
491
            vb->transport.len=tmp-vb->transport.s;
1016
491
            vb->proto=PROTO_UDP;
1017
491
            state=F_HOST; /* start looking for host*/
1018
491
            goto main_via;
1019
562
          case FIN_TCP:
1020
            /* finished proto parsing */
1021
562
            vb->transport.len=tmp-vb->transport.s;
1022
562
            vb->proto=PROTO_TCP;
1023
562
            state=F_HOST; /* start looking for host*/
1024
562
            goto main_via;
1025
408
          case FIN_TLS:
1026
            /* finished proto parsing */
1027
408
            vb->transport.len=tmp-vb->transport.s;
1028
408
            vb->proto=PROTO_TLS;
1029
408
            state=F_HOST; /* start looking for host*/
1030
408
            goto main_via;
1031
443
          case FIN_SCTP:
1032
            /* finished proto parsing */
1033
443
            vb->transport.len=tmp-vb->transport.s;
1034
443
            vb->proto=PROTO_SCTP;
1035
443
            state=F_HOST; /* start looking for host*/
1036
443
            goto main_via;
1037
0
          case FIN_WS:
1038
938
          case WS_WSS:
1039
            /* finished proto parsing */
1040
938
            vb->transport.len=tmp-vb->transport.s;
1041
938
            vb->proto=PROTO_WS;
1042
938
            state=F_HOST; /* start looking for host*/
1043
938
            goto main_via;
1044
472
          case FIN_WSS:
1045
            /* finished proto parsing */
1046
472
            vb->transport.len=tmp-vb->transport.s;
1047
472
            vb->proto=PROTO_WSS;
1048
472
            state=F_HOST; /* start looking for host*/
1049
472
            goto main_via;
1050
146k
          case OTHER_PROTO:
1051
            /* finished proto parsing */
1052
146k
            vb->transport.len=tmp-vb->transport.s;
1053
146k
            vb->proto=PROTO_OTHER;
1054
146k
            state=F_HOST; /* start looking for host*/
1055
146k
            goto main_via;
1056
4.95k
          case FIN_SIP:
1057
4.95k
            vb->name.len=tmp-vb->name.s;
1058
4.95k
            state=L_VER;
1059
4.95k
            break;
1060
5.06k
          case FIN_VER:
1061
5.06k
            vb->version.len=tmp-vb->version.s;
1062
5.06k
            state=L_PROTO;
1063
5.06k
            break;
1064
3.49k
          case F_LF:
1065
4.34k
          case F_CRLF:
1066
7.75k
          case F_CR: /* header continues on this line */
1067
7.75k
            state=saved_state;
1068
7.75k
            break;
1069
13
          default:
1070
13
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1071
13
            goto parse_error;
1072
181k
        }
1073
31.4k
        break;
1074
31.4k
      case '\n':
1075
11.3k
        switch(state){
1076
522
          case L_VER:
1077
1.03k
          case F_SIP:
1078
1.47k
          case F_VER:
1079
1.91k
          case F_PROTO:
1080
2.30k
          case L_PROTO:
1081
2.30k
            saved_state=state;
1082
2.30k
            state=F_LF;
1083
2.30k
            break;
1084
392
          case FIN_UDP:
1085
392
            vb->transport.len=tmp-vb->transport.s;
1086
392
            vb->proto=PROTO_UDP;
1087
392
            state=F_LF;
1088
392
            saved_state=F_HOST; /* start looking for host*/
1089
392
            goto main_via;
1090
937
          case FIN_TCP:
1091
937
            vb->transport.len=tmp-vb->transport.s;
1092
937
            vb->proto=PROTO_TCP;
1093
937
            state=F_LF;
1094
937
            saved_state=F_HOST; /* start looking for host*/
1095
937
            goto main_via;
1096
420
          case FIN_TLS:
1097
420
            vb->transport.len=tmp-vb->transport.s;
1098
420
            vb->proto=PROTO_TLS;
1099
420
            state=F_LF;
1100
420
            saved_state=F_HOST; /* start looking for host*/
1101
420
            goto main_via;
1102
412
          case WS_WSS:
1103
412
          case FIN_WS:
1104
412
            vb->transport.len=tmp-vb->transport.s;
1105
412
            vb->proto=PROTO_WS;
1106
412
            state=F_LF;
1107
412
            saved_state=F_HOST; /* start looking for host*/
1108
412
            goto main_via;
1109
686
          case FIN_WSS:
1110
686
            vb->transport.len=tmp-vb->transport.s;
1111
686
            vb->proto=PROTO_WS;
1112
686
            state=F_LF;
1113
686
            saved_state=F_HOST; /* start looking for host*/
1114
686
            goto main_via;
1115
3.99k
          case OTHER_PROTO:
1116
            /* finished proto parsing */
1117
3.99k
            vb->transport.len=tmp-vb->transport.s;
1118
3.99k
            vb->proto=PROTO_OTHER;
1119
3.99k
            state=F_LF;
1120
3.99k
            saved_state=F_HOST; /* start looking for host*/
1121
3.99k
            goto main_via;
1122
450
          case FIN_SIP:
1123
450
            vb->name.len=tmp-vb->name.s;
1124
450
            state=F_LF;
1125
450
            saved_state=L_VER;
1126
450
            break;
1127
839
          case FIN_VER:
1128
839
            vb->version.len=tmp-vb->version.s;
1129
839
            state=F_LF;
1130
839
            saved_state=L_PROTO;
1131
839
            break;
1132
869
          case F_CR:
1133
869
            state=F_CRLF;
1134
869
            break;
1135
5
          case F_LF:
1136
7
          case F_CRLF:
1137
7
            state=saved_state;
1138
7
            goto endofheader;
1139
4
          default:
1140
4
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1141
4
            goto parse_error;
1142
11.3k
        }
1143
4.46k
        break;
1144
8.30k
      case '\r':
1145
8.30k
        switch(state){
1146
552
          case L_VER:
1147
1.74k
          case F_SIP:
1148
2.39k
          case F_VER:
1149
2.95k
          case F_PROTO:
1150
3.34k
          case L_PROTO:
1151
3.34k
            saved_state=state;
1152
3.34k
            state=F_CR;
1153
3.34k
            break;
1154
397
          case FIN_UDP:
1155
397
            vb->transport.len=tmp-vb->transport.s;
1156
397
            vb->proto=PROTO_UDP;
1157
397
            state=F_CR;
1158
397
            saved_state=F_HOST;
1159
397
            goto main_via;
1160
268
          case FIN_TCP:
1161
268
            vb->transport.len=tmp-vb->transport.s;
1162
268
            vb->proto=PROTO_TCP;
1163
268
            state=F_CR;
1164
268
            saved_state=F_HOST;
1165
268
            goto main_via;
1166
414
          case FIN_TLS:
1167
414
            vb->transport.len=tmp-vb->transport.s;
1168
414
            vb->proto=PROTO_TLS;
1169
414
            state=F_CR;
1170
414
            saved_state=F_HOST;
1171
414
            goto main_via;
1172
539
          case WS_WSS:
1173
539
          case FIN_WS:
1174
539
            vb->transport.len=tmp-vb->transport.s;
1175
539
            vb->proto=PROTO_WS;
1176
539
            state=F_CR;
1177
539
            saved_state=F_HOST;
1178
539
            goto main_via;
1179
393
          case FIN_WSS:
1180
393
            vb->transport.len=tmp-vb->transport.s;
1181
393
            vb->proto=PROTO_WSS;
1182
393
            state=F_CR;
1183
393
            saved_state=F_HOST;
1184
393
            goto main_via;
1185
1.89k
          case OTHER_PROTO:
1186
1.89k
            vb->transport.len=tmp-vb->transport.s;
1187
1.89k
            vb->proto=PROTO_OTHER;
1188
1.89k
            state=F_CR;
1189
1.89k
            saved_state=F_HOST;
1190
1.89k
            goto main_via;
1191
397
          case FIN_SIP:
1192
397
            vb->name.len=tmp-vb->name.s;
1193
397
            state=F_CR;
1194
397
            saved_state=L_VER;
1195
397
            break;
1196
628
          case FIN_VER:
1197
628
            vb->version.len=tmp-vb->version.s;
1198
628
            state=F_CR;
1199
628
            saved_state=L_PROTO;
1200
628
            break;
1201
2
          case F_LF: /*end of line ?next header?*/
1202
15
          case F_CR:
1203
19
          case F_CRLF:
1204
19
            state=saved_state;
1205
19
            goto endofheader;
1206
5
          default:
1207
5
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1208
5
            goto parse_error;
1209
8.30k
        }
1210
4.36k
        break;
1211
1212
327k
      case '/':
1213
327k
        switch(state){
1214
157k
          case FIN_SIP:
1215
157k
            vb->name.len=tmp-vb->name.s;
1216
157k
            state=F_VER;
1217
157k
            break;
1218
156k
          case FIN_VER:
1219
156k
            vb->version.len=tmp-vb->version.s;
1220
156k
            state=F_PROTO;
1221
156k
            break;
1222
5.71k
          case L_VER:
1223
5.71k
            state=F_VER;
1224
5.71k
            break;
1225
6.45k
          case L_PROTO:
1226
6.45k
            state=F_PROTO;
1227
6.45k
            break;
1228
112
          default:
1229
112
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1230
112
            goto parse_error;
1231
327k
        }
1232
326k
        break;
1233
        /* match SIP*/
1234
326k
      case 'S':
1235
276k
      case 's':
1236
276k
        switch(state){
1237
163k
          case F_SIP:
1238
163k
            state=SIP1;
1239
163k
            vb->name.s=tmp;
1240
163k
            break;
1241
7.85k
          case TLS2:
1242
7.85k
            state=FIN_TLS;
1243
7.85k
            break;
1244
27.6k
          case F_PROTO:
1245
27.6k
            state=SCTP1;
1246
27.6k
            vb->transport.s=tmp;
1247
27.6k
            break;
1248
15.8k
          case WS1:
1249
15.8k
            state=WS_WSS;
1250
15.8k
            break;
1251
7.82k
          case WS_WSS:
1252
7.82k
            state=FIN_WSS;
1253
7.82k
            break;
1254
47.1k
          case OTHER_PROTO:
1255
47.1k
            break;
1256
498
          case UDP1:
1257
1.07k
          case UDP2:
1258
1.51k
          case FIN_UDP:
1259
1.97k
          case TCP_TLS1:
1260
2.43k
          case TCP2:
1261
3.29k
          case FIN_TCP:
1262
3.80k
          case FIN_TLS:
1263
4.43k
          case SCTP1:
1264
4.83k
          case SCTP2:
1265
5.23k
          case SCTP3:
1266
5.90k
          case FIN_SCTP:
1267
5.90k
          case FIN_WS:
1268
6.37k
          case FIN_WSS:
1269
6.37k
            state=OTHER_PROTO;
1270
6.37k
            break;
1271
15
          default:
1272
15
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1273
15
            goto parse_error;
1274
276k
        }
1275
276k
        break;
1276
276k
      case 'I':
1277
195k
      case 'i':
1278
195k
        switch(state){
1279
163k
          case SIP1:
1280
163k
            state=SIP2;
1281
163k
            break;
1282
24.2k
          case OTHER_PROTO:
1283
24.2k
            break;
1284
639
          case UDP1:
1285
1.03k
          case UDP2:
1286
1.64k
          case FIN_UDP:
1287
2.19k
          case TCP_TLS1:
1288
2.48k
          case TCP2:
1289
3.40k
          case FIN_TCP:
1290
3.80k
          case TLS2:
1291
4.10k
          case FIN_TLS:
1292
4.74k
          case SCTP1:
1293
5.20k
          case SCTP2:
1294
5.80k
          case SCTP3:
1295
6.23k
          case FIN_SCTP:
1296
6.68k
          case WS1:
1297
7.08k
          case WS_WSS:
1298
7.08k
          case FIN_WS:
1299
7.55k
          case FIN_WSS:
1300
7.55k
            state=OTHER_PROTO;
1301
7.55k
            break;
1302
20
          default:
1303
20
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1304
20
            goto parse_error;
1305
195k
        }
1306
195k
        break;
1307
195k
      case 'p':
1308
235k
      case 'P':
1309
235k
        switch(state){
1310
163k
          case SIP2:
1311
163k
            state=FIN_SIP;
1312
163k
            break;
1313
          /* allow p in PROTO */
1314
7.96k
          case UDP2:
1315
7.96k
            state=FIN_UDP;
1316
7.96k
            break;
1317
9.73k
          case TCP2:
1318
9.73k
            state=FIN_TCP;
1319
9.73k
            break;
1320
6.69k
          case SCTP3:
1321
6.69k
            state=FIN_SCTP;
1322
6.69k
            break;
1323
41.1k
          case OTHER_PROTO:
1324
41.1k
            break;
1325
625
          case UDP1:
1326
1.02k
          case FIN_UDP:
1327
1.69k
          case TCP_TLS1:
1328
2.08k
          case FIN_TCP:
1329
2.47k
          case TLS2:
1330
2.96k
          case FIN_TLS:
1331
4.08k
          case SCTP1:
1332
4.56k
          case SCTP2:
1333
5.08k
          case FIN_SCTP:
1334
5.64k
          case WS1:
1335
6.09k
          case WS_WSS:
1336
6.09k
          case FIN_WS:
1337
6.52k
          case FIN_WSS:
1338
6.52k
            state=OTHER_PROTO;
1339
6.52k
            break;
1340
24
          default:
1341
24
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1342
24
            goto parse_error;
1343
235k
        }
1344
235k
        break;
1345
235k
      case 'U':
1346
58.9k
      case 'u':
1347
58.9k
        switch(state){
1348
23.3k
          case F_PROTO:
1349
23.3k
            state=UDP1;
1350
23.3k
            vb->transport.s=tmp;
1351
23.3k
            break;
1352
26.2k
          case OTHER_PROTO:
1353
26.2k
            break;
1354
747
          case UDP1:
1355
1.22k
          case UDP2:
1356
1.61k
          case FIN_UDP:
1357
2.26k
          case TCP_TLS1:
1358
2.73k
          case TCP2:
1359
4.23k
          case FIN_TCP:
1360
5.09k
          case TLS2:
1361
5.56k
          case FIN_TLS:
1362
6.04k
          case SCTP1:
1363
6.66k
          case SCTP2:
1364
7.11k
          case SCTP3:
1365
7.51k
          case FIN_SCTP:
1366
8.04k
          case WS1:
1367
8.92k
          case WS_WSS:
1368
8.92k
          case FIN_WS:
1369
9.37k
          case FIN_WSS:
1370
9.37k
            state=OTHER_PROTO;
1371
9.37k
            break;
1372
9
          default:
1373
9
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1374
9
            goto parse_error;
1375
58.9k
        }
1376
58.9k
        break;
1377
58.9k
      case 'D':
1378
43.4k
      case 'd':
1379
43.4k
        switch(state){
1380
14.5k
          case UDP1:
1381
14.5k
            state=UDP2;
1382
14.5k
            break;
1383
22.4k
          case OTHER_PROTO:
1384
22.4k
            break;
1385
662
          case UDP2:
1386
1.23k
          case FIN_UDP:
1387
1.70k
          case TCP_TLS1:
1388
1.99k
          case TCP2:
1389
2.25k
          case FIN_TCP:
1390
2.67k
          case TLS2:
1391
3.08k
          case FIN_TLS:
1392
3.88k
          case SCTP1:
1393
4.28k
          case SCTP2:
1394
4.54k
          case SCTP3:
1395
4.94k
          case FIN_SCTP:
1396
5.35k
          case WS1:
1397
5.84k
          case WS_WSS:
1398
5.84k
          case FIN_WS:
1399
6.40k
          case FIN_WSS:
1400
6.40k
            state=OTHER_PROTO;
1401
6.40k
            break;
1402
8
          default:
1403
8
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1404
8
            goto parse_error;
1405
43.4k
        }
1406
43.4k
        break;
1407
56.9k
      case 'T':
1408
110k
      case 't':
1409
110k
        switch(state){
1410
38.0k
          case F_PROTO:
1411
38.0k
            state=TCP_TLS1;
1412
38.0k
            vb->transport.s=tmp;
1413
38.0k
            break;
1414
12.2k
          case SCTP2:
1415
12.2k
            state=SCTP3;
1416
12.2k
            break;
1417
52.3k
          case OTHER_PROTO:
1418
52.3k
            break;
1419
792
          case UDP1:
1420
1.52k
          case UDP2:
1421
2.12k
          case FIN_UDP:
1422
2.57k
          case TCP_TLS1:
1423
3.01k
          case TCP2:
1424
3.61k
          case FIN_TCP:
1425
4.02k
          case TLS2:
1426
4.41k
          case FIN_TLS:
1427
5.23k
          case SCTP1:
1428
5.80k
          case SCTP3:
1429
6.06k
          case FIN_SCTP:
1430
6.54k
          case WS1:
1431
6.97k
          case WS_WSS:
1432
6.97k
          case FIN_WS:
1433
7.38k
          case FIN_WSS:
1434
7.38k
            state=OTHER_PROTO;
1435
7.38k
            break;
1436
13
          default:
1437
13
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1438
13
            goto parse_error;
1439
110k
        }
1440
110k
        break;
1441
110k
      case 'C':
1442
70.0k
      case 'c':
1443
70.0k
        switch(state){
1444
15.3k
          case TCP_TLS1:
1445
15.3k
            state=TCP2;
1446
15.3k
            break;
1447
19.4k
          case SCTP1:
1448
19.4k
            state=SCTP2;
1449
19.4k
            break;
1450
27.6k
          case OTHER_PROTO:
1451
27.6k
            break;
1452
1.21k
          case UDP1:
1453
1.62k
          case UDP2:
1454
2.07k
          case FIN_UDP:
1455
2.57k
          case TCP2:
1456
2.96k
          case FIN_TCP:
1457
3.40k
          case TLS2:
1458
3.79k
          case FIN_TLS:
1459
4.65k
          case SCTP2:
1460
5.08k
          case SCTP3:
1461
5.71k
          case FIN_SCTP:
1462
6.41k
          case WS1:
1463
6.86k
          case WS_WSS:
1464
6.86k
          case FIN_WS:
1465
7.59k
          case FIN_WSS:
1466
7.59k
            state=OTHER_PROTO;
1467
7.59k
            break;
1468
10
          default:
1469
10
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1470
10
            goto parse_error;
1471
70.0k
        }
1472
70.0k
        break;
1473
70.0k
      case 'L':
1474
44.8k
      case 'l':
1475
44.8k
        switch(state){
1476
14.9k
          case TCP_TLS1:
1477
14.9k
            state=TLS2;
1478
14.9k
            break;
1479
22.4k
          case OTHER_PROTO:
1480
22.4k
            break;
1481
408
          case UDP1:
1482
833
          case UDP2:
1483
1.22k
          case FIN_UDP:
1484
1.86k
          case TCP2:
1485
2.25k
          case FIN_TCP:
1486
2.79k
          case TLS2:
1487
3.99k
          case FIN_TLS:
1488
4.69k
          case SCTP1:
1489
5.08k
          case SCTP2:
1490
5.47k
          case SCTP3:
1491
5.91k
          case FIN_SCTP:
1492
6.43k
          case WS1:
1493
6.98k
          case WS_WSS:
1494
6.98k
          case FIN_WS:
1495
7.42k
          case FIN_WSS:
1496
7.42k
            state=OTHER_PROTO;
1497
7.42k
            break;
1498
11
          default:
1499
11
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1500
11
            goto parse_error;
1501
44.8k
        }
1502
44.8k
        break;
1503
44.8k
      case 'W':
1504
63.9k
      case 'w':
1505
63.9k
        switch(state){
1506
22.8k
          case F_PROTO:
1507
22.8k
            state=WS1;
1508
22.8k
            vb->transport.s=tmp;
1509
22.8k
            break;
1510
31.7k
          case OTHER_PROTO:
1511
31.7k
            break;
1512
889
          case UDP1:
1513
1.68k
          case UDP2:
1514
2.17k
          case FIN_UDP:
1515
2.82k
          case TCP_TLS1:
1516
3.22k
          case TCP2:
1517
4.26k
          case FIN_TCP:
1518
5.15k
          case TLS2:
1519
5.55k
          case FIN_TLS:
1520
5.94k
          case SCTP1:
1521
6.43k
          case SCTP2:
1522
6.89k
          case SCTP3:
1523
7.36k
          case FIN_SCTP:
1524
8.30k
          case WS1:
1525
8.74k
          case WS_WSS:
1526
8.74k
          case FIN_WS:
1527
9.39k
          case FIN_WSS:
1528
9.39k
            state=OTHER_PROTO;
1529
9.39k
            break;
1530
7
          default:
1531
7
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1532
7
            goto parse_error;
1533
63.9k
        }
1534
63.9k
        break;
1535
      /*match 2.0*/
1536
186k
      case '2':
1537
186k
        switch(state){
1538
163k
          case F_VER:
1539
163k
            state=VER1;
1540
163k
            vb->version.s=tmp;
1541
163k
            break;
1542
15.2k
          case OTHER_PROTO:
1543
15.2k
            break;
1544
549
          case UDP1:
1545
1.11k
          case UDP2:
1546
1.53k
          case FIN_UDP:
1547
2.45k
          case TCP_TLS1:
1548
2.83k
          case TCP2:
1549
3.23k
          case FIN_TCP:
1550
3.62k
          case TLS2:
1551
4.10k
          case FIN_TLS:
1552
4.72k
          case SCTP1:
1553
5.22k
          case SCTP2:
1554
5.62k
          case SCTP3:
1555
6.02k
          case FIN_SCTP:
1556
6.50k
          case WS1:
1557
7.16k
          case WS_WSS:
1558
7.16k
          case FIN_WS:
1559
7.55k
          case FIN_WSS:
1560
7.55k
            state=OTHER_PROTO;
1561
7.55k
            break;
1562
14
          default:
1563
14
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1564
14
            goto parse_error;
1565
186k
        }
1566
186k
        break;
1567
204k
      case '.':
1568
204k
        switch(state){
1569
163k
          case VER1:
1570
163k
            state=VER2;
1571
163k
            break;
1572
33.9k
          case OTHER_PROTO:
1573
33.9k
            break;
1574
464
          case UDP1:
1575
947
          case UDP2:
1576
1.37k
          case FIN_UDP:
1577
1.80k
          case TCP_TLS1:
1578
2.06k
          case TCP2:
1579
2.32k
          case FIN_TCP:
1580
3.11k
          case TLS2:
1581
3.55k
          case FIN_TLS:
1582
3.95k
          case SCTP1:
1583
4.84k
          case SCTP2:
1584
5.26k
          case SCTP3:
1585
5.65k
          case FIN_SCTP:
1586
6.19k
          case WS1:
1587
6.66k
          case WS_WSS:
1588
6.66k
          case FIN_WS:
1589
7.12k
          case FIN_WSS:
1590
7.12k
            state=OTHER_PROTO;
1591
7.12k
            break;
1592
18
          default:
1593
18
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1594
18
            goto parse_error;
1595
204k
        }
1596
204k
         break;
1597
204k
      case '0':
1598
196k
        switch(state){
1599
163k
          case VER2:
1600
163k
            state=FIN_VER;
1601
163k
            break;
1602
24.9k
          case OTHER_PROTO:
1603
24.9k
            break;
1604
827
          case UDP1:
1605
1.25k
          case UDP2:
1606
2.03k
          case FIN_UDP:
1607
2.88k
          case TCP_TLS1:
1608
3.47k
          case TCP2:
1609
3.94k
          case FIN_TCP:
1610
4.36k
          case TLS2:
1611
4.85k
          case FIN_TLS:
1612
5.31k
          case SCTP1:
1613
5.73k
          case SCTP2:
1614
6.25k
          case SCTP3:
1615
6.67k
          case FIN_SCTP:
1616
7.13k
          case WS1:
1617
7.55k
          case WS_WSS:
1618
7.55k
          case FIN_WS:
1619
7.85k
          case FIN_WSS:
1620
7.85k
            state=OTHER_PROTO;
1621
7.85k
            break;
1622
14
          default:
1623
14
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1624
14
            goto parse_error;
1625
196k
        }
1626
196k
        break;
1627
1628
913k
      default:
1629
913k
        switch(state){
1630
51.3k
          case F_PROTO:
1631
51.3k
            state=OTHER_PROTO;
1632
51.3k
            vb->transport.s=tmp;
1633
51.3k
            break;
1634
848k
          case OTHER_PROTO:
1635
848k
            break;
1636
1.13k
          case UDP1:
1637
1.76k
          case UDP2:
1638
2.44k
          case FIN_UDP:
1639
4.07k
          case TCP_TLS1:
1640
4.93k
          case TCP2:
1641
5.41k
          case FIN_TCP:
1642
6.58k
          case TLS2:
1643
7.22k
          case FIN_TLS:
1644
8.39k
          case SCTP1:
1645
9.65k
          case SCTP2:
1646
10.3k
          case SCTP3:
1647
11.1k
          case FIN_SCTP:
1648
12.1k
          case WS1:
1649
12.5k
          case WS_WSS:
1650
12.5k
          case FIN_WS:
1651
13.0k
          case FIN_WSS:
1652
13.0k
            state=OTHER_PROTO;
1653
13.0k
            break;
1654
244
          default:
1655
244
            LM_ERR("bad char <%c> on state %d\n", *tmp, state);
1656
244
            goto parse_error;
1657
913k
        }
1658
912k
        break;
1659
3.12M
    }
1660
3.12M
  } /* for tmp*/
1661
1662
  /* we should not be here! if everything is ok > main_via*/
1663
3.10k
  LM_ERR("bad via: end of packet on state=%d\n", state);
1664
3.10k
  goto parse_error;
1665
1666
160k
 main_via:
1667
  /* inc tmp to point to the next char*/
1668
160k
  tmp++;
1669
160k
  c_nest=0;
1670
160k
  /*state should always be F_HOST here*/;
1671
3.46M
  for(;tmp<end;tmp++){
1672
3.44M
    switch(*tmp){
1673
54.7k
    case ' ':
1674
88.8k
    case '\t':
1675
88.8k
      switch(state){
1676
26.1k
          case F_HOST:/*eat the spaces*/
1677
26.1k
            break;
1678
6.17k
          case P_HOST:
1679
             /*mark end of host*/
1680
6.17k
             vb->host.len=tmp-vb->host.s;
1681
6.17k
             state=L_PORT;
1682
6.17k
             break;
1683
2.77k
          case L_PORT: /*eat the spaces*/
1684
4.42k
          case F_PORT:
1685
4.42k
            break;
1686
776
          case P_PORT:
1687
            /*end of port */
1688
776
            vb->port_str.len=tmp-vb->port_str.s;
1689
776
            state=L_PARAM;
1690
776
            break;
1691
1.34k
          case L_PARAM: /* eat the space */
1692
3.67k
          case F_PARAM:
1693
3.67k
            break;
1694
0
          case P_PARAM:
1695
          /*  *tmp=0;*/ /*!?end of param*/
1696
0
            state=L_PARAM;
1697
0
            break;
1698
1.96k
          case L_VIA:
1699
14.8k
          case F_VIA: /* eat the space */
1700
14.8k
            break;
1701
749
          case F_COMMENT:
1702
7.03k
          case P_COMMENT:
1703
7.03k
            break;
1704
2
          case F_IP6HOST: /*no spaces allowed*/
1705
4
          case P_IP6HOST:
1706
4
            LM_ERR("bad ipv6 reference\n");
1707
4
            goto parse_error;
1708
3.68k
          case F_CRLF:
1709
17.2k
          case F_LF:
1710
25.6k
          case F_CR:
1711
            /*previous=crlf and now =' '*/
1712
25.6k
            state=saved_state;
1713
25.6k
            break;
1714
0
          default:
1715
0
            LM_ERR("on <%c>, state=%d\n",*tmp, state);
1716
0
            goto parse_error;
1717
88.8k
        }
1718
88.8k
      break;
1719
105k
      case '\n':
1720
105k
        switch(state){
1721
810
          case F_HOST:/*eat the spaces*/
1722
5.21k
          case L_PORT: /*eat the spaces*/
1723
5.60k
          case F_PORT:
1724
6.08k
          case L_PARAM: /* eat the space */
1725
6.66k
          case F_PARAM:
1726
7.09k
          case F_VIA: /* eat the space */
1727
9.28k
          case L_VIA:
1728
9.92k
          case F_COMMENT:
1729
10.8k
          case P_COMMENT:
1730
11.2k
          case F_IP6HOST:
1731
11.6k
          case P_IP6HOST:
1732
11.6k
            saved_state=state;
1733
11.6k
            state=F_LF;
1734
11.6k
            break;
1735
83.0k
          case P_HOST:
1736
             /*mark end of host*/
1737
83.0k
             vb->host.len=tmp-vb->host.s;
1738
83.0k
             saved_state=L_PORT;
1739
83.0k
             state=F_LF;
1740
83.0k
             break;
1741
1.49k
          case P_PORT:
1742
            /*end of port */
1743
1.49k
            vb->port_str.len=tmp-vb->port_str.s;
1744
1.49k
            saved_state=L_PARAM;
1745
1.49k
            state=F_LF;
1746
1.49k
            break;
1747
0
          case P_PARAM:
1748
          /*  *tmp=0;*/ /*!?end of param*/
1749
0
            saved_state=L_PARAM;
1750
0
            state=F_LF;
1751
0
            break;
1752
9.70k
          case F_CR:
1753
9.70k
            state=F_CRLF;
1754
9.70k
            break;
1755
4
          case F_CRLF:
1756
44
          case F_LF:
1757
44
            state=saved_state;
1758
44
            goto endofheader;
1759
0
          default:
1760
0
            LM_CRIT("BUG on <%c>\n",*tmp);
1761
0
            goto  parse_error;
1762
105k
        }
1763
105k
      break;
1764
105k
    case '\r':
1765
20.7k
        switch(state){
1766
936
          case F_HOST:/*eat the spaces*/
1767
2.06k
          case L_PORT: /*eat the spaces*/
1768
2.44k
          case F_PORT:
1769
2.85k
          case L_PARAM: /* eat the space */
1770
3.31k
          case F_PARAM:
1771
4.26k
          case F_VIA: /* eat the space */
1772
4.94k
          case L_VIA:
1773
5.34k
          case F_COMMENT:
1774
7.21k
          case P_COMMENT:
1775
7.62k
          case F_IP6HOST:
1776
8.32k
          case P_IP6HOST:
1777
8.32k
            saved_state=state;
1778
8.32k
            state=F_CR;
1779
8.32k
            break;
1780
11.0k
          case P_HOST:
1781
             /*mark end of host*/
1782
11.0k
             vb->host.len=tmp-vb->host.s;
1783
11.0k
             saved_state=L_PORT;
1784
11.0k
             state=F_CR;
1785
11.0k
             break;
1786
758
          case P_PORT:
1787
            /*end of port */
1788
758
            vb->port_str.len=tmp-vb->port_str.s;
1789
758
            saved_state=L_PARAM;
1790
758
            state=F_CR;
1791
758
            break;
1792
0
          case P_PARAM:
1793
          /*  *tmp=0;*/ /*!?end of param*/
1794
0
            saved_state=L_PARAM;
1795
0
            state=F_CR;
1796
0
            break;
1797
56
          case F_CRLF:
1798
524
          case F_CR:
1799
546
          case F_LF:
1800
546
            state=saved_state;
1801
546
            goto endofheader;
1802
0
          default:
1803
0
            LM_ERR("on <%c>\n",*tmp);
1804
0
            goto parse_error;
1805
20.7k
        }
1806
20.1k
      break;
1807
1808
22.4k
      case ':':
1809
22.4k
        switch(state){
1810
827
          case F_IP6HOST:
1811
827
            state=P_IP6HOST;
1812
827
            break;
1813
845
          case P_IP6HOST:
1814
845
            break;
1815
4.72k
          case P_HOST:
1816
            /*mark  end of host*/
1817
4.72k
            vb->host.len=tmp-vb->host.s;
1818
4.72k
            state=F_PORT;
1819
4.72k
            break;
1820
749
          case L_PORT:
1821
749
            state=F_PORT;
1822
749
            break;
1823
2
          case P_PORT:
1824
2
            LM_ERR("bad port\n");
1825
2
            goto parse_error;
1826
3
          case L_PARAM:
1827
5
          case F_PARAM:
1828
5
          case P_PARAM:
1829
5
            LM_ERR("bad char <%c> in state %d\n",
1830
5
              *tmp,state);
1831
5
            goto parse_error;
1832
2
          case L_VIA:
1833
4
          case F_VIA:
1834
4
            LM_ERR("bad char in compact via\n");
1835
4
            goto parse_error;
1836
1.02k
          case F_CRLF:
1837
3.57k
          case F_LF:
1838
4.36k
          case F_CR:
1839
            /*previous=crlf and now !=' '*/
1840
4.36k
            goto endofheader;
1841
710
          case F_COMMENT:/*everything is allowed in a comment*/
1842
710
            vb->comment.s=tmp;
1843
710
            state=P_COMMENT;
1844
710
            break;
1845
10.2k
          case P_COMMENT: /*everything is allowed in a comment*/
1846
10.2k
            break;
1847
13
          default:
1848
13
            LM_ERR("on <%c> state %d\n", *tmp, state);
1849
13
            goto parse_error;
1850
22.4k
        }
1851
18.0k
        break;
1852
37.9k
      case ';':
1853
37.9k
        switch(state){
1854
5
          case F_HOST:
1855
7
          case F_IP6HOST:
1856
7
            LM_ERR(" no host found\n");
1857
7
            goto parse_error;
1858
2
          case P_IP6HOST:
1859
2
            LM_ERR(" bad ipv6 reference\n");
1860
2
            goto parse_error;
1861
26.4k
          case P_HOST:
1862
26.4k
            vb->host.len=tmp-vb->host.s;
1863
26.4k
            state=F_PARAM;
1864
26.4k
            param_start=tmp+1;
1865
26.4k
            break;
1866
1.06k
          case P_PORT:
1867
            /*mark the end*/
1868
1.06k
            vb->port_str.len=tmp-vb->port_str.s;
1869
            /* fall through */
1870
2.49k
          case L_PORT:
1871
6.82k
          case L_PARAM:
1872
6.82k
            state=F_PARAM;
1873
6.82k
            param_start=tmp+1;
1874
6.82k
            break;
1875
2
          case F_PORT:
1876
2
            LM_ERR(" bad char <%c> in state %d\n",
1877
2
              *tmp,state);
1878
2
            goto parse_error;
1879
8
          case F_PARAM:
1880
8
            LM_ERR("null param?\n");
1881
8
            goto parse_error;
1882
0
          case P_PARAM:
1883
            /*hmm next, param?*/
1884
0
            state=F_PARAM;
1885
0
            param_start=tmp+1;
1886
0
            break;
1887
2
          case L_VIA:
1888
5
          case F_VIA:
1889
5
            LM_ERR("bad char <%c> in next via\n", *tmp);
1890
5
            goto parse_error;
1891
492
          case F_CRLF:
1892
1.08k
          case F_LF:
1893
1.48k
          case F_CR:
1894
            /*previous=crlf and now !=' '*/
1895
1.48k
            goto endofheader;
1896
799
          case F_COMMENT:/*everything is allowed in a comment*/
1897
799
            vb->comment.s=tmp;
1898
799
            state=P_COMMENT;
1899
799
            break;
1900
2.32k
          case P_COMMENT: /*everything is allowed in a comment*/
1901
2.32k
            break;
1902
1903
0
          default:
1904
0
            LM_ERR("on <%c> state %d\n", *tmp, state);
1905
0
            goto parse_error;
1906
37.9k
        }
1907
36.4k
      break;
1908
36.4k
      case ',':
1909
34.7k
        switch(state){
1910
3
          case F_HOST:
1911
5
          case F_IP6HOST:
1912
5
            LM_ERR("no host found\n");
1913
5
            goto parse_error;
1914
2
          case P_IP6HOST:
1915
2
            LM_ERR(" bad ipv6 reference\n");
1916
2
            goto parse_error;
1917
15.4k
          case P_HOST:
1918
            /*mark the end*/
1919
15.4k
            vb->host.len=tmp-vb->host.s;
1920
15.4k
            state=F_VIA;
1921
15.4k
            break;
1922
710
          case P_PORT:
1923
            /*mark the end*/
1924
710
            vb->port_str.len=tmp-vb->port_str.s;
1925
710
            state=F_VIA;
1926
710
            break;
1927
581
          case L_PORT:
1928
1.09k
          case L_PARAM:
1929
1.09k
          case P_PARAM:
1930
1.39k
          case L_VIA:
1931
1.39k
            state=F_VIA;
1932
1.39k
            break;
1933
2
          case F_PORT:
1934
4
          case F_PARAM:
1935
4
            LM_ERR("invalid char <%c> in state %d\n", *tmp,state);
1936
4
            goto parse_error;
1937
4.13k
          case F_VIA:
1938
            /* do  nothing,  eat ","*/
1939
4.13k
            break;
1940
402
          case F_CRLF:
1941
1.34k
          case F_LF:
1942
1.77k
          case F_CR:
1943
            /*previous=crlf and now !=' '*/
1944
1.77k
            goto endofheader;
1945
805
          case F_COMMENT:/*everything is allowed in a comment*/
1946
805
            vb->comment.s=tmp;
1947
805
            state=P_COMMENT;
1948
805
            break;
1949
10.4k
          case P_COMMENT: /*everything is allowed in a comment*/
1950
10.4k
            break;
1951
0
          default:
1952
0
            LM_ERR("on <%c> state %d\n",*tmp, state);
1953
0
            goto  parse_error;
1954
34.7k
        }
1955
32.9k
      break;
1956
2.21M
      case '(':
1957
2.21M
        switch(state){
1958
2
          case F_HOST:
1959
5
          case F_PORT:
1960
7
          case F_PARAM:
1961
10
          case F_VIA:
1962
12
          case F_IP6HOST:
1963
14
          case P_IP6HOST: /*must be terminated in ']'*/
1964
14
            LM_ERR(" on <%c> state %d\n", *tmp, state);
1965
14
            goto  parse_error;
1966
1.16k
          case P_HOST:
1967
            /*mark the end*/
1968
1.16k
            vb->host.len=tmp-vb->host.s;
1969
1.16k
            state=F_COMMENT;
1970
1.16k
            c_nest++;
1971
1.16k
            break;
1972
498
          case P_PORT:
1973
            /*mark the end*/
1974
498
            vb->port_str.len=tmp-vb->port_str.s;
1975
498
            state=F_COMMENT;
1976
498
            c_nest++;
1977
498
            break;
1978
0
          case P_PARAM:
1979
            /*mark the end*/
1980
0
            vb->params.len=tmp-vb->params.s;
1981
0
            state=F_COMMENT;
1982
0
            c_nest++;
1983
0
            break;
1984
590
          case L_PORT:
1985
1.14k
          case L_PARAM:
1986
4.88k
          case L_VIA:
1987
4.88k
            state=F_COMMENT;
1988
4.88k
            vb->params.len=tmp-vb->params.s;
1989
4.88k
            c_nest++;
1990
4.88k
            break;
1991
2.20M
          case P_COMMENT:
1992
2.20M
          case F_COMMENT:
1993
2.20M
            c_nest++;
1994
2.20M
            break;
1995
539
          case F_CRLF:
1996
1.47k
          case F_LF:
1997
1.96k
          case F_CR:
1998
            /*previous=crlf and now !=' '*/
1999
1.96k
            goto endofheader;
2000
0
          default:
2001
0
            LM_ERR("on <%c> state %d\n", *tmp, state);
2002
0
            goto  parse_error;
2003
2.21M
        }
2004
2.20M
      break;
2005
2.20M
      case ')':
2006
40.7k
        switch(state){
2007
2.37k
          case F_COMMENT:
2008
38.5k
          case P_COMMENT:
2009
38.5k
            if (c_nest){
2010
38.5k
              c_nest--;
2011
38.5k
              if(c_nest==0){
2012
6.14k
                state=L_VIA;
2013
6.14k
                vb->comment.len=tmp-vb->comment.s;
2014
6.14k
                break;
2015
6.14k
              }
2016
38.5k
            }else{
2017
0
              LM_ERR(" missing '(' - nesting= %d\n", c_nest);
2018
0
               goto parse_error;
2019
0
            }
2020
32.4k
            break;
2021
32.4k
          case F_HOST:
2022
4
          case F_PORT:
2023
6
          case F_PARAM:
2024
8
          case F_VIA:
2025
10
          case P_HOST:
2026
12
          case P_PORT:
2027
12
          case P_PARAM:
2028
14
          case L_PORT:
2029
16
          case L_PARAM:
2030
21
          case L_VIA:
2031
23
          case F_IP6HOST:
2032
25
          case P_IP6HOST:
2033
25
            LM_ERR(" on <%c> state %d\n",*tmp, state);
2034
25
            goto  parse_error;
2035
881
          case F_CRLF:
2036
1.70k
          case F_LF:
2037
2.11k
          case F_CR:
2038
            /*previous=crlf and now !=' '*/
2039
2.11k
            goto endofheader;
2040
0
          default:
2041
0
            LM_ERR("on <%c> state %d\n", *tmp, state);
2042
0
            goto  parse_error;
2043
40.7k
        }
2044
38.5k
        break;
2045
38.5k
      case '[':
2046
6.17k
        switch(state){
2047
1.53k
          case F_HOST:
2048
1.53k
            vb->host.s=tmp; /* mark start here (include [])*/
2049
1.53k
            state=F_IP6HOST;
2050
1.53k
            break;
2051
637
          case F_COMMENT:/*everything is allowed in a comment*/
2052
637
            vb->comment.s=tmp;
2053
637
            state=P_COMMENT;
2054
637
            break;
2055
1.31k
          case P_COMMENT:
2056
1.31k
            break;
2057
969
          case F_CRLF:
2058
1.82k
          case F_LF:
2059
2.69k
          case F_CR:
2060
            /*previous=crlf and now !=' '*/
2061
2.69k
            goto endofheader;
2062
3
          default:
2063
3
            LM_ERR("on <%c> state %d\n",*tmp, state);
2064
3
            goto  parse_error;
2065
6.17k
        }
2066
3.47k
        break;
2067
5.19k
      case ']':
2068
5.19k
        switch(state){
2069
1.32k
          case P_IP6HOST:
2070
            /*mark the end*/
2071
1.32k
            vb->host.len=(tmp-vb->host.s)+1; /* include "]" */
2072
1.32k
            state=L_PORT;
2073
1.32k
            break;
2074
561
          case F_CRLF:
2075
2.09k
          case F_LF:
2076
2.63k
          case F_CR:
2077
            /*previous=crlf and now !=' '*/
2078
2.63k
            goto endofheader;
2079
419
          case F_COMMENT:/*everything is allowed in a comment*/
2080
419
            vb->comment.s=tmp;
2081
419
            state=P_COMMENT;
2082
419
            break;
2083
816
          case P_COMMENT:
2084
816
            break;
2085
3
          default:
2086
3
            LM_ERR("on <%c> state %d\n",*tmp, state);
2087
3
            goto  parse_error;
2088
5.19k
        }
2089
2.55k
        break;
2090
121k
      case '\0':
2091
121k
        break;
2092
2093
753k
      default:
2094
753k
        switch(state){
2095
158k
          case F_HOST:
2096
158k
            state=P_HOST;
2097
158k
            vb->host.s=tmp;
2098
            //break;
2099
212k
          case P_HOST:
2100
            /*check if host allowed char*/
2101
212k
            if ( (*tmp<'a' || *tmp>'z') && (*tmp<'A' || *tmp>'Z')
2102
51.9k
            && (*tmp<'0' || *tmp>'9') && *tmp!='-' && *tmp!='.')
2103
135
              goto parse_error;
2104
212k
            break;
2105
212k
          case F_PORT:
2106
5.39k
            state=P_PORT;
2107
5.39k
            vb->port_str.s=tmp;
2108
            //break;
2109
9.78k
          case P_PORT:
2110
            /*check if number*/
2111
9.78k
            if ( *tmp<'0' || *tmp>'9' )
2112
45
              goto parse_error;
2113
9.74k
            break;
2114
140k
          case F_PARAM:
2115
140k
            /*state=P_PARAM*/;
2116
140k
            if(vb->params.s==0) vb->params.s=param_start;
2117
140k
            param=pkg_malloc(sizeof(struct via_param));
2118
140k
            if (param==0){
2119
0
              LM_ERR("no pkg memory left\n");
2120
0
              goto error;
2121
0
            }
2122
140k
            memset(param,0, sizeof(struct via_param));
2123
140k
            param->start=param_start;
2124
140k
            tmp=parse_via_param(tmp, end, &state, &saved_state,
2125
140k
                      param);
2126
2127
140k
            switch(state){
2128
107k
              case F_PARAM:
2129
107k
                param_start=tmp+1;
2130
110k
              case L_PARAM:
2131
112k
              case F_LF:
2132
114k
              case F_CR:
2133
114k
                vb->params.len=tmp - vb->params.s;
2134
114k
                break;
2135
3.57k
              case F_VIA:
2136
3.57k
                vb->params.len=param->start+param->size
2137
3.57k
                        -vb->params.s;
2138
3.57k
                break;
2139
20.5k
              case END_OF_HEADER:
2140
20.5k
                vb->params.len=param->start+param->size
2141
20.5k
                        -vb->params.s;
2142
20.5k
                break;
2143
2.16k
              case PARAM_ERROR:
2144
2.16k
                pkg_free(param);
2145
2.16k
                goto parse_error;
2146
0
              default:
2147
0
                pkg_free(param);
2148
0
                LM_ERR(" after parse_via_param: invalid "
2149
0
                    "char <%c> on state %d\n",*tmp, state);
2150
0
                goto parse_error;
2151
140k
            }
2152
            /*add param to the list*/
2153
138k
            if (vb->last_param)  vb->last_param->next=param;
2154
27.8k
            else        vb->param_lst=param;
2155
138k
            vb->last_param=param;
2156
            /* update param. shortcuts */
2157
138k
            switch(param->type){
2158
1.94k
              case PARAM_BRANCH:
2159
1.94k
                vb->branch=param;
2160
1.94k
                break;
2161
6.00k
              case PARAM_RECEIVED:
2162
6.00k
                vb->received=param;
2163
6.00k
                break;
2164
4.98k
              case PARAM_RPORT:
2165
4.98k
                vb->rport=param;
2166
4.98k
                break;
2167
2.49k
              case PARAM_I:
2168
2.49k
                vb->i=param;
2169
2.49k
                break;
2170
2.51k
              case PARAM_ALIAS:
2171
2.51k
                vb->alias=param;
2172
2.51k
                break;
2173
2.19k
              case PARAM_MADDR:
2174
2.19k
                vb->maddr=param;
2175
2.19k
                break;
2176
138k
            }
2177
2178
138k
            if (state==END_OF_HEADER){
2179
20.5k
              state=saved_state;
2180
20.5k
              goto endofheader;
2181
20.5k
            }
2182
117k
            break;
2183
117k
          case P_PARAM:
2184
0
            break;
2185
20.9k
          case F_VIA:
2186
            /*vb->next=tmp;*/ /*???*/
2187
20.9k
            goto nextvia;
2188
23
          case L_PORT:
2189
43
          case L_PARAM:
2190
50
          case L_VIA:
2191
50
            LM_ERR("on <%c> state %d (default)\n",*tmp, state);
2192
50
            goto  parse_error;
2193
1.10k
          case F_COMMENT:
2194
1.10k
            state=P_COMMENT;
2195
1.10k
            vb->comment.s=tmp;
2196
1.10k
            break;
2197
279k
          case P_COMMENT:
2198
279k
            break;
2199
660
          case F_IP6HOST:
2200
660
            state=P_IP6HOST;
2201
            //break;
2202
2.61k
          case P_IP6HOST:
2203
            /*check if host allowed char*/
2204
2.61k
            if ( (*tmp<'a' || *tmp>'f') && (*tmp<'A' || *tmp>'F')
2205
612
            && (*tmp<'0' || *tmp>'9') && *tmp!=':')
2206
47
              goto parse_error;
2207
2.56k
            break;
2208
2.56k
          case F_CRLF:
2209
84.0k
          case F_LF:
2210
86.9k
          case F_CR:
2211
            /*previous=crlf and now !=' '*/
2212
86.9k
            goto endofheader;
2213
0
          default:
2214
0
            LM_ERR("invalid char <%c> in state %d\n",*tmp, state);
2215
0
            goto parse_error;
2216
753k
        }
2217
2218
2219
3.44M
    }
2220
3.44M
  }
2221
2222
11.8k
  LM_DBG("end of packet reached, state=%d\n", state);
2223
11.8k
  goto endofpacket; /*end of packet, probably should be goto error*/
2224
2225
125k
endofheader:
2226
125k
  state=saved_state;
2227
125k
  LM_DBG("end of header reached, state=%d\n", state);
2228
136k
endofpacket:
2229
  /* check if error*/
2230
136k
  switch(state){
2231
9.87k
    case P_HOST:
2232
108k
    case L_PORT:
2233
108k
    case P_PORT:
2234
132k
    case L_PARAM:
2235
132k
    case P_PARAM:
2236
132k
    case P_VALUE:
2237
132k
    case GEN_PARAM:
2238
132k
    case FIN_HIDDEN:
2239
135k
    case L_VIA:
2240
135k
      break;
2241
1.93k
    default:
2242
1.93k
      LM_ERR(" invalid via - end of header in state %d\n", state);
2243
1.93k
      goto parse_error;
2244
136k
  }
2245
2246
2247
  /*
2248
  if (proto) printf("<SIP/2.0/%s>\n", proto);
2249
  if (host) printf("host= <%s>\n", host);
2250
  if (port_str) printf("port= <%s>\n", port_str);
2251
  if (param) printf("params= <%s>\n", param);
2252
  if (comment) printf("comment= <%s>\n", comment);
2253
  if(next_via) printf("next_via= <%s>\n", next_via);
2254
  */
2255
  /*LM_DBG("rest=<%s>\n", tmp);*/
2256
2257
135k
  vb->error=PARSE_OK;
2258
135k
  vb->bsize=tmp-buffer;
2259
135k
  if (vb->port_str.s){
2260
4.24k
    vb->port=str2s(vb->port_str.s, vb->port_str.len, &err);
2261
4.24k
    if (err){
2262
11
          LM_ERR(" invalid port number <%.*s>\n",
2263
11
            vb->port_str.len, ZSW(vb->port_str.s));
2264
11
          goto parse_error;
2265
11
    }
2266
4.24k
  }
2267
135k
  return tmp;
2268
20.9k
nextvia:
2269
20.9k
  LM_DBG("next_via\n");
2270
20.9k
  vb->error=PARSE_OK;
2271
20.9k
  vb->bsize=tmp-buffer;
2272
20.9k
  if (vb->port_str.s){
2273
1.02k
    vb->port=str2s(vb->port_str.s, vb->port_str.len, &err);
2274
1.02k
    if (err){
2275
2
          LM_ERR(" invalid port number <%.*s>\n",
2276
2
            vb->port_str.len, ZSW(vb->port_str.s));
2277
2
          goto parse_error;
2278
2
    }
2279
1.02k
  }
2280
20.9k
  vb->next=pkg_malloc(sizeof(struct via_body));
2281
20.9k
  if (vb->next==0){
2282
0
    LM_ERR(" out of pkg memory\n");
2283
0
    goto error;
2284
0
  }
2285
20.9k
  vb=vb->next;
2286
20.9k
  memset(vb, 0, sizeof(struct via_body));
2287
20.9k
  buffer=tmp;
2288
20.9k
  goto parse_again;
2289
2290
8.14k
parse_error:
2291
8.14k
  if (end>buffer){
2292
8.14k
    LM_ERR(" <%.*s>\n", (int)(end-buffer), ZSW(buffer));
2293
8.14k
  }
2294
8.14k
  if ((tmp>buffer)&&(tmp<end)){
2295
1.34k
    LM_ERR("parsed so far:<%.*s>\n",
2296
1.34k
        (int)(tmp-buffer), ZSW(buffer) );
2297
6.80k
  }else{
2298
6.80k
    LM_ERR("via parse failed\n");
2299
6.80k
  }
2300
8.14k
error:
2301
8.14k
  vb->error=PARSE_ERROR;
2302
8.14k
  vbody->error=PARSE_ERROR; /* make sure the first via body is marked
2303
                 as bad also */
2304
8.14k
  return tmp;
2305
8.14k
}
2306
2307
2308
static inline void free_via_param_list(struct via_param* vp)
2309
27.8k
{
2310
27.8k
  struct via_param* foo;
2311
166k
  while(vp){
2312
138k
    foo=vp;
2313
138k
    vp=vp->next;
2314
138k
    pkg_free(foo);
2315
138k
  }
2316
27.8k
}
2317
2318
2319
void free_via_list(struct via_body* vb)
2320
143k
{
2321
143k
  struct via_body* foo;
2322
307k
  while(vb){
2323
164k
    foo=vb;
2324
164k
    vb=vb->next;
2325
164k
    if (foo->param_lst) free_via_param_list(foo->param_lst);
2326
164k
    pkg_free(foo);
2327
164k
  }
2328
143k
}