1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio 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  * Kamailio 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 
22 /** Parser :: parse header files
23  * @file
24  * @ingroup parser
25  */
26 
27 
28 #include "hf.h"
29 #include "parse_via.h"
30 #include "parse_to.h"
31 #include "parse_cseq.h"
32 #include "parse_date.h"
33 #include "parse_identity.h"
34 #include "parse_identityinfo.h"
35 #include "../dprint.h"
36 #include "../mem/mem.h"
37 #include "parse_def.h"
38 #include "digest/digest.h" /* free_credentials */
39 #include "parse_event.h"
40 #include "parse_expires.h"
41 #include "parse_sipifmatch.h"
42 #include "parse_rr.h"
43 #include "parse_subscription_state.h"
44 #include "contact/parse_contact.h"
45 #include "parse_disposition.h"
46 #include "parse_allow.h"
47 #include "../ut.h"
48 #include "parse_ppi_pai.h"
49 
50 /** Frees a hdr_field structure.
51  * WARNING: it frees only parsed (and not name.s, body.s)
52  */
clean_hdr_field(struct hdr_field * const hf)53 void clean_hdr_field(struct hdr_field* const hf)
54 {
55 	void** h_parsed;
56 
57 	if (hf->parsed){
58 		h_parsed=&hf->parsed; /* strict aliasing warnings workarround */
59 		switch(hf->type){
60 		/* headers with pkg alloc for parsed structure (alphabetic order) */
61 		case HDR_ACCEPT_T:
62 			pkg_free(hf->parsed);
63 			break;
64 
65 		case HDR_ALLOW_T:
66 			free_allow_header(hf);
67 			break;
68 
69 		case HDR_AUTHORIZATION_T:
70 			free_credentials((auth_body_t**)h_parsed);
71 			break;
72 
73 		case HDR_CONTACT_T:
74 			free_contact((contact_body_t**)h_parsed);
75 			break;
76 
77 		case HDR_CONTENTDISPOSITION_T:
78 			free_disposition( ((struct disposition**)h_parsed));
79 			break;
80 
81 		case HDR_CSEQ_T:
82 			free_cseq(hf->parsed);
83 			break;
84 
85 		case HDR_DATE_T:
86 			free_date(hf->parsed);
87 			break;
88 
89 		case HDR_DIVERSION_T:
90 			free_to(hf->parsed);
91 			break;
92 
93 		case HDR_EVENT_T:
94 			free_event((event_t**)h_parsed);
95 			break;
96 
97 		case HDR_EXPIRES_T:
98 			free_expires((exp_body_t**)h_parsed);
99 			break;
100 
101 		case HDR_MIN_EXPIRES_T:
102 			free_expires((exp_body_t**)h_parsed);
103 			break;
104 
105 		case HDR_FROM_T:
106 			free_to(hf->parsed);
107 			break;
108 
109 		case HDR_IDENTITY_INFO_T:
110 			free_identityinfo(hf->parsed);
111 			break;
112 
113 		case HDR_IDENTITY_T:
114 			free_identity(hf->parsed);
115 			break;
116 
117 		case HDR_PAI_T:
118 			free_pai_ppi_body(hf->parsed);
119 			break;
120 
121 		case HDR_PPI_T:
122 			free_pai_ppi_body(hf->parsed);
123 			break;
124 
125 		case HDR_PROXYAUTH_T:
126 			free_credentials((auth_body_t**)h_parsed);
127 			break;
128 
129 		case HDR_RECORDROUTE_T:
130 			free_rr((rr_t**)h_parsed);
131 			break;
132 
133 		case HDR_REFER_TO_T:
134 			free_to(hf->parsed);
135 			break;
136 
137 		case HDR_ROUTE_T:
138 			free_rr((rr_t**)h_parsed);
139 			break;
140 
141 		case HDR_RPID_T:
142 			free_to(hf->parsed);
143 			break;
144 
145 		case HDR_SESSIONEXPIRES_T:
146 			hdr_free_parsed(h_parsed);
147 			break;
148 
149 		case HDR_SIPIFMATCH_T:
150 			free_sipifmatch((str **)h_parsed);
151 			break;
152 
153 		case HDR_SUBSCRIPTION_STATE_T:
154 			free_subscription_state((subscription_state_t**)h_parsed);
155 			break;
156 
157 		case HDR_REQUIRE_T:
158 		case HDR_SUPPORTED_T:
159 			hdr_free_parsed(h_parsed);
160 			break;
161 
162 		case HDR_TO_T:
163 			free_to(hf->parsed);
164 			break;
165 
166 		case HDR_VIA_T:
167 			free_via_list(hf->parsed);
168 			break;
169 
170 		/* headers with no alloc for parsed structure */
171 		case HDR_CALLID_T:
172 		case HDR_MAXFORWARDS_T:
173 		case HDR_CONTENTTYPE_T:
174 		case HDR_CONTENTLENGTH_T:
175 		case HDR_RETRY_AFTER_T:
176 		case HDR_PROXYREQUIRE_T:
177 		case HDR_UNSUPPORTED_T:
178 		case HDR_ACCEPTLANGUAGE_T:
179 		case HDR_ORGANIZATION_T:
180 		case HDR_PRIORITY_T:
181 		case HDR_SUBJECT_T:
182 		case HDR_USERAGENT_T:
183 		case HDR_SERVER_T:
184 		case HDR_MIN_SE_T:
185 		case HDR_ACCEPTCONTACT_T:
186 		case HDR_ALLOWEVENTS_T:
187 		case HDR_CONTENTENCODING_T:
188 		case HDR_REFERREDBY_T:
189 		case HDR_REJECTCONTACT_T:
190 		case HDR_REQUESTDISPOSITION_T:
191 		case HDR_WWW_AUTHENTICATE_T:
192 		case HDR_PROXY_AUTHENTICATE_T:
193 		case HDR_PATH_T:
194 		case HDR_PRIVACY_T:
195 		case HDR_REASON_T:
196 		case HDR_CALLINFO_T:
197 			break;
198 
199 		default:
200 			LM_CRIT("BUG: unknown header type %d\n", hf->type);
201 			break;
202 		}
203 	}
204 }
205 
206 
207 /** Frees a hdr_field list.
208  * WARNING: frees only ->parsed and ->next*/
free_hdr_field_lst(struct hdr_field * hf)209 void free_hdr_field_lst(struct hdr_field* hf)
210 {
211 	struct hdr_field* foo;
212 
213 	while(hf) {
214 		foo=hf;
215 		hf=hf->next;
216 		clean_hdr_field(foo);
217 		pkg_free(foo);
218 		foo = 0;
219 	}
220 }
221 
222 /* print the content of hdr_field */
dump_hdr_field(struct hdr_field const * const hf)223 void dump_hdr_field(struct hdr_field const* const hf )
224 {
225 	LM_ERR("DEBUG: type=%d, name=%.*s, "
226 		"body=%.*s, parsed=%p, next=%p\n",
227 		hf->type, hf->name.len, ZSW(hf->name.s),
228 		hf->body.len, ZSW(hf->body.s),
229 		hf->parsed, hf->next );
230 }
231 
232 /**
233  * free hdr parsed structure using inner free function
234  * - hdr parsed struct must have as first file a free function,
235  *   so it can be caseted to hf_parsed_t
236  */
hdr_free_parsed(void ** h_parsed)237 void hdr_free_parsed(void **h_parsed)
238 {
239 	if(h_parsed==NULL || *h_parsed==NULL)
240 		return;
241 
242 	if(((hf_parsed_t*)(*h_parsed))->hfree) {
243 		((hf_parsed_t*)(*h_parsed))->hfree(*h_parsed);
244 	}
245 	*h_parsed = 0;
246 }
247