Coverage Report

Created: 2025-07-11 06:28

/src/opensips/parser/parse_diversion.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2001-2003 FhG Fokus
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 */
20
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
0
{
40
0
  struct to_body* diversion_b;
41
42
0
  if (!msg->diversion && (parse_headers(msg, HDR_DIVERSION_F, 0) == -1 ||
43
0
        !msg->diversion)) {
44
0
    goto error;
45
0
  }
46
47
  /* maybe the header is already parsed! */
48
0
  if (msg->diversion->parsed)
49
0
    return 0;
50
51
  /* bad luck! :-( - we have to parse it */
52
  /* first, get some memory */
53
0
  diversion_b = pkg_malloc(sizeof(struct to_body));
54
0
  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
0
  parse_multi_to(msg->diversion->body.s,
61
0
    msg->diversion->body.s + msg->diversion->body.len + 1, diversion_b);
62
0
  if (diversion_b->error == PARSE_ERROR) {
63
0
    LM_ERR("bad diversion header\n");
64
0
    pkg_free(diversion_b);
65
0
    goto error;
66
0
  }
67
0
  msg->diversion->parsed = diversion_b;
68
69
0
  return 0;
70
0
error:
71
0
  return -1;
72
0
}
73
74
75
/*
76
 * Get value of given diversion parameter
77
 */
78
str *diversion_param(struct sip_msg *msg, str name)
79
0
{
80
0
  struct to_param *params;
81
82
0
  if (parse_diversion_header(msg) == -1) {
83
0
    LM_ERR("could not get diversion parameter\n");
84
0
    return 0;
85
0
  }
86
87
0
  params =  ((struct to_body*)(msg->diversion->parsed))->param_lst;
88
89
0
  while (params) {
90
0
    if ((params->name.len == name.len) &&
91
0
    (strncmp(params->name.s, name.s, name.len) == 0)) {
92
0
      return &(params->value);
93
0
    }
94
0
    params = params->next;
95
0
  }
96
97
0
  return 0;
98
0
}