Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[69e503]: server / subscriber.cpp Maximize Restore History

Download this file

subscriber.cpp    227 lines (197 with data), 6.8 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
// Copyright (C) 2007-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>
#include <stddef.h>
NAMESPACE_SIPWITCH
using namespace UCOMMON_NAMESPACE;
static class __LOCAL subscriber : private modules::sipwitch
{
private:
static subscriber _sub;
void registration(int id, modules::regmode_t mode);
bool authenticate(int id, const char *remote_realm);
void update(void);
public:
subscriber();
void reload(service *cfg);
void start(service *cfg);
void stop(service *cfg);
void snapshot(FILE *fp);
} _sub;
static volatile bool changed = false;
static volatile timeout_t interval = 50;
static volatile time_t refresh = 60;
static volatile time_t updated = 0;
static int priority = 0;
static const char *iface = NULL;
static char *server = NULL;
static char *proxy = NULL;
static char *userid = NULL;
static char *volatile secret = NULL;
static char *identity = NULL;
static MappedRegistry provider; // fake provider record to be used...
static unsigned short port = 9000;
subscriber::subscriber() :
modules::sipwitch()
{
zero_unsafe<MappedRegistry>(provider);
provider.rid = -1;
provider.type = MappedRegistry::EXTERNAL;
// we already know provider is normally external / outside NAT....
String::set(provider.network, sizeof(provider.network), "*");
}
void subscriber::update(void)
{
char contact[MAX_URI_SIZE];
char uri[MAX_URI_SIZE];
char reg[MAX_URI_SIZE];
unsigned len;
Socket::address dest = server;
modules::random_uuid(provider.remote);
snprintf(uri, sizeof(uri), "sip:%s@%s", userid, server);
snprintf(reg, sizeof(reg), "sip:%s", server);
snprintf(contact, sizeof(contact), "sip:%s@", provider.remote);
changed = false;
len = strlen(contact);
Socket::via((struct sockaddr *)&provider.contact, dest.getAddr());
Socket::query((struct sockaddr *)&provider.contact, contact + len, sizeof(contact) - len);
len = strlen(contact);
snprintf(contact + len, sizeof(contact) - len, ":%u", sip_port);
shell::debug(3, "registering %s with %s", contact, server);
provider.rid = modules::create_registration(uri, reg, contact, (int)refresh);
if(provider.rid == -1)
provider.status = MappedRegistry::OFFLINE;
else
provider.status = MappedRegistry::IDLE;
}
void subscriber::start(service *cfg)
{
assert(cfg != NULL);
if(count) {
provider.source.external.statnode = stats::request("provider");
if(changed)
update();
}
}
void subscriber::stop(service *cfg)
{
assert(cfg != NULL);
}
void subscriber::snapshot(FILE *fp)
{
assert(fp != NULL);
fprintf(fp, "subscriber:\n");
}
void subscriber::reload(service *cfg)
{
assert(cfg != NULL);
char *temp;
const char *key = NULL, *value;
linked_pointer<service::keynode> sp = cfg->getList("subscriber");
char buffer[160];
updated = 0l;
while(is(sp)) {
key = sp->getId();
value = sp->getPointer();
if(key && value) {
if(!stricmp(key, "count") && !is_configured())
count = atoi(value);
else if(!stricmp(key, "interface") && !is_configured())
iface = strdup(value);
else if(!stricmp(key, "interval"))
interval = atol(value);
else if(!stricmp(key, "priority") && !is_configured())
priority = atoi(value);
else if(!stricmp(key, "port") && !is_configured())
port = atoi(value);
// very rare we may wish to override provider network/nat state
else if(!stricmp(key, "network"))
String::set(provider.network, sizeof(provider.network), value);
else if(!stricmp(key, "refresh"))
refresh = atoi(value);
else if(!stricmp(key, "registrar") || !stricmp(key, "server")) {
if(uri::resolve(value, buffer, sizeof(buffer))) {
changed = true;
server = cfg->dup(buffer);
shell::debug(2, "subscriber provider is %s", buffer);
}
else {
changed = false;
shell::log(shell::ERR, "subscriber: %s: cannot resolve", value);
}
}
else if(!stricmp(key, "proxy")) {
temp = proxy;
proxy = strdup(value);
if(temp)
free(temp);
}
else if(!stricmp(key, "userid")) {
temp = userid;
userid = strdup(value);
if(temp)
free(temp);
}
else if(!stricmp(key, "secret")) {
temp = secret;
secret = strdup(value);
if(temp)
free(temp);
}
else if(!stricmp(key, "identity")) {
temp = identity;
identity = strdup(value);
if(temp)
free(temp);
}
}
sp.next();
}
if(!is_configured() && count)
stats::allocate(1);
}
void subscriber::registration(int id, modules::regmode_t mode)
{
if(id == -1 || id != provider.rid)
return;
switch(mode) {
case modules::REG_FAILED:
shell::log(shell::ERR, "service provider failed");
provider.rid = -1;
provider.status = MappedRegistry::OFFLINE;
if(changed)
update();
return;
case modules::REG_SUCCESS:
shell::log(shell::NOTIFY, "service provider active");
provider.status = MappedRegistry::IDLE;
return;
}
}
bool subscriber::authenticate(int id, const char *remote_realm)
{
if(id == -1 || id != provider.rid)
return false;
if(secret && *secret)
shell::debug(3, "authorizing %s for %s", userid, remote_realm);
else {
shell::debug(3, "cannot authorize %s for %s", userid, remote_realm);
return false;
}
modules::add_authentication(userid, secret, remote_realm);
return true;
}
END_NAMESPACE