-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathreg.c
485 lines (387 loc) · 9.47 KB
/
reg.c
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/**
* @file reg.c SIP Registration
*
* Copyright (C) 2010 Creytiv.com
*/
#include <re_types.h>
#include <re_mem.h>
#include <re_mbuf.h>
#include <re_sa.h>
#include <re_list.h>
#include <re_hash.h>
#include <re_fmt.h>
#include <re_uri.h>
#include <re_sys.h>
#include <re_tmr.h>
#include <re_msg.h>
#include <re_sip.h>
#include <re_sipreg.h>
enum {
DEFAULT_EXPIRES = 3600,
};
/** Defines a SIP Registration client */
struct sipreg {
struct sip_loopstate ls;
struct sa laddr;
struct tmr tmr;
struct sip *sip;
struct sip_keepalive *ka;
struct sip_request *req;
struct sip_dialog *dlg;
struct sip_auth *auth;
struct mbuf *hdrs;
char *cuser;
sip_resp_h *resph;
void *arg;
uint32_t expires;
uint32_t pexpires;
uint32_t failc;
uint32_t rwait;
uint32_t wait;
uint32_t fbregint;
enum sip_transp tp;
bool registered;
bool terminated;
char *params;
int regid;
};
static int request(struct sipreg *reg, bool reset_ls);
static void dummy_handler(int err, const struct sip_msg *msg, void *arg)
{
(void)err;
(void)msg;
(void)arg;
}
static void destructor(void *arg)
{
struct sipreg *reg = arg;
tmr_cancel(®->tmr);
if (!reg->terminated) {
reg->resph = dummy_handler;
reg->terminated = true;
if (reg->req) {
mem_ref(reg);
return;
}
if (reg->registered && !request(reg, true)) {
mem_ref(reg);
return;
}
}
mem_deref(reg->ka);
mem_deref(reg->dlg);
mem_deref(reg->auth);
mem_deref(reg->cuser);
mem_deref(reg->sip);
mem_deref(reg->hdrs);
mem_deref(reg->params);
}
static uint32_t failwait(uint32_t failc)
{
return min(1800, (30 * (1<<min(failc, 6)))) * (500 + rand_u16() % 501);
}
static void tmr_handler(void *arg)
{
struct sipreg *reg = arg;
int err;
err = request(reg, true);
if (err) {
tmr_start(®->tmr, failwait(++reg->failc), tmr_handler, reg);
reg->resph(err, NULL, reg->arg);
}
}
static void keepalive_handler(int err, void *arg)
{
struct sipreg *reg = arg;
/* failure will be handled in response handler */
if (reg->req || reg->terminated)
return;
tmr_start(®->tmr, failwait(++reg->failc), tmr_handler, reg);
reg->resph(err, NULL, reg->arg);
}
static void start_outbound(struct sipreg *reg, const struct sip_msg *msg)
{
const struct sip_hdr *flowtimer;
if (!sip_msg_hdr_has_value(msg, SIP_HDR_REQUIRE, "outbound"))
return;
flowtimer = sip_msg_hdr(msg, SIP_HDR_FLOW_TIMER);
(void)sip_keepalive_start(®->ka, reg->sip, msg,
flowtimer ? pl_u32(&flowtimer->val) : 0,
keepalive_handler, reg);
}
static bool contact_handler(const struct sip_hdr *hdr,
const struct sip_msg *msg, void *arg)
{
struct sipreg *reg = arg;
struct sip_addr c;
struct pl pval;
char uri[256];
if (sip_addr_decode(&c, &hdr->val))
return false;
if (re_snprintf(uri, sizeof(uri), "sip:%s@%J%s", reg->cuser,
®->laddr, sip_transp_param(reg->tp)) < 0)
return false;
if (pl_strcmp(&c.auri, uri))
return false;
if (!msg_param_decode(&c.params, "expires", &pval)) {
reg->wait = pl_u32(&pval);
}
else if (pl_isset(&msg->expires))
reg->wait = pl_u32(&msg->expires);
else
reg->wait = DEFAULT_EXPIRES;
return true;
}
static void response_handler(int err, const struct sip_msg *msg, void *arg)
{
const struct sip_hdr *minexp;
struct sipreg *reg = arg;
reg->wait = failwait(reg->failc + 1);
if (err || !msg || sip_request_loops(®->ls, msg->scode)) {
reg->failc++;
goto out;
}
if (msg->scode < 200) {
return;
}
else if (msg->scode < 300) {
reg->wait = reg->expires;
sip_msg_hdr_apply(msg, true, SIP_HDR_CONTACT, contact_handler,
reg);
reg->registered = reg->wait > 0;
reg->pexpires = reg->wait;
reg->wait *= reg->rwait * (1000 / 100);
reg->failc = 0;
if (reg->regid > 0 && !reg->terminated && !reg->ka)
start_outbound(reg, msg);
}
else {
if (reg->terminated && !reg->registered)
goto out;
switch (msg->scode) {
case 401:
case 407:
sip_auth_reset(reg->auth);
err = sip_auth_authenticate(reg->auth, msg);
if (err) {
err = (err == EAUTH) ? 0 : err;
break;
}
err = request(reg, false);
if (err)
break;
return;
case 403:
sip_auth_reset(reg->auth);
break;
case 423:
minexp = sip_msg_hdr(msg, SIP_HDR_MIN_EXPIRES);
if (!minexp || !pl_u32(&minexp->val) || !reg->expires)
break;
reg->expires = pl_u32(&minexp->val);
err = request(reg, false);
if (err)
break;
return;
}
++reg->failc;
}
out:
if (!reg->expires) {
if (msg && msg->scode >= 400 && msg->scode < 500)
reg->fbregint = 0;
if (!reg->terminated && reg->fbregint) {
tmr_start(®->tmr, reg->fbregint * 1000, tmr_handler,
reg);
reg->resph(err, msg, reg->arg);
}
else if (reg->terminated) {
mem_deref(reg);
}
}
else if (reg->terminated) {
if (!reg->registered || request(reg, true))
mem_deref(reg);
}
else {
tmr_start(®->tmr, reg->wait, tmr_handler, reg);
reg->resph(err, msg, reg->arg);
}
}
static int send_handler(enum sip_transp tp, const struct sa *src,
const struct sa *dst, struct mbuf *mb, void *arg)
{
struct sipreg *reg = arg;
int err;
(void)dst;
reg->laddr = *src;
reg->tp = tp;
err = mbuf_printf(mb, "Contact: <sip:%s@%J%s>;expires=%u%s%s",
reg->cuser, ®->laddr, sip_transp_param(reg->tp),
reg->expires,
reg->params ? ";" : "",
reg->params ? reg->params : "");
if (reg->regid > 0)
err |= mbuf_printf(mb, ";reg-id=%d", reg->regid);
err |= mbuf_printf(mb, "\r\n");
return err;
}
static int request(struct sipreg *reg, bool reset_ls)
{
if (reg->terminated)
reg->expires = 0;
if (reset_ls) {
sip_loopstate_reset(®->ls);
}
return sip_drequestf(®->req, reg->sip, true, "REGISTER", reg->dlg,
0, reg->auth, send_handler, response_handler, reg,
"%s"
"%b"
"Content-Length: 0\r\n"
"\r\n",
reg->regid > 0
? "Supported: gruu, outbound, path\r\n" : "",
reg->hdrs ? mbuf_buf(reg->hdrs) : NULL,
reg->hdrs ? mbuf_get_left(reg->hdrs) : (size_t)0);
}
/**
* Allocate a SIP Registration client
*
* @param regp Pointer to allocated SIP Registration client
* @param sip SIP Stack instance
* @param reg_uri SIP Request URI
* @param to_uri SIP To-header URI
* @param from_name SIP From-header display name (optional)
* @param from_uri SIP From-header URI
* @param expires Registration expiry time in [seconds]
* @param cuser Contact username
* @param routev Optional route vector
* @param routec Number of routes
* @param regid Register identification
* @param authh Authentication handler
* @param aarg Authentication handler argument
* @param aref True to ref argument
* @param resph Response handler
* @param arg Response handler argument
* @param params Optional Contact-header parameters
* @param fmt Formatted strings with extra SIP Headers
*
* @return 0 if success, otherwise errorcode
*/
int sipreg_register(struct sipreg **regp, struct sip *sip, const char *reg_uri,
const char *to_uri, const char *from_name,
const char *from_uri, uint32_t expires,
const char *cuser, const char *routev[], uint32_t routec,
int regid, sip_auth_h *authh, void *aarg, bool aref,
sip_resp_h *resph, void *arg,
const char *params, const char *fmt, ...)
{
struct sipreg *reg;
int err;
if (!regp || !sip || !reg_uri || !to_uri || !from_uri || !cuser)
return EINVAL;
reg = mem_zalloc(sizeof(*reg), destructor);
if (!reg)
return ENOMEM;
err = sip_dialog_alloc(®->dlg, reg_uri, to_uri, from_name, from_uri,
routev, routec);
if (err)
goto out;
err = sip_auth_alloc(®->auth, authh, aarg, aref);
if (err)
goto out;
err = str_dup(®->cuser, cuser);
if (params)
err |= str_dup(®->params, params);
if (err)
goto out;
/* Custom SIP headers */
if (fmt) {
va_list ap;
reg->hdrs = mbuf_alloc(256);
if (!reg->hdrs) {
err = ENOMEM;
goto out;
}
va_start(ap, fmt);
err = mbuf_vprintf(reg->hdrs, fmt, ap);
reg->hdrs->pos = 0;
va_end(ap);
if (err)
goto out;
}
reg->sip = mem_ref(sip);
reg->expires = expires;
reg->rwait = 90;
reg->resph = resph ? resph : dummy_handler;
reg->arg = arg;
reg->regid = regid;
err = request(reg, true);
if (err)
goto out;
out:
if (err)
mem_deref(reg);
else
*regp = reg;
return err;
}
/**
* Set the relative registration interval in percent from proxy expiry time. A
* value from 5-95% is accepted.
*
* @param reg SIP Registration client
* @param rwait The relative registration interval in [%].
*
* @return 0 if success, otherwise errorcode
*/
int sipreg_set_rwait(struct sipreg *reg, uint32_t rwait)
{
if (!reg || rwait < 5 || rwait > 95)
return EINVAL;
reg->rwait = rwait;
return 0;
}
/**
* Get the local socket address for a SIP Registration client
*
* @param reg SIP Registration client
*
* @return Local socket address
*/
const struct sa *sipreg_laddr(const struct sipreg *reg)
{
return reg ? ®->laddr : NULL;
}
/**
* Get the proxy expires value of a SIP registration client
*
* @param reg SIP registration client
*
* @return the proxy expires value
*/
uint32_t sipreg_proxy_expires(const struct sipreg *reg)
{
return reg ? reg->pexpires : 0;
}
bool sipreg_registered(const struct sipreg *reg)
{
return reg ? reg->registered : false;
}
bool sipreg_failed(const struct sipreg *reg)
{
return reg ? reg->failc > 0 : false;
}
void sipreg_incfailc(struct sipreg *reg)
{
if (!reg)
return;
reg->failc++;
}
int sipreg_set_fbregint(struct sipreg *reg, uint32_t fbregint)
{
if (!reg)
return EINVAL;
reg->fbregint = fbregint;
return 0;
}