Debian Codegraph @

Xpjproject/pjsip/src/pjsua-lib/pjsua_acc.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjsua-lib/pjsua.h>
20 #include <pjsua-lib/pjsua_internal.h>
21
22
23 #define THIS_FILE "pjsua_acc.c"
24
25 enum
26 {
27 OUTBOUND_UNKNOWN, // status unknown
28 OUTBOUND_WANTED, // initiated in registration
29 OUTBOUND_ACTIVE, // got positive response from server
30 OUTBOUND_NA // not wanted or got negative response from server
31 };
32
33
34 static int get_ip_addr_ver(const pj_str_t *host);
35 static void schedule_reregistration(pjsua_acc *acc);
36 static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te);
37
38 /* Bridge: maps low-level auth challenge -> pjsua on_auth_challenge */
39 pj_bool_t pjsua_auth_on_challenge(
40 pjsip_auth_clt_sess *sess,
41 void *token,
42 const pjsip_auth_clt_async_on_chal_param *param)
43 {
44 pjsua_on_auth_challenge_param cb_param;
45 pjsua_acc_id acc_id = (pjsua_acc_id)(pj_ssize_t)param->user_data;
46 pjsua_call_id call_id = PJSUA_INVALID_ID;
47
48 PJ_UNUSED_ARG(sess);
49
50 /* Do NOT acquire PJSUA_LOCK here. This callback is invoked with
51 * the transaction grp_lock held (pjsip_tsx_recv_msg -> state_handler
52 * -> regc_tsx_callback -> here), while pjsua_acc_del() holds
53 * PJSUA_LOCK (recursively via pjsua_acc_set_registration) and then
54 * acquires a tsx grp_lock through pjsip_regc_send ->
55 * pjsip_tsx_set_transport. Acquiring PJSUA_LOCK here would create
56 * an ABBA lock-order inversion.
57 *
58 * This is safe without the lock because:
59 * - pjsua_var.acc[] is a fixed-size static array; indexing is
60 * always valid for a bounded acc_id.
61 * - A racy read of the 'valid' flag can only produce a false
62 * positive (proceed with a just-deleted account), which is
63 * harmless: respond() rechecks validity under lock for the
64 * deferred path, and reinit_req handles a zeroed auth session
65 * for the synchronous path.
66 * - &shared_auth_sess is a stable address (static array member).
67 * - pjsua_var.mod.id is immutable after pjsua_init().
68 */
69 if (!pjsua_acc_is_valid(acc_id)) {
70 return PJ_FALSE;
71 }
72
73 /* Determine call_id from rdata -> dialog -> mod_data */
74 if (param->rdata) {
75 pjsip_dialog *dlg = pjsip_rdata_get_dlg(
76 (pjsip_rx_data*)param->rdata);
77 if (dlg) {
78 pjsua_call *call =
79 (pjsua_call*)dlg->mod_data[pjsua_var.mod.id];
80 if (call)
81 call_id = call->index;
82 }
83 }
84
85 pj_bzero(&cb_param, sizeof(cb_param));
86 cb_param.acc_id = acc_id;
87 cb_param.call_id = call_id;
88 /* Always use the shared auth session (account-level lifetime) instead
89 * of the potentially short-lived module-owned session (e.g. regc's).
90 * The shared session has the same credentials and survives regc
91 * destroy/recreate cycles.
92 */
93 cb_param.auth_sess = &pjsua_var.acc[acc_id].shared_auth_sess;
94 cb_param.token = token;
95 cb_param.rdata = param->rdata;
96 cb_param.tdata = param->tdata;
97
98 (*pjsua_var.ua_cfg.cb.on_auth_challenge)(&cb_param);
99
100 return cb_param.handled;
101 }
102
103 /*
104 * Get number of current accounts.
105 */
106 PJ_DEF(unsigned) pjsua_acc_get_count(void)
107 {
108 return pjsua_var.acc_cnt;
109 }
110
111
112 /*
113 * Check if the specified account ID is valid.
114 */
115 PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
116 {
117 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
118 pjsua_var.acc[acc_id].valid;
119 }
120
121
122 /*
123 * Set default account
124 */
125 PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
126 {
127 pjsua_var.default_acc = acc_id;
128 return PJ_SUCCESS;
129 }
130
131
132 /*
133 * Get default account.
134 */
135 PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
136 {
137 return pjsua_var.default_acc;
138 }
139
140
141 /*
142 * Copy account configuration.
143 */
144 PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
145 pjsua_acc_config *dst,
146 const pjsua_acc_config *src)
147 {
148 unsigned i;
149
150 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
151
152 pj_strdup_with_null(pool, &dst->id, &src->id);
153 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
154 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
155 pj_strdup_with_null(pool, &dst->reg_contact_params,
156 &src->reg_contact_params);
157 pj_strdup_with_null(pool, &dst->reg_contact_uri_params,
158 &src->reg_contact_uri_params);
159 pj_strdup_with_null(pool, &dst->contact_params, &src->contact_params);
160 pj_strdup_with_null(pool, &dst->contact_uri_params,
161 &src->contact_uri_params);
162 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
163 pj_strdup_with_null(pool, &dst->rfc5626_instance_id,
164 &src->rfc5626_instance_id);
165 pj_strdup_with_null(pool, &dst->rfc5626_reg_id, &src->rfc5626_reg_id);
166
167 dst->proxy_cnt = src->proxy_cnt;
168 for (i=0; i<src->proxy_cnt; ++i)
169 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
170
171 dst->reg_timeout = src->reg_timeout;
172 dst->reg_delay_before_refresh = src->reg_delay_before_refresh;
173 dst->cred_count = src->cred_count;
174
175 for (i=0; i<src->cred_count; ++i) {
176 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
177 }
178
179 pj_list_init(&dst->reg_hdr_list);
180 if (!pj_list_empty(&src->reg_hdr_list)) {
181 const pjsip_hdr *hdr;
182
183 hdr = src->reg_hdr_list.next;
184 while (hdr != &src->reg_hdr_list) {
185 pj_list_push_back(&dst->reg_hdr_list, pjsip_hdr_clone(pool, hdr));
186 hdr = hdr->next;
187 }
188 }
189
190 pj_list_init(&dst->sub_hdr_list);
191 if (!pj_list_empty(&src->sub_hdr_list)) {
192 const pjsip_hdr *hdr;
193
194 hdr = src->sub_hdr_list.next;
195 while (hdr != &src->sub_hdr_list) {
196 pj_list_push_back(&dst->sub_hdr_list, pjsip_hdr_clone(pool, hdr));
197 hdr = hdr->next;
198 }
199 }
200
201 pjsip_auth_clt_pref_dup(pool, &dst->auth_pref, &src->auth_pref);
202
203 pjsua_transport_config_dup(pool, &dst->rtp_cfg, &src->rtp_cfg);
204
205 pjsua_ice_config_dup(pool, &dst->ice_cfg, &src->ice_cfg);
206 pjsua_turn_config_dup(pool, &dst->turn_cfg, &src->turn_cfg);
207
208 pjsua_srtp_opt_dup(pool, &dst->srtp_opt, &src->srtp_opt, PJ_FALSE);
209
210 pj_strdup(pool, &dst->ka_data, &src->ka_data);
211
212 pjmedia_rtcp_fb_setting_dup(pool, &dst->rtcp_fb_cfg, &src->rtcp_fb_cfg);
213 }
214
215 /*
216 * Calculate CRC of proxy list.
217 */
218 static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt)
219 {
220 pj_crc32_context ctx;
221 unsigned i;
222
223 pj_crc32_init(&ctx);
224 for (i=0; i<cnt; ++i) {
225 pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen);
226 }
227
228 return pj_crc32_final(&ctx);
229 }
230
231 /*
232 * Initialize outbound settings.
233 */
234 static void init_outbound_setting(pjsua_acc *acc)
235 {
236 pjsua_acc_config *acc_cfg = &acc->cfg;
237 if (acc_cfg->rfc5626_instance_id.slen == 0) {
238 const pj_str_t *hostname;
239 pj_uint32_t hval;
240 pj_size_t pos;
241 char instprm[] = ";+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000CCDDEEFF>\"";
242
243 hostname = pj_gethostname();
244 pos = pj_ansi_strlen(instprm) - 10;
245 hval = pj_hash_calc(0, hostname->ptr, (unsigned)hostname->slen);
246 pj_val_to_hex_digit(((char*)&hval)[0], instprm + pos + 0);
247 pj_val_to_hex_digit(((char*)&hval)[1], instprm + pos + 2);
248 pj_val_to_hex_digit(((char*)&hval)[2], instprm + pos + 4);
249 pj_val_to_hex_digit(((char*)&hval)[3], instprm + pos + 6);
250
251 pj_strdup2(acc->pool, &acc->rfc5626_instprm, instprm);
252 } else {
253 const char *prmname = ";+sip.instance=\"";
254 pj_size_t len;
255
256 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_instance_id.slen + 1;
257 acc->rfc5626_instprm.ptr = (char*)pj_pool_alloc(acc->pool, len + 1);
258 pj_ansi_snprintf(acc->rfc5626_instprm.ptr, len + 1,
259 "%s%.*s\"",
260 prmname,
261 (int)acc_cfg->rfc5626_instance_id.slen,
262 acc_cfg->rfc5626_instance_id.ptr);
263 acc->rfc5626_instprm.slen = len;
264 }
265
266 if (acc_cfg->rfc5626_reg_id.slen == 0) {
267 acc->rfc5626_regprm = pj_str(";reg-id=1");
268 } else {
269 const char *prmname = ";reg-id=";
270 pj_size_t len;
271
272 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_reg_id.slen;
273 acc->rfc5626_regprm.ptr = (char*)pj_pool_alloc(acc->pool, len + 1);
274 pj_ansi_snprintf(acc->rfc5626_regprm.ptr, len + 1,
275 "%s%.*s\"",
276 prmname,
277 (int)acc_cfg->rfc5626_reg_id.slen,
278 acc_cfg->rfc5626_reg_id.ptr);
279 acc->rfc5626_regprm.slen = len;
280 }
281
282 acc->rfc5626_status = OUTBOUND_WANTED;
283 }
284
285 /*
286 * Destroy account's registration and deinit.
287 */
288 static pj_status_t destroy_regc(pjsua_acc *acc, pj_bool_t force)
289 {
290 if (acc->regc) {
291 pj_status_t status = pjsip_regc_destroy2(acc->regc, force);
292 if (status != PJ_SUCCESS && !force) {
293 /* If regc destroy failed and not forced, we should not
294 * deinit and return here.
295 */
296 return status;
297 }
298 }
299 acc->regc = NULL;
300 acc->contact.slen = 0;
301 acc->reg_mapped_addr.slen = 0;
302 acc->rfc5626_status = OUTBOUND_UNKNOWN;
303 acc->rfc5626_flowtmr = 0;
304
305 return PJ_SUCCESS;
306 }
307
308 /*
309 * Initialize a new account (after configuration is set).
310 */
311 static pj_status_t initialize_acc(unsigned acc_id)
312 {
313 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
314 pjsua_acc *acc = &pjsua_var.acc[acc_id];
315 pjsip_name_addr *name_addr;
316 pjsip_sip_uri *sip_reg_uri;
317 pj_status_t status;
318 unsigned i;
319
320 /* Need to parse local_uri to get the elements: */
321
322 name_addr = (pjsip_name_addr*)
323 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
324 acc_cfg->id.slen,
325 PJSIP_PARSE_URI_AS_NAMEADDR);
326 if (name_addr == NULL) {
327 pjsua_perror(THIS_FILE, "Invalid local URI",
328 PJSIP_EINVALIDURI);
329 return PJSIP_EINVALIDURI;
330 }
331
332 /* Local URI MUST be a SIP or SIPS: */
333 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
334 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
335 {
336 acc->display = name_addr->display;
337 acc->user_part = name_addr->display;
338 acc->srv_domain = pj_str("");
339 acc->srv_port = 0;
340 } else {
341 pjsip_sip_uri *sip_uri;
342
343 /* Get the SIP URI object: */
344 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
345
346 /* Save the user and domain part. These will be used when finding an
347 * account for incoming requests.
348 */
349 acc->display = name_addr->display;
350 acc->user_part = sip_uri->user;
351 acc->srv_domain = sip_uri->host;
352 acc->srv_port = 0;
353
354 /* Escape user part (ticket #2010) */
355 if (acc->user_part.slen) {
356 const pjsip_parser_const_t *pconst;
357 char buf[PJSIP_MAX_URL_SIZE];
358 pj_str_t user_part;
359
360 pconst = pjsip_parser_const();
361 pj_strset(&user_part, buf, sizeof(buf));
362 pj_strncpy_escape(&user_part, &sip_uri->user, sizeof(buf),
363 &pconst->pjsip_USER_SPEC_LENIENT);
364 if (user_part.slen > acc->user_part.slen)
365 pj_strdup(acc->pool, &acc->user_part, &user_part);
366 }
367 }
368 acc->is_sips = PJSIP_URI_SCHEME_IS_SIPS(name_addr);
369
370
371 /* Parse registrar URI, if any */
372 if (acc_cfg->reg_uri.slen) {
373 pjsip_uri *reg_uri;
374
375 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
376 acc_cfg->reg_uri.slen, 0);
377 if (reg_uri == NULL) {
378 pjsua_perror(THIS_FILE, "Invalid registrar URI",
379 PJSIP_EINVALIDURI);
380 return PJSIP_EINVALIDURI;
381 }
382
383 /* Registrar URI MUST be a SIP or SIPS: */
384 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
385 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
386 {
387 pjsua_perror(THIS_FILE, "Invalid registar URI",
388 PJSIP_EINVALIDSCHEME);
389 return PJSIP_EINVALIDSCHEME;
390 }
391
392 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
393
394 } else {
395 sip_reg_uri = NULL;
396 }
397 pjsip_auth_clt_init( &acc->shared_auth_sess, pjsua_var.endpt, acc->pool, 0);
398
399 /* Configure async auth if pjsua callback is set */
400 if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
401 pjsip_auth_clt_async_setting async_opt;
402 pj_bzero(&async_opt, sizeof(async_opt));
403 async_opt.cb = &pjsua_auth_on_challenge;
404 async_opt.user_data = (void*)(pj_ssize_t)acc->index;
405 pjsip_auth_clt_async_configure(&acc->shared_auth_sess, &async_opt);
406 }
407
408 if (sip_reg_uri) {
409 acc->srv_port = sip_reg_uri->port;
410 }
411
412 /* Create Contact header if not present. */
413 //if (acc_cfg->contact.slen == 0) {
414 // acc_cfg->contact = acc_cfg->id;
415 //}
416
417 /* Build account route-set from outbound proxies and route set from
418 * account configuration.
419 */
420 pj_list_init(&acc->route_set);
421
422 if (!pj_list_empty(&pjsua_var.outbound_proxy)) {
423 pjsip_route_hdr *r;
424
425 r = pjsua_var.outbound_proxy.next;
426 while (r != &pjsua_var.outbound_proxy) {
427 pj_list_push_back(&acc->route_set,
428 pjsip_hdr_shallow_clone(acc->pool, r));
429 r = r->next;
430 }
431 }
432
433 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
434 pj_str_t hname = { "Route", 5};
435 pjsip_route_hdr *r;
436 pj_str_t tmp;
437
438 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
439 r = (pjsip_route_hdr*)
440 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
441 if (r == NULL) {
442 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
443 PJ_EINVAL);
444 return PJ_EINVAL;
445 }
446 pj_list_push_back(&acc->route_set, r);
447 }
448
449 /* Concatenate credentials from account config and global config */
450 acc->cred_cnt = 0;
451 for (i=0; i<acc_cfg->cred_count; ++i) {
452 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
453 }
454 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
455 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
456 {
457 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
458 }
459
460 /* Set credentials and preference on shared auth session so it can
461 * generate Authorization headers when used by the on_auth_challenge
462 * bridge.
463 */
464 if (acc->cred_cnt) {
465 pjsip_auth_clt_set_credentials(&acc->shared_auth_sess,
466 acc->cred_cnt, acc->cred);
467 }
468 pjsip_auth_clt_set_prefs(&acc->shared_auth_sess, &acc->cfg.auth_pref);
469
470 /* If account's ICE and TURN customization is not set, then
471 * initialize it with the settings from the global media config.
472 */
473 if (acc->cfg.ice_cfg_use == PJSUA_ICE_CONFIG_USE_DEFAULT) {
474 pjsua_ice_config_from_media_config(NULL, &acc->cfg.ice_cfg,
475 &pjsua_var.media_cfg);
476 }
477 if (acc->cfg.turn_cfg_use == PJSUA_TURN_CONFIG_USE_DEFAULT) {
478 pjsua_turn_config_from_media_config(NULL, &acc->cfg.turn_cfg,
479 &pjsua_var.media_cfg);
480 }
481
482 /* If ICE is enabled, add "+sip.ice" media feature tag in account's
483 * contact params.
484 */
485 #if PJSUA_ADD_ICE_TAGS
486 if (acc_cfg->ice_cfg.enable_ice) {
487 pj_ssize_t new_len;
488 pj_str_t new_prm;
489
490 new_len = acc_cfg->contact_params.slen + 10;
491 new_prm.ptr = (char*)pj_pool_alloc(acc->pool, new_len);
492 pj_strcpy(&new_prm, &acc_cfg->contact_params);
493 pj_strcat2(&new_prm, ";+sip.ice");
494 acc_cfg->contact_params = new_prm;
495 }
496 #endif
497
498 status = pjsua_pres_init_acc(acc_id);
499 if (status != PJ_SUCCESS)
500 return status;
501
502 /* If SIP outbound is enabled, generate instance and reg ID if they are
503 * not specified
504 */
505 if (acc_cfg->use_rfc5626) {
506 init_outbound_setting(acc);
507 }
508
509 /* Mark account as valid */
510 pjsua_var.acc[acc_id].valid = PJ_TRUE;
511
512 /* Insert account ID into account ID array, sorted by priority */
513 for (i=0; i<pjsua_var.acc_cnt; ++i) {
514 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
515 pjsua_var.acc[acc_id].cfg.priority)
516 {
517 break;
518 }
519 }
520 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
521 pjsua_var.acc_cnt, i, &acc_id);
522
523 if (acc_cfg->transport_id != PJSUA_INVALID_ID) {
524 acc->tp_type = pjsua_var.tpdata[acc_cfg->transport_id].type;
525 } else {
526 acc->tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
527 }
528
529 acc->ip_change_op = PJSUA_IP_CHANGE_OP_NULL;
530
531 return PJ_SUCCESS;
532 }
533
534
535 /*
536 * Add a new account to pjsua.
537 */
538 PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
539 pj_bool_t is_default,
540 pjsua_acc_id *p_acc_id)
541 {
542 pjsua_acc *acc;
543 unsigned i, id;
544 pj_status_t status = PJ_SUCCESS;
545
546 PJ_ASSERT_RETURN(cfg, PJ_EINVAL);
547 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
548 PJ_ETOOMANY);
549
550 #if !PJ_HAS_IPV6
551 PJ_ASSERT_RETURN(cfg->ipv6_sip_use == PJSUA_IPV6_DISABLED, PJ_EINVAL);
552 PJ_ASSERT_RETURN(cfg->ipv6_media_use == PJSUA_IPV6_DISABLED, PJ_EINVAL);
553 PJ_ASSERT_RETURN(cfg->nat64_opt == PJSUA_NAT64_DISABLED, PJ_EINVAL);
554 #endif
555
556 /* Must have a transport */
557 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
558
559 PJ_LOG(4,(THIS_FILE, "Adding account: id=%.*s",
560 (int)cfg->id.slen, cfg->id.ptr));
561 pj_log_push_indent();
562
563 PJSUA_LOCK();
564
565 /* Find empty account id. */
566 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
567 if (pjsua_var.acc[id].valid == PJ_FALSE)
568 break;
569 }
570
571 /* Expect to find a slot */
572 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
573 {PJSUA_UNLOCK(); return PJ_EBUG;});
574
575 acc = &pjsua_var.acc[id];
576
577 /* Create pool for this account. */
578 if (acc->pool)
579 pj_pool_reset(acc->pool);
580 else
581 acc->pool = pjsua_pool_create("acc%p", PJSUA_POOL_LEN_ACC,
582 PJSUA_POOL_INC_ACC);
583
584 /* Copy config */
585 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
586
587 /* Normalize registration timeout and refresh delay */
588 if (pjsua_var.acc[id].cfg.reg_uri.slen) {
589 if (pjsua_var.acc[id].cfg.reg_timeout == 0) {
590 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
591 }
592 if (pjsua_var.acc[id].cfg.reg_delay_before_refresh == 0) {
593 pjsua_var.acc[id].cfg.reg_delay_before_refresh =
594 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
595 }
596 }
597
598 /* Check the route URI's and force loose route if required */
599 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
600 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
601 if (status != PJ_SUCCESS) {
602 PJSUA_UNLOCK();
603 pj_log_pop_indent();
604 return status;
605 }
606 }
607
608 /* Get CRC of account proxy setting */
609 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
610
611 /* Get CRC of global outbound proxy setting */
612 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
613 pjsua_var.ua_cfg.outbound_proxy_cnt);
614
615 status = initialize_acc(id);
616 if (status != PJ_SUCCESS) {
617 pjsua_perror(THIS_FILE, "Error adding account", status);
618 pj_pool_release(acc->pool);
619 acc->pool = NULL;
620 PJSUA_UNLOCK();
621 pj_log_pop_indent();
622 return status;
623 }
624
625 if (is_default)
626 pjsua_var.default_acc = id;
627
628 if (p_acc_id)
629 *p_acc_id = id;
630
631 pjsua_var.acc_cnt++;
632
633 PJSUA_UNLOCK();
634
635 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
636 (int)cfg->id.slen, cfg->id.ptr, id));
637
638 /* If accounts has registration enabled, start registration */
639 if (pjsua_var.acc[id].cfg.reg_uri.slen) {
640 if (pjsua_var.acc[id].cfg.register_on_acc_add)
641 pjsua_acc_set_registration(id, PJ_TRUE);
642 } else {
643 /* Otherwise subscribe to MWI, if it's enabled */
644 if (pjsua_var.acc[id].cfg.mwi_enabled)
645 pjsua_start_mwi(id, PJ_TRUE);
646
647 /* Start publish too */
648 if (acc->cfg.publish_enabled)
649 pjsua_pres_init_publish_acc(id);
650 }
651
652 pj_log_pop_indent();
653 return PJ_SUCCESS;
654 }
655
656
657 /*
658 * Add local account
659 */
660 PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
661 pj_bool_t is_default,
662 pjsua_acc_id *p_acc_id)
663 {
664 pjsua_acc_config cfg;
665 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
666 char transport_param[32];
667 char uri[PJSIP_MAX_URL_SIZE];
668 char addr_buf[PJ_INET6_ADDRSTRLEN+10];
669 pjsua_acc_id acc_id;
670 pj_status_t status;
671
672 /* ID must be valid */
673 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
674 PJ_EINVAL);
675
676 /* Transport must be valid */
677 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
678
679 pjsua_acc_config_default(&cfg);
680
681 /* Lower the priority of local account */
682 --cfg.priority;
683
684 /* Don't add transport parameter if it's UDP */
685 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
686 pj_ansi_snprintf(transport_param, sizeof(transport_param),
687 ";transport=%s",
688 pjsip_transport_get_type_name(t->type));
689 } else {
690 transport_param[0] = '\0';
691 }
692
693 /* Build URI for the account */
694 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
695 "<sip:%s%s>",
696 pj_addr_str_print(&t->local_name.host, t->local_name.port,
697 addr_buf, sizeof(addr_buf), 1),
698 transport_param);
699
700 cfg.id = pj_str(uri);
701 cfg.transport_id = tid;
702
703 status = pjsua_acc_add(&cfg, is_default, &acc_id);
704 if (status == PJ_SUCCESS) {
705 pjsua_var.acc[acc_id].tp_type = t->type;
706 if (p_acc_id)
707 *p_acc_id = acc_id;
708 }
709
710 return status;
711 }
712
713
714 /*
715 * Set arbitrary data to be associated with the account.
716 */
717 PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
718 void *user_data)
719 {
720 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
721 PJ_EINVAL);
722 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
723
724 PJSUA_LOCK();
725
726 pjsua_var.acc[acc_id].cfg.user_data = user_data;
727
728 PJSUA_UNLOCK();
729
730 return PJ_SUCCESS;
731 }
732
733
734 /*
735 * Retrieve arbitrary data associated with the account.
736 */
737 PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
738 {
739 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
740 NULL);
741 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
742
743 return pjsua_var.acc[acc_id].cfg.user_data;
744 }
745
746
747 /*
748 * Delete account.
749 */
750 PJ_DEF(void) pjsua_acc_del_param_default(pjsua_acc_del_param *prm)
751 {
752 pj_bzero(prm, sizeof(*prm));
753 }
754
755
756 PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
757 {
758 pjsua_acc_del_param prm;
759 pjsua_acc_del_param_default(&prm);
760 prm.force = PJ_TRUE;
761 return pjsua_acc_del2(acc_id, &prm);
762 }
763
764
765 PJ_DEF(pj_status_t) pjsua_acc_del2(pjsua_acc_id acc_id,
766 const pjsua_acc_del_param *prm)
767 {
768 pjsua_acc *acc;
769 unsigned i;
770
771 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
772 PJ_EINVAL);
773 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
774
775 PJ_LOG(4,(THIS_FILE, "Deleting account %d..", acc_id));
776 pj_log_push_indent();
777
778 PJSUA_LOCK();
779
780 acc = &pjsua_var.acc[acc_id];
781
782 /* Check for active calls using this account */
783 for (i = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
784 if (pjsua_var.calls[i].acc_id == acc_id &&
785 (pjsua_var.calls[i].inv != NULL ||
786 pjsua_var.calls[i].async_call.dlg != NULL))
787 {
788 if (!prm->force) {
789 PJ_LOG(2, (THIS_FILE,
790 "Unable to delete account %d: call %d still "
791 "exists", acc_id, i));
792 PJSUA_UNLOCK();
793 pj_log_pop_indent();
794 return PJ_EBUSY;
795 }
796 PJ_LOG(2, (THIS_FILE,
797 "Warning: deleting account %d while call %d is "
798 "still active (forced)", acc_id, i));
799 break;
800 }
801 }
802
803 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
804 pjsua_buddy *b = &pjsua_var.buddy[i];
805
806 if (!pjsua_buddy_is_valid(i))
807 continue;
808 if (b->acc_id == acc_id) {
809 PJ_LOG(3, (THIS_FILE, "Warning: Account %d is used by "
810 "buddy %d. Disassociating it.",
811 acc_id, i));
812 b->acc_id = PJSUA_INVALID_ID;
813 }
814 }
815
816 /* Cancel keep-alive timer, if any */
817 if (acc->ka_timer.id) {
818 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
819 acc->ka_timer.id = PJ_FALSE;
820 }
821 if (acc->ka_transport) {
822 pjsip_transport_dec_ref(acc->ka_transport);
823 acc->ka_transport = NULL;
824 }
825
826 /* Cancel any re-registration timer */
827 if (acc->auto_rereg.timer.id) {
828 acc->auto_rereg.timer.id = PJ_FALSE;
829 pjsua_cancel_timer(&acc->auto_rereg.timer);
830 }
831
832 /* Delete registration */
833 if (acc->regc != NULL) {
834 pjsua_acc_set_registration(acc_id, PJ_FALSE);
835 destroy_regc(acc, PJ_TRUE);
836 }
837
838 /* Terminate mwi subscription */
839 if (acc->cfg.mwi_enabled) {
840 acc->cfg.mwi_enabled = PJ_FALSE;
841 pjsua_start_mwi(acc_id, PJ_FALSE);
842 }
843
844 /* Delete server presence subscription */
845 pjsua_pres_delete_acc(acc_id, 0);
846
847 /* Release & wipe account pool */
848 if (acc->pool) {
849 pj_pool_secure_release(&acc->pool);
850 }
851
852 /* Invalidate */
853 pj_bzero(acc, sizeof(*acc));
854 acc->index = acc_id;
855
856 /* Remove from array */
857 for (i=0; i<pjsua_var.acc_cnt; ++i) {
858 if (pjsua_var.acc_ids[i] == acc_id)
859 break;
860 }
861 if (i != pjsua_var.acc_cnt) {
862 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
863 pjsua_var.acc_cnt, i);
864 --pjsua_var.acc_cnt;
865 }
866
867 /* Ideally calls using this account should have been terminated
868 * before calling this function, unless force deletion is used.
869 */
870
871 /* Update default account */
872 if (pjsua_var.default_acc == acc_id)
873 pjsua_var.default_acc = 0;
874
875 #if PJ_HAS_SSL_SOCK
876 pj_turn_sock_tls_cfg_wipe_keys(&acc->cfg.turn_cfg.turn_tls_setting);
877 #endif
878
879 PJSUA_UNLOCK();
880
881 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
882
883 pj_log_pop_indent();
884 return PJ_SUCCESS;
885 }
886
887
888 /* Get config */
889 PJ_DEF(pj_status_t) pjsua_acc_get_config(pjsua_acc_id acc_id,
890 pj_pool_t *pool,
891 pjsua_acc_config *acc_cfg)
892 {
893 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc)
894 && pjsua_var.acc[acc_id].valid, PJ_EINVAL);
895 //this now would not work due to corrupt header list
896 //pj_memcpy(acc_cfg, &pjsua_var.acc[acc_id].cfg, sizeof(*acc_cfg));
897 pjsua_acc_config_dup(pool, acc_cfg, &pjsua_var.acc[acc_id].cfg);
898 return PJ_SUCCESS;
899 }
900
901 /* Compare two SIP headers. Return zero if equal */
902 static int pjsip_hdr_cmp(const pjsip_hdr *h1, const pjsip_hdr *h2)
903 {
904 char buf1[PJSIP_MAX_URL_SIZE];
905 char buf2[PJSIP_MAX_URL_SIZE];
906 pj_str_t p1, p2;
907
908 p1.ptr = buf1;
909 p1.slen = 0;
910 p2.ptr = buf2;
911 p2.slen = 0;
912
913 p1.slen = pjsip_hdr_print_on((void*)h1, buf1, sizeof(buf1));
914 if (p1.slen < 0)
915 p1.slen = 0;
916 p2.slen = pjsip_hdr_print_on((void*)h2, buf2, sizeof(buf2));
917 if (p2.slen < 0)
918 p2.slen = 0;
919
920 return pj_strcmp(&p1, &p2);
921 }
922
923 /* Update SIP header list from another list. Return PJ_TRUE if
924 * the list has been updated */
925 static pj_bool_t update_hdr_list(pj_pool_t *pool, pjsip_hdr *dst,
926 const pjsip_hdr *src)
927 {
928 pjsip_hdr *dst_i;
929 const pjsip_hdr *src_i;
930 pj_bool_t changed = PJ_FALSE;
931
932 /* Remove header that's no longer needed */
933 for (dst_i = dst->next; dst_i != dst; ) {
934 for (src_i = src->next; src_i != src; src_i = src_i->next) {
935 if (pjsip_hdr_cmp(dst_i, src_i) == 0)
936 break;
937 }
938 if (src_i == src) {
939 pjsip_hdr *next = dst_i->next;
940 pj_list_erase(dst_i);
941 changed = PJ_TRUE;
942 dst_i = next;
943 } else {
944 dst_i = dst_i->next;
945 }
946 }
947
948 /* Add new header */
949 for (src_i = src->next; src_i != src; src_i = src_i->next) {
950 for (dst_i = dst->next; dst_i != dst; dst_i = dst_i->next) {
951 if (pjsip_hdr_cmp(dst_i, src_i) == 0)
952 break;
953 }
954 if (dst_i == dst) {
955 dst_i = pjsip_hdr_clone(pool, src_i);
956 pj_list_push_back(dst, dst_i);
957 changed = PJ_TRUE;
958 }
959 }
960
961 return changed;
962 }
963
964 /*
965 * Modify account information.
966 */
967 PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
968 const pjsua_acc_config *cfg)
969 {
970 pjsua_acc *acc;
971 pjsip_name_addr *id_name_addr = NULL;
972 pjsip_sip_uri *id_sip_uri = NULL;
973 pjsip_sip_uri *reg_sip_uri = NULL;
974 pj_uint32_t local_route_crc;
975 pjsip_route_hdr local_route;
976 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
977 pj_bool_t update_reg = PJ_FALSE;
978 pj_bool_t unreg_first = PJ_FALSE;
979 pj_bool_t update_mwi = PJ_FALSE;
980 pj_status_t status = PJ_SUCCESS;
981
982 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
983 PJ_EINVAL);
984
985 #if !PJ_HAS_IPV6
986 PJ_ASSERT_RETURN(cfg->ipv6_sip_use == PJSUA_IPV6_DISABLED, PJ_EINVAL);
987 PJ_ASSERT_RETURN(cfg->ipv6_media_use == PJSUA_IPV6_DISABLED, PJ_EINVAL);
988 PJ_ASSERT_RETURN(cfg->nat64_opt == PJSUA_NAT64_DISABLED, PJ_EINVAL);
989 #endif
990
991 PJ_LOG(4,(THIS_FILE, "Modifying account %d", acc_id));
992 pj_log_push_indent();
993
994 PJSUA_LOCK();
995
996 acc = &pjsua_var.acc[acc_id];
997 if (!acc->valid) {
998 status = PJ_EINVAL;
999 goto on_return;
1000 }
1001
1002 /* == Validate first == */
1003
1004 /* Account id */
1005 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
1006 /* Need to parse id to get the elements: */
1007 id_name_addr = (pjsip_name_addr*)
1008 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
1009 PJSIP_PARSE_URI_AS_NAMEADDR);
1010 if (id_name_addr == NULL) {
1011 status = PJSIP_EINVALIDURI;
1012 pjsua_perror(THIS_FILE, "Invalid local URI", status);
1013 goto on_return;
1014 }
1015
1016 /* URI MUST be a SIP or SIPS: */
1017 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
1018 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
1019 {
1020 status = PJSIP_EINVALIDSCHEME;
1021 pjsua_perror(THIS_FILE, "Invalid local URI", status);
1022 goto on_return;
1023 }
1024
1025 /* Get the SIP URI object: */
1026 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
1027 }
1028
1029 /* Registrar URI */
1030 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
1031 pjsip_uri *reg_uri;
1032 unsigned rcnt;
1033
1034 /* Need to parse reg_uri to get the elements: */
1035 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
1036 cfg->reg_uri.slen, 0);
1037 if (reg_uri == NULL) {
1038 status = PJSIP_EINVALIDURI;
1039 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
1040 goto on_return;
1041 }
1042
1043 /* Registrar URI MUST be a SIP or SIPS: */
1044 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
1045 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
1046 {
1047 status = PJSIP_EINVALIDSCHEME;
1048 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
1049 goto on_return;
1050 }
1051
1052 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
1053
1054 /* Clear route headers from the previous registrar. */
1055 rcnt = (unsigned)pj_list_size(&acc->route_set);
1056 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1057 pjsip_route_hdr *hr;
1058 unsigned i;
1059
1060 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1061 hr=acc->route_set.prev;
1062 i<rcnt;
1063 ++i)
1064 {
1065 pjsip_route_hdr *prev = hr->prev;
1066 pj_list_erase(hr);
1067 hr = prev;
1068 }
1069 }
1070 }
1071
1072 /* REGISTER header list */
1073 if (update_hdr_list(acc->pool, &acc->cfg.reg_hdr_list, &cfg->reg_hdr_list)) {
1074 update_reg = PJ_TRUE;
1075 unreg_first = PJ_TRUE;
1076 }
1077
1078 /* SUBSCRIBE header list */
1079 update_hdr_list(acc->pool, &acc->cfg.sub_hdr_list, &cfg->sub_hdr_list);
1080
1081 /* Account proxy */
1082 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
1083 if (local_route_crc != acc->local_route_crc) {
1084 pjsip_route_hdr *r;
1085 unsigned i;
1086
1087 /* Validate the local route and save it to temporary var */
1088 pj_list_init(&local_route);
1089 for (i=0; i<cfg->proxy_cnt; ++i) {
1090 pj_str_t hname = { "Route", 5};
1091
1092 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
1093 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
1094 if (status != PJ_SUCCESS)
1095 goto on_return;
1096 r = (pjsip_route_hdr*)
1097 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
1098 acc_proxy[i].slen, NULL);
1099 if (r == NULL) {
1100 status = PJSIP_EINVALIDURI;
1101 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
1102 status);
1103 goto on_return;
1104 }
1105
1106 pj_list_push_back(&local_route, r);
1107 }
1108
1109 /* Recalculate the CRC again after route URI normalization */
1110 local_route_crc = calc_proxy_crc(acc_proxy, cfg->proxy_cnt);
1111 }
1112
1113 if (local_route_crc != acc->local_route_crc) {
1114 unsigned i;
1115 pjsip_route_hdr *r = &acc->route_set;
1116
1117 /* Remove the current account proxies from the route set */
1118 for (i = 0; i < pjsua_var.ua_cfg.outbound_proxy_cnt; i++)
1119 r = r->next;
1120 for (i = 0; i < acc->cfg.proxy_cnt; ++i) {
1121 pjsip_route_hdr *r_ = r->next;
1122 pj_list_erase(r_);
1123 }
1124
1125 /* Insert new proxy setting to the route set */
1126 pj_list_insert_list_after(r, &local_route);
1127
1128 /* Update the proxy setting */
1129 acc->cfg.proxy_cnt = cfg->proxy_cnt;
1130 for (i = 0; i < cfg->proxy_cnt; ++i)
1131 acc->cfg.proxy[i] = acc_proxy[i];
1132
1133 /* Update local route CRC */
1134 acc->local_route_crc = local_route_crc;
1135
1136 update_reg = PJ_TRUE;
1137 unreg_first = PJ_TRUE;
1138 }
1139
1140
1141 /* == Apply the new config == */
1142
1143 /* Account ID. */
1144 if (id_name_addr && id_sip_uri) {
1145 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
1146 pj_strdup_with_null(acc->pool, &acc->display, &id_name_addr->display);
1147 pj_strdup_with_null(acc->pool, &acc->user_part, &id_sip_uri->user);
1148 pj_strdup_with_null(acc->pool, &acc->srv_domain, &id_sip_uri->host);
1149 acc->srv_port = 0;
1150 acc->is_sips = PJSIP_URI_SCHEME_IS_SIPS(id_name_addr);
1151 update_reg = PJ_TRUE;
1152 unreg_first = PJ_TRUE;
1153 }
1154
1155 /* SIP IP version preference */
1156 if (acc->cfg.ipv6_sip_use != cfg->ipv6_sip_use) {
1157 acc->cfg.ipv6_sip_use = cfg->ipv6_sip_use;
1158 update_reg = PJ_TRUE;
1159 unreg_first = PJ_TRUE;
1160
1161 /* Set client registration's transport based on acc's config. */
1162 if (acc->regc) {
1163 pjsip_tpselector tp_sel;
1164
1165 pjsua_init_tpselector(acc->index, &tp_sel);
1166 pjsip_regc_set_transport(acc->regc, &tp_sel);
1167 }
1168 }
1169
1170 /* User data */
1171 acc->cfg.user_data = cfg->user_data;
1172
1173 /* Priority */
1174 if (acc->cfg.priority != cfg->priority) {
1175 unsigned i;
1176
1177 acc->cfg.priority = cfg->priority;
1178
1179 /* Resort accounts priority */
1180 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1181 if (pjsua_var.acc_ids[i] == acc_id)
1182 break;
1183 }
1184 pj_assert(i < pjsua_var.acc_cnt);
1185 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
1186 pjsua_var.acc_cnt, i);
1187 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1188 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
1189 acc->cfg.priority)
1190 {
1191 break;
1192 }
1193 }
1194 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
1195 pjsua_var.acc_cnt, i, &acc_id);
1196 }
1197
1198 /* MWI */
1199 if (acc->cfg.mwi_enabled != cfg->mwi_enabled) {
1200 acc->cfg.mwi_enabled = cfg->mwi_enabled;
1201 update_mwi = PJ_TRUE;
1202 }
1203 if (acc->cfg.mwi_expires != cfg->mwi_expires && cfg->mwi_expires > 0) {
1204 acc->cfg.mwi_expires = cfg->mwi_expires;
1205 update_mwi = PJ_TRUE;
1206 }
1207
1208 /* PIDF tuple ID */
1209 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
1210 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
1211 &cfg->pidf_tuple_id);
1212
1213 /* Publish */
1214 acc->cfg.publish_opt = cfg->publish_opt;
1215 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
1216 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
1217 acc->cfg.publish_enabled = cfg->publish_enabled;
1218 if (!acc->cfg.publish_enabled)
1219 pjsua_pres_unpublish(acc, 0);
1220 else
1221 update_reg = PJ_TRUE;
1222 }
1223
1224 /* Force contact URI */
1225 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
1226 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
1227 &cfg->force_contact);
1228 update_reg = PJ_TRUE;
1229 unreg_first = PJ_TRUE;
1230 }
1231
1232 /* Register contact params */
1233 if (pj_strcmp(&acc->cfg.reg_contact_params, &cfg->reg_contact_params)) {
1234 pj_strdup_with_null(acc->pool, &acc->cfg.reg_contact_params,
1235 &cfg->reg_contact_params);
1236 update_reg = PJ_TRUE;
1237 unreg_first = PJ_TRUE;
1238 }
1239
1240 /* Register contact URI params */
1241 if (pj_strcmp(&acc->cfg.reg_contact_uri_params,
1242 &cfg->reg_contact_uri_params))
1243 {
1244 pj_strdup_with_null(acc->pool, &acc->cfg.reg_contact_uri_params,
1245 &cfg->reg_contact_uri_params);
1246 update_reg = PJ_TRUE;
1247 unreg_first = PJ_TRUE;
1248 }
1249
1250 /* Contact param */
1251 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
1252 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
1253 &cfg->contact_params);
1254 update_reg = PJ_TRUE;
1255 unreg_first = PJ_TRUE;
1256 }
1257
1258 /* Contact URI params */
1259 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
1260 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
1261 &cfg->contact_uri_params);
1262 update_reg = PJ_TRUE;
1263 unreg_first = PJ_TRUE;
1264 }
1265
1266 /* Reliable provisional response */
1267 acc->cfg.require_100rel = cfg->require_100rel;
1268
1269 /* Session timer */
1270 acc->cfg.use_timer = cfg->use_timer;
1271 acc->cfg.timer_setting = cfg->timer_setting;
1272
1273 /* SIPREC */
1274 acc->cfg.use_siprec = cfg->use_siprec;
1275
1276 /* Transport */
1277 if (acc->cfg.transport_id != cfg->transport_id) {
1278 pjsua_acc_set_transport(acc_id, cfg->transport_id);
1279
1280 update_reg = PJ_TRUE;
1281 unreg_first = PJ_TRUE;
1282 }
1283
1284 /* Update keep-alive */
1285 if (acc->cfg.ka_interval != cfg->ka_interval ||
1286 pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
1287 {
1288 pjsip_transport *ka_transport = acc->ka_transport;
1289
1290 if (acc->ka_timer.id) {
1291 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1292 acc->ka_timer.id = PJ_FALSE;
1293 }
1294 if (acc->ka_transport) {
1295 pjsip_transport_dec_ref(acc->ka_transport);
1296 acc->ka_transport = NULL;
1297 }
1298
1299 acc->cfg.ka_interval = cfg->ka_interval;
1300
1301 if (cfg->ka_interval) {
1302 if (ka_transport) {
1303 /* Keep-alive has been running so we can just restart it */
1304 pj_time_val delay;
1305
1306 pjsip_transport_add_ref(ka_transport);
1307 acc->ka_transport = ka_transport;
1308
1309 acc->ka_timer.cb = &keep_alive_timer_cb;
1310 acc->ka_timer.user_data = (void*)acc;
1311
1312 delay.sec = acc->cfg.ka_interval;
1313 delay.msec = 0;
1314 status = pjsua_schedule_timer(&acc->ka_timer, &delay);
1315 if (status == PJ_SUCCESS) {
1316 acc->ka_timer.id = PJ_TRUE;
1317 } else {
1318 pjsip_transport_dec_ref(ka_transport);
1319 acc->ka_transport = NULL;
1320 pjsua_perror(THIS_FILE, "Error starting keep-alive timer",
1321 status);
1322 }
1323
1324 } else {
1325 /* Keep-alive has not been running, we need to (re)register
1326 * first.
1327 */
1328 update_reg = PJ_TRUE;
1329 }
1330 }
1331 }
1332
1333 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
1334 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
1335 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1336 acc->cfg.use_srtp = cfg->use_srtp;
1337 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
1338 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
1339 #endif
1340
1341 #if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
1342 acc->cfg.use_stream_ka = cfg->use_stream_ka;
1343 #endif
1344
1345 /* Use of proxy */
1346 if (acc->cfg.reg_use_proxy != cfg->reg_use_proxy) {
1347 acc->cfg.reg_use_proxy = cfg->reg_use_proxy;
1348 update_reg = PJ_TRUE;
1349 unreg_first = PJ_TRUE;
1350 }
1351
1352 /* Credential info */
1353 {
1354 unsigned i;
1355 pj_bool_t cred_changed = PJ_FALSE;
1356
1357 /* Selective update credential info. */
1358 for (i = 0; i < cfg->cred_count; ++i) {
1359 unsigned j;
1360 pjsip_cred_info ci;
1361
1362 /* Find if this credential is already listed */
1363 for (j = i; j < acc->cfg.cred_count; ++j) {
1364 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
1365 &cfg->cred_info[i]) == 0)
1366 {
1367 /* Found, but different index/position, swap */
1368 if (j != i) {
1369 ci = acc->cfg.cred_info[i];
1370 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
1371 acc->cfg.cred_info[j] = ci;
1372 }
1373 break;
1374 }
1375 }
1376
1377 /* Not found, insert this */
1378 if (j == acc->cfg.cred_count) {
1379 cred_changed = PJ_TRUE;
1380
1381 /* If account credential is full, discard the last one. */
1382 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
1383 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
1384 acc->cfg.cred_count, acc->cfg.cred_count-1);
1385 acc->cfg.cred_count--;
1386 }
1387
1388 /* Insert this */
1389 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
1390 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
1391 acc->cfg.cred_count, i, &ci);
1392 }
1393 }
1394 acc->cfg.cred_count = cfg->cred_count;
1395
1396 /* Concatenate credentials from account config and global config */
1397 acc->cred_cnt = 0;
1398 for (i=0; i<acc->cfg.cred_count; ++i) {
1399 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
1400 }
1401 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
1402 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
1403 {
1404 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
1405 }
1406
1407 if (cred_changed) {
1408 update_reg = PJ_TRUE;
1409 unreg_first = PJ_TRUE;
1410 }
1411
1412 /* Propagate updated credentials to shared auth session */
1413 pjsip_auth_clt_set_credentials(&acc->shared_auth_sess,
1414 acc->cred_cnt, acc->cred);
1415 }
1416
1417 /* Authentication preference */
1418 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
1419 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm)) {
1420 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
1421 &cfg->auth_pref.algorithm);
1422 update_reg = PJ_TRUE;
1423 unreg_first = PJ_TRUE;
1424 }
1425 pjsip_auth_clt_set_prefs(&acc->shared_auth_sess, &acc->cfg.auth_pref);
1426
1427 /* Shared authentication session */
1428 acc->cfg.use_shared_auth = cfg->use_shared_auth;
1429
1430 /* Registration */
1431 if (acc->cfg.reg_timeout != cfg->reg_timeout) {
1432 acc->cfg.reg_timeout = cfg->reg_timeout;
1433 if (acc->regc != NULL)
1434 pjsip_regc_update_expires(acc->regc, acc->cfg.reg_timeout);
1435
1436 update_reg = PJ_TRUE;
1437 }
1438 acc->cfg.unreg_timeout = cfg->unreg_timeout;
1439 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
1440 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
1441 acc->cfg.reg_first_retry_interval = cfg->reg_first_retry_interval;
1442 acc->cfg.reg_retry_random_interval = cfg->reg_retry_random_interval;
1443 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
1444 acc->cfg.register_on_acc_add = cfg->register_on_acc_add;
1445 if (acc->cfg.reg_delay_before_refresh != cfg->reg_delay_before_refresh) {
1446 acc->cfg.reg_delay_before_refresh = cfg->reg_delay_before_refresh;
1447 if (acc->regc != NULL)
1448 pjsip_regc_set_delay_before_refresh(acc->regc,
1449 cfg->reg_delay_before_refresh);
1450 }
1451
1452 /* Allow via rewrite */
1453 if (acc->cfg.allow_via_rewrite != cfg->allow_via_rewrite) {
1454 if (acc->regc != NULL) {
1455 if (cfg->allow_via_rewrite) {
1456 pjsip_regc_set_via_sent_by(acc->regc, &acc->via_addr,
1457 acc->via_tp);
1458 } else
1459 pjsip_regc_set_via_sent_by(acc->regc, NULL, NULL);
1460 }
1461 if (acc->publish_sess != NULL) {
1462 if (cfg->allow_via_rewrite) {
1463 pjsip_publishc_set_via_sent_by(acc->publish_sess,
1464 &acc->via_addr, acc->via_tp);
1465 } else
1466 pjsip_publishc_set_via_sent_by(acc->publish_sess, NULL, NULL);
1467 }
1468 acc->cfg.allow_via_rewrite = cfg->allow_via_rewrite;
1469 }
1470
1471 /* Normalize registration timeout and refresh delay */
1472 if (acc->cfg.reg_uri.slen ) {
1473 if (acc->cfg.reg_timeout == 0) {
1474 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
1475 }
1476 if (acc->cfg.reg_delay_before_refresh == 0) {
1477 acc->cfg.reg_delay_before_refresh =
1478 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
1479 }
1480 }
1481
1482 /* Registrar URI */
1483 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
1484 if (cfg->reg_uri.slen) {
1485 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
1486 if (reg_sip_uri)
1487 acc->srv_port = reg_sip_uri->port;
1488 }
1489 update_reg = PJ_TRUE;
1490 unreg_first = PJ_TRUE;
1491 }
1492
1493 /* SIP outbound setting */
1494 if (acc->cfg.use_rfc5626 != cfg->use_rfc5626 ||
1495 pj_strcmp(&acc->cfg.rfc5626_instance_id, &cfg->rfc5626_instance_id) ||
1496 pj_strcmp(&acc->cfg.rfc5626_reg_id, &cfg->rfc5626_reg_id))
1497 {
1498 if (acc->cfg.use_rfc5626 != cfg->use_rfc5626)
1499 acc->cfg.use_rfc5626 = cfg->use_rfc5626;
1500
1501 if (pj_strcmp(&acc->cfg.rfc5626_instance_id,
1502 &cfg->rfc5626_instance_id))
1503 {
1504 pj_strdup_with_null(acc->pool, &acc->cfg.rfc5626_instance_id,
1505 &cfg->rfc5626_instance_id);
1506 }
1507 if (pj_strcmp(&acc->cfg.rfc5626_reg_id, &cfg->rfc5626_reg_id)) {
1508 pj_strdup_with_null(acc->pool, &acc->cfg.rfc5626_reg_id,
1509 &cfg->rfc5626_reg_id);
1510 }
1511 init_outbound_setting(acc);
1512 update_reg = PJ_TRUE;
1513 unreg_first = PJ_TRUE;
1514 }
1515
1516 /* Video settings */
1517 acc->cfg.vid_in_auto_show = cfg->vid_in_auto_show;
1518 acc->cfg.vid_out_auto_transmit = cfg->vid_out_auto_transmit;
1519 acc->cfg.vid_wnd_flags = cfg->vid_wnd_flags;
1520 acc->cfg.vid_cap_dev = cfg->vid_cap_dev;
1521 acc->cfg.vid_rend_dev = cfg->vid_rend_dev;
1522 acc->cfg.vid_stream_rc_cfg = cfg->vid_stream_rc_cfg;
1523
1524 /* Media settings */
1525 if (acc->cfg.rtp_cfg.port != cfg->rtp_cfg.port) {
1526 /* Note that changing port range only changes the calculation of
1527 * the next rtp port but doesn't reset it.
1528 */
1529 acc->next_rtp_port = 0;
1530 }
1531
1532 /* Text settings. */
1533 acc->cfg.txt_red_level = cfg->txt_red_level;
1534
1535 if (pj_stricmp(&acc->cfg.rtp_cfg.public_addr, &cfg->rtp_cfg.public_addr) ||
1536 pj_stricmp(&acc->cfg.rtp_cfg.bound_addr, &cfg->rtp_cfg.bound_addr))
1537 {
1538 pjsua_transport_config_dup(acc->pool, &acc->cfg.rtp_cfg,
1539 &cfg->rtp_cfg);
1540 } else {
1541 pj_str_t p_addr = acc->cfg.rtp_cfg.public_addr;
1542 pj_str_t b_addr = acc->cfg.rtp_cfg.bound_addr;
1543
1544 /* ..to save memory by not using the pool */
1545 acc->cfg.rtp_cfg = cfg->rtp_cfg;
1546 acc->cfg.rtp_cfg.public_addr = p_addr;
1547 acc->cfg.rtp_cfg.bound_addr = b_addr;
1548 }
1549
1550 acc->cfg.nat64_opt = PJ_HAS_IPV6? cfg->nat64_opt :
1551 PJSUA_NAT64_DISABLED;
1552 acc->cfg.ipv6_media_use = PJ_HAS_IPV6? cfg->ipv6_media_use :
1553 PJSUA_IPV6_DISABLED;
1554 acc->cfg.enable_rtcp_mux = cfg->enable_rtcp_mux;
1555 acc->cfg.lock_codec = cfg->lock_codec;
1556 acc->cfg.enable_rtcp_xr = cfg->enable_rtcp_xr;
1557
1558 /* STUN and Media customization */
1559 if (acc->cfg.sip_stun_use != cfg->sip_stun_use) {
1560 acc->cfg.sip_stun_use = cfg->sip_stun_use;
1561 update_reg = PJ_TRUE;
1562 }
1563 acc->cfg.media_stun_use = cfg->media_stun_use;
1564
1565 /* ICE settings */
1566 acc->cfg.ice_cfg_use = cfg->ice_cfg_use;
1567 switch (acc->cfg.ice_cfg_use) {
1568 case PJSUA_ICE_CONFIG_USE_DEFAULT:
1569 /* Copy ICE settings from media settings so that we don't need to
1570 * check the media config if we look for ICE config.
1571 */
1572 pjsua_ice_config_from_media_config(NULL, &acc->cfg.ice_cfg,
1573 &pjsua_var.media_cfg);
1574 break;
1575 case PJSUA_ICE_CONFIG_USE_CUSTOM:
1576 pjsua_ice_config_dup(acc->pool, &acc->cfg.ice_cfg, &cfg->ice_cfg);
1577 break;
1578 }
1579
1580 /* TURN settings */
1581 acc->cfg.turn_cfg_use = cfg->turn_cfg_use;
1582 switch (acc->cfg.turn_cfg_use) {
1583 case PJSUA_TURN_CONFIG_USE_DEFAULT:
1584 /* Copy TURN settings from media settings so that we don't need to
1585 * check the media config if we look for TURN config.
1586 */
1587 pjsua_turn_config_from_media_config(NULL, &acc->cfg.turn_cfg,
1588 &pjsua_var.media_cfg);
1589 break;
1590 case PJSUA_TURN_CONFIG_USE_CUSTOM:
1591 pjsua_turn_config_dup(acc->pool, &acc->cfg.turn_cfg,
1592 &cfg->turn_cfg);
1593 break;
1594 }
1595
1596 acc->cfg.use_srtp = cfg->use_srtp;
1597
1598 /* Call hold type */
1599 acc->cfg.call_hold_type = cfg->call_hold_type;
1600
1601 /* Unregister first */
1602 if (unreg_first) {
1603 if (acc->regc && !cfg->disable_reg_on_modify) {
1604 status = pjsua_acc_set_registration(acc->index, PJ_FALSE);
1605 if (status != PJ_SUCCESS) {
1606 pjsua_perror(THIS_FILE, "Ignored failure in unregistering the "
1607 "old account setting in modifying account",
1608 status);
1609 /* Not really sure if we should return error */
1610 status = PJ_SUCCESS;
1611 }
1612 }
1613 destroy_regc(acc, PJ_TRUE);
1614
1615 if (!cfg->reg_uri.slen) {
1616 /* Reg URI still needed, delay unset after sending unregister. */
1617 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
1618 }
1619 }
1620
1621 /* Update registration */
1622 if (update_reg && !cfg->disable_reg_on_modify) {
1623 /* If accounts has registration enabled, start registration */
1624 if (acc->cfg.reg_uri.slen) {
1625 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
1626 if (status != PJ_SUCCESS) {
1627 pjsua_perror(THIS_FILE, "Failed to register with new account "
1628 "setting in modifying account", status);
1629 goto on_return;
1630 }
1631 }
1632 }
1633
1634 /* Update MWI subscription */
1635 if (update_mwi) {
1636 status = pjsua_start_mwi(acc_id, PJ_TRUE);
1637 if (status != PJ_SUCCESS) {
1638 pjsua_perror(THIS_FILE, "Failed in starting MWI subscription for "
1639 "new account setting in modifying account", status);
1640 }
1641 }
1642
1643 /* IP Change config */
1644 acc->cfg.ip_change_cfg.shutdown_tp = cfg->ip_change_cfg.shutdown_tp;
1645 acc->cfg.ip_change_cfg.hangup_calls = cfg->ip_change_cfg.hangup_calls;
1646 acc->cfg.ip_change_cfg.reinvite_flags = cfg->ip_change_cfg.reinvite_flags;
1647
1648 /* SRTP setting */
1649 pjsua_srtp_opt_dup(acc->pool, &acc->cfg.srtp_opt, &cfg->srtp_opt,
1650 PJ_TRUE);
1651
1652 /* RTCP-FB config */
1653 pjmedia_rtcp_fb_setting_dup(acc->pool, &acc->cfg.rtcp_fb_cfg,
1654 &cfg->rtcp_fb_cfg);
1655
1656 on_return:
1657 PJSUA_UNLOCK();
1658 pj_log_pop_indent();
1659 return status;
1660 }
1661
1662 typedef struct send_request_data
1663 {
1664 pjsua_acc_id acc_id;
1665 void *token;
1666 } send_request_data;
1667
1668 static void on_send_request(void *request_data, pjsip_event *event)
1669 {
1670 send_request_data *data = (send_request_data*) request_data;
1671 if (data && pjsua_var.ua_cfg.cb.on_acc_send_request)
1672 (pjsua_var.ua_cfg.cb.on_acc_send_request)(data->acc_id, data->token, event);
1673 }
1674
1675 PJ_DEF(pj_status_t) pjsua_acc_send_request(pjsua_acc_id acc_id,
1676 const pj_str_t *dest_uri,
1677 const pj_str_t *method,
1678 void *options,
1679 void *token,
1680 const pjsua_msg_data *msg_data)
1681 {
1682 const pjsip_hdr *cap_hdr = NULL;
1683 pjsip_method method_;
1684 pj_status_t status;
1685 pjsip_tx_data *tdata = NULL;
1686 send_request_data *request_data = NULL;
1687
1688 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1689 PJ_EINVAL);
1690 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1691 PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL);
1692 PJ_ASSERT_RETURN(method, PJ_EINVAL);
1693 PJ_UNUSED_ARG(options);
1694
1695 PJ_LOG(4,(THIS_FILE, "Account %d sending %.*s request..",
1696 acc_id, (int)method->slen, method->ptr));
1697 pj_log_push_indent();
1698
1699 pjsip_method_init_np(&method_, (pj_str_t*)method);
1700 status = pjsua_acc_create_request(acc_id, &method_, dest_uri, &tdata);
1701 if (status != PJ_SUCCESS) {
1702 pjsua_perror(THIS_FILE, "Unable to create request", status);
1703 goto on_return;
1704 }
1705
1706 request_data = PJ_POOL_ZALLOC_T(tdata->pool, send_request_data);
1707 if (!request_data) {
1708 status = PJ_ENOMEM;
1709 goto on_return;
1710 }
1711 request_data->acc_id = acc_id;
1712 request_data->token = token;
1713
1714 if (msg_data)
1715 pjsua_process_msg_data(tdata, msg_data);
1716
1717 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
1718 if (cap_hdr) {
1719 pjsip_msg_add_hdr(tdata->msg,
1720 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
1721 }
1722
1723 status = pjsip_endpt_send_request(pjsua_var.endpt, tdata, -1, request_data, &on_send_request);
1724 if (status != PJ_SUCCESS) {
1725 pjsua_perror(THIS_FILE, "Unable to send request", status);
1726 goto on_return;
1727 }
1728
1729 on_return:
1730 pj_log_pop_indent();
1731 return status;
1732 }
1733
1734
1735 /*
1736 * Modify account's presence status to be advertised to remote/presence
1737 * subscribers.
1738 */
1739 PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
1740 pj_bool_t is_online)
1741 {
1742 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1743 PJ_EINVAL);
1744 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1745
1746 PJ_LOG(4,(THIS_FILE, "Acc %d: setting online status to %d..",
1747 acc_id, is_online));
1748 pj_log_push_indent();
1749
1750 pjsua_var.acc[acc_id].online_status = is_online;
1751 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
1752 pjsua_pres_update_acc(acc_id, PJ_FALSE);
1753
1754 pj_log_pop_indent();
1755 return PJ_SUCCESS;
1756 }
1757
1758
1759 /*
1760 * Set online status with extended information
1761 */
1762 PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
1763 pj_bool_t is_online,
1764 const pjrpid_element *pr)
1765 {
1766 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1767 PJ_EINVAL);
1768 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1769
1770 PJ_LOG(4,(THIS_FILE, "Acc %d: setting online status to %d..",
1771 acc_id, is_online));
1772 pj_log_push_indent();
1773
1774 PJSUA_LOCK();
1775 pjsua_var.acc[acc_id].online_status = is_online;
1776 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
1777 PJSUA_UNLOCK();
1778
1779 pjsua_pres_update_acc(acc_id, PJ_TRUE);
1780 pj_log_pop_indent();
1781
1782 return PJ_SUCCESS;
1783 }
1784
1785 /* Create reg_contact, adding SIP outbound params and other REGISTER specific
1786 * Contact params, i.e: reg_contact_params, reg_contact_uri_params.
1787 */
1788 static void update_regc_contact(pjsua_acc *acc)
1789 {
1790 pjsua_acc_config *acc_cfg = &acc->cfg;
1791 pj_bool_t need_outbound = PJ_FALSE;
1792 const pj_str_t tcp_param = pj_str(";transport=tcp");
1793 const pj_str_t tls_param = pj_str(";transport=tls");
1794
1795 if (!acc_cfg->use_rfc5626)
1796 goto done;
1797
1798 /* Check if outbound has been requested and rejected */
1799 if (acc->rfc5626_status == OUTBOUND_NA)
1800 goto done;
1801
1802 if (pj_stristr(&acc->contact, &tcp_param)==NULL &&
1803 pj_stristr(&acc->contact, &tls_param)==NULL)
1804 {
1805 /* Currently we can only do SIP outbound for TCP
1806 * and TLS.
1807 */
1808 goto done;
1809 }
1810
1811 /* looks like we can use outbound */
1812 need_outbound = PJ_TRUE;
1813
1814 done:
1815 {
1816 pj_ssize_t len;
1817 pj_str_t reg_contact;
1818
1819 acc->rfc5626_status = OUTBOUND_WANTED;
1820 len = acc->contact.slen +
1821 acc->cfg.contact_params.slen +
1822 acc->cfg.reg_contact_params.slen +
1823 acc->cfg.reg_contact_uri_params.slen +
1824 (need_outbound?
1825 (acc->rfc5626_instprm.slen + acc->rfc5626_regprm.slen): 0) +
1826 5; /* allowance */
1827 if (len > acc->contact.slen) {
1828 reg_contact.ptr = (char*) pj_pool_alloc(acc->pool, len);
1829
1830 pj_strcpy(&reg_contact, &acc->contact);
1831
1832 /* Contact URI params */
1833 if (acc->cfg.reg_contact_uri_params.slen) {
1834 pj_pool_t *pool;
1835 pjsip_contact_hdr *contact_hdr;
1836 pjsip_sip_uri *uri;
1837 pj_str_t uri_param = acc->cfg.reg_contact_uri_params;
1838 const pj_str_t STR_CONTACT = { "Contact", 7 };
1839 char tmp_uri[PJSIP_MAX_URL_SIZE];
1840 pj_ssize_t tmp_len;
1841
1842 /* Get the URI string */
1843 pool = pjsua_pool_create("tmp", 512, 512);
1844 contact_hdr = (pjsip_contact_hdr*)
1845 pjsip_parse_hdr(pool, &STR_CONTACT,
1846 reg_contact.ptr,
1847 reg_contact.slen, NULL);
1848 pj_assert(contact_hdr != NULL);
1849 uri = (pjsip_sip_uri*) contact_hdr->uri;
1850 pj_assert(uri != NULL);
1851 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
1852 tmp_len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
1853 uri, tmp_uri,
1854 sizeof(tmp_uri));
1855 pj_assert(tmp_len > 0);
1856 pj_pool_release(pool);
1857
1858 /* Regenerate Contact */
1859 reg_contact.slen = pj_ansi_snprintf(
1860 reg_contact.ptr, len,
1861 "<%.*s%.*s>%.*s",
1862 (int)tmp_len, tmp_uri,
1863 (int)uri_param.slen, uri_param.ptr,
1864 (int)acc->cfg.contact_params.slen,
1865 acc->cfg.contact_params.ptr);
1866 pj_assert(reg_contact.slen > 0);
1867 }
1868
1869 /* Outbound */
1870 if (need_outbound) {
1871 acc->rfc5626_status = OUTBOUND_WANTED;
1872
1873 /* Need to use outbound, append the contact with
1874 * +sip.instance and reg-id parameters.
1875 */
1876 pj_strcat(&reg_contact, &acc->rfc5626_regprm);
1877 pj_strcat(&reg_contact, &acc->rfc5626_instprm);
1878 } else {
1879 acc->rfc5626_status = OUTBOUND_NA;
1880 }
1881
1882 /* Contact params */
1883 pj_strcat(&reg_contact, &acc->cfg.reg_contact_params);
1884
1885 acc->reg_contact = reg_contact;
1886
1887 PJ_LOG(4,(THIS_FILE,
1888 "Contact for acc %d updated: %.*s",
1889 acc->index,
1890 (int)acc->reg_contact.slen,
1891 acc->reg_contact.ptr));
1892
1893 } else {
1894 /* Outbound is not needed/wanted for the account and there's
1895 * no custom registration Contact params. acc->reg_contact
1896 * is set to the same as acc->contact.
1897 */
1898 acc->reg_contact = acc->contact;
1899 acc->rfc5626_status = OUTBOUND_NA;
1900 }
1901 }
1902 }
1903
1904 /* Check if IP is private IP address */
1905 static pj_bool_t is_private_ip(const pj_str_t *addr)
1906 {
1907 const pj_str_t private_net[] =
1908 {
1909 { "10.", 3 },
1910 { "127.", 4 },
1911 { "172.16.", 7 }, { "172.17.", 7 }, { "172.18.", 7 }, { "172.19.", 7 },
1912 { "172.20.", 7 }, { "172.21.", 7 }, { "172.22.", 7 }, { "172.23.", 7 },
1913 { "172.24.", 7 }, { "172.25.", 7 }, { "172.26.", 7 }, { "172.27.", 7 },
1914 { "172.28.", 7 }, { "172.29.", 7 }, { "172.30.", 7 }, { "172.31.", 7 },
1915 { "192.168.", 8 }
1916 };
1917 unsigned i;
1918
1919 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
1920 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
1921 return PJ_TRUE;
1922 }
1923
1924 return PJ_FALSE;
1925 }
1926
1927 /* Update NAT address from the REGISTER response */
1928 static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
1929 int contact_rewrite_method,
1930 struct pjsip_regc_cbparam *param)
1931 {
1932 pjsip_transport *tp;
1933 const pj_str_t *via_addr;
1934 pj_pool_t *pool;
1935 int rport;
1936 pjsip_sip_uri *uri;
1937 pjsip_via_hdr *via;
1938 pj_sockaddr contact_addr;
1939 pj_sockaddr recv_addr = {{0}};
1940 pj_status_t status;
1941 pj_bool_t matched;
1942 pj_str_t srv_ip;
1943 pjsip_contact_hdr *contact_hdr;
1944 char host_addr_buf[PJ_INET6_ADDRSTRLEN+10];
1945 char via_addr_buf[PJ_INET6_ADDRSTRLEN+10];
1946 pj_str_t recv_addr_str;
1947 char recv_addr_buf[PJ_INET6_ADDRSTRLEN+10];
1948 const pj_str_t STR_CONTACT = { "Contact", 7 };
1949
1950 tp = param->rdata->tp_info.transport;
1951
1952 /* Get the received and rport info */
1953 via = param->rdata->msg_info.via;
1954 if (via->rport_param < 1) {
1955 /* Remote doesn't support rport */
1956 rport = via->sent_by.port;
1957 if (rport==0) {
1958 pjsip_transport_type_e tp_type;
1959 tp_type = (pjsip_transport_type_e) tp->key.type;
1960 rport = pjsip_transport_get_default_port_for_type(tp_type);
1961 }
1962 } else
1963 rport = via->rport_param;
1964
1965 if (via->recvd_param.slen != 0)
1966 via_addr = &via->recvd_param;
1967 else
1968 via_addr = &via->sent_by.host;
1969
1970 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1971 &recv_addr);
1972 /* Even though against the RFC, some registrars may put port number in
1973 * via received param. Just ignore the port.
1974 */
1975 if (status == PJ_SUCCESS && pj_sockaddr_get_port(&recv_addr) != 0) {
1976 pj_sockaddr_set_port(&recv_addr, 0);
1977 pj_sockaddr_print(&recv_addr, recv_addr_buf, sizeof(recv_addr_buf), 0);
1978 recv_addr_str = pj_str(recv_addr_buf);
1979 via_addr = &recv_addr_str;
1980 }
1981
1982 /* If allow_via_rewrite is enabled, we save the Via "received" address
1983 * from the response, if either of the following condition is met:
1984 * - the Via "received" address differs from saved one (or we haven't
1985 * saved any yet)
1986 * - transport is different
1987 * - only the port has changed, AND either the received address is
1988 * public IP or allow_contact_rewrite is 2
1989 */
1990 if (acc->cfg.allow_via_rewrite &&
1991 (pj_strcmp(&acc->via_addr.host, via_addr) || acc->via_tp != tp ||
1992 (acc->via_addr.port != rport &&
1993 (!is_private_ip(via_addr) || acc->cfg.allow_contact_rewrite == 2))))
1994 {
1995 if (pj_strcmp(&acc->via_addr.host, via_addr))
1996 pj_strdup(acc->pool, &acc->via_addr.host, via_addr);
1997 acc->via_addr.port = rport;
1998 acc->via_tp = tp;
1999 pjsip_regc_set_via_sent_by(acc->regc, &acc->via_addr, acc->via_tp);
2000 if (acc->publish_sess != NULL) {
2001 pjsip_publishc_set_via_sent_by(acc->publish_sess,
2002 &acc->via_addr, acc->via_tp);
2003 }
2004 }
2005
2006 /* Save mapped address if needed */
2007 if (acc->cfg.allow_sdp_nat_rewrite &&
2008 pj_strcmp(&acc->reg_mapped_addr, via_addr))
2009 {
2010 pj_strdup(acc->pool, &acc->reg_mapped_addr, via_addr);
2011 }
2012
2013 /* Only update if account is configured to auto-update */
2014 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
2015 return PJ_FALSE;
2016
2017 /* If SIP outbound is active, no need to update */
2018 if (acc->rfc5626_status == OUTBOUND_ACTIVE) {
2019 PJ_LOG(4,(THIS_FILE, "Acc %d has SIP outbound active, no need to "
2020 "update registration Contact", acc->index));
2021 return PJ_FALSE;
2022 }
2023
2024 #if 0
2025 // Always update
2026 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
2027
2028 /* For UDP, only update if STUN is enabled (for now).
2029 * For TCP/TLS, always check.
2030 */
2031 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
2032 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
2033 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
2034 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
2035 (tp->key.type == PJSIP_TRANSPORT_TLS))
2036 {
2037 /* Yes we will check */
2038 } else {
2039 return PJ_FALSE;
2040 }
2041 #endif
2042
2043 /* Compare received and rport with the URI in our registration */
2044 pool = pjsua_pool_create("tmp", 512, 512);
2045 contact_hdr = (pjsip_contact_hdr*)
2046 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
2047 acc->contact.slen, NULL);
2048 pj_assert(contact_hdr != NULL);
2049 uri = (pjsip_sip_uri*) contact_hdr->uri;
2050 pj_assert(uri != NULL);
2051 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
2052
2053 if (uri->port == 0) {
2054 pjsip_transport_type_e tp_type;
2055 tp_type = (pjsip_transport_type_e) tp->key.type;
2056 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
2057 }
2058
2059 /* Convert IP address strings into sockaddr for comparison.
2060 * (https://github.com/pjsip/pjproject/issues/863)
2061 */
2062 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
2063 &contact_addr);
2064 if (status == PJ_SUCCESS && pj_sockaddr_has_addr(&recv_addr)) {
2065 /* Compare the addresses as sockaddr according to the ticket above,
2066 * but only if they have the same family (ipv4 vs ipv4, or
2067 * ipv6 vs ipv6).
2068 * Checking for the same address family is currently disabled,
2069 * since it can be useful in cases such as when on NAT64,
2070 * in order to get the IPv4-mapped address from IPv6.
2071 */
2072 matched = //(contact_addr.addr.sa_family != recv_addr.addr.sa_family)||
2073 (uri->port == rport &&
2074 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
2075 } else {
2076 /* Compare the addresses as string, as before */
2077 matched = (uri->port == rport &&
2078 pj_stricmp(&uri->host, via_addr)==0);
2079 }
2080
2081 if (matched) {
2082 /* Address doesn't change */
2083 pj_pool_release(pool);
2084 return PJ_FALSE;
2085 }
2086
2087 /* Get server IP */
2088 srv_ip = pj_str(param->rdata->pkt_info.src_name);
2089
2090 /* At this point we've detected that the address as seen by registrar.
2091 * has changed.
2092 */
2093
2094 /* Do not switch if both Contact and server's IP address are
2095 * public but response contains private IP. A NAT in the middle
2096 * might have messed up with the SIP packets. See:
2097 * https://github.com/pjsip/pjproject/issues/643
2098 *
2099 * This exception can be disabled by setting allow_contact_rewrite
2100 * to 2. In this case, the switch will always be done whenever there
2101 * is difference in the IP address in the response.
2102 */
2103 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
2104 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
2105 {
2106 /* Don't switch */
2107 pj_pool_release(pool);
2108 return PJ_FALSE;
2109 }
2110
2111 /* Also don't switch if only the port number part is different, and
2112 * the Via received address is private.
2113 * See https://github.com/pjsip/pjproject/issues/864
2114 */
2115 if (acc->cfg.allow_contact_rewrite != 2 &&
2116 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
2117 is_private_ip(via_addr))
2118 {
2119 /* Don't switch */
2120 pj_pool_release(pool);
2121 return PJ_FALSE;
2122 }
2123 pj_addr_str_print(&uri->host, uri->port, host_addr_buf,
2124 sizeof(host_addr_buf), 1);
2125 pj_addr_str_print(via_addr, rport, via_addr_buf,
2126 sizeof(via_addr_buf), 1);
2127 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
2128 "(%s --> %s). Updating registration "
2129 "(using method %d)",
2130 acc->index, host_addr_buf, via_addr_buf,
2131 contact_rewrite_method));
2132
2133 pj_assert(contact_rewrite_method == PJSUA_CONTACT_REWRITE_UNREGISTER ||
2134 contact_rewrite_method == PJSUA_CONTACT_REWRITE_NO_UNREG ||
2135 contact_rewrite_method == PJSUA_CONTACT_REWRITE_ALWAYS_UPDATE);
2136
2137 if (contact_rewrite_method == PJSUA_CONTACT_REWRITE_UNREGISTER) {
2138 /* Unregister current contact */
2139 pjsua_acc_set_registration(acc->index, PJ_FALSE);
2140 destroy_regc(acc, PJ_TRUE);
2141 }
2142
2143 /*
2144 * Build new Contact header
2145 */
2146 {
2147 const char *ob = ";ob";
2148 char *tmp;
2149 const char *beginquote, *endquote;
2150 char transport_param[32];
2151 int len;
2152 pj_bool_t secure;
2153
2154 secure = pjsip_transport_get_flag_from_type(tp->key.type) &
2155 PJSIP_TRANSPORT_SECURE;
2156
2157 /* Enclose IPv6 address in square brackets */
2158 if ((tp->key.type & PJSIP_TRANSPORT_IPV6) && via_addr->ptr[0] != '[') {
2159 beginquote = "[";
2160 endquote = "]";
2161 } else {
2162 beginquote = endquote = "";
2163 }
2164
2165 /* Don't add transport parameter if it's UDP */
2166 if (tp->key.type != PJSIP_TRANSPORT_UDP &&
2167 tp->key.type != PJSIP_TRANSPORT_UDP6)
2168 {
2169 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2170 ";transport=%s",
2171 pjsip_transport_get_type_name(
2172 (pjsip_transport_type_e)tp->key.type));
2173 } else {
2174 transport_param[0] = '\0';
2175 }
2176
2177 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
2178 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
2179 "<%s:%.*s%s%s%.*s%s:%d%s%.*s%s>%.*s",
2180 ((secure && acc->is_sips)? "sips" : "sip"),
2181 (int)acc->user_part.slen,
2182 acc->user_part.ptr,
2183 (acc->user_part.slen? "@" : ""),
2184 beginquote,
2185 (int)via_addr->slen,
2186 via_addr->ptr,
2187 endquote,
2188 rport,
2189 transport_param,
2190 (int)acc->cfg.contact_uri_params.slen,
2191 acc->cfg.contact_uri_params.ptr,
2192 (acc->cfg.use_rfc5626? ob: ""),
2193 (int)acc->cfg.contact_params.slen,
2194 acc->cfg.contact_params.ptr);
2195 if (len < 1 || len >= PJSIP_MAX_URL_SIZE) {
2196 PJ_LOG(1,(THIS_FILE, "URI too long"));
2197 pj_pool_release(pool);
2198 return PJ_FALSE;
2199 }
2200 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
2201
2202 update_regc_contact(acc);
2203
2204 /* Always update, by https://github.com/pjsip/pjproject/issues/864. */
2205 /* Since the Via address will now be overwritten to the correct
2206 * address by https://github.com/pjsip/pjproject/issues/1537, we do
2207 * not need to update the transport address.
2208 */
2209 /*
2210 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
2211 tp->local_name.port = rport;
2212 */
2213
2214 }
2215
2216 if (contact_rewrite_method == PJSUA_CONTACT_REWRITE_NO_UNREG &&
2217 acc->regc != NULL)
2218 {
2219 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
2220 }
2221
2222 /* Perform new registration */
2223 if (contact_rewrite_method < PJSUA_CONTACT_REWRITE_ALWAYS_UPDATE) {
2224 pjsua_acc_set_registration(acc->index, PJ_TRUE);
2225 }
2226
2227 pj_pool_release(pool);
2228
2229 return PJ_TRUE;
2230 }
2231
2232 /* Check and update Service-Route header */
2233 static void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
2234 {
2235 pjsip_generic_string_hdr *hsr = NULL;
2236 pjsip_route_hdr *hr, *h;
2237 const pj_str_t HNAME = { "Service-Route", 13 };
2238 const pj_str_t HROUTE = { "Route", 5 };
2239 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
2240 unsigned i, uri_cnt = 0;
2241 pj_size_t rcnt;
2242
2243 /* Find and parse Service-Route headers */
2244 for (;(rdata);) {
2245 char saved;
2246 int parsed_len;
2247
2248 /* Find Service-Route header */
2249 hsr = (pjsip_generic_string_hdr*)
2250 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
2251 if (!hsr)
2252 break;
2253
2254 /* Parse as Route header since the syntax is similar. This may
2255 * return more than one headers.
2256 */
2257 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
2258 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
2259 hr = (pjsip_route_hdr*)
2260 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
2261 hsr->hvalue.slen, &parsed_len);
2262 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
2263
2264 if (hr == NULL) {
2265 /* Error */
2266 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
2267 return;
2268 }
2269
2270 /* Save each URI in the result */
2271 h = hr;
2272 do {
2273 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
2274 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
2275 {
2276 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
2277 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
2278 return;
2279 }
2280
2281 uri[uri_cnt++] = h->name_addr.uri;
2282 h = h->next;
2283 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
2284
2285 if (h != hr) {
2286 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
2287 return;
2288 }
2289
2290 /* Prepare to find next Service-Route header */
2291 hsr = hsr->next;
2292 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
2293 break;
2294 }
2295
2296 /*
2297 * Update account's route set
2298 */
2299
2300 /* First remove all routes which are not the outbound proxies */
2301 rcnt = pj_list_size(&acc->route_set);
2302 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
2303 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
2304 hr=acc->route_set.prev;
2305 i<rcnt;
2306 ++i)
2307 {
2308 pjsip_route_hdr *prev = hr->prev;
2309 pj_list_erase(hr);
2310 hr = prev;
2311 }
2312 }
2313
2314 /* Then append the Service-Route URIs */
2315 for (i=0; i<uri_cnt; ++i) {
2316 hr = pjsip_route_hdr_create(acc->pool);
2317 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
2318 pj_list_push_back(&acc->route_set, hr);
2319 }
2320
2321 /* Done */
2322
2323 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
2324 acc->index, uri_cnt));
2325 }
2326
2327 /* Keep alive timer callback */
2328 static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2329 {
2330 pjsua_acc *acc;
2331 pjsip_tpselector tp_sel;
2332 pj_time_val delay;
2333 char addrtxt[PJ_INET6_ADDRSTRLEN];
2334 pj_status_t status;
2335 unsigned ka_timer;
2336 unsigned lower_bound;
2337
2338 PJ_UNUSED_ARG(th);
2339
2340 PJSUA_LOCK();
2341
2342 te->id = PJ_FALSE;
2343
2344 acc = (pjsua_acc*) te->user_data;
2345
2346 /* Check if the account is still active. It might have just been deleted
2347 * while the keep-alive timer was about to be called (race condition).
2348 */
2349 if (acc->ka_transport == NULL)
2350 goto on_return;
2351
2352 /* Select the transport to send the packet */
2353 pj_bzero(&tp_sel, sizeof(tp_sel));
2354 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
2355 tp_sel.u.transport = acc->ka_transport;
2356
2357 PJ_LOG(5,(THIS_FILE,
2358 "Sending %ld bytes keep-alive packet for acc %d to %s",
2359 acc->cfg.ka_data.slen, acc->index,
2360 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
2361
2362 /* Send raw packet */
2363 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
2364 acc->ka_transport->key.type, &tp_sel,
2365 NULL, acc->cfg.ka_data.ptr,
2366 acc->cfg.ka_data.slen,
2367 &acc->ka_target, acc->ka_target_len,
2368 NULL, NULL);
2369
2370 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
2371 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
2372 }
2373
2374 /* Check just in case keep-alive has been disabled. This shouldn't happen
2375 * though as when ka_interval is changed this timer should have been
2376 * cancelled.
2377 *
2378 * Also check if Flow Timer (rfc5626) is not set.
2379 */
2380 if (acc->cfg.ka_interval == 0 && acc->rfc5626_flowtmr == 0)
2381 goto on_return;
2382
2383 ka_timer = acc->rfc5626_flowtmr ? acc->rfc5626_flowtmr :
2384 acc->cfg.ka_interval;
2385
2386 lower_bound = (unsigned)((float)ka_timer * 0.8f);
2387 delay.sec = pj_rand() % (ka_timer - lower_bound) + lower_bound;
2388 delay.msec = 0;
2389
2390 /* Reschedule next timer */
2391 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
2392 if (status == PJ_SUCCESS) {
2393 te->id = PJ_TRUE;
2394 } else {
2395 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
2396 }
2397
2398 on_return:
2399 PJSUA_UNLOCK();
2400 }
2401
2402
2403 /* Update keep-alive for the account */
2404 static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
2405 struct pjsip_regc_cbparam *param)
2406 {
2407 /* In all cases, stop keep-alive timer if it's running. */
2408 if (acc->ka_timer.id) {
2409 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
2410 acc->ka_timer.id = PJ_FALSE;
2411
2412 if (acc->ka_transport) {
2413 pjsip_transport_dec_ref(acc->ka_transport);
2414 acc->ka_transport = NULL;
2415 }
2416 }
2417
2418 if (start) {
2419 pj_time_val delay;
2420 pj_status_t status;
2421 pjsip_generic_string_hdr *hsr = NULL;
2422 unsigned ka_timer;
2423 unsigned lower_bound;
2424
2425 static const pj_str_t STR_FLOW_TIMER = { "Flow-Timer", 10 };
2426
2427 hsr = (pjsip_generic_string_hdr*)
2428 pjsip_msg_find_hdr_by_name(param->rdata->msg_info.msg,
2429 &STR_FLOW_TIMER, hsr);
2430
2431 if (hsr) {
2432 acc->rfc5626_flowtmr = pj_strtoul(&hsr->hvalue);
2433 }
2434
2435 /* Only do keep-alive if:
2436 * - REGISTER response contain Flow-Timer header, otherwise
2437 * - ka_interval is not zero in the account, and
2438 * - transport is UDP.
2439 *
2440 * Previously we only enabled keep-alive when STUN is enabled, since
2441 * we thought that keep-alive is only needed in Internet situation.
2442 * But it has been discovered that Windows Firewall on WinXP also
2443 * needs to be kept-alive, otherwise incoming packets will be dropped.
2444 * So because of this, now keep-alive is always enabled for UDP,
2445 * regardless of whether STUN is enabled or not.
2446 *
2447 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
2448 * is done by the transport layer.
2449 */
2450 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
2451 ((acc->cfg.ka_interval == 0) && (acc->rfc5626_flowtmr == 0)) ||
2452 (!hsr && ((param->rdata->tp_info.transport->key.type &
2453 ~PJSIP_TRANSPORT_IPV6) != PJSIP_TRANSPORT_UDP)))
2454 {
2455 /* Keep alive is not necessary */
2456 return;
2457 }
2458
2459 /* Save transport and destination address. */
2460 acc->ka_transport = param->rdata->tp_info.transport;
2461 pjsip_transport_add_ref(acc->ka_transport);
2462
2463 /* https://github.com/pjsip/pjproject/issues/1607:
2464 * Calculate the destination address from the original request. Some
2465 * (broken) servers send the response using different source address
2466 * than the one that receives the request, which is forbidden by RFC
2467 * 3581.
2468 */
2469 {
2470 pjsip_transaction *tsx;
2471 pjsip_tx_data *req;
2472
2473 tsx = pjsip_rdata_get_tsx(param->rdata);
2474 PJ_ASSERT_ON_FAIL(tsx, return);
2475
2476 req = tsx->last_tx;
2477
2478 pj_memcpy(&acc->ka_target, &req->tp_info.dst_addr,
2479 req->tp_info.dst_addr_len);
2480 acc->ka_target_len = req->tp_info.dst_addr_len;
2481 }
2482
2483 /* Setup and start the timer */
2484 acc->ka_timer.cb = &keep_alive_timer_cb;
2485 acc->ka_timer.user_data = (void*)acc;
2486
2487 ka_timer = acc->rfc5626_flowtmr ? acc->rfc5626_flowtmr :
2488 acc->cfg.ka_interval;
2489
2490 lower_bound = (unsigned)((float)ka_timer * 0.8f);
2491 delay.sec = pj_rand() % (ka_timer - lower_bound) + lower_bound;
2492 delay.msec = 0;
2493 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
2494 &delay);
2495 if (status == PJ_SUCCESS) {
2496 char addr[PJ_INET6_ADDRSTRLEN+10];
2497 pj_str_t input_str = pj_str(param->rdata->pkt_info.src_name);
2498 acc->ka_timer.id = PJ_TRUE;
2499
2500 pj_addr_str_print(&input_str, param->rdata->pkt_info.src_port,
2501 addr, sizeof(addr), 1);
2502 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
2503 "destination:%s, interval:%lds",
2504 acc->index, addr, delay.sec));
2505 } else {
2506 acc->ka_timer.id = PJ_FALSE;
2507 pjsip_transport_dec_ref(acc->ka_transport);
2508 acc->ka_transport = NULL;
2509 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
2510 }
2511 }
2512 }
2513
2514
2515 /* Update the status of SIP outbound registration request */
2516 static void update_rfc5626_status(pjsua_acc *acc, pjsip_rx_data *rdata)
2517 {
2518 pjsip_require_hdr *hreq;
2519 const pj_str_t STR_OUTBOUND = {"outbound", 8};
2520 unsigned i;
2521
2522 if (acc->rfc5626_status == OUTBOUND_UNKNOWN) {
2523 goto on_return;
2524 }
2525
2526 hreq = rdata->msg_info.require;
2527 if (!hreq) {
2528 acc->rfc5626_status = OUTBOUND_NA;
2529 goto on_return;
2530 }
2531
2532 for (i=0; i<hreq->count; ++i) {
2533 if (pj_stricmp(&hreq->values[i], &STR_OUTBOUND)==0) {
2534 acc->rfc5626_status = OUTBOUND_ACTIVE;
2535 goto on_return;
2536 }
2537 }
2538
2539 /* Server does not support outbound */
2540 acc->rfc5626_status = OUTBOUND_NA;
2541
2542 on_return:
2543 if (acc->rfc5626_status != OUTBOUND_ACTIVE) {
2544 acc->reg_contact = acc->contact;
2545 }
2546 PJ_LOG(4,(THIS_FILE, "SIP outbound status for acc %d is %s",
2547 acc->index, (acc->rfc5626_status==OUTBOUND_ACTIVE?
2548 "active": "not active")));
2549 }
2550
2551 static void regc_tsx_cb(struct pjsip_regc_tsx_cb_param *param)
2552 {
2553 pjsua_acc *acc = (pjsua_acc*) param->cbparam.token;
2554
2555 PJSUA_LOCK();
2556
2557 if (param->cbparam.regc != acc->regc) {
2558 PJSUA_UNLOCK();
2559 return;
2560 }
2561
2562 pj_log_push_indent();
2563
2564 /* Check if we should do NAT bound address check for contact rewrite.
2565 * Note that '!contact_rewritten' check here is to avoid overriding
2566 * the current contact generated from last 2xx.
2567 */
2568 if (!acc->contact_rewritten &&
2569 (acc->cfg.contact_rewrite_method &
2570 PJSUA_CONTACT_REWRITE_ALWAYS_UPDATE) ==
2571 PJSUA_CONTACT_REWRITE_ALWAYS_UPDATE &&
2572 param->cbparam.code >= 400 &&
2573 param->cbparam.rdata)
2574 {
2575 if (acc_check_nat_addr(acc, PJSUA_CONTACT_REWRITE_ALWAYS_UPDATE,
2576 &param->cbparam))
2577 {
2578 param->contact_cnt = 1;
2579 param->contact[0] = acc->reg_contact;
2580
2581 /* Don't set 'contact_rewritten' to PJ_TRUE here to allow
2582 * further check of NAT bound address in 2xx response.
2583 */
2584 }
2585 }
2586
2587 PJSUA_UNLOCK();
2588 pj_log_pop_indent();
2589 }
2590
2591 /*
2592 * Timer callback to handle call on IP change process.
2593 */
2594 static void handle_call_on_ip_change_cb(void *user_data)
2595 {
2596 pjsua_acc *acc = (pjsua_acc*)user_data;
2597 pjsua_acc_handle_call_on_ip_change(acc);
2598 }
2599
2600 /*
2601 * This callback is called by pjsip_regc when outgoing register
2602 * request has completed.
2603 */
2604 static void regc_cb(struct pjsip_regc_cbparam *param)
2605 {
2606
2607 pjsua_acc *acc = (pjsua_acc*) param->token;
2608
2609 PJSUA_LOCK();
2610
2611 if (param->regc != acc->regc) {
2612 PJSUA_UNLOCK();
2613 return;
2614 }
2615
2616 pj_log_push_indent();
2617
2618 /*
2619 * Print registration status.
2620 */
2621 if (param->status!=PJ_SUCCESS) {
2622 pj_status_t status;
2623
2624 pjsua_perror(THIS_FILE, "SIP registration error",
2625 param->status);
2626
2627 if (param->status == PJSIP_EBUSY) {
2628 pj_log_pop_indent();
2629 PJSUA_UNLOCK();
2630 return;
2631 }
2632
2633 /* This callback is called without holding the registration's lock,
2634 * so there can be a race condition with another registration
2635 * process. Therefore, we must not forcefully try to destroy
2636 * the registration here.
2637 */
2638 status = destroy_regc(acc, PJ_FALSE);
2639 if (status == PJ_SUCCESS) {
2640 /* Stop keep-alive timer if any. */
2641 update_keep_alive(acc, PJ_FALSE, NULL);
2642 } else {
2643 /* Another registration is in progress. */
2644 pj_assert(status == PJ_EBUSY);
2645 pjsua_perror(THIS_FILE, "Deleting registration failed",
2646 status);
2647 }
2648
2649 } else if (param->code < 0 || param->code >= 300) {
2650 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
2651 param->code,
2652 (int)param->reason.slen, param->reason.ptr));
2653 destroy_regc(acc, PJ_TRUE);
2654
2655 /* Clear Service-Route header */
2656 update_service_route(acc, NULL);
2657
2658 /* Stop keep-alive timer if any. */
2659 update_keep_alive(acc, PJ_FALSE, NULL);
2660 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
2661
2662 /* Update auto registration flag */
2663 acc->auto_rereg.active = PJ_FALSE;
2664 acc->auto_rereg.attempt_cnt = 0;
2665
2666 if (param->expiration < 1) {
2667 destroy_regc(acc, PJ_TRUE);
2668 /* Reset pointer to registration transport */
2669 //acc->auto_rereg.reg_tp = NULL;
2670
2671 /* Stop keep-alive timer if any. */
2672 update_keep_alive(acc, PJ_FALSE, NULL);
2673
2674 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
2675 pjsua_var.acc[acc->index].cfg.id.ptr));
2676
2677 } else {
2678 /* Check and update SIP outbound status first, since the result
2679 * will determine if we should update re-registration
2680 */
2681 update_rfc5626_status(acc, param->rdata);
2682
2683 /* Check NAT bound address if it hasn't been done before */
2684 if (!acc->contact_rewritten &&
2685 acc_check_nat_addr(acc, (acc->cfg.contact_rewrite_method & 3),
2686 param))
2687 {
2688 PJSUA_UNLOCK();
2689 pj_log_pop_indent();
2690
2691 /* Avoid another check of NAT bound address */
2692 acc->contact_rewritten = PJ_TRUE;
2693 return;
2694 }
2695
2696 /* Check and update Service-Route header */
2697 update_service_route(acc, param->rdata);
2698
2699 #if PJSUA_REG_AUTO_REG_REFRESH
2700
2701 PJ_LOG(3, (THIS_FILE,
2702 "%s: registration success, status=%d (%.*s), "
2703 "will re-register in %d seconds",
2704 pjsua_var.acc[acc->index].cfg.id.ptr,
2705 param->code,
2706 (int)param->reason.slen, param->reason.ptr,
2707 param->expiration));
2708
2709 #else
2710
2711 PJ_LOG(3, (THIS_FILE,
2712 "%s: registration success, status=%d (%.*s), "
2713 "auto re-register disabled",
2714 pjsua_var.acc[acc->index].cfg.id.ptr,
2715 param->code,
2716 (int)param->reason.slen, param->reason.ptr));
2717
2718 #endif
2719 /* Start keep-alive timer if necessary. */
2720 update_keep_alive(acc, PJ_TRUE, param);
2721
2722 /* Send initial PUBLISH if it is enabled */
2723 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
2724 pjsua_pres_init_publish_acc(acc->index);
2725
2726 /* Subscribe to MWI, if it's enabled */
2727 if (acc->cfg.mwi_enabled)
2728 pjsua_start_mwi(acc->index, PJ_FALSE);
2729
2730 }
2731 } else {
2732 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
2733 }
2734
2735 acc->reg_last_err = param->status;
2736 acc->reg_last_code = param->code;
2737
2738 /* Reaching this point means no contact rewrite, so reset the flag */
2739 acc->contact_rewritten = PJ_FALSE;
2740
2741 /* Check if we need to auto retry registration. Basically, registration
2742 * failure codes triggering auto-retry are those of temporal failures
2743 * considered to be recoverable in relatively short term.
2744 */
2745 if (acc->cfg.reg_retry_interval &&
2746 acc->ip_change_op != PJSUA_IP_CHANGE_OP_ACC_UPDATE_CONTACT &&
2747 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
2748 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
2749 param->code == PJSIP_SC_BAD_GATEWAY ||
2750 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
2751 param->code == PJSIP_SC_SERVER_TIMEOUT ||
2752 param->code == PJSIP_SC_TEMPORARILY_UNAVAILABLE ||
2753 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
2754 {
2755 schedule_reregistration(acc);
2756 }
2757
2758 /* Call the registration status callback */
2759
2760 if (pjsua_var.ua_cfg.cb.on_reg_state) {
2761 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
2762 }
2763
2764 if (pjsua_var.ua_cfg.cb.on_reg_state2) {
2765 pjsua_reg_info reg_info;
2766 pjsip_regc_info rinfo;
2767
2768 pjsip_regc_get_info(param->regc, &rinfo);
2769 reg_info.cbparam = param;
2770 reg_info.regc = param->regc;
2771 reg_info.renew = !param->is_unreg;
2772 (*pjsua_var.ua_cfg.cb.on_reg_state2)(acc->index, &reg_info);
2773 }
2774
2775 if (acc->ip_change_op == PJSUA_IP_CHANGE_OP_ACC_UPDATE_CONTACT) {
2776 if (pjsua_var.ua_cfg.cb.on_ip_change_progress) {
2777 pjsua_ip_change_op_info ip_chg_info;
2778 pjsip_regc_info rinfo;
2779
2780 pj_bzero(&ip_chg_info, sizeof(ip_chg_info));
2781 pjsip_regc_get_info(param->regc, &rinfo);
2782 ip_chg_info.acc_update_contact.acc_id = acc->index;
2783 ip_chg_info.acc_update_contact.code = param->code;
2784 ip_chg_info.acc_update_contact.is_register = !param->is_unreg;
2785 (*pjsua_var.ua_cfg.cb.on_ip_change_progress)(acc->ip_change_op,
2786 param->status,
2787 &ip_chg_info);
2788 }
2789
2790 if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
2791 if (param->expiration < 1) {
2792 pj_status_t status;
2793 /* Send re-register. */
2794 PJ_LOG(3, (THIS_FILE, "%.*s: send registration triggered by IP"
2795 " change", (int)pjsua_var.acc[acc->index].cfg.id.slen,
2796 pjsua_var.acc[acc->index].cfg.id.ptr));
2797
2798 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2799 if (status != PJ_SUCCESS) {
2800 if (pjsua_var.ua_cfg.cb.on_ip_change_progress) {
2801 pjsua_ip_change_op_info ip_chg_info;
2802
2803 pj_bzero(&ip_chg_info, sizeof(ip_chg_info));
2804 ip_chg_info.acc_update_contact.acc_id = acc->index;
2805 ip_chg_info.acc_update_contact.is_register = PJ_TRUE;
2806 (*pjsua_var.ua_cfg.cb.on_ip_change_progress)(
2807 acc->ip_change_op,
2808 status,
2809 &ip_chg_info);
2810 }
2811 pjsua_acc_end_ip_change(acc);
2812 }
2813 } else {
2814 /* Avoid deadlock issue when sending BYE or Re-INVITE. */
2815 pjsua_schedule_timer2(&handle_call_on_ip_change_cb,
2816 (void*)acc, 0);
2817 }
2818 } else {
2819 pjsua_acc_end_ip_change(acc);
2820 }
2821 }
2822
2823 PJSUA_UNLOCK();
2824 pj_log_pop_indent();
2825 }
2826
2827
2828 /*
2829 * Initialize client registration.
2830 */
2831 static pj_status_t pjsua_regc_init(int acc_id)
2832 {
2833 pjsua_acc *acc;
2834 pj_pool_t *pool;
2835 pjsip_tpselector tp_sel;
2836 pj_status_t status;
2837
2838 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2839 acc = &pjsua_var.acc[acc_id];
2840
2841 if (acc->cfg.reg_uri.slen == 0) {
2842 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
2843 return PJ_SUCCESS;
2844 }
2845
2846 /* Destroy existing session, if any */
2847 destroy_regc(acc, PJ_TRUE);
2848
2849 /* initialize SIP registration if registrar is configured */
2850
2851 status = pjsip_regc_create( pjsua_var.endpt,
2852 acc, &regc_cb, &acc->regc);
2853
2854 if (status != PJ_SUCCESS) {
2855 pjsua_perror(THIS_FILE, "Unable to create client registration",
2856 status);
2857 return status;
2858 }
2859
2860 pool = pjsua_pool_create("tmpregc", 512, 512);
2861
2862 if (acc->contact.slen == 0) {
2863 pj_str_t tmp_contact;
2864
2865 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
2866 acc_id, &acc->cfg.reg_uri);
2867 if (status != PJ_SUCCESS) {
2868 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
2869 " for registration",
2870 status);
2871 goto on_return;
2872 }
2873
2874 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
2875 update_regc_contact(acc);
2876 }
2877
2878 status = pjsip_regc_init( acc->regc,
2879 &acc->cfg.reg_uri,
2880 &acc->cfg.id,
2881 &acc->cfg.id,
2882 1, &acc->reg_contact,
2883 acc->cfg.reg_timeout);
2884 if (status != PJ_SUCCESS) {
2885 pjsua_perror(THIS_FILE,
2886 "Client registration initialization error",
2887 status);
2888 goto on_return;
2889 }
2890
2891 status = pjsip_regc_set_reg_tsx_cb(acc->regc, regc_tsx_cb);
2892 if (status != PJ_SUCCESS) {
2893 pjsua_perror(THIS_FILE,
2894 "Failed setting registration callback",
2895 status);
2896 goto on_return;
2897 }
2898
2899 /* Set client registration's transport based on acc's config. */
2900 pjsua_init_tpselector(acc_id, &tp_sel);
2901 status = pjsip_regc_set_transport(acc->regc, &tp_sel);
2902 if (status != PJ_SUCCESS) {
2903 pjsua_perror(THIS_FILE,
2904 "Failed setting registration transport",
2905 status);
2906 goto on_return;
2907 }
2908
2909 if (acc->cfg.use_shared_auth) {
2910 status = pjsip_regc_set_auth_sess(acc->regc, &acc->shared_auth_sess);
2911 if (status != PJ_SUCCESS) {
2912 pjsua_perror(THIS_FILE,
2913 "Failed setting registration shared auth session",
2914 status);
2915 goto on_return;
2916 }
2917 }
2918
2919 /* Set credentials */
2920 if (acc->cred_cnt) {
2921 status = pjsip_regc_set_credentials(acc->regc, acc->cred_cnt,
2922 acc->cred);
2923 if (status != PJ_SUCCESS) {
2924 pjsua_perror(THIS_FILE,
2925 "Failed setting credentials for registration",
2926 status);
2927 goto on_return;
2928 }
2929 }
2930
2931 /* Configure async auth on non-shared regc auth session */
2932 if (!acc->cfg.use_shared_auth &&
2933 pjsua_var.ua_cfg.cb.on_auth_challenge)
2934 {
2935 pjsip_auth_clt_async_setting async_opt;
2936 pj_bzero(&async_opt, sizeof(async_opt));
2937 async_opt.cb = &pjsua_auth_on_challenge;
2938 async_opt.user_data = (void*)(pj_ssize_t)acc->index;
2939 pjsip_auth_clt_async_configure(
2940 pjsip_regc_get_auth_sess(acc->regc), &async_opt);
2941 }
2942
2943 /* Set delay before registration refresh */
2944 status = pjsip_regc_set_delay_before_refresh(
2945 acc->regc,
2946 acc->cfg.reg_delay_before_refresh);
2947 if (status != PJ_SUCCESS) {
2948 /* Maybe too big, it will fallback to the default setting,
2949 * just print warning.
2950 */
2951 pjsua_perror(THIS_FILE,
2952 "Warning: failed setting registration refresh delay",
2953 status);
2954 }
2955
2956 /* Set authentication preference */
2957 status = pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
2958 if (status != PJ_SUCCESS) {
2959 pjsua_perror(THIS_FILE,
2960 "Failed setting registration auth preference",
2961 status);
2962 goto on_return;
2963 }
2964
2965 /* Set route-set */
2966 if (acc->cfg.reg_use_proxy) {
2967 pjsip_route_hdr route_set;
2968 const pjsip_route_hdr *r;
2969
2970 pj_list_init(&route_set);
2971
2972 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) {
2973 r = pjsua_var.outbound_proxy.next;
2974 while (r != &pjsua_var.outbound_proxy) {
2975 pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r));
2976 r = r->next;
2977 }
2978 }
2979
2980 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY &&
2981 acc->cfg.proxy_cnt)
2982 {
2983 int cnt = acc->cfg.proxy_cnt;
2984 pjsip_route_hdr *pos = route_set.prev;
2985 int i;
2986
2987 r = acc->route_set.prev;
2988 for (i=0; i<cnt; ++i) {
2989 pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r));
2990 r = r->prev;
2991 }
2992 }
2993
2994 if (!pj_list_empty(&route_set)) {
2995 status = pjsip_regc_set_route_set( acc->regc, &route_set );
2996 if (status != PJ_SUCCESS) {
2997 pjsua_perror(THIS_FILE,
2998 "Failed setting registration route set",
2999 status);
3000 goto on_return;
3001 }
3002 }
3003 }
3004
3005 /* Add custom request headers specified in the account config */
3006 status = pjsip_regc_add_headers(acc->regc, &acc->cfg.reg_hdr_list);
3007 if (status != PJ_SUCCESS) {
3008 pjsua_perror(THIS_FILE,
3009 "Failed setting registration custom headers",
3010 status);
3011 goto on_return;
3012 }
3013
3014 /* Add other request headers. */
3015 if (pjsua_var.ua_cfg.user_agent.slen) {
3016 pjsip_hdr hdr_list;
3017 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
3018 pjsip_generic_string_hdr *h;
3019
3020 pj_list_init(&hdr_list);
3021
3022 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
3023 &pjsua_var.ua_cfg.user_agent);
3024 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
3025
3026 status = pjsip_regc_add_headers(acc->regc, &hdr_list);
3027 if (status != PJ_SUCCESS) {
3028 /* Informational header, just print warning */
3029 pjsua_perror(THIS_FILE,
3030 "Warning: failed setting registration "
3031 "user-agent header",
3032 status);
3033 status = PJ_SUCCESS;
3034 }
3035 }
3036
3037 /* If SIP outbound is used, add "Supported: outbound, path header" */
3038 if (acc->rfc5626_status == OUTBOUND_WANTED ||
3039 acc->rfc5626_status == OUTBOUND_ACTIVE)
3040 {
3041 pjsip_hdr hdr_list;
3042 pjsip_supported_hdr *hsup;
3043
3044 pj_list_init(&hdr_list);
3045 hsup = pjsip_supported_hdr_create(pool);
3046 pj_list_push_back(&hdr_list, hsup);
3047
3048 hsup->count = 2;
3049 hsup->values[0] = pj_str("outbound");
3050 hsup->values[1] = pj_str("path");
3051
3052 status = pjsip_regc_add_headers(acc->regc, &hdr_list);
3053 if (status != PJ_SUCCESS) {
3054 pjsua_perror(THIS_FILE,
3055 "Failed setting registration outbound support "
3056 "indication",
3057 status);
3058 goto on_return;
3059 }
3060 }
3061
3062 on_return:
3063 if (status != PJ_SUCCESS) {
3064 if (acc->regc)
3065 destroy_regc(acc, PJ_TRUE);
3066
3067 pjsua_perror(THIS_FILE, "Error initializing client registration",
3068 status);
3069 }
3070 pj_pool_release(pool);
3071 return status;
3072 }
3073
3074 pj_bool_t pjsua_sip_acc_is_using_ipv6(pjsua_acc_id acc_id)
3075 {
3076 #if PJ_HAS_IPV6
3077 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3078
3079 return ((acc->tp_type & PJSIP_TRANSPORT_IPV6) == PJSIP_TRANSPORT_IPV6 ||
3080 pjsua_var.acc[acc_id].cfg.ipv6_sip_use ==
3081 PJSUA_IPV6_ENABLED_USE_IPV6_ONLY);
3082 #else
3083 PJ_UNUSED_ARG(acc_id);
3084 return PJ_FALSE;
3085 #endif
3086 }
3087
3088 static int sip_acc_get_pref_ip_ver(pjsua_acc_id acc_id)
3089 {
3090 #if PJ_HAS_IPV6
3091 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3092
3093 if ((acc->tp_type & PJSIP_TRANSPORT_IPV6) == PJSIP_TRANSPORT_IPV6 ||
3094 pjsua_var.acc[acc_id].cfg.ipv6_sip_use ==
3095 PJSUA_IPV6_ENABLED_PREFER_IPV6 ||
3096 pjsua_var.acc[acc_id].cfg.ipv6_sip_use ==
3097 PJSUA_IPV6_ENABLED_USE_IPV6_ONLY)
3098 {
3099 return 6;
3100 } else if (acc->tp_type != PJSIP_TRANSPORT_UNSPECIFIED ||
3101 pjsua_var.acc[acc_id].cfg.ipv6_sip_use ==
3102 PJSUA_IPV6_ENABLED_PREFER_IPV4 ||
3103 pjsua_var.acc[acc_id].cfg.ipv6_sip_use ==
3104 PJSUA_IPV6_DISABLED)
3105 {
3106 return 4;
3107 } else {
3108 /* No preference.
3109 * (acc->tp_type == PJSIP_TRANSPORT_UNSPECIFIED &&
3110 * pjsua_var.acc[acc_id].cfg.ipv6_sip_use ==
3111 * PJSUA_IPV6_ENABLED_NO_PREFERENCE)
3112 */
3113 return 0;
3114 }
3115 #else
3116 PJ_UNUSED_ARG(acc_id);
3117 return 4;
3118 #endif
3119 }
3120
3121 pj_bool_t pjsua_sip_acc_is_using_stun(pjsua_acc_id acc_id)
3122 {
3123 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3124
3125 return acc->cfg.sip_stun_use != PJSUA_STUN_USE_DISABLED &&
3126 pjsua_var.ua_cfg.stun_srv_cnt != 0;
3127 }
3128
3129 pj_bool_t pjsua_media_acc_is_using_stun(pjsua_acc_id acc_id)
3130 {
3131 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3132
3133 return acc->cfg.media_stun_use != PJSUA_STUN_USE_DISABLED &&
3134 pjsua_var.ua_cfg.stun_srv_cnt != 0;
3135 }
3136
3137 pj_bool_t pjsua_sip_acc_is_using_upnp(pjsua_acc_id acc_id)
3138 {
3139 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3140
3141 return acc->cfg.sip_upnp_use != PJSUA_UPNP_USE_DISABLED &&
3142 pjsua_var.ua_cfg.enable_upnp &&
3143 pjsua_var.upnp_status == PJ_SUCCESS;
3144 }
3145
3146 pj_bool_t pjsua_media_acc_is_using_upnp(pjsua_acc_id acc_id)
3147 {
3148 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3149
3150 return acc->cfg.media_upnp_use != PJSUA_UPNP_USE_DISABLED &&
3151 pjsua_var.ua_cfg.enable_upnp &&
3152 pjsua_var.upnp_status == PJ_SUCCESS;
3153 }
3154
3155 /*
3156 * Update registration or perform unregistration.
3157 */
3158 PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
3159 pj_bool_t renew)
3160 {
3161 pjsua_acc *acc;
3162 pj_status_t status = 0;
3163 pjsip_tx_data *tdata = 0;
3164
3165 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
3166 PJ_EINVAL);
3167 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
3168
3169 PJ_LOG(4,(THIS_FILE, "Acc %d: setting %sregistration..",
3170 acc_id, (renew? "" : "un")));
3171 pj_log_push_indent();
3172
3173 PJSUA_LOCK();
3174
3175 acc = &pjsua_var.acc[acc_id];
3176
3177 /* Cancel any re-registration timer */
3178 if (pjsua_var.acc[acc_id].auto_rereg.timer.id) {
3179 pjsua_var.acc[acc_id].auto_rereg.timer.id = PJ_FALSE;
3180 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
3181 }
3182
3183 /* Reset pointer to registration transport */
3184 // Do not reset this here, as if currently there is another registration
3185 // on progress, this registration will fail but transport pointer will
3186 // become NULL which will prevent transport to be destroyed immediately
3187 // after disconnected (which may cause iOS app getting killed (see #1482).
3188 //pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
3189
3190 if (renew) {
3191 if (pjsua_var.acc[acc_id].regc == NULL) {
3192 status = pjsua_regc_init(acc_id);
3193 if (status != PJ_SUCCESS) {
3194 pjsua_perror(THIS_FILE, "Unable to create registration",
3195 status);
3196 goto on_return;
3197 }
3198 }
3199 if (!pjsua_var.acc[acc_id].regc) {
3200 status = PJ_EINVALIDOP;
3201 goto on_return;
3202 }
3203
3204 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc,
3205 PJSUA_REG_AUTO_REG_REFRESH,
3206 &tdata);
3207
3208 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
3209 pjsip_authorization_hdr *h;
3210 char *uri;
3211 int d;
3212
3213 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
3214 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
3215 uri, acc->cfg.reg_uri.slen+10);
3216 pj_assert(d > 0);
3217 PJ_UNUSED_ARG(d);
3218
3219 h = pjsip_authorization_hdr_create(tdata->pool);
3220 h->scheme = pjsip_DIGEST_STR;
3221 h->credential.digest.username = acc->cred[0].username;
3222 h->credential.digest.realm = acc->srv_domain;
3223 h->credential.digest.uri = pj_str(uri);
3224 h->credential.digest.algorithm = pjsip_MD5_STR;
3225
3226 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
3227 }
3228
3229 } else {
3230 if (pjsua_var.acc[acc_id].regc == NULL) {
3231 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
3232 status = PJ_EINVALIDOP;
3233 goto on_return;
3234 }
3235
3236 pjsua_pres_unpublish(&pjsua_var.acc[acc_id], 0);
3237
3238 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
3239 }
3240
3241 if (status == PJ_SUCCESS) {
3242 pjsip_regc *regc = pjsua_var.acc[acc_id].regc;
3243
3244 if (pjsua_var.acc[acc_id].cfg.allow_via_rewrite &&
3245 pjsua_var.acc[acc_id].via_addr.host.slen > 0)
3246 {
3247 pjsip_regc_set_via_sent_by(pjsua_var.acc[acc_id].regc,
3248 &pjsua_var.acc[acc_id].via_addr,
3249 pjsua_var.acc[acc_id].via_tp);
3250 } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
3251 !pjsua_sip_acc_is_using_upnp(acc_id))
3252 {
3253 /* Choose local interface to use in Via if acc is not using
3254 * STUN nor UPnP.
3255 */
3256 pjsua_acc_get_uac_addr(acc_id, tdata->pool,
3257 &acc->cfg.reg_uri,
3258 &tdata->via_addr,
3259 NULL, NULL,
3260 &tdata->via_tp);
3261 }
3262
3263 /* Increment ref counter and release PJSUA lock here, to avoid
3264 * deadlock while making sure that regc won't be destroyed.
3265 */
3266 pjsip_regc_add_ref(regc);
3267 PJSUA_UNLOCK();
3268
3269 //pjsua_process_msg_data(tdata, NULL);
3270 status = pjsip_regc_send( regc, tdata );
3271
3272 PJSUA_LOCK();
3273 if (pjsip_regc_dec_ref(regc) == PJ_EGONE) {
3274 /* regc has been deleted. */
3275 goto on_return;
3276 }
3277 }
3278
3279 /* Update pointer to registration transport */
3280 if (status == PJ_SUCCESS) {
3281 /* Variable auto_rereg.reg_tp is currently unused since it may differ
3282 * with the transport used by regc (for example, when a resolver is
3283 * employed). A more reliable way is to query the regc directly
3284 * when needed.
3285 */
3286 //pjsip_regc_info reg_info;
3287
3288 //pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
3289 //pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
3290
3291 if (pjsua_var.ua_cfg.cb.on_reg_started) {
3292 (*pjsua_var.ua_cfg.cb.on_reg_started)(acc_id, renew);
3293 }
3294 if (pjsua_var.ua_cfg.cb.on_reg_started2) {
3295 pjsua_reg_info rinfo;
3296
3297 rinfo.cbparam = NULL;
3298 rinfo.regc = pjsua_var.acc[acc_id].regc;
3299 rinfo.renew = renew;
3300 (*pjsua_var.ua_cfg.cb.on_reg_started2)(acc_id, &rinfo);
3301 }
3302 }
3303
3304 if (status != PJ_SUCCESS) {
3305 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
3306 status);
3307 } else {
3308 PJ_LOG(4,(THIS_FILE, "Acc %d: %s sent", acc_id,
3309 (renew? "Registration" : "Unregistration")));
3310 }
3311
3312 on_return:
3313 PJSUA_UNLOCK();
3314 pj_log_pop_indent();
3315 return status;
3316 }
3317
3318
3319 /*
3320 * Get account information.
3321 */
3322 PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
3323 pjsua_acc_info *info)
3324 {
3325 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3326 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
3327
3328 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
3329 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
3330
3331 pj_bzero(info, sizeof(pjsua_acc_info));
3332
3333 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
3334 PJ_EINVAL);
3335 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
3336
3337 PJSUA_LOCK();
3338
3339 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
3340 PJSUA_UNLOCK();
3341 return PJ_EINVALIDOP;
3342 }
3343
3344 info->id = acc_id;
3345 info->is_default = (pjsua_var.default_acc == acc_id);
3346 info->acc_uri = acc_cfg->id;
3347 info->has_registration = (acc->cfg.reg_uri.slen > 0);
3348 info->online_status = acc->online_status;
3349 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
3350 if (info->rpid.note.slen)
3351 info->online_status_text = info->rpid.note;
3352 else if (info->online_status)
3353 info->online_status_text = pj_str("Online");
3354 else
3355 info->online_status_text = pj_str("Offline");
3356
3357 if (acc->reg_last_code) {
3358 if (info->has_registration) {
3359 info->status = (pjsip_status_code) acc->reg_last_code;
3360 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
3361 if (acc->reg_last_err)
3362 info->reg_last_err = acc->reg_last_err;
3363 } else {
3364 info->status = (pjsip_status_code) 0;
3365 info->status_text = pj_str("not registered");
3366 }
3367 } else if (acc->cfg.reg_uri.slen) {
3368 info->status = PJSIP_SC_TRYING;
3369 info->status_text = pj_str("In Progress");
3370 } else {
3371 info->status = (pjsip_status_code) 0;
3372 info->status_text = pj_str("does not register");
3373 }
3374
3375 if (acc->regc) {
3376 pjsip_regc_info regc_info;
3377 pjsip_regc_get_info(acc->regc, &regc_info);
3378 info->expires = regc_info.next_reg;
3379 } else {
3380 info->expires = PJSIP_EXPIRES_NOT_SPECIFIED;
3381 }
3382
3383 PJSUA_UNLOCK();
3384
3385 return PJ_SUCCESS;
3386
3387 }
3388
3389
3390 /*
3391 * Enum accounts all account ids.
3392 */
3393 PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
3394 unsigned *count )
3395 {
3396 unsigned i, c;
3397
3398 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
3399
3400 PJSUA_LOCK();
3401
3402 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
3403 if (!pjsua_var.acc[i].valid)
3404 continue;
3405 ids[c] = i;
3406 ++c;
3407 }
3408
3409 *count = c;
3410
3411 PJSUA_UNLOCK();
3412
3413 return PJ_SUCCESS;
3414 }
3415
3416
3417 /*
3418 * Enum accounts info.
3419 */
3420 PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
3421 unsigned *count )
3422 {
3423 unsigned i, c;
3424
3425 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
3426
3427 PJSUA_LOCK();
3428
3429 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
3430 if (!pjsua_var.acc[i].valid)
3431 continue;
3432
3433 pjsua_acc_get_info(i, &info[c]);
3434 ++c;
3435 }
3436
3437 *count = c;
3438
3439 PJSUA_UNLOCK();
3440
3441 return PJ_SUCCESS;
3442 }
3443
3444
3445 /*
3446 * This is an internal function to find the most appropriate account to
3447 * used to reach to the specified URL.
3448 */
3449 PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
3450 {
3451 pj_str_t tmp;
3452 pjsip_uri *uri;
3453 pjsip_sip_uri *sip_uri;
3454 pj_pool_t *tmp_pool;
3455 unsigned i;
3456
3457 PJSUA_LOCK();
3458
3459 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
3460
3461 pj_strdup_with_null(tmp_pool, &tmp, url);
3462
3463 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
3464 if (!uri) {
3465 pj_pool_release(tmp_pool);
3466 PJSUA_UNLOCK();
3467 return pjsua_var.default_acc;
3468 }
3469
3470 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
3471 !PJSIP_URI_SCHEME_IS_SIPS(uri))
3472 {
3473 /* Return the first account with proxy */
3474 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
3475 if (!pjsua_var.acc[i].valid)
3476 continue;
3477 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
3478 break;
3479 }
3480
3481 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
3482 /* Found rather matching account */
3483 pj_pool_release(tmp_pool);
3484 PJSUA_UNLOCK();
3485 return i;
3486 }
3487
3488 /* Not found, use default account */
3489 pj_pool_release(tmp_pool);
3490 PJSUA_UNLOCK();
3491 return pjsua_var.default_acc;
3492 }
3493
3494 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
3495
3496 /* Find matching domain AND port */
3497 for (i=0; i<pjsua_var.acc_cnt; ++i) {
3498 unsigned acc_id = pjsua_var.acc_ids[i];
3499 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
3500 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
3501 {
3502 pj_pool_release(tmp_pool);
3503 PJSUA_UNLOCK();
3504 return acc_id;
3505 }
3506 }
3507
3508 /* If no match, try to match the domain part only */
3509 for (i=0; i<pjsua_var.acc_cnt; ++i) {
3510 unsigned acc_id = pjsua_var.acc_ids[i];
3511 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
3512 {
3513 pj_pool_release(tmp_pool);
3514 PJSUA_UNLOCK();
3515 return acc_id;
3516 }
3517 }
3518
3519
3520 /* Still no match, just use default account */
3521 pj_pool_release(tmp_pool);
3522 PJSUA_UNLOCK();
3523 return pjsua_var.default_acc;
3524 }
3525
3526
3527 /*
3528 * This is an internal function to find the most appropriate account to be
3529 * used to handle incoming calls.
3530 *
3531 * If the To header contains a SIP(S) URI, then it will be used for
3532 * finding a appropriate account, else the request URI.
3533 * The function selects an account by weighted score.
3534 *
3535 * The matching priority order is: transport type (matched or not set),
3536 * domain part, user part, and the user part of the request URI (if it is
3537 * a SIP URI). Note that the transport type has higher priority as
3538 * unmatched transport type may cause failure in sending response.
3539 */
3540 PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
3541 {
3542 pjsip_uri *uri;
3543 pjsip_uri *request_uri;
3544 pjsip_sip_uri *sip_uri;
3545 pjsip_sip_uri *request_sip_uri = NULL;
3546 pjsua_acc_id id = PJSUA_INVALID_ID;
3547 int max_score;
3548 unsigned i;
3549
3550 if (pjsua_var.acc_cnt == 0) {
3551 PJ_LOG(2, (THIS_FILE, "No available account to handle %s",
3552 pjsip_rx_data_get_info(rdata)));
3553
3554 return PJSUA_INVALID_ID;
3555 }
3556
3557 uri = rdata->msg_info.to->uri;
3558 request_uri = rdata->msg_info.msg->line.req.uri;
3559
3560 PJSUA_LOCK();
3561
3562 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
3563 !PJSIP_URI_SCHEME_IS_SIPS(uri))
3564 {
3565 if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG)
3566 uri = rdata->msg_info.msg->line.req.uri;
3567 else
3568 goto on_return;
3569 }
3570
3571 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
3572 !PJSIP_URI_SCHEME_IS_SIPS(uri))
3573 {
3574 goto on_return;
3575 }
3576
3577 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
3578
3579 if (PJSIP_URI_SCHEME_IS_SIP(request_uri) ||
3580 PJSIP_URI_SCHEME_IS_SIPS(request_uri))
3581 {
3582 request_sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(request_uri);
3583 }
3584
3585 max_score = 0;
3586 for (i=0; i < pjsua_var.acc_cnt; ++i) {
3587 unsigned acc_id = pjsua_var.acc_ids[i];
3588 pjsua_acc *acc = &pjsua_var.acc[acc_id];
3589 int score = 0;
3590
3591 if (!acc->valid)
3592 continue;
3593
3594 /* Match transport type */
3595 if (acc->tp_type == rdata->tp_info.transport->key.type ||
3596 acc->tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
3597 {
3598 score |= 8;
3599 }
3600
3601 /* Match domain */
3602 if (pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
3603 score |= 4;
3604 }
3605
3606 /* Match username */
3607 if (pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
3608 score |= 2;
3609 }
3610
3611 /* Match username of request URI */
3612 if (request_sip_uri && pj_stricmp(&acc->user_part, &request_sip_uri->user)==0) {
3613 score |= 1;
3614 }
3615
3616 if (score > max_score) {
3617 id = acc_id;
3618 max_score = score;
3619 }
3620 }
3621
3622 PJ_LOG(6,(THIS_FILE, "Account selected for incoming call: #%u, score: %d", id, max_score));
3623
3624 on_return:
3625 PJSUA_UNLOCK();
3626
3627 /* Still no match, use default account */
3628 if (id == PJSUA_INVALID_ID)
3629 id = pjsua_var.default_acc;
3630
3631 /* Invoke account find callback */
3632 if (pjsua_var.ua_cfg.cb.on_acc_find_for_incoming)
3633 (*pjsua_var.ua_cfg.cb.on_acc_find_for_incoming)(rdata, &id);
3634
3635 /* Verify if the specified account id is valid */
3636 if (!pjsua_acc_is_valid(id))
3637 id = pjsua_var.default_acc;
3638
3639 return id;
3640 }
3641
3642
3643 /*
3644 * Create arbitrary requests for this account.
3645 */
3646 PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
3647 const pjsip_method *method,
3648 const pj_str_t *target,
3649 pjsip_tx_data **p_tdata)
3650 {
3651 pjsip_tx_data *tdata;
3652 pjsua_acc *acc;
3653 pjsip_route_hdr *r;
3654 pjsip_tpselector tp_sel;
3655 pj_status_t status;
3656
3657 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
3658 PJ_EINVAL);
3659 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
3660 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
3661
3662 PJSUA_LOCK();
3663
3664 acc = &pjsua_var.acc[acc_id];
3665
3666 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
3667 &acc->cfg.id, target,
3668 NULL, NULL, -1, NULL, &tdata);
3669 if (status != PJ_SUCCESS) {
3670 pjsua_perror(THIS_FILE, "Unable to create request", status);
3671 PJSUA_UNLOCK();
3672 return status;
3673 }
3674
3675 /* Copy routeset */
3676 r = acc->route_set.next;
3677 while (r != &acc->route_set) {
3678 pjsip_msg_add_hdr(tdata->msg,
3679 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
3680 r = r->next;
3681 }
3682
3683 /* Set transmit data's transport based on acc's config. */
3684 pjsua_init_tpselector(acc_id, &tp_sel);
3685 pjsip_tx_data_set_transport(tdata, &tp_sel);
3686
3687 /* If via_addr is set, use this address for the Via header. */
3688 if (pjsua_var.acc[acc_id].cfg.allow_via_rewrite &&
3689 pjsua_var.acc[acc_id].via_addr.host.slen > 0)
3690 {
3691 tdata->via_addr = pjsua_var.acc[acc_id].via_addr;
3692 tdata->via_tp = pjsua_var.acc[acc_id].via_tp;
3693 } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
3694 !pjsua_sip_acc_is_using_upnp(acc_id))
3695 {
3696 /* Choose local interface to use in Via if acc is not using
3697 * STUN nor UPnP.
3698 */
3699 pjsua_acc_get_uac_addr(acc_id, tdata->pool,
3700 target,
3701 &tdata->via_addr,
3702 NULL, NULL,
3703 &tdata->via_tp);
3704 }
3705
3706 PJSUA_UNLOCK();
3707
3708 /* Done */
3709 *p_tdata = tdata;
3710 return PJ_SUCCESS;
3711 }
3712
3713 /*
3714 * Internal:
3715 * determine if an address is a valid IP address, and if it is,
3716 * return the IP version (4 or 6).
3717 */
3718 static int get_ip_addr_ver(const pj_str_t *host)
3719 {
3720 pj_in_addr dummy;
3721 pj_in6_addr dummy6;
3722
3723 /* Check for empty address */
3724 if (host->slen == 0)
3725 return 0;
3726
3727 /* First check if this is an IPv4 address */
3728 if (pj_inet_pton(pj_AF_INET(), host, &dummy) == PJ_SUCCESS)
3729 return 4;
3730
3731 /* Then check if this is an IPv6 address */
3732 if (pj_inet_pton(pj_AF_INET6(), host, &dummy6) == PJ_SUCCESS)
3733 return 6;
3734
3735 /* Not an IP address */
3736 return 0;
3737 }
3738
3739 /* Get local transport address suitable to be used for Via or Contact address
3740 * to send request to the specified destination URI.
3741 */
3742 pj_status_t pjsua_acc_get_uac_addr(pjsua_acc_id acc_id,
3743 pj_pool_t *pool,
3744 const pj_str_t *dst_uri,
3745 pjsip_host_port *addr,
3746 pjsip_transport_type_e *p_tp_type,
3747 int *secure,
3748 const void **p_tp)
3749 {
3750 pjsua_acc *acc;
3751 pjsip_sip_uri *sip_uri;
3752 pj_status_t status;
3753 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
3754 unsigned flag;
3755 int sip_pref_ip;
3756 pjsip_tpselector tp_sel;
3757 pjsip_tpmgr *tpmgr;
3758 pjsip_tpmgr_fla2_param tfla2_prm;
3759 pj_bool_t update_addr = PJ_TRUE;
3760
3761 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
3762 acc = &pjsua_var.acc[acc_id];
3763
3764 /* If route-set is configured for the account, then URI is the
3765 * first entry of the route-set.
3766 */
3767 if (!pj_list_empty(&acc->route_set)) {
3768 sip_uri = (pjsip_sip_uri*)
3769 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
3770 } else {
3771 pj_str_t tmp;
3772 pjsip_uri *uri;
3773
3774 pj_strdup_with_null(pool, &tmp, dst_uri);
3775
3776 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
3777 if (uri == NULL)
3778 return PJSIP_EINVALIDURI;
3779
3780 /* For non-SIP scheme, route set should be configured */
3781 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
3782 return PJSIP_ENOROUTESET;
3783
3784 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
3785 }
3786
3787 /* Get transport type of the URI */
3788 if (sip_uri->transport_param.slen == 0) {
3789 tp_type = PJSIP_TRANSPORT_UDP;
3790 } else
3791 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
3792
3793 /* If the URI uses sips scheme, make sure we use secure transport. */
3794 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
3795 unsigned tp_flag;
3796
3797 tp_flag = PJSIP_TRANSPORT_SECURE;
3798 flag = pjsip_transport_get_flag_from_type(tp_type);
3799 if ((flag & tp_flag) != tp_flag) {
3800 tp_type = pjsip_transport_get_type_from_flag(tp_flag);
3801 }
3802 }
3803
3804 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
3805 return PJSIP_EUNSUPTRANSPORT;
3806
3807 /* If destination URI specifies IPv6 or account is configured to use IPv6,
3808 * then set transport type to use IPv6 as well.
3809 */
3810 sip_pref_ip = sip_acc_get_pref_ip_ver(acc_id);
3811 if (pj_strchr(&sip_uri->host, ':') || sip_pref_ip == 6)
3812 tp_type |= PJSIP_TRANSPORT_IPV6;
3813
3814 flag = pjsip_transport_get_flag_from_type(tp_type);
3815
3816 /* Init transport selector. */
3817 pjsua_init_tpselector(acc_id, &tp_sel);
3818
3819 /* Get local address suitable to send request from */
3820 pjsip_tpmgr_fla2_param_default(&tfla2_prm);
3821 tfla2_prm.tp_type = tp_type;
3822 tfla2_prm.tp_sel = &tp_sel;
3823 tfla2_prm.dst_host = sip_uri->host;
3824 tfla2_prm.local_if = ((!pjsua_sip_acc_is_using_stun(acc_id) &&
3825 !pjsua_sip_acc_is_using_upnp(acc_id)) ||
3826 (flag & PJSIP_TRANSPORT_RELIABLE));
3827
3828 tpmgr = pjsip_endpt_get_tpmgr(pjsua_var.endpt);
3829 status = pjsip_tpmgr_find_local_addr2(tpmgr, pool, &tfla2_prm);
3830 if (status != PJ_SUCCESS)
3831 return status;
3832
3833 /* Set this as default return value. This may be changed below. */
3834 addr->host = tfla2_prm.ret_addr;
3835 addr->port = tfla2_prm.ret_port;
3836
3837 if (pj_strchr(&addr->host, ':')) {
3838 tp_type |= PJSIP_TRANSPORT_IPV6;
3839 }
3840
3841 /* If we are behind NAT64, use the Contact and Via address from
3842 * the UDP6 transport, which should be obtained from STUN.
3843 */
3844 if (acc->cfg.nat64_opt != PJSUA_NAT64_DISABLED) {
3845 pjsip_tpmgr_fla2_param tfla2_prm2 = tfla2_prm;
3846
3847 tfla2_prm2.tp_type = PJSIP_TRANSPORT_UDP6;
3848 tfla2_prm2.tp_sel = NULL;
3849 tfla2_prm2.local_if = (!pjsua_sip_acc_is_using_stun(acc_id) &&
3850 !pjsua_sip_acc_is_using_upnp(acc_id));
3851 status = pjsip_tpmgr_find_local_addr2(tpmgr, pool, &tfla2_prm2);
3852 if (status == PJ_SUCCESS) {
3853 update_addr = PJ_FALSE;
3854 addr->host = tfla2_prm2.ret_addr;
3855 if (pj_strchr(&addr->host, ':')) {
3856 tp_type |= PJSIP_TRANSPORT_IPV6;
3857 }
3858 pj_strdup(acc->pool, &acc->via_addr.host, &addr->host);
3859 acc->via_addr.port = addr->port;
3860 acc->via_tp = (pjsip_transport *)tfla2_prm.ret_tp;
3861 }
3862 } else
3863 /* For UDP transport, check if we need to overwrite the address
3864 * with its configured bound/public address.
3865 */
3866 if ((flag & PJSIP_TRANSPORT_DATAGRAM) && tfla2_prm.local_if &&
3867 tfla2_prm.ret_tp)
3868 {
3869 int i;
3870
3871 for (i = 0; i < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata); i++) {
3872 if (tfla2_prm.ret_tp==(const void *)pjsua_var.tpdata[i].data.tp) {
3873 if (pjsua_var.tpdata[i].has_cfg_addr) {
3874 pj_strdup(pool, &addr->host,
3875 &pjsua_var.tpdata[i].data.tp->local_name.host);
3876 addr->port = (pj_uint16_t)
3877 pjsua_var.tpdata[i].data.tp->local_name.port;
3878 if (pj_strchr(&addr->host, ':')) {
3879 tp_type |= PJSIP_TRANSPORT_IPV6;
3880 }
3881 }
3882 break;
3883 }
3884 }
3885 }
3886
3887 /* For TCP/TLS, acc may request to specify source port */
3888 if (acc->cfg.contact_use_src_port) {
3889 pjsip_host_info dinfo;
3890 pjsip_transport *tp = NULL;
3891 int af;
3892 pj_addrinfo ai[2];
3893 unsigned ai_cnt = 0;
3894 int ip_addr_ver;
3895 unsigned i;
3896 pjsip_tx_data tdata;
3897 pj_uint16_t port;
3898
3899 status = pjsip_get_dest_info((pjsip_uri *)sip_uri, NULL,
3900 pool, &dinfo);
3901 if (status != PJ_SUCCESS) {
3902 PJ_PERROR(4, (THIS_FILE, status, "Unable to use source local "
3903 "TCP/TLS socket address for Contact"));
3904 goto on_return;
3905 }
3906
3907 if ((dinfo.flag & PJSIP_TRANSPORT_RELIABLE) ==0 ) {
3908 /* Not TCP or TLS. No need to do this */
3909 goto on_return;
3910 }
3911
3912 ip_addr_ver = get_ip_addr_ver(&dinfo.addr.host);
3913 if (ip_addr_ver == 0 && pjsua_var.ua_cfg.nameserver_count) {
3914 /* If nameserver is configured, PJSIP will resolve destinations
3915 * by their DNS SRV record first. On the other hand, we will
3916 * resolve destination with DNS A record via pj_getaddrinfo().
3917 * They may yield different IP addresses, hence causing different
3918 * TCP/TLS connection to be created and hence different source
3919 * address.
3920 */
3921 PJ_LOG(4,(THIS_FILE, "Warning: cannot use source TCP/TLS socket"
3922 " address for Contact when nameserver is configured."));
3923 goto on_return;
3924 }
3925
3926 if (PJ_HAS_IPV6 && (ip_addr_ver == 6 || sip_pref_ip == 6)) {
3927 /* Get IPv6 address if dest host is IPv6 or we prefer IPv6. */
3928 af = pj_AF_INET6();
3929 } else if (!PJ_HAS_IPV6 || ip_addr_ver == 4 || sip_pref_ip == 4) {
3930 /* Get IPv4 address if dest host is IPv4 or we prefer IPv4. */
3931 af = pj_AF_INET();
3932 } else {
3933 /* (ip_addr_ver == 0 && sip_pref_ip == 0)
3934 * Destination host is not an IP address, and account config
3935 * indicates no IP version preference, so we'll just use
3936 * the first address returned by the OS.
3937 */
3938 af = pj_AF_UNSPEC();
3939 }
3940
3941 ai_cnt = 1;
3942 status = pj_getaddrinfo(af, &dinfo.addr.host, &ai_cnt, &ai[0]);
3943 if (status != PJ_SUCCESS)
3944 ai_cnt = 0;
3945
3946 /* Get fallback address, only if the host is not IP address and
3947 * account is not bound to a certain transport.
3948 */
3949 if (PJ_HAS_IPV6 && ip_addr_ver == 0 &&
3950 acc->tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
3951 {
3952 unsigned cnt = 1;
3953
3954 /* If first address is IPv4, fallback to IPv6, and vice versa. */
3955 if (ai_cnt) {
3956 af = (ai[0].ai_addr.addr.sa_family == pj_AF_INET())?
3957 pj_AF_INET6(): pj_AF_INET();
3958 } else {
3959 af = (af == pj_AF_INET())? pj_AF_INET6(): pj_AF_INET();
3960 }
3961 /* Check if the fallback addr is allowed. */
3962 if ((af == pj_AF_INET() &&
3963 acc->cfg.ipv6_sip_use != PJSUA_IPV6_ENABLED_USE_IPV6_ONLY) ||
3964 (af == pj_AF_INET6() &&
3965 acc->cfg.ipv6_sip_use != PJSUA_IPV6_DISABLED))
3966 {
3967 status = pj_getaddrinfo(af, &dinfo.addr.host, &cnt, &ai[ai_cnt]);
3968 if (status != PJ_SUCCESS)
3969 cnt = 0;
3970 ai_cnt += cnt;
3971 }
3972 }
3973
3974 if (ai_cnt == 0) {
3975 PJ_LOG(4, (THIS_FILE, "Unable to resolve host to "
3976 "generate Contact address"));
3977 goto on_return;
3978 }
3979
3980 /* Create a dummy tdata to inform remote host name to transport */
3981 pj_bzero(&tdata, sizeof(tdata));
3982 pj_strdup(pool, &tdata.dest_info.name, &dinfo.addr.host);
3983 port = (pj_uint16_t)dinfo.addr.port;
3984 if (port == 0) {
3985 port = (dinfo.flag & PJSIP_TRANSPORT_SECURE) ? 5061 : 5060;
3986 }
3987
3988 /* Try to acquire transport, fallback if fails. */
3989 for (i = 0; i < ai_cnt; i++) {
3990 int addr_len = pj_sockaddr_get_len(&ai[i].ai_addr);
3991
3992 if (ai[i].ai_addr.addr.sa_family == pj_AF_INET6()) {
3993 dinfo.type |= PJSIP_TRANSPORT_IPV6;
3994 tp_type |= PJSIP_TRANSPORT_IPV6;
3995 } else {
3996 dinfo.type &= ~PJSIP_TRANSPORT_IPV6;
3997 tp_type &= ~PJSIP_TRANSPORT_IPV6;
3998 }
3999
4000 pj_sockaddr_set_port(&ai[i].ai_addr, port);
4001 status = pjsip_endpt_acquire_transport2(pjsua_var.endpt,
4002 dinfo.type,
4003 &ai[i].ai_addr,
4004 addr_len,
4005 &tp_sel,
4006 &tdata, &tp);
4007 if (status == PJ_SUCCESS)
4008 break;
4009 }
4010
4011 if (status != PJ_SUCCESS) {
4012 PJ_LOG(4,(THIS_FILE, "Unable to acquire transport for "
4013 "Contact address"));
4014 goto on_return;
4015 }
4016
4017 if (tp->local_name.port == 0 || tp->local_name.host.slen==0 ||
4018 *tp->local_name.host.ptr=='0')
4019 {
4020 /* Trap zero port or "0.0.0.0" address. */
4021 /* The TCP/TLS transport is still connecting and unfortunately
4022 * this OS doesn't report the bound local address in this state.
4023 */
4024 PJ_LOG(4,(THIS_FILE, "Unable to get transport local port "
4025 "for Contact address (OS doesn't support)"));
4026 } else {
4027 /* Got the local transport address, don't update if
4028 * we are on NAT64 and already obtained the address
4029 * from STUN above.
4030 */
4031
4032 if (update_addr) {
4033 pj_strdup(pool, &addr->host, &tp->local_name.host);
4034 tp_type = tp->key.type;
4035
4036 if (pj_strchr(&addr->host, ':')) {
4037 tp_type |= PJSIP_TRANSPORT_IPV6;
4038 }
4039 }
4040 addr->port = tp->local_name.port;
4041 }
4042
4043 /* Here the transport's ref counter WILL reach zero. But the
4044 * transport will NOT get destroyed because it should have an
4045 * idle timer.
4046 */
4047 pjsip_transport_dec_ref(tp);
4048 }
4049
4050 on_return:
4051 if (p_tp_type)
4052 *p_tp_type = tp_type;
4053
4054 if (secure) {
4055 *secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
4056 }
4057
4058 if (p_tp)
4059 *p_tp = tfla2_prm.ret_tp;
4060
4061 return PJ_SUCCESS;
4062 }
4063
4064
4065 PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
4066 pj_str_t *contact,
4067 pjsua_acc_id acc_id,
4068 const pj_str_t *suri)
4069 {
4070 pjsua_acc *acc;
4071 pj_status_t status;
4072 pjsip_transport_type_e tp_type;
4073 pjsip_host_port addr;
4074 int secure;
4075 const char *beginquote, *endquote;
4076 char transport_param[32];
4077 const char *ob = ";ob";
4078
4079
4080 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
4081 acc = &pjsua_var.acc[acc_id];
4082
4083 /* If force_contact is configured, then use use it */
4084 if (acc->cfg.force_contact.slen) {
4085 *contact = acc->cfg.force_contact;
4086 return PJ_SUCCESS;
4087 }
4088
4089 status = pjsua_acc_get_uac_addr(acc_id, pool, suri, &addr,
4090 &tp_type, &secure, NULL);
4091 if (status != PJ_SUCCESS)
4092 return status;
4093
4094 /* Enclose IPv6 address in square brackets */
4095 if (tp_type & PJSIP_TRANSPORT_IPV6) {
4096 beginquote = "[";
4097 endquote = "]";
4098 } else {
4099 beginquote = endquote = "";
4100 }
4101
4102 /* Don't add transport parameter if it's UDP */
4103 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
4104 pj_ansi_snprintf(transport_param, sizeof(transport_param),
4105 ";transport=%s",
4106 pjsip_transport_get_type_name(tp_type));
4107 } else {
4108 transport_param[0] = '\0';
4109 }
4110
4111
4112 /* Create the contact header */
4113 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
4114 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
4115 "%s%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s%s>%.*s",
4116 (acc->display.slen?"\"" : ""),
4117 (int)acc->display.slen,
4118 acc->display.ptr,
4119 (acc->display.slen?"\" " : ""),
4120 ((secure && acc->is_sips)? "sips" : "sip"),
4121 (int)acc->user_part.slen,
4122 acc->user_part.ptr,
4123 (acc->user_part.slen?"@":""),
4124 beginquote,
4125 (int)addr.host.slen,
4126 addr.host.ptr,
4127 endquote,
4128 addr.port,
4129 transport_param,
4130 (int)acc->cfg.contact_uri_params.slen,
4131 acc->cfg.contact_uri_params.ptr,
4132 (acc->cfg.use_rfc5626? ob: ""),
4133 (int)acc->cfg.contact_params.slen,
4134 acc->cfg.contact_params.ptr);
4135 if (contact->slen < 1 || contact->slen >= (int)PJSIP_MAX_URL_SIZE)
4136 return PJ_ETOOSMALL;
4137 return PJ_SUCCESS;
4138 }
4139
4140
4141
4142 PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
4143 pj_str_t *contact,
4144 pjsua_acc_id acc_id,
4145 pjsip_rx_data *rdata )
4146 {
4147 /*
4148 * Section 12.1.1, paragraph about using SIPS URI in Contact.
4149 * If the request that initiated the dialog contained a SIPS URI
4150 * in the Request-URI or in the top Record-Route header field value,
4151 * if there was any, or the Contact header field if there was no
4152 * Record-Route header field, the Contact header field in the response
4153 * MUST be a SIPS URI.
4154 */
4155 pjsua_acc *acc;
4156 pjsip_sip_uri *sip_uri;
4157 pj_status_t status;
4158 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
4159 pj_str_t local_addr;
4160 pjsip_tpselector tp_sel;
4161 pjsip_tpmgr *tpmgr;
4162 pjsip_tpmgr_fla2_param tfla2_prm;
4163 unsigned flag;
4164 int secure;
4165 int local_port;
4166 const char *beginquote, *endquote;
4167 char transport_param[32];
4168
4169 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
4170 acc = &pjsua_var.acc[acc_id];
4171
4172 /* If force_contact is configured, then use use it */
4173 if (acc->cfg.force_contact.slen) {
4174 *contact = acc->cfg.force_contact;
4175 return PJ_SUCCESS;
4176 }
4177
4178 /* If Record-Route is present, then URI is the top Record-Route. */
4179 if (rdata->msg_info.record_route) {
4180 sip_uri = (pjsip_sip_uri*)
4181 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
4182 } else {
4183 pjsip_hdr *pos = NULL;
4184 pjsip_contact_hdr *h_contact;
4185 pjsip_uri *uri = NULL;
4186
4187 /* Otherwise URI is Contact URI.
4188 * Iterate the Contact URI until we find sip: or sips: scheme.
4189 */
4190 do {
4191 h_contact = (pjsip_contact_hdr*)
4192 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
4193 pos);
4194 if (h_contact) {
4195 if (h_contact->uri)
4196 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
4197 else
4198 uri = NULL;
4199 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
4200 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
4201 {
4202 pos = (pjsip_hdr*)h_contact->next;
4203 if (pos == &rdata->msg_info.msg->hdr)
4204 h_contact = NULL;
4205 } else {
4206 break;
4207 }
4208 }
4209 } while (h_contact);
4210
4211
4212 /* Or if Contact URI is not present, take the remote URI from
4213 * the From URI.
4214 */
4215 if (uri == NULL)
4216 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
4217
4218
4219 /* Can only do sip/sips scheme at present. */
4220 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
4221 return PJSIP_EINVALIDREQURI;
4222
4223 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
4224 }
4225
4226 /* Get transport type of the URI */
4227 if (sip_uri->transport_param.slen == 0) {
4228 tp_type = PJSIP_TRANSPORT_UDP;
4229 } else
4230 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
4231
4232 /* If the URI uses sips scheme, make sure we use secure transport. */
4233 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
4234 unsigned tp_flag;
4235
4236 tp_flag = PJSIP_TRANSPORT_SECURE;
4237 flag = pjsip_transport_get_flag_from_type(tp_type);
4238 if ((flag & tp_flag) != tp_flag) {
4239 tp_type = pjsip_transport_get_type_from_flag(tp_flag);
4240 }
4241 }
4242
4243 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
4244 return PJSIP_EUNSUPTRANSPORT;
4245
4246 /* If destination URI specifies IPv6 or account is configured to use IPv6
4247 * or the transport being used to receive data is an IPv6 transport,
4248 * then set transport type to use IPv6 as well.
4249 */
4250 if (pj_strchr(&sip_uri->host, ':') ||
4251 pjsua_sip_acc_is_using_ipv6(acc_id) ||
4252 (rdata->tp_info.transport->key.type & PJSIP_TRANSPORT_IPV6))
4253 {
4254 if (!PJ_HAS_IPV6 || acc->cfg.ipv6_sip_use == PJSUA_IPV6_DISABLED)
4255 return PJSIP_EUNSUPTRANSPORT;
4256 tp_type = (pjsip_transport_type_e)
4257 (((int)tp_type) | PJSIP_TRANSPORT_IPV6);
4258 }
4259
4260 flag = pjsip_transport_get_flag_from_type(tp_type);
4261 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
4262
4263 /* Init transport selector. */
4264 pj_bzero(&tp_sel, sizeof(tp_sel));
4265 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
4266 pjsua_init_tpselector(acc_id, &tp_sel);
4267 }
4268
4269 /* Get local address suitable to send response from */
4270 pjsip_tpmgr_fla2_param_default(&tfla2_prm);
4271 tfla2_prm.tp_type = tp_type;
4272 tfla2_prm.tp_sel = &tp_sel;
4273 tfla2_prm.dst_host = sip_uri->host;
4274 tfla2_prm.local_if = ((!pjsua_sip_acc_is_using_stun(acc_id) &&
4275 !pjsua_sip_acc_is_using_upnp(acc_id)) ||
4276 (flag & PJSIP_TRANSPORT_RELIABLE));
4277
4278 tpmgr = pjsip_endpt_get_tpmgr(pjsua_var.endpt);
4279 status = pjsip_tpmgr_find_local_addr2(tpmgr, pool, &tfla2_prm);
4280 if (status != PJ_SUCCESS)
4281 return status;
4282
4283 local_addr = tfla2_prm.ret_addr;
4284 local_port = tfla2_prm.ret_port;
4285
4286 /* For UDP transport, check if we need to overwrite the address
4287 * with its configured bound/public address.
4288 */
4289 if ((flag & PJSIP_TRANSPORT_DATAGRAM) && tfla2_prm.local_if &&
4290 tfla2_prm.ret_tp)
4291 {
4292 int i;
4293
4294 for (i = 0; i < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata); i++) {
4295 if (tfla2_prm.ret_tp==(const void *)pjsua_var.tpdata[i].data.tp) {
4296 if (pjsua_var.tpdata[i].has_cfg_addr) {
4297 pj_strdup(pool, &local_addr,
4298 &pjsua_var.tpdata[i].data.tp->local_name.host);
4299 local_port = (pj_uint16_t)
4300 pjsua_var.tpdata[i].data.tp->local_name.port;
4301 }
4302 break;
4303 }
4304 }
4305 }
4306
4307 /* Enclose IPv6 address in square brackets */
4308 if (tp_type & PJSIP_TRANSPORT_IPV6) {
4309 beginquote = "[";
4310 endquote = "]";
4311 } else {
4312 beginquote = endquote = "";
4313 }
4314
4315 /* Don't add transport parameter if it's UDP */
4316 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
4317 pj_ansi_snprintf(transport_param, sizeof(transport_param),
4318 ";transport=%s",
4319 pjsip_transport_get_type_name(tp_type));
4320 } else {
4321 transport_param[0] = '\0';
4322 }
4323
4324
4325 /* Create the contact header */
4326 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
4327 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
4328 "%s%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
4329 (acc->display.slen?"\"" : ""),
4330 (int)acc->display.slen,
4331 acc->display.ptr,
4332 (acc->display.slen?"\" " : ""),
4333 ((secure && acc->is_sips)? "sips" : "sip"),
4334 (int)acc->user_part.slen,
4335 acc->user_part.ptr,
4336 (acc->user_part.slen?"@":""),
4337 beginquote,
4338 (int)local_addr.slen,
4339 local_addr.ptr,
4340 endquote,
4341 local_port,
4342 transport_param,
4343 (int)acc->cfg.contact_uri_params.slen,
4344 acc->cfg.contact_uri_params.ptr,
4345 (int)acc->cfg.contact_params.slen,
4346 acc->cfg.contact_params.ptr);
4347 if (contact->slen < 1 || contact->slen >= (int)PJSIP_MAX_URL_SIZE)
4348 return PJ_ETOOSMALL;
4349
4350 return PJ_SUCCESS;
4351 }
4352
4353
4354 PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
4355 pjsua_transport_id tp_id)
4356 {
4357 pjsua_acc *acc;
4358
4359 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
4360 acc = &pjsua_var.acc[acc_id];
4361
4362 PJ_ASSERT_RETURN(tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
4363
4364 if (acc->cfg.transport_id == tp_id)
4365 return PJ_SUCCESS;
4366
4367 acc->cfg.transport_id = tp_id;
4368
4369 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
4370 acc->tp_type = pjsua_var.tpdata[acc->cfg.transport_id].type;
4371 if (acc->regc) {
4372 /* Update client registration's transport. */
4373 pjsip_tpselector tp_sel;
4374
4375 pjsua_init_tpselector(acc_id, &tp_sel);
4376 pjsip_regc_set_transport(acc->regc, &tp_sel);
4377 }
4378 } else {
4379 acc->tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
4380 }
4381
4382 return PJ_SUCCESS;
4383 }
4384
4385
4386 /* Auto re-registration timeout callback */
4387 static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
4388 {
4389 pjsua_acc *acc;
4390 pj_status_t status;
4391
4392 PJ_UNUSED_ARG(th);
4393 acc = (pjsua_acc*) te->user_data;
4394 pj_assert(acc);
4395
4396 PJSUA_LOCK();
4397
4398 /* Check if the reregistration timer is still valid, e.g: while waiting
4399 * timeout timer application might have deleted the account or disabled
4400 * the auto-reregistration.
4401 */
4402 if (!acc->valid || !acc->auto_rereg.active ||
4403 acc->cfg.reg_retry_interval == 0)
4404 {
4405 goto on_return;
4406 }
4407
4408 /* Start re-registration */
4409 acc->auto_rereg.attempt_cnt++;
4410
4411 /* Generate new contact as the current contact may use a disconnected
4412 * transport. Only do this when outbound is not active and contact is not
4413 * rewritten (where the contact address may really be used by server to
4414 * contact the UA).
4415 */
4416 if (acc->rfc5626_status != OUTBOUND_ACTIVE && !acc->contact_rewritten) {
4417 pj_str_t tmp_contact;
4418 pj_pool_t *pool;
4419
4420 pool = pjsua_pool_create("tmpregc", 512, 512);
4421
4422 status = pjsua_acc_create_uac_contact(pool, &tmp_contact, acc->index,
4423 &acc->cfg.reg_uri);
4424 if (status != PJ_SUCCESS) {
4425 pjsua_perror(THIS_FILE,
4426 "Unable to generate suitable Contact header"
4427 " for re-registration", status);
4428 pj_pool_release(pool);
4429 schedule_reregistration(acc);
4430 goto on_return;
4431 }
4432
4433 if (pj_strcmp(&tmp_contact, &acc->contact)) {
4434 if (acc->contact.slen < tmp_contact.slen) {
4435 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
4436 } else {
4437 pj_strncpy_with_null(&acc->contact, &tmp_contact,
4438 PJSIP_MAX_URL_SIZE);
4439 }
4440 update_regc_contact(acc);
4441 if (acc->regc)
4442 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
4443 }
4444 pj_pool_release(pool);
4445 }
4446
4447 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
4448 if (status != PJ_SUCCESS)
4449 schedule_reregistration(acc);
4450
4451 on_return:
4452 PJSUA_UNLOCK();
4453 }
4454
4455
4456 /* Schedule reregistration for specified account. Note that the first
4457 * re-registration after a registration failure will be done immediately.
4458 * Also note that this function should be called within PJSUA mutex.
4459 */
4460 static void schedule_reregistration(pjsua_acc *acc)
4461 {
4462 pj_time_val delay;
4463
4464 pj_assert(acc);
4465
4466 /* Validate the account and re-registration feature status */
4467 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
4468 return;
4469 }
4470
4471 /* If configured, disconnect calls of this account after the first
4472 * reregistration attempt failed.
4473 */
4474 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
4475 {
4476 unsigned i, cnt;
4477
4478 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
4479 if (pjsua_var.calls[i].acc_id == acc->index) {
4480 pjsua_call_hangup(i, 0, NULL, NULL);
4481 ++cnt;
4482 }
4483 }
4484
4485 if (cnt) {
4486 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
4487 "after reregistration attempt failed",
4488 cnt, acc->index));
4489 }
4490 }
4491
4492 /* Cancel any re-registration timer */
4493 if (acc->auto_rereg.timer.id) {
4494 acc->auto_rereg.timer.id = PJ_FALSE;
4495 pjsua_cancel_timer(&acc->auto_rereg.timer);
4496 }
4497
4498 /* Update re-registration flag */
4499 acc->auto_rereg.active = PJ_TRUE;
4500
4501 /* Set up timer for reregistration */
4502 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
4503 acc->auto_rereg.timer.user_data = acc;
4504
4505 /* Reregistration attempt. The first attempt will be done immediately. */
4506 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval :
4507 acc->cfg.reg_first_retry_interval;
4508 delay.msec = 0;
4509
4510 /* Randomize interval by +/- reg_retry_random_interval, if configured */
4511 if (acc->cfg.reg_retry_random_interval) {
4512 long rand_ms = acc->cfg.reg_retry_random_interval * 1000;
4513 if (delay.sec >= (long)acc->cfg.reg_retry_random_interval) {
4514 delay.msec = -rand_ms + (pj_rand() % (rand_ms * 2));
4515 } else {
4516 delay.sec = 0;
4517 delay.msec = (pj_rand() % (delay.sec * 1000 + rand_ms));
4518 }
4519 }
4520 pj_time_val_normalize(&delay);
4521
4522 PJ_LOG(4,(THIS_FILE,
4523 "Scheduling re-registration retry for acc %d in %lu seconds..",
4524 acc->index, delay.sec));
4525
4526 acc->auto_rereg.timer.id = PJ_TRUE;
4527 if (pjsua_schedule_timer(&acc->auto_rereg.timer, &delay) != PJ_SUCCESS)
4528 acc->auto_rereg.timer.id = PJ_FALSE;
4529 }
4530
4531
4532 /* Internal function to perform auto-reregistration on transport
4533 * connection/disconnection events.
4534 */
4535 void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
4536 pjsip_transport_state state,
4537 const pjsip_transport_state_info *info)
4538 {
4539 unsigned i;
4540
4541 PJ_UNUSED_ARG(info);
4542
4543 /* Only care for transport disconnection events */
4544 if (state != PJSIP_TP_STATE_DISCONNECTED)
4545 return;
4546
4547 PJ_LOG(4,(THIS_FILE, "Disconnected notification for transport %s",
4548 tp->obj_name));
4549 pj_log_push_indent();
4550
4551 /* Shutdown this transport, to make sure that the transport manager
4552 * will create a new transport for reconnection.
4553 */
4554 pjsip_transport_shutdown(tp);
4555
4556 PJSUA_LOCK();
4557
4558 /* Enumerate accounts using this transport and perform actions
4559 * based on the transport state.
4560 */
4561 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
4562 pjsua_acc *acc = &pjsua_var.acc[i];
4563
4564 /* Skip if this account is not valid. */
4565 if (!acc->valid)
4566 continue;
4567
4568 /* Reset Account's via transport and via address */
4569 if (acc->via_tp == (void*)tp) {
4570 pj_bzero(&acc->via_addr, sizeof(acc->via_addr));
4571 acc->via_tp = NULL;
4572
4573 /* Also reset regc's Via addr */
4574 if (acc->regc)
4575 pjsip_regc_set_via_sent_by(acc->regc, NULL, NULL);
4576 }
4577
4578 /* Release transport immediately if regc is using it
4579 * See https://github.com/pjsip/pjproject/issues/1481
4580 */
4581 if (acc->regc) {
4582 pjsip_regc_info reg_info;
4583
4584 pjsip_regc_get_info(acc->regc, &reg_info);
4585 if (reg_info.transport != tp)
4586 continue;
4587
4588 pjsip_regc_release_transport(pjsua_var.acc[i].regc);
4589
4590 /* Reset contact rewrite flag so that re-registration will
4591 * generate a new Contact based on the new transport.
4592 */
4593 acc->contact_rewritten = PJ_FALSE;
4594 if (acc->rfc5626_status == OUTBOUND_ACTIVE) {
4595 acc->rfc5626_status = OUTBOUND_WANTED;
4596 }
4597
4598 if (pjsua_var.acc[i].ip_change_op ==
4599 PJSUA_IP_CHANGE_OP_ACC_SHUTDOWN_TP)
4600 {
4601 /* Before progressing to next step, report here. */
4602 if (pjsua_var.ua_cfg.cb.on_ip_change_progress) {
4603 pjsua_ip_change_op_info ch_info;
4604
4605 pj_bzero(&ch_info, sizeof(ch_info));
4606 ch_info.acc_shutdown_tp.acc_id = acc->index;
4607
4608 pjsua_var.ua_cfg.cb.on_ip_change_progress(
4609 acc->ip_change_op,
4610 PJ_SUCCESS,
4611 &ch_info);
4612 }
4613
4614 if (acc->cfg.allow_contact_rewrite) {
4615 pjsua_acc_update_contact_on_ip_change(acc);
4616 } else {
4617 pjsua_acc_handle_call_on_ip_change(acc);
4618 }
4619 } else if (acc->cfg.reg_retry_interval) {
4620 /* Schedule reregistration for this account */
4621 schedule_reregistration(acc);
4622 }
4623 }
4624 }
4625
4626 PJSUA_UNLOCK();
4627 pj_log_pop_indent();
4628 }
4629
4630
4631 /*
4632 * Internal function to update contact on ip change process.
4633 */
4634 pj_status_t pjsua_acc_update_contact_on_ip_change(pjsua_acc *acc)
4635 {
4636 pj_status_t status;
4637 pj_bool_t need_unreg = ((acc->cfg.contact_rewrite_method &
4638 PJSUA_CONTACT_REWRITE_UNREGISTER) != 0);
4639 pj_bool_t no_unreg = ((acc->cfg.contact_rewrite_method &
4640 PJSUA_CONTACT_REWRITE_NO_UNREG) != 0);
4641
4642 acc->ip_change_op = PJSUA_IP_CHANGE_OP_ACC_UPDATE_CONTACT;
4643
4644 PJ_LOG(3, (THIS_FILE, "%.*s: send %sregistration triggered "
4645 "by IP change", (int)acc->cfg.id.slen,
4646 acc->cfg.id.ptr, (need_unreg ? "un-" : "")));
4647
4648 /* Prepare for contact rewrite */
4649 acc->contact_rewritten = PJ_FALSE;
4650
4651 status = pjsua_acc_set_registration(acc->index, !need_unreg);
4652 if ((status != PJ_SUCCESS)
4653 && (acc->ip_change_op == PJSUA_IP_CHANGE_OP_ACC_UPDATE_CONTACT))
4654 {
4655 if (status == PJSIP_EBUSY) {
4656 PJ_LOG(4, (THIS_FILE, "%.*s: Retrying %sregistration triggered "
4657 "by IP change", (int)acc->cfg.id.slen,
4658 acc->cfg.id.ptr,
4659 (need_unreg ? "un-" : "")));
4660
4661 /* Retry the (un)registration. */
4662 if (acc->regc) {
4663 pj_str_t old_reg_contact = acc->reg_contact;
4664 status = PJ_SUCCESS;
4665 destroy_regc(acc, PJ_TRUE);
4666 update_keep_alive(acc, PJ_FALSE, NULL);
4667
4668 status = pjsua_regc_init(acc->index);
4669 if (need_unreg || no_unreg)
4670 pjsip_regc_update_contact(acc->regc, 1, &old_reg_contact);
4671 if (no_unreg)
4672 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
4673
4674 if (status == PJ_SUCCESS) {
4675 status = pjsua_acc_set_registration(acc->index, !need_unreg);
4676 if (status == PJ_SUCCESS) {
4677 return status;
4678 }
4679 }
4680 }
4681 }
4682
4683 if (pjsua_var.ua_cfg.cb.on_ip_change_progress) {
4684 /* If update contact fails, notification might already been
4685 * triggered from registration callback.
4686 */
4687 pjsua_ip_change_op_info info;
4688
4689 pj_bzero(&info, sizeof(info));
4690 info.acc_update_contact.acc_id = acc->index;
4691 info.acc_update_contact.is_register = !need_unreg;
4692
4693 pjsua_var.ua_cfg.cb.on_ip_change_progress(acc->ip_change_op,
4694 status,
4695 &info);
4696 }
4697 pjsua_acc_end_ip_change(acc);
4698 }
4699 return status;
4700 }
4701
4702
4703 /*
4704 * Internal function to handle call on ip change process.
4705 */
4706 pj_status_t pjsua_acc_handle_call_on_ip_change(pjsua_acc *acc)
4707 {
4708 pj_status_t status = PJ_SUCCESS;
4709 unsigned i = 0;
4710
4711 PJSUA_LOCK();
4712 if (acc->cfg.ip_change_cfg.hangup_calls ||
4713 acc->cfg.ip_change_cfg.reinvite_flags)
4714 {
4715 for (i = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
4716 pjsua_call_info call_info;
4717 pjsua_call *call;
4718 pjsip_dialog *dlg = NULL;
4719
4720 if (!pjsua_call_is_active(i) ||
4721 pjsua_var.calls[i].acc_id != acc->index ||
4722 pjsua_call_get_info(i, &call_info) != PJ_SUCCESS)
4723 {
4724 continue;
4725 }
4726
4727 status = acquire_call("call_tpsel_on_ip_change()",
4728 i, &call, &dlg);
4729 if (status == PJ_SUCCESS) {
4730 pjsip_tpselector tp_sel;
4731
4732 /* Set dialog's transport based on acc's config. */
4733 pjsua_init_tpselector(call->acc_id, &tp_sel);
4734 pjsip_dlg_set_transport(dlg, &tp_sel);
4735
4736 pjsip_dlg_dec_lock(dlg);
4737 } else {
4738 PJ_LOG(4, (THIS_FILE, "Failed to update call %d's transport "
4739 "selector", i));
4740 }
4741
4742 if ((acc->cfg.ip_change_cfg.hangup_calls) &&
4743 (call_info.state >= PJSIP_INV_STATE_EARLY))
4744 {
4745 acc->ip_change_op = PJSUA_IP_CHANGE_OP_ACC_HANGUP_CALLS;
4746 PJ_LOG(3, (THIS_FILE, "call to %.*s: hangup "
4747 "triggered by IP change",
4748 (int)call_info.remote_info.slen,
4749 call_info.remote_info.ptr));
4750
4751 status = pjsua_call_hangup(i, PJSIP_SC_GONE, NULL, NULL);
4752
4753 if (pjsua_var.ua_cfg.cb.on_ip_change_progress) {
4754 pjsua_ip_change_op_info info;
4755
4756 pj_bzero(&info, sizeof(info));
4757 info.acc_hangup_calls.acc_id = acc->index;
4758 info.acc_hangup_calls.call_id = call_info.id;
4759
4760 pjsua_var.ua_cfg.cb.on_ip_change_progress(
4761 acc->ip_change_op,
4762 status,
4763 &info);
4764 }
4765 }
4766 else if (call_info.role==PJSIP_ROLE_UAC &&
4767 call_info.state < PJSIP_INV_STATE_CONFIRMED)
4768 {
4769 /* Hangup not yet confirmed outgoing calls */
4770 acc->ip_change_op = PJSUA_IP_CHANGE_OP_ACC_HANGUP_CALLS;
4771 PJ_LOG(3, (THIS_FILE, "Unconfirmed outgoing call to %.*s: "
4772 "hangup triggered by IP change",
4773 (int)call_info.remote_info.slen,
4774 call_info.remote_info.ptr));
4775
4776 status = pjsua_call_hangup(i, PJSIP_SC_GONE, NULL, NULL);
4777
4778 if (pjsua_var.ua_cfg.cb.on_ip_change_progress) {
4779 pjsua_ip_change_op_info info;
4780
4781 pj_bzero(&info, sizeof(info));
4782 info.acc_hangup_calls.acc_id = acc->index;
4783 info.acc_hangup_calls.call_id = call_info.id;
4784
4785 pjsua_var.ua_cfg.cb.on_ip_change_progress(
4786 acc->ip_change_op,
4787 status,
4788 &info);
4789 }
4790 } else if ((acc->cfg.ip_change_cfg.reinvite_flags) &&
4791 (call_info.state == PJSIP_INV_STATE_CONFIRMED))
4792 {
4793 pj_bool_t use_update = acc->cfg.ip_change_cfg.reinv_use_update;
4794
4795 /* Check if remote support SIP UPDATE method */
4796 if (use_update) {
4797 PJ_LOG(5, (THIS_FILE, "Call #%d: IP change is configured "
4798 "to using UPDATE", i));
4799
4800 status = acquire_call("handle_call_on_ip_change()",
4801 i, &call, &dlg);
4802 if (status != PJ_SUCCESS) {
4803 use_update = PJ_FALSE;
4804 PJ_PERROR(3,(THIS_FILE, status,
4805 "Call #%d: IP change cannot "
4806 "check if remote supports UPDATE due to "
4807 "failure in acquiring dialog lock", i));
4808 } else {
4809 const pj_str_t ST_UPDATE = {"UPDATE", 6};
4810 use_update = pjsip_dlg_remote_has_cap(
4811 dlg, PJSIP_H_ALLOW, NULL,
4812 &ST_UPDATE)
4813 == PJSIP_DIALOG_CAP_SUPPORTED;
4814 pjsip_dlg_dec_lock(dlg);
4815
4816 if (!use_update) {
4817 PJ_LOG(3, (THIS_FILE, "Call #%d: IP change will "
4818 "use re-INVITE because remote does "
4819 "not support UPDATE", i));
4820 }
4821 }
4822 }
4823
4824 acc->ip_change_op = PJSUA_IP_CHANGE_OP_ACC_REINVITE_CALLS;
4825
4826 pjsua_call_cleanup_flag(&call_info.setting);
4827 call_info.setting.flag |=
4828 acc->cfg.ip_change_cfg.reinvite_flags;
4829
4830 PJ_LOG(3, (THIS_FILE, "Call #%d to %.*s: send %s "
4831 "with flags 0x%x triggered "
4832 "by IP change (IP change flag: 0x%x)",
4833 i,
4834 (int)call_info.remote_info.slen,
4835 call_info.remote_info.ptr,
4836 (use_update? "UPDATE" : "re-INVITE"),
4837 call_info.setting.flag,
4838 acc->cfg.ip_change_cfg.reinvite_flags));
4839
4840 /* Refresh call using UPDATE */
4841 if (use_update) {
4842 status = pjsua_call_update(i, call_info.setting.flag,
4843 NULL);
4844 /* If this fails, retry using INVITE below */
4845 if (status != PJ_SUCCESS) {
4846 PJ_LOG(3, (THIS_FILE, "Call #%d: failed sending UPDATE"
4847 " retrying using re-INVITE", i));
4848 use_update = PJ_FALSE;
4849 }
4850 }
4851
4852 /* Refresh call using re-INVITE */
4853 if (!use_update) {
4854 status = pjsua_call_reinvite(i, call_info.setting.flag,
4855 NULL);
4856 }
4857
4858 if (pjsua_var.ua_cfg.cb.on_ip_change_progress) {
4859 pjsua_ip_change_op_info info;
4860
4861 pj_bzero(&info, sizeof(info));
4862 info.acc_reinvite_calls.acc_id = acc->index;
4863 info.acc_reinvite_calls.call_id = call_info.id;
4864
4865 pjsua_var.ua_cfg.cb.on_ip_change_progress(
4866 acc->ip_change_op,
4867 status,
4868 &info);
4869 }
4870
4871 }
4872 }
4873 }
4874 pjsua_acc_end_ip_change(acc);
4875 PJSUA_UNLOCK();
4876 return status;
4877 }
4878
4879 void pjsua_acc_end_ip_change(pjsua_acc *acc)
4880 {
4881 int i = 0;
4882 pj_bool_t all_done = PJ_TRUE;
4883
4884 PJSUA_LOCK();
4885 if (acc && acc->ip_change_op < PJSUA_IP_CHANGE_OP_COMPLETED) {
4886 PJ_LOG(3, (THIS_FILE, "IP address change handling for acc %d "
4887 "completed", acc->index));
4888 acc->ip_change_op = PJSUA_IP_CHANGE_OP_COMPLETED;
4889 if (pjsua_var.acc_cnt) {
4890 for (; i < (int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
4891 if (pjsua_var.acc[i].valid &&
4892 pjsua_var.acc[i].ip_change_op !=
4893 PJSUA_IP_CHANGE_OP_COMPLETED)
4894 {
4895 all_done = PJ_FALSE;
4896 break;
4897 }
4898 }
4899 }
4900 }
4901 if (all_done && pjsua_var.ua_cfg.cb.on_ip_change_progress) {
4902 PJ_LOG(3, (THIS_FILE, "IP address change handling completed"));
4903 pjsua_var.ua_cfg.cb.on_ip_change_progress(
4904 PJSUA_IP_CHANGE_OP_COMPLETED,
4905 PJ_SUCCESS,
4906 NULL);
4907 }
4908 PJSUA_UNLOCK();
4909 }
4910
4911 /*
4912 * Send response to incoming SIP MESSAGE request.
4913 */
4914 PJ_DEF(pj_status_t) pjsua_acc_send_response(pjsua_acc_id acc_id,
4915 pjsip_rx_data *rdata,
4916 pjsip_transaction *tsx,
4917 int st_code,
4918 const pj_str_t *st_text,
4919 const pjsua_msg_data *msg_data)
4920 {
4921 pjsip_tx_data *tdata = NULL;
4922 pj_status_t status;
4923
4924 PJ_ASSERT_RETURN(rdata != NULL, PJ_EINVAL);
4925 PJ_ASSERT_RETURN(st_code >= 200 && st_code < 700, PJ_EINVAL);
4926 PJ_UNUSED_ARG(acc_id);
4927
4928 if (!tsx) {
4929 PJ_LOG(1,(THIS_FILE, "UAS transaction not found for MESSAGE response"));
4930 return PJ_ENOTFOUND;
4931 }
4932
4933 PJ_ASSERT_RETURN(tsx->role == PJSIP_ROLE_UAS, PJ_EINVAL);
4934 PJ_ASSERT_RETURN(pjsip_method_cmp(&tsx->method, &pjsip_message_method) == 0,
4935 PJ_EINVAL);
4936
4937 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata,
4938 st_code, st_text, &tdata);
4939 if (status != PJ_SUCCESS) {
4940 pjsua_perror(THIS_FILE, "Unable to create response", status);
4941 return status;
4942 }
4943
4944 if (msg_data) {
4945 const pjsip_hdr *hdr;
4946 for (hdr = msg_data->hdr_list.next; hdr && hdr != &msg_data->hdr_list;
4947 hdr=hdr->next) {
4948 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(
4949 tdata->pool, hdr));
4950 }
4951 }
4952
4953 status = pjsip_tsx_send_msg(tsx, tdata);
4954 if (status != PJ_SUCCESS) {
4955 pjsua_perror(THIS_FILE, "Unable to send response", status);
4956 pjsip_tx_data_dec_ref(tdata);
4957 return status;
4958 }
4959
4960 return PJ_SUCCESS;
4961 }