blob: 98fe5cd8738a4205184c1d4b4b0f130fed9fd5f1 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 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;
Sauw Ming9b80d512011-03-15 03:20:37 +0000102 dst->reg_delay_before_refresh = src->reg_delay_before_refresh;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103 dst->cred_count = src->cred_count;
104
105 for (i=0; i<src->cred_count; ++i) {
106 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
107 }
Benny Prijonobddef2c2007-10-31 13:28:08 +0000108
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +0000109 pj_list_init(&dst->reg_hdr_list);
110 if (!pj_list_empty(&src->reg_hdr_list)) {
111 const pjsip_hdr *hdr;
112
113 hdr = src->reg_hdr_list.next;
114 while (hdr != &src->reg_hdr_list) {
115 pj_list_push_back(&dst->reg_hdr_list, pjsip_hdr_clone(pool, hdr));
116 hdr = hdr->next;
117 }
118 }
119
Benny Prijono639873e2011-03-23 03:46:26 +0000120 pj_list_init(&dst->sub_hdr_list);
121 if (!pj_list_empty(&src->sub_hdr_list)) {
122 const pjsip_hdr *hdr;
123
124 hdr = src->sub_hdr_list.next;
125 while (hdr != &src->sub_hdr_list) {
126 pj_list_push_back(&dst->sub_hdr_list, pjsip_hdr_clone(pool, hdr));
127 hdr = hdr->next;
128 }
129 }
130
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +0000131 pjsip_auth_clt_pref_dup(pool, &dst->auth_pref, &src->auth_pref);
132
Benny Prijono0bc99a92011-03-17 04:34:43 +0000133 pjsua_transport_config_dup(pool, &dst->rtp_cfg, &src->rtp_cfg);
134
Benny Prijonobddef2c2007-10-31 13:28:08 +0000135 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000136}
137
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000138/*
139 * Calculate CRC of proxy list.
140 */
141static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt)
142{
143 pj_crc32_context ctx;
144 unsigned i;
145
146 pj_crc32_init(&ctx);
147 for (i=0; i<cnt; ++i) {
148 pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen);
149 }
150
151 return pj_crc32_final(&ctx);
152}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000153
154/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000155 * Initialize a new account (after configuration is set).
156 */
157static pj_status_t initialize_acc(unsigned acc_id)
158{
159 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
160 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000161 pjsip_name_addr *name_addr;
Benny Prijonoc7545782010-09-28 07:43:18 +0000162 pjsip_sip_uri *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000163 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000164 unsigned i;
165
166 /* Need to parse local_uri to get the elements: */
167
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000168 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000169 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000170 acc_cfg->id.slen,
171 PJSIP_PARSE_URI_AS_NAMEADDR);
172 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173 pjsua_perror(THIS_FILE, "Invalid local URI",
174 PJSIP_EINVALIDURI);
175 return PJSIP_EINVALIDURI;
176 }
177
178 /* Local URI MUST be a SIP or SIPS: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000179 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
180 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181 {
Benny Prijonoc7545782010-09-28 07:43:18 +0000182 acc->display = name_addr->display;
183 acc->user_part = name_addr->display;
184 acc->srv_domain = pj_str("");
185 acc->srv_port = 0;
186 } else {
187 pjsip_sip_uri *sip_uri;
188
189 /* Get the SIP URI object: */
190 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
191
192 /* Save the user and domain part. These will be used when finding an
193 * account for incoming requests.
194 */
195 acc->display = name_addr->display;
196 acc->user_part = sip_uri->user;
197 acc->srv_domain = sip_uri->host;
198 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 }
200
201
Benny Prijonob4a17c92006-07-10 14:40:21 +0000202 /* Parse registrar URI, if any */
203 if (acc_cfg->reg_uri.slen) {
204 pjsip_uri *reg_uri;
205
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000206 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000207 acc_cfg->reg_uri.slen, 0);
208 if (reg_uri == NULL) {
209 pjsua_perror(THIS_FILE, "Invalid registrar URI",
210 PJSIP_EINVALIDURI);
211 return PJSIP_EINVALIDURI;
212 }
213
214 /* Registrar URI MUST be a SIP or SIPS: */
215 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
216 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
217 {
218 pjsua_perror(THIS_FILE, "Invalid registar URI",
219 PJSIP_EINVALIDSCHEME);
220 return PJSIP_EINVALIDSCHEME;
221 }
222
223 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
224
225 } else {
226 sip_reg_uri = NULL;
227 }
228
Benny Prijonob4a17c92006-07-10 14:40:21 +0000229 if (sip_reg_uri) {
230 acc->srv_port = sip_reg_uri->port;
231 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000232
233 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000234 //if (acc_cfg->contact.slen == 0) {
235 // acc_cfg->contact = acc_cfg->id;
236 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000238 /* Build account route-set from outbound proxies and route set from
239 * account configuration.
240 */
241 pj_list_init(&acc->route_set);
242
Benny Prijono29c8ca32010-06-22 06:02:13 +0000243 if (!pj_list_empty(&pjsua_var.outbound_proxy)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244 pjsip_route_hdr *r;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000245
Benny Prijono29c8ca32010-06-22 06:02:13 +0000246 r = pjsua_var.outbound_proxy.next;
247 while (r != &pjsua_var.outbound_proxy) {
248 pj_list_push_back(&acc->route_set,
249 pjsip_hdr_shallow_clone(acc->pool, r));
250 r = r->next;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000251 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252 }
253
254 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
255 pj_str_t hname = { "Route", 5};
256 pjsip_route_hdr *r;
257 pj_str_t tmp;
258
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000259 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000260 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000261 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000262 if (r == NULL) {
263 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
264 PJ_EINVAL);
265 return PJ_EINVAL;
266 }
267 pj_list_push_back(&acc->route_set, r);
268 }
269
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270 /* Concatenate credentials from account config and global config */
271 acc->cred_cnt = 0;
272 for (i=0; i<acc_cfg->cred_count; ++i) {
273 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
274 }
275 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
276 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
277 {
278 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
279 }
280
Benny Prijono07fe2302010-06-24 12:33:18 +0000281 /* If ICE is enabled, add "+sip.ice" media feature tag in account's
282 * contact params.
283 */
284#if PJSUA_ADD_ICE_TAGS
285 if (pjsua_var.media_cfg.enable_ice) {
286 unsigned new_len;
287 pj_str_t new_prm;
288
289 new_len = acc_cfg->contact_params.slen + 10;
290 new_prm.ptr = (char*)pj_pool_alloc(acc->pool, new_len);
291 pj_strcpy(&new_prm, &acc_cfg->contact_params);
292 pj_strcat2(&new_prm, ";+sip.ice");
293 acc_cfg->contact_params = new_prm;
294 }
295#endif
296
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000297 status = pjsua_pres_init_acc(acc_id);
298 if (status != PJ_SUCCESS)
299 return status;
300
Benny Prijonob54719f2010-11-16 03:07:46 +0000301 /* If SIP outbound is enabled, generate instance and reg ID if they are
302 * not specified
303 */
304 if (acc_cfg->use_rfc5626) {
305 if (acc_cfg->rfc5626_instance_id.slen==0) {
306 const pj_str_t *hostname;
307 pj_uint32_t hval, pos;
308 char instprm[] = ";+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000CCDDEEFF>\"";
309
310 hostname = pj_gethostname();
311 pos = pj_ansi_strlen(instprm) - 10;
312 hval = pj_hash_calc(0, hostname->ptr, hostname->slen);
313 pj_val_to_hex_digit( ((char*)&hval)[0], instprm+pos+0);
314 pj_val_to_hex_digit( ((char*)&hval)[1], instprm+pos+2);
315 pj_val_to_hex_digit( ((char*)&hval)[2], instprm+pos+4);
316 pj_val_to_hex_digit( ((char*)&hval)[3], instprm+pos+6);
317
318 pj_strdup2(acc->pool, &acc->rfc5626_instprm, instprm);
319 } else {
320 const char *prmname = ";+sip.instance=\"";
321 unsigned len;
322
323 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_instance_id.slen + 1;
324 acc->rfc5626_instprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
325 pj_ansi_snprintf(acc->rfc5626_instprm.ptr, len+1,
326 "%s%.*s\"",
327 prmname,
328 (int)acc_cfg->rfc5626_instance_id.slen,
329 acc_cfg->rfc5626_instance_id.ptr);
330 acc->rfc5626_instprm.slen = len;
331 }
332
333 if (acc_cfg->rfc5626_reg_id.slen==0) {
334 acc->rfc5626_regprm = pj_str(";reg-id=1");
335 } else {
336 const char *prmname = ";reg-id=";
337 unsigned len;
338
339 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_reg_id.slen;
340 acc->rfc5626_regprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
341 pj_ansi_snprintf(acc->rfc5626_regprm.ptr, len+1,
342 "%s%.*s\"",
343 prmname,
344 (int)acc_cfg->rfc5626_reg_id.slen,
345 acc_cfg->rfc5626_reg_id.ptr);
346 acc->rfc5626_regprm.slen = len;
347 }
348 }
349
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000350 /* Mark account as valid */
351 pjsua_var.acc[acc_id].valid = PJ_TRUE;
352
Benny Prijono093d3022006-09-24 00:07:11 +0000353 /* Insert account ID into account ID array, sorted by priority */
354 for (i=0; i<pjsua_var.acc_cnt; ++i) {
355 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
356 pjsua_var.acc[acc_id].cfg.priority)
357 {
358 break;
359 }
360 }
361 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
362 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363
364 return PJ_SUCCESS;
365}
366
367
368/*
369 * Add a new account to pjsua.
370 */
371PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
372 pj_bool_t is_default,
373 pjsua_acc_id *p_acc_id)
374{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000375 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000376 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377 pj_status_t status;
378
379 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
380 PJ_ETOOMANY);
381
382 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000383 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000385 /* Verify media count */
386#if !defined(PJMEDIA_HAS_VIDEO) || (PJMEDIA_HAS_VIDEO == 0)
387 PJ_ASSERT_RETURN(cfg->max_video_cnt == 0, PJ_EINVAL);
388#endif
389 PJ_ASSERT_RETURN(cfg->max_audio_cnt + cfg->max_video_cnt <=
390 PJSUA_MAX_CALL_MEDIA, PJ_ETOOMANY);
391
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392 PJSUA_LOCK();
393
394 /* Find empty account id. */
395 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
396 if (pjsua_var.acc[id].valid == PJ_FALSE)
397 break;
398 }
399
400 /* Expect to find a slot */
401 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
402 {PJSUA_UNLOCK(); return PJ_EBUG;});
403
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000404 acc = &pjsua_var.acc[id];
405
406 /* Create pool for this account. */
407 if (acc->pool)
408 pj_pool_reset(acc->pool);
409 else
410 acc->pool = pjsua_pool_create("acc%p", 512, 256);
411
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000412 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000413 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000414
Sauw Ming9b80d512011-03-15 03:20:37 +0000415 /* Normalize registration timeout and refresh delay */
416 if (pjsua_var.acc[id].cfg.reg_uri.slen) {
417 if (pjsua_var.acc[id].cfg.reg_timeout == 0) {
418 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
419 }
420 if (pjsua_var.acc[id].cfg.reg_delay_before_refresh == 0) {
421 pjsua_var.acc[id].cfg.reg_delay_before_refresh =
422 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
423 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000424 }
425
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000426 /* Get CRC of account proxy setting */
427 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
428
429 /* Get CRC of global outbound proxy setting */
430 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
431 pjsua_var.ua_cfg.outbound_proxy_cnt);
432
Benny Prijono91d06b62008-09-20 12:16:56 +0000433 /* Check the route URI's and force loose route if required */
434 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
435 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
436 if (status != PJ_SUCCESS)
437 return status;
438 }
439
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000440 status = initialize_acc(id);
441 if (status != PJ_SUCCESS) {
442 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000443 pj_pool_release(acc->pool);
444 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 PJSUA_UNLOCK();
446 return status;
447 }
448
449 if (is_default)
450 pjsua_var.default_acc = id;
451
452 if (p_acc_id)
453 *p_acc_id = id;
454
455 pjsua_var.acc_cnt++;
456
457 PJSUA_UNLOCK();
458
459 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
460 (int)cfg->id.slen, cfg->id.ptr, id));
461
462 /* If accounts has registration enabled, start registration */
463 if (pjsua_var.acc[id].cfg.reg_uri.slen)
464 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000465 else {
466 /* Otherwise subscribe to MWI, if it's enabled */
467 if (pjsua_var.acc[id].cfg.mwi_enabled)
468 pjsua_start_mwi(&pjsua_var.acc[id]);
469 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000470
471 return PJ_SUCCESS;
472}
473
474
475/*
476 * Add local account
477 */
478PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
479 pj_bool_t is_default,
480 pjsua_acc_id *p_acc_id)
481{
482 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000483 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000484 const char *beginquote, *endquote;
485 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000486 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487
Benny Prijonoe93e2872006-06-28 16:46:49 +0000488 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000489 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
490 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000491
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000493 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000494
495 pjsua_acc_config_default(&cfg);
496
Benny Prijono093d3022006-09-24 00:07:11 +0000497 /* Lower the priority of local account */
498 --cfg.priority;
499
Benny Prijonod0bd4982007-12-02 15:40:52 +0000500 /* Enclose IPv6 address in square brackets */
501 if (t->type & PJSIP_TRANSPORT_IPV6) {
502 beginquote = "[";
503 endquote = "]";
504 } else {
505 beginquote = endquote = "";
506 }
507
508 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000509 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000510 pj_ansi_snprintf(transport_param, sizeof(transport_param),
511 ";transport=%s",
512 pjsip_transport_get_type_name(t->type));
513 } else {
514 transport_param[0] = '\0';
515 }
516
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000517 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000518 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000519 "<sip:%s%.*s%s:%d%s>",
520 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000521 (int)t->local_name.host.slen,
522 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000523 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000524 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000525 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000526
527 cfg.id = pj_str(uri);
528
529 return pjsua_acc_add(&cfg, is_default, p_acc_id);
530}
531
532
533/*
Benny Prijono705e7842008-07-21 18:12:51 +0000534 * Set arbitrary data to be associated with the account.
535 */
536PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
537 void *user_data)
538{
539 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
540 PJ_EINVAL);
541 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
542
543 PJSUA_LOCK();
544
545 pjsua_var.acc[acc_id].cfg.user_data = user_data;
546
547 PJSUA_UNLOCK();
548
549 return PJ_SUCCESS;
550}
551
552
553/*
554 * Retrieve arbitrary data associated with the account.
555 */
556PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
557{
558 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
559 NULL);
560 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
561
562 return pjsua_var.acc[acc_id].cfg.user_data;
563}
564
565
566/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000567 * Delete account.
568 */
569PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
570{
Benny Prijono093d3022006-09-24 00:07:11 +0000571 unsigned i;
572
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000573 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
574 PJ_EINVAL);
575 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
576
577 PJSUA_LOCK();
578
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000579 /* Cancel any re-registration timer */
580 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
581
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000582 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000583 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000584 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000585 if (pjsua_var.acc[acc_id].regc) {
586 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
587 }
Benny Prijono922933b2007-01-21 16:23:56 +0000588 pjsua_var.acc[acc_id].regc = NULL;
589 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000590
591 /* Delete server presence subscription */
592 pjsua_pres_delete_acc(acc_id);
593
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000594 /* Release account pool */
595 if (pjsua_var.acc[acc_id].pool) {
596 pj_pool_release(pjsua_var.acc[acc_id].pool);
597 pjsua_var.acc[acc_id].pool = NULL;
598 }
599
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000600 /* Invalidate */
601 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000602 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000603
Benny Prijono093d3022006-09-24 00:07:11 +0000604 /* Remove from array */
605 for (i=0; i<pjsua_var.acc_cnt; ++i) {
606 if (pjsua_var.acc_ids[i] == acc_id)
607 break;
608 }
609 if (i != pjsua_var.acc_cnt) {
610 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
611 pjsua_var.acc_cnt, i);
612 --pjsua_var.acc_cnt;
613 }
614
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000615 /* Leave the calls intact, as I don't think calls need to
616 * access account once it's created
617 */
618
Benny Prijonofb2b3652007-06-28 07:15:03 +0000619 /* Update default account */
620 if (pjsua_var.default_acc == acc_id)
621 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000622
623 PJSUA_UNLOCK();
624
625 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
626
627 return PJ_SUCCESS;
628}
629
630
631/*
632 * Modify account information.
633 */
634PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
635 const pjsua_acc_config *cfg)
636{
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000637 pjsua_acc *acc;
638 pjsip_name_addr *id_name_addr = NULL;
639 pjsip_sip_uri *id_sip_uri = NULL;
640 pjsip_sip_uri *reg_sip_uri = NULL;
641 pj_uint32_t local_route_crc, global_route_crc;
642 pjsip_route_hdr global_route;
643 pjsip_route_hdr local_route;
644 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
645 pj_bool_t update_reg = PJ_FALSE;
646 pj_status_t status = PJ_SUCCESS;
647
648 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
649 PJ_EINVAL);
650
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000651 /* Verify media count */
652#if !defined(PJMEDIA_HAS_VIDEO) || (PJMEDIA_HAS_VIDEO == 0)
653 PJ_ASSERT_RETURN(cfg->max_video_cnt == 0, PJ_EINVAL);
654#endif
655 PJ_ASSERT_RETURN(cfg->max_audio_cnt + cfg->max_video_cnt <=
656 PJSUA_MAX_CALL_MEDIA, PJ_ETOOMANY);
657
658
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000659 PJSUA_LOCK();
660
661 acc = &pjsua_var.acc[acc_id];
662 if (!acc->valid) {
663 status = PJ_EINVAL;
664 goto on_return;
665 }
666
667 /* == Validate first == */
668
669 /* Account id */
670 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
671 /* Need to parse id to get the elements: */
672 id_name_addr = (pjsip_name_addr*)
673 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
674 PJSIP_PARSE_URI_AS_NAMEADDR);
675 if (id_name_addr == NULL) {
676 status = PJSIP_EINVALIDURI;
677 pjsua_perror(THIS_FILE, "Invalid local URI", status);
678 goto on_return;
679 }
680
681 /* URI MUST be a SIP or SIPS: */
682 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
683 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
684 {
685 status = PJSIP_EINVALIDSCHEME;
686 pjsua_perror(THIS_FILE, "Invalid local URI", status);
687 goto on_return;
688 }
689
690 /* Get the SIP URI object: */
691 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
692 }
693
694 /* Registrar URI */
695 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
696 pjsip_uri *reg_uri;
697
698 /* Need to parse reg_uri to get the elements: */
699 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
700 cfg->reg_uri.slen, 0);
701 if (reg_uri == NULL) {
702 status = PJSIP_EINVALIDURI;
703 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
704 goto on_return;
705 }
706
707 /* Registrar URI MUST be a SIP or SIPS: */
708 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
709 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
710 {
711 status = PJSIP_EINVALIDSCHEME;
712 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
713 goto on_return;
714 }
715
716 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
717 }
718
719 /* Global outbound proxy */
720 global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
721 pjsua_var.ua_cfg.outbound_proxy_cnt);
722 if (global_route_crc != acc->global_route_crc) {
723 pjsip_route_hdr *r;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000724
Benny Prijono29c8ca32010-06-22 06:02:13 +0000725 /* Copy from global outbound proxies */
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000726 pj_list_init(&global_route);
Benny Prijono29c8ca32010-06-22 06:02:13 +0000727 r = pjsua_var.outbound_proxy.next;
728 while (r != &pjsua_var.outbound_proxy) {
729 pj_list_push_back(&global_route,
730 pjsip_hdr_shallow_clone(acc->pool, r));
731 r = r->next;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000732 }
733 }
734
735 /* Account proxy */
736 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
737 if (local_route_crc != acc->local_route_crc) {
738 pjsip_route_hdr *r;
739 unsigned i;
740
741 /* Validate the local route and save it to temporary var */
742 pj_list_init(&local_route);
743 for (i=0; i<cfg->proxy_cnt; ++i) {
744 pj_str_t hname = { "Route", 5};
745
746 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
747 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
748 if (status != PJ_SUCCESS)
749 goto on_return;
750 r = (pjsip_route_hdr*)
751 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
752 acc_proxy[i].slen, NULL);
753 if (r == NULL) {
754 status = PJSIP_EINVALIDURI;
755 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
756 status);
757 goto on_return;
758 }
759
760 pj_list_push_back(&local_route, r);
761 }
762 }
763
764
765 /* == Apply the new config == */
766
767 /* Account ID. */
768 if (id_name_addr && id_sip_uri) {
769 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
Benny Prijonof0ba2dc2011-04-14 15:38:23 +0000770 pj_strdup_with_null(acc->pool, &acc->display, &id_name_addr->display);
771 pj_strdup_with_null(acc->pool, &acc->user_part, &id_sip_uri->user);
772 pj_strdup_with_null(acc->pool, &acc->srv_domain, &id_sip_uri->host);
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000773 acc->srv_port = 0;
774 update_reg = PJ_TRUE;
775 }
776
777 /* User data */
778 acc->cfg.user_data = cfg->user_data;
779
780 /* Priority */
781 if (acc->cfg.priority != cfg->priority) {
782 unsigned i;
783
784 acc->cfg.priority = cfg->priority;
785
786 /* Resort accounts priority */
787 for (i=0; i<pjsua_var.acc_cnt; ++i) {
788 if (pjsua_var.acc_ids[i] == acc_id)
789 break;
790 }
791 pj_assert(i < pjsua_var.acc_cnt);
792 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
793 pjsua_var.acc_cnt, i);
794 for (i=0; i<pjsua_var.acc_cnt; ++i) {
795 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
796 acc->cfg.priority)
797 {
798 break;
799 }
800 }
801 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
802 pjsua_var.acc_cnt, i, &acc_id);
803 }
804
805 /* MWI */
806 acc->cfg.mwi_enabled = cfg->mwi_enabled;
807
808 /* PIDF tuple ID */
809 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
810 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
811 &cfg->pidf_tuple_id);
812
813 /* Publish */
814 acc->cfg.publish_opt = cfg->publish_opt;
815 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
816 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
817 acc->cfg.publish_enabled = cfg->publish_enabled;
818 if (!acc->cfg.publish_enabled)
819 pjsua_pres_unpublish(acc);
820 else
821 update_reg = PJ_TRUE;
822 }
823
824 /* Force contact URI */
825 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
826 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
827 &cfg->force_contact);
828 update_reg = PJ_TRUE;
829 }
830
831 /* Contact param */
832 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
833 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
834 &cfg->contact_params);
835 update_reg = PJ_TRUE;
836 }
837
838 /* Contact URI params */
839 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
840 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
841 &cfg->contact_uri_params);
842 update_reg = PJ_TRUE;
843 }
844
845 /* Reliable provisional response */
846 acc->cfg.require_100rel = cfg->require_100rel;
847
848 /* Session timer */
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000849 acc->cfg.use_timer = cfg->use_timer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000850 acc->cfg.timer_setting = cfg->timer_setting;
851
852 /* Transport and keep-alive */
853 if (acc->cfg.transport_id != cfg->transport_id) {
854 acc->cfg.transport_id = cfg->transport_id;
855 update_reg = PJ_TRUE;
856 }
857 acc->cfg.ka_interval = cfg->ka_interval;
858 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
859 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
860#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
861 acc->cfg.use_srtp = cfg->use_srtp;
862 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000863 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000864#endif
865
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000866#if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
867 acc->cfg.use_stream_ka = cfg->use_stream_ka;
868#endif
869
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000870 /* Global outbound proxy */
871 if (global_route_crc != acc->global_route_crc) {
872 unsigned i, rcnt;
873
874 /* Remove the outbound proxies from the route set */
875 rcnt = pj_list_size(&acc->route_set);
876 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
877 pjsip_route_hdr *r = acc->route_set.next;
878 pj_list_erase(r);
879 }
880
881 /* Insert the outbound proxies to the beginning of route set */
882 pj_list_merge_first(&acc->route_set, &global_route);
883
884 /* Update global route CRC */
885 acc->global_route_crc = global_route_crc;
886
887 update_reg = PJ_TRUE;
888 }
889
890 /* Account proxy */
891 if (local_route_crc != acc->local_route_crc) {
892 unsigned i;
893
894 /* Remove the current account proxies from the route set */
895 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
896 pjsip_route_hdr *r = acc->route_set.prev;
897 pj_list_erase(r);
898 }
899
900 /* Insert new proxy setting to the route set */
901 pj_list_merge_last(&acc->route_set, &local_route);
902
903 /* Update the proxy setting */
904 acc->cfg.proxy_cnt = cfg->proxy_cnt;
905 for (i = 0; i < cfg->proxy_cnt; ++i)
906 acc->cfg.proxy[i] = acc_proxy[i];
907
908 /* Update local route CRC */
909 acc->local_route_crc = local_route_crc;
910
911 update_reg = PJ_TRUE;
912 }
913
914 /* Credential info */
915 {
916 unsigned i;
917
918 /* Selective update credential info. */
919 for (i = 0; i < cfg->cred_count; ++i) {
920 unsigned j;
921 pjsip_cred_info ci;
922
923 /* Find if this credential is already listed */
Nanang Izzuddin7f8d76f2011-03-29 03:47:16 +0000924 for (j = i; j < acc->cfg.cred_count; ++j) {
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000925 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
926 &cfg->cred_info[i]) == 0)
927 {
928 /* Found, but different index/position, swap */
929 if (j != i) {
930 ci = acc->cfg.cred_info[i];
931 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
932 acc->cfg.cred_info[j] = ci;
933 }
934 break;
935 }
936 }
937
938 /* Not found, insert this */
939 if (j == acc->cfg.cred_count) {
940 /* If account credential is full, discard the last one. */
941 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
942 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
943 acc->cfg.cred_count, acc->cfg.cred_count-1);
944 acc->cfg.cred_count--;
945 }
946
947 /* Insert this */
948 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
949 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
950 acc->cfg.cred_count, i, &ci);
951 }
952 }
953 acc->cfg.cred_count = cfg->cred_count;
954
955 /* Concatenate credentials from account config and global config */
956 acc->cred_cnt = 0;
957 for (i=0; i<acc->cfg.cred_count; ++i) {
958 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
959 }
960 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
961 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
962 {
963 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
964 }
965 }
966
967 /* Authentication preference */
968 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
969 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
970 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
971 &cfg->auth_pref.algorithm);
972
973 /* Registration */
974 acc->cfg.reg_timeout = cfg->reg_timeout;
975 acc->cfg.unreg_timeout = cfg->unreg_timeout;
976 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
977 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
978 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
Sauw Ming8ad06c52011-03-15 10:49:59 +0000979 if (acc->cfg.reg_delay_before_refresh != cfg->reg_delay_before_refresh) {
980 acc->cfg.reg_delay_before_refresh = cfg->reg_delay_before_refresh;
981 pjsip_regc_set_delay_before_refresh(acc->regc,
982 cfg->reg_delay_before_refresh);
983 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000984
Sauw Ming9b80d512011-03-15 03:20:37 +0000985 /* Normalize registration timeout and refresh delay */
986 if (acc->cfg.reg_uri.slen ) {
987 if (acc->cfg.reg_timeout == 0) {
988 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
989 }
990 if (acc->cfg.reg_delay_before_refresh == 0) {
991 acc->cfg.reg_delay_before_refresh =
992 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
993 }
994 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000995
996 /* Registrar URI */
997 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
998 if (cfg->reg_uri.slen) {
999 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
1000 if (reg_sip_uri)
1001 acc->srv_port = reg_sip_uri->port;
1002 } else {
1003 /* Unregister if registration was set */
1004 if (acc->cfg.reg_uri.slen)
1005 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1006 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
1007 }
1008 update_reg = PJ_TRUE;
1009 }
1010
Benny Prijonob54719f2010-11-16 03:07:46 +00001011 /* SIP outbound setting */
1012 if (acc->cfg.use_rfc5626 != cfg->use_rfc5626 ||
1013 pj_strcmp(&acc->cfg.rfc5626_instance_id, &cfg->rfc5626_instance_id) ||
1014 pj_strcmp(&acc->cfg.rfc5626_reg_id, &cfg->rfc5626_reg_id))
1015 {
1016 update_reg = PJ_TRUE;
1017 }
1018
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001019 /* Update registration */
1020 if (update_reg) {
1021 /* If accounts has registration enabled, start registration */
1022 if (acc->cfg.reg_uri.slen)
1023 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1024 else {
1025 /* Otherwise subscribe to MWI, if it's enabled */
1026 if (acc->cfg.mwi_enabled)
1027 pjsua_start_mwi(acc);
1028 }
1029 }
1030
1031on_return:
1032 PJSUA_UNLOCK();
1033 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001034}
1035
1036
1037/*
1038 * Modify account's presence status to be advertised to remote/presence
1039 * subscribers.
1040 */
1041PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
1042 pj_bool_t is_online)
1043{
1044 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1045 PJ_EINVAL);
1046 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1047
1048 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001049 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
1050 pjsua_pres_update_acc(acc_id, PJ_FALSE);
1051 return PJ_SUCCESS;
1052}
1053
1054
1055/*
1056 * Set online status with extended information
1057 */
1058PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
1059 pj_bool_t is_online,
1060 const pjrpid_element *pr)
1061{
1062 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1063 PJ_EINVAL);
1064 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1065
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001066 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +00001067 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001068 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
1069 PJSUA_UNLOCK();
1070
Benny Prijono4461c7d2007-08-25 13:36:15 +00001071 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001072 return PJ_SUCCESS;
1073}
1074
Benny Prijonob54719f2010-11-16 03:07:46 +00001075/* Create reg_contact, mainly for SIP outbound */
1076static void update_regc_contact(pjsua_acc *acc)
1077{
1078 pjsua_acc_config *acc_cfg = &acc->cfg;
1079 pj_bool_t need_outbound = PJ_FALSE;
1080 const pj_str_t tcp_param = pj_str(";transport=tcp");
1081 const pj_str_t tls_param = pj_str(";transport=tls");
1082
1083 if (!acc_cfg->use_rfc5626)
1084 goto done;
1085
Benny Prijono2562b752010-11-16 06:01:38 +00001086 /* Check if outbound has been requested and rejected */
1087 if (acc->rfc5626_status == OUTBOUND_NA)
1088 goto done;
1089
Benny Prijonob54719f2010-11-16 03:07:46 +00001090 if (pj_stristr(&acc->contact, &tcp_param)==NULL &&
1091 pj_stristr(&acc->contact, &tls_param)==NULL)
1092 {
1093 /* Currently we can only do SIP outbound for TCP
1094 * and TLS.
1095 */
1096 goto done;
1097 }
1098
1099 /* looks like we can use outbound */
1100 need_outbound = PJ_TRUE;
1101
1102done:
1103 if (!need_outbound) {
1104 /* Outbound is not needed/wanted for the account. acc->reg_contact
1105 * is set to the same as acc->contact.
1106 */
1107 acc->reg_contact = acc->contact;
Benny Prijono2562b752010-11-16 06:01:38 +00001108 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001109 } else {
1110 /* Need to use outbound, append the contact with +sip.instance and
1111 * reg-id parameters.
1112 */
1113 unsigned len;
1114 pj_str_t reg_contact;
1115
1116 acc->rfc5626_status = OUTBOUND_WANTED;
1117 len = acc->contact.slen + acc->rfc5626_instprm.slen +
1118 acc->rfc5626_regprm.slen;
Benny Prijonoe49e6202010-11-16 07:01:25 +00001119 reg_contact.ptr = (char*) pj_pool_alloc(acc->pool, len);
Benny Prijonob54719f2010-11-16 03:07:46 +00001120
1121 pj_strcpy(&reg_contact, &acc->contact);
1122 pj_strcat(&reg_contact, &acc->rfc5626_regprm);
1123 pj_strcat(&reg_contact, &acc->rfc5626_instprm);
1124
1125 acc->reg_contact = reg_contact;
1126
1127 PJ_LOG(4,(THIS_FILE,
1128 "Contact for acc %d updated for SIP outbound: %.*s",
1129 acc->index,
1130 (int)acc->reg_contact.slen,
1131 acc->reg_contact.ptr));
1132 }
1133}
1134
Benny Prijono7f630432008-09-24 16:52:41 +00001135/* Check if IP is private IP address */
1136static pj_bool_t is_private_ip(const pj_str_t *addr)
1137{
1138 const pj_str_t private_net[] =
1139 {
1140 { "10.", 3 },
1141 { "127.", 4 },
1142 { "172.16.", 7 },
1143 { "192.168.", 8 }
1144 };
1145 unsigned i;
1146
1147 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
1148 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
1149 return PJ_TRUE;
1150 }
1151
1152 return PJ_FALSE;
1153}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001154
Benny Prijono15b02302007-09-27 14:07:07 +00001155/* Update NAT address from the REGISTER response */
1156static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
1157 struct pjsip_regc_cbparam *param)
1158{
1159 pjsip_transport *tp;
1160 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001161 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +00001162 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001163 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +00001164 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +00001165 pj_sockaddr contact_addr;
1166 pj_sockaddr recv_addr;
1167 pj_status_t status;
1168 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +00001169 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001170 pjsip_contact_hdr *contact_hdr;
1171 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +00001172
1173 tp = param->rdata->tp_info.transport;
1174
1175 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001176 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +00001177 return PJ_FALSE;
1178
Benny Prijonob54719f2010-11-16 03:07:46 +00001179 /* If SIP outbound is active, no need to update */
1180 if (acc->rfc5626_status == OUTBOUND_ACTIVE) {
1181 PJ_LOG(4,(THIS_FILE, "Acc %d has SIP outbound active, no need to "
1182 "update registration Contact", acc->index));
1183 return PJ_FALSE;
1184 }
1185
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001186#if 0
1187 // Always update
1188 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +00001189
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001190 /* For UDP, only update if STUN is enabled (for now).
1191 * For TCP/TLS, always check.
1192 */
1193 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
1194 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
1195 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
1196 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
1197 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +00001198 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001199 /* Yes we will check */
1200 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001201 return PJ_FALSE;
1202 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001203#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001204
1205 /* Get the received and rport info */
1206 via = param->rdata->msg_info.via;
1207 if (via->rport_param < 1) {
1208 /* Remote doesn't support rport */
1209 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001210 if (rport==0) {
1211 pjsip_transport_type_e tp_type;
1212 tp_type = (pjsip_transport_type_e) tp->key.type;
1213 rport = pjsip_transport_get_default_port_for_type(tp_type);
1214 }
Benny Prijono15b02302007-09-27 14:07:07 +00001215 } else
1216 rport = via->rport_param;
1217
1218 if (via->recvd_param.slen != 0)
1219 via_addr = &via->recvd_param;
1220 else
1221 via_addr = &via->sent_by.host;
1222
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001223 /* Compare received and rport with the URI in our registration */
1224 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001225 contact_hdr = (pjsip_contact_hdr*)
1226 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1227 acc->contact.slen, NULL);
1228 pj_assert(contact_hdr != NULL);
1229 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001230 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001231 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001232
Benny Prijono24a21852008-04-14 04:04:30 +00001233 if (uri->port == 0) {
1234 pjsip_transport_type_e tp_type;
1235 tp_type = (pjsip_transport_type_e) tp->key.type;
1236 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1237 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001238
Benny Prijono84d24932009-06-04 15:51:39 +00001239 /* Convert IP address strings into sockaddr for comparison.
1240 * (http://trac.pjsip.org/repos/ticket/863)
1241 */
1242 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1243 &contact_addr);
1244 if (status == PJ_SUCCESS)
1245 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1246 &recv_addr);
1247 if (status == PJ_SUCCESS) {
1248 /* Compare the addresses as sockaddr according to the ticket above */
1249 matched = (uri->port == rport &&
1250 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1251 } else {
1252 /* Compare the addresses as string, as before */
1253 matched = (uri->port == rport &&
1254 pj_stricmp(&uri->host, via_addr)==0);
1255 }
1256
1257 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001258 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001259 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001260 return PJ_FALSE;
1261 }
1262
Benny Prijono7f630432008-09-24 16:52:41 +00001263 /* Get server IP */
1264 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1265
Benny Prijono15b02302007-09-27 14:07:07 +00001266 /* At this point we've detected that the address as seen by registrar.
1267 * has changed.
1268 */
Benny Prijono7f630432008-09-24 16:52:41 +00001269
1270 /* Do not switch if both Contact and server's IP address are
1271 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001272 * might have messed up with the SIP packets. See:
1273 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001274 *
1275 * This exception can be disabled by setting allow_contact_rewrite
1276 * to 2. In this case, the switch will always be done whenever there
1277 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001278 */
Benny Prijono247921b2008-09-26 22:06:11 +00001279 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1280 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001281 {
1282 /* Don't switch */
1283 pj_pool_release(pool);
1284 return PJ_FALSE;
1285 }
1286
Benny Prijono4f933762009-11-10 03:45:42 +00001287 /* Also don't switch if only the port number part is different, and
1288 * the Via received address is private.
1289 * See http://trac.pjsip.org/repos/ticket/864
1290 */
1291 if (acc->cfg.allow_contact_rewrite != 2 &&
1292 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1293 is_private_ip(via_addr))
1294 {
1295 /* Don't switch */
1296 pj_pool_release(pool);
1297 return PJ_FALSE;
1298 }
1299
Benny Prijono15b02302007-09-27 14:07:07 +00001300 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001301 "(%.*s:%d --> %.*s:%d). Updating registration "
1302 "(using method %d)",
Benny Prijono15b02302007-09-27 14:07:07 +00001303 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001304 (int)uri->host.slen,
1305 uri->host.ptr,
1306 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001307 (int)via_addr->slen,
1308 via_addr->ptr,
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001309 rport,
1310 acc->cfg.contact_rewrite_method));
Benny Prijono15b02302007-09-27 14:07:07 +00001311
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001312 pj_assert(acc->cfg.contact_rewrite_method == 1 ||
1313 acc->cfg.contact_rewrite_method == 2);
1314
1315 if (acc->cfg.contact_rewrite_method == 1) {
1316 /* Unregister current contact */
1317 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1318 if (acc->regc != NULL) {
1319 pjsip_regc_destroy(acc->regc);
1320 acc->regc = NULL;
1321 acc->contact.slen = 0;
1322 }
Benny Prijono15b02302007-09-27 14:07:07 +00001323 }
1324
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001325 /*
1326 * Build new Contact header
1327 */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001328 {
Benny Prijonob54719f2010-11-16 03:07:46 +00001329 const char *ob = ";ob";
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001330 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001331 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001332 int len;
1333
Benny Prijono8972bf02009-04-05 18:30:45 +00001334 /* Enclose IPv6 address in square brackets */
1335 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1336 beginquote = "[";
1337 endquote = "]";
1338 } else {
1339 beginquote = endquote = "";
1340 }
1341
Benny Prijono24a21852008-04-14 04:04:30 +00001342 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001343 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00001344 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s%s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001345 (int)acc->user_part.slen,
1346 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001347 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001348 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001349 (int)via_addr->slen,
1350 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001351 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001352 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001353 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001354 (int)acc->cfg.contact_uri_params.slen,
1355 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00001356 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00001357 (int)acc->cfg.contact_params.slen,
1358 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001359 if (len < 1) {
1360 PJ_LOG(1,(THIS_FILE, "URI too long"));
1361 pj_pool_release(pool);
1362 return PJ_FALSE;
1363 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001364 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001365
Benny Prijonob54719f2010-11-16 03:07:46 +00001366 update_regc_contact(acc);
1367
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001368 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1369 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1370 tp->local_name.port = rport;
1371
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001372 }
1373
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001374 if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001375 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001376 }
Benny Prijono15b02302007-09-27 14:07:07 +00001377
1378 /* Perform new registration */
1379 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1380
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001381 pj_pool_release(pool);
1382
Benny Prijono15b02302007-09-27 14:07:07 +00001383 return PJ_TRUE;
1384}
1385
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001386/* Check and update Service-Route header */
1387void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1388{
1389 pjsip_generic_string_hdr *hsr = NULL;
1390 pjsip_route_hdr *hr, *h;
1391 const pj_str_t HNAME = { "Service-Route", 13 };
1392 const pj_str_t HROUTE = { "Route", 5 };
1393 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001394 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001395
1396 /* Find and parse Service-Route headers */
1397 for (;;) {
1398 char saved;
1399 int parsed_len;
1400
1401 /* Find Service-Route header */
1402 hsr = (pjsip_generic_string_hdr*)
1403 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1404 if (!hsr)
1405 break;
1406
1407 /* Parse as Route header since the syntax is similar. This may
1408 * return more than one headers.
1409 */
1410 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1411 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1412 hr = (pjsip_route_hdr*)
1413 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1414 hsr->hvalue.slen, &parsed_len);
1415 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1416
1417 if (hr == NULL) {
1418 /* Error */
1419 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1420 return;
1421 }
1422
1423 /* Save each URI in the result */
1424 h = hr;
1425 do {
1426 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1427 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1428 {
1429 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1430 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1431 return;
1432 }
1433
1434 uri[uri_cnt++] = h->name_addr.uri;
1435 h = h->next;
1436 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1437
1438 if (h != hr) {
1439 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1440 return;
1441 }
1442
1443 /* Prepare to find next Service-Route header */
1444 hsr = hsr->next;
1445 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1446 break;
1447 }
1448
1449 if (uri_cnt == 0)
1450 return;
1451
1452 /*
1453 * Update account's route set
1454 */
1455
1456 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001457 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001458 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1459 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1460 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001461 i<rcnt;
1462 ++i)
1463 {
1464 pjsip_route_hdr *prev = hr->prev;
1465 pj_list_erase(hr);
1466 hr = prev;
1467 }
1468 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001469
1470 /* Then append the Service-Route URIs */
1471 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001472 hr = pjsip_route_hdr_create(acc->pool);
1473 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001474 pj_list_push_back(&acc->route_set, hr);
1475 }
1476
1477 /* Done */
1478
1479 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1480 acc->index, uri_cnt));
1481}
1482
Benny Prijonobddef2c2007-10-31 13:28:08 +00001483
1484/* Keep alive timer callback */
1485static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1486{
1487 pjsua_acc *acc;
1488 pjsip_tpselector tp_sel;
1489 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001490 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001491 pj_status_t status;
1492
1493 PJ_UNUSED_ARG(th);
1494
1495 PJSUA_LOCK();
1496
1497 te->id = PJ_FALSE;
1498
1499 acc = (pjsua_acc*) te->user_data;
1500
1501 /* Select the transport to send the packet */
1502 pj_bzero(&tp_sel, sizeof(tp_sel));
1503 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1504 tp_sel.u.transport = acc->ka_transport;
1505
1506 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001507 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001508 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001509 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001510
1511 /* Send raw packet */
1512 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1513 PJSIP_TRANSPORT_UDP, &tp_sel,
1514 NULL, acc->cfg.ka_data.ptr,
1515 acc->cfg.ka_data.slen,
1516 &acc->ka_target, acc->ka_target_len,
1517 NULL, NULL);
1518
1519 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1520 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1521 }
1522
1523 /* Reschedule next timer */
1524 delay.sec = acc->cfg.ka_interval;
1525 delay.msec = 0;
1526 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1527 if (status == PJ_SUCCESS) {
1528 te->id = PJ_TRUE;
1529 } else {
1530 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1531 }
1532
1533 PJSUA_UNLOCK();
1534}
1535
1536
1537/* Update keep-alive for the account */
1538static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1539 struct pjsip_regc_cbparam *param)
1540{
1541 /* In all cases, stop keep-alive timer if it's running. */
1542 if (acc->ka_timer.id) {
1543 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1544 acc->ka_timer.id = PJ_FALSE;
1545
1546 pjsip_transport_dec_ref(acc->ka_transport);
1547 acc->ka_transport = NULL;
1548 }
1549
1550 if (start) {
1551 pj_time_val delay;
1552 pj_status_t status;
1553
1554 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001555 * - ka_interval is not zero in the account, and
1556 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001557 *
1558 * Previously we only enabled keep-alive when STUN is enabled, since
1559 * we thought that keep-alive is only needed in Internet situation.
1560 * But it has been discovered that Windows Firewall on WinXP also
1561 * needs to be kept-alive, otherwise incoming packets will be dropped.
1562 * So because of this, now keep-alive is always enabled for UDP,
1563 * regardless of whether STUN is enabled or not.
1564 *
1565 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1566 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001567 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001568 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001569 acc->cfg.ka_interval == 0 ||
1570 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1571 {
1572 /* Keep alive is not necessary */
1573 return;
1574 }
1575
1576 /* Save transport and destination address. */
1577 acc->ka_transport = param->rdata->tp_info.transport;
1578 pjsip_transport_add_ref(acc->ka_transport);
1579 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1580 param->rdata->pkt_info.src_addr_len);
1581 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1582
1583 /* Setup and start the timer */
1584 acc->ka_timer.cb = &keep_alive_timer_cb;
1585 acc->ka_timer.user_data = (void*)acc;
1586
1587 delay.sec = acc->cfg.ka_interval;
1588 delay.msec = 0;
1589 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1590 &delay);
1591 if (status == PJ_SUCCESS) {
1592 acc->ka_timer.id = PJ_TRUE;
1593 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1594 "destination:%s:%d, interval:%ds",
1595 acc->index,
1596 param->rdata->pkt_info.src_name,
1597 param->rdata->pkt_info.src_port,
1598 acc->cfg.ka_interval));
1599 } else {
1600 acc->ka_timer.id = PJ_FALSE;
1601 pjsip_transport_dec_ref(acc->ka_transport);
1602 acc->ka_transport = NULL;
1603 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1604 }
1605 }
1606}
1607
1608
Benny Prijonob54719f2010-11-16 03:07:46 +00001609/* Update the status of SIP outbound registration request */
1610static void update_rfc5626_status(pjsua_acc *acc, pjsip_rx_data *rdata)
1611{
1612 pjsip_require_hdr *hreq;
1613 const pj_str_t STR_OUTBOUND = {"outbound", 8};
1614 unsigned i;
1615
Benny Prijono2562b752010-11-16 06:01:38 +00001616 if (acc->rfc5626_status == OUTBOUND_UNKNOWN) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001617 goto on_return;
1618 }
1619
1620 hreq = rdata->msg_info.require;
1621 if (!hreq) {
Benny Prijono2562b752010-11-16 06:01:38 +00001622 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001623 goto on_return;
1624 }
1625
1626 for (i=0; i<hreq->count; ++i) {
1627 if (pj_stricmp(&hreq->values[i], &STR_OUTBOUND)==0) {
1628 acc->rfc5626_status = OUTBOUND_ACTIVE;
1629 goto on_return;
1630 }
1631 }
1632
1633 /* Server does not support outbound */
Benny Prijono2562b752010-11-16 06:01:38 +00001634 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001635
1636on_return:
Benny Prijono2562b752010-11-16 06:01:38 +00001637 if (acc->rfc5626_status != OUTBOUND_ACTIVE) {
1638 acc->reg_contact = acc->contact;
1639 }
Benny Prijonob54719f2010-11-16 03:07:46 +00001640 PJ_LOG(4,(THIS_FILE, "SIP outbound status for acc %d is %s",
1641 acc->index, (acc->rfc5626_status==OUTBOUND_ACTIVE?
1642 "active": "not active")));
1643}
1644
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001645/*
1646 * This callback is called by pjsip_regc when outgoing register
1647 * request has completed.
1648 */
1649static void regc_cb(struct pjsip_regc_cbparam *param)
1650{
1651
Benny Prijonoa1e69682007-05-11 15:14:34 +00001652 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001653
Benny Prijono15b02302007-09-27 14:07:07 +00001654 if (param->regc != acc->regc)
1655 return;
1656
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001657 PJSUA_LOCK();
1658
1659 /*
1660 * Print registration status.
1661 */
1662 if (param->status!=PJ_SUCCESS) {
1663 pjsua_perror(THIS_FILE, "SIP registration error",
1664 param->status);
1665 pjsip_regc_destroy(acc->regc);
1666 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001667 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001668
Benny Prijonobddef2c2007-10-31 13:28:08 +00001669 /* Stop keep-alive timer if any. */
1670 update_keep_alive(acc, PJ_FALSE, NULL);
1671
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001672 } else if (param->code < 0 || param->code >= 300) {
1673 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1674 param->code,
1675 (int)param->reason.slen, param->reason.ptr));
1676 pjsip_regc_destroy(acc->regc);
1677 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001678 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001679
Benny Prijonobddef2c2007-10-31 13:28:08 +00001680 /* Stop keep-alive timer if any. */
1681 update_keep_alive(acc, PJ_FALSE, NULL);
1682
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001683 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1684
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001685 /* Update auto registration flag */
1686 acc->auto_rereg.active = PJ_FALSE;
1687 acc->auto_rereg.attempt_cnt = 0;
1688
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001689 if (param->expiration < 1) {
1690 pjsip_regc_destroy(acc->regc);
1691 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001692 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001693
1694 /* Stop keep-alive timer if any. */
1695 update_keep_alive(acc, PJ_FALSE, NULL);
1696
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001697 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1698 pjsua_var.acc[acc->index].cfg.id.ptr));
1699 } else {
Benny Prijonob54719f2010-11-16 03:07:46 +00001700 /* Check and update SIP outbound status first, since the result
1701 * will determine if we should update re-registration
1702 */
1703 update_rfc5626_status(acc, param->rdata);
1704
Benny Prijono15b02302007-09-27 14:07:07 +00001705 /* Check NAT bound address */
1706 if (acc_check_nat_addr(acc, param)) {
Benny Prijono15b02302007-09-27 14:07:07 +00001707 PJSUA_UNLOCK();
1708 return;
1709 }
1710
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001711 /* Check and update Service-Route header */
1712 update_service_route(acc, param->rdata);
1713
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001714 PJ_LOG(3, (THIS_FILE,
1715 "%s: registration success, status=%d (%.*s), "
1716 "will re-register in %d seconds",
1717 pjsua_var.acc[acc->index].cfg.id.ptr,
1718 param->code,
1719 (int)param->reason.slen, param->reason.ptr,
1720 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001721
Benny Prijonobddef2c2007-10-31 13:28:08 +00001722 /* Start keep-alive timer if necessary. */
1723 update_keep_alive(acc, PJ_TRUE, param);
1724
Benny Prijono8b6834f2007-02-24 13:29:22 +00001725 /* Send initial PUBLISH if it is enabled */
1726 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1727 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001728
1729 /* Subscribe to MWI, if it's enabled */
1730 if (acc->cfg.mwi_enabled)
1731 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001732 }
1733
1734 } else {
1735 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1736 }
1737
1738 acc->reg_last_err = param->status;
1739 acc->reg_last_code = param->code;
1740
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001741 /* Check if we need to auto retry registration. Basically, registration
1742 * failure codes triggering auto-retry are those of temporal failures
1743 * considered to be recoverable in relatively short term.
1744 */
1745 if (acc->cfg.reg_retry_interval &&
1746 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1747 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1748 param->code == PJSIP_SC_BAD_GATEWAY ||
1749 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1750 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1751 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1752 {
1753 schedule_reregistration(acc);
1754 }
1755
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001756 /* Call the registration status callback */
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00001757
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001758 if (pjsua_var.ua_cfg.cb.on_reg_state) {
1759 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1760 }
1761
1762 if (pjsua_var.ua_cfg.cb.on_reg_state2) {
1763 pjsua_reg_info reg_info;
1764
1765 reg_info.cbparam = param;
1766 (*pjsua_var.ua_cfg.cb.on_reg_state2)(acc->index, &reg_info);
1767 }
1768
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001769 PJSUA_UNLOCK();
1770}
1771
1772
1773/*
1774 * Initialize client registration.
1775 */
1776static pj_status_t pjsua_regc_init(int acc_id)
1777{
1778 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001779 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001780 pj_status_t status;
1781
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001782 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001783 acc = &pjsua_var.acc[acc_id];
1784
1785 if (acc->cfg.reg_uri.slen == 0) {
1786 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1787 return PJ_SUCCESS;
1788 }
1789
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001790 /* Destroy existing session, if any */
1791 if (acc->regc) {
1792 pjsip_regc_destroy(acc->regc);
1793 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001794 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001795 }
1796
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001797 /* initialize SIP registration if registrar is configured */
1798
1799 status = pjsip_regc_create( pjsua_var.endpt,
1800 acc, &regc_cb, &acc->regc);
1801
1802 if (status != PJ_SUCCESS) {
1803 pjsua_perror(THIS_FILE, "Unable to create client registration",
1804 status);
1805 return status;
1806 }
1807
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001808 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001809
1810 if (acc->contact.slen == 0) {
1811 pj_str_t tmp_contact;
1812
1813 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1814 acc_id, &acc->cfg.reg_uri);
1815 if (status != PJ_SUCCESS) {
1816 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1817 " for registration",
1818 status);
1819 pjsip_regc_destroy(acc->regc);
1820 pj_pool_release(pool);
1821 acc->regc = NULL;
1822 return status;
1823 }
1824
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001825 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonob54719f2010-11-16 03:07:46 +00001826 update_regc_contact(acc);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001827 }
1828
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001829 status = pjsip_regc_init( acc->regc,
1830 &acc->cfg.reg_uri,
1831 &acc->cfg.id,
1832 &acc->cfg.id,
Benny Prijonob54719f2010-11-16 03:07:46 +00001833 1, &acc->reg_contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001834 acc->cfg.reg_timeout);
1835 if (status != PJ_SUCCESS) {
1836 pjsua_perror(THIS_FILE,
1837 "Client registration initialization error",
1838 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001839 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001840 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001841 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001842 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001843 return status;
1844 }
1845
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001846 /* If account is locked to specific transport, then set transport to
1847 * the client registration.
1848 */
1849 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1850 pjsip_tpselector tp_sel;
1851
1852 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1853 pjsip_regc_set_transport(acc->regc, &tp_sel);
1854 }
1855
1856
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001857 /* Set credentials
1858 */
1859 if (acc->cred_cnt) {
1860 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1861 }
1862
Sauw Ming9b80d512011-03-15 03:20:37 +00001863 /* Set delay before registration refresh */
1864 pjsip_regc_set_delay_before_refresh(acc->regc,
1865 acc->cfg.reg_delay_before_refresh);
1866
Benny Prijono48ab2b72007-11-08 09:24:30 +00001867 /* Set authentication preference */
1868 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1869
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001870 /* Set route-set
1871 */
Benny Prijono29c8ca32010-06-22 06:02:13 +00001872 if (acc->cfg.reg_use_proxy) {
1873 pjsip_route_hdr route_set;
1874 const pjsip_route_hdr *r;
1875
1876 pj_list_init(&route_set);
1877
1878 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) {
1879 r = pjsua_var.outbound_proxy.next;
1880 while (r != &pjsua_var.outbound_proxy) {
1881 pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r));
1882 r = r->next;
1883 }
1884 }
1885
1886 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY &&
1887 acc->cfg.proxy_cnt)
1888 {
1889 int cnt = acc->cfg.proxy_cnt;
1890 pjsip_route_hdr *pos = route_set.prev;
1891 int i;
1892
1893 r = acc->route_set.prev;
1894 for (i=0; i<cnt; ++i) {
1895 pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r));
1896 r = r->prev;
1897 }
1898 }
1899
1900 if (!pj_list_empty(&route_set))
1901 pjsip_regc_set_route_set( acc->regc, &route_set );
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001902 }
1903
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +00001904 /* Add custom request headers specified in the account config */
1905 pjsip_regc_add_headers(acc->regc, &acc->cfg.reg_hdr_list);
1906
Benny Prijono8fc6de02006-11-11 21:25:55 +00001907 /* Add other request headers. */
1908 if (pjsua_var.ua_cfg.user_agent.slen) {
1909 pjsip_hdr hdr_list;
1910 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1911 pjsip_generic_string_hdr *h;
1912
Benny Prijono8fc6de02006-11-11 21:25:55 +00001913 pj_list_init(&hdr_list);
1914
1915 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1916 &pjsua_var.ua_cfg.user_agent);
1917 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1918
1919 pjsip_regc_add_headers(acc->regc, &hdr_list);
1920 }
1921
Benny Prijonob54719f2010-11-16 03:07:46 +00001922 /* If SIP outbound is used, add "Supported: outbound, path header" */
1923 if (acc->rfc5626_status == OUTBOUND_WANTED) {
1924 pjsip_hdr hdr_list;
1925 pjsip_supported_hdr *hsup;
1926
1927 pj_list_init(&hdr_list);
1928 hsup = pjsip_supported_hdr_create(pool);
1929 pj_list_push_back(&hdr_list, hsup);
1930
1931 hsup->count = 2;
1932 hsup->values[0] = pj_str("outbound");
1933 hsup->values[1] = pj_str("path");
1934
1935 pjsip_regc_add_headers(acc->regc, &hdr_list);
1936 }
1937
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001938 pj_pool_release(pool);
1939
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001940 return PJ_SUCCESS;
1941}
1942
1943
1944/*
1945 * Update registration or perform unregistration.
1946 */
1947PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1948 pj_bool_t renew)
1949{
1950 pj_status_t status = 0;
1951 pjsip_tx_data *tdata = 0;
1952
1953 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1954 PJ_EINVAL);
1955 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1956
1957 PJSUA_LOCK();
1958
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001959 /* Cancel any re-registration timer */
1960 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1961
1962 /* Reset pointer to registration transport */
1963 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1964
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001965 if (renew) {
1966 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001967 status = pjsua_regc_init(acc_id);
1968 if (status != PJ_SUCCESS) {
1969 pjsua_perror(THIS_FILE, "Unable to create registration",
1970 status);
1971 goto on_return;
1972 }
1973 }
1974 if (!pjsua_var.acc[acc_id].regc) {
1975 status = PJ_EINVALIDOP;
1976 goto on_return;
1977 }
1978
1979 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1980 &tdata);
1981
Benny Prijono28f673a2007-10-15 07:04:59 +00001982 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1983 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1984 pjsip_authorization_hdr *h;
1985 char *uri;
1986 int d;
1987
1988 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1989 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1990 uri, acc->cfg.reg_uri.slen+10);
1991 pj_assert(d > 0);
1992
1993 h = pjsip_authorization_hdr_create(tdata->pool);
1994 h->scheme = pj_str("Digest");
1995 h->credential.digest.username = acc->cred[0].username;
1996 h->credential.digest.realm = acc->srv_domain;
1997 h->credential.digest.uri = pj_str(uri);
1998 h->credential.digest.algorithm = pj_str("md5");
1999
2000 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
2001 }
2002
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002003 } else {
2004 if (pjsua_var.acc[acc_id].regc == NULL) {
2005 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
2006 status = PJ_EINVALIDOP;
2007 goto on_return;
2008 }
Benny Prijono166d5022010-02-10 14:24:48 +00002009
2010 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
2011
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002012 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
2013 }
2014
Benny Prijono56315612006-07-18 14:39:40 +00002015 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00002016 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002017 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00002018 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002019
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002020 /* Update pointer to registration transport */
2021 if (status == PJ_SUCCESS) {
2022 pjsip_regc_info reg_info;
2023
2024 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
2025 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
2026 }
2027
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002028 if (status != PJ_SUCCESS) {
2029 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
2030 status);
2031 } else {
2032 PJ_LOG(3,(THIS_FILE, "%s sent",
2033 (renew? "Registration" : "Unregistration")));
2034 }
2035
2036on_return:
2037 PJSUA_UNLOCK();
2038 return status;
2039}
2040
2041
2042/*
2043 * Get account information.
2044 */
2045PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
2046 pjsua_acc_info *info)
2047{
2048 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2049 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
2050
2051 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002052 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002053
Benny Prijonoac623b32006-07-03 15:19:31 +00002054 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002055
2056 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
2057 PJ_EINVAL);
2058 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
2059
2060 PJSUA_LOCK();
2061
2062 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
2063 PJSUA_UNLOCK();
2064 return PJ_EINVALIDOP;
2065 }
2066
2067 info->id = acc_id;
2068 info->is_default = (pjsua_var.default_acc == acc_id);
2069 info->acc_uri = acc_cfg->id;
2070 info->has_registration = (acc->cfg.reg_uri.slen > 0);
2071 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00002072 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
2073 if (info->rpid.note.slen)
2074 info->online_status_text = info->rpid.note;
2075 else if (info->online_status)
2076 info->online_status_text = pj_str("Online");
2077 else
2078 info->online_status_text = pj_str("Offline");
2079
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002080 if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00002081 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002082 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002083 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002084 if (acc->reg_last_err)
2085 info->reg_last_err = acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002086 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002087 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002088 info->status_text = pj_str("not registered");
2089 }
2090 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002091 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002092 info->status_text = pj_str("In Progress");
2093 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002094 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002095 info->status_text = pj_str("does not register");
2096 }
2097
2098 if (acc->regc) {
2099 pjsip_regc_info regc_info;
2100 pjsip_regc_get_info(acc->regc, &regc_info);
2101 info->expires = regc_info.next_reg;
2102 } else {
2103 info->expires = -1;
2104 }
2105
2106 PJSUA_UNLOCK();
2107
2108 return PJ_SUCCESS;
2109
2110}
2111
2112
2113/*
2114 * Enum accounts all account ids.
2115 */
2116PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
2117 unsigned *count )
2118{
2119 unsigned i, c;
2120
2121 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
2122
2123 PJSUA_LOCK();
2124
2125 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2126 if (!pjsua_var.acc[i].valid)
2127 continue;
2128 ids[c] = i;
2129 ++c;
2130 }
2131
2132 *count = c;
2133
2134 PJSUA_UNLOCK();
2135
2136 return PJ_SUCCESS;
2137}
2138
2139
2140/*
2141 * Enum accounts info.
2142 */
2143PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
2144 unsigned *count )
2145{
2146 unsigned i, c;
2147
2148 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
2149
2150 PJSUA_LOCK();
2151
2152 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2153 if (!pjsua_var.acc[i].valid)
2154 continue;
2155
2156 pjsua_acc_get_info(i, &info[c]);
2157 ++c;
2158 }
2159
2160 *count = c;
2161
2162 PJSUA_UNLOCK();
2163
2164 return PJ_SUCCESS;
2165}
2166
2167
2168/*
2169 * This is an internal function to find the most appropriate account to
2170 * used to reach to the specified URL.
2171 */
2172PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
2173{
2174 pj_str_t tmp;
2175 pjsip_uri *uri;
2176 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002177 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00002178 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002179
2180 PJSUA_LOCK();
2181
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002182 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002183
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002184 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002185
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002186 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002187 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002188 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002189 PJSUA_UNLOCK();
2190 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002191 }
2192
2193 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2194 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2195 {
2196 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00002197 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2198 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002199 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00002200 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002201 break;
2202 }
2203
Benny Prijono093d3022006-09-24 00:07:11 +00002204 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002205 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002206 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002207 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002208 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002209 }
2210
2211 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002212 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002213 PJSUA_UNLOCK();
2214 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002215 }
2216
Benny Prijonoa1e69682007-05-11 15:14:34 +00002217 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002218
Benny Prijonob4a17c92006-07-10 14:40:21 +00002219 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00002220 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2221 unsigned acc_id = pjsua_var.acc_ids[i];
2222 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
2223 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
2224 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002225 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002226 PJSUA_UNLOCK();
2227 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00002228 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002229 }
2230
Benny Prijonob4a17c92006-07-10 14:40:21 +00002231 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00002232 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2233 unsigned acc_id = pjsua_var.acc_ids[i];
2234 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
2235 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002236 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002237 PJSUA_UNLOCK();
2238 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00002239 }
2240 }
2241
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002242
Benny Prijono093d3022006-09-24 00:07:11 +00002243 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002244 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002245 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00002246 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002247}
2248
2249
2250/*
2251 * This is an internal function to find the most appropriate account to be
2252 * used to handle incoming calls.
2253 */
2254PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
2255{
2256 pjsip_uri *uri;
2257 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00002258 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002259
Benny Prijono371cf0a2007-06-19 00:35:37 +00002260 /* Check that there's at least one account configured */
2261 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
2262
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002263 uri = rdata->msg_info.to->uri;
2264
2265 /* Just return default account if To URI is not SIP: */
2266 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2267 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2268 {
2269 return pjsua_var.default_acc;
2270 }
2271
2272
2273 PJSUA_LOCK();
2274
2275 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
2276
2277 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00002278 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2279 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002280 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2281
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002282 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00002283 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002284 {
2285 /* Match ! */
2286 PJSUA_UNLOCK();
2287 return acc_id;
2288 }
2289 }
2290
Benny Prijono093d3022006-09-24 00:07:11 +00002291 /* No matching account, try match domain part only. */
2292 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2293 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002294 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2295
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002296 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002297 /* Match ! */
2298 PJSUA_UNLOCK();
2299 return acc_id;
2300 }
2301 }
2302
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002303 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00002304 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2305 unsigned acc_id = pjsua_var.acc_ids[i];
2306 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2307
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002308 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
2309
2310 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2311 pjsip_transport_type_e type;
2312 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2313 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
2314 type = PJSIP_TRANSPORT_UDP;
2315
2316 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
2317 continue;
2318 }
2319
Benny Prijono093d3022006-09-24 00:07:11 +00002320 /* Match ! */
2321 PJSUA_UNLOCK();
2322 return acc_id;
2323 }
2324 }
2325
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002326 /* Still no match, use default account */
2327 PJSUA_UNLOCK();
2328 return pjsua_var.default_acc;
2329}
2330
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002331
Benny Prijonofff245c2007-04-02 11:44:47 +00002332/*
2333 * Create arbitrary requests for this account.
2334 */
2335PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2336 const pjsip_method *method,
2337 const pj_str_t *target,
2338 pjsip_tx_data **p_tdata)
2339{
2340 pjsip_tx_data *tdata;
2341 pjsua_acc *acc;
2342 pjsip_route_hdr *r;
2343 pj_status_t status;
2344
2345 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2346 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2347
2348 acc = &pjsua_var.acc[acc_id];
2349
2350 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2351 &acc->cfg.id, target,
2352 NULL, NULL, -1, NULL, &tdata);
2353 if (status != PJ_SUCCESS) {
2354 pjsua_perror(THIS_FILE, "Unable to create request", status);
2355 return status;
2356 }
2357
2358 /* Copy routeset */
2359 r = acc->route_set.next;
2360 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002361 pjsip_msg_add_hdr(tdata->msg,
2362 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002363 r = r->next;
2364 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002365
2366 /* If account is locked to specific transport, then set that transport to
2367 * the transmit data.
2368 */
2369 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2370 pjsip_tpselector tp_sel;
2371
2372 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2373 pjsip_tx_data_set_transport(tdata, &tp_sel);
2374 }
2375
Benny Prijonofff245c2007-04-02 11:44:47 +00002376 /* Done */
2377 *p_tdata = tdata;
2378 return PJ_SUCCESS;
2379}
2380
2381
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002382PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2383 pj_str_t *contact,
2384 pjsua_acc_id acc_id,
2385 const pj_str_t *suri)
2386{
2387 pjsua_acc *acc;
2388 pjsip_sip_uri *sip_uri;
2389 pj_status_t status;
2390 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2391 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002392 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002393 unsigned flag;
2394 int secure;
2395 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002396 const char *beginquote, *endquote;
2397 char transport_param[32];
Benny Prijonob54719f2010-11-16 03:07:46 +00002398 const char *ob = ";ob";
Benny Prijonod0bd4982007-12-02 15:40:52 +00002399
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002400
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002401 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002402 acc = &pjsua_var.acc[acc_id];
2403
Benny Prijonof75eceb2007-03-23 19:09:54 +00002404 /* If force_contact is configured, then use use it */
2405 if (acc->cfg.force_contact.slen) {
2406 *contact = acc->cfg.force_contact;
2407 return PJ_SUCCESS;
2408 }
2409
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002410 /* If route-set is configured for the account, then URI is the
2411 * first entry of the route-set.
2412 */
2413 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002414 sip_uri = (pjsip_sip_uri*)
2415 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002416 } else {
2417 pj_str_t tmp;
2418 pjsip_uri *uri;
2419
2420 pj_strdup_with_null(pool, &tmp, suri);
2421
2422 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2423 if (uri == NULL)
2424 return PJSIP_EINVALIDURI;
2425
2426 /* For non-SIP scheme, route set should be configured */
2427 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
Benny Prijonoc7545782010-09-28 07:43:18 +00002428 return PJSIP_ENOROUTESET;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002429
Benny Prijono8c7a6172007-02-18 21:17:46 +00002430 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002431 }
2432
2433 /* Get transport type of the URI */
2434 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2435 tp_type = PJSIP_TRANSPORT_TLS;
2436 else if (sip_uri->transport_param.slen == 0) {
2437 tp_type = PJSIP_TRANSPORT_UDP;
2438 } else
2439 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2440
2441 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2442 return PJSIP_EUNSUPTRANSPORT;
2443
Benny Prijonod0bd4982007-12-02 15:40:52 +00002444 /* If destination URI specifies IPv6, then set transport type
2445 * to use IPv6 as well.
2446 */
Benny Prijono19b29372007-12-05 04:08:40 +00002447 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002448 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2449
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002450 flag = pjsip_transport_get_flag_from_type(tp_type);
2451 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2452
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002453 /* Init transport selector. */
2454 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2455
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002456 /* Get local address suitable to send request from */
2457 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002458 pool, tp_type, &tp_sel,
2459 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002460 if (status != PJ_SUCCESS)
2461 return status;
2462
Benny Prijonod0bd4982007-12-02 15:40:52 +00002463 /* Enclose IPv6 address in square brackets */
2464 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2465 beginquote = "[";
2466 endquote = "]";
2467 } else {
2468 beginquote = endquote = "";
2469 }
2470
2471 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002472 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002473 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2474 ";transport=%s",
2475 pjsip_transport_get_type_name(tp_type));
2476 } else {
2477 transport_param[0] = '\0';
2478 }
2479
2480
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002481 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002482 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002483 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00002484 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s%s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002485 (int)acc->display.slen,
2486 acc->display.ptr,
2487 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002488 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002489 (int)acc->user_part.slen,
2490 acc->user_part.ptr,
2491 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002492 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002493 (int)local_addr.slen,
2494 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002495 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002496 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002497 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002498 (int)acc->cfg.contact_uri_params.slen,
2499 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00002500 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00002501 (int)acc->cfg.contact_params.slen,
2502 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002503
2504 return PJ_SUCCESS;
2505}
2506
2507
2508
2509PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2510 pj_str_t *contact,
2511 pjsua_acc_id acc_id,
2512 pjsip_rx_data *rdata )
2513{
2514 /*
2515 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2516 * If the request that initiated the dialog contained a SIPS URI
2517 * in the Request-URI or in the top Record-Route header field value,
2518 * if there was any, or the Contact header field if there was no
2519 * Record-Route header field, the Contact header field in the response
2520 * MUST be a SIPS URI.
2521 */
2522 pjsua_acc *acc;
2523 pjsip_sip_uri *sip_uri;
2524 pj_status_t status;
2525 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2526 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002527 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002528 unsigned flag;
2529 int secure;
2530 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002531 const char *beginquote, *endquote;
2532 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002533
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002534 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002535 acc = &pjsua_var.acc[acc_id];
2536
Benny Prijonof75eceb2007-03-23 19:09:54 +00002537 /* If force_contact is configured, then use use it */
2538 if (acc->cfg.force_contact.slen) {
2539 *contact = acc->cfg.force_contact;
2540 return PJ_SUCCESS;
2541 }
2542
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002543 /* If Record-Route is present, then URI is the top Record-Route. */
2544 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002545 sip_uri = (pjsip_sip_uri*)
2546 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002547 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002548 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002549 pjsip_contact_hdr *h_contact;
2550 pjsip_uri *uri = NULL;
2551
Benny Prijonoa330d452008-08-05 20:14:39 +00002552 /* Otherwise URI is Contact URI.
2553 * Iterate the Contact URI until we find sip: or sips: scheme.
2554 */
2555 do {
2556 h_contact = (pjsip_contact_hdr*)
2557 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2558 pos);
2559 if (h_contact) {
Benny Prijono8b33bba2010-06-02 03:03:43 +00002560 if (h_contact->uri)
2561 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2562 else
2563 uri = NULL;
2564 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2565 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
Benny Prijonoa330d452008-08-05 20:14:39 +00002566 {
2567 pos = (pjsip_hdr*)h_contact->next;
2568 if (pos == &rdata->msg_info.msg->hdr)
2569 h_contact = NULL;
2570 } else {
2571 break;
2572 }
2573 }
2574 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002575
2576
2577 /* Or if Contact URI is not present, take the remote URI from
2578 * the From URI.
2579 */
2580 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002581 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002582
2583
2584 /* Can only do sip/sips scheme at present. */
2585 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2586 return PJSIP_EINVALIDREQURI;
2587
Benny Prijono8c7a6172007-02-18 21:17:46 +00002588 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002589 }
2590
2591 /* Get transport type of the URI */
2592 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2593 tp_type = PJSIP_TRANSPORT_TLS;
2594 else if (sip_uri->transport_param.slen == 0) {
2595 tp_type = PJSIP_TRANSPORT_UDP;
2596 } else
2597 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002598
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002599 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2600 return PJSIP_EUNSUPTRANSPORT;
2601
Benny Prijonod0bd4982007-12-02 15:40:52 +00002602 /* If destination URI specifies IPv6, then set transport type
2603 * to use IPv6 as well.
2604 */
2605 if (pj_strchr(&sip_uri->host, ':'))
2606 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2607
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002608 flag = pjsip_transport_get_flag_from_type(tp_type);
2609 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2610
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002611 /* Init transport selector. */
2612 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2613
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002614 /* Get local address suitable to send request from */
2615 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002616 pool, tp_type, &tp_sel,
2617 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002618 if (status != PJ_SUCCESS)
2619 return status;
2620
Benny Prijonod0bd4982007-12-02 15:40:52 +00002621 /* Enclose IPv6 address in square brackets */
2622 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2623 beginquote = "[";
2624 endquote = "]";
2625 } else {
2626 beginquote = endquote = "";
2627 }
2628
2629 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002630 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002631 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2632 ";transport=%s",
2633 pjsip_transport_get_type_name(tp_type));
2634 } else {
2635 transport_param[0] = '\0';
2636 }
2637
2638
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002639 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002640 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002641 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002642 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002643 (int)acc->display.slen,
2644 acc->display.ptr,
2645 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002646 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002647 (int)acc->user_part.slen,
2648 acc->user_part.ptr,
2649 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002650 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002651 (int)local_addr.slen,
2652 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002653 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002654 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002655 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002656 (int)acc->cfg.contact_uri_params.slen,
2657 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002658 (int)acc->cfg.contact_params.slen,
2659 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002660
2661 return PJ_SUCCESS;
2662}
2663
2664
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002665PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2666 pjsua_transport_id tp_id)
2667{
2668 pjsua_acc *acc;
2669
2670 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2671 acc = &pjsua_var.acc[acc_id];
2672
Benny Prijonoa1e69682007-05-11 15:14:34 +00002673 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002674 PJ_EINVAL);
2675
2676 acc->cfg.transport_id = tp_id;
2677
2678 return PJ_SUCCESS;
2679}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002680
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002681
2682/* Auto re-registration timeout callback */
2683static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2684{
2685 pjsua_acc *acc;
2686 pj_status_t status;
2687
2688 PJ_UNUSED_ARG(th);
2689 acc = (pjsua_acc*) te->user_data;
2690 pj_assert(acc);
2691
2692 PJSUA_LOCK();
2693
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002694 /* Check if the reregistration timer is still valid, e.g: while waiting
2695 * timeout timer application might have deleted the account or disabled
2696 * the auto-reregistration.
2697 */
2698 if (!acc->valid || !acc->auto_rereg.active ||
2699 acc->cfg.reg_retry_interval == 0)
2700 {
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002701 goto on_return;
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002702 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002703
2704 /* Start re-registration */
2705 acc->auto_rereg.attempt_cnt++;
2706 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2707 if (status != PJ_SUCCESS)
2708 schedule_reregistration(acc);
2709
Nanang Izzuddin66580002010-04-14 08:12:08 +00002710on_return:
2711 PJSUA_UNLOCK();
2712}
2713
2714
2715/* Schedule reregistration for specified account. Note that the first
2716 * re-registration after a registration failure will be done immediately.
2717 * Also note that this function should be called within PJSUA mutex.
2718 */
2719static void schedule_reregistration(pjsua_acc *acc)
2720{
2721 pj_time_val delay;
2722
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002723 pj_assert(acc);
2724
2725 /* Validate the account and re-registration feature status */
2726 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
2727 return;
2728 }
Nanang Izzuddin66580002010-04-14 08:12:08 +00002729
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002730 /* If configured, disconnect calls of this account after the first
2731 * reregistration attempt failed.
2732 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002733 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002734 {
2735 unsigned i, cnt;
2736
2737 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2738 if (pjsua_var.calls[i].acc_id == acc->index) {
2739 pjsua_call_hangup(i, 0, NULL, NULL);
2740 ++cnt;
2741 }
2742 }
2743
2744 if (cnt) {
2745 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2746 "after reregistration attempt failed",
2747 cnt, acc->index));
2748 }
2749 }
2750
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002751 /* Cancel any re-registration timer */
2752 pjsua_cancel_timer(&acc->auto_rereg.timer);
2753
2754 /* Update re-registration flag */
2755 acc->auto_rereg.active = PJ_TRUE;
2756
2757 /* Set up timer for reregistration */
2758 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2759 acc->auto_rereg.timer.user_data = acc;
2760
2761 /* Reregistration attempt. The first attempt will be done immediately. */
2762 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2763 delay.msec = 0;
2764 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2765}
2766
2767
2768/* Internal function to perform auto-reregistration on transport
2769 * connection/disconnection events.
2770 */
2771void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2772 pjsip_transport_state state,
2773 const pjsip_transport_state_info *info)
2774{
2775 unsigned i;
2776
2777 PJ_UNUSED_ARG(info);
2778
2779 /* Only care for transport disconnection events */
2780 if (state != PJSIP_TP_STATE_DISCONNECTED)
2781 return;
2782
2783 /* Shutdown this transport, to make sure that the transport manager
2784 * will create a new transport for reconnection.
2785 */
2786 pjsip_transport_shutdown(tp);
2787
2788 PJSUA_LOCK();
2789
2790 /* Enumerate accounts using this transport and perform actions
2791 * based on the transport state.
2792 */
2793 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2794 pjsua_acc *acc = &pjsua_var.acc[i];
2795
2796 /* Skip if this account is not valid OR auto re-registration
2797 * feature is disabled OR this transport is not used by this account.
2798 */
2799 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2800 tp != acc->auto_rereg.reg_tp)
2801 {
2802 continue;
2803 }
2804
2805 /* Schedule reregistration for this account */
2806 schedule_reregistration(acc);
2807 }
2808
2809 PJSUA_UNLOCK();
2810}