[ce0b0e]: server / thread.cpp Maximize Restore History

Download this file

thread.cpp    345 lines (299 with data), 8.6 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
// Copyright (C) 2006-2007 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 "server.h"
NAMESPACE_SIPWITCH
using namespace UCOMMON_NAMESPACE;
static volatile bool warning_registry = false;
static bool shutdown_flag = false;
static unsigned shutdown_count = 0;
static unsigned startup_count = 0;
static char *remove_quotes(char *c)
{
char *o = c;
char *d = c;
if(*c != '\"')
return c;
++c;
while(*c)
*(d++) = *(c++);
*(--d) = 0;
return o;
}
thread::thread() : DetachedThread(stack::sip.stacksize)
{
config = NULL;
registry = NULL;
via = NULL;
}
bool thread::authorize(void)
{
osip_message_t *reply = NULL;
int error = 401;
if(!authenticate())
return false;
registry = registry::access(identity);
if(registry) {
warning_registry = false;
if(!registry->expires)
return true;
time(&current);
if(registry->expires < current)
return true;
}
else {
if(!warning_registry) {
warning_registry = true;
service::errlog(service::WARN, "registry capacity reached");
}
error = 480;
}
eXosip_lock();
eXosip_message_build_answer(sevent->tid, error, &reply);
eXosip_message_send_answer(sevent->tid, error, reply);
eXosip_unlock();
return false;
}
bool thread::authenticate(void)
{
osip_message_t *reply = NULL;
osip_authorization_t *auth = NULL;
service::keynode *node = NULL, *leaf;
stringbuf<64> digest;
int error = 407;
auth = NULL;
if(!sevent->request || osip_message_get_authorization(sevent->request, 0, &auth) != 0 || !auth || !auth->username || !auth->response) {
service::errlog(service::DEBUG, "challenge request required");
challenge();
return false;
}
remove_quotes(auth->username);
remove_quotes(auth->uri);
remove_quotes(auth->nonce);
remove_quotes(auth->response);
node = config::getProvision(auth->username);
if(!node) {
service::errlog(service::NOTICE, "rejecting unknown %s", auth->username);
error = 404;
goto failed;
}
leaf = node->leaf("digest");
if(!leaf || !leaf->getPointer()) {
service::errlog(service::NOTICE, "rejecting unsupported %s", auth->username);
error = 403;
goto failed;
}
snprintf(buffer, sizeof(buffer), "%s:%s", sevent->request->sip_method, auth->uri);
if(!stricmp(registry::getDigest(), "sha1"))
digest::sha1(digest, buffer);
else if(!stricmp(registry::getDigest(), "rmd160"))
digest::rmd160(digest, buffer);
else
digest::md5(digest, buffer);
snprintf(buffer, sizeof(buffer), "%s:%s:%s", leaf->getPointer(), auth->nonce, *digest);
if(!stricmp(registry::getDigest(), "sha1"))
digest::sha1(digest, buffer);
else if(!stricmp(registry::getDigest(), "rmd160"))
digest::rmd160(digest, buffer);
else
digest::md5(digest, buffer);
if(stricmp(*digest, auth->response)) {
service::errlog(service::NOTICE, "rejecting unauthorized %s", auth->username);
goto failed;
}
config = node;
identity = auth->username;
return true;
failed:
config::release(node);
eXosip_lock();
eXosip_message_build_answer(sevent->tid, error, &reply);
eXosip_message_send_answer(sevent->tid, error, reply);
eXosip_unlock();
return false;
}
void thread::challenge(void)
{
osip_message_t *reply = NULL;
char nonce[32];
time_t now;
time(&now);
snprintf(nonce, sizeof(nonce), "%08lx", now);
snprintf(buffer, sizeof(buffer),
"Digest realm=\"%s\", nonce=\"%s\", algorithm=%s",
registry::getRealm(), nonce, registry::getDigest());
eXosip_lock();
eXosip_message_build_answer(sevent->tid, 407, &reply);
osip_message_set_header(reply, "WWW-Authenticate", buffer);
eXosip_message_send_answer(sevent->tid, 407, reply);
eXosip_unlock();
}
void thread::registration(void)
{
osip_header_t *header = NULL;
osip_contact_t *contact = NULL;
osip_uri_param_t *param = NULL;
int interval = -1;
int pos = 0, vpos = 0;
osip_message_get_expires(sevent->request, 0, &header);
while(osip_list_eol(OSIP2_LIST_PTR sevent->request->contacts, pos) == 0) {
contact = (osip_contact_t *)osip_list_get(OSIP2_LIST_PTR sevent->request->contacts, pos++);
if(contact && contact->url) {
osip_contact_param_get_byname(contact, "expires", &param);
if(param && param->gvalue)
interval = osip_atoi(param->gvalue);
break;
}
}
if(!contact) {
deregister();
return;
}
if(interval < 0 && header && header->hvalue)
interval = atoi(header->hvalue);
if(interval < 0)
interval = registry::getExpires();
if(!interval || !contact) {
deregister();
return;
}
reregister(interval);
}
void thread::reregister(time_t interval)
{
int answer = 200;
osip_message_t *reply = NULL;
time_t expire;
unsigned count;
registry = registry::create(identity);
if(!registry) {
if(!warning_registry) {
warning_registry = true;
service::errlog(service::ERROR, "registry capacity reached");
}
answer = 480;
interval = 0;
goto reply;
}
warning_registry = false;
time(&expire);
expire += interval + 3; // overdraft 3 seconds...
if(registry->type == REG_USER && (registry->profile.features & USER_PROFILE_MULTITARGET))
count = registry::addTarget(registry, via, expire);
else
count = registry::setTarget(registry, via, expire);
Socket::getaddress(via->getAddr(), buffer, sizeof(buffer));
if(count)
service::errlog(service::DEBUG, "authorizing %s for %ld seconds from %s", identity, interval, buffer);
else {
service::errlog(service::ERROR, "cannot authorize %s from %s", identity, buffer);
answer = 403;
}
reply:
eXosip_lock();
eXosip_message_build_answer(sevent->tid, answer, &reply);
eXosip_message_send_answer(sevent->tid, answer, reply);
eXosip_unlock();
}
void thread::deregister()
{
service::errlog(service::DEBUG, "deauthorize %s", identity);
}
void thread::options(void)
{
osip_message_t *reply = NULL;
eXosip_lock();
if(eXosip_options_build_answer(sevent->tid, 200, &reply) == 0) {
osip_message_set_header(reply, "Accept", "application/sdp, text/plain");
osip_message_set_header(reply, "Allow", "INVITE,ACK,CANCEL,OPTIONS,INFO,REFER,MESSAGE,SUBSCRIBE,NOTIFY,REGISTER,PRACK");
eXosip_options_send_answer(sevent->tid, 200, reply);
}
eXosip_unlock();
}
void thread::shutdown(void)
{
shutdown_flag = true;
while(shutdown_count < startup_count)
Thread::sleep(50);
}
void thread::run(void)
{
static volatile time_t last = 0;
time_t now;
int cpos, vpos;
osip_via_t *hvia;
instance = ++startup_count;
service::errlog(service::DEBUG, "starting thread %d", instance);
for(;;) {
sevent = eXosip_event_wait(0, stack::sip.timing);
if(shutdown_flag) {
++shutdown_count;
service::errlog(service::DEBUG, "stopping thread %d", instance);
delete this;
pthread_exit(NULL);
}
if(!sevent)
{
time(&now);
if(now != current) {
current = now;
eXosip_lock();
if(now != last) {
eXosip_automatic_action();
last = current;
}
eXosip_unlock();
}
continue;
}
vpos = 0;
hvia = NULL;
while(sevent->request && osip_list_eol(OSIP2_LIST_PTR sevent->request->vias, vpos) == 0)
hvia = (osip_via_t *)osip_list_get(OSIP2_LIST_PTR sevent->request->vias, vpos++);
if(hvia) {
service::errlog(service::DEBUG, "sip: event %d; cid=%d, did=%d, instance=%d, via=%s:%s",
sevent->type, sevent->cid, sevent->did, instance, hvia->host, hvia->port);
via = new stack::address(hvia->host, hvia->port);
}
else
service::errlog(service::DEBUG, "sip: event %d; cid=%d, did=%d, instance=%d",
sevent->type, sevent->cid, sevent->did, instance);
switch(sevent->type) {
case EXOSIP_MESSAGE_NEW:
if(!sevent->request)
break;
if(MSG_IS_OPTIONS(sevent->request))
options();
else if(MSG_IS_REGISTER(sevent->request) && authenticate())
registration();
break;
default:
service::errlog(service::WARN, "unknown message");
}
if(via) {
delete via;
via = NULL;
}
if(registry) {
registry::release(registry);
registry = NULL;
}
if(config) {
config::release(config);
config = NULL;
}
eXosip_event_free(sevent);
}
}
END_NAMESPACE