Coverage Report

Created: 2025-07-11 06:28

/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
0
  do{\
56
0
    LM_DBG("%.*s=%.*s\n",param->name.len,ZSW(param->name.s),\
57
0
      param->value.len,ZSW(param->value.s));\
58
0
    if (!(_body)->param_lst)  (_body)->param_lst=(_param);\
59
0
    else (_body)->last_param->next=(_param);\
60
0
    (_body)->last_param =(_param);\
61
0
    if ((_param)->type==TAG_PARAM)\
62
0
      memcpy(&((_body)->tag_value),&((_param)->value),sizeof(str));\
63
0
    (_param) = 0;\
64
0
  }while(0);
65
66
67
68
void free_to_params(struct to_body* tb)
69
0
{
70
0
  struct to_param *tp=tb->param_lst;
71
0
  struct to_param *foo;
72
0
  while (tp){
73
0
    foo = tp->next;
74
0
    pkg_free(tp);
75
0
    tp=foo;
76
0
  }
77
78
0
  tb->param_lst = tb->last_param = NULL;
79
0
}
80
81
82
void free_to(struct to_body* tb)
83
0
{
84
0
  if (tb) {
85
0
    free_to( tb->next );
86
0
    free_to_params(tb);
87
0
    pkg_free(tb);
88
0
  }
89
0
}
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
0
{
97
0
  struct to_param *param;
98
0
  int status;
99
0
  int saved_status;
100
0
  char  *tmp;
101
102
0
  param=0;
103
0
  status=E_PARA_VALUE;
104
0
  saved_status=E_PARA_VALUE;
105
0
  for( tmp=buffer; tmp<end; tmp++)
106
0
  {
107
0
    switch(*tmp)
108
0
    {
109
0
      case ' ':
110
0
      case '\t':
111
0
        switch (status)
112
0
        {
113
0
          case TAG3:
114
0
            param->type=TAG_PARAM;
115
0
          case PARA_NAME:
116
0
          case TAG1:
117
0
          case TAG2:
118
0
            param->name.len = tmp-param->name.s;
119
0
            status = S_EQUAL;
120
0
            break;
121
0
          case PARA_VALUE_TOKEN:
122
0
            param->value.len = tmp-param->value.s;
123
0
            status = E_PARA_VALUE;
124
0
            add_param( param , to_b );
125
0
            break;
126
0
          case F_CRLF:
127
0
          case F_LF:
128
0
          case F_CR:
129
            /*previous=crlf and now =' '*/
130
0
            status=saved_status;
131
0
            break;
132
0
        }
133
0
        break;
134
0
      case '\n':
135
0
        switch (status)
136
0
        {
137
0
          case S_PARA_NAME:
138
0
          case S_EQUAL:
139
0
          case S_PARA_VALUE:
140
0
          case E_PARA_VALUE:
141
0
            saved_status=status;
142
0
            status=F_LF;
143
0
            break;
144
0
          case TAG3:
145
0
            param->type=TAG_PARAM;
146
0
          case PARA_NAME:
147
0
          case TAG1:
148
0
          case TAG2:
149
0
            param->name.len = tmp-param->name.s;
150
0
            saved_status = S_EQUAL;
151
0
            status = F_LF;
152
0
            break;
153
0
          case PARA_VALUE_TOKEN:
154
0
            param->value.len = tmp-param->value.s;
155
0
            saved_status = E_PARA_VALUE;
156
0
            status = F_LF;
157
0
            add_param( param , to_b );
158
0
            break;
159
0
          case F_CR:
160
0
            status=F_CRLF;
161
0
            break;
162
0
          case F_CRLF:
163
0
          case F_LF:
164
0
            status=saved_status;
165
0
            goto endofheader;
166
0
          default:
167
0
            goto parse_error;
168
0
        }
169
0
        break;
170
0
      case '\r':
171
0
        switch (status)
172
0
        {
173
0
          case S_PARA_NAME:
174
0
          case S_EQUAL:
175
0
          case S_PARA_VALUE:
176
0
          case E_PARA_VALUE:
177
0
            saved_status=status;
178
0
            status=F_CR;
179
0
            break;
180
0
          case TAG3:
181
0
            param->type=TAG_PARAM;
182
0
          case PARA_NAME:
183
0
          case TAG1:
184
0
          case TAG2:
185
0
            param->name.len = tmp-param->name.s;
186
0
            saved_status = S_EQUAL;
187
0
            status = F_CR;
188
0
            break;
189
0
          case PARA_VALUE_TOKEN:
190
0
            param->value.len = tmp-param->value.s;
191
0
            saved_status = E_PARA_VALUE;
192
0
            status = F_CR;
193
0
            add_param( param , to_b );
194
0
            break;
195
0
          case F_CRLF:
196
0
          case F_CR:
197
0
          case F_LF:
198
0
            status=saved_status;
199
0
            goto endofheader;
200
0
          default:
201
0
            goto parse_error;
202
0
        }
203
0
        break;
204
0
      case  0:
205
0
      case ',':
206
0
        switch (status)
207
0
        {
208
0
          case PARA_VALUE_QUOTED:
209
0
            break;
210
0
          case PARA_NAME:
211
0
            param->name.len = tmp-param->name.s;
212
0
          case S_EQUAL:
213
0
          case S_PARA_VALUE:
214
0
            if (param->type==TAG_PARAM)
215
0
              goto parse_error;
216
0
            param->value.s = tmp;
217
0
          case PARA_VALUE_TOKEN:
218
0
            status = E_PARA_VALUE;
219
0
            param->value.len = tmp-param->value.s;
220
0
            add_param( param , to_b );
221
0
          case E_PARA_VALUE:
222
0
            saved_status = status;
223
0
            if ( !multi && *tmp==',')
224
0
              goto parse_error;
225
0
            goto endofheader;
226
0
            break;
227
0
          default:
228
0
            goto parse_error;
229
0
        }
230
0
        break;
231
0
      case '\\':
232
0
        switch (status)
233
0
        {
234
0
          case PARA_VALUE_QUOTED:
235
0
            if (tmp+1==end)
236
0
              goto parse_error;
237
0
            switch (*(tmp+1))
238
0
            {
239
0
              case '\r':
240
0
              case '\n':
241
0
                break;
242
0
              default:
243
0
                tmp++;
244
0
                break;
245
0
            }
246
0
            break;
247
0
          default:
248
0
            goto parse_error;
249
0
        }
250
0
        break;
251
0
      case '"':
252
0
        switch (status)
253
0
        {
254
0
          case S_PARA_VALUE:
255
0
            param->value.s = tmp+1;
256
0
            status = PARA_VALUE_QUOTED;
257
0
            break;
258
0
          case PARA_VALUE_QUOTED:
259
0
            param->value.len=tmp-param->value.s ;
260
0
            add_param( param , to_b );
261
0
            status = E_PARA_VALUE;
262
0
            break;
263
0
          case F_CRLF:
264
0
          case F_LF:
265
0
          case F_CR:
266
            /*previous=crlf and now !=' '*/
267
0
            goto endofheader;
268
0
          default:
269
0
            goto parse_error;
270
0
        }
271
0
        break;
272
0
      case ';' :
273
0
        switch (status)
274
0
        {
275
0
          case PARA_VALUE_QUOTED:
276
0
            break;
277
0
          case PARA_NAME:
278
0
            param->name.len = tmp-param->name.s;
279
0
          case S_EQUAL:
280
0
          case S_PARA_VALUE:
281
0
            if (param->type==TAG_PARAM)
282
0
              goto parse_error;
283
0
            param->value.s = tmp;
284
0
          case PARA_VALUE_TOKEN:
285
0
            param->value.len=tmp-param->value.s;
286
0
            add_param(param,to_b);
287
0
          case E_PARA_VALUE:
288
0
            param = (struct to_param*)
289
0
              pkg_malloc(sizeof(struct to_param));
290
0
            if (!param){
291
0
              LM_ERR("out of pkg memory\n" );
292
0
              goto error;
293
0
            }
294
0
            memset(param,0,sizeof(struct to_param));
295
0
            param->type=GENERAL_PARAM;
296
0
            status = S_PARA_NAME;
297
0
            break;
298
0
          case F_CRLF:
299
0
          case F_LF:
300
0
          case F_CR:
301
            /*previous=crlf and now !=' '*/
302
0
            goto endofheader;
303
0
          default:
304
0
            goto parse_error;
305
0
        }
306
0
        break;
307
0
      case 'T':
308
0
      case 't' :
309
0
        switch (status)
310
0
        {
311
0
          case PARA_VALUE_QUOTED:
312
0
          case PARA_VALUE_TOKEN:
313
0
          case PARA_NAME:
314
0
            break;
315
0
          case S_PARA_NAME:
316
0
            param->name.s = tmp;
317
0
            status = TAG1;
318
0
            break;
319
0
          case S_PARA_VALUE:
320
0
            param->value.s = tmp;
321
0
            status = PARA_VALUE_TOKEN;
322
0
            break;
323
0
          case TAG1:
324
0
          case TAG2:
325
0
          case TAG3:
326
0
            status = PARA_NAME;
327
0
            break;
328
0
          case F_CRLF:
329
0
          case F_LF:
330
0
          case F_CR:
331
            /*previous=crlf and now !=' '*/
332
0
            goto endofheader;
333
0
          default:
334
0
            goto parse_error;
335
0
        }
336
0
        break;
337
0
      case 'A':
338
0
      case 'a' :
339
0
        switch (status)
340
0
        {
341
0
          case PARA_VALUE_QUOTED:
342
0
          case PARA_VALUE_TOKEN:
343
0
          case PARA_NAME:
344
0
            break;
345
0
          case S_PARA_NAME:
346
0
            param->name.s = tmp;
347
0
            status = PARA_NAME;
348
0
            break;
349
0
          case S_PARA_VALUE:
350
0
            param->value.s = tmp;
351
0
            status = PARA_VALUE_TOKEN;
352
0
            break;
353
0
          case TAG1:
354
0
            status = TAG2;
355
0
            break;
356
0
          case TAG2:
357
0
          case TAG3:
358
0
            status = PARA_NAME;
359
0
            break;
360
0
          case F_CRLF:
361
0
          case F_LF:
362
0
          case F_CR:
363
            /*previous=crlf and now !=' '*/
364
0
            goto endofheader;
365
0
          default:
366
0
            goto parse_error;
367
0
        }
368
0
        break;
369
0
      case 'G':
370
0
      case 'g' :
371
0
        switch (status)
372
0
        {
373
0
          case PARA_VALUE_QUOTED:
374
0
          case PARA_VALUE_TOKEN:
375
0
          case PARA_NAME:
376
0
            break;
377
0
          case S_PARA_NAME:
378
0
            param->name.s = tmp;
379
0
            status = PARA_NAME;
380
0
            break;
381
0
          case S_PARA_VALUE:
382
0
            param->value.s = tmp;
383
0
            status = PARA_VALUE_TOKEN;
384
0
            break;
385
0
          case TAG1:
386
0
          case TAG3:
387
0
            status = PARA_NAME;
388
0
            break;
389
0
          case TAG2:
390
0
            status = TAG3;
391
0
            break;
392
0
          case F_CRLF:
393
0
          case F_LF:
394
0
          case F_CR:
395
            /*previous=crlf and now !=' '*/
396
0
            goto endofheader;
397
0
          default:
398
0
            goto parse_error;
399
0
        }
400
0
        break;
401
0
      case '=':
402
0
        switch (status)
403
0
        {
404
0
          case PARA_VALUE_QUOTED:
405
0
            break;
406
0
          case TAG3:
407
0
            param->type=TAG_PARAM;
408
0
          case PARA_NAME:
409
0
          case TAG1:
410
0
          case TAG2:
411
0
            param->name.len = tmp-param->name.s;
412
0
            status = S_PARA_VALUE;
413
0
            break;
414
0
          case S_EQUAL:
415
0
            status = S_PARA_VALUE;
416
0
            break;
417
0
          case F_CRLF:
418
0
          case F_LF:
419
0
          case F_CR:
420
            /*previous=crlf and now !=' '*/
421
0
            goto endofheader;
422
0
          default:
423
0
            goto parse_error;
424
0
        }
425
0
        break;
426
0
      default:
427
0
        switch (status)
428
0
        {
429
0
          case TAG1:
430
0
          case TAG2:
431
0
          case TAG3:
432
0
            status = PARA_NAME;
433
0
            break;
434
0
          case PARA_VALUE_TOKEN:
435
0
          case PARA_NAME:
436
0
          case PARA_VALUE_QUOTED:
437
0
            break;
438
0
          case S_PARA_NAME:
439
0
            param->name.s = tmp;
440
0
            status = PARA_NAME;
441
0
            break;
442
0
          case S_PARA_VALUE:
443
0
            param->value.s = tmp;
444
0
            status = PARA_VALUE_TOKEN;
445
0
            break;
446
0
          case F_CRLF:
447
0
          case F_LF:
448
0
          case F_CR:
449
            /*previous=crlf and now !=' '*/
450
0
            goto endofheader;
451
0
          default:
452
0
            LM_ERR("spitting out [%c] in status %d\n",*tmp,status );
453
0
            goto error;
454
0
        }
455
0
    }/*switch*/
456
0
  }/*for*/
457
458
0
  if (status==PARA_VALUE_QUOTED) {
459
0
      LM_ERR("unexpected end of header in state %d\n", status);
460
0
      goto parse_error;
461
0
  }
462
463
0
endofheader:
464
0
  LM_DBG("end of header reached, state=%d\n", status);
465
0
  if (param) {
466
0
    if (saved_status==S_EQUAL||saved_status==S_PARA_VALUE) {
467
0
      saved_status = E_PARA_VALUE;
468
0
      param->value.s= 0;
469
0
      param->value.len=0;
470
0
      if (param->type==TAG_PARAM)
471
0
        goto parse_error;
472
0
      add_param(param, to_b);
473
0
    } else {
474
0
      pkg_free(param);
475
0
    }
476
0
  }
477
0
  *returned_status=saved_status;
478
0
  return tmp;
479
480
0
parse_error:
481
0
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
482
0
      tmp < end? *tmp : *(end-1),status, (int)(tmp-buffer), ZSW(buffer));
483
0
error:
484
0
  if (param) pkg_free(param);
485
0
  free_to_params(to_b);
486
0
  to_b->error=PARSE_ERROR;
487
0
  *returned_status = status;
488
0
  return tmp;
489
0
}
490
491
492
493
494
static inline char* _parse_to(char* buffer, char *end, struct to_body *to_b,
495
                                  int multi)
496
0
{
497
0
  int status;
498
0
  int saved_status;
499
0
  char  *tmp;
500
0
  char  *end_mark;
501
0
  struct to_body *first_b = to_b;
502
503
0
  status=START_TO;
504
0
  saved_status=START_TO;
505
0
  memset(to_b, 0, sizeof(struct to_body));
506
0
  to_b->error=PARSE_OK;
507
0
  end_mark=0;
508
509
0
  for( tmp=buffer; tmp<end; tmp++)
510
0
  {
511
0
    switch(*tmp)
512
0
    {
513
0
      case ' ':
514
0
      case '\t':
515
0
        switch (status)
516
0
        {
517
0
          case F_CRLF:
518
0
          case F_LF:
519
0
          case F_CR:
520
            /*previous=crlf and now =' '*/
521
0
            status=saved_status;
522
0
            break;
523
0
          case URI_ENCLOSED:
524
0
            to_b->uri.len = tmp - to_b->uri.s;
525
0
            status = E_URI_ENCLOSED;
526
0
            break;
527
0
          case URI_OR_TOKEN:
528
0
            status = MAYBE_URI_END;
529
0
            end_mark = tmp;
530
0
            break;
531
0
          case DISPLAY_TOKEN:
532
0
            end_mark = tmp;
533
0
            status = DISPLAY_TOKEN2;
534
0
            break;
535
0
        }
536
0
        break;
537
0
      case '\n':
538
0
        switch (status)
539
0
        {
540
0
          case URI_OR_TOKEN:
541
0
            end_mark = tmp;
542
0
            status = MAYBE_URI_END;
543
0
          case MAYBE_URI_END:
544
0
          case DISPLAY_TOKEN:
545
0
          case DISPLAY_TOKEN2:
546
0
          case E_DISPLAY_QUOTED:
547
0
          case END:
548
0
            saved_status=status;
549
0
            status=F_LF;
550
0
            break;
551
0
          case F_CR:
552
0
            status=F_CRLF;
553
0
            break;
554
0
          case F_CRLF:
555
0
          case F_LF:
556
0
            status=saved_status;
557
0
            goto endofheader;
558
0
          default:
559
0
            goto parse_error;
560
0
        }
561
0
        break;
562
0
      case '\r':
563
0
        switch (status)
564
0
        {
565
0
          case URI_OR_TOKEN:
566
0
            end_mark = tmp;
567
0
            status = MAYBE_URI_END;
568
            /* fall through */
569
0
          case MAYBE_URI_END:
570
0
          case DISPLAY_TOKEN:
571
0
          case DISPLAY_TOKEN2:
572
0
          case E_DISPLAY_QUOTED:
573
0
          case END:
574
0
            saved_status=status;
575
0
            status=F_CR;
576
0
            break;
577
0
          case F_CRLF:
578
0
          case F_CR:
579
0
          case F_LF:
580
0
            status=saved_status;
581
0
            goto endofheader;
582
0
          default:
583
0
            goto parse_error;
584
0
        }
585
0
        break;
586
0
      case 0:
587
0
        switch (status)
588
0
        {
589
0
          case URI_OR_TOKEN:
590
0
          case MAYBE_URI_END:
591
0
            to_b->uri.len = tmp - to_b->uri.s;
592
            /* fall through */
593
0
          case END:
594
0
            saved_status = status = END;
595
0
            goto endofheader;
596
0
          default:
597
0
            goto parse_error;
598
0
        }
599
0
        break;
600
0
      case ',':
601
0
        switch (status)
602
0
        {
603
0
          case DISPLAY_QUOTED:
604
0
          case URI_ENCLOSED:
605
0
            break;
606
0
          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
0
            if (multi==0)
616
0
              break;
617
0
          case MAYBE_URI_END:
618
0
            to_b->uri.len = tmp - to_b->uri.s;
619
            /* fall through */
620
0
          case END:
621
0
            if (multi==0)
622
0
              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
0
          default:
636
0
            goto parse_error;
637
0
        }
638
0
        break;
639
0
      case '\\':
640
0
        switch (status)
641
0
        {
642
0
          case DISPLAY_QUOTED:
643
0
            tmp++; /* jump over next char */
644
0
            break;
645
0
          default:
646
0
            goto parse_error;
647
0
        }
648
0
        break;
649
0
      case '<':
650
0
        switch (status)
651
0
        {
652
0
          case START_TO:
653
0
            to_b->body.s=tmp;
654
0
            status = S_URI_ENCLOSED;
655
0
            break;
656
0
          case DISPLAY_QUOTED:
657
0
            break;
658
0
          case E_DISPLAY_QUOTED:
659
0
            status = S_URI_ENCLOSED;
660
0
            break;
661
0
          case URI_OR_TOKEN:
662
0
          case DISPLAY_TOKEN:
663
0
            end_mark = tmp;
664
            /* fall through */
665
0
          case DISPLAY_TOKEN2:
666
0
          case MAYBE_URI_END:
667
0
            to_b->display.len=end_mark-to_b->display.s;
668
0
            status = S_URI_ENCLOSED;
669
0
            break;
670
0
          case F_CRLF:
671
0
          case F_LF:
672
0
          case F_CR:
673
            /*previous=crlf and now !=' '*/
674
0
            goto endofheader;
675
0
          default:
676
0
            goto parse_error;
677
0
        }
678
0
        break;
679
0
      case '>':
680
0
        switch (status)
681
0
        {
682
0
          case DISPLAY_QUOTED:
683
0
            break;
684
0
          case URI_ENCLOSED:
685
0
            to_b->uri.len = tmp - to_b->uri.s;
686
            /* fall through */
687
0
          case E_URI_ENCLOSED:
688
0
            status = END;
689
0
            break;
690
0
          case F_CRLF:
691
0
          case F_LF:
692
0
          case F_CR:
693
            /*previous=crlf and now !=' '*/
694
0
            goto endofheader;
695
0
          default:
696
0
            goto parse_error;
697
0
        }
698
0
        break;
699
0
      case '"':
700
0
        switch (status)
701
0
        {
702
0
          case START_TO:
703
0
            to_b->body.s = tmp;
704
0
            to_b->display.s = tmp;
705
0
            status = DISPLAY_QUOTED;
706
0
            break;
707
0
          case DISPLAY_QUOTED:
708
0
            status = E_DISPLAY_QUOTED;
709
0
            to_b->display.len = tmp-to_b->display.s+1;
710
0
            break;
711
0
          case F_CRLF:
712
0
          case F_LF:
713
0
          case F_CR:
714
            /*previous=crlf and now !=' '*/
715
0
            goto endofheader;
716
0
          default:
717
0
            goto parse_error;
718
0
        }
719
0
        break;
720
0
      case ';' :
721
0
        switch (status)
722
0
        {
723
0
          case DISPLAY_QUOTED:
724
0
          case DISPLAY_TOKEN:
725
0
          case URI_ENCLOSED:
726
0
            break;
727
0
          case URI_OR_TOKEN:
728
0
            end_mark = tmp;
729
            /* fall through */
730
0
          case MAYBE_URI_END:
731
0
            to_b->uri.len = end_mark - to_b->uri.s;
732
            /* fall through */
733
0
          case END:
734
0
            to_b->body.len = tmp-to_b->body.s;
735
0
            tmp = parse_to_param(tmp,end,to_b,&saved_status,multi);
736
0
            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
0
            } else {
751
0
              goto endofheader;
752
0
            }
753
0
          case F_CRLF:
754
0
          case F_LF:
755
0
          case F_CR:
756
            /*previous=crlf and now !=' '*/
757
0
            goto endofheader;
758
0
          default:
759
0
            goto parse_error;
760
0
        }
761
0
        break;
762
0
      default:
763
0
        switch (status)
764
0
        {
765
0
          case START_TO:
766
0
            to_b->uri.s = to_b->body.s = tmp;
767
0
            status = URI_OR_TOKEN;
768
0
            to_b->display.s=tmp;
769
0
            break;
770
0
          case S_URI_ENCLOSED:
771
0
            to_b->uri.s=tmp;
772
0
            status=URI_ENCLOSED;
773
0
            break;
774
0
          case MAYBE_URI_END:
775
0
          case DISPLAY_TOKEN2:
776
0
            status = DISPLAY_TOKEN;
777
0
          case DISPLAY_QUOTED:
778
0
          case DISPLAY_TOKEN:
779
0
          case URI_ENCLOSED:
780
0
          case URI_OR_TOKEN:
781
0
            break;
782
0
          case F_CRLF:
783
0
          case F_LF:
784
0
          case F_CR:
785
            /*previous=crlf and now !=' '*/
786
0
            goto endofheader;
787
0
          default:
788
0
            LM_DBG("spitting out [%c] in status %d\n",
789
0
            *tmp,status );
790
0
            goto error;
791
0
        }
792
0
    }/*char switch*/
793
0
  }/*for*/
794
795
0
endofheader:
796
0
  if (to_b->display.len==0) to_b->display.s=0;
797
0
  status=saved_status;
798
0
  LM_DBG("end of header reached, state=%d\n", status);
799
  /* check if error*/
800
0
  switch(status){
801
0
    case MAYBE_URI_END:
802
0
      to_b->uri.len = end_mark - to_b->uri.s;
803
0
    case END:
804
0
      to_b->body.len = tmp - to_b->body.s;
805
0
    case E_PARA_VALUE:
806
0
      break;
807
0
    default:
808
0
      LM_ERR("unexpected end of header in state %d\n", status);
809
0
      goto error;
810
0
  }
811
812
0
  LM_DBG("display={%.*s}, ruri={%.*s}\n",
813
0
    to_b->display.len, ZSW(to_b->display.s),
814
0
    to_b->uri.len, ZSW(to_b->uri.s));
815
0
  return tmp;
816
817
0
parse_error:
818
0
  LM_ERR("unexpected char [%c] in status %d: <<%.*s>> .\n",
819
0
      tmp < end? *tmp : *(end-1), status, (int)(tmp-buffer), buffer);
820
0
error:
821
0
  first_b->error=PARSE_ERROR;
822
0
  free_to_params(first_b);
823
0
  free_to(first_b->next);
824
0
  return tmp;
825
826
0
}
827
828
829
char* parse_to(char* buffer, char *end, struct to_body *to_b)
830
0
{
831
0
  return _parse_to( buffer, end, to_b, 0/*multi*/);
832
0
}
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
}