Coverage Report

Created: 2026-10-28 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/msg_parser.c
Line
Count
Source
1
/*
2
 * sip msg. header proxy parser
3
 *
4
 * Copyright (C) 2001-2003 FhG Fokus
5
 *
6
 * This file is part of opensips, a free SIP server.
7
 *
8
 * opensips is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version
12
 *
13
 * opensips is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
21
 *
22
 * History:
23
 * ---------
24
 *  2003-02-28  scratchpad compatibility abandoned (jiri)
25
 *  2003-01-29  scrathcpad removed (jiri)
26
 *  2003-01-27  next baby-step to removing ZT - PRESERVE_ZT (jiri)
27
 *  2003-03-31  removed msg->repl_add_rm (andrei)
28
 *  2003-04-26 ZSW (jiri)
29
 *  2003-05-01  parser extended to support Accept header field (janakj)
30
 *  2005-03-02  free_via_list(vb) on via parse error (andrei)
31
 *  2006-02-17 Session-Expires, Min-SE (dhsueh@somanetworks.com)
32
 *  2006-03-02 header of same type are linked as sibling (bogdan)
33
 *  2006-11-28 Added statistic support for bad message headers.
34
 *             (Jeffrey Magder - SOMA Networks)
35
 *  2008-09-09 Added sdp parsing support (osas)
36
 */
37
38
39
#include <string.h>
40
#include <stdlib.h>
41
42
#include "msg_parser.h"
43
#include "parser_f.h"
44
#include "../ut.h"
45
#include "../error.h"
46
#include "../dprint.h"
47
#include "../data_lump.h"
48
#include "../data_lump_rpl.h"
49
#include "../mem/mem.h"
50
#include "../error.h"
51
#include "../globals.h"
52
#include "../core_stats.h"
53
#include "../errinfo.h"
54
#include "../dset.h"
55
#include "../sdp_ops.h"
56
#include "parse_hname2.h"
57
#include "parse_uri.h"
58
#include "parse_content.h"
59
#include "../msg_callbacks.h"
60
61
#ifdef DEBUG_DMALLOC
62
#include <mem/dmalloc.h>
63
#endif
64
65
66
952k
#define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
67
68
/* number of via's encountered */
69
int via_cnt;
70
71
/* returns pointer to next header line, and fill hdr_f ;
72
 * if at end of header returns pointer to the last crlf  (always buf)*/
73
char* get_hdr_field_aux(char* buf, char* end, struct hdr_field* hdr,int sip_well_known_parse)
74
953k
{
75
76
953k
  char* tmp;
77
953k
  char *match;
78
953k
  struct via_body *vb;
79
953k
  struct cseq_body* cseq_b;
80
953k
  struct to_body* to_b;
81
953k
  int integer;
82
83
953k
  if ((*buf)=='\n' || (*buf)=='\r'){
84
    /* double crlf or lflf or crcr */
85
963
    LM_DBG("found end of header\n");
86
963
    hdr->type=HDR_EOH_T;
87
963
    return buf;
88
963
  }
89
90
952k
  tmp=parse_hname(buf, end, hdr);
91
952k
  if (hdr->type==HDR_ERROR_T){
92
12.2k
    LM_ERR("bad header\n");
93
12.2k
    goto error_bad_hdr;
94
12.2k
  }
95
96
  /* eliminate leading whitespace */
97
940k
  tmp=eat_lws_end(tmp, end);
98
940k
  if (tmp>=end) {
99
1.09k
    LM_ERR("hf empty\n");
100
1.09k
    goto error_bad_hdr;
101
1.09k
  }
102
103
  /* if header-field well-known, parse it, find its end otherwise ;
104
   * after leaving the hdr->type switch, tmp should be set to the
105
   * next header field
106
   */
107
939k
  switch(hdr->type){
108
119k
    case HDR_VIA_T:
109
      /* keep number of vias parsed -- we want to report it in
110
         replies for diagnostic purposes */
111
119k
      if (sip_well_known_parse)
112
119k
        via_cnt++;
113
119k
      if (sip_well_known_parse) {
114
119k
        vb=pkg_malloc(sizeof(struct via_body));
115
119k
        if (vb==0){
116
0
          LM_ERR("out of pkg memory\n");
117
0
          goto error;
118
0
        }
119
119k
        memset(vb,0,sizeof(struct via_body));
120
119k
        hdr->body.s=tmp;
121
119k
        tmp=parse_via(tmp, end, vb);
122
119k
        if (vb->error==PARSE_ERROR){
123
8.37k
          LM_ERR("bad via\n");
124
8.37k
          free_via_list(vb);
125
8.37k
          set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
126
8.37k
            "error parsing Via");
127
8.37k
          set_err_reply(400, "bad Via header");
128
8.37k
          goto error;
129
8.37k
        }
130
110k
        hdr->parsed=vb;
131
110k
        vb->hdr.s=hdr->name.s;
132
110k
        vb->hdr.len=hdr->name.len;
133
110k
        hdr->body.len=tmp-hdr->body.s;
134
110k
      } else {
135
        /* just skip over it */
136
0
        hdr->body.s=tmp;
137
        /* find end of header */
138
        /* find lf */
139
0
        do{
140
0
          match=q_memchr(tmp, '\n', end-tmp);
141
0
          if (match){
142
0
            match++;
143
0
          }else {
144
0
            LM_ERR("bad body for <%.*s>(%d)\n",
145
0
               hdr->name.len, hdr->name.s, hdr->type);
146
0
            tmp=end;
147
0
            goto error_bad_hdr;
148
0
          }
149
0
          tmp=match;
150
0
        }while( match<end &&( (*match==' ')||(*match=='\t') ) );
151
0
        tmp=match;
152
0
        hdr->body.len=match-hdr->body.s;
153
0
      }
154
110k
      break;
155
110k
    case HDR_CSEQ_T:
156
63.5k
      if (sip_well_known_parse) {
157
63.5k
        cseq_b=pkg_malloc(sizeof(struct cseq_body));
158
63.5k
        if (cseq_b==0){
159
0
          LM_ERR("out of pkg memory\n");
160
0
          goto error;
161
0
        }
162
63.5k
        memset(cseq_b, 0, sizeof(struct cseq_body));
163
63.5k
        hdr->body.s=tmp;
164
63.5k
        tmp=parse_cseq(tmp, end, cseq_b);
165
63.5k
        if (cseq_b->error==PARSE_ERROR){
166
991
          LM_ERR("bad cseq\n");
167
991
          pkg_free(cseq_b);
168
991
          set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
169
991
            "error parsing CSeq`");
170
991
          set_err_reply(400, "bad CSeq header");
171
991
          goto error;
172
991
        }
173
62.5k
        hdr->parsed=cseq_b;
174
62.5k
        hdr->body.len=tmp-hdr->body.s;
175
62.5k
        LM_DBG("cseq <%.*s>: <%.*s> <%.*s>\n",
176
62.5k
            hdr->name.len, ZSW(hdr->name.s),
177
0
            cseq_b->number.len, ZSW(cseq_b->number.s),
178
0
            cseq_b->method.len, cseq_b->method.s);
179
62.5k
      } else {
180
        /* just skip over it */
181
0
        hdr->body.s=tmp;
182
        /* find end of header */
183
        /* find lf */
184
0
        do{
185
0
          match=q_memchr(tmp, '\n', end-tmp);
186
0
          if (match){
187
0
            match++;
188
0
          }else {
189
0
            LM_ERR("bad body for <%.*s>(%d)\n",
190
0
               hdr->name.len, hdr->name.s, hdr->type);
191
0
            tmp=end;
192
0
            goto error_bad_hdr;
193
0
          }
194
0
          tmp=match;
195
0
        }while( match<end &&( (*match==' ')||(*match=='\t') ) );
196
0
        tmp=match;
197
0
        hdr->body.len=match-hdr->body.s;
198
0
      }
199
62.5k
      break;
200
62.5k
    case HDR_TO_T:
201
45.5k
      if (sip_well_known_parse) {
202
45.5k
        to_b=pkg_malloc(sizeof(struct to_body));
203
45.5k
        if (to_b==0){
204
0
          LM_ERR("out of pkg memory\n");
205
0
          goto error;
206
0
        }
207
45.5k
        memset(to_b, 0, sizeof(struct to_body));
208
45.5k
        hdr->body.s=tmp;
209
45.5k
        tmp=parse_to(tmp, end,to_b);
210
45.5k
        if (to_b->error==PARSE_ERROR){
211
980
          LM_ERR("bad to header\n");
212
980
          pkg_free(to_b);
213
980
          set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
214
980
            "error parsing To header");
215
980
          set_err_reply(400, "bad header");
216
980
          goto error;
217
980
        }
218
44.5k
        hdr->parsed=to_b;
219
44.5k
        hdr->body.len=tmp-hdr->body.s;
220
44.5k
        LM_DBG("<%.*s> [%d]; uri=[%.*s] \n",
221
44.5k
          hdr->name.len, ZSW(hdr->name.s),
222
0
          hdr->body.len, to_b->uri.len,ZSW(to_b->uri.s));
223
44.5k
        LM_DBG("to body [%.*s]\n",to_b->body.len, ZSW(to_b->body.s));
224
44.5k
      } else {
225
        /* just skip over it */
226
0
        hdr->body.s=tmp;
227
        /* find end of header */
228
        /* find lf */
229
0
        do{
230
0
          match=q_memchr(tmp, '\n', end-tmp);
231
0
          if (match){
232
0
            match++;
233
0
          }else {
234
0
            LM_ERR("bad body for <%.*s>(%d)\n",
235
0
               hdr->name.len, hdr->name.s, hdr->type);
236
0
            tmp=end;
237
0
            goto error_bad_hdr;
238
0
          }
239
0
          tmp=match;
240
0
        }while( match<end &&( (*match==' ')||(*match=='\t') ) );
241
0
        tmp=match;
242
0
        hdr->body.len=match-hdr->body.s;
243
0
      }
244
44.5k
      break;
245
44.5k
    case HDR_CONTENTLENGTH_T:
246
4.00k
      if (sip_well_known_parse) {
247
4.00k
        hdr->body.s=tmp;
248
4.00k
        tmp=parse_content_length(tmp,end, &integer);
249
4.00k
        if (tmp==0){
250
204
          LM_ERR("bad content_length header\n");
251
204
          set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
252
204
            "error parsing Content-Length");
253
204
          set_err_reply(400, "bad Content-Length header");
254
204
          goto error;
255
204
        }
256
3.80k
        hdr->parsed=(void*)(long)integer;
257
3.80k
        hdr->body.len=tmp-hdr->body.s;
258
3.80k
        LM_DBG("content_length=%d\n", (int)(long)hdr->parsed);
259
3.80k
      } else {
260
        /* just skip over it */
261
0
        hdr->body.s=tmp;
262
        /* find end of header */
263
        /* find lf */
264
0
        do{
265
0
          match=q_memchr(tmp, '\n', end-tmp);
266
0
          if (match){
267
0
            match++;
268
0
          }else {
269
0
            LM_ERR("bad body for <%.*s>(%d)\n",
270
0
               hdr->name.len, hdr->name.s, hdr->type);
271
0
            tmp=end;
272
0
            goto error_bad_hdr;
273
0
          }
274
0
          tmp=match;
275
0
        }while( match<end &&( (*match==' ')||(*match=='\t') ) );
276
0
        tmp=match;
277
0
        hdr->body.len=match-hdr->body.s;
278
0
      }
279
3.80k
      break;
280
5.49k
    case HDR_SUPPORTED_T:
281
17.2k
    case HDR_CONTENTTYPE_T:
282
23.8k
    case HDR_FROM_T:
283
35.5k
    case HDR_CALLID_T:
284
44.1k
    case HDR_CONTACT_T:
285
48.4k
    case HDR_ROUTE_T:
286
53.1k
    case HDR_RECORDROUTE_T:
287
58.1k
    case HDR_PATH_T:
288
59.4k
    case HDR_MAXFORWARDS_T:
289
63.5k
    case HDR_AUTHORIZATION_T:
290
75.2k
    case HDR_EXPIRES_T:
291
82.4k
    case HDR_PROXYAUTH_T:
292
89.0k
    case HDR_PROXYREQUIRE_T:
293
92.9k
    case HDR_UNSUPPORTED_T:
294
99.9k
    case HDR_ALLOW_T:
295
109k
    case HDR_EVENT_T:
296
113k
    case HDR_ACCEPT_T:
297
118k
    case HDR_ACCEPTLANGUAGE_T:
298
121k
    case HDR_ORGANIZATION_T:
299
124k
    case HDR_PRIORITY_T:
300
128k
    case HDR_SUBJECT_T:
301
131k
    case HDR_USERAGENT_T:
302
133k
    case HDR_CONTENTDISPOSITION_T:
303
138k
    case HDR_ACCEPTDISPOSITION_T:
304
143k
    case HDR_DIVERSION_T:
305
146k
    case HDR_RPID_T:
306
150k
    case HDR_REFER_TO_T:
307
159k
    case HDR_SESSION_EXPIRES_T:
308
165k
    case HDR_MIN_SE_T:
309
168k
    case HDR_MIN_EXPIRES_T:
310
172k
    case HDR_PPI_T:
311
175k
    case HDR_PAI_T:
312
181k
    case HDR_PRIVACY_T:
313
182k
    case HDR_RETRY_AFTER_T:
314
185k
    case HDR_CALL_INFO_T:
315
192k
    case HDR_WWW_AUTHENTICATE_T:
316
196k
    case HDR_PROXY_AUTHENTICATE_T:
317
199k
    case HDR_FEATURE_CAPS_T:
318
202k
    case HDR_REPLACES_T:
319
206k
    case HDR_TO_PATH_T:
320
207k
    case HDR_FROM_PATH_T:
321
211k
    case HDR_MESSAGE_ID_T:
322
213k
    case HDR_BYTE_RANGE_T:
323
214k
    case HDR_FAILURE_REPORT_T:
324
215k
    case HDR_SUCCESS_REPORT_T:
325
220k
    case HDR_STATUS_T:
326
221k
    case HDR_USE_PATH_T:
327
224k
    case HDR_SECURITY_CLIENT_T:
328
229k
    case HDR_SECURITY_SERVER_T:
329
232k
    case HDR_SECURITY_VERIFY_T:
330
707k
    case HDR_OTHER_T:
331
      /* just skip over it */
332
707k
      hdr->body.s=tmp;
333
      /* find end of header */
334
      /* find lf */
335
713k
      do{
336
713k
        match=q_memchr(tmp, '\n', end-tmp);
337
713k
        if (match){
338
712k
          match++;
339
712k
        }else {
340
1.52k
          LM_ERR("bad body for <%.*s>(%d)\n",
341
1.52k
                   hdr->name.len, hdr->name.s, hdr->type);
342
1.52k
          tmp=end;
343
1.52k
          goto error_bad_hdr;
344
1.52k
        }
345
712k
        tmp=match;
346
712k
      }while( match<end &&( (*match==' ')||(*match=='\t') ) );
347
705k
      tmp=match;
348
705k
      hdr->body.len=match-hdr->body.s;
349
705k
      break;
350
0
    default:
351
0
      LM_CRIT("unknown header type %d\n", hdr->type);
352
0
      goto error;
353
939k
  }
354
  /* jku: if \r covered by current length, shrink it */
355
927k
  trim_r( hdr->body );
356
927k
  hdr->len=tmp-hdr->name.s;
357
927k
  return tmp;
358
359
14.8k
error_bad_hdr:
360
14.8k
  if (sip_well_known_parse) {
361
14.8k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
362
14.8k
      "error parsing headers");
363
14.8k
    set_err_reply(400, "bad headers");
364
14.8k
  }
365
25.3k
error:
366
25.3k
  LM_DBG("error exit\n");
367
25.3k
  if (sip_well_known_parse)
368
25.3k
    update_stat(bad_msg_hdr, 1);
369
25.3k
  hdr->type=HDR_ERROR_T;
370
25.3k
  hdr->len=tmp-hdr->name.s;
371
25.3k
  return tmp;
372
14.8k
}
373
374
/* parse the headers and adds them to msg->headers and msg->to, from etc.
375
 * It stops when all the headers requested in flags were parsed, on error
376
 * (bad header) or end of headers */
377
/* note: it continues where it previously stopped and goes ahead until
378
   end is encountered or desired HFs are found; if you call it twice
379
   for the same HF which is present only once, it will fail the second
380
   time; if you call it twice and the HF is found on second time too,
381
   it's not replaced in the well-known HF pointer but just added to
382
   header list; if you want to use a dumb convenience function which will
383
   give you the first occurrence of a header you are interested in,
384
   look at check_transaction_quadruple
385
*/
386
int parse_headers(struct sip_msg *msg, hdr_flags_t flags, int next)
387
649k
{
388
649k
  struct hdr_field *hf;
389
649k
  char* tmp;
390
649k
  char* rest;
391
649k
  char* end;
392
649k
  hdr_flags_t orig_flag;
393
394
649k
#define link_sibling_hdr(_hook, _hdr) _add_last(_hdr, msg->_hook, sibling)
395
396
649k
  end=msg->buf+msg->len;
397
649k
  tmp=msg->unparsed;
398
399
649k
  if (next) {
400
0
    orig_flag = msg->parsed_flag;
401
0
    msg->parsed_flag &= ~flags;
402
0
  }else
403
649k
    orig_flag=0;
404
405
649k
  LM_DBG("flags=%llx\n", (unsigned long long)flags);
406
1.57M
  while( tmp<end && (flags & msg->parsed_flag) != flags){
407
953k
    hf=pkg_malloc(sizeof(struct hdr_field));
408
953k
    if (hf==0){
409
0
      ser_error=E_OUT_OF_MEM;
410
0
      LM_ERR("pkg memory allocation failed\n");
411
0
      goto error;
412
0
    }
413
953k
    memset(hf,0, sizeof(struct hdr_field));
414
953k
    hf->type=HDR_ERROR_T;
415
953k
    rest=get_hdr_field_aux(tmp, msg->buf+msg->len, hf, 1);
416
953k
    switch (hf->type){
417
25.3k
      case HDR_ERROR_T:
418
25.3k
        LM_INFO("bad header field\n");
419
25.3k
        goto  error;
420
963
      case HDR_EOH_T:
421
963
        msg->eoh=tmp; /* or rest?*/
422
963
        msg->parsed_flag|=HDR_EOH_F;
423
963
        pkg_free(hf);
424
963
        goto skip;
425
473k
      case HDR_OTHER_T: /*do nothing*/
426
476k
      case HDR_TO_PATH_T:
427
478k
      case HDR_FROM_PATH_T:
428
481k
      case HDR_MESSAGE_ID_T:
429
483k
      case HDR_BYTE_RANGE_T:
430
484k
      case HDR_FAILURE_REPORT_T:
431
485k
      case HDR_SUCCESS_REPORT_T:
432
490k
      case HDR_STATUS_T:
433
491k
      case HDR_USE_PATH_T:
434
491k
        break;
435
11.6k
      case HDR_CALLID_T:
436
11.6k
        if (msg->callid==0) msg->callid=hf;
437
11.6k
        msg->parsed_flag|=HDR_CALLID_F;
438
11.6k
        break;
439
44.5k
      case HDR_TO_T:
440
44.5k
        if (msg->to==0) msg->to=hf;
441
44.5k
        msg->parsed_flag|=HDR_TO_F;
442
44.5k
        break;
443
62.5k
      case HDR_CSEQ_T:
444
62.5k
        if (msg->cseq==0) msg->cseq=hf;
445
62.5k
        msg->parsed_flag|=HDR_CSEQ_F;
446
62.5k
        break;
447
6.57k
      case HDR_FROM_T:
448
6.57k
        if (msg->from==0) msg->from=hf;
449
6.57k
        msg->parsed_flag|=HDR_FROM_F;
450
6.57k
        break;
451
8.65k
      case HDR_CONTACT_T:
452
8.65k
        link_sibling_hdr(contact,hf);
453
8.65k
        msg->parsed_flag|=HDR_CONTACT_F;
454
8.65k
        break;
455
1.34k
      case HDR_MAXFORWARDS_T:
456
1.34k
        if(msg->maxforwards==0) msg->maxforwards=hf;
457
1.34k
        msg->parsed_flag|=HDR_MAXFORWARDS_F;
458
1.34k
        break;
459
4.25k
      case HDR_ROUTE_T:
460
4.25k
        link_sibling_hdr(route,hf);
461
4.25k
        msg->parsed_flag|=HDR_ROUTE_F;
462
4.25k
        break;
463
4.66k
      case HDR_RECORDROUTE_T:
464
4.66k
        link_sibling_hdr(record_route,hf);
465
4.66k
        msg->parsed_flag|=HDR_RECORDROUTE_F;
466
4.66k
        break;
467
5.01k
      case HDR_PATH_T:
468
5.01k
        link_sibling_hdr(path,hf);
469
5.01k
        msg->parsed_flag|=HDR_PATH_F;
470
5.01k
        break;
471
11.7k
      case HDR_CONTENTTYPE_T:
472
11.7k
        if (msg->content_type==0) msg->content_type = hf;
473
11.7k
        msg->parsed_flag|=HDR_CONTENTTYPE_F;
474
11.7k
        break;
475
3.80k
      case HDR_CONTENTLENGTH_T:
476
3.80k
        if (msg->content_length==0) msg->content_length = hf;
477
3.80k
        msg->parsed_flag|=HDR_CONTENTLENGTH_F;
478
3.80k
        break;
479
4.03k
      case HDR_AUTHORIZATION_T:
480
4.03k
        link_sibling_hdr(authorization,hf);
481
4.03k
        msg->parsed_flag|=HDR_AUTHORIZATION_F;
482
4.03k
        break;
483
11.7k
      case HDR_EXPIRES_T:
484
11.7k
        if (msg->expires==0) msg->expires = hf;
485
11.7k
        msg->parsed_flag|=HDR_EXPIRES_F;
486
11.7k
        break;
487
7.14k
      case HDR_PROXYAUTH_T:
488
7.14k
        link_sibling_hdr(proxy_auth,hf);
489
7.14k
        msg->parsed_flag|=HDR_PROXYAUTH_F;
490
7.14k
        break;
491
6.64k
      case HDR_PROXYREQUIRE_T:
492
6.64k
        link_sibling_hdr(proxy_require,hf);
493
6.64k
        msg->parsed_flag|=HDR_PROXYREQUIRE_F;
494
6.64k
        break;
495
5.48k
      case HDR_SUPPORTED_T:
496
5.48k
        link_sibling_hdr(supported,hf);
497
5.48k
        msg->parsed_flag|=HDR_SUPPORTED_F;
498
5.48k
        break;
499
3.85k
      case HDR_UNSUPPORTED_T:
500
3.85k
        link_sibling_hdr(unsupported,hf);
501
3.85k
        msg->parsed_flag|=HDR_UNSUPPORTED_F;
502
3.85k
        break;
503
7.06k
      case HDR_ALLOW_T:
504
7.06k
        link_sibling_hdr(allow,hf);
505
7.06k
        msg->parsed_flag|=HDR_ALLOW_F;
506
7.06k
        break;
507
9.48k
      case HDR_EVENT_T:
508
9.48k
        link_sibling_hdr(event,hf);
509
9.48k
        msg->parsed_flag|=HDR_EVENT_F;
510
9.48k
        break;
511
4.49k
      case HDR_ACCEPT_T:
512
4.49k
        link_sibling_hdr(accept,hf);
513
4.49k
        msg->parsed_flag|=HDR_ACCEPT_F;
514
4.49k
        break;
515
4.25k
      case HDR_ACCEPTLANGUAGE_T:
516
4.25k
        link_sibling_hdr(accept_language,hf);
517
4.25k
        msg->parsed_flag|=HDR_ACCEPTLANGUAGE_F;
518
4.25k
        break;
519
2.86k
      case HDR_ORGANIZATION_T:
520
2.86k
        if (msg->organization==0) msg->organization = hf;
521
2.86k
        msg->parsed_flag|=HDR_ORGANIZATION_F;
522
2.86k
        break;
523
3.18k
      case HDR_PRIORITY_T:
524
3.18k
        if (msg->priority==0) msg->priority = hf;
525
3.18k
        msg->parsed_flag|=HDR_PRIORITY_F;
526
3.18k
        break;
527
4.53k
      case HDR_SUBJECT_T:
528
4.53k
        if (msg->subject==0) msg->subject = hf;
529
4.53k
        msg->parsed_flag|=HDR_SUBJECT_F;
530
4.53k
        break;
531
2.82k
      case HDR_USERAGENT_T:
532
2.82k
        if (msg->user_agent==0) msg->user_agent = hf;
533
2.82k
        msg->parsed_flag|=HDR_USERAGENT_F;
534
2.82k
        break;
535
1.61k
      case HDR_CONTENTDISPOSITION_T:
536
1.61k
        if (msg->content_disposition==0) msg->content_disposition = hf;
537
1.61k
        msg->parsed_flag|=HDR_CONTENTDISPOSITION_F;
538
1.61k
        break;
539
5.30k
      case HDR_ACCEPTDISPOSITION_T:
540
5.30k
        link_sibling_hdr(accept_disposition,hf);
541
5.30k
        msg->parsed_flag|=HDR_ACCEPTDISPOSITION_F;
542
5.30k
        break;
543
4.68k
      case HDR_DIVERSION_T:
544
4.68k
        link_sibling_hdr(diversion,hf);
545
4.68k
        msg->parsed_flag|=HDR_DIVERSION_F;
546
4.68k
        break;
547
2.96k
      case HDR_RPID_T:
548
2.96k
        if (msg->rpid==0) msg->rpid = hf;
549
2.96k
        msg->parsed_flag|=HDR_RPID_F;
550
2.96k
        break;
551
3.40k
      case HDR_CALL_INFO_T:
552
3.40k
        link_sibling_hdr(call_info,hf);
553
3.40k
        msg->parsed_flag|=HDR_CALL_INFO_F;
554
3.40k
        break;
555
6.47k
      case HDR_WWW_AUTHENTICATE_T:
556
6.47k
        link_sibling_hdr(www_authenticate,hf);
557
6.47k
        msg->parsed_flag|=HDR_WWW_AUTHENTICATE_F;
558
6.47k
        break;
559
4.07k
      case HDR_PROXY_AUTHENTICATE_T:
560
4.07k
        link_sibling_hdr(proxy_authenticate,hf);
561
4.07k
        msg->parsed_flag|=HDR_PROXY_AUTHENTICATE_F;
562
4.07k
        break;
563
4.05k
      case HDR_REFER_TO_T:
564
4.05k
        if (msg->refer_to==0) msg->refer_to = hf;
565
4.05k
        msg->parsed_flag|=HDR_REFER_TO_F;
566
4.05k
        break;
567
8.72k
      case HDR_SESSION_EXPIRES_T:
568
8.72k
        if ( msg->session_expires == 0 ) msg->session_expires = hf;
569
8.72k
        msg->parsed_flag |= HDR_SESSION_EXPIRES_F;
570
8.72k
        break;
571
6.11k
      case HDR_MIN_SE_T:
572
6.11k
        if ( msg->min_se == 0 ) msg->min_se = hf;
573
6.11k
        msg->parsed_flag |= HDR_MIN_SE_F;
574
6.11k
        break;
575
3.48k
      case HDR_MIN_EXPIRES_T:
576
3.48k
        if ( msg->min_expires == 0 ) msg->min_expires = hf;
577
3.48k
        msg->parsed_flag |= HDR_MIN_EXPIRES_F;
578
3.48k
        break;
579
3.47k
      case HDR_PPI_T:
580
3.47k
        link_sibling_hdr(ppi,hf);
581
3.47k
        msg->parsed_flag|=HDR_PPI_F;
582
3.47k
        break;
583
3.15k
      case HDR_PAI_T:
584
3.15k
        link_sibling_hdr(pai,hf);
585
3.15k
        msg->parsed_flag|=HDR_PAI_F;
586
3.15k
        break;
587
6.36k
      case HDR_PRIVACY_T:
588
6.36k
        if (msg->privacy==0) msg->privacy = hf;
589
6.36k
        msg->parsed_flag|=HDR_PRIVACY_F;
590
6.36k
        break;
591
912
      case HDR_RETRY_AFTER_T:
592
912
        break;
593
110k
      case HDR_VIA_T:
594
110k
        link_sibling_hdr(h_via1,hf);
595
110k
        msg->parsed_flag|=HDR_VIA_F;
596
110k
        LM_DBG("via found, flags=%llx\n", (unsigned long long)flags);
597
110k
        if (msg->via1==0) {
598
15.4k
          LM_DBG("this is the first via\n");
599
15.4k
          msg->h_via1=hf;
600
15.4k
          msg->via1=hf->parsed;
601
15.4k
          if (msg->via1->next){
602
259
            msg->via2=msg->via1->next;
603
259
            msg->parsed_flag|=HDR_VIA2_F;
604
259
          }
605
15.4k
        } else
606
95.3k
          if (msg->via2==0) {
607
2.56k
            msg->via2=hf->parsed;
608
2.56k
            msg->parsed_flag|=HDR_VIA2_F;
609
2.56k
          }
610
110k
        break;
611
3.27k
      case HDR_FEATURE_CAPS_T:
612
3.27k
        link_sibling_hdr(feature_caps, hf);
613
3.27k
        msg->parsed_flag |= HDR_FEATURE_CAPS_F;
614
3.27k
        break;
615
3.12k
      case HDR_REPLACES_T:
616
3.12k
        link_sibling_hdr(replaces, hf);
617
3.12k
        msg->parsed_flag |= HDR_REPLACES_F;
618
3.12k
        break;
619
3.32k
      case HDR_SECURITY_CLIENT_T:
620
3.32k
        link_sibling_hdr(security_client, hf);
621
3.32k
        msg->parsed_flag |= HDR_SECURITY_CLIENT_F;
622
3.32k
        break;
623
4.73k
      case HDR_SECURITY_SERVER_T:
624
4.73k
        link_sibling_hdr(security_server, hf);
625
4.73k
        msg->parsed_flag |= HDR_SECURITY_SERVER_F;
626
4.73k
        break;
627
3.38k
      case HDR_SECURITY_VERIFY_T:
628
3.38k
        link_sibling_hdr(security_verify, hf);
629
3.38k
        msg->parsed_flag |= HDR_SECURITY_VERIFY_F;
630
3.38k
        break;
631
0
      default:
632
0
        LM_CRIT("unknown header type %d\n",  hf->type);
633
0
        goto error;
634
953k
    }
635
    /* add the header to the list*/
636
927k
    if (msg->last_header==0){
637
24.5k
      msg->headers=hf;
638
24.5k
      msg->last_header=hf;
639
902k
    }else{
640
902k
      msg->last_header->next=hf;
641
902k
      msg->last_header=hf;
642
902k
    }
643
#ifdef EXTRA_DEBUG
644
    LM_DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
645
      hf->type,
646
      hf->name.len, ZSW(hf->name.s),
647
      hf->body.len, ZSW(hf->body.s));
648
#endif
649
927k
    tmp=rest;
650
927k
  }
651
624k
skip:
652
624k
  msg->unparsed=tmp;
653
624k
  return 0;
654
655
25.3k
error:
656
25.3k
  ser_error=E_BAD_REQ;
657
25.3k
  if (hf) pkg_free(hf);
658
25.3k
  if (next) msg->parsed_flag |= orig_flag;
659
25.3k
  return -1;
660
649k
}
661
662
/* clones the headers list from the `from` sip_msg
663
 * into the `to` sip_msg structure */
664
int clone_headers(struct sip_msg *from_msg, struct sip_msg *to_msg)
665
0
{
666
0
  int hdrs_no, i;
667
0
  struct hdr_field *hdrs;
668
0
  struct hdr_field *hdr;
669
670
0
#define link_sibling_hdr_case(_hook, _hdr_type) \
671
0
  case _hdr_type: \
672
0
    _add_last(&hdrs[i], to_msg->_hook, sibling);\
673
0
    break
674
0
#define link_hdr_case(_hook, _hdr_type) \
675
0
  case _hdr_type: \
676
0
    to_msg->_hook=&hdrs[i];\
677
0
    break
678
679
   /*
680
   * we need to duplicate the headers because the hdr->parsed field resides
681
   * in shm memory, so there might be a different process that access the
682
   * parsed field and find the structure parsed by the current process in
683
   * pkg
684
   */
685
0
  for (hdrs_no = 0, hdr = from_msg->headers; hdr; hdr = hdr->next)
686
0
    hdrs_no++;
687
688
0
  hdrs = pkg_malloc(hdrs_no * sizeof(struct hdr_field));
689
0
  if (!hdrs) {
690
0
    LM_ERR("could not allocate %d contact headers!\n", hdrs_no);
691
0
    return -1;
692
0
  }
693
694
  /* reset all header fields before populating new ones */
695
0
  to_msg->h_via1 = NULL;
696
0
  to_msg->callid = NULL;
697
0
  to_msg->to = NULL;
698
0
  to_msg->cseq = NULL;
699
0
  to_msg->from = NULL;
700
0
  to_msg->contact = NULL;
701
0
  to_msg->maxforwards = NULL;
702
0
  to_msg->route = NULL;
703
0
  to_msg->record_route = NULL;
704
0
  to_msg->path = NULL;
705
0
  to_msg->content_type = NULL;
706
0
  to_msg->content_length = NULL;
707
0
  to_msg->authorization = NULL;
708
0
  to_msg->expires = NULL;
709
0
  to_msg->proxy_auth = NULL;
710
0
  to_msg->supported = NULL;
711
0
  to_msg->proxy_require = NULL;
712
0
  to_msg->unsupported = NULL;
713
0
  to_msg->allow = NULL;
714
0
  to_msg->event = NULL;
715
0
  to_msg->accept = NULL;
716
0
  to_msg->accept_language = NULL;
717
0
  to_msg->organization = NULL;
718
0
  to_msg->priority = NULL;
719
0
  to_msg->subject = NULL;
720
0
  to_msg->user_agent = NULL;
721
0
  to_msg->content_disposition = NULL;
722
0
  to_msg->accept_disposition = NULL;
723
0
  to_msg->diversion = NULL;
724
0
  to_msg->rpid = NULL;
725
0
  to_msg->refer_to = NULL;
726
0
  to_msg->session_expires = NULL;
727
0
  to_msg->min_se = NULL;
728
0
  to_msg->ppi = NULL;
729
0
  to_msg->pai = NULL;
730
0
  to_msg->privacy = NULL;
731
0
  to_msg->call_info = NULL;
732
0
  to_msg->www_authenticate = NULL;
733
0
  to_msg->proxy_authenticate = NULL;
734
0
  to_msg->min_expires = NULL;
735
0
  to_msg->feature_caps = NULL;
736
0
  to_msg->replaces = NULL;
737
0
  to_msg->security_client = NULL;
738
0
  to_msg->security_server = NULL;
739
0
  to_msg->security_verify = NULL;
740
741
0
  for (i = 0, hdr = from_msg->headers; hdr; i++, hdr = hdr->next) {
742
0
    memcpy(&hdrs[i], hdr, sizeof(struct hdr_field));
743
    /* fix next and sibling */
744
0
    hdrs[i].next = &hdrs[i + 1];
745
0
    hdrs[i].sibling = NULL;
746
0
    switch(hdr->type) {
747
0
      link_sibling_hdr_case(h_via1, HDR_VIA_T);
748
0
      link_hdr_case        (callid, HDR_CALLID_T);
749
0
      link_hdr_case        (to, HDR_TO_T);
750
0
      link_hdr_case        (cseq, HDR_CSEQ_T);
751
0
      link_hdr_case        (from, HDR_FROM_T);
752
0
      link_sibling_hdr_case(contact, HDR_CONTACT_T);
753
0
      link_hdr_case        (maxforwards, HDR_MAXFORWARDS_T);
754
0
      link_sibling_hdr_case(route, HDR_ROUTE_T);
755
0
      link_sibling_hdr_case(record_route, HDR_RECORDROUTE_T);
756
0
      link_sibling_hdr_case(path, HDR_PATH_T);
757
0
      link_hdr_case        (content_type, HDR_CONTENTTYPE_T);
758
0
      link_hdr_case        (content_length, HDR_CONTENTLENGTH_T);
759
0
      link_sibling_hdr_case(authorization, HDR_AUTHORIZATION_T);
760
0
      link_hdr_case        (expires, HDR_EXPIRES_T);
761
0
      link_sibling_hdr_case(proxy_auth, HDR_PROXYAUTH_T);
762
0
      link_sibling_hdr_case(supported, HDR_SUPPORTED_T);
763
0
      link_sibling_hdr_case(proxy_require, HDR_PROXYREQUIRE_T);
764
0
      link_sibling_hdr_case(unsupported, HDR_UNSUPPORTED_T);
765
0
      link_sibling_hdr_case(allow, HDR_ALLOW_T);
766
0
      link_sibling_hdr_case(event, HDR_EVENT_T);
767
0
      link_sibling_hdr_case(accept, HDR_ACCEPT_T);
768
0
      link_sibling_hdr_case(accept_language, HDR_ACCEPTLANGUAGE_T);
769
0
      link_hdr_case        (organization, HDR_ORGANIZATION_T);
770
0
      link_hdr_case        (priority, HDR_PRIORITY_T);
771
0
      link_hdr_case        (subject, HDR_SUBJECT_T);
772
0
      link_hdr_case        (user_agent, HDR_USERAGENT_T);
773
0
      link_hdr_case        (content_disposition, HDR_CONTENTDISPOSITION_T);
774
0
      link_sibling_hdr_case(accept_disposition, HDR_ACCEPTDISPOSITION_T);
775
0
      link_sibling_hdr_case(diversion, HDR_DIVERSION_T);
776
0
      link_hdr_case        (rpid, HDR_RPID_T);
777
0
      link_hdr_case        (refer_to, HDR_REFER_TO_T);
778
0
      link_hdr_case        (session_expires, HDR_SESSION_EXPIRES_T);
779
0
      link_hdr_case        (min_se, HDR_MIN_SE_T);
780
0
      link_sibling_hdr_case(ppi, HDR_PPI_T);
781
0
      link_sibling_hdr_case(pai, HDR_PAI_T);
782
0
      link_hdr_case        (privacy, HDR_PRIVACY_T);
783
0
      link_sibling_hdr_case(call_info, HDR_CALL_INFO_T);
784
0
      link_sibling_hdr_case(www_authenticate, HDR_WWW_AUTHENTICATE_T);
785
0
      link_sibling_hdr_case(proxy_authenticate, HDR_PROXY_AUTHENTICATE_T);
786
0
      link_hdr_case        (min_expires, HDR_MIN_EXPIRES_T);
787
0
      link_sibling_hdr_case(feature_caps, HDR_FEATURE_CAPS_T);
788
0
      link_hdr_case        (replaces, HDR_REPLACES_T);
789
0
      link_sibling_hdr_case(security_client, HDR_SECURITY_CLIENT_T);
790
0
      link_sibling_hdr_case(security_server, HDR_SECURITY_SERVER_T);
791
0
      link_sibling_hdr_case(security_verify, HDR_SECURITY_VERIFY_T);
792
793
      /* not used in sip_msg, used in MSRP */
794
0
      case HDR_TO_PATH_T:
795
0
      case HDR_FROM_PATH_T:
796
0
      case HDR_MESSAGE_ID_T:
797
0
      case HDR_BYTE_RANGE_T:
798
0
      case HDR_FAILURE_REPORT_T:
799
0
      case HDR_SUCCESS_REPORT_T:
800
0
      case HDR_STATUS_T:
801
0
      case HDR_USE_PATH_T:
802
803
      /* not having shortcut */
804
0
      case HDR_RETRY_AFTER_T:
805
0
      case HDR_VIA2_T:
806
807
      /* not actual hdrs */
808
0
      case HDR_OTHER_T:
809
0
      case HDR_ERROR_T:
810
0
      case HDR_EOH_T:
811
0
        break;
812
      /* we do not have a "default" on purpose, so we get
813
       * a compile err/war where a new HDR is added and we do 
814
       * not handle it here.
815
      default:
816
        LM_ERR("unknown header type %d\n", hdr->type);
817
        break;
818
      */
819
0
    }
820
0
  }
821
0
  hdrs[i - 1].next = 0;
822
0
  to_msg->headers = hdrs;
823
0
#undef link_hdr_case
824
0
#undef link_sibling_hdr_case
825
0
  return 0;
826
0
}
827
828
829
/* returns 0 if ok, -1 for errors */
830
int parse_msg_opt(char* buf, unsigned int len, struct sip_msg* msg,
831
                              int free_on_err)
832
43.4k
{
833
834
43.4k
  char *tmp;
835
43.4k
  char* rest;
836
43.4k
  struct msg_start *fl;
837
43.4k
  int offset;
838
43.4k
  hdr_flags_t flags;
839
840
  /* eat crlf from the beginning */
841
44.2k
  for (tmp=buf; (unsigned int)(tmp-buf) < len
842
44.2k
          && (*tmp=='\n' || *tmp=='\r'); tmp++);
843
43.4k
  offset=tmp-buf;
844
43.4k
  fl=&(msg->first_line);
845
43.4k
  rest=parse_first_line(tmp, len-offset, fl);
846
847
43.4k
  offset+=rest-tmp;
848
43.4k
  tmp=rest;
849
43.4k
  switch(fl->type){
850
2.31k
    case SIP_INVALID:
851
2.31k
      LM_DBG("invalid message\n");
852
      /* if failed to parse the first line, we simply consider that the whole
853
         buffer was parsed, so that nothing is left to be parsed :) - this will
854
         do the trick and make "msg" struct acceptable for following parsing
855
         attempts */
856
2.31k
      msg->unparsed = msg->buf + msg->len;
857
2.31k
      goto error;
858
0
      break;
859
39.6k
    case SIP_REQUEST:
860
39.6k
      LM_DBG("SIP Request:\n");
861
39.6k
      LM_DBG(" method:  <%.*s>\n",fl->u.request.method.len,
862
0
        ZSW(fl->u.request.method.s));
863
39.6k
      LM_DBG(" uri:     <%.*s>\n",fl->u.request.uri.len,
864
0
        ZSW(fl->u.request.uri.s));
865
39.6k
      LM_DBG(" version: <%.*s>\n",fl->u.request.version.len,
866
0
        ZSW(fl->u.request.version.s));
867
39.6k
      flags=HDR_EOH_F;
868
39.6k
      break;
869
1.49k
    case SIP_REPLY:
870
1.49k
      LM_DBG("SIP Reply  (status):\n");
871
1.49k
      LM_DBG(" version: <%.*s>\n",fl->u.reply.version.len,
872
0
          ZSW(fl->u.reply.version.s));
873
1.49k
      LM_DBG(" status:  <%.*s>\n", fl->u.reply.status.len,
874
0
          ZSW(fl->u.reply.status.s));
875
1.49k
      LM_DBG(" reason:  <%.*s>\n", fl->u.reply.reason.len,
876
0
          ZSW(fl->u.reply.reason.s));
877
1.49k
      flags=HDR_EOH_F;
878
1.49k
      break;
879
0
    default:
880
0
      LM_DBG("unknown type %d\n",fl->type);
881
0
      goto error;
882
43.4k
  }
883
41.1k
  msg->unparsed=tmp;
884
  /*find first Via: */
885
41.1k
  if (parse_headers(msg, flags, 0)==-1) goto parse_error;
886
887
#ifdef EXTRA_DEBUG
888
  /* dump parsed data */
889
  if (msg->via1){
890
    LM_DBG(" first  via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>\n",
891
      msg->via1->name.len,
892
      ZSW(msg->via1->name.s),
893
      msg->via1->version.len,
894
      ZSW(msg->via1->version.s),
895
      msg->via1->transport.len,
896
      ZSW(msg->via1->transport.s),
897
      msg->via1->host.len,
898
      ZSW(msg->via1->host.s),
899
      msg->via1->port_str.len,
900
      ZSW(msg->via1->port_str.s),
901
      msg->via1->port);
902
    if (msg->via1->params.s)  LM_DBG(";<%.*s>\n",
903
        msg->via1->params.len, ZSW(msg->via1->params.s));
904
    if (msg->via1->comment.s)
905
        LM_DBG(" <%.*s>\n",
906
          msg->via1->comment.len, ZSW(msg->via1->comment.s));
907
    LM_DBG ("\n");
908
  }
909
  if (msg->via2){
910
    LM_DBG(" first  via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>\n",
911
      msg->via2->name.len,
912
      ZSW(msg->via2->name.s),
913
      msg->via2->version.len,
914
      ZSW(msg->via2->version.s),
915
      msg->via2->transport.len,
916
      ZSW(msg->via2->transport.s),
917
      msg->via2->host.len,
918
      ZSW(msg->via2->host.s),
919
      msg->via2->port_str.len,
920
      ZSW(msg->via2->port_str.s),
921
      msg->via2->port);
922
    if (msg->via2->params.s)  LM_DBG(";<%.*s>\n",
923
        msg->via2->params.len, ZSW(msg->via2->params.s));
924
    if (msg->via2->comment.s) LM_DBG(" <%.*s>\n",
925
        msg->via2->comment.len, ZSW(msg->via2->comment.s));
926
    LM_DBG ("\n");
927
  }
928
#endif
929
930
931
#ifdef EXTRA_DEBUG
932
  LM_DBG("exiting\n");
933
#endif
934
935
15.7k
  return 0;
936
937
25.3k
parse_error:
938
25.3k
  if (free_on_err) {
939
    /* Free the SIP message if it has already been created to
940
     * prevent memory leaks */
941
25.3k
    free_sip_msg(msg);
942
25.3k
    memset(msg, 0, sizeof(*msg));
943
25.3k
  }
944
945
27.7k
error:
946
  /* more debugging, msg->orig is/should be null terminated*/
947
27.7k
  LM_ERR("message=<%.*s>\n", (int)len, ZSW(buf));
948
27.7k
  return -1;
949
25.3k
}
950
951
952
953
void free_reply_lump( struct lump_rpl *lump)
954
0
{
955
0
  struct lump_rpl *foo, *bar;
956
0
  for(foo=lump;foo;)
957
0
  {
958
0
    bar=foo->next;
959
0
    free_lump_rpl(foo);
960
0
    foo = bar;
961
0
  }
962
0
}
963
964
965
/* Free only the content, not the msg structure itself
966
 * NOTE: the function doesn't do any cleanup/reset of the subfields */
967
void free_sip_msg(struct sip_msg* msg)
968
68.8k
{
969
68.8k
  if (msg->msg_cb)
970
0
    msg_callback_process(msg, MSG_DESTROY, NULL);
971
68.8k
  if (msg->new_uri.s)
972
0
    pkg_free(msg->new_uri.s);
973
68.8k
  if (msg->set_global_address.s)
974
0
    pkg_free(msg->set_global_address.s);
975
68.8k
  if (msg->set_global_port.s)
976
0
    pkg_free(msg->set_global_port.s);
977
68.8k
  if (msg->dst_uri.s)
978
0
    pkg_free(msg->dst_uri.s);
979
68.8k
  if (msg->path_vec.s)
980
0
    pkg_free(msg->path_vec.s);
981
68.8k
  if (msg->headers)
982
24.5k
    free_hdr_field_lst(msg->headers);
983
68.8k
  if (msg->add_rm)
984
12.3k
    free_lump_list(msg->add_rm);
985
68.8k
  if (msg->body_lumps)
986
0
    free_lump_list(msg->body_lumps);
987
68.8k
  if (msg->sdp_ops )
988
0
    free_sdp_ops(msg->sdp_ops);
989
68.8k
  if (msg->reply_lump)
990
0
    free_reply_lump(msg->reply_lump);
991
68.8k
  if (msg->body )
992
165
    free_sip_body(msg->body);
993
  /* Parsed fields and callbacks may still point into this buffer. */
994
68.8k
  if (msg->pkg_buf)
995
0
    pkg_free(msg->pkg_buf);
996
  /* msg->buf is otherwise owned by the caller. */
997
68.8k
}
998
999
1000
/* make sure all HFs needed for transaction identification have been
1001
   parsed; return 0 if those HFs can't be found
1002
*/
1003
1004
int check_transaction_quadruple( struct sip_msg* msg )
1005
0
{
1006
0
  if ( parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F,0)!=-1
1007
0
    && msg->from && msg->to && msg->callid && msg->cseq ) {
1008
0
    return 1;
1009
0
  } else {
1010
0
    ser_error=E_BAD_TUPEL;
1011
0
    return 0;
1012
0
  }
1013
0
}
1014
1015
1016
/*
1017
 * Make a private copy of the string and assign it to new_uri
1018
 */
1019
int set_ruri(struct sip_msg *msg, str *uri)
1020
0
{
1021
0
  if (!msg || !uri) {
1022
0
    LM_ERR("invalid parameter value\n");
1023
0
    return -1;
1024
0
  }
1025
1026
  /* strange/corrupt input: best to assume it's an empty URI */
1027
0
  if (!uri->s || uri->len == 0) {
1028
0
    pkg_free(msg->new_uri.s);
1029
0
    memset(&msg->new_uri, 0, sizeof msg->new_uri);
1030
0
    return 0;
1031
0
  }
1032
1033
0
  if (msg->new_uri.s && (msg->new_uri.len >= uri->len)) {
1034
0
    memcpy(msg->new_uri.s, uri->s, uri->len);
1035
0
    msg->new_uri.len = uri->len;
1036
0
  } else {
1037
0
    msg->new_uri.s = pkg_realloc(msg->new_uri.s, uri->len + 1);
1038
0
    if (!msg->new_uri.s) {
1039
0
      LM_ERR("not enough pkg memory (%d)\n",uri->len);
1040
0
      return -1;
1041
0
    }
1042
1043
0
    memcpy(msg->new_uri.s, uri->s, uri->len);
1044
0
    msg->new_uri.len = uri->len;
1045
0
  }
1046
1047
0
  set_ruri_q(msg, Q_UNSPECIFIED);
1048
0
  msg->parsed_uri_ok = 0;
1049
0
  return 0;
1050
0
}
1051
1052
1053
1054
/*
1055
 * Make a private copy of the string and assign it to dst_uri
1056
 */
1057
int set_dst_uri(struct sip_msg *msg, str *uri)
1058
0
{
1059
0
  if (!msg || !uri) {
1060
0
    LM_ERR("invalid parameter value\n");
1061
0
    return -1;
1062
0
  }
1063
1064
  /* strange/corrupt input: best to assume it's an empty URI */
1065
0
  if (!uri->s || uri->len == 0) {
1066
0
    pkg_free(msg->dst_uri.s);
1067
0
    memset(&msg->dst_uri, 0, sizeof msg->dst_uri);
1068
0
    return 0;
1069
0
  }
1070
1071
0
  if (msg->dst_uri.s && (msg->dst_uri.len >= uri->len)) {
1072
0
    memcpy(msg->dst_uri.s, uri->s, uri->len);
1073
0
    msg->dst_uri.len = uri->len;
1074
0
  } else {
1075
0
    msg->dst_uri.s = pkg_realloc(msg->dst_uri.s, uri->len);
1076
0
    if (!msg->dst_uri.s) {
1077
0
      LM_ERR("not enough pkg memory\n");
1078
0
      return -1;
1079
0
    }
1080
1081
0
    memcpy(msg->dst_uri.s, uri->s, uri->len);
1082
0
    msg->dst_uri.len = uri->len;
1083
0
  }
1084
1085
0
  return 0;
1086
0
}
1087
1088
void reset_dst_uri(struct sip_msg *msg)
1089
11.6k
{
1090
11.6k
  if(msg->dst_uri.s!=0)
1091
0
    pkg_free(msg->dst_uri.s);
1092
11.6k
  msg->dst_uri.s = 0;
1093
11.6k
  msg->dst_uri.len = 0;
1094
11.6k
}
1095
1096
int set_dst_host_port(struct sip_msg *msg, str *host, str *port)
1097
0
{
1098
0
  char *tmp, *end, *crt, *new_uri;
1099
0
  int len;
1100
0
  struct sip_uri uri;
1101
0
  int user = 0;
1102
1103
0
  tmp = msg->dst_uri.s;
1104
0
  len = msg->dst_uri.len;
1105
1106
0
  if (tmp == NULL || len == 0) {
1107
0
    LM_ERR("failure - null uri\n");
1108
0
    return -1;
1109
0
  }
1110
0
  if (host && (host->s == NULL || host->len == 0)) {
1111
0
    LM_ERR("cannot set a null uri domain\n");
1112
0
    return -1;
1113
0
  }
1114
0
  if (parse_uri(tmp, len, &uri)<0) {
1115
0
    LM_ERR("bad uri <%.*s>, dropping packet\n", len, tmp);
1116
0
    return -1;
1117
0
  }
1118
0
  new_uri=pkg_malloc(MAX_URI_SIZE);
1119
0
  if (new_uri == NULL) {
1120
0
    LM_ERR("memory allocation failure\n");
1121
0
    return -1;
1122
0
  }
1123
0
  end=new_uri+MAX_URI_SIZE;
1124
0
  crt=new_uri;
1125
0
  len = (uri.user.len?uri.user.s:uri.host.s) - tmp;
1126
0
  if (crt+len>end) goto error_uri;
1127
0
  memcpy(crt,tmp,len);
1128
0
  crt += len;
1129
  /* user */
1130
0
  tmp = uri.user.s;
1131
0
  len = uri.user.len;
1132
0
  if (tmp) {
1133
0
    if (crt+len>end) goto error_uri;
1134
0
    memcpy(crt,tmp,len);
1135
0
    crt += len;
1136
0
    user = 1;
1137
0
  }
1138
  /* passwd */
1139
0
  tmp = uri.passwd.s;
1140
0
  len = uri.passwd.len;
1141
0
  if (tmp) {
1142
0
    if (crt+len+1>end) goto error_uri;
1143
0
    *crt++=':';
1144
0
    memcpy(crt, tmp, len);
1145
0
    crt += len;
1146
0
  }
1147
  /* host */
1148
0
  if (host) {
1149
0
    tmp = host->s;
1150
0
    len = host->len;
1151
0
  } else {
1152
0
    tmp = uri.host.s;
1153
0
    len = uri.host.len;
1154
0
  }
1155
0
  if (tmp) {
1156
0
    if (user) {
1157
0
      if (crt+1>end) goto error_uri;
1158
0
      *crt++='@';
1159
0
    }
1160
0
    if (crt+len+1>end) goto error_uri;
1161
0
    memcpy(crt, tmp, len);
1162
0
    crt += len;
1163
0
  }
1164
  /* port */
1165
0
  if (port) {
1166
0
    tmp = port->s;
1167
0
    len = port->len;
1168
0
  } else {
1169
0
    tmp = uri.port.s;
1170
0
    len = uri.port.len;
1171
0
  }
1172
0
  if (tmp && len > 0) {
1173
0
    if (crt+len+1>end) goto error_uri;
1174
0
    *crt++=':';
1175
0
    memcpy(crt, tmp, len);
1176
0
    crt += len;
1177
0
  }
1178
  /* params */
1179
0
  tmp=uri.params.s;
1180
0
  if (tmp){
1181
0
    len=uri.params.len; if(crt+len+1>end) goto error_uri;
1182
0
    *crt++=';';
1183
0
    memcpy(crt,tmp,len);
1184
0
    crt += len;
1185
0
  }
1186
  /* headers */
1187
0
  tmp=uri.headers.s;
1188
0
  if (tmp){
1189
0
    len=uri.headers.len; if(crt+len+1>end) goto error_uri;
1190
0
    *crt++='?';
1191
0
    memcpy(crt,tmp,len);
1192
0
    crt += len;
1193
0
  }
1194
0
  *crt=0; /* null terminate the thing */
1195
  /* copy it to the msg */
1196
0
  pkg_free(msg->dst_uri.s);
1197
0
  msg->dst_uri.s=new_uri;
1198
0
  msg->dst_uri.len=crt-new_uri;
1199
  
1200
0
  return 0;
1201
1202
0
error_uri:
1203
0
  pkg_free(new_uri);
1204
0
  return -1;
1205
0
}
1206
1207
int rewrite_ruri(struct sip_msg *msg, str *sval, int ival,
1208
        enum rw_ruri_part part)
1209
0
{
1210
0
  int user = 0;
1211
0
  char *tmp, *new_uri, *end, *crt;
1212
0
  int len;
1213
0
  struct sip_uri uri;
1214
1215
0
  if (msg->new_uri.s) {
1216
0
    tmp=msg->new_uri.s;
1217
0
    len=msg->new_uri.len;
1218
0
  }else{
1219
0
    tmp=msg->first_line.u.request.uri.s;
1220
0
    len=msg->first_line.u.request.uri.len;
1221
0
  }
1222
0
  if (parse_uri(tmp, len, &uri)<0){
1223
0
    LM_ERR("bad uri <%.*s>, dropping packet\n", len, tmp);
1224
0
    return -1;
1225
0
  }
1226
1227
0
  new_uri=pkg_malloc(MAX_URI_SIZE);
1228
0
  if (new_uri==0){
1229
0
    LM_ERR("memory allocation failure\n");
1230
0
    return -1;
1231
0
  }
1232
0
  end=new_uri+MAX_URI_SIZE;
1233
0
  crt=new_uri;
1234
  /* begin copying */
1235
0
  len = (uri.user.len?uri.user.s:uri.host.s) - tmp;
1236
0
  if (crt+len>end) goto error;
1237
0
  memcpy(crt,tmp,len);crt+=len;
1238
1239
0
  if (part==RW_RURI_PREFIX && sval->len) {
1240
0
    if (crt+sval->len>end) goto error;
1241
0
    memcpy( crt, sval->s, sval->len);
1242
0
    crt+=sval->len;
1243
    /* whatever we had before, with prefix we have username
1244
       now */
1245
0
    user=1;
1246
0
  }
1247
1248
0
  if ((part==RW_RURI_USER)||(part==RW_RURI_USERPASS)) {
1249
0
    tmp=sval->s;
1250
0
    len=sval->len;
1251
0
  } else if (part==RW_RURI_STRIP) {
1252
0
    if (ival>uri.user.len) {
1253
0
      LM_WARN("too long strip asked; "
1254
0
          " deleting username: %d of <%.*s>\n",
1255
0
          ival, uri.user.len, uri.user.s);
1256
0
      len=0;
1257
0
    } else if (ival==uri.user.len) {
1258
0
      len=0;
1259
0
    } else {
1260
0
      tmp=uri.user.s + ival;
1261
0
      len=uri.user.len - ival;
1262
0
    }
1263
0
  } else if (part==RW_RURI_STRIP_TAIL) {
1264
0
    if (ival>uri.user.len) {
1265
0
      LM_WARN("too long strip_tail asked;"
1266
0
          " deleting username: %d of <%.*s>\n",
1267
0
          ival, uri.user.len, uri.user.s);
1268
0
      len=0;
1269
0
    } else if (ival==uri.user.len) {
1270
0
      len=0;
1271
0
    } else {
1272
0
      tmp=uri.user.s;
1273
0
      len=uri.user.len - ival;
1274
0
    }
1275
0
  } else {
1276
0
    tmp=uri.user.s;
1277
0
    len=uri.user.len;
1278
0
  }
1279
1280
0
  if (len){
1281
0
    if(crt+len>end) goto error;
1282
0
    memcpy(crt,tmp,len);crt+=len;
1283
0
    user=1; /* we have an user field so mark it */
1284
0
  }
1285
1286
0
  if (part==RW_RURI_USERPASS) tmp=0;
1287
0
  else tmp=uri.passwd.s;
1288
  /* passwd */
1289
0
  if (tmp){
1290
0
    len=uri.passwd.len; if(crt+len+1>end) goto error;
1291
0
    *crt=':'; crt++;
1292
0
    memcpy(crt,tmp,len);crt+=len;
1293
0
  }
1294
  /* host */
1295
0
  if (user || tmp){ /* add @ */
1296
0
    if(crt+1>end) goto error;
1297
0
    *crt='@'; crt++;
1298
0
  }
1299
0
  if ((part==RW_RURI_HOST) ||(part==RW_RURI_HOSTPORT)) {
1300
0
    tmp=sval->s;
1301
0
    len=sval->len;
1302
0
  } else {
1303
0
    tmp=uri.host.s;
1304
0
    len = uri.host.len;
1305
0
  }
1306
0
  if (tmp){
1307
0
    if(crt+len>end) goto error;
1308
0
    memcpy(crt,tmp,len);crt+=len;
1309
0
  }
1310
  /* port */
1311
0
  if (part==RW_RURI_HOSTPORT) tmp=0;
1312
0
  else if (part==RW_RURI_PORT) {
1313
0
    tmp=sval->s;
1314
0
    len=sval->len;
1315
0
  } else {
1316
0
    tmp=uri.port.s;
1317
0
    len = uri.port.len;
1318
0
  }
1319
0
  if (tmp && len>0){
1320
0
    if(crt+len+1>end) goto error;
1321
0
    *crt=':'; crt++;
1322
0
    memcpy(crt,tmp,len);crt+=len;
1323
0
  }
1324
  /* params */
1325
0
  tmp=uri.params.s;
1326
0
  if (tmp){
1327
    /* include in param string the starting ';' */
1328
0
    len=uri.params.len+1;
1329
0
    tmp--;
1330
0
    if(crt+len+1>end) goto error;
1331
    /* if a maddr param is present, strip it out */
1332
0
    if (uri.maddr.len &&
1333
0
    (part==RW_RURI_HOSTPORT || part==RW_RURI_HOST)) {
1334
0
      memcpy(crt,tmp,uri.maddr.s-tmp-1);
1335
0
      crt+=uri.maddr.s-tmp-1;
1336
0
      memcpy(crt,uri.maddr_val.s+uri.maddr_val.len,
1337
0
        tmp+len-uri.maddr_val.s-uri.maddr_val.len);
1338
0
      crt+=tmp+len-uri.maddr_val.s-uri.maddr_val.len;
1339
0
    } else {
1340
0
      memcpy(crt,tmp,len);crt+=len;
1341
0
    }
1342
0
  }
1343
  /* headers */
1344
0
  tmp=uri.headers.s;
1345
0
  if (tmp){
1346
0
    len=uri.headers.len; if(crt+len+1>end) goto error;
1347
0
    *crt='?'; crt++;
1348
0
    memcpy(crt,tmp,len);crt+=len;
1349
0
  }
1350
0
  *crt=0; /* null terminate the thing */
1351
  /* copy it to the msg */
1352
0
  if (msg->new_uri.s) pkg_free(msg->new_uri.s);
1353
0
  msg->new_uri.s=new_uri;
1354
0
  msg->new_uri.len=crt-new_uri;
1355
0
  msg->parsed_uri_ok=0;
1356
1357
0
  return 0;
1358
1359
0
error:
1360
0
  LM_ERR("uri too long\n");
1361
0
  if (new_uri)
1362
0
    pkg_free(new_uri);
1363
0
  return -1;
1364
0
}
1365
1366
/*
1367
 * Make a private copy of the string and assign it to path_vec
1368
 */
1369
int set_path_vector(struct sip_msg *msg, str *path)
1370
0
{
1371
0
  if (!msg || !path) {
1372
0
    LM_ERR("invalid parameter value\n");
1373
0
    return -1;
1374
0
  }
1375
1376
  /* strange/corrupt input: best to assume it's an empty URI */
1377
0
  if (!path->s || path->len == 0) {
1378
0
    pkg_free(msg->path_vec.s);
1379
0
    memset(&msg->path_vec, 0, sizeof msg->path_vec);
1380
0
    return 0;
1381
0
  }
1382
1383
0
  if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
1384
0
    memcpy(msg->path_vec.s, path->s, path->len);
1385
0
    msg->path_vec.len = path->len;
1386
0
  } else {
1387
0
    msg->path_vec.s = pkg_realloc(msg->path_vec.s, path->len);
1388
0
    if (!msg->path_vec.s) {
1389
0
      LM_ERR("not enough pkg memory\n");
1390
0
      return -1;
1391
0
    }
1392
1393
0
    memcpy(msg->path_vec.s, path->s, path->len);
1394
0
    msg->path_vec.len = path->len;
1395
0
  }
1396
1397
0
  return 0;
1398
0
}
1399
1400
void clear_path_vector(struct sip_msg *msg)
1401
0
{
1402
0
  if (msg->path_vec.s) {
1403
0
    pkg_free(msg->path_vec.s);
1404
0
    memset(&msg->path_vec, 0, sizeof msg->path_vec);
1405
0
  }
1406
0
}
1407
1408
/* convenience macros */
1409
0
#define LC(_cp) ((*(_cp))|0x20)
1410
#define SET_FOUND(_new_state) \
1411
0
  do{\
1412
0
    fill->s=b;fill->len=p-b;\
1413
0
    LM_DBG("hdr %d extracted as <%.*s>\n",\
1414
0
      flag,fill->len,fill->s);\
1415
0
    flags&=~(flag);\
1416
0
    if (flags) {state=_new_state;}\
1417
0
    else {goto done;}\
1418
0
  }while(0)
1419
#define GET_CSEQ() \
1420
0
  do{\
1421
0
    for(p++;p<end&&isspace((int)*p);p++);\
1422
0
    for(fill->s=b;p<end&&isdigit((int)*p);p++);\
1423
0
    fill->len=p-fill->s;\
1424
0
    if ( (flags&=~(flag))==0) goto done;\
1425
0
    state=1;\
1426
0
  }while(0)
1427
int extract_ftc_hdrs( char *buf, int len, str *from, str *to, str *cseq,str *callid)
1428
0
{
1429
0
  char *end, *p;
1430
0
  char *b;
1431
0
  str  *fill;
1432
0
  int state;
1433
0
  int flags;
1434
0
  int flag;
1435
1436
0
  p = buf;
1437
0
  end = buf+len;
1438
0
  state = 1;
1439
0
  b = 0;
1440
0
  flags = ((from!=0)?0x1:0) | ((to!=0)?0x2:0) | ((cseq!=0)?0x4:0)
1441
0
        | ((callid!=0)?0x8:0);
1442
0
  flag = 0;
1443
0
  fill = 0;
1444
1445
0
  LM_DBG("flags = %d\n",flags);
1446
1447
0
  while(p<end) {
1448
0
    switch (*p) {
1449
0
      case '\n':
1450
0
      case '\r':
1451
0
        switch (state) {
1452
0
          case 4: state=5;break;
1453
0
          case 5: state=6;break;
1454
0
          case 6: if(!(*p=='\n' && *(p-1)=='\r')) SET_FOUND(1);break;
1455
0
          default : state=2;break;
1456
0
        }
1457
0
        break;
1458
0
      case ' ':
1459
0
      case '\t':
1460
0
        switch (state) {
1461
0
          case 4: case 6: state=5; break;
1462
0
          case 2: state=1; break;/*folded line*/
1463
0
        }
1464
0
        break;
1465
0
      case ':':
1466
0
        switch (state) {
1467
0
          case 4:case 5: state=5;if(flag==0x04)GET_CSEQ();break;
1468
0
          case 6: SET_FOUND(1);break;/*found*/
1469
0
          case 2: state=1;break;
1470
0
        }
1471
0
        break;
1472
0
      case 'f':
1473
0
      case 'F':
1474
0
        if (state==5) break;
1475
0
        if (state==6) SET_FOUND(2);/*found*/;
1476
0
        if (state!=2) {state = 1;break;}
1477
        /* hdr starting with 'f' */
1478
0
        if (from==0) break;
1479
0
        b = p;
1480
0
        if (p+3<end && LC(p+1)=='r' && LC(p+2)=='o' && LC(p+3)=='m')
1481
0
          p+=3;
1482
0
        state = 4; /* "f" or "from" found */
1483
0
        fill = from;
1484
0
        flag = 0x1;
1485
0
        break;
1486
0
      case 't':
1487
0
      case 'T':
1488
0
        if (state==5) break;
1489
0
        if (state==6) SET_FOUND(2);/*found*/;
1490
0
        if (state!=2) {state = 1;break;}
1491
        /* hdr starting with 't' */
1492
0
        if (to==0) break;
1493
0
        b = p;
1494
0
        if (p+1<end && LC(p+1)=='o')
1495
0
          p+=1;
1496
0
        state = 4; /* "t" or "to" found */
1497
0
        fill = to;
1498
0
        flag = 0x2;
1499
0
        break;
1500
0
      case 'c':
1501
0
      case 'C':
1502
0
        if (state==5) break;
1503
0
        if (state==6) SET_FOUND(2);/*found*/;
1504
0
        if (state!=2) {state = 1;break;}
1505
        /* hdr starting with 'c' */
1506
0
        if (cseq==0 && callid == 0) break;
1507
0
        if (cseq && p+3<end && LC(p+1)=='s' && LC(p+2)=='e' && LC(p+3)=='q') {
1508
0
          b = p;
1509
0
          p+=3;
1510
0
          state = 4; /* "cseq" found */
1511
0
          fill = cseq;
1512
0
          flag = 0x4;
1513
0
        } else if (callid && p+6<end && LC(p+1)=='a' && LC(p+2) == 'l' &&
1514
0
          LC(p+3) == 'l' && LC(p+4) == '-' && LC(p+5) == 'i' &&
1515
0
          LC(p+6) == 'd') {
1516
0
          b = p;
1517
0
          p+=6;
1518
0
          state = 4; /* callid found */
1519
0
          fill = callid;
1520
0
          flag = 0x8;
1521
0
        }
1522
0
        break;
1523
0
      case 'I':
1524
0
      case 'i':
1525
0
        if (state==5) break;
1526
0
        if (state==6) SET_FOUND(2);/*found*/;
1527
0
        if (state!=2) {state = 1;break;}
1528
0
        if (callid == 0) break;
1529
0
        b = p;
1530
0
        state=4; /* callid found */
1531
0
        fill = callid;
1532
0
        flag=0x8;
1533
0
        break;
1534
0
      default:
1535
0
        switch (state) {
1536
0
          case 2:case 4: state=1; break;
1537
0
          case 6: SET_FOUND(1);break;/*found*/;
1538
0
        }
1539
0
    }
1540
0
    p++;
1541
0
  }
1542
1543
0
  LM_CRIT("no hdrs found in outgoing buffer\n");
1544
0
  return -1;
1545
0
done:
1546
0
  return 0;
1547
0
}
1548
1549
1550
int delete_headers(struct sip_msg *msg, struct hdr_field *hdr)
1551
0
{
1552
0
  for (; hdr; hdr = hdr->sibling) {
1553
0
    if (!del_lump(msg, hdr->name.s - msg->buf, hdr->len, hdr->type)) {
1554
0
      LM_ERR("failed to delete contact '%.*s'\n", hdr->name.len,
1555
0
             hdr->name.s);
1556
0
      return -1;
1557
0
    }
1558
0
  }
1559
1560
0
  return 0;
1561
0
}