presence: function to search presentity items for in memory subscriptions
[sip-router] / src / modules / presence / hash.h
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 :: Hash tables
26  * \ingroup presence 
27  */
28
29
30 #ifndef PS_HASH_H
31 #define PS_HASH_H
32
33 #include <stdint.h>
34
35 #include "../../core/lock_ops.h"
36
37 struct presentity;
38 #define REMOTE_TYPE 1 << 1
39 #define LOCAL_TYPE 1 << 2
40
41 #define PKG_MEM_STR "pkg"
42 #define SHARE_MEM "share"
43
44 #define ERR_MEM(mem_type)                        \
45         do {                                         \
46                 LM_ERR("No more %s memory\n", mem_type); \
47                 goto error;                              \
48         } while(0)
49
50 #define CONT_COPY(buf, dest, source)          \
51         do {                                      \
52                 dest.s = (char *)buf + size;          \
53                 memcpy(dest.s, source.s, source.len); \
54                 dest.len = source.len;                \
55                 size += source.len;                   \
56         } while(0)
57
58 #define PKG_MEM_TYPE (1 << 1)
59 #define SHM_MEM_TYPE (1 << 2)
60
61 extern int pres_delete_same_subs;
62
63 /* subscribe hash entry */
64 struct subscription;
65
66 typedef struct subs_entry
67 {
68         struct subscription *entries;
69         gen_lock_t lock;
70 } subs_entry_t;
71
72 typedef subs_entry_t *shtable_t;
73
74 shtable_t new_shtable(int hash_size);
75
76 struct subscription *search_shtable(shtable_t htable, str callid, str to_tag,
77                 str from_tag, unsigned int hash_code);
78
79 int insert_shtable(
80                 shtable_t htable, unsigned int hash_code, struct subscription *subs);
81
82 int delete_shtable(
83                 shtable_t htable, unsigned int hash_code, struct subscription *subs);
84
85 int update_shtable(shtable_t htable, unsigned int hash_code,
86                 struct subscription *subs, int type);
87
88 struct subscription *mem_copy_subs(struct subscription *s, int mem_type);
89
90 void free_subs_list(struct subscription *s_array, int mem_type, int ic);
91
92 void destroy_shtable(shtable_t htable, int hash_size);
93
94 /* subs htable functions type definitions */
95 typedef shtable_t (*new_shtable_t)(int hash_size);
96
97 typedef struct subscription *(*search_shtable_t)(shtable_t htable, str callid,
98                 str to_tag, str from_tag, unsigned int hash_code);
99
100 typedef int (*insert_shtable_t)(
101                 shtable_t htable, unsigned int hash_code, struct subscription *subs);
102
103 typedef int (*delete_shtable_t)(
104                 shtable_t htable, unsigned int hash_code, struct subscription *subs);
105
106 typedef int (*update_shtable_t)(shtable_t htable, unsigned int hash_code,
107                 struct subscription *subs, int type);
108
109 typedef void (*destroy_shtable_t)(shtable_t htable, int hash_size);
110
111 typedef struct subscription *(*mem_copy_subs_t)(
112                 struct subscription *s, int mem_type);
113
114
115 /* presentity hash table */
116 typedef struct pres_entry
117 {
118         str pres_uri;
119         int event;
120         int publ_count;
121         char *sphere;
122         struct pres_entry *next;
123 } pres_entry_t;
124
125 typedef struct pres_phtable
126 {
127         pres_entry_t *entries;
128         gen_lock_t lock;
129 } phtable_t;
130
131 phtable_t *new_phtable(void);
132
133 pres_entry_t *search_phtable(str *pres_uri, int event, unsigned int hash_code);
134
135 int insert_phtable(str *pres_uri, int event, char *sphere);
136
137 int update_phtable(struct presentity *presentity, str *pres_uri, str *body);
138
139 int delete_phtable(str *pres_uri, int event);
140
141 void destroy_phtable(void);
142
143 int delete_db_subs(str* to_tag, str* from_tag, str* callid);
144
145 typedef struct ps_presentity {
146         uint32_t bsize;
147         uint32_t hashid;
148         str user;
149         str domain;
150         str ruid;
151         str sender;
152         str event;
153         str etag;
154         int expires;
155         int received_time;
156         int priority;
157         str body;
158         struct ps_presentity *next;
159         struct ps_presentity *prev;
160 } ps_presentity_t;
161
162 typedef struct ps_pslot {
163         ps_presentity_t *plist;
164         gen_lock_t lock;
165 } ps_pslot_t;
166
167 typedef struct ps_ptable {
168         int ssize;
169         ps_pslot_t *slots;
170 } ps_ptable_t;
171
172 ps_presentity_t *ps_presentity_new(ps_presentity_t *pt, int mtype);
173 void ps_presentity_free(ps_presentity_t *pt, int mtype);
174 void ps_presentity_list_free(ps_presentity_t *pt, int mtype);
175 int ps_presentity_match(ps_presentity_t *pta, ps_presentity_t *ptb, int mmode);
176 int ps_ptable_init(int ssize);
177 void ps_ptable_destroy(void);
178 int ps_ptable_insert(ps_presentity_t *pt);
179 int ps_ptable_replace(ps_presentity_t *ptm, ps_presentity_t *pt);
180 int ps_ptable_update(ps_presentity_t *ptm, ps_presentity_t *pt);
181 int ps_ptable_remove(ps_presentity_t *pt);
182 ps_presentity_t *ps_ptable_get_list(str *user, str *domain);
183 ps_presentity_t *ps_ptable_get_item(str *user, str *domain, str *event, str *etag);
184 ps_presentity_t *ps_ptable_search(ps_presentity_t *ptm, int rmode);
185 ps_presentity_t *ps_ptable_get_expired(int eval);
186 ps_ptable_t *ps_ptable_get(void);
187
188 #endif