Coverage Report

Created: 2025-07-12 06:14

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