Coverage Report

Created: 2023-11-19 07:03

/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
92.7k
  do{\
56
92.7k
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
92.7k
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
92.7k
    else (_body)->last_param->next=(_param);\
60
92.7k
    (_body)->last_param =(_param);\
61
92.7k
    if ((_param)->type==TAG_PARAM)\
62
92.7k
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
92.7k
    (_param) = 0;\
64
92.7k
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
6.01M
{
70
6.01M
  struct to_param *tp=tb->param_lst;
71
6.01M
  struct to_param *foo;
72
6.11M
  while (tp){
73
92.7k
    foo = tp->next;
74
92.7k
    pkg_free(tp);
75
92.7k
    tp=foo;
76
92.7k
  }
77
78
6.01M
  tb->param_lst = tb->last_param = NULL;
79
6.01M
}
80
81
82
void free_to(struct to_body* tb)
83
6.10M
{
84
6.10M
  if (tb) {
85
6.01M
    free_to( tb->next );
86
6.01M
    free_to_params(tb);
87
6.01M
    pkg_free(tb);
88
6.01M
  }
89
6.10M
}
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
55.2k
{
97
55.2k
  struct to_param *param;
98
55.2k
  int status;
99
55.2k
  int saved_status;
100
55.2k
  char  *tmp;
101
102
55.2k
  param=0;
103
55.2k
  status=E_PARA_VALUE;
104
55.2k
  saved_status=E_PARA_VALUE;
105
2.43M
  for( tmp=buffer; tmp<end; tmp++)
106
2.43M
  {
107
2.43M
    switch(*tmp)
108
2.43M
    {
109
13.7k
      case ' ':
110
24.9k
      case '\t':
111
24.9k
        switch (status)
112
24.9k
        {
113
677
          case TAG3:
114
677
            param->type=TAG_PARAM;
115
7.15k
          case PARA_NAME:
116
7.76k
          case TAG1:
117
8.25k
          case TAG2:
118
8.25k
            param->name.len = tmp-param->name.s;
119
8.25k
            status = S_EQUAL;
120
8.25k
            break;
121
3.50k
          case PARA_VALUE_TOKEN:
122
3.50k
            param->value.len = tmp-param->value.s;
123
3.50k
            status = E_PARA_VALUE;
124
3.50k
            add_param( param , to_b );
125
3.50k
            break;
126
443
          case F_CRLF:
127
4.69k
          case F_LF:
128
9.19k
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
9.19k
            status=saved_status;
131
9.19k
            break;
132
24.9k
        }
133
24.9k
        break;
134
33.3k
      case '\n':
135
33.3k
        switch (status)
136
33.3k
        {
137
1.50k
          case S_PARA_NAME:
138
4.44k
          case S_EQUAL:
139
5.40k
          case S_PARA_VALUE:
140
8.17k
          case E_PARA_VALUE:
141
8.17k
            saved_status=status;
142
8.17k
            status=F_LF;
143
8.17k
            break;
144
530
          case TAG3:
145
530
            param->type=TAG_PARAM;
146
12.5k
          case PARA_NAME:
147
16.2k
          case TAG1:
148
17.5k
          case TAG2:
149
17.5k
            param->name.len = tmp-param->name.s;
150
17.5k
            saved_status = S_EQUAL;
151
17.5k
            status = F_LF;
152
17.5k
            break;
153
2.69k
          case PARA_VALUE_TOKEN:
154
2.69k
            param->value.len = tmp-param->value.s;
155
2.69k
            saved_status = E_PARA_VALUE;
156
2.69k
            status = F_LF;
157
2.69k
            add_param( param , to_b );
158
2.69k
            break;
159
4.61k
          case F_CR:
160
4.61k
            status=F_CRLF;
161
4.61k
            break;
162
2
          case F_CRLF:
163
7
          case F_LF:
164
7
            status=saved_status;
165
7
            goto endofheader;
166
262
          default:
167
262
            goto parse_error;
168
33.3k
        }
169
33.0k
        break;
170
33.0k
      case '\r':
171
18.4k
        switch (status)
172
18.4k
        {
173
979
          case S_PARA_NAME:
174
1.57k
          case S_EQUAL:
175
2.08k
          case S_PARA_VALUE:
176
2.51k
          case E_PARA_VALUE:
177
2.51k
            saved_status=status;
178
2.51k
            status=F_CR;
179
2.51k
            break;
180
412
          case TAG3:
181
412
            param->type=TAG_PARAM;
182
11.1k
          case PARA_NAME:
183
13.1k
          case TAG1:
184
13.6k
          case TAG2:
185
13.6k
            param->name.len = tmp-param->name.s;
186
13.6k
            saved_status = S_EQUAL;
187
13.6k
            status = F_CR;
188
13.6k
            break;
189
2.19k
          case PARA_VALUE_TOKEN:
190
2.19k
            param->value.len = tmp-param->value.s;
191
2.19k
            saved_status = E_PARA_VALUE;
192
2.19k
            status = F_CR;
193
2.19k
            add_param( param , to_b );
194
2.19k
            break;
195
3
          case F_CRLF:
196
66
          case F_CR:
197
70
          case F_LF:
198
70
            status=saved_status;
199
70
            goto endofheader;
200
40
          default:
201
40
            goto parse_error;
202
18.4k
        }
203
18.3k
        break;
204
18.3k
      case  0:
205
16.1k
      case ',':
206
16.1k
        switch (status)
207
16.1k
        {
208
856
          case PARA_VALUE_QUOTED:
209
856
            break;
210
8.56k
          case PARA_NAME:
211
8.56k
            param->name.len = tmp-param->name.s;
212
9.80k
          case S_EQUAL:
213
10.5k
          case S_PARA_VALUE:
214
10.5k
            if (param->type==TAG_PARAM)
215
18
              goto parse_error;
216
10.5k
            param->value.s = tmp;
217
14.3k
          case PARA_VALUE_TOKEN:
218
14.3k
            status = E_PARA_VALUE;
219
14.3k
            param->value.len = tmp-param->value.s;
220
14.3k
            add_param( param , to_b );
221
15.1k
          case E_PARA_VALUE:
222
15.1k
            saved_status = status;
223
15.1k
            if ( !multi && *tmp==',')
224
211
              goto parse_error;
225
14.9k
            goto endofheader;
226
14.9k
            break;
227
14.9k
          default:
228
82
            goto parse_error;
229
16.1k
        }
230
856
        break;
231
856
      case '\\':
232
487
        switch (status)
233
487
        {
234
443
          case PARA_VALUE_QUOTED:
235
443
            if (tmp+1==end)
236
3
              goto parse_error;
237
440
            switch (*(tmp+1))
238
440
            {
239
19
              case '\r':
240
37
              case '\n':
241
37
                break;
242
403
              default:
243
403
                tmp++;
244
403
                break;
245
440
            }
246
440
            break;
247
440
          default:
248
44
            goto parse_error;
249
487
        }
250
440
        break;
251
12.4k
      case '"':
252
12.4k
        switch (status)
253
12.4k
        {
254
4.56k
          case S_PARA_VALUE:
255
4.56k
            param->value.s = tmp+1;
256
4.56k
            status = PARA_VALUE_QUOTED;
257
4.56k
            break;
258
4.10k
          case PARA_VALUE_QUOTED:
259
4.10k
            param->value.len=tmp-param->value.s ;
260
4.10k
            add_param( param , to_b );
261
4.10k
            status = E_PARA_VALUE;
262
4.10k
            break;
263
919
          case F_CRLF:
264
1.85k
          case F_LF:
265
3.59k
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
3.59k
            goto endofheader;
268
138
          default:
269
138
            goto parse_error;
270
12.4k
        }
271
8.67k
        break;
272
100k
      case ';' :
273
100k
        switch (status)
274
100k
        {
275
553
          case PARA_VALUE_QUOTED:
276
553
            break;
277
22.7k
          case PARA_NAME:
278
22.7k
            param->name.len = tmp-param->name.s;
279
25.9k
          case S_EQUAL:
280
29.1k
          case S_PARA_VALUE:
281
29.1k
            if (param->type==TAG_PARAM)
282
18
              goto parse_error;
283
29.1k
            param->value.s = tmp;
284
35.1k
          case PARA_VALUE_TOKEN:
285
35.1k
            param->value.len=tmp-param->value.s;
286
35.1k
            add_param(param,to_b);
287
96.2k
          case E_PARA_VALUE:
288
96.2k
            param = (struct to_param*)
289
96.2k
              pkg_malloc(sizeof(struct to_param));
290
96.2k
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
96.2k
            memset(param,0,sizeof(struct to_param));
295
96.2k
            param->type=GENERAL_PARAM;
296
96.2k
            status = S_PARA_NAME;
297
96.2k
            break;
298
388
          case F_CRLF:
299
3.09k
          case F_LF:
300
3.51k
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
3.51k
            goto endofheader;
303
74
          default:
304
74
            goto parse_error;
305
100k
        }
306
96.8k
        break;
307
96.8k
      case 'T':
308
80.4k
      case 't' :
309
80.4k
        switch (status)
310
80.4k
        {
311
436
          case PARA_VALUE_QUOTED:
312
3.19k
          case PARA_VALUE_TOKEN:
313
22.6k
          case PARA_NAME:
314
22.6k
            break;
315
37.1k
          case S_PARA_NAME:
316
37.1k
            param->name.s = tmp;
317
37.1k
            status = TAG1;
318
37.1k
            break;
319
1.18k
          case S_PARA_VALUE:
320
1.18k
            param->value.s = tmp;
321
1.18k
            status = PARA_VALUE_TOKEN;
322
1.18k
            break;
323
607
          case TAG1:
324
1.03k
          case TAG2:
325
6.11k
          case TAG3:
326
6.11k
            status = PARA_NAME;
327
6.11k
            break;
328
1.02k
          case F_CRLF:
329
12.1k
          case F_LF:
330
13.3k
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
13.3k
            goto endofheader;
333
75
          default:
334
75
            goto parse_error;
335
80.4k
        }
336
67.0k
        break;
337
67.0k
      case 'A':
338
84.2k
      case 'a' :
339
84.2k
        switch (status)
340
84.2k
        {
341
460
          case PARA_VALUE_QUOTED:
342
4.08k
          case PARA_VALUE_TOKEN:
343
43.7k
          case PARA_NAME:
344
43.7k
            break;
345
8.72k
          case S_PARA_NAME:
346
8.72k
            param->name.s = tmp;
347
8.72k
            status = PARA_NAME;
348
8.72k
            break;
349
1.62k
          case S_PARA_VALUE:
350
1.62k
            param->value.s = tmp;
351
1.62k
            status = PARA_VALUE_TOKEN;
352
1.62k
            break;
353
21.5k
          case TAG1:
354
21.5k
            status = TAG2;
355
21.5k
            break;
356
411
          case TAG2:
357
5.22k
          case TAG3:
358
5.22k
            status = PARA_NAME;
359
5.22k
            break;
360
453
          case F_CRLF:
361
2.25k
          case F_LF:
362
3.28k
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
3.28k
            goto endofheader;
365
52
          default:
366
52
            goto parse_error;
367
84.2k
        }
368
80.8k
        break;
369
80.8k
      case 'G':
370
35.1k
      case 'g' :
371
35.1k
        switch (status)
372
35.1k
        {
373
436
          case PARA_VALUE_QUOTED:
374
1.08k
          case PARA_VALUE_TOKEN:
375
10.4k
          case PARA_NAME:
376
10.4k
            break;
377
3.03k
          case S_PARA_NAME:
378
3.03k
            param->name.s = tmp;
379
3.03k
            status = PARA_NAME;
380
3.03k
            break;
381
828
          case S_PARA_VALUE:
382
828
            param->value.s = tmp;
383
828
            status = PARA_VALUE_TOKEN;
384
828
            break;
385
1.33k
          case TAG1:
386
1.74k
          case TAG3:
387
1.74k
            status = PARA_NAME;
388
1.74k
            break;
389
16.7k
          case TAG2:
390
16.7k
            status = TAG3;
391
16.7k
            break;
392
389
          case F_CRLF:
393
1.89k
          case F_LF:
394
2.28k
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
2.28k
            goto endofheader;
397
48
          default:
398
48
            goto parse_error;
399
35.1k
        }
400
32.8k
        break;
401
32.8k
      case '=':
402
29.6k
        switch (status)
403
29.6k
        {
404
403
          case PARA_VALUE_QUOTED:
405
403
            break;
406
4.22k
          case TAG3:
407
4.22k
            param->type=TAG_PARAM;
408
17.9k
          case PARA_NAME:
409
23.0k
          case TAG1:
410
23.4k
          case TAG2:
411
23.4k
            param->name.len = tmp-param->name.s;
412
23.4k
            status = S_PARA_VALUE;
413
23.4k
            break;
414
4.04k
          case S_EQUAL:
415
4.04k
            status = S_PARA_VALUE;
416
4.04k
            break;
417
441
          case F_CRLF:
418
1.16k
          case F_LF:
419
1.56k
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
1.56k
            goto endofheader;
422
172
          default:
423
172
            goto parse_error;
424
29.6k
        }
425
27.9k
        break;
426
1.99M
      default:
427
1.99M
        switch (status)
428
1.99M
        {
429
2.40k
          case TAG1:
430
3.47k
          case TAG2:
431
4.11k
          case TAG3:
432
4.11k
            status = PARA_NAME;
433
4.11k
            break;
434
154k
          case PARA_VALUE_TOKEN:
435
1.92M
          case PARA_NAME:
436
1.92M
          case PARA_VALUE_QUOTED:
437
1.92M
            break;
438
45.7k
          case S_PARA_NAME:
439
45.7k
            param->name.s = tmp;
440
45.7k
            status = PARA_NAME;
441
45.7k
            break;
442
14.6k
          case S_PARA_VALUE:
443
14.6k
            param->value.s = tmp;
444
14.6k
            status = PARA_VALUE_TOKEN;
445
14.6k
            break;
446
550
          case F_CRLF:
447
4.76k
          case F_LF:
448
8.54k
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
8.54k
            goto endofheader;
451
710
          default:
452
710
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
710
            goto error;
454
1.99M
        }
455
2.43M
    }/*switch*/
456
2.43M
  }/*for*/
457
458
2.19k
  if (status==PARA_VALUE_QUOTED) {
459
148
      LM_ERR("unexpected end of header in state %d\n", status);
460
148
      goto parse_error;
461
148
  }
462
463
53.1k
endofheader:
464
53.1k
  LM_DBG("end of header reached, state=%d\n", status);
465
53.1k
  if (param) {
466
32.8k
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
30.9k
      saved_status = E_PARA_VALUE;
468
30.9k
      param->value.s= 0;
469
30.9k
      param->value.len=0;
470
30.9k
      if (param->type==TAG_PARAM)
471
190
        goto parse_error;
472
30.8k
      add_param(param, to_b);
473
30.8k
    } else {
474
1.81k
      pkg_free(param);
475
1.81k
    }
476
32.8k
  }
477
53.0k
  *returned_status=saved_status;
478
53.0k
  return tmp;
479
480
1.57k
parse_error:
481
1.57k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
1.57k
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
2.28k
error:
484
2.28k
  if (param) pkg_free(param);
485
2.28k
  free_to_params(to_b);
486
2.28k
  to_b->error=PARSE_ERROR;
487
2.28k
  *returned_status = status;
488
2.28k
  return tmp;
489
1.57k
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
95.1k
{
497
95.1k
  int status;
498
95.1k
  int saved_status;
499
95.1k
  char  *tmp;
500
95.1k
  char  *end_mark;
501
95.1k
  struct to_body *first_b = to_b;
502
503
95.1k
  status=START_TO;
504
95.1k
  saved_status=START_TO;
505
95.1k
  memset(to_b, 0, sizeof(struct to_body));
506
95.1k
  to_b->error=PARSE_OK;
507
95.1k
  end_mark=0;
508
509
15.4M
  for( tmp=buffer; tmp<end; tmp++)
510
15.4M
  {
511
15.4M
    switch(*tmp)
512
15.4M
    {
513
1.89M
      case ' ':
514
1.90M
      case '\t':
515
1.90M
        switch (status)
516
1.90M
        {
517
547
          case F_CRLF:
518
3.03k
          case F_LF:
519
5.48k
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
5.48k
            status=saved_status;
522
5.48k
            break;
523
583
          case URI_ENCLOSED:
524
583
            to_b->uri.len = tmp - to_b->uri.s;
525
583
            status = E_URI_ENCLOSED;
526
583
            break;
527
9.12k
          case URI_OR_TOKEN:
528
9.12k
            status = MAYBE_URI_END;
529
9.12k
            end_mark = tmp;
530
9.12k
            break;
531
1.69k
          case DISPLAY_TOKEN:
532
1.69k
            end_mark = tmp;
533
1.69k
            status = DISPLAY_TOKEN2;
534
1.69k
            break;
535
1.90M
        }
536
1.90M
        break;
537
1.90M
      case '\n':
538
30.2k
        switch (status)
539
30.2k
        {
540
14.4k
          case URI_OR_TOKEN:
541
14.4k
            end_mark = tmp;
542
14.4k
            status = MAYBE_URI_END;
543
15.2k
          case MAYBE_URI_END:
544
16.1k
          case DISPLAY_TOKEN:
545
16.7k
          case DISPLAY_TOKEN2:
546
17.1k
          case E_DISPLAY_QUOTED:
547
22.0k
          case END:
548
22.0k
            saved_status=status;
549
22.0k
            status=F_LF;
550
22.0k
            break;
551
7.33k
          case F_CR:
552
7.33k
            status=F_CRLF;
553
7.33k
            break;
554
2
          case F_CRLF:
555
5
          case F_LF:
556
5
            status=saved_status;
557
5
            goto endofheader;
558
856
          default:
559
856
            goto parse_error;
560
30.2k
        }
561
29.3k
        break;
562
29.3k
      case '\r':
563
18.2k
        switch (status)
564
18.2k
        {
565
14.4k
          case URI_OR_TOKEN:
566
14.4k
            end_mark = tmp;
567
14.4k
            status = MAYBE_URI_END;
568
            /* fall through */
569
15.7k
          case MAYBE_URI_END:
570
16.2k
          case DISPLAY_TOKEN:
571
16.7k
          case DISPLAY_TOKEN2:
572
17.1k
          case E_DISPLAY_QUOTED:
573
18.1k
          case END:
574
18.1k
            saved_status=status;
575
18.1k
            status=F_CR;
576
18.1k
            break;
577
2
          case F_CRLF:
578
43
          case F_CR:
579
45
          case F_LF:
580
45
            status=saved_status;
581
45
            goto endofheader;
582
38
          default:
583
38
            goto parse_error;
584
18.2k
        }
585
18.1k
        break;
586
18.1k
      case 0:
587
5.98k
        switch (status)
588
5.98k
        {
589
3.19k
          case URI_OR_TOKEN:
590
5.29k
          case MAYBE_URI_END:
591
5.29k
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
5.89k
          case END:
594
5.89k
            saved_status = status = END;
595
5.89k
            goto endofheader;
596
96
          default:
597
96
            goto parse_error;
598
5.98k
        }
599
0
        break;
600
5.94M
      case ',':
601
5.94M
        switch (status)
602
5.94M
        {
603
423
          case DISPLAY_QUOTED:
604
17.8k
          case URI_ENCLOSED:
605
17.8k
            break;
606
5.92M
          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
5.92M
            if (multi==0)
616
7.63k
              break;
617
5.91M
          case MAYBE_URI_END:
618
5.91M
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
5.91M
          case END:
621
5.91M
            if (multi==0)
622
31
              goto parse_error;
623
5.91M
            to_b->next = (struct to_body*)
624
5.91M
              pkg_malloc(sizeof(struct to_body));
625
5.91M
            if (to_b->next==NULL) {
626
0
              LM_ERR("failed to allocate new TO body\n");
627
0
              goto error;
628
0
            }
629
5.91M
            to_b = to_b->next;
630
5.91M
            memset(to_b, 0, sizeof(struct to_body));
631
5.91M
            to_b->error = PARSE_OK;
632
5.91M
            saved_status = status = START_TO;
633
5.91M
            end_mark=0;
634
5.91M
            break;
635
68
          default:
636
68
            goto parse_error;
637
5.94M
        }
638
5.94M
        break;
639
5.94M
      case '\\':
640
457
        switch (status)
641
457
        {
642
414
          case DISPLAY_QUOTED:
643
414
            tmp++; /* jump over next char */
644
414
            break;
645
43
          default:
646
43
            goto parse_error;
647
457
        }
648
414
        break;
649
10.7k
      case '<':
650
10.7k
        switch (status)
651
10.7k
        {
652
4.87k
          case START_TO:
653
4.87k
            to_b->body.s=tmp;
654
4.87k
            status = S_URI_ENCLOSED;
655
4.87k
            break;
656
557
          case DISPLAY_QUOTED:
657
557
            break;
658
422
          case E_DISPLAY_QUOTED:
659
422
            status = S_URI_ENCLOSED;
660
422
            break;
661
1.34k
          case URI_OR_TOKEN:
662
1.95k
          case DISPLAY_TOKEN:
663
1.95k
            end_mark = tmp;
664
            /* fall through */
665
2.42k
          case DISPLAY_TOKEN2:
666
2.90k
          case MAYBE_URI_END:
667
2.90k
            to_b->display.len=end_mark-to_b->display.s;
668
2.90k
            status = S_URI_ENCLOSED;
669
2.90k
            break;
670
426
          case F_CRLF:
671
1.35k
          case F_LF:
672
2.00k
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
2.00k
            goto endofheader;
675
45
          default:
676
45
            goto parse_error;
677
10.7k
        }
678
8.75k
        break;
679
13.9k
      case '>':
680
13.9k
        switch (status)
681
13.9k
        {
682
403
          case DISPLAY_QUOTED:
683
403
            break;
684
7.17k
          case URI_ENCLOSED:
685
7.17k
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
7.73k
          case E_URI_ENCLOSED:
688
7.73k
            status = END;
689
7.73k
            break;
690
2.85k
          case F_CRLF:
691
3.75k
          case F_LF:
692
5.76k
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
5.76k
            goto endofheader;
695
61
          default:
696
61
            goto parse_error;
697
13.9k
        }
698
8.14k
        break;
699
8.14k
      case '"':
700
5.63k
        switch (status)
701
5.63k
        {
702
788
          case START_TO:
703
788
            to_b->body.s = tmp;
704
788
            to_b->display.s = tmp;
705
788
            status = DISPLAY_QUOTED;
706
788
            break;
707
553
          case DISPLAY_QUOTED:
708
553
            status = E_DISPLAY_QUOTED;
709
553
            to_b->display.len = tmp-to_b->display.s+1;
710
553
            break;
711
438
          case F_CRLF:
712
3.51k
          case F_LF:
713
4.24k
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
4.24k
            goto endofheader;
716
50
          default:
717
50
            goto parse_error;
718
5.63k
        }
719
1.34k
        break;
720
68.8k
      case ';' :
721
68.8k
        switch (status)
722
68.8k
        {
723
463
          case DISPLAY_QUOTED:
724
1.24k
          case DISPLAY_TOKEN:
725
9.52k
          case URI_ENCLOSED:
726
9.52k
            break;
727
49.8k
          case URI_OR_TOKEN:
728
49.8k
            end_mark = tmp;
729
            /* fall through */
730
54.1k
          case MAYBE_URI_END:
731
54.1k
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
55.2k
          case END:
734
55.2k
            to_b->body.len = tmp-to_b->body.s;
735
55.2k
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
55.2k
            if (to_b->error!=PARSE_ERROR && multi && *tmp==',') {
737
              /* continue with a new body instance */
738
2.53k
              to_b->next = (struct to_body*)
739
2.53k
                pkg_malloc(sizeof(struct to_body));
740
2.53k
              if (to_b->next==NULL) {
741
0
                LM_ERR("failed to allocate new TO body\n");
742
0
                goto error;
743
0
              }
744
2.53k
              to_b = to_b->next;
745
2.53k
              memset(to_b, 0, sizeof(struct to_body));
746
2.53k
              to_b->error=PARSE_OK;
747
2.53k
              saved_status = status = START_TO;
748
2.53k
              end_mark=0;
749
2.53k
              break;
750
52.7k
            } else {
751
52.7k
              goto endofheader;
752
52.7k
            }
753
1.85k
          case F_CRLF:
754
2.36k
          case F_LF:
755
3.94k
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
3.94k
            goto endofheader;
758
48
          default:
759
48
            goto parse_error;
760
68.8k
        }
761
12.0k
        break;
762
7.43M
      default:
763
7.43M
        switch (status)
764
7.43M
        {
765
6.00M
          case START_TO:
766
6.00M
            to_b->uri.s = to_b->body.s = tmp;
767
6.00M
            status = URI_OR_TOKEN;
768
6.00M
            to_b->display.s=tmp;
769
6.00M
            break;
770
8.02k
          case S_URI_ENCLOSED:
771
8.02k
            to_b->uri.s=tmp;
772
8.02k
            status=URI_ENCLOSED;
773
8.02k
            break;
774
1.77k
          case MAYBE_URI_END:
775
2.83k
          case DISPLAY_TOKEN2:
776
2.83k
            status = DISPLAY_TOKEN;
777
6.77k
          case DISPLAY_QUOTED:
778
982k
          case DISPLAY_TOKEN:
779
1.06M
          case URI_ENCLOSED:
780
1.39M
          case URI_OR_TOKEN:
781
1.39M
            break;
782
1.20k
          case F_CRLF:
783
14.5k
          case F_LF:
784
17.7k
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
17.7k
            goto endofheader;
787
106
          default:
788
106
            LM_DBG("spitting out [%c] in status %d\n",
789
106
            *tmp,status );
790
106
            goto error;
791
7.43M
        }
792
15.4M
    }/*char switch*/
793
15.4M
  }/*for*/
794
795
93.7k
endofheader:
796
93.7k
  if (to_b->display.len==0) to_b->display.s=0;
797
93.7k
  status=saved_status;
798
93.7k
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
93.7k
  switch(status){
801
28.9k
    case MAYBE_URI_END:
802
28.9k
      to_b->uri.len = end_mark - to_b->uri.s;
803
39.9k
    case END:
804
39.9k
      to_b->body.len = tmp - to_b->body.s;
805
89.6k
    case E_PARA_VALUE:
806
89.6k
      break;
807
4.03k
    default:
808
4.03k
      LM_ERR("unexpected end of header in state %d\n", status);
809
4.03k
      goto error;
810
93.7k
  }
811
812
89.6k
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
89.6k
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
89.6k
  return tmp;
816
817
1.33k
parse_error:
818
1.33k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
1.33k
      tmp < end? *tmp : *(end-1), status, (int)(tmp-buffer), buffer);
820
5.47k
error:
821
5.47k
  first_b->error=PARSE_ERROR;
822
5.47k
  free_to_params(first_b);
823
5.47k
  free_to(first_b->next);
824
5.47k
  return tmp;
825
826
1.33k
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
93.7k
{
831
93.7k
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
93.7k
}
833
834
835
char* parse_multi_to(char* buffer, char *end, struct to_body *to_b)
836
1.42k
{
837
1.42k
  return _parse_to( buffer, end, to_b, 1/*multi*/);
838
1.42k
}
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.31k
    return &tb->parsed_uri;
854
855
7.33k
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
856
5.56k
  {
857
5.56k
    LM_ERR("failed to parse To uri\n");
858
5.56k
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
859
5.56k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
860
5.56k
    set_err_reply(400, "bad To uri");
861
5.56k
    return NULL;
862
5.56k
  }
863
864
1.77k
  return &tb->parsed_uri;
865
7.33k
}
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
}