blob: e77273ad0bf8581990f7fd496ebc0eb562e2c2ef [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
39#define THIS_FILE "sip_regc.c"
40
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;
68 char *contact_buf;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000069 pjsip_generic_string_hdr *contact_hdr;
Benny Prijono84126ab2006-02-09 09:30:09 +000070 pjsip_expires_hdr *expires_hdr;
71 pjsip_contact_hdr *unreg_contact_hdr;
72 pjsip_expires_hdr *unreg_expires_hdr;
73 pj_uint32_t expires;
74 pjsip_route_hdr route_set;
Benny Prijono8fc6de02006-11-11 21:25:55 +000075 pjsip_hdr hdr_list;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000076
Benny Prijono5dcb38d2005-11-21 01:55:47 +000077 /* Authorization sessions. */
Benny Prijono84126ab2006-02-09 09:30:09 +000078 pjsip_auth_clt_sess auth_sess;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000079
80 /* Auto refresh registration. */
Benny Prijono84126ab2006-02-09 09:30:09 +000081 pj_bool_t auto_reg;
Benny Prijonobcaed6c2006-02-19 15:37:19 +000082 pj_time_val last_reg;
83 pj_time_val next_reg;
Benny Prijono84126ab2006-02-09 09:30:09 +000084 pj_timer_entry timer;
Benny Prijono720d0a82007-01-12 06:37:35 +000085
86 /* Transport selector */
87 pjsip_tpselector tp_sel;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000088};
89
90
91
Benny Prijonoccf95622006-02-07 18:48:01 +000092PJ_DEF(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token,
93 pjsip_regc_cb *cb,
94 pjsip_regc **p_regc)
Benny Prijono5dcb38d2005-11-21 01:55:47 +000095{
96 pj_pool_t *pool;
97 pjsip_regc *regc;
Benny Prijonoccf95622006-02-07 18:48:01 +000098 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000099
Benny Prijonoccf95622006-02-07 18:48:01 +0000100 /* Verify arguments. */
101 PJ_ASSERT_RETURN(endpt && cb && p_regc, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000102
103 pool = pjsip_endpt_create_pool(endpt, "regc%p", 1024, 1024);
Benny Prijonoccf95622006-02-07 18:48:01 +0000104 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
105
106 regc = pj_pool_zalloc(pool, sizeof(struct pjsip_regc));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000107
108 regc->pool = pool;
109 regc->endpt = endpt;
110 regc->token = token;
111 regc->cb = cb;
112 regc->contact_buf = pj_pool_alloc(pool, PJSIP_REGC_CONTACT_BUF_SIZE);
113 regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED;
114
Benny Prijonoccf95622006-02-07 18:48:01 +0000115 status = pjsip_auth_clt_init(&regc->auth_sess, endpt, regc->pool, 0);
116 if (status != PJ_SUCCESS)
117 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000118
Benny Prijono84126ab2006-02-09 09:30:09 +0000119 pj_list_init(&regc->route_set);
Benny Prijono8fc6de02006-11-11 21:25:55 +0000120 pj_list_init(&regc->hdr_list);
Benny Prijono84126ab2006-02-09 09:30:09 +0000121
Benny Prijonoccf95622006-02-07 18:48:01 +0000122 /* Done */
123 *p_regc = regc;
124 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000125}
126
127
Benny Prijonoccf95622006-02-07 18:48:01 +0000128PJ_DEF(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000129{
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000130 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
131
Benny Prijono25a86c72006-12-01 20:50:01 +0000132 if (regc->has_tsx || regc->busy) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000133 regc->_delete_flag = 1;
134 regc->cb = NULL;
135 } else {
Benny Prijono720d0a82007-01-12 06:37:35 +0000136 pjsip_tpselector_dec_ref(&regc->tp_sel);
Benny Prijonoccf95622006-02-07 18:48:01 +0000137 pjsip_endpt_release_pool(regc->endpt, regc->pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000138 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000139
140 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000141}
142
143
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000144PJ_DEF(pj_status_t) pjsip_regc_get_info( pjsip_regc *regc,
145 pjsip_regc_info *info )
146{
147 PJ_ASSERT_RETURN(regc && info, PJ_EINVAL);
148
149 info->server_uri = regc->str_srv_url;
150 info->client_uri = regc->from_uri;
Benny Prijono25a86c72006-12-01 20:50:01 +0000151 info->is_busy = (regc->busy || regc->has_tsx);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000152 info->auto_reg = regc->auto_reg;
153 info->interval = regc->expires;
154
Benny Prijono25a86c72006-12-01 20:50:01 +0000155 if (regc->has_tsx)
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000156 info->next_reg = 0;
157 else if (regc->auto_reg == 0)
158 info->next_reg = 0;
159 else if (regc->expires < 0)
160 info->next_reg = regc->expires;
161 else {
162 pj_time_val now, next_reg;
163
164 next_reg = regc->next_reg;
165 pj_gettimeofday(&now);
166 PJ_TIME_VAL_SUB(next_reg, now);
167 info->next_reg = next_reg.sec;
168 }
169
170 return PJ_SUCCESS;
171}
172
173
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000174PJ_DEF(pj_pool_t*) pjsip_regc_get_pool(pjsip_regc *regc)
175{
176 return regc->pool;
177}
178
179static void set_expires( pjsip_regc *regc, pj_uint32_t expires)
180{
181 if (expires != regc->expires) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000182 regc->expires_hdr = pjsip_expires_hdr_create(regc->pool, expires);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000183 } else {
184 regc->expires_hdr = NULL;
185 }
186}
187
188
189static pj_status_t set_contact( pjsip_regc *regc,
190 int contact_cnt,
191 const pj_str_t contact[] )
192{
193 int i;
194 char *s;
195 const pj_str_t contact_STR = { "Contact", 7};
196
197 /* Concatenate contacts. */
198 for (i=0, s=regc->contact_buf; i<contact_cnt; ++i) {
199 if ((s-regc->contact_buf) + contact[i].slen + 2 > PJSIP_REGC_CONTACT_BUF_SIZE) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000200 return PJSIP_EURITOOLONG;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000201 }
202 pj_memcpy(s, contact[i].ptr, contact[i].slen);
203 s += contact[i].slen;
204
205 if (i != contact_cnt - 1) {
206 *s++ = ',';
207 *s++ = ' ';
208 }
209 }
210
211 /* Set "Contact" header. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000212 regc->contact_hdr = pjsip_generic_string_hdr_create(regc->pool,
213 &contact_STR,
214 NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000215 regc->contact_hdr->hvalue.ptr = regc->contact_buf;
216 regc->contact_hdr->hvalue.slen = (s - regc->contact_buf);
217
Benny Prijonoccf95622006-02-07 18:48:01 +0000218 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000219}
220
221
222PJ_DEF(pj_status_t) pjsip_regc_init( pjsip_regc *regc,
223 const pj_str_t *srv_url,
224 const pj_str_t *from_url,
225 const pj_str_t *to_url,
226 int contact_cnt,
227 const pj_str_t contact[],
228 pj_uint32_t expires)
229{
230 pj_str_t tmp;
Benny Prijonoccf95622006-02-07 18:48:01 +0000231 pj_status_t status;
232
233 PJ_ASSERT_RETURN(regc && srv_url && from_url && to_url &&
234 contact_cnt && contact && expires, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000235
236 /* Copy server URL. */
237 pj_strdup_with_null(regc->pool, &regc->str_srv_url, srv_url);
238
239 /* Set server URL. */
240 tmp = regc->str_srv_url;
241 regc->srv_url = pjsip_parse_uri( regc->pool, tmp.ptr, tmp.slen, 0);
242 if (regc->srv_url == NULL) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000243 return PJSIP_EINVALIDURI;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000244 }
245
246 /* Set "From" header. */
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000247 pj_strdup_with_null(regc->pool, &regc->from_uri, from_url);
248 tmp = regc->from_uri;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000249 regc->from_hdr = pjsip_from_hdr_create(regc->pool);
250 regc->from_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
251 PJSIP_PARSE_URI_AS_NAMEADDR);
252 if (!regc->from_hdr->uri) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000253 PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s",
254 from_url->slen, from_url->ptr));
255 return PJSIP_EINVALIDURI;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000256 }
257
258 /* Set "To" header. */
259 pj_strdup_with_null(regc->pool, &tmp, to_url);
260 regc->to_hdr = pjsip_to_hdr_create(regc->pool);
261 regc->to_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
262 PJSIP_PARSE_URI_AS_NAMEADDR);
263 if (!regc->to_hdr->uri) {
264 PJ_LOG(4,(THIS_FILE, "regc: invalid target URI %.*s", to_url->slen, to_url->ptr));
Benny Prijonoccf95622006-02-07 18:48:01 +0000265 return PJSIP_EINVALIDURI;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000266 }
267
268
269 /* Set "Contact" header. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000270 status = set_contact( regc, contact_cnt, contact);
271 if (status != PJ_SUCCESS)
272 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000273
274 /* Set "Expires" header, if required. */
275 set_expires( regc, expires);
276
277 /* Set "Call-ID" header. */
278 regc->cid_hdr = pjsip_cid_hdr_create(regc->pool);
279 pj_create_unique_string(regc->pool, &regc->cid_hdr->id);
280
281 /* Set "CSeq" header. */
282 regc->cseq_hdr = pjsip_cseq_hdr_create(regc->pool);
Benny Prijono59ca70f2006-02-22 22:18:58 +0000283 regc->cseq_hdr->cseq = pj_rand() % 0xFFFF;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000284 pjsip_method_set( &regc->cseq_hdr->method, PJSIP_REGISTER_METHOD);
285
286 /* Create "Contact" header used in unregistration. */
287 regc->unreg_contact_hdr = pjsip_contact_hdr_create(regc->pool);
288 regc->unreg_contact_hdr->star = 1;
289
290 /* Create "Expires" header used in unregistration. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000291 regc->unreg_expires_hdr = pjsip_expires_hdr_create( regc->pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000292
293 /* Done. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000294 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000295}
296
297PJ_DEF(pj_status_t) pjsip_regc_set_credentials( pjsip_regc *regc,
298 int count,
299 const pjsip_cred_info cred[] )
300{
Benny Prijonoccf95622006-02-07 18:48:01 +0000301 PJ_ASSERT_RETURN(regc && count && cred, PJ_EINVAL);
302 return pjsip_auth_clt_set_credentials(&regc->auth_sess, count, cred);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000303}
304
Benny Prijono84126ab2006-02-09 09:30:09 +0000305PJ_DEF(pj_status_t) pjsip_regc_set_route_set( pjsip_regc *regc,
306 const pjsip_route_hdr *route_set)
307{
308 const pjsip_route_hdr *chdr;
309
310 PJ_ASSERT_RETURN(regc && route_set, PJ_EINVAL);
311
312 pj_list_init(&regc->route_set);
313
314 chdr = route_set->next;
315 while (chdr != route_set) {
316 pj_list_push_back(&regc->route_set, pjsip_hdr_clone(regc->pool, chdr));
317 chdr = chdr->next;
318 }
319
320 return PJ_SUCCESS;
321}
322
Benny Prijono720d0a82007-01-12 06:37:35 +0000323
324/*
325 * Bind client registration to a specific transport/listener.
326 */
327PJ_DEF(pj_status_t) pjsip_regc_set_transport( pjsip_regc *regc,
328 const pjsip_tpselector *sel)
329{
330 PJ_ASSERT_RETURN(regc && sel, PJ_EINVAL);
331
332 pjsip_tpselector_dec_ref(&regc->tp_sel);
333 pj_memcpy(&regc->tp_sel, sel, sizeof(*sel));
334 pjsip_tpselector_add_ref(&regc->tp_sel);
335
336 return PJ_SUCCESS;
337}
338
339
Benny Prijono8fc6de02006-11-11 21:25:55 +0000340PJ_DEF(pj_status_t) pjsip_regc_add_headers( pjsip_regc *regc,
341 const pjsip_hdr *hdr_list)
342{
343 const pjsip_hdr *hdr;
344
345 PJ_ASSERT_RETURN(regc && hdr_list, PJ_EINVAL);
346
347 //This is "add" operation, so don't remove headers.
348 //pj_list_init(&regc->hdr_list);
349
350 hdr = hdr_list->next;
351 while (hdr != hdr_list) {
352 pj_list_push_back(&regc->hdr_list, pjsip_hdr_clone(regc->pool, hdr));
353 hdr = hdr->next;
354 }
355
356 return PJ_SUCCESS;
357}
358
Benny Prijonoccf95622006-02-07 18:48:01 +0000359static pj_status_t create_request(pjsip_regc *regc,
360 pjsip_tx_data **p_tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000361{
Benny Prijonoccf95622006-02-07 18:48:01 +0000362 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000363 pjsip_tx_data *tdata;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000364
Benny Prijonoccf95622006-02-07 18:48:01 +0000365 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000366
Benny Prijonoccf95622006-02-07 18:48:01 +0000367 /* Create the request. */
368 status = pjsip_endpt_create_request_from_hdr( regc->endpt,
369 &pjsip_register_method,
370 regc->srv_url,
371 regc->from_hdr,
372 regc->to_hdr,
373 NULL,
374 regc->cid_hdr,
375 regc->cseq_hdr->cseq,
376 NULL,
377 &tdata);
378 if (status != PJ_SUCCESS)
379 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000380
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000381 /* Add cached authorization headers. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000382 pjsip_auth_clt_init_req( &regc->auth_sess, tdata );
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000383
Benny Prijono84126ab2006-02-09 09:30:09 +0000384 /* Add Route headers from route set, ideally after Via header */
385 if (!pj_list_empty(&regc->route_set)) {
386 pjsip_hdr *route_pos;
387 const pjsip_route_hdr *route;
388
389 route_pos = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
390 if (!route_pos)
391 route_pos = &tdata->msg->hdr;
392
393 route = regc->route_set.next;
394 while (route != &regc->route_set) {
395 pjsip_hdr *new_hdr = pjsip_hdr_shallow_clone(tdata->pool, route);
396 pj_list_insert_after(route_pos, new_hdr);
397 route_pos = new_hdr;
398 route = route->next;
399 }
400 }
401
Benny Prijono8fc6de02006-11-11 21:25:55 +0000402 /* Add additional request headers */
403 if (!pj_list_empty(&regc->hdr_list)) {
404 const pjsip_hdr *hdr;
405
406 hdr = regc->hdr_list.next;
407 while (hdr != &regc->hdr_list) {
408 pjsip_hdr *new_hdr = pjsip_hdr_shallow_clone(tdata->pool, hdr);
409 pjsip_msg_add_hdr(tdata->msg, new_hdr);
410 hdr = hdr->next;
411 }
412 }
413
Benny Prijonoccf95622006-02-07 18:48:01 +0000414 /* Done. */
415 *p_tdata = tdata;
416 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000417}
418
419
Benny Prijonoccf95622006-02-07 18:48:01 +0000420PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg,
421 pjsip_tx_data **p_tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000422{
423 pjsip_msg *msg;
Benny Prijonoccf95622006-02-07 18:48:01 +0000424 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000425 pjsip_tx_data *tdata;
426
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000427 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
428
Benny Prijonoccf95622006-02-07 18:48:01 +0000429 status = create_request(regc, &tdata);
430 if (status != PJ_SUCCESS)
431 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000432
Benny Prijonoccf95622006-02-07 18:48:01 +0000433 /* Add Contact header. */
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000434 msg = tdata->msg;
Benny Prijono4093f7c2006-09-13 23:48:45 +0000435 pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool,
436 regc->contact_hdr));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000437 if (regc->expires_hdr)
Benny Prijono4093f7c2006-09-13 23:48:45 +0000438 pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool,
439 regc->expires_hdr));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000440
441 if (regc->timer.id != 0) {
442 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
443 regc->timer.id = 0;
444 }
445
446 regc->auto_reg = autoreg;
447
Benny Prijonoccf95622006-02-07 18:48:01 +0000448 /* Done */
449 *p_tdata = tdata;
450 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000451}
452
453
Benny Prijonoccf95622006-02-07 18:48:01 +0000454PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc,
455 pjsip_tx_data **p_tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000456{
457 pjsip_tx_data *tdata;
458 pjsip_msg *msg;
Benny Prijonoccf95622006-02-07 18:48:01 +0000459 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000460
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000461 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
462
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000463 if (regc->timer.id != 0) {
464 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
465 regc->timer.id = 0;
466 }
467
Benny Prijonoccf95622006-02-07 18:48:01 +0000468 status = create_request(regc, &tdata);
469 if (status != PJ_SUCCESS)
470 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000471
472 msg = tdata->msg;
Benny Prijonodfc4c482006-12-02 07:25:29 +0000473 pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool,
474 regc->contact_hdr));
475 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr);
476
477 *p_tdata = tdata;
478 return PJ_SUCCESS;
479}
480
481PJ_DEF(pj_status_t) pjsip_regc_unregister_all(pjsip_regc *regc,
482 pjsip_tx_data **p_tdata)
483{
484 pjsip_tx_data *tdata;
485 pjsip_msg *msg;
486 pj_status_t status;
487
488 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
489
490 if (regc->timer.id != 0) {
491 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
492 regc->timer.id = 0;
493 }
494
495 status = create_request(regc, &tdata);
496 if (status != PJ_SUCCESS)
497 return status;
498
499 msg = tdata->msg;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000500 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_contact_hdr);
501 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr);
502
Benny Prijonoccf95622006-02-07 18:48:01 +0000503 *p_tdata = tdata;
504 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000505}
506
507
508PJ_DEF(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc,
509 int contact_cnt,
510 const pj_str_t contact[] )
511{
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000512 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000513 return set_contact( regc, contact_cnt, contact );
514}
515
516
517PJ_DEF(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc,
518 pj_uint32_t expires )
519{
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000520 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000521 set_expires( regc, expires );
Benny Prijonoccf95622006-02-07 18:48:01 +0000522 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000523}
524
525
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000526static void call_callback(pjsip_regc *regc, pj_status_t status, int st_code,
527 const pj_str_t *reason,
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000528 pjsip_rx_data *rdata, pj_int32_t expiration,
529 int contact_cnt, pjsip_contact_hdr *contact[])
530{
531 struct pjsip_regc_cbparam cbparam;
532
533
Benny Prijonof2651802007-01-26 17:13:56 +0000534 if (!regc->cb)
535 return;
536
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000537 cbparam.regc = regc;
538 cbparam.token = regc->token;
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000539 cbparam.status = status;
540 cbparam.code = st_code;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000541 cbparam.reason = *reason;
542 cbparam.rdata = rdata;
543 cbparam.contact_cnt = contact_cnt;
544 cbparam.expiration = expiration;
545 if (contact_cnt) {
546 pj_memcpy( cbparam.contact, contact,
547 contact_cnt*sizeof(pjsip_contact_hdr*));
548 }
549
550 (*regc->cb)(&cbparam);
551}
552
553static void regc_refresh_timer_cb( pj_timer_heap_t *timer_heap,
554 struct pj_timer_entry *entry)
555{
556 pjsip_regc *regc = entry->user_data;
557 pjsip_tx_data *tdata;
Benny Prijonoccf95622006-02-07 18:48:01 +0000558 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000559
Benny Prijonoccf95622006-02-07 18:48:01 +0000560 PJ_UNUSED_ARG(timer_heap);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000561
Benny Prijonof2651802007-01-26 17:13:56 +0000562 /* Temporarily increase busy flag to prevent regc from being deleted
563 * in pjsip_regc_send()
564 */
565 regc->busy++;
566
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000567 entry->id = 0;
Benny Prijonoccf95622006-02-07 18:48:01 +0000568 status = pjsip_regc_register(regc, 1, &tdata);
569 if (status == PJ_SUCCESS) {
Benny Prijono27042582006-08-08 14:04:21 +0000570 status = pjsip_regc_send(regc, tdata);
571 }
572
Benny Prijonof2651802007-01-26 17:13:56 +0000573 if (status != PJ_SUCCESS && regc->cb) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000574 char errmsg[PJ_ERR_MSG_SIZE];
575 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000576 call_callback(regc, status, 400, &reason, NULL, -1, 0, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000577 }
Benny Prijonof2651802007-01-26 17:13:56 +0000578
579 regc->busy--;
580
581 /* Delete the record if user destroy regc during the callback. */
582 if (regc->_delete_flag && regc->busy==0) {
583 pjsip_regc_destroy(regc);
584 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000585}
586
587static void tsx_callback(void *token, pjsip_event *event)
588{
Benny Prijonoccf95622006-02-07 18:48:01 +0000589 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000590 pjsip_regc *regc = token;
Benny Prijonoccf95622006-02-07 18:48:01 +0000591 pjsip_transaction *tsx = event->body.tsx_state.tsx;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000592
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000593 /* Decrement pending transaction counter. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000594 pj_assert(regc->has_tsx);
595 regc->has_tsx = PJ_FALSE;
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000596
Benny Prijono58990232007-01-21 16:11:18 +0000597 /* Handle 401/407 challenge (even when _delete_flag is set) */
598 if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED ||
599 tsx->status_code == PJSIP_SC_UNAUTHORIZED)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000600 {
Benny Prijonoccf95622006-02-07 18:48:01 +0000601 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000602 pjsip_tx_data *tdata;
603
Benny Prijonoccf95622006-02-07 18:48:01 +0000604 status = pjsip_auth_clt_reinit_req( &regc->auth_sess,
605 rdata,
606 tsx->last_tx,
607 &tdata);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000608
Benny Prijonoccf95622006-02-07 18:48:01 +0000609 if (status == PJ_SUCCESS) {
Benny Prijono27042582006-08-08 14:04:21 +0000610 status = pjsip_regc_send(regc, tdata);
611 }
612
613 if (status != PJ_SUCCESS) {
Benny Prijono58990232007-01-21 16:11:18 +0000614
615 /* Only call callback if application is still interested
616 * in it.
617 */
618 if (regc->_delete_flag == 0) {
619 /* Increment busy flag temporarily to prevent regc from
620 * being destroyed.
621 */
622 ++regc->busy;
623
624 call_callback(regc, status, tsx->status_code,
625 &rdata->msg_info.msg->line.status.reason,
626 rdata, -1, 0, NULL);
627
628 /* Decrement busy flag */
629 --regc->busy;
630 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000631 }
Benny Prijono27042582006-08-08 14:04:21 +0000632
Benny Prijono58990232007-01-21 16:11:18 +0000633 } else if (regc->_delete_flag) {
634
635 /* User has called pjsip_regc_destroy(), so don't call callback.
636 * This regc will be destroyed later in this function.
637 */
638
639 /* Nothing to do */
640 ;
Benny Prijono27042582006-08-08 14:04:21 +0000641
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000642 } else {
643 int contact_cnt = 0;
644 pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT];
645 pjsip_rx_data *rdata;
646 pj_int32_t expiration = 0xFFFF;
647
648 if (tsx->status_code/100 == 2) {
649 int i;
650 pjsip_contact_hdr *hdr;
651 pjsip_msg *msg;
652 pjsip_expires_hdr *expires;
653
Benny Prijonoccf95622006-02-07 18:48:01 +0000654 rdata = event->body.tsx_state.src.rdata;
655 msg = rdata->msg_info.msg;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000656 hdr = pjsip_msg_find_hdr( msg, PJSIP_H_CONTACT, NULL);
657 while (hdr) {
658 contact[contact_cnt++] = hdr;
659 hdr = hdr->next;
660 if (hdr == (void*)&msg->hdr)
661 break;
662 hdr = pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, hdr);
663 }
664
665 expires = pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
666
667 if (expires)
668 expiration = expires->ivalue;
669
670 for (i=0; i<contact_cnt; ++i) {
671 hdr = contact[i];
672 if (hdr->expires >= 0 && hdr->expires < expiration)
673 expiration = contact[i]->expires;
674 }
675
676 if (regc->auto_reg && expiration != 0 && expiration != 0xFFFF) {
677 pj_time_val delay = { 0, 0};
678
679 delay.sec = expiration - DELAY_BEFORE_REFRESH;
680 if (regc->expires != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED &&
681 delay.sec > (pj_int32_t)regc->expires)
682 {
683 delay.sec = regc->expires;
684 }
685 if (delay.sec < DELAY_BEFORE_REFRESH)
686 delay.sec = DELAY_BEFORE_REFRESH;
687 regc->timer.cb = &regc_refresh_timer_cb;
688 regc->timer.id = REFRESH_TIMER;
689 regc->timer.user_data = regc;
690 pjsip_endpt_schedule_timer( regc->endpt, &regc->timer, &delay);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000691 pj_gettimeofday(&regc->last_reg);
692 regc->next_reg = regc->last_reg;
693 regc->next_reg.sec += delay.sec;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000694 }
695
696 } else {
Benny Prijonoccf95622006-02-07 18:48:01 +0000697 rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ?
698 event->body.tsx_state.src.rdata : NULL;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000699 }
700
Benny Prijono25a86c72006-12-01 20:50:01 +0000701 /* Increment busy flag temporarily to prevent regc from
Benny Prijono197cabf2006-10-16 20:05:27 +0000702 * being destroyed.
703 */
Benny Prijono25a86c72006-12-01 20:50:01 +0000704 ++regc->busy;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000705
706 /* Call callback. */
707 if (expiration == 0xFFFF) expiration = -1;
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000708 call_callback(regc, PJ_SUCCESS, tsx->status_code,
Benny Prijonoccf95622006-02-07 18:48:01 +0000709 (rdata ? &rdata->msg_info.msg->line.status.reason
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000710 : pjsip_get_status_text(tsx->status_code)),
711 rdata, expiration,
712 contact_cnt, contact);
713
Benny Prijono25a86c72006-12-01 20:50:01 +0000714 /* Decrement busy flag */
715 --regc->busy;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000716 }
717
718 /* Delete the record if user destroy regc during the callback. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000719 if (regc->_delete_flag && regc->busy==0) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000720 pjsip_regc_destroy(regc);
721 }
722}
723
Benny Prijonoccf95622006-02-07 18:48:01 +0000724PJ_DEF(pj_status_t) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000725{
Benny Prijonoccf95622006-02-07 18:48:01 +0000726 pj_status_t status;
Benny Prijono59ca70f2006-02-22 22:18:58 +0000727 pjsip_cseq_hdr *cseq_hdr;
728 pj_uint32_t cseq;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000729
730 /* Make sure we don't have pending transaction. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000731 if (regc->has_tsx) {
Benny Prijono5b656872006-08-08 00:41:00 +0000732 PJ_LOG(4,(THIS_FILE, "Unable to send request, regc has another "
733 "transaction pending"));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000734 pjsip_tx_data_dec_ref( tdata );
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000735 return PJSIP_EBUSY;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000736 }
737
738 /* Invalidate message buffer. */
739 pjsip_tx_data_invalidate_msg(tdata);
740
741 /* Increment CSeq */
Benny Prijono59ca70f2006-02-22 22:18:58 +0000742 cseq = ++regc->cseq_hdr->cseq;
743 cseq_hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
744 cseq_hdr->cseq = cseq;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000745
Benny Prijonodc39fe82006-05-26 12:17:46 +0000746 /* Increment pending transaction first, since transaction callback
747 * may be called even before send_request() returns!
748 */
Benny Prijono25a86c72006-12-01 20:50:01 +0000749 regc->has_tsx = PJ_TRUE;
750 ++regc->busy;
Benny Prijono0f35f912007-02-05 18:59:31 +0000751 status = pjsip_endpt_send_request(regc->endpt, tdata, REGC_TSX_TIMEOUT,
752 regc, &tsx_callback);
Benny Prijono5b656872006-08-08 00:41:00 +0000753 if (status!=PJ_SUCCESS) {
Benny Prijono5b656872006-08-08 00:41:00 +0000754 PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status));
755 }
Benny Prijono25a86c72006-12-01 20:50:01 +0000756 --regc->busy;
Benny Prijono197cabf2006-10-16 20:05:27 +0000757
758 /* Delete the record if user destroy regc during the callback. */
Benny Prijono25a86c72006-12-01 20:50:01 +0000759 if (regc->_delete_flag && regc->busy==0) {
Benny Prijono197cabf2006-10-16 20:05:27 +0000760 pjsip_regc_destroy(regc);
761 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000762
763 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000764}
765
766