blob: 9bcd1ddd8ac4dd8e9ccfb16b799f5c8f3ec98dfb [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 Prijonob90fd382011-09-18 14:59:56 +0000377 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378
Benny Prijonob90fd382011-09-18 14:59:56 +0000379 PJ_ASSERT_RETURN(cfg, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000380 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
381 PJ_ETOOMANY);
382
383 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000384 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000385
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000386 /* Verify media count */
387#if !defined(PJMEDIA_HAS_VIDEO) || (PJMEDIA_HAS_VIDEO == 0)
Benny Prijonob90fd382011-09-18 14:59:56 +0000388 /* Enable PJMEDIA_HAS_VIDEO in your config_site.h! */
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000389 PJ_ASSERT_RETURN(cfg->max_video_cnt == 0, PJ_EINVAL);
390#endif
391 PJ_ASSERT_RETURN(cfg->max_audio_cnt + cfg->max_video_cnt <=
392 PJSUA_MAX_CALL_MEDIA, PJ_ETOOMANY);
393
Benny Prijonob90fd382011-09-18 14:59:56 +0000394 PJ_LOG(4,(THIS_FILE, "Adding account: id=%.*s",
395 (int)cfg->id.slen, cfg->id.ptr));
396 pj_log_push_indent();
397
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000398 PJSUA_LOCK();
399
400 /* Find empty account id. */
401 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
402 if (pjsua_var.acc[id].valid == PJ_FALSE)
403 break;
404 }
405
406 /* Expect to find a slot */
407 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
408 {PJSUA_UNLOCK(); return PJ_EBUG;});
409
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000410 acc = &pjsua_var.acc[id];
411
412 /* Create pool for this account. */
413 if (acc->pool)
414 pj_pool_reset(acc->pool);
415 else
416 acc->pool = pjsua_pool_create("acc%p", 512, 256);
417
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000418 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000419 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000420
Sauw Ming9b80d512011-03-15 03:20:37 +0000421 /* Normalize registration timeout and refresh delay */
422 if (pjsua_var.acc[id].cfg.reg_uri.slen) {
423 if (pjsua_var.acc[id].cfg.reg_timeout == 0) {
424 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
425 }
426 if (pjsua_var.acc[id].cfg.reg_delay_before_refresh == 0) {
427 pjsua_var.acc[id].cfg.reg_delay_before_refresh =
428 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
429 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000430 }
431
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000432 /* Get CRC of account proxy setting */
433 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
434
435 /* Get CRC of global outbound proxy setting */
436 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
437 pjsua_var.ua_cfg.outbound_proxy_cnt);
438
Benny Prijono91d06b62008-09-20 12:16:56 +0000439 /* Check the route URI's and force loose route if required */
440 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
441 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
Benny Prijonob90fd382011-09-18 14:59:56 +0000442 if (status != PJ_SUCCESS) {
443 PJSUA_UNLOCK();
444 pj_log_pop_indent();
Benny Prijono91d06b62008-09-20 12:16:56 +0000445 return status;
Benny Prijonob90fd382011-09-18 14:59:56 +0000446 }
Benny Prijono91d06b62008-09-20 12:16:56 +0000447 }
448
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000449 status = initialize_acc(id);
450 if (status != PJ_SUCCESS) {
451 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000452 pj_pool_release(acc->pool);
453 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000454 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +0000455 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000456 return status;
457 }
458
459 if (is_default)
460 pjsua_var.default_acc = id;
461
462 if (p_acc_id)
463 *p_acc_id = id;
464
465 pjsua_var.acc_cnt++;
466
467 PJSUA_UNLOCK();
468
469 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
470 (int)cfg->id.slen, cfg->id.ptr, id));
471
472 /* If accounts has registration enabled, start registration */
473 if (pjsua_var.acc[id].cfg.reg_uri.slen)
474 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000475 else {
476 /* Otherwise subscribe to MWI, if it's enabled */
477 if (pjsua_var.acc[id].cfg.mwi_enabled)
478 pjsua_start_mwi(&pjsua_var.acc[id]);
479 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000480
Benny Prijonob90fd382011-09-18 14:59:56 +0000481 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000482 return PJ_SUCCESS;
483}
484
485
486/*
487 * Add local account
488 */
489PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
490 pj_bool_t is_default,
491 pjsua_acc_id *p_acc_id)
492{
493 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000494 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000495 const char *beginquote, *endquote;
496 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000497 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498
Benny Prijonoe93e2872006-06-28 16:46:49 +0000499 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000500 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
501 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000502
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000503 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000504 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505
506 pjsua_acc_config_default(&cfg);
507
Benny Prijono093d3022006-09-24 00:07:11 +0000508 /* Lower the priority of local account */
509 --cfg.priority;
510
Benny Prijonod0bd4982007-12-02 15:40:52 +0000511 /* Enclose IPv6 address in square brackets */
512 if (t->type & PJSIP_TRANSPORT_IPV6) {
513 beginquote = "[";
514 endquote = "]";
515 } else {
516 beginquote = endquote = "";
517 }
518
519 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000520 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000521 pj_ansi_snprintf(transport_param, sizeof(transport_param),
522 ";transport=%s",
523 pjsip_transport_get_type_name(t->type));
524 } else {
525 transport_param[0] = '\0';
526 }
527
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000529 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000530 "<sip:%s%.*s%s:%d%s>",
531 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000532 (int)t->local_name.host.slen,
533 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000534 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000535 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000536 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537
538 cfg.id = pj_str(uri);
539
540 return pjsua_acc_add(&cfg, is_default, p_acc_id);
541}
542
543
544/*
Benny Prijono705e7842008-07-21 18:12:51 +0000545 * Set arbitrary data to be associated with the account.
546 */
547PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
548 void *user_data)
549{
550 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
551 PJ_EINVAL);
552 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
553
554 PJSUA_LOCK();
555
556 pjsua_var.acc[acc_id].cfg.user_data = user_data;
557
558 PJSUA_UNLOCK();
559
560 return PJ_SUCCESS;
561}
562
563
564/*
565 * Retrieve arbitrary data associated with the account.
566 */
567PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
568{
569 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
570 NULL);
571 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
572
573 return pjsua_var.acc[acc_id].cfg.user_data;
574}
575
576
577/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000578 * Delete account.
579 */
580PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
581{
Benny Prijono093d3022006-09-24 00:07:11 +0000582 unsigned i;
583
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000584 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
585 PJ_EINVAL);
586 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
587
Benny Prijonob90fd382011-09-18 14:59:56 +0000588 PJ_LOG(4,(THIS_FILE, "Deleting account %d..", acc_id));
589 pj_log_push_indent();
590
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000591 PJSUA_LOCK();
592
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000593 /* Cancel any re-registration timer */
594 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
595
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000596 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000597 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000598 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000599 if (pjsua_var.acc[acc_id].regc) {
600 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
601 }
Benny Prijono922933b2007-01-21 16:23:56 +0000602 pjsua_var.acc[acc_id].regc = NULL;
603 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000604
605 /* Delete server presence subscription */
606 pjsua_pres_delete_acc(acc_id);
607
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000608 /* Release account pool */
609 if (pjsua_var.acc[acc_id].pool) {
610 pj_pool_release(pjsua_var.acc[acc_id].pool);
611 pjsua_var.acc[acc_id].pool = NULL;
612 }
613
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000614 /* Invalidate */
615 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000616 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000617
Benny Prijono093d3022006-09-24 00:07:11 +0000618 /* Remove from array */
619 for (i=0; i<pjsua_var.acc_cnt; ++i) {
620 if (pjsua_var.acc_ids[i] == acc_id)
621 break;
622 }
623 if (i != pjsua_var.acc_cnt) {
624 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
625 pjsua_var.acc_cnt, i);
626 --pjsua_var.acc_cnt;
627 }
628
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000629 /* Leave the calls intact, as I don't think calls need to
630 * access account once it's created
631 */
632
Benny Prijonofb2b3652007-06-28 07:15:03 +0000633 /* Update default account */
634 if (pjsua_var.default_acc == acc_id)
635 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000636
637 PJSUA_UNLOCK();
638
639 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
640
Benny Prijonob90fd382011-09-18 14:59:56 +0000641 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000642 return PJ_SUCCESS;
643}
644
645
646/*
647 * Modify account information.
648 */
649PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
650 const pjsua_acc_config *cfg)
651{
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000652 pjsua_acc *acc;
653 pjsip_name_addr *id_name_addr = NULL;
654 pjsip_sip_uri *id_sip_uri = NULL;
655 pjsip_sip_uri *reg_sip_uri = NULL;
656 pj_uint32_t local_route_crc, global_route_crc;
657 pjsip_route_hdr global_route;
658 pjsip_route_hdr local_route;
659 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
660 pj_bool_t update_reg = PJ_FALSE;
661 pj_status_t status = PJ_SUCCESS;
662
663 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
664 PJ_EINVAL);
665
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000666 /* Verify media count */
667#if !defined(PJMEDIA_HAS_VIDEO) || (PJMEDIA_HAS_VIDEO == 0)
668 PJ_ASSERT_RETURN(cfg->max_video_cnt == 0, PJ_EINVAL);
669#endif
670 PJ_ASSERT_RETURN(cfg->max_audio_cnt + cfg->max_video_cnt <=
671 PJSUA_MAX_CALL_MEDIA, PJ_ETOOMANY);
672
Benny Prijonob90fd382011-09-18 14:59:56 +0000673 PJ_LOG(4,(THIS_FILE, "Modifying accunt %d", acc_id));
674 pj_log_push_indent();
Nanang Izzuddin63b3c132011-07-19 11:11:07 +0000675
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000676 PJSUA_LOCK();
677
678 acc = &pjsua_var.acc[acc_id];
679 if (!acc->valid) {
680 status = PJ_EINVAL;
681 goto on_return;
682 }
683
684 /* == Validate first == */
685
686 /* Account id */
687 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
688 /* Need to parse id to get the elements: */
689 id_name_addr = (pjsip_name_addr*)
690 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
691 PJSIP_PARSE_URI_AS_NAMEADDR);
692 if (id_name_addr == NULL) {
693 status = PJSIP_EINVALIDURI;
694 pjsua_perror(THIS_FILE, "Invalid local URI", status);
695 goto on_return;
696 }
697
698 /* URI MUST be a SIP or SIPS: */
699 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
700 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
701 {
702 status = PJSIP_EINVALIDSCHEME;
703 pjsua_perror(THIS_FILE, "Invalid local URI", status);
704 goto on_return;
705 }
706
707 /* Get the SIP URI object: */
708 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
709 }
710
711 /* Registrar URI */
712 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
713 pjsip_uri *reg_uri;
714
715 /* Need to parse reg_uri to get the elements: */
716 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
717 cfg->reg_uri.slen, 0);
718 if (reg_uri == NULL) {
719 status = PJSIP_EINVALIDURI;
720 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
721 goto on_return;
722 }
723
724 /* Registrar URI MUST be a SIP or SIPS: */
725 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
726 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
727 {
728 status = PJSIP_EINVALIDSCHEME;
729 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
730 goto on_return;
731 }
732
733 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
734 }
735
736 /* Global outbound proxy */
737 global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
738 pjsua_var.ua_cfg.outbound_proxy_cnt);
739 if (global_route_crc != acc->global_route_crc) {
740 pjsip_route_hdr *r;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000741
Benny Prijono29c8ca32010-06-22 06:02:13 +0000742 /* Copy from global outbound proxies */
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000743 pj_list_init(&global_route);
Benny Prijono29c8ca32010-06-22 06:02:13 +0000744 r = pjsua_var.outbound_proxy.next;
745 while (r != &pjsua_var.outbound_proxy) {
746 pj_list_push_back(&global_route,
747 pjsip_hdr_shallow_clone(acc->pool, r));
748 r = r->next;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000749 }
750 }
751
752 /* Account proxy */
753 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
754 if (local_route_crc != acc->local_route_crc) {
755 pjsip_route_hdr *r;
756 unsigned i;
757
758 /* Validate the local route and save it to temporary var */
759 pj_list_init(&local_route);
760 for (i=0; i<cfg->proxy_cnt; ++i) {
761 pj_str_t hname = { "Route", 5};
762
763 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
764 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
765 if (status != PJ_SUCCESS)
766 goto on_return;
767 r = (pjsip_route_hdr*)
768 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
769 acc_proxy[i].slen, NULL);
770 if (r == NULL) {
771 status = PJSIP_EINVALIDURI;
772 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
773 status);
774 goto on_return;
775 }
776
777 pj_list_push_back(&local_route, r);
778 }
779 }
780
781
782 /* == Apply the new config == */
783
784 /* Account ID. */
785 if (id_name_addr && id_sip_uri) {
786 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
Benny Prijonof0ba2dc2011-04-14 15:38:23 +0000787 pj_strdup_with_null(acc->pool, &acc->display, &id_name_addr->display);
788 pj_strdup_with_null(acc->pool, &acc->user_part, &id_sip_uri->user);
789 pj_strdup_with_null(acc->pool, &acc->srv_domain, &id_sip_uri->host);
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000790 acc->srv_port = 0;
791 update_reg = PJ_TRUE;
792 }
793
794 /* User data */
795 acc->cfg.user_data = cfg->user_data;
796
797 /* Priority */
798 if (acc->cfg.priority != cfg->priority) {
799 unsigned i;
800
801 acc->cfg.priority = cfg->priority;
802
803 /* Resort accounts priority */
804 for (i=0; i<pjsua_var.acc_cnt; ++i) {
805 if (pjsua_var.acc_ids[i] == acc_id)
806 break;
807 }
808 pj_assert(i < pjsua_var.acc_cnt);
809 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
810 pjsua_var.acc_cnt, i);
811 for (i=0; i<pjsua_var.acc_cnt; ++i) {
812 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
813 acc->cfg.priority)
814 {
815 break;
816 }
817 }
818 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
819 pjsua_var.acc_cnt, i, &acc_id);
820 }
821
822 /* MWI */
823 acc->cfg.mwi_enabled = cfg->mwi_enabled;
824
825 /* PIDF tuple ID */
826 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
827 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
828 &cfg->pidf_tuple_id);
829
830 /* Publish */
831 acc->cfg.publish_opt = cfg->publish_opt;
832 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
833 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
834 acc->cfg.publish_enabled = cfg->publish_enabled;
835 if (!acc->cfg.publish_enabled)
836 pjsua_pres_unpublish(acc);
837 else
838 update_reg = PJ_TRUE;
839 }
840
841 /* Force contact URI */
842 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
843 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
844 &cfg->force_contact);
845 update_reg = PJ_TRUE;
846 }
847
848 /* Contact param */
849 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
850 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
851 &cfg->contact_params);
852 update_reg = PJ_TRUE;
853 }
854
855 /* Contact URI params */
856 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
857 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
858 &cfg->contact_uri_params);
859 update_reg = PJ_TRUE;
860 }
861
862 /* Reliable provisional response */
863 acc->cfg.require_100rel = cfg->require_100rel;
864
865 /* Session timer */
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000866 acc->cfg.use_timer = cfg->use_timer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000867 acc->cfg.timer_setting = cfg->timer_setting;
868
869 /* Transport and keep-alive */
870 if (acc->cfg.transport_id != cfg->transport_id) {
871 acc->cfg.transport_id = cfg->transport_id;
872 update_reg = PJ_TRUE;
873 }
874 acc->cfg.ka_interval = cfg->ka_interval;
875 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
876 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
877#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
878 acc->cfg.use_srtp = cfg->use_srtp;
879 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000880 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000881#endif
882
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000883#if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
884 acc->cfg.use_stream_ka = cfg->use_stream_ka;
885#endif
886
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000887 /* Global outbound proxy */
888 if (global_route_crc != acc->global_route_crc) {
889 unsigned i, rcnt;
890
891 /* Remove the outbound proxies from the route set */
892 rcnt = pj_list_size(&acc->route_set);
893 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
894 pjsip_route_hdr *r = acc->route_set.next;
895 pj_list_erase(r);
896 }
897
898 /* Insert the outbound proxies to the beginning of route set */
899 pj_list_merge_first(&acc->route_set, &global_route);
900
901 /* Update global route CRC */
902 acc->global_route_crc = global_route_crc;
903
904 update_reg = PJ_TRUE;
905 }
906
907 /* Account proxy */
908 if (local_route_crc != acc->local_route_crc) {
909 unsigned i;
910
911 /* Remove the current account proxies from the route set */
912 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
913 pjsip_route_hdr *r = acc->route_set.prev;
914 pj_list_erase(r);
915 }
916
917 /* Insert new proxy setting to the route set */
918 pj_list_merge_last(&acc->route_set, &local_route);
919
920 /* Update the proxy setting */
921 acc->cfg.proxy_cnt = cfg->proxy_cnt;
922 for (i = 0; i < cfg->proxy_cnt; ++i)
923 acc->cfg.proxy[i] = acc_proxy[i];
924
925 /* Update local route CRC */
926 acc->local_route_crc = local_route_crc;
927
928 update_reg = PJ_TRUE;
929 }
930
931 /* Credential info */
932 {
933 unsigned i;
934
935 /* Selective update credential info. */
936 for (i = 0; i < cfg->cred_count; ++i) {
937 unsigned j;
938 pjsip_cred_info ci;
939
940 /* Find if this credential is already listed */
Nanang Izzuddin7f8d76f2011-03-29 03:47:16 +0000941 for (j = i; j < acc->cfg.cred_count; ++j) {
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000942 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
943 &cfg->cred_info[i]) == 0)
944 {
945 /* Found, but different index/position, swap */
946 if (j != i) {
947 ci = acc->cfg.cred_info[i];
948 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
949 acc->cfg.cred_info[j] = ci;
950 }
951 break;
952 }
953 }
954
955 /* Not found, insert this */
956 if (j == acc->cfg.cred_count) {
957 /* If account credential is full, discard the last one. */
958 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
959 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
960 acc->cfg.cred_count, acc->cfg.cred_count-1);
961 acc->cfg.cred_count--;
962 }
963
964 /* Insert this */
965 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
966 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
967 acc->cfg.cred_count, i, &ci);
968 }
969 }
970 acc->cfg.cred_count = cfg->cred_count;
971
972 /* Concatenate credentials from account config and global config */
973 acc->cred_cnt = 0;
974 for (i=0; i<acc->cfg.cred_count; ++i) {
975 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
976 }
977 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
978 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
979 {
980 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
981 }
982 }
983
984 /* Authentication preference */
985 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
986 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
987 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
988 &cfg->auth_pref.algorithm);
989
990 /* Registration */
991 acc->cfg.reg_timeout = cfg->reg_timeout;
992 acc->cfg.unreg_timeout = cfg->unreg_timeout;
993 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
994 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
995 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
Sauw Ming8ad06c52011-03-15 10:49:59 +0000996 if (acc->cfg.reg_delay_before_refresh != cfg->reg_delay_before_refresh) {
997 acc->cfg.reg_delay_before_refresh = cfg->reg_delay_before_refresh;
998 pjsip_regc_set_delay_before_refresh(acc->regc,
999 cfg->reg_delay_before_refresh);
1000 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001001
Sauw Ming9b80d512011-03-15 03:20:37 +00001002 /* Normalize registration timeout and refresh delay */
1003 if (acc->cfg.reg_uri.slen ) {
1004 if (acc->cfg.reg_timeout == 0) {
1005 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
1006 }
1007 if (acc->cfg.reg_delay_before_refresh == 0) {
1008 acc->cfg.reg_delay_before_refresh =
1009 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
1010 }
1011 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001012
1013 /* Registrar URI */
1014 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
1015 if (cfg->reg_uri.slen) {
1016 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
1017 if (reg_sip_uri)
1018 acc->srv_port = reg_sip_uri->port;
1019 } else {
1020 /* Unregister if registration was set */
1021 if (acc->cfg.reg_uri.slen)
1022 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1023 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
1024 }
1025 update_reg = PJ_TRUE;
1026 }
1027
Benny Prijonob54719f2010-11-16 03:07:46 +00001028 /* SIP outbound setting */
1029 if (acc->cfg.use_rfc5626 != cfg->use_rfc5626 ||
1030 pj_strcmp(&acc->cfg.rfc5626_instance_id, &cfg->rfc5626_instance_id) ||
1031 pj_strcmp(&acc->cfg.rfc5626_reg_id, &cfg->rfc5626_reg_id))
1032 {
1033 update_reg = PJ_TRUE;
1034 }
1035
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001036 /* Update registration */
1037 if (update_reg) {
1038 /* If accounts has registration enabled, start registration */
1039 if (acc->cfg.reg_uri.slen)
1040 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1041 else {
1042 /* Otherwise subscribe to MWI, if it's enabled */
1043 if (acc->cfg.mwi_enabled)
1044 pjsua_start_mwi(acc);
1045 }
1046 }
1047
Nanang Izzuddine7acf222011-07-26 08:10:29 +00001048 /* Max number of audio and video stream in a call */
1049 acc->cfg.max_audio_cnt = cfg->max_audio_cnt;
1050 acc->cfg.max_video_cnt = cfg->max_video_cnt;
1051
1052 /* Video settings */
1053 acc->cfg.vid_in_auto_show = cfg->vid_in_auto_show;
1054 acc->cfg.vid_out_auto_transmit = cfg->vid_out_auto_transmit;
1055 acc->cfg.vid_cap_dev = cfg->vid_cap_dev;
1056 acc->cfg.vid_rend_dev = cfg->vid_rend_dev;
1057
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001058on_return:
1059 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00001060 pj_log_pop_indent();
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001061 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001062}
1063
1064
1065/*
1066 * Modify account's presence status to be advertised to remote/presence
1067 * subscribers.
1068 */
1069PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
1070 pj_bool_t is_online)
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 Prijonob90fd382011-09-18 14:59:56 +00001076 PJ_LOG(4,(THIS_FILE, "Acc %d: setting online status to %d..",
1077 acc_id, is_online));
1078 pj_log_push_indent();
1079
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001080 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001081 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
1082 pjsua_pres_update_acc(acc_id, PJ_FALSE);
Benny Prijonob90fd382011-09-18 14:59:56 +00001083
1084 pj_log_pop_indent();
Benny Prijono4461c7d2007-08-25 13:36:15 +00001085 return PJ_SUCCESS;
1086}
1087
1088
1089/*
1090 * Set online status with extended information
1091 */
1092PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
1093 pj_bool_t is_online,
1094 const pjrpid_element *pr)
1095{
1096 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1097 PJ_EINVAL);
1098 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1099
Benny Prijonob90fd382011-09-18 14:59:56 +00001100 PJ_LOG(4,(THIS_FILE, "Acc %d: setting online status to %d..",
1101 acc_id, is_online));
1102 pj_log_push_indent();
1103
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001104 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +00001105 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001106 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
1107 PJSUA_UNLOCK();
1108
Benny Prijono4461c7d2007-08-25 13:36:15 +00001109 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonob90fd382011-09-18 14:59:56 +00001110 pj_log_pop_indent();
1111
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001112 return PJ_SUCCESS;
1113}
1114
Benny Prijonob54719f2010-11-16 03:07:46 +00001115/* Create reg_contact, mainly for SIP outbound */
1116static void update_regc_contact(pjsua_acc *acc)
1117{
1118 pjsua_acc_config *acc_cfg = &acc->cfg;
1119 pj_bool_t need_outbound = PJ_FALSE;
1120 const pj_str_t tcp_param = pj_str(";transport=tcp");
1121 const pj_str_t tls_param = pj_str(";transport=tls");
1122
1123 if (!acc_cfg->use_rfc5626)
1124 goto done;
1125
Benny Prijono2562b752010-11-16 06:01:38 +00001126 /* Check if outbound has been requested and rejected */
1127 if (acc->rfc5626_status == OUTBOUND_NA)
1128 goto done;
1129
Benny Prijonob54719f2010-11-16 03:07:46 +00001130 if (pj_stristr(&acc->contact, &tcp_param)==NULL &&
1131 pj_stristr(&acc->contact, &tls_param)==NULL)
1132 {
1133 /* Currently we can only do SIP outbound for TCP
1134 * and TLS.
1135 */
1136 goto done;
1137 }
1138
1139 /* looks like we can use outbound */
1140 need_outbound = PJ_TRUE;
1141
1142done:
1143 if (!need_outbound) {
1144 /* Outbound is not needed/wanted for the account. acc->reg_contact
1145 * is set to the same as acc->contact.
1146 */
1147 acc->reg_contact = acc->contact;
Benny Prijono2562b752010-11-16 06:01:38 +00001148 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001149 } else {
1150 /* Need to use outbound, append the contact with +sip.instance and
1151 * reg-id parameters.
1152 */
1153 unsigned len;
1154 pj_str_t reg_contact;
1155
1156 acc->rfc5626_status = OUTBOUND_WANTED;
1157 len = acc->contact.slen + acc->rfc5626_instprm.slen +
1158 acc->rfc5626_regprm.slen;
Benny Prijonoe49e6202010-11-16 07:01:25 +00001159 reg_contact.ptr = (char*) pj_pool_alloc(acc->pool, len);
Benny Prijonob54719f2010-11-16 03:07:46 +00001160
1161 pj_strcpy(&reg_contact, &acc->contact);
1162 pj_strcat(&reg_contact, &acc->rfc5626_regprm);
1163 pj_strcat(&reg_contact, &acc->rfc5626_instprm);
1164
1165 acc->reg_contact = reg_contact;
1166
1167 PJ_LOG(4,(THIS_FILE,
1168 "Contact for acc %d updated for SIP outbound: %.*s",
1169 acc->index,
1170 (int)acc->reg_contact.slen,
1171 acc->reg_contact.ptr));
1172 }
1173}
1174
Benny Prijono7f630432008-09-24 16:52:41 +00001175/* Check if IP is private IP address */
1176static pj_bool_t is_private_ip(const pj_str_t *addr)
1177{
1178 const pj_str_t private_net[] =
1179 {
1180 { "10.", 3 },
1181 { "127.", 4 },
1182 { "172.16.", 7 },
1183 { "192.168.", 8 }
1184 };
1185 unsigned i;
1186
1187 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
1188 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
1189 return PJ_TRUE;
1190 }
1191
1192 return PJ_FALSE;
1193}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001194
Benny Prijono15b02302007-09-27 14:07:07 +00001195/* Update NAT address from the REGISTER response */
1196static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
1197 struct pjsip_regc_cbparam *param)
1198{
1199 pjsip_transport *tp;
1200 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001201 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +00001202 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001203 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +00001204 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +00001205 pj_sockaddr contact_addr;
1206 pj_sockaddr recv_addr;
1207 pj_status_t status;
1208 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +00001209 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001210 pjsip_contact_hdr *contact_hdr;
1211 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +00001212
1213 tp = param->rdata->tp_info.transport;
1214
1215 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001216 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +00001217 return PJ_FALSE;
1218
Benny Prijonob54719f2010-11-16 03:07:46 +00001219 /* If SIP outbound is active, no need to update */
1220 if (acc->rfc5626_status == OUTBOUND_ACTIVE) {
1221 PJ_LOG(4,(THIS_FILE, "Acc %d has SIP outbound active, no need to "
1222 "update registration Contact", acc->index));
1223 return PJ_FALSE;
1224 }
1225
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001226#if 0
1227 // Always update
1228 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +00001229
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001230 /* For UDP, only update if STUN is enabled (for now).
1231 * For TCP/TLS, always check.
1232 */
1233 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
1234 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
1235 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
1236 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
1237 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +00001238 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001239 /* Yes we will check */
1240 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001241 return PJ_FALSE;
1242 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001243#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001244
1245 /* Get the received and rport info */
1246 via = param->rdata->msg_info.via;
1247 if (via->rport_param < 1) {
1248 /* Remote doesn't support rport */
1249 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001250 if (rport==0) {
1251 pjsip_transport_type_e tp_type;
1252 tp_type = (pjsip_transport_type_e) tp->key.type;
1253 rport = pjsip_transport_get_default_port_for_type(tp_type);
1254 }
Benny Prijono15b02302007-09-27 14:07:07 +00001255 } else
1256 rport = via->rport_param;
1257
1258 if (via->recvd_param.slen != 0)
1259 via_addr = &via->recvd_param;
1260 else
1261 via_addr = &via->sent_by.host;
1262
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001263 /* Compare received and rport with the URI in our registration */
1264 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001265 contact_hdr = (pjsip_contact_hdr*)
1266 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1267 acc->contact.slen, NULL);
1268 pj_assert(contact_hdr != NULL);
1269 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001270 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001271 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001272
Benny Prijono24a21852008-04-14 04:04:30 +00001273 if (uri->port == 0) {
1274 pjsip_transport_type_e tp_type;
1275 tp_type = (pjsip_transport_type_e) tp->key.type;
1276 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1277 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001278
Benny Prijono84d24932009-06-04 15:51:39 +00001279 /* Convert IP address strings into sockaddr for comparison.
1280 * (http://trac.pjsip.org/repos/ticket/863)
1281 */
1282 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1283 &contact_addr);
1284 if (status == PJ_SUCCESS)
1285 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1286 &recv_addr);
1287 if (status == PJ_SUCCESS) {
1288 /* Compare the addresses as sockaddr according to the ticket above */
1289 matched = (uri->port == rport &&
1290 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1291 } else {
1292 /* Compare the addresses as string, as before */
1293 matched = (uri->port == rport &&
1294 pj_stricmp(&uri->host, via_addr)==0);
1295 }
1296
1297 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001298 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001299 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001300 return PJ_FALSE;
1301 }
1302
Benny Prijono7f630432008-09-24 16:52:41 +00001303 /* Get server IP */
1304 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1305
Benny Prijono15b02302007-09-27 14:07:07 +00001306 /* At this point we've detected that the address as seen by registrar.
1307 * has changed.
1308 */
Benny Prijono7f630432008-09-24 16:52:41 +00001309
1310 /* Do not switch if both Contact and server's IP address are
1311 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001312 * might have messed up with the SIP packets. See:
1313 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001314 *
1315 * This exception can be disabled by setting allow_contact_rewrite
1316 * to 2. In this case, the switch will always be done whenever there
1317 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001318 */
Benny Prijono247921b2008-09-26 22:06:11 +00001319 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1320 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001321 {
1322 /* Don't switch */
1323 pj_pool_release(pool);
1324 return PJ_FALSE;
1325 }
1326
Benny Prijono4f933762009-11-10 03:45:42 +00001327 /* Also don't switch if only the port number part is different, and
1328 * the Via received address is private.
1329 * See http://trac.pjsip.org/repos/ticket/864
1330 */
1331 if (acc->cfg.allow_contact_rewrite != 2 &&
1332 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1333 is_private_ip(via_addr))
1334 {
1335 /* Don't switch */
1336 pj_pool_release(pool);
1337 return PJ_FALSE;
1338 }
1339
Benny Prijono15b02302007-09-27 14:07:07 +00001340 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001341 "(%.*s:%d --> %.*s:%d). Updating registration "
1342 "(using method %d)",
Benny Prijono15b02302007-09-27 14:07:07 +00001343 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001344 (int)uri->host.slen,
1345 uri->host.ptr,
1346 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001347 (int)via_addr->slen,
1348 via_addr->ptr,
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001349 rport,
1350 acc->cfg.contact_rewrite_method));
Benny Prijono15b02302007-09-27 14:07:07 +00001351
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001352 pj_assert(acc->cfg.contact_rewrite_method == 1 ||
1353 acc->cfg.contact_rewrite_method == 2);
1354
1355 if (acc->cfg.contact_rewrite_method == 1) {
1356 /* Unregister current contact */
1357 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1358 if (acc->regc != NULL) {
1359 pjsip_regc_destroy(acc->regc);
1360 acc->regc = NULL;
1361 acc->contact.slen = 0;
1362 }
Benny Prijono15b02302007-09-27 14:07:07 +00001363 }
1364
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001365 /*
1366 * Build new Contact header
1367 */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001368 {
Benny Prijonob54719f2010-11-16 03:07:46 +00001369 const char *ob = ";ob";
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001370 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001371 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001372 int len;
1373
Benny Prijono8972bf02009-04-05 18:30:45 +00001374 /* Enclose IPv6 address in square brackets */
1375 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1376 beginquote = "[";
1377 endquote = "]";
1378 } else {
1379 beginquote = endquote = "";
1380 }
1381
Benny Prijono24a21852008-04-14 04:04:30 +00001382 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001383 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00001384 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s%s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001385 (int)acc->user_part.slen,
1386 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001387 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001388 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001389 (int)via_addr->slen,
1390 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001391 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001392 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001393 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001394 (int)acc->cfg.contact_uri_params.slen,
1395 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00001396 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00001397 (int)acc->cfg.contact_params.slen,
1398 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001399 if (len < 1) {
1400 PJ_LOG(1,(THIS_FILE, "URI too long"));
1401 pj_pool_release(pool);
1402 return PJ_FALSE;
1403 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001404 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001405
Benny Prijonob54719f2010-11-16 03:07:46 +00001406 update_regc_contact(acc);
1407
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001408 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1409 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1410 tp->local_name.port = rport;
1411
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001412 }
1413
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001414 if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001415 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001416 }
Benny Prijono15b02302007-09-27 14:07:07 +00001417
1418 /* Perform new registration */
1419 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1420
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001421 pj_pool_release(pool);
1422
Benny Prijono15b02302007-09-27 14:07:07 +00001423 return PJ_TRUE;
1424}
1425
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001426/* Check and update Service-Route header */
1427void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1428{
1429 pjsip_generic_string_hdr *hsr = NULL;
1430 pjsip_route_hdr *hr, *h;
1431 const pj_str_t HNAME = { "Service-Route", 13 };
1432 const pj_str_t HROUTE = { "Route", 5 };
1433 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001434 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001435
1436 /* Find and parse Service-Route headers */
1437 for (;;) {
1438 char saved;
1439 int parsed_len;
1440
1441 /* Find Service-Route header */
1442 hsr = (pjsip_generic_string_hdr*)
1443 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1444 if (!hsr)
1445 break;
1446
1447 /* Parse as Route header since the syntax is similar. This may
1448 * return more than one headers.
1449 */
1450 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1451 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1452 hr = (pjsip_route_hdr*)
1453 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1454 hsr->hvalue.slen, &parsed_len);
1455 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1456
1457 if (hr == NULL) {
1458 /* Error */
1459 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1460 return;
1461 }
1462
1463 /* Save each URI in the result */
1464 h = hr;
1465 do {
1466 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1467 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1468 {
1469 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1470 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1471 return;
1472 }
1473
1474 uri[uri_cnt++] = h->name_addr.uri;
1475 h = h->next;
1476 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1477
1478 if (h != hr) {
1479 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1480 return;
1481 }
1482
1483 /* Prepare to find next Service-Route header */
1484 hsr = hsr->next;
1485 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1486 break;
1487 }
1488
1489 if (uri_cnt == 0)
1490 return;
1491
1492 /*
1493 * Update account's route set
1494 */
1495
1496 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001497 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001498 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1499 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1500 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001501 i<rcnt;
1502 ++i)
1503 {
1504 pjsip_route_hdr *prev = hr->prev;
1505 pj_list_erase(hr);
1506 hr = prev;
1507 }
1508 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001509
1510 /* Then append the Service-Route URIs */
1511 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001512 hr = pjsip_route_hdr_create(acc->pool);
1513 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001514 pj_list_push_back(&acc->route_set, hr);
1515 }
1516
1517 /* Done */
1518
1519 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1520 acc->index, uri_cnt));
1521}
1522
Benny Prijonobddef2c2007-10-31 13:28:08 +00001523
1524/* Keep alive timer callback */
1525static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1526{
1527 pjsua_acc *acc;
1528 pjsip_tpselector tp_sel;
1529 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001530 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001531 pj_status_t status;
1532
1533 PJ_UNUSED_ARG(th);
1534
1535 PJSUA_LOCK();
1536
1537 te->id = PJ_FALSE;
1538
1539 acc = (pjsua_acc*) te->user_data;
1540
1541 /* Select the transport to send the packet */
1542 pj_bzero(&tp_sel, sizeof(tp_sel));
1543 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1544 tp_sel.u.transport = acc->ka_transport;
1545
1546 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001547 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001548 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001549 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001550
1551 /* Send raw packet */
1552 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1553 PJSIP_TRANSPORT_UDP, &tp_sel,
1554 NULL, acc->cfg.ka_data.ptr,
1555 acc->cfg.ka_data.slen,
1556 &acc->ka_target, acc->ka_target_len,
1557 NULL, NULL);
1558
1559 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1560 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1561 }
1562
1563 /* Reschedule next timer */
1564 delay.sec = acc->cfg.ka_interval;
1565 delay.msec = 0;
1566 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1567 if (status == PJ_SUCCESS) {
1568 te->id = PJ_TRUE;
1569 } else {
1570 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1571 }
1572
1573 PJSUA_UNLOCK();
1574}
1575
1576
1577/* Update keep-alive for the account */
1578static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1579 struct pjsip_regc_cbparam *param)
1580{
1581 /* In all cases, stop keep-alive timer if it's running. */
1582 if (acc->ka_timer.id) {
1583 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1584 acc->ka_timer.id = PJ_FALSE;
1585
1586 pjsip_transport_dec_ref(acc->ka_transport);
1587 acc->ka_transport = NULL;
1588 }
1589
1590 if (start) {
1591 pj_time_val delay;
1592 pj_status_t status;
1593
1594 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001595 * - ka_interval is not zero in the account, and
1596 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001597 *
1598 * Previously we only enabled keep-alive when STUN is enabled, since
1599 * we thought that keep-alive is only needed in Internet situation.
1600 * But it has been discovered that Windows Firewall on WinXP also
1601 * needs to be kept-alive, otherwise incoming packets will be dropped.
1602 * So because of this, now keep-alive is always enabled for UDP,
1603 * regardless of whether STUN is enabled or not.
1604 *
1605 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1606 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001607 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001608 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001609 acc->cfg.ka_interval == 0 ||
1610 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1611 {
1612 /* Keep alive is not necessary */
1613 return;
1614 }
1615
1616 /* Save transport and destination address. */
1617 acc->ka_transport = param->rdata->tp_info.transport;
1618 pjsip_transport_add_ref(acc->ka_transport);
1619 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1620 param->rdata->pkt_info.src_addr_len);
1621 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1622
1623 /* Setup and start the timer */
1624 acc->ka_timer.cb = &keep_alive_timer_cb;
1625 acc->ka_timer.user_data = (void*)acc;
1626
1627 delay.sec = acc->cfg.ka_interval;
1628 delay.msec = 0;
1629 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1630 &delay);
1631 if (status == PJ_SUCCESS) {
1632 acc->ka_timer.id = PJ_TRUE;
1633 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1634 "destination:%s:%d, interval:%ds",
1635 acc->index,
1636 param->rdata->pkt_info.src_name,
1637 param->rdata->pkt_info.src_port,
1638 acc->cfg.ka_interval));
1639 } else {
1640 acc->ka_timer.id = PJ_FALSE;
1641 pjsip_transport_dec_ref(acc->ka_transport);
1642 acc->ka_transport = NULL;
1643 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1644 }
1645 }
1646}
1647
1648
Benny Prijonob54719f2010-11-16 03:07:46 +00001649/* Update the status of SIP outbound registration request */
1650static void update_rfc5626_status(pjsua_acc *acc, pjsip_rx_data *rdata)
1651{
1652 pjsip_require_hdr *hreq;
1653 const pj_str_t STR_OUTBOUND = {"outbound", 8};
1654 unsigned i;
1655
Benny Prijono2562b752010-11-16 06:01:38 +00001656 if (acc->rfc5626_status == OUTBOUND_UNKNOWN) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001657 goto on_return;
1658 }
1659
1660 hreq = rdata->msg_info.require;
1661 if (!hreq) {
Benny Prijono2562b752010-11-16 06:01:38 +00001662 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001663 goto on_return;
1664 }
1665
1666 for (i=0; i<hreq->count; ++i) {
1667 if (pj_stricmp(&hreq->values[i], &STR_OUTBOUND)==0) {
1668 acc->rfc5626_status = OUTBOUND_ACTIVE;
1669 goto on_return;
1670 }
1671 }
1672
1673 /* Server does not support outbound */
Benny Prijono2562b752010-11-16 06:01:38 +00001674 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001675
1676on_return:
Benny Prijono2562b752010-11-16 06:01:38 +00001677 if (acc->rfc5626_status != OUTBOUND_ACTIVE) {
1678 acc->reg_contact = acc->contact;
1679 }
Benny Prijonob54719f2010-11-16 03:07:46 +00001680 PJ_LOG(4,(THIS_FILE, "SIP outbound status for acc %d is %s",
1681 acc->index, (acc->rfc5626_status==OUTBOUND_ACTIVE?
1682 "active": "not active")));
1683}
1684
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001685/*
1686 * This callback is called by pjsip_regc when outgoing register
1687 * request has completed.
1688 */
1689static void regc_cb(struct pjsip_regc_cbparam *param)
1690{
1691
Benny Prijonoa1e69682007-05-11 15:14:34 +00001692 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001693
Benny Prijono15b02302007-09-27 14:07:07 +00001694 if (param->regc != acc->regc)
1695 return;
1696
Benny Prijonob90fd382011-09-18 14:59:56 +00001697 pj_log_push_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001698 PJSUA_LOCK();
1699
1700 /*
1701 * Print registration status.
1702 */
1703 if (param->status!=PJ_SUCCESS) {
1704 pjsua_perror(THIS_FILE, "SIP registration error",
1705 param->status);
1706 pjsip_regc_destroy(acc->regc);
1707 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001708 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001709
Benny Prijonobddef2c2007-10-31 13:28:08 +00001710 /* Stop keep-alive timer if any. */
1711 update_keep_alive(acc, PJ_FALSE, NULL);
1712
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001713 } else if (param->code < 0 || param->code >= 300) {
1714 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1715 param->code,
1716 (int)param->reason.slen, param->reason.ptr));
1717 pjsip_regc_destroy(acc->regc);
1718 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001719 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001720
Benny Prijonobddef2c2007-10-31 13:28:08 +00001721 /* Stop keep-alive timer if any. */
1722 update_keep_alive(acc, PJ_FALSE, NULL);
1723
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001724 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1725
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001726 /* Update auto registration flag */
1727 acc->auto_rereg.active = PJ_FALSE;
1728 acc->auto_rereg.attempt_cnt = 0;
1729
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001730 if (param->expiration < 1) {
1731 pjsip_regc_destroy(acc->regc);
1732 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001733 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001734
1735 /* Stop keep-alive timer if any. */
1736 update_keep_alive(acc, PJ_FALSE, NULL);
1737
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001738 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1739 pjsua_var.acc[acc->index].cfg.id.ptr));
1740 } else {
Benny Prijonob54719f2010-11-16 03:07:46 +00001741 /* Check and update SIP outbound status first, since the result
1742 * will determine if we should update re-registration
1743 */
1744 update_rfc5626_status(acc, param->rdata);
1745
Benny Prijono15b02302007-09-27 14:07:07 +00001746 /* Check NAT bound address */
1747 if (acc_check_nat_addr(acc, param)) {
Benny Prijono15b02302007-09-27 14:07:07 +00001748 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00001749 pj_log_pop_indent();
Benny Prijono15b02302007-09-27 14:07:07 +00001750 return;
1751 }
1752
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001753 /* Check and update Service-Route header */
1754 update_service_route(acc, param->rdata);
1755
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001756 PJ_LOG(3, (THIS_FILE,
1757 "%s: registration success, status=%d (%.*s), "
1758 "will re-register in %d seconds",
1759 pjsua_var.acc[acc->index].cfg.id.ptr,
1760 param->code,
1761 (int)param->reason.slen, param->reason.ptr,
1762 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001763
Benny Prijonobddef2c2007-10-31 13:28:08 +00001764 /* Start keep-alive timer if necessary. */
1765 update_keep_alive(acc, PJ_TRUE, param);
1766
Benny Prijono8b6834f2007-02-24 13:29:22 +00001767 /* Send initial PUBLISH if it is enabled */
1768 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1769 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001770
1771 /* Subscribe to MWI, if it's enabled */
1772 if (acc->cfg.mwi_enabled)
1773 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001774 }
1775
1776 } else {
1777 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1778 }
1779
1780 acc->reg_last_err = param->status;
1781 acc->reg_last_code = param->code;
1782
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001783 /* Check if we need to auto retry registration. Basically, registration
1784 * failure codes triggering auto-retry are those of temporal failures
1785 * considered to be recoverable in relatively short term.
1786 */
1787 if (acc->cfg.reg_retry_interval &&
1788 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1789 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1790 param->code == PJSIP_SC_BAD_GATEWAY ||
1791 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1792 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1793 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1794 {
1795 schedule_reregistration(acc);
1796 }
1797
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001798 /* Call the registration status callback */
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00001799
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001800 if (pjsua_var.ua_cfg.cb.on_reg_state) {
1801 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1802 }
1803
1804 if (pjsua_var.ua_cfg.cb.on_reg_state2) {
1805 pjsua_reg_info reg_info;
1806
1807 reg_info.cbparam = param;
1808 (*pjsua_var.ua_cfg.cb.on_reg_state2)(acc->index, &reg_info);
1809 }
1810
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001811 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00001812 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001813}
1814
1815
1816/*
1817 * Initialize client registration.
1818 */
1819static pj_status_t pjsua_regc_init(int acc_id)
1820{
1821 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001822 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001823 pj_status_t status;
1824
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001825 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001826 acc = &pjsua_var.acc[acc_id];
1827
1828 if (acc->cfg.reg_uri.slen == 0) {
1829 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1830 return PJ_SUCCESS;
1831 }
1832
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001833 /* Destroy existing session, if any */
1834 if (acc->regc) {
1835 pjsip_regc_destroy(acc->regc);
1836 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001837 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001838 }
1839
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001840 /* initialize SIP registration if registrar is configured */
1841
1842 status = pjsip_regc_create( pjsua_var.endpt,
1843 acc, &regc_cb, &acc->regc);
1844
1845 if (status != PJ_SUCCESS) {
1846 pjsua_perror(THIS_FILE, "Unable to create client registration",
1847 status);
1848 return status;
1849 }
1850
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001851 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001852
1853 if (acc->contact.slen == 0) {
1854 pj_str_t tmp_contact;
1855
1856 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1857 acc_id, &acc->cfg.reg_uri);
1858 if (status != PJ_SUCCESS) {
1859 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1860 " for registration",
1861 status);
1862 pjsip_regc_destroy(acc->regc);
1863 pj_pool_release(pool);
1864 acc->regc = NULL;
1865 return status;
1866 }
1867
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001868 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonob54719f2010-11-16 03:07:46 +00001869 update_regc_contact(acc);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001870 }
1871
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001872 status = pjsip_regc_init( acc->regc,
1873 &acc->cfg.reg_uri,
1874 &acc->cfg.id,
1875 &acc->cfg.id,
Benny Prijonob54719f2010-11-16 03:07:46 +00001876 1, &acc->reg_contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001877 acc->cfg.reg_timeout);
1878 if (status != PJ_SUCCESS) {
1879 pjsua_perror(THIS_FILE,
1880 "Client registration initialization error",
1881 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001882 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001883 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001884 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001885 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001886 return status;
1887 }
1888
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001889 /* If account is locked to specific transport, then set transport to
1890 * the client registration.
1891 */
1892 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1893 pjsip_tpselector tp_sel;
1894
1895 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1896 pjsip_regc_set_transport(acc->regc, &tp_sel);
1897 }
1898
1899
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001900 /* Set credentials
1901 */
1902 if (acc->cred_cnt) {
1903 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1904 }
1905
Sauw Ming9b80d512011-03-15 03:20:37 +00001906 /* Set delay before registration refresh */
1907 pjsip_regc_set_delay_before_refresh(acc->regc,
1908 acc->cfg.reg_delay_before_refresh);
1909
Benny Prijono48ab2b72007-11-08 09:24:30 +00001910 /* Set authentication preference */
1911 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1912
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001913 /* Set route-set
1914 */
Benny Prijono29c8ca32010-06-22 06:02:13 +00001915 if (acc->cfg.reg_use_proxy) {
1916 pjsip_route_hdr route_set;
1917 const pjsip_route_hdr *r;
1918
1919 pj_list_init(&route_set);
1920
1921 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) {
1922 r = pjsua_var.outbound_proxy.next;
1923 while (r != &pjsua_var.outbound_proxy) {
1924 pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r));
1925 r = r->next;
1926 }
1927 }
1928
1929 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY &&
1930 acc->cfg.proxy_cnt)
1931 {
1932 int cnt = acc->cfg.proxy_cnt;
1933 pjsip_route_hdr *pos = route_set.prev;
1934 int i;
1935
1936 r = acc->route_set.prev;
1937 for (i=0; i<cnt; ++i) {
1938 pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r));
1939 r = r->prev;
1940 }
1941 }
1942
1943 if (!pj_list_empty(&route_set))
1944 pjsip_regc_set_route_set( acc->regc, &route_set );
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001945 }
1946
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +00001947 /* Add custom request headers specified in the account config */
1948 pjsip_regc_add_headers(acc->regc, &acc->cfg.reg_hdr_list);
1949
Benny Prijono8fc6de02006-11-11 21:25:55 +00001950 /* Add other request headers. */
1951 if (pjsua_var.ua_cfg.user_agent.slen) {
1952 pjsip_hdr hdr_list;
1953 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1954 pjsip_generic_string_hdr *h;
1955
Benny Prijono8fc6de02006-11-11 21:25:55 +00001956 pj_list_init(&hdr_list);
1957
1958 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1959 &pjsua_var.ua_cfg.user_agent);
1960 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1961
1962 pjsip_regc_add_headers(acc->regc, &hdr_list);
1963 }
1964
Benny Prijonob54719f2010-11-16 03:07:46 +00001965 /* If SIP outbound is used, add "Supported: outbound, path header" */
1966 if (acc->rfc5626_status == OUTBOUND_WANTED) {
1967 pjsip_hdr hdr_list;
1968 pjsip_supported_hdr *hsup;
1969
1970 pj_list_init(&hdr_list);
1971 hsup = pjsip_supported_hdr_create(pool);
1972 pj_list_push_back(&hdr_list, hsup);
1973
1974 hsup->count = 2;
1975 hsup->values[0] = pj_str("outbound");
1976 hsup->values[1] = pj_str("path");
1977
1978 pjsip_regc_add_headers(acc->regc, &hdr_list);
1979 }
1980
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001981 pj_pool_release(pool);
1982
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001983 return PJ_SUCCESS;
1984}
1985
1986
1987/*
1988 * Update registration or perform unregistration.
1989 */
1990PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1991 pj_bool_t renew)
1992{
1993 pj_status_t status = 0;
1994 pjsip_tx_data *tdata = 0;
1995
1996 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1997 PJ_EINVAL);
1998 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1999
Benny Prijonob90fd382011-09-18 14:59:56 +00002000 PJ_LOG(4,(THIS_FILE, "Acc %d: setting %sregistration..",
2001 acc_id, (renew? "" : "un")));
2002 pj_log_push_indent();
2003
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002004 PJSUA_LOCK();
2005
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002006 /* Cancel any re-registration timer */
2007 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
2008
2009 /* Reset pointer to registration transport */
2010 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
2011
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002012 if (renew) {
2013 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002014 status = pjsua_regc_init(acc_id);
2015 if (status != PJ_SUCCESS) {
2016 pjsua_perror(THIS_FILE, "Unable to create registration",
2017 status);
2018 goto on_return;
2019 }
2020 }
2021 if (!pjsua_var.acc[acc_id].regc) {
2022 status = PJ_EINVALIDOP;
2023 goto on_return;
2024 }
2025
2026 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
2027 &tdata);
2028
Benny Prijono28f673a2007-10-15 07:04:59 +00002029 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
2030 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2031 pjsip_authorization_hdr *h;
2032 char *uri;
2033 int d;
2034
2035 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
2036 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
2037 uri, acc->cfg.reg_uri.slen+10);
2038 pj_assert(d > 0);
2039
2040 h = pjsip_authorization_hdr_create(tdata->pool);
2041 h->scheme = pj_str("Digest");
2042 h->credential.digest.username = acc->cred[0].username;
2043 h->credential.digest.realm = acc->srv_domain;
2044 h->credential.digest.uri = pj_str(uri);
2045 h->credential.digest.algorithm = pj_str("md5");
2046
2047 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
2048 }
2049
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002050 } else {
2051 if (pjsua_var.acc[acc_id].regc == NULL) {
2052 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
2053 status = PJ_EINVALIDOP;
2054 goto on_return;
2055 }
Benny Prijono166d5022010-02-10 14:24:48 +00002056
2057 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
2058
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002059 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
2060 }
2061
Benny Prijono56315612006-07-18 14:39:40 +00002062 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00002063 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002064 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00002065 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002066
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002067 /* Update pointer to registration transport */
2068 if (status == PJ_SUCCESS) {
2069 pjsip_regc_info reg_info;
2070
2071 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
2072 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
2073 }
2074
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002075 if (status != PJ_SUCCESS) {
2076 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
2077 status);
2078 } else {
Benny Prijonob90fd382011-09-18 14:59:56 +00002079 PJ_LOG(4,(THIS_FILE, "Acc %d: %s sent", acc_id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002080 (renew? "Registration" : "Unregistration")));
2081 }
2082
2083on_return:
2084 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00002085 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002086 return status;
2087}
2088
2089
2090/*
2091 * Get account information.
2092 */
2093PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
2094 pjsua_acc_info *info)
2095{
2096 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2097 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
2098
2099 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002100 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002101
Benny Prijonoac623b32006-07-03 15:19:31 +00002102 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002103
2104 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
2105 PJ_EINVAL);
2106 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
2107
2108 PJSUA_LOCK();
2109
2110 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
2111 PJSUA_UNLOCK();
2112 return PJ_EINVALIDOP;
2113 }
2114
2115 info->id = acc_id;
2116 info->is_default = (pjsua_var.default_acc == acc_id);
2117 info->acc_uri = acc_cfg->id;
2118 info->has_registration = (acc->cfg.reg_uri.slen > 0);
2119 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00002120 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
2121 if (info->rpid.note.slen)
2122 info->online_status_text = info->rpid.note;
2123 else if (info->online_status)
2124 info->online_status_text = pj_str("Online");
2125 else
2126 info->online_status_text = pj_str("Offline");
2127
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002128 if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00002129 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002130 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002131 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002132 if (acc->reg_last_err)
2133 info->reg_last_err = acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002134 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002135 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002136 info->status_text = pj_str("not registered");
2137 }
2138 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002139 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002140 info->status_text = pj_str("In Progress");
2141 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002142 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002143 info->status_text = pj_str("does not register");
2144 }
2145
2146 if (acc->regc) {
2147 pjsip_regc_info regc_info;
2148 pjsip_regc_get_info(acc->regc, &regc_info);
2149 info->expires = regc_info.next_reg;
2150 } else {
2151 info->expires = -1;
2152 }
2153
2154 PJSUA_UNLOCK();
2155
2156 return PJ_SUCCESS;
2157
2158}
2159
2160
2161/*
2162 * Enum accounts all account ids.
2163 */
2164PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
2165 unsigned *count )
2166{
2167 unsigned i, c;
2168
2169 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
2170
2171 PJSUA_LOCK();
2172
2173 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2174 if (!pjsua_var.acc[i].valid)
2175 continue;
2176 ids[c] = i;
2177 ++c;
2178 }
2179
2180 *count = c;
2181
2182 PJSUA_UNLOCK();
2183
2184 return PJ_SUCCESS;
2185}
2186
2187
2188/*
2189 * Enum accounts info.
2190 */
2191PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
2192 unsigned *count )
2193{
2194 unsigned i, c;
2195
2196 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
2197
2198 PJSUA_LOCK();
2199
2200 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2201 if (!pjsua_var.acc[i].valid)
2202 continue;
2203
2204 pjsua_acc_get_info(i, &info[c]);
2205 ++c;
2206 }
2207
2208 *count = c;
2209
2210 PJSUA_UNLOCK();
2211
2212 return PJ_SUCCESS;
2213}
2214
2215
2216/*
2217 * This is an internal function to find the most appropriate account to
2218 * used to reach to the specified URL.
2219 */
2220PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
2221{
2222 pj_str_t tmp;
2223 pjsip_uri *uri;
2224 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002225 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00002226 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002227
2228 PJSUA_LOCK();
2229
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002230 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002231
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002232 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002233
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002234 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002235 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002236 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002237 PJSUA_UNLOCK();
2238 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002239 }
2240
2241 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2242 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2243 {
2244 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00002245 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2246 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002247 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00002248 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002249 break;
2250 }
2251
Benny Prijono093d3022006-09-24 00:07:11 +00002252 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002253 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002254 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002255 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002256 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002257 }
2258
2259 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002260 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002261 PJSUA_UNLOCK();
2262 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002263 }
2264
Benny Prijonoa1e69682007-05-11 15:14:34 +00002265 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002266
Benny Prijonob4a17c92006-07-10 14:40:21 +00002267 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00002268 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2269 unsigned acc_id = pjsua_var.acc_ids[i];
2270 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
2271 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
2272 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002273 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002274 PJSUA_UNLOCK();
2275 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00002276 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002277 }
2278
Benny Prijonob4a17c92006-07-10 14:40:21 +00002279 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00002280 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2281 unsigned acc_id = pjsua_var.acc_ids[i];
2282 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
2283 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002284 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002285 PJSUA_UNLOCK();
2286 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00002287 }
2288 }
2289
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002290
Benny Prijono093d3022006-09-24 00:07:11 +00002291 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002292 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002293 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00002294 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002295}
2296
2297
2298/*
2299 * This is an internal function to find the most appropriate account to be
2300 * used to handle incoming calls.
2301 */
2302PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
2303{
2304 pjsip_uri *uri;
2305 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00002306 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002307
Benny Prijono371cf0a2007-06-19 00:35:37 +00002308 /* Check that there's at least one account configured */
2309 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
2310
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002311 uri = rdata->msg_info.to->uri;
2312
2313 /* Just return default account if To URI is not SIP: */
2314 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2315 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2316 {
2317 return pjsua_var.default_acc;
2318 }
2319
2320
2321 PJSUA_LOCK();
2322
2323 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
2324
2325 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00002326 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2327 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002328 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2329
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002330 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00002331 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002332 {
2333 /* Match ! */
2334 PJSUA_UNLOCK();
2335 return acc_id;
2336 }
2337 }
2338
Benny Prijono093d3022006-09-24 00:07:11 +00002339 /* No matching account, try match domain part only. */
2340 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2341 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002342 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2343
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002344 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002345 /* Match ! */
2346 PJSUA_UNLOCK();
2347 return acc_id;
2348 }
2349 }
2350
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002351 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00002352 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2353 unsigned acc_id = pjsua_var.acc_ids[i];
2354 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2355
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002356 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
2357
2358 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2359 pjsip_transport_type_e type;
2360 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2361 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
2362 type = PJSIP_TRANSPORT_UDP;
2363
2364 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
2365 continue;
2366 }
2367
Benny Prijono093d3022006-09-24 00:07:11 +00002368 /* Match ! */
2369 PJSUA_UNLOCK();
2370 return acc_id;
2371 }
2372 }
2373
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002374 /* Still no match, use default account */
2375 PJSUA_UNLOCK();
2376 return pjsua_var.default_acc;
2377}
2378
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002379
Benny Prijonofff245c2007-04-02 11:44:47 +00002380/*
2381 * Create arbitrary requests for this account.
2382 */
2383PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2384 const pjsip_method *method,
2385 const pj_str_t *target,
2386 pjsip_tx_data **p_tdata)
2387{
2388 pjsip_tx_data *tdata;
2389 pjsua_acc *acc;
2390 pjsip_route_hdr *r;
2391 pj_status_t status;
2392
2393 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2394 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2395
2396 acc = &pjsua_var.acc[acc_id];
2397
2398 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2399 &acc->cfg.id, target,
2400 NULL, NULL, -1, NULL, &tdata);
2401 if (status != PJ_SUCCESS) {
2402 pjsua_perror(THIS_FILE, "Unable to create request", status);
2403 return status;
2404 }
2405
2406 /* Copy routeset */
2407 r = acc->route_set.next;
2408 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002409 pjsip_msg_add_hdr(tdata->msg,
2410 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002411 r = r->next;
2412 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002413
2414 /* If account is locked to specific transport, then set that transport to
2415 * the transmit data.
2416 */
2417 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2418 pjsip_tpselector tp_sel;
2419
2420 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2421 pjsip_tx_data_set_transport(tdata, &tp_sel);
2422 }
2423
Benny Prijonofff245c2007-04-02 11:44:47 +00002424 /* Done */
2425 *p_tdata = tdata;
2426 return PJ_SUCCESS;
2427}
2428
2429
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002430PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2431 pj_str_t *contact,
2432 pjsua_acc_id acc_id,
2433 const pj_str_t *suri)
2434{
2435 pjsua_acc *acc;
2436 pjsip_sip_uri *sip_uri;
2437 pj_status_t status;
2438 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2439 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002440 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002441 unsigned flag;
2442 int secure;
2443 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002444 const char *beginquote, *endquote;
2445 char transport_param[32];
Benny Prijonob54719f2010-11-16 03:07:46 +00002446 const char *ob = ";ob";
Benny Prijonod0bd4982007-12-02 15:40:52 +00002447
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002448
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002449 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002450 acc = &pjsua_var.acc[acc_id];
2451
Benny Prijonof75eceb2007-03-23 19:09:54 +00002452 /* If force_contact is configured, then use use it */
2453 if (acc->cfg.force_contact.slen) {
2454 *contact = acc->cfg.force_contact;
2455 return PJ_SUCCESS;
2456 }
2457
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002458 /* If route-set is configured for the account, then URI is the
2459 * first entry of the route-set.
2460 */
2461 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002462 sip_uri = (pjsip_sip_uri*)
2463 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002464 } else {
2465 pj_str_t tmp;
2466 pjsip_uri *uri;
2467
2468 pj_strdup_with_null(pool, &tmp, suri);
2469
2470 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2471 if (uri == NULL)
2472 return PJSIP_EINVALIDURI;
2473
2474 /* For non-SIP scheme, route set should be configured */
2475 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
Benny Prijonoc7545782010-09-28 07:43:18 +00002476 return PJSIP_ENOROUTESET;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002477
Benny Prijono8c7a6172007-02-18 21:17:46 +00002478 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002479 }
2480
2481 /* Get transport type of the URI */
2482 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2483 tp_type = PJSIP_TRANSPORT_TLS;
2484 else if (sip_uri->transport_param.slen == 0) {
2485 tp_type = PJSIP_TRANSPORT_UDP;
2486 } else
2487 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2488
2489 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2490 return PJSIP_EUNSUPTRANSPORT;
2491
Benny Prijonod0bd4982007-12-02 15:40:52 +00002492 /* If destination URI specifies IPv6, then set transport type
2493 * to use IPv6 as well.
2494 */
Benny Prijono19b29372007-12-05 04:08:40 +00002495 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002496 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2497
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002498 flag = pjsip_transport_get_flag_from_type(tp_type);
2499 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2500
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002501 /* Init transport selector. */
2502 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2503
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002504 /* Get local address suitable to send request from */
2505 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002506 pool, tp_type, &tp_sel,
2507 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002508 if (status != PJ_SUCCESS)
2509 return status;
2510
Benny Prijonod0bd4982007-12-02 15:40:52 +00002511 /* Enclose IPv6 address in square brackets */
2512 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2513 beginquote = "[";
2514 endquote = "]";
2515 } else {
2516 beginquote = endquote = "";
2517 }
2518
2519 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002520 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002521 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2522 ";transport=%s",
2523 pjsip_transport_get_type_name(tp_type));
2524 } else {
2525 transport_param[0] = '\0';
2526 }
2527
2528
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002529 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002530 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002531 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00002532 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s%s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002533 (int)acc->display.slen,
2534 acc->display.ptr,
2535 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002536 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002537 (int)acc->user_part.slen,
2538 acc->user_part.ptr,
2539 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002540 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002541 (int)local_addr.slen,
2542 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002543 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002544 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002545 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002546 (int)acc->cfg.contact_uri_params.slen,
2547 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00002548 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00002549 (int)acc->cfg.contact_params.slen,
2550 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002551
2552 return PJ_SUCCESS;
2553}
2554
2555
2556
2557PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2558 pj_str_t *contact,
2559 pjsua_acc_id acc_id,
2560 pjsip_rx_data *rdata )
2561{
2562 /*
2563 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2564 * If the request that initiated the dialog contained a SIPS URI
2565 * in the Request-URI or in the top Record-Route header field value,
2566 * if there was any, or the Contact header field if there was no
2567 * Record-Route header field, the Contact header field in the response
2568 * MUST be a SIPS URI.
2569 */
2570 pjsua_acc *acc;
2571 pjsip_sip_uri *sip_uri;
2572 pj_status_t status;
2573 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2574 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002575 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002576 unsigned flag;
2577 int secure;
2578 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002579 const char *beginquote, *endquote;
2580 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002581
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002582 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002583 acc = &pjsua_var.acc[acc_id];
2584
Benny Prijonof75eceb2007-03-23 19:09:54 +00002585 /* If force_contact is configured, then use use it */
2586 if (acc->cfg.force_contact.slen) {
2587 *contact = acc->cfg.force_contact;
2588 return PJ_SUCCESS;
2589 }
2590
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002591 /* If Record-Route is present, then URI is the top Record-Route. */
2592 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002593 sip_uri = (pjsip_sip_uri*)
2594 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002595 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002596 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002597 pjsip_contact_hdr *h_contact;
2598 pjsip_uri *uri = NULL;
2599
Benny Prijonoa330d452008-08-05 20:14:39 +00002600 /* Otherwise URI is Contact URI.
2601 * Iterate the Contact URI until we find sip: or sips: scheme.
2602 */
2603 do {
2604 h_contact = (pjsip_contact_hdr*)
2605 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2606 pos);
2607 if (h_contact) {
Benny Prijono8b33bba2010-06-02 03:03:43 +00002608 if (h_contact->uri)
2609 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2610 else
2611 uri = NULL;
2612 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2613 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
Benny Prijonoa330d452008-08-05 20:14:39 +00002614 {
2615 pos = (pjsip_hdr*)h_contact->next;
2616 if (pos == &rdata->msg_info.msg->hdr)
2617 h_contact = NULL;
2618 } else {
2619 break;
2620 }
2621 }
2622 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002623
2624
2625 /* Or if Contact URI is not present, take the remote URI from
2626 * the From URI.
2627 */
2628 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002629 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002630
2631
2632 /* Can only do sip/sips scheme at present. */
2633 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2634 return PJSIP_EINVALIDREQURI;
2635
Benny Prijono8c7a6172007-02-18 21:17:46 +00002636 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002637 }
2638
2639 /* Get transport type of the URI */
2640 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2641 tp_type = PJSIP_TRANSPORT_TLS;
2642 else if (sip_uri->transport_param.slen == 0) {
2643 tp_type = PJSIP_TRANSPORT_UDP;
2644 } else
2645 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002646
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002647 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2648 return PJSIP_EUNSUPTRANSPORT;
2649
Benny Prijonod0bd4982007-12-02 15:40:52 +00002650 /* If destination URI specifies IPv6, then set transport type
2651 * to use IPv6 as well.
2652 */
2653 if (pj_strchr(&sip_uri->host, ':'))
2654 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2655
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002656 flag = pjsip_transport_get_flag_from_type(tp_type);
2657 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2658
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002659 /* Init transport selector. */
2660 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2661
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002662 /* Get local address suitable to send request from */
2663 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002664 pool, tp_type, &tp_sel,
2665 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002666 if (status != PJ_SUCCESS)
2667 return status;
2668
Benny Prijonod0bd4982007-12-02 15:40:52 +00002669 /* Enclose IPv6 address in square brackets */
2670 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2671 beginquote = "[";
2672 endquote = "]";
2673 } else {
2674 beginquote = endquote = "";
2675 }
2676
2677 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002678 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002679 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2680 ";transport=%s",
2681 pjsip_transport_get_type_name(tp_type));
2682 } else {
2683 transport_param[0] = '\0';
2684 }
2685
2686
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002687 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002688 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002689 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002690 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002691 (int)acc->display.slen,
2692 acc->display.ptr,
2693 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002694 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002695 (int)acc->user_part.slen,
2696 acc->user_part.ptr,
2697 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002698 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002699 (int)local_addr.slen,
2700 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002701 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002702 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002703 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002704 (int)acc->cfg.contact_uri_params.slen,
2705 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002706 (int)acc->cfg.contact_params.slen,
2707 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002708
2709 return PJ_SUCCESS;
2710}
2711
2712
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002713PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2714 pjsua_transport_id tp_id)
2715{
2716 pjsua_acc *acc;
2717
2718 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2719 acc = &pjsua_var.acc[acc_id];
2720
Benny Prijonoa1e69682007-05-11 15:14:34 +00002721 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002722 PJ_EINVAL);
2723
2724 acc->cfg.transport_id = tp_id;
2725
2726 return PJ_SUCCESS;
2727}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002728
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002729
2730/* Auto re-registration timeout callback */
2731static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2732{
2733 pjsua_acc *acc;
2734 pj_status_t status;
2735
2736 PJ_UNUSED_ARG(th);
2737 acc = (pjsua_acc*) te->user_data;
2738 pj_assert(acc);
2739
2740 PJSUA_LOCK();
2741
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002742 /* Check if the reregistration timer is still valid, e.g: while waiting
2743 * timeout timer application might have deleted the account or disabled
2744 * the auto-reregistration.
2745 */
2746 if (!acc->valid || !acc->auto_rereg.active ||
2747 acc->cfg.reg_retry_interval == 0)
2748 {
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002749 goto on_return;
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002750 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002751
2752 /* Start re-registration */
2753 acc->auto_rereg.attempt_cnt++;
2754 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2755 if (status != PJ_SUCCESS)
2756 schedule_reregistration(acc);
2757
Nanang Izzuddin66580002010-04-14 08:12:08 +00002758on_return:
2759 PJSUA_UNLOCK();
2760}
2761
2762
2763/* Schedule reregistration for specified account. Note that the first
2764 * re-registration after a registration failure will be done immediately.
2765 * Also note that this function should be called within PJSUA mutex.
2766 */
2767static void schedule_reregistration(pjsua_acc *acc)
2768{
2769 pj_time_val delay;
2770
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002771 pj_assert(acc);
2772
2773 /* Validate the account and re-registration feature status */
2774 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
2775 return;
2776 }
Nanang Izzuddin66580002010-04-14 08:12:08 +00002777
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002778 /* If configured, disconnect calls of this account after the first
2779 * reregistration attempt failed.
2780 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002781 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002782 {
2783 unsigned i, cnt;
2784
2785 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2786 if (pjsua_var.calls[i].acc_id == acc->index) {
2787 pjsua_call_hangup(i, 0, NULL, NULL);
2788 ++cnt;
2789 }
2790 }
2791
2792 if (cnt) {
2793 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2794 "after reregistration attempt failed",
2795 cnt, acc->index));
2796 }
2797 }
2798
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002799 /* Cancel any re-registration timer */
2800 pjsua_cancel_timer(&acc->auto_rereg.timer);
2801
2802 /* Update re-registration flag */
2803 acc->auto_rereg.active = PJ_TRUE;
2804
2805 /* Set up timer for reregistration */
2806 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2807 acc->auto_rereg.timer.user_data = acc;
2808
2809 /* Reregistration attempt. The first attempt will be done immediately. */
2810 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2811 delay.msec = 0;
2812 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2813}
2814
2815
2816/* Internal function to perform auto-reregistration on transport
2817 * connection/disconnection events.
2818 */
2819void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2820 pjsip_transport_state state,
2821 const pjsip_transport_state_info *info)
2822{
2823 unsigned i;
2824
2825 PJ_UNUSED_ARG(info);
2826
2827 /* Only care for transport disconnection events */
2828 if (state != PJSIP_TP_STATE_DISCONNECTED)
2829 return;
2830
Benny Prijonob90fd382011-09-18 14:59:56 +00002831 PJ_LOG(4,(THIS_FILE, "Disconnected notification for transport %s",
2832 tp->obj_name));
2833 pj_log_push_indent();
2834
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002835 /* Shutdown this transport, to make sure that the transport manager
2836 * will create a new transport for reconnection.
2837 */
2838 pjsip_transport_shutdown(tp);
2839
2840 PJSUA_LOCK();
2841
2842 /* Enumerate accounts using this transport and perform actions
2843 * based on the transport state.
2844 */
2845 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2846 pjsua_acc *acc = &pjsua_var.acc[i];
2847
2848 /* Skip if this account is not valid OR auto re-registration
2849 * feature is disabled OR this transport is not used by this account.
2850 */
2851 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2852 tp != acc->auto_rereg.reg_tp)
2853 {
2854 continue;
2855 }
2856
2857 /* Schedule reregistration for this account */
2858 schedule_reregistration(acc);
2859 }
2860
2861 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00002862 pj_log_pop_indent();
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002863}