1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 /*! \file
22  *  \brief USRLOC - Usrloc domain structure
23  *  \ingroup usrloc
24  */
25 
26 #ifndef UDOMAIN_H
27 #define UDOMAIN_H
28 
29 
30 #include <stdio.h>
31 #include "../../core/counters.h"
32 #include "../../core/locking.h"
33 #include "../../core/str.h"
34 #include "../../lib/srdb1/db.h"
35 #include "../usrloc/usrloc.h"
36 #include "urecord.h"
37 #include "hslot.h"
38 
39 
40 struct hslot;   /*!< Hash table slot */
41 struct urecord; /*!< Usrloc record */
42 
43 
44 /*! \brief
45  * The structure represents a usrloc domain
46  */
47 struct udomain {
48 	str* name;                 /*!< Domain name (NULL terminated) */
49 	int size;                  /*!< Hash table size */
50 	struct hslot* table;       /*!< Hash table - array of collision slots */
51 	/* statistics */
52 	stat_var *users;           /*!< no of registered users */
53 	stat_var *contacts;        /*!< no of registered contacts */
54 	stat_var *expires;         /*!< no of expires */
55 	/* for ul_db_layer */
56 	int dbt;                   /* type of the database */
57 	db1_con_t * dbh;            /* database handle */
58 };
59 
60 
61 /*!
62  * \brief Create a new domain structure
63  * \param  _n is pointer to str representing name of the domain, the string is
64  * not copied, it should point to str structure stored in domain list
65  * \param _s is hash table size
66  * \param _d new created domain
67  * \return 0 on success, -1 on failure
68  */
69 int new_udomain(str* _n, int _s, udomain_t** _d);
70 
71 
72 /*!
73  * \brief Free all memory allocated for the domain
74  * \param _d freed domain
75  */
76 void free_udomain(udomain_t* _d);
77 
78 
79 /*!
80  * \brief Print udomain, debugging helper function
81  */
82 void print_udomain(FILE* _f, udomain_t* _d);
83 
84 
85 /*!
86  * \brief Timer function to cleanup expired contacts, DB_ONLY db_mode
87  * \param _d cleaned domain
88  * \return 0 on success, -1 on failure
89  */
90 /*
91 int db_timer_udomain(udomain_t* _d);
92 */
93 
94 
95 /*!
96  * \brief Run timer handler for given domain
97  * \param _d domain
98  */
99 void mem_timer_udomain(udomain_t* _d);
100 
101 
102 /*!
103  * \brief Insert a new record into domain in memory
104  * \param _d domain the record belongs to
105  * \param _aor address of record
106  * \param _r new created record
107  * \return 0 on success, -1 on failure
108  */
109 int mem_insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r);
110 
111 
112 /*!
113  * \brief Remove a record from domain in memory
114  * \param _d domain the record belongs to
115  * \param _r deleted record
116  */
117 void mem_delete_urecord(udomain_t* _d, struct urecord* _r);
118 
119 
120 
121 /*!
122  * \brief  Get lock for a slot
123  * \param _d domain
124  * \param i slot number
125  */
126 void lock_ulslot(udomain_t* _d, int i);
127 
128 /*!
129  * \brief Release lock for a slot
130  * \param _d domain
131  * \param i slot number
132  */
133 void unlock_ulslot(udomain_t* _d, int i);
134 
135 /* ===== module interface ======= */
136 
137 /*! \brief
138  * Timer handler for given domain
139  */
140 void lock_udomain(udomain_t* _d, str *_aor);
141 
142 
143 /*!
144  * \brief Release lock for a domain
145  * \param _d domain
146  * \param _aor address of record, uses as hash source for the lock slot
147  */
148 void unlock_udomain(udomain_t* _d, str *_aor);
149 
150 
151 
152 /*!
153  * \brief Create and insert a new record
154  * \param _d domain to insert the new record
155  * \param _aor address of the record
156  * \param _r new created record
157  * \return return 0 on success, -1 on failure
158  */
159 int insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r);
160 
161 
162 /*!
163  * \brief Obtain a urecord pointer if the urecord exists in domain
164  * \param _d domain to search the record
165  * \param _aor address of record
166  * \param _r new created record
167  * \return 0 if a record was found, 1 if nothing could be found
168  */
169 int get_urecord(udomain_t* _d, str* _aor, struct urecord** _r);
170 
171 
172 /*!
173  * \brief Obtain a urecord pointer if the urecord exists in domain (lock slot)
174  * \param _d domain to search the record
175  * \param _aorhash hash id for address of record
176  * \param _ruid record internal unique id
177  * \param _r store pointer to location record
178  * \param _c store pointer to contact structure
179  * \return 0 if a record was found, 1 if nothing could be found
180  */
181 int get_urecord_by_ruid(udomain_t* _d, unsigned int _aorhash,
182 		str *_ruid, struct urecord** _r, struct ucontact** _c);
183 
184 /*!
185  * \brief Delete a urecord from domain
186  * \param _d domain where the record should be deleted
187  * \param _aor address of record
188  * \param _r deleted record
189  * \return 0 on success, -1 if the record could not be deleted
190  */
191 int delete_urecord(udomain_t* _d, str* _aor, struct urecord* _r);
192 
193 
194 #endif
195