Merge pull request #924 from linuxmaniac/vseva/fix_app_python
[sip-router] / src / modules / presence / hash.c
1 /*
2  * presence module - presence server implementation
3  *
4  * Copyright (C) 2007 Voice Sistem S.R.L.
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
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
12  *
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.
17  *
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
21  *
22  */
23
24 /*! \file
25  * \brief Kamailio presence module
26  * \ingroup presence 
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
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"
36 #include "presence.h"
37 #include "hash.h"
38 #include "notify.h"
39
40 /* matching mode when removing subscriptions from memory */
41 extern int pres_subs_remove_match;
42
43 /**
44  * create the subscription hash table in shared memory
45  * - hash_size: number of slots
46  */
47 shtable_t new_shtable(int hash_size)
48 {
49         shtable_t htable= NULL;
50         int i, j;
51
52         i = 0;
53         htable= (subs_entry_t*)shm_malloc(hash_size* sizeof(subs_entry_t));
54         if(htable== NULL)
55         {
56                 ERR_MEM(SHARE_MEM);
57         }
58         memset(htable, 0, hash_size* sizeof(subs_entry_t));
59         for(i= 0; i< hash_size; i++)
60         {
61                 if(lock_init(&htable[i].lock)== 0)
62                 {
63                         LM_ERR("initializing lock [%d]\n", i);
64                         goto error;
65                 }
66                 htable[i].entries= (subs_t*)shm_malloc(sizeof(subs_t));
67                 if(htable[i].entries== NULL)
68                 {
69                         lock_destroy(&htable[i].lock);
70                         ERR_MEM(SHARE_MEM);
71                 }
72                 memset(htable[i].entries, 0, sizeof(subs_t));
73                 htable[i].entries->next= NULL;
74         }
75
76         return htable;
77
78 error:
79         if(htable)
80         {
81                 for(j=0; j< i; j++)
82                 {
83                         lock_destroy(&htable[j].lock);
84                         shm_free(htable[j].entries);
85                 }
86                 shm_free(htable);
87         }
88         return NULL;
89
90 }
91
92 void destroy_shtable(shtable_t htable, int hash_size)
93 {
94         int i;
95
96         if(htable== NULL)
97                 return;
98
99         for(i= 0; i< hash_size; i++)
100         {
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;
105         }
106         shm_free(htable);
107         htable= NULL;
108 }
109
110 subs_t* search_shtable(shtable_t htable,str callid,str to_tag,
111                 str from_tag,unsigned int hash_code)
112 {
113         subs_t* s;
114
115         s= htable[hash_code].entries?htable[hash_code].entries->next:NULL;
116
117         while(s)
118         {
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)
125                         return s;
126                 s= s->next;
127         }
128
129         return NULL;
130 }
131
132 subs_t* mem_copy_subs(subs_t* s, int mem_type)
133 {
134         int size;
135         subs_t* dest;
136
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
142                 + s->user_agent.len
143                 + 1)*sizeof(char);
144
145         if(mem_type & PKG_MEM_TYPE)
146                 dest= (subs_t*)pkg_malloc(size);
147         else
148                 dest= (subs_t*)shm_malloc(size);
149
150         if(dest== NULL)
151         {
152                 ERR_MEM((mem_type==PKG_MEM_TYPE)?PKG_MEM_STR:SHARE_MEM);
153         }
154         memset(dest, 0, size);
155         size= sizeof(subs_t);
156
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);
172         if(s->event_id.s)
173                 CONT_COPY(dest, dest->event_id, s->event_id);
174         if(s->reason.s)
175                 CONT_COPY(dest, dest->reason, s->reason);
176
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;
186
187         return dest;
188
189 error:
190         return NULL;
191 }
192
193
194 subs_t* mem_copy_subs_noc(subs_t* s)
195 {
196         int size;
197         subs_t* dest;
198
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
204                 + s->user_agent.len
205                 + 1)*sizeof(char);
206
207         dest= (subs_t*)shm_malloc(size);
208         if(dest== NULL)
209         {
210                 ERR_MEM(SHARE_MEM);
211         }
212         memset(dest, 0, size);
213         size= sizeof(subs_t);
214
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);
229         if(s->event_id.s)
230                 CONT_COPY(dest, dest->event_id, s->event_id);
231         if(s->reason.s)
232                 CONT_COPY(dest, dest->reason, s->reason);
233
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;
243
244         dest->contact.s= (char*)shm_malloc(s->contact.len* sizeof(char));
245         if(dest->contact.s== NULL)
246         {
247                 ERR_MEM(SHARE_MEM);
248         }
249         memcpy(dest->contact.s, s->contact.s, s->contact.len);
250         dest->contact.len= s->contact.len;
251
252         return dest;
253
254 error:
255         if(dest)
256                         shm_free(dest);
257         return NULL;
258 }
259
260 int insert_shtable(shtable_t htable,unsigned int hash_code, subs_t* subs)
261 {
262         subs_t* new_rec= NULL;
263
264         new_rec= mem_copy_subs_noc(subs);
265         if(new_rec== NULL)
266         {
267                 LM_ERR("copying in share memory a subs_t structure\n");
268                 return -1;
269         }
270         new_rec->expires+= (int)time(NULL);
271
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);
276
277         return 0;
278 }
279
280 int delete_shtable(shtable_t htable,unsigned int hash_code,subs_t* subs)
281 {
282         subs_t* s= NULL, *ps= NULL;
283         int found= -1;
284
285         lock_get(&htable[hash_code].lock);
286         
287         ps= htable[hash_code].entries;
288         s= ps?ps->next:NULL;
289                 
290         while(s)
291         {
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)
296                         {
297                                 found = 0;
298                         }
299                 } else {
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)
307                         {
308                                 found = 0;
309                         }
310                 }
311                 if(found==0)
312                 {
313                         found= s->local_cseq +1;
314                         ps->next= s->next;
315                         if(s->contact.s!=NULL) {
316                                 shm_free(s->contact.s);
317                                 s->contact.s = NULL;
318                         }
319                         if (s) {
320                                 shm_free(s);
321                                 s = NULL;
322                         }
323                         break;
324                 }
325                 ps= s;
326                 s= s->next;
327         }
328         lock_release(&htable[hash_code].lock);
329         return found;
330 }
331
332 void free_subs_list(subs_t* s_array, int mem_type, int ic)
333 {
334         subs_t* s;
335
336         while(s_array)
337         {
338                 s= s_array;
339                 s_array= s_array->next;
340                 if(mem_type & PKG_MEM_TYPE)
341                 {
342                         if(ic) {
343                                 pkg_free(s->contact.s);
344                                 s->contact.s = NULL;
345                         }
346                         pkg_free(s);
347                         s = NULL;
348                 }
349                 else
350                 {
351                         if(ic) {
352                                 shm_free(s->contact.s);
353                                 s->contact.s = NULL;
354                         }
355                         shm_free(s);
356                         s = NULL;
357                 }
358         }
359         
360 }
361
362 int update_shtable(shtable_t htable,unsigned int hash_code, 
363                 subs_t* subs, int type)
364 {
365         subs_t* s;
366
367         lock_get(&htable[hash_code].lock);
368
369         s= search_shtable(htable,subs->callid, subs->to_tag, subs->from_tag,
370                         hash_code);
371         if(s== NULL)
372         {
373                 LM_DBG("record not found in hash table\n");
374                 lock_release(&htable[hash_code].lock);
375                 return -1;
376         }
377
378         if(type & REMOTE_TYPE)
379         {
380                 s->expires= subs->expires+ (int)time(NULL);
381                 s->remote_cseq= subs->remote_cseq;
382         }
383         else
384         {
385                 subs->local_cseq = ++s->local_cseq;
386                 subs->version = ++s->version;
387         }
388         
389         if(presence_sip_uri_match(&s->contact, &subs->contact))
390         {
391                 shm_free(s->contact.s);
392                 s->contact.s= (char*)shm_malloc(subs->contact.len* sizeof(char));
393                 if(s->contact.s== NULL)
394                 {
395                         lock_release(&htable[hash_code].lock);
396                         LM_ERR("no more shared memory\n");
397                         return -1;
398                 }
399                 memcpy(s->contact.s, subs->contact.s, subs->contact.len);
400                 s->contact.len= subs->contact.len;
401         }
402
403         s->status= subs->status;
404         s->event= subs->event;
405         subs->db_flag= s->db_flag;
406
407         if(s->db_flag & NO_UPDATEDB_FLAG)
408                 s->db_flag= UPDATEDB_FLAG;
409
410         lock_release(&htable[hash_code].lock);
411         
412         return 0;
413 }
414
415 phtable_t* new_phtable(void)
416 {
417         phtable_t* htable= NULL;
418         int i, j;
419
420         i = 0;
421         htable= (phtable_t*)shm_malloc(phtable_size* sizeof(phtable_t));
422         if(htable== NULL)
423         {
424                 ERR_MEM(SHARE_MEM);
425         }
426         memset(htable, 0, phtable_size* sizeof(phtable_t));
427
428         for(i= 0; i< phtable_size; i++)
429         {
430                 if(lock_init(&htable[i].lock)== 0)
431                 {
432                         LM_ERR("initializing lock [%d]\n", i);
433                         goto error;
434                 }
435                 htable[i].entries= (pres_entry_t*)shm_malloc(sizeof(pres_entry_t));
436                 if(htable[i].entries== NULL)
437                 {
438                         ERR_MEM(SHARE_MEM);
439                 }
440                 memset(htable[i].entries, 0, sizeof(pres_entry_t));
441                 htable[i].entries->next= NULL;
442         }
443
444         return htable;
445
446 error:
447         if(htable)
448         {
449                 for(j=0; j< i; j++)
450                 {
451                         if(htable[i].entries)
452                                 shm_free(htable[i].entries);
453                         else 
454                                 break;
455                         lock_destroy(&htable[i].lock);
456                 }
457                 shm_free(htable);
458         }
459         return NULL;
460
461 }
462
463 void destroy_phtable(void)
464 {
465         int i;
466         pres_entry_t* p, *prev_p;
467
468         if(pres_htable== NULL)
469                 return;
470
471         for(i= 0; i< phtable_size; i++)
472         {
473                 lock_destroy(&pres_htable[i].lock);
474                 p= pres_htable[i].entries;
475                 while(p)
476                 {
477                         prev_p= p;
478                         p= p->next;
479                         if(prev_p->sphere)
480                                 shm_free(prev_p->sphere);
481                         shm_free(prev_p);
482                 }
483         }
484         shm_free(pres_htable);
485 }
486 /* entry must be locked before calling this function */
487
488 pres_entry_t* search_phtable(str* pres_uri,int event, unsigned int hash_code)
489 {
490         pres_entry_t* p;
491
492         LM_DBG("pres_uri= %.*s\n", pres_uri->len,  pres_uri->s);
493         p= pres_htable[hash_code].entries->next;
494         while(p)
495         {
496                 if(p->event== event && p->pres_uri.len== pres_uri->len &&
497                                 presence_sip_uri_match(&p->pres_uri, pres_uri)== 0 )
498                         return p;
499                 p= p->next;
500         }
501         return NULL;
502 }
503
504 int insert_phtable(str* pres_uri, int event, char* sphere)
505 {
506         unsigned int hash_code;
507         pres_entry_t* p= NULL;
508         int size;
509
510         hash_code= core_case_hash(pres_uri, NULL, phtable_size);
511
512         lock_get(&pres_htable[hash_code].lock);
513         
514         p= search_phtable(pres_uri, event, hash_code);
515         if(p)
516         {
517                 p->publ_count++;
518                 lock_release(&pres_htable[hash_code].lock);
519                 return 0;
520         }
521         size= sizeof(pres_entry_t)+ pres_uri->len* sizeof(char);
522
523         p= (pres_entry_t*)shm_malloc(size);
524         if(p== NULL)
525         {
526                 lock_release(&pres_htable[hash_code].lock);
527                 ERR_MEM(SHARE_MEM);
528         }
529         memset(p, 0, size);
530
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;
535         
536         if(sphere)
537         {
538                 p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
539                 if(p->sphere== NULL)
540                 {
541                         lock_release(&pres_htable[hash_code].lock);
542                         shm_free(p);
543                         ERR_MEM(SHARE_MEM);
544                 }
545                 strcpy(p->sphere, sphere);
546         }
547
548         p->event= event;
549         p->publ_count=1;
550
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;
554
555         lock_release(&pres_htable[hash_code].lock);
556         
557         return 0;
558
559 error:
560         return -1;
561 }
562
563 int delete_phtable(str* pres_uri, int event)
564 {
565         unsigned int hash_code;
566         pres_entry_t* p= NULL, *prev_p= NULL;
567
568         hash_code= core_case_hash(pres_uri, NULL, phtable_size);
569
570         lock_get(&pres_htable[hash_code].lock);
571         
572         p= search_phtable(pres_uri, event, hash_code);
573         if(p== NULL)
574         {
575                 LM_DBG("record not found\n");
576                 lock_release(&pres_htable[hash_code].lock);
577                 return 0;
578         }
579         
580         p->publ_count--;
581         if(p->publ_count== 0)
582         {
583                 /* delete record */     
584                 prev_p= pres_htable[hash_code].entries;
585                 while(prev_p->next)
586                 {
587                         if(prev_p->next== p)
588                                 break;
589                         prev_p= prev_p->next;
590                 }
591                 if(prev_p->next== NULL)
592                 {
593                         LM_ERR("record not found\n");
594                         lock_release(&pres_htable[hash_code].lock);
595                         return -1;
596                 }
597                 prev_p->next= p->next;
598                 if(p->sphere)
599                         shm_free(p->sphere);
600
601                 shm_free(p);
602         }
603         lock_release(&pres_htable[hash_code].lock);
604
605         return 0;       
606 }
607
608 int update_phtable(presentity_t* presentity, str pres_uri, str body)
609 {
610         char* sphere= NULL;
611         unsigned int hash_code;
612         pres_entry_t* p;
613         int ret= 0;
614         str* xcap_doc= NULL;
615
616         /* get new sphere */
617         sphere= extract_sphere(body);
618         if(sphere==NULL)
619         {
620                 LM_DBG("no sphere defined in new body\n");
621                 return 0;
622         }
623
624         /* search for record in hash table */
625         hash_code= core_case_hash(&pres_uri, NULL, phtable_size);
626         
627         lock_get(&pres_htable[hash_code].lock);
628
629         p= search_phtable(&pres_uri, presentity->event->evp->type, hash_code);
630         if(p== NULL)
631         {
632                 lock_release(&pres_htable[hash_code].lock);
633                 goto done;
634         }
635         
636         if(p->sphere)
637         {
638                 if(strcmp(p->sphere, sphere)!= 0)
639                 {
640                         /* new sphere definition */
641                         shm_free(p->sphere);
642                 }
643                 else
644                 {
645                         /* no change in sphere definition */
646                         lock_release(&pres_htable[hash_code].lock);
647                         pkg_free(sphere);
648                         return 0;
649                 }
650         
651         }
652
653
654         p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
655         if(p->sphere== NULL)
656         {
657                 lock_release(&pres_htable[hash_code].lock);
658                 ret= -1;
659                 goto done;
660         }
661         strcpy(p->sphere, sphere);
662                 
663         lock_release(&pres_htable[hash_code].lock);
664
665         /* call for watchers status update */
666
667         if(presentity->event->get_rules_doc(&presentity->user, &presentity->domain,
668                                 &xcap_doc)< 0)
669         {
670                 LM_ERR("failed to retrieve xcap document\n");
671                 ret= -1;
672                 goto done;
673         }
674
675         update_watchers_status(pres_uri, presentity->event, xcap_doc);
676
677
678 done:
679
680         if(xcap_doc)
681         {
682                 if(xcap_doc->s)
683                         pkg_free(xcap_doc->s);
684                 pkg_free(xcap_doc);
685         }
686
687         if(sphere)
688                 pkg_free(sphere);
689         return ret;
690 }