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 + s->record_route.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->record_route, s->record_route);
228 CONT_COPY(dest, dest->user_agent, s->user_agent);
230 CONT_COPY(dest, dest->event_id, s->event_id);
232 CONT_COPY(dest, dest->reason, s->reason);
234 dest->event= s->event;
235 dest->local_cseq= s->local_cseq;
236 dest->remote_cseq= s->remote_cseq;
237 dest->status= s->status;
238 dest->version= s->version;
239 dest->send_on_cback= s->send_on_cback;
240 dest->expires= s->expires;
241 dest->db_flag= s->db_flag;
242 dest->flags= s->flags;
244 dest->contact.s= (char*)shm_malloc(s->contact.len* sizeof(char));
245 if(dest->contact.s== NULL)
249 memcpy(dest->contact.s, s->contact.s, s->contact.len);
250 dest->contact.len= s->contact.len;
260 int insert_shtable(shtable_t htable,unsigned int hash_code, subs_t* subs)
262 subs_t* new_rec= NULL;
264 new_rec= mem_copy_subs_noc(subs);
267 LM_ERR("copying in share memory a subs_t structure\n");
270 new_rec->expires+= (int)time(NULL);
272 lock_get(&htable[hash_code].lock);
273 new_rec->next= htable[hash_code].entries->next;
274 htable[hash_code].entries->next= new_rec;
275 lock_release(&htable[hash_code].lock);
280 int delete_shtable(shtable_t htable,unsigned int hash_code,subs_t* subs)
282 subs_t* s= NULL, *ps= NULL;
285 lock_get(&htable[hash_code].lock);
287 ps= htable[hash_code].entries;
292 if(pres_subs_remove_match==0) {
293 /* match on to-tag only (unique, local generated - faster) */
294 if(s->to_tag.len==subs->to_tag.len
295 && strncmp(s->to_tag.s,subs->to_tag.s,subs->to_tag.len)==0)
300 /* match on all dialog attributes (distributed systems) */
301 if(s->callid.len==subs->callid.len
302 && s->to_tag.len==subs->to_tag.len
303 && s->from_tag.len==subs->from_tag.len
304 && strncmp(s->callid.s,subs->callid.s,subs->callid.len)==0
305 && strncmp(s->to_tag.s,subs->to_tag.s,subs->to_tag.len)==0
306 && strncmp(s->from_tag.s,subs->from_tag.s,subs->from_tag.len)==0)
313 found= s->local_cseq +1;
315 if(s->contact.s!=NULL) {
316 shm_free(s->contact.s);
328 lock_release(&htable[hash_code].lock);
332 void free_subs_list(subs_t* s_array, int mem_type, int ic)
339 s_array= s_array->next;
340 if(mem_type & PKG_MEM_TYPE)
343 pkg_free(s->contact.s);
352 shm_free(s->contact.s);
362 int update_shtable(shtable_t htable,unsigned int hash_code,
363 subs_t* subs, int type)
367 lock_get(&htable[hash_code].lock);
369 s= search_shtable(htable,subs->callid, subs->to_tag, subs->from_tag,
373 LM_DBG("record not found in hash table\n");
374 lock_release(&htable[hash_code].lock);
378 if(type & REMOTE_TYPE)
380 s->expires= subs->expires+ (int)time(NULL);
381 s->remote_cseq= subs->remote_cseq;
385 subs->local_cseq = ++s->local_cseq;
386 subs->version = ++s->version;
389 if(presence_sip_uri_match(&s->contact, &subs->contact))
391 shm_free(s->contact.s);
392 s->contact.s= (char*)shm_malloc(subs->contact.len* sizeof(char));
393 if(s->contact.s== NULL)
395 lock_release(&htable[hash_code].lock);
396 LM_ERR("no more shared memory\n");
399 memcpy(s->contact.s, subs->contact.s, subs->contact.len);
400 s->contact.len= subs->contact.len;
403 s->status= subs->status;
404 s->event= subs->event;
405 subs->db_flag= s->db_flag;
407 if(s->db_flag & NO_UPDATEDB_FLAG)
408 s->db_flag= UPDATEDB_FLAG;
410 lock_release(&htable[hash_code].lock);
415 phtable_t* new_phtable(void)
417 phtable_t* htable= NULL;
421 htable= (phtable_t*)shm_malloc(phtable_size* sizeof(phtable_t));
426 memset(htable, 0, phtable_size* sizeof(phtable_t));
428 for(i= 0; i< phtable_size; i++)
430 if(lock_init(&htable[i].lock)== 0)
432 LM_ERR("initializing lock [%d]\n", i);
435 htable[i].entries= (pres_entry_t*)shm_malloc(sizeof(pres_entry_t));
436 if(htable[i].entries== NULL)
440 memset(htable[i].entries, 0, sizeof(pres_entry_t));
441 htable[i].entries->next= NULL;
451 if(htable[i].entries)
452 shm_free(htable[i].entries);
455 lock_destroy(&htable[i].lock);
463 void destroy_phtable(void)
466 pres_entry_t* p, *prev_p;
468 if(pres_htable== NULL)
471 for(i= 0; i< phtable_size; i++)
473 lock_destroy(&pres_htable[i].lock);
474 p= pres_htable[i].entries;
480 shm_free(prev_p->sphere);
484 shm_free(pres_htable);
486 /* entry must be locked before calling this function */
488 pres_entry_t* search_phtable(str* pres_uri,int event, unsigned int hash_code)
492 LM_DBG("pres_uri= %.*s\n", pres_uri->len, pres_uri->s);
493 p= pres_htable[hash_code].entries->next;
496 if(p->event== event && p->pres_uri.len== pres_uri->len &&
497 presence_sip_uri_match(&p->pres_uri, pres_uri)== 0 )
504 int insert_phtable(str* pres_uri, int event, char* sphere)
506 unsigned int hash_code;
507 pres_entry_t* p= NULL;
510 hash_code= core_case_hash(pres_uri, NULL, phtable_size);
512 lock_get(&pres_htable[hash_code].lock);
514 p= search_phtable(pres_uri, event, hash_code);
518 lock_release(&pres_htable[hash_code].lock);
521 size= sizeof(pres_entry_t)+ pres_uri->len* sizeof(char);
523 p= (pres_entry_t*)shm_malloc(size);
526 lock_release(&pres_htable[hash_code].lock);
531 size= sizeof(pres_entry_t);
532 p->pres_uri.s= (char*)p+ size;
533 memcpy(p->pres_uri.s, pres_uri->s, pres_uri->len);
534 p->pres_uri.len= pres_uri->len;
538 p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
541 lock_release(&pres_htable[hash_code].lock);
545 strcpy(p->sphere, sphere);
551 /* link the item in the hash table */
552 p->next= pres_htable[hash_code].entries->next;
553 pres_htable[hash_code].entries->next= p;
555 lock_release(&pres_htable[hash_code].lock);
563 int delete_phtable(str* pres_uri, int event)
565 unsigned int hash_code;
566 pres_entry_t* p= NULL, *prev_p= NULL;
568 hash_code= core_case_hash(pres_uri, NULL, phtable_size);
570 lock_get(&pres_htable[hash_code].lock);
572 p= search_phtable(pres_uri, event, hash_code);
575 LM_DBG("record not found\n");
576 lock_release(&pres_htable[hash_code].lock);
581 if(p->publ_count== 0)
584 prev_p= pres_htable[hash_code].entries;
589 prev_p= prev_p->next;
591 if(prev_p->next== NULL)
593 LM_ERR("record not found\n");
594 lock_release(&pres_htable[hash_code].lock);
597 prev_p->next= p->next;
603 lock_release(&pres_htable[hash_code].lock);
608 int update_phtable(presentity_t* presentity, str pres_uri, str body)
611 unsigned int hash_code;
617 sphere= extract_sphere(body);
620 LM_DBG("no sphere defined in new body\n");
624 /* search for record in hash table */
625 hash_code= core_case_hash(&pres_uri, NULL, phtable_size);
627 lock_get(&pres_htable[hash_code].lock);
629 p= search_phtable(&pres_uri, presentity->event->evp->type, hash_code);
632 lock_release(&pres_htable[hash_code].lock);
638 if(strcmp(p->sphere, sphere)!= 0)
640 /* new sphere definition */
645 /* no change in sphere definition */
646 lock_release(&pres_htable[hash_code].lock);
654 p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
657 lock_release(&pres_htable[hash_code].lock);
661 strcpy(p->sphere, sphere);
663 lock_release(&pres_htable[hash_code].lock);
665 /* call for watchers status update */
667 if(presentity->event->get_rules_doc(&presentity->user, &presentity->domain,
670 LM_ERR("failed to retrieve xcap document\n");
675 update_watchers_status(pres_uri, presentity->event, xcap_doc);
683 pkg_free(xcap_doc->s);