blob: 90e352afe040c312dbb8d262ab24db7271defa9b [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjsua-lib/pjsua.h>
21#include <pjsua-lib/pjsua_internal.h>
22
23
24#define THIS_FILE "pjsua_acc.c"
25
Benny Prijonob54719f2010-11-16 03:07:46 +000026enum
27{
Benny Prijono2562b752010-11-16 06:01:38 +000028 OUTBOUND_UNKNOWN, // status unknown
29 OUTBOUND_WANTED, // initiated in registration
30 OUTBOUND_ACTIVE, // got positive response from server
31 OUTBOUND_NA // not wanted or got negative response from server
Benny Prijonob54719f2010-11-16 03:07:46 +000032};
33
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +000035static void schedule_reregistration(pjsua_acc *acc);
36
Benny Prijonoeebe9af2006-06-13 22:57:13 +000037/*
38 * Get number of current accounts.
39 */
40PJ_DEF(unsigned) pjsua_acc_get_count(void)
41{
42 return pjsua_var.acc_cnt;
43}
44
45
46/*
47 * Check if the specified account ID is valid.
48 */
49PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
50{
Benny Prijonoa1e69682007-05-11 15:14:34 +000051 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052 pjsua_var.acc[acc_id].valid;
53}
54
55
56/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000057 * Set default account
58 */
59PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
60{
61 pjsua_var.default_acc = acc_id;
62 return PJ_SUCCESS;
63}
64
65
66/*
67 * Get default account.
68 */
69PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
70{
71 return pjsua_var.default_acc;
72}
73
74
75/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000076 * Copy account configuration.
77 */
Benny Prijonobddef2c2007-10-31 13:28:08 +000078PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
79 pjsua_acc_config *dst,
80 const pjsua_acc_config *src)
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081{
82 unsigned i;
83
84 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
85
86 pj_strdup_with_null(pool, &dst->id, &src->id);
87 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000088 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +000089 pj_strdup_with_null(pool, &dst->contact_params, &src->contact_params);
90 pj_strdup_with_null(pool, &dst->contact_uri_params,
91 &src->contact_uri_params);
Benny Prijonofe04fb52007-08-24 08:28:52 +000092 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +000093 pj_strdup_with_null(pool, &dst->rfc5626_instance_id,
94 &src->rfc5626_instance_id);
Benny Prijonob54719f2010-11-16 03:07:46 +000095 pj_strdup_with_null(pool, &dst->rfc5626_reg_id, &src->rfc5626_reg_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000096
97 dst->proxy_cnt = src->proxy_cnt;
98 for (i=0; i<src->proxy_cnt; ++i)
99 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
100
101 dst->reg_timeout = src->reg_timeout;
102 dst->cred_count = src->cred_count;
103
104 for (i=0; i<src->cred_count; ++i) {
105 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
106 }
Benny Prijonobddef2c2007-10-31 13:28:08 +0000107
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +0000108 pj_list_init(&dst->reg_hdr_list);
109 if (!pj_list_empty(&src->reg_hdr_list)) {
110 const pjsip_hdr *hdr;
111
112 hdr = src->reg_hdr_list.next;
113 while (hdr != &src->reg_hdr_list) {
114 pj_list_push_back(&dst->reg_hdr_list, pjsip_hdr_clone(pool, hdr));
115 hdr = hdr->next;
116 }
117 }
118
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +0000119 pjsip_auth_clt_pref_dup(pool, &dst->auth_pref, &src->auth_pref);
120
Benny Prijono0bc99a92011-03-17 04:34:43 +0000121 pjsua_transport_config_dup(pool, &dst->rtp_cfg, &src->rtp_cfg);
122
Benny Prijonobddef2c2007-10-31 13:28:08 +0000123 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000124}
125
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000126/*
127 * Calculate CRC of proxy list.
128 */
129static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt)
130{
131 pj_crc32_context ctx;
132 unsigned i;
133
134 pj_crc32_init(&ctx);
135 for (i=0; i<cnt; ++i) {
136 pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen);
137 }
138
139 return pj_crc32_final(&ctx);
140}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000141
142/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000143 * Initialize a new account (after configuration is set).
144 */
145static pj_status_t initialize_acc(unsigned acc_id)
146{
147 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
148 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000149 pjsip_name_addr *name_addr;
Benny Prijonoc7545782010-09-28 07:43:18 +0000150 pjsip_sip_uri *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000151 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000152 unsigned i;
153
154 /* Need to parse local_uri to get the elements: */
155
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000156 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000157 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000158 acc_cfg->id.slen,
159 PJSIP_PARSE_URI_AS_NAMEADDR);
160 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000161 pjsua_perror(THIS_FILE, "Invalid local URI",
162 PJSIP_EINVALIDURI);
163 return PJSIP_EINVALIDURI;
164 }
165
166 /* Local URI MUST be a SIP or SIPS: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000167 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
168 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000169 {
Benny Prijonoc7545782010-09-28 07:43:18 +0000170 acc->display = name_addr->display;
171 acc->user_part = name_addr->display;
172 acc->srv_domain = pj_str("");
173 acc->srv_port = 0;
174 } else {
175 pjsip_sip_uri *sip_uri;
176
177 /* Get the SIP URI object: */
178 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
179
180 /* Save the user and domain part. These will be used when finding an
181 * account for incoming requests.
182 */
183 acc->display = name_addr->display;
184 acc->user_part = sip_uri->user;
185 acc->srv_domain = sip_uri->host;
186 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000187 }
188
189
Benny Prijonob4a17c92006-07-10 14:40:21 +0000190 /* Parse registrar URI, if any */
191 if (acc_cfg->reg_uri.slen) {
192 pjsip_uri *reg_uri;
193
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000194 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000195 acc_cfg->reg_uri.slen, 0);
196 if (reg_uri == NULL) {
197 pjsua_perror(THIS_FILE, "Invalid registrar URI",
198 PJSIP_EINVALIDURI);
199 return PJSIP_EINVALIDURI;
200 }
201
202 /* Registrar URI MUST be a SIP or SIPS: */
203 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
204 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
205 {
206 pjsua_perror(THIS_FILE, "Invalid registar URI",
207 PJSIP_EINVALIDSCHEME);
208 return PJSIP_EINVALIDSCHEME;
209 }
210
211 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
212
213 } else {
214 sip_reg_uri = NULL;
215 }
216
Benny Prijonob4a17c92006-07-10 14:40:21 +0000217 if (sip_reg_uri) {
218 acc->srv_port = sip_reg_uri->port;
219 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000220
221 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000222 //if (acc_cfg->contact.slen == 0) {
223 // acc_cfg->contact = acc_cfg->id;
224 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000225
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000226 /* Build account route-set from outbound proxies and route set from
227 * account configuration.
228 */
229 pj_list_init(&acc->route_set);
230
Benny Prijono29c8ca32010-06-22 06:02:13 +0000231 if (!pj_list_empty(&pjsua_var.outbound_proxy)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000232 pjsip_route_hdr *r;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233
Benny Prijono29c8ca32010-06-22 06:02:13 +0000234 r = pjsua_var.outbound_proxy.next;
235 while (r != &pjsua_var.outbound_proxy) {
236 pj_list_push_back(&acc->route_set,
237 pjsip_hdr_shallow_clone(acc->pool, r));
238 r = r->next;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000239 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240 }
241
242 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
243 pj_str_t hname = { "Route", 5};
244 pjsip_route_hdr *r;
245 pj_str_t tmp;
246
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000247 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000248 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000249 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000250 if (r == NULL) {
251 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
252 PJ_EINVAL);
253 return PJ_EINVAL;
254 }
255 pj_list_push_back(&acc->route_set, r);
256 }
257
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000258 /* Concatenate credentials from account config and global config */
259 acc->cred_cnt = 0;
260 for (i=0; i<acc_cfg->cred_count; ++i) {
261 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
262 }
263 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
264 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
265 {
266 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
267 }
268
Benny Prijono07fe2302010-06-24 12:33:18 +0000269 /* If ICE is enabled, add "+sip.ice" media feature tag in account's
270 * contact params.
271 */
272#if PJSUA_ADD_ICE_TAGS
273 if (pjsua_var.media_cfg.enable_ice) {
274 unsigned new_len;
275 pj_str_t new_prm;
276
277 new_len = acc_cfg->contact_params.slen + 10;
278 new_prm.ptr = (char*)pj_pool_alloc(acc->pool, new_len);
279 pj_strcpy(&new_prm, &acc_cfg->contact_params);
280 pj_strcat2(&new_prm, ";+sip.ice");
281 acc_cfg->contact_params = new_prm;
282 }
283#endif
284
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000285 status = pjsua_pres_init_acc(acc_id);
286 if (status != PJ_SUCCESS)
287 return status;
288
Benny Prijonob54719f2010-11-16 03:07:46 +0000289 /* If SIP outbound is enabled, generate instance and reg ID if they are
290 * not specified
291 */
292 if (acc_cfg->use_rfc5626) {
293 if (acc_cfg->rfc5626_instance_id.slen==0) {
294 const pj_str_t *hostname;
295 pj_uint32_t hval, pos;
296 char instprm[] = ";+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000CCDDEEFF>\"";
297
298 hostname = pj_gethostname();
299 pos = pj_ansi_strlen(instprm) - 10;
300 hval = pj_hash_calc(0, hostname->ptr, hostname->slen);
301 pj_val_to_hex_digit( ((char*)&hval)[0], instprm+pos+0);
302 pj_val_to_hex_digit( ((char*)&hval)[1], instprm+pos+2);
303 pj_val_to_hex_digit( ((char*)&hval)[2], instprm+pos+4);
304 pj_val_to_hex_digit( ((char*)&hval)[3], instprm+pos+6);
305
306 pj_strdup2(acc->pool, &acc->rfc5626_instprm, instprm);
307 } else {
308 const char *prmname = ";+sip.instance=\"";
309 unsigned len;
310
311 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_instance_id.slen + 1;
312 acc->rfc5626_instprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
313 pj_ansi_snprintf(acc->rfc5626_instprm.ptr, len+1,
314 "%s%.*s\"",
315 prmname,
316 (int)acc_cfg->rfc5626_instance_id.slen,
317 acc_cfg->rfc5626_instance_id.ptr);
318 acc->rfc5626_instprm.slen = len;
319 }
320
321 if (acc_cfg->rfc5626_reg_id.slen==0) {
322 acc->rfc5626_regprm = pj_str(";reg-id=1");
323 } else {
324 const char *prmname = ";reg-id=";
325 unsigned len;
326
327 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_reg_id.slen;
328 acc->rfc5626_regprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
329 pj_ansi_snprintf(acc->rfc5626_regprm.ptr, len+1,
330 "%s%.*s\"",
331 prmname,
332 (int)acc_cfg->rfc5626_reg_id.slen,
333 acc_cfg->rfc5626_reg_id.ptr);
334 acc->rfc5626_regprm.slen = len;
335 }
336 }
337
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000338 /* Mark account as valid */
339 pjsua_var.acc[acc_id].valid = PJ_TRUE;
340
Benny Prijono093d3022006-09-24 00:07:11 +0000341 /* Insert account ID into account ID array, sorted by priority */
342 for (i=0; i<pjsua_var.acc_cnt; ++i) {
343 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
344 pjsua_var.acc[acc_id].cfg.priority)
345 {
346 break;
347 }
348 }
349 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
350 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000351
352 return PJ_SUCCESS;
353}
354
355
356/*
357 * Add a new account to pjsua.
358 */
359PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
360 pj_bool_t is_default,
361 pjsua_acc_id *p_acc_id)
362{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000363 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000364 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000365 pj_status_t status;
366
367 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
368 PJ_ETOOMANY);
369
370 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000371 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372
373 PJSUA_LOCK();
374
375 /* Find empty account id. */
376 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
377 if (pjsua_var.acc[id].valid == PJ_FALSE)
378 break;
379 }
380
381 /* Expect to find a slot */
382 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
383 {PJSUA_UNLOCK(); return PJ_EBUG;});
384
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000385 acc = &pjsua_var.acc[id];
386
387 /* Create pool for this account. */
388 if (acc->pool)
389 pj_pool_reset(acc->pool);
390 else
391 acc->pool = pjsua_pool_create("acc%p", 512, 256);
392
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000393 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000394 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395
396 /* Normalize registration timeout */
397 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
398 pjsua_var.acc[id].cfg.reg_timeout == 0)
399 {
400 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
401 }
402
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000403 /* Get CRC of account proxy setting */
404 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
405
406 /* Get CRC of global outbound proxy setting */
407 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
408 pjsua_var.ua_cfg.outbound_proxy_cnt);
409
Benny Prijono91d06b62008-09-20 12:16:56 +0000410 /* Check the route URI's and force loose route if required */
411 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
412 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
413 if (status != PJ_SUCCESS)
414 return status;
415 }
416
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 status = initialize_acc(id);
418 if (status != PJ_SUCCESS) {
419 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000420 pj_pool_release(acc->pool);
421 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000422 PJSUA_UNLOCK();
423 return status;
424 }
425
426 if (is_default)
427 pjsua_var.default_acc = id;
428
429 if (p_acc_id)
430 *p_acc_id = id;
431
432 pjsua_var.acc_cnt++;
433
434 PJSUA_UNLOCK();
435
436 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
437 (int)cfg->id.slen, cfg->id.ptr, id));
438
439 /* If accounts has registration enabled, start registration */
440 if (pjsua_var.acc[id].cfg.reg_uri.slen)
441 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000442 else {
443 /* Otherwise subscribe to MWI, if it's enabled */
444 if (pjsua_var.acc[id].cfg.mwi_enabled)
445 pjsua_start_mwi(&pjsua_var.acc[id]);
446 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000447
448 return PJ_SUCCESS;
449}
450
451
452/*
453 * Add local account
454 */
455PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
456 pj_bool_t is_default,
457 pjsua_acc_id *p_acc_id)
458{
459 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000460 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000461 const char *beginquote, *endquote;
462 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000463 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000464
Benny Prijonoe93e2872006-06-28 16:46:49 +0000465 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000466 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
467 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000468
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000469 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000470 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471
472 pjsua_acc_config_default(&cfg);
473
Benny Prijono093d3022006-09-24 00:07:11 +0000474 /* Lower the priority of local account */
475 --cfg.priority;
476
Benny Prijonod0bd4982007-12-02 15:40:52 +0000477 /* Enclose IPv6 address in square brackets */
478 if (t->type & PJSIP_TRANSPORT_IPV6) {
479 beginquote = "[";
480 endquote = "]";
481 } else {
482 beginquote = endquote = "";
483 }
484
485 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000486 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000487 pj_ansi_snprintf(transport_param, sizeof(transport_param),
488 ";transport=%s",
489 pjsip_transport_get_type_name(t->type));
490 } else {
491 transport_param[0] = '\0';
492 }
493
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000494 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000495 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000496 "<sip:%s%.*s%s:%d%s>",
497 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000498 (int)t->local_name.host.slen,
499 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000500 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000501 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000502 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000503
504 cfg.id = pj_str(uri);
505
506 return pjsua_acc_add(&cfg, is_default, p_acc_id);
507}
508
509
510/*
Benny Prijono705e7842008-07-21 18:12:51 +0000511 * Set arbitrary data to be associated with the account.
512 */
513PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
514 void *user_data)
515{
516 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
517 PJ_EINVAL);
518 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
519
520 PJSUA_LOCK();
521
522 pjsua_var.acc[acc_id].cfg.user_data = user_data;
523
524 PJSUA_UNLOCK();
525
526 return PJ_SUCCESS;
527}
528
529
530/*
531 * Retrieve arbitrary data associated with the account.
532 */
533PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
534{
535 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
536 NULL);
537 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
538
539 return pjsua_var.acc[acc_id].cfg.user_data;
540}
541
542
543/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000544 * Delete account.
545 */
546PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
547{
Benny Prijono093d3022006-09-24 00:07:11 +0000548 unsigned i;
549
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000550 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
551 PJ_EINVAL);
552 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
553
554 PJSUA_LOCK();
555
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000556 /* Cancel any re-registration timer */
557 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
558
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000560 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000561 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000562 if (pjsua_var.acc[acc_id].regc) {
563 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
564 }
Benny Prijono922933b2007-01-21 16:23:56 +0000565 pjsua_var.acc[acc_id].regc = NULL;
566 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000567
568 /* Delete server presence subscription */
569 pjsua_pres_delete_acc(acc_id);
570
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000571 /* Release account pool */
572 if (pjsua_var.acc[acc_id].pool) {
573 pj_pool_release(pjsua_var.acc[acc_id].pool);
574 pjsua_var.acc[acc_id].pool = NULL;
575 }
576
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000577 /* Invalidate */
578 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000579 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000580
Benny Prijono093d3022006-09-24 00:07:11 +0000581 /* Remove from array */
582 for (i=0; i<pjsua_var.acc_cnt; ++i) {
583 if (pjsua_var.acc_ids[i] == acc_id)
584 break;
585 }
586 if (i != pjsua_var.acc_cnt) {
587 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
588 pjsua_var.acc_cnt, i);
589 --pjsua_var.acc_cnt;
590 }
591
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000592 /* Leave the calls intact, as I don't think calls need to
593 * access account once it's created
594 */
595
Benny Prijonofb2b3652007-06-28 07:15:03 +0000596 /* Update default account */
597 if (pjsua_var.default_acc == acc_id)
598 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000599
600 PJSUA_UNLOCK();
601
602 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
603
604 return PJ_SUCCESS;
605}
606
607
608/*
609 * Modify account information.
610 */
611PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
612 const pjsua_acc_config *cfg)
613{
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000614 pjsua_acc *acc;
615 pjsip_name_addr *id_name_addr = NULL;
616 pjsip_sip_uri *id_sip_uri = NULL;
617 pjsip_sip_uri *reg_sip_uri = NULL;
618 pj_uint32_t local_route_crc, global_route_crc;
619 pjsip_route_hdr global_route;
620 pjsip_route_hdr local_route;
621 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
622 pj_bool_t update_reg = PJ_FALSE;
623 pj_status_t status = PJ_SUCCESS;
624
625 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
626 PJ_EINVAL);
627
628 PJSUA_LOCK();
629
630 acc = &pjsua_var.acc[acc_id];
631 if (!acc->valid) {
632 status = PJ_EINVAL;
633 goto on_return;
634 }
635
636 /* == Validate first == */
637
638 /* Account id */
639 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
640 /* Need to parse id to get the elements: */
641 id_name_addr = (pjsip_name_addr*)
642 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
643 PJSIP_PARSE_URI_AS_NAMEADDR);
644 if (id_name_addr == NULL) {
645 status = PJSIP_EINVALIDURI;
646 pjsua_perror(THIS_FILE, "Invalid local URI", status);
647 goto on_return;
648 }
649
650 /* URI MUST be a SIP or SIPS: */
651 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
652 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
653 {
654 status = PJSIP_EINVALIDSCHEME;
655 pjsua_perror(THIS_FILE, "Invalid local URI", status);
656 goto on_return;
657 }
658
659 /* Get the SIP URI object: */
660 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
661 }
662
663 /* Registrar URI */
664 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
665 pjsip_uri *reg_uri;
666
667 /* Need to parse reg_uri to get the elements: */
668 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
669 cfg->reg_uri.slen, 0);
670 if (reg_uri == NULL) {
671 status = PJSIP_EINVALIDURI;
672 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
673 goto on_return;
674 }
675
676 /* Registrar URI MUST be a SIP or SIPS: */
677 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
678 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
679 {
680 status = PJSIP_EINVALIDSCHEME;
681 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
682 goto on_return;
683 }
684
685 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
686 }
687
688 /* Global outbound proxy */
689 global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
690 pjsua_var.ua_cfg.outbound_proxy_cnt);
691 if (global_route_crc != acc->global_route_crc) {
692 pjsip_route_hdr *r;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000693
Benny Prijono29c8ca32010-06-22 06:02:13 +0000694 /* Copy from global outbound proxies */
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000695 pj_list_init(&global_route);
Benny Prijono29c8ca32010-06-22 06:02:13 +0000696 r = pjsua_var.outbound_proxy.next;
697 while (r != &pjsua_var.outbound_proxy) {
698 pj_list_push_back(&global_route,
699 pjsip_hdr_shallow_clone(acc->pool, r));
700 r = r->next;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000701 }
702 }
703
704 /* Account proxy */
705 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
706 if (local_route_crc != acc->local_route_crc) {
707 pjsip_route_hdr *r;
708 unsigned i;
709
710 /* Validate the local route and save it to temporary var */
711 pj_list_init(&local_route);
712 for (i=0; i<cfg->proxy_cnt; ++i) {
713 pj_str_t hname = { "Route", 5};
714
715 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
716 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
717 if (status != PJ_SUCCESS)
718 goto on_return;
719 r = (pjsip_route_hdr*)
720 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
721 acc_proxy[i].slen, NULL);
722 if (r == NULL) {
723 status = PJSIP_EINVALIDURI;
724 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
725 status);
726 goto on_return;
727 }
728
729 pj_list_push_back(&local_route, r);
730 }
731 }
732
733
734 /* == Apply the new config == */
735
736 /* Account ID. */
737 if (id_name_addr && id_sip_uri) {
738 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
739 acc->display = id_name_addr->display;
740 acc->user_part = id_sip_uri->user;
741 acc->srv_domain = id_sip_uri->host;
742 acc->srv_port = 0;
743 update_reg = PJ_TRUE;
744 }
745
746 /* User data */
747 acc->cfg.user_data = cfg->user_data;
748
749 /* Priority */
750 if (acc->cfg.priority != cfg->priority) {
751 unsigned i;
752
753 acc->cfg.priority = cfg->priority;
754
755 /* Resort accounts priority */
756 for (i=0; i<pjsua_var.acc_cnt; ++i) {
757 if (pjsua_var.acc_ids[i] == acc_id)
758 break;
759 }
760 pj_assert(i < pjsua_var.acc_cnt);
761 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
762 pjsua_var.acc_cnt, i);
763 for (i=0; i<pjsua_var.acc_cnt; ++i) {
764 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
765 acc->cfg.priority)
766 {
767 break;
768 }
769 }
770 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
771 pjsua_var.acc_cnt, i, &acc_id);
772 }
773
774 /* MWI */
775 acc->cfg.mwi_enabled = cfg->mwi_enabled;
776
777 /* PIDF tuple ID */
778 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
779 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
780 &cfg->pidf_tuple_id);
781
782 /* Publish */
783 acc->cfg.publish_opt = cfg->publish_opt;
784 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
785 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
786 acc->cfg.publish_enabled = cfg->publish_enabled;
787 if (!acc->cfg.publish_enabled)
788 pjsua_pres_unpublish(acc);
789 else
790 update_reg = PJ_TRUE;
791 }
792
793 /* Force contact URI */
794 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
795 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
796 &cfg->force_contact);
797 update_reg = PJ_TRUE;
798 }
799
800 /* Contact param */
801 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
802 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
803 &cfg->contact_params);
804 update_reg = PJ_TRUE;
805 }
806
807 /* Contact URI params */
808 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
809 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
810 &cfg->contact_uri_params);
811 update_reg = PJ_TRUE;
812 }
813
814 /* Reliable provisional response */
815 acc->cfg.require_100rel = cfg->require_100rel;
816
817 /* Session timer */
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000818 acc->cfg.use_timer = cfg->use_timer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000819 acc->cfg.timer_setting = cfg->timer_setting;
820
821 /* Transport and keep-alive */
822 if (acc->cfg.transport_id != cfg->transport_id) {
823 acc->cfg.transport_id = cfg->transport_id;
824 update_reg = PJ_TRUE;
825 }
826 acc->cfg.ka_interval = cfg->ka_interval;
827 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
828 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
829#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
830 acc->cfg.use_srtp = cfg->use_srtp;
831 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000832 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000833#endif
834
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000835#if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
836 acc->cfg.use_stream_ka = cfg->use_stream_ka;
837#endif
838
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000839 /* Global outbound proxy */
840 if (global_route_crc != acc->global_route_crc) {
841 unsigned i, rcnt;
842
843 /* Remove the outbound proxies from the route set */
844 rcnt = pj_list_size(&acc->route_set);
845 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
846 pjsip_route_hdr *r = acc->route_set.next;
847 pj_list_erase(r);
848 }
849
850 /* Insert the outbound proxies to the beginning of route set */
851 pj_list_merge_first(&acc->route_set, &global_route);
852
853 /* Update global route CRC */
854 acc->global_route_crc = global_route_crc;
855
856 update_reg = PJ_TRUE;
857 }
858
859 /* Account proxy */
860 if (local_route_crc != acc->local_route_crc) {
861 unsigned i;
862
863 /* Remove the current account proxies from the route set */
864 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
865 pjsip_route_hdr *r = acc->route_set.prev;
866 pj_list_erase(r);
867 }
868
869 /* Insert new proxy setting to the route set */
870 pj_list_merge_last(&acc->route_set, &local_route);
871
872 /* Update the proxy setting */
873 acc->cfg.proxy_cnt = cfg->proxy_cnt;
874 for (i = 0; i < cfg->proxy_cnt; ++i)
875 acc->cfg.proxy[i] = acc_proxy[i];
876
877 /* Update local route CRC */
878 acc->local_route_crc = local_route_crc;
879
880 update_reg = PJ_TRUE;
881 }
882
883 /* Credential info */
884 {
885 unsigned i;
886
887 /* Selective update credential info. */
888 for (i = 0; i < cfg->cred_count; ++i) {
889 unsigned j;
890 pjsip_cred_info ci;
891
892 /* Find if this credential is already listed */
893 for (j = i; j < acc->cfg.cred_count; ++i) {
894 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
895 &cfg->cred_info[i]) == 0)
896 {
897 /* Found, but different index/position, swap */
898 if (j != i) {
899 ci = acc->cfg.cred_info[i];
900 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
901 acc->cfg.cred_info[j] = ci;
902 }
903 break;
904 }
905 }
906
907 /* Not found, insert this */
908 if (j == acc->cfg.cred_count) {
909 /* If account credential is full, discard the last one. */
910 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
911 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
912 acc->cfg.cred_count, acc->cfg.cred_count-1);
913 acc->cfg.cred_count--;
914 }
915
916 /* Insert this */
917 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
918 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
919 acc->cfg.cred_count, i, &ci);
920 }
921 }
922 acc->cfg.cred_count = cfg->cred_count;
923
924 /* Concatenate credentials from account config and global config */
925 acc->cred_cnt = 0;
926 for (i=0; i<acc->cfg.cred_count; ++i) {
927 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
928 }
929 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
930 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
931 {
932 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
933 }
934 }
935
936 /* Authentication preference */
937 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
938 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
939 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
940 &cfg->auth_pref.algorithm);
941
942 /* Registration */
943 acc->cfg.reg_timeout = cfg->reg_timeout;
944 acc->cfg.unreg_timeout = cfg->unreg_timeout;
945 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
946 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
947 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
948
949 /* Normalize registration timeout */
950 if (acc->cfg.reg_uri.slen && acc->cfg.reg_timeout == 0)
951 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
952
953 /* Registrar URI */
954 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
955 if (cfg->reg_uri.slen) {
956 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
957 if (reg_sip_uri)
958 acc->srv_port = reg_sip_uri->port;
959 } else {
960 /* Unregister if registration was set */
961 if (acc->cfg.reg_uri.slen)
962 pjsua_acc_set_registration(acc->index, PJ_FALSE);
963 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
964 }
965 update_reg = PJ_TRUE;
966 }
967
Benny Prijonob54719f2010-11-16 03:07:46 +0000968 /* SIP outbound setting */
969 if (acc->cfg.use_rfc5626 != cfg->use_rfc5626 ||
970 pj_strcmp(&acc->cfg.rfc5626_instance_id, &cfg->rfc5626_instance_id) ||
971 pj_strcmp(&acc->cfg.rfc5626_reg_id, &cfg->rfc5626_reg_id))
972 {
973 update_reg = PJ_TRUE;
974 }
975
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000976 /* Update registration */
977 if (update_reg) {
978 /* If accounts has registration enabled, start registration */
979 if (acc->cfg.reg_uri.slen)
980 pjsua_acc_set_registration(acc->index, PJ_TRUE);
981 else {
982 /* Otherwise subscribe to MWI, if it's enabled */
983 if (acc->cfg.mwi_enabled)
984 pjsua_start_mwi(acc);
985 }
986 }
987
988on_return:
989 PJSUA_UNLOCK();
990 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000991}
992
993
994/*
995 * Modify account's presence status to be advertised to remote/presence
996 * subscribers.
997 */
998PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
999 pj_bool_t is_online)
1000{
1001 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1002 PJ_EINVAL);
1003 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1004
1005 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001006 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
1007 pjsua_pres_update_acc(acc_id, PJ_FALSE);
1008 return PJ_SUCCESS;
1009}
1010
1011
1012/*
1013 * Set online status with extended information
1014 */
1015PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
1016 pj_bool_t is_online,
1017 const pjrpid_element *pr)
1018{
1019 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1020 PJ_EINVAL);
1021 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1022
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001023 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +00001024 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001025 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
1026 PJSUA_UNLOCK();
1027
Benny Prijono4461c7d2007-08-25 13:36:15 +00001028 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001029 return PJ_SUCCESS;
1030}
1031
Benny Prijonob54719f2010-11-16 03:07:46 +00001032/* Create reg_contact, mainly for SIP outbound */
1033static void update_regc_contact(pjsua_acc *acc)
1034{
1035 pjsua_acc_config *acc_cfg = &acc->cfg;
1036 pj_bool_t need_outbound = PJ_FALSE;
1037 const pj_str_t tcp_param = pj_str(";transport=tcp");
1038 const pj_str_t tls_param = pj_str(";transport=tls");
1039
1040 if (!acc_cfg->use_rfc5626)
1041 goto done;
1042
Benny Prijono2562b752010-11-16 06:01:38 +00001043 /* Check if outbound has been requested and rejected */
1044 if (acc->rfc5626_status == OUTBOUND_NA)
1045 goto done;
1046
Benny Prijonob54719f2010-11-16 03:07:46 +00001047 if (pj_stristr(&acc->contact, &tcp_param)==NULL &&
1048 pj_stristr(&acc->contact, &tls_param)==NULL)
1049 {
1050 /* Currently we can only do SIP outbound for TCP
1051 * and TLS.
1052 */
1053 goto done;
1054 }
1055
1056 /* looks like we can use outbound */
1057 need_outbound = PJ_TRUE;
1058
1059done:
1060 if (!need_outbound) {
1061 /* Outbound is not needed/wanted for the account. acc->reg_contact
1062 * is set to the same as acc->contact.
1063 */
1064 acc->reg_contact = acc->contact;
Benny Prijono2562b752010-11-16 06:01:38 +00001065 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001066 } else {
1067 /* Need to use outbound, append the contact with +sip.instance and
1068 * reg-id parameters.
1069 */
1070 unsigned len;
1071 pj_str_t reg_contact;
1072
1073 acc->rfc5626_status = OUTBOUND_WANTED;
1074 len = acc->contact.slen + acc->rfc5626_instprm.slen +
1075 acc->rfc5626_regprm.slen;
Benny Prijonoe49e6202010-11-16 07:01:25 +00001076 reg_contact.ptr = (char*) pj_pool_alloc(acc->pool, len);
Benny Prijonob54719f2010-11-16 03:07:46 +00001077
1078 pj_strcpy(&reg_contact, &acc->contact);
1079 pj_strcat(&reg_contact, &acc->rfc5626_regprm);
1080 pj_strcat(&reg_contact, &acc->rfc5626_instprm);
1081
1082 acc->reg_contact = reg_contact;
1083
1084 PJ_LOG(4,(THIS_FILE,
1085 "Contact for acc %d updated for SIP outbound: %.*s",
1086 acc->index,
1087 (int)acc->reg_contact.slen,
1088 acc->reg_contact.ptr));
1089 }
1090}
1091
Benny Prijono7f630432008-09-24 16:52:41 +00001092/* Check if IP is private IP address */
1093static pj_bool_t is_private_ip(const pj_str_t *addr)
1094{
1095 const pj_str_t private_net[] =
1096 {
1097 { "10.", 3 },
1098 { "127.", 4 },
1099 { "172.16.", 7 },
1100 { "192.168.", 8 }
1101 };
1102 unsigned i;
1103
1104 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
1105 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
1106 return PJ_TRUE;
1107 }
1108
1109 return PJ_FALSE;
1110}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001111
Benny Prijono15b02302007-09-27 14:07:07 +00001112/* Update NAT address from the REGISTER response */
1113static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
1114 struct pjsip_regc_cbparam *param)
1115{
1116 pjsip_transport *tp;
1117 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001118 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +00001119 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001120 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +00001121 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +00001122 pj_sockaddr contact_addr;
1123 pj_sockaddr recv_addr;
1124 pj_status_t status;
1125 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +00001126 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001127 pjsip_contact_hdr *contact_hdr;
1128 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +00001129
1130 tp = param->rdata->tp_info.transport;
1131
1132 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001133 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +00001134 return PJ_FALSE;
1135
Benny Prijonob54719f2010-11-16 03:07:46 +00001136 /* If SIP outbound is active, no need to update */
1137 if (acc->rfc5626_status == OUTBOUND_ACTIVE) {
1138 PJ_LOG(4,(THIS_FILE, "Acc %d has SIP outbound active, no need to "
1139 "update registration Contact", acc->index));
1140 return PJ_FALSE;
1141 }
1142
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001143#if 0
1144 // Always update
1145 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +00001146
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001147 /* For UDP, only update if STUN is enabled (for now).
1148 * For TCP/TLS, always check.
1149 */
1150 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
1151 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
1152 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
1153 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
1154 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +00001155 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001156 /* Yes we will check */
1157 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001158 return PJ_FALSE;
1159 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001160#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001161
1162 /* Get the received and rport info */
1163 via = param->rdata->msg_info.via;
1164 if (via->rport_param < 1) {
1165 /* Remote doesn't support rport */
1166 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001167 if (rport==0) {
1168 pjsip_transport_type_e tp_type;
1169 tp_type = (pjsip_transport_type_e) tp->key.type;
1170 rport = pjsip_transport_get_default_port_for_type(tp_type);
1171 }
Benny Prijono15b02302007-09-27 14:07:07 +00001172 } else
1173 rport = via->rport_param;
1174
1175 if (via->recvd_param.slen != 0)
1176 via_addr = &via->recvd_param;
1177 else
1178 via_addr = &via->sent_by.host;
1179
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001180 /* Compare received and rport with the URI in our registration */
1181 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001182 contact_hdr = (pjsip_contact_hdr*)
1183 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1184 acc->contact.slen, NULL);
1185 pj_assert(contact_hdr != NULL);
1186 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001187 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001188 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001189
Benny Prijono24a21852008-04-14 04:04:30 +00001190 if (uri->port == 0) {
1191 pjsip_transport_type_e tp_type;
1192 tp_type = (pjsip_transport_type_e) tp->key.type;
1193 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1194 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001195
Benny Prijono84d24932009-06-04 15:51:39 +00001196 /* Convert IP address strings into sockaddr for comparison.
1197 * (http://trac.pjsip.org/repos/ticket/863)
1198 */
1199 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1200 &contact_addr);
1201 if (status == PJ_SUCCESS)
1202 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1203 &recv_addr);
1204 if (status == PJ_SUCCESS) {
1205 /* Compare the addresses as sockaddr according to the ticket above */
1206 matched = (uri->port == rport &&
1207 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1208 } else {
1209 /* Compare the addresses as string, as before */
1210 matched = (uri->port == rport &&
1211 pj_stricmp(&uri->host, via_addr)==0);
1212 }
1213
1214 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001215 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001216 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001217 return PJ_FALSE;
1218 }
1219
Benny Prijono7f630432008-09-24 16:52:41 +00001220 /* Get server IP */
1221 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1222
Benny Prijono15b02302007-09-27 14:07:07 +00001223 /* At this point we've detected that the address as seen by registrar.
1224 * has changed.
1225 */
Benny Prijono7f630432008-09-24 16:52:41 +00001226
1227 /* Do not switch if both Contact and server's IP address are
1228 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001229 * might have messed up with the SIP packets. See:
1230 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001231 *
1232 * This exception can be disabled by setting allow_contact_rewrite
1233 * to 2. In this case, the switch will always be done whenever there
1234 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001235 */
Benny Prijono247921b2008-09-26 22:06:11 +00001236 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1237 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001238 {
1239 /* Don't switch */
1240 pj_pool_release(pool);
1241 return PJ_FALSE;
1242 }
1243
Benny Prijono4f933762009-11-10 03:45:42 +00001244 /* Also don't switch if only the port number part is different, and
1245 * the Via received address is private.
1246 * See http://trac.pjsip.org/repos/ticket/864
1247 */
1248 if (acc->cfg.allow_contact_rewrite != 2 &&
1249 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1250 is_private_ip(via_addr))
1251 {
1252 /* Don't switch */
1253 pj_pool_release(pool);
1254 return PJ_FALSE;
1255 }
1256
Benny Prijono15b02302007-09-27 14:07:07 +00001257 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001258 "(%.*s:%d --> %.*s:%d). Updating registration "
1259 "(using method %d)",
Benny Prijono15b02302007-09-27 14:07:07 +00001260 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001261 (int)uri->host.slen,
1262 uri->host.ptr,
1263 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001264 (int)via_addr->slen,
1265 via_addr->ptr,
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001266 rport,
1267 acc->cfg.contact_rewrite_method));
Benny Prijono15b02302007-09-27 14:07:07 +00001268
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001269 pj_assert(acc->cfg.contact_rewrite_method == 1 ||
1270 acc->cfg.contact_rewrite_method == 2);
1271
1272 if (acc->cfg.contact_rewrite_method == 1) {
1273 /* Unregister current contact */
1274 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1275 if (acc->regc != NULL) {
1276 pjsip_regc_destroy(acc->regc);
1277 acc->regc = NULL;
1278 acc->contact.slen = 0;
1279 }
Benny Prijono15b02302007-09-27 14:07:07 +00001280 }
1281
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001282 /*
1283 * Build new Contact header
1284 */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001285 {
Benny Prijonob54719f2010-11-16 03:07:46 +00001286 const char *ob = ";ob";
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001287 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001288 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001289 int len;
1290
Benny Prijono8972bf02009-04-05 18:30:45 +00001291 /* Enclose IPv6 address in square brackets */
1292 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1293 beginquote = "[";
1294 endquote = "]";
1295 } else {
1296 beginquote = endquote = "";
1297 }
1298
Benny Prijono24a21852008-04-14 04:04:30 +00001299 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001300 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00001301 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s%s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001302 (int)acc->user_part.slen,
1303 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001304 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001305 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001306 (int)via_addr->slen,
1307 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001308 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001309 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001310 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001311 (int)acc->cfg.contact_uri_params.slen,
1312 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00001313 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00001314 (int)acc->cfg.contact_params.slen,
1315 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001316 if (len < 1) {
1317 PJ_LOG(1,(THIS_FILE, "URI too long"));
1318 pj_pool_release(pool);
1319 return PJ_FALSE;
1320 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001321 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001322
Benny Prijonob54719f2010-11-16 03:07:46 +00001323 update_regc_contact(acc);
1324
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001325 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1326 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1327 tp->local_name.port = rport;
1328
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001329 }
1330
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001331 if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001332 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001333 }
Benny Prijono15b02302007-09-27 14:07:07 +00001334
1335 /* Perform new registration */
1336 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1337
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001338 pj_pool_release(pool);
1339
Benny Prijono15b02302007-09-27 14:07:07 +00001340 return PJ_TRUE;
1341}
1342
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001343/* Check and update Service-Route header */
1344void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1345{
1346 pjsip_generic_string_hdr *hsr = NULL;
1347 pjsip_route_hdr *hr, *h;
1348 const pj_str_t HNAME = { "Service-Route", 13 };
1349 const pj_str_t HROUTE = { "Route", 5 };
1350 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001351 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001352
1353 /* Find and parse Service-Route headers */
1354 for (;;) {
1355 char saved;
1356 int parsed_len;
1357
1358 /* Find Service-Route header */
1359 hsr = (pjsip_generic_string_hdr*)
1360 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1361 if (!hsr)
1362 break;
1363
1364 /* Parse as Route header since the syntax is similar. This may
1365 * return more than one headers.
1366 */
1367 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1368 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1369 hr = (pjsip_route_hdr*)
1370 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1371 hsr->hvalue.slen, &parsed_len);
1372 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1373
1374 if (hr == NULL) {
1375 /* Error */
1376 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1377 return;
1378 }
1379
1380 /* Save each URI in the result */
1381 h = hr;
1382 do {
1383 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1384 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1385 {
1386 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1387 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1388 return;
1389 }
1390
1391 uri[uri_cnt++] = h->name_addr.uri;
1392 h = h->next;
1393 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1394
1395 if (h != hr) {
1396 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1397 return;
1398 }
1399
1400 /* Prepare to find next Service-Route header */
1401 hsr = hsr->next;
1402 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1403 break;
1404 }
1405
1406 if (uri_cnt == 0)
1407 return;
1408
1409 /*
1410 * Update account's route set
1411 */
1412
1413 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001414 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001415 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1416 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1417 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001418 i<rcnt;
1419 ++i)
1420 {
1421 pjsip_route_hdr *prev = hr->prev;
1422 pj_list_erase(hr);
1423 hr = prev;
1424 }
1425 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001426
1427 /* Then append the Service-Route URIs */
1428 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001429 hr = pjsip_route_hdr_create(acc->pool);
1430 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001431 pj_list_push_back(&acc->route_set, hr);
1432 }
1433
1434 /* Done */
1435
1436 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1437 acc->index, uri_cnt));
1438}
1439
Benny Prijonobddef2c2007-10-31 13:28:08 +00001440
1441/* Keep alive timer callback */
1442static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1443{
1444 pjsua_acc *acc;
1445 pjsip_tpselector tp_sel;
1446 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001447 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001448 pj_status_t status;
1449
1450 PJ_UNUSED_ARG(th);
1451
1452 PJSUA_LOCK();
1453
1454 te->id = PJ_FALSE;
1455
1456 acc = (pjsua_acc*) te->user_data;
1457
1458 /* Select the transport to send the packet */
1459 pj_bzero(&tp_sel, sizeof(tp_sel));
1460 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1461 tp_sel.u.transport = acc->ka_transport;
1462
1463 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001464 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001465 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001466 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001467
1468 /* Send raw packet */
1469 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1470 PJSIP_TRANSPORT_UDP, &tp_sel,
1471 NULL, acc->cfg.ka_data.ptr,
1472 acc->cfg.ka_data.slen,
1473 &acc->ka_target, acc->ka_target_len,
1474 NULL, NULL);
1475
1476 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1477 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1478 }
1479
1480 /* Reschedule next timer */
1481 delay.sec = acc->cfg.ka_interval;
1482 delay.msec = 0;
1483 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1484 if (status == PJ_SUCCESS) {
1485 te->id = PJ_TRUE;
1486 } else {
1487 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1488 }
1489
1490 PJSUA_UNLOCK();
1491}
1492
1493
1494/* Update keep-alive for the account */
1495static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1496 struct pjsip_regc_cbparam *param)
1497{
1498 /* In all cases, stop keep-alive timer if it's running. */
1499 if (acc->ka_timer.id) {
1500 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1501 acc->ka_timer.id = PJ_FALSE;
1502
1503 pjsip_transport_dec_ref(acc->ka_transport);
1504 acc->ka_transport = NULL;
1505 }
1506
1507 if (start) {
1508 pj_time_val delay;
1509 pj_status_t status;
1510
1511 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001512 * - ka_interval is not zero in the account, and
1513 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001514 *
1515 * Previously we only enabled keep-alive when STUN is enabled, since
1516 * we thought that keep-alive is only needed in Internet situation.
1517 * But it has been discovered that Windows Firewall on WinXP also
1518 * needs to be kept-alive, otherwise incoming packets will be dropped.
1519 * So because of this, now keep-alive is always enabled for UDP,
1520 * regardless of whether STUN is enabled or not.
1521 *
1522 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1523 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001524 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001525 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001526 acc->cfg.ka_interval == 0 ||
1527 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1528 {
1529 /* Keep alive is not necessary */
1530 return;
1531 }
1532
1533 /* Save transport and destination address. */
1534 acc->ka_transport = param->rdata->tp_info.transport;
1535 pjsip_transport_add_ref(acc->ka_transport);
1536 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1537 param->rdata->pkt_info.src_addr_len);
1538 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1539
1540 /* Setup and start the timer */
1541 acc->ka_timer.cb = &keep_alive_timer_cb;
1542 acc->ka_timer.user_data = (void*)acc;
1543
1544 delay.sec = acc->cfg.ka_interval;
1545 delay.msec = 0;
1546 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1547 &delay);
1548 if (status == PJ_SUCCESS) {
1549 acc->ka_timer.id = PJ_TRUE;
1550 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1551 "destination:%s:%d, interval:%ds",
1552 acc->index,
1553 param->rdata->pkt_info.src_name,
1554 param->rdata->pkt_info.src_port,
1555 acc->cfg.ka_interval));
1556 } else {
1557 acc->ka_timer.id = PJ_FALSE;
1558 pjsip_transport_dec_ref(acc->ka_transport);
1559 acc->ka_transport = NULL;
1560 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1561 }
1562 }
1563}
1564
1565
Benny Prijonob54719f2010-11-16 03:07:46 +00001566/* Update the status of SIP outbound registration request */
1567static void update_rfc5626_status(pjsua_acc *acc, pjsip_rx_data *rdata)
1568{
1569 pjsip_require_hdr *hreq;
1570 const pj_str_t STR_OUTBOUND = {"outbound", 8};
1571 unsigned i;
1572
Benny Prijono2562b752010-11-16 06:01:38 +00001573 if (acc->rfc5626_status == OUTBOUND_UNKNOWN) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001574 goto on_return;
1575 }
1576
1577 hreq = rdata->msg_info.require;
1578 if (!hreq) {
Benny Prijono2562b752010-11-16 06:01:38 +00001579 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001580 goto on_return;
1581 }
1582
1583 for (i=0; i<hreq->count; ++i) {
1584 if (pj_stricmp(&hreq->values[i], &STR_OUTBOUND)==0) {
1585 acc->rfc5626_status = OUTBOUND_ACTIVE;
1586 goto on_return;
1587 }
1588 }
1589
1590 /* Server does not support outbound */
Benny Prijono2562b752010-11-16 06:01:38 +00001591 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001592
1593on_return:
Benny Prijono2562b752010-11-16 06:01:38 +00001594 if (acc->rfc5626_status != OUTBOUND_ACTIVE) {
1595 acc->reg_contact = acc->contact;
1596 }
Benny Prijonob54719f2010-11-16 03:07:46 +00001597 PJ_LOG(4,(THIS_FILE, "SIP outbound status for acc %d is %s",
1598 acc->index, (acc->rfc5626_status==OUTBOUND_ACTIVE?
1599 "active": "not active")));
1600}
1601
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001602/*
1603 * This callback is called by pjsip_regc when outgoing register
1604 * request has completed.
1605 */
1606static void regc_cb(struct pjsip_regc_cbparam *param)
1607{
1608
Benny Prijonoa1e69682007-05-11 15:14:34 +00001609 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001610
Benny Prijono15b02302007-09-27 14:07:07 +00001611 if (param->regc != acc->regc)
1612 return;
1613
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001614 PJSUA_LOCK();
1615
1616 /*
1617 * Print registration status.
1618 */
1619 if (param->status!=PJ_SUCCESS) {
1620 pjsua_perror(THIS_FILE, "SIP registration error",
1621 param->status);
1622 pjsip_regc_destroy(acc->regc);
1623 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001624 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001625
Benny Prijonobddef2c2007-10-31 13:28:08 +00001626 /* Stop keep-alive timer if any. */
1627 update_keep_alive(acc, PJ_FALSE, NULL);
1628
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001629 } else if (param->code < 0 || param->code >= 300) {
1630 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1631 param->code,
1632 (int)param->reason.slen, param->reason.ptr));
1633 pjsip_regc_destroy(acc->regc);
1634 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001635 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001636
Benny Prijonobddef2c2007-10-31 13:28:08 +00001637 /* Stop keep-alive timer if any. */
1638 update_keep_alive(acc, PJ_FALSE, NULL);
1639
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001640 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1641
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001642 /* Update auto registration flag */
1643 acc->auto_rereg.active = PJ_FALSE;
1644 acc->auto_rereg.attempt_cnt = 0;
1645
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001646 if (param->expiration < 1) {
1647 pjsip_regc_destroy(acc->regc);
1648 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001649 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001650
1651 /* Stop keep-alive timer if any. */
1652 update_keep_alive(acc, PJ_FALSE, NULL);
1653
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001654 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1655 pjsua_var.acc[acc->index].cfg.id.ptr));
1656 } else {
Benny Prijonob54719f2010-11-16 03:07:46 +00001657 /* Check and update SIP outbound status first, since the result
1658 * will determine if we should update re-registration
1659 */
1660 update_rfc5626_status(acc, param->rdata);
1661
Benny Prijono15b02302007-09-27 14:07:07 +00001662 /* Check NAT bound address */
1663 if (acc_check_nat_addr(acc, param)) {
Benny Prijono15b02302007-09-27 14:07:07 +00001664 PJSUA_UNLOCK();
1665 return;
1666 }
1667
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001668 /* Check and update Service-Route header */
1669 update_service_route(acc, param->rdata);
1670
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001671 PJ_LOG(3, (THIS_FILE,
1672 "%s: registration success, status=%d (%.*s), "
1673 "will re-register in %d seconds",
1674 pjsua_var.acc[acc->index].cfg.id.ptr,
1675 param->code,
1676 (int)param->reason.slen, param->reason.ptr,
1677 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001678
Benny Prijonobddef2c2007-10-31 13:28:08 +00001679 /* Start keep-alive timer if necessary. */
1680 update_keep_alive(acc, PJ_TRUE, param);
1681
Benny Prijono8b6834f2007-02-24 13:29:22 +00001682 /* Send initial PUBLISH if it is enabled */
1683 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1684 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001685
1686 /* Subscribe to MWI, if it's enabled */
1687 if (acc->cfg.mwi_enabled)
1688 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001689 }
1690
1691 } else {
1692 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1693 }
1694
1695 acc->reg_last_err = param->status;
1696 acc->reg_last_code = param->code;
1697
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001698 /* Check if we need to auto retry registration. Basically, registration
1699 * failure codes triggering auto-retry are those of temporal failures
1700 * considered to be recoverable in relatively short term.
1701 */
1702 if (acc->cfg.reg_retry_interval &&
1703 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1704 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1705 param->code == PJSIP_SC_BAD_GATEWAY ||
1706 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1707 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1708 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1709 {
1710 schedule_reregistration(acc);
1711 }
1712
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001713 /* Call the registration status callback */
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00001714
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001715 if (pjsua_var.ua_cfg.cb.on_reg_state) {
1716 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1717 }
1718
1719 if (pjsua_var.ua_cfg.cb.on_reg_state2) {
1720 pjsua_reg_info reg_info;
1721
1722 reg_info.cbparam = param;
1723 (*pjsua_var.ua_cfg.cb.on_reg_state2)(acc->index, &reg_info);
1724 }
1725
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001726 PJSUA_UNLOCK();
1727}
1728
1729
1730/*
1731 * Initialize client registration.
1732 */
1733static pj_status_t pjsua_regc_init(int acc_id)
1734{
1735 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001736 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001737 pj_status_t status;
1738
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001739 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001740 acc = &pjsua_var.acc[acc_id];
1741
1742 if (acc->cfg.reg_uri.slen == 0) {
1743 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1744 return PJ_SUCCESS;
1745 }
1746
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001747 /* Destroy existing session, if any */
1748 if (acc->regc) {
1749 pjsip_regc_destroy(acc->regc);
1750 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001751 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001752 }
1753
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001754 /* initialize SIP registration if registrar is configured */
1755
1756 status = pjsip_regc_create( pjsua_var.endpt,
1757 acc, &regc_cb, &acc->regc);
1758
1759 if (status != PJ_SUCCESS) {
1760 pjsua_perror(THIS_FILE, "Unable to create client registration",
1761 status);
1762 return status;
1763 }
1764
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001765 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001766
1767 if (acc->contact.slen == 0) {
1768 pj_str_t tmp_contact;
1769
1770 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1771 acc_id, &acc->cfg.reg_uri);
1772 if (status != PJ_SUCCESS) {
1773 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1774 " for registration",
1775 status);
1776 pjsip_regc_destroy(acc->regc);
1777 pj_pool_release(pool);
1778 acc->regc = NULL;
1779 return status;
1780 }
1781
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001782 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonob54719f2010-11-16 03:07:46 +00001783 update_regc_contact(acc);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001784 }
1785
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001786 status = pjsip_regc_init( acc->regc,
1787 &acc->cfg.reg_uri,
1788 &acc->cfg.id,
1789 &acc->cfg.id,
Benny Prijonob54719f2010-11-16 03:07:46 +00001790 1, &acc->reg_contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001791 acc->cfg.reg_timeout);
1792 if (status != PJ_SUCCESS) {
1793 pjsua_perror(THIS_FILE,
1794 "Client registration initialization error",
1795 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001796 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001797 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001798 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001799 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001800 return status;
1801 }
1802
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001803 /* If account is locked to specific transport, then set transport to
1804 * the client registration.
1805 */
1806 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1807 pjsip_tpselector tp_sel;
1808
1809 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1810 pjsip_regc_set_transport(acc->regc, &tp_sel);
1811 }
1812
1813
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001814 /* Set credentials
1815 */
1816 if (acc->cred_cnt) {
1817 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1818 }
1819
Benny Prijono48ab2b72007-11-08 09:24:30 +00001820 /* Set authentication preference */
1821 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1822
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001823 /* Set route-set
1824 */
Benny Prijono29c8ca32010-06-22 06:02:13 +00001825 if (acc->cfg.reg_use_proxy) {
1826 pjsip_route_hdr route_set;
1827 const pjsip_route_hdr *r;
1828
1829 pj_list_init(&route_set);
1830
1831 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) {
1832 r = pjsua_var.outbound_proxy.next;
1833 while (r != &pjsua_var.outbound_proxy) {
1834 pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r));
1835 r = r->next;
1836 }
1837 }
1838
1839 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY &&
1840 acc->cfg.proxy_cnt)
1841 {
1842 int cnt = acc->cfg.proxy_cnt;
1843 pjsip_route_hdr *pos = route_set.prev;
1844 int i;
1845
1846 r = acc->route_set.prev;
1847 for (i=0; i<cnt; ++i) {
1848 pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r));
1849 r = r->prev;
1850 }
1851 }
1852
1853 if (!pj_list_empty(&route_set))
1854 pjsip_regc_set_route_set( acc->regc, &route_set );
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001855 }
1856
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +00001857 /* Add custom request headers specified in the account config */
1858 pjsip_regc_add_headers(acc->regc, &acc->cfg.reg_hdr_list);
1859
Benny Prijono8fc6de02006-11-11 21:25:55 +00001860 /* Add other request headers. */
1861 if (pjsua_var.ua_cfg.user_agent.slen) {
1862 pjsip_hdr hdr_list;
1863 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1864 pjsip_generic_string_hdr *h;
1865
Benny Prijono8fc6de02006-11-11 21:25:55 +00001866 pj_list_init(&hdr_list);
1867
1868 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1869 &pjsua_var.ua_cfg.user_agent);
1870 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1871
1872 pjsip_regc_add_headers(acc->regc, &hdr_list);
1873 }
1874
Benny Prijonob54719f2010-11-16 03:07:46 +00001875 /* If SIP outbound is used, add "Supported: outbound, path header" */
1876 if (acc->rfc5626_status == OUTBOUND_WANTED) {
1877 pjsip_hdr hdr_list;
1878 pjsip_supported_hdr *hsup;
1879
1880 pj_list_init(&hdr_list);
1881 hsup = pjsip_supported_hdr_create(pool);
1882 pj_list_push_back(&hdr_list, hsup);
1883
1884 hsup->count = 2;
1885 hsup->values[0] = pj_str("outbound");
1886 hsup->values[1] = pj_str("path");
1887
1888 pjsip_regc_add_headers(acc->regc, &hdr_list);
1889 }
1890
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001891 pj_pool_release(pool);
1892
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001893 return PJ_SUCCESS;
1894}
1895
1896
1897/*
1898 * Update registration or perform unregistration.
1899 */
1900PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1901 pj_bool_t renew)
1902{
1903 pj_status_t status = 0;
1904 pjsip_tx_data *tdata = 0;
1905
1906 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1907 PJ_EINVAL);
1908 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1909
1910 PJSUA_LOCK();
1911
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001912 /* Cancel any re-registration timer */
1913 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1914
1915 /* Reset pointer to registration transport */
1916 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1917
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001918 if (renew) {
1919 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001920 status = pjsua_regc_init(acc_id);
1921 if (status != PJ_SUCCESS) {
1922 pjsua_perror(THIS_FILE, "Unable to create registration",
1923 status);
1924 goto on_return;
1925 }
1926 }
1927 if (!pjsua_var.acc[acc_id].regc) {
1928 status = PJ_EINVALIDOP;
1929 goto on_return;
1930 }
1931
1932 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1933 &tdata);
1934
Benny Prijono28f673a2007-10-15 07:04:59 +00001935 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1936 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1937 pjsip_authorization_hdr *h;
1938 char *uri;
1939 int d;
1940
1941 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1942 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1943 uri, acc->cfg.reg_uri.slen+10);
1944 pj_assert(d > 0);
1945
1946 h = pjsip_authorization_hdr_create(tdata->pool);
1947 h->scheme = pj_str("Digest");
1948 h->credential.digest.username = acc->cred[0].username;
1949 h->credential.digest.realm = acc->srv_domain;
1950 h->credential.digest.uri = pj_str(uri);
1951 h->credential.digest.algorithm = pj_str("md5");
1952
1953 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1954 }
1955
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001956 } else {
1957 if (pjsua_var.acc[acc_id].regc == NULL) {
1958 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1959 status = PJ_EINVALIDOP;
1960 goto on_return;
1961 }
Benny Prijono166d5022010-02-10 14:24:48 +00001962
1963 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
1964
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001965 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1966 }
1967
Benny Prijono56315612006-07-18 14:39:40 +00001968 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001969 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001970 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001971 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001972
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001973 /* Update pointer to registration transport */
1974 if (status == PJ_SUCCESS) {
1975 pjsip_regc_info reg_info;
1976
1977 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
1978 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
1979 }
1980
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001981 if (status != PJ_SUCCESS) {
1982 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1983 status);
1984 } else {
1985 PJ_LOG(3,(THIS_FILE, "%s sent",
1986 (renew? "Registration" : "Unregistration")));
1987 }
1988
1989on_return:
1990 PJSUA_UNLOCK();
1991 return status;
1992}
1993
1994
1995/*
1996 * Get account information.
1997 */
1998PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1999 pjsua_acc_info *info)
2000{
2001 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2002 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
2003
2004 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002005 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002006
Benny Prijonoac623b32006-07-03 15:19:31 +00002007 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002008
2009 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
2010 PJ_EINVAL);
2011 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
2012
2013 PJSUA_LOCK();
2014
2015 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
2016 PJSUA_UNLOCK();
2017 return PJ_EINVALIDOP;
2018 }
2019
2020 info->id = acc_id;
2021 info->is_default = (pjsua_var.default_acc == acc_id);
2022 info->acc_uri = acc_cfg->id;
2023 info->has_registration = (acc->cfg.reg_uri.slen > 0);
2024 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00002025 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
2026 if (info->rpid.note.slen)
2027 info->online_status_text = info->rpid.note;
2028 else if (info->online_status)
2029 info->online_status_text = pj_str("Online");
2030 else
2031 info->online_status_text = pj_str("Offline");
2032
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002033 if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00002034 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002035 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002036 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002037 if (acc->reg_last_err)
2038 info->reg_last_err = acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002039 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002040 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002041 info->status_text = pj_str("not registered");
2042 }
2043 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002044 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002045 info->status_text = pj_str("In Progress");
2046 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002047 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002048 info->status_text = pj_str("does not register");
2049 }
2050
2051 if (acc->regc) {
2052 pjsip_regc_info regc_info;
2053 pjsip_regc_get_info(acc->regc, &regc_info);
2054 info->expires = regc_info.next_reg;
2055 } else {
2056 info->expires = -1;
2057 }
2058
2059 PJSUA_UNLOCK();
2060
2061 return PJ_SUCCESS;
2062
2063}
2064
2065
2066/*
2067 * Enum accounts all account ids.
2068 */
2069PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
2070 unsigned *count )
2071{
2072 unsigned i, c;
2073
2074 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
2075
2076 PJSUA_LOCK();
2077
2078 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2079 if (!pjsua_var.acc[i].valid)
2080 continue;
2081 ids[c] = i;
2082 ++c;
2083 }
2084
2085 *count = c;
2086
2087 PJSUA_UNLOCK();
2088
2089 return PJ_SUCCESS;
2090}
2091
2092
2093/*
2094 * Enum accounts info.
2095 */
2096PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
2097 unsigned *count )
2098{
2099 unsigned i, c;
2100
2101 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
2102
2103 PJSUA_LOCK();
2104
2105 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2106 if (!pjsua_var.acc[i].valid)
2107 continue;
2108
2109 pjsua_acc_get_info(i, &info[c]);
2110 ++c;
2111 }
2112
2113 *count = c;
2114
2115 PJSUA_UNLOCK();
2116
2117 return PJ_SUCCESS;
2118}
2119
2120
2121/*
2122 * This is an internal function to find the most appropriate account to
2123 * used to reach to the specified URL.
2124 */
2125PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
2126{
2127 pj_str_t tmp;
2128 pjsip_uri *uri;
2129 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002130 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00002131 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002132
2133 PJSUA_LOCK();
2134
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002135 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002136
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002137 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002138
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002139 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002140 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002141 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002142 PJSUA_UNLOCK();
2143 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002144 }
2145
2146 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2147 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2148 {
2149 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00002150 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2151 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002152 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00002153 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002154 break;
2155 }
2156
Benny Prijono093d3022006-09-24 00:07:11 +00002157 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002158 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002159 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002160 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002161 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002162 }
2163
2164 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002165 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002166 PJSUA_UNLOCK();
2167 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002168 }
2169
Benny Prijonoa1e69682007-05-11 15:14:34 +00002170 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002171
Benny Prijonob4a17c92006-07-10 14:40:21 +00002172 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00002173 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2174 unsigned acc_id = pjsua_var.acc_ids[i];
2175 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
2176 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
2177 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002178 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002179 PJSUA_UNLOCK();
2180 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00002181 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002182 }
2183
Benny Prijonob4a17c92006-07-10 14:40:21 +00002184 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00002185 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2186 unsigned acc_id = pjsua_var.acc_ids[i];
2187 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
2188 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002189 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002190 PJSUA_UNLOCK();
2191 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00002192 }
2193 }
2194
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002195
Benny Prijono093d3022006-09-24 00:07:11 +00002196 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002197 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002198 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00002199 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002200}
2201
2202
2203/*
2204 * This is an internal function to find the most appropriate account to be
2205 * used to handle incoming calls.
2206 */
2207PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
2208{
2209 pjsip_uri *uri;
2210 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00002211 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002212
Benny Prijono371cf0a2007-06-19 00:35:37 +00002213 /* Check that there's at least one account configured */
2214 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
2215
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002216 uri = rdata->msg_info.to->uri;
2217
2218 /* Just return default account if To URI is not SIP: */
2219 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2220 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2221 {
2222 return pjsua_var.default_acc;
2223 }
2224
2225
2226 PJSUA_LOCK();
2227
2228 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
2229
2230 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00002231 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2232 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002233 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2234
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002235 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00002236 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002237 {
2238 /* Match ! */
2239 PJSUA_UNLOCK();
2240 return acc_id;
2241 }
2242 }
2243
Benny Prijono093d3022006-09-24 00:07:11 +00002244 /* No matching account, try match domain part only. */
2245 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2246 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002247 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2248
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002249 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002250 /* Match ! */
2251 PJSUA_UNLOCK();
2252 return acc_id;
2253 }
2254 }
2255
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002256 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00002257 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2258 unsigned acc_id = pjsua_var.acc_ids[i];
2259 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2260
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002261 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
2262
2263 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2264 pjsip_transport_type_e type;
2265 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2266 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
2267 type = PJSIP_TRANSPORT_UDP;
2268
2269 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
2270 continue;
2271 }
2272
Benny Prijono093d3022006-09-24 00:07:11 +00002273 /* Match ! */
2274 PJSUA_UNLOCK();
2275 return acc_id;
2276 }
2277 }
2278
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002279 /* Still no match, use default account */
2280 PJSUA_UNLOCK();
2281 return pjsua_var.default_acc;
2282}
2283
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002284
Benny Prijonofff245c2007-04-02 11:44:47 +00002285/*
2286 * Create arbitrary requests for this account.
2287 */
2288PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2289 const pjsip_method *method,
2290 const pj_str_t *target,
2291 pjsip_tx_data **p_tdata)
2292{
2293 pjsip_tx_data *tdata;
2294 pjsua_acc *acc;
2295 pjsip_route_hdr *r;
2296 pj_status_t status;
2297
2298 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2299 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2300
2301 acc = &pjsua_var.acc[acc_id];
2302
2303 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2304 &acc->cfg.id, target,
2305 NULL, NULL, -1, NULL, &tdata);
2306 if (status != PJ_SUCCESS) {
2307 pjsua_perror(THIS_FILE, "Unable to create request", status);
2308 return status;
2309 }
2310
2311 /* Copy routeset */
2312 r = acc->route_set.next;
2313 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002314 pjsip_msg_add_hdr(tdata->msg,
2315 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002316 r = r->next;
2317 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002318
2319 /* If account is locked to specific transport, then set that transport to
2320 * the transmit data.
2321 */
2322 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2323 pjsip_tpselector tp_sel;
2324
2325 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2326 pjsip_tx_data_set_transport(tdata, &tp_sel);
2327 }
2328
Benny Prijonofff245c2007-04-02 11:44:47 +00002329 /* Done */
2330 *p_tdata = tdata;
2331 return PJ_SUCCESS;
2332}
2333
2334
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002335PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2336 pj_str_t *contact,
2337 pjsua_acc_id acc_id,
2338 const pj_str_t *suri)
2339{
2340 pjsua_acc *acc;
2341 pjsip_sip_uri *sip_uri;
2342 pj_status_t status;
2343 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2344 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002345 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002346 unsigned flag;
2347 int secure;
2348 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002349 const char *beginquote, *endquote;
2350 char transport_param[32];
Benny Prijonob54719f2010-11-16 03:07:46 +00002351 const char *ob = ";ob";
Benny Prijonod0bd4982007-12-02 15:40:52 +00002352
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002353
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002354 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002355 acc = &pjsua_var.acc[acc_id];
2356
Benny Prijonof75eceb2007-03-23 19:09:54 +00002357 /* If force_contact is configured, then use use it */
2358 if (acc->cfg.force_contact.slen) {
2359 *contact = acc->cfg.force_contact;
2360 return PJ_SUCCESS;
2361 }
2362
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002363 /* If route-set is configured for the account, then URI is the
2364 * first entry of the route-set.
2365 */
2366 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002367 sip_uri = (pjsip_sip_uri*)
2368 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002369 } else {
2370 pj_str_t tmp;
2371 pjsip_uri *uri;
2372
2373 pj_strdup_with_null(pool, &tmp, suri);
2374
2375 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2376 if (uri == NULL)
2377 return PJSIP_EINVALIDURI;
2378
2379 /* For non-SIP scheme, route set should be configured */
2380 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
Benny Prijonoc7545782010-09-28 07:43:18 +00002381 return PJSIP_ENOROUTESET;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002382
Benny Prijono8c7a6172007-02-18 21:17:46 +00002383 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002384 }
2385
2386 /* Get transport type of the URI */
2387 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2388 tp_type = PJSIP_TRANSPORT_TLS;
2389 else if (sip_uri->transport_param.slen == 0) {
2390 tp_type = PJSIP_TRANSPORT_UDP;
2391 } else
2392 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2393
2394 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2395 return PJSIP_EUNSUPTRANSPORT;
2396
Benny Prijonod0bd4982007-12-02 15:40:52 +00002397 /* If destination URI specifies IPv6, then set transport type
2398 * to use IPv6 as well.
2399 */
Benny Prijono19b29372007-12-05 04:08:40 +00002400 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002401 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2402
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002403 flag = pjsip_transport_get_flag_from_type(tp_type);
2404 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2405
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002406 /* Init transport selector. */
2407 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2408
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002409 /* Get local address suitable to send request from */
2410 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002411 pool, tp_type, &tp_sel,
2412 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002413 if (status != PJ_SUCCESS)
2414 return status;
2415
Benny Prijonod0bd4982007-12-02 15:40:52 +00002416 /* Enclose IPv6 address in square brackets */
2417 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2418 beginquote = "[";
2419 endquote = "]";
2420 } else {
2421 beginquote = endquote = "";
2422 }
2423
2424 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002425 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002426 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2427 ";transport=%s",
2428 pjsip_transport_get_type_name(tp_type));
2429 } else {
2430 transport_param[0] = '\0';
2431 }
2432
2433
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002434 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002435 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002436 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00002437 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s%s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002438 (int)acc->display.slen,
2439 acc->display.ptr,
2440 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002441 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002442 (int)acc->user_part.slen,
2443 acc->user_part.ptr,
2444 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002445 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002446 (int)local_addr.slen,
2447 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002448 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002449 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002450 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002451 (int)acc->cfg.contact_uri_params.slen,
2452 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00002453 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00002454 (int)acc->cfg.contact_params.slen,
2455 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002456
2457 return PJ_SUCCESS;
2458}
2459
2460
2461
2462PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2463 pj_str_t *contact,
2464 pjsua_acc_id acc_id,
2465 pjsip_rx_data *rdata )
2466{
2467 /*
2468 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2469 * If the request that initiated the dialog contained a SIPS URI
2470 * in the Request-URI or in the top Record-Route header field value,
2471 * if there was any, or the Contact header field if there was no
2472 * Record-Route header field, the Contact header field in the response
2473 * MUST be a SIPS URI.
2474 */
2475 pjsua_acc *acc;
2476 pjsip_sip_uri *sip_uri;
2477 pj_status_t status;
2478 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2479 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002480 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002481 unsigned flag;
2482 int secure;
2483 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002484 const char *beginquote, *endquote;
2485 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002486
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002487 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002488 acc = &pjsua_var.acc[acc_id];
2489
Benny Prijonof75eceb2007-03-23 19:09:54 +00002490 /* If force_contact is configured, then use use it */
2491 if (acc->cfg.force_contact.slen) {
2492 *contact = acc->cfg.force_contact;
2493 return PJ_SUCCESS;
2494 }
2495
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002496 /* If Record-Route is present, then URI is the top Record-Route. */
2497 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002498 sip_uri = (pjsip_sip_uri*)
2499 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002500 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002501 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002502 pjsip_contact_hdr *h_contact;
2503 pjsip_uri *uri = NULL;
2504
Benny Prijonoa330d452008-08-05 20:14:39 +00002505 /* Otherwise URI is Contact URI.
2506 * Iterate the Contact URI until we find sip: or sips: scheme.
2507 */
2508 do {
2509 h_contact = (pjsip_contact_hdr*)
2510 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2511 pos);
2512 if (h_contact) {
Benny Prijono8b33bba2010-06-02 03:03:43 +00002513 if (h_contact->uri)
2514 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2515 else
2516 uri = NULL;
2517 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2518 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
Benny Prijonoa330d452008-08-05 20:14:39 +00002519 {
2520 pos = (pjsip_hdr*)h_contact->next;
2521 if (pos == &rdata->msg_info.msg->hdr)
2522 h_contact = NULL;
2523 } else {
2524 break;
2525 }
2526 }
2527 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002528
2529
2530 /* Or if Contact URI is not present, take the remote URI from
2531 * the From URI.
2532 */
2533 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002534 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002535
2536
2537 /* Can only do sip/sips scheme at present. */
2538 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2539 return PJSIP_EINVALIDREQURI;
2540
Benny Prijono8c7a6172007-02-18 21:17:46 +00002541 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002542 }
2543
2544 /* Get transport type of the URI */
2545 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2546 tp_type = PJSIP_TRANSPORT_TLS;
2547 else if (sip_uri->transport_param.slen == 0) {
2548 tp_type = PJSIP_TRANSPORT_UDP;
2549 } else
2550 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002551
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002552 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2553 return PJSIP_EUNSUPTRANSPORT;
2554
Benny Prijonod0bd4982007-12-02 15:40:52 +00002555 /* If destination URI specifies IPv6, then set transport type
2556 * to use IPv6 as well.
2557 */
2558 if (pj_strchr(&sip_uri->host, ':'))
2559 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2560
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002561 flag = pjsip_transport_get_flag_from_type(tp_type);
2562 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2563
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002564 /* Init transport selector. */
2565 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2566
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002567 /* Get local address suitable to send request from */
2568 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002569 pool, tp_type, &tp_sel,
2570 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002571 if (status != PJ_SUCCESS)
2572 return status;
2573
Benny Prijonod0bd4982007-12-02 15:40:52 +00002574 /* Enclose IPv6 address in square brackets */
2575 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2576 beginquote = "[";
2577 endquote = "]";
2578 } else {
2579 beginquote = endquote = "";
2580 }
2581
2582 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002583 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002584 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2585 ";transport=%s",
2586 pjsip_transport_get_type_name(tp_type));
2587 } else {
2588 transport_param[0] = '\0';
2589 }
2590
2591
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002592 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002593 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002594 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002595 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002596 (int)acc->display.slen,
2597 acc->display.ptr,
2598 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002599 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002600 (int)acc->user_part.slen,
2601 acc->user_part.ptr,
2602 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002603 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002604 (int)local_addr.slen,
2605 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002606 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002607 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002608 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002609 (int)acc->cfg.contact_uri_params.slen,
2610 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002611 (int)acc->cfg.contact_params.slen,
2612 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002613
2614 return PJ_SUCCESS;
2615}
2616
2617
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002618PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2619 pjsua_transport_id tp_id)
2620{
2621 pjsua_acc *acc;
2622
2623 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2624 acc = &pjsua_var.acc[acc_id];
2625
Benny Prijonoa1e69682007-05-11 15:14:34 +00002626 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002627 PJ_EINVAL);
2628
2629 acc->cfg.transport_id = tp_id;
2630
2631 return PJ_SUCCESS;
2632}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002633
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002634
2635/* Auto re-registration timeout callback */
2636static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2637{
2638 pjsua_acc *acc;
2639 pj_status_t status;
2640
2641 PJ_UNUSED_ARG(th);
2642 acc = (pjsua_acc*) te->user_data;
2643 pj_assert(acc);
2644
2645 PJSUA_LOCK();
2646
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002647 /* Check if the reregistration timer is still valid, e.g: while waiting
2648 * timeout timer application might have deleted the account or disabled
2649 * the auto-reregistration.
2650 */
2651 if (!acc->valid || !acc->auto_rereg.active ||
2652 acc->cfg.reg_retry_interval == 0)
2653 {
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002654 goto on_return;
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002655 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002656
2657 /* Start re-registration */
2658 acc->auto_rereg.attempt_cnt++;
2659 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2660 if (status != PJ_SUCCESS)
2661 schedule_reregistration(acc);
2662
Nanang Izzuddin66580002010-04-14 08:12:08 +00002663on_return:
2664 PJSUA_UNLOCK();
2665}
2666
2667
2668/* Schedule reregistration for specified account. Note that the first
2669 * re-registration after a registration failure will be done immediately.
2670 * Also note that this function should be called within PJSUA mutex.
2671 */
2672static void schedule_reregistration(pjsua_acc *acc)
2673{
2674 pj_time_val delay;
2675
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002676 pj_assert(acc);
2677
2678 /* Validate the account and re-registration feature status */
2679 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
2680 return;
2681 }
Nanang Izzuddin66580002010-04-14 08:12:08 +00002682
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002683 /* If configured, disconnect calls of this account after the first
2684 * reregistration attempt failed.
2685 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002686 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002687 {
2688 unsigned i, cnt;
2689
2690 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2691 if (pjsua_var.calls[i].acc_id == acc->index) {
2692 pjsua_call_hangup(i, 0, NULL, NULL);
2693 ++cnt;
2694 }
2695 }
2696
2697 if (cnt) {
2698 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2699 "after reregistration attempt failed",
2700 cnt, acc->index));
2701 }
2702 }
2703
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002704 /* Cancel any re-registration timer */
2705 pjsua_cancel_timer(&acc->auto_rereg.timer);
2706
2707 /* Update re-registration flag */
2708 acc->auto_rereg.active = PJ_TRUE;
2709
2710 /* Set up timer for reregistration */
2711 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2712 acc->auto_rereg.timer.user_data = acc;
2713
2714 /* Reregistration attempt. The first attempt will be done immediately. */
2715 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2716 delay.msec = 0;
2717 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2718}
2719
2720
2721/* Internal function to perform auto-reregistration on transport
2722 * connection/disconnection events.
2723 */
2724void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2725 pjsip_transport_state state,
2726 const pjsip_transport_state_info *info)
2727{
2728 unsigned i;
2729
2730 PJ_UNUSED_ARG(info);
2731
2732 /* Only care for transport disconnection events */
2733 if (state != PJSIP_TP_STATE_DISCONNECTED)
2734 return;
2735
2736 /* Shutdown this transport, to make sure that the transport manager
2737 * will create a new transport for reconnection.
2738 */
2739 pjsip_transport_shutdown(tp);
2740
2741 PJSUA_LOCK();
2742
2743 /* Enumerate accounts using this transport and perform actions
2744 * based on the transport state.
2745 */
2746 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2747 pjsua_acc *acc = &pjsua_var.acc[i];
2748
2749 /* Skip if this account is not valid OR auto re-registration
2750 * feature is disabled OR this transport is not used by this account.
2751 */
2752 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2753 tp != acc->auto_rereg.reg_tp)
2754 {
2755 continue;
2756 }
2757
2758 /* Schedule reregistration for this account */
2759 schedule_reregistration(acc);
2760 }
2761
2762 PJSUA_UNLOCK();
2763}