Coverage Report

Created: 2026-02-26 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_diversion.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
21
#include <stdlib.h>
22
#include <string.h>
23
#include "../dprint.h"
24
#include "../ut.h"
25
#include "../mem/mem.h"
26
#include "parse_from.h"
27
#include "parse_to.h"
28
#include "msg_parser.h"
29
30
/*
31
 * This method is used to parse DIVERSION header as per RFC5806
32
 *  (Diversion Indication in SIP)
33
 *
34
 * params: msg : sip msg
35
 * returns 0 on success,
36
 *        -1 on failure.
37
 */
38
int parse_diversion_header(struct sip_msg *msg)
39
36.2k
{
40
36.2k
  struct to_body* diversion_b;
41
42
36.2k
  if (!msg->diversion && (parse_headers(msg, HDR_DIVERSION_F, 0) == -1 ||
43
35.0k
        !msg->diversion)) {
44
35.0k
    goto error;
45
35.0k
  }
46
47
  /* maybe the header is already parsed! */
48
1.19k
  if (msg->diversion->parsed)
49
524
    return 0;
50
51
  /* bad luck! :-( - we have to parse it */
52
  /* first, get some memory */
53
674
  diversion_b = pkg_malloc(sizeof(struct to_body));
54
674
  if (diversion_b == 0) {
55
0
    LM_ERR("out of pkg_memory\n");
56
0
    goto error;
57
0
  }
58
59
  /* now parse it!! */
60
674
  parse_multi_to(msg->diversion->body.s,
61
674
    msg->diversion->body.s + msg->diversion->body.len + 1, diversion_b);
62
674
  if (diversion_b->error == PARSE_ERROR) {
63
543
    LM_ERR("bad diversion header\n");
64
543
    pkg_free(diversion_b);
65
543
    goto error;
66
543
  }
67
131
  msg->diversion->parsed = diversion_b;
68
69
131
  return 0;
70
35.5k
error:
71
35.5k
  return -1;
72
674
}
73
74
75
/*
76
 * Get value of given diversion parameter
77
 */
78
str *diversion_param(struct sip_msg *msg, str name)
79
262
{
80
262
  struct to_param *params;
81
82
262
  if (parse_diversion_header(msg) == -1) {
83
0
    LM_ERR("could not get diversion parameter\n");
84
0
    return 0;
85
0
  }
86
87
262
  params =  ((struct to_body*)(msg->diversion->parsed))->param_lst;
88
89
2.13k
  while (params) {
90
1.88k
    if ((params->name.len == name.len) &&
91
544
    (strncmp(params->name.s, name.s, name.len) == 0)) {
92
8
      return &(params->value);
93
8
    }
94
1.87k
    params = params->next;
95
1.87k
  }
96
97
254
  return 0;
98
262
}