1 /*
2  * $Id$
3  *
4  * Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
5  * Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
6  *
7  * The initial version of this code was written by Dragos Vingarzan
8  * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
9  * Fruanhofer Institute. It was and still is maintained in a separate
10  * branch of the original SER. We are therefore migrating it to
11  * Kamailio/SR and look forward to maintaining it from here on out.
12  * 2011/2012 Smile Communications, Pty. Ltd.
13  * ported/maintained/improved by
14  * Jason Penton (jason(dot)penton(at)smilecoms.com and
15  * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
16  * effort to add full IMS support to Kamailio/SR using a new and
17  * improved architecture
18  *
19  * NB: Alot of this code was originally part of OpenIMSCore,
20  * FhG Fokus.
21  * Copyright (C) 2004-2006 FhG Fokus
22  * Thanks for great work! This is an effort to
23  * break apart the various CSCF functions into logically separate
24  * components. We hope this will drive wider use. We also feel
25  * that in this way the architecture is more complete and thereby easier
26  * to manage in the Kamailio/SR environment
27  *
28  * This file is part of Kamailio, a free SIP server.
29  *
30  * Kamailio is free software; you can redistribute it and/or modify
31  * it under the terms of the GNU General Public License as published by
32  * the Free Software Foundation; either version 2 of the License, or
33  * (at your option) any later version
34  *
35  * Kamailio is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  * GNU General Public License for more details.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
43  *
44  */
45 
46 #include "dlist.h"
47 #include <stdlib.h>	       /* abort */
48 #include <string.h>            /* strlen, memcmp */
49 #include <stdio.h>             /* printf */
50 #include "../../core/ut.h"
51 #include "../../lib/srdb1/db_ut.h"
52 #include "../../core/mem/shm_mem.h"
53 #include "../../core/dprint.h"
54 #include "../../core/ip_addr.h"
55 #include "../../core/socket_info.h"
56 #include "udomain.h"           /* new_udomain, free_udomain */
57 #include "usrloc.h"
58 #include "utime.h"
59 #include "ims_usrloc_pcscf_mod.h"
60 #include "pcontact.h"
61 
62 dlist_t* root = 0;
63 
find_dlist(str * _n,dlist_t ** _d)64 static inline int find_dlist(str* _n, dlist_t** _d)
65 {
66 	dlist_t* ptr;
67 
68 	ptr = root;
69 	while(ptr) {
70 		if ((_n->len == ptr->name.len) &&
71 		    !memcmp(_n->s, ptr->name.s, _n->len)) {
72 			*_d = ptr;
73 			return 0;
74 		}
75 
76 		ptr = ptr->next;
77 	}
78 
79 	return 1;
80 }
81 
get_all_mem_ucontacts(void * buf,int len,unsigned int flags,unsigned int part_idx,unsigned int part_max)82 static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags,
83 							unsigned int part_idx, unsigned int part_max)
84 {
85 	dlist_t *p;
86 	pcontact_t *c;
87 	void *cp;
88 	int shortage;
89 	int needed;
90 	int i = 0;
91 	cp = buf;
92 	shortage = 0;
93 	unsigned int received_len = 0;
94 	char received_s[60]; // IPv6-Address (39) + "sip:" (4) + ":" (1) + Port (5); 60 should be plenty.
95 	/* Reserve space for terminating 0000 */
96 	len -= sizeof(int);
97 
98 	for (p = root; p != NULL; p = p->next) {
99 
100 		for(i=0; i<p->d->size; i++) {
101 
102 			if ( (i % part_max) != part_idx )
103 				continue;
104 
105 			lock_ulslot(p->d, i);
106 			if(p->d->table[i].n<=0)
107 			{
108 				unlock_ulslot(p->d, i);
109 				continue;
110 			}
111 			for (c = p->d->table[i].first; c != NULL; c = c->next) {
112 					if (c->received_host.s) {
113 						received_len = snprintf(received_s, sizeof(received_s), "sip:%.*s:%x", c->received_host.len,
114 							c->received_host.s, c->received_port) - 1;
115 
116 						needed = (int)(sizeof(received_len)
117 								+ received_len + sizeof(c->sock)
118 								+ sizeof(unsigned int) + sizeof(c->path.len)
119 								+ c->path.len);
120 						if (len >= needed) {
121 							cp = (char*)cp + received_len;
122 							cp = (char*)cp + sizeof(received_len);
123 
124 							memcpy(cp, received_s, received_len);
125 							cp = (char*)cp + received_len;
126 
127 							memcpy(cp, &c->sock, sizeof(c->sock));
128 							cp = (char*)cp + sizeof(c->sock);
129 
130 							memset(cp, 0, sizeof(unsigned int));
131 							cp = (char*)cp + sizeof(unsigned int);
132 
133 							memcpy(cp, &c->path.len, sizeof(c->path.len));
134 							cp = (char*)cp + sizeof(c->path.len);
135 							memcpy(cp, c->path.s, c->path.len);
136 							cp = (char*)cp + c->path.len;
137 							len -= needed;
138 						} else {
139 							shortage += needed;
140 						}
141 					}
142 			}
143 			unlock_ulslot(p->d, i);
144 		}
145 	}
146 	/* len < 0 is possible, if size of the buffer < sizeof(c->c.len) */
147 	if (len >= 0)
148 		memset(cp, 0, sizeof(int));
149 
150 	/* Shouldn't happen */
151 	if (shortage > 0 && len > shortage) {
152 		abort();
153 	}
154 
155 	shortage -= len;
156 
157 	return shortage > 0 ? shortage : 0;
158 }
159 
get_all_ucontacts(void * buf,int len,unsigned int flags,unsigned int part_idx,unsigned int part_max)160 int get_all_ucontacts(void *buf, int len, unsigned int flags,
161 								unsigned int part_idx, unsigned int part_max)
162 {
163 	return get_all_mem_ucontacts( buf, len, flags, part_idx, part_max);
164 }
165 
new_dlist(str * _n,dlist_t ** _d)166 static inline int new_dlist(str* _n, dlist_t** _d)
167 {
168 	dlist_t* ptr;
169 
170 	/* Domains are created before ser forks,
171 	 * so we can create them using pkg_malloc
172 	 */
173 	ptr = (dlist_t*)shm_malloc(sizeof(dlist_t));
174 	if (ptr == 0) {
175 		LM_ERR("no more share memory\n");
176 		return -1;
177 	}
178 	memset(ptr, 0, sizeof(dlist_t));
179 
180 	/* copy domain name as null terminated string */
181 	ptr->name.s = (char*)shm_malloc(_n->len+1);
182 	if (ptr->name.s == 0) {
183 		LM_ERR("no more memory left\n");
184 		shm_free(ptr);
185 		return -2;
186 	}
187 
188 	memcpy(ptr->name.s, _n->s, _n->len);
189 	ptr->name.len = _n->len;
190 	ptr->name.s[ptr->name.len] = 0;
191 
192 	if (new_udomain(&(ptr->name), ul_hash_size, &(ptr->d)) < 0) {
193 		LM_ERR("creating domain structure failed\n");
194 		shm_free(ptr->name.s);
195 		shm_free(ptr);
196 		return -3;
197 	}
198 
199 	*_d = ptr;
200 	return 0;
201 }
202 
get_udomain(const char * _n,udomain_t ** _d)203 int get_udomain(const char* _n, udomain_t** _d)
204 {
205 	dlist_t* d;
206 	str s;
207 
208 	s.s = (char*)_n;
209 	s.len = strlen(_n);
210 
211 	if (find_dlist(&s, &d) == 0) {
212 		*_d = d->d;
213 		return 0;
214 	}
215 	*_d = NULL;
216 	return -1;
217 }
218 
register_udomain(const char * _n,udomain_t ** _d)219 int register_udomain(const char* _n, udomain_t** _d)
220 {
221 	dlist_t* d;
222 	str s;
223 
224 	s.s = (char*)_n;
225 	s.len = strlen(_n);
226 
227 	if (find_dlist(&s, &d) == 0) {
228 		*_d = d->d;
229 		return 0;
230 	}
231 
232 	if (new_dlist(&s, &d) < 0) {
233 		LM_ERR("failed to create new domain\n");
234 		return -1;
235 	}
236 
237 	d->next = root;
238 	root = d;
239 
240 	*_d = d->d;
241 	return 0;
242 }
243 
free_all_udomains(void)244 void free_all_udomains(void)
245 {
246 	dlist_t* ptr;
247 
248 	while(root) {
249 		ptr = root;
250 		root = root->next;
251 
252 		free_udomain(ptr->d);
253 		shm_free(ptr->name.s);
254 		shm_free(ptr);
255 	}
256 }
257 
print_all_udomains(FILE * _f)258 void print_all_udomains(FILE* _f)
259 {
260 	dlist_t* ptr;
261 
262 	ptr = root;
263 
264 	fprintf(_f, "===Domain list===\n");
265 	while(ptr) {
266 		print_udomain(_f, ptr->d);
267 		ptr = ptr->next;
268 	}
269 	fprintf(_f, "===/Domain list===\n");
270 }
271 
synchronize_all_udomains(void)272 int synchronize_all_udomains(void)
273 {
274 	int res = 0;
275 	dlist_t* ptr;
276 
277 	get_act_time(); /* Get and save actual time */
278 
279 	for( ptr=root ; ptr ; ptr=ptr->next)
280 		mem_timer_udomain(ptr->d);
281 
282 	return res;
283 }
284 
find_domain(str * _d,udomain_t ** _p)285 int find_domain(str* _d, udomain_t** _p)
286 {
287 	dlist_t* d;
288 
289 	if (find_dlist(_d, &d) == 0) {
290 	        *_p = d->d;
291 		return 0;
292 	}
293 
294 	return 1;
295 }
296 
get_number_of_contacts(void)297 unsigned long get_number_of_contacts(void)
298 {
299         long numberOfUsers = 0;
300 
301         dlist_t* current_dlist;
302 
303         current_dlist = root;
304 
305         while (current_dlist)
306         {
307                 numberOfUsers += get_stat_val(current_dlist->d->contacts);
308                 current_dlist  = current_dlist->next;
309         }
310 
311         return numberOfUsers;
312 }
313 
get_number_of_expired(void)314 unsigned long get_number_of_expired(void)
315 {
316         long numberOfExpired = 0;
317 
318         dlist_t* current_dlist;
319 
320         current_dlist = root;
321 
322         while (current_dlist)
323         {
324                 numberOfExpired += get_stat_val(current_dlist->d->expired);
325                 current_dlist  = current_dlist->next;
326         }
327 
328         return numberOfExpired;
329 }
330 
get_number_of_impu(void)331 unsigned long get_number_of_impu(void)
332 {
333         long numberOfExpired = 0;
334 
335         dlist_t* current_dlist;
336 
337         current_dlist = root;
338 
339         while (current_dlist)
340         {
341                 numberOfExpired += get_stat_val(current_dlist->d->expired);
342                 current_dlist  = current_dlist->next;
343         }
344 
345         return numberOfExpired;
346 }
347