-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathSubscriberRegistry.h
243 lines (187 loc) · 6.39 KB
/
SubscriberRegistry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright 2011 Kestrel Signal Processing, Inc.
* Copyright 2011, 2012, 2014 Range Networks, Inc.
*
* This software is distributed under the terms of the GNU Affero Public License.
* See the COPYING file in the main directory for details.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SubscriberRegistry_H
#define SubscriberRegistry_H
#include <map>
#include <stdlib.h>
#include <Logger.h>
#include <Timeval.h>
#include <Threads.h>
#include <map>
#include <string>
#include "sqlite3.h"
using namespace std;
class SubscriberRegistry {
private:
sqlite3 *mDB; ///< database connection
unsigned mNumSQLTries; ///< Number of times to try an sqlite command before giving up.
#ifndef SR_API_ONLY
mutable Mutex mLock; ///< control for multithreaded read/write access to the memory based sip_buddies table
Thread mSyncer; ///< thread responsible for synchronizing the memory and disk based sip_buddies tables
#endif
public:
~SubscriberRegistry();
/**
Initialize the subscriber registry using parameters from gConfig.
@return 0 if the database was successfully opened and initialized; 1 otherwise
*/
int init();
typedef enum {
SUCCESS=0, ///< operation successful
FAILURE=1, ///< operation not successful
DELAYED=2, ///< operation successful, but effect delayed
TRYAGAIN=3 ///< operation not attempted, try again later
} Status;
sqlite3 *db()
{
return mDB;
}
/**
Grab the memory based sqlite db as a table string
*/
string getResultsAsString(string query);
/**
Grab the columns from a table
*/
vector<string> getTableColumns(string tableName);
#ifndef SR_API_ONLY
/**
Generate the SQL query which is used to sync the in-memory database to disk
to account for differences in database schemas.
*/
string generateSyncToDiskQuery();
/**
Sync the in-memory sip_buddies table to disk.
*/
bool syncMemoryDB();
#endif
/**
Resolve an ISDN or other numeric address to an IMSI.
@param ISDN Any numeric address, E.164, local extension, etc.
@return A C-string to be freed by the caller,
NULL if the ISDN cannot be resolved.
*/
char* getIMSI(const char* ISDN);
char* getIMSI2(const char *ISDN); // This version works with a plus
/**
Given an IMSI, return the local CLID, which should be a numeric address.
@param IMSI The subscriber IMSI.
@return A C-string to be freed by the caller,
NULL if the IMSI isn't found.
*/
char* getCLIDLocal(const char* IMSI);
/**
Given an IMSI, return the global CLID, which should be a numeric address.
@param IMSI The subscriber IMSI.
@return A C-string to be freed by the caller,
NULL if the IMSI isn't found.
*/
char* getCLIDGlobal(const char* IMSI);
/**
Given an IMSI, return the IP address of the most recent registration.
@param IMSI The subscriber IMSI
@return A C-string to be freed by the caller, "111.222.333.444:port",
NULL if the ISMI isn't registered.
*/
char* getRegistrationIP(const char* IMSI);
/**
Get a subscriber's property.
@param imsi imsi of the subscriber
@param key name of the property
*/
string imsiGet(string imsi, string key);
#ifndef SR_API_ONLY
/**
Set a subscriber's property.
@param imsi imsi of the subscriber
@param key name of the property
@param value value of the property
*/
bool imsiSet(string imsi, string key, string value);
/**
Set a subscriber's property.
@param imsi imsi of the subscriber
@param key1 name of the property
@param value1 value of the property
@param key2 name of the property
@param value2 value of the property
*/
bool imsiSet(string imsi, string key1, string value1, string key2, string value2);
#endif
/**
Add a new user to the SubscriberRegistry.
@param IMSI The user's IMSI or SIP username.
@param CLID The user's local CLID.
*/
Status addUser(const char* IMSI, const char* CLID);
/**
Remove a user from the SubscriberRegistry.
@param IMSI The user's IMSI or SIP username.
*/
Status removeUser(const char* IMSI);
/**
Set the current time as the time of the most recent registration for an IMSI.
@param IMSI The user's IMSI or SIP username.
*/
Status setRegTime(const char* IMSI);
char *mapCLIDGlobal(const char *local);
/**
Update the RRLP location for user
@param name IMSI to be updated
@param lat Latitude
@param lon Longitude
@param err Approximate Error
*/
Status RRLPUpdate(string name, string lat, string lon, string err);
private:
/**
Run sql statments locally.
@param stmt The sql statements.
@param resultptr Set this to point to the result of executing the statements.
*/
Status sqlLocal(const char *stmt, char **resultptr);
/**
Run an sql query (select unknownColumn from table where knownColumn = knownValue).
@param unknownColumn The column whose value you want.
@param table The table to look in.
@param knownColumn The column with the value you know.
@param knownValue The known value of knownColumn.
*/
char *sqlQuery(const char *unknownColumn, const char *table, const char *knownColumn, const char *knownValue);
/**
Run an sql query (select unknownColumn from table where knownColumn = knownValue).
@param unknownColumn The column whose value you want.
@param table The table to look in.
@param knownColumn The column with the value you know.
@param knownValue1 The first known value of knownColumn.
@param knownValue2 The second known value of knownColumn.
*/
char *sqlQuery2(const char *unknownColumn, const char *table, const char *knownColumn, const char *knownValue1, const char *knownValue2);
/**
Run an sql update.
@param stmt The update statement.
*/
Status sqlUpdate(const char *stmt);
};
/** Periodically triggers SubscriberRegistry::syncMemoryDB(). */
void* subscriberRegistrySyncer(void*);
#endif
// vim: ts=4 sw=4