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 headers.
23  * @file
24  *
25  * @ingroup parser
26  */
27 
28 
29 
30 #ifndef HF_H
31 #define HF_H
32 
33 #include "../str.h"
34 #include "../comp_defs.h"
35 
36 
37 
38 /** header types enum.
39  *
40  * @note
41  * if you add a new type:
42  *  - make sure it's not greater than 63
43  *  - make sure you add the corresponding flag to the hdr_flags_t defs below
44  *  - update clean_hdr_field (in hf.c)
45  *  - update sip_msg_cloner (modules/tm/sip_msg.c)
46  *  - update parse_headers (msg_parser.c)
47  *  - update get_hdr_field (msg_parser.c)
48  */
49 
50 enum _hdr_types_t {
51 	HDR_ERROR_T					= -1   /*!< Error while parsing */,
52 	HDR_OTHER_T					=  0   /*!< Some other header field */,
53 	HDR_VIA_T					=  1   /*!< Via header field */,
54 	HDR_VIA1_T					=  1   /*!< First Via header field */,
55 	HDR_VIA2_T					=  2   /*!< only used as flag */,
56 	HDR_TO_T					       /*!< To header field */,
57 	HDR_FROM_T					       /*!< From header field */,
58 	HDR_CSEQ_T					       /*!< CSeq header field */,
59 	HDR_CALLID_T				       /*!< Call-Id header field */,
60 	HDR_CONTACT_T				       /*!< Contact header field */,
61 	HDR_MAXFORWARDS_T			       /*!< MaxForwards header field */,
62 	HDR_ROUTE_T					       /*!< Route header field */,
63 	HDR_RECORDROUTE_T			       /*!< Record-Route header field */,
64 	HDR_CONTENTTYPE_T			       /*!< Content-Type header field */,
65 	HDR_CONTENTLENGTH_T			       /*!< Content-Length header field */,
66 	HDR_AUTHORIZATION_T			       /*!< Authorization header field */,
67 	HDR_EXPIRES_T				       /*!< Expires header field */,
68 	HDR_MIN_EXPIRES_T			       /*!< Min-Expires header */,
69 	HDR_PROXYAUTH_T				       /*!< Proxy-Authorization hdr field */,
70 	HDR_SUPPORTED_T				       /*!< Supported  header field */,
71 	HDR_REQUIRE_T				       /*!< Require header */,
72 	HDR_PROXYREQUIRE_T			       /*!< Proxy-Require header field */,
73 	HDR_UNSUPPORTED_T			       /*!< Unsupported header field */,
74 	HDR_ALLOW_T					       /*!< Allow header field */,
75 	HDR_EVENT_T					       /*!< Event header field */,
76 	HDR_ACCEPT_T				       /*!< Accept header field */,
77 	HDR_ACCEPTLANGUAGE_T		       /*!< Accept-Language header field */,
78 	HDR_ORGANIZATION_T			       /*!< Organization header field */,
79 	HDR_PRIORITY_T				       /*!< Priority header field */,
80 	HDR_SUBJECT_T				       /*!< Subject header field */,
81 	HDR_USERAGENT_T				       /*!< User-Agent header field */,
82 	HDR_SERVER_T				       /*!< Server header field */,
83 	HDR_CONTENTDISPOSITION_T	       /*!< Content-Disposition hdr field */,
84 	HDR_DIVERSION_T				       /*!< Diversion header field */,
85 	HDR_RPID_T					       /*!< Remote-Party-ID header field */,
86 	HDR_REFER_TO_T				       /*!< Refer-To header fiels */,
87 	HDR_SIPIFMATCH_T			       /*!< SIP-If-Match header field */,
88 	HDR_SESSIONEXPIRES_T		       /*!< Session-Expires header */,
89 	HDR_MIN_SE_T				       /*!< Min-SE */,
90 	HDR_SUBSCRIPTION_STATE_T	       /*!< Subscription-State */,
91 	HDR_ACCEPTCONTACT_T			       /*!< Accept-Contact header */,
92 	HDR_ALLOWEVENTS_T			       /*!< Allow-Events header */,
93 	HDR_CONTENTENCODING_T		       /*!< Content-Encoding header */,
94 	HDR_REFERREDBY_T			       /*!< Referred-By header */,
95 	HDR_REJECTCONTACT_T			       /*!< Reject-Contact header */,
96 	HDR_REQUESTDISPOSITION_T	       /*!< Request-Disposition header */,
97 	HDR_WWW_AUTHENTICATE_T		       /*!< WWW-Authenticate header field */,
98 	HDR_PROXY_AUTHENTICATE_T	       /*!< Proxy-Authenticate header field */,
99 	HDR_DATE_T			       /*!< Date header field */,
100 	HDR_IDENTITY_T			       /*!< Identity header field */,
101 	HDR_IDENTITY_INFO_T		       /*!< Identity-info header field */,
102 	HDR_RETRY_AFTER_T		           /*!< Retry-After header field */,
103 	HDR_PPI_T                          /*!< P-Preferred-Identity header field*/,
104 	HDR_PAI_T                          /*!< P-Asserted-Identity header field*/,
105 	HDR_PATH_T                         /*!< Path header field */,
106 	HDR_PRIVACY_T				       /*!< Privacy header field */,
107 	HDR_REASON_T				       /**< Reason header field */,
108 	HDR_CALLINFO_T                     /*!< Call-Info header field*/,
109 	HDR_EOH_T					       /*!< End of message header */
110 };
111 
112 
113 typedef unsigned long long hdr_flags_t;
114 
115 /** type to flag conversion.
116  * WARNING: HDR_ERROR_T has no corresponding FLAG ! */
117 #define HDR_T2F(type)	\
118 		(((type)!=HDR_EOH_T)?((hdr_flags_t)1<<(type)):(~(hdr_flags_t)0))
119 
120 /** helper macro for easy defining and keeping in sync the flags enum. */
121 #define HDR_F_DEF(name)		HDR_T2F(HDR_##name##_T)
122 
123 /** @name flags definitions.
124  * (enum won't work with all the compiler (e.g. icc) due to the 64bit size) */
125 /*!{ */
126 #define HDR_EOH_F					HDR_F_DEF(EOH)
127 #define HDR_VIA_F					HDR_F_DEF(VIA)
128 #define HDR_VIA1_F					HDR_F_DEF(VIA1)
129 #define HDR_VIA2_F					HDR_F_DEF(VIA2)
130 #define HDR_TO_F					HDR_F_DEF(TO)
131 #define HDR_FROM_F					HDR_F_DEF(FROM)
132 #define HDR_CSEQ_F					HDR_F_DEF(CSEQ)
133 #define HDR_CALLID_F				HDR_F_DEF(CALLID)
134 #define HDR_CONTACT_F				HDR_F_DEF(CONTACT)
135 #define HDR_MAXFORWARDS_F			HDR_F_DEF(MAXFORWARDS)
136 #define HDR_ROUTE_F					HDR_F_DEF(ROUTE)
137 #define HDR_RECORDROUTE_F			HDR_F_DEF(RECORDROUTE)
138 #define HDR_CONTENTTYPE_F			HDR_F_DEF(CONTENTTYPE)
139 #define HDR_CONTENTLENGTH_F			HDR_F_DEF(CONTENTLENGTH)
140 #define HDR_AUTHORIZATION_F			HDR_F_DEF(AUTHORIZATION)
141 #define HDR_EXPIRES_F				HDR_F_DEF(EXPIRES)
142 #define HDR_MIN_EXPIRES_F			HDR_F_DEF(MIN_EXPIRES)
143 #define HDR_PROXYAUTH_F				HDR_F_DEF(PROXYAUTH)
144 #define HDR_SUPPORTED_F				HDR_F_DEF(SUPPORTED)
145 #define HDR_REQUIRE_F				HDR_F_DEF(REQUIRE)
146 #define HDR_PROXYREQUIRE_F			HDR_F_DEF(PROXYREQUIRE)
147 #define HDR_UNSUPPORTED_F			HDR_F_DEF(UNSUPPORTED)
148 #define HDR_ALLOW_F					HDR_F_DEF(ALLOW)
149 #define HDR_EVENT_F					HDR_F_DEF(EVENT)
150 #define HDR_ACCEPT_F				HDR_F_DEF(ACCEPT)
151 #define HDR_ACCEPTLANGUAGE_F		HDR_F_DEF(ACCEPTLANGUAGE)
152 #define HDR_ORGANIZATION_F			HDR_F_DEF(ORGANIZATION)
153 #define HDR_PRIORITY_F				HDR_F_DEF(PRIORITY)
154 #define HDR_SUBJECT_F				HDR_F_DEF(SUBJECT)
155 #define HDR_USERAGENT_F				HDR_F_DEF(USERAGENT)
156 #define HDR_SERVER_F				HDR_F_DEF(SERVER)
157 #define HDR_CONTENTDISPOSITION_F	HDR_F_DEF(CONTENTDISPOSITION)
158 #define HDR_DIVERSION_F				HDR_F_DEF(DIVERSION)
159 #define HDR_RPID_F					HDR_F_DEF(RPID)
160 #define HDR_REFER_TO_F				HDR_F_DEF(REFER_TO)
161 #define HDR_SIPIFMATCH_F			HDR_F_DEF(SIPIFMATCH)
162 #define HDR_SESSIONEXPIRES_F		HDR_F_DEF(SESSIONEXPIRES)
163 #define HDR_MIN_SE_F				HDR_F_DEF(MIN_SE)
164 #define HDR_SUBSCRIPTION_STATE_F	HDR_F_DEF(SUBSCRIPTION_STATE)
165 #define HDR_ACCEPTCONTACT_F			HDR_F_DEF(ACCEPTCONTACT)
166 #define HDR_ALLOWEVENTS_F			HDR_F_DEF(ALLOWEVENTS)
167 #define HDR_CONTENTENCODING_F		HDR_F_DEF(CONTENTENCODING)
168 #define HDR_REFERREDBY_F			HDR_F_DEF(REFERREDBY)
169 #define HDR_REJECTCONTACT_F			HDR_F_DEF(REJECTCONTACT)
170 #define HDR_REQUESTDISPOSITION_F	HDR_F_DEF(REQUESTDISPOSITION)
171 #define HDR_WWW_AUTHENTICATE_F		HDR_F_DEF(WWW_AUTHENTICATE)
172 #define HDR_PROXY_AUTHENTICATE_F	HDR_F_DEF(PROXY_AUTHENTICATE)
173 #define HDR_DATE_F			HDR_F_DEF(DATE)
174 #define HDR_IDENTITY_F			HDR_F_DEF(IDENTITY)
175 #define HDR_IDENTITY_INFO_F		HDR_F_DEF(IDENTITY_INFO)
176 #define HDR_RETRY_AFTER_F			HDR_F_DEF(RETRY_AFTER)
177 #define HDR_PPI_F                   HDR_F_DEF(PPI)
178 #define HDR_PAI_F                   HDR_F_DEF(PAI)
179 #define HDR_PATH_F                  HDR_F_DEF(PATH)
180 #define HDR_PRIVACY_F               HDR_F_DEF(PRIVACY)
181 #define HDR_REASON_F				HDR_F_DEF(REASON)
182 #define HDR_CALLINFO_F				HDR_F_DEF(CALLINFO)
183 
184 #define HDR_OTHER_F					HDR_F_DEF(OTHER)
185 
186 /*!} */ /* Doxygen end marker*/
187 
188 typedef enum _hdr_types_t hdr_types_t;
189 
190 /** Format: name':' body.
191  */
192 typedef struct hdr_field {
193 	hdr_types_t type;       /*!< Header field type */
194 	str name;               /*!< Header field name */
195 	str body;               /*!< Header field body (may not include CRLF) */
196 	int len;		/*!< length from hdr start until EoHF (incl.CRLF) */
197 	void* parsed;           /*!< Parsed data structures */
198 	struct hdr_field* next; /*!< Next header field in the list */
199 } hdr_field_t;
200 
201 
202 /* type of the function to free the structure of parsed header field */
203 typedef void (*hf_parsed_free_f)(void *parsed);
204 
205 /* structure to hold the function to free the parsed header field */
206 typedef struct hdr_parsed {
207 	hf_parsed_free_f hfree;
208 } hf_parsed_t;
209 
210 /** returns true if the header links allocated memory on parse field. */
hdr_allocs_parse(struct hdr_field * hdr)211 static inline int hdr_allocs_parse(struct hdr_field* hdr)
212 {
213 	switch(hdr->type){
214 		case HDR_ACCEPT_T:
215 		case HDR_ALLOW_T:
216 		case HDR_AUTHORIZATION_T:
217 		case HDR_CONTACT_T:
218 		case HDR_CONTENTDISPOSITION_T:
219 		case HDR_CSEQ_T:
220 		case HDR_DATE_T:
221 		case HDR_DIVERSION_T:
222 		case HDR_EVENT_T:
223 		case HDR_EXPIRES_T:
224 		case HDR_MIN_EXPIRES_T:
225 		case HDR_FROM_T:
226 		case HDR_IDENTITY_INFO_T:
227 		case HDR_IDENTITY_T:
228 		case HDR_PAI_T:
229 		case HDR_PPI_T:
230 		case HDR_PROXYAUTH_T:
231 		case HDR_RECORDROUTE_T:
232 		case HDR_REFER_TO_T:
233 		case HDR_ROUTE_T:
234 		case HDR_RPID_T:
235 		case HDR_SESSIONEXPIRES_T:
236 		case HDR_SIPIFMATCH_T:
237 		case HDR_SUBSCRIPTION_STATE_T:
238 		case HDR_SUPPORTED_T:
239 		case HDR_TO_T:
240 		case HDR_VIA_T:
241 			return 1;
242 		default:
243 			return 0;
244 	}
245 }
246 
247 /** frees a hdr_field structure.
248  * WARNING: it frees only parsed (and not name.s, body.s)
249  */
250 void clean_hdr_field(struct hdr_field* const hf);
251 
252 
253 /** frees a hdr_field list.
254  * WARNING: frees only ->parsed and ->next
255  */
256 void free_hdr_field_lst(struct hdr_field* hf);
257 
258 /* print content of hdr_field */
259 void dump_hdr_field( struct hdr_field const* const hf);
260 
261 /**
262  * free hdr parsed structure using inner free function
263  * - hdr parsed struct must have as first file a free function,
264  *   so it can be caseted to hf_parsed_t
265  */
266 void hdr_free_parsed(void **h_parsed);
267 
268 #endif /* HF_H */
269