2 * presence module - presence server implementation
4 * Copyright (C) 2007 Voice Sistem S.R.L.
6 * This file is part of Kamailio, a free SIP server.
8 * Kamailio is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version
13 * Kamailio is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * \brief Kamailio presence module
31 #include "../../core/mem/shm_mem.h"
32 #include "../../core/hashes.h"
33 #include "../../core/dprint.h"
34 #include "../../core/str.h"
35 #include "../pua/hash.h"
40 /* matching mode when removing subscriptions from memory */
41 extern int pres_subs_remove_match;
44 * create the subscription hash table in shared memory
45 * - hash_size: number of slots
47 shtable_t new_shtable(int hash_size)
49 shtable_t htable= NULL;
53 htable= (subs_entry_t*)shm_malloc(hash_size* sizeof(subs_entry_t));
58 memset(htable, 0, hash_size* sizeof(subs_entry_t));
59 for(i= 0; i< hash_size; i++)
61 if(lock_init(&htable[i].lock)== 0)
63 LM_ERR("initializing lock [%d]\n", i);
66 htable[i].entries= (subs_t*)shm_malloc(sizeof(subs_t));
67 if(htable[i].entries== NULL)
69 lock_destroy(&htable[i].lock);
72 memset(htable[i].entries, 0, sizeof(subs_t));
73 htable[i].entries->next= NULL;
83 lock_destroy(&htable[j].lock);
84 shm_free(htable[j].entries);
92 void destroy_shtable(shtable_t htable, int hash_size)
99 for(i= 0; i< hash_size; i++)
101 lock_destroy(&htable[i].lock);
102 free_subs_list(htable[i].entries->next, SHM_MEM_TYPE, 1);
103 shm_free(htable[i].entries);
104 htable[i].entries = NULL;
110 subs_t* search_shtable(shtable_t htable,str callid,str to_tag,
111 str from_tag,unsigned int hash_code)
115 s= htable[hash_code].entries?htable[hash_code].entries->next:NULL;
119 if(s->callid.len==callid.len &&
120 strncmp(s->callid.s, callid.s, callid.len)==0 &&
121 s->to_tag.len== to_tag.len &&
122 strncmp(s->to_tag.s, to_tag.s, to_tag.len)==0 &&
123 s->from_tag.len== from_tag.len &&
124 strncmp(s->from_tag.s, from_tag.s, from_tag.len)== 0)
132 subs_t* mem_copy_subs(subs_t* s, int mem_type)
137 size= sizeof(subs_t)+ (s->pres_uri.len+ s->to_user.len
138 + s->to_domain.len+ s->from_user.len+ s->from_domain.len+ s->callid.len
139 + s->to_tag.len+ s->from_tag.len+s->sockinfo_str.len+s->event_id.len
140 + s->local_contact.len+ s->contact.len+ s->record_route.len
141 + s->reason.len+ s->watcher_user.len+ s->watcher_domain.len
145 if(mem_type & PKG_MEM_TYPE)
146 dest= (subs_t*)pkg_malloc(size);
148 dest= (subs_t*)shm_malloc(size);
152 ERR_MEM((mem_type==PKG_MEM_TYPE)?PKG_MEM_STR:SHARE_MEM);
154 memset(dest, 0, size);
155 size= sizeof(subs_t);
157 CONT_COPY(dest, dest->pres_uri, s->pres_uri);
158 CONT_COPY(dest, dest->to_user, s->to_user);
159 CONT_COPY(dest, dest->to_domain, s->to_domain);
160 CONT_COPY(dest, dest->from_user, s->from_user);
161 CONT_COPY(dest, dest->from_domain, s->from_domain);
162 CONT_COPY(dest, dest->watcher_user, s->watcher_user);
163 CONT_COPY(dest, dest->watcher_domain, s->watcher_domain);
164 CONT_COPY(dest, dest->to_tag, s->to_tag);
165 CONT_COPY(dest, dest->from_tag, s->from_tag);
166 CONT_COPY(dest, dest->callid, s->callid);
167 CONT_COPY(dest, dest->sockinfo_str, s->sockinfo_str);
168 CONT_COPY(dest, dest->local_contact, s->local_contact);
169 CONT_COPY(dest, dest->contact, s->contact);
170 CONT_COPY(dest, dest->record_route, s->record_route);
171 CONT_COPY(dest, dest->user_agent, s->user_agent);
173 CONT_COPY(dest, dest->event_id, s->event_id);
175 CONT_COPY(dest, dest->reason, s->reason);
177 dest->event= s->event;
178 dest->local_cseq= s->local_cseq;
179 dest->remote_cseq= s->remote_cseq;
180 dest->status= s->status;
181 dest->version= s->version;
182 dest->send_on_cback= s->send_on_cback;
183 dest->expires= s->expires;
184 dest->db_flag= s->db_flag;
185 dest->flags= s->flags;
194 subs_t* mem_copy_subs_noc(subs_t* s)
199 size= sizeof(subs_t)+ (s->pres_uri.len+ s->to_user.len
200 + s->to_domain.len+ s->from_user.len+ s->from_domain.len+ s->callid.len
201 + s->to_tag.len+ s->from_tag.len+s->sockinfo_str.len+s->event_id.len
202 + s->local_contact.len
203 + s->reason.len+ s->watcher_user.len+ s->watcher_domain.len
207 dest= (subs_t*)shm_malloc(size);
212 memset(dest, 0, size);
213 size= sizeof(subs_t);
215 CONT_COPY(dest, dest->pres_uri, s->pres_uri);
216 CONT_COPY(dest, dest->to_user, s->to_user);
217 CONT_COPY(dest, dest->to_domain, s->to_domain);
218 CONT_COPY(dest, dest->from_user, s->from_user);
219 CONT_COPY(dest, dest->from_domain, s->from_domain);
220 CONT_COPY(dest, dest->watcher_user, s->watcher_user);
221 CONT_COPY(dest, dest->watcher_domain, s->watcher_domain);
222 CONT_COPY(dest, dest->to_tag, s->to_tag);
223 CONT_COPY(dest, dest->from_tag, s->from_tag);
224 CONT_COPY(dest, dest->callid, s->callid);
225 CONT_COPY(dest, dest->sockinfo_str, s->sockinfo_str);
226 CONT_COPY(dest, dest->local_contact, s->local_contact);
227 CONT_COPY(dest, dest->user_agent, s->user_agent);
229 CONT_COPY(dest, dest->event_id, s->event_id);
231 CONT_COPY(dest, dest->reason, s->reason);
233 dest->event= s->event;
234 dest->local_cseq= s->local_cseq;
235 dest->remote_cseq= s->remote_cseq;
236 dest->status= s->status;
237 dest->version= s->version;
238 dest->send_on_cback= s->send_on_cback;
239 dest->expires= s->expires;
240 dest->db_flag= s->db_flag;
241 dest->flags= s->flags;
243 dest->contact.s= (char*)shm_malloc(s->contact.len* sizeof(char));
244 if(dest->contact.s== NULL)
248 memcpy(dest->contact.s, s->contact.s, s->contact.len);
249 dest->contact.len= s->contact.len;
251 dest->record_route.s= (char*)shm_malloc((s->record_route.len + 1) * sizeof(char));
252 if(dest->record_route.s== NULL)
256 memcpy(dest->record_route.s, s->record_route.s, s->record_route.len);
257 dest->record_route.len= s->record_route.len;
267 int insert_shtable(shtable_t htable,unsigned int hash_code, subs_t* subs)
269 subs_t* new_rec= NULL;
271 new_rec= mem_copy_subs_noc(subs);
274 LM_ERR("copying in share memory a subs_t structure\n");
277 new_rec->expires+= (int)time(NULL);
279 lock_get(&htable[hash_code].lock);
280 new_rec->next= htable[hash_code].entries->next;
281 htable[hash_code].entries->next= new_rec;
282 lock_release(&htable[hash_code].lock);
287 int delete_shtable(shtable_t htable,unsigned int hash_code,subs_t* subs)
289 subs_t* s= NULL, *ps= NULL;
292 lock_get(&htable[hash_code].lock);
294 ps= htable[hash_code].entries;
299 if(pres_subs_remove_match==0) {
300 /* match on to-tag only (unique, local generated - faster) */
301 if(s->to_tag.len==subs->to_tag.len
302 && strncmp(s->to_tag.s,subs->to_tag.s,subs->to_tag.len)==0)
307 /* match on all dialog attributes (distributed systems) */
308 if(s->callid.len==subs->callid.len
309 && s->to_tag.len==subs->to_tag.len
310 && s->from_tag.len==subs->from_tag.len
311 && strncmp(s->callid.s,subs->callid.s,subs->callid.len)==0
312 && strncmp(s->to_tag.s,subs->to_tag.s,subs->to_tag.len)==0
313 && strncmp(s->from_tag.s,subs->from_tag.s,subs->from_tag.len)==0)
320 found= s->local_cseq +1;
322 if(s->contact.s!=NULL) {
323 shm_free(s->contact.s);
326 if(s->record_route.s!=NULL) {
327 shm_free(s->record_route.s);
328 s->record_route.s = NULL;
339 lock_release(&htable[hash_code].lock);
343 void free_subs_list(subs_t* s_array, int mem_type, int ic)
350 s_array= s_array->next;
351 if(mem_type & PKG_MEM_TYPE)
354 pkg_free(s->contact.s);
363 shm_free(s->contact.s);
373 int update_shtable(shtable_t htable,unsigned int hash_code,
374 subs_t* subs, int type)
378 lock_get(&htable[hash_code].lock);
380 s= search_shtable(htable,subs->callid, subs->to_tag, subs->from_tag,
384 LM_DBG("record not found in hash table\n");
385 lock_release(&htable[hash_code].lock);
389 if(type & REMOTE_TYPE)
391 s->expires= subs->expires+ (int)time(NULL);
392 s->remote_cseq= subs->remote_cseq;
396 subs->local_cseq = ++s->local_cseq;
397 subs->version = ++s->version;
400 if(presence_sip_uri_match(&s->contact, &subs->contact))
402 shm_free(s->contact.s);
403 s->contact.s= (char*)shm_malloc(subs->contact.len* sizeof(char));
404 if(s->contact.s== NULL)
406 lock_release(&htable[hash_code].lock);
407 LM_ERR("no more shared memory\n");
410 memcpy(s->contact.s, subs->contact.s, subs->contact.len);
411 s->contact.len= subs->contact.len;
414 shm_free(s->record_route.s);
415 s->record_route.s= (char*)shm_malloc(subs->record_route.len* sizeof(char));
416 if(s->record_route.s== NULL)
418 lock_release(&htable[hash_code].lock);
419 LM_ERR("no more shared memory\n");
422 memcpy(s->record_route.s, subs->record_route.s, subs->record_route.len);
423 s->record_route.len= subs->record_route.len;
425 s->status= subs->status;
426 s->event= subs->event;
427 subs->db_flag= s->db_flag;
429 if(s->db_flag & NO_UPDATEDB_FLAG)
430 s->db_flag= UPDATEDB_FLAG;
432 lock_release(&htable[hash_code].lock);
437 phtable_t* new_phtable(void)
439 phtable_t* htable= NULL;
443 htable= (phtable_t*)shm_malloc(phtable_size* sizeof(phtable_t));
448 memset(htable, 0, phtable_size* sizeof(phtable_t));
450 for(i= 0; i< phtable_size; i++)
452 if(lock_init(&htable[i].lock)== 0)
454 LM_ERR("initializing lock [%d]\n", i);
457 htable[i].entries= (pres_entry_t*)shm_malloc(sizeof(pres_entry_t));
458 if(htable[i].entries== NULL)
462 memset(htable[i].entries, 0, sizeof(pres_entry_t));
463 htable[i].entries->next= NULL;
473 if(htable[i].entries)
474 shm_free(htable[i].entries);
477 lock_destroy(&htable[i].lock);
485 void destroy_phtable(void)
488 pres_entry_t* p, *prev_p;
490 if(pres_htable== NULL)
493 for(i= 0; i< phtable_size; i++)
495 lock_destroy(&pres_htable[i].lock);
496 p= pres_htable[i].entries;
502 shm_free(prev_p->sphere);
506 shm_free(pres_htable);
508 /* entry must be locked before calling this function */
510 pres_entry_t* search_phtable(str* pres_uri,int event, unsigned int hash_code)
514 LM_DBG("pres_uri= %.*s\n", pres_uri->len, pres_uri->s);
515 p= pres_htable[hash_code].entries->next;
518 if(p->event== event && p->pres_uri.len== pres_uri->len &&
519 presence_sip_uri_match(&p->pres_uri, pres_uri)== 0 )
526 int insert_phtable(str* pres_uri, int event, char* sphere)
528 unsigned int hash_code;
529 pres_entry_t* p= NULL;
532 hash_code= core_case_hash(pres_uri, NULL, phtable_size);
534 lock_get(&pres_htable[hash_code].lock);
536 p= search_phtable(pres_uri, event, hash_code);
540 lock_release(&pres_htable[hash_code].lock);
543 size= sizeof(pres_entry_t)+ pres_uri->len* sizeof(char);
545 p= (pres_entry_t*)shm_malloc(size);
548 lock_release(&pres_htable[hash_code].lock);
553 size= sizeof(pres_entry_t);
554 p->pres_uri.s= (char*)p+ size;
555 memcpy(p->pres_uri.s, pres_uri->s, pres_uri->len);
556 p->pres_uri.len= pres_uri->len;
560 p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
563 lock_release(&pres_htable[hash_code].lock);
567 strcpy(p->sphere, sphere);
573 /* link the item in the hash table */
574 p->next= pres_htable[hash_code].entries->next;
575 pres_htable[hash_code].entries->next= p;
577 lock_release(&pres_htable[hash_code].lock);
585 int delete_phtable(str* pres_uri, int event)
587 unsigned int hash_code;
588 pres_entry_t* p= NULL, *prev_p= NULL;
590 hash_code= core_case_hash(pres_uri, NULL, phtable_size);
592 lock_get(&pres_htable[hash_code].lock);
594 p= search_phtable(pres_uri, event, hash_code);
597 LM_DBG("record not found\n");
598 lock_release(&pres_htable[hash_code].lock);
603 if(p->publ_count== 0)
606 prev_p= pres_htable[hash_code].entries;
611 prev_p= prev_p->next;
613 if(prev_p->next== NULL)
615 LM_ERR("record not found\n");
616 lock_release(&pres_htable[hash_code].lock);
619 prev_p->next= p->next;
625 lock_release(&pres_htable[hash_code].lock);
630 int update_phtable(presentity_t* presentity, str pres_uri, str body)
633 unsigned int hash_code;
639 sphere= extract_sphere(body);
642 LM_DBG("no sphere defined in new body\n");
646 /* search for record in hash table */
647 hash_code= core_case_hash(&pres_uri, NULL, phtable_size);
649 lock_get(&pres_htable[hash_code].lock);
651 p= search_phtable(&pres_uri, presentity->event->evp->type, hash_code);
654 lock_release(&pres_htable[hash_code].lock);
660 if(strcmp(p->sphere, sphere)!= 0)
662 /* new sphere definition */
667 /* no change in sphere definition */
668 lock_release(&pres_htable[hash_code].lock);
676 p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
679 lock_release(&pres_htable[hash_code].lock);
683 strcpy(p->sphere, sphere);
685 lock_release(&pres_htable[hash_code].lock);
687 /* call for watchers status update */
689 if(presentity->event->get_rules_doc(&presentity->user, &presentity->domain,
692 LM_ERR("failed to retrieve xcap document\n");
697 update_watchers_status(pres_uri, presentity->event, xcap_doc);
705 pkg_free(xcap_doc->s);