Coverage Report

Created: 2024-05-20 06:10

/src/opensips/parser/parse_to.c
Line
Count
Source (jump to first uncovered line)
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
66.2k
  do{\
56
66.2k
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
66.2k
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
66.2k
    else (_body)->last_param->next=(_param);\
60
66.2k
    (_body)->last_param =(_param);\
61
66.2k
    if ((_param)->type==TAG_PARAM)\
62
66.2k
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
66.2k
    (_param) = 0;\
64
66.2k
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
3.18M
{
70
3.18M
  struct to_param *tp=tb->param_lst;
71
3.18M
  struct to_param *foo;
72
3.25M
  while (tp){
73
66.2k
    foo = tp->next;
74
66.2k
    pkg_free(tp);
75
66.2k
    tp=foo;
76
66.2k
  }
77
78
3.18M
  tb->param_lst = tb->last_param = NULL;
79
3.18M
}
80
81
82
void free_to(struct to_body* tb)
83
3.23M
{
84
3.23M
  if (tb) {
85
3.17M
    free_to( tb->next );
86
3.17M
    free_to_params(tb);
87
3.17M
    pkg_free(tb);
88
3.17M
  }
89
3.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
35.4k
{
97
35.4k
  struct to_param *param;
98
35.4k
  int status;
99
35.4k
  int saved_status;
100
35.4k
  char  *tmp;
101
102
35.4k
  param=0;
103
35.4k
  status=E_PARA_VALUE;
104
35.4k
  saved_status=E_PARA_VALUE;
105
1.93M
  for( tmp=buffer; tmp<end; tmp++)
106
1.93M
  {
107
1.93M
    switch(*tmp)
108
1.93M
    {
109
10.2k
      case ' ':
110
18.4k
      case '\t':
111
18.4k
        switch (status)
112
18.4k
        {
113
502
          case TAG3:
114
502
            param->type=TAG_PARAM;
115
3.78k
          case PARA_NAME:
116
4.50k
          case TAG1:
117
4.97k
          case TAG2:
118
4.97k
            param->name.len = tmp-param->name.s;
119
4.97k
            status = S_EQUAL;
120
4.97k
            break;
121
2.10k
          case PARA_VALUE_TOKEN:
122
2.10k
            param->value.len = tmp-param->value.s;
123
2.10k
            status = E_PARA_VALUE;
124
2.10k
            add_param( param , to_b );
125
2.10k
            break;
126
1.06k
          case F_CRLF:
127
5.64k
          case F_LF:
128
9.58k
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
9.58k
            status=saved_status;
131
9.58k
            break;
132
18.4k
        }
133
18.4k
        break;
134
22.0k
      case '\n':
135
22.0k
        switch (status)
136
22.0k
        {
137
1.61k
          case S_PARA_NAME:
138
3.08k
          case S_EQUAL:
139
3.91k
          case S_PARA_VALUE:
140
4.82k
          case E_PARA_VALUE:
141
4.82k
            saved_status=status;
142
4.82k
            status=F_LF;
143
4.82k
            break;
144
587
          case TAG3:
145
587
            param->type=TAG_PARAM;
146
7.89k
          case PARA_NAME:
147
9.45k
          case TAG1:
148
9.99k
          case TAG2:
149
9.99k
            param->name.len = tmp-param->name.s;
150
9.99k
            saved_status = S_EQUAL;
151
9.99k
            status = F_LF;
152
9.99k
            break;
153
1.99k
          case PARA_VALUE_TOKEN:
154
1.99k
            param->value.len = tmp-param->value.s;
155
1.99k
            saved_status = E_PARA_VALUE;
156
1.99k
            status = F_LF;
157
1.99k
            add_param( param , to_b );
158
1.99k
            break;
159
4.92k
          case F_CR:
160
4.92k
            status=F_CRLF;
161
4.92k
            break;
162
2
          case F_CRLF:
163
6
          case F_LF:
164
6
            status=saved_status;
165
6
            goto endofheader;
166
270
          default:
167
270
            goto parse_error;
168
22.0k
        }
169
21.7k
        break;
170
21.7k
      case '\r':
171
17.4k
        switch (status)
172
17.4k
        {
173
1.04k
          case S_PARA_NAME:
174
1.66k
          case S_EQUAL:
175
2.15k
          case S_PARA_VALUE:
176
2.61k
          case E_PARA_VALUE:
177
2.61k
            saved_status=status;
178
2.61k
            status=F_CR;
179
2.61k
            break;
180
403
          case TAG3:
181
403
            param->type=TAG_PARAM;
182
11.7k
          case PARA_NAME:
183
12.7k
          case TAG1:
184
13.1k
          case TAG2:
185
13.1k
            param->name.len = tmp-param->name.s;
186
13.1k
            saved_status = S_EQUAL;
187
13.1k
            status = F_CR;
188
13.1k
            break;
189
1.71k
          case PARA_VALUE_TOKEN:
190
1.71k
            param->value.len = tmp-param->value.s;
191
1.71k
            saved_status = E_PARA_VALUE;
192
1.71k
            status = F_CR;
193
1.71k
            add_param( param , to_b );
194
1.71k
            break;
195
2
          case F_CRLF:
196
22
          case F_CR:
197
24
          case F_LF:
198
24
            status=saved_status;
199
24
            goto endofheader;
200
20
          default:
201
20
            goto parse_error;
202
17.4k
        }
203
17.4k
        break;
204
17.4k
      case  0:
205
9.32k
      case ',':
206
9.32k
        switch (status)
207
9.32k
        {
208
940
          case PARA_VALUE_QUOTED:
209
940
            break;
210
4.51k
          case PARA_NAME:
211
4.51k
            param->name.len = tmp-param->name.s;
212
5.13k
          case S_EQUAL:
213
5.82k
          case S_PARA_VALUE:
214
5.82k
            if (param->type==TAG_PARAM)
215
19
              goto parse_error;
216
5.80k
            param->value.s = tmp;
217
7.49k
          case PARA_VALUE_TOKEN:
218
7.49k
            status = E_PARA_VALUE;
219
7.49k
            param->value.len = tmp-param->value.s;
220
7.49k
            add_param( param , to_b );
221
8.25k
          case E_PARA_VALUE:
222
8.25k
            saved_status = status;
223
8.25k
            if ( !multi && *tmp==',')
224
261
              goto parse_error;
225
7.99k
            goto endofheader;
226
7.99k
            break;
227
7.99k
          default:
228
113
            goto parse_error;
229
9.32k
        }
230
940
        break;
231
940
      case '\\':
232
547
        switch (status)
233
547
        {
234
452
          case PARA_VALUE_QUOTED:
235
452
            if (tmp+1==end)
236
3
              goto parse_error;
237
449
            switch (*(tmp+1))
238
449
            {
239
18
              case '\r':
240
47
              case '\n':
241
47
                break;
242
402
              default:
243
402
                tmp++;
244
402
                break;
245
449
            }
246
449
            break;
247
449
          default:
248
95
            goto parse_error;
249
547
        }
250
449
        break;
251
4.94k
      case '"':
252
4.94k
        switch (status)
253
4.94k
        {
254
1.95k
          case S_PARA_VALUE:
255
1.95k
            param->value.s = tmp+1;
256
1.95k
            status = PARA_VALUE_QUOTED;
257
1.95k
            break;
258
1.50k
          case PARA_VALUE_QUOTED:
259
1.50k
            param->value.len=tmp-param->value.s ;
260
1.50k
            add_param( param , to_b );
261
1.50k
            status = E_PARA_VALUE;
262
1.50k
            break;
263
396
          case F_CRLF:
264
916
          case F_LF:
265
1.35k
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
1.35k
            goto endofheader;
268
134
          default:
269
134
            goto parse_error;
270
4.94k
        }
271
3.45k
        break;
272
71.6k
      case ';' :
273
71.6k
        switch (status)
274
71.6k
        {
275
575
          case PARA_VALUE_QUOTED:
276
575
            break;
277
17.6k
          case PARA_NAME:
278
17.6k
            param->name.len = tmp-param->name.s;
279
21.6k
          case S_EQUAL:
280
25.1k
          case S_PARA_VALUE:
281
25.1k
            if (param->type==TAG_PARAM)
282
18
              goto parse_error;
283
25.1k
            param->value.s = tmp;
284
30.2k
          case PARA_VALUE_TOKEN:
285
30.2k
            param->value.len=tmp-param->value.s;
286
30.2k
            add_param(param,to_b);
287
69.6k
          case E_PARA_VALUE:
288
69.6k
            param = (struct to_param*)
289
69.6k
              pkg_malloc(sizeof(struct to_param));
290
69.6k
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
69.6k
            memset(param,0,sizeof(struct to_param));
295
69.6k
            param->type=GENERAL_PARAM;
296
69.6k
            status = S_PARA_NAME;
297
69.6k
            break;
298
409
          case F_CRLF:
299
908
          case F_LF:
300
1.30k
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
1.30k
            goto endofheader;
303
87
          default:
304
87
            goto parse_error;
305
71.6k
        }
306
70.1k
        break;
307
70.1k
      case 'T':
308
38.4k
      case 't' :
309
38.4k
        switch (status)
310
38.4k
        {
311
487
          case PARA_VALUE_QUOTED:
312
1.27k
          case PARA_VALUE_TOKEN:
313
7.92k
          case PARA_NAME:
314
7.92k
            break;
315
18.5k
          case S_PARA_NAME:
316
18.5k
            param->name.s = tmp;
317
18.5k
            status = TAG1;
318
18.5k
            break;
319
599
          case S_PARA_VALUE:
320
599
            param->value.s = tmp;
321
599
            status = PARA_VALUE_TOKEN;
322
599
            break;
323
506
          case TAG1:
324
1.00k
          case TAG2:
325
1.42k
          case TAG3:
326
1.42k
            status = PARA_NAME;
327
1.42k
            break;
328
749
          case F_CRLF:
329
6.35k
          case F_LF:
330
9.90k
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
9.90k
            goto endofheader;
333
93
          default:
334
93
            goto parse_error;
335
38.4k
        }
336
28.4k
        break;
337
28.4k
      case 'A':
338
30.6k
      case 'a' :
339
30.6k
        switch (status)
340
30.6k
        {
341
449
          case PARA_VALUE_QUOTED:
342
1.19k
          case PARA_VALUE_TOKEN:
343
11.9k
          case PARA_NAME:
344
11.9k
            break;
345
4.27k
          case S_PARA_NAME:
346
4.27k
            param->name.s = tmp;
347
4.27k
            status = PARA_NAME;
348
4.27k
            break;
349
1.13k
          case S_PARA_VALUE:
350
1.13k
            param->value.s = tmp;
351
1.13k
            status = PARA_VALUE_TOKEN;
352
1.13k
            break;
353
9.71k
          case TAG1:
354
9.71k
            status = TAG2;
355
9.71k
            break;
356
429
          case TAG2:
357
864
          case TAG3:
358
864
            status = PARA_NAME;
359
864
            break;
360
609
          case F_CRLF:
361
1.96k
          case F_LF:
362
2.71k
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
2.71k
            goto endofheader;
365
56
          default:
366
56
            goto parse_error;
367
30.6k
        }
368
27.9k
        break;
369
27.9k
      case 'G':
370
19.5k
      case 'g' :
371
19.5k
        switch (status)
372
19.5k
        {
373
514
          case PARA_VALUE_QUOTED:
374
1.20k
          case PARA_VALUE_TOKEN:
375
4.51k
          case PARA_NAME:
376
4.51k
            break;
377
5.51k
          case S_PARA_NAME:
378
5.51k
            param->name.s = tmp;
379
5.51k
            status = PARA_NAME;
380
5.51k
            break;
381
757
          case S_PARA_VALUE:
382
757
            param->value.s = tmp;
383
757
            status = PARA_VALUE_TOKEN;
384
757
            break;
385
769
          case TAG1:
386
1.19k
          case TAG3:
387
1.19k
            status = PARA_NAME;
388
1.19k
            break;
389
6.09k
          case TAG2:
390
6.09k
            status = TAG3;
391
6.09k
            break;
392
530
          case F_CRLF:
393
1.03k
          case F_LF:
394
1.43k
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
1.43k
            goto endofheader;
397
42
          default:
398
42
            goto parse_error;
399
19.5k
        }
400
18.0k
        break;
401
21.8k
      case '=':
402
21.8k
        switch (status)
403
21.8k
        {
404
564
          case PARA_VALUE_QUOTED:
405
564
            break;
406
2.81k
          case TAG3:
407
2.81k
            param->type=TAG_PARAM;
408
14.3k
          case PARA_NAME:
409
16.9k
          case TAG1:
410
17.4k
          case TAG2:
411
17.4k
            param->name.len = tmp-param->name.s;
412
17.4k
            status = S_PARA_VALUE;
413
17.4k
            break;
414
2.11k
          case S_EQUAL:
415
2.11k
            status = S_PARA_VALUE;
416
2.11k
            break;
417
519
          case F_CRLF:
418
965
          case F_LF:
419
1.54k
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
1.54k
            goto endofheader;
422
172
          default:
423
172
            goto parse_error;
424
21.8k
        }
425
20.1k
        break;
426
1.67M
      default:
427
1.67M
        switch (status)
428
1.67M
        {
429
1.71k
          case TAG1:
430
2.44k
          case TAG2:
431
2.95k
          case TAG3:
432
2.95k
            status = PARA_NAME;
433
2.95k
            break;
434
20.1k
          case PARA_VALUE_TOKEN:
435
1.61M
          case PARA_NAME:
436
1.61M
          case PARA_VALUE_QUOTED:
437
1.61M
            break;
438
39.7k
          case S_PARA_NAME:
439
39.7k
            param->name.s = tmp;
440
39.7k
            status = PARA_NAME;
441
39.7k
            break;
442
10.2k
          case S_PARA_VALUE:
443
10.2k
            param->value.s = tmp;
444
10.2k
            status = PARA_VALUE_TOKEN;
445
10.2k
            break;
446
634
          case F_CRLF:
447
2.82k
          case F_LF:
448
5.02k
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
5.02k
            goto endofheader;
451
740
          default:
452
740
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
740
            goto error;
454
1.67M
        }
455
1.93M
    }/*switch*/
456
1.93M
  }/*for*/
457
458
2.05k
  if (status==PARA_VALUE_QUOTED) {
459
150
      LM_ERR("unexpected end of header in state %d\n", status);
460
150
      goto parse_error;
461
150
  }
462
463
33.2k
endofheader:
464
33.2k
  LM_DBG("end of header reached, state=%d\n", status);
465
33.2k
  if (param) {
466
23.0k
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
21.3k
      saved_status = E_PARA_VALUE;
468
21.3k
      param->value.s= 0;
469
21.3k
      param->value.len=0;
470
21.3k
      if (param->type==TAG_PARAM)
471
147
        goto parse_error;
472
21.1k
      add_param(param, to_b);
473
21.1k
    } else {
474
1.68k
      pkg_free(param);
475
1.68k
    }
476
23.0k
  }
477
33.0k
  *returned_status=saved_status;
478
33.0k
  return tmp;
479
480
1.68k
parse_error:
481
1.68k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
1.68k
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
2.42k
error:
484
2.42k
  if (param) pkg_free(param);
485
2.42k
  free_to_params(to_b);
486
2.42k
  to_b->error=PARSE_ERROR;
487
2.42k
  *returned_status = status;
488
2.42k
  return tmp;
489
1.68k
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
60.1k
{
497
60.1k
  int status;
498
60.1k
  int saved_status;
499
60.1k
  char  *tmp;
500
60.1k
  char  *end_mark;
501
60.1k
  struct to_body *first_b = to_b;
502
503
60.1k
  status=START_TO;
504
60.1k
  saved_status=START_TO;
505
60.1k
  memset(to_b, 0, sizeof(struct to_body));
506
60.1k
  to_b->error=PARSE_OK;
507
60.1k
  end_mark=0;
508
509
7.92M
  for( tmp=buffer; tmp<end; tmp++)
510
7.92M
  {
511
7.92M
    switch(*tmp)
512
7.92M
    {
513
1.29M
      case ' ':
514
1.30M
      case '\t':
515
1.30M
        switch (status)
516
1.30M
        {
517
485
          case F_CRLF:
518
2.72k
          case F_LF:
519
4.90k
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
4.90k
            status=saved_status;
522
4.90k
            break;
523
822
          case URI_ENCLOSED:
524
822
            to_b->uri.len = tmp - to_b->uri.s;
525
822
            status = E_URI_ENCLOSED;
526
822
            break;
527
4.77k
          case URI_OR_TOKEN:
528
4.77k
            status = MAYBE_URI_END;
529
4.77k
            end_mark = tmp;
530
4.77k
            break;
531
1.26k
          case DISPLAY_TOKEN:
532
1.26k
            end_mark = tmp;
533
1.26k
            status = DISPLAY_TOKEN2;
534
1.26k
            break;
535
1.30M
        }
536
1.30M
        break;
537
1.30M
      case '\n':
538
20.8k
        switch (status)
539
20.8k
        {
540
8.45k
          case URI_OR_TOKEN:
541
8.45k
            end_mark = tmp;
542
8.45k
            status = MAYBE_URI_END;
543
9.39k
          case MAYBE_URI_END:
544
10.0k
          case DISPLAY_TOKEN:
545
10.5k
          case DISPLAY_TOKEN2:
546
10.9k
          case E_DISPLAY_QUOTED:
547
16.5k
          case END:
548
16.5k
            saved_status=status;
549
16.5k
            status=F_LF;
550
16.5k
            break;
551
3.58k
          case F_CR:
552
3.58k
            status=F_CRLF;
553
3.58k
            break;
554
2
          case F_CRLF:
555
6
          case F_LF:
556
6
            status=saved_status;
557
6
            goto endofheader;
558
762
          default:
559
762
            goto parse_error;
560
20.8k
        }
561
20.0k
        break;
562
20.0k
      case '\r':
563
11.0k
        switch (status)
564
11.0k
        {
565
8.11k
          case URI_OR_TOKEN:
566
8.11k
            end_mark = tmp;
567
8.11k
            status = MAYBE_URI_END;
568
            /* fall through */
569
9.12k
          case MAYBE_URI_END:
570
9.60k
          case DISPLAY_TOKEN:
571
10.0k
          case DISPLAY_TOKEN2:
572
10.4k
          case E_DISPLAY_QUOTED:
573
11.0k
          case END:
574
11.0k
            saved_status=status;
575
11.0k
            status=F_CR;
576
11.0k
            break;
577
4
          case F_CRLF:
578
25
          case F_CR:
579
27
          case F_LF:
580
27
            status=saved_status;
581
27
            goto endofheader;
582
22
          default:
583
22
            goto parse_error;
584
11.0k
        }
585
11.0k
        break;
586
11.0k
      case 0:
587
2.81k
        switch (status)
588
2.81k
        {
589
1.75k
          case URI_OR_TOKEN:
590
2.20k
          case MAYBE_URI_END:
591
2.20k
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
2.75k
          case END:
594
2.75k
            saved_status = status = END;
595
2.75k
            goto endofheader;
596
60
          default:
597
60
            goto parse_error;
598
2.81k
        }
599
0
        break;
600
3.12M
      case ',':
601
3.12M
        switch (status)
602
3.12M
        {
603
402
          case DISPLAY_QUOTED:
604
1.98k
          case URI_ENCLOSED:
605
1.98k
            break;
606
3.11M
          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
3.11M
            if (multi==0)
616
424
              break;
617
3.11M
          case MAYBE_URI_END:
618
3.11M
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
3.11M
          case END:
621
3.11M
            if (multi==0)
622
22
              goto parse_error;
623
3.11M
            to_b->next = (struct to_body*)
624
3.11M
              pkg_malloc(sizeof(struct to_body));
625
3.11M
            if (to_b->next==NULL) {
626
0
              LM_ERR("failed to allocate new TO body\n");
627
0
              goto error;
628
0
            }
629
3.11M
            to_b = to_b->next;
630
3.11M
            memset(to_b, 0, sizeof(struct to_body));
631
3.11M
            to_b->error = PARSE_OK;
632
3.11M
            saved_status = status = START_TO;
633
3.11M
            end_mark=0;
634
3.11M
            break;
635
60
          default:
636
60
            goto parse_error;
637
3.12M
        }
638
3.12M
        break;
639
3.12M
      case '\\':
640
459
        switch (status)
641
459
        {
642
403
          case DISPLAY_QUOTED:
643
403
            tmp++; /* jump over next char */
644
403
            break;
645
56
          default:
646
56
            goto parse_error;
647
459
        }
648
403
        break;
649
12.2k
      case '<':
650
12.2k
        switch (status)
651
12.2k
        {
652
5.25k
          case START_TO:
653
5.25k
            to_b->body.s=tmp;
654
5.25k
            status = S_URI_ENCLOSED;
655
5.25k
            break;
656
406
          case DISPLAY_QUOTED:
657
406
            break;
658
440
          case E_DISPLAY_QUOTED:
659
440
            status = S_URI_ENCLOSED;
660
440
            break;
661
1.23k
          case URI_OR_TOKEN:
662
1.80k
          case DISPLAY_TOKEN:
663
1.80k
            end_mark = tmp;
664
            /* fall through */
665
2.25k
          case DISPLAY_TOKEN2:
666
2.84k
          case MAYBE_URI_END:
667
2.84k
            to_b->display.len=end_mark-to_b->display.s;
668
2.84k
            status = S_URI_ENCLOSED;
669
2.84k
            break;
670
400
          case F_CRLF:
671
952
          case F_LF:
672
3.21k
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
3.21k
            goto endofheader;
675
52
          default:
676
52
            goto parse_error;
677
12.2k
        }
678
8.94k
        break;
679
10.7k
      case '>':
680
10.7k
        switch (status)
681
10.7k
        {
682
414
          case DISPLAY_QUOTED:
683
414
            break;
684
7.38k
          case URI_ENCLOSED:
685
7.38k
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
8.18k
          case E_URI_ENCLOSED:
688
8.18k
            status = END;
689
8.18k
            break;
690
998
          case F_CRLF:
691
1.51k
          case F_LF:
692
2.06k
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
2.06k
            goto endofheader;
695
128
          default:
696
128
            goto parse_error;
697
10.7k
        }
698
8.59k
        break;
699
8.59k
      case '"':
700
3.29k
        switch (status)
701
3.29k
        {
702
795
          case START_TO:
703
795
            to_b->body.s = tmp;
704
795
            to_b->display.s = tmp;
705
795
            status = DISPLAY_QUOTED;
706
795
            break;
707
575
          case DISPLAY_QUOTED:
708
575
            status = E_DISPLAY_QUOTED;
709
575
            to_b->display.len = tmp-to_b->display.s+1;
710
575
            break;
711
703
          case F_CRLF:
712
1.47k
          case F_LF:
713
1.86k
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
1.86k
            goto endofheader;
716
62
          default:
717
62
            goto parse_error;
718
3.29k
        }
719
1.37k
        break;
720
48.0k
      case ';' :
721
48.0k
        switch (status)
722
48.0k
        {
723
414
          case DISPLAY_QUOTED:
724
841
          case DISPLAY_TOKEN:
725
9.83k
          case URI_ENCLOSED:
726
9.83k
            break;
727
33.1k
          case URI_OR_TOKEN:
728
33.1k
            end_mark = tmp;
729
            /* fall through */
730
34.7k
          case MAYBE_URI_END:
731
34.7k
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
35.4k
          case END:
734
35.4k
            to_b->body.len = tmp-to_b->body.s;
735
35.4k
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
35.4k
            if (to_b->error!=PARSE_ERROR && multi && *tmp==',') {
737
              /* continue with a new body instance */
738
2.42k
              to_b->next = (struct to_body*)
739
2.42k
                pkg_malloc(sizeof(struct to_body));
740
2.42k
              if (to_b->next==NULL) {
741
0
                LM_ERR("failed to allocate new TO body\n");
742
0
                goto error;
743
0
              }
744
2.42k
              to_b = to_b->next;
745
2.42k
              memset(to_b, 0, sizeof(struct to_body));
746
2.42k
              to_b->error=PARSE_OK;
747
2.42k
              saved_status = status = START_TO;
748
2.42k
              end_mark=0;
749
2.42k
              break;
750
33.0k
            } else {
751
33.0k
              goto endofheader;
752
33.0k
            }
753
415
          case F_CRLF:
754
2.25k
          case F_LF:
755
2.64k
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
2.64k
            goto endofheader;
758
98
          default:
759
98
            goto parse_error;
760
48.0k
        }
761
12.2k
        break;
762
3.38M
      default:
763
3.38M
        switch (status)
764
3.38M
        {
765
3.17M
          case START_TO:
766
3.17M
            to_b->uri.s = to_b->body.s = tmp;
767
3.17M
            status = URI_OR_TOKEN;
768
3.17M
            to_b->display.s=tmp;
769
3.17M
            break;
770
8.38k
          case S_URI_ENCLOSED:
771
8.38k
            to_b->uri.s=tmp;
772
8.38k
            status=URI_ENCLOSED;
773
8.38k
            break;
774
1.64k
          case MAYBE_URI_END:
775
2.33k
          case DISPLAY_TOKEN2:
776
2.33k
            status = DISPLAY_TOKEN;
777
2.90k
          case DISPLAY_QUOTED:
778
6.01k
          case DISPLAY_TOKEN:
779
73.3k
          case URI_ENCLOSED:
780
193k
          case URI_OR_TOKEN:
781
193k
            break;
782
570
          case F_CRLF:
783
10.3k
          case F_LF:
784
11.8k
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
11.8k
            goto endofheader;
787
76
          default:
788
76
            LM_DBG("spitting out [%c] in status %d\n",
789
76
            *tmp,status );
790
76
            goto error;
791
3.38M
        }
792
7.92M
    }/*char switch*/
793
7.92M
  }/*for*/
794
795
58.7k
endofheader:
796
58.7k
  if (to_b->display.len==0) to_b->display.s=0;
797
58.7k
  status=saved_status;
798
58.7k
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
58.7k
  switch(status){
801
16.7k
    case MAYBE_URI_END:
802
16.7k
      to_b->uri.len = end_mark - to_b->uri.s;
803
24.7k
    case END:
804
24.7k
      to_b->body.len = tmp - to_b->body.s;
805
54.9k
    case E_PARA_VALUE:
806
54.9k
      break;
807
3.84k
    default:
808
3.84k
      LM_ERR("unexpected end of header in state %d\n", status);
809
3.84k
      goto error;
810
58.7k
  }
811
812
54.9k
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
54.9k
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
54.9k
  return tmp;
816
817
1.32k
parse_error:
818
1.32k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
1.32k
      tmp < end? *tmp : *(end-1), status, (int)(tmp-buffer), buffer);
820
5.23k
error:
821
5.23k
  first_b->error=PARSE_ERROR;
822
5.23k
  free_to_params(first_b);
823
5.23k
  free_to(first_b->next);
824
5.23k
  return tmp;
825
826
1.32k
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
58.8k
{
831
58.8k
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
58.8k
}
833
834
835
char* parse_multi_to(char* buffer, char *end, struct to_body *to_b)
836
1.35k
{
837
1.35k
  return _parse_to( buffer, end, to_b, 1/*multi*/);
838
1.35k
}
839
840
841
/**
842
 *
843
 */
844
struct sip_uri *parse_to_uri(struct sip_msg *msg)
845
12.6k
{
846
12.6k
  struct to_body *tb = NULL;
847
12.6k
  if(msg==NULL || msg->to==NULL || msg->to->parsed==NULL)
848
0
    return NULL;
849
850
12.6k
  tb = get_to(msg);
851
852
12.6k
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
853
5.34k
    return &tb->parsed_uri;
854
855
7.31k
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
856
5.53k
  {
857
5.53k
    LM_ERR("failed to parse To uri\n");
858
5.53k
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
859
5.53k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
860
5.53k
    set_err_reply(400, "bad To uri");
861
5.53k
    return NULL;
862
5.53k
  }
863
864
1.78k
  return &tb->parsed_uri;
865
7.31k
}
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
}