blob: 6947c1aa61beb88fef66461a5c742ee1e0192f94 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjsua-lib/pjsua.h>
21#include <pjsua-lib/pjsua_internal.h>
22
23
24#define THIS_FILE "pjsua_acc.c"
25
Benny Prijonob54719f2010-11-16 03:07:46 +000026enum
27{
Benny Prijono2562b752010-11-16 06:01:38 +000028 OUTBOUND_UNKNOWN, // status unknown
29 OUTBOUND_WANTED, // initiated in registration
30 OUTBOUND_ACTIVE, // got positive response from server
31 OUTBOUND_NA // not wanted or got negative response from server
Benny Prijonob54719f2010-11-16 03:07:46 +000032};
33
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +000035static void schedule_reregistration(pjsua_acc *acc);
36
Benny Prijonoeebe9af2006-06-13 22:57:13 +000037/*
38 * Get number of current accounts.
39 */
40PJ_DEF(unsigned) pjsua_acc_get_count(void)
41{
42 return pjsua_var.acc_cnt;
43}
44
45
46/*
47 * Check if the specified account ID is valid.
48 */
49PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
50{
Benny Prijonoa1e69682007-05-11 15:14:34 +000051 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052 pjsua_var.acc[acc_id].valid;
53}
54
55
56/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000057 * Set default account
58 */
59PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
60{
61 pjsua_var.default_acc = acc_id;
62 return PJ_SUCCESS;
63}
64
65
66/*
67 * Get default account.
68 */
69PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
70{
71 return pjsua_var.default_acc;
72}
73
74
75/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000076 * Copy account configuration.
77 */
Benny Prijonobddef2c2007-10-31 13:28:08 +000078PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
79 pjsua_acc_config *dst,
80 const pjsua_acc_config *src)
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081{
82 unsigned i;
83
84 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
85
86 pj_strdup_with_null(pool, &dst->id, &src->id);
87 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000088 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +000089 pj_strdup_with_null(pool, &dst->contact_params, &src->contact_params);
90 pj_strdup_with_null(pool, &dst->contact_uri_params,
91 &src->contact_uri_params);
Benny Prijonofe04fb52007-08-24 08:28:52 +000092 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +000093 pj_strdup_with_null(pool, &dst->rfc5626_instance_id,
94 &src->rfc5626_instance_id);
Benny Prijonob54719f2010-11-16 03:07:46 +000095 pj_strdup_with_null(pool, &dst->rfc5626_reg_id, &src->rfc5626_reg_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000096
97 dst->proxy_cnt = src->proxy_cnt;
98 for (i=0; i<src->proxy_cnt; ++i)
99 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
100
101 dst->reg_timeout = src->reg_timeout;
Sauw Ming9b80d512011-03-15 03:20:37 +0000102 dst->reg_delay_before_refresh = src->reg_delay_before_refresh;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103 dst->cred_count = src->cred_count;
104
105 for (i=0; i<src->cred_count; ++i) {
106 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
107 }
Benny Prijonobddef2c2007-10-31 13:28:08 +0000108
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +0000109 pj_list_init(&dst->reg_hdr_list);
110 if (!pj_list_empty(&src->reg_hdr_list)) {
111 const pjsip_hdr *hdr;
112
113 hdr = src->reg_hdr_list.next;
114 while (hdr != &src->reg_hdr_list) {
115 pj_list_push_back(&dst->reg_hdr_list, pjsip_hdr_clone(pool, hdr));
116 hdr = hdr->next;
117 }
118 }
119
Benny Prijono639873e2011-03-23 03:46:26 +0000120 pj_list_init(&dst->sub_hdr_list);
121 if (!pj_list_empty(&src->sub_hdr_list)) {
122 const pjsip_hdr *hdr;
123
124 hdr = src->sub_hdr_list.next;
125 while (hdr != &src->sub_hdr_list) {
126 pj_list_push_back(&dst->sub_hdr_list, pjsip_hdr_clone(pool, hdr));
127 hdr = hdr->next;
128 }
129 }
130
Nanang Izzuddin5fc7fcf2010-12-01 08:53:52 +0000131 pjsip_auth_clt_pref_dup(pool, &dst->auth_pref, &src->auth_pref);
132
Benny Prijono0bc99a92011-03-17 04:34:43 +0000133 pjsua_transport_config_dup(pool, &dst->rtp_cfg, &src->rtp_cfg);
134
Benny Prijonobddef2c2007-10-31 13:28:08 +0000135 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000136}
137
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000138/*
139 * Calculate CRC of proxy list.
140 */
141static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt)
142{
143 pj_crc32_context ctx;
144 unsigned i;
145
146 pj_crc32_init(&ctx);
147 for (i=0; i<cnt; ++i) {
148 pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen);
149 }
150
151 return pj_crc32_final(&ctx);
152}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000153
154/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000155 * Initialize a new account (after configuration is set).
156 */
157static pj_status_t initialize_acc(unsigned acc_id)
158{
159 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
160 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000161 pjsip_name_addr *name_addr;
Benny Prijonoc7545782010-09-28 07:43:18 +0000162 pjsip_sip_uri *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000163 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000164 unsigned i;
165
166 /* Need to parse local_uri to get the elements: */
167
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000168 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000169 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000170 acc_cfg->id.slen,
171 PJSIP_PARSE_URI_AS_NAMEADDR);
172 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173 pjsua_perror(THIS_FILE, "Invalid local URI",
174 PJSIP_EINVALIDURI);
175 return PJSIP_EINVALIDURI;
176 }
177
178 /* Local URI MUST be a SIP or SIPS: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000179 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
180 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181 {
Benny Prijonoc7545782010-09-28 07:43:18 +0000182 acc->display = name_addr->display;
183 acc->user_part = name_addr->display;
184 acc->srv_domain = pj_str("");
185 acc->srv_port = 0;
186 } else {
187 pjsip_sip_uri *sip_uri;
188
189 /* Get the SIP URI object: */
190 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
191
192 /* Save the user and domain part. These will be used when finding an
193 * account for incoming requests.
194 */
195 acc->display = name_addr->display;
196 acc->user_part = sip_uri->user;
197 acc->srv_domain = sip_uri->host;
198 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 }
200
201
Benny Prijonob4a17c92006-07-10 14:40:21 +0000202 /* Parse registrar URI, if any */
203 if (acc_cfg->reg_uri.slen) {
204 pjsip_uri *reg_uri;
205
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000206 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000207 acc_cfg->reg_uri.slen, 0);
208 if (reg_uri == NULL) {
209 pjsua_perror(THIS_FILE, "Invalid registrar URI",
210 PJSIP_EINVALIDURI);
211 return PJSIP_EINVALIDURI;
212 }
213
214 /* Registrar URI MUST be a SIP or SIPS: */
215 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
216 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
217 {
218 pjsua_perror(THIS_FILE, "Invalid registar URI",
219 PJSIP_EINVALIDSCHEME);
220 return PJSIP_EINVALIDSCHEME;
221 }
222
223 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
224
225 } else {
226 sip_reg_uri = NULL;
227 }
228
Benny Prijonob4a17c92006-07-10 14:40:21 +0000229 if (sip_reg_uri) {
230 acc->srv_port = sip_reg_uri->port;
231 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000232
233 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000234 //if (acc_cfg->contact.slen == 0) {
235 // acc_cfg->contact = acc_cfg->id;
236 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000238 /* Build account route-set from outbound proxies and route set from
239 * account configuration.
240 */
241 pj_list_init(&acc->route_set);
242
Benny Prijono29c8ca32010-06-22 06:02:13 +0000243 if (!pj_list_empty(&pjsua_var.outbound_proxy)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244 pjsip_route_hdr *r;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000245
Benny Prijono29c8ca32010-06-22 06:02:13 +0000246 r = pjsua_var.outbound_proxy.next;
247 while (r != &pjsua_var.outbound_proxy) {
248 pj_list_push_back(&acc->route_set,
249 pjsip_hdr_shallow_clone(acc->pool, r));
250 r = r->next;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000251 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252 }
253
254 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
255 pj_str_t hname = { "Route", 5};
256 pjsip_route_hdr *r;
257 pj_str_t tmp;
258
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000259 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000260 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000261 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000262 if (r == NULL) {
263 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
264 PJ_EINVAL);
265 return PJ_EINVAL;
266 }
267 pj_list_push_back(&acc->route_set, r);
268 }
269
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270 /* Concatenate credentials from account config and global config */
271 acc->cred_cnt = 0;
272 for (i=0; i<acc_cfg->cred_count; ++i) {
273 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
274 }
275 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
276 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
277 {
278 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
279 }
280
Benny Prijono07fe2302010-06-24 12:33:18 +0000281 /* If ICE is enabled, add "+sip.ice" media feature tag in account's
282 * contact params.
283 */
284#if PJSUA_ADD_ICE_TAGS
285 if (pjsua_var.media_cfg.enable_ice) {
286 unsigned new_len;
287 pj_str_t new_prm;
288
289 new_len = acc_cfg->contact_params.slen + 10;
290 new_prm.ptr = (char*)pj_pool_alloc(acc->pool, new_len);
291 pj_strcpy(&new_prm, &acc_cfg->contact_params);
292 pj_strcat2(&new_prm, ";+sip.ice");
293 acc_cfg->contact_params = new_prm;
294 }
295#endif
296
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000297 status = pjsua_pres_init_acc(acc_id);
298 if (status != PJ_SUCCESS)
299 return status;
300
Benny Prijonob54719f2010-11-16 03:07:46 +0000301 /* If SIP outbound is enabled, generate instance and reg ID if they are
302 * not specified
303 */
304 if (acc_cfg->use_rfc5626) {
305 if (acc_cfg->rfc5626_instance_id.slen==0) {
306 const pj_str_t *hostname;
307 pj_uint32_t hval, pos;
308 char instprm[] = ";+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000CCDDEEFF>\"";
309
310 hostname = pj_gethostname();
311 pos = pj_ansi_strlen(instprm) - 10;
312 hval = pj_hash_calc(0, hostname->ptr, hostname->slen);
313 pj_val_to_hex_digit( ((char*)&hval)[0], instprm+pos+0);
314 pj_val_to_hex_digit( ((char*)&hval)[1], instprm+pos+2);
315 pj_val_to_hex_digit( ((char*)&hval)[2], instprm+pos+4);
316 pj_val_to_hex_digit( ((char*)&hval)[3], instprm+pos+6);
317
318 pj_strdup2(acc->pool, &acc->rfc5626_instprm, instprm);
319 } else {
320 const char *prmname = ";+sip.instance=\"";
321 unsigned len;
322
323 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_instance_id.slen + 1;
324 acc->rfc5626_instprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
325 pj_ansi_snprintf(acc->rfc5626_instprm.ptr, len+1,
326 "%s%.*s\"",
327 prmname,
328 (int)acc_cfg->rfc5626_instance_id.slen,
329 acc_cfg->rfc5626_instance_id.ptr);
330 acc->rfc5626_instprm.slen = len;
331 }
332
333 if (acc_cfg->rfc5626_reg_id.slen==0) {
334 acc->rfc5626_regprm = pj_str(";reg-id=1");
335 } else {
336 const char *prmname = ";reg-id=";
337 unsigned len;
338
339 len = pj_ansi_strlen(prmname) + acc_cfg->rfc5626_reg_id.slen;
340 acc->rfc5626_regprm.ptr = (char*)pj_pool_alloc(acc->pool, len+1);
341 pj_ansi_snprintf(acc->rfc5626_regprm.ptr, len+1,
342 "%s%.*s\"",
343 prmname,
344 (int)acc_cfg->rfc5626_reg_id.slen,
345 acc_cfg->rfc5626_reg_id.ptr);
346 acc->rfc5626_regprm.slen = len;
347 }
348 }
349
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000350 /* Mark account as valid */
351 pjsua_var.acc[acc_id].valid = PJ_TRUE;
352
Benny Prijono093d3022006-09-24 00:07:11 +0000353 /* Insert account ID into account ID array, sorted by priority */
354 for (i=0; i<pjsua_var.acc_cnt; ++i) {
355 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
356 pjsua_var.acc[acc_id].cfg.priority)
357 {
358 break;
359 }
360 }
361 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
362 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363
364 return PJ_SUCCESS;
365}
366
367
368/*
369 * Add a new account to pjsua.
370 */
371PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
372 pj_bool_t is_default,
373 pjsua_acc_id *p_acc_id)
374{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000375 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000376 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377 pj_status_t status;
378
379 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
380 PJ_ETOOMANY);
381
382 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000383 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384
385 PJSUA_LOCK();
386
387 /* Find empty account id. */
388 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
389 if (pjsua_var.acc[id].valid == PJ_FALSE)
390 break;
391 }
392
393 /* Expect to find a slot */
394 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
395 {PJSUA_UNLOCK(); return PJ_EBUG;});
396
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000397 acc = &pjsua_var.acc[id];
398
399 /* Create pool for this account. */
400 if (acc->pool)
401 pj_pool_reset(acc->pool);
402 else
403 acc->pool = pjsua_pool_create("acc%p", 512, 256);
404
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000405 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000406 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000407
Sauw Ming9b80d512011-03-15 03:20:37 +0000408 /* Normalize registration timeout and refresh delay */
409 if (pjsua_var.acc[id].cfg.reg_uri.slen) {
410 if (pjsua_var.acc[id].cfg.reg_timeout == 0) {
411 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
412 }
413 if (pjsua_var.acc[id].cfg.reg_delay_before_refresh == 0) {
414 pjsua_var.acc[id].cfg.reg_delay_before_refresh =
415 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
416 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 }
418
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000419 /* Get CRC of account proxy setting */
420 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
421
422 /* Get CRC of global outbound proxy setting */
423 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
424 pjsua_var.ua_cfg.outbound_proxy_cnt);
425
Benny Prijono91d06b62008-09-20 12:16:56 +0000426 /* Check the route URI's and force loose route if required */
427 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
428 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
429 if (status != PJ_SUCCESS)
430 return status;
431 }
432
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000433 status = initialize_acc(id);
434 if (status != PJ_SUCCESS) {
435 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000436 pj_pool_release(acc->pool);
437 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000438 PJSUA_UNLOCK();
439 return status;
440 }
441
442 if (is_default)
443 pjsua_var.default_acc = id;
444
445 if (p_acc_id)
446 *p_acc_id = id;
447
448 pjsua_var.acc_cnt++;
449
450 PJSUA_UNLOCK();
451
452 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
453 (int)cfg->id.slen, cfg->id.ptr, id));
454
455 /* If accounts has registration enabled, start registration */
456 if (pjsua_var.acc[id].cfg.reg_uri.slen)
457 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000458 else {
459 /* Otherwise subscribe to MWI, if it's enabled */
460 if (pjsua_var.acc[id].cfg.mwi_enabled)
461 pjsua_start_mwi(&pjsua_var.acc[id]);
462 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000463
464 return PJ_SUCCESS;
465}
466
467
468/*
469 * Add local account
470 */
471PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
472 pj_bool_t is_default,
473 pjsua_acc_id *p_acc_id)
474{
475 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000476 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000477 const char *beginquote, *endquote;
478 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000479 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000480
Benny Prijonoe93e2872006-06-28 16:46:49 +0000481 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000482 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
483 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000484
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000485 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000486 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487
488 pjsua_acc_config_default(&cfg);
489
Benny Prijono093d3022006-09-24 00:07:11 +0000490 /* Lower the priority of local account */
491 --cfg.priority;
492
Benny Prijonod0bd4982007-12-02 15:40:52 +0000493 /* Enclose IPv6 address in square brackets */
494 if (t->type & PJSIP_TRANSPORT_IPV6) {
495 beginquote = "[";
496 endquote = "]";
497 } else {
498 beginquote = endquote = "";
499 }
500
501 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000502 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000503 pj_ansi_snprintf(transport_param, sizeof(transport_param),
504 ";transport=%s",
505 pjsip_transport_get_type_name(t->type));
506 } else {
507 transport_param[0] = '\0';
508 }
509
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000511 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000512 "<sip:%s%.*s%s:%d%s>",
513 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000514 (int)t->local_name.host.slen,
515 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000516 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000517 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000518 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519
520 cfg.id = pj_str(uri);
521
522 return pjsua_acc_add(&cfg, is_default, p_acc_id);
523}
524
525
526/*
Benny Prijono705e7842008-07-21 18:12:51 +0000527 * Set arbitrary data to be associated with the account.
528 */
529PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
530 void *user_data)
531{
532 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
533 PJ_EINVAL);
534 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
535
536 PJSUA_LOCK();
537
538 pjsua_var.acc[acc_id].cfg.user_data = user_data;
539
540 PJSUA_UNLOCK();
541
542 return PJ_SUCCESS;
543}
544
545
546/*
547 * Retrieve arbitrary data associated with the account.
548 */
549PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
550{
551 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
552 NULL);
553 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
554
555 return pjsua_var.acc[acc_id].cfg.user_data;
556}
557
558
559/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000560 * Delete account.
561 */
562PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
563{
Benny Prijono093d3022006-09-24 00:07:11 +0000564 unsigned i;
565
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000566 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
567 PJ_EINVAL);
568 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
569
570 PJSUA_LOCK();
571
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000572 /* Cancel any re-registration timer */
573 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
574
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000575 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000576 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000577 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000578 if (pjsua_var.acc[acc_id].regc) {
579 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
580 }
Benny Prijono922933b2007-01-21 16:23:56 +0000581 pjsua_var.acc[acc_id].regc = NULL;
582 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000583
584 /* Delete server presence subscription */
585 pjsua_pres_delete_acc(acc_id);
586
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000587 /* Release account pool */
588 if (pjsua_var.acc[acc_id].pool) {
589 pj_pool_release(pjsua_var.acc[acc_id].pool);
590 pjsua_var.acc[acc_id].pool = NULL;
591 }
592
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000593 /* Invalidate */
594 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000595 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000596
Benny Prijono093d3022006-09-24 00:07:11 +0000597 /* Remove from array */
598 for (i=0; i<pjsua_var.acc_cnt; ++i) {
599 if (pjsua_var.acc_ids[i] == acc_id)
600 break;
601 }
602 if (i != pjsua_var.acc_cnt) {
603 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
604 pjsua_var.acc_cnt, i);
605 --pjsua_var.acc_cnt;
606 }
607
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000608 /* Leave the calls intact, as I don't think calls need to
609 * access account once it's created
610 */
611
Benny Prijonofb2b3652007-06-28 07:15:03 +0000612 /* Update default account */
613 if (pjsua_var.default_acc == acc_id)
614 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000615
616 PJSUA_UNLOCK();
617
618 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
619
620 return PJ_SUCCESS;
621}
622
623
624/*
625 * Modify account information.
626 */
627PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
628 const pjsua_acc_config *cfg)
629{
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000630 pjsua_acc *acc;
631 pjsip_name_addr *id_name_addr = NULL;
632 pjsip_sip_uri *id_sip_uri = NULL;
633 pjsip_sip_uri *reg_sip_uri = NULL;
634 pj_uint32_t local_route_crc, global_route_crc;
635 pjsip_route_hdr global_route;
636 pjsip_route_hdr local_route;
637 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
638 pj_bool_t update_reg = PJ_FALSE;
639 pj_status_t status = PJ_SUCCESS;
640
641 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
642 PJ_EINVAL);
643
644 PJSUA_LOCK();
645
646 acc = &pjsua_var.acc[acc_id];
647 if (!acc->valid) {
648 status = PJ_EINVAL;
649 goto on_return;
650 }
651
652 /* == Validate first == */
653
654 /* Account id */
655 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
656 /* Need to parse id to get the elements: */
657 id_name_addr = (pjsip_name_addr*)
658 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
659 PJSIP_PARSE_URI_AS_NAMEADDR);
660 if (id_name_addr == NULL) {
661 status = PJSIP_EINVALIDURI;
662 pjsua_perror(THIS_FILE, "Invalid local URI", status);
663 goto on_return;
664 }
665
666 /* URI MUST be a SIP or SIPS: */
667 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
668 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
669 {
670 status = PJSIP_EINVALIDSCHEME;
671 pjsua_perror(THIS_FILE, "Invalid local URI", status);
672 goto on_return;
673 }
674
675 /* Get the SIP URI object: */
676 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
677 }
678
679 /* Registrar URI */
680 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
681 pjsip_uri *reg_uri;
682
683 /* Need to parse reg_uri to get the elements: */
684 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
685 cfg->reg_uri.slen, 0);
686 if (reg_uri == NULL) {
687 status = PJSIP_EINVALIDURI;
688 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
689 goto on_return;
690 }
691
692 /* Registrar URI MUST be a SIP or SIPS: */
693 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
694 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
695 {
696 status = PJSIP_EINVALIDSCHEME;
697 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
698 goto on_return;
699 }
700
701 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
702 }
703
704 /* Global outbound proxy */
705 global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
706 pjsua_var.ua_cfg.outbound_proxy_cnt);
707 if (global_route_crc != acc->global_route_crc) {
708 pjsip_route_hdr *r;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000709
Benny Prijono29c8ca32010-06-22 06:02:13 +0000710 /* Copy from global outbound proxies */
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000711 pj_list_init(&global_route);
Benny Prijono29c8ca32010-06-22 06:02:13 +0000712 r = pjsua_var.outbound_proxy.next;
713 while (r != &pjsua_var.outbound_proxy) {
714 pj_list_push_back(&global_route,
715 pjsip_hdr_shallow_clone(acc->pool, r));
716 r = r->next;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000717 }
718 }
719
720 /* Account proxy */
721 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
722 if (local_route_crc != acc->local_route_crc) {
723 pjsip_route_hdr *r;
724 unsigned i;
725
726 /* Validate the local route and save it to temporary var */
727 pj_list_init(&local_route);
728 for (i=0; i<cfg->proxy_cnt; ++i) {
729 pj_str_t hname = { "Route", 5};
730
731 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
732 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
733 if (status != PJ_SUCCESS)
734 goto on_return;
735 r = (pjsip_route_hdr*)
736 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
737 acc_proxy[i].slen, NULL);
738 if (r == NULL) {
739 status = PJSIP_EINVALIDURI;
740 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
741 status);
742 goto on_return;
743 }
744
745 pj_list_push_back(&local_route, r);
746 }
747 }
748
749
750 /* == Apply the new config == */
751
752 /* Account ID. */
753 if (id_name_addr && id_sip_uri) {
754 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
Benny Prijonof0ba2dc2011-04-14 15:38:23 +0000755 pj_strdup_with_null(acc->pool, &acc->display, &id_name_addr->display);
756 pj_strdup_with_null(acc->pool, &acc->user_part, &id_sip_uri->user);
757 pj_strdup_with_null(acc->pool, &acc->srv_domain, &id_sip_uri->host);
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000758 acc->srv_port = 0;
759 update_reg = PJ_TRUE;
760 }
761
762 /* User data */
763 acc->cfg.user_data = cfg->user_data;
764
765 /* Priority */
766 if (acc->cfg.priority != cfg->priority) {
767 unsigned i;
768
769 acc->cfg.priority = cfg->priority;
770
771 /* Resort accounts priority */
772 for (i=0; i<pjsua_var.acc_cnt; ++i) {
773 if (pjsua_var.acc_ids[i] == acc_id)
774 break;
775 }
776 pj_assert(i < pjsua_var.acc_cnt);
777 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
778 pjsua_var.acc_cnt, i);
779 for (i=0; i<pjsua_var.acc_cnt; ++i) {
780 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
781 acc->cfg.priority)
782 {
783 break;
784 }
785 }
786 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
787 pjsua_var.acc_cnt, i, &acc_id);
788 }
789
790 /* MWI */
791 acc->cfg.mwi_enabled = cfg->mwi_enabled;
792
793 /* PIDF tuple ID */
794 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
795 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
796 &cfg->pidf_tuple_id);
797
798 /* Publish */
799 acc->cfg.publish_opt = cfg->publish_opt;
800 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
801 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
802 acc->cfg.publish_enabled = cfg->publish_enabled;
803 if (!acc->cfg.publish_enabled)
804 pjsua_pres_unpublish(acc);
805 else
806 update_reg = PJ_TRUE;
807 }
808
809 /* Force contact URI */
810 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
811 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
812 &cfg->force_contact);
813 update_reg = PJ_TRUE;
814 }
815
816 /* Contact param */
817 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
818 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
819 &cfg->contact_params);
820 update_reg = PJ_TRUE;
821 }
822
823 /* Contact URI params */
824 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
825 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
826 &cfg->contact_uri_params);
827 update_reg = PJ_TRUE;
828 }
829
830 /* Reliable provisional response */
831 acc->cfg.require_100rel = cfg->require_100rel;
832
833 /* Session timer */
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000834 acc->cfg.use_timer = cfg->use_timer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000835 acc->cfg.timer_setting = cfg->timer_setting;
836
837 /* Transport and keep-alive */
838 if (acc->cfg.transport_id != cfg->transport_id) {
839 acc->cfg.transport_id = cfg->transport_id;
840 update_reg = PJ_TRUE;
841 }
842 acc->cfg.ka_interval = cfg->ka_interval;
843 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
844 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
845#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
846 acc->cfg.use_srtp = cfg->use_srtp;
847 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000848 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000849#endif
850
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000851#if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
852 acc->cfg.use_stream_ka = cfg->use_stream_ka;
853#endif
854
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000855 /* Global outbound proxy */
856 if (global_route_crc != acc->global_route_crc) {
857 unsigned i, rcnt;
858
859 /* Remove the outbound proxies from the route set */
860 rcnt = pj_list_size(&acc->route_set);
861 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
862 pjsip_route_hdr *r = acc->route_set.next;
863 pj_list_erase(r);
864 }
865
866 /* Insert the outbound proxies to the beginning of route set */
867 pj_list_merge_first(&acc->route_set, &global_route);
868
869 /* Update global route CRC */
870 acc->global_route_crc = global_route_crc;
871
872 update_reg = PJ_TRUE;
873 }
874
875 /* Account proxy */
876 if (local_route_crc != acc->local_route_crc) {
877 unsigned i;
878
879 /* Remove the current account proxies from the route set */
880 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
881 pjsip_route_hdr *r = acc->route_set.prev;
882 pj_list_erase(r);
883 }
884
885 /* Insert new proxy setting to the route set */
886 pj_list_merge_last(&acc->route_set, &local_route);
887
888 /* Update the proxy setting */
889 acc->cfg.proxy_cnt = cfg->proxy_cnt;
890 for (i = 0; i < cfg->proxy_cnt; ++i)
891 acc->cfg.proxy[i] = acc_proxy[i];
892
893 /* Update local route CRC */
894 acc->local_route_crc = local_route_crc;
895
896 update_reg = PJ_TRUE;
897 }
898
899 /* Credential info */
900 {
901 unsigned i;
902
903 /* Selective update credential info. */
904 for (i = 0; i < cfg->cred_count; ++i) {
905 unsigned j;
906 pjsip_cred_info ci;
907
908 /* Find if this credential is already listed */
Nanang Izzuddin7f8d76f2011-03-29 03:47:16 +0000909 for (j = i; j < acc->cfg.cred_count; ++j) {
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000910 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
911 &cfg->cred_info[i]) == 0)
912 {
913 /* Found, but different index/position, swap */
914 if (j != i) {
915 ci = acc->cfg.cred_info[i];
916 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
917 acc->cfg.cred_info[j] = ci;
918 }
919 break;
920 }
921 }
922
923 /* Not found, insert this */
924 if (j == acc->cfg.cred_count) {
925 /* If account credential is full, discard the last one. */
926 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
927 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
928 acc->cfg.cred_count, acc->cfg.cred_count-1);
929 acc->cfg.cred_count--;
930 }
931
932 /* Insert this */
933 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
934 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
935 acc->cfg.cred_count, i, &ci);
936 }
937 }
938 acc->cfg.cred_count = cfg->cred_count;
939
940 /* Concatenate credentials from account config and global config */
941 acc->cred_cnt = 0;
942 for (i=0; i<acc->cfg.cred_count; ++i) {
943 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
944 }
945 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
946 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
947 {
948 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
949 }
950 }
951
952 /* Authentication preference */
953 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
954 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
955 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
956 &cfg->auth_pref.algorithm);
957
958 /* Registration */
959 acc->cfg.reg_timeout = cfg->reg_timeout;
960 acc->cfg.unreg_timeout = cfg->unreg_timeout;
961 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
962 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
963 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
Sauw Ming8ad06c52011-03-15 10:49:59 +0000964 if (acc->cfg.reg_delay_before_refresh != cfg->reg_delay_before_refresh) {
965 acc->cfg.reg_delay_before_refresh = cfg->reg_delay_before_refresh;
966 pjsip_regc_set_delay_before_refresh(acc->regc,
967 cfg->reg_delay_before_refresh);
968 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000969
Sauw Ming9b80d512011-03-15 03:20:37 +0000970 /* Normalize registration timeout and refresh delay */
971 if (acc->cfg.reg_uri.slen ) {
972 if (acc->cfg.reg_timeout == 0) {
973 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
974 }
975 if (acc->cfg.reg_delay_before_refresh == 0) {
976 acc->cfg.reg_delay_before_refresh =
977 PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH;
978 }
979 }
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000980
981 /* Registrar URI */
982 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
983 if (cfg->reg_uri.slen) {
984 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
985 if (reg_sip_uri)
986 acc->srv_port = reg_sip_uri->port;
987 } else {
988 /* Unregister if registration was set */
989 if (acc->cfg.reg_uri.slen)
990 pjsua_acc_set_registration(acc->index, PJ_FALSE);
991 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
992 }
993 update_reg = PJ_TRUE;
994 }
995
Benny Prijonob54719f2010-11-16 03:07:46 +0000996 /* SIP outbound setting */
997 if (acc->cfg.use_rfc5626 != cfg->use_rfc5626 ||
998 pj_strcmp(&acc->cfg.rfc5626_instance_id, &cfg->rfc5626_instance_id) ||
999 pj_strcmp(&acc->cfg.rfc5626_reg_id, &cfg->rfc5626_reg_id))
1000 {
1001 update_reg = PJ_TRUE;
1002 }
1003
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +00001004 /* Update registration */
1005 if (update_reg) {
1006 /* If accounts has registration enabled, start registration */
1007 if (acc->cfg.reg_uri.slen)
1008 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1009 else {
1010 /* Otherwise subscribe to MWI, if it's enabled */
1011 if (acc->cfg.mwi_enabled)
1012 pjsua_start_mwi(acc);
1013 }
1014 }
1015
1016on_return:
1017 PJSUA_UNLOCK();
1018 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001019}
1020
1021
1022/*
1023 * Modify account's presence status to be advertised to remote/presence
1024 * subscribers.
1025 */
1026PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
1027 pj_bool_t is_online)
1028{
1029 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1030 PJ_EINVAL);
1031 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1032
1033 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001034 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
1035 pjsua_pres_update_acc(acc_id, PJ_FALSE);
1036 return PJ_SUCCESS;
1037}
1038
1039
1040/*
1041 * Set online status with extended information
1042 */
1043PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
1044 pj_bool_t is_online,
1045 const pjrpid_element *pr)
1046{
1047 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1048 PJ_EINVAL);
1049 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1050
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001051 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +00001052 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001053 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
1054 PJSUA_UNLOCK();
1055
Benny Prijono4461c7d2007-08-25 13:36:15 +00001056 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001057 return PJ_SUCCESS;
1058}
1059
Benny Prijonob54719f2010-11-16 03:07:46 +00001060/* Create reg_contact, mainly for SIP outbound */
1061static void update_regc_contact(pjsua_acc *acc)
1062{
1063 pjsua_acc_config *acc_cfg = &acc->cfg;
1064 pj_bool_t need_outbound = PJ_FALSE;
1065 const pj_str_t tcp_param = pj_str(";transport=tcp");
1066 const pj_str_t tls_param = pj_str(";transport=tls");
1067
1068 if (!acc_cfg->use_rfc5626)
1069 goto done;
1070
Benny Prijono2562b752010-11-16 06:01:38 +00001071 /* Check if outbound has been requested and rejected */
1072 if (acc->rfc5626_status == OUTBOUND_NA)
1073 goto done;
1074
Benny Prijonob54719f2010-11-16 03:07:46 +00001075 if (pj_stristr(&acc->contact, &tcp_param)==NULL &&
1076 pj_stristr(&acc->contact, &tls_param)==NULL)
1077 {
1078 /* Currently we can only do SIP outbound for TCP
1079 * and TLS.
1080 */
1081 goto done;
1082 }
1083
1084 /* looks like we can use outbound */
1085 need_outbound = PJ_TRUE;
1086
1087done:
1088 if (!need_outbound) {
1089 /* Outbound is not needed/wanted for the account. acc->reg_contact
1090 * is set to the same as acc->contact.
1091 */
1092 acc->reg_contact = acc->contact;
Benny Prijono2562b752010-11-16 06:01:38 +00001093 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001094 } else {
1095 /* Need to use outbound, append the contact with +sip.instance and
1096 * reg-id parameters.
1097 */
1098 unsigned len;
1099 pj_str_t reg_contact;
1100
1101 acc->rfc5626_status = OUTBOUND_WANTED;
1102 len = acc->contact.slen + acc->rfc5626_instprm.slen +
1103 acc->rfc5626_regprm.slen;
Benny Prijonoe49e6202010-11-16 07:01:25 +00001104 reg_contact.ptr = (char*) pj_pool_alloc(acc->pool, len);
Benny Prijonob54719f2010-11-16 03:07:46 +00001105
1106 pj_strcpy(&reg_contact, &acc->contact);
1107 pj_strcat(&reg_contact, &acc->rfc5626_regprm);
1108 pj_strcat(&reg_contact, &acc->rfc5626_instprm);
1109
1110 acc->reg_contact = reg_contact;
1111
1112 PJ_LOG(4,(THIS_FILE,
1113 "Contact for acc %d updated for SIP outbound: %.*s",
1114 acc->index,
1115 (int)acc->reg_contact.slen,
1116 acc->reg_contact.ptr));
1117 }
1118}
1119
Benny Prijono7f630432008-09-24 16:52:41 +00001120/* Check if IP is private IP address */
1121static pj_bool_t is_private_ip(const pj_str_t *addr)
1122{
1123 const pj_str_t private_net[] =
1124 {
1125 { "10.", 3 },
1126 { "127.", 4 },
1127 { "172.16.", 7 },
1128 { "192.168.", 8 }
1129 };
1130 unsigned i;
1131
1132 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
1133 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
1134 return PJ_TRUE;
1135 }
1136
1137 return PJ_FALSE;
1138}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001139
Benny Prijono15b02302007-09-27 14:07:07 +00001140/* Update NAT address from the REGISTER response */
1141static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
1142 struct pjsip_regc_cbparam *param)
1143{
1144 pjsip_transport *tp;
1145 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001146 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +00001147 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001148 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +00001149 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +00001150 pj_sockaddr contact_addr;
1151 pj_sockaddr recv_addr;
1152 pj_status_t status;
1153 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +00001154 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001155 pjsip_contact_hdr *contact_hdr;
1156 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +00001157
1158 tp = param->rdata->tp_info.transport;
1159
1160 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001161 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +00001162 return PJ_FALSE;
1163
Benny Prijonob54719f2010-11-16 03:07:46 +00001164 /* If SIP outbound is active, no need to update */
1165 if (acc->rfc5626_status == OUTBOUND_ACTIVE) {
1166 PJ_LOG(4,(THIS_FILE, "Acc %d has SIP outbound active, no need to "
1167 "update registration Contact", acc->index));
1168 return PJ_FALSE;
1169 }
1170
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001171#if 0
1172 // Always update
1173 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +00001174
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001175 /* For UDP, only update if STUN is enabled (for now).
1176 * For TCP/TLS, always check.
1177 */
1178 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
1179 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
1180 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
1181 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
1182 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +00001183 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001184 /* Yes we will check */
1185 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001186 return PJ_FALSE;
1187 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001188#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001189
1190 /* Get the received and rport info */
1191 via = param->rdata->msg_info.via;
1192 if (via->rport_param < 1) {
1193 /* Remote doesn't support rport */
1194 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001195 if (rport==0) {
1196 pjsip_transport_type_e tp_type;
1197 tp_type = (pjsip_transport_type_e) tp->key.type;
1198 rport = pjsip_transport_get_default_port_for_type(tp_type);
1199 }
Benny Prijono15b02302007-09-27 14:07:07 +00001200 } else
1201 rport = via->rport_param;
1202
1203 if (via->recvd_param.slen != 0)
1204 via_addr = &via->recvd_param;
1205 else
1206 via_addr = &via->sent_by.host;
1207
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001208 /* Compare received and rport with the URI in our registration */
1209 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001210 contact_hdr = (pjsip_contact_hdr*)
1211 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1212 acc->contact.slen, NULL);
1213 pj_assert(contact_hdr != NULL);
1214 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001215 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001216 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001217
Benny Prijono24a21852008-04-14 04:04:30 +00001218 if (uri->port == 0) {
1219 pjsip_transport_type_e tp_type;
1220 tp_type = (pjsip_transport_type_e) tp->key.type;
1221 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1222 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001223
Benny Prijono84d24932009-06-04 15:51:39 +00001224 /* Convert IP address strings into sockaddr for comparison.
1225 * (http://trac.pjsip.org/repos/ticket/863)
1226 */
1227 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1228 &contact_addr);
1229 if (status == PJ_SUCCESS)
1230 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1231 &recv_addr);
1232 if (status == PJ_SUCCESS) {
1233 /* Compare the addresses as sockaddr according to the ticket above */
1234 matched = (uri->port == rport &&
1235 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1236 } else {
1237 /* Compare the addresses as string, as before */
1238 matched = (uri->port == rport &&
1239 pj_stricmp(&uri->host, via_addr)==0);
1240 }
1241
1242 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001243 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001244 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001245 return PJ_FALSE;
1246 }
1247
Benny Prijono7f630432008-09-24 16:52:41 +00001248 /* Get server IP */
1249 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1250
Benny Prijono15b02302007-09-27 14:07:07 +00001251 /* At this point we've detected that the address as seen by registrar.
1252 * has changed.
1253 */
Benny Prijono7f630432008-09-24 16:52:41 +00001254
1255 /* Do not switch if both Contact and server's IP address are
1256 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001257 * might have messed up with the SIP packets. See:
1258 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001259 *
1260 * This exception can be disabled by setting allow_contact_rewrite
1261 * to 2. In this case, the switch will always be done whenever there
1262 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001263 */
Benny Prijono247921b2008-09-26 22:06:11 +00001264 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1265 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001266 {
1267 /* Don't switch */
1268 pj_pool_release(pool);
1269 return PJ_FALSE;
1270 }
1271
Benny Prijono4f933762009-11-10 03:45:42 +00001272 /* Also don't switch if only the port number part is different, and
1273 * the Via received address is private.
1274 * See http://trac.pjsip.org/repos/ticket/864
1275 */
1276 if (acc->cfg.allow_contact_rewrite != 2 &&
1277 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1278 is_private_ip(via_addr))
1279 {
1280 /* Don't switch */
1281 pj_pool_release(pool);
1282 return PJ_FALSE;
1283 }
1284
Benny Prijono15b02302007-09-27 14:07:07 +00001285 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001286 "(%.*s:%d --> %.*s:%d). Updating registration "
1287 "(using method %d)",
Benny Prijono15b02302007-09-27 14:07:07 +00001288 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001289 (int)uri->host.slen,
1290 uri->host.ptr,
1291 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001292 (int)via_addr->slen,
1293 via_addr->ptr,
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001294 rport,
1295 acc->cfg.contact_rewrite_method));
Benny Prijono15b02302007-09-27 14:07:07 +00001296
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001297 pj_assert(acc->cfg.contact_rewrite_method == 1 ||
1298 acc->cfg.contact_rewrite_method == 2);
1299
1300 if (acc->cfg.contact_rewrite_method == 1) {
1301 /* Unregister current contact */
1302 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1303 if (acc->regc != NULL) {
1304 pjsip_regc_destroy(acc->regc);
1305 acc->regc = NULL;
1306 acc->contact.slen = 0;
1307 }
Benny Prijono15b02302007-09-27 14:07:07 +00001308 }
1309
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001310 /*
1311 * Build new Contact header
1312 */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001313 {
Benny Prijonob54719f2010-11-16 03:07:46 +00001314 const char *ob = ";ob";
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001315 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001316 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001317 int len;
1318
Benny Prijono8972bf02009-04-05 18:30:45 +00001319 /* Enclose IPv6 address in square brackets */
1320 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1321 beginquote = "[";
1322 endquote = "]";
1323 } else {
1324 beginquote = endquote = "";
1325 }
1326
Benny Prijono24a21852008-04-14 04:04:30 +00001327 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001328 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00001329 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s%s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001330 (int)acc->user_part.slen,
1331 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001332 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001333 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001334 (int)via_addr->slen,
1335 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001336 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001337 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001338 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001339 (int)acc->cfg.contact_uri_params.slen,
1340 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00001341 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00001342 (int)acc->cfg.contact_params.slen,
1343 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001344 if (len < 1) {
1345 PJ_LOG(1,(THIS_FILE, "URI too long"));
1346 pj_pool_release(pool);
1347 return PJ_FALSE;
1348 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001349 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001350
Benny Prijonob54719f2010-11-16 03:07:46 +00001351 update_regc_contact(acc);
1352
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001353 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1354 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1355 tp->local_name.port = rport;
1356
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001357 }
1358
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001359 if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001360 pjsip_regc_update_contact(acc->regc, 1, &acc->reg_contact);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001361 }
Benny Prijono15b02302007-09-27 14:07:07 +00001362
1363 /* Perform new registration */
1364 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1365
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001366 pj_pool_release(pool);
1367
Benny Prijono15b02302007-09-27 14:07:07 +00001368 return PJ_TRUE;
1369}
1370
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001371/* Check and update Service-Route header */
1372void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1373{
1374 pjsip_generic_string_hdr *hsr = NULL;
1375 pjsip_route_hdr *hr, *h;
1376 const pj_str_t HNAME = { "Service-Route", 13 };
1377 const pj_str_t HROUTE = { "Route", 5 };
1378 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001379 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001380
1381 /* Find and parse Service-Route headers */
1382 for (;;) {
1383 char saved;
1384 int parsed_len;
1385
1386 /* Find Service-Route header */
1387 hsr = (pjsip_generic_string_hdr*)
1388 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1389 if (!hsr)
1390 break;
1391
1392 /* Parse as Route header since the syntax is similar. This may
1393 * return more than one headers.
1394 */
1395 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1396 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1397 hr = (pjsip_route_hdr*)
1398 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1399 hsr->hvalue.slen, &parsed_len);
1400 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1401
1402 if (hr == NULL) {
1403 /* Error */
1404 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1405 return;
1406 }
1407
1408 /* Save each URI in the result */
1409 h = hr;
1410 do {
1411 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1412 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1413 {
1414 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1415 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1416 return;
1417 }
1418
1419 uri[uri_cnt++] = h->name_addr.uri;
1420 h = h->next;
1421 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1422
1423 if (h != hr) {
1424 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1425 return;
1426 }
1427
1428 /* Prepare to find next Service-Route header */
1429 hsr = hsr->next;
1430 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1431 break;
1432 }
1433
1434 if (uri_cnt == 0)
1435 return;
1436
1437 /*
1438 * Update account's route set
1439 */
1440
1441 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001442 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001443 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1444 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1445 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001446 i<rcnt;
1447 ++i)
1448 {
1449 pjsip_route_hdr *prev = hr->prev;
1450 pj_list_erase(hr);
1451 hr = prev;
1452 }
1453 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001454
1455 /* Then append the Service-Route URIs */
1456 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001457 hr = pjsip_route_hdr_create(acc->pool);
1458 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001459 pj_list_push_back(&acc->route_set, hr);
1460 }
1461
1462 /* Done */
1463
1464 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1465 acc->index, uri_cnt));
1466}
1467
Benny Prijonobddef2c2007-10-31 13:28:08 +00001468
1469/* Keep alive timer callback */
1470static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1471{
1472 pjsua_acc *acc;
1473 pjsip_tpselector tp_sel;
1474 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001475 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001476 pj_status_t status;
1477
1478 PJ_UNUSED_ARG(th);
1479
1480 PJSUA_LOCK();
1481
1482 te->id = PJ_FALSE;
1483
1484 acc = (pjsua_acc*) te->user_data;
1485
1486 /* Select the transport to send the packet */
1487 pj_bzero(&tp_sel, sizeof(tp_sel));
1488 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1489 tp_sel.u.transport = acc->ka_transport;
1490
1491 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001492 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001493 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001494 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001495
1496 /* Send raw packet */
1497 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1498 PJSIP_TRANSPORT_UDP, &tp_sel,
1499 NULL, acc->cfg.ka_data.ptr,
1500 acc->cfg.ka_data.slen,
1501 &acc->ka_target, acc->ka_target_len,
1502 NULL, NULL);
1503
1504 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1505 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1506 }
1507
1508 /* Reschedule next timer */
1509 delay.sec = acc->cfg.ka_interval;
1510 delay.msec = 0;
1511 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1512 if (status == PJ_SUCCESS) {
1513 te->id = PJ_TRUE;
1514 } else {
1515 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1516 }
1517
1518 PJSUA_UNLOCK();
1519}
1520
1521
1522/* Update keep-alive for the account */
1523static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1524 struct pjsip_regc_cbparam *param)
1525{
1526 /* In all cases, stop keep-alive timer if it's running. */
1527 if (acc->ka_timer.id) {
1528 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1529 acc->ka_timer.id = PJ_FALSE;
1530
1531 pjsip_transport_dec_ref(acc->ka_transport);
1532 acc->ka_transport = NULL;
1533 }
1534
1535 if (start) {
1536 pj_time_val delay;
1537 pj_status_t status;
1538
1539 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001540 * - ka_interval is not zero in the account, and
1541 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001542 *
1543 * Previously we only enabled keep-alive when STUN is enabled, since
1544 * we thought that keep-alive is only needed in Internet situation.
1545 * But it has been discovered that Windows Firewall on WinXP also
1546 * needs to be kept-alive, otherwise incoming packets will be dropped.
1547 * So because of this, now keep-alive is always enabled for UDP,
1548 * regardless of whether STUN is enabled or not.
1549 *
1550 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1551 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001552 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001553 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001554 acc->cfg.ka_interval == 0 ||
1555 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1556 {
1557 /* Keep alive is not necessary */
1558 return;
1559 }
1560
1561 /* Save transport and destination address. */
1562 acc->ka_transport = param->rdata->tp_info.transport;
1563 pjsip_transport_add_ref(acc->ka_transport);
1564 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1565 param->rdata->pkt_info.src_addr_len);
1566 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1567
1568 /* Setup and start the timer */
1569 acc->ka_timer.cb = &keep_alive_timer_cb;
1570 acc->ka_timer.user_data = (void*)acc;
1571
1572 delay.sec = acc->cfg.ka_interval;
1573 delay.msec = 0;
1574 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1575 &delay);
1576 if (status == PJ_SUCCESS) {
1577 acc->ka_timer.id = PJ_TRUE;
1578 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1579 "destination:%s:%d, interval:%ds",
1580 acc->index,
1581 param->rdata->pkt_info.src_name,
1582 param->rdata->pkt_info.src_port,
1583 acc->cfg.ka_interval));
1584 } else {
1585 acc->ka_timer.id = PJ_FALSE;
1586 pjsip_transport_dec_ref(acc->ka_transport);
1587 acc->ka_transport = NULL;
1588 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1589 }
1590 }
1591}
1592
1593
Benny Prijonob54719f2010-11-16 03:07:46 +00001594/* Update the status of SIP outbound registration request */
1595static void update_rfc5626_status(pjsua_acc *acc, pjsip_rx_data *rdata)
1596{
1597 pjsip_require_hdr *hreq;
1598 const pj_str_t STR_OUTBOUND = {"outbound", 8};
1599 unsigned i;
1600
Benny Prijono2562b752010-11-16 06:01:38 +00001601 if (acc->rfc5626_status == OUTBOUND_UNKNOWN) {
Benny Prijonob54719f2010-11-16 03:07:46 +00001602 goto on_return;
1603 }
1604
1605 hreq = rdata->msg_info.require;
1606 if (!hreq) {
Benny Prijono2562b752010-11-16 06:01:38 +00001607 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001608 goto on_return;
1609 }
1610
1611 for (i=0; i<hreq->count; ++i) {
1612 if (pj_stricmp(&hreq->values[i], &STR_OUTBOUND)==0) {
1613 acc->rfc5626_status = OUTBOUND_ACTIVE;
1614 goto on_return;
1615 }
1616 }
1617
1618 /* Server does not support outbound */
Benny Prijono2562b752010-11-16 06:01:38 +00001619 acc->rfc5626_status = OUTBOUND_NA;
Benny Prijonob54719f2010-11-16 03:07:46 +00001620
1621on_return:
Benny Prijono2562b752010-11-16 06:01:38 +00001622 if (acc->rfc5626_status != OUTBOUND_ACTIVE) {
1623 acc->reg_contact = acc->contact;
1624 }
Benny Prijonob54719f2010-11-16 03:07:46 +00001625 PJ_LOG(4,(THIS_FILE, "SIP outbound status for acc %d is %s",
1626 acc->index, (acc->rfc5626_status==OUTBOUND_ACTIVE?
1627 "active": "not active")));
1628}
1629
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001630/*
1631 * This callback is called by pjsip_regc when outgoing register
1632 * request has completed.
1633 */
1634static void regc_cb(struct pjsip_regc_cbparam *param)
1635{
1636
Benny Prijonoa1e69682007-05-11 15:14:34 +00001637 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001638
Benny Prijono15b02302007-09-27 14:07:07 +00001639 if (param->regc != acc->regc)
1640 return;
1641
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001642 PJSUA_LOCK();
1643
1644 /*
1645 * Print registration status.
1646 */
1647 if (param->status!=PJ_SUCCESS) {
1648 pjsua_perror(THIS_FILE, "SIP registration error",
1649 param->status);
1650 pjsip_regc_destroy(acc->regc);
1651 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001652 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001653
Benny Prijonobddef2c2007-10-31 13:28:08 +00001654 /* Stop keep-alive timer if any. */
1655 update_keep_alive(acc, PJ_FALSE, NULL);
1656
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001657 } else if (param->code < 0 || param->code >= 300) {
1658 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1659 param->code,
1660 (int)param->reason.slen, param->reason.ptr));
1661 pjsip_regc_destroy(acc->regc);
1662 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001663 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001664
Benny Prijonobddef2c2007-10-31 13:28:08 +00001665 /* Stop keep-alive timer if any. */
1666 update_keep_alive(acc, PJ_FALSE, NULL);
1667
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001668 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1669
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001670 /* Update auto registration flag */
1671 acc->auto_rereg.active = PJ_FALSE;
1672 acc->auto_rereg.attempt_cnt = 0;
1673
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001674 if (param->expiration < 1) {
1675 pjsip_regc_destroy(acc->regc);
1676 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001677 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001678
1679 /* Stop keep-alive timer if any. */
1680 update_keep_alive(acc, PJ_FALSE, NULL);
1681
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001682 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1683 pjsua_var.acc[acc->index].cfg.id.ptr));
1684 } else {
Benny Prijonob54719f2010-11-16 03:07:46 +00001685 /* Check and update SIP outbound status first, since the result
1686 * will determine if we should update re-registration
1687 */
1688 update_rfc5626_status(acc, param->rdata);
1689
Benny Prijono15b02302007-09-27 14:07:07 +00001690 /* Check NAT bound address */
1691 if (acc_check_nat_addr(acc, param)) {
Benny Prijono15b02302007-09-27 14:07:07 +00001692 PJSUA_UNLOCK();
1693 return;
1694 }
1695
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001696 /* Check and update Service-Route header */
1697 update_service_route(acc, param->rdata);
1698
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001699 PJ_LOG(3, (THIS_FILE,
1700 "%s: registration success, status=%d (%.*s), "
1701 "will re-register in %d seconds",
1702 pjsua_var.acc[acc->index].cfg.id.ptr,
1703 param->code,
1704 (int)param->reason.slen, param->reason.ptr,
1705 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001706
Benny Prijonobddef2c2007-10-31 13:28:08 +00001707 /* Start keep-alive timer if necessary. */
1708 update_keep_alive(acc, PJ_TRUE, param);
1709
Benny Prijono8b6834f2007-02-24 13:29:22 +00001710 /* Send initial PUBLISH if it is enabled */
1711 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1712 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001713
1714 /* Subscribe to MWI, if it's enabled */
1715 if (acc->cfg.mwi_enabled)
1716 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001717 }
1718
1719 } else {
1720 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1721 }
1722
1723 acc->reg_last_err = param->status;
1724 acc->reg_last_code = param->code;
1725
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001726 /* Check if we need to auto retry registration. Basically, registration
1727 * failure codes triggering auto-retry are those of temporal failures
1728 * considered to be recoverable in relatively short term.
1729 */
1730 if (acc->cfg.reg_retry_interval &&
1731 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1732 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1733 param->code == PJSIP_SC_BAD_GATEWAY ||
1734 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1735 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1736 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1737 {
1738 schedule_reregistration(acc);
1739 }
1740
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001741 /* Call the registration status callback */
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00001742
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001743 if (pjsua_var.ua_cfg.cb.on_reg_state) {
1744 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1745 }
1746
1747 if (pjsua_var.ua_cfg.cb.on_reg_state2) {
1748 pjsua_reg_info reg_info;
1749
1750 reg_info.cbparam = param;
1751 (*pjsua_var.ua_cfg.cb.on_reg_state2)(acc->index, &reg_info);
1752 }
1753
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001754 PJSUA_UNLOCK();
1755}
1756
1757
1758/*
1759 * Initialize client registration.
1760 */
1761static pj_status_t pjsua_regc_init(int acc_id)
1762{
1763 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001764 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001765 pj_status_t status;
1766
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001767 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001768 acc = &pjsua_var.acc[acc_id];
1769
1770 if (acc->cfg.reg_uri.slen == 0) {
1771 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1772 return PJ_SUCCESS;
1773 }
1774
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001775 /* Destroy existing session, if any */
1776 if (acc->regc) {
1777 pjsip_regc_destroy(acc->regc);
1778 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001779 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001780 }
1781
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001782 /* initialize SIP registration if registrar is configured */
1783
1784 status = pjsip_regc_create( pjsua_var.endpt,
1785 acc, &regc_cb, &acc->regc);
1786
1787 if (status != PJ_SUCCESS) {
1788 pjsua_perror(THIS_FILE, "Unable to create client registration",
1789 status);
1790 return status;
1791 }
1792
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001793 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001794
1795 if (acc->contact.slen == 0) {
1796 pj_str_t tmp_contact;
1797
1798 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1799 acc_id, &acc->cfg.reg_uri);
1800 if (status != PJ_SUCCESS) {
1801 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1802 " for registration",
1803 status);
1804 pjsip_regc_destroy(acc->regc);
1805 pj_pool_release(pool);
1806 acc->regc = NULL;
1807 return status;
1808 }
1809
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001810 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonob54719f2010-11-16 03:07:46 +00001811 update_regc_contact(acc);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001812 }
1813
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001814 status = pjsip_regc_init( acc->regc,
1815 &acc->cfg.reg_uri,
1816 &acc->cfg.id,
1817 &acc->cfg.id,
Benny Prijonob54719f2010-11-16 03:07:46 +00001818 1, &acc->reg_contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001819 acc->cfg.reg_timeout);
1820 if (status != PJ_SUCCESS) {
1821 pjsua_perror(THIS_FILE,
1822 "Client registration initialization error",
1823 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001824 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001825 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001826 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001827 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001828 return status;
1829 }
1830
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001831 /* If account is locked to specific transport, then set transport to
1832 * the client registration.
1833 */
1834 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1835 pjsip_tpselector tp_sel;
1836
1837 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1838 pjsip_regc_set_transport(acc->regc, &tp_sel);
1839 }
1840
1841
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001842 /* Set credentials
1843 */
1844 if (acc->cred_cnt) {
1845 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1846 }
1847
Sauw Ming9b80d512011-03-15 03:20:37 +00001848 /* Set delay before registration refresh */
1849 pjsip_regc_set_delay_before_refresh(acc->regc,
1850 acc->cfg.reg_delay_before_refresh);
1851
Benny Prijono48ab2b72007-11-08 09:24:30 +00001852 /* Set authentication preference */
1853 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1854
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001855 /* Set route-set
1856 */
Benny Prijono29c8ca32010-06-22 06:02:13 +00001857 if (acc->cfg.reg_use_proxy) {
1858 pjsip_route_hdr route_set;
1859 const pjsip_route_hdr *r;
1860
1861 pj_list_init(&route_set);
1862
1863 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) {
1864 r = pjsua_var.outbound_proxy.next;
1865 while (r != &pjsua_var.outbound_proxy) {
1866 pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r));
1867 r = r->next;
1868 }
1869 }
1870
1871 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY &&
1872 acc->cfg.proxy_cnt)
1873 {
1874 int cnt = acc->cfg.proxy_cnt;
1875 pjsip_route_hdr *pos = route_set.prev;
1876 int i;
1877
1878 r = acc->route_set.prev;
1879 for (i=0; i<cnt; ++i) {
1880 pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r));
1881 r = r->prev;
1882 }
1883 }
1884
1885 if (!pj_list_empty(&route_set))
1886 pjsip_regc_set_route_set( acc->regc, &route_set );
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001887 }
1888
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +00001889 /* Add custom request headers specified in the account config */
1890 pjsip_regc_add_headers(acc->regc, &acc->cfg.reg_hdr_list);
1891
Benny Prijono8fc6de02006-11-11 21:25:55 +00001892 /* Add other request headers. */
1893 if (pjsua_var.ua_cfg.user_agent.slen) {
1894 pjsip_hdr hdr_list;
1895 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1896 pjsip_generic_string_hdr *h;
1897
Benny Prijono8fc6de02006-11-11 21:25:55 +00001898 pj_list_init(&hdr_list);
1899
1900 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1901 &pjsua_var.ua_cfg.user_agent);
1902 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1903
1904 pjsip_regc_add_headers(acc->regc, &hdr_list);
1905 }
1906
Benny Prijonob54719f2010-11-16 03:07:46 +00001907 /* If SIP outbound is used, add "Supported: outbound, path header" */
1908 if (acc->rfc5626_status == OUTBOUND_WANTED) {
1909 pjsip_hdr hdr_list;
1910 pjsip_supported_hdr *hsup;
1911
1912 pj_list_init(&hdr_list);
1913 hsup = pjsip_supported_hdr_create(pool);
1914 pj_list_push_back(&hdr_list, hsup);
1915
1916 hsup->count = 2;
1917 hsup->values[0] = pj_str("outbound");
1918 hsup->values[1] = pj_str("path");
1919
1920 pjsip_regc_add_headers(acc->regc, &hdr_list);
1921 }
1922
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001923 pj_pool_release(pool);
1924
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001925 return PJ_SUCCESS;
1926}
1927
1928
1929/*
1930 * Update registration or perform unregistration.
1931 */
1932PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1933 pj_bool_t renew)
1934{
1935 pj_status_t status = 0;
1936 pjsip_tx_data *tdata = 0;
1937
1938 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1939 PJ_EINVAL);
1940 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1941
1942 PJSUA_LOCK();
1943
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001944 /* Cancel any re-registration timer */
1945 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1946
1947 /* Reset pointer to registration transport */
1948 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1949
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001950 if (renew) {
1951 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001952 status = pjsua_regc_init(acc_id);
1953 if (status != PJ_SUCCESS) {
1954 pjsua_perror(THIS_FILE, "Unable to create registration",
1955 status);
1956 goto on_return;
1957 }
1958 }
1959 if (!pjsua_var.acc[acc_id].regc) {
1960 status = PJ_EINVALIDOP;
1961 goto on_return;
1962 }
1963
1964 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1965 &tdata);
1966
Benny Prijono28f673a2007-10-15 07:04:59 +00001967 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1968 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1969 pjsip_authorization_hdr *h;
1970 char *uri;
1971 int d;
1972
1973 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1974 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1975 uri, acc->cfg.reg_uri.slen+10);
1976 pj_assert(d > 0);
1977
1978 h = pjsip_authorization_hdr_create(tdata->pool);
1979 h->scheme = pj_str("Digest");
1980 h->credential.digest.username = acc->cred[0].username;
1981 h->credential.digest.realm = acc->srv_domain;
1982 h->credential.digest.uri = pj_str(uri);
1983 h->credential.digest.algorithm = pj_str("md5");
1984
1985 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1986 }
1987
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001988 } else {
1989 if (pjsua_var.acc[acc_id].regc == NULL) {
1990 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1991 status = PJ_EINVALIDOP;
1992 goto on_return;
1993 }
Benny Prijono166d5022010-02-10 14:24:48 +00001994
1995 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
1996
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001997 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1998 }
1999
Benny Prijono56315612006-07-18 14:39:40 +00002000 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00002001 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002002 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00002003 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002004
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002005 /* Update pointer to registration transport */
2006 if (status == PJ_SUCCESS) {
2007 pjsip_regc_info reg_info;
2008
2009 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
2010 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
2011 }
2012
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002013 if (status != PJ_SUCCESS) {
2014 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
2015 status);
2016 } else {
2017 PJ_LOG(3,(THIS_FILE, "%s sent",
2018 (renew? "Registration" : "Unregistration")));
2019 }
2020
2021on_return:
2022 PJSUA_UNLOCK();
2023 return status;
2024}
2025
2026
2027/*
2028 * Get account information.
2029 */
2030PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
2031 pjsua_acc_info *info)
2032{
2033 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2034 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
2035
2036 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002037 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002038
Benny Prijonoac623b32006-07-03 15:19:31 +00002039 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002040
2041 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
2042 PJ_EINVAL);
2043 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
2044
2045 PJSUA_LOCK();
2046
2047 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
2048 PJSUA_UNLOCK();
2049 return PJ_EINVALIDOP;
2050 }
2051
2052 info->id = acc_id;
2053 info->is_default = (pjsua_var.default_acc == acc_id);
2054 info->acc_uri = acc_cfg->id;
2055 info->has_registration = (acc->cfg.reg_uri.slen > 0);
2056 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00002057 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
2058 if (info->rpid.note.slen)
2059 info->online_status_text = info->rpid.note;
2060 else if (info->online_status)
2061 info->online_status_text = pj_str("Online");
2062 else
2063 info->online_status_text = pj_str("Offline");
2064
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002065 if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00002066 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002067 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002068 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
Sauw Ming48f6dbf2010-09-07 05:10:25 +00002069 if (acc->reg_last_err)
2070 info->reg_last_err = acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002071 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002072 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002073 info->status_text = pj_str("not registered");
2074 }
2075 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002076 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002077 info->status_text = pj_str("In Progress");
2078 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00002079 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002080 info->status_text = pj_str("does not register");
2081 }
2082
2083 if (acc->regc) {
2084 pjsip_regc_info regc_info;
2085 pjsip_regc_get_info(acc->regc, &regc_info);
2086 info->expires = regc_info.next_reg;
2087 } else {
2088 info->expires = -1;
2089 }
2090
2091 PJSUA_UNLOCK();
2092
2093 return PJ_SUCCESS;
2094
2095}
2096
2097
2098/*
2099 * Enum accounts all account ids.
2100 */
2101PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
2102 unsigned *count )
2103{
2104 unsigned i, c;
2105
2106 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
2107
2108 PJSUA_LOCK();
2109
2110 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2111 if (!pjsua_var.acc[i].valid)
2112 continue;
2113 ids[c] = i;
2114 ++c;
2115 }
2116
2117 *count = c;
2118
2119 PJSUA_UNLOCK();
2120
2121 return PJ_SUCCESS;
2122}
2123
2124
2125/*
2126 * Enum accounts info.
2127 */
2128PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
2129 unsigned *count )
2130{
2131 unsigned i, c;
2132
2133 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
2134
2135 PJSUA_LOCK();
2136
2137 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2138 if (!pjsua_var.acc[i].valid)
2139 continue;
2140
2141 pjsua_acc_get_info(i, &info[c]);
2142 ++c;
2143 }
2144
2145 *count = c;
2146
2147 PJSUA_UNLOCK();
2148
2149 return PJ_SUCCESS;
2150}
2151
2152
2153/*
2154 * This is an internal function to find the most appropriate account to
2155 * used to reach to the specified URL.
2156 */
2157PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
2158{
2159 pj_str_t tmp;
2160 pjsip_uri *uri;
2161 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002162 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00002163 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002164
2165 PJSUA_LOCK();
2166
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002167 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002168
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002169 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002170
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002171 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002172 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002173 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002174 PJSUA_UNLOCK();
2175 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002176 }
2177
2178 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2179 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2180 {
2181 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00002182 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2183 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002184 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00002185 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002186 break;
2187 }
2188
Benny Prijono093d3022006-09-24 00:07:11 +00002189 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002190 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002191 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002192 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002193 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002194 }
2195
2196 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002197 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002198 PJSUA_UNLOCK();
2199 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002200 }
2201
Benny Prijonoa1e69682007-05-11 15:14:34 +00002202 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002203
Benny Prijonob4a17c92006-07-10 14:40:21 +00002204 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00002205 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2206 unsigned acc_id = pjsua_var.acc_ids[i];
2207 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
2208 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
2209 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002210 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002211 PJSUA_UNLOCK();
2212 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00002213 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002214 }
2215
Benny Prijonob4a17c92006-07-10 14:40:21 +00002216 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00002217 for (i=0; i<pjsua_var.acc_cnt; ++i) {
2218 unsigned acc_id = pjsua_var.acc_ids[i];
2219 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
2220 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002221 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00002222 PJSUA_UNLOCK();
2223 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00002224 }
2225 }
2226
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002227
Benny Prijono093d3022006-09-24 00:07:11 +00002228 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00002229 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002230 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00002231 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002232}
2233
2234
2235/*
2236 * This is an internal function to find the most appropriate account to be
2237 * used to handle incoming calls.
2238 */
2239PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
2240{
2241 pjsip_uri *uri;
2242 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00002243 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002244
Benny Prijono371cf0a2007-06-19 00:35:37 +00002245 /* Check that there's at least one account configured */
2246 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
2247
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002248 uri = rdata->msg_info.to->uri;
2249
2250 /* Just return default account if To URI is not SIP: */
2251 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2252 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2253 {
2254 return pjsua_var.default_acc;
2255 }
2256
2257
2258 PJSUA_LOCK();
2259
2260 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
2261
2262 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00002263 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2264 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002265 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2266
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002267 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00002268 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002269 {
2270 /* Match ! */
2271 PJSUA_UNLOCK();
2272 return acc_id;
2273 }
2274 }
2275
Benny Prijono093d3022006-09-24 00:07:11 +00002276 /* No matching account, try match domain part only. */
2277 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2278 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002279 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2280
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002281 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002282 /* Match ! */
2283 PJSUA_UNLOCK();
2284 return acc_id;
2285 }
2286 }
2287
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002288 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00002289 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2290 unsigned acc_id = pjsua_var.acc_ids[i];
2291 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2292
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002293 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
2294
2295 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2296 pjsip_transport_type_e type;
2297 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2298 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
2299 type = PJSIP_TRANSPORT_UDP;
2300
2301 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
2302 continue;
2303 }
2304
Benny Prijono093d3022006-09-24 00:07:11 +00002305 /* Match ! */
2306 PJSUA_UNLOCK();
2307 return acc_id;
2308 }
2309 }
2310
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002311 /* Still no match, use default account */
2312 PJSUA_UNLOCK();
2313 return pjsua_var.default_acc;
2314}
2315
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002316
Benny Prijonofff245c2007-04-02 11:44:47 +00002317/*
2318 * Create arbitrary requests for this account.
2319 */
2320PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2321 const pjsip_method *method,
2322 const pj_str_t *target,
2323 pjsip_tx_data **p_tdata)
2324{
2325 pjsip_tx_data *tdata;
2326 pjsua_acc *acc;
2327 pjsip_route_hdr *r;
2328 pj_status_t status;
2329
2330 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2331 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2332
2333 acc = &pjsua_var.acc[acc_id];
2334
2335 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2336 &acc->cfg.id, target,
2337 NULL, NULL, -1, NULL, &tdata);
2338 if (status != PJ_SUCCESS) {
2339 pjsua_perror(THIS_FILE, "Unable to create request", status);
2340 return status;
2341 }
2342
2343 /* Copy routeset */
2344 r = acc->route_set.next;
2345 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002346 pjsip_msg_add_hdr(tdata->msg,
2347 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002348 r = r->next;
2349 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002350
2351 /* If account is locked to specific transport, then set that transport to
2352 * the transmit data.
2353 */
2354 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2355 pjsip_tpselector tp_sel;
2356
2357 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2358 pjsip_tx_data_set_transport(tdata, &tp_sel);
2359 }
2360
Benny Prijonofff245c2007-04-02 11:44:47 +00002361 /* Done */
2362 *p_tdata = tdata;
2363 return PJ_SUCCESS;
2364}
2365
2366
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002367PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2368 pj_str_t *contact,
2369 pjsua_acc_id acc_id,
2370 const pj_str_t *suri)
2371{
2372 pjsua_acc *acc;
2373 pjsip_sip_uri *sip_uri;
2374 pj_status_t status;
2375 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2376 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002377 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002378 unsigned flag;
2379 int secure;
2380 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002381 const char *beginquote, *endquote;
2382 char transport_param[32];
Benny Prijonob54719f2010-11-16 03:07:46 +00002383 const char *ob = ";ob";
Benny Prijonod0bd4982007-12-02 15:40:52 +00002384
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002385
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002386 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002387 acc = &pjsua_var.acc[acc_id];
2388
Benny Prijonof75eceb2007-03-23 19:09:54 +00002389 /* If force_contact is configured, then use use it */
2390 if (acc->cfg.force_contact.slen) {
2391 *contact = acc->cfg.force_contact;
2392 return PJ_SUCCESS;
2393 }
2394
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002395 /* If route-set is configured for the account, then URI is the
2396 * first entry of the route-set.
2397 */
2398 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002399 sip_uri = (pjsip_sip_uri*)
2400 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002401 } else {
2402 pj_str_t tmp;
2403 pjsip_uri *uri;
2404
2405 pj_strdup_with_null(pool, &tmp, suri);
2406
2407 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2408 if (uri == NULL)
2409 return PJSIP_EINVALIDURI;
2410
2411 /* For non-SIP scheme, route set should be configured */
2412 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
Benny Prijonoc7545782010-09-28 07:43:18 +00002413 return PJSIP_ENOROUTESET;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002414
Benny Prijono8c7a6172007-02-18 21:17:46 +00002415 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002416 }
2417
2418 /* Get transport type of the URI */
2419 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2420 tp_type = PJSIP_TRANSPORT_TLS;
2421 else if (sip_uri->transport_param.slen == 0) {
2422 tp_type = PJSIP_TRANSPORT_UDP;
2423 } else
2424 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2425
2426 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2427 return PJSIP_EUNSUPTRANSPORT;
2428
Benny Prijonod0bd4982007-12-02 15:40:52 +00002429 /* If destination URI specifies IPv6, then set transport type
2430 * to use IPv6 as well.
2431 */
Benny Prijono19b29372007-12-05 04:08:40 +00002432 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002433 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2434
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002435 flag = pjsip_transport_get_flag_from_type(tp_type);
2436 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2437
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002438 /* Init transport selector. */
2439 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2440
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002441 /* Get local address suitable to send request from */
2442 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002443 pool, tp_type, &tp_sel,
2444 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002445 if (status != PJ_SUCCESS)
2446 return status;
2447
Benny Prijonod0bd4982007-12-02 15:40:52 +00002448 /* Enclose IPv6 address in square brackets */
2449 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2450 beginquote = "[";
2451 endquote = "]";
2452 } else {
2453 beginquote = endquote = "";
2454 }
2455
2456 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002457 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002458 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2459 ";transport=%s",
2460 pjsip_transport_get_type_name(tp_type));
2461 } else {
2462 transport_param[0] = '\0';
2463 }
2464
2465
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002466 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002467 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002468 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonob54719f2010-11-16 03:07:46 +00002469 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s%s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002470 (int)acc->display.slen,
2471 acc->display.ptr,
2472 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002473 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002474 (int)acc->user_part.slen,
2475 acc->user_part.ptr,
2476 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002477 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002478 (int)local_addr.slen,
2479 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002480 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002481 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002482 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002483 (int)acc->cfg.contact_uri_params.slen,
2484 acc->cfg.contact_uri_params.ptr,
Benny Prijonob54719f2010-11-16 03:07:46 +00002485 ob,
Benny Prijono30fe4852008-12-10 16:54:16 +00002486 (int)acc->cfg.contact_params.slen,
2487 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002488
2489 return PJ_SUCCESS;
2490}
2491
2492
2493
2494PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2495 pj_str_t *contact,
2496 pjsua_acc_id acc_id,
2497 pjsip_rx_data *rdata )
2498{
2499 /*
2500 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2501 * If the request that initiated the dialog contained a SIPS URI
2502 * in the Request-URI or in the top Record-Route header field value,
2503 * if there was any, or the Contact header field if there was no
2504 * Record-Route header field, the Contact header field in the response
2505 * MUST be a SIPS URI.
2506 */
2507 pjsua_acc *acc;
2508 pjsip_sip_uri *sip_uri;
2509 pj_status_t status;
2510 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2511 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002512 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002513 unsigned flag;
2514 int secure;
2515 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002516 const char *beginquote, *endquote;
2517 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002518
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002519 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002520 acc = &pjsua_var.acc[acc_id];
2521
Benny Prijonof75eceb2007-03-23 19:09:54 +00002522 /* If force_contact is configured, then use use it */
2523 if (acc->cfg.force_contact.slen) {
2524 *contact = acc->cfg.force_contact;
2525 return PJ_SUCCESS;
2526 }
2527
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002528 /* If Record-Route is present, then URI is the top Record-Route. */
2529 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002530 sip_uri = (pjsip_sip_uri*)
2531 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002532 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002533 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002534 pjsip_contact_hdr *h_contact;
2535 pjsip_uri *uri = NULL;
2536
Benny Prijonoa330d452008-08-05 20:14:39 +00002537 /* Otherwise URI is Contact URI.
2538 * Iterate the Contact URI until we find sip: or sips: scheme.
2539 */
2540 do {
2541 h_contact = (pjsip_contact_hdr*)
2542 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2543 pos);
2544 if (h_contact) {
Benny Prijono8b33bba2010-06-02 03:03:43 +00002545 if (h_contact->uri)
2546 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2547 else
2548 uri = NULL;
2549 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2550 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
Benny Prijonoa330d452008-08-05 20:14:39 +00002551 {
2552 pos = (pjsip_hdr*)h_contact->next;
2553 if (pos == &rdata->msg_info.msg->hdr)
2554 h_contact = NULL;
2555 } else {
2556 break;
2557 }
2558 }
2559 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002560
2561
2562 /* Or if Contact URI is not present, take the remote URI from
2563 * the From URI.
2564 */
2565 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002566 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002567
2568
2569 /* Can only do sip/sips scheme at present. */
2570 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2571 return PJSIP_EINVALIDREQURI;
2572
Benny Prijono8c7a6172007-02-18 21:17:46 +00002573 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002574 }
2575
2576 /* Get transport type of the URI */
2577 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2578 tp_type = PJSIP_TRANSPORT_TLS;
2579 else if (sip_uri->transport_param.slen == 0) {
2580 tp_type = PJSIP_TRANSPORT_UDP;
2581 } else
2582 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002583
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002584 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2585 return PJSIP_EUNSUPTRANSPORT;
2586
Benny Prijonod0bd4982007-12-02 15:40:52 +00002587 /* If destination URI specifies IPv6, then set transport type
2588 * to use IPv6 as well.
2589 */
2590 if (pj_strchr(&sip_uri->host, ':'))
2591 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2592
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002593 flag = pjsip_transport_get_flag_from_type(tp_type);
2594 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2595
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002596 /* Init transport selector. */
2597 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2598
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002599 /* Get local address suitable to send request from */
2600 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002601 pool, tp_type, &tp_sel,
2602 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002603 if (status != PJ_SUCCESS)
2604 return status;
2605
Benny Prijonod0bd4982007-12-02 15:40:52 +00002606 /* Enclose IPv6 address in square brackets */
2607 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2608 beginquote = "[";
2609 endquote = "]";
2610 } else {
2611 beginquote = endquote = "";
2612 }
2613
2614 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002615 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002616 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2617 ";transport=%s",
2618 pjsip_transport_get_type_name(tp_type));
2619 } else {
2620 transport_param[0] = '\0';
2621 }
2622
2623
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002624 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002625 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002626 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002627 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002628 (int)acc->display.slen,
2629 acc->display.ptr,
2630 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002631 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002632 (int)acc->user_part.slen,
2633 acc->user_part.ptr,
2634 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002635 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002636 (int)local_addr.slen,
2637 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002638 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002639 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002640 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002641 (int)acc->cfg.contact_uri_params.slen,
2642 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002643 (int)acc->cfg.contact_params.slen,
2644 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002645
2646 return PJ_SUCCESS;
2647}
2648
2649
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002650PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2651 pjsua_transport_id tp_id)
2652{
2653 pjsua_acc *acc;
2654
2655 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2656 acc = &pjsua_var.acc[acc_id];
2657
Benny Prijonoa1e69682007-05-11 15:14:34 +00002658 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002659 PJ_EINVAL);
2660
2661 acc->cfg.transport_id = tp_id;
2662
2663 return PJ_SUCCESS;
2664}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002665
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002666
2667/* Auto re-registration timeout callback */
2668static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2669{
2670 pjsua_acc *acc;
2671 pj_status_t status;
2672
2673 PJ_UNUSED_ARG(th);
2674 acc = (pjsua_acc*) te->user_data;
2675 pj_assert(acc);
2676
2677 PJSUA_LOCK();
2678
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002679 /* Check if the reregistration timer is still valid, e.g: while waiting
2680 * timeout timer application might have deleted the account or disabled
2681 * the auto-reregistration.
2682 */
2683 if (!acc->valid || !acc->auto_rereg.active ||
2684 acc->cfg.reg_retry_interval == 0)
2685 {
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002686 goto on_return;
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002687 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002688
2689 /* Start re-registration */
2690 acc->auto_rereg.attempt_cnt++;
2691 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2692 if (status != PJ_SUCCESS)
2693 schedule_reregistration(acc);
2694
Nanang Izzuddin66580002010-04-14 08:12:08 +00002695on_return:
2696 PJSUA_UNLOCK();
2697}
2698
2699
2700/* Schedule reregistration for specified account. Note that the first
2701 * re-registration after a registration failure will be done immediately.
2702 * Also note that this function should be called within PJSUA mutex.
2703 */
2704static void schedule_reregistration(pjsua_acc *acc)
2705{
2706 pj_time_val delay;
2707
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002708 pj_assert(acc);
2709
2710 /* Validate the account and re-registration feature status */
2711 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
2712 return;
2713 }
Nanang Izzuddin66580002010-04-14 08:12:08 +00002714
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002715 /* If configured, disconnect calls of this account after the first
2716 * reregistration attempt failed.
2717 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002718 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002719 {
2720 unsigned i, cnt;
2721
2722 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2723 if (pjsua_var.calls[i].acc_id == acc->index) {
2724 pjsua_call_hangup(i, 0, NULL, NULL);
2725 ++cnt;
2726 }
2727 }
2728
2729 if (cnt) {
2730 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2731 "after reregistration attempt failed",
2732 cnt, acc->index));
2733 }
2734 }
2735
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002736 /* Cancel any re-registration timer */
2737 pjsua_cancel_timer(&acc->auto_rereg.timer);
2738
2739 /* Update re-registration flag */
2740 acc->auto_rereg.active = PJ_TRUE;
2741
2742 /* Set up timer for reregistration */
2743 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2744 acc->auto_rereg.timer.user_data = acc;
2745
2746 /* Reregistration attempt. The first attempt will be done immediately. */
2747 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2748 delay.msec = 0;
2749 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2750}
2751
2752
2753/* Internal function to perform auto-reregistration on transport
2754 * connection/disconnection events.
2755 */
2756void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2757 pjsip_transport_state state,
2758 const pjsip_transport_state_info *info)
2759{
2760 unsigned i;
2761
2762 PJ_UNUSED_ARG(info);
2763
2764 /* Only care for transport disconnection events */
2765 if (state != PJSIP_TP_STATE_DISCONNECTED)
2766 return;
2767
2768 /* Shutdown this transport, to make sure that the transport manager
2769 * will create a new transport for reconnection.
2770 */
2771 pjsip_transport_shutdown(tp);
2772
2773 PJSUA_LOCK();
2774
2775 /* Enumerate accounts using this transport and perform actions
2776 * based on the transport state.
2777 */
2778 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2779 pjsua_acc *acc = &pjsua_var.acc[i];
2780
2781 /* Skip if this account is not valid OR auto re-registration
2782 * feature is disabled OR this transport is not used by this account.
2783 */
2784 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2785 tp != acc->auto_rereg.reg_tp)
2786 {
2787 continue;
2788 }
2789
2790 /* Schedule reregistration for this account */
2791 schedule_reregistration(acc);
2792 }
2793
2794 PJSUA_UNLOCK();
2795}