[50ef25]: server / forward.cpp  Maximize  Restore  History

Download this file

417 lines (367 with data), 11.1 kB

  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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (C) 2008-2010 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <sipwitch-config.h>
#include <sipwitch/sipwitch.h>
NAMESPACE_SIPWITCH
using namespace UCOMMON_NAMESPACE;
#define INDEX_SIZE 177
class __LOCAL forward : public modules::sipwitch
{
public:
class __LOCAL regmap : public LinkedObject
{
public:
friend class forward;
MappedRegistry *entry;
bool active;
};
char *volatile proxy;
char *volatile realm;
char *volatile digest;
char *volatile server;
char *volatile refer;
time_t expires;
bool enabled;
condlock_t locking;
unsigned allocated, active;
regmap *freelist;
regmap *index[INDEX_SIZE];
memalloc pager;
forward();
void activate(int id);
void disable(int id);
bool isActive(int id);
void remove(int id);
void add(MappedRegistry *rr);
MappedRegistry *find(int id);
void releaseMap(MappedRegistry *rr);
private:
void start(service *cfg);
void reload(service *cfg);
void activating(MappedRegistry *rr);
void expiring(MappedRegistry *rr);
void registration(int id, modules::regmode_t mode);
bool announce(MappedRegistry *rr, const char *msgtype, const char *event, const char *expires, const char *body);
bool authenticate(int id, const char *remote_realm);
char *referLocal(MappedRegistry *rr, const char *target, char *buffer, size_t size);
};
static forward forward_plugin;
forward::forward() :
modules::sipwitch()
{
shell::log(shell::INFO, "%s\n",
_TEXT("server forward plugin loaded"));
enabled = false;
refer = NULL;
digest = (char *)"MD5";
realm = (char *)"GNU Telephony";
proxy = NULL;
freelist = NULL;
memset(index, 0, sizeof(index));
allocated = active = 0;
expires = 120;
}
void forward::releaseMap(MappedRegistry *rr)
{
if(rr)
locking.release();
}
bool forward::isActive(int id)
{
bool activeflag = false;
linked_pointer<regmap> mp;
int path = id % INDEX_SIZE;
locking.access();
mp = index[path];
while(is(mp)) {
if(mp->active) {
activeflag = true;
break;
}
mp.next();
}
locking.release();
return activeflag;
}
MappedRegistry *forward::find(int id)
{
linked_pointer<regmap> mp;
int path = id % INDEX_SIZE;
locking.access();
mp = index[path];
while(is(mp)) {
if(mp->entry->rid == id)
return mp->entry;
mp.next();
}
locking.release();
return NULL;
}
void forward::disable(int id)
{
linked_pointer<regmap> mp;
int path = id % INDEX_SIZE;
locking.access();
mp = index[path];
while(is(mp)) {
if(mp->entry->rid == id) {
mp->active = false;
break;
}
mp.next();
}
locking.release();
}
void forward::activate(int id)
{
linked_pointer<regmap> mp;
int path = id % INDEX_SIZE;
locking.access();
mp = index[path];
while(is(mp)) {
if(mp->entry->rid == id) {
mp->active = true;
break;
}
mp.next();
}
locking.release();
}
void forward::remove(int id)
{
regmap *prior = NULL;
linked_pointer<regmap> mp;
int path = id % INDEX_SIZE;
locking.modify();
mp = index[path];
while(is(mp)) {
if(mp->entry->rid == id) {
if(prior)
prior->next = mp->next;
else
index[path] = (regmap *)mp->next;
mp->next = freelist;
freelist = *mp;
shell::debug(3, "forward unmap %s from %d", mp->entry->userid, id);
--active;
locking.commit();
mp->entry->rid = -1;
return;
}
mp.next();
}
shell::debug(3, "forward map %d not found", id);
locking.commit();
}
void forward::add(MappedRegistry *rr)
{
regmap *map;
int path = rr->rid % INDEX_SIZE;
locking.modify();
map = freelist;
if(map)
freelist = (regmap *)map->next;
else {
++allocated;
map = (regmap *)pager.alloc(sizeof(regmap));
}
map->entry = rr;
map->next = index[path];
index[path] = map;
locking.commit();
shell::debug(3, "forward mapped %s as %d", rr->userid, rr->rid);
++active;
}
void forward::reload(service *cfg)
{
assert(cfg != NULL);
char buffer[160];
bool refering = false;
bool enable = false;
const char *key = NULL, *value;
char *tmp_realm = (char *)realm, *tmp_digest = cfg->dup((char *)digest);
linked_pointer<service::keynode> fp = cfg->getList("forward");
linked_pointer<service::keynode> sp = cfg->getList("registry");
while(is(sp)) {
key = sp->getId();
value = sp->getPointer();
if(key && value) {
if(String::equal(key, "digest"))
tmp_digest = cfg->dup(value);
else if(String::equal(key, "realm"))
tmp_realm = cfg->dup(value);
}
sp.next();
}
while(is(fp)) {
key = fp->getId();
value = fp->getPointer();
if(key && value) {
if(String::equal(key, "refer")) {
if(uri::resolve(value, buffer, sizeof(buffer))) {
refer = cfg->dup(buffer);
refering = true;
shell::debug(2, "forward refer resolved as %s", buffer);
}
else {
shell::log(shell::ERR, "forward: %s: cannot resolve", value);
server = NULL;
}
}
else if(String::equal(key, "server")) {
if(uri::resolve(value, buffer, sizeof(buffer))) {
server = cfg->dup(buffer);
shell::debug(2, "forward server resolved as %s", buffer);
}
else {
shell::log(shell::ERR, "forward: %s: cannot resolve", value);
server = NULL;
}
if(server && *server) {
enable = true;
service::dialmode = service::EXT_DIALING;
}
}
else if(String::equal(key, "expires"))
expires = atoi(value);
else if(String::equal(key, "digest"))
tmp_digest = cfg->dup(value);
else if(String::equal(key, "realm"))
tmp_realm = cfg->dup(value);
}
fp.next();
}
if(!refering)
refer = NULL;
String::upper(tmp_digest);
realm = tmp_realm;
digest = tmp_digest;
if(enable && !enabled)
shell::log(shell::INFO, "server forward plugin activated");
else if(!enable && enabled)
shell::log(shell::INFO, "server forward plugin disabled");
enabled = enable;
}
void forward::start(service *cfg)
{
assert(cfg != NULL);
}
void forward::activating(MappedRegistry *rr)
{
char contact[MAX_URI_SIZE];
char uri[MAX_URI_SIZE];
char reg[MAX_URI_SIZE];
unsigned len;
if(!enabled || rr->rid != -1)
return;
// must also have extension to forward...
if(rr->remote[0] && rr->ext && rr->type == MappedRegistry::USER) {
snprintf(uri, sizeof(uri), "sip:%s@%s", rr->userid, server);
snprintf(reg, sizeof(reg), "sip:%s", server);
snprintf(contact, sizeof(contact), "sip:%s@", rr->remote);
len = strlen(contact);
Socket::getaddress((struct sockaddr *)&rr->contact, contact + len, sizeof(contact) - len);
len = strlen(contact);
snprintf(contact + len, sizeof(contact) - len, ":%d", Socket::getservice((struct sockaddr *)&rr->contact));
shell::debug(3, "registering %s with %s", contact, server);
rr->rid = modules::create_registration(uri, reg, contact, (int)expires);
if(rr->rid != -1)
add(rr);
}
}
bool forward::announce(MappedRegistry *rr, const char *msgtype, const char *event, const char *expiration, const char *body)
{
char uri_to[MAX_URI_SIZE];
char contact[MAX_URI_SIZE];
size_t len;
if(!isActive(rr->rid) || !rr->remote[0])
return false;
snprintf(uri_to, sizeof(uri_to), "sip:%s@%s", rr->userid, server);
snprintf(contact, sizeof(contact), "sip:%s@", rr->remote);
len = strlen(contact);
Socket::getaddress((struct sockaddr *)&rr->contact, contact + len, sizeof(contact) - len);
len = strlen(contact);
snprintf(contact + len, sizeof(contact) - len, ":%d", Socket::getservice((struct sockaddr *)&rr->contact));
shell::debug(3, "publishing %s with %s", contact, server);
modules::publish(uri_to, contact, event, expiration, msgtype, body);
return true;
}
void forward::expiring(MappedRegistry *rr)
{
int id = rr->rid;
if(id == -1)
return;
remove(rr->rid);
if(!enabled)
return;
modules::release_registration(id);
}
char *forward::referLocal(MappedRegistry *rr, const char *target, char *buffer, size_t size)
{
if(!refer)
return NULL;
if(!isActive(rr->rid))
return NULL;
if(sip_tlsmode)
snprintf(buffer, sizeof(buffer), "sips:%s@%s", target, refer);
else
snprintf(buffer, sizeof(buffer), "sip:%s@%s", target, refer);
return buffer;
}
bool forward::authenticate(int id, const char *remote_realm)
{
MappedRegistry *rr;
service::keynode *node, *leaf;
const char *secret = NULL;
if(id == -1)
return false;
rr = find(id);
if(!rr)
return false;
node = service::getUser(rr->userid);
if(node) {
leaf = node->leaf("secret");
if(leaf)
secret = leaf->getPointer();
}
if(secret && *secret)
shell::debug(3, "authorizing %s for %s", rr->userid, remote_realm);
else {
shell::debug(3, "cannot authorize %s for %s", rr->userid, remote_realm);
service::release(node);
releaseMap(rr);
remove(id);
return false;
}
modules::add_authentication(rr->userid, secret, remote_realm);
service::release(node);
releaseMap(rr);
return true;
}
void forward::registration(int id, modules::regmode_t mode)
{
switch(mode) {
case modules::REG_FAILED:
disable(id);
return;
case modules::REG_TERMINATED:
remove(id);
return;
case modules::REG_SUCCESS:
activate(id);
return;
}
}
END_NAMESPACE

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks