blob: b61fa469ed20c78e4ed5cf24dbe9a207d3a1ddae [file] [log] [blame]
Benny Prijono21b9ad92006-08-15 13:11:22 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsip-ua/sip_regc.h>
20#include <pjsip/sip_endpoint.h>
21#include <pjsip/sip_parser.h>
22#include <pjsip/sip_module.h>
23#include <pjsip/sip_transaction.h>
24#include <pjsip/sip_event.h>
25#include <pjsip/sip_util.h>
26#include <pjsip/sip_auth_msg.h>
27#include <pjsip/sip_errno.h>
28#include <pj/assert.h>
29#include <pj/guid.h>
30#include <pj/os.h>
31#include <pj/pool.h>
32#include <pj/log.h>
33#include <pj/rand.h>
34#include <pj/string.h>
35
36
37#define REFRESH_TIMER 1
38#define DELAY_BEFORE_REFRESH 5
39#define THIS_FILE "sip_regc.c"
40
41/**
42 * SIP client registration structure.
43 */
44struct pjsip_regc
45{
46 pj_pool_t *pool;
47 pjsip_endpoint *endpt;
48 pj_bool_t _delete_flag;
49 int pending_tsx;
50
51 void *token;
52 pjsip_regc_cb *cb;
53
54 pj_str_t str_srv_url;
55 pjsip_uri *srv_url;
56 pjsip_cid_hdr *cid_hdr;
57 pjsip_cseq_hdr *cseq_hdr;
58 pj_str_t from_uri;
59 pjsip_from_hdr *from_hdr;
60 pjsip_to_hdr *to_hdr;
61 char *contact_buf;
62 pjsip_generic_string_hdr *contact_hdr;
63 pjsip_expires_hdr *expires_hdr;
64 pjsip_contact_hdr *unreg_contact_hdr;
65 pjsip_expires_hdr *unreg_expires_hdr;
66 pj_uint32_t expires;
67 pjsip_route_hdr route_set;
68
69 /* Authorization sessions. */
70 pjsip_auth_clt_sess auth_sess;
71
72 /* Auto refresh registration. */
73 pj_bool_t auto_reg;
74 pj_time_val last_reg;
75 pj_time_val next_reg;
76 pj_timer_entry timer;
77};
78
79
80
81PJ_DEF(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token,
82 pjsip_regc_cb *cb,
83 pjsip_regc **p_regc)
84{
85 pj_pool_t *pool;
86 pjsip_regc *regc;
87 pj_status_t status;
88
89 /* Verify arguments. */
90 PJ_ASSERT_RETURN(endpt && cb && p_regc, PJ_EINVAL);
91
92 pool = pjsip_endpt_create_pool(endpt, "regc%p", 1024, 1024);
93 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
94
95 regc = pj_pool_zalloc(pool, sizeof(struct pjsip_regc));
96
97 regc->pool = pool;
98 regc->endpt = endpt;
99 regc->token = token;
100 regc->cb = cb;
101 regc->contact_buf = pj_pool_alloc(pool, PJSIP_REGC_CONTACT_BUF_SIZE);
102 regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED;
103
104 status = pjsip_auth_clt_init(&regc->auth_sess, endpt, regc->pool, 0);
105 if (status != PJ_SUCCESS)
106 return status;
107
108 pj_list_init(&regc->route_set);
109
110 /* Done */
111 *p_regc = regc;
112 return PJ_SUCCESS;
113}
114
115
116PJ_DEF(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc)
117{
118 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
119
120 if (regc->pending_tsx) {
121 regc->_delete_flag = 1;
122 regc->cb = NULL;
123 } else {
124 pjsip_endpt_release_pool(regc->endpt, regc->pool);
125 }
126
127 return PJ_SUCCESS;
128}
129
130
131PJ_DEF(pj_status_t) pjsip_regc_get_info( pjsip_regc *regc,
132 pjsip_regc_info *info )
133{
134 PJ_ASSERT_RETURN(regc && info, PJ_EINVAL);
135
136 info->server_uri = regc->str_srv_url;
137 info->client_uri = regc->from_uri;
138 info->is_busy = (regc->pending_tsx != 0);
139 info->auto_reg = regc->auto_reg;
140 info->interval = regc->expires;
141
142 if (regc->pending_tsx)
143 info->next_reg = 0;
144 else if (regc->auto_reg == 0)
145 info->next_reg = 0;
146 else if (regc->expires < 0)
147 info->next_reg = regc->expires;
148 else {
149 pj_time_val now, next_reg;
150
151 next_reg = regc->next_reg;
152 pj_gettimeofday(&now);
153 PJ_TIME_VAL_SUB(next_reg, now);
154 info->next_reg = next_reg.sec;
155 }
156
157 return PJ_SUCCESS;
158}
159
160
161PJ_DEF(pj_pool_t*) pjsip_regc_get_pool(pjsip_regc *regc)
162{
163 return regc->pool;
164}
165
166static void set_expires( pjsip_regc *regc, pj_uint32_t expires)
167{
168 if (expires != regc->expires) {
169 regc->expires_hdr = pjsip_expires_hdr_create(regc->pool, expires);
170 } else {
171 regc->expires_hdr = NULL;
172 }
173}
174
175
176static pj_status_t set_contact( pjsip_regc *regc,
177 int contact_cnt,
178 const pj_str_t contact[] )
179{
180 int i;
181 char *s;
182 const pj_str_t contact_STR = { "Contact", 7};
183
184 /* Concatenate contacts. */
185 for (i=0, s=regc->contact_buf; i<contact_cnt; ++i) {
186 if ((s-regc->contact_buf) + contact[i].slen + 2 > PJSIP_REGC_CONTACT_BUF_SIZE) {
187 return PJSIP_EURITOOLONG;
188 }
189 pj_memcpy(s, contact[i].ptr, contact[i].slen);
190 s += contact[i].slen;
191
192 if (i != contact_cnt - 1) {
193 *s++ = ',';
194 *s++ = ' ';
195 }
196 }
197
198 /* Set "Contact" header. */
199 regc->contact_hdr = pjsip_generic_string_hdr_create(regc->pool,
200 &contact_STR,
201 NULL);
202 regc->contact_hdr->hvalue.ptr = regc->contact_buf;
203 regc->contact_hdr->hvalue.slen = (s - regc->contact_buf);
204
205 return PJ_SUCCESS;
206}
207
208
209PJ_DEF(pj_status_t) pjsip_regc_init( pjsip_regc *regc,
210 const pj_str_t *srv_url,
211 const pj_str_t *from_url,
212 const pj_str_t *to_url,
213 int contact_cnt,
214 const pj_str_t contact[],
215 pj_uint32_t expires)
216{
217 pj_str_t tmp;
218 pj_status_t status;
219
220 PJ_ASSERT_RETURN(regc && srv_url && from_url && to_url &&
221 contact_cnt && contact && expires, PJ_EINVAL);
222
223 /* Copy server URL. */
224 pj_strdup_with_null(regc->pool, &regc->str_srv_url, srv_url);
225
226 /* Set server URL. */
227 tmp = regc->str_srv_url;
228 regc->srv_url = pjsip_parse_uri( regc->pool, tmp.ptr, tmp.slen, 0);
229 if (regc->srv_url == NULL) {
230 return PJSIP_EINVALIDURI;
231 }
232
233 /* Set "From" header. */
234 pj_strdup_with_null(regc->pool, &regc->from_uri, from_url);
235 tmp = regc->from_uri;
236 regc->from_hdr = pjsip_from_hdr_create(regc->pool);
237 regc->from_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
238 PJSIP_PARSE_URI_AS_NAMEADDR);
239 if (!regc->from_hdr->uri) {
240 PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s",
241 from_url->slen, from_url->ptr));
242 return PJSIP_EINVALIDURI;
243 }
244
245 /* Set "To" header. */
246 pj_strdup_with_null(regc->pool, &tmp, to_url);
247 regc->to_hdr = pjsip_to_hdr_create(regc->pool);
248 regc->to_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
249 PJSIP_PARSE_URI_AS_NAMEADDR);
250 if (!regc->to_hdr->uri) {
251 PJ_LOG(4,(THIS_FILE, "regc: invalid target URI %.*s", to_url->slen, to_url->ptr));
252 return PJSIP_EINVALIDURI;
253 }
254
255
256 /* Set "Contact" header. */
257 status = set_contact( regc, contact_cnt, contact);
258 if (status != PJ_SUCCESS)
259 return status;
260
261 /* Set "Expires" header, if required. */
262 set_expires( regc, expires);
263
264 /* Set "Call-ID" header. */
265 regc->cid_hdr = pjsip_cid_hdr_create(regc->pool);
266 pj_create_unique_string(regc->pool, &regc->cid_hdr->id);
267
268 /* Set "CSeq" header. */
269 regc->cseq_hdr = pjsip_cseq_hdr_create(regc->pool);
270 regc->cseq_hdr->cseq = pj_rand() % 0xFFFF;
271 pjsip_method_set( &regc->cseq_hdr->method, PJSIP_REGISTER_METHOD);
272
273 /* Create "Contact" header used in unregistration. */
274 regc->unreg_contact_hdr = pjsip_contact_hdr_create(regc->pool);
275 regc->unreg_contact_hdr->star = 1;
276
277 /* Create "Expires" header used in unregistration. */
278 regc->unreg_expires_hdr = pjsip_expires_hdr_create( regc->pool, 0);
279
280 /* Done. */
281 return PJ_SUCCESS;
282}
283
284PJ_DEF(pj_status_t) pjsip_regc_set_credentials( pjsip_regc *regc,
285 int count,
286 const pjsip_cred_info cred[] )
287{
288 PJ_ASSERT_RETURN(regc && count && cred, PJ_EINVAL);
289 return pjsip_auth_clt_set_credentials(&regc->auth_sess, count, cred);
290}
291
292PJ_DEF(pj_status_t) pjsip_regc_set_route_set( pjsip_regc *regc,
293 const pjsip_route_hdr *route_set)
294{
295 const pjsip_route_hdr *chdr;
296
297 PJ_ASSERT_RETURN(regc && route_set, PJ_EINVAL);
298
299 pj_list_init(&regc->route_set);
300
301 chdr = route_set->next;
302 while (chdr != route_set) {
303 pj_list_push_back(&regc->route_set, pjsip_hdr_clone(regc->pool, chdr));
304 chdr = chdr->next;
305 }
306
307 return PJ_SUCCESS;
308}
309
310static pj_status_t create_request(pjsip_regc *regc,
311 pjsip_tx_data **p_tdata)
312{
313 pj_status_t status;
314 pjsip_tx_data *tdata;
315
316 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
317
318 /* Create the request. */
319 status = pjsip_endpt_create_request_from_hdr( regc->endpt,
320 &pjsip_register_method,
321 regc->srv_url,
322 regc->from_hdr,
323 regc->to_hdr,
324 NULL,
325 regc->cid_hdr,
326 regc->cseq_hdr->cseq,
327 NULL,
328 &tdata);
329 if (status != PJ_SUCCESS)
330 return status;
331
332 /* Add cached authorization headers. */
333 pjsip_auth_clt_init_req( &regc->auth_sess, tdata );
334
335 /* Add Route headers from route set, ideally after Via header */
336 if (!pj_list_empty(&regc->route_set)) {
337 pjsip_hdr *route_pos;
338 const pjsip_route_hdr *route;
339
340 route_pos = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
341 if (!route_pos)
342 route_pos = &tdata->msg->hdr;
343
344 route = regc->route_set.next;
345 while (route != &regc->route_set) {
346 pjsip_hdr *new_hdr = pjsip_hdr_shallow_clone(tdata->pool, route);
347 pj_list_insert_after(route_pos, new_hdr);
348 route_pos = new_hdr;
349 route = route->next;
350 }
351 }
352
353 /* Done. */
354 *p_tdata = tdata;
355 return PJ_SUCCESS;
356}
357
358
359PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg,
360 pjsip_tx_data **p_tdata)
361{
362 pjsip_msg *msg;
363 pj_status_t status;
364 pjsip_tx_data *tdata;
365
366 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
367
368 status = create_request(regc, &tdata);
369 if (status != PJ_SUCCESS)
370 return status;
371
372 /* Add Contact header. */
373 msg = tdata->msg;
374 pjsip_msg_add_hdr(msg, (pjsip_hdr*) regc->contact_hdr);
375 if (regc->expires_hdr)
376 pjsip_msg_add_hdr(msg, (pjsip_hdr*) regc->expires_hdr);
377
378 if (regc->timer.id != 0) {
379 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
380 regc->timer.id = 0;
381 }
382
383 regc->auto_reg = autoreg;
384
385 /* Done */
386 *p_tdata = tdata;
387 return PJ_SUCCESS;
388}
389
390
391PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc,
392 pjsip_tx_data **p_tdata)
393{
394 pjsip_tx_data *tdata;
395 pjsip_msg *msg;
396 pj_status_t status;
397
398 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
399
400 if (regc->timer.id != 0) {
401 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
402 regc->timer.id = 0;
403 }
404
405 status = create_request(regc, &tdata);
406 if (status != PJ_SUCCESS)
407 return status;
408
409 msg = tdata->msg;
410 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_contact_hdr);
411 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr);
412
413 *p_tdata = tdata;
414 return PJ_SUCCESS;
415}
416
417
418PJ_DEF(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc,
419 int contact_cnt,
420 const pj_str_t contact[] )
421{
422 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
423 return set_contact( regc, contact_cnt, contact );
424}
425
426
427PJ_DEF(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc,
428 pj_uint32_t expires )
429{
430 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
431 set_expires( regc, expires );
432 return PJ_SUCCESS;
433}
434
435
436static void call_callback(pjsip_regc *regc, pj_status_t status, int st_code,
437 const pj_str_t *reason,
438 pjsip_rx_data *rdata, pj_int32_t expiration,
439 int contact_cnt, pjsip_contact_hdr *contact[])
440{
441 struct pjsip_regc_cbparam cbparam;
442
443
444 cbparam.regc = regc;
445 cbparam.token = regc->token;
446 cbparam.status = status;
447 cbparam.code = st_code;
448 cbparam.reason = *reason;
449 cbparam.rdata = rdata;
450 cbparam.contact_cnt = contact_cnt;
451 cbparam.expiration = expiration;
452 if (contact_cnt) {
453 pj_memcpy( cbparam.contact, contact,
454 contact_cnt*sizeof(pjsip_contact_hdr*));
455 }
456
457 (*regc->cb)(&cbparam);
458}
459
460static void regc_refresh_timer_cb( pj_timer_heap_t *timer_heap,
461 struct pj_timer_entry *entry)
462{
463 pjsip_regc *regc = entry->user_data;
464 pjsip_tx_data *tdata;
465 pj_status_t status;
466
467 PJ_UNUSED_ARG(timer_heap);
468
469 entry->id = 0;
470 status = pjsip_regc_register(regc, 1, &tdata);
471 if (status == PJ_SUCCESS) {
472 status = pjsip_regc_send(regc, tdata);
473 }
474
475 if (status != PJ_SUCCESS) {
476 char errmsg[PJ_ERR_MSG_SIZE];
477 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg));
478 call_callback(regc, status, 400, &reason, NULL, -1, 0, NULL);
479 }
480}
481
482static void tsx_callback(void *token, pjsip_event *event)
483{
484 pj_status_t status;
485 pjsip_regc *regc = token;
486 pjsip_transaction *tsx = event->body.tsx_state.tsx;
487
488 /* Decrement pending transaction counter. */
489 pj_assert(regc->pending_tsx > 0);
490 --regc->pending_tsx;
491
492 /* If registration data has been deleted by user then remove registration
493 * data from transaction's callback, and don't call callback.
494 */
495 if (regc->_delete_flag) {
496
497 /* Nothing to do */
498 ;
499
500 } else if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED ||
501 tsx->status_code == PJSIP_SC_UNAUTHORIZED)
502 {
503 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
504 pjsip_tx_data *tdata;
505
506 status = pjsip_auth_clt_reinit_req( &regc->auth_sess,
507 rdata,
508 tsx->last_tx,
509 &tdata);
510
511 if (status == PJ_SUCCESS) {
512 status = pjsip_regc_send(regc, tdata);
513 }
514
515 if (status != PJ_SUCCESS) {
516 call_callback(regc, status, tsx->status_code,
517 &rdata->msg_info.msg->line.status.reason,
518 rdata, -1, 0, NULL);
519 }
520
521 return;
522
523 } else {
524 int contact_cnt = 0;
525 pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT];
526 pjsip_rx_data *rdata;
527 pj_int32_t expiration = 0xFFFF;
528
529 if (tsx->status_code/100 == 2) {
530 int i;
531 pjsip_contact_hdr *hdr;
532 pjsip_msg *msg;
533 pjsip_expires_hdr *expires;
534
535 rdata = event->body.tsx_state.src.rdata;
536 msg = rdata->msg_info.msg;
537 hdr = pjsip_msg_find_hdr( msg, PJSIP_H_CONTACT, NULL);
538 while (hdr) {
539 contact[contact_cnt++] = hdr;
540 hdr = hdr->next;
541 if (hdr == (void*)&msg->hdr)
542 break;
543 hdr = pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, hdr);
544 }
545
546 expires = pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
547
548 if (expires)
549 expiration = expires->ivalue;
550
551 for (i=0; i<contact_cnt; ++i) {
552 hdr = contact[i];
553 if (hdr->expires >= 0 && hdr->expires < expiration)
554 expiration = contact[i]->expires;
555 }
556
557 if (regc->auto_reg && expiration != 0 && expiration != 0xFFFF) {
558 pj_time_val delay = { 0, 0};
559
560 delay.sec = expiration - DELAY_BEFORE_REFRESH;
561 if (regc->expires != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED &&
562 delay.sec > (pj_int32_t)regc->expires)
563 {
564 delay.sec = regc->expires;
565 }
566 if (delay.sec < DELAY_BEFORE_REFRESH)
567 delay.sec = DELAY_BEFORE_REFRESH;
568 regc->timer.cb = &regc_refresh_timer_cb;
569 regc->timer.id = REFRESH_TIMER;
570 regc->timer.user_data = regc;
571 pjsip_endpt_schedule_timer( regc->endpt, &regc->timer, &delay);
572 pj_gettimeofday(&regc->last_reg);
573 regc->next_reg = regc->last_reg;
574 regc->next_reg.sec += delay.sec;
575 }
576
577 } else {
578 rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ?
579 event->body.tsx_state.src.rdata : NULL;
580 }
581
582
583 /* Call callback. */
584 if (expiration == 0xFFFF) expiration = -1;
585 call_callback(regc, PJ_SUCCESS, tsx->status_code,
586 (rdata ? &rdata->msg_info.msg->line.status.reason
587 : pjsip_get_status_text(tsx->status_code)),
588 rdata, expiration,
589 contact_cnt, contact);
590
591 }
592
593 /* Delete the record if user destroy regc during the callback. */
594 if (regc->_delete_flag && regc->pending_tsx==0) {
595 pjsip_regc_destroy(regc);
596 }
597}
598
599PJ_DEF(pj_status_t) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata)
600{
601 pj_status_t status;
602 pjsip_cseq_hdr *cseq_hdr;
603 pj_uint32_t cseq;
604
605 /* Make sure we don't have pending transaction. */
606 if (regc->pending_tsx) {
607 PJ_LOG(4,(THIS_FILE, "Unable to send request, regc has another "
608 "transaction pending"));
609 pjsip_tx_data_dec_ref( tdata );
610 return PJSIP_EBUSY;
611 }
612
613 /* Invalidate message buffer. */
614 pjsip_tx_data_invalidate_msg(tdata);
615
616 /* Increment CSeq */
617 cseq = ++regc->cseq_hdr->cseq;
618 cseq_hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
619 cseq_hdr->cseq = cseq;
620
621 /* Increment pending transaction first, since transaction callback
622 * may be called even before send_request() returns!
623 */
624 ++regc->pending_tsx;
625 status = pjsip_endpt_send_request(regc->endpt, tdata, -1, regc, &tsx_callback);
626 if (status!=PJ_SUCCESS) {
627 --regc->pending_tsx;
628 PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status));
629 }
630
631 return status;
632}
633
634