Coverage Report

Created: 2024-02-11 06:21

/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
129k
  do{\
56
129k
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
129k
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
129k
    else (_body)->last_param->next=(_param);\
60
129k
    (_body)->last_param =(_param);\
61
129k
    if ((_param)->type==TAG_PARAM)\
62
129k
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
129k
    (_param) = 0;\
64
129k
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
6.28M
{
70
6.28M
  struct to_param *tp=tb->param_lst;
71
6.28M
  struct to_param *foo;
72
6.41M
  while (tp){
73
129k
    foo = tp->next;
74
129k
    pkg_free(tp);
75
129k
    tp=foo;
76
129k
  }
77
78
6.28M
  tb->param_lst = tb->last_param = NULL;
79
6.28M
}
80
81
82
void free_to(struct to_body* tb)
83
6.39M
{
84
6.39M
  if (tb) {
85
6.27M
    free_to( tb->next );
86
6.27M
    free_to_params(tb);
87
6.27M
    pkg_free(tb);
88
6.27M
  }
89
6.39M
}
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
83.1k
{
97
83.1k
  struct to_param *param;
98
83.1k
  int status;
99
83.1k
  int saved_status;
100
83.1k
  char  *tmp;
101
102
83.1k
  param=0;
103
83.1k
  status=E_PARA_VALUE;
104
83.1k
  saved_status=E_PARA_VALUE;
105
3.11M
  for( tmp=buffer; tmp<end; tmp++)
106
3.11M
  {
107
3.11M
    switch(*tmp)
108
3.11M
    {
109
14.2k
      case ' ':
110
30.8k
      case '\t':
111
30.8k
        switch (status)
112
30.8k
        {
113
799
          case TAG3:
114
799
            param->type=TAG_PARAM;
115
5.42k
          case PARA_NAME:
116
6.36k
          case TAG1:
117
6.81k
          case TAG2:
118
6.81k
            param->name.len = tmp-param->name.s;
119
6.81k
            status = S_EQUAL;
120
6.81k
            break;
121
3.61k
          case PARA_VALUE_TOKEN:
122
3.61k
            param->value.len = tmp-param->value.s;
123
3.61k
            status = E_PARA_VALUE;
124
3.61k
            add_param( param , to_b );
125
3.61k
            break;
126
1.12k
          case F_CRLF:
127
6.53k
          case F_LF:
128
15.8k
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
15.8k
            status=saved_status;
131
15.8k
            break;
132
30.8k
        }
133
30.8k
        break;
134
61.9k
      case '\n':
135
61.9k
        switch (status)
136
61.9k
        {
137
1.68k
          case S_PARA_NAME:
138
3.16k
          case S_EQUAL:
139
4.37k
          case S_PARA_VALUE:
140
7.24k
          case E_PARA_VALUE:
141
7.24k
            saved_status=status;
142
7.24k
            status=F_LF;
143
7.24k
            break;
144
503
          case TAG3:
145
503
            param->type=TAG_PARAM;
146
16.0k
          case PARA_NAME:
147
19.4k
          case TAG1:
148
20.2k
          case TAG2:
149
20.2k
            param->name.len = tmp-param->name.s;
150
20.2k
            saved_status = S_EQUAL;
151
20.2k
            status = F_LF;
152
20.2k
            break;
153
2.25k
          case PARA_VALUE_TOKEN:
154
2.25k
            param->value.len = tmp-param->value.s;
155
2.25k
            saved_status = E_PARA_VALUE;
156
2.25k
            status = F_LF;
157
2.25k
            add_param( param , to_b );
158
2.25k
            break;
159
31.8k
          case F_CR:
160
31.8k
            status=F_CRLF;
161
31.8k
            break;
162
2
          case F_CRLF:
163
7
          case F_LF:
164
7
            status=saved_status;
165
7
            goto endofheader;
166
272
          default:
167
272
            goto parse_error;
168
61.9k
        }
169
61.6k
        break;
170
61.6k
      case '\r':
171
51.4k
        switch (status)
172
51.4k
        {
173
1.13k
          case S_PARA_NAME:
174
2.49k
          case S_EQUAL:
175
3.17k
          case S_PARA_VALUE:
176
6.47k
          case E_PARA_VALUE:
177
6.47k
            saved_status=status;
178
6.47k
            status=F_CR;
179
6.47k
            break;
180
281
          case TAG3:
181
281
            param->type=TAG_PARAM;
182
37.8k
          case PARA_NAME:
183
38.5k
          case TAG1:
184
39.0k
          case TAG2:
185
39.0k
            param->name.len = tmp-param->name.s;
186
39.0k
            saved_status = S_EQUAL;
187
39.0k
            status = F_CR;
188
39.0k
            break;
189
5.88k
          case PARA_VALUE_TOKEN:
190
5.88k
            param->value.len = tmp-param->value.s;
191
5.88k
            saved_status = E_PARA_VALUE;
192
5.88k
            status = F_CR;
193
5.88k
            add_param( param , to_b );
194
5.88k
            break;
195
2
          case F_CRLF:
196
25
          case F_CR:
197
29
          case F_LF:
198
29
            status=saved_status;
199
29
            goto endofheader;
200
36
          default:
201
36
            goto parse_error;
202
51.4k
        }
203
51.3k
        break;
204
51.3k
      case  0:
205
16.7k
      case ',':
206
16.7k
        switch (status)
207
16.7k
        {
208
1.19k
          case PARA_VALUE_QUOTED:
209
1.19k
            break;
210
6.21k
          case PARA_NAME:
211
6.21k
            param->name.len = tmp-param->name.s;
212
6.96k
          case S_EQUAL:
213
8.32k
          case S_PARA_VALUE:
214
8.32k
            if (param->type==TAG_PARAM)
215
27
              goto parse_error;
216
8.29k
            param->value.s = tmp;
217
13.1k
          case PARA_VALUE_TOKEN:
218
13.1k
            status = E_PARA_VALUE;
219
13.1k
            param->value.len = tmp-param->value.s;
220
13.1k
            add_param( param , to_b );
221
15.3k
          case E_PARA_VALUE:
222
15.3k
            saved_status = status;
223
15.3k
            if ( !multi && *tmp==',')
224
218
              goto parse_error;
225
15.1k
            goto endofheader;
226
15.1k
            break;
227
15.1k
          default:
228
109
            goto parse_error;
229
16.7k
        }
230
1.19k
        break;
231
1.19k
      case '\\':
232
517
        switch (status)
233
517
        {
234
464
          case PARA_VALUE_QUOTED:
235
464
            if (tmp+1==end)
236
6
              goto parse_error;
237
458
            switch (*(tmp+1))
238
458
            {
239
19
              case '\r':
240
47
              case '\n':
241
47
                break;
242
411
              default:
243
411
                tmp++;
244
411
                break;
245
458
            }
246
458
            break;
247
458
          default:
248
53
            goto parse_error;
249
517
        }
250
458
        break;
251
8.52k
      case '"':
252
8.52k
        switch (status)
253
8.52k
        {
254
3.44k
          case S_PARA_VALUE:
255
3.44k
            param->value.s = tmp+1;
256
3.44k
            status = PARA_VALUE_QUOTED;
257
3.44k
            break;
258
3.00k
          case PARA_VALUE_QUOTED:
259
3.00k
            param->value.len=tmp-param->value.s ;
260
3.00k
            add_param( param , to_b );
261
3.00k
            status = E_PARA_VALUE;
262
3.00k
            break;
263
462
          case F_CRLF:
264
1.50k
          case F_LF:
265
1.98k
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
1.98k
            goto endofheader;
268
96
          default:
269
96
            goto parse_error;
270
8.52k
        }
271
6.44k
        break;
272
136k
      case ';' :
273
136k
        switch (status)
274
136k
        {
275
649
          case PARA_VALUE_QUOTED:
276
649
            break;
277
23.1k
          case PARA_NAME:
278
23.1k
            param->name.len = tmp-param->name.s;
279
27.6k
          case S_EQUAL:
280
29.6k
          case S_PARA_VALUE:
281
29.6k
            if (param->type==TAG_PARAM)
282
18
              goto parse_error;
283
29.6k
            param->value.s = tmp;
284
44.6k
          case PARA_VALUE_TOKEN:
285
44.6k
            param->value.len=tmp-param->value.s;
286
44.6k
            add_param(param,to_b);
287
133k
          case E_PARA_VALUE:
288
133k
            param = (struct to_param*)
289
133k
              pkg_malloc(sizeof(struct to_param));
290
133k
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
133k
            memset(param,0,sizeof(struct to_param));
295
133k
            param->type=GENERAL_PARAM;
296
133k
            status = S_PARA_NAME;
297
133k
            break;
298
815
          case F_CRLF:
299
2.56k
          case F_LF:
300
2.95k
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
2.95k
            goto endofheader;
303
76
          default:
304
76
            goto parse_error;
305
136k
        }
306
133k
        break;
307
133k
      case 'T':
308
86.3k
      case 't' :
309
86.3k
        switch (status)
310
86.3k
        {
311
592
          case PARA_VALUE_QUOTED:
312
6.20k
          case PARA_VALUE_TOKEN:
313
20.2k
          case PARA_NAME:
314
20.2k
            break;
315
41.8k
          case S_PARA_NAME:
316
41.8k
            param->name.s = tmp;
317
41.8k
            status = TAG1;
318
41.8k
            break;
319
917
          case S_PARA_VALUE:
320
917
            param->value.s = tmp;
321
917
            status = PARA_VALUE_TOKEN;
322
917
            break;
323
782
          case TAG1:
324
1.66k
          case TAG2:
325
3.84k
          case TAG3:
326
3.84k
            status = PARA_NAME;
327
3.84k
            break;
328
3.95k
          case F_CRLF:
329
15.9k
          case F_LF:
330
19.3k
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
19.3k
            goto endofheader;
333
86
          default:
334
86
            goto parse_error;
335
86.3k
        }
336
66.9k
        break;
337
66.9k
      case 'A':
338
78.6k
      case 'a' :
339
78.6k
        switch (status)
340
78.6k
        {
341
598
          case PARA_VALUE_QUOTED:
342
3.61k
          case PARA_VALUE_TOKEN:
343
21.4k
          case PARA_NAME:
344
21.4k
            break;
345
9.07k
          case S_PARA_NAME:
346
9.07k
            param->name.s = tmp;
347
9.07k
            status = PARA_NAME;
348
9.07k
            break;
349
2.50k
          case S_PARA_VALUE:
350
2.50k
            param->value.s = tmp;
351
2.50k
            status = PARA_VALUE_TOKEN;
352
2.50k
            break;
353
21.8k
          case TAG1:
354
21.8k
            status = TAG2;
355
21.8k
            break;
356
440
          case TAG2:
357
2.62k
          case TAG3:
358
2.62k
            status = PARA_NAME;
359
2.62k
            break;
360
19.2k
          case F_CRLF:
361
20.0k
          case F_LF:
362
21.2k
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
21.2k
            goto endofheader;
365
38
          default:
366
38
            goto parse_error;
367
78.6k
        }
368
57.4k
        break;
369
57.4k
      case 'G':
370
50.7k
      case 'g' :
371
50.7k
        switch (status)
372
50.7k
        {
373
485
          case PARA_VALUE_QUOTED:
374
5.11k
          case PARA_VALUE_TOKEN:
375
22.8k
          case PARA_NAME:
376
22.8k
            break;
377
5.26k
          case S_PARA_NAME:
378
5.26k
            param->name.s = tmp;
379
5.26k
            status = PARA_NAME;
380
5.26k
            break;
381
1.12k
          case S_PARA_VALUE:
382
1.12k
            param->value.s = tmp;
383
1.12k
            status = PARA_VALUE_TOKEN;
384
1.12k
            break;
385
2.02k
          case TAG1:
386
2.78k
          case TAG3:
387
2.78k
            status = PARA_NAME;
388
2.78k
            break;
389
16.4k
          case TAG2:
390
16.4k
            status = TAG3;
391
16.4k
            break;
392
410
          case F_CRLF:
393
1.25k
          case F_LF:
394
2.25k
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
2.25k
            goto endofheader;
397
50
          default:
398
50
            goto parse_error;
399
50.7k
        }
400
48.4k
        break;
401
48.4k
      case '=':
402
45.9k
        switch (status)
403
45.9k
        {
404
693
          case PARA_VALUE_QUOTED:
405
693
            break;
406
9.03k
          case TAG3:
407
9.03k
            param->type=TAG_PARAM;
408
29.0k
          case PARA_NAME:
409
35.0k
          case TAG1:
410
35.9k
          case TAG2:
411
35.9k
            param->name.len = tmp-param->name.s;
412
35.9k
            status = S_PARA_VALUE;
413
35.9k
            break;
414
3.49k
          case S_EQUAL:
415
3.49k
            status = S_PARA_VALUE;
416
3.49k
            break;
417
2.29k
          case F_CRLF:
418
5.18k
          case F_LF:
419
5.59k
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
5.59k
            goto endofheader;
422
186
          default:
423
186
            goto parse_error;
424
45.9k
        }
425
40.1k
        break;
426
2.54M
      default:
427
2.54M
        switch (status)
428
2.54M
        {
429
6.02k
          case TAG1:
430
7.45k
          case TAG2:
431
8.15k
          case TAG3:
432
8.15k
            status = PARA_NAME;
433
8.15k
            break;
434
218k
          case PARA_VALUE_TOKEN:
435
2.41M
          case PARA_NAME:
436
2.42M
          case PARA_VALUE_QUOTED:
437
2.42M
            break;
438
75.5k
          case S_PARA_NAME:
439
75.5k
            param->name.s = tmp;
440
75.5k
            status = PARA_NAME;
441
75.5k
            break;
442
27.3k
          case S_PARA_VALUE:
443
27.3k
            param->value.s = tmp;
444
27.3k
            status = PARA_VALUE_TOKEN;
445
27.3k
            break;
446
3.60k
          case F_CRLF:
447
7.65k
          case F_LF:
448
10.8k
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
10.8k
            goto endofheader;
451
745
          default:
452
745
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
745
            goto error;
454
2.54M
        }
455
3.11M
    }/*switch*/
456
3.11M
  }/*for*/
457
458
1.76k
  if (status==PARA_VALUE_QUOTED) {
459
122
      LM_ERR("unexpected end of header in state %d\n", status);
460
122
      goto parse_error;
461
122
  }
462
463
81.0k
endofheader:
464
81.0k
  LM_DBG("end of header reached, state=%d\n", status);
465
81.0k
  if (param) {
466
58.9k
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
57.4k
      saved_status = E_PARA_VALUE;
468
57.4k
      param->value.s= 0;
469
57.4k
      param->value.len=0;
470
57.4k
      if (param->type==TAG_PARAM)
471
169
        goto parse_error;
472
57.2k
      add_param(param, to_b);
473
57.2k
    } else {
474
1.47k
      pkg_free(param);
475
1.47k
    }
476
58.9k
  }
477
80.8k
  *returned_status=saved_status;
478
80.8k
  return tmp;
479
480
1.56k
parse_error:
481
1.56k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
1.56k
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
2.30k
error:
484
2.30k
  if (param) pkg_free(param);
485
2.30k
  free_to_params(to_b);
486
2.30k
  to_b->error=PARSE_ERROR;
487
2.30k
  *returned_status = status;
488
2.30k
  return tmp;
489
1.56k
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
118k
{
497
118k
  int status;
498
118k
  int saved_status;
499
118k
  char  *tmp;
500
118k
  char  *end_mark;
501
118k
  struct to_body *first_b = to_b;
502
503
118k
  status=START_TO;
504
118k
  saved_status=START_TO;
505
118k
  memset(to_b, 0, sizeof(struct to_body));
506
118k
  to_b->error=PARSE_OK;
507
118k
  end_mark=0;
508
509
21.1M
  for( tmp=buffer; tmp<end; tmp++)
510
21.1M
  {
511
21.1M
    switch(*tmp)
512
21.1M
    {
513
4.61M
      case ' ':
514
4.62M
      case '\t':
515
4.62M
        switch (status)
516
4.62M
        {
517
790
          case F_CRLF:
518
3.29k
          case F_LF:
519
7.14k
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
7.14k
            status=saved_status;
522
7.14k
            break;
523
1.17k
          case URI_ENCLOSED:
524
1.17k
            to_b->uri.len = tmp - to_b->uri.s;
525
1.17k
            status = E_URI_ENCLOSED;
526
1.17k
            break;
527
11.5k
          case URI_OR_TOKEN:
528
11.5k
            status = MAYBE_URI_END;
529
11.5k
            end_mark = tmp;
530
11.5k
            break;
531
2.15k
          case DISPLAY_TOKEN:
532
2.15k
            end_mark = tmp;
533
2.15k
            status = DISPLAY_TOKEN2;
534
2.15k
            break;
535
4.62M
        }
536
4.62M
        break;
537
4.62M
      case '\n':
538
32.9k
        switch (status)
539
32.9k
        {
540
15.6k
          case URI_OR_TOKEN:
541
15.6k
            end_mark = tmp;
542
15.6k
            status = MAYBE_URI_END;
543
16.8k
          case MAYBE_URI_END:
544
17.9k
          case DISPLAY_TOKEN:
545
18.3k
          case DISPLAY_TOKEN2:
546
18.8k
          case E_DISPLAY_QUOTED:
547
24.2k
          case END:
548
24.2k
            saved_status=status;
549
24.2k
            status=F_LF;
550
24.2k
            break;
551
8.02k
          case F_CR:
552
8.02k
            status=F_CRLF;
553
8.02k
            break;
554
3
          case F_CRLF:
555
10
          case F_LF:
556
10
            status=saved_status;
557
10
            goto endofheader;
558
665
          default:
559
665
            goto parse_error;
560
32.9k
        }
561
32.3k
        break;
562
32.3k
      case '\r':
563
16.7k
        switch (status)
564
16.7k
        {
565
12.7k
          case URI_OR_TOKEN:
566
12.7k
            end_mark = tmp;
567
12.7k
            status = MAYBE_URI_END;
568
            /* fall through */
569
13.8k
          case MAYBE_URI_END:
570
14.4k
          case DISPLAY_TOKEN:
571
15.3k
          case DISPLAY_TOKEN2:
572
15.7k
          case E_DISPLAY_QUOTED:
573
16.7k
          case END:
574
16.7k
            saved_status=status;
575
16.7k
            status=F_CR;
576
16.7k
            break;
577
2
          case F_CRLF:
578
33
          case F_CR:
579
35
          case F_LF:
580
35
            status=saved_status;
581
35
            goto endofheader;
582
35
          default:
583
35
            goto parse_error;
584
16.7k
        }
585
16.7k
        break;
586
16.7k
      case 0:
587
4.63k
        switch (status)
588
4.63k
        {
589
2.67k
          case URI_OR_TOKEN:
590
4.10k
          case MAYBE_URI_END:
591
4.10k
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
4.55k
          case END:
594
4.55k
            saved_status = status = END;
595
4.55k
            goto endofheader;
596
89
          default:
597
89
            goto parse_error;
598
4.63k
        }
599
0
        break;
600
6.16M
      case ',':
601
6.16M
        switch (status)
602
6.16M
        {
603
1.02k
          case DISPLAY_QUOTED:
604
5.61k
          case URI_ENCLOSED:
605
5.61k
            break;
606
6.15M
          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.15M
            if (multi==0)
616
2.37k
              break;
617
6.15M
          case MAYBE_URI_END:
618
6.15M
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
6.15M
          case END:
621
6.15M
            if (multi==0)
622
22
              goto parse_error;
623
6.15M
            to_b->next = (struct to_body*)
624
6.15M
              pkg_malloc(sizeof(struct to_body));
625
6.15M
            if (to_b->next==NULL) {
626
0
              LM_ERR("failed to allocate new TO body\n");
627
0
              goto error;
628
0
            }
629
6.15M
            to_b = to_b->next;
630
6.15M
            memset(to_b, 0, sizeof(struct to_body));
631
6.15M
            to_b->error = PARSE_OK;
632
6.15M
            saved_status = status = START_TO;
633
6.15M
            end_mark=0;
634
6.15M
            break;
635
114
          default:
636
114
            goto parse_error;
637
6.16M
        }
638
6.16M
        break;
639
6.16M
      case '\\':
640
500
        switch (status)
641
500
        {
642
454
          case DISPLAY_QUOTED:
643
454
            tmp++; /* jump over next char */
644
454
            break;
645
46
          default:
646
46
            goto parse_error;
647
500
        }
648
454
        break;
649
13.2k
      case '<':
650
13.2k
        switch (status)
651
13.2k
        {
652
4.74k
          case START_TO:
653
4.74k
            to_b->body.s=tmp;
654
4.74k
            status = S_URI_ENCLOSED;
655
4.74k
            break;
656
425
          case DISPLAY_QUOTED:
657
425
            break;
658
305
          case E_DISPLAY_QUOTED:
659
305
            status = S_URI_ENCLOSED;
660
305
            break;
661
1.77k
          case URI_OR_TOKEN:
662
2.53k
          case DISPLAY_TOKEN:
663
2.53k
            end_mark = tmp;
664
            /* fall through */
665
3.08k
          case DISPLAY_TOKEN2:
666
3.57k
          case MAYBE_URI_END:
667
3.57k
            to_b->display.len=end_mark-to_b->display.s;
668
3.57k
            status = S_URI_ENCLOSED;
669
3.57k
            break;
670
508
          case F_CRLF:
671
3.73k
          case F_LF:
672
4.17k
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
4.17k
            goto endofheader;
675
60
          default:
676
60
            goto parse_error;
677
13.2k
        }
678
9.04k
        break;
679
13.5k
      case '>':
680
13.5k
        switch (status)
681
13.5k
        {
682
537
          case DISPLAY_QUOTED:
683
537
            break;
684
7.09k
          case URI_ENCLOSED:
685
7.09k
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
8.17k
          case E_URI_ENCLOSED:
688
8.17k
            status = END;
689
8.17k
            break;
690
3.33k
          case F_CRLF:
691
3.98k
          case F_LF:
692
4.81k
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
4.81k
            goto endofheader;
695
54
          default:
696
54
            goto parse_error;
697
13.5k
        }
698
8.71k
        break;
699
8.71k
      case '"':
700
3.91k
        switch (status)
701
3.91k
        {
702
628
          case START_TO:
703
628
            to_b->body.s = tmp;
704
628
            to_b->display.s = tmp;
705
628
            status = DISPLAY_QUOTED;
706
628
            break;
707
428
          case DISPLAY_QUOTED:
708
428
            status = E_DISPLAY_QUOTED;
709
428
            to_b->display.len = tmp-to_b->display.s+1;
710
428
            break;
711
397
          case F_CRLF:
712
1.86k
          case F_LF:
713
2.81k
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
2.81k
            goto endofheader;
716
39
          default:
717
39
            goto parse_error;
718
3.91k
        }
719
1.05k
        break;
720
100k
      case ';' :
721
100k
        switch (status)
722
100k
        {
723
531
          case DISPLAY_QUOTED:
724
2.78k
          case DISPLAY_TOKEN:
725
14.8k
          case URI_ENCLOSED:
726
14.8k
            break;
727
76.3k
          case URI_OR_TOKEN:
728
76.3k
            end_mark = tmp;
729
            /* fall through */
730
81.5k
          case MAYBE_URI_END:
731
81.5k
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
83.1k
          case END:
734
83.1k
            to_b->body.len = tmp-to_b->body.s;
735
83.1k
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
83.1k
            if (to_b->error!=PARSE_ERROR && multi && *tmp==',') {
737
              /* continue with a new body instance */
738
4.53k
              to_b->next = (struct to_body*)
739
4.53k
                pkg_malloc(sizeof(struct to_body));
740
4.53k
              if (to_b->next==NULL) {
741
0
                LM_ERR("failed to allocate new TO body\n");
742
0
                goto error;
743
0
              }
744
4.53k
              to_b = to_b->next;
745
4.53k
              memset(to_b, 0, sizeof(struct to_body));
746
4.53k
              to_b->error=PARSE_OK;
747
4.53k
              saved_status = status = START_TO;
748
4.53k
              end_mark=0;
749
4.53k
              break;
750
78.6k
            } else {
751
78.6k
              goto endofheader;
752
78.6k
            }
753
1.37k
          case F_CRLF:
754
2.43k
          case F_LF:
755
2.85k
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
2.85k
            goto endofheader;
758
49
          default:
759
49
            goto parse_error;
760
100k
        }
761
19.3k
        break;
762
10.2M
      default:
763
10.2M
        switch (status)
764
10.2M
        {
765
6.27M
          case START_TO:
766
6.27M
            to_b->uri.s = to_b->body.s = tmp;
767
6.27M
            status = URI_OR_TOKEN;
768
6.27M
            to_b->display.s=tmp;
769
6.27M
            break;
770
8.47k
          case S_URI_ENCLOSED:
771
8.47k
            to_b->uri.s=tmp;
772
8.47k
            status=URI_ENCLOSED;
773
8.47k
            break;
774
2.18k
          case MAYBE_URI_END:
775
3.66k
          case DISPLAY_TOKEN2:
776
3.66k
            status = DISPLAY_TOKEN;
777
21.7k
          case DISPLAY_QUOTED:
778
2.29M
          case DISPLAY_TOKEN:
779
3.55M
          case URI_ENCLOSED:
780
3.91M
          case URI_OR_TOKEN:
781
3.91M
            break;
782
1.61k
          case F_CRLF:
783
15.9k
          case F_LF:
784
17.9k
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
17.9k
            goto endofheader;
787
128
          default:
788
128
            LM_DBG("spitting out [%c] in status %d\n",
789
128
            *tmp,status );
790
128
            goto error;
791
10.2M
        }
792
21.1M
    }/*char switch*/
793
21.1M
  }/*for*/
794
795
117k
endofheader:
796
117k
  if (to_b->display.len==0) to_b->display.s=0;
797
117k
  status=saved_status;
798
117k
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
117k
  switch(status){
801
27.6k
    case MAYBE_URI_END:
802
27.6k
      to_b->uri.len = end_mark - to_b->uri.s;
803
37.5k
    case END:
804
37.5k
      to_b->body.len = tmp - to_b->body.s;
805
113k
    case E_PARA_VALUE:
806
113k
      break;
807
3.83k
    default:
808
3.83k
      LM_ERR("unexpected end of header in state %d\n", status);
809
3.83k
      goto error;
810
117k
  }
811
812
113k
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
113k
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
113k
  return tmp;
816
817
1.17k
parse_error:
818
1.17k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
1.17k
      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.17k
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
117k
{
831
117k
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
117k
}
833
834
835
char* parse_multi_to(char* buffer, char *end, struct to_body *to_b)
836
1.33k
{
837
1.33k
  return _parse_to( buffer, end, to_b, 1/*multi*/);
838
1.33k
}
839
840
841
/**
842
 *
843
 */
844
struct sip_uri *parse_to_uri(struct sip_msg *msg)
845
11.8k
{
846
11.8k
  struct to_body *tb = NULL;
847
11.8k
  if(msg==NULL || msg->to==NULL || msg->to->parsed==NULL)
848
0
    return NULL;
849
850
11.8k
  tb = get_to(msg);
851
852
11.8k
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
853
4.82k
    return &tb->parsed_uri;
854
855
7.04k
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
856
5.44k
  {
857
5.44k
    LM_ERR("failed to parse To uri\n");
858
5.44k
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
859
5.44k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
860
5.44k
    set_err_reply(400, "bad To uri");
861
5.44k
    return NULL;
862
5.44k
  }
863
864
1.60k
  return &tb->parsed_uri;
865
7.04k
}
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
}