Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_body.c
Line
Count
Source
1
/**
2
 * Copyright (C) 2009 Voice Sistem SRL
3
 * Copyright (C) 2016 OpenSIPS Solutions
4
 *
5
 * This file is part of opensips, a free SIP server.
6
 *
7
 * opensips is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version
11
 *
12
 * opensips is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
20
 *
21
 */
22
23
#include "../mem/mem.h"
24
#include "../ut.h"
25
#include "../sdp_ops.h"
26
#include "parse_body.h"
27
#include "parse_content.h"
28
#include "parse_hname2.h"
29
#include "parser_f.h"
30
#include "sdp/sdp_helpr_funcs.h"
31
32
0
#define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
33
34
static struct body_part * new_part(void)
35
0
{
36
0
  struct body_part * temp;
37
38
0
  temp = pkg_malloc(sizeof (struct body_part));
39
40
0
  if (temp == 0)
41
0
  {
42
0
    LM_ERR("Unable to allocate memory\n");
43
0
    return 0;
44
0
  }
45
0
  memset(temp, 0, sizeof (struct body_part));
46
47
0
  return temp;
48
0
};
49
50
51
static char* get_hdr_field_unparsed(char* buf, char* end,struct hdr_field* hdr)
52
0
{
53
54
0
  char* tmp;
55
0
  char *match;
56
57
0
  if ((*buf) == '\n' || (*buf) == '\r')
58
0
  {
59
    /* double crlf or lflf or crcr */
60
0
    LM_DBG("found end of header\n");
61
0
    hdr->type = HDR_EOH_T;
62
0
    return buf;
63
0
  }
64
65
0
  LM_DBG("getting hdr from [%.*s...]\n",
66
0
    (int)((end-buf)<25?end-buf:25), buf);
67
68
0
  tmp = parse_hname(buf, end, hdr);
69
0
  if (hdr->type == HDR_ERROR_T)
70
0
  {
71
0
    LM_ERR("bad header\n");
72
0
    goto error_bad_hdr;
73
0
  }
74
75
  /* eliminate leading whitespace */
76
0
  tmp = eat_lws_end(tmp, end);
77
0
  if (tmp >= end)
78
0
  {
79
0
    LM_ERR("hf empty\n");
80
0
    goto error_bad_hdr;
81
0
  }
82
83
  /* just skip over it */
84
0
  hdr->body.s = tmp;
85
  /* find end of header */
86
  /* find lf */
87
0
  do
88
0
  {
89
0
    match = q_memchr(tmp, '\n', end - tmp);
90
0
    if (match)
91
0
    {
92
0
      match++;
93
0
    } else
94
0
    {
95
0
      LM_ERR("bad body for <%s>(%d)\n", hdr->name.s, hdr->type);
96
0
      tmp = end;
97
0
      goto error_bad_hdr;
98
0
    }
99
0
    tmp = match;
100
0
  } while (match < end && ((*match == ' ') || (*match == '\t')));
101
0
  tmp = match;
102
0
  hdr->body.len = match - hdr->body.s;
103
104
105
  /* jku: if \r covered by current length, shrink it */
106
0
  trim_r(hdr->body);
107
0
  hdr->len = tmp - hdr->name.s;
108
0
  return tmp;
109
110
0
error_bad_hdr:
111
0
  LM_ERR("Unable to parse headers\n");
112
113
0
  hdr->type = HDR_ERROR_T;
114
0
  hdr->len = tmp - hdr->name.s;
115
0
  return tmp;
116
0
}
117
118
119
static char *find_line_delimiter(char* p, char* plimit, str delimiter)
120
0
{
121
0
  static char delimiterhead[3] = "--";
122
0
  char *cp, *cp1;
123
124
  /* Iterate through body */
125
0
  cp = p;
126
0
  for (;;) {
127
0
    if (cp >= plimit)
128
0
      return NULL;
129
0
    for(;;) {
130
0
      cp1 = l_memmem(cp, delimiterhead, plimit-cp, 2);
131
0
      if (cp1 == NULL)
132
0
        return NULL;
133
      /* ensure enough room for the delimiter match */
134
0
      if (plimit - cp1 < 2 + delimiter.len) {
135
0
        return NULL;
136
0
      }
137
      /* We matched '--',
138
       * now let's match the boundary delimiter */
139
0
      if (strncmp(cp1+2, delimiter.s, delimiter.len) == 0)
140
0
        break;
141
0
      else
142
0
        cp = cp1 + 2 + delimiter.len;
143
0
      if (cp >= plimit)
144
0
        return NULL;
145
0
    }
146
0
    if (cp1[-1] == '\n' || cp1[-1] == '\r')
147
0
      return cp1;
148
0
    cp = cp1 + 2 + delimiter.len;
149
0
  }
150
0
}
151
152
153
static int parse_single_part(struct body_part *part, char * start, char * end)
154
0
{
155
0
  char * tmp, *body_end, * mime_end;
156
0
  unsigned int mime;
157
158
0
  part->mime = -1;
159
160
0
  if (*start=='\r') {
161
0
    start++;
162
0
    if (*start=='\n') start++;
163
0
  } else if (*start=='\n') {
164
0
    start++;
165
0
  } else {
166
0
    LM_ERR("invalid separator between boundry and body [%x/%x]\n",
167
0
      *start,*(start+1));
168
0
    return -1;
169
0
  }
170
171
0
  if (*(end-1)=='\n') {
172
0
    end--;
173
0
    if (*(end-1)=='\r') end--;
174
0
  } else if (*(end-1)=='\r') {
175
0
    end--;
176
0
  } else {
177
0
    LM_ERR("invalid separator between body and boundry [%x/%x]\n",
178
0
      *(end-2),*(end-1));
179
0
    return -1;
180
0
  }
181
182
0
  LM_DBG("parsing part [%p,%p] (len=%d) [%.*s...]\n",start,end,
183
0
    (int)(end-start), (int)((end-start)<25?(end-start):25), start);
184
185
0
  tmp = start;
186
0
  while (1)
187
0
  {
188
0
    struct hdr_field hd;
189
0
    memset(&hd, 0, sizeof (struct hdr_field));
190
191
0
    tmp = get_hdr_field_unparsed(tmp, end, &hd);
192
0
    if (tmp == NULL || hd.type == HDR_ERROR_T)
193
0
    {
194
0
      LM_ERR("Error parsing body part header\n");
195
0
      return -1;
196
0
    }
197
198
0
    if (hd.type == HDR_CONTENTTYPE_T)
199
0
    {
200
0
      body_end = hd.body.s + hd.body.len;
201
0
      mime_end = decode_mime_type(hd.body.s, body_end, &mime, NULL);
202
203
0
      if (mime_end == NULL)
204
0
      {
205
0
        LM_ERR("Error parsing MIME\n");
206
0
        return -1;
207
0
      }
208
0
      part->mime = mime;
209
0
      part->mime_s = hd.body;
210
0
    }
211
212
0
    if (hd.type == HDR_EOH_T)
213
0
      break;
214
0
  }
215
216
0
  part->headers.s = start;
217
0
  part->headers.len = tmp - start;
218
219
0
  if (part->mime < 0)
220
0
    part->mime = ((TYPE_TEXT) << 16) + SUBTYPE_PLAIN;
221
222
0
  if (tmp>end-2) {
223
    /* we hit the end of body part while looking for EOH, so
224
     * it means there is no actual body */
225
0
    part->body.s = NULL;
226
0
    part->body.len = 0;
227
0
  } else {
228
    /* we hit the hdr<>body separator inside the body part, so
229
     * let's skip it*/
230
0
    part->body.s = tmp + 2;
231
0
    part->body.len = end - part->body.s;
232
0
  }
233
234
0
  return 0;
235
0
};
236
237
238
int parse_sip_body(struct sip_msg * msg)
239
0
{
240
0
  char *start, *end;
241
0
  int type = 0, new_sdp = 0;
242
0
  struct body_part *part, *last;
243
0
  str delimiter, body;
244
245
  /* is body already parsed ? */
246
0
  if (msg->body) {
247
0
    if (!have_sdp_ops(msg))
248
0
      return 0;
249
250
0
    free_sip_body(msg->body);
251
0
    msg->body = NULL;
252
0
  }
253
254
0
  if ( get_body(msg,&body)!=0 || body.len==0)
255
0
    return 0;
256
257
0
  type = parse_content_type_hdr(msg);
258
0
  if (type <= 0) {
259
0
    if (!msg->sdp_ops || msg->sdp_ops->sdp.len == 0)
260
0
      return 0;
261
0
    type = (TYPE_APPLICATION<<16) + SUBTYPE_SDP;
262
0
    new_sdp = 1;
263
0
  }
264
265
0
  msg->body = pkg_malloc(sizeof (struct sip_msg_body));
266
0
  if (msg->body == 0)
267
0
  {
268
0
    LM_ERR("Unable to allocate memory\n");
269
0
    return -1;
270
0
  }
271
0
  memset(msg->body, 0, sizeof (struct sip_msg_body));
272
273
0
  msg->body->body = body;
274
275
0
  if (!new_sdp)
276
0
    msg->body->boundary = ((content_t *) msg->content_type->parsed)->boundary;
277
278
0
  if ((type >> 16) == TYPE_MULTIPART)
279
0
  {
280
0
    msg->body->flags |= SIP_BODY_RCV_MULTIPART;
281
0
    delimiter = ((content_t*) msg->content_type->parsed)->boundary;
282
283
0
    LM_DBG("Starting parsing with boundary = [%.*s]\n",
284
0
      delimiter.len, delimiter.s);
285
286
0
    start = find_line_delimiter( body.s, body.s + body.len, delimiter);
287
0
    if (start == NULL) {
288
0
      LM_ERR("Unable to parse multipart type:"
289
0
        " malformed - missing start delimiters\n");
290
0
      goto out_free;
291
0
    }
292
293
    /* mark as first part (no previous one) */
294
0
    last = NULL;
295
296
0
    while (1)
297
0
    {
298
0
      end = find_line_delimiter(start + delimiter.len + 2,
299
0
        body.s + body.len, delimiter);
300
0
      if (end == NULL)
301
0
        break;
302
303
      /* is it the first part ? */
304
0
      if (last==NULL) {
305
0
        part = &msg->body->first;
306
0
      } else {
307
0
        if ( (part=new_part()) == NULL )
308
0
          return 0;
309
0
      }
310
311
      /* add 4 to delimiter 2 for "--"*/
312
0
      if (parse_single_part(part, start + delimiter.len + 2, end)!=0) {
313
0
        LM_ERR("Unable to parse part:[%.*s]\n",(int)(end-start),start);
314
0
        goto out_free;
315
0
      }
316
317
      /* set the parsing for the next cycle */
318
0
      start = end;
319
320
      /* link the new part; note that the first part is part of 
321
       * the body structure, no need to be linked */
322
0
      if (last)
323
0
        last->next = part;
324
0
      last = part;
325
0
      msg->body->part_count++;
326
327
0
    }
328
329
0
  } else {
330
331
    /* only one part in the body */
332
0
    part = &msg->body->first;
333
334
0
    part->mime = type;
335
0
    part->mime_s = !new_sdp ? msg->content_type->body : str_init("application/sdp\r\n");
336
0
    part->body = body;
337
0
    part->headers.s = NULL;
338
0
    part->headers.len = 0;
339
0
    msg->body->part_count++;
340
0
  }
341
342
0
  msg->body->updated_part_count = msg->body->part_count;
343
344
0
  return 0;
345
346
0
out_free:
347
0
  free_sip_body(msg->body);
348
0
  msg->body = NULL;
349
0
  return -1;
350
0
}
351
352
353
struct body_part* add_body_part(struct sip_msg *msg, str *mime_s,
354
                            str * hdrs, str *body)
355
0
{
356
0
  struct body_part *part, *last;
357
0
  char *m;
358
359
0
  if ( parse_sip_body(msg)<0) {
360
0
    LM_ERR("failed to parse existing SIP body\n");
361
0
    return NULL;
362
0
  }
363
364
0
  LM_DBG("adding mime <%.*s>, hdrs <%.*s>, body=<%.*s>\n",
365
0
    mime_s->len, mime_s->s, hdrs?hdrs->len:0 , hdrs?hdrs->s:NULL,
366
0
    body?body->len:0, body?body->s:NULL );
367
368
0
  if (msg->body==NULL) {
369
370
    /* the message has no body so far */
371
0
    msg->body = (struct sip_msg_body*)pkg_malloc(
372
0
      sizeof(struct sip_msg_body) + (body?body->len:0) +
373
0
      (hdrs?hdrs->len:0) + mime_s->len );
374
0
    if (!msg->body) {
375
0
      LM_ERR("No more pkg memory!\n");
376
0
      return NULL;
377
0
    }
378
0
    memset(msg->body, 0, sizeof(struct sip_msg_body));
379
380
0
    msg->body->part_count = 0;
381
0
    msg->body->updated_part_count = 1;
382
0
    msg->body->flags = SIP_BODY_FLAG_NEW;
383
0
    msg->body->boundary.s = NULL;
384
0
    msg->body->boundary.len = 0;
385
386
0
    part = &msg->body->first;
387
0
    m = (char*)(msg->body+1); /* pointer to mime */
388
389
0
  } else {
390
391
    /* allocate a new body part */
392
0
    part = (struct body_part*)pkg_malloc(
393
0
      sizeof(struct body_part) + (body?body->len:0) +
394
0
      (hdrs?hdrs->len:0) + mime_s->len );
395
0
    if (part==NULL) {
396
0
      LM_ERR("failed to allocated pkg mem\n");
397
0
      return NULL;
398
0
    }
399
400
0
    m = (char*)(part+1); /* pointer to mime */
401
402
    /* link new part at the end of the parts list */
403
0
    for (last=&msg->body->first; last->next ; last=last->next);
404
0
    last->next = part;
405
406
0
    msg->body->updated_part_count++;
407
0
  }
408
409
0
  memset( part, 0, sizeof(struct body_part) );
410
411
0
  part->flags = SIP_BODY_PART_FLAG_NEW;
412
413
  /* mime follows right after the part, in the same mem chunk */
414
0
  memcpy( m, mime_s->s, mime_s->len);
415
0
  part->mime_s.s = m;
416
0
  part->mime_s.len = mime_s->len;
417
418
0
  if (hdrs) {
419
    /* SIP hdrs follow right after mime, in the same mem chunk */
420
0
    part->headers.s = m + mime_s->len;
421
0
    memcpy( part->headers.s, hdrs->s, hdrs->len);
422
0
    part->headers.len = hdrs->len;
423
0
  }
424
425
0
  if (body) {
426
    /* body follows right after headers, in the same mem chunk */
427
0
    part->body.s = m + mime_s->len + (hdrs?hdrs->len:0);
428
0
    memcpy( part->body.s, body->s, body->len);
429
0
    part->body.len = body->len;
430
0
  }
431
432
0
  return part;
433
0
}
434
435
436
int delete_body_part(struct sip_msg *msg, struct body_part *part)
437
0
{
438
0
  if (msg->body==NULL) {
439
0
    LM_BUG("deleting a body part, but body not found/parsed :-/\n");
440
0
    return -1;
441
0
  }
442
443
  /* mark the part as deleted */
444
0
  part->flags |= SIP_BODY_PART_FLAG_DELETED;
445
446
0
  msg->body->updated_part_count--;
447
448
0
  return 0;
449
0
}
450
451
452
void free_sip_body(struct sip_msg_body *body)
453
0
{
454
0
  struct body_part * p, *tmp;
455
0
  osips_free_f my_free;
456
457
0
  if (body) {
458
0
    my_free = (body->flags&SIP_BODY_FLAG_SHM) ? shm_free_func : pkg_free_func;
459
    /* the first part does not need to be freed */
460
0
    p = &body->first;
461
0
    if (p->parsed && p->free_parsed_f)
462
0
      p->free_parsed_f( p->parsed, my_free );
463
    /* following parts need to be also freed */
464
0
    p = p->next;
465
0
    while(p) {
466
0
      tmp =  p;
467
0
      p = p->next;
468
      /* any need to free some parsed format of the part ? */
469
0
      if (tmp->parsed && tmp->free_parsed_f)
470
0
        tmp->free_parsed_f( tmp->parsed, my_free );
471
0
      func_free(my_free, tmp);
472
0
    }
473
0
    func_free(my_free, body);
474
0
  }
475
0
}
476
477
478
/* Clones the sip_msg_body structure attached to a sip msg into shm or pkg
479
 * memory.
480
 * Parameters:
481
 *    * src_msg - the original sip_msg containing the sip_msg_body to be cloned
482
 *                It can be in shm or pkg and it is mandatory.
483
 *    * dst_msg - the destination sip_msg - this is need only when cloning into
484
 *                shm, as we need to translate to this new sip msg all the 
485
 *                pointers (from our structure) into inside the buffer of the
486
 *                sip msg; must be provided if cloning to shm;
487
 *    * p_dst   - the holder where the clone will be returned if success.
488
 *    * shared  - 1 if to SHM or 0 if to PKG
489
 */
490
int clone_sip_msg_body(struct sip_msg *src_msg, struct sip_msg *dst_msg,
491
                  struct sip_msg_body **p_dst, int shared)
492
0
{
493
0
  struct sip_msg_body *dst, *src;
494
0
  struct body_part *p, *np;
495
0
  osips_malloc_f my_malloc;
496
0
  int extra_len;
497
498
0
  if (src_msg==NULL || src_msg->body==NULL) {
499
0
    *p_dst = NULL;
500
0
    return 0;
501
0
  }
502
503
0
  my_malloc = shared ? shm_malloc_func : pkg_malloc_func;
504
0
  src = src_msg->body;
505
506
  /* clone the SIP MSG BODY */
507
0
  extra_len = (src->flags&SIP_BODY_FLAG_NEW) ?
508
0
    src->first.mime_s.len+src->first.body.len : 0 ;
509
0
  if ( (dst=func_malloc(my_malloc, sizeof(struct sip_msg_body)+extra_len))==NULL ) {
510
0
    LM_ERR("failed to allocate new sip_msg_body clone (shared=%d)\n",
511
0
      shared);
512
0
    goto err;
513
0
  }
514
0
  memcpy( dst, src, sizeof(struct sip_msg_body)+extra_len);
515
0
  if (shared)
516
0
    dst->flags |= SIP_BODY_FLAG_SHM;
517
0
  else
518
0
    dst->flags &= ~SIP_BODY_FLAG_SHM;
519
  /* update the links inside it */
520
0
  if (dst_msg) { \
521
0
    dst->body.s = translate_pointer(dst_msg->buf ,src_msg->buf,
522
0
      src->body.s );
523
0
    dst->boundary.s = translate_pointer(dst_msg->buf ,src_msg->buf,
524
0
      src->boundary.s );
525
0
  }
526
  /* clone the body parts */
527
0
  for( p=&src->first,np=NULL ; p ; p=p->next) {
528
0
    if (np==NULL) { \
529
      /* first body part */
530
0
      np = &dst->first;
531
0
      extra_len = 0;
532
0
    } else {
533
0
      extra_len = (p->flags&SIP_BODY_PART_FLAG_NEW) ?
534
0
        p->mime_s.len+p->body.len+p->headers.len : 0 ;
535
0
      if((np->next=func_malloc(my_malloc, sizeof(struct body_part)+extra_len))==NULL){
536
0
        LM_ERR("failed to allocate new body_part clone (shared=%d)\n",
537
0
          shared);
538
0
        goto err;
539
0
      } \
540
0
      np = np->next;
541
0
    } \
542
0
    memcpy( np, p, sizeof(struct body_part)+extra_len);
543
    /* update the links inside it */
544
0
    if (p->flags&SIP_BODY_PART_FLAG_NEW) {
545
      /* links are pointing inside the body_part structure */
546
0
      if (p==&src->first) {
547
0
        np->body.s = translate_pointer((char*)dst ,(char*)src,
548
0
          p->body.s);
549
0
        np->mime_s.s = translate_pointer((char*)dst, (char*)src,
550
0
          p->mime_s.s);
551
0
        np->headers.s = translate_pointer((char*)dst, (char*)src,
552
0
          p->headers.s);
553
0
      } else {
554
0
        np->body.s = translate_pointer((char*)np ,(char*)p,
555
0
          p->body.s);
556
0
        np->mime_s.s = translate_pointer((char*)np, (char*)p,
557
0
          p->mime_s.s);
558
0
        np->headers.s = translate_pointer((char*)np, (char*)p,
559
0
          p->headers.s);
560
0
      }
561
0
    } else {
562
      /* links are pointing inside the sip msg body, so update only
563
       * a new sip msg was provided */ \
564
0
      if (dst_msg) { \
565
0
        np->body.s = translate_pointer( dst_msg->buf,
566
0
          src_msg->buf, p->body.s );
567
0
        np->mime_s.s = translate_pointer( dst_msg->buf,
568
0
          src_msg->buf, p->mime_s.s );
569
0
        np->headers.s = translate_pointer( dst_msg->buf,
570
0
            src_msg->buf, p->headers.s );
571
0
      }
572
0
    }
573
0
    if (p->parsed && p->clone_parsed_f)
574
0
      np->parsed = p->clone_parsed_f(p, np, src_msg, dst_msg, my_malloc);
575
0
    else
576
0
      np->parsed = NULL;
577
0
  }
578
579
0
  *p_dst = dst;
580
0
  return 0;
581
0
err:
582
0
  if (dst) free_sip_body(dst);
583
0
  return -1;
584
0
}
585
586
587
588
int should_update_sip_body(struct sip_msg *msg)
589
0
{
590
0
  struct body_part *part;
591
592
0
  if (!msg->body)
593
0
    return 0;
594
595
  /* body changed using lumps */
596
0
  if (msg->body_lumps)
597
0
    return 1;
598
599
0
  for(part = &msg->body->first; part; part = part->next) {
600
    /* body removed or added */
601
0
    if (part->flags & (SIP_BODY_PART_FLAG_NEW|SIP_BODY_PART_FLAG_DELETED))
602
0
      return 1;
603
604
    /* binary body */
605
0
    if (part->dump_f)
606
0
      return 1;
607
0
  }
608
  /* no changes on the body - should not be updated */
609
0
  return 0;
610
0
}
611
612
str *get_body_part(struct sip_msg *msg, unsigned int type, unsigned int subtype)
613
0
{
614
0
  struct body_part *p;
615
616
0
  if (parse_sip_body(msg)<0 || msg->body==NULL) {
617
0
    LM_DBG("cannot parse body\n");
618
0
    return NULL;
619
0
  }
620
0
  if (type == 0 && subtype == 0)
621
0
    return &msg->body->first.body; /* not interested in a particular type */
622
623
0
  for (p = &msg->body->first; p; p = p->next) {
624
0
    if (is_body_part_received(p) &&
625
0
        (p->mime & (type << 16) &&
626
0
        (subtype == 0 || p->mime == (type << 16) + subtype)))
627
0
      return &p->body;
628
0
  }
629
630
0
  return NULL;
631
0
}
632
633
int has_body_part(struct sip_msg *msg, unsigned int type, unsigned int subtype)
634
0
{
635
0
  return (get_body_part(msg, type, subtype) == NULL?0:1);
636
0
}