blob: 57f4093a02c430c63efa92b51976ef3dbb2e4b84 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjsua-lib/pjsua.h>
21#include <pjsua-lib/pjsua_internal.h>
22
23
24#define THIS_FILE "pjsua_acc.c"
25
Benny Prijonob54719f2010-11-16 03:07:46 +000026enum
27{
Benny Prijono2562b752010-11-16 06:01:38 +000028 OUTBOUND_UNKNOWN, // status unknown
29 OUTBOUND_WANTED, // initiated in registration
30 OUTBOUND_ACTIVE, // got positive response from server
31 OUTBOUND_NA // not wanted or got negative response from server
Benny Prijonob54719f2010-11-16 03:07:46 +000032};
33
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +000035static void schedule_reregistration(pjsua_acc *acc);
36
Benny Prijonoeebe9af2006-06-13 22:57:13 +000037/*
38 * Get number of current accounts.
39 */
40PJ_DEF(unsigned) pjsua_acc_get_count(void)
41{
42 return pjsua_var.acc_cnt;
43}
44
45
46/*
47 * Check if the specified account ID is valid.
48 */
49PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
50{
Benny Prijonoa1e69682007-05-11 15:14:34 +000051 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052 pjsua_var.acc[acc_id].valid;
53}
54
55
56/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000057 * Set default account
58 */
59PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
60{
61 pjsua_var.default_acc = acc_id;
62 return PJ_SUCCESS;
63}
64
65
66/*
67 * Get default account.
68 */
69PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
70{
71 return pjsua_var.default_acc;
72}
73
74
75/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000076 * Copy account configuration.
77 */
Benny Prijonobddef2c2007-10-31 13:28:08 +000078PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
79 pjsua_acc_config *dst,
80 const pjsua_acc_config *src)
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081{
82 unsigned i;
83
84 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
85
86 pj_strdup_with_null(pool, &dst->id, &src->id);
87 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000088 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +000089 pj_strdup_with_null(pool, &dst->contact_params, &src->contact_params);
90 pj_strdup_with_null(pool, &dst->contact_uri_params,
91 &src->contact_uri_params);
Benny Prijonofe04fb52007-08-24 08:28:52 +000092 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +000093 pj_strdup_with_null(pool, &dst->rfc5626_instance_id,
94 &src->rfc5626_instance_id);
Benny Prijonob54719f2010-11-16 03:07:46 +000095 pj_strdup_with_null(pool, &dst->rfc5626_reg_id, &src->rfc5626_reg_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000096
97 dst->proxy_cnt = src->proxy_cnt;
98 for (i=0; i<src->proxy_cnt; ++i)
99 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
100
101 dst->reg_timeout = src->reg_timeout;
Sauw Ming9b80d512011-03-15 03:20:37 +0000102 dst->reg_delay_before_refresh = src->reg_delay_before_refresh;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103 dst->cred_count = src->cred_count;
104
105 for (i=0; i<src->cred_count; ++i) {
106 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
107 }
Benny Prijonobddef2c2007-10-31 13:28:08 +0000108
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +0000109 pj_list_init(&dst->reg_hdr_list);
110 if (!pj_list_empty(&src->reg_hdr_list)) {
111 const pjsip_hdr *hdr;
112
113 hdr = src->reg_hdr_list.next;
114 while (hdr != &src->reg_hdr_list) {
115 pj_list_push_back(&dst->reg_hdr_list, pjsip_hdr_clone(pool, hdr));
116 hdr = hdr->next;
117 }
118 }
119
Benny Prijono639873e2011-03-23 03:46:26 +0000120 pj_list_init(&dst->sub_hdr_list);
121 if (!pj_list_empty(&src->sub_hdr_list)) {
122 const pjsip_hdr *hdr;
123
124 hdr = src->sub_hdr_list.next;
125 while (hdr != &src->sub_hdr_list) {
126 pj_list_push_back(&dst->sub_hdr_list, pjsip_hdr_clone(pool, hdr));
127 hdr = hdr->next;
128 }
129 }
130
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +0000131 pjsip_auth_clt_pref_dup(pool, &dst->auth_pref, &src->auth_pref);
132
Benny Prijono0bc99a92011-03-17 04:34:43 +0000133 pjsua_transport_config_dup(pool, &dst->rtp_cfg, &src->rtp_cfg);
134
Benny Prijonobddef2c2007-10-31 13:28:08 +0000135 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000136}
137
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000138/*
139 * Calculate CRC of proxy list.
140 */
141static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt)
142{
143 pj_crc32_context ctx;
144 unsigned i;
145
146 pj_crc32_init(&ctx);
147 for (i=0; i<cnt; ++i) {
148 pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen);
149 }
150
151 return pj_crc32_final(&ctx);
152}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000153
154/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000155 * Initialize a new account (after configuration is set).
156 */
157static pj_status_t initialize_acc(unsigned acc_id)
158{
159 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
160 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000161 pjsip_name_addr *name_addr;
Benny Prijonoc7545782010-09-28 07:43:18 +0000162 pjsip_sip_uri *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000163 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000164 unsigned i;
165
166 /* Need to parse local_uri to get the elements: */
167
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000168 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000169 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000170 acc_cfg->id.slen,
171 PJSIP_PARSE_URI_AS_NAMEADDR);
172 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173 pjsua_perror(THIS_FILE, "Invalid local URI",
174 PJSIP_EINVALIDURI);
175 return PJSIP_EINVALIDURI;
176 }
177
178 /* Local URI MUST be a SIP or SIPS: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000179 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
180 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181 {
Benny Prijonoc7545782010-09-28 07:43:18 +0000182 acc->display = name_addr->display;
183 acc->user_part = name_addr->display;
184 acc->srv_domain = pj_str("");
185 acc->srv_port = 0;
186 } else {
187 pjsip_sip_uri *sip_uri;
188
189 /* Get the SIP URI object: */
190 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
191
192 /* Save the user and domain part. These will be used when finding an
193 * account for incoming requests.
194 */
195 acc->display = name_addr->display;
196 acc->user_part = sip_uri->user;
197 acc->srv_domain = sip_uri->host;
198 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 }
200
201
Benny Prijonob4a17c92006-07-10 14:40:21 +0000202 /* Parse registrar URI, if any */
203 if (acc_cfg->reg_uri.slen) {
204 pjsip_uri *reg_uri;
205
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000206 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000207 acc_cfg->reg_uri.slen, 0);
208 if (reg_uri == NULL) {
209 pjsua_perror(THIS_FILE, "Invalid registrar URI",
210 PJSIP_EINVALIDURI);
211 return PJSIP_EINVALIDURI;
212 }
213
214 /* Registrar URI MUST be a SIP or SIPS: */
215 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
216 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
217 {
218 pjsua_perror(THIS_FILE, "Invalid registar URI",
219 PJSIP_EINVALIDSCHEME);
220 return PJSIP_EINVALIDSCHEME;
221 }
222
223 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
224
225 } else {
226 sip_reg_uri = NULL;
227 }
228
Benny Prijonob4a17c92006-07-10 14:40:21 +0000229 if (sip_reg_uri) {
230 acc->srv_port = sip_reg_uri->port;
231 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000232
233 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000234 //if (acc_cfg->contact.slen == 0) {
235 // acc_cfg->contact = acc_cfg->id;
236 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000238 /* Build account route-set from outbound proxies and route set from
239 * account configuration.
240 */
241 pj_list_init(&acc->route_set);
242
Benny Prijono29c8ca32010-06-22 06:02:13 +0000243 if (!pj_list_empty(&pjsua_var.outbound_proxy)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244 pjsip_route_hdr *r;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000245
Benny Prijono29c8ca32010-06-22 06:02:13 +0000246 r = pjsua_var.outbound_proxy.next;
247 while (r != &pjsua_var.outbound_proxy) {
248 pj_list_push_back(&acc->route_set,
249 pjsip_hdr_shallow_clone(acc->pool, r));
250 r = r->next;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000251 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252 }
253
254 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
255 pj_str_t hname = { "Route", 5};
256 pjsip_route_hdr *r;
257 pj_str_t tmp;
258
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000259 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000260 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000261 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000262 if (r == NULL) {
263 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
264 PJ_EINVAL);
265 return PJ_EINVAL;
266 }
267 pj_list_push_back(&acc->route_set, r);
268 }
269
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270 /* Concatenate credentials from account config and global config */
271 acc->cred_cnt = 0;
272 for (i=0; i<acc_cfg->cred_count; ++i) {
273 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
274 }
275 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
276 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
277 {
278 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
279 }
280
Benny Prijono07fe2302010-06-24 12:33:18 +0000281 /* If ICE is enabled, add "+sip.ice" media feature tag in account's
282 * contact params.
283 */
284#if PJSUA_ADD_ICE_TAGS
285 if (pjsua_var.media_cfg.enable_ice) {
286 unsigned new_len;
287 pj_str_t new_prm;
288
289 new_len = acc_cfg->contact_params.slen + 10;
290 new_prm.ptr = (char*)pj_pool_alloc(acc->pool, new_len);
291 pj_strcpy(&new_prm, &acc_cfg->contact_params);
292 pj_strcat2(&new_prm, ";+sip.ice");
293 acc_cfg->contact_params = new_prm;
294 }
295#endif
296
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000297 status = pjsua_pres_init_acc(acc_id);
298 if (status != PJ_SUCCESS)
299 return status;
300
Benny Prijonob54719f2010-11-16 03:07:46 +0000301 /* If SIP outbound is enabled, generate instance and reg ID if they are
302 * not specified
303 */
304 if (acc_cfg->use_rfc5626) {
305 if (acc_cfg->rfc5626_instance_id.slen==0) {
306 const pj_str_t *hostname;
307 pj_uint32_t hval, pos;
308 char instprm[] = ";+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000CCDDEEFF>\"";
309
310 hostname = pj_gethostname();
311 pos = pj_ansi_strlen(instprm) - 10;
312 hval = pj_hash_calc(0, hostname->ptr, hostname->slen);
313 pj_val_to_hex_digit( ((char*)&hval)[0], instprm+pos+0);
314 pj_val_to_hex_digit( ((char*)&hval)[1], instprm+pos+2);
315 pj_val_to_hex_digit( ((char*)&hval)[2], instprm+pos+4);
316 pj_val_to_hex_digit( ((char*)&hval)[3], instprm+pos+6);
317
318 pj_strdup2(acc->pool, &acc->rfc5626_instprm, instprm);
319 } else {
320 const char *prmname = ";+sip.instance=\"";
321 unsigned len;
322
323 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_instance_id.slen + 1;
324 acc->rfc5626_instprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
325 pj_ansi_snprintf(acc->rfc5626_instprm.ptr, len+1,
326 "%s%.*s\"",
327 prmname,
328 (int)acc_cfg->rfc5626_instance_id.slen,
329 acc_cfg->rfc5626_instance_id.ptr);
330 acc->rfc5626_instprm.slen = len;
331 }
332
333 if (acc_cfg->rfc5626_reg_id.slen==0) {
334 acc->rfc5626_regprm = pj_str(";reg-id=1");
335 } else {
336 const char *prmname = ";reg-id=";
337 unsigned len;
338
339 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_reg_id.slen;
340 acc->rfc5626_regprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
341 pj_ansi_snprintf(acc->rfc5626_regprm.ptr, len+1,
342 "%s%.*s\"",
343 prmname,
344 (int)acc_cfg->rfc5626_reg_id.slen,
345 acc_cfg->rfc5626_reg_id.ptr);
346 acc->rfc5626_regprm.slen = len;
347 }
348 }
349
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000350 /* Mark account as valid */
351 pjsua_var.acc[acc_id].valid = PJ_TRUE;
352
Benny Prijono093d3022006-09-24 00:07:11 +0000353 /* Insert account ID into account ID array, sorted by priority */
354 for (i=0; i<pjsua_var.acc_cnt; ++i) {
355 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
356 pjsua_var.acc[acc_id].cfg.priority)
357 {
358 break;
359 }
360 }
361 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
362 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363
364 return PJ_SUCCESS;
365}
366
367
368/*
369 * Add a new account to pjsua.
370 */
371PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
372 pj_bool_t is_default,
373 pjsua_acc_id *p_acc_id)
374{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000375 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000376 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377 pj_status_t status;
378
379 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
380 PJ_ETOOMANY);
381
382 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000383 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000385 /* Verify media count */
386#if !defined(PJMEDIA_HAS_VIDEO) || (PJMEDIA_HAS_VIDEO == 0)
387 PJ_ASSERT_RETURN(cfg->max_video_cnt == 0, PJ_EINVAL);
388#endif
389 PJ_ASSERT_RETURN(cfg->max_audio_cnt + cfg->max_video_cnt <=
390 PJSUA_MAX_CALL_MEDIA, PJ_ETOOMANY);
391
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392 PJSUA_LOCK();
393
394 /* Find empty account id. */
395 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
396 if (pjsua_var.acc[id].valid == PJ_FALSE)
397 break;
398 }
399
400 /* Expect to find a slot */
401 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
402 {PJSUA_UNLOCK(); return PJ_EBUG;});
403
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000404 acc = &pjsua_var.acc[id];
405
406 /* Create pool for this account. */
407 if (acc->pool)
408 pj_pool_reset(acc->pool);
409 else
410 acc->pool = pjsua_pool_create("acc%p", 512, 256);
411
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000412 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000413 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000414
Sauw Ming9b80d512011-03-15 03:20:37 +0000415 /* Normalize registration timeout and refresh delay */
416 if (pjsua_var.acc[id].cfg.reg_uri.slen) {
417 if (pjsua_var.acc[id].cfg.reg_timeout == 0) {
418 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
419 }
420 if (pjsua_var.acc[id].cfg.reg_delay_before_refresh == 0) {
421 pjsua_var.acc[id].cfg.reg_delay_before_refresh =
422 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
423 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000424 }
425
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000426 /* Get CRC of account proxy setting */
427 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
428
429 /* Get CRC of global outbound proxy setting */
430 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
431 pjsua_var.ua_cfg.outbound_proxy_cnt);
432
Benny Prijono91d06b62008-09-20 12:16:56 +0000433 /* Check the route URI's and force loose route if required */
434 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
435 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
436 if (status != PJ_SUCCESS)
437 return status;
438 }
439
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000440 status = initialize_acc(id);
441 if (status != PJ_SUCCESS) {
442 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000443 pj_pool_release(acc->pool);
444 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 PJSUA_UNLOCK();
446 return status;
447 }
448
449 if (is_default)
450 pjsua_var.default_acc = id;
451
452 if (p_acc_id)
453 *p_acc_id = id;
454
455 pjsua_var.acc_cnt++;
456
457 PJSUA_UNLOCK();
458
459 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
460 (int)cfg->id.slen, cfg->id.ptr, id));
461
462 /* If accounts has registration enabled, start registration */
463 if (pjsua_var.acc[id].cfg.reg_uri.slen)
464 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000465 else {
466 /* Otherwise subscribe to MWI, if it's enabled */
467 if (pjsua_var.acc[id].cfg.mwi_enabled)
468 pjsua_start_mwi(&pjsua_var.acc[id]);
469 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000470
471 return PJ_SUCCESS;
472}
473
474
475/*
476 * Add local account
477 */
478PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
479 pj_bool_t is_default,
480 pjsua_acc_id *p_acc_id)
481{
482 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000483 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000484 const char *beginquote, *endquote;
485 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000486 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487
Benny Prijonoe93e2872006-06-28 16:46:49 +0000488 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000489 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
490 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000491
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000493 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000494
495 pjsua_acc_config_default(&cfg);
496
Benny Prijono093d3022006-09-24 00:07:11 +0000497 /* Lower the priority of local account */
498 --cfg.priority;
499
Benny Prijonod0bd4982007-12-02 15:40:52 +0000500 /* Enclose IPv6 address in square brackets */
501 if (t->type & PJSIP_TRANSPORT_IPV6) {
502 beginquote = "[";
503 endquote = "]";
504 } else {
505 beginquote = endquote = "";
506 }
507
508 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000509 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000510 pj_ansi_snprintf(transport_param, sizeof(transport_param),
511 ";transport=%s",
512 pjsip_transport_get_type_name(t->type));
513 } else {
514 transport_param[0] = '\0';
515 }
516
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000517 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000518 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000519 "<sip:%s%.*s%s:%d%s>",
520 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000521 (int)t->local_name.host.slen,
522 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000523 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000524 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000525 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000526
527 cfg.id = pj_str(uri);
528
529 return pjsua_acc_add(&cfg, is_default, p_acc_id);
530}
531
532
533/*
Benny Prijono705e7842008-07-21 18:12:51 +0000534 * Set arbitrary data to be associated with the account.
535 */
536PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
537 void *user_data)
538{
539 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
540 PJ_EINVAL);
541 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
542
543 PJSUA_LOCK();
544
545 pjsua_var.acc[acc_id].cfg.user_data = user_data;
546
547 PJSUA_UNLOCK();
548
549 return PJ_SUCCESS;
550}
551
552
553/*
554 * Retrieve arbitrary data associated with the account.
555 */
556PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
557{
558 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
559 NULL);
560 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
561
562 return pjsua_var.acc[acc_id].cfg.user_data;
563}
564
565
566/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000567 * Delete account.
568 */
569PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
570{
Benny Prijono093d3022006-09-24 00:07:11 +0000571 unsigned i;
572
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000573 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
574 PJ_EINVAL);
575 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
576
577 PJSUA_LOCK();
578
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000579 /* Cancel any re-registration timer */
580 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
581
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000582 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000583 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000584 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000585 if (pjsua_var.acc[acc_id].regc) {
586 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
587 }
Benny Prijono922933b2007-01-21 16:23:56 +0000588 pjsua_var.acc[acc_id].regc = NULL;
589 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000590
591 /* Delete server presence subscription */
592 pjsua_pres_delete_acc(acc_id);
593
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000594 /* Release account pool */
595 if (pjsua_var.acc[acc_id].pool) {
596 pj_pool_release(pjsua_var.acc[acc_id].pool);
597 pjsua_var.acc[acc_id].pool = NULL;
598 }
599
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000600 /* Invalidate */
601 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000602 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000603
Benny Prijono093d3022006-09-24 00:07:11 +0000604 /* Remove from array */
605 for (i=0; i<pjsua_var.acc_cnt; ++i) {
606 if (pjsua_var.acc_ids[i] == acc_id)
607 break;
608 }
609 if (i != pjsua_var.acc_cnt) {
610 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
611 pjsua_var.acc_cnt, i);
612 --pjsua_var.acc_cnt;
613 }
614
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000615 /* Leave the calls intact, as I don't think calls need to
616 * access account once it's created
617 */
618
Benny Prijonofb2b3652007-06-28 07:15:03 +0000619 /* Update default account */
620 if (pjsua_var.default_acc == acc_id)
621 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000622
623 PJSUA_UNLOCK();
624
625 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
626
627 return PJ_SUCCESS;
628}
629
630
631/*
632 * Modify account information.
633 */
634PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
635 const pjsua_acc_config *cfg)
636{
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000637 pjsua_acc *acc;
638 pjsip_name_addr *id_name_addr = NULL;
639 pjsip_sip_uri *id_sip_uri = NULL;
640 pjsip_sip_uri *reg_sip_uri = NULL;
641 pj_uint32_t local_route_crc, global_route_crc;
642 pjsip_route_hdr global_route;
643 pjsip_route_hdr local_route;
644 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
645 pj_bool_t update_reg = PJ_FALSE;
646 pj_status_t status = PJ_SUCCESS;
647
648 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
649 PJ_EINVAL);
650
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000651 /* Verify media count */
652#if !defined(PJMEDIA_HAS_VIDEO) || (PJMEDIA_HAS_VIDEO == 0)
653 PJ_ASSERT_RETURN(cfg->max_video_cnt == 0, PJ_EINVAL);
654#endif
655 PJ_ASSERT_RETURN(cfg->max_audio_cnt + cfg->max_video_cnt <=
656 PJSUA_MAX_CALL_MEDIA, PJ_ETOOMANY);
657
658
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000659 PJSUA_LOCK();
660
661 acc = &pjsua_var.acc[acc_id];
662 if (!acc->valid) {
663 status = PJ_EINVAL;
664 goto on_return;
665 }
666
667 /* == Validate first == */
668
669 /* Account id */
670 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
671 /* Need to parse id to get the elements: */
672 id_name_addr = (pjsip_name_addr*)
673 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
674 PJSIP_PARSE_URI_AS_NAMEADDR);
675 if (id_name_addr == NULL) {
676 status = PJSIP_EINVALIDURI;
677 pjsua_perror(THIS_FILE, "Invalid local URI", status);
678 goto on_return;
679 }
680
681 /* URI MUST be a SIP or SIPS: */
682 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
683 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
684 {
685 status = PJSIP_EINVALIDSCHEME;
686 pjsua_perror(THIS_FILE, "Invalid local URI", status);
687 goto on_return;
688 }
689
690 /* Get the SIP URI object: */
691 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
692 }
693
694 /* Registrar URI */
695 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
696 pjsip_uri *reg_uri;
697
698 /* Need to parse reg_uri to get the elements: */
699 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
700 cfg->reg_uri.slen, 0);
701 if (reg_uri == NULL) {
702 status = PJSIP_EINVALIDURI;
703 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
704 goto on_return;
705 }
706
707 /* Registrar URI MUST be a SIP or SIPS: */
708 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
709 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
710 {
711 status = PJSIP_EINVALIDSCHEME;
712 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
713 goto on_return;
714 }
715
716 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
717 }
718
719 /* Global outbound proxy */
720 global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
721 pjsua_var.ua_cfg.outbound_proxy_cnt);
722 if (global_route_crc != acc->global_route_crc) {
723 pjsip_route_hdr *r;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000724
Benny Prijono29c8ca32010-06-22 06:02:13 +0000725 /* Copy from global outbound proxies */
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000726 pj_list_init(&global_route);
Benny Prijono29c8ca32010-06-22 06:02:13 +0000727 r = pjsua_var.outbound_proxy.next;
728 while (r != &pjsua_var.outbound_proxy) {
729 pj_list_push_back(&global_route,
730 pjsip_hdr_shallow_clone(acc->pool, r));
731 r = r->next;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000732 }
733 }
734
735 /* Account proxy */
736 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
737 if (local_route_crc != acc->local_route_crc) {
738 pjsip_route_hdr *r;
739 unsigned i;
740
741 /* Validate the local route and save it to temporary var */
742 pj_list_init(&local_route);
743 for (i=0; i<cfg->proxy_cnt; ++i) {
744 pj_str_t hname = { "Route", 5};
745
746 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
747 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
748 if (status != PJ_SUCCESS)
749 goto on_return;
750 r = (pjsip_route_hdr*)
751 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
752 acc_proxy[i].slen, NULL);
753 if (r == NULL) {
754 status = PJSIP_EINVALIDURI;
755 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
756 status);
757 goto on_return;
758 }
759
760 pj_list_push_back(&local_route, r);
761 }
762 }
763
764
765 /* == Apply the new config == */
766
767 /* Account ID. */
768 if (id_name_addr && id_sip_uri) {
769 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
Benny Prijonof0ba2dc2011-04-14 15:38:23 +0000770 pj_strdup_with_null(acc->pool, &acc->display, &id_name_addr->display);
771 pj_strdup_with_null(acc->pool, &acc->user_part, &id_sip_uri->user);
772 pj_strdup_with_null(acc->pool, &acc->srv_domain, &id_sip_uri->host);
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000773 acc->srv_port = 0;
774 update_reg = PJ_TRUE;
775 }
776
777 /* User data */
778 acc->cfg.user_data = cfg->user_data;
779
780 /* Priority */
781 if (acc->cfg.priority != cfg->priority) {
782 unsigned i;
783
784 acc->cfg.priority = cfg->priority;
785
786 /* Resort accounts priority */
787 for (i=0; i<pjsua_var.acc_cnt; ++i) {
788 if (pjsua_var.acc_ids[i] == acc_id)
789 break;
790 }
791 pj_assert(i < pjsua_var.acc_cnt);
792 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
793 pjsua_var.acc_cnt, i);
794 for (i=0; i<pjsua_var.acc_cnt; ++i) {
795 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
796 acc->cfg.priority)
797 {
798 break;
799 }
800 }
801 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
802 pjsua_var.acc_cnt, i, &acc_id);
803 }
804
805 /* MWI */
806 acc->cfg.mwi_enabled = cfg->mwi_enabled;
807
808 /* PIDF tuple ID */
809 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
810 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
811 &cfg->pidf_tuple_id);
812
813 /* Publish */
814 acc->cfg.publish_opt = cfg->publish_opt;
815 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
816 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
817 acc->cfg.publish_enabled = cfg->publish_enabled;
818 if (!acc->cfg.publish_enabled)
819 pjsua_pres_unpublish(acc);
820 else
821 update_reg = PJ_TRUE;
822 }
823
824 /* Force contact URI */
825 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
826 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
827 &cfg->force_contact);
828 update_reg = PJ_TRUE;
829 }
830
831 /* Contact param */
832 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
833 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
834 &cfg->contact_params);
835 update_reg = PJ_TRUE;
836 }
837
838 /* Contact URI params */
839 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
840 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
841 &cfg->contact_uri_params);
842 update_reg = PJ_TRUE;
843 }
844
845 /* Reliable provisional response */
846 acc->cfg.require_100rel = cfg->require_100rel;
847
848 /* Session timer */
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000849 acc->cfg.use_timer = cfg->use_timer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000850 acc->cfg.timer_setting = cfg->timer_setting;
851
852 /* Transport and keep-alive */
853 if (acc->cfg.transport_id != cfg->transport_id) {
854 acc->cfg.transport_id = cfg->transport_id;
855 update_reg = PJ_TRUE;
856 }
857 acc->cfg.ka_interval = cfg->ka_interval;
858 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
859 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
860#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
861 acc->cfg.use_srtp = cfg->use_srtp;
862 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000863 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000864#endif
865
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000866#if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
867 acc->cfg.use_stream_ka = cfg->use_stream_ka;
868#endif
869
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000870 /* Global outbound proxy */
871 if (global_route_crc != acc->global_route_crc) {
872 unsigned i, rcnt;
873
874 /* Remove the outbound proxies from the route set */
875 rcnt = pj_list_size(&acc->route_set);
876 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
877 pjsip_route_hdr *r = acc->route_set.next;
878 pj_list_erase(r);
879 }
880
881 /* Insert the outbound proxies to the beginning of route set */
882 pj_list_merge_first(&acc->route_set, &global_route);
883
884 /* Update global route CRC */
885 acc->global_route_crc = global_route_crc;
886
887 update_reg = PJ_TRUE;
888 }
889
890 /* Account proxy */
891 if (local_route_crc != acc->local_route_crc) {
892 unsigned i;
893
894 /* Remove the current account proxies from the route set */
895 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
896 pjsip_route_hdr *r = acc->route_set.prev;
897 pj_list_erase(r);
898 }
899
900 /* Insert new proxy setting to the route set */
901 pj_list_merge_last(&acc->route_set, &local_route);
902
903 /* Update the proxy setting */
904 acc->cfg.proxy_cnt = cfg->proxy_cnt;
905 for (i = 0; i < cfg->proxy_cnt; ++i)
906 acc->cfg.proxy[i] = acc_proxy[i];
907
908 /* Update local route CRC */
909 acc->local_route_crc = local_route_crc;
910
911 update_reg = PJ_TRUE;
912 }
913
914 /* Credential info */
915 {
916 unsigned i;
917
918 /* Selective update credential info. */
919 for (i = 0; i < cfg->cred_count; ++i) {
920 unsigned j;
921 pjsip_cred_info ci;
922
923 /* Find if this credential is already listed */
Nanang Izzuddin7f8d76f2011-03-29 03:47:16 +0000924 for (j = i; j < acc->cfg.cred_count; ++j) {
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000925 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
926 &cfg->cred_info[i]) == 0)
927 {
928 /* Found, but different index/position, swap */
929 if (j != i) {
930 ci = acc->cfg.cred_info[i];
931 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
932 acc->cfg.cred_info[j] = ci;
933 }
934 break;
935 }
936 }
937
938 /* Not found, insert this */
939 if (j == acc->cfg.cred_count) {
940 /* If account credential is full, discard the last one. */
941 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
942 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
943 acc->cfg.cred_count, acc->cfg.cred_count-1);
944 acc->cfg.cred_count--;
945 }
946
947 /* Insert this */
948 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
949 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
950 acc->cfg.cred_count, i, &ci);
951 }
952 }
953 acc->cfg.cred_count = cfg->cred_count;
954
955 /* Concatenate credentials from account config and global config */
956 acc->cred_cnt = 0;
957 for (i=0; i<acc->cfg.cred_count; ++i) {
958 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
959 }
960 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
961 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
962 {
963 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
964 }
965 }
966
967 /* Authentication preference */
968 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
969 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
970 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
971 &cfg->auth_pref.algorithm);
972
973 /* Registration */
974 acc->cfg.reg_timeout = cfg->reg_timeout;
975 acc->cfg.unreg_timeout = cfg->unreg_timeout;
976 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
977 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
978 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
Sauw Ming8ad06c52011-03-15 10:49:59 +0000979 if (acc->cfg.reg_delay_before_refresh != cfg->reg_delay_before_refresh) {
980 acc->cfg.reg_delay_before_refresh = cfg->reg_delay_before_refresh;
981 pjsip_regc_set_delay_before_refresh(acc->regc,
982 cfg->reg_delay_before_refresh);
983 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000984
Sauw Ming9b80d512011-03-15 03:20:37 +0000985 /* Normalize registration timeout and refresh delay */
986 if (acc->cfg.reg_uri.slen ) {
987 if (acc->cfg.reg_timeout == 0) {
988 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
989 }
990 if (acc->cfg.reg_delay_before_refresh == 0) {
991 acc->cfg.reg_delay_before_refresh =
992 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
993 }
994 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000995
996 /* Registrar URI */
997 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
998 if (cfg->reg_uri.slen) {
999 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
1000 if (reg_sip_uri)
1001 acc->srv_port = reg_sip_uri->port;
1002 } else {
1003 /* Unregister if registration was set */
1004 if (acc->cfg.reg_uri.slen)
1005 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1006 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
1007 }
1008 update_reg = PJ_TRUE;
1009 }
1010
Benny Prijonob54719f2010-11-16 03:07:46 +00001011 /* SIP outbound setting */
1012 if (acc->cfg.use_rfc5626 != cfg->use_rfc5626 ||
1013 pj_strcmp(&acc->cfg.rfc5626_instance_id, &cfg->rfc5626_instance_id) ||
1014 pj_strcmp(&acc->cfg.rfc5626_reg_id, &cfg->rfc5626_reg_id))
1015 {
1016 update_reg = PJ_TRUE;
1017 }
1018
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001019 /* Update registration */
1020 if (update_reg) {
1021 /* If accounts has registration enabled, start registration */
1022 if (acc->cfg.reg_uri.slen)
1023 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1024 else {
1025 /* Otherwise subscribe to MWI, if it's enabled */
1026 if (acc->cfg.mwi_enabled)
1027 pjsua_start_mwi(acc);
1028 }
1029 }
1030
Nanang Izzuddine7acf222011-07-26 08:10:29 +00001031 /* Max number of audio and video stream in a call */
1032 acc->cfg.max_audio_cnt = cfg->max_audio_cnt;
1033 acc->cfg.max_video_cnt = cfg->max_video_cnt;
1034
1035 /* Video settings */
1036 acc->cfg.vid_in_auto_show = cfg->vid_in_auto_show;
1037 acc->cfg.vid_out_auto_transmit = cfg->vid_out_auto_transmit;
1038 acc->cfg.vid_cap_dev = cfg->vid_cap_dev;
1039 acc->cfg.vid_rend_dev = cfg->vid_rend_dev;
1040
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001041on_return:
1042 PJSUA_UNLOCK();
1043 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001044}
1045
1046
1047/*
1048 * Modify account's presence status to be advertised to remote/presence
1049 * subscribers.
1050 */
1051PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
1052 pj_bool_t is_online)
1053{
1054 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1055 PJ_EINVAL);
1056 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1057
1058 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001059 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
1060 pjsua_pres_update_acc(acc_id, PJ_FALSE);
1061 return PJ_SUCCESS;
1062}
1063
1064
1065/*
1066 * Set online status with extended information
1067 */
1068PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
1069 pj_bool_t is_online,
1070 const pjrpid_element *pr)
1071{
1072 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1073 PJ_EINVAL);
1074 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1075
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001076 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +00001077 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001078 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
1079 PJSUA_UNLOCK();
1080
Benny Prijono4461c7d2007-08-25 13:36:15 +00001081 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001082 return PJ_SUCCESS;
1083}
1084
Benny Prijonob54719f2010-11-16 03:07:46 +00001085/* Create reg_contact, mainly for SIP outbound */
1086static void update_regc_contact(pjsua_acc *acc)
1087{
1088 pjsua_acc_config *acc_cfg = &acc->cfg;
1089 pj_bool_t need_outbound = PJ_FALSE;
1090 const pj_str_t tcp_param = pj_str(";transport=tcp");
1091 const pj_str_t tls_param = pj_str(";transport=tls");
1092
1093 if (!acc_cfg->use_rfc5626)
1094 goto done;
1095
Benny Prijono2562b752010-11-16 06:01:38 +00001096 /* Check if outbound has been requested and rejected */
1097 if (acc->rfc5626_status == OUTBOUND_NA)
1098 goto done;
1099
Benny Prijonob54719f2010-11-16 03:07:46 +00001100 if (pj_stristr(&acc->contact, &tcp_param)==NULL &&
1101 pj_stristr(&acc->contact, &tls_param)==NULL)
1102 {
1103 /* Currently we can only do SIP outbound for TCP
1104 * and TLS.
1105 */
1106 goto done;
1107 }
1108
1109 /* looks like we can use outbound */
1110 need_outbound = PJ_TRUE;
1111
1112done:
1113 if (!need_outbound) {
1114 /* Outbound is not needed/wanted for the account. acc->reg_contact
1115 * is set to the same as acc->contact.
1116 */
1117 acc->reg_contact = acc->contact;
Benny Prijono2562b752010-11-16 06:01:38 +00001118 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001119 } else {
1120 /* Need to use outbound, append the contact with +sip.instance and
1121 * reg-id parameters.
1122 */
1123 unsigned len;
1124 pj_str_t reg_contact;
1125
1126 acc->rfc5626_status = OUTBOUND_WANTED;
1127 len = acc->contact.slen + acc->rfc5626_instprm.slen +
1128 acc->rfc5626_regprm.slen;
Benny Prijonoe49e6202010-11-16 07:01:25 +00001129 reg_contact.ptr = (char*) pj_pool_alloc(acc->pool, len);
Benny Prijonob54719f2010-11-16 03:07:46 +00001130
1131 pj_strcpy(&reg_contact, &acc->contact);
1132 pj_strcat(&reg_contact, &acc->rfc5626_regprm);
1133 pj_strcat(&reg_contact, &acc->rfc5626_instprm);
1134
1135 acc->reg_contact = reg_contact;
1136
1137 PJ_LOG(4,(THIS_FILE,
1138 "Contact for acc %d updated for SIP outbound: %.*s",
1139 acc->index,
1140 (int)acc->reg_contact.slen,
1141 acc->reg_contact.ptr));
1142 }
1143}
1144
Benny Prijono7f630432008-09-24 16:52:41 +00001145/* Check if IP is private IP address */
1146static pj_bool_t is_private_ip(const pj_str_t *addr)
1147{
1148 const pj_str_t private_net[] =
1149 {
1150 { "10.", 3 },
1151 { "127.", 4 },
1152 { "172.16.", 7 },
1153 { "192.168.", 8 }
1154 };
1155 unsigned i;
1156
1157 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
1158 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
1159 return PJ_TRUE;
1160 }
1161
1162 return PJ_FALSE;
1163}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001164
Benny Prijono15b02302007-09-27 14:07:07 +00001165/* Update NAT address from the REGISTER response */
1166static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
1167 struct pjsip_regc_cbparam *param)
1168{
1169 pjsip_transport *tp;
1170 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001171 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +00001172 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001173 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +00001174 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +00001175 pj_sockaddr contact_addr;
1176 pj_sockaddr recv_addr;
1177 pj_status_t status;
1178 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +00001179 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001180 pjsip_contact_hdr *contact_hdr;
1181 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +00001182
1183 tp = param->rdata->tp_info.transport;
1184
1185 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001186 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +00001187 return PJ_FALSE;
1188
Benny Prijonob54719f2010-11-16 03:07:46 +00001189 /* If SIP outbound is active, no need to update */
1190 if (acc->rfc5626_status == OUTBOUND_ACTIVE) {
1191 PJ_LOG(4,(THIS_FILE, "Acc %d has SIP outbound active, no need to "
1192 "update registration Contact", acc->index));
1193 return PJ_FALSE;
1194 }
1195
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001196#if 0
1197 // Always update
1198 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +00001199
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001200 /* For UDP, only update if STUN is enabled (for now).
1201 * For TCP/TLS, always check.
1202 */
1203 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
1204 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
1205 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
1206 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
1207 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +00001208 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001209 /* Yes we will check */
1210 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001211 return PJ_FALSE;
1212 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001213#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001214
1215 /* Get the received and rport info */
1216 via = param->rdata->msg_info.via;
1217 if (via->rport_param < 1) {
1218 /* Remote doesn't support rport */
1219 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001220 if (rport==0) {
1221 pjsip_transport_type_e tp_type;
1222 tp_type = (pjsip_transport_type_e) tp->key.type;
1223 rport = pjsip_transport_get_default_port_for_type(tp_type);
1224 }
Benny Prijono15b02302007-09-27 14:07:07 +00001225 } else
1226 rport = via->rport_param;
1227
1228 if (via->recvd_param.slen != 0)
1229 via_addr = &via->recvd_param;
1230 else
1231 via_addr = &via->sent_by.host;
1232
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001233 /* Compare received and rport with the URI in our registration */
1234 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001235 contact_hdr = (pjsip_contact_hdr*)
1236 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1237 acc->contact.slen, NULL);
1238 pj_assert(contact_hdr != NULL);
1239 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001240 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001241 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001242
Benny Prijono24a21852008-04-14 04:04:30 +00001243 if (uri->port == 0) {
1244 pjsip_transport_type_e tp_type;
1245 tp_type = (pjsip_transport_type_e) tp->key.type;
1246 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1247 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001248
Benny Prijono84d24932009-06-04 15:51:39 +00001249 /* Convert IP address strings into sockaddr for comparison.
1250 * (http://trac.pjsip.org/repos/ticket/863)
1251 */
1252 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1253 &contact_addr);
1254 if (status == PJ_SUCCESS)
1255 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1256 &recv_addr);
1257 if (status == PJ_SUCCESS) {
1258 /* Compare the addresses as sockaddr according to the ticket above */
1259 matched = (uri->port == rport &&
1260 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1261 } else {
1262 /* Compare the addresses as string, as before */
1263 matched = (uri->port == rport &&
1264 pj_stricmp(&uri->host, via_addr)==0);
1265 }
1266
1267 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001268 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001269 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001270 return PJ_FALSE;
1271 }
1272
Benny Prijono7f630432008-09-24 16:52:41 +00001273 /* Get server IP */
1274 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1275
Benny Prijono15b02302007-09-27 14:07:07 +00001276 /* At this point we've detected that the address as seen by registrar.
1277 * has changed.
1278 */
Benny Prijono7f630432008-09-24 16:52:41 +00001279
1280 /* Do not switch if both Contact and server's IP address are
1281 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001282 * might have messed up with the SIP packets. See:
1283 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001284 *
1285 * This exception can be disabled by setting allow_contact_rewrite
1286 * to 2. In this case, the switch will always be done whenever there
1287 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001288 */
Benny Prijono247921b2008-09-26 22:06:11 +00001289 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1290 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001291 {
1292 /* Don't switch */
1293 pj_pool_release(pool);
1294 return PJ_FALSE;
1295 }
1296
Benny Prijono4f933762009-11-10 03:45:42 +00001297 /* Also don't switch if only the port number part is different, and
1298 * the Via received address is private.
1299 * See http://trac.pjsip.org/repos/ticket/864
1300 */
1301 if (acc->cfg.allow_contact_rewrite != 2 &&
1302 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1303 is_private_ip(via_addr))
1304 {
1305 /* Don't switch */
1306 pj_pool_release(pool);
1307 return PJ_FALSE;
1308 }
1309
Benny Prijono15b02302007-09-27 14:07:07 +00001310 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001311 "(%.*s:%d --> %.*s:%d). Updating registration "
1312 "(using method %d)",
Benny Prijono15b02302007-09-27 14:07:07 +00001313 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001314 (int)uri->host.slen,
1315 uri->host.ptr,
1316 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001317 (int)via_addr->slen,
1318 via_addr->ptr,
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001319 rport,
1320 acc->cfg.contact_rewrite_method));
Benny Prijono15b02302007-09-27 14:07:07 +00001321
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001322 pj_assert(acc->cfg.contact_rewrite_method == 1 ||
1323 acc->cfg.contact_rewrite_method == 2);
1324
1325 if (acc->cfg.contact_rewrite_method == 1) {
1326 /* Unregister current contact */
1327 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1328 if (acc->regc != NULL) {
1329 pjsip_regc_destroy(acc->regc);
1330 acc->regc = NULL;
1331 acc->contact.slen = 0;
1332 }
Benny Prijono15b02302007-09-27 14:07:07 +00001333 }
1334
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001335 /*
1336 * Build new Contact header
1337 */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001338 {
Benny Prijonob54719f2010-11-16 03:07:46 +00001339 const char *ob = ";ob";
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001340 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001341 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001342 int len;
1343
Benny Prijono8972bf02009-04-05 18:30:45 +00001344 /* Enclose IPv6 address in square brackets */
1345 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1346 beginquote = "[";
1347 endquote = "]";
1348 } else {
1349 beginquote = endquote = "";
1350 }
1351
Benny Prijono24a21852008-04-14 04:04:30 +00001352 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001353 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00001354 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s%s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001355 (int)acc->user_part.slen,
1356 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001357 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001358 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001359 (int)via_addr->slen,
1360 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001361 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001362 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001363 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001364 (int)acc->cfg.contact_uri_params.slen,
1365 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00001366 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00001367 (int)acc->cfg.contact_params.slen,
1368 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001369 if (len < 1) {
1370 PJ_LOG(1,(THIS_FILE, "URI too long"));
1371 pj_pool_release(pool);
1372 return PJ_FALSE;
1373 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001374 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001375
Benny Prijonob54719f2010-11-16 03:07:46 +00001376 update_regc_contact(acc);
1377
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001378 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1379 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1380 tp->local_name.port = rport;
1381
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001382 }
1383
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001384 if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001385 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001386 }
Benny Prijono15b02302007-09-27 14:07:07 +00001387
1388 /* Perform new registration */
1389 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1390
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001391 pj_pool_release(pool);
1392
Benny Prijono15b02302007-09-27 14:07:07 +00001393 return PJ_TRUE;
1394}
1395
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001396/* Check and update Service-Route header */
1397void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1398{
1399 pjsip_generic_string_hdr *hsr = NULL;
1400 pjsip_route_hdr *hr, *h;
1401 const pj_str_t HNAME = { "Service-Route", 13 };
1402 const pj_str_t HROUTE = { "Route", 5 };
1403 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001404 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001405
1406 /* Find and parse Service-Route headers */
1407 for (;;) {
1408 char saved;
1409 int parsed_len;
1410
1411 /* Find Service-Route header */
1412 hsr = (pjsip_generic_string_hdr*)
1413 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1414 if (!hsr)
1415 break;
1416
1417 /* Parse as Route header since the syntax is similar. This may
1418 * return more than one headers.
1419 */
1420 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1421 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1422 hr = (pjsip_route_hdr*)
1423 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1424 hsr->hvalue.slen, &parsed_len);
1425 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1426
1427 if (hr == NULL) {
1428 /* Error */
1429 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1430 return;
1431 }
1432
1433 /* Save each URI in the result */
1434 h = hr;
1435 do {
1436 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1437 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1438 {
1439 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1440 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1441 return;
1442 }
1443
1444 uri[uri_cnt++] = h->name_addr.uri;
1445 h = h->next;
1446 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1447
1448 if (h != hr) {
1449 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1450 return;
1451 }
1452
1453 /* Prepare to find next Service-Route header */
1454 hsr = hsr->next;
1455 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1456 break;
1457 }
1458
1459 if (uri_cnt == 0)
1460 return;
1461
1462 /*
1463 * Update account's route set
1464 */
1465
1466 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001467 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001468 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1469 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1470 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001471 i<rcnt;
1472 ++i)
1473 {
1474 pjsip_route_hdr *prev = hr->prev;
1475 pj_list_erase(hr);
1476 hr = prev;
1477 }
1478 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001479
1480 /* Then append the Service-Route URIs */
1481 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001482 hr = pjsip_route_hdr_create(acc->pool);
1483 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001484 pj_list_push_back(&acc->route_set, hr);
1485 }
1486
1487 /* Done */
1488
1489 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1490 acc->index, uri_cnt));
1491}
1492
Benny Prijonobddef2c2007-10-31 13:28:08 +00001493
1494/* Keep alive timer callback */
1495static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1496{
1497 pjsua_acc *acc;
1498 pjsip_tpselector tp_sel;
1499 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001500 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001501 pj_status_t status;
1502
1503 PJ_UNUSED_ARG(th);
1504
1505 PJSUA_LOCK();
1506
1507 te->id = PJ_FALSE;
1508
1509 acc = (pjsua_acc*) te->user_data;
1510
1511 /* Select the transport to send the packet */
1512 pj_bzero(&tp_sel, sizeof(tp_sel));
1513 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1514 tp_sel.u.transport = acc->ka_transport;
1515
1516 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001517 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001518 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001519 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001520
1521 /* Send raw packet */
1522 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1523 PJSIP_TRANSPORT_UDP, &tp_sel,
1524 NULL, acc->cfg.ka_data.ptr,
1525 acc->cfg.ka_data.slen,
1526 &acc->ka_target, acc->ka_target_len,
1527 NULL, NULL);
1528
1529 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1530 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1531 }
1532
1533 /* Reschedule next timer */
1534 delay.sec = acc->cfg.ka_interval;
1535 delay.msec = 0;
1536 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1537 if (status == PJ_SUCCESS) {
1538 te->id = PJ_TRUE;
1539 } else {
1540 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1541 }
1542
1543 PJSUA_UNLOCK();
1544}
1545
1546
1547/* Update keep-alive for the account */
1548static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1549 struct pjsip_regc_cbparam *param)
1550{
1551 /* In all cases, stop keep-alive timer if it's running. */
1552 if (acc->ka_timer.id) {
1553 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1554 acc->ka_timer.id = PJ_FALSE;
1555
1556 pjsip_transport_dec_ref(acc->ka_transport);
1557 acc->ka_transport = NULL;
1558 }
1559
1560 if (start) {
1561 pj_time_val delay;
1562 pj_status_t status;
1563
1564 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001565 * - ka_interval is not zero in the account, and
1566 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001567 *
1568 * Previously we only enabled keep-alive when STUN is enabled, since
1569 * we thought that keep-alive is only needed in Internet situation.
1570 * But it has been discovered that Windows Firewall on WinXP also
1571 * needs to be kept-alive, otherwise incoming packets will be dropped.
1572 * So because of this, now keep-alive is always enabled for UDP,
1573 * regardless of whether STUN is enabled or not.
1574 *
1575 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1576 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001577 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001578 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001579 acc->cfg.ka_interval == 0 ||
1580 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1581 {
1582 /* Keep alive is not necessary */
1583 return;
1584 }
1585
1586 /* Save transport and destination address. */
1587 acc->ka_transport = param->rdata->tp_info.transport;
1588 pjsip_transport_add_ref(acc->ka_transport);
1589 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1590 param->rdata->pkt_info.src_addr_len);
1591 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1592
1593 /* Setup and start the timer */
1594 acc->ka_timer.cb = &keep_alive_timer_cb;
1595 acc->ka_timer.user_data = (void*)acc;
1596
1597 delay.sec = acc->cfg.ka_interval;
1598 delay.msec = 0;
1599 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1600 &delay);
1601 if (status == PJ_SUCCESS) {
1602 acc->ka_timer.id = PJ_TRUE;
1603 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1604 "destination:%s:%d, interval:%ds",
1605 acc->index,
1606 param->rdata->pkt_info.src_name,
1607 param->rdata->pkt_info.src_port,
1608 acc->cfg.ka_interval));
1609 } else {
1610 acc->ka_timer.id = PJ_FALSE;
1611 pjsip_transport_dec_ref(acc->ka_transport);
1612 acc->ka_transport = NULL;
1613 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1614 }
1615 }
1616}
1617
1618
Benny Prijonob54719f2010-11-16 03:07:46 +00001619/* Update the status of SIP outbound registration request */
1620static void update_rfc5626_status(pjsua_acc *acc, pjsip_rx_data *rdata)
1621{
1622 pjsip_require_hdr *hreq;
1623 const pj_str_t STR_OUTBOUND = {"outbound", 8};
1624 unsigned i;
1625
Benny Prijono2562b752010-11-16 06:01:38 +00001626 if (acc->rfc5626_status == OUTBOUND_UNKNOWN) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001627 goto on_return;
1628 }
1629
1630 hreq = rdata->msg_info.require;
1631 if (!hreq) {
Benny Prijono2562b752010-11-16 06:01:38 +00001632 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001633 goto on_return;
1634 }
1635
1636 for (i=0; i<hreq->count; ++i) {
1637 if (pj_stricmp(&hreq->values[i], &STR_OUTBOUND)==0) {
1638 acc->rfc5626_status = OUTBOUND_ACTIVE;
1639 goto on_return;
1640 }
1641 }
1642
1643 /* Server does not support outbound */
Benny Prijono2562b752010-11-16 06:01:38 +00001644 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001645
1646on_return:
Benny Prijono2562b752010-11-16 06:01:38 +00001647 if (acc->rfc5626_status != OUTBOUND_ACTIVE) {
1648 acc->reg_contact = acc->contact;
1649 }
Benny Prijonob54719f2010-11-16 03:07:46 +00001650 PJ_LOG(4,(THIS_FILE, "SIP outbound status for acc %d is %s",
1651 acc->index, (acc->rfc5626_status==OUTBOUND_ACTIVE?
1652 "active": "not active")));
1653}
1654
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001655/*
1656 * This callback is called by pjsip_regc when outgoing register
1657 * request has completed.
1658 */
1659static void regc_cb(struct pjsip_regc_cbparam *param)
1660{
1661
Benny Prijonoa1e69682007-05-11 15:14:34 +00001662 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001663
Benny Prijono15b02302007-09-27 14:07:07 +00001664 if (param->regc != acc->regc)
1665 return;
1666
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001667 PJSUA_LOCK();
1668
1669 /*
1670 * Print registration status.
1671 */
1672 if (param->status!=PJ_SUCCESS) {
1673 pjsua_perror(THIS_FILE, "SIP registration error",
1674 param->status);
1675 pjsip_regc_destroy(acc->regc);
1676 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001677 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001678
Benny Prijonobddef2c2007-10-31 13:28:08 +00001679 /* Stop keep-alive timer if any. */
1680 update_keep_alive(acc, PJ_FALSE, NULL);
1681
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001682 } else if (param->code < 0 || param->code >= 300) {
1683 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1684 param->code,
1685 (int)param->reason.slen, param->reason.ptr));
1686 pjsip_regc_destroy(acc->regc);
1687 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001688 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001689
Benny Prijonobddef2c2007-10-31 13:28:08 +00001690 /* Stop keep-alive timer if any. */
1691 update_keep_alive(acc, PJ_FALSE, NULL);
1692
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001693 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1694
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001695 /* Update auto registration flag */
1696 acc->auto_rereg.active = PJ_FALSE;
1697 acc->auto_rereg.attempt_cnt = 0;
1698
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001699 if (param->expiration < 1) {
1700 pjsip_regc_destroy(acc->regc);
1701 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001702 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001703
1704 /* Stop keep-alive timer if any. */
1705 update_keep_alive(acc, PJ_FALSE, NULL);
1706
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001707 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1708 pjsua_var.acc[acc->index].cfg.id.ptr));
1709 } else {
Benny Prijonob54719f2010-11-16 03:07:46 +00001710 /* Check and update SIP outbound status first, since the result
1711 * will determine if we should update re-registration
1712 */
1713 update_rfc5626_status(acc, param->rdata);
1714
Benny Prijono15b02302007-09-27 14:07:07 +00001715 /* Check NAT bound address */
1716 if (acc_check_nat_addr(acc, param)) {
Benny Prijono15b02302007-09-27 14:07:07 +00001717 PJSUA_UNLOCK();
1718 return;
1719 }
1720
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001721 /* Check and update Service-Route header */
1722 update_service_route(acc, param->rdata);
1723
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001724 PJ_LOG(3, (THIS_FILE,
1725 "%s: registration success, status=%d (%.*s), "
1726 "will re-register in %d seconds",
1727 pjsua_var.acc[acc->index].cfg.id.ptr,
1728 param->code,
1729 (int)param->reason.slen, param->reason.ptr,
1730 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001731
Benny Prijonobddef2c2007-10-31 13:28:08 +00001732 /* Start keep-alive timer if necessary. */
1733 update_keep_alive(acc, PJ_TRUE, param);
1734
Benny Prijono8b6834f2007-02-24 13:29:22 +00001735 /* Send initial PUBLISH if it is enabled */
1736 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1737 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001738
1739 /* Subscribe to MWI, if it's enabled */
1740 if (acc->cfg.mwi_enabled)
1741 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001742 }
1743
1744 } else {
1745 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1746 }
1747
1748 acc->reg_last_err = param->status;
1749 acc->reg_last_code = param->code;
1750
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001751 /* Check if we need to auto retry registration. Basically, registration
1752 * failure codes triggering auto-retry are those of temporal failures
1753 * considered to be recoverable in relatively short term.
1754 */
1755 if (acc->cfg.reg_retry_interval &&
1756 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1757 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1758 param->code == PJSIP_SC_BAD_GATEWAY ||
1759 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1760 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1761 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1762 {
1763 schedule_reregistration(acc);
1764 }
1765
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001766 /* Call the registration status callback */
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00001767
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001768 if (pjsua_var.ua_cfg.cb.on_reg_state) {
1769 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1770 }
1771
1772 if (pjsua_var.ua_cfg.cb.on_reg_state2) {
1773 pjsua_reg_info reg_info;
1774
1775 reg_info.cbparam = param;
1776 (*pjsua_var.ua_cfg.cb.on_reg_state2)(acc->index, &reg_info);
1777 }
1778
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001779 PJSUA_UNLOCK();
1780}
1781
1782
1783/*
1784 * Initialize client registration.
1785 */
1786static pj_status_t pjsua_regc_init(int acc_id)
1787{
1788 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001789 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001790 pj_status_t status;
1791
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001792 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001793 acc = &pjsua_var.acc[acc_id];
1794
1795 if (acc->cfg.reg_uri.slen == 0) {
1796 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1797 return PJ_SUCCESS;
1798 }
1799
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001800 /* Destroy existing session, if any */
1801 if (acc->regc) {
1802 pjsip_regc_destroy(acc->regc);
1803 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001804 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001805 }
1806
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001807 /* initialize SIP registration if registrar is configured */
1808
1809 status = pjsip_regc_create( pjsua_var.endpt,
1810 acc, &regc_cb, &acc->regc);
1811
1812 if (status != PJ_SUCCESS) {
1813 pjsua_perror(THIS_FILE, "Unable to create client registration",
1814 status);
1815 return status;
1816 }
1817
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001818 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001819
1820 if (acc->contact.slen == 0) {
1821 pj_str_t tmp_contact;
1822
1823 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1824 acc_id, &acc->cfg.reg_uri);
1825 if (status != PJ_SUCCESS) {
1826 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1827 " for registration",
1828 status);
1829 pjsip_regc_destroy(acc->regc);
1830 pj_pool_release(pool);
1831 acc->regc = NULL;
1832 return status;
1833 }
1834
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001835 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonob54719f2010-11-16 03:07:46 +00001836 update_regc_contact(acc);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001837 }
1838
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001839 status = pjsip_regc_init( acc->regc,
1840 &acc->cfg.reg_uri,
1841 &acc->cfg.id,
1842 &acc->cfg.id,
Benny Prijonob54719f2010-11-16 03:07:46 +00001843 1, &acc->reg_contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001844 acc->cfg.reg_timeout);
1845 if (status != PJ_SUCCESS) {
1846 pjsua_perror(THIS_FILE,
1847 "Client registration initialization error",
1848 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001849 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001850 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001851 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001852 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001853 return status;
1854 }
1855
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001856 /* If account is locked to specific transport, then set transport to
1857 * the client registration.
1858 */
1859 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1860 pjsip_tpselector tp_sel;
1861
1862 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1863 pjsip_regc_set_transport(acc->regc, &tp_sel);
1864 }
1865
1866
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001867 /* Set credentials
1868 */
1869 if (acc->cred_cnt) {
1870 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1871 }
1872
Sauw Ming9b80d512011-03-15 03:20:37 +00001873 /* Set delay before registration refresh */
1874 pjsip_regc_set_delay_before_refresh(acc->regc,
1875 acc->cfg.reg_delay_before_refresh);
1876
Benny Prijono48ab2b72007-11-08 09:24:30 +00001877 /* Set authentication preference */
1878 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1879
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001880 /* Set route-set
1881 */
Benny Prijono29c8ca32010-06-22 06:02:13 +00001882 if (acc->cfg.reg_use_proxy) {
1883 pjsip_route_hdr route_set;
1884 const pjsip_route_hdr *r;
1885
1886 pj_list_init(&route_set);
1887
1888 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) {
1889 r = pjsua_var.outbound_proxy.next;
1890 while (r != &pjsua_var.outbound_proxy) {
1891 pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r));
1892 r = r->next;
1893 }
1894 }
1895
1896 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY &&
1897 acc->cfg.proxy_cnt)
1898 {
1899 int cnt = acc->cfg.proxy_cnt;
1900 pjsip_route_hdr *pos = route_set.prev;
1901 int i;
1902
1903 r = acc->route_set.prev;
1904 for (i=0; i<cnt; ++i) {
1905 pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r));
1906 r = r->prev;
1907 }
1908 }
1909
1910 if (!pj_list_empty(&route_set))
1911 pjsip_regc_set_route_set( acc->regc, &route_set );
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001912 }
1913
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +00001914 /* Add custom request headers specified in the account config */
1915 pjsip_regc_add_headers(acc->regc, &acc->cfg.reg_hdr_list);
1916
Benny Prijono8fc6de02006-11-11 21:25:55 +00001917 /* Add other request headers. */
1918 if (pjsua_var.ua_cfg.user_agent.slen) {
1919 pjsip_hdr hdr_list;
1920 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1921 pjsip_generic_string_hdr *h;
1922
Benny Prijono8fc6de02006-11-11 21:25:55 +00001923 pj_list_init(&hdr_list);
1924
1925 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1926 &pjsua_var.ua_cfg.user_agent);
1927 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1928
1929 pjsip_regc_add_headers(acc->regc, &hdr_list);
1930 }
1931
Benny Prijonob54719f2010-11-16 03:07:46 +00001932 /* If SIP outbound is used, add "Supported: outbound, path header" */
1933 if (acc->rfc5626_status == OUTBOUND_WANTED) {
1934 pjsip_hdr hdr_list;
1935 pjsip_supported_hdr *hsup;
1936
1937 pj_list_init(&hdr_list);
1938 hsup = pjsip_supported_hdr_create(pool);
1939 pj_list_push_back(&hdr_list, hsup);
1940
1941 hsup->count = 2;
1942 hsup->values[0] = pj_str("outbound");
1943 hsup->values[1] = pj_str("path");
1944
1945 pjsip_regc_add_headers(acc->regc, &hdr_list);
1946 }
1947
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001948 pj_pool_release(pool);
1949
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001950 return PJ_SUCCESS;
1951}
1952
1953
1954/*
1955 * Update registration or perform unregistration.
1956 */
1957PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1958 pj_bool_t renew)
1959{
1960 pj_status_t status = 0;
1961 pjsip_tx_data *tdata = 0;
1962
1963 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1964 PJ_EINVAL);
1965 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1966
1967 PJSUA_LOCK();
1968
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001969 /* Cancel any re-registration timer */
1970 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1971
1972 /* Reset pointer to registration transport */
1973 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1974
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001975 if (renew) {
1976 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001977 status = pjsua_regc_init(acc_id);
1978 if (status != PJ_SUCCESS) {
1979 pjsua_perror(THIS_FILE, "Unable to create registration",
1980 status);
1981 goto on_return;
1982 }
1983 }
1984 if (!pjsua_var.acc[acc_id].regc) {
1985 status = PJ_EINVALIDOP;
1986 goto on_return;
1987 }
1988
1989 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1990 &tdata);
1991
Benny Prijono28f673a2007-10-15 07:04:59 +00001992 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1993 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1994 pjsip_authorization_hdr *h;
1995 char *uri;
1996 int d;
1997
1998 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1999 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
2000 uri, acc->cfg.reg_uri.slen+10);
2001 pj_assert(d > 0);
2002
2003 h = pjsip_authorization_hdr_create(tdata->pool);
2004 h->scheme = pj_str("Digest");
2005 h->credential.digest.username = acc->cred[0].username;
2006 h->credential.digest.realm = acc->srv_domain;
2007 h->credential.digest.uri = pj_str(uri);
2008 h->credential.digest.algorithm = pj_str("md5");
2009
2010 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
2011 }
2012
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002013 } else {
2014 if (pjsua_var.acc[acc_id].regc == NULL) {
2015 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
2016 status = PJ_EINVALIDOP;
2017 goto on_return;
2018 }
Benny Prijono166d5022010-02-10 14:24:48 +00002019
2020 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
2021
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002022 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
2023 }
2024
Benny Prijono56315612006-07-18 14:39:40 +00002025 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00002026 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002027 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00002028 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002029
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002030 /* Update pointer to registration transport */
2031 if (status == PJ_SUCCESS) {
2032 pjsip_regc_info reg_info;
2033
2034 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
2035 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
2036 }
2037
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002038 if (status != PJ_SUCCESS) {
2039 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
2040 status);
2041 } else {
2042 PJ_LOG(3,(THIS_FILE, "%s sent",
2043 (renew? "Registration" : "Unregistration")));
2044 }
2045
2046on_return:
2047 PJSUA_UNLOCK();
2048 return status;
2049}
2050
2051
2052/*
2053 * Get account information.
2054 */
2055PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
2056 pjsua_acc_info *info)
2057{
2058 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2059 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
2060
2061 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002062 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002063
Benny Prijonoac623b32006-07-03 15:19:31 +00002064 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002065
2066 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
2067 PJ_EINVAL);
2068 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
2069
2070 PJSUA_LOCK();
2071
2072 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
2073 PJSUA_UNLOCK();
2074 return PJ_EINVALIDOP;
2075 }
2076
2077 info->id = acc_id;
2078 info->is_default = (pjsua_var.default_acc == acc_id);
2079 info->acc_uri = acc_cfg->id;
2080 info->has_registration = (acc->cfg.reg_uri.slen > 0);
2081 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00002082 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
2083 if (info->rpid.note.slen)
2084 info->online_status_text = info->rpid.note;
2085 else if (info->online_status)
2086 info->online_status_text = pj_str("Online");
2087 else
2088 info->online_status_text = pj_str("Offline");
2089
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002090 if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00002091 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002092 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002093 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002094 if (acc->reg_last_err)
2095 info->reg_last_err = acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002096 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002097 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002098 info->status_text = pj_str("not registered");
2099 }
2100 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002101 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002102 info->status_text = pj_str("In Progress");
2103 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002104 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002105 info->status_text = pj_str("does not register");
2106 }
2107
2108 if (acc->regc) {
2109 pjsip_regc_info regc_info;
2110 pjsip_regc_get_info(acc->regc, &regc_info);
2111 info->expires = regc_info.next_reg;
2112 } else {
2113 info->expires = -1;
2114 }
2115
2116 PJSUA_UNLOCK();
2117
2118 return PJ_SUCCESS;
2119
2120}
2121
2122
2123/*
2124 * Enum accounts all account ids.
2125 */
2126PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
2127 unsigned *count )
2128{
2129 unsigned i, c;
2130
2131 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
2132
2133 PJSUA_LOCK();
2134
2135 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2136 if (!pjsua_var.acc[i].valid)
2137 continue;
2138 ids[c] = i;
2139 ++c;
2140 }
2141
2142 *count = c;
2143
2144 PJSUA_UNLOCK();
2145
2146 return PJ_SUCCESS;
2147}
2148
2149
2150/*
2151 * Enum accounts info.
2152 */
2153PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
2154 unsigned *count )
2155{
2156 unsigned i, c;
2157
2158 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
2159
2160 PJSUA_LOCK();
2161
2162 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2163 if (!pjsua_var.acc[i].valid)
2164 continue;
2165
2166 pjsua_acc_get_info(i, &info[c]);
2167 ++c;
2168 }
2169
2170 *count = c;
2171
2172 PJSUA_UNLOCK();
2173
2174 return PJ_SUCCESS;
2175}
2176
2177
2178/*
2179 * This is an internal function to find the most appropriate account to
2180 * used to reach to the specified URL.
2181 */
2182PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
2183{
2184 pj_str_t tmp;
2185 pjsip_uri *uri;
2186 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002187 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00002188 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002189
2190 PJSUA_LOCK();
2191
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002192 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002193
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002194 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002195
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002196 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002197 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002198 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002199 PJSUA_UNLOCK();
2200 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002201 }
2202
2203 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2204 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2205 {
2206 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00002207 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2208 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002209 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00002210 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002211 break;
2212 }
2213
Benny Prijono093d3022006-09-24 00:07:11 +00002214 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002215 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002216 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002217 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002218 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002219 }
2220
2221 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002222 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002223 PJSUA_UNLOCK();
2224 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002225 }
2226
Benny Prijonoa1e69682007-05-11 15:14:34 +00002227 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002228
Benny Prijonob4a17c92006-07-10 14:40:21 +00002229 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00002230 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2231 unsigned acc_id = pjsua_var.acc_ids[i];
2232 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
2233 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
2234 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002235 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002236 PJSUA_UNLOCK();
2237 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00002238 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002239 }
2240
Benny Prijonob4a17c92006-07-10 14:40:21 +00002241 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00002242 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2243 unsigned acc_id = pjsua_var.acc_ids[i];
2244 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
2245 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002246 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002247 PJSUA_UNLOCK();
2248 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00002249 }
2250 }
2251
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002252
Benny Prijono093d3022006-09-24 00:07:11 +00002253 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002254 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002255 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00002256 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002257}
2258
2259
2260/*
2261 * This is an internal function to find the most appropriate account to be
2262 * used to handle incoming calls.
2263 */
2264PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
2265{
2266 pjsip_uri *uri;
2267 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00002268 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002269
Benny Prijono371cf0a2007-06-19 00:35:37 +00002270 /* Check that there's at least one account configured */
2271 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
2272
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002273 uri = rdata->msg_info.to->uri;
2274
2275 /* Just return default account if To URI is not SIP: */
2276 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2277 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2278 {
2279 return pjsua_var.default_acc;
2280 }
2281
2282
2283 PJSUA_LOCK();
2284
2285 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
2286
2287 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00002288 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2289 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002290 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2291
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002292 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00002293 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002294 {
2295 /* Match ! */
2296 PJSUA_UNLOCK();
2297 return acc_id;
2298 }
2299 }
2300
Benny Prijono093d3022006-09-24 00:07:11 +00002301 /* No matching account, try match domain part only. */
2302 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2303 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002304 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2305
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002306 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002307 /* Match ! */
2308 PJSUA_UNLOCK();
2309 return acc_id;
2310 }
2311 }
2312
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002313 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00002314 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2315 unsigned acc_id = pjsua_var.acc_ids[i];
2316 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2317
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002318 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
2319
2320 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2321 pjsip_transport_type_e type;
2322 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2323 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
2324 type = PJSIP_TRANSPORT_UDP;
2325
2326 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
2327 continue;
2328 }
2329
Benny Prijono093d3022006-09-24 00:07:11 +00002330 /* Match ! */
2331 PJSUA_UNLOCK();
2332 return acc_id;
2333 }
2334 }
2335
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002336 /* Still no match, use default account */
2337 PJSUA_UNLOCK();
2338 return pjsua_var.default_acc;
2339}
2340
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002341
Benny Prijonofff245c2007-04-02 11:44:47 +00002342/*
2343 * Create arbitrary requests for this account.
2344 */
2345PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2346 const pjsip_method *method,
2347 const pj_str_t *target,
2348 pjsip_tx_data **p_tdata)
2349{
2350 pjsip_tx_data *tdata;
2351 pjsua_acc *acc;
2352 pjsip_route_hdr *r;
2353 pj_status_t status;
2354
2355 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2356 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2357
2358 acc = &pjsua_var.acc[acc_id];
2359
2360 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2361 &acc->cfg.id, target,
2362 NULL, NULL, -1, NULL, &tdata);
2363 if (status != PJ_SUCCESS) {
2364 pjsua_perror(THIS_FILE, "Unable to create request", status);
2365 return status;
2366 }
2367
2368 /* Copy routeset */
2369 r = acc->route_set.next;
2370 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002371 pjsip_msg_add_hdr(tdata->msg,
2372 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002373 r = r->next;
2374 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002375
2376 /* If account is locked to specific transport, then set that transport to
2377 * the transmit data.
2378 */
2379 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2380 pjsip_tpselector tp_sel;
2381
2382 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2383 pjsip_tx_data_set_transport(tdata, &tp_sel);
2384 }
2385
Benny Prijonofff245c2007-04-02 11:44:47 +00002386 /* Done */
2387 *p_tdata = tdata;
2388 return PJ_SUCCESS;
2389}
2390
2391
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002392PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2393 pj_str_t *contact,
2394 pjsua_acc_id acc_id,
2395 const pj_str_t *suri)
2396{
2397 pjsua_acc *acc;
2398 pjsip_sip_uri *sip_uri;
2399 pj_status_t status;
2400 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2401 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002402 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002403 unsigned flag;
2404 int secure;
2405 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002406 const char *beginquote, *endquote;
2407 char transport_param[32];
Benny Prijonob54719f2010-11-16 03:07:46 +00002408 const char *ob = ";ob";
Benny Prijonod0bd4982007-12-02 15:40:52 +00002409
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002410
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002411 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002412 acc = &pjsua_var.acc[acc_id];
2413
Benny Prijonof75eceb2007-03-23 19:09:54 +00002414 /* If force_contact is configured, then use use it */
2415 if (acc->cfg.force_contact.slen) {
2416 *contact = acc->cfg.force_contact;
2417 return PJ_SUCCESS;
2418 }
2419
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002420 /* If route-set is configured for the account, then URI is the
2421 * first entry of the route-set.
2422 */
2423 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002424 sip_uri = (pjsip_sip_uri*)
2425 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002426 } else {
2427 pj_str_t tmp;
2428 pjsip_uri *uri;
2429
2430 pj_strdup_with_null(pool, &tmp, suri);
2431
2432 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2433 if (uri == NULL)
2434 return PJSIP_EINVALIDURI;
2435
2436 /* For non-SIP scheme, route set should be configured */
2437 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
Benny Prijonoc7545782010-09-28 07:43:18 +00002438 return PJSIP_ENOROUTESET;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002439
Benny Prijono8c7a6172007-02-18 21:17:46 +00002440 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002441 }
2442
2443 /* Get transport type of the URI */
2444 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2445 tp_type = PJSIP_TRANSPORT_TLS;
2446 else if (sip_uri->transport_param.slen == 0) {
2447 tp_type = PJSIP_TRANSPORT_UDP;
2448 } else
2449 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2450
2451 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2452 return PJSIP_EUNSUPTRANSPORT;
2453
Benny Prijonod0bd4982007-12-02 15:40:52 +00002454 /* If destination URI specifies IPv6, then set transport type
2455 * to use IPv6 as well.
2456 */
Benny Prijono19b29372007-12-05 04:08:40 +00002457 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002458 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2459
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002460 flag = pjsip_transport_get_flag_from_type(tp_type);
2461 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2462
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002463 /* Init transport selector. */
2464 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2465
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002466 /* Get local address suitable to send request from */
2467 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002468 pool, tp_type, &tp_sel,
2469 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002470 if (status != PJ_SUCCESS)
2471 return status;
2472
Benny Prijonod0bd4982007-12-02 15:40:52 +00002473 /* Enclose IPv6 address in square brackets */
2474 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2475 beginquote = "[";
2476 endquote = "]";
2477 } else {
2478 beginquote = endquote = "";
2479 }
2480
2481 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002482 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002483 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2484 ";transport=%s",
2485 pjsip_transport_get_type_name(tp_type));
2486 } else {
2487 transport_param[0] = '\0';
2488 }
2489
2490
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002491 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002492 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002493 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00002494 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s%s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002495 (int)acc->display.slen,
2496 acc->display.ptr,
2497 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002498 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002499 (int)acc->user_part.slen,
2500 acc->user_part.ptr,
2501 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002502 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002503 (int)local_addr.slen,
2504 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002505 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002506 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002507 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002508 (int)acc->cfg.contact_uri_params.slen,
2509 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00002510 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00002511 (int)acc->cfg.contact_params.slen,
2512 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002513
2514 return PJ_SUCCESS;
2515}
2516
2517
2518
2519PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2520 pj_str_t *contact,
2521 pjsua_acc_id acc_id,
2522 pjsip_rx_data *rdata )
2523{
2524 /*
2525 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2526 * If the request that initiated the dialog contained a SIPS URI
2527 * in the Request-URI or in the top Record-Route header field value,
2528 * if there was any, or the Contact header field if there was no
2529 * Record-Route header field, the Contact header field in the response
2530 * MUST be a SIPS URI.
2531 */
2532 pjsua_acc *acc;
2533 pjsip_sip_uri *sip_uri;
2534 pj_status_t status;
2535 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2536 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002537 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002538 unsigned flag;
2539 int secure;
2540 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002541 const char *beginquote, *endquote;
2542 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002543
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002544 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002545 acc = &pjsua_var.acc[acc_id];
2546
Benny Prijonof75eceb2007-03-23 19:09:54 +00002547 /* If force_contact is configured, then use use it */
2548 if (acc->cfg.force_contact.slen) {
2549 *contact = acc->cfg.force_contact;
2550 return PJ_SUCCESS;
2551 }
2552
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002553 /* If Record-Route is present, then URI is the top Record-Route. */
2554 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002555 sip_uri = (pjsip_sip_uri*)
2556 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002557 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002558 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002559 pjsip_contact_hdr *h_contact;
2560 pjsip_uri *uri = NULL;
2561
Benny Prijonoa330d452008-08-05 20:14:39 +00002562 /* Otherwise URI is Contact URI.
2563 * Iterate the Contact URI until we find sip: or sips: scheme.
2564 */
2565 do {
2566 h_contact = (pjsip_contact_hdr*)
2567 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2568 pos);
2569 if (h_contact) {
Benny Prijono8b33bba2010-06-02 03:03:43 +00002570 if (h_contact->uri)
2571 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2572 else
2573 uri = NULL;
2574 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2575 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
Benny Prijonoa330d452008-08-05 20:14:39 +00002576 {
2577 pos = (pjsip_hdr*)h_contact->next;
2578 if (pos == &rdata->msg_info.msg->hdr)
2579 h_contact = NULL;
2580 } else {
2581 break;
2582 }
2583 }
2584 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002585
2586
2587 /* Or if Contact URI is not present, take the remote URI from
2588 * the From URI.
2589 */
2590 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002591 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002592
2593
2594 /* Can only do sip/sips scheme at present. */
2595 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2596 return PJSIP_EINVALIDREQURI;
2597
Benny Prijono8c7a6172007-02-18 21:17:46 +00002598 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002599 }
2600
2601 /* Get transport type of the URI */
2602 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2603 tp_type = PJSIP_TRANSPORT_TLS;
2604 else if (sip_uri->transport_param.slen == 0) {
2605 tp_type = PJSIP_TRANSPORT_UDP;
2606 } else
2607 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002608
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002609 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2610 return PJSIP_EUNSUPTRANSPORT;
2611
Benny Prijonod0bd4982007-12-02 15:40:52 +00002612 /* If destination URI specifies IPv6, then set transport type
2613 * to use IPv6 as well.
2614 */
2615 if (pj_strchr(&sip_uri->host, ':'))
2616 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2617
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002618 flag = pjsip_transport_get_flag_from_type(tp_type);
2619 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2620
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002621 /* Init transport selector. */
2622 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2623
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002624 /* Get local address suitable to send request from */
2625 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002626 pool, tp_type, &tp_sel,
2627 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002628 if (status != PJ_SUCCESS)
2629 return status;
2630
Benny Prijonod0bd4982007-12-02 15:40:52 +00002631 /* Enclose IPv6 address in square brackets */
2632 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2633 beginquote = "[";
2634 endquote = "]";
2635 } else {
2636 beginquote = endquote = "";
2637 }
2638
2639 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002640 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002641 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2642 ";transport=%s",
2643 pjsip_transport_get_type_name(tp_type));
2644 } else {
2645 transport_param[0] = '\0';
2646 }
2647
2648
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002649 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002650 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002651 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002652 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002653 (int)acc->display.slen,
2654 acc->display.ptr,
2655 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002656 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002657 (int)acc->user_part.slen,
2658 acc->user_part.ptr,
2659 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002660 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002661 (int)local_addr.slen,
2662 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002663 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002664 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002665 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002666 (int)acc->cfg.contact_uri_params.slen,
2667 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002668 (int)acc->cfg.contact_params.slen,
2669 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002670
2671 return PJ_SUCCESS;
2672}
2673
2674
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002675PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2676 pjsua_transport_id tp_id)
2677{
2678 pjsua_acc *acc;
2679
2680 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2681 acc = &pjsua_var.acc[acc_id];
2682
Benny Prijonoa1e69682007-05-11 15:14:34 +00002683 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002684 PJ_EINVAL);
2685
2686 acc->cfg.transport_id = tp_id;
2687
2688 return PJ_SUCCESS;
2689}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002690
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002691
2692/* Auto re-registration timeout callback */
2693static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2694{
2695 pjsua_acc *acc;
2696 pj_status_t status;
2697
2698 PJ_UNUSED_ARG(th);
2699 acc = (pjsua_acc*) te->user_data;
2700 pj_assert(acc);
2701
2702 PJSUA_LOCK();
2703
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002704 /* Check if the reregistration timer is still valid, e.g: while waiting
2705 * timeout timer application might have deleted the account or disabled
2706 * the auto-reregistration.
2707 */
2708 if (!acc->valid || !acc->auto_rereg.active ||
2709 acc->cfg.reg_retry_interval == 0)
2710 {
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002711 goto on_return;
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002712 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002713
2714 /* Start re-registration */
2715 acc->auto_rereg.attempt_cnt++;
2716 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2717 if (status != PJ_SUCCESS)
2718 schedule_reregistration(acc);
2719
Nanang Izzuddin66580002010-04-14 08:12:08 +00002720on_return:
2721 PJSUA_UNLOCK();
2722}
2723
2724
2725/* Schedule reregistration for specified account. Note that the first
2726 * re-registration after a registration failure will be done immediately.
2727 * Also note that this function should be called within PJSUA mutex.
2728 */
2729static void schedule_reregistration(pjsua_acc *acc)
2730{
2731 pj_time_val delay;
2732
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002733 pj_assert(acc);
2734
2735 /* Validate the account and re-registration feature status */
2736 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
2737 return;
2738 }
Nanang Izzuddin66580002010-04-14 08:12:08 +00002739
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002740 /* If configured, disconnect calls of this account after the first
2741 * reregistration attempt failed.
2742 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002743 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002744 {
2745 unsigned i, cnt;
2746
2747 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2748 if (pjsua_var.calls[i].acc_id == acc->index) {
2749 pjsua_call_hangup(i, 0, NULL, NULL);
2750 ++cnt;
2751 }
2752 }
2753
2754 if (cnt) {
2755 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2756 "after reregistration attempt failed",
2757 cnt, acc->index));
2758 }
2759 }
2760
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002761 /* Cancel any re-registration timer */
2762 pjsua_cancel_timer(&acc->auto_rereg.timer);
2763
2764 /* Update re-registration flag */
2765 acc->auto_rereg.active = PJ_TRUE;
2766
2767 /* Set up timer for reregistration */
2768 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2769 acc->auto_rereg.timer.user_data = acc;
2770
2771 /* Reregistration attempt. The first attempt will be done immediately. */
2772 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2773 delay.msec = 0;
2774 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2775}
2776
2777
2778/* Internal function to perform auto-reregistration on transport
2779 * connection/disconnection events.
2780 */
2781void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2782 pjsip_transport_state state,
2783 const pjsip_transport_state_info *info)
2784{
2785 unsigned i;
2786
2787 PJ_UNUSED_ARG(info);
2788
2789 /* Only care for transport disconnection events */
2790 if (state != PJSIP_TP_STATE_DISCONNECTED)
2791 return;
2792
2793 /* Shutdown this transport, to make sure that the transport manager
2794 * will create a new transport for reconnection.
2795 */
2796 pjsip_transport_shutdown(tp);
2797
2798 PJSUA_LOCK();
2799
2800 /* Enumerate accounts using this transport and perform actions
2801 * based on the transport state.
2802 */
2803 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2804 pjsua_acc *acc = &pjsua_var.acc[i];
2805
2806 /* Skip if this account is not valid OR auto re-registration
2807 * feature is disabled OR this transport is not used by this account.
2808 */
2809 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2810 tp != acc->auto_rereg.reg_tp)
2811 {
2812 continue;
2813 }
2814
2815 /* Schedule reregistration for this account */
2816 schedule_reregistration(acc);
2817 }
2818
2819 PJSUA_UNLOCK();
2820}