Coverage Report

Created: 2025-12-12 06:47

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
72.1k
  do{\
56
72.1k
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
72.1k
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
72.1k
    else (_body)->last_param->next=(_param);\
60
72.1k
    (_body)->last_param =(_param);\
61
72.1k
    if ((_param)->type==TAG_PARAM)\
62
72.1k
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
72.1k
    (_param) = 0;\
64
72.1k
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
6.16M
{
70
6.16M
  struct to_param *tp=tb->param_lst;
71
6.16M
  struct to_param *foo;
72
6.23M
  while (tp){
73
72.1k
    foo = tp->next;
74
72.1k
    pkg_free(tp);
75
72.1k
    tp=foo;
76
72.1k
  }
77
78
6.16M
  tb->param_lst = tb->last_param = NULL;
79
6.16M
}
80
81
82
void free_to(struct to_body* tb)
83
6.23M
{
84
6.23M
  if (tb) {
85
6.15M
    free_to( tb->next );
86
6.15M
    free_to_params(tb);
87
6.15M
    pkg_free(tb);
88
6.15M
  }
89
6.23M
}
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
39.9k
{
97
39.9k
  struct to_param *param;
98
39.9k
  int status;
99
39.9k
  int saved_status;
100
39.9k
  char  *tmp;
101
102
39.9k
  param=0;
103
39.9k
  status=E_PARA_VALUE;
104
39.9k
  saved_status=E_PARA_VALUE;
105
591k
  for( tmp=buffer; tmp<end; tmp++)
106
589k
  {
107
589k
    switch(*tmp)
108
589k
    {
109
15.0k
      case ' ':
110
25.2k
      case '\t':
111
25.2k
        switch (status)
112
25.2k
        {
113
536
          case TAG3:
114
536
            param->type=TAG_PARAM;
115
4.49k
          case PARA_NAME:
116
5.70k
          case TAG1:
117
6.17k
          case TAG2:
118
6.17k
            param->name.len = tmp-param->name.s;
119
6.17k
            status = S_EQUAL;
120
6.17k
            break;
121
2.58k
          case PARA_VALUE_TOKEN:
122
2.58k
            param->value.len = tmp-param->value.s;
123
2.58k
            status = E_PARA_VALUE;
124
2.58k
            add_param( param , to_b );
125
2.58k
            break;
126
590
          case F_CRLF:
127
5.72k
          case F_LF:
128
10.4k
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
10.4k
            status=saved_status;
131
10.4k
            break;
132
25.2k
        }
133
25.2k
        break;
134
28.6k
      case '\n':
135
28.6k
        switch (status)
136
28.6k
        {
137
1.02k
          case S_PARA_NAME:
138
1.68k
          case S_EQUAL:
139
2.33k
          case S_PARA_VALUE:
140
3.09k
          case E_PARA_VALUE:
141
3.09k
            saved_status=status;
142
3.09k
            status=F_LF;
143
3.09k
            break;
144
494
          case TAG3:
145
494
            param->type=TAG_PARAM;
146
8.36k
          case PARA_NAME:
147
10.0k
          case TAG1:
148
11.2k
          case TAG2:
149
11.2k
            param->name.len = tmp-param->name.s;
150
11.2k
            saved_status = S_EQUAL;
151
11.2k
            status = F_LF;
152
11.2k
            break;
153
5.05k
          case PARA_VALUE_TOKEN:
154
5.05k
            param->value.len = tmp-param->value.s;
155
5.05k
            saved_status = E_PARA_VALUE;
156
5.05k
            status = F_LF;
157
5.05k
            add_param( param , to_b );
158
5.05k
            break;
159
9.01k
          case F_CR:
160
9.01k
            status=F_CRLF;
161
9.01k
            break;
162
2
          case F_CRLF:
163
6
          case F_LF:
164
6
            status=saved_status;
165
6
            goto endofheader;
166
244
          default:
167
244
            goto parse_error;
168
28.6k
        }
169
28.4k
        break;
170
28.4k
      case '\r':
171
20.4k
        switch (status)
172
20.4k
        {
173
1.26k
          case S_PARA_NAME:
174
2.01k
          case S_EQUAL:
175
2.68k
          case S_PARA_VALUE:
176
3.16k
          case E_PARA_VALUE:
177
3.16k
            saved_status=status;
178
3.16k
            status=F_CR;
179
3.16k
            break;
180
402
          case TAG3:
181
402
            param->type=TAG_PARAM;
182
12.5k
          case PARA_NAME:
183
14.0k
          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.59k
          case PARA_VALUE_TOKEN:
190
2.59k
            param->value.len = tmp-param->value.s;
191
2.59k
            saved_status = E_PARA_VALUE;
192
2.59k
            status = F_CR;
193
2.59k
            add_param( param , to_b );
194
2.59k
            break;
195
2
          case F_CRLF:
196
23
          case F_CR:
197
26
          case F_LF:
198
26
            status=saved_status;
199
26
            goto endofheader;
200
21
          default:
201
21
            goto parse_error;
202
20.4k
        }
203
20.3k
        break;
204
20.3k
      case  0:
205
9.19k
      case ',':
206
9.19k
        switch (status)
207
9.19k
        {
208
941
          case PARA_VALUE_QUOTED:
209
941
            break;
210
3.80k
          case PARA_NAME:
211
3.80k
            param->name.len = tmp-param->name.s;
212
4.75k
          case S_EQUAL:
213
5.58k
          case S_PARA_VALUE:
214
5.58k
            if (param->type==TAG_PARAM)
215
20
              goto parse_error;
216
5.56k
            param->value.s = tmp;
217
7.26k
          case PARA_VALUE_TOKEN:
218
7.26k
            status = E_PARA_VALUE;
219
7.26k
            param->value.len = tmp-param->value.s;
220
7.26k
            add_param( param , to_b );
221
8.02k
          case E_PARA_VALUE:
222
8.02k
            saved_status = status;
223
8.02k
            if ( !multi && *tmp==',')
224
226
              goto parse_error;
225
7.80k
            goto endofheader;
226
7.80k
            break;
227
7.80k
          default:
228
206
            goto parse_error;
229
9.19k
        }
230
941
        break;
231
941
      case '\\':
232
618
        switch (status)
233
618
        {
234
451
          case PARA_VALUE_QUOTED:
235
451
            if (tmp+1==end)
236
4
              goto parse_error;
237
447
            switch (*(tmp+1))
238
447
            {
239
18
              case '\r':
240
45
              case '\n':
241
45
                break;
242
402
              default:
243
402
                tmp++;
244
402
                break;
245
447
            }
246
447
            break;
247
447
          default:
248
167
            goto parse_error;
249
618
        }
250
447
        break;
251
8.20k
      case '"':
252
8.20k
        switch (status)
253
8.20k
        {
254
3.12k
          case S_PARA_VALUE:
255
3.12k
            param->value.s = tmp+1;
256
3.12k
            status = PARA_VALUE_QUOTED;
257
3.12k
            break;
258
2.69k
          case PARA_VALUE_QUOTED:
259
2.69k
            param->value.len=tmp-param->value.s ;
260
2.69k
            add_param( param , to_b );
261
2.69k
            status = E_PARA_VALUE;
262
2.69k
            break;
263
439
          case F_CRLF:
264
1.82k
          case F_LF:
265
2.22k
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
2.22k
            goto endofheader;
268
167
          default:
269
167
            goto parse_error;
270
8.20k
        }
271
5.81k
        break;
272
77.7k
      case ';' :
273
77.7k
        switch (status)
274
77.7k
        {
275
464
          case PARA_VALUE_QUOTED:
276
464
            break;
277
16.6k
          case PARA_NAME:
278
16.6k
            param->name.len = tmp-param->name.s;
279
22.9k
          case S_EQUAL:
280
24.6k
          case S_PARA_VALUE:
281
24.6k
            if (param->type==TAG_PARAM)
282
18
              goto parse_error;
283
24.6k
            param->value.s = tmp;
284
29.4k
          case PARA_VALUE_TOKEN:
285
29.4k
            param->value.len=tmp-param->value.s;
286
29.4k
            add_param(param,to_b);
287
75.3k
          case E_PARA_VALUE:
288
75.3k
            param = (struct to_param*)
289
75.3k
              pkg_malloc(sizeof(struct to_param));
290
75.3k
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
75.3k
            memset(param,0,sizeof(struct to_param));
295
75.3k
            param->type=GENERAL_PARAM;
296
75.3k
            status = S_PARA_NAME;
297
75.3k
            break;
298
404
          case F_CRLF:
299
938
          case F_LF:
300
1.88k
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
1.88k
            goto endofheader;
303
58
          default:
304
58
            goto parse_error;
305
77.7k
        }
306
75.8k
        break;
307
75.8k
      case 'T':
308
46.8k
      case 't' :
309
46.8k
        switch (status)
310
46.8k
        {
311
500
          case PARA_VALUE_QUOTED:
312
2.88k
          case PARA_VALUE_TOKEN:
313
8.25k
          case PARA_NAME:
314
8.25k
            break;
315
28.5k
          case S_PARA_NAME:
316
28.5k
            param->name.s = tmp;
317
28.5k
            status = TAG1;
318
28.5k
            break;
319
1.22k
          case S_PARA_VALUE:
320
1.22k
            param->value.s = tmp;
321
1.22k
            status = PARA_VALUE_TOKEN;
322
1.22k
            break;
323
711
          case TAG1:
324
1.28k
          case TAG2:
325
1.70k
          case TAG3:
326
1.70k
            status = PARA_NAME;
327
1.70k
            break;
328
614
          case F_CRLF:
329
4.99k
          case F_LF:
330
7.07k
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
7.07k
            goto endofheader;
333
72
          default:
334
72
            goto parse_error;
335
46.8k
        }
336
39.7k
        break;
337
39.7k
      case 'A':
338
106k
      case 'a' :
339
106k
        switch (status)
340
106k
        {
341
37.9k
          case PARA_VALUE_QUOTED:
342
43.4k
          case PARA_VALUE_TOKEN:
343
78.7k
          case PARA_NAME:
344
78.7k
            break;
345
3.66k
          case S_PARA_NAME:
346
3.66k
            param->name.s = tmp;
347
3.66k
            status = PARA_NAME;
348
3.66k
            break;
349
755
          case S_PARA_VALUE:
350
755
            param->value.s = tmp;
351
755
            status = PARA_VALUE_TOKEN;
352
755
            break;
353
16.3k
          case TAG1:
354
16.3k
            status = TAG2;
355
16.3k
            break;
356
1.75k
          case TAG2:
357
2.51k
          case TAG3:
358
2.51k
            status = PARA_NAME;
359
2.51k
            break;
360
3.43k
          case F_CRLF:
361
3.98k
          case F_LF:
362
4.39k
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
4.39k
            goto endofheader;
365
22
          default:
366
22
            goto parse_error;
367
106k
        }
368
102k
        break;
369
102k
      case 'G':
370
33.0k
      case 'g' :
371
33.0k
        switch (status)
372
33.0k
        {
373
458
          case PARA_VALUE_QUOTED:
374
2.53k
          case PARA_VALUE_TOKEN:
375
7.47k
          case PARA_NAME:
376
7.47k
            break;
377
5.79k
          case S_PARA_NAME:
378
5.79k
            param->name.s = tmp;
379
5.79k
            status = PARA_NAME;
380
5.79k
            break;
381
3.27k
          case S_PARA_VALUE:
382
3.27k
            param->value.s = tmp;
383
3.27k
            status = PARA_VALUE_TOKEN;
384
3.27k
            break;
385
1.12k
          case TAG1:
386
2.34k
          case TAG3:
387
2.34k
            status = PARA_NAME;
388
2.34k
            break;
389
10.4k
          case TAG2:
390
10.4k
            status = TAG3;
391
10.4k
            break;
392
505
          case F_CRLF:
393
3.26k
          case F_LF:
394
3.66k
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
3.66k
            goto endofheader;
397
84
          default:
398
84
            goto parse_error;
399
33.0k
        }
400
29.2k
        break;
401
29.2k
      case '=':
402
25.8k
        switch (status)
403
25.8k
        {
404
442
          case PARA_VALUE_QUOTED:
405
442
            break;
406
6.01k
          case TAG3:
407
6.01k
            param->type=TAG_PARAM;
408
18.3k
          case PARA_NAME:
409
20.5k
          case TAG1:
410
21.1k
          case TAG2:
411
21.1k
            param->name.len = tmp-param->name.s;
412
21.1k
            status = S_PARA_VALUE;
413
21.1k
            break;
414
2.05k
          case S_EQUAL:
415
2.05k
            status = S_PARA_VALUE;
416
2.05k
            break;
417
546
          case F_CRLF:
418
1.56k
          case F_LF:
419
2.00k
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
2.00k
            goto endofheader;
422
178
          default:
423
178
            goto parse_error;
424
25.8k
        }
425
23.6k
        break;
426
207k
      default:
427
207k
        switch (status)
428
207k
        {
429
3.58k
          case TAG1:
430
4.35k
          case TAG2:
431
4.90k
          case TAG3:
432
4.90k
            status = PARA_NAME;
433
4.90k
            break;
434
44.2k
          case PARA_VALUE_TOKEN:
435
144k
          case PARA_NAME:
436
146k
          case PARA_VALUE_QUOTED:
437
146k
            break;
438
36.1k
          case S_PARA_NAME:
439
36.1k
            param->name.s = tmp;
440
36.1k
            status = PARA_NAME;
441
36.1k
            break;
442
11.7k
          case S_PARA_VALUE:
443
11.7k
            param->value.s = tmp;
444
11.7k
            status = PARA_VALUE_TOKEN;
445
11.7k
            break;
446
2.47k
          case F_CRLF:
447
5.29k
          case F_LF:
448
6.92k
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
6.92k
            goto endofheader;
451
713
          default:
452
713
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
713
            goto error;
454
207k
        }
455
589k
    }/*switch*/
456
589k
  }/*for*/
457
458
1.72k
  if (status==PARA_VALUE_QUOTED) {
459
156
      LM_ERR("unexpected end of header in state %d\n", status);
460
156
      goto parse_error;
461
156
  }
462
463
37.5k
endofheader:
464
37.5k
  LM_DBG("end of header reached, state=%d\n", status);
465
37.5k
  if (param) {
466
23.9k
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
22.5k
      saved_status = E_PARA_VALUE;
468
22.5k
      param->value.s= 0;
469
22.5k
      param->value.len=0;
470
22.5k
      if (param->type==TAG_PARAM)
471
130
        goto parse_error;
472
22.4k
      add_param(param, to_b);
473
22.4k
    } else {
474
1.40k
      pkg_free(param);
475
1.40k
    }
476
23.9k
  }
477
37.4k
  *returned_status=saved_status;
478
37.4k
  return tmp;
479
480
1.77k
parse_error:
481
1.77k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
1.77k
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
2.48k
error:
484
2.48k
  if (param) pkg_free(param);
485
2.48k
  free_to_params(to_b);
486
2.48k
  to_b->error=PARSE_ERROR;
487
2.48k
  *returned_status = status;
488
2.48k
  return tmp;
489
1.77k
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
75.1k
{
497
75.1k
  int status;
498
75.1k
  int saved_status;
499
75.1k
  char  *tmp;
500
75.1k
  char  *end_mark;
501
75.1k
  struct to_body *first_b = to_b;
502
503
75.1k
  status=START_TO;
504
75.1k
  saved_status=START_TO;
505
75.1k
  memset(to_b, 0, sizeof(struct to_body));
506
75.1k
  to_b->error=PARSE_OK;
507
75.1k
  end_mark=0;
508
509
16.4M
  for( tmp=buffer; tmp<end; tmp++)
510
16.4M
  {
511
16.4M
    switch(*tmp)
512
16.4M
    {
513
396k
      case ' ':
514
403k
      case '\t':
515
403k
        switch (status)
516
403k
        {
517
1.45k
          case F_CRLF:
518
3.98k
          case F_LF:
519
6.33k
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
6.33k
            status=saved_status;
522
6.33k
            break;
523
803
          case URI_ENCLOSED:
524
803
            to_b->uri.len = tmp - to_b->uri.s;
525
803
            status = E_URI_ENCLOSED;
526
803
            break;
527
8.57k
          case URI_OR_TOKEN:
528
8.57k
            status = MAYBE_URI_END;
529
8.57k
            end_mark = tmp;
530
8.57k
            break;
531
1.15k
          case DISPLAY_TOKEN:
532
1.15k
            end_mark = tmp;
533
1.15k
            status = DISPLAY_TOKEN2;
534
1.15k
            break;
535
403k
        }
536
403k
        break;
537
403k
      case '\n':
538
33.1k
        switch (status)
539
33.1k
        {
540
14.8k
          case URI_OR_TOKEN:
541
14.8k
            end_mark = tmp;
542
14.8k
            status = MAYBE_URI_END;
543
15.4k
          case MAYBE_URI_END:
544
16.1k
          case DISPLAY_TOKEN:
545
16.5k
          case DISPLAY_TOKEN2:
546
17.2k
          case E_DISPLAY_QUOTED:
547
22.4k
          case END:
548
22.4k
            saved_status=status;
549
22.4k
            status=F_LF;
550
22.4k
            break;
551
9.89k
          case F_CR:
552
9.89k
            status=F_CRLF;
553
9.89k
            break;
554
3
          case F_CRLF:
555
9
          case F_LF:
556
9
            status=saved_status;
557
9
            goto endofheader;
558
761
          default:
559
761
            goto parse_error;
560
33.1k
        }
561
32.3k
        break;
562
32.3k
      case '\r':
563
17.5k
        switch (status)
564
17.5k
        {
565
11.5k
          case URI_OR_TOKEN:
566
11.5k
            end_mark = tmp;
567
11.5k
            status = MAYBE_URI_END;
568
            /* fall through */
569
14.9k
          case MAYBE_URI_END:
570
15.5k
          case DISPLAY_TOKEN:
571
16.0k
          case DISPLAY_TOKEN2:
572
16.5k
          case E_DISPLAY_QUOTED:
573
17.4k
          case END:
574
17.4k
            saved_status=status;
575
17.4k
            status=F_CR;
576
17.4k
            break;
577
3
          case F_CRLF:
578
26
          case F_CR:
579
30
          case F_LF:
580
30
            status=saved_status;
581
30
            goto endofheader;
582
55
          default:
583
55
            goto parse_error;
584
17.5k
        }
585
17.4k
        break;
586
17.4k
      case 0:
587
3.16k
        switch (status)
588
3.16k
        {
589
2.14k
          case URI_OR_TOKEN:
590
2.67k
          case MAYBE_URI_END:
591
2.67k
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
3.06k
          case END:
594
3.06k
            saved_status = status = END;
595
3.06k
            goto endofheader;
596
99
          default:
597
99
            goto parse_error;
598
3.16k
        }
599
0
        break;
600
6.37M
      case ',':
601
6.37M
        switch (status)
602
6.37M
        {
603
496
          case DISPLAY_QUOTED:
604
281k
          case URI_ENCLOSED:
605
281k
            break;
606
6.08M
          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
6.08M
            if (multi==0)
616
2.63k
              break;
617
6.08M
          case MAYBE_URI_END:
618
6.08M
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
6.08M
          case END:
621
6.08M
            if (multi==0)
622
22
              goto parse_error;
623
6.08M
            to_b->next = (struct to_body*)
624
6.08M
              pkg_malloc(sizeof(struct to_body));
625
6.08M
            if (to_b->next==NULL) {
626
0
              LM_ERR("failed to allocate new TO body\n");
627
0
              goto error;
628
0
            }
629
6.08M
            to_b = to_b->next;
630
6.08M
            memset(to_b, 0, sizeof(struct to_body));
631
6.08M
            to_b->error = PARSE_OK;
632
6.08M
            saved_status = status = START_TO;
633
6.08M
            end_mark=0;
634
6.08M
            break;
635
95
          default:
636
95
            goto parse_error;
637
6.37M
        }
638
6.37M
        break;
639
6.37M
      case '\\':
640
444
        switch (status)
641
444
        {
642
404
          case DISPLAY_QUOTED:
643
404
            tmp++; /* jump over next char */
644
404
            break;
645
40
          default:
646
40
            goto parse_error;
647
444
        }
648
404
        break;
649
10.4k
      case '<':
650
10.4k
        switch (status)
651
10.4k
        {
652
5.49k
          case START_TO:
653
5.49k
            to_b->body.s=tmp;
654
5.49k
            status = S_URI_ENCLOSED;
655
5.49k
            break;
656
546
          case DISPLAY_QUOTED:
657
546
            break;
658
404
          case E_DISPLAY_QUOTED:
659
404
            status = S_URI_ENCLOSED;
660
404
            break;
661
1.10k
          case URI_OR_TOKEN:
662
1.63k
          case DISPLAY_TOKEN:
663
1.63k
            end_mark = tmp;
664
            /* fall through */
665
2.05k
          case DISPLAY_TOKEN2:
666
2.73k
          case MAYBE_URI_END:
667
2.73k
            to_b->display.len=end_mark-to_b->display.s;
668
2.73k
            status = S_URI_ENCLOSED;
669
2.73k
            break;
670
415
          case F_CRLF:
671
812
          case F_LF:
672
1.21k
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
1.21k
            goto endofheader;
675
44
          default:
676
44
            goto parse_error;
677
10.4k
        }
678
9.17k
        break;
679
11.9k
      case '>':
680
11.9k
        switch (status)
681
11.9k
        {
682
459
          case DISPLAY_QUOTED:
683
459
            break;
684
7.37k
          case URI_ENCLOSED:
685
7.37k
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
8.14k
          case E_URI_ENCLOSED:
688
8.14k
            status = END;
689
8.14k
            break;
690
2.05k
          case F_CRLF:
691
2.47k
          case F_LF:
692
3.30k
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
3.30k
            goto endofheader;
695
90
          default:
696
90
            goto parse_error;
697
11.9k
        }
698
8.59k
        break;
699
8.59k
      case '"':
700
3.13k
        switch (status)
701
3.13k
        {
702
739
          case START_TO:
703
739
            to_b->body.s = tmp;
704
739
            to_b->display.s = tmp;
705
739
            status = DISPLAY_QUOTED;
706
739
            break;
707
530
          case DISPLAY_QUOTED:
708
530
            status = E_DISPLAY_QUOTED;
709
530
            to_b->display.len = tmp-to_b->display.s+1;
710
530
            break;
711
883
          case F_CRLF:
712
1.40k
          case F_LF:
713
1.80k
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
1.80k
            goto endofheader;
716
56
          default:
717
56
            goto parse_error;
718
3.13k
        }
719
1.26k
        break;
720
59.4k
      case ';' :
721
59.4k
        switch (status)
722
59.4k
        {
723
612
          case DISPLAY_QUOTED:
724
1.13k
          case DISPLAY_TOKEN:
725
17.2k
          case URI_ENCLOSED:
726
17.2k
            break;
727
35.4k
          case URI_OR_TOKEN:
728
35.4k
            end_mark = tmp;
729
            /* fall through */
730
38.2k
          case MAYBE_URI_END:
731
38.2k
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
39.9k
          case END:
734
39.9k
            to_b->body.len = tmp-to_b->body.s;
735
39.9k
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
39.9k
            if (to_b->error!=PARSE_ERROR && multi && *tmp==',') {
737
              /* continue with a new body instance */
738
3.08k
              to_b->next = (struct to_body*)
739
3.08k
                pkg_malloc(sizeof(struct to_body));
740
3.08k
              if (to_b->next==NULL) {
741
0
                LM_ERR("failed to allocate new TO body\n");
742
0
                goto error;
743
0
              }
744
3.08k
              to_b = to_b->next;
745
3.08k
              memset(to_b, 0, sizeof(struct to_body));
746
3.08k
              to_b->error=PARSE_OK;
747
3.08k
              saved_status = status = START_TO;
748
3.08k
              end_mark=0;
749
3.08k
              break;
750
36.8k
            } else {
751
36.8k
              goto endofheader;
752
36.8k
            }
753
404
          case F_CRLF:
754
817
          case F_LF:
755
2.24k
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
2.24k
            goto endofheader;
758
43
          default:
759
43
            goto parse_error;
760
59.4k
        }
761
20.3k
        break;
762
9.53M
      default:
763
9.53M
        switch (status)
764
9.53M
        {
765
6.15M
          case START_TO:
766
6.15M
            to_b->uri.s = to_b->body.s = tmp;
767
6.15M
            status = URI_OR_TOKEN;
768
6.15M
            to_b->display.s=tmp;
769
6.15M
            break;
770
8.42k
          case S_URI_ENCLOSED:
771
8.42k
            to_b->uri.s=tmp;
772
8.42k
            status=URI_ENCLOSED;
773
8.42k
            break;
774
1.55k
          case MAYBE_URI_END:
775
2.16k
          case DISPLAY_TOKEN2:
776
2.16k
            status = DISPLAY_TOKEN;
777
71.2k
          case DISPLAY_QUOTED:
778
2.66M
          case DISPLAY_TOKEN:
779
3.04M
          case URI_ENCLOSED:
780
3.34M
          case URI_OR_TOKEN:
781
3.34M
            break;
782
4.67k
          case F_CRLF:
783
22.1k
          case F_LF:
784
24.0k
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
24.0k
            goto endofheader;
787
109
          default:
788
109
            LM_DBG("spitting out [%c] in status %d\n",
789
109
            *tmp,status );
790
109
            goto error;
791
9.53M
        }
792
16.4M
    }/*char switch*/
793
16.4M
  }/*for*/
794
795
73.7k
endofheader:
796
73.7k
  if (to_b->display.len==0) to_b->display.s=0;
797
73.7k
  status=saved_status;
798
73.7k
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
73.7k
  switch(status){
801
27.6k
    case MAYBE_URI_END:
802
27.6k
      to_b->uri.len = end_mark - to_b->uri.s;
803
36.0k
    case END:
804
36.0k
      to_b->body.len = tmp - to_b->body.s;
805
70.0k
    case E_PARA_VALUE:
806
70.0k
      break;
807
3.72k
    default:
808
3.72k
      LM_ERR("unexpected end of header in state %d\n", status);
809
3.72k
      goto error;
810
73.7k
  }
811
812
70.0k
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
70.0k
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
70.0k
  return tmp;
816
817
1.30k
parse_error:
818
1.30k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
1.30k
      tmp < end? *tmp : *(end-1), status, (int)(tmp-buffer), buffer);
820
5.13k
error:
821
5.13k
  first_b->error=PARSE_ERROR;
822
5.13k
  free_to_params(first_b);
823
5.13k
  free_to(first_b->next);
824
5.13k
  return tmp;
825
826
1.30k
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
73.9k
{
831
73.9k
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
73.9k
}
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.8k
{
846
12.8k
  struct to_body *tb = NULL;
847
12.8k
  if(msg==NULL || msg->to==NULL || msg->to->parsed==NULL)
848
0
    return NULL;
849
850
12.8k
  tb = get_to(msg);
851
852
12.8k
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
853
5.35k
    return &tb->parsed_uri;
854
855
7.49k
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
856
5.70k
  {
857
5.70k
    LM_ERR("failed to parse To uri\n");
858
5.70k
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
859
5.70k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
860
5.70k
    set_err_reply(400, "bad To uri");
861
5.70k
    return NULL;
862
5.70k
  }
863
864
1.78k
  return &tb->parsed_uri;
865
7.49k
}
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
}