2
* $Id: hash.c 2583 2007-08-08 11:33:25Z anca_vamanu $
4
* presence module - presence server implementation
6
* Copyright (C) 2007 Voice Sistem S.R.L.
8
* This file is part of Kamailio, a free SIP server.
10
* Kamailio 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
15
* Kamailio is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
* 2007-08-20 initial version (Anca Vamanu)
30
* \brief Kamailio presence module
36
#include "../../mem/shm_mem.h"
37
#include "../../hashes.h"
38
#include "../../dprint.h"
39
#include "../../str.h"
40
#include "../pua/hash.h"
45
shtable_t new_shtable(int hash_size)
47
shtable_t htable= NULL;
51
htable= (subs_entry_t*)shm_malloc(hash_size* sizeof(subs_entry_t));
56
memset(htable, 0, hash_size* sizeof(subs_entry_t));
57
for(i= 0; i< hash_size; i++)
59
if(lock_init(&htable[i].lock)== 0)
61
LM_ERR("initializing lock [%d]\n", i);
64
htable[i].entries= (subs_t*)shm_malloc(sizeof(subs_t));
65
if(htable[i].entries== NULL)
67
lock_destroy(&htable[i].lock);
70
memset(htable[i].entries, 0, sizeof(subs_t));
71
htable[i].entries->next= NULL;
81
lock_destroy(&htable[j].lock);
82
shm_free(htable[j].entries);
90
void destroy_shtable(shtable_t htable, int hash_size)
97
for(i= 0; i< hash_size; i++)
99
lock_destroy(&htable[i].lock);
100
free_subs_list(htable[i].entries->next, SHM_MEM_TYPE, 1);
101
shm_free(htable[i].entries);
107
subs_t* search_shtable(shtable_t htable,str callid,str to_tag,
108
str from_tag,unsigned int hash_code)
112
s= htable[hash_code].entries->next;
116
if(s->callid.len==callid.len &&
117
strncmp(s->callid.s, callid.s, callid.len)==0 &&
118
s->to_tag.len== to_tag.len &&
119
strncmp(s->to_tag.s, to_tag.s, to_tag.len)==0 &&
120
s->from_tag.len== from_tag.len &&
121
strncmp(s->from_tag.s, from_tag.s, from_tag.len)== 0)
129
subs_t* mem_copy_subs(subs_t* s, int mem_type)
134
size= sizeof(subs_t)+ (s->pres_uri.len+ s->to_user.len
135
+ s->to_domain.len+ s->from_user.len+ s->from_domain.len+ s->callid.len
136
+ s->to_tag.len+ s->from_tag.len+s->sockinfo_str.len+s->event_id.len
137
+ s->local_contact.len+ s->contact.len+ s->record_route.len
138
+ s->reason.len+ s->watcher_user.len+ s->watcher_domain.len
141
if(mem_type & PKG_MEM_TYPE)
142
dest= (subs_t*)pkg_malloc(size);
144
dest= (subs_t*)shm_malloc(size);
148
ERR_MEM((mem_type==PKG_MEM_TYPE)?PKG_MEM_STR:SHARE_MEM);
150
memset(dest, 0, size);
151
size= sizeof(subs_t);
153
CONT_COPY(dest, dest->pres_uri, s->pres_uri)
154
CONT_COPY(dest, dest->to_user, s->to_user)
155
CONT_COPY(dest, dest->to_domain, s->to_domain)
156
CONT_COPY(dest, dest->from_user, s->from_user)
157
CONT_COPY(dest, dest->from_domain, s->from_domain)
158
CONT_COPY(dest, dest->watcher_user, s->watcher_user)
159
CONT_COPY(dest, dest->watcher_domain, s->watcher_domain)
160
CONT_COPY(dest, dest->to_tag, s->to_tag)
161
CONT_COPY(dest, dest->from_tag, s->from_tag)
162
CONT_COPY(dest, dest->callid, s->callid)
163
CONT_COPY(dest, dest->sockinfo_str, s->sockinfo_str)
164
CONT_COPY(dest, dest->local_contact, s->local_contact)
165
CONT_COPY(dest, dest->contact, s->contact)
166
CONT_COPY(dest, dest->record_route, s->record_route)
168
CONT_COPY(dest, dest->event_id, s->event_id)
170
CONT_COPY(dest, dest->reason, s->reason)
172
dest->event= s->event;
173
dest->local_cseq= s->local_cseq;
174
dest->remote_cseq= s->remote_cseq;
175
dest->status= s->status;
176
dest->version= s->version;
177
dest->send_on_cback= s->send_on_cback;
178
dest->expires= s->expires;
179
dest->db_flag= s->db_flag;
186
if(mem_type & PKG_MEM_TYPE)
195
subs_t* mem_copy_subs_noc(subs_t* s)
200
size= sizeof(subs_t)+ (s->pres_uri.len+ s->to_user.len
201
+ s->to_domain.len+ s->from_user.len+ s->from_domain.len+ s->callid.len
202
+ s->to_tag.len+ s->from_tag.len+s->sockinfo_str.len+s->event_id.len
203
+ s->local_contact.len + s->record_route.len+
204
+ 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)
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;
242
dest->contact.s= (char*)shm_malloc(s->contact.len* sizeof(char));
243
if(dest->contact.s== NULL)
247
memcpy(dest->contact.s, s->contact.s, s->contact.len);
248
dest->contact.len= s->contact.len;
258
int insert_shtable(shtable_t htable,unsigned int hash_code, subs_t* subs)
260
subs_t* new_rec= NULL;
262
new_rec= mem_copy_subs_noc(subs);
265
LM_ERR("copying in share memory a subs_t structure\n");
268
new_rec->expires+= (int)time(NULL);
270
lock_get(&htable[hash_code].lock);
271
new_rec->next= htable[hash_code].entries->next;
272
htable[hash_code].entries->next= new_rec;
273
lock_release(&htable[hash_code].lock);
278
int delete_shtable(shtable_t htable,unsigned int hash_code,str to_tag)
280
subs_t* s= NULL, *ps= NULL;
283
lock_get(&htable[hash_code].lock);
285
ps= htable[hash_code].entries;
290
if(s->to_tag.len== to_tag.len &&
291
strncmp(s->to_tag.s, to_tag.s, to_tag.len)== 0)
293
found= s->local_cseq +1;
295
if(s->contact.s!=NULL)
296
shm_free(s->contact.s);
303
lock_release(&htable[hash_code].lock);
307
void free_subs_list(subs_t* s_array, int mem_type, int ic)
314
s_array= s_array->next;
315
if(mem_type & PKG_MEM_TYPE)
318
pkg_free(s->contact.s);
324
shm_free(s->contact.s);
331
int update_shtable(shtable_t htable,unsigned int hash_code,
332
subs_t* subs, int type)
336
lock_get(&htable[hash_code].lock);
338
s= search_shtable(htable,subs->callid, subs->to_tag, subs->from_tag,
342
LM_DBG("record not found in hash table\n");
343
lock_release(&htable[hash_code].lock);
347
if(type & REMOTE_TYPE)
349
s->expires= subs->expires+ (int)time(NULL);
350
s->remote_cseq= subs->remote_cseq;
354
subs->local_cseq = ++s->local_cseq;
355
subs->version = ++s->version;
358
if(strncmp(s->contact.s, subs->contact.s, subs->contact.len))
360
shm_free(s->contact.s);
361
s->contact.s= (char*)shm_malloc(subs->contact.len* sizeof(char));
362
if(s->contact.s== NULL)
364
lock_release(&htable[hash_code].lock);
365
LM_ERR("no more shared memory\n");
368
memcpy(s->contact.s, subs->contact.s, subs->contact.len);
369
s->contact.len= subs->contact.len;
372
s->status= subs->status;
373
s->event= subs->event;
374
subs->db_flag= s->db_flag;
376
if(s->db_flag & NO_UPDATEDB_FLAG)
377
s->db_flag= UPDATEDB_FLAG;
379
lock_release(&htable[hash_code].lock);
384
phtable_t* new_phtable(void)
386
phtable_t* htable= NULL;
390
htable= (phtable_t*)shm_malloc(phtable_size* sizeof(phtable_t));
395
memset(htable, 0, phtable_size* sizeof(phtable_t));
397
for(i= 0; i< phtable_size; i++)
399
if(lock_init(&htable[i].lock)== 0)
401
LM_ERR("initializing lock [%d]\n", i);
404
htable[i].entries= (pres_entry_t*)shm_malloc(sizeof(pres_entry_t));
405
if(htable[i].entries== NULL)
409
memset(htable[i].entries, 0, sizeof(pres_entry_t));
410
htable[i].entries->next= NULL;
420
if(htable[i].entries)
421
shm_free(htable[i].entries);
424
lock_destroy(&htable[i].lock);
432
void destroy_phtable(void)
435
pres_entry_t* p, *prev_p;
437
if(pres_htable== NULL)
440
for(i= 0; i< phtable_size; i++)
442
lock_destroy(&pres_htable[i].lock);
443
p= pres_htable[i].entries;
449
shm_free(prev_p->sphere);
453
shm_free(pres_htable);
455
/* entry must be locked before calling this function */
457
pres_entry_t* search_phtable(str* pres_uri,int event, unsigned int hash_code)
461
LM_DBG("pres_uri= %.*s\n", pres_uri->len, pres_uri->s);
462
p= pres_htable[hash_code].entries->next;
465
if(p->event== event && p->pres_uri.len== pres_uri->len &&
466
strncmp(p->pres_uri.s, pres_uri->s, pres_uri->len)== 0 )
473
int insert_phtable(str* pres_uri, int event, char* sphere)
475
unsigned int hash_code;
476
pres_entry_t* p= NULL;
479
hash_code= core_hash(pres_uri, NULL, phtable_size);
481
lock_get(&pres_htable[hash_code].lock);
483
p= search_phtable(pres_uri, event, hash_code);
487
lock_release(&pres_htable[hash_code].lock);
490
size= sizeof(pres_entry_t)+ pres_uri->len* sizeof(char);
492
p= (pres_entry_t*)shm_malloc(size);
495
lock_release(&pres_htable[hash_code].lock);
500
size= sizeof(pres_entry_t);
501
p->pres_uri.s= (char*)p+ size;
502
memcpy(p->pres_uri.s, pres_uri->s, pres_uri->len);
503
p->pres_uri.len= pres_uri->len;
507
p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
510
lock_release(&pres_htable[hash_code].lock);
513
strcpy(p->sphere, sphere);
519
p->next= pres_htable[hash_code].entries->next;
520
pres_htable[hash_code].entries->next= p;
522
lock_release(&pres_htable[hash_code].lock);
530
int delete_phtable(str* pres_uri, int event)
532
unsigned int hash_code;
533
pres_entry_t* p= NULL, *prev_p= NULL;
535
hash_code= core_hash(pres_uri, NULL, phtable_size);
537
lock_get(&pres_htable[hash_code].lock);
539
p= search_phtable(pres_uri, event, hash_code);
542
LM_DBG("record not found\n");
543
lock_release(&pres_htable[hash_code].lock);
548
if(p->publ_count== 0)
551
prev_p= pres_htable[hash_code].entries;
556
prev_p= prev_p->next;
558
if(prev_p->next== NULL)
560
LM_ERR("record not found\n");
561
lock_release(&pres_htable[hash_code].lock);
564
prev_p->next= p->next;
570
lock_release(&pres_htable[hash_code].lock);
575
int update_phtable(presentity_t* presentity, str pres_uri, str body)
578
unsigned int hash_code;
584
sphere= extract_sphere(body);
587
LM_DBG("no sphere defined in new body\n");
591
/* search for record in hash table */
592
hash_code= core_hash(&pres_uri, NULL, phtable_size);
594
lock_get(&pres_htable[hash_code].lock);
596
p= search_phtable(&pres_uri, presentity->event->evp->type, hash_code);
599
lock_release(&pres_htable[hash_code].lock);
605
if(strcmp(p->sphere, sphere)!= 0)
607
/* new sphere definition */
612
/* no change in sphere definition */
613
lock_release(&pres_htable[hash_code].lock);
621
p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
624
lock_release(&pres_htable[hash_code].lock);
628
strcpy(p->sphere, sphere);
630
lock_release(&pres_htable[hash_code].lock);
632
/* call for watchers status update */
634
if(presentity->event->get_rules_doc(&presentity->user, &presentity->domain,
637
LM_ERR("failed to retreive xcap document\n");
642
update_watchers_status(pres_uri, presentity->event, xcap_doc);
650
pkg_free(xcap_doc->s);