Coverage Report

Created: 2026-04-29 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_to.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2003 Fhg Fokus
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 *
20
 * History:
21
 * ---------
22
 * 2003-04-26 ZSW (jiri)
23
 * 2006-05-29 removed the NO_PINGTEL_TAG_HACK - it's conflicting the RFC 3261;
24
 *            TAG parameter must have value; other parameters are accepted
25
 *            without value (bogdan)
26
 */
27
28
29
#include "parse_to.h"
30
#include <stdlib.h>
31
#include <string.h>
32
#include "../dprint.h"
33
#include "msg_parser.h"
34
#include "parse_uri.h"
35
#include "../ut.h"
36
#include "../mem/mem.h"
37
#include "../errinfo.h"
38
39
40
enum {
41
  START_TO, DISPLAY_QUOTED, E_DISPLAY_QUOTED, DISPLAY_TOKEN, DISPLAY_TOKEN2,
42
  S_URI_ENCLOSED, URI_ENCLOSED, E_URI_ENCLOSED,
43
  URI_OR_TOKEN, MAYBE_URI_END, END, F_CR, F_LF, F_CRLF
44
};
45
46
47
enum {
48
  S_PARA_NAME=20, PARA_NAME, S_EQUAL, S_PARA_VALUE, TAG1, TAG2,
49
  TAG3, PARA_VALUE_TOKEN , PARA_VALUE_QUOTED, E_PARA_VALUE
50
};
51
52
53
54
#define add_param( _param , _body ) \
55
97.9k
  do{\
56
97.9k
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
97.9k
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
97.9k
    else (_body)->last_param->next=(_param);\
60
97.9k
    (_body)->last_param =(_param);\
61
97.9k
    if ((_param)->type==TAG_PARAM)\
62
97.9k
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
97.9k
    (_param) = 0;\
64
97.9k
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
5.31M
{
70
5.31M
  struct to_param *tp=tb->param_lst;
71
5.31M
  struct to_param *foo;
72
5.40M
  while (tp){
73
97.9k
    foo = tp->next;
74
97.9k
    pkg_free(tp);
75
97.9k
    tp=foo;
76
97.9k
  }
77
78
5.31M
  tb->param_lst = tb->last_param = NULL;
79
5.31M
}
80
81
82
void free_to(struct to_body* tb)
83
5.38M
{
84
5.38M
  if (tb) {
85
5.30M
    free_to( tb->next );
86
5.30M
    free_to_params(tb);
87
5.30M
    pkg_free(tb);
88
5.30M
  }
89
5.38M
}
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
47.6k
{
97
47.6k
  struct to_param *param;
98
47.6k
  int status;
99
47.6k
  int saved_status;
100
47.6k
  char  *tmp;
101
102
47.6k
  param=0;
103
47.6k
  status=E_PARA_VALUE;
104
47.6k
  saved_status=E_PARA_VALUE;
105
1.50M
  for( tmp=buffer; tmp<end; tmp++)
106
1.50M
  {
107
1.50M
    switch(*tmp)
108
1.50M
    {
109
20.0k
      case ' ':
110
27.5k
      case '\t':
111
27.5k
        switch (status)
112
27.5k
        {
113
519
          case TAG3:
114
519
            param->type=TAG_PARAM;
115
4.64k
          case PARA_NAME:
116
5.30k
          case TAG1:
117
5.62k
          case TAG2:
118
5.62k
            param->name.len = tmp-param->name.s;
119
5.62k
            status = S_EQUAL;
120
5.62k
            break;
121
2.79k
          case PARA_VALUE_TOKEN:
122
2.79k
            param->value.len = tmp-param->value.s;
123
2.79k
            status = E_PARA_VALUE;
124
2.79k
            add_param( param , to_b );
125
2.79k
            break;
126
2.26k
          case F_CRLF:
127
8.00k
          case F_LF:
128
13.4k
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
13.4k
            status=saved_status;
131
13.4k
            break;
132
27.5k
        }
133
27.5k
        break;
134
35.0k
      case '\n':
135
35.0k
        switch (status)
136
35.0k
        {
137
1.00k
          case S_PARA_NAME:
138
2.72k
          case S_EQUAL:
139
3.47k
          case S_PARA_VALUE:
140
4.01k
          case E_PARA_VALUE:
141
4.01k
            saved_status=status;
142
4.01k
            status=F_LF;
143
4.01k
            break;
144
352
          case TAG3:
145
352
            param->type=TAG_PARAM;
146
6.67k
          case PARA_NAME:
147
8.11k
          case TAG1:
148
14.0k
          case TAG2:
149
14.0k
            param->name.len = tmp-param->name.s;
150
14.0k
            saved_status = S_EQUAL;
151
14.0k
            status = F_LF;
152
14.0k
            break;
153
6.01k
          case PARA_VALUE_TOKEN:
154
6.01k
            param->value.len = tmp-param->value.s;
155
6.01k
            saved_status = E_PARA_VALUE;
156
6.01k
            status = F_LF;
157
6.01k
            add_param( param , to_b );
158
6.01k
            break;
159
10.7k
          case F_CR:
160
10.7k
            status=F_CRLF;
161
10.7k
            break;
162
3
          case F_CRLF:
163
7
          case F_LF:
164
7
            status=saved_status;
165
7
            goto endofheader;
166
188
          default:
167
188
            goto parse_error;
168
35.0k
        }
169
34.8k
        break;
170
34.8k
      case '\r':
171
22.4k
        switch (status)
172
22.4k
        {
173
904
          case S_PARA_NAME:
174
2.68k
          case S_EQUAL:
175
3.37k
          case S_PARA_VALUE:
176
3.79k
          case E_PARA_VALUE:
177
3.79k
            saved_status=status;
178
3.79k
            status=F_CR;
179
3.79k
            break;
180
485
          case TAG3:
181
485
            param->type=TAG_PARAM;
182
13.2k
          case PARA_NAME:
183
14.6k
          case TAG1:
184
15.2k
          case TAG2:
185
15.2k
            param->name.len = tmp-param->name.s;
186
15.2k
            saved_status = S_EQUAL;
187
15.2k
            status = F_CR;
188
15.2k
            break;
189
3.34k
          case PARA_VALUE_TOKEN:
190
3.34k
            param->value.len = tmp-param->value.s;
191
3.34k
            saved_status = E_PARA_VALUE;
192
3.34k
            status = F_CR;
193
3.34k
            add_param( param , to_b );
194
3.34k
            break;
195
2
          case F_CRLF:
196
26
          case F_CR:
197
32
          case F_LF:
198
32
            status=saved_status;
199
32
            goto endofheader;
200
25
          default:
201
25
            goto parse_error;
202
22.4k
        }
203
22.3k
        break;
204
22.3k
      case  0:
205
13.7k
      case ',':
206
13.7k
        switch (status)
207
13.7k
        {
208
1.02k
          case PARA_VALUE_QUOTED:
209
1.02k
            break;
210
8.89k
          case PARA_NAME:
211
8.89k
            param->name.len = tmp-param->name.s;
212
9.74k
          case S_EQUAL:
213
10.6k
          case S_PARA_VALUE:
214
10.6k
            if (param->type==TAG_PARAM)
215
17
              goto parse_error;
216
10.6k
            param->value.s = tmp;
217
11.9k
          case PARA_VALUE_TOKEN:
218
11.9k
            status = E_PARA_VALUE;
219
11.9k
            param->value.len = tmp-param->value.s;
220
11.9k
            add_param( param , to_b );
221
12.5k
          case E_PARA_VALUE:
222
12.5k
            saved_status = status;
223
12.5k
            if ( !multi && *tmp==',')
224
146
              goto parse_error;
225
12.4k
            goto endofheader;
226
12.4k
            break;
227
12.4k
          default:
228
186
            goto parse_error;
229
13.7k
        }
230
1.02k
        break;
231
1.02k
      case '\\':
232
498
        switch (status)
233
498
        {
234
412
          case PARA_VALUE_QUOTED:
235
412
            if (tmp+1==end)
236
3
              goto parse_error;
237
409
            switch (*(tmp+1))
238
409
            {
239
20
              case '\r':
240
26
              case '\n':
241
26
                break;
242
383
              default:
243
383
                tmp++;
244
383
                break;
245
409
            }
246
409
            break;
247
409
          default:
248
86
            goto parse_error;
249
498
        }
250
409
        break;
251
4.19k
      case '"':
252
4.19k
        switch (status)
253
4.19k
        {
254
1.55k
          case S_PARA_VALUE:
255
1.55k
            param->value.s = tmp+1;
256
1.55k
            status = PARA_VALUE_QUOTED;
257
1.55k
            break;
258
1.22k
          case PARA_VALUE_QUOTED:
259
1.22k
            param->value.len=tmp-param->value.s ;
260
1.22k
            add_param( param , to_b );
261
1.22k
            status = E_PARA_VALUE;
262
1.22k
            break;
263
373
          case F_CRLF:
264
891
          case F_LF:
265
1.36k
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
1.36k
            goto endofheader;
268
47
          default:
269
47
            goto parse_error;
270
4.19k
        }
271
2.78k
        break;
272
103k
      case ';' :
273
103k
        switch (status)
274
103k
        {
275
529
          case PARA_VALUE_QUOTED:
276
529
            break;
277
31.2k
          case PARA_NAME:
278
31.2k
            param->name.len = tmp-param->name.s;
279
37.8k
          case S_EQUAL:
280
39.9k
          case S_PARA_VALUE:
281
39.9k
            if (param->type==TAG_PARAM)
282
15
              goto parse_error;
283
39.9k
            param->value.s = tmp;
284
47.4k
          case PARA_VALUE_TOKEN:
285
47.4k
            param->value.len=tmp-param->value.s;
286
47.4k
            add_param(param,to_b);
287
100k
          case E_PARA_VALUE:
288
100k
            param = (struct to_param*)
289
100k
              pkg_malloc(sizeof(struct to_param));
290
100k
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
100k
            memset(param,0,sizeof(struct to_param));
295
100k
            param->type=GENERAL_PARAM;
296
100k
            status = S_PARA_NAME;
297
100k
            break;
298
328
          case F_CRLF:
299
1.48k
          case F_LF:
300
2.35k
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
2.35k
            goto endofheader;
303
115
          default:
304
115
            goto parse_error;
305
103k
        }
306
101k
        break;
307
101k
      case 'T':
308
59.0k
      case 't' :
309
59.0k
        switch (status)
310
59.0k
        {
311
496
          case PARA_VALUE_QUOTED:
312
4.79k
          case PARA_VALUE_TOKEN:
313
12.2k
          case PARA_NAME:
314
12.2k
            break;
315
35.2k
          case S_PARA_NAME:
316
35.2k
            param->name.s = tmp;
317
35.2k
            status = TAG1;
318
35.2k
            break;
319
920
          case S_PARA_VALUE:
320
920
            param->value.s = tmp;
321
920
            status = PARA_VALUE_TOKEN;
322
920
            break;
323
777
          case TAG1:
324
1.58k
          case TAG2:
325
2.69k
          case TAG3:
326
2.69k
            status = PARA_NAME;
327
2.69k
            break;
328
616
          case F_CRLF:
329
6.93k
          case F_LF:
330
7.88k
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
7.88k
            goto endofheader;
333
53
          default:
334
53
            goto parse_error;
335
59.0k
        }
336
51.1k
        break;
337
51.1k
      case 'A':
338
672k
      case 'a' :
339
672k
        switch (status)
340
672k
        {
341
552
          case PARA_VALUE_QUOTED:
342
10.1k
          case PARA_VALUE_TOKEN:
343
639k
          case PARA_NAME:
344
639k
            break;
345
3.53k
          case S_PARA_NAME:
346
3.53k
            param->name.s = tmp;
347
3.53k
            status = PARA_NAME;
348
3.53k
            break;
349
865
          case S_PARA_VALUE:
350
865
            param->value.s = tmp;
351
865
            status = PARA_VALUE_TOKEN;
352
865
            break;
353
22.5k
          case TAG1:
354
22.5k
            status = TAG2;
355
22.5k
            break;
356
1.68k
          case TAG2:
357
2.64k
          case TAG3:
358
2.64k
            status = PARA_NAME;
359
2.64k
            break;
360
2.52k
          case F_CRLF:
361
3.46k
          case F_LF:
362
4.06k
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
4.06k
            goto endofheader;
365
70
          default:
366
70
            goto parse_error;
367
672k
        }
368
668k
        break;
369
668k
      case 'G':
370
36.3k
      case 'g' :
371
36.3k
        switch (status)
372
36.3k
        {
373
586
          case PARA_VALUE_QUOTED:
374
6.17k
          case PARA_VALUE_TOKEN:
375
13.5k
          case PARA_NAME:
376
13.5k
            break;
377
3.63k
          case S_PARA_NAME:
378
3.63k
            param->name.s = tmp;
379
3.63k
            status = PARA_NAME;
380
3.63k
            break;
381
1.54k
          case S_PARA_VALUE:
382
1.54k
            param->value.s = tmp;
383
1.54k
            status = PARA_VALUE_TOKEN;
384
1.54k
            break;
385
3.10k
          case TAG1:
386
4.92k
          case TAG3:
387
4.92k
            status = PARA_NAME;
388
4.92k
            break;
389
11.3k
          case TAG2:
390
11.3k
            status = TAG3;
391
11.3k
            break;
392
208
          case F_CRLF:
393
1.01k
          case F_LF:
394
1.29k
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
1.29k
            goto endofheader;
397
66
          default:
398
66
            goto parse_error;
399
36.3k
        }
400
35.0k
        break;
401
35.0k
      case '=':
402
31.8k
        switch (status)
403
31.8k
        {
404
222
          case PARA_VALUE_QUOTED:
405
222
            break;
406
5.34k
          case TAG3:
407
5.34k
            param->type=TAG_PARAM;
408
21.0k
          case PARA_NAME:
409
23.2k
          case TAG1:
410
24.0k
          case TAG2:
411
24.0k
            param->name.len = tmp-param->name.s;
412
24.0k
            status = S_PARA_VALUE;
413
24.0k
            break;
414
2.20k
          case S_EQUAL:
415
2.20k
            status = S_PARA_VALUE;
416
2.20k
            break;
417
950
          case F_CRLF:
418
4.84k
          case F_LF:
419
5.25k
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
5.25k
            goto endofheader;
422
82
          default:
423
82
            goto parse_error;
424
31.8k
        }
425
26.4k
        break;
426
496k
      default:
427
496k
        switch (status)
428
496k
        {
429
3.17k
          case TAG1:
430
4.18k
          case TAG2:
431
4.95k
          case TAG3:
432
4.95k
            status = PARA_NAME;
433
4.95k
            break;
434
140k
          case PARA_VALUE_TOKEN:
435
404k
          case PARA_NAME:
436
406k
          case PARA_VALUE_QUOTED:
437
406k
            break;
438
57.0k
          case S_PARA_NAME:
439
57.0k
            param->name.s = tmp;
440
57.0k
            status = PARA_NAME;
441
57.0k
            break;
442
17.7k
          case S_PARA_VALUE:
443
17.7k
            param->value.s = tmp;
444
17.7k
            status = PARA_VALUE_TOKEN;
445
17.7k
            break;
446
3.50k
          case F_CRLF:
447
7.43k
          case F_LF:
448
9.72k
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
9.72k
            goto endofheader;
451
555
          default:
452
555
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
555
            goto error;
454
496k
        }
455
1.50M
    }/*switch*/
456
1.50M
  }/*for*/
457
458
1.59k
  if (status==PARA_VALUE_QUOTED) {
459
113
      LM_ERR("unexpected end of header in state %d\n", status);
460
113
      goto parse_error;
461
113
  }
462
463
45.8k
endofheader:
464
45.8k
  LM_DBG("end of header reached, state=%d\n", status);
465
45.8k
  if (param) {
466
26.6k
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
25.3k
      saved_status = E_PARA_VALUE;
468
25.3k
      param->value.s= 0;
469
25.3k
      param->value.len=0;
470
25.3k
      if (param->type==TAG_PARAM)
471
126
        goto parse_error;
472
25.2k
      add_param(param, to_b);
473
25.2k
    } else {
474
1.28k
      pkg_free(param);
475
1.28k
    }
476
26.6k
  }
477
45.7k
  *returned_status=saved_status;
478
45.7k
  return tmp;
479
480
1.33k
parse_error:
481
1.33k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
1.33k
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
1.89k
error:
484
1.89k
  if (param) pkg_free(param);
485
1.89k
  free_to_params(to_b);
486
1.89k
  to_b->error=PARSE_ERROR;
487
1.89k
  *returned_status = status;
488
1.89k
  return tmp;
489
1.33k
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
77.1k
{
497
77.1k
  int status;
498
77.1k
  int saved_status;
499
77.1k
  char  *tmp;
500
77.1k
  char  *end_mark;
501
77.1k
  struct to_body *first_b = to_b;
502
503
77.1k
  status=START_TO;
504
77.1k
  saved_status=START_TO;
505
77.1k
  memset(to_b, 0, sizeof(struct to_body));
506
77.1k
  to_b->error=PARSE_OK;
507
77.1k
  end_mark=0;
508
509
11.3M
  for( tmp=buffer; tmp<end; tmp++)
510
11.3M
  {
511
11.3M
    switch(*tmp)
512
11.3M
    {
513
33.7k
      case ' ':
514
42.5k
      case '\t':
515
42.5k
        switch (status)
516
42.5k
        {
517
1.02k
          case F_CRLF:
518
3.35k
          case F_LF:
519
5.73k
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
5.73k
            status=saved_status;
522
5.73k
            break;
523
1.42k
          case URI_ENCLOSED:
524
1.42k
            to_b->uri.len = tmp - to_b->uri.s;
525
1.42k
            status = E_URI_ENCLOSED;
526
1.42k
            break;
527
5.73k
          case URI_OR_TOKEN:
528
5.73k
            status = MAYBE_URI_END;
529
5.73k
            end_mark = tmp;
530
5.73k
            break;
531
1.78k
          case DISPLAY_TOKEN:
532
1.78k
            end_mark = tmp;
533
1.78k
            status = DISPLAY_TOKEN2;
534
1.78k
            break;
535
42.5k
        }
536
42.5k
        break;
537
42.5k
      case '\n':
538
25.1k
        switch (status)
539
25.1k
        {
540
12.6k
          case URI_OR_TOKEN:
541
12.6k
            end_mark = tmp;
542
12.6k
            status = MAYBE_URI_END;
543
13.4k
          case MAYBE_URI_END:
544
14.0k
          case DISPLAY_TOKEN:
545
14.3k
          case DISPLAY_TOKEN2:
546
14.8k
          case E_DISPLAY_QUOTED:
547
19.3k
          case END:
548
19.3k
            saved_status=status;
549
19.3k
            status=F_LF;
550
19.3k
            break;
551
5.15k
          case F_CR:
552
5.15k
            status=F_CRLF;
553
5.15k
            break;
554
2
          case F_CRLF:
555
5
          case F_LF:
556
5
            status=saved_status;
557
5
            goto endofheader;
558
618
          default:
559
618
            goto parse_error;
560
25.1k
        }
561
24.4k
        break;
562
24.4k
      case '\r':
563
12.5k
        switch (status)
564
12.5k
        {
565
8.21k
          case URI_OR_TOKEN:
566
8.21k
            end_mark = tmp;
567
8.21k
            status = MAYBE_URI_END;
568
            /* fall through */
569
9.33k
          case MAYBE_URI_END:
570
9.89k
          case DISPLAY_TOKEN:
571
10.3k
          case DISPLAY_TOKEN2:
572
10.8k
          case E_DISPLAY_QUOTED:
573
12.4k
          case END:
574
12.4k
            saved_status=status;
575
12.4k
            status=F_CR;
576
12.4k
            break;
577
3
          case F_CRLF:
578
51
          case F_CR:
579
56
          case F_LF:
580
56
            status=saved_status;
581
56
            goto endofheader;
582
36
          default:
583
36
            goto parse_error;
584
12.5k
        }
585
12.4k
        break;
586
12.4k
      case 0:
587
4.17k
        switch (status)
588
4.17k
        {
589
3.00k
          case URI_OR_TOKEN:
590
3.64k
          case MAYBE_URI_END:
591
3.64k
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
4.09k
          case END:
594
4.09k
            saved_status = status = END;
595
4.09k
            goto endofheader;
596
78
          default:
597
78
            goto parse_error;
598
4.17k
        }
599
0
        break;
600
5.23M
      case ',':
601
5.23M
        switch (status)
602
5.23M
        {
603
450
          case DISPLAY_QUOTED:
604
1.34k
          case URI_ENCLOSED:
605
1.34k
            break;
606
5.22M
          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.22M
            if (multi==0)
616
800
              break;
617
5.22M
          case MAYBE_URI_END:
618
5.22M
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
5.22M
          case END:
621
5.22M
            if (multi==0)
622
43
              goto parse_error;
623
5.22M
            to_b->next = (struct to_body*)
624
5.22M
              pkg_malloc(sizeof(struct to_body));
625
5.22M
            if (to_b->next==NULL) {
626
0
              LM_ERR("failed to allocate new TO body\n");
627
0
              goto error;
628
0
            }
629
5.22M
            to_b = to_b->next;
630
5.22M
            memset(to_b, 0, sizeof(struct to_body));
631
5.22M
            to_b->error = PARSE_OK;
632
5.22M
            saved_status = status = START_TO;
633
5.22M
            end_mark=0;
634
5.22M
            break;
635
96
          default:
636
96
            goto parse_error;
637
5.23M
        }
638
5.23M
        break;
639
5.23M
      case '\\':
640
439
        switch (status)
641
439
        {
642
399
          case DISPLAY_QUOTED:
643
399
            tmp++; /* jump over next char */
644
399
            break;
645
40
          default:
646
40
            goto parse_error;
647
439
        }
648
399
        break;
649
9.53k
      case '<':
650
9.53k
        switch (status)
651
9.53k
        {
652
4.37k
          case START_TO:
653
4.37k
            to_b->body.s=tmp;
654
4.37k
            status = S_URI_ENCLOSED;
655
4.37k
            break;
656
482
          case DISPLAY_QUOTED:
657
482
            break;
658
433
          case E_DISPLAY_QUOTED:
659
433
            status = S_URI_ENCLOSED;
660
433
            break;
661
1.33k
          case URI_OR_TOKEN:
662
2.09k
          case DISPLAY_TOKEN:
663
2.09k
            end_mark = tmp;
664
            /* fall through */
665
2.52k
          case DISPLAY_TOKEN2:
666
3.01k
          case MAYBE_URI_END:
667
3.01k
            to_b->display.len=end_mark-to_b->display.s;
668
3.01k
            status = S_URI_ENCLOSED;
669
3.01k
            break;
670
420
          case F_CRLF:
671
796
          case F_LF:
672
1.18k
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
1.18k
            goto endofheader;
675
44
          default:
676
44
            goto parse_error;
677
9.53k
        }
678
8.30k
        break;
679
12.0k
      case '>':
680
12.0k
        switch (status)
681
12.0k
        {
682
2.79k
          case DISPLAY_QUOTED:
683
2.79k
            break;
684
6.03k
          case URI_ENCLOSED:
685
6.03k
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
7.39k
          case E_URI_ENCLOSED:
688
7.39k
            status = END;
689
7.39k
            break;
690
690
          case F_CRLF:
691
1.16k
          case F_LF:
692
1.68k
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
1.68k
            goto endofheader;
695
126
          default:
696
126
            goto parse_error;
697
12.0k
        }
698
10.1k
        break;
699
10.1k
      case '"':
700
4.12k
        switch (status)
701
4.12k
        {
702
724
          case START_TO:
703
724
            to_b->body.s = tmp;
704
724
            to_b->display.s = tmp;
705
724
            status = DISPLAY_QUOTED;
706
724
            break;
707
559
          case DISPLAY_QUOTED:
708
559
            status = E_DISPLAY_QUOTED;
709
559
            to_b->display.len = tmp-to_b->display.s+1;
710
559
            break;
711
542
          case F_CRLF:
712
2.28k
          case F_LF:
713
2.77k
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
2.77k
            goto endofheader;
716
59
          default:
717
59
            goto parse_error;
718
4.12k
        }
719
1.28k
        break;
720
62.8k
      case ';' :
721
62.8k
        switch (status)
722
62.8k
        {
723
786
          case DISPLAY_QUOTED:
724
1.35k
          case DISPLAY_TOKEN:
725
12.9k
          case URI_ENCLOSED:
726
12.9k
            break;
727
44.6k
          case URI_OR_TOKEN:
728
44.6k
            end_mark = tmp;
729
            /* fall through */
730
46.2k
          case MAYBE_URI_END:
731
46.2k
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
47.6k
          case END:
734
47.6k
            to_b->body.len = tmp-to_b->body.s;
735
47.6k
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
47.6k
            if (to_b->error!=PARSE_ERROR && multi && *tmp==',') {
737
              /* continue with a new body instance */
738
2.16k
              to_b->next = (struct to_body*)
739
2.16k
                pkg_malloc(sizeof(struct to_body));
740
2.16k
              if (to_b->next==NULL) {
741
0
                LM_ERR("failed to allocate new TO body\n");
742
0
                goto error;
743
0
              }
744
2.16k
              to_b = to_b->next;
745
2.16k
              memset(to_b, 0, sizeof(struct to_body));
746
2.16k
              to_b->error=PARSE_OK;
747
2.16k
              saved_status = status = START_TO;
748
2.16k
              end_mark=0;
749
2.16k
              break;
750
45.4k
            } else {
751
45.4k
              goto endofheader;
752
45.4k
            }
753
603
          case F_CRLF:
754
1.15k
          case F_LF:
755
2.26k
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
2.26k
            goto endofheader;
758
40
          default:
759
40
            goto parse_error;
760
62.8k
        }
761
15.0k
        break;
762
5.89M
      default:
763
5.89M
        switch (status)
764
5.89M
        {
765
5.30M
          case START_TO:
766
5.30M
            to_b->uri.s = to_b->body.s = tmp;
767
5.30M
            status = URI_OR_TOKEN;
768
5.30M
            to_b->display.s=tmp;
769
5.30M
            break;
770
7.67k
          case S_URI_ENCLOSED:
771
7.67k
            to_b->uri.s=tmp;
772
7.67k
            status=URI_ENCLOSED;
773
7.67k
            break;
774
1.77k
          case MAYBE_URI_END:
775
2.98k
          case DISPLAY_TOKEN2:
776
2.98k
            status = DISPLAY_TOKEN;
777
14.8k
          case DISPLAY_QUOTED:
778
26.5k
          case DISPLAY_TOKEN:
779
256k
          case URI_ENCLOSED:
780
568k
          case URI_OR_TOKEN:
781
568k
            break;
782
1.86k
          case F_CRLF:
783
15.1k
          case F_LF:
784
17.3k
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
17.3k
            goto endofheader;
787
126
          default:
788
126
            LM_DBG("spitting out [%c] in status %d\n",
789
126
            *tmp,status );
790
126
            goto error;
791
5.89M
        }
792
11.3M
    }/*char switch*/
793
11.3M
  }/*for*/
794
795
75.8k
endofheader:
796
75.8k
  if (to_b->display.len==0) to_b->display.s=0;
797
75.8k
  status=saved_status;
798
75.8k
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
75.8k
  switch(status){
801
20.8k
    case MAYBE_URI_END:
802
20.8k
      to_b->uri.len = end_mark - to_b->uri.s;
803
29.6k
    case END:
804
29.6k
      to_b->body.len = tmp - to_b->body.s;
805
72.8k
    case E_PARA_VALUE:
806
72.8k
      break;
807
3.08k
    default:
808
3.08k
      LM_ERR("unexpected end of header in state %d\n", status);
809
3.08k
      goto error;
810
75.8k
  }
811
812
72.8k
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
72.8k
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
72.8k
  return tmp;
816
817
1.18k
parse_error:
818
1.18k
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
1.18k
      tmp < end? *tmp : *(end-1), status, (int)(tmp-buffer), buffer);
820
4.39k
error:
821
4.39k
  first_b->error=PARSE_ERROR;
822
4.39k
  free_to_params(first_b);
823
4.39k
  free_to(first_b->next);
824
4.39k
  return tmp;
825
826
1.18k
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
76.0k
{
831
76.0k
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
76.0k
}
833
834
835
char* parse_multi_to(char* buffer, char *end, struct to_body *to_b)
836
1.13k
{
837
1.13k
  return _parse_to( buffer, end, to_b, 1/*multi*/);
838
1.13k
}
839
840
841
/**
842
 *
843
 */
844
struct sip_uri *parse_to_uri(struct sip_msg *msg)
845
10.9k
{
846
10.9k
  struct to_body *tb = NULL;
847
10.9k
  if(msg==NULL || msg->to==NULL || msg->to->parsed==NULL)
848
0
    return NULL;
849
850
10.9k
  tb = get_to(msg);
851
852
10.9k
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
853
4.45k
    return &tb->parsed_uri;
854
855
6.49k
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
856
5.01k
  {
857
5.01k
    LM_ERR("failed to parse To uri\n");
858
5.01k
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
859
5.01k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
860
5.01k
    set_err_reply(400, "bad To uri");
861
5.01k
    return NULL;
862
5.01k
  }
863
864
1.48k
  return &tb->parsed_uri;
865
6.49k
}
866
867
int parse_to_header( struct sip_msg *msg)
868
0
{
869
0
  struct to_body* to_b;
870
871
0
  if ( !msg->to && ( parse_headers(msg,HDR_TO_F,0)==-1 || !msg->to)) {
872
0
    LM_ERR("bad msg or missing To header\n");
873
0
    goto error;
874
0
  }
875
876
  /* maybe the header is already parsed! */
877
0
  if (msg->to->parsed)
878
0
    return 0;
879
880
  /* bad luck! :-( - we have to parse it */
881
  /* first, get some memory */
882
0
  to_b = pkg_malloc(sizeof(struct to_body));
883
0
  if (to_b == 0) {
884
0
    LM_ERR("out of pkg_memory\n");
885
0
    goto error;
886
0
  }
887
888
  /* now parse it!! */
889
0
  memset(to_b, 0, sizeof(struct to_body));
890
0
  parse_to(msg->to->body.s,msg->to->body.s+msg->to->body.len+1,to_b);
891
0
  if (to_b->error == PARSE_ERROR) {
892
0
    LM_ERR("bad to header\n");
893
0
    pkg_free(to_b);
894
0
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
895
0
      "error parsing too header");
896
0
    set_err_reply(400, "bad header");
897
0
    goto error;
898
0
  }
899
900
0
  msg->to->parsed = to_b;
901
902
0
  return 0;
903
0
error:
904
0
  return -1;
905
0
}
906
907
/*
908
 * Checks if From includes a To-tag -- good to identify
909
 * if a request creates a new dialog
910
 */
911
int has_totag(struct sip_msg* _m)
912
0
{
913
0
  str tag;
914
915
0
  if (!_m->to && parse_headers(_m, HDR_TO_F,0)==-1) {
916
0
    LM_ERR("To parsing failed\n");
917
0
    return 0;
918
0
  }
919
0
  if (!_m->to) {
920
0
    LM_ERR("no To\n");
921
0
    return 0;
922
0
  }
923
0
  tag=get_to(_m)->tag_value;
924
0
  if (tag.s==0 || tag.len==0) {
925
0
    LM_DBG("no totag\n");
926
0
    return 0;
927
0
  }
928
0
  LM_DBG("totag found\n");
929
0
  return 1;
930
0
}
931
932
/*
933
 * Parses the URI from a generic to_body structure
934
 * Helper function (not specific to TO hdr)
935
 */
936
int parse_to_body_uri(struct to_body *to_b)
937
0
{
938
0
  if (to_b==NULL)
939
0
    return -1;
940
941
0
  if (parse_uri(to_b->uri.s, to_b->uri.len, &to_b->parsed_uri) < 0) {
942
0
    memset( &to_b->parsed_uri, 0, sizeof(struct sip_uri));
943
0
    return -1;
944
0
  }
945
0
  return 0;
946
0
}