blob: 7e061537542b14658b506e33bfcdf53f1e4cbab6 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
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 */
Benny Prijonoccf95622006-02-07 18:48:01 +000019#include <pjsip-ua/sip_regc.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000020#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>
Benny Prijonoccf95622006-02-07 18:48:01 +000027#include <pjsip/sip_errno.h>
Benny Prijonoccf95622006-02-07 18:48:01 +000028#include <pj/assert.h>
Benny Prijonobcaed6c2006-02-19 15:37:19 +000029#include <pj/guid.h>
30#include <pj/os.h>
31#include <pj/pool.h>
32#include <pj/log.h>
Benny Prijono59ca70f2006-02-22 22:18:58 +000033#include <pj/rand.h>
Benny Prijonobcaed6c2006-02-19 15:37:19 +000034#include <pj/string.h>
Benny Prijonoccf95622006-02-07 18:48:01 +000035
Benny Prijono5dcb38d2005-11-21 01:55:47 +000036
37#define REFRESH_TIMER 1
38#define DELAY_BEFORE_REFRESH 5
Benny Prijono315999b2007-09-30 10:58:36 +000039#define THIS_FILE "sip_reg.c"
Benny Prijono5dcb38d2005-11-21 01:55:47 +000040
Benny Prijono0f35f912007-02-05 18:59:31 +000041/* Outgoing transaction timeout when server sends 100 but never replies
42 * with final response. Value is in MILISECONDS!
43 */
44#define REGC_TSX_TIMEOUT 33000
45
46
Benny Prijono5dcb38d2005-11-21 01:55:47 +000047/**
48 * SIP client registration structure.
49 */
50struct pjsip_regc
51{
Benny Prijono84126ab2006-02-09 09:30:09 +000052 pj_pool_t *pool;
53 pjsip_endpoint *endpt;
54 pj_bool_t _delete_flag;
Benny Prijono25a86c72006-12-01 20:50:01 +000055 pj_bool_t has_tsx;
56 pj_int32_t busy;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000057
Benny Prijono84126ab2006-02-09 09:30:09 +000058 void *token;
59 pjsip_regc_cb *cb;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000060
Benny Prijono84126ab2006-02-09 09:30:09 +000061 pj_str_t str_srv_url;
62 pjsip_uri *srv_url;
63 pjsip_cid_hdr *cid_hdr;
64 pjsip_cseq_hdr *cseq_hdr;
Benny Prijonobcaed6c2006-02-19 15:37:19 +000065 pj_str_t from_uri;
Benny Prijono84126ab2006-02-09 09:30:09 +000066 pjsip_from_hdr *from_hdr;
67 pjsip_to_hdr *to_hdr;
Benny Prijonoe56b5b12007-09-11 09:04:51 +000068 pjsip_hdr contact_hdr_list;
Benny Prijono84126ab2006-02-09 09:30:09 +000069 pjsip_expires_hdr *expires_hdr;
70 pjsip_contact_hdr *unreg_contact_hdr;
71 pjsip_expires_hdr *unreg_expires_hdr;
72 pj_uint32_t expires;
73 pjsip_route_hdr route_set;
Benny Prijono8fc6de02006-11-11 21:25:55 +000074 pjsip_hdr hdr_list;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000075
Benny Prijono5dcb38d2005-11-21 01:55:47 +000076 /* Authorization sessions. */
Benny Prijono84126ab2006-02-09 09:30:09 +000077 pjsip_auth_clt_sess auth_sess;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000078
79 /* Auto refresh registration. */
Benny Prijono84126ab2006-02-09 09:30:09 +000080 pj_bool_t auto_reg;
Benny Prijonobcaed6c2006-02-19 15:37:19 +000081 pj_time_val last_reg;
82 pj_time_val next_reg;
Benny Prijono84126ab2006-02-09 09:30:09 +000083 pj_timer_entry timer;
Benny Prijono720d0a82007-01-12 06:37:35 +000084
85 /* Transport selector */
86 pjsip_tpselector tp_sel;
Benny Prijonobdc093f2007-10-04 09:48:25 +000087
88 /* Last transport used. We acquire the transport to keep
89 * it open.
90 */
91 pjsip_transport *last_transport;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000092};
93
94
95
Benny Prijonoccf95622006-02-07 18:48:01 +000096PJ_DEF(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token,
97 pjsip_regc_cb *cb,
98 pjsip_regc **p_regc)
Benny Prijono5dcb38d2005-11-21 01:55:47 +000099{
100 pj_pool_t *pool;
101 pjsip_regc *regc;
Benny Prijonoccf95622006-02-07 18:48:01 +0000102 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000103
Benny Prijonoccf95622006-02-07 18:48:01 +0000104 /* Verify arguments. */
105 PJ_ASSERT_RETURN(endpt && cb && p_regc, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000106
107 pool = pjsip_endpt_create_pool(endpt, "regc%p", 1024, 1024);
Benny Prijonoccf95622006-02-07 18:48:01 +0000108 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
109
Benny Prijonoa1e69682007-05-11 15:14:34 +0000110 regc = PJ_POOL_ZALLOC_T(pool, pjsip_regc);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000111
112 regc->pool = pool;
113 regc->endpt = endpt;
114 regc->token = token;
115 regc->cb = cb;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000116 regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED;
117
Benny Prijonoccf95622006-02-07 18:48:01 +0000118 status = pjsip_auth_clt_init(&regc->auth_sess, endpt, regc->pool, 0);
119 if (status != PJ_SUCCESS)
120 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000121
Benny Prijono84126ab2006-02-09 09:30:09 +0000122 pj_list_init(&regc->route_set);
Benny Prijono8fc6de02006-11-11 21:25:55 +0000123 pj_list_init(&regc->hdr_list);
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000124 pj_list_init(&regc->contact_hdr_list);
Benny Prijono84126ab2006-02-09 09:30:09 +0000125
Benny Prijonoccf95622006-02-07 18:48:01 +0000126 /* Done */
127 *p_regc = regc;
128 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000129}
130
131
Benny Prijonoccf95622006-02-07 18:48:01 +0000132PJ_DEF(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000133{
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000134 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
135
Benny Prijono25a86c72006-12-01 20:50:01 +0000136 if (regc->has_tsx || regc->busy) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000137 regc->_delete_flag = 1;
138 regc->cb = NULL;
139 } else {
Benny Prijono720d0a82007-01-12 06:37:35 +0000140 pjsip_tpselector_dec_ref(&regc->tp_sel);
Benny Prijonobdc093f2007-10-04 09:48:25 +0000141 if (regc->last_transport) {
142 pjsip_transport_dec_ref(regc->last_transport);
143 regc->last_transport = NULL;
144 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000145 pjsip_endpt_release_pool(regc->endpt, regc->pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000146 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000147
148 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000149}
150
151
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000152PJ_DEF(pj_status_t) pjsip_regc_get_info( pjsip_regc *regc,
153 pjsip_regc_info *info )
154{
155 PJ_ASSERT_RETURN(regc && info, PJ_EINVAL);
156
157 info->server_uri = regc->str_srv_url;
158 info->client_uri = regc->from_uri;
Benny Prijono25a86c72006-12-01 20:50:01 +0000159 info->is_busy = (regc->busy || regc->has_tsx);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000160 info->auto_reg = regc->auto_reg;
161 info->interval = regc->expires;
162
Benny Prijono25a86c72006-12-01 20:50:01 +0000163 if (regc->has_tsx)
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000164 info->next_reg = 0;
165 else if (regc->auto_reg == 0)
166 info->next_reg = 0;
167 else if (regc->expires < 0)
168 info->next_reg = regc->expires;
169 else {
170 pj_time_val now, next_reg;
171
172 next_reg = regc->next_reg;
173 pj_gettimeofday(&now);
174 PJ_TIME_VAL_SUB(next_reg, now);
175 info->next_reg = next_reg.sec;
176 }
177
178 return PJ_SUCCESS;
179}
180
181
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000182PJ_DEF(pj_pool_t*) pjsip_regc_get_pool(pjsip_regc *regc)
183{
184 return regc->pool;
185}
186
187static void set_expires( pjsip_regc *regc, pj_uint32_t expires)
188{
189 if (expires != regc->expires) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000190 regc->expires_hdr = pjsip_expires_hdr_create(regc->pool, expires);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000191 } else {
192 regc->expires_hdr = NULL;
193 }
194}
195
196
197static pj_status_t set_contact( pjsip_regc *regc,
198 int contact_cnt,
199 const pj_str_t contact[] )
200{
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000201 const pj_str_t CONTACT = { "Contact", 7 };
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000202 int i;
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000203
204 /* Clear existing contacts */
205 pj_list_init(&regc->contact_hdr_list);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000206
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000207 for (i=0; i<contact_cnt; ++i) {
208 pjsip_hdr *hdr;
209 pj_str_t tmp;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000210
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000211 pj_strdup_with_null(regc->pool, &tmp, &contact[i]);
Benny Prijono91a5a3a2007-09-24 21:16:48 +0000212 hdr = (pjsip_hdr*)
213 pjsip_parse_hdr(regc->pool, &CONTACT, tmp.ptr, tmp.slen, NULL);
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000214 if (hdr == NULL) {
215 PJ_LOG(4,(THIS_FILE, "Invalid Contact URI: \"%.*s\"",
216 (int)tmp.slen, tmp.ptr));
217 return PJSIP_EINVALIDURI;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000218 }
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000219
220 pj_list_push_back(&regc->contact_hdr_list, hdr);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000221 }
222
Benny Prijonoccf95622006-02-07 18:48:01 +0000223 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000224}
225
226
227PJ_DEF(pj_status_t) pjsip_regc_init( pjsip_regc *regc,
228 const pj_str_t *srv_url,
229 const pj_str_t *from_url,
230 const pj_str_t *to_url,
231 int contact_cnt,
232 const pj_str_t contact[],
233 pj_uint32_t expires)
234{
235 pj_str_t tmp;
Benny Prijonoccf95622006-02-07 18:48:01 +0000236 pj_status_t status;
237
238 PJ_ASSERT_RETURN(regc && srv_url && from_url && to_url &&
239 contact_cnt && contact && expires, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000240
241 /* Copy server URL. */
242 pj_strdup_with_null(regc->pool, &regc->str_srv_url, srv_url);
243
244 /* Set server URL. */
245 tmp = regc->str_srv_url;
246 regc->srv_url = pjsip_parse_uri( regc->pool, tmp.ptr, tmp.slen, 0);
247 if (regc->srv_url == NULL) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000248 return PJSIP_EINVALIDURI;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000249 }
250
251 /* Set "From" header. */
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000252 pj_strdup_with_null(regc->pool, &regc->from_uri, from_url);
253 tmp = regc->from_uri;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000254 regc->from_hdr = pjsip_from_hdr_create(regc->pool);
255 regc->from_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
256 PJSIP_PARSE_URI_AS_NAMEADDR);
257 if (!regc->from_hdr->uri) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000258 PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s",
259 from_url->slen, from_url->ptr));
260 return PJSIP_EINVALIDURI;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000261 }
262
263 /* Set "To" header. */
264 pj_strdup_with_null(regc->pool, &tmp, to_url);
265 regc->to_hdr = pjsip_to_hdr_create(regc->pool);
266 regc->to_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
267 PJSIP_PARSE_URI_AS_NAMEADDR);
268 if (!regc->to_hdr->uri) {
269 PJ_LOG(4,(THIS_FILE, "regc: invalid target URI %.*s", to_url->slen, to_url->ptr));
Benny Prijonoccf95622006-02-07 18:48:01 +0000270 return PJSIP_EINVALIDURI;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000271 }
272
273
274 /* Set "Contact" header. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000275 status = set_contact( regc, contact_cnt, contact);
276 if (status != PJ_SUCCESS)
277 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000278
279 /* Set "Expires" header, if required. */
280 set_expires( regc, expires);
281
282 /* Set "Call-ID" header. */
283 regc->cid_hdr = pjsip_cid_hdr_create(regc->pool);
284 pj_create_unique_string(regc->pool, &regc->cid_hdr->id);
285
286 /* Set "CSeq" header. */
287 regc->cseq_hdr = pjsip_cseq_hdr_create(regc->pool);
Benny Prijono59ca70f2006-02-22 22:18:58 +0000288 regc->cseq_hdr->cseq = pj_rand() % 0xFFFF;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000289 pjsip_method_set( &regc->cseq_hdr->method, PJSIP_REGISTER_METHOD);
290
291 /* Create "Contact" header used in unregistration. */
292 regc->unreg_contact_hdr = pjsip_contact_hdr_create(regc->pool);
293 regc->unreg_contact_hdr->star = 1;
294
295 /* Create "Expires" header used in unregistration. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000296 regc->unreg_expires_hdr = pjsip_expires_hdr_create( regc->pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000297
298 /* Done. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000299 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000300}
301
302PJ_DEF(pj_status_t) pjsip_regc_set_credentials( pjsip_regc *regc,
303 int count,
304 const pjsip_cred_info cred[] )
305{
Benny Prijonoccf95622006-02-07 18:48:01 +0000306 PJ_ASSERT_RETURN(regc && count && cred, PJ_EINVAL);
307 return pjsip_auth_clt_set_credentials(&regc->auth_sess, count, cred);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000308}
309
Benny Prijono84126ab2006-02-09 09:30:09 +0000310PJ_DEF(pj_status_t) pjsip_regc_set_route_set( pjsip_regc *regc,
311 const pjsip_route_hdr *route_set)
312{
313 const pjsip_route_hdr *chdr;
314
315 PJ_ASSERT_RETURN(regc && route_set, PJ_EINVAL);
316
317 pj_list_init(&regc->route_set);
318
319 chdr = route_set->next;
320 while (chdr != route_set) {
321 pj_list_push_back(&regc->route_set, pjsip_hdr_clone(regc->pool, chdr));
322 chdr = chdr->next;
323 }
324
325 return PJ_SUCCESS;
326}
327
Benny Prijono720d0a82007-01-12 06:37:35 +0000328
329/*
330 * Bind client registration to a specific transport/listener.
331 */
332PJ_DEF(pj_status_t) pjsip_regc_set_transport( pjsip_regc *regc,
333 const pjsip_tpselector *sel)
334{
335 PJ_ASSERT_RETURN(regc && sel, PJ_EINVAL);
336
337 pjsip_tpselector_dec_ref(&regc->tp_sel);
338 pj_memcpy(&regc->tp_sel, sel, sizeof(*sel));
339 pjsip_tpselector_add_ref(&regc->tp_sel);
340
341 return PJ_SUCCESS;
342}
343
344
Benny Prijono8fc6de02006-11-11 21:25:55 +0000345PJ_DEF(pj_status_t) pjsip_regc_add_headers( pjsip_regc *regc,
346 const pjsip_hdr *hdr_list)
347{
348 const pjsip_hdr *hdr;
349
350 PJ_ASSERT_RETURN(regc && hdr_list, PJ_EINVAL);
351
352 //This is "add" operation, so don't remove headers.
353 //pj_list_init(&regc->hdr_list);
354
355 hdr = hdr_list->next;
356 while (hdr != hdr_list) {
357 pj_list_push_back(&regc->hdr_list, pjsip_hdr_clone(regc->pool, hdr));
358 hdr = hdr->next;
359 }
360
361 return PJ_SUCCESS;
362}
363
Benny Prijonoccf95622006-02-07 18:48:01 +0000364static pj_status_t create_request(pjsip_regc *regc,
365 pjsip_tx_data **p_tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000366{
Benny Prijonoccf95622006-02-07 18:48:01 +0000367 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000368 pjsip_tx_data *tdata;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000369
Benny Prijonoccf95622006-02-07 18:48:01 +0000370 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000371
Benny Prijonoccf95622006-02-07 18:48:01 +0000372 /* Create the request. */
373 status = pjsip_endpt_create_request_from_hdr( regc->endpt,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000374 pjsip_get_register_method(),
Benny Prijonoccf95622006-02-07 18:48:01 +0000375 regc->srv_url,
376 regc->from_hdr,
377 regc->to_hdr,
378 NULL,
379 regc->cid_hdr,
380 regc->cseq_hdr->cseq,
381 NULL,
382 &tdata);
383 if (status != PJ_SUCCESS)
384 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000385
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000386 /* Add cached authorization headers. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000387 pjsip_auth_clt_init_req( &regc->auth_sess, tdata );
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000388
Benny Prijono84126ab2006-02-09 09:30:09 +0000389 /* Add Route headers from route set, ideally after Via header */
390 if (!pj_list_empty(&regc->route_set)) {
391 pjsip_hdr *route_pos;
392 const pjsip_route_hdr *route;
393
Benny Prijonoa1e69682007-05-11 15:14:34 +0000394 route_pos = (pjsip_hdr*)
395 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
Benny Prijono84126ab2006-02-09 09:30:09 +0000396 if (!route_pos)
397 route_pos = &tdata->msg->hdr;
398
399 route = regc->route_set.next;
400 while (route != &regc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000401 pjsip_hdr *new_hdr = (pjsip_hdr*)
402 pjsip_hdr_shallow_clone(tdata->pool, route);
Benny Prijono84126ab2006-02-09 09:30:09 +0000403 pj_list_insert_after(route_pos, new_hdr);
404 route_pos = new_hdr;
405 route = route->next;
406 }
407 }
408
Benny Prijono8fc6de02006-11-11 21:25:55 +0000409 /* Add additional request headers */
410 if (!pj_list_empty(&regc->hdr_list)) {
411 const pjsip_hdr *hdr;
412
413 hdr = regc->hdr_list.next;
414 while (hdr != &regc->hdr_list) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000415 pjsip_hdr *new_hdr = (pjsip_hdr*)
416 pjsip_hdr_shallow_clone(tdata->pool, hdr);
Benny Prijono8fc6de02006-11-11 21:25:55 +0000417 pjsip_msg_add_hdr(tdata->msg, new_hdr);
418 hdr = hdr->next;
419 }
420 }
421
Benny Prijonoccf95622006-02-07 18:48:01 +0000422 /* Done. */
423 *p_tdata = tdata;
424 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000425}
426
427
Benny Prijonoccf95622006-02-07 18:48:01 +0000428PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg,
429 pjsip_tx_data **p_tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000430{
431 pjsip_msg *msg;
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000432 pjsip_hdr *hdr;
Benny Prijonoccf95622006-02-07 18:48:01 +0000433 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000434 pjsip_tx_data *tdata;
435
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000436 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
437
Benny Prijonoccf95622006-02-07 18:48:01 +0000438 status = create_request(regc, &tdata);
439 if (status != PJ_SUCCESS)
440 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000441
442 msg = tdata->msg;
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000443
444 /* Add Contact headers. */
445 hdr = regc->contact_hdr_list.next;
446 while (hdr != &regc->contact_hdr_list) {
447 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
448 pjsip_hdr_shallow_clone(tdata->pool, hdr));
449 hdr = hdr->next;
450 }
451
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000452 if (regc->expires_hdr)
Benny Prijonoa1e69682007-05-11 15:14:34 +0000453 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
454 pjsip_hdr_shallow_clone(tdata->pool,
Benny Prijono4093f7c2006-09-13 23:48:45 +0000455 regc->expires_hdr));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000456
457 if (regc->timer.id != 0) {
458 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
459 regc->timer.id = 0;
460 }
461
462 regc->auto_reg = autoreg;
463
Benny Prijonoccf95622006-02-07 18:48:01 +0000464 /* Done */
465 *p_tdata = tdata;
466 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000467}
468
469
Benny Prijonoccf95622006-02-07 18:48:01 +0000470PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc,
471 pjsip_tx_data **p_tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000472{
473 pjsip_tx_data *tdata;
474 pjsip_msg *msg;
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000475 pjsip_hdr *hdr;
Benny Prijonoccf95622006-02-07 18:48:01 +0000476 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000477
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000478 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
479
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000480 if (regc->timer.id != 0) {
481 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
482 regc->timer.id = 0;
483 }
484
Benny Prijonoccf95622006-02-07 18:48:01 +0000485 status = create_request(regc, &tdata);
486 if (status != PJ_SUCCESS)
487 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000488
489 msg = tdata->msg;
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000490
491 /* Add Contact headers. */
492 hdr = regc->contact_hdr_list.next;
493 while (hdr != &regc->contact_hdr_list) {
494 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
495 pjsip_hdr_shallow_clone(tdata->pool, hdr));
496 hdr = hdr->next;
497 }
498
Benny Prijonodfc4c482006-12-02 07:25:29 +0000499 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr);
500
501 *p_tdata = tdata;
502 return PJ_SUCCESS;
503}
504
505PJ_DEF(pj_status_t) pjsip_regc_unregister_all(pjsip_regc *regc,
506 pjsip_tx_data **p_tdata)
507{
508 pjsip_tx_data *tdata;
509 pjsip_msg *msg;
510 pj_status_t status;
511
512 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
513
514 if (regc->timer.id != 0) {
515 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
516 regc->timer.id = 0;
517 }
518
519 status = create_request(regc, &tdata);
520 if (status != PJ_SUCCESS)
521 return status;
522
523 msg = tdata->msg;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000524 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_contact_hdr);
525 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr);
526
Benny Prijonoccf95622006-02-07 18:48:01 +0000527 *p_tdata = tdata;
528 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000529}
530
531
532PJ_DEF(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc,
533 int contact_cnt,
534 const pj_str_t contact[] )
535{
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000536 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000537 return set_contact( regc, contact_cnt, contact );
538}
539
540
541PJ_DEF(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc,
542 pj_uint32_t expires )
543{
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000544 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000545 set_expires( regc, expires );
Benny Prijonoccf95622006-02-07 18:48:01 +0000546 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000547}
548
549
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000550static void call_callback(pjsip_regc *regc, pj_status_t status, int st_code,
551 const pj_str_t *reason,
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000552 pjsip_rx_data *rdata, pj_int32_t expiration,
553 int contact_cnt, pjsip_contact_hdr *contact[])
554{
555 struct pjsip_regc_cbparam cbparam;
556
557
Benny Prijonof2651802007-01-26 17:13:56 +0000558 if (!regc->cb)
559 return;
560
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000561 cbparam.regc = regc;
562 cbparam.token = regc->token;
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000563 cbparam.status = status;
564 cbparam.code = st_code;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000565 cbparam.reason = *reason;
566 cbparam.rdata = rdata;
567 cbparam.contact_cnt = contact_cnt;
568 cbparam.expiration = expiration;
569 if (contact_cnt) {
570 pj_memcpy( cbparam.contact, contact,
571 contact_cnt*sizeof(pjsip_contact_hdr*));
572 }
573
574 (*regc->cb)(&cbparam);
575}
576
577static void regc_refresh_timer_cb( pj_timer_heap_t *timer_heap,
578 struct pj_timer_entry *entry)
579{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000580 pjsip_regc *regc = (pjsip_regc*) entry->user_data;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000581 pjsip_tx_data *tdata;
Benny Prijonoccf95622006-02-07 18:48:01 +0000582 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000583
Benny Prijonoccf95622006-02-07 18:48:01 +0000584 PJ_UNUSED_ARG(timer_heap);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000585
Benny Prijonof2651802007-01-26 17:13:56 +0000586 /* Temporarily increase busy flag to prevent regc from being deleted
587 * in pjsip_regc_send()
588 */
589 regc->busy++;
590
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000591 entry->id = 0;
Benny Prijonoccf95622006-02-07 18:48:01 +0000592 status = pjsip_regc_register(regc, 1, &tdata);
593 if (status == PJ_SUCCESS) {
Benny Prijono27042582006-08-08 14:04:21 +0000594 status = pjsip_regc_send(regc, tdata);
595 }
596
Benny Prijonof2651802007-01-26 17:13:56 +0000597 if (status != PJ_SUCCESS && regc->cb) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000598 char errmsg[PJ_ERR_MSG_SIZE];
599 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000600 call_callback(regc, status, 400, &reason, NULL, -1, 0, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000601 }
Benny Prijonof2651802007-01-26 17:13:56 +0000602
603 regc->busy--;
604
605 /* Delete the record if user destroy regc during the callback. */
606 if (regc->_delete_flag && regc->busy==0) {
607 pjsip_regc_destroy(regc);
608 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000609}
610
611static void tsx_callback(void *token, pjsip_event *event)
612{
Benny Prijonoccf95622006-02-07 18:48:01 +0000613 pj_status_t status;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000614 pjsip_regc *regc = (pjsip_regc*) token;
Benny Prijonoccf95622006-02-07 18:48:01 +0000615 pjsip_transaction *tsx = event->body.tsx_state.tsx;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000616
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000617 /* Decrement pending transaction counter. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000618 pj_assert(regc->has_tsx);
619 regc->has_tsx = PJ_FALSE;
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000620
Benny Prijonobdc093f2007-10-04 09:48:25 +0000621 /* Add reference to the transport */
622 if (tsx->transport != regc->last_transport) {
623 if (regc->last_transport) {
624 pjsip_transport_dec_ref(regc->last_transport);
625 regc->last_transport = NULL;
626 }
627
628 if (tsx->transport) {
629 regc->last_transport = tsx->transport;
630 pjsip_transport_add_ref(regc->last_transport);
631 }
632 }
633
Benny Prijono58990232007-01-21 16:11:18 +0000634 /* Handle 401/407 challenge (even when _delete_flag is set) */
635 if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED ||
636 tsx->status_code == PJSIP_SC_UNAUTHORIZED)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000637 {
Benny Prijonoccf95622006-02-07 18:48:01 +0000638 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000639 pjsip_tx_data *tdata;
640
Benny Prijonoccf95622006-02-07 18:48:01 +0000641 status = pjsip_auth_clt_reinit_req( &regc->auth_sess,
642 rdata,
643 tsx->last_tx,
644 &tdata);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000645
Benny Prijonoccf95622006-02-07 18:48:01 +0000646 if (status == PJ_SUCCESS) {
Benny Prijono3fe034a2007-06-01 12:51:07 +0000647 ++regc->busy;
Benny Prijono27042582006-08-08 14:04:21 +0000648 status = pjsip_regc_send(regc, tdata);
Benny Prijono3fe034a2007-06-01 12:51:07 +0000649 --regc->busy;
650 }
Benny Prijono27042582006-08-08 14:04:21 +0000651
652 if (status != PJ_SUCCESS) {
Benny Prijono58990232007-01-21 16:11:18 +0000653
654 /* Only call callback if application is still interested
655 * in it.
656 */
657 if (regc->_delete_flag == 0) {
658 /* Increment busy flag temporarily to prevent regc from
659 * being destroyed.
660 */
661 ++regc->busy;
662
663 call_callback(regc, status, tsx->status_code,
664 &rdata->msg_info.msg->line.status.reason,
665 rdata, -1, 0, NULL);
666
667 /* Decrement busy flag */
668 --regc->busy;
669 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000670 }
Benny Prijono27042582006-08-08 14:04:21 +0000671
Benny Prijono58990232007-01-21 16:11:18 +0000672 } else if (regc->_delete_flag) {
673
674 /* User has called pjsip_regc_destroy(), so don't call callback.
675 * This regc will be destroyed later in this function.
676 */
677
678 /* Nothing to do */
679 ;
Benny Prijono27042582006-08-08 14:04:21 +0000680
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000681 } else {
682 int contact_cnt = 0;
683 pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT];
684 pjsip_rx_data *rdata;
685 pj_int32_t expiration = 0xFFFF;
686
687 if (tsx->status_code/100 == 2) {
688 int i;
689 pjsip_contact_hdr *hdr;
690 pjsip_msg *msg;
691 pjsip_expires_hdr *expires;
692
Benny Prijonoccf95622006-02-07 18:48:01 +0000693 rdata = event->body.tsx_state.src.rdata;
694 msg = rdata->msg_info.msg;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000695 hdr = (pjsip_contact_hdr*)
696 pjsip_msg_find_hdr( msg, PJSIP_H_CONTACT, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000697 while (hdr) {
698 contact[contact_cnt++] = hdr;
699 hdr = hdr->next;
700 if (hdr == (void*)&msg->hdr)
701 break;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000702 hdr = (pjsip_contact_hdr*)
703 pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, hdr);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000704 }
705
Benny Prijonoa1e69682007-05-11 15:14:34 +0000706 expires = (pjsip_expires_hdr*)
707 pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000708
709 if (expires)
710 expiration = expires->ivalue;
711
712 for (i=0; i<contact_cnt; ++i) {
713 hdr = contact[i];
Benny Prijonoe56b5b12007-09-11 09:04:51 +0000714 if (hdr->expires >= 0 && hdr->expires < expiration) {
715 pjsip_contact_hdr *our_contact;
716
717 our_contact = (pjsip_contact_hdr*)
718 regc->contact_hdr_list.next;
719 if ((void*)our_contact==(void*)&regc->contact_hdr_list.next)
720 continue;
721
722 /* Only set expiration time if this is the same Contact
723 * that we register.
724 */
725 if (pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR,
726 hdr->uri,
727 our_contact->uri)==0)
728 {
729 expiration = contact[i]->expires;
730 }
731 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000732 }
733
734 if (regc->auto_reg && expiration != 0 && expiration != 0xFFFF) {
735 pj_time_val delay = { 0, 0};
736
737 delay.sec = expiration - DELAY_BEFORE_REFRESH;
738 if (regc->expires != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED &&
739 delay.sec > (pj_int32_t)regc->expires)
740 {
741 delay.sec = regc->expires;
742 }
743 if (delay.sec < DELAY_BEFORE_REFRESH)
744 delay.sec = DELAY_BEFORE_REFRESH;
745 regc->timer.cb = &regc_refresh_timer_cb;
746 regc->timer.id = REFRESH_TIMER;
747 regc->timer.user_data = regc;
748 pjsip_endpt_schedule_timer( regc->endpt, &regc->timer, &delay);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000749 pj_gettimeofday(&regc->last_reg);
750 regc->next_reg = regc->last_reg;
751 regc->next_reg.sec += delay.sec;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000752 }
753
754 } else {
Benny Prijonoccf95622006-02-07 18:48:01 +0000755 rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ?
756 event->body.tsx_state.src.rdata : NULL;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000757 }
758
Benny Prijono25a86c72006-12-01 20:50:01 +0000759 /* Increment busy flag temporarily to prevent regc from
Benny Prijono197cabf2006-10-16 20:05:27 +0000760 * being destroyed.
761 */
Benny Prijono25a86c72006-12-01 20:50:01 +0000762 ++regc->busy;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000763
764 /* Call callback. */
765 if (expiration == 0xFFFF) expiration = -1;
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000766 call_callback(regc, PJ_SUCCESS, tsx->status_code,
Benny Prijonoccf95622006-02-07 18:48:01 +0000767 (rdata ? &rdata->msg_info.msg->line.status.reason
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000768 : pjsip_get_status_text(tsx->status_code)),
769 rdata, expiration,
770 contact_cnt, contact);
771
Benny Prijono25a86c72006-12-01 20:50:01 +0000772 /* Decrement busy flag */
773 --regc->busy;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000774 }
775
776 /* Delete the record if user destroy regc during the callback. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000777 if (regc->_delete_flag && regc->busy==0) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000778 pjsip_regc_destroy(regc);
779 }
780}
781
Benny Prijonoccf95622006-02-07 18:48:01 +0000782PJ_DEF(pj_status_t) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000783{
Benny Prijonoccf95622006-02-07 18:48:01 +0000784 pj_status_t status;
Benny Prijono59ca70f2006-02-22 22:18:58 +0000785 pjsip_cseq_hdr *cseq_hdr;
786 pj_uint32_t cseq;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000787
788 /* Make sure we don't have pending transaction. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000789 if (regc->has_tsx) {
Benny Prijono5b656872006-08-08 00:41:00 +0000790 PJ_LOG(4,(THIS_FILE, "Unable to send request, regc has another "
791 "transaction pending"));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000792 pjsip_tx_data_dec_ref( tdata );
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000793 return PJSIP_EBUSY;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000794 }
795
796 /* Invalidate message buffer. */
797 pjsip_tx_data_invalidate_msg(tdata);
798
799 /* Increment CSeq */
Benny Prijono59ca70f2006-02-22 22:18:58 +0000800 cseq = ++regc->cseq_hdr->cseq;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000801 cseq_hdr = (pjsip_cseq_hdr*)
802 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
Benny Prijono59ca70f2006-02-22 22:18:58 +0000803 cseq_hdr->cseq = cseq;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000804
Benny Prijonodc39fe82006-05-26 12:17:46 +0000805 /* Increment pending transaction first, since transaction callback
806 * may be called even before send_request() returns!
807 */
Benny Prijono25a86c72006-12-01 20:50:01 +0000808 regc->has_tsx = PJ_TRUE;
809 ++regc->busy;
Benny Prijono0f35f912007-02-05 18:59:31 +0000810 status = pjsip_endpt_send_request(regc->endpt, tdata, REGC_TSX_TIMEOUT,
811 regc, &tsx_callback);
Benny Prijono5b656872006-08-08 00:41:00 +0000812 if (status!=PJ_SUCCESS) {
Benny Prijono5b656872006-08-08 00:41:00 +0000813 PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status));
814 }
Benny Prijono25a86c72006-12-01 20:50:01 +0000815 --regc->busy;
Benny Prijono197cabf2006-10-16 20:05:27 +0000816
817 /* Delete the record if user destroy regc during the callback. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000818 if (regc->_delete_flag && regc->busy==0) {
Benny Prijono197cabf2006-10-16 20:05:27 +0000819 pjsip_regc_destroy(regc);
820 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000821
822 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000823}
824
825