Coverage Report

Created: 2025-11-09 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
34.5k
  do{\
56
34.5k
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
34.5k
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
34.5k
    else (_body)->last_param->next=(_param);\
60
34.5k
    (_body)->last_param =(_param);\
61
34.5k
    if ((_param)->type==TAG_PARAM)\
62
34.5k
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
34.5k
    (_param) = 0;\
64
34.5k
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
34.6k
{
70
34.6k
  struct to_param *tp=tb->param_lst;
71
34.6k
  struct to_param *foo;
72
69.2k
  while (tp){
73
34.5k
    foo = tp->next;
74
34.5k
    pkg_free(tp);
75
34.5k
    tp=foo;
76
34.5k
  }
77
78
34.6k
  tb->param_lst = tb->last_param = NULL;
79
34.6k
}
80
81
82
void free_to(struct to_body* tb)
83
68.4k
{
84
68.4k
  if (tb) {
85
33.9k
    free_to( tb->next );
86
33.9k
    free_to_params(tb);
87
33.9k
    pkg_free(tb);
88
33.9k
  }
89
68.4k
}
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
19.2k
{
97
19.2k
  struct to_param *param;
98
19.2k
  int status;
99
19.2k
  int saved_status;
100
19.2k
  char  *tmp;
101
102
19.2k
  param=0;
103
19.2k
  status=E_PARA_VALUE;
104
19.2k
  saved_status=E_PARA_VALUE;
105
220k
  for( tmp=buffer; tmp<end; tmp++)
106
220k
  {
107
220k
    switch(*tmp)
108
220k
    {
109
4.76k
      case ' ':
110
8.96k
      case '\t':
111
8.96k
        switch (status)
112
8.96k
        {
113
327
          case TAG3:
114
327
            param->type=TAG_PARAM;
115
1.86k
          case PARA_NAME:
116
2.28k
          case TAG1:
117
2.49k
          case TAG2:
118
2.49k
            param->name.len = tmp-param->name.s;
119
2.49k
            status = S_EQUAL;
120
2.49k
            break;
121
936
          case PARA_VALUE_TOKEN:
122
936
            param->value.len = tmp-param->value.s;
123
936
            status = E_PARA_VALUE;
124
936
            add_param( param , to_b );
125
936
            break;
126
199
          case F_CRLF:
127
2.41k
          case F_LF:
128
4.50k
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
4.50k
            status=saved_status;
131
4.50k
            break;
132
8.96k
        }
133
8.96k
        break;
134
13.6k
      case '\n':
135
13.6k
        switch (status)
136
13.6k
        {
137
309
          case S_PARA_NAME:
138
636
          case S_EQUAL:
139
1.60k
          case S_PARA_VALUE:
140
2.03k
          case E_PARA_VALUE:
141
2.03k
            saved_status=status;
142
2.03k
            status=F_LF;
143
2.03k
            break;
144
195
          case TAG3:
145
195
            param->type=TAG_PARAM;
146
4.60k
          case PARA_NAME:
147
5.14k
          case TAG1:
148
6.10k
          case TAG2:
149
6.10k
            param->name.len = tmp-param->name.s;
150
6.10k
            saved_status = S_EQUAL;
151
6.10k
            status = F_LF;
152
6.10k
            break;
153
2.17k
          case PARA_VALUE_TOKEN:
154
2.17k
            param->value.len = tmp-param->value.s;
155
2.17k
            saved_status = E_PARA_VALUE;
156
2.17k
            status = F_LF;
157
2.17k
            add_param( param , to_b );
158
2.17k
            break;
159
3.36k
          case F_CR:
160
3.36k
            status=F_CRLF;
161
3.36k
            break;
162
1
          case F_CRLF:
163
4
          case F_LF:
164
4
            status=saved_status;
165
4
            goto endofheader;
166
4
          default:
167
4
            goto parse_error;
168
13.6k
        }
169
13.6k
        break;
170
13.6k
      case '\r':
171
8.96k
        switch (status)
172
8.96k
        {
173
672
          case S_PARA_NAME:
174
875
          case S_EQUAL:
175
1.07k
          case S_PARA_VALUE:
176
1.28k
          case E_PARA_VALUE:
177
1.28k
            saved_status=status;
178
1.28k
            status=F_CR;
179
1.28k
            break;
180
194
          case TAG3:
181
194
            param->type=TAG_PARAM;
182
5.55k
          case PARA_NAME:
183
6.36k
          case TAG1:
184
6.60k
          case TAG2:
185
6.60k
            param->name.len = tmp-param->name.s;
186
6.60k
            saved_status = S_EQUAL;
187
6.60k
            status = F_CR;
188
6.60k
            break;
189
1.07k
          case PARA_VALUE_TOKEN:
190
1.07k
            param->value.len = tmp-param->value.s;
191
1.07k
            saved_status = E_PARA_VALUE;
192
1.07k
            status = F_CR;
193
1.07k
            add_param( param , to_b );
194
1.07k
            break;
195
1
          case F_CRLF:
196
2
          case F_CR:
197
4
          case F_LF:
198
4
            status=saved_status;
199
4
            goto endofheader;
200
2
          default:
201
2
            goto parse_error;
202
8.96k
        }
203
8.96k
        break;
204
8.96k
      case  0:
205
4.41k
      case ',':
206
4.41k
        switch (status)
207
4.41k
        {
208
402
          case PARA_VALUE_QUOTED:
209
402
            break;
210
2.16k
          case PARA_NAME:
211
2.16k
            param->name.len = tmp-param->name.s;
212
2.57k
          case S_EQUAL:
213
2.92k
          case S_PARA_VALUE:
214
2.92k
            if (param->type==TAG_PARAM)
215
1
              goto parse_error;
216
2.92k
            param->value.s = tmp;
217
3.81k
          case PARA_VALUE_TOKEN:
218
3.81k
            status = E_PARA_VALUE;
219
3.81k
            param->value.len = tmp-param->value.s;
220
3.81k
            add_param( param , to_b );
221
4.00k
          case E_PARA_VALUE:
222
4.00k
            saved_status = status;
223
4.00k
            if ( !multi && *tmp==',')
224
5
              goto parse_error;
225
4.00k
            goto endofheader;
226
4.00k
            break;
227
4.00k
          default:
228
3
            goto parse_error;
229
4.41k
        }
230
402
        break;
231
402
      case '\\':
232
203
        switch (status)
233
203
        {
234
200
          case PARA_VALUE_QUOTED:
235
200
            if (tmp+1==end)
236
2
              goto parse_error;
237
198
            switch (*(tmp+1))
238
198
            {
239
2
              case '\r':
240
3
              case '\n':
241
3
                break;
242
195
              default:
243
195
                tmp++;
244
195
                break;
245
198
            }
246
198
            break;
247
198
          default:
248
3
            goto parse_error;
249
203
        }
250
198
        break;
251
3.44k
      case '"':
252
3.44k
        switch (status)
253
3.44k
        {
254
1.23k
          case S_PARA_VALUE:
255
1.23k
            param->value.s = tmp+1;
256
1.23k
            status = PARA_VALUE_QUOTED;
257
1.23k
            break;
258
1.15k
          case PARA_VALUE_QUOTED:
259
1.15k
            param->value.len=tmp-param->value.s ;
260
1.15k
            add_param( param , to_b );
261
1.15k
            status = E_PARA_VALUE;
262
1.15k
            break;
263
194
          case F_CRLF:
264
856
          case F_LF:
265
1.05k
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
1.05k
            goto endofheader;
268
2
          default:
269
2
            goto parse_error;
270
3.44k
        }
271
2.38k
        break;
272
36.6k
      case ';' :
273
36.6k
        switch (status)
274
36.6k
        {
275
195
          case PARA_VALUE_QUOTED:
276
195
            break;
277
7.97k
          case PARA_NAME:
278
7.97k
            param->name.len = tmp-param->name.s;
279
10.0k
          case S_EQUAL:
280
11.5k
          case S_PARA_VALUE:
281
11.5k
            if (param->type==TAG_PARAM)
282
1
              goto parse_error;
283
11.5k
            param->value.s = tmp;
284
13.5k
          case PARA_VALUE_TOKEN:
285
13.5k
            param->value.len=tmp-param->value.s;
286
13.5k
            add_param(param,to_b);
287
35.1k
          case E_PARA_VALUE:
288
35.1k
            param = (struct to_param*)
289
35.1k
              pkg_malloc(sizeof(struct to_param));
290
35.1k
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
35.1k
            memset(param,0,sizeof(struct to_param));
295
35.1k
            param->type=GENERAL_PARAM;
296
35.1k
            status = S_PARA_NAME;
297
35.1k
            break;
298
310
          case F_CRLF:
299
644
          case F_LF:
300
1.24k
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
1.24k
            goto endofheader;
303
5
          default:
304
5
            goto parse_error;
305
36.6k
        }
306
35.3k
        break;
307
35.3k
      case 'T':
308
22.0k
      case 't' :
309
22.0k
        switch (status)
310
22.0k
        {
311
205
          case PARA_VALUE_QUOTED:
312
1.03k
          case PARA_VALUE_TOKEN:
313
2.08k
          case PARA_NAME:
314
2.08k
            break;
315
13.6k
          case S_PARA_NAME:
316
13.6k
            param->name.s = tmp;
317
13.6k
            status = TAG1;
318
13.6k
            break;
319
591
          case S_PARA_VALUE:
320
591
            param->value.s = tmp;
321
591
            status = PARA_VALUE_TOKEN;
322
591
            break;
323
275
          case TAG1:
324
477
          case TAG2:
325
681
          case TAG3:
326
681
            status = PARA_NAME;
327
681
            break;
328
326
          case F_CRLF:
329
3.75k
          case F_LF:
330
5.05k
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
5.05k
            goto endofheader;
333
1
          default:
334
1
            goto parse_error;
335
22.0k
        }
336
17.0k
        break;
337
17.0k
      case 'A':
338
31.1k
      case 'a' :
339
31.1k
        switch (status)
340
31.1k
        {
341
198
          case PARA_VALUE_QUOTED:
342
1.53k
          case PARA_VALUE_TOKEN:
343
16.3k
          case PARA_NAME:
344
16.3k
            break;
345
3.23k
          case S_PARA_NAME:
346
3.23k
            param->name.s = tmp;
347
3.23k
            status = PARA_NAME;
348
3.23k
            break;
349
389
          case S_PARA_VALUE:
350
389
            param->value.s = tmp;
351
389
            status = PARA_VALUE_TOKEN;
352
389
            break;
353
7.96k
          case TAG1:
354
7.96k
            status = TAG2;
355
7.96k
            break;
356
826
          case TAG2:
357
1.16k
          case TAG3:
358
1.16k
            status = PARA_NAME;
359
1.16k
            break;
360
1.22k
          case F_CRLF:
361
1.81k
          case F_LF:
362
2.03k
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
2.03k
            goto endofheader;
365
2
          default:
366
2
            goto parse_error;
367
31.1k
        }
368
29.1k
        break;
369
29.1k
      case 'G':
370
15.0k
      case 'g' :
371
15.0k
        switch (status)
372
15.0k
        {
373
195
          case PARA_VALUE_QUOTED:
374
1.25k
          case PARA_VALUE_TOKEN:
375
2.58k
          case PARA_NAME:
376
2.58k
            break;
377
2.64k
          case S_PARA_NAME:
378
2.64k
            param->name.s = tmp;
379
2.64k
            status = PARA_NAME;
380
2.64k
            break;
381
1.75k
          case S_PARA_VALUE:
382
1.75k
            param->value.s = tmp;
383
1.75k
            status = PARA_VALUE_TOKEN;
384
1.75k
            break;
385
555
          case TAG1:
386
1.21k
          case TAG3:
387
1.21k
            status = PARA_NAME;
388
1.21k
            break;
389
5.05k
          case TAG2:
390
5.05k
            status = TAG3;
391
5.05k
            break;
392
272
          case F_CRLF:
393
1.56k
          case F_LF:
394
1.84k
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
1.84k
            goto endofheader;
397
2
          default:
398
2
            goto parse_error;
399
15.0k
        }
400
13.2k
        break;
401
13.2k
      case '=':
402
11.9k
        switch (status)
403
11.9k
        {
404
194
          case PARA_VALUE_QUOTED:
405
194
            break;
406
2.92k
          case TAG3:
407
2.92k
            param->type=TAG_PARAM;
408
7.16k
          case PARA_NAME:
409
9.24k
          case TAG1:
410
9.45k
          case TAG2:
411
9.45k
            param->name.len = tmp-param->name.s;
412
9.45k
            status = S_PARA_VALUE;
413
9.45k
            break;
414
1.53k
          case S_EQUAL:
415
1.53k
            status = S_PARA_VALUE;
416
1.53k
            break;
417
194
          case F_CRLF:
418
566
          case F_LF:
419
769
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
769
            goto endofheader;
422
2
          default:
423
2
            goto parse_error;
424
11.9k
        }
425
11.1k
        break;
426
63.7k
      default:
427
63.7k
        switch (status)
428
63.7k
        {
429
1.03k
          case TAG1:
430
1.30k
          case TAG2:
431
1.51k
          case TAG3:
432
1.51k
            status = PARA_NAME;
433
1.51k
            break;
434
13.3k
          case PARA_VALUE_TOKEN:
435
39.5k
          case PARA_NAME:
436
39.8k
          case PARA_VALUE_QUOTED:
437
39.8k
            break;
438
15.3k
          case S_PARA_NAME:
439
15.3k
            param->name.s = tmp;
440
15.3k
            status = PARA_NAME;
441
15.3k
            break;
442
4.44k
          case S_PARA_VALUE:
443
4.44k
            param->value.s = tmp;
444
4.44k
            status = PARA_VALUE_TOKEN;
445
4.44k
            break;
446
638
          case F_CRLF:
447
1.95k
          case F_LF:
448
2.57k
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
2.57k
            goto endofheader;
451
6
          default:
452
6
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
6
            goto error;
454
63.7k
        }
455
220k
    }/*switch*/
456
220k
  }/*for*/
457
458
626
  if (status==PARA_VALUE_QUOTED) {
459
65
      LM_ERR("unexpected end of header in state %d\n", status);
460
65
      goto parse_error;
461
65
  }
462
463
19.1k
endofheader:
464
19.1k
  LM_DBG("end of header reached, state=%d\n", status);
465
19.1k
  if (param) {
466
12.3k
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
11.8k
      saved_status = E_PARA_VALUE;
468
11.8k
      param->value.s= 0;
469
11.8k
      param->value.len=0;
470
11.8k
      if (param->type==TAG_PARAM)
471
16
        goto parse_error;
472
11.8k
      add_param(param, to_b);
473
11.8k
    } else {
474
491
      pkg_free(param);
475
491
    }
476
12.3k
  }
477
19.1k
  *returned_status=saved_status;
478
19.1k
  return tmp;
479
480
116
parse_error:
481
116
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
116
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
122
error:
484
122
  if (param) pkg_free(param);
485
122
  free_to_params(to_b);
486
122
  to_b->error=PARSE_ERROR;
487
122
  *returned_status = status;
488
122
  return tmp;
489
116
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
34.5k
{
497
34.5k
  int status;
498
34.5k
  int saved_status;
499
34.5k
  char  *tmp;
500
34.5k
  char  *end_mark;
501
34.5k
  struct to_body *first_b = to_b;
502
503
34.5k
  status=START_TO;
504
34.5k
  saved_status=START_TO;
505
34.5k
  memset(to_b, 0, sizeof(struct to_body));
506
34.5k
  to_b->error=PARSE_OK;
507
34.5k
  end_mark=0;
508
509
207k
  for( tmp=buffer; tmp<end; tmp++)
510
207k
  {
511
207k
    switch(*tmp)
512
207k
    {
513
5.28k
      case ' ':
514
7.65k
      case '\t':
515
7.65k
        switch (status)
516
7.65k
        {
517
362
          case F_CRLF:
518
1.51k
          case F_LF:
519
2.65k
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
2.65k
            status=saved_status;
522
2.65k
            break;
523
225
          case URI_ENCLOSED:
524
225
            to_b->uri.len = tmp - to_b->uri.s;
525
225
            status = E_URI_ENCLOSED;
526
225
            break;
527
2.73k
          case URI_OR_TOKEN:
528
2.73k
            status = MAYBE_URI_END;
529
2.73k
            end_mark = tmp;
530
2.73k
            break;
531
651
          case DISPLAY_TOKEN:
532
651
            end_mark = tmp;
533
651
            status = DISPLAY_TOKEN2;
534
651
            break;
535
7.65k
        }
536
7.65k
        break;
537
12.6k
      case '\n':
538
12.6k
        switch (status)
539
12.6k
        {
540
4.55k
          case URI_OR_TOKEN:
541
4.55k
            end_mark = tmp;
542
4.55k
            status = MAYBE_URI_END;
543
5.49k
          case MAYBE_URI_END:
544
5.68k
          case DISPLAY_TOKEN:
545
5.88k
          case DISPLAY_TOKEN2:
546
6.08k
          case E_DISPLAY_QUOTED:
547
7.65k
          case END:
548
7.65k
            saved_status=status;
549
7.65k
            status=F_LF;
550
7.65k
            break;
551
4.98k
          case F_CR:
552
4.98k
            status=F_CRLF;
553
4.98k
            break;
554
1
          case F_CRLF:
555
2
          case F_LF:
556
2
            status=saved_status;
557
2
            goto endofheader;
558
10
          default:
559
10
            goto parse_error;
560
12.6k
        }
561
12.6k
        break;
562
12.6k
      case '\r':
563
8.43k
        switch (status)
564
8.43k
        {
565
6.90k
          case URI_OR_TOKEN:
566
6.90k
            end_mark = tmp;
567
6.90k
            status = MAYBE_URI_END;
568
            /* fall through */
569
7.61k
          case MAYBE_URI_END:
570
7.81k
          case DISPLAY_TOKEN:
571
8.01k
          case DISPLAY_TOKEN2:
572
8.20k
          case E_DISPLAY_QUOTED:
573
8.42k
          case END:
574
8.42k
            saved_status=status;
575
8.42k
            status=F_CR;
576
8.42k
            break;
577
1
          case F_CRLF:
578
2
          case F_CR:
579
3
          case F_LF:
580
3
            status=saved_status;
581
3
            goto endofheader;
582
6
          default:
583
6
            goto parse_error;
584
8.43k
        }
585
8.42k
        break;
586
8.42k
      case 0:
587
1.64k
        switch (status)
588
1.64k
        {
589
1.12k
          case URI_OR_TOKEN:
590
1.43k
          case MAYBE_URI_END:
591
1.43k
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
1.63k
          case END:
594
1.63k
            saved_status = status = END;
595
1.63k
            goto endofheader;
596
13
          default:
597
13
            goto parse_error;
598
1.64k
        }
599
0
        break;
600
709
      case ',':
601
709
        switch (status)
602
709
        {
603
202
          case DISPLAY_QUOTED:
604
402
          case URI_ENCLOSED:
605
402
            break;
606
302
          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
302
            if (multi==0)
616
302
              break;
617
2
          case MAYBE_URI_END:
618
2
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
4
          case END:
621
4
            if (multi==0)
622
4
              goto parse_error;
623
0
            to_b->next = (struct to_body*)
624
0
              pkg_malloc(sizeof(struct to_body));
625
0
            if (to_b->next==NULL) {
626
0
              LM_ERR("failed to allocate new TO body\n");
627
0
              goto error;
628
0
            }
629
0
            to_b = to_b->next;
630
0
            memset(to_b, 0, sizeof(struct to_body));
631
0
            to_b->error = PARSE_OK;
632
0
            saved_status = status = START_TO;
633
0
            end_mark=0;
634
0
            break;
635
1
          default:
636
1
            goto parse_error;
637
709
        }
638
704
        break;
639
704
      case '\\':
640
242
        switch (status)
641
242
        {
642
239
          case DISPLAY_QUOTED:
643
239
            tmp++; /* jump over next char */
644
239
            break;
645
3
          default:
646
3
            goto parse_error;
647
242
        }
648
239
        break;
649
2.72k
      case '<':
650
2.72k
        switch (status)
651
2.72k
        {
652
662
          case START_TO:
653
662
            to_b->body.s=tmp;
654
662
            status = S_URI_ENCLOSED;
655
662
            break;
656
233
          case DISPLAY_QUOTED:
657
233
            break;
658
201
          case E_DISPLAY_QUOTED:
659
201
            status = S_URI_ENCLOSED;
660
201
            break;
661
412
          case URI_OR_TOKEN:
662
642
          case DISPLAY_TOKEN:
663
642
            end_mark = tmp;
664
            /* fall through */
665
838
          case DISPLAY_TOKEN2:
666
1.03k
          case MAYBE_URI_END:
667
1.03k
            to_b->display.len=end_mark-to_b->display.s;
668
1.03k
            status = S_URI_ENCLOSED;
669
1.03k
            break;
670
199
          case F_CRLF:
671
393
          case F_LF:
672
588
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
588
            goto endofheader;
675
1
          default:
676
1
            goto parse_error;
677
2.72k
        }
678
2.13k
        break;
679
4.09k
      case '>':
680
4.09k
        switch (status)
681
4.09k
        {
682
213
          case DISPLAY_QUOTED:
683
213
            break;
684
1.60k
          case URI_ENCLOSED:
685
1.60k
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
1.82k
          case E_URI_ENCLOSED:
688
1.82k
            status = END;
689
1.82k
            break;
690
1.27k
          case F_CRLF:
691
1.58k
          case F_LF:
692
2.05k
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
2.05k
            goto endofheader;
695
4
          default:
696
4
            goto parse_error;
697
4.09k
        }
698
2.03k
        break;
699
2.03k
      case '"':
700
1.28k
        switch (status)
701
1.28k
        {
702
268
          case START_TO:
703
268
            to_b->body.s = tmp;
704
268
            to_b->display.s = tmp;
705
268
            status = DISPLAY_QUOTED;
706
268
            break;
707
226
          case DISPLAY_QUOTED:
708
226
            status = E_DISPLAY_QUOTED;
709
226
            to_b->display.len = tmp-to_b->display.s+1;
710
226
            break;
711
195
          case F_CRLF:
712
590
          case F_LF:
713
784
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
784
            goto endofheader;
716
4
          default:
717
4
            goto parse_error;
718
1.28k
        }
719
494
        break;
720
21.0k
      case ';' :
721
21.0k
        switch (status)
722
21.0k
        {
723
209
          case DISPLAY_QUOTED:
724
617
          case DISPLAY_TOKEN:
725
814
          case URI_ENCLOSED:
726
814
            break;
727
17.8k
          case URI_OR_TOKEN:
728
17.8k
            end_mark = tmp;
729
            /* fall through */
730
19.0k
          case MAYBE_URI_END:
731
19.0k
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
19.2k
          case END:
734
19.2k
            to_b->body.len = tmp-to_b->body.s;
735
19.2k
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
19.2k
            if (to_b->error!=PARSE_ERROR && multi && *tmp==',') {
737
              /* continue with a new body instance */
738
0
              to_b->next = (struct to_body*)
739
0
                pkg_malloc(sizeof(struct to_body));
740
0
              if (to_b->next==NULL) {
741
0
                LM_ERR("failed to allocate new TO body\n");
742
0
                goto error;
743
0
              }
744
0
              to_b = to_b->next;
745
0
              memset(to_b, 0, sizeof(struct to_body));
746
0
              to_b->error=PARSE_OK;
747
0
              saved_status = status = START_TO;
748
0
              end_mark=0;
749
0
              break;
750
19.2k
            } else {
751
19.2k
              goto endofheader;
752
19.2k
            }
753
194
          case F_CRLF:
754
402
          case F_LF:
755
970
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
970
            goto endofheader;
758
1
          default:
759
1
            goto parse_error;
760
21.0k
        }
761
814
        break;
762
146k
      default:
763
146k
        switch (status)
764
146k
        {
765
33.5k
          case START_TO:
766
33.5k
            to_b->uri.s = to_b->body.s = tmp;
767
33.5k
            status = URI_OR_TOKEN;
768
33.5k
            to_b->display.s=tmp;
769
33.5k
            break;
770
1.85k
          case S_URI_ENCLOSED:
771
1.85k
            to_b->uri.s=tmp;
772
1.85k
            status=URI_ENCLOSED;
773
1.85k
            break;
774
494
          case MAYBE_URI_END:
775
928
          case DISPLAY_TOKEN2:
776
928
            status = DISPLAY_TOKEN;
777
7.51k
          case DISPLAY_QUOTED:
778
8.65k
          case DISPLAY_TOKEN:
779
9.10k
          case URI_ENCLOSED:
780
102k
          case URI_OR_TOKEN:
781
102k
            break;
782
2.76k
          case F_CRLF:
783
8.10k
          case F_LF:
784
8.94k
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
8.94k
            goto endofheader;
787
3
          default:
788
3
            LM_DBG("spitting out [%c] in status %d\n",
789
3
            *tmp,status );
790
3
            goto error;
791
146k
        }
792
207k
    }/*char switch*/
793
207k
  }/*for*/
794
795
34.4k
endofheader:
796
34.4k
  if (to_b->display.len==0) to_b->display.s=0;
797
34.4k
  status=saved_status;
798
34.4k
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
34.4k
  switch(status){
801
11.9k
    case MAYBE_URI_END:
802
11.9k
      to_b->uri.len = end_mark - to_b->uri.s;
803
15.0k
    case END:
804
15.0k
      to_b->body.len = tmp - to_b->body.s;
805
33.9k
    case E_PARA_VALUE:
806
33.9k
      break;
807
516
    default:
808
516
      LM_ERR("unexpected end of header in state %d\n", status);
809
516
      goto error;
810
34.4k
  }
811
812
33.9k
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
33.9k
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
33.9k
  return tmp;
816
817
47
parse_error:
818
47
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
47
      tmp < end? *tmp : *(end-1), status, (int)(tmp-buffer), buffer);
820
566
error:
821
566
  first_b->error=PARSE_ERROR;
822
566
  free_to_params(first_b);
823
566
  free_to(first_b->next);
824
566
  return tmp;
825
826
47
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
34.5k
{
831
34.5k
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
34.5k
}
833
834
835
char* parse_multi_to(char* buffer, char *end, struct to_body *to_b)
836
0
{
837
0
  return _parse_to( buffer, end, to_b, 1/*multi*/);
838
0
}
839
840
841
/**
842
 *
843
 */
844
struct sip_uri *parse_to_uri(struct sip_msg *msg)
845
0
{
846
0
  struct to_body *tb = NULL;
847
0
  if(msg==NULL || msg->to==NULL || msg->to->parsed==NULL)
848
0
    return NULL;
849
850
0
  tb = get_to(msg);
851
852
0
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
853
0
    return &tb->parsed_uri;
854
855
0
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
856
0
  {
857
0
    LM_ERR("failed to parse To uri\n");
858
0
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
859
0
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
860
0
    set_err_reply(400, "bad To uri");
861
0
    return NULL;
862
0
  }
863
864
0
  return &tb->parsed_uri;
865
0
}
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
}