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