Coverage Report

Created: 2025-07-12 06:14

/src/opensips/parser/parse_from.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
 * History:
21
 * --------
22
 * 2005-07-12 missing TAG is reported as error (bogdan)
23
 */
24
25
26
#include "parse_from.h"
27
#include "parse_to.h"
28
#include "parse_uri.h"
29
#include <stdlib.h>
30
#include <string.h>
31
#include "../errinfo.h"
32
#include "../dprint.h"
33
#include "../ut.h"
34
#include "../mem/mem.h"
35
#include "msg_parser.h"
36
37
/*
38
 * This method is used to parse the from header. It was decided not to parse
39
 * anything in core that is not *needed* so this method gets called by
40
 * rad_acc module and any other modules that needs the FROM header.
41
 *
42
 * params: msg : sip msg
43
 * returns =0 on success,
44
 *         <0 on failure.
45
 */
46
int parse_from_header( struct sip_msg *msg)
47
109k
{
48
109k
  struct to_body* from_b;
49
50
109k
  if ( !msg->from && ( parse_headers(msg,HDR_FROM_F,0)==-1 || !msg->from)) {
51
103k
    LM_ERR("bad msg or missing FROM header\n");
52
103k
    goto error;
53
103k
  }
54
55
  /* maybe the header is already parsed! */
56
6.11k
  if (msg->from->parsed)
57
2.23k
    return 0;
58
59
  /* bad luck! :-( - we have to parse it */
60
  /* first, get some memory */
61
3.88k
  from_b = pkg_malloc(sizeof(struct to_body));
62
3.88k
  if (from_b == 0) {
63
0
    LM_ERR("out of pkg_memory\n");
64
0
    goto error;
65
0
  }
66
67
  /* now parse it!! */
68
3.88k
  memset(from_b, 0, sizeof(struct to_body));
69
3.88k
  parse_to(msg->from->body.s,msg->from->body.s+msg->from->body.len+1,from_b);
70
3.88k
  if (from_b->error == PARSE_ERROR) {
71
3.69k
    LM_ERR("bad from header\n");
72
3.69k
    pkg_free(from_b);
73
3.69k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
74
3.69k
      "error parsing From header");
75
3.69k
    set_err_reply(400, "bad header");
76
3.69k
    goto error;
77
3.69k
  }
78
  /* REGISTER doesn't have a from tag :( -bogdan
79
  if (from_b->tag_value.len==0 || from_b->tag_value.s==0) {
80
    LM_ERR("missing TAG value\n");
81
    free_to(from_b);
82
    goto error;
83
  }
84
  */
85
186
  msg->from->parsed = from_b;
86
87
186
  return 0;
88
106k
error:
89
106k
  return -1;
90
3.88k
}
91
92
/**
93
 *
94
 */
95
struct sip_uri *parse_from_uri(struct sip_msg *msg)
96
744
{
97
744
  struct to_body *tb = NULL;
98
99
744
  if(msg==NULL)
100
0
    return NULL;
101
102
744
  if(parse_from_header(msg)<0)
103
0
  {
104
0
    LM_ERR("cannot parse FROM header\n");
105
0
    return NULL;
106
0
  }
107
108
744
  if(msg->from==NULL || get_from(msg)==NULL)
109
0
    return NULL;
110
111
744
  tb = get_from(msg);
112
113
744
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
114
375
    return &tb->parsed_uri;
115
116
369
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
117
244
  {
118
244
    LM_ERR("failed to parse From uri\n");
119
244
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
120
244
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing From uri");
121
244
    set_err_reply(400, "bad From uri");
122
244
    return NULL;
123
244
  }
124
125
125
  return &tb->parsed_uri;
126
369
}