Coverage Report

Created: 2025-10-13 06:08

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