modules/ims_qos: added patch for flow-description bug when request originates from...
[sip-router] / src / core / parser / hf.h
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_EOH_T                                              /*!< End of message header */
109 };
110
111
112 typedef unsigned long long hdr_flags_t;
113
114 /** type to flag conversion.
115  * WARNING: HDR_ERROR_T has no corresponding FLAG ! */
116 #define HDR_T2F(type)   \
117                 (((type)!=HDR_EOH_T)?((hdr_flags_t)1<<(type)):(~(hdr_flags_t)0))
118
119 /** helper macro for easy defining and keeping in sync the flags enum. */
120 #define HDR_F_DEF(name)         HDR_T2F(HDR_##name##_T)
121
122 /** @name flags definitions.
123  * (enum won't work with all the compiler (e.g. icc) due to the 64bit size) */
124 /*!{ */
125 #define HDR_EOH_F                                       HDR_F_DEF(EOH)
126 #define HDR_VIA_F                                       HDR_F_DEF(VIA)
127 #define HDR_VIA1_F                                      HDR_F_DEF(VIA1)
128 #define HDR_VIA2_F                                      HDR_F_DEF(VIA2)
129 #define HDR_TO_F                                        HDR_F_DEF(TO)
130 #define HDR_FROM_F                                      HDR_F_DEF(FROM)
131 #define HDR_CSEQ_F                                      HDR_F_DEF(CSEQ)
132 #define HDR_CALLID_F                            HDR_F_DEF(CALLID)
133 #define HDR_CONTACT_F                           HDR_F_DEF(CONTACT)
134 #define HDR_MAXFORWARDS_F                       HDR_F_DEF(MAXFORWARDS)
135 #define HDR_ROUTE_F                                     HDR_F_DEF(ROUTE)
136 #define HDR_RECORDROUTE_F                       HDR_F_DEF(RECORDROUTE)
137 #define HDR_CONTENTTYPE_F                       HDR_F_DEF(CONTENTTYPE)
138 #define HDR_CONTENTLENGTH_F                     HDR_F_DEF(CONTENTLENGTH)
139 #define HDR_AUTHORIZATION_F                     HDR_F_DEF(AUTHORIZATION)
140 #define HDR_EXPIRES_F                           HDR_F_DEF(EXPIRES)
141 #define HDR_MIN_EXPIRES_F                       HDR_F_DEF(MIN_EXPIRES)
142 #define HDR_PROXYAUTH_F                         HDR_F_DEF(PROXYAUTH)
143 #define HDR_SUPPORTED_F                         HDR_F_DEF(SUPPORTED)
144 #define HDR_REQUIRE_F                           HDR_F_DEF(REQUIRE)
145 #define HDR_PROXYREQUIRE_F                      HDR_F_DEF(PROXYREQUIRE)
146 #define HDR_UNSUPPORTED_F                       HDR_F_DEF(UNSUPPORTED)
147 #define HDR_ALLOW_F                                     HDR_F_DEF(ALLOW)
148 #define HDR_EVENT_F                                     HDR_F_DEF(EVENT)
149 #define HDR_ACCEPT_F                            HDR_F_DEF(ACCEPT)
150 #define HDR_ACCEPTLANGUAGE_F            HDR_F_DEF(ACCEPTLANGUAGE)
151 #define HDR_ORGANIZATION_F                      HDR_F_DEF(ORGANIZATION)
152 #define HDR_PRIORITY_F                          HDR_F_DEF(PRIORITY)
153 #define HDR_SUBJECT_F                           HDR_F_DEF(SUBJECT)
154 #define HDR_USERAGENT_F                         HDR_F_DEF(USERAGENT)
155 #define HDR_SERVER_F                            HDR_F_DEF(SERVER)
156 #define HDR_CONTENTDISPOSITION_F        HDR_F_DEF(CONTENTDISPOSITION)
157 #define HDR_DIVERSION_F                         HDR_F_DEF(DIVERSION)
158 #define HDR_RPID_F                                      HDR_F_DEF(RPID)
159 #define HDR_REFER_TO_F                          HDR_F_DEF(REFER_TO)
160 #define HDR_SIPIFMATCH_F                        HDR_F_DEF(SIPIFMATCH)
161 #define HDR_SESSIONEXPIRES_F            HDR_F_DEF(SESSIONEXPIRES)
162 #define HDR_MIN_SE_F                            HDR_F_DEF(MIN_SE)
163 #define HDR_SUBSCRIPTION_STATE_F        HDR_F_DEF(SUBSCRIPTION_STATE)
164 #define HDR_ACCEPTCONTACT_F                     HDR_F_DEF(ACCEPTCONTACT)
165 #define HDR_ALLOWEVENTS_F                       HDR_F_DEF(ALLOWEVENTS)
166 #define HDR_CONTENTENCODING_F           HDR_F_DEF(CONTENTENCODING)
167 #define HDR_REFERREDBY_F                        HDR_F_DEF(REFERREDBY)
168 #define HDR_REJECTCONTACT_F                     HDR_F_DEF(REJECTCONTACT)
169 #define HDR_REQUESTDISPOSITION_F        HDR_F_DEF(REQUESTDISPOSITION)
170 #define HDR_WWW_AUTHENTICATE_F          HDR_F_DEF(WWW_AUTHENTICATE)
171 #define HDR_PROXY_AUTHENTICATE_F        HDR_F_DEF(PROXY_AUTHENTICATE)
172 #define HDR_DATE_F                      HDR_F_DEF(DATE)
173 #define HDR_IDENTITY_F                  HDR_F_DEF(IDENTITY)
174 #define HDR_IDENTITY_INFO_F             HDR_F_DEF(IDENTITY_INFO)
175 #define HDR_RETRY_AFTER_F                       HDR_F_DEF(RETRY_AFTER)
176 #define HDR_PPI_F                   HDR_F_DEF(PPI)
177 #define HDR_PAI_F                   HDR_F_DEF(PAI)
178 #define HDR_PATH_F                  HDR_F_DEF(PATH)
179 #define HDR_PRIVACY_F               HDR_F_DEF(PRIVACY)
180 #define HDR_REASON_F                            HDR_F_DEF(REASON)
181
182 #define HDR_OTHER_F                                     HDR_F_DEF(OTHER)
183
184 /*!} */ /* Doxygen end marker*/
185
186 typedef enum _hdr_types_t hdr_types_t;
187
188 /** Format: name':' body.
189  */
190 typedef struct hdr_field {
191         hdr_types_t type;       /*!< Header field type */
192         str name;               /*!< Header field name */
193         str body;               /*!< Header field body (may not include CRLF) */
194         int len;                /*!< length from hdr start until EoHF (incl.CRLF) */
195         void* parsed;           /*!< Parsed data structures */
196         struct hdr_field* next; /*!< Next header field in the list */
197 } hdr_field_t;
198
199
200 /* type of the function to free the structure of parsed header field */
201 typedef void (*hf_parsed_free_f)(void *parsed);
202
203 /* structure to hold the function to free the parsed header field */
204 typedef struct hdr_parsed {
205         hf_parsed_free_f hfree;
206 } hf_parsed_t;
207
208 /** returns true if the header links allocated memory on parse field. */
209 static inline int hdr_allocs_parse(struct hdr_field* hdr)
210 {
211         switch(hdr->type){
212                 case HDR_ACCEPT_T:
213                 case HDR_ALLOW_T:
214                 case HDR_AUTHORIZATION_T:
215                 case HDR_CONTACT_T:
216                 case HDR_CONTENTDISPOSITION_T:
217                 case HDR_CSEQ_T:
218                 case HDR_DATE_T:
219                 case HDR_DIVERSION_T:
220                 case HDR_EVENT_T:
221                 case HDR_EXPIRES_T:
222                 case HDR_MIN_EXPIRES_T:
223                 case HDR_FROM_T:
224                 case HDR_IDENTITY_INFO_T:
225                 case HDR_IDENTITY_T:
226                 case HDR_PAI_T:
227                 case HDR_PPI_T:
228                 case HDR_PROXYAUTH_T:
229                 case HDR_RECORDROUTE_T:
230                 case HDR_REFER_TO_T:
231                 case HDR_ROUTE_T:
232                 case HDR_RPID_T:
233                 case HDR_SESSIONEXPIRES_T:
234                 case HDR_SIPIFMATCH_T:
235                 case HDR_SUBSCRIPTION_STATE_T:
236                 case HDR_SUPPORTED_T:
237                 case HDR_TO_T:
238                 case HDR_VIA_T:
239                         return 1;
240                 default:
241                         return 0;
242         }
243 }
244
245 /** frees a hdr_field structure.
246  * WARNING: it frees only parsed (and not name.s, body.s)
247  */
248 void clean_hdr_field(struct hdr_field* const hf);
249
250
251 /** frees a hdr_field list.
252  * WARNING: frees only ->parsed and ->next
253  */
254 void free_hdr_field_lst(struct hdr_field* hf);
255
256 /* print content of hdr_field */
257 void dump_hdr_field( struct hdr_field const* const hf);
258
259 /**
260  * free hdr parsed structure using inner free function
261  * - hdr parsed struct must have as first file a free function,
262  *   so it can be caseted to hf_parsed_t
263  */
264 void hdr_free_parsed(void **h_parsed);
265
266 #endif /* HF_H */