Coverage Report

Created: 2026-03-12 06:54

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