Coverage Report

Created: 2025-10-13 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_to.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2003 Fhg Fokus
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 *
20
 * History:
21
 * ---------
22
 * 2003-04-26 ZSW (jiri)
23
 * 2006-05-29 removed the NO_PINGTEL_TAG_HACK - it's conflicting the RFC 3261;
24
 *            TAG parameter must have value; other parameters are accepted
25
 *            without value (bogdan)
26
 */
27
28
29
#include "parse_to.h"
30
#include <stdlib.h>
31
#include <string.h>
32
#include "../dprint.h"
33
#include "msg_parser.h"
34
#include "parse_uri.h"
35
#include "../ut.h"
36
#include "../mem/mem.h"
37
#include "../errinfo.h"
38
39
40
enum {
41
  START_TO, DISPLAY_QUOTED, E_DISPLAY_QUOTED, DISPLAY_TOKEN, DISPLAY_TOKEN2,
42
  S_URI_ENCLOSED, URI_ENCLOSED, E_URI_ENCLOSED,
43
  URI_OR_TOKEN, MAYBE_URI_END, END, F_CR, F_LF, F_CRLF
44
};
45
46
47
enum {
48
  S_PARA_NAME=20, PARA_NAME, S_EQUAL, S_PARA_VALUE, TAG1, TAG2,
49
  TAG3, PARA_VALUE_TOKEN , PARA_VALUE_QUOTED, E_PARA_VALUE
50
};
51
52
53
54
#define add_param( _param , _body ) \
55
75.8k
  do{\
56
75.8k
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
75.8k
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
75.8k
    else (_body)->last_param->next=(_param);\
60
75.8k
    (_body)->last_param =(_param);\
61
75.8k
    if ((_param)->type==TAG_PARAM)\
62
75.8k
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
75.8k
    (_param) = 0;\
64
75.8k
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
4.77M
{
70
4.77M
  struct to_param *tp=tb->param_lst;
71
4.77M
  struct to_param *foo;
72
4.84M
  while (tp){
73
75.8k
    foo = tp->next;
74
75.8k
    pkg_free(tp);
75
75.8k
    tp=foo;
76
75.8k
  }
77
78
4.77M
  tb->param_lst = tb->last_param = NULL;
79
4.77M
}
80
81
82
void free_to(struct to_body* tb)
83
4.82M
{
84
4.82M
  if (tb) {
85
4.76M
    free_to( tb->next );
86
4.76M
    free_to_params(tb);
87
4.76M
    pkg_free(tb);
88
4.76M
  }
89
4.82M
}
90
91
92
static inline char* parse_to_param(char *buffer, char *end,
93
          struct to_body *to_b,
94
          int *returned_status,
95
          int multi)
96
36.3k
{
97
36.3k
  struct to_param *param;
98
36.3k
  int status;
99
36.3k
  int saved_status;
100
36.3k
  char  *tmp;
101
102
36.3k
  param=0;
103
36.3k
  status=E_PARA_VALUE;
104
36.3k
  saved_status=E_PARA_VALUE;
105
830k
  for( tmp=buffer; tmp<end; tmp++)
106
829k
  {
107
829k
    switch(*tmp)
108
829k
    {
109
14.6k
      case ' ':
110
26.4k
      case '\t':
111
26.4k
        switch (status)
112
26.4k
        {
113
482
          case TAG3:
114
482
            param->type=TAG_PARAM;
115
4.61k
          case PARA_NAME:
116
5.89k
          case TAG1:
117
6.34k
          case TAG2:
118
6.34k
            param->name.len = tmp-param->name.s;
119
6.34k
            status = S_EQUAL;
120
6.34k
            break;
121
3.23k
          case PARA_VALUE_TOKEN:
122
3.23k
            param->value.len = tmp-param->value.s;
123
3.23k
            status = E_PARA_VALUE;
124
3.23k
            add_param( param , to_b );
125
3.23k
            break;
126
996
          case F_CRLF:
127
7.04k
          case F_LF:
128
12.4k
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
12.4k
            status=saved_status;
131
12.4k
            break;
132
26.4k
        }
133
26.4k
        break;
134
26.6k
      case '\n':
135
26.6k
        switch (status)
136
26.6k
        {
137
885
          case S_PARA_NAME:
138
2.15k
          case S_EQUAL:
139
3.30k
          case S_PARA_VALUE:
140
4.48k
          case E_PARA_VALUE:
141
4.48k
            saved_status=status;
142
4.48k
            status=F_LF;
143
4.48k
            break;
144
436
          case TAG3:
145
436
            param->type=TAG_PARAM;
146
8.56k
          case PARA_NAME:
147
10.2k
          case TAG1:
148
10.7k
          case TAG2:
149
10.7k
            param->name.len = tmp-param->name.s;
150
10.7k
            saved_status = S_EQUAL;
151
10.7k
            status = F_LF;
152
10.7k
            break;
153
3.12k
          case PARA_VALUE_TOKEN:
154
3.12k
            param->value.len = tmp-param->value.s;
155
3.12k
            saved_status = E_PARA_VALUE;
156
3.12k
            status = F_LF;
157
3.12k
            add_param( param , to_b );
158
3.12k
            break;
159
8.02k
          case F_CR:
160
8.02k
            status=F_CRLF;
161
8.02k
            break;
162
3
          case F_CRLF:
163
6
          case F_LF:
164
6
            status=saved_status;
165
6
            goto endofheader;
166
223
          default:
167
223
            goto parse_error;
168
26.6k
        }
169
26.3k
        break;
170
26.3k
      case '\r':
171
20.3k
        switch (status)
172
20.3k
        {
173
1.22k
          case S_PARA_NAME:
174
1.96k
          case S_EQUAL:
175
2.43k
          case S_PARA_VALUE:
176
3.04k
          case E_PARA_VALUE:
177
3.04k
            saved_status=status;
178
3.04k
            status=F_CR;
179
3.04k
            break;
180
558
          case TAG3:
181
558
            param->type=TAG_PARAM;
182
11.6k
          case PARA_NAME:
183
14.1k
          case TAG1:
184
14.6k
          case TAG2:
185
14.6k
            param->name.len = tmp-param->name.s;
186
14.6k
            saved_status = S_EQUAL;
187
14.6k
            status = F_CR;
188
14.6k
            break;
189
2.66k
          case PARA_VALUE_TOKEN:
190
2.66k
            param->value.len = tmp-param->value.s;
191
2.66k
            saved_status = E_PARA_VALUE;
192
2.66k
            status = F_CR;
193
2.66k
            add_param( param , to_b );
194
2.66k
            break;
195
3
          case F_CRLF:
196
37
          case F_CR:
197
43
          case F_LF:
198
43
            status=saved_status;
199
43
            goto endofheader;
200
30
          default:
201
30
            goto parse_error;
202
20.3k
        }
203
20.3k
        break;
204
20.3k
      case  0:
205
9.38k
      case ',':
206
9.38k
        switch (status)
207
9.38k
        {
208
1.74k
          case PARA_VALUE_QUOTED:
209
1.74k
            break;
210
3.58k
          case PARA_NAME:
211
3.58k
            param->name.len = tmp-param->name.s;
212
4.39k
          case S_EQUAL:
213
4.88k
          case S_PARA_VALUE:
214
4.88k
            if (param->type==TAG_PARAM)
215
18
              goto parse_error;
216
4.86k
            param->value.s = tmp;
217
6.80k
          case PARA_VALUE_TOKEN:
218
6.80k
            status = E_PARA_VALUE;
219
6.80k
            param->value.len = tmp-param->value.s;
220
6.80k
            add_param( param , to_b );
221
7.49k
          case E_PARA_VALUE:
222
7.49k
            saved_status = status;
223
7.49k
            if ( !multi && *tmp==',')
224
194
              goto parse_error;
225
7.29k
            goto endofheader;
226
7.29k
            break;
227
7.29k
          default:
228
128
            goto parse_error;
229
9.38k
        }
230
1.74k
        break;
231
1.74k
      case '\\':
232
936
        switch (status)
233
936
        {
234
769
          case PARA_VALUE_QUOTED:
235
769
            if (tmp+1==end)
236
6
              goto parse_error;
237
763
            switch (*(tmp+1))
238
763
            {
239
18
              case '\r':
240
37
              case '\n':
241
37
                break;
242
726
              default:
243
726
                tmp++;
244
726
                break;
245
763
            }
246
763
            break;
247
763
          default:
248
167
            goto parse_error;
249
936
        }
250
763
        break;
251
5.76k
      case '"':
252
5.76k
        switch (status)
253
5.76k
        {
254
2.20k
          case S_PARA_VALUE:
255
2.20k
            param->value.s = tmp+1;
256
2.20k
            status = PARA_VALUE_QUOTED;
257
2.20k
            break;
258
1.82k
          case PARA_VALUE_QUOTED:
259
1.82k
            param->value.len=tmp-param->value.s ;
260
1.82k
            add_param( param , to_b );
261
1.82k
            status = E_PARA_VALUE;
262
1.82k
            break;
263
415
          case F_CRLF:
264
1.19k
          case F_LF:
265
1.62k
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
1.62k
            goto endofheader;
268
111
          default:
269
111
            goto parse_error;
270
5.76k
        }
271
4.02k
        break;
272
82.0k
      case ';' :
273
82.0k
        switch (status)
274
82.0k
        {
275
1.16k
          case PARA_VALUE_QUOTED:
276
1.16k
            break;
277
21.3k
          case PARA_NAME:
278
21.3k
            param->name.len = tmp-param->name.s;
279
26.1k
          case S_EQUAL:
280
28.9k
          case S_PARA_VALUE:
281
28.9k
            if (param->type==TAG_PARAM)
282
28
              goto parse_error;
283
28.9k
            param->value.s = tmp;
284
36.5k
          case PARA_VALUE_TOKEN:
285
36.5k
            param->value.len=tmp-param->value.s;
286
36.5k
            add_param(param,to_b);
287
78.9k
          case E_PARA_VALUE:
288
78.9k
            param = (struct to_param*)
289
78.9k
              pkg_malloc(sizeof(struct to_param));
290
78.9k
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
78.9k
            memset(param,0,sizeof(struct to_param));
295
78.9k
            param->type=GENERAL_PARAM;
296
78.9k
            status = S_PARA_NAME;
297
78.9k
            break;
298
582
          case F_CRLF:
299
1.24k
          case F_LF:
300
1.82k
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
1.82k
            goto endofheader;
303
101
          default:
304
101
            goto parse_error;
305
82.0k
        }
306
80.0k
        break;
307
80.0k
      case 'T':
308
58.8k
      case 't' :
309
58.8k
        switch (status)
310
58.8k
        {
311
847
          case PARA_VALUE_QUOTED:
312
4.96k
          case PARA_VALUE_TOKEN:
313
14.5k
          case PARA_NAME:
314
14.5k
            break;
315
32.5k
          case S_PARA_NAME:
316
32.5k
            param->name.s = tmp;
317
32.5k
            status = TAG1;
318
32.5k
            break;
319
1.64k
          case S_PARA_VALUE:
320
1.64k
            param->value.s = tmp;
321
1.64k
            status = PARA_VALUE_TOKEN;
322
1.64k
            break;
323
1.17k
          case TAG1:
324
2.52k
          case TAG2:
325
3.07k
          case TAG3:
326
3.07k
            status = PARA_NAME;
327
3.07k
            break;
328
1.35k
          case F_CRLF:
329
5.44k
          case F_LF:
330
7.01k
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
7.01k
            goto endofheader;
333
38
          default:
334
38
            goto parse_error;
335
58.8k
        }
336
51.8k
        break;
337
51.8k
      case 'A':
338
52.6k
      case 'a' :
339
52.6k
        switch (status)
340
52.6k
        {
341
1.13k
          case PARA_VALUE_QUOTED:
342
7.29k
          case PARA_VALUE_TOKEN:
343
25.4k
          case PARA_NAME:
344
25.4k
            break;
345
2.59k
          case S_PARA_NAME:
346
2.59k
            param->name.s = tmp;
347
2.59k
            status = PARA_NAME;
348
2.59k
            break;
349
1.10k
          case S_PARA_VALUE:
350
1.10k
            param->value.s = tmp;
351
1.10k
            status = PARA_VALUE_TOKEN;
352
1.10k
            break;
353
17.4k
          case TAG1:
354
17.4k
            status = TAG2;
355
17.4k
            break;
356
1.10k
          case TAG2:
357
1.64k
          case TAG3:
358
1.64k
            status = PARA_NAME;
359
1.64k
            break;
360
2.53k
          case F_CRLF:
361
3.84k
          case F_LF:
362
4.29k
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
4.29k
            goto endofheader;
365
47
          default:
366
47
            goto parse_error;
367
52.6k
        }
368
48.2k
        break;
369
48.2k
      case 'G':
370
29.8k
      case 'g' :
371
29.8k
        switch (status)
372
29.8k
        {
373
609
          case PARA_VALUE_QUOTED:
374
3.62k
          case PARA_VALUE_TOKEN:
375
8.92k
          case PARA_NAME:
376
8.92k
            break;
377
2.72k
          case S_PARA_NAME:
378
2.72k
            param->name.s = tmp;
379
2.72k
            status = PARA_NAME;
380
2.72k
            break;
381
2.82k
          case S_PARA_VALUE:
382
2.82k
            param->value.s = tmp;
383
2.82k
            status = PARA_VALUE_TOKEN;
384
2.82k
            break;
385
785
          case TAG1:
386
2.17k
          case TAG3:
387
2.17k
            status = PARA_NAME;
388
2.17k
            break;
389
10.5k
          case TAG2:
390
10.5k
            status = TAG3;
391
10.5k
            break;
392
447
          case F_CRLF:
393
2.02k
          case F_LF:
394
2.61k
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
2.61k
            goto endofheader;
397
84
          default:
398
84
            goto parse_error;
399
29.8k
        }
400
27.1k
        break;
401
27.6k
      case '=':
402
27.6k
        switch (status)
403
27.6k
        {
404
801
          case PARA_VALUE_QUOTED:
405
801
            break;
406
5.59k
          case TAG3:
407
5.59k
            param->type=TAG_PARAM;
408
17.4k
          case PARA_NAME:
409
20.2k
          case TAG1:
410
20.9k
          case TAG2:
411
20.9k
            param->name.len = tmp-param->name.s;
412
20.9k
            status = S_PARA_VALUE;
413
20.9k
            break;
414
4.16k
          case S_EQUAL:
415
4.16k
            status = S_PARA_VALUE;
416
4.16k
            break;
417
491
          case F_CRLF:
418
1.11k
          case F_LF:
419
1.51k
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
1.51k
            goto endofheader;
422
193
          default:
423
193
            goto parse_error;
424
27.6k
        }
425
25.8k
        break;
426
488k
      default:
427
488k
        switch (status)
428
488k
        {
429
4.93k
          case TAG1:
430
7.33k
          case TAG2:
431
8.28k
          case TAG3:
432
8.28k
            status = PARA_NAME;
433
8.28k
            break;
434
171k
          case PARA_VALUE_TOKEN:
435
401k
          case PARA_NAME:
436
420k
          case PARA_VALUE_QUOTED:
437
420k
            break;
438
39.9k
          case S_PARA_NAME:
439
39.9k
            param->name.s = tmp;
440
39.9k
            status = PARA_NAME;
441
39.9k
            break;
442
13.2k
          case S_PARA_VALUE:
443
13.2k
            param->value.s = tmp;
444
13.2k
            status = PARA_VALUE_TOKEN;
445
13.2k
            break;
446
1.20k
          case F_CRLF:
447
3.87k
          case F_LF:
448
6.46k
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
6.46k
            goto endofheader;
451
839
          default:
452
839
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
839
            goto error;
454
488k
        }
455
829k
    }/*switch*/
456
829k
  }/*for*/
457
458
1.45k
  if (status==PARA_VALUE_QUOTED) {
459
120
      LM_ERR("unexpected end of header in state %d\n", status);
460
120
      goto parse_error;
461
120
  }
462
463
34.0k
endofheader:
464
34.0k
  LM_DBG("end of header reached, state=%d\n", status);
465
34.0k
  if (param) {
466
23.0k
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
21.9k
      saved_status = E_PARA_VALUE;
468
21.9k
      param->value.s= 0;
469
21.9k
      param->value.len=0;
470
21.9k
      if (param->type==TAG_PARAM)
471
191
        goto parse_error;
472
21.7k
      add_param(param, to_b);
473
21.7k
    } else {
474
1.14k
      pkg_free(param);
475
1.14k
    }
476
23.0k
  }
477
33.8k
  *returned_status=saved_status;
478
33.8k
  return tmp;
479
480
1.67k
parse_error:
481
1.67k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
1.67k
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
2.51k
error:
484
2.51k
  if (param) pkg_free(param);
485
2.51k
  free_to_params(to_b);
486
2.51k
  to_b->error=PARSE_ERROR;
487
2.51k
  *returned_status = status;
488
2.51k
  return tmp;
489
1.67k
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
65.8k
{
497
65.8k
  int status;
498
65.8k
  int saved_status;
499
65.8k
  char  *tmp;
500
65.8k
  char  *end_mark;
501
65.8k
  struct to_body *first_b = to_b;
502
503
65.8k
  status=START_TO;
504
65.8k
  saved_status=START_TO;
505
65.8k
  memset(to_b, 0, sizeof(struct to_body));
506
65.8k
  to_b->error=PARSE_OK;
507
65.8k
  end_mark=0;
508
509
11.3M
  for( tmp=buffer; tmp<end; tmp++)
510
11.3M
  {
511
11.3M
    switch(*tmp)
512
11.3M
    {
513
809k
      case ' ':
514
819k
      case '\t':
515
819k
        switch (status)
516
819k
        {
517
698
          case F_CRLF:
518
3.25k
          case F_LF:
519
5.44k
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
5.44k
            status=saved_status;
522
5.44k
            break;
523
1.72k
          case URI_ENCLOSED:
524
1.72k
            to_b->uri.len = tmp - to_b->uri.s;
525
1.72k
            status = E_URI_ENCLOSED;
526
1.72k
            break;
527
7.50k
          case URI_OR_TOKEN:
528
7.50k
            status = MAYBE_URI_END;
529
7.50k
            end_mark = tmp;
530
7.50k
            break;
531
1.60k
          case DISPLAY_TOKEN:
532
1.60k
            end_mark = tmp;
533
1.60k
            status = DISPLAY_TOKEN2;
534
1.60k
            break;
535
819k
        }
536
819k
        break;
537
819k
      case '\n':
538
26.4k
        switch (status)
539
26.4k
        {
540
10.7k
          case URI_OR_TOKEN:
541
10.7k
            end_mark = tmp;
542
10.7k
            status = MAYBE_URI_END;
543
11.8k
          case MAYBE_URI_END:
544
12.4k
          case DISPLAY_TOKEN:
545
13.0k
          case DISPLAY_TOKEN2:
546
13.8k
          case E_DISPLAY_QUOTED:
547
18.9k
          case END:
548
18.9k
            saved_status=status;
549
18.9k
            status=F_LF;
550
18.9k
            break;
551
6.94k
          case F_CR:
552
6.94k
            status=F_CRLF;
553
6.94k
            break;
554
2
          case F_CRLF:
555
9
          case F_LF:
556
9
            status=saved_status;
557
9
            goto endofheader;
558
560
          default:
559
560
            goto parse_error;
560
26.4k
        }
561
25.8k
        break;
562
25.8k
      case '\r':
563
14.6k
        switch (status)
564
14.6k
        {
565
10.9k
          case URI_OR_TOKEN:
566
10.9k
            end_mark = tmp;
567
10.9k
            status = MAYBE_URI_END;
568
            /* fall through */
569
11.9k
          case MAYBE_URI_END:
570
12.5k
          case DISPLAY_TOKEN:
571
13.1k
          case DISPLAY_TOKEN2:
572
13.6k
          case E_DISPLAY_QUOTED:
573
14.5k
          case END:
574
14.5k
            saved_status=status;
575
14.5k
            status=F_CR;
576
14.5k
            break;
577
3
          case F_CRLF:
578
40
          case F_CR:
579
45
          case F_LF:
580
45
            status=saved_status;
581
45
            goto endofheader;
582
42
          default:
583
42
            goto parse_error;
584
14.6k
        }
585
14.5k
        break;
586
14.5k
      case 0:
587
2.95k
        switch (status)
588
2.95k
        {
589
1.76k
          case URI_OR_TOKEN:
590
2.30k
          case MAYBE_URI_END:
591
2.30k
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
2.84k
          case END:
594
2.84k
            saved_status = status = END;
595
2.84k
            goto endofheader;
596
111
          default:
597
111
            goto parse_error;
598
2.95k
        }
599
0
        break;
600
4.93M
      case ',':
601
4.93M
        switch (status)
602
4.93M
        {
603
799
          case DISPLAY_QUOTED:
604
232k
          case URI_ENCLOSED:
605
232k
            break;
606
4.70M
          case URI_OR_TOKEN:
607
            /* the next transition cannot be determined here. The
608
             * ',' maybe part of the username inside URI, or 
609
             * it can be separator between 2 hdr parts. As this
610
             * parsed is not URI aware (we do not actually parse
611
             * the URI, but we simply skip it), we have no idea
612
             * in which care we are..... For the moment, if the
613
             * header is marked as single part, at least let's
614
             * consider the ',' as part of the URI */
615
4.70M
            if (multi==0)
616
2.25k
              break;
617
4.69M
          case MAYBE_URI_END:
618
4.69M
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
4.70M
          case END:
621
4.70M
            if (multi==0)
622
23
              goto parse_error;
623
4.70M
            to_b->next = (struct to_body*)
624
4.70M
              pkg_malloc(sizeof(struct to_body));
625
4.70M
            if (to_b->next==NULL) {
626
0
              LM_ERR("failed to allocate new TO body\n");
627
0
              goto error;
628
0
            }
629
4.70M
            to_b = to_b->next;
630
4.70M
            memset(to_b, 0, sizeof(struct to_body));
631
4.70M
            to_b->error = PARSE_OK;
632
4.70M
            saved_status = status = START_TO;
633
4.70M
            end_mark=0;
634
4.70M
            break;
635
125
          default:
636
125
            goto parse_error;
637
4.93M
        }
638
4.93M
        break;
639
4.93M
      case '\\':
640
830
        switch (status)
641
830
        {
642
783
          case DISPLAY_QUOTED:
643
783
            tmp++; /* jump over next char */
644
783
            break;
645
47
          default:
646
47
            goto parse_error;
647
830
        }
648
783
        break;
649
11.6k
      case '<':
650
11.6k
        switch (status)
651
11.6k
        {
652
5.74k
          case START_TO:
653
5.74k
            to_b->body.s=tmp;
654
5.74k
            status = S_URI_ENCLOSED;
655
5.74k
            break;
656
480
          case DISPLAY_QUOTED:
657
480
            break;
658
444
          case E_DISPLAY_QUOTED:
659
444
            status = S_URI_ENCLOSED;
660
444
            break;
661
1.31k
          case URI_OR_TOKEN:
662
2.14k
          case DISPLAY_TOKEN:
663
2.14k
            end_mark = tmp;
664
            /* fall through */
665
2.56k
          case DISPLAY_TOKEN2:
666
3.14k
          case MAYBE_URI_END:
667
3.14k
            to_b->display.len=end_mark-to_b->display.s;
668
3.14k
            status = S_URI_ENCLOSED;
669
3.14k
            break;
670
757
          case F_CRLF:
671
1.32k
          case F_LF:
672
1.78k
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
1.78k
            goto endofheader;
675
48
          default:
676
48
            goto parse_error;
677
11.6k
        }
678
9.81k
        break;
679
14.3k
      case '>':
680
14.3k
        switch (status)
681
14.3k
        {
682
782
          case DISPLAY_QUOTED:
683
782
            break;
684
7.26k
          case URI_ENCLOSED:
685
7.26k
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
8.94k
          case E_URI_ENCLOSED:
688
8.94k
            status = END;
689
8.94k
            break;
690
2.52k
          case F_CRLF:
691
3.25k
          case F_LF:
692
4.51k
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
4.51k
            goto endofheader;
695
104
          default:
696
104
            goto parse_error;
697
14.3k
        }
698
9.73k
        break;
699
9.73k
      case '"':
700
2.77k
        switch (status)
701
2.77k
        {
702
745
          case START_TO:
703
745
            to_b->body.s = tmp;
704
745
            to_b->display.s = tmp;
705
745
            status = DISPLAY_QUOTED;
706
745
            break;
707
551
          case DISPLAY_QUOTED:
708
551
            status = E_DISPLAY_QUOTED;
709
551
            to_b->display.len = tmp-to_b->display.s+1;
710
551
            break;
711
393
          case F_CRLF:
712
879
          case F_LF:
713
1.42k
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
1.42k
            goto endofheader;
716
51
          default:
717
51
            goto parse_error;
718
2.77k
        }
719
1.29k
        break;
720
55.0k
      case ';' :
721
55.0k
        switch (status)
722
55.0k
        {
723
484
          case DISPLAY_QUOTED:
724
1.11k
          case DISPLAY_TOKEN:
725
16.4k
          case URI_ENCLOSED:
726
16.4k
            break;
727
31.7k
          case URI_OR_TOKEN:
728
31.7k
            end_mark = tmp;
729
            /* fall through */
730
34.4k
          case MAYBE_URI_END:
731
34.4k
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
36.3k
          case END:
734
36.3k
            to_b->body.len = tmp-to_b->body.s;
735
36.3k
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
36.3k
            if (to_b->error!=PARSE_ERROR && multi && *tmp==',') {
737
              /* continue with a new body instance */
738
2.96k
              to_b->next = (struct to_body*)
739
2.96k
                pkg_malloc(sizeof(struct to_body));
740
2.96k
              if (to_b->next==NULL) {
741
0
                LM_ERR("failed to allocate new TO body\n");
742
0
                goto error;
743
0
              }
744
2.96k
              to_b = to_b->next;
745
2.96k
              memset(to_b, 0, sizeof(struct to_body));
746
2.96k
              to_b->error=PARSE_OK;
747
2.96k
              saved_status = status = START_TO;
748
2.96k
              end_mark=0;
749
2.96k
              break;
750
33.3k
            } else {
751
33.3k
              goto endofheader;
752
33.3k
            }
753
452
          case F_CRLF:
754
1.74k
          case F_LF:
755
2.24k
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
2.24k
            goto endofheader;
758
38
          default:
759
38
            goto parse_error;
760
55.0k
        }
761
19.3k
        break;
762
5.50M
      default:
763
5.50M
        switch (status)
764
5.50M
        {
765
4.76M
          case START_TO:
766
4.76M
            to_b->uri.s = to_b->body.s = tmp;
767
4.76M
            status = URI_OR_TOKEN;
768
4.76M
            to_b->display.s=tmp;
769
4.76M
            break;
770
9.21k
          case S_URI_ENCLOSED:
771
9.21k
            to_b->uri.s=tmp;
772
9.21k
            status=URI_ENCLOSED;
773
9.21k
            break;
774
1.92k
          case MAYBE_URI_END:
775
2.92k
          case DISPLAY_TOKEN2:
776
2.92k
            status = DISPLAY_TOKEN;
777
12.1k
          case DISPLAY_QUOTED:
778
19.7k
          case DISPLAY_TOKEN:
779
401k
          case URI_ENCLOSED:
780
713k
          case URI_OR_TOKEN:
781
713k
            break;
782
2.11k
          case F_CRLF:
783
14.6k
          case F_LF:
784
17.1k
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
17.1k
            goto endofheader;
787
93
          default:
788
93
            LM_DBG("spitting out [%c] in status %d\n",
789
93
            *tmp,status );
790
93
            goto error;
791
5.50M
        }
792
11.3M
    }/*char switch*/
793
11.3M
  }/*for*/
794
795
64.5k
endofheader:
796
64.5k
  if (to_b->display.len==0) to_b->display.s=0;
797
64.5k
  status=saved_status;
798
64.5k
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
64.5k
  switch(status){
801
22.0k
    case MAYBE_URI_END:
802
22.0k
      to_b->uri.len = end_mark - to_b->uri.s;
803
30.2k
    case END:
804
30.2k
      to_b->body.len = tmp - to_b->body.s;
805
61.0k
    case E_PARA_VALUE:
806
61.0k
      break;
807
3.50k
    default:
808
3.50k
      LM_ERR("unexpected end of header in state %d\n", status);
809
3.50k
      goto error;
810
64.5k
  }
811
812
61.0k
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
61.0k
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
61.0k
  return tmp;
816
817
1.14k
parse_error:
818
1.14k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
1.14k
      tmp < end? *tmp : *(end-1), status, (int)(tmp-buffer), buffer);
820
4.74k
error:
821
4.74k
  first_b->error=PARSE_ERROR;
822
4.74k
  free_to_params(first_b);
823
4.74k
  free_to(first_b->next);
824
4.74k
  return tmp;
825
826
1.14k
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
64.5k
{
831
64.5k
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
64.5k
}
833
834
835
char* parse_multi_to(char* buffer, char *end, struct to_body *to_b)
836
1.26k
{
837
1.26k
  return _parse_to( buffer, end, to_b, 1/*multi*/);
838
1.26k
}
839
840
841
/**
842
 *
843
 */
844
struct sip_uri *parse_to_uri(struct sip_msg *msg)
845
12.5k
{
846
12.5k
  struct to_body *tb = NULL;
847
12.5k
  if(msg==NULL || msg->to==NULL || msg->to->parsed==NULL)
848
0
    return NULL;
849
850
12.5k
  tb = get_to(msg);
851
852
12.5k
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
853
5.04k
    return &tb->parsed_uri;
854
855
7.54k
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
856
5.86k
  {
857
5.86k
    LM_ERR("failed to parse To uri\n");
858
5.86k
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
859
5.86k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
860
5.86k
    set_err_reply(400, "bad To uri");
861
5.86k
    return NULL;
862
5.86k
  }
863
864
1.68k
  return &tb->parsed_uri;
865
7.54k
}
866
867
int parse_to_header( struct sip_msg *msg)
868
0
{
869
0
  struct to_body* to_b;
870
871
0
  if ( !msg->to && ( parse_headers(msg,HDR_TO_F,0)==-1 || !msg->to)) {
872
0
    LM_ERR("bad msg or missing To header\n");
873
0
    goto error;
874
0
  }
875
876
  /* maybe the header is already parsed! */
877
0
  if (msg->to->parsed)
878
0
    return 0;
879
880
  /* bad luck! :-( - we have to parse it */
881
  /* first, get some memory */
882
0
  to_b = pkg_malloc(sizeof(struct to_body));
883
0
  if (to_b == 0) {
884
0
    LM_ERR("out of pkg_memory\n");
885
0
    goto error;
886
0
  }
887
888
  /* now parse it!! */
889
0
  memset(to_b, 0, sizeof(struct to_body));
890
0
  parse_to(msg->to->body.s,msg->to->body.s+msg->to->body.len+1,to_b);
891
0
  if (to_b->error == PARSE_ERROR) {
892
0
    LM_ERR("bad to header\n");
893
0
    pkg_free(to_b);
894
0
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
895
0
      "error parsing too header");
896
0
    set_err_reply(400, "bad header");
897
0
    goto error;
898
0
  }
899
900
0
  msg->to->parsed = to_b;
901
902
0
  return 0;
903
0
error:
904
0
  return -1;
905
0
}
906
907
/*
908
 * Checks if From includes a To-tag -- good to identify
909
 * if a request creates a new dialog
910
 */
911
int has_totag(struct sip_msg* _m)
912
0
{
913
0
  str tag;
914
915
0
  if (!_m->to && parse_headers(_m, HDR_TO_F,0)==-1) {
916
0
    LM_ERR("To parsing failed\n");
917
0
    return 0;
918
0
  }
919
0
  if (!_m->to) {
920
0
    LM_ERR("no To\n");
921
0
    return 0;
922
0
  }
923
0
  tag=get_to(_m)->tag_value;
924
0
  if (tag.s==0 || tag.len==0) {
925
0
    LM_DBG("no totag\n");
926
0
    return 0;
927
0
  }
928
0
  LM_DBG("totag found\n");
929
0
  return 1;
930
0
}
931
932
/*
933
 * Parses the URI from a generic to_body structure
934
 * Helper function (not specific to TO hdr)
935
 */
936
int parse_to_body_uri(struct to_body *to_b)
937
0
{
938
0
  if (to_b==NULL)
939
0
    return -1;
940
941
0
  if (parse_uri(to_b->uri.s, to_b->uri.len, &to_b->parsed_uri) < 0) {
942
0
    memset( &to_b->parsed_uri, 0, sizeof(struct sip_uri));
943
0
    return -1;
944
0
  }
945
0
  return 0;
946
0
}