1 /*
2  * Copyright (C) 2001-2004 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  * \file
23  * \brief Kamailio core :: Destination set handling
24  * \ingroup core
25  * Module: \ref core
26  */
27 
28 #ifndef _DSET_H
29 #define _DSET_H
30 
31 #include "ip_addr.h"
32 #include "qvalue.h"
33 #include "flags.h"
34 #include "parser/msg_parser.h"
35 
36 
37 extern unsigned int nr_branches;
38 extern int ruri_is_new;
39 
40 #define DS_FLAGS    1
41 #define DS_PATH     2
42 
43 /*! \brief
44  * Structure for storing branch attributes
45  */
46 typedef struct branch
47 {
48     char uri[MAX_URI_SIZE];
49     unsigned int len;
50 
51     /* Real destination of the request */
52     char dst_uri[MAX_URI_SIZE];
53     unsigned int dst_uri_len;
54 
55     /* Path set */
56     char path[MAX_PATH_SIZE];
57     unsigned int path_len;
58 
59     int q; /* Preference of the contact among
60         * contact within the array */
61     struct socket_info* force_send_socket;
62 
63     /* +sip.instance contact header param value */
64     char instance[MAX_INSTANCE_SIZE];
65     unsigned int instance_len;
66 
67     /* reg-id contact header param value */
68     unsigned int reg_id;
69 
70     /* ruid value from usrloc */
71     char ruid[MAX_RUID_SIZE];
72     unsigned int ruid_len;
73 
74     char location_ua[MAX_UA_SIZE + 1];
75     unsigned int location_ua_len;
76 
77 	/* tcp connection id */
78 	int otcpid;
79 
80     /* Branch flags */
81     flag_t flags;
82 } branch_t;
83 
84 typedef struct branch_data {
85 	str uri;
86 	str dst_uri;
87 	qvalue_t q;
88 	str path;
89 	unsigned int flags;
90 	socket_info_t* force_socket;
91 	str ruid;
92 	str instance;
93 	str location_ua;
94 	int otcpid;
95 } branch_data_t;
96 
97 /*! \brief
98  * Return pointer to branch[idx] structure
99  */
100 branch_t *get_sip_branch(int idx);
101 
102 /*! \brief
103  * Drop branch[idx]
104  */
105 int drop_sip_branch(int idx);
106 
107 /*! \brief
108  * Push a new branch to current destination set
109  */
110 branch_t *ksr_push_branch(sip_msg_t* msg, str* uri, str* dst_uri, str* path,
111 		  qvalue_t q, unsigned int flags,
112 		  struct socket_info* force_socket,
113 		  str* instance, unsigned int reg_id,
114 		  str* ruid, str* location_ua);
115 
116 /*! \brief
117  * Add a new branch to current destination set
118  */
119 int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
120 		  qvalue_t q, unsigned int flags,
121 		  struct socket_info* force_socket,
122 		  str* instance, unsigned int reg_id,
123 		  str* ruid, str* location_ua);
124 
125 /*! \brief kamailio compatible version */
126 #define km_append_branch(msg, uri, dst_uri, path, q, flags, force_socket) \
127     append_branch(msg, uri, dst_uri, path, q, flags, force_socket, 0, 0, 0, 0)
128 
129 /*! \brief ser compatible append_branch version.
130  *  append_branch version compatible with ser: no path or branch flags support
131  *  and no str parameters.
132  */
ser_append_branch(struct sip_msg * msg,char * uri,int uri_len,char * dst_uri,int dst_uri_len,qvalue_t q,struct socket_info * force_socket)133 static inline int ser_append_branch(struct sip_msg* msg,
134 				    char* uri, int uri_len,
135 				    char* dst_uri, int dst_uri_len,
136 				    qvalue_t q,
137 				    struct socket_info* force_socket)
138 {
139     str s_uri, s_dst_uri;
140     s_uri.s=uri;
141     s_uri.len=uri_len;
142     s_dst_uri.s=dst_uri;
143     s_dst_uri.len=dst_uri_len;
144     return append_branch(msg, &s_uri, &s_dst_uri, 0, q, 0, force_socket, 0, 0, 0, 0);
145 }
146 
147 
148 
149 /*! \brief
150  * Init the index to iterate through the list of transaction branches
151  */
152 void init_branch_iterator(void);
153 
154 /*! \brief
155  * Return branch iterator position
156  */
157 int get_branch_iterator(void);
158 
159 /*! \brief
160  * Set branch iterator position
161  */
162 void set_branch_iterator(int n);
163 
164 /*! \brief Get the next branch in the current transaction.
165  * @return pointer to the uri of the next branch (which the length written in
166  *  *len) or 0 if there are no more branches.
167  */
168 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
169 		  unsigned int* flags, struct socket_info** force_socket,
170 		  str *ruid, str *instance, str *location_ua);
171 
172 char* get_branch( unsigned int i, int* len, qvalue_t* q, str* dst_uri,
173 		  str* path, unsigned int *flags,
174 		  struct socket_info** force_socket,
175 		  str* ruid, str *instance, str *location_ua);
176 
177 int get_branch_data(unsigned int i, branch_data_t *vbranch);
178 int next_branch_data(branch_data_t *vbranch);
179 
180 /*! \brief
181  * Empty the array of branches
182  */
183 void clear_branches(void);
184 
185 
186 /*! \brief
187  * Create a Contact header field from the
188  * list of current branches
189  */
190 char* print_dset(struct sip_msg* msg, int* len, int options);
191 
192 
193 /*! \brief
194  * Set the q value of the Request-URI
195  */
196 void set_ruri_q(qvalue_t q);
197 
198 
199 /*! \brief
200  * Get src ip, port and proto as SIP uri or proxy address
201  */
202 int msg_get_src_addr(sip_msg_t *msg, str *uri, int mode);
203 
204 /*! \brief
205  * Get the q value of the Request-URI
206  */
207 qvalue_t get_ruri_q(void);
208 
209 
210 
211 /*
212  * Get actual Request-URI
213  */
get_request_uri(struct sip_msg * _m,str * _u)214 inline static int get_request_uri(struct sip_msg* _m, str* _u)
215 {
216 	*_u=*GET_RURI(_m);
217 	return 0;
218 }
219 
220 
221 #define ruri_mark_new() (ruri_is_new = 1)
222 
223 #define ruri_mark_consumed()  (ruri_is_new = 0)
224 
225 /** returns whether or not ruri should be used when forking.
226   * (useful for serial forking)
227   * @return 0 if already marked as consumed, 1 if not.
228  */
229 #define ruri_get_forking_state() (ruri_is_new)
230 
231 int rewrite_uri(struct sip_msg* _m, str* _s);
232 
233 /*! \brief
234  * Set a per-branch flag to 1.
235  *
236  * This function sets the value of one particular branch flag to 1.
237  * @param branch Number of the branch (0 for the main Request-URI branch)
238  * @param flag Number of the flag to be set (starting with 0)
239  * @return 1 on success, -1 on failure.
240  */
241 int setbflag(unsigned int branch, flag_t flag);
242 
243 /*! \brief
244  * Reset a per-branch flag value to 0.
245  *
246  * This function resets the value of one particular branch flag to 0.
247  * @param branch Number of the branch (0 for the main Request-URI branch)
248  * @param flag Number of the flag to be reset (starting with 0)
249  * @return 1 on success, -1 on failure.
250  */
251 int resetbflag(unsigned int branch, flag_t flag);
252 
253 /*! \brief
254  * Determine if a branch flag is set.
255  *
256  * This function tests the value of one particular per-branch flag.
257  * @param branch Number of the branch (0 for the main Request-URI branch)
258  * @param flag Number of the flag to be tested (starting with 0)
259  * @return 1 if the branch flag is set, -1 if not or on failure.
260  */
261 int isbflagset(unsigned int branch, flag_t flag);
262 
263 /*! \brief
264  * Get the value of all branch flags for a branch
265  *
266  * This function returns the value of all branch flags
267  * combined in a single variable.
268  * @param branch Number of the branch (0 for the main Request-URI branch)
269  * @param res A pointer to a variable to store the result
270  * @return 1 on success, -1 on failure
271  */
272 int getbflagsval(unsigned int branch, flag_t* res);
273 
274 /*! \brief
275  * Set the value of all branch flags at once for a given branch.
276  *
277  * This function sets the value of all branch flags for a given
278  * branch at once.
279  * @param branch Number of the branch (0 for the main Request-URI branch)
280  * @param val All branch flags combined into a single variable
281  * @return 1 on success, -1 on failure
282  */
283 int setbflagsval(unsigned int branch, flag_t val);
284 
285 int uri_add_rcv_alias(sip_msg_t *msg, str *uri, str *nuri);
286 int uri_restore_rcv_alias(str *uri, str *nuri, str *suri);
287 int uri_trim_rcv_alias(str *uri, str *nuri);
288 
289 int init_dst_set(void);
290 
291 int set_aor_case_sensitive(int mode);
292 
293 int get_aor_case_sensitive(void);
294 
295 #endif /* _DSET_H */
296