Coverage Report

Created: 2026-03-11 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_from.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
 * 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
94.2k
{
48
94.2k
  struct to_body* from_b;
49
50
94.2k
  if ( !msg->from && ( parse_headers(msg,HDR_FROM_F,0)==-1 || !msg->from)) {
51
89.2k
    LM_ERR("bad msg or missing FROM header\n");
52
89.2k
    goto error;
53
89.2k
  }
54
55
  /* maybe the header is already parsed! */
56
5.05k
  if (msg->from->parsed)
57
1.80k
    return 0;
58
59
  /* bad luck! :-( - we have to parse it */
60
  /* first, get some memory */
61
3.25k
  from_b = pkg_malloc(sizeof(struct to_body));
62
3.25k
  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.25k
  memset(from_b, 0, sizeof(struct to_body));
69
3.25k
  parse_to(msg->from->body.s,msg->from->body.s+msg->from->body.len+1,from_b);
70
3.25k
  if (from_b->error == PARSE_ERROR) {
71
3.10k
    LM_ERR("bad from header\n");
72
3.10k
    pkg_free(from_b);
73
3.10k
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM,
74
3.10k
      "error parsing From header");
75
3.10k
    set_err_reply(400, "bad header");
76
3.10k
    goto error;
77
3.10k
  }
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
150
  msg->from->parsed = from_b;
86
87
150
  return 0;
88
92.3k
error:
89
92.3k
  return -1;
90
3.25k
}
91
92
/**
93
 *
94
 */
95
struct sip_uri *parse_from_uri(struct sip_msg *msg)
96
600
{
97
600
  struct to_body *tb = NULL;
98
99
600
  if(msg==NULL)
100
0
    return NULL;
101
102
600
  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
600
  if(msg->from==NULL || get_from(msg)==NULL)
109
0
    return NULL;
110
111
600
  tb = get_from(msg);
112
113
600
  if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
114
261
    return &tb->parsed_uri;
115
116
339
  if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
117
252
  {
118
252
    LM_ERR("failed to parse From uri\n");
119
252
    memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
120
252
    set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing From uri");
121
252
    set_err_reply(400, "bad From uri");
122
252
    return NULL;
123
252
  }
124
125
87
  return &tb->parsed_uri;
126
339
}