- utils keeps only the applications related to kamailio c code
- the other are now in misc/tools
- utils/misc/vim moved to misc/extra/
- obsolete and scripts folders moved to misc/
1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,408 +0,0 @@ |
1 |
-/* |
|
2 |
- * $Id$ |
|
3 |
- * |
|
4 |
- * Lookup contacts in usrloc |
|
5 |
- * |
|
6 |
- * Copyright (C) 2001-2003 FhG Fokus |
|
7 |
- * |
|
8 |
- * This file is part of ser, a free SIP server. |
|
9 |
- * |
|
10 |
- * ser is free software; you can redistribute it and/or modify |
|
11 |
- * it under the terms of the GNU General Public License as published by |
|
12 |
- * the Free Software Foundation; either version 2 of the License, or |
|
13 |
- * (at your option) any later version |
|
14 |
- * |
|
15 |
- * For a license to use the ser software under conditions |
|
16 |
- * other than those described here, or to purchase support for this |
|
17 |
- * software, please contact iptel.org by e-mail at the following addresses: |
|
18 |
- * info@iptel.org |
|
19 |
- * |
|
20 |
- * ser is distributed in the hope that it will be useful, |
|
21 |
- * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22 |
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23 |
- * GNU General Public License for more details. |
|
24 |
- * |
|
25 |
- * You should have received a copy of the GNU General Public License |
|
26 |
- * along with this program; if not, write to the Free Software |
|
27 |
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
28 |
- * |
|
29 |
- * History: |
|
30 |
- * --------- |
|
31 |
- * 2005-08-01 fix: "received=" is used also for the branches (andrei) |
|
32 |
- * 2005-04-22 added received_to_uri option support (andrei) |
|
33 |
- * 2003-03-12 added support for zombie state (nils) |
|
34 |
- * 2005-02-25 lookup() forces the socket stored in USRLOC (bogdan) |
|
35 |
- */ |
|
36 |
- |
|
37 |
- |
|
38 |
-#include <string.h> |
|
39 |
-#include "../../ut.h" |
|
40 |
-#include "../../dset.h" |
|
41 |
-#include "../../str.h" |
|
42 |
-#include "../../config.h" |
|
43 |
-#include "../../action.h" |
|
44 |
-#include "../usrloc/usrloc.h" |
|
45 |
-#include "../../error.h" |
|
46 |
-#include "../../id.h" |
|
47 |
-#include "../../sr_module.h" |
|
48 |
-#include "common.h" |
|
49 |
-#include "regtime.h" |
|
50 |
-#include "reg_mod.h" |
|
51 |
-#include "lookup.h" |
|
52 |
-#include "../../usr_avp.h" |
|
53 |
- |
|
54 |
- |
|
55 |
- |
|
56 |
-/* new_uri= uri "; received=" received |
|
57 |
- * returns 0 on success, < 0 on error (out of memory) |
|
58 |
- * new_uri.s is pkg_malloc'ed (so you must pkg_free it when you are done |
|
59 |
- * with it) |
|
60 |
- */ |
|
61 |
-static inline int add_received(str* new_uri, str* uri, str* received) |
|
62 |
-{ |
|
63 |
- /* copy received into an uri param */ |
|
64 |
- new_uri->len=uri->len+RECEIVED_LEN+1+received->len+1; |
|
65 |
- new_uri->s=pkg_malloc(new_uri->len+1); |
|
66 |
- if (new_uri->s==0){ |
|
67 |
- LOG(L_ERR, "ERROR: add_received(): out of memory\n"); |
|
68 |
- return -1; |
|
69 |
- } |
|
70 |
- memcpy(new_uri->s, uri->s, uri->len); |
|
71 |
- memcpy(new_uri->s+uri->len, RECEIVED, RECEIVED_LEN); |
|
72 |
- new_uri->s[uri->len+RECEIVED_LEN]='"'; |
|
73 |
- memcpy(new_uri->s+uri->len+RECEIVED_LEN+1, received->s, received->len); |
|
74 |
- new_uri->s[new_uri->len-1]='"'; |
|
75 |
- new_uri->s[new_uri->len]=0; /* for debugging */ |
|
76 |
- return 0; |
|
77 |
-} |
|
78 |
- |
|
79 |
- |
|
80 |
- |
|
81 |
-/* |
|
82 |
- * Lookup contact in the database and rewrite Request-URI |
|
83 |
- */ |
|
84 |
-int lookup(struct sip_msg* _m, char* _t, char* _s) |
|
85 |
-{ |
|
86 |
- urecord_t* r; |
|
87 |
- str uid; |
|
88 |
- ucontact_t* ptr; |
|
89 |
- int res; |
|
90 |
- unsigned int nat; |
|
91 |
- str new_uri; |
|
92 |
- |
|
93 |
- nat = 0; |
|
94 |
- |
|
95 |
- if (get_to_uid(&uid, _m) < 0) return -1; |
|
96 |
- |
|
97 |
- get_act_time(); |
|
98 |
- |
|
99 |
- ul.lock_udomain((udomain_t*)_t); |
|
100 |
- res = ul.get_urecord((udomain_t*)_t, &uid, &r); |
|
101 |
- if (res < 0) { |
|
102 |
- LOG(L_ERR, "lookup(): Error while querying usrloc\n"); |
|
103 |
- ul.unlock_udomain((udomain_t*)_t); |
|
104 |
- return -2; |
|
105 |
- } |
|
106 |
- |
|
107 |
- if (res > 0) { |
|
108 |
- DBG("lookup(): '%.*s' Not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
109 |
- ul.unlock_udomain((udomain_t*)_t); |
|
110 |
- return -3; |
|
111 |
- } |
|
112 |
- |
|
113 |
- ptr = r->contacts; |
|
114 |
- while ((ptr) && !VALID_CONTACT(ptr, act_time)) |
|
115 |
- ptr = ptr->next; |
|
116 |
- |
|
117 |
- if (ptr) { |
|
118 |
- if (ptr->received.s && ptr->received.len) { |
|
119 |
- if (received_to_uri){ |
|
120 |
- if (add_received(&new_uri, &ptr->c, &ptr->received)<0){ |
|
121 |
- LOG(L_ERR, "ERROR: lookup(): out of memory\n"); |
|
122 |
- return -4; |
|
123 |
- } |
|
124 |
- /* replace the msg uri */ |
|
125 |
- if (_m->new_uri.s) pkg_free(_m->new_uri.s); |
|
126 |
- _m->new_uri=new_uri; |
|
127 |
- _m->parsed_uri_ok=0; |
|
128 |
- ruri_mark_new(); |
|
129 |
- goto skip_rewrite_uri; |
|
130 |
- }else if (set_dst_uri(_m, &ptr->received) < 0) { |
|
131 |
- ul.unlock_udomain((udomain_t*)_t); |
|
132 |
- return -4; |
|
133 |
- } |
|
134 |
- } |
|
135 |
- |
|
136 |
- if (rewrite_uri(_m, &ptr->c) < 0) { |
|
137 |
- LOG(L_ERR, "lookup(): Unable to rewrite Request-URI\n"); |
|
138 |
- ul.unlock_udomain((udomain_t*)_t); |
|
139 |
- return -4; |
|
140 |
- } |
|
141 |
- |
|
142 |
- if (ptr->sock) { |
|
143 |
- set_force_socket(_m, ptr->sock); |
|
144 |
- } |
|
145 |
- |
|
146 |
-skip_rewrite_uri: |
|
147 |
- |
|
148 |
- set_ruri_q(ptr->q); |
|
149 |
- |
|
150 |
- nat |= ptr->flags & FL_NAT; |
|
151 |
- ptr = ptr->next; |
|
152 |
- } else { |
|
153 |
- /* All contacts expired */ |
|
154 |
- ul.unlock_udomain((udomain_t*)_t); |
|
155 |
- return -5; |
|
156 |
- } |
|
157 |
- |
|
158 |
- /* Append branches if enabled */ |
|
159 |
- if (!append_branches) goto skip; |
|
160 |
- |
|
161 |
- while(ptr) { |
|
162 |
- if (VALID_CONTACT(ptr, act_time)) { |
|
163 |
- if (received_to_uri && ptr->received.s && ptr->received.len){ |
|
164 |
- if (add_received(&new_uri, &ptr->c, &ptr->received)<0){ |
|
165 |
- LOG(L_ERR, "ERROR: lookup(): branch: out of memory\n"); |
|
166 |
- goto cont; /* try to continue */ |
|
167 |
- } |
|
168 |
- if (append_branch(_m, &new_uri, 0, 0, ptr->q, |
|
169 |
- 0, 0, 0, 0) == -1) { |
|
170 |
- LOG(L_ERR, "lookup(): Error while appending a branch\n"); |
|
171 |
- pkg_free(new_uri.s); |
|
172 |
- if (ser_error==E_TOO_MANY_BRANCHES) goto skip; |
|
173 |
- else goto cont; /* try to continue, maybe we have an |
|
174 |
- oversized contact */ |
|
175 |
- } |
|
176 |
- pkg_free(new_uri.s); /* append_branch doesn't free it */ |
|
177 |
- }else{ |
|
178 |
- if (append_branch(_m, &ptr->c, &ptr->received, |
|
179 |
- 0 /* path */, |
|
180 |
- ptr->q, 0 /* brflags*/, |
|
181 |
- ptr->sock, 0, 0) == -1) { |
|
182 |
- LOG(L_ERR, "lookup(): Error while appending a branch\n"); |
|
183 |
- goto skip; /* Return OK here so the function succeeds */ |
|
184 |
- } |
|
185 |
- } |
|
186 |
- |
|
187 |
- nat |= ptr->flags & FL_NAT; |
|
188 |
- } |
|
189 |
-cont: |
|
190 |
- ptr = ptr->next; |
|
191 |
- } |
|
192 |
- |
|
193 |
- skip: |
|
194 |
- ul.unlock_udomain((udomain_t*)_t); |
|
195 |
- if (nat) setflag(_m, load_nat_flag); |
|
196 |
- return 1; |
|
197 |
-} |
|
198 |
- |
|
199 |
- |
|
200 |
-/* |
|
201 |
- * Return true if the AOR in the Request-URI is registered, |
|
202 |
- * it is similar to lookup but registered neither rewrites |
|
203 |
- * the Request-URI nor appends branches |
|
204 |
- */ |
|
205 |
-int registered(struct sip_msg* _m, char* _t, char* _s) |
|
206 |
-{ |
|
207 |
- str uid; |
|
208 |
- urecord_t* r; |
|
209 |
- ucontact_t* ptr; |
|
210 |
- int res; |
|
211 |
- |
|
212 |
- if (get_to_uid(&uid, _m) < 0) return -1; |
|
213 |
- |
|
214 |
- ul.lock_udomain((udomain_t*)_t); |
|
215 |
- res = ul.get_urecord((udomain_t*)_t, &uid, &r); |
|
216 |
- |
|
217 |
- if (res < 0) { |
|
218 |
- ul.unlock_udomain((udomain_t*)_t); |
|
219 |
- LOG(L_ERR, "registered(): Error while querying usrloc\n"); |
|
220 |
- return -1; |
|
221 |
- } |
|
222 |
- |
|
223 |
- if (res == 0) { |
|
224 |
- ptr = r->contacts; |
|
225 |
- while (ptr && !VALID_CONTACT(ptr, act_time)) { |
|
226 |
- ptr = ptr->next; |
|
227 |
- } |
|
228 |
- |
|
229 |
- if (ptr) { |
|
230 |
- ul.unlock_udomain((udomain_t*)_t); |
|
231 |
- DBG("registered(): '%.*s' found in usrloc\n", uid.len, ZSW(uid.s)); |
|
232 |
- return 1; |
|
233 |
- } |
|
234 |
- } |
|
235 |
- |
|
236 |
- ul.unlock_udomain((udomain_t*)_t); |
|
237 |
- DBG("registered(): '%.*s' not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
238 |
- return -1; |
|
239 |
-} |
|
240 |
- |
|
241 |
-/* |
|
242 |
- * Lookup contact in the database and rewrite Request-URI, |
|
243 |
- * and filter them by aor |
|
244 |
- */ |
|
245 |
-int lookup2(struct sip_msg* msg, char* table, char* p2) |
|
246 |
-{ |
|
247 |
- urecord_t* r; |
|
248 |
- str uid; |
|
249 |
- ucontact_t* ptr; |
|
250 |
- int res; |
|
251 |
- unsigned int nat; |
|
252 |
- str new_uri, aor; |
|
253 |
- |
|
254 |
- nat = 0; |
|
255 |
- |
|
256 |
- if (get_str_fparam(&aor, msg, (fparam_t*)p2) != 0) { |
|
257 |
- ERR("Unable to get the AOR value\n"); |
|
258 |
- return -1; |
|
259 |
- } |
|
260 |
- |
|
261 |
- if (get_to_uid(&uid, msg) < 0) return -1; |
|
262 |
- get_act_time(); |
|
263 |
- |
|
264 |
- ul.lock_udomain((udomain_t*)table); |
|
265 |
- res = ul.get_urecord((udomain_t*)table, &uid, &r); |
|
266 |
- if (res < 0) { |
|
267 |
- ERR("Error while querying usrloc\n"); |
|
268 |
- ul.unlock_udomain((udomain_t*)table); |
|
269 |
- return -2; |
|
270 |
- } |
|
271 |
- |
|
272 |
- if (res > 0) { |
|
273 |
- DBG("'%.*s' Not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
274 |
- ul.unlock_udomain((udomain_t*)table); |
|
275 |
- return -3; |
|
276 |
- } |
|
277 |
- |
|
278 |
- ptr = r->contacts; |
|
279 |
- while (ptr && (!VALID_CONTACT(ptr, act_time) || !VALID_AOR(ptr, aor))) |
|
280 |
- ptr = ptr->next; |
|
281 |
- |
|
282 |
- if (ptr) { |
|
283 |
- if (ptr->received.s && ptr->received.len) { |
|
284 |
- if (received_to_uri){ |
|
285 |
- if (add_received(&new_uri, &ptr->c, &ptr->received) < 0) { |
|
286 |
- ERR("Out of memory\n"); |
|
287 |
- return -4; |
|
288 |
- } |
|
289 |
- /* replace the msg uri */ |
|
290 |
- if (msg->new_uri.s) pkg_free(msg->new_uri.s); |
|
291 |
- msg->new_uri = new_uri; |
|
292 |
- msg->parsed_uri_ok = 0; |
|
293 |
- ruri_mark_new(); |
|
294 |
- goto skip_rewrite_uri; |
|
295 |
- } else if (set_dst_uri(msg, &ptr->received) < 0) { |
|
296 |
- ul.unlock_udomain((udomain_t*)table); |
|
297 |
- return -4; |
|
298 |
- } |
|
299 |
- } |
|
300 |
- |
|
301 |
- if (rewrite_uri(msg, &ptr->c) < 0) { |
|
302 |
- ERR("Unable to rewrite Request-URI\n"); |
|
303 |
- ul.unlock_udomain((udomain_t*)table); |
|
304 |
- return -4; |
|
305 |
- } |
|
306 |
- |
|
307 |
- if (ptr->sock) { |
|
308 |
- set_force_socket(msg, ptr->sock); |
|
309 |
- } |
|
310 |
- |
|
311 |
-skip_rewrite_uri: |
|
312 |
- set_ruri_q(ptr->q); |
|
313 |
- |
|
314 |
- nat |= ptr->flags & FL_NAT; |
|
315 |
- ptr = ptr->next; |
|
316 |
- } else { |
|
317 |
- /* All contacts expired */ |
|
318 |
- ul.unlock_udomain((udomain_t*)table); |
|
319 |
- return -5; |
|
320 |
- } |
|
321 |
- |
|
322 |
- /* Append branches if enabled */ |
|
323 |
- if (!append_branches) goto skip; |
|
324 |
- |
|
325 |
- while(ptr) { |
|
326 |
- if (VALID_CONTACT(ptr, act_time) && VALID_AOR(ptr, aor)) { |
|
327 |
- if (received_to_uri && ptr->received.s && ptr->received.len) { |
|
328 |
- if (add_received(&new_uri, &ptr->c, &ptr->received) < 0) { |
|
329 |
- ERR("branch: out of memory\n"); |
|
330 |
- goto cont; /* try to continue */ |
|
331 |
- } |
|
332 |
- if (append_branch(msg, &new_uri, 0, 0, ptr->q, |
|
333 |
- 0, 0, 0, 0) == -1) { |
|
334 |
- ERR("Error while appending a branch\n"); |
|
335 |
- pkg_free(new_uri.s); |
|
336 |
- if (ser_error == E_TOO_MANY_BRANCHES) goto skip; |
|
337 |
- else goto cont; /* try to continue, maybe we have an |
|
338 |
- oversized contact */ |
|
339 |
- } |
|
340 |
- pkg_free(new_uri.s); /* append_branch doesn't free it */ |
|
341 |
- } else { |
|
342 |
- if (append_branch(msg, &ptr->c, &ptr->received, |
|
343 |
- 0 /* path */, |
|
344 |
- ptr->q, 0, ptr->sock, |
|
345 |
- 0, 0) == -1) { |
|
346 |
- ERR("Error while appending a branch\n"); |
|
347 |
- goto skip; /* Return OK here so the function succeeds */ |
|
348 |
- } |
|
349 |
- } |
|
350 |
- |
|
351 |
- nat |= ptr->flags & FL_NAT; |
|
352 |
- } |
|
353 |
-cont: |
|
354 |
- ptr = ptr->next; |
|
355 |
- } |
|
356 |
- |
|
357 |
- skip: |
|
358 |
- ul.unlock_udomain((udomain_t*)table); |
|
359 |
- if (nat) setflag(msg, load_nat_flag); |
|
360 |
- return 1; |
|
361 |
-} |
|
362 |
- |
|
363 |
- |
|
364 |
-/* |
|
365 |
- * Return true if the AOR in the Request-URI is registered, |
|
366 |
- * it is similar to lookup but registered neither rewrites |
|
367 |
- * the Request-URI nor appends branches |
|
368 |
- */ |
|
369 |
-int registered2(struct sip_msg* _m, char* _t, char* p2) |
|
370 |
-{ |
|
371 |
- str uid, aor; |
|
372 |
- urecord_t* r; |
|
373 |
- ucontact_t* ptr; |
|
374 |
- int res; |
|
375 |
- |
|
376 |
- if (get_str_fparam(&aor, _m, (fparam_t*)p2) != 0) { |
|
377 |
- ERR("Unable to get the AOR value\n"); |
|
378 |
- return -1; |
|
379 |
- } |
|
380 |
- |
|
381 |
- if (get_to_uid(&uid, _m) < 0) return -1; |
|
382 |
- |
|
383 |
- ul.lock_udomain((udomain_t*)_t); |
|
384 |
- res = ul.get_urecord((udomain_t*)_t, &uid, &r); |
|
385 |
- |
|
386 |
- if (res < 0) { |
|
387 |
- ul.unlock_udomain((udomain_t*)_t); |
|
388 |
- LOG(L_ERR, "registered(): Error while querying usrloc\n"); |
|
389 |
- return -1; |
|
390 |
- } |
|
391 |
- |
|
392 |
- if (res == 0) { |
|
393 |
- ptr = r->contacts; |
|
394 |
- while (ptr && (!VALID_CONTACT(ptr, act_time) || !VALID_AOR(ptr, aor))) { |
|
395 |
- ptr = ptr->next; |
|
396 |
- } |
|
397 |
- |
|
398 |
- if (ptr) { |
|
399 |
- ul.unlock_udomain((udomain_t*)_t); |
|
400 |
- DBG("registered(): '%.*s' found in usrloc\n", uid.len, ZSW(uid.s)); |
|
401 |
- return 1; |
|
402 |
- } |
|
403 |
- } |
|
404 |
- |
|
405 |
- ul.unlock_udomain((udomain_t*)_t); |
|
406 |
- DBG("registered(): '%.*s' not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
407 |
- return -1; |
|
408 |
-} |
... | ... |
@@ -24,7 +24,7 @@ |
24 | 24 |
* |
25 | 25 |
* You should have received a copy of the GNU General Public License |
26 | 26 |
* along with this program; if not, write to the Free Software |
27 |
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
27 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
28 | 28 |
* |
29 | 29 |
* History: |
30 | 30 |
* --------- |
- use instead the other module with same name
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,408 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ * |
|
4 |
+ * Lookup contacts in usrloc |
|
5 |
+ * |
|
6 |
+ * Copyright (C) 2001-2003 FhG Fokus |
|
7 |
+ * |
|
8 |
+ * This file is part of ser, a free SIP server. |
|
9 |
+ * |
|
10 |
+ * ser is free software; you can redistribute it and/or modify |
|
11 |
+ * it under the terms of the GNU General Public License as published by |
|
12 |
+ * the Free Software Foundation; either version 2 of the License, or |
|
13 |
+ * (at your option) any later version |
|
14 |
+ * |
|
15 |
+ * For a license to use the ser software under conditions |
|
16 |
+ * other than those described here, or to purchase support for this |
|
17 |
+ * software, please contact iptel.org by e-mail at the following addresses: |
|
18 |
+ * info@iptel.org |
|
19 |
+ * |
|
20 |
+ * ser is distributed in the hope that it will be useful, |
|
21 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23 |
+ * GNU General Public License for more details. |
|
24 |
+ * |
|
25 |
+ * You should have received a copy of the GNU General Public License |
|
26 |
+ * along with this program; if not, write to the Free Software |
|
27 |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
28 |
+ * |
|
29 |
+ * History: |
|
30 |
+ * --------- |
|
31 |
+ * 2005-08-01 fix: "received=" is used also for the branches (andrei) |
|
32 |
+ * 2005-04-22 added received_to_uri option support (andrei) |
|
33 |
+ * 2003-03-12 added support for zombie state (nils) |
|
34 |
+ * 2005-02-25 lookup() forces the socket stored in USRLOC (bogdan) |
|
35 |
+ */ |
|
36 |
+ |
|
37 |
+ |
|
38 |
+#include <string.h> |
|
39 |
+#include "../../ut.h" |
|
40 |
+#include "../../dset.h" |
|
41 |
+#include "../../str.h" |
|
42 |
+#include "../../config.h" |
|
43 |
+#include "../../action.h" |
|
44 |
+#include "../usrloc/usrloc.h" |
|
45 |
+#include "../../error.h" |
|
46 |
+#include "../../id.h" |
|
47 |
+#include "../../sr_module.h" |
|
48 |
+#include "common.h" |
|
49 |
+#include "regtime.h" |
|
50 |
+#include "reg_mod.h" |
|
51 |
+#include "lookup.h" |
|
52 |
+#include "../../usr_avp.h" |
|
53 |
+ |
|
54 |
+ |
|
55 |
+ |
|
56 |
+/* new_uri= uri "; received=" received |
|
57 |
+ * returns 0 on success, < 0 on error (out of memory) |
|
58 |
+ * new_uri.s is pkg_malloc'ed (so you must pkg_free it when you are done |
|
59 |
+ * with it) |
|
60 |
+ */ |
|
61 |
+static inline int add_received(str* new_uri, str* uri, str* received) |
|
62 |
+{ |
|
63 |
+ /* copy received into an uri param */ |
|
64 |
+ new_uri->len=uri->len+RECEIVED_LEN+1+received->len+1; |
|
65 |
+ new_uri->s=pkg_malloc(new_uri->len+1); |
|
66 |
+ if (new_uri->s==0){ |
|
67 |
+ LOG(L_ERR, "ERROR: add_received(): out of memory\n"); |
|
68 |
+ return -1; |
|
69 |
+ } |
|
70 |
+ memcpy(new_uri->s, uri->s, uri->len); |
|
71 |
+ memcpy(new_uri->s+uri->len, RECEIVED, RECEIVED_LEN); |
|
72 |
+ new_uri->s[uri->len+RECEIVED_LEN]='"'; |
|
73 |
+ memcpy(new_uri->s+uri->len+RECEIVED_LEN+1, received->s, received->len); |
|
74 |
+ new_uri->s[new_uri->len-1]='"'; |
|
75 |
+ new_uri->s[new_uri->len]=0; /* for debugging */ |
|
76 |
+ return 0; |
|
77 |
+} |
|
78 |
+ |
|
79 |
+ |
|
80 |
+ |
|
81 |
+/* |
|
82 |
+ * Lookup contact in the database and rewrite Request-URI |
|
83 |
+ */ |
|
84 |
+int lookup(struct sip_msg* _m, char* _t, char* _s) |
|
85 |
+{ |
|
86 |
+ urecord_t* r; |
|
87 |
+ str uid; |
|
88 |
+ ucontact_t* ptr; |
|
89 |
+ int res; |
|
90 |
+ unsigned int nat; |
|
91 |
+ str new_uri; |
|
92 |
+ |
|
93 |
+ nat = 0; |
|
94 |
+ |
|
95 |
+ if (get_to_uid(&uid, _m) < 0) return -1; |
|
96 |
+ |
|
97 |
+ get_act_time(); |
|
98 |
+ |
|
99 |
+ ul.lock_udomain((udomain_t*)_t); |
|
100 |
+ res = ul.get_urecord((udomain_t*)_t, &uid, &r); |
|
101 |
+ if (res < 0) { |
|
102 |
+ LOG(L_ERR, "lookup(): Error while querying usrloc\n"); |
|
103 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
104 |
+ return -2; |
|
105 |
+ } |
|
106 |
+ |
|
107 |
+ if (res > 0) { |
|
108 |
+ DBG("lookup(): '%.*s' Not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
109 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
110 |
+ return -3; |
|
111 |
+ } |
|
112 |
+ |
|
113 |
+ ptr = r->contacts; |
|
114 |
+ while ((ptr) && !VALID_CONTACT(ptr, act_time)) |
|
115 |
+ ptr = ptr->next; |
|
116 |
+ |
|
117 |
+ if (ptr) { |
|
118 |
+ if (ptr->received.s && ptr->received.len) { |
|
119 |
+ if (received_to_uri){ |
|
120 |
+ if (add_received(&new_uri, &ptr->c, &ptr->received)<0){ |
|
121 |
+ LOG(L_ERR, "ERROR: lookup(): out of memory\n"); |
|
122 |
+ return -4; |
|
123 |
+ } |
|
124 |
+ /* replace the msg uri */ |
|
125 |
+ if (_m->new_uri.s) pkg_free(_m->new_uri.s); |
|
126 |
+ _m->new_uri=new_uri; |
|
127 |
+ _m->parsed_uri_ok=0; |
|
128 |
+ ruri_mark_new(); |
|
129 |
+ goto skip_rewrite_uri; |
|
130 |
+ }else if (set_dst_uri(_m, &ptr->received) < 0) { |
|
131 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
132 |
+ return -4; |
|
133 |
+ } |
|
134 |
+ } |
|
135 |
+ |
|
136 |
+ if (rewrite_uri(_m, &ptr->c) < 0) { |
|
137 |
+ LOG(L_ERR, "lookup(): Unable to rewrite Request-URI\n"); |
|
138 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
139 |
+ return -4; |
|
140 |
+ } |
|
141 |
+ |
|
142 |
+ if (ptr->sock) { |
|
143 |
+ set_force_socket(_m, ptr->sock); |
|
144 |
+ } |
|
145 |
+ |
|
146 |
+skip_rewrite_uri: |
|
147 |
+ |
|
148 |
+ set_ruri_q(ptr->q); |
|
149 |
+ |
|
150 |
+ nat |= ptr->flags & FL_NAT; |
|
151 |
+ ptr = ptr->next; |
|
152 |
+ } else { |
|
153 |
+ /* All contacts expired */ |
|
154 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
155 |
+ return -5; |
|
156 |
+ } |
|
157 |
+ |
|
158 |
+ /* Append branches if enabled */ |
|
159 |
+ if (!append_branches) goto skip; |
|
160 |
+ |
|
161 |
+ while(ptr) { |
|
162 |
+ if (VALID_CONTACT(ptr, act_time)) { |
|
163 |
+ if (received_to_uri && ptr->received.s && ptr->received.len){ |
|
164 |
+ if (add_received(&new_uri, &ptr->c, &ptr->received)<0){ |
|
165 |
+ LOG(L_ERR, "ERROR: lookup(): branch: out of memory\n"); |
|
166 |
+ goto cont; /* try to continue */ |
|
167 |
+ } |
|
168 |
+ if (append_branch(_m, &new_uri, 0, 0, ptr->q, |
|
169 |
+ 0, 0, 0, 0) == -1) { |
|
170 |
+ LOG(L_ERR, "lookup(): Error while appending a branch\n"); |
|
171 |
+ pkg_free(new_uri.s); |
|
172 |
+ if (ser_error==E_TOO_MANY_BRANCHES) goto skip; |
|
173 |
+ else goto cont; /* try to continue, maybe we have an |
|
174 |
+ oversized contact */ |
|
175 |
+ } |
|
176 |
+ pkg_free(new_uri.s); /* append_branch doesn't free it */ |
|
177 |
+ }else{ |
|
178 |
+ if (append_branch(_m, &ptr->c, &ptr->received, |
|
179 |
+ 0 /* path */, |
|
180 |
+ ptr->q, 0 /* brflags*/, |
|
181 |
+ ptr->sock, 0, 0) == -1) { |
|
182 |
+ LOG(L_ERR, "lookup(): Error while appending a branch\n"); |
|
183 |
+ goto skip; /* Return OK here so the function succeeds */ |
|
184 |
+ } |
|
185 |
+ } |
|
186 |
+ |
|
187 |
+ nat |= ptr->flags & FL_NAT; |
|
188 |
+ } |
|
189 |
+cont: |
|
190 |
+ ptr = ptr->next; |
|
191 |
+ } |
|
192 |
+ |
|
193 |
+ skip: |
|
194 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
195 |
+ if (nat) setflag(_m, load_nat_flag); |
|
196 |
+ return 1; |
|
197 |
+} |
|
198 |
+ |
|
199 |
+ |
|
200 |
+/* |
|
201 |
+ * Return true if the AOR in the Request-URI is registered, |
|
202 |
+ * it is similar to lookup but registered neither rewrites |
|
203 |
+ * the Request-URI nor appends branches |
|
204 |
+ */ |
|
205 |
+int registered(struct sip_msg* _m, char* _t, char* _s) |
|
206 |
+{ |
|
207 |
+ str uid; |
|
208 |
+ urecord_t* r; |
|
209 |
+ ucontact_t* ptr; |
|
210 |
+ int res; |
|
211 |
+ |
|
212 |
+ if (get_to_uid(&uid, _m) < 0) return -1; |
|
213 |
+ |
|
214 |
+ ul.lock_udomain((udomain_t*)_t); |
|
215 |
+ res = ul.get_urecord((udomain_t*)_t, &uid, &r); |
|
216 |
+ |
|
217 |
+ if (res < 0) { |
|
218 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
219 |
+ LOG(L_ERR, "registered(): Error while querying usrloc\n"); |
|
220 |
+ return -1; |
|
221 |
+ } |
|
222 |
+ |
|
223 |
+ if (res == 0) { |
|
224 |
+ ptr = r->contacts; |
|
225 |
+ while (ptr && !VALID_CONTACT(ptr, act_time)) { |
|
226 |
+ ptr = ptr->next; |
|
227 |
+ } |
|
228 |
+ |
|
229 |
+ if (ptr) { |
|
230 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
231 |
+ DBG("registered(): '%.*s' found in usrloc\n", uid.len, ZSW(uid.s)); |
|
232 |
+ return 1; |
|
233 |
+ } |
|
234 |
+ } |
|
235 |
+ |
|
236 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
237 |
+ DBG("registered(): '%.*s' not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
238 |
+ return -1; |
|
239 |
+} |
|
240 |
+ |
|
241 |
+/* |
|
242 |
+ * Lookup contact in the database and rewrite Request-URI, |
|
243 |
+ * and filter them by aor |
|
244 |
+ */ |
|
245 |
+int lookup2(struct sip_msg* msg, char* table, char* p2) |
|
246 |
+{ |
|
247 |
+ urecord_t* r; |
|
248 |
+ str uid; |
|
249 |
+ ucontact_t* ptr; |
|
250 |
+ int res; |
|
251 |
+ unsigned int nat; |
|
252 |
+ str new_uri, aor; |
|
253 |
+ |
|
254 |
+ nat = 0; |
|
255 |
+ |
|
256 |
+ if (get_str_fparam(&aor, msg, (fparam_t*)p2) != 0) { |
|
257 |
+ ERR("Unable to get the AOR value\n"); |
|
258 |
+ return -1; |
|
259 |
+ } |
|
260 |
+ |
|
261 |
+ if (get_to_uid(&uid, msg) < 0) return -1; |
|
262 |
+ get_act_time(); |
|
263 |
+ |
|
264 |
+ ul.lock_udomain((udomain_t*)table); |
|
265 |
+ res = ul.get_urecord((udomain_t*)table, &uid, &r); |
|
266 |
+ if (res < 0) { |
|
267 |
+ ERR("Error while querying usrloc\n"); |
|
268 |
+ ul.unlock_udomain((udomain_t*)table); |
|
269 |
+ return -2; |
|
270 |
+ } |
|
271 |
+ |
|
272 |
+ if (res > 0) { |
|
273 |
+ DBG("'%.*s' Not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
274 |
+ ul.unlock_udomain((udomain_t*)table); |
|
275 |
+ return -3; |
|
276 |
+ } |
|
277 |
+ |
|
278 |
+ ptr = r->contacts; |
|
279 |
+ while (ptr && (!VALID_CONTACT(ptr, act_time) || !VALID_AOR(ptr, aor))) |
|
280 |
+ ptr = ptr->next; |
|
281 |
+ |
|
282 |
+ if (ptr) { |
|
283 |
+ if (ptr->received.s && ptr->received.len) { |
|
284 |
+ if (received_to_uri){ |
|
285 |
+ if (add_received(&new_uri, &ptr->c, &ptr->received) < 0) { |
|
286 |
+ ERR("Out of memory\n"); |
|
287 |
+ return -4; |
|
288 |
+ } |
|
289 |
+ /* replace the msg uri */ |
|
290 |
+ if (msg->new_uri.s) pkg_free(msg->new_uri.s); |
|
291 |
+ msg->new_uri = new_uri; |
|
292 |
+ msg->parsed_uri_ok = 0; |
|
293 |
+ ruri_mark_new(); |
|
294 |
+ goto skip_rewrite_uri; |
|
295 |
+ } else if (set_dst_uri(msg, &ptr->received) < 0) { |
|
296 |
+ ul.unlock_udomain((udomain_t*)table); |
|
297 |
+ return -4; |
|
298 |
+ } |
|
299 |
+ } |
|
300 |
+ |
|
301 |
+ if (rewrite_uri(msg, &ptr->c) < 0) { |
|
302 |
+ ERR("Unable to rewrite Request-URI\n"); |
|
303 |
+ ul.unlock_udomain((udomain_t*)table); |
|
304 |
+ return -4; |
|
305 |
+ } |
|
306 |
+ |
|
307 |
+ if (ptr->sock) { |
|
308 |
+ set_force_socket(msg, ptr->sock); |
|
309 |
+ } |
|
310 |
+ |
|
311 |
+skip_rewrite_uri: |
|
312 |
+ set_ruri_q(ptr->q); |
|
313 |
+ |
|
314 |
+ nat |= ptr->flags & FL_NAT; |
|
315 |
+ ptr = ptr->next; |
|
316 |
+ } else { |
|
317 |
+ /* All contacts expired */ |
|
318 |
+ ul.unlock_udomain((udomain_t*)table); |
|
319 |
+ return -5; |
|
320 |
+ } |
|
321 |
+ |
|
322 |
+ /* Append branches if enabled */ |
|
323 |
+ if (!append_branches) goto skip; |
|
324 |
+ |
|
325 |
+ while(ptr) { |
|
326 |
+ if (VALID_CONTACT(ptr, act_time) && VALID_AOR(ptr, aor)) { |
|
327 |
+ if (received_to_uri && ptr->received.s && ptr->received.len) { |
|
328 |
+ if (add_received(&new_uri, &ptr->c, &ptr->received) < 0) { |
|
329 |
+ ERR("branch: out of memory\n"); |
|
330 |
+ goto cont; /* try to continue */ |
|
331 |
+ } |
|
332 |
+ if (append_branch(msg, &new_uri, 0, 0, ptr->q, |
|
333 |
+ 0, 0, 0, 0) == -1) { |
|
334 |
+ ERR("Error while appending a branch\n"); |
|
335 |
+ pkg_free(new_uri.s); |
|
336 |
+ if (ser_error == E_TOO_MANY_BRANCHES) goto skip; |
|
337 |
+ else goto cont; /* try to continue, maybe we have an |
|
338 |
+ oversized contact */ |
|
339 |
+ } |
|
340 |
+ pkg_free(new_uri.s); /* append_branch doesn't free it */ |
|
341 |
+ } else { |
|
342 |
+ if (append_branch(msg, &ptr->c, &ptr->received, |
|
343 |
+ 0 /* path */, |
|
344 |
+ ptr->q, 0, ptr->sock, |
|
345 |
+ 0, 0) == -1) { |
|
346 |
+ ERR("Error while appending a branch\n"); |
|
347 |
+ goto skip; /* Return OK here so the function succeeds */ |
|
348 |
+ } |
|
349 |
+ } |
|
350 |
+ |
|
351 |
+ nat |= ptr->flags & FL_NAT; |
|
352 |
+ } |
|
353 |
+cont: |
|
354 |
+ ptr = ptr->next; |
|
355 |
+ } |
|
356 |
+ |
|
357 |
+ skip: |
|
358 |
+ ul.unlock_udomain((udomain_t*)table); |
|
359 |
+ if (nat) setflag(msg, load_nat_flag); |
|
360 |
+ return 1; |
|
361 |
+} |
|
362 |
+ |
|
363 |
+ |
|
364 |
+/* |
|
365 |
+ * Return true if the AOR in the Request-URI is registered, |
|
366 |
+ * it is similar to lookup but registered neither rewrites |
|
367 |
+ * the Request-URI nor appends branches |
|
368 |
+ */ |
|
369 |
+int registered2(struct sip_msg* _m, char* _t, char* p2) |
|
370 |
+{ |
|
371 |
+ str uid, aor; |
|
372 |
+ urecord_t* r; |
|
373 |
+ ucontact_t* ptr; |
|
374 |
+ int res; |
|
375 |
+ |
|
376 |
+ if (get_str_fparam(&aor, _m, (fparam_t*)p2) != 0) { |
|
377 |
+ ERR("Unable to get the AOR value\n"); |
|
378 |
+ return -1; |
|
379 |
+ } |
|
380 |
+ |
|
381 |
+ if (get_to_uid(&uid, _m) < 0) return -1; |
|
382 |
+ |
|
383 |
+ ul.lock_udomain((udomain_t*)_t); |
|
384 |
+ res = ul.get_urecord((udomain_t*)_t, &uid, &r); |
|
385 |
+ |
|
386 |
+ if (res < 0) { |
|
387 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
388 |
+ LOG(L_ERR, "registered(): Error while querying usrloc\n"); |
|
389 |
+ return -1; |
|
390 |
+ } |
|
391 |
+ |
|
392 |
+ if (res == 0) { |
|
393 |
+ ptr = r->contacts; |
|
394 |
+ while (ptr && (!VALID_CONTACT(ptr, act_time) || !VALID_AOR(ptr, aor))) { |
|
395 |
+ ptr = ptr->next; |
|
396 |
+ } |
|
397 |
+ |
|
398 |
+ if (ptr) { |
|
399 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
400 |
+ DBG("registered(): '%.*s' found in usrloc\n", uid.len, ZSW(uid.s)); |
|
401 |
+ return 1; |
|
402 |
+ } |
|
403 |
+ } |
|
404 |
+ |
|
405 |
+ ul.unlock_udomain((udomain_t*)_t); |
|
406 |
+ DBG("registered(): '%.*s' not found in usrloc\n", uid.len, ZSW(uid.s)); |
|
407 |
+ return -1; |
|
408 |
+} |