blob: 6af6b3d492d16974303e4a593a1f61022fa80717 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 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
26
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +000027static void schedule_reregistration(pjsua_acc *acc);
28
Benny Prijonoeebe9af2006-06-13 22:57:13 +000029/*
30 * Get number of current accounts.
31 */
32PJ_DEF(unsigned) pjsua_acc_get_count(void)
33{
34 return pjsua_var.acc_cnt;
35}
36
37
38/*
39 * Check if the specified account ID is valid.
40 */
41PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
42{
Benny Prijonoa1e69682007-05-11 15:14:34 +000043 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000044 pjsua_var.acc[acc_id].valid;
45}
46
47
48/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000049 * Set default account
50 */
51PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
52{
53 pjsua_var.default_acc = acc_id;
54 return PJ_SUCCESS;
55}
56
57
58/*
59 * Get default account.
60 */
61PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
62{
63 return pjsua_var.default_acc;
64}
65
66
67/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000068 * Copy account configuration.
69 */
Benny Prijonobddef2c2007-10-31 13:28:08 +000070PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
71 pjsua_acc_config *dst,
72 const pjsua_acc_config *src)
Benny Prijonoeebe9af2006-06-13 22:57:13 +000073{
74 unsigned i;
75
76 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
77
78 pj_strdup_with_null(pool, &dst->id, &src->id);
79 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000080 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Benny Prijonofe04fb52007-08-24 08:28:52 +000081 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000082
83 dst->proxy_cnt = src->proxy_cnt;
84 for (i=0; i<src->proxy_cnt; ++i)
85 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
86
87 dst->reg_timeout = src->reg_timeout;
88 dst->cred_count = src->cred_count;
89
90 for (i=0; i<src->cred_count; ++i) {
91 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
92 }
Benny Prijonobddef2c2007-10-31 13:28:08 +000093
94 dst->ka_interval = src->ka_interval;
95 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000096}
97
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +000098/*
99 * Calculate CRC of proxy list.
100 */
101static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt)
102{
103 pj_crc32_context ctx;
104 unsigned i;
105
106 pj_crc32_init(&ctx);
107 for (i=0; i<cnt; ++i) {
108 pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen);
109 }
110
111 return pj_crc32_final(&ctx);
112}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000113
114/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000115 * Initialize a new account (after configuration is set).
116 */
117static pj_status_t initialize_acc(unsigned acc_id)
118{
119 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
120 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000121 pjsip_name_addr *name_addr;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000122 pjsip_sip_uri *sip_uri, *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000123 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000124 unsigned i;
125
126 /* Need to parse local_uri to get the elements: */
127
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000128 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000129 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000130 acc_cfg->id.slen,
131 PJSIP_PARSE_URI_AS_NAMEADDR);
132 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000133 pjsua_perror(THIS_FILE, "Invalid local URI",
134 PJSIP_EINVALIDURI);
135 return PJSIP_EINVALIDURI;
136 }
137
138 /* Local URI MUST be a SIP or SIPS: */
139
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000140 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
141 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000142 {
143 pjsua_perror(THIS_FILE, "Invalid local URI",
144 PJSIP_EINVALIDSCHEME);
145 return PJSIP_EINVALIDSCHEME;
146 }
147
148
149 /* Get the SIP URI object: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000150 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000151
Benny Prijonob4a17c92006-07-10 14:40:21 +0000152
153 /* Parse registrar URI, if any */
154 if (acc_cfg->reg_uri.slen) {
155 pjsip_uri *reg_uri;
156
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000157 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000158 acc_cfg->reg_uri.slen, 0);
159 if (reg_uri == NULL) {
160 pjsua_perror(THIS_FILE, "Invalid registrar URI",
161 PJSIP_EINVALIDURI);
162 return PJSIP_EINVALIDURI;
163 }
164
165 /* Registrar URI MUST be a SIP or SIPS: */
166 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
167 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
168 {
169 pjsua_perror(THIS_FILE, "Invalid registar URI",
170 PJSIP_EINVALIDSCHEME);
171 return PJSIP_EINVALIDSCHEME;
172 }
173
174 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
175
176 } else {
177 sip_reg_uri = NULL;
178 }
179
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000180 /* Save the user and domain part. These will be used when finding an
181 * account for incoming requests.
182 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000183 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000184 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000185 acc->srv_domain = sip_uri->host;
186 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000187
Benny Prijonob4a17c92006-07-10 14:40:21 +0000188 if (sip_reg_uri) {
189 acc->srv_port = sip_reg_uri->port;
190 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191
192 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000193 //if (acc_cfg->contact.slen == 0) {
194 // acc_cfg->contact = acc_cfg->id;
195 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000196
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000197 /* Build account route-set from outbound proxies and route set from
198 * account configuration.
199 */
200 pj_list_init(&acc->route_set);
201
202 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
203 pj_str_t hname = { "Route", 5};
204 pjsip_route_hdr *r;
205 pj_str_t tmp;
206
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000207 pj_strdup_with_null(acc->pool, &tmp,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000208 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000209 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000210 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000211 if (r == NULL) {
212 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
213 PJSIP_EINVALIDURI);
214 return PJSIP_EINVALIDURI;
215 }
216 pj_list_push_back(&acc->route_set, r);
217 }
218
219 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
220 pj_str_t hname = { "Route", 5};
221 pjsip_route_hdr *r;
222 pj_str_t tmp;
223
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000224 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000225 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000226 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000227 if (r == NULL) {
228 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
229 PJ_EINVAL);
230 return PJ_EINVAL;
231 }
232 pj_list_push_back(&acc->route_set, r);
233 }
234
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000235 /* Concatenate credentials from account config and global config */
236 acc->cred_cnt = 0;
237 for (i=0; i<acc_cfg->cred_count; ++i) {
238 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
239 }
240 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
241 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
242 {
243 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
244 }
245
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000246 status = pjsua_pres_init_acc(acc_id);
247 if (status != PJ_SUCCESS)
248 return status;
249
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000250 /* Mark account as valid */
251 pjsua_var.acc[acc_id].valid = PJ_TRUE;
252
Benny Prijono093d3022006-09-24 00:07:11 +0000253 /* Insert account ID into account ID array, sorted by priority */
254 for (i=0; i<pjsua_var.acc_cnt; ++i) {
255 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
256 pjsua_var.acc[acc_id].cfg.priority)
257 {
258 break;
259 }
260 }
261 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
262 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000263
264 return PJ_SUCCESS;
265}
266
267
268/*
269 * Add a new account to pjsua.
270 */
271PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
272 pj_bool_t is_default,
273 pjsua_acc_id *p_acc_id)
274{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000275 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000276 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000277 pj_status_t status;
278
279 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
280 PJ_ETOOMANY);
281
282 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000283 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000284
285 PJSUA_LOCK();
286
287 /* Find empty account id. */
288 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
289 if (pjsua_var.acc[id].valid == PJ_FALSE)
290 break;
291 }
292
293 /* Expect to find a slot */
294 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
295 {PJSUA_UNLOCK(); return PJ_EBUG;});
296
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000297 acc = &pjsua_var.acc[id];
298
299 /* Create pool for this account. */
300 if (acc->pool)
301 pj_pool_reset(acc->pool);
302 else
303 acc->pool = pjsua_pool_create("acc%p", 512, 256);
304
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000305 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000306 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000307
308 /* Normalize registration timeout */
309 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
310 pjsua_var.acc[id].cfg.reg_timeout == 0)
311 {
312 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
313 }
314
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000315 /* Get CRC of account proxy setting */
316 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
317
318 /* Get CRC of global outbound proxy setting */
319 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
320 pjsua_var.ua_cfg.outbound_proxy_cnt);
321
Benny Prijono91d06b62008-09-20 12:16:56 +0000322 /* Check the route URI's and force loose route if required */
323 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
324 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
325 if (status != PJ_SUCCESS)
326 return status;
327 }
328
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329 status = initialize_acc(id);
330 if (status != PJ_SUCCESS) {
331 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000332 pj_pool_release(acc->pool);
333 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000334 PJSUA_UNLOCK();
335 return status;
336 }
337
338 if (is_default)
339 pjsua_var.default_acc = id;
340
341 if (p_acc_id)
342 *p_acc_id = id;
343
344 pjsua_var.acc_cnt++;
345
346 PJSUA_UNLOCK();
347
348 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
349 (int)cfg->id.slen, cfg->id.ptr, id));
350
351 /* If accounts has registration enabled, start registration */
352 if (pjsua_var.acc[id].cfg.reg_uri.slen)
353 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000354 else {
355 /* Otherwise subscribe to MWI, if it's enabled */
356 if (pjsua_var.acc[id].cfg.mwi_enabled)
357 pjsua_start_mwi(&pjsua_var.acc[id]);
358 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000359
360 return PJ_SUCCESS;
361}
362
363
364/*
365 * Add local account
366 */
367PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
368 pj_bool_t is_default,
369 pjsua_acc_id *p_acc_id)
370{
371 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000372 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000373 const char *beginquote, *endquote;
374 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000375 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000376
Benny Prijonoe93e2872006-06-28 16:46:49 +0000377 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000378 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
379 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000380
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000382 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000383
384 pjsua_acc_config_default(&cfg);
385
Benny Prijono093d3022006-09-24 00:07:11 +0000386 /* Lower the priority of local account */
387 --cfg.priority;
388
Benny Prijonod0bd4982007-12-02 15:40:52 +0000389 /* Enclose IPv6 address in square brackets */
390 if (t->type & PJSIP_TRANSPORT_IPV6) {
391 beginquote = "[";
392 endquote = "]";
393 } else {
394 beginquote = endquote = "";
395 }
396
397 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000398 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000399 pj_ansi_snprintf(transport_param, sizeof(transport_param),
400 ";transport=%s",
401 pjsip_transport_get_type_name(t->type));
402 } else {
403 transport_param[0] = '\0';
404 }
405
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000406 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000407 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000408 "<sip:%s%.*s%s:%d%s>",
409 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000410 (int)t->local_name.host.slen,
411 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000412 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000413 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000414 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000415
416 cfg.id = pj_str(uri);
417
418 return pjsua_acc_add(&cfg, is_default, p_acc_id);
419}
420
421
422/*
Benny Prijono705e7842008-07-21 18:12:51 +0000423 * Set arbitrary data to be associated with the account.
424 */
425PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
426 void *user_data)
427{
428 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
429 PJ_EINVAL);
430 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
431
432 PJSUA_LOCK();
433
434 pjsua_var.acc[acc_id].cfg.user_data = user_data;
435
436 PJSUA_UNLOCK();
437
438 return PJ_SUCCESS;
439}
440
441
442/*
443 * Retrieve arbitrary data associated with the account.
444 */
445PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
446{
447 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
448 NULL);
449 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
450
451 return pjsua_var.acc[acc_id].cfg.user_data;
452}
453
454
455/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000456 * Delete account.
457 */
458PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
459{
Benny Prijono093d3022006-09-24 00:07:11 +0000460 unsigned i;
461
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000462 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
463 PJ_EINVAL);
464 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
465
466 PJSUA_LOCK();
467
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000468 /* Cancel any re-registration timer */
469 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
470
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000472 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000473 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000474 if (pjsua_var.acc[acc_id].regc) {
475 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
476 }
Benny Prijono922933b2007-01-21 16:23:56 +0000477 pjsua_var.acc[acc_id].regc = NULL;
478 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000479
480 /* Delete server presence subscription */
481 pjsua_pres_delete_acc(acc_id);
482
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000483 /* Release account pool */
484 if (pjsua_var.acc[acc_id].pool) {
485 pj_pool_release(pjsua_var.acc[acc_id].pool);
486 pjsua_var.acc[acc_id].pool = NULL;
487 }
488
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000489 /* Invalidate */
490 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000491 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492
Benny Prijono093d3022006-09-24 00:07:11 +0000493 /* Remove from array */
494 for (i=0; i<pjsua_var.acc_cnt; ++i) {
495 if (pjsua_var.acc_ids[i] == acc_id)
496 break;
497 }
498 if (i != pjsua_var.acc_cnt) {
499 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
500 pjsua_var.acc_cnt, i);
501 --pjsua_var.acc_cnt;
502 }
503
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000504 /* Leave the calls intact, as I don't think calls need to
505 * access account once it's created
506 */
507
Benny Prijonofb2b3652007-06-28 07:15:03 +0000508 /* Update default account */
509 if (pjsua_var.default_acc == acc_id)
510 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511
512 PJSUA_UNLOCK();
513
514 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
515
516 return PJ_SUCCESS;
517}
518
519
520/*
521 * Modify account information.
522 */
523PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
524 const pjsua_acc_config *cfg)
525{
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000526 pjsua_acc *acc;
527 pjsip_name_addr *id_name_addr = NULL;
528 pjsip_sip_uri *id_sip_uri = NULL;
529 pjsip_sip_uri *reg_sip_uri = NULL;
530 pj_uint32_t local_route_crc, global_route_crc;
531 pjsip_route_hdr global_route;
532 pjsip_route_hdr local_route;
533 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
534 pj_bool_t update_reg = PJ_FALSE;
535 pj_status_t status = PJ_SUCCESS;
536
537 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
538 PJ_EINVAL);
539
540 PJSUA_LOCK();
541
542 acc = &pjsua_var.acc[acc_id];
543 if (!acc->valid) {
544 status = PJ_EINVAL;
545 goto on_return;
546 }
547
548 /* == Validate first == */
549
550 /* Account id */
551 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
552 /* Need to parse id to get the elements: */
553 id_name_addr = (pjsip_name_addr*)
554 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
555 PJSIP_PARSE_URI_AS_NAMEADDR);
556 if (id_name_addr == NULL) {
557 status = PJSIP_EINVALIDURI;
558 pjsua_perror(THIS_FILE, "Invalid local URI", status);
559 goto on_return;
560 }
561
562 /* URI MUST be a SIP or SIPS: */
563 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
564 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
565 {
566 status = PJSIP_EINVALIDSCHEME;
567 pjsua_perror(THIS_FILE, "Invalid local URI", status);
568 goto on_return;
569 }
570
571 /* Get the SIP URI object: */
572 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
573 }
574
575 /* Registrar URI */
576 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
577 pjsip_uri *reg_uri;
578
579 /* Need to parse reg_uri to get the elements: */
580 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
581 cfg->reg_uri.slen, 0);
582 if (reg_uri == NULL) {
583 status = PJSIP_EINVALIDURI;
584 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
585 goto on_return;
586 }
587
588 /* Registrar URI MUST be a SIP or SIPS: */
589 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
590 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
591 {
592 status = PJSIP_EINVALIDSCHEME;
593 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
594 goto on_return;
595 }
596
597 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
598 }
599
600 /* Global outbound proxy */
601 global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
602 pjsua_var.ua_cfg.outbound_proxy_cnt);
603 if (global_route_crc != acc->global_route_crc) {
604 pjsip_route_hdr *r;
605 unsigned i;
606
607 /* Validate the global route and save it to temporary var */
608 pj_list_init(&global_route);
609 for (i=0; i < pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
610 pj_str_t hname = { "Route", 5};
611 pj_str_t tmp;
612
613 pj_strdup_with_null(acc->pool, &tmp,
614 &pjsua_var.ua_cfg.outbound_proxy[i]);
615 r = (pjsip_route_hdr*)
616 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
617 if (r == NULL) {
618 status = PJSIP_EINVALIDURI;
619 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI", status);
620 goto on_return;
621 }
622 pj_list_push_back(&global_route, r);
623 }
624 }
625
626 /* Account proxy */
627 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
628 if (local_route_crc != acc->local_route_crc) {
629 pjsip_route_hdr *r;
630 unsigned i;
631
632 /* Validate the local route and save it to temporary var */
633 pj_list_init(&local_route);
634 for (i=0; i<cfg->proxy_cnt; ++i) {
635 pj_str_t hname = { "Route", 5};
636
637 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
638 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
639 if (status != PJ_SUCCESS)
640 goto on_return;
641 r = (pjsip_route_hdr*)
642 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
643 acc_proxy[i].slen, NULL);
644 if (r == NULL) {
645 status = PJSIP_EINVALIDURI;
646 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
647 status);
648 goto on_return;
649 }
650
651 pj_list_push_back(&local_route, r);
652 }
653 }
654
655
656 /* == Apply the new config == */
657
658 /* Account ID. */
659 if (id_name_addr && id_sip_uri) {
660 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
661 acc->display = id_name_addr->display;
662 acc->user_part = id_sip_uri->user;
663 acc->srv_domain = id_sip_uri->host;
664 acc->srv_port = 0;
665 update_reg = PJ_TRUE;
666 }
667
668 /* User data */
669 acc->cfg.user_data = cfg->user_data;
670
671 /* Priority */
672 if (acc->cfg.priority != cfg->priority) {
673 unsigned i;
674
675 acc->cfg.priority = cfg->priority;
676
677 /* Resort accounts priority */
678 for (i=0; i<pjsua_var.acc_cnt; ++i) {
679 if (pjsua_var.acc_ids[i] == acc_id)
680 break;
681 }
682 pj_assert(i < pjsua_var.acc_cnt);
683 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
684 pjsua_var.acc_cnt, i);
685 for (i=0; i<pjsua_var.acc_cnt; ++i) {
686 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
687 acc->cfg.priority)
688 {
689 break;
690 }
691 }
692 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
693 pjsua_var.acc_cnt, i, &acc_id);
694 }
695
696 /* MWI */
697 acc->cfg.mwi_enabled = cfg->mwi_enabled;
698
699 /* PIDF tuple ID */
700 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
701 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
702 &cfg->pidf_tuple_id);
703
704 /* Publish */
705 acc->cfg.publish_opt = cfg->publish_opt;
706 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
707 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
708 acc->cfg.publish_enabled = cfg->publish_enabled;
709 if (!acc->cfg.publish_enabled)
710 pjsua_pres_unpublish(acc);
711 else
712 update_reg = PJ_TRUE;
713 }
714
715 /* Force contact URI */
716 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
717 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
718 &cfg->force_contact);
719 update_reg = PJ_TRUE;
720 }
721
722 /* Contact param */
723 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
724 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
725 &cfg->contact_params);
726 update_reg = PJ_TRUE;
727 }
728
729 /* Contact URI params */
730 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
731 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
732 &cfg->contact_uri_params);
733 update_reg = PJ_TRUE;
734 }
735
736 /* Reliable provisional response */
737 acc->cfg.require_100rel = cfg->require_100rel;
738
739 /* Session timer */
740 acc->cfg.require_timer = cfg->require_timer;
741 acc->cfg.timer_setting = cfg->timer_setting;
742
743 /* Transport and keep-alive */
744 if (acc->cfg.transport_id != cfg->transport_id) {
745 acc->cfg.transport_id = cfg->transport_id;
746 update_reg = PJ_TRUE;
747 }
748 acc->cfg.ka_interval = cfg->ka_interval;
749 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
750 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
751#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
752 acc->cfg.use_srtp = cfg->use_srtp;
753 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
754#endif
755
756 /* Global outbound proxy */
757 if (global_route_crc != acc->global_route_crc) {
758 unsigned i, rcnt;
759
760 /* Remove the outbound proxies from the route set */
761 rcnt = pj_list_size(&acc->route_set);
762 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
763 pjsip_route_hdr *r = acc->route_set.next;
764 pj_list_erase(r);
765 }
766
767 /* Insert the outbound proxies to the beginning of route set */
768 pj_list_merge_first(&acc->route_set, &global_route);
769
770 /* Update global route CRC */
771 acc->global_route_crc = global_route_crc;
772
773 update_reg = PJ_TRUE;
774 }
775
776 /* Account proxy */
777 if (local_route_crc != acc->local_route_crc) {
778 unsigned i;
779
780 /* Remove the current account proxies from the route set */
781 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
782 pjsip_route_hdr *r = acc->route_set.prev;
783 pj_list_erase(r);
784 }
785
786 /* Insert new proxy setting to the route set */
787 pj_list_merge_last(&acc->route_set, &local_route);
788
789 /* Update the proxy setting */
790 acc->cfg.proxy_cnt = cfg->proxy_cnt;
791 for (i = 0; i < cfg->proxy_cnt; ++i)
792 acc->cfg.proxy[i] = acc_proxy[i];
793
794 /* Update local route CRC */
795 acc->local_route_crc = local_route_crc;
796
797 update_reg = PJ_TRUE;
798 }
799
800 /* Credential info */
801 {
802 unsigned i;
803
804 /* Selective update credential info. */
805 for (i = 0; i < cfg->cred_count; ++i) {
806 unsigned j;
807 pjsip_cred_info ci;
808
809 /* Find if this credential is already listed */
810 for (j = i; j < acc->cfg.cred_count; ++i) {
811 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
812 &cfg->cred_info[i]) == 0)
813 {
814 /* Found, but different index/position, swap */
815 if (j != i) {
816 ci = acc->cfg.cred_info[i];
817 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
818 acc->cfg.cred_info[j] = ci;
819 }
820 break;
821 }
822 }
823
824 /* Not found, insert this */
825 if (j == acc->cfg.cred_count) {
826 /* If account credential is full, discard the last one. */
827 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
828 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
829 acc->cfg.cred_count, acc->cfg.cred_count-1);
830 acc->cfg.cred_count--;
831 }
832
833 /* Insert this */
834 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
835 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
836 acc->cfg.cred_count, i, &ci);
837 }
838 }
839 acc->cfg.cred_count = cfg->cred_count;
840
841 /* Concatenate credentials from account config and global config */
842 acc->cred_cnt = 0;
843 for (i=0; i<acc->cfg.cred_count; ++i) {
844 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
845 }
846 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
847 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
848 {
849 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
850 }
851 }
852
853 /* Authentication preference */
854 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
855 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
856 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
857 &cfg->auth_pref.algorithm);
858
859 /* Registration */
860 acc->cfg.reg_timeout = cfg->reg_timeout;
861 acc->cfg.unreg_timeout = cfg->unreg_timeout;
862 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
863 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
864 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
865
866 /* Normalize registration timeout */
867 if (acc->cfg.reg_uri.slen && acc->cfg.reg_timeout == 0)
868 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
869
870 /* Registrar URI */
871 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
872 if (cfg->reg_uri.slen) {
873 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
874 if (reg_sip_uri)
875 acc->srv_port = reg_sip_uri->port;
876 } else {
877 /* Unregister if registration was set */
878 if (acc->cfg.reg_uri.slen)
879 pjsua_acc_set_registration(acc->index, PJ_FALSE);
880 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
881 }
882 update_reg = PJ_TRUE;
883 }
884
885 /* Update registration */
886 if (update_reg) {
887 /* If accounts has registration enabled, start registration */
888 if (acc->cfg.reg_uri.slen)
889 pjsua_acc_set_registration(acc->index, PJ_TRUE);
890 else {
891 /* Otherwise subscribe to MWI, if it's enabled */
892 if (acc->cfg.mwi_enabled)
893 pjsua_start_mwi(acc);
894 }
895 }
896
897on_return:
898 PJSUA_UNLOCK();
899 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000900}
901
902
903/*
904 * Modify account's presence status to be advertised to remote/presence
905 * subscribers.
906 */
907PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
908 pj_bool_t is_online)
909{
910 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
911 PJ_EINVAL);
912 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
913
914 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000915 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
916 pjsua_pres_update_acc(acc_id, PJ_FALSE);
917 return PJ_SUCCESS;
918}
919
920
921/*
922 * Set online status with extended information
923 */
924PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
925 pj_bool_t is_online,
926 const pjrpid_element *pr)
927{
928 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
929 PJ_EINVAL);
930 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
931
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000932 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000933 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000934 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
935 PJSUA_UNLOCK();
936
Benny Prijono4461c7d2007-08-25 13:36:15 +0000937 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000938 return PJ_SUCCESS;
939}
940
Benny Prijono7f630432008-09-24 16:52:41 +0000941/* Check if IP is private IP address */
942static pj_bool_t is_private_ip(const pj_str_t *addr)
943{
944 const pj_str_t private_net[] =
945 {
946 { "10.", 3 },
947 { "127.", 4 },
948 { "172.16.", 7 },
949 { "192.168.", 8 }
950 };
951 unsigned i;
952
953 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
954 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
955 return PJ_TRUE;
956 }
957
958 return PJ_FALSE;
959}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000960
Benny Prijono15b02302007-09-27 14:07:07 +0000961/* Update NAT address from the REGISTER response */
962static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
963 struct pjsip_regc_cbparam *param)
964{
965 pjsip_transport *tp;
966 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000967 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000968 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000969 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000970 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +0000971 pj_sockaddr contact_addr;
972 pj_sockaddr recv_addr;
973 pj_status_t status;
974 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +0000975 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000976 pjsip_contact_hdr *contact_hdr;
977 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +0000978
979 tp = param->rdata->tp_info.transport;
980
981 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000982 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000983 return PJ_FALSE;
984
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000985#if 0
986 // Always update
987 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000988
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000989 /* For UDP, only update if STUN is enabled (for now).
990 * For TCP/TLS, always check.
991 */
992 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
993 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
994 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
995 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
996 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000997 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000998 /* Yes we will check */
999 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001000 return PJ_FALSE;
1001 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001002#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001003
1004 /* Get the received and rport info */
1005 via = param->rdata->msg_info.via;
1006 if (via->rport_param < 1) {
1007 /* Remote doesn't support rport */
1008 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001009 if (rport==0) {
1010 pjsip_transport_type_e tp_type;
1011 tp_type = (pjsip_transport_type_e) tp->key.type;
1012 rport = pjsip_transport_get_default_port_for_type(tp_type);
1013 }
Benny Prijono15b02302007-09-27 14:07:07 +00001014 } else
1015 rport = via->rport_param;
1016
1017 if (via->recvd_param.slen != 0)
1018 via_addr = &via->recvd_param;
1019 else
1020 via_addr = &via->sent_by.host;
1021
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001022 /* Compare received and rport with the URI in our registration */
1023 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001024 contact_hdr = (pjsip_contact_hdr*)
1025 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1026 acc->contact.slen, NULL);
1027 pj_assert(contact_hdr != NULL);
1028 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001029 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001030 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001031
Benny Prijono24a21852008-04-14 04:04:30 +00001032 if (uri->port == 0) {
1033 pjsip_transport_type_e tp_type;
1034 tp_type = (pjsip_transport_type_e) tp->key.type;
1035 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1036 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001037
Benny Prijono84d24932009-06-04 15:51:39 +00001038 /* Convert IP address strings into sockaddr for comparison.
1039 * (http://trac.pjsip.org/repos/ticket/863)
1040 */
1041 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1042 &contact_addr);
1043 if (status == PJ_SUCCESS)
1044 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1045 &recv_addr);
1046 if (status == PJ_SUCCESS) {
1047 /* Compare the addresses as sockaddr according to the ticket above */
1048 matched = (uri->port == rport &&
1049 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1050 } else {
1051 /* Compare the addresses as string, as before */
1052 matched = (uri->port == rport &&
1053 pj_stricmp(&uri->host, via_addr)==0);
1054 }
1055
1056 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001057 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001058 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001059 return PJ_FALSE;
1060 }
1061
Benny Prijono7f630432008-09-24 16:52:41 +00001062 /* Get server IP */
1063 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1064
Benny Prijono15b02302007-09-27 14:07:07 +00001065 /* At this point we've detected that the address as seen by registrar.
1066 * has changed.
1067 */
Benny Prijono7f630432008-09-24 16:52:41 +00001068
1069 /* Do not switch if both Contact and server's IP address are
1070 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001071 * might have messed up with the SIP packets. See:
1072 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001073 *
1074 * This exception can be disabled by setting allow_contact_rewrite
1075 * to 2. In this case, the switch will always be done whenever there
1076 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001077 */
Benny Prijono247921b2008-09-26 22:06:11 +00001078 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1079 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001080 {
1081 /* Don't switch */
1082 pj_pool_release(pool);
1083 return PJ_FALSE;
1084 }
1085
Benny Prijono4f933762009-11-10 03:45:42 +00001086 /* Also don't switch if only the port number part is different, and
1087 * the Via received address is private.
1088 * See http://trac.pjsip.org/repos/ticket/864
1089 */
1090 if (acc->cfg.allow_contact_rewrite != 2 &&
1091 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1092 is_private_ip(via_addr))
1093 {
1094 /* Don't switch */
1095 pj_pool_release(pool);
1096 return PJ_FALSE;
1097 }
1098
Benny Prijono15b02302007-09-27 14:07:07 +00001099 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
1100 "(%.*s:%d --> %.*s:%d). Updating registration..",
1101 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001102 (int)uri->host.slen,
1103 uri->host.ptr,
1104 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001105 (int)via_addr->slen,
1106 via_addr->ptr,
1107 rport));
1108
1109 /* Unregister current contact */
1110 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1111 if (acc->regc != NULL) {
1112 pjsip_regc_destroy(acc->regc);
1113 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001114 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +00001115 }
1116
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001117 /* Update account's Contact header */
1118 {
1119 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001120 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001121 int len;
1122
Benny Prijono8972bf02009-04-05 18:30:45 +00001123 /* Enclose IPv6 address in square brackets */
1124 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1125 beginquote = "[";
1126 endquote = "]";
1127 } else {
1128 beginquote = endquote = "";
1129 }
1130
Benny Prijono24a21852008-04-14 04:04:30 +00001131 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001132 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001133 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001134 (int)acc->user_part.slen,
1135 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001136 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001137 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001138 (int)via_addr->slen,
1139 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001140 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001141 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001142 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001143 (int)acc->cfg.contact_uri_params.slen,
1144 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00001145 (int)acc->cfg.contact_params.slen,
1146 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001147 if (len < 1) {
1148 PJ_LOG(1,(THIS_FILE, "URI too long"));
1149 pj_pool_release(pool);
1150 return PJ_FALSE;
1151 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001152 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001153 }
1154
Benny Prijono4f933762009-11-10 03:45:42 +00001155 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1156 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1157 tp->local_name.port = rport;
Benny Prijono15b02302007-09-27 14:07:07 +00001158
1159 /* Perform new registration */
1160 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1161
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001162 pj_pool_release(pool);
1163
Benny Prijono15b02302007-09-27 14:07:07 +00001164 return PJ_TRUE;
1165}
1166
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001167/* Check and update Service-Route header */
1168void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1169{
1170 pjsip_generic_string_hdr *hsr = NULL;
1171 pjsip_route_hdr *hr, *h;
1172 const pj_str_t HNAME = { "Service-Route", 13 };
1173 const pj_str_t HROUTE = { "Route", 5 };
1174 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001175 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001176
1177 /* Find and parse Service-Route headers */
1178 for (;;) {
1179 char saved;
1180 int parsed_len;
1181
1182 /* Find Service-Route header */
1183 hsr = (pjsip_generic_string_hdr*)
1184 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1185 if (!hsr)
1186 break;
1187
1188 /* Parse as Route header since the syntax is similar. This may
1189 * return more than one headers.
1190 */
1191 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1192 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1193 hr = (pjsip_route_hdr*)
1194 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1195 hsr->hvalue.slen, &parsed_len);
1196 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1197
1198 if (hr == NULL) {
1199 /* Error */
1200 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1201 return;
1202 }
1203
1204 /* Save each URI in the result */
1205 h = hr;
1206 do {
1207 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1208 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1209 {
1210 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1211 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1212 return;
1213 }
1214
1215 uri[uri_cnt++] = h->name_addr.uri;
1216 h = h->next;
1217 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1218
1219 if (h != hr) {
1220 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1221 return;
1222 }
1223
1224 /* Prepare to find next Service-Route header */
1225 hsr = hsr->next;
1226 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1227 break;
1228 }
1229
1230 if (uri_cnt == 0)
1231 return;
1232
1233 /*
1234 * Update account's route set
1235 */
1236
1237 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001238 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001239 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1240 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1241 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001242 i<rcnt;
1243 ++i)
1244 {
1245 pjsip_route_hdr *prev = hr->prev;
1246 pj_list_erase(hr);
1247 hr = prev;
1248 }
1249 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001250
1251 /* Then append the Service-Route URIs */
1252 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001253 hr = pjsip_route_hdr_create(acc->pool);
1254 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001255 pj_list_push_back(&acc->route_set, hr);
1256 }
1257
1258 /* Done */
1259
1260 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1261 acc->index, uri_cnt));
1262}
1263
Benny Prijonobddef2c2007-10-31 13:28:08 +00001264
1265/* Keep alive timer callback */
1266static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1267{
1268 pjsua_acc *acc;
1269 pjsip_tpselector tp_sel;
1270 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001271 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001272 pj_status_t status;
1273
1274 PJ_UNUSED_ARG(th);
1275
1276 PJSUA_LOCK();
1277
1278 te->id = PJ_FALSE;
1279
1280 acc = (pjsua_acc*) te->user_data;
1281
1282 /* Select the transport to send the packet */
1283 pj_bzero(&tp_sel, sizeof(tp_sel));
1284 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1285 tp_sel.u.transport = acc->ka_transport;
1286
1287 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001288 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001289 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001290 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001291
1292 /* Send raw packet */
1293 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1294 PJSIP_TRANSPORT_UDP, &tp_sel,
1295 NULL, acc->cfg.ka_data.ptr,
1296 acc->cfg.ka_data.slen,
1297 &acc->ka_target, acc->ka_target_len,
1298 NULL, NULL);
1299
1300 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1301 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1302 }
1303
1304 /* Reschedule next timer */
1305 delay.sec = acc->cfg.ka_interval;
1306 delay.msec = 0;
1307 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1308 if (status == PJ_SUCCESS) {
1309 te->id = PJ_TRUE;
1310 } else {
1311 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1312 }
1313
1314 PJSUA_UNLOCK();
1315}
1316
1317
1318/* Update keep-alive for the account */
1319static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1320 struct pjsip_regc_cbparam *param)
1321{
1322 /* In all cases, stop keep-alive timer if it's running. */
1323 if (acc->ka_timer.id) {
1324 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1325 acc->ka_timer.id = PJ_FALSE;
1326
1327 pjsip_transport_dec_ref(acc->ka_transport);
1328 acc->ka_transport = NULL;
1329 }
1330
1331 if (start) {
1332 pj_time_val delay;
1333 pj_status_t status;
1334
1335 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001336 * - ka_interval is not zero in the account, and
1337 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001338 *
1339 * Previously we only enabled keep-alive when STUN is enabled, since
1340 * we thought that keep-alive is only needed in Internet situation.
1341 * But it has been discovered that Windows Firewall on WinXP also
1342 * needs to be kept-alive, otherwise incoming packets will be dropped.
1343 * So because of this, now keep-alive is always enabled for UDP,
1344 * regardless of whether STUN is enabled or not.
1345 *
1346 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1347 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001348 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001349 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001350 acc->cfg.ka_interval == 0 ||
1351 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1352 {
1353 /* Keep alive is not necessary */
1354 return;
1355 }
1356
1357 /* Save transport and destination address. */
1358 acc->ka_transport = param->rdata->tp_info.transport;
1359 pjsip_transport_add_ref(acc->ka_transport);
1360 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1361 param->rdata->pkt_info.src_addr_len);
1362 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1363
1364 /* Setup and start the timer */
1365 acc->ka_timer.cb = &keep_alive_timer_cb;
1366 acc->ka_timer.user_data = (void*)acc;
1367
1368 delay.sec = acc->cfg.ka_interval;
1369 delay.msec = 0;
1370 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1371 &delay);
1372 if (status == PJ_SUCCESS) {
1373 acc->ka_timer.id = PJ_TRUE;
1374 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1375 "destination:%s:%d, interval:%ds",
1376 acc->index,
1377 param->rdata->pkt_info.src_name,
1378 param->rdata->pkt_info.src_port,
1379 acc->cfg.ka_interval));
1380 } else {
1381 acc->ka_timer.id = PJ_FALSE;
1382 pjsip_transport_dec_ref(acc->ka_transport);
1383 acc->ka_transport = NULL;
1384 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1385 }
1386 }
1387}
1388
1389
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001390/*
1391 * This callback is called by pjsip_regc when outgoing register
1392 * request has completed.
1393 */
1394static void regc_cb(struct pjsip_regc_cbparam *param)
1395{
1396
Benny Prijonoa1e69682007-05-11 15:14:34 +00001397 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001398
Benny Prijono15b02302007-09-27 14:07:07 +00001399 if (param->regc != acc->regc)
1400 return;
1401
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001402 PJSUA_LOCK();
1403
1404 /*
1405 * Print registration status.
1406 */
1407 if (param->status!=PJ_SUCCESS) {
1408 pjsua_perror(THIS_FILE, "SIP registration error",
1409 param->status);
1410 pjsip_regc_destroy(acc->regc);
1411 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001412 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001413
Benny Prijonobddef2c2007-10-31 13:28:08 +00001414 /* Stop keep-alive timer if any. */
1415 update_keep_alive(acc, PJ_FALSE, NULL);
1416
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001417 } else if (param->code < 0 || param->code >= 300) {
1418 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1419 param->code,
1420 (int)param->reason.slen, param->reason.ptr));
1421 pjsip_regc_destroy(acc->regc);
1422 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001423 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001424
Benny Prijonobddef2c2007-10-31 13:28:08 +00001425 /* Stop keep-alive timer if any. */
1426 update_keep_alive(acc, PJ_FALSE, NULL);
1427
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001428 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1429
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001430 /* Update auto registration flag */
1431 acc->auto_rereg.active = PJ_FALSE;
1432 acc->auto_rereg.attempt_cnt = 0;
1433
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001434 if (param->expiration < 1) {
1435 pjsip_regc_destroy(acc->regc);
1436 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001437 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001438
1439 /* Stop keep-alive timer if any. */
1440 update_keep_alive(acc, PJ_FALSE, NULL);
1441
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001442 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1443 pjsua_var.acc[acc->index].cfg.id.ptr));
1444 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001445 /* Check NAT bound address */
1446 if (acc_check_nat_addr(acc, param)) {
1447 /* Update address, don't notify application yet */
1448 PJSUA_UNLOCK();
1449 return;
1450 }
1451
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001452 /* Check and update Service-Route header */
1453 update_service_route(acc, param->rdata);
1454
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001455 PJ_LOG(3, (THIS_FILE,
1456 "%s: registration success, status=%d (%.*s), "
1457 "will re-register in %d seconds",
1458 pjsua_var.acc[acc->index].cfg.id.ptr,
1459 param->code,
1460 (int)param->reason.slen, param->reason.ptr,
1461 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001462
Benny Prijonobddef2c2007-10-31 13:28:08 +00001463 /* Start keep-alive timer if necessary. */
1464 update_keep_alive(acc, PJ_TRUE, param);
1465
Benny Prijono8b6834f2007-02-24 13:29:22 +00001466 /* Send initial PUBLISH if it is enabled */
1467 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1468 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001469
1470 /* Subscribe to MWI, if it's enabled */
1471 if (acc->cfg.mwi_enabled)
1472 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001473 }
1474
1475 } else {
1476 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1477 }
1478
1479 acc->reg_last_err = param->status;
1480 acc->reg_last_code = param->code;
1481
1482 if (pjsua_var.ua_cfg.cb.on_reg_state)
1483 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1484
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001485 /* Check if we need to auto retry registration. Basically, registration
1486 * failure codes triggering auto-retry are those of temporal failures
1487 * considered to be recoverable in relatively short term.
1488 */
1489 if (acc->cfg.reg_retry_interval &&
1490 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1491 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1492 param->code == PJSIP_SC_BAD_GATEWAY ||
1493 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1494 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1495 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1496 {
1497 schedule_reregistration(acc);
1498 }
1499
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001500 PJSUA_UNLOCK();
1501}
1502
1503
1504/*
1505 * Initialize client registration.
1506 */
1507static pj_status_t pjsua_regc_init(int acc_id)
1508{
1509 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001510 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001511 pj_status_t status;
1512
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001513 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001514 acc = &pjsua_var.acc[acc_id];
1515
1516 if (acc->cfg.reg_uri.slen == 0) {
1517 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1518 return PJ_SUCCESS;
1519 }
1520
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001521 /* Destroy existing session, if any */
1522 if (acc->regc) {
1523 pjsip_regc_destroy(acc->regc);
1524 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001525 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001526 }
1527
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001528 /* initialize SIP registration if registrar is configured */
1529
1530 status = pjsip_regc_create( pjsua_var.endpt,
1531 acc, &regc_cb, &acc->regc);
1532
1533 if (status != PJ_SUCCESS) {
1534 pjsua_perror(THIS_FILE, "Unable to create client registration",
1535 status);
1536 return status;
1537 }
1538
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001539 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001540
1541 if (acc->contact.slen == 0) {
1542 pj_str_t tmp_contact;
1543
1544 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1545 acc_id, &acc->cfg.reg_uri);
1546 if (status != PJ_SUCCESS) {
1547 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1548 " for registration",
1549 status);
1550 pjsip_regc_destroy(acc->regc);
1551 pj_pool_release(pool);
1552 acc->regc = NULL;
1553 return status;
1554 }
1555
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001556 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001557 }
1558
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001559 status = pjsip_regc_init( acc->regc,
1560 &acc->cfg.reg_uri,
1561 &acc->cfg.id,
1562 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001563 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001564 acc->cfg.reg_timeout);
1565 if (status != PJ_SUCCESS) {
1566 pjsua_perror(THIS_FILE,
1567 "Client registration initialization error",
1568 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001569 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001570 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001571 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001572 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001573 return status;
1574 }
1575
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001576 /* If account is locked to specific transport, then set transport to
1577 * the client registration.
1578 */
1579 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1580 pjsip_tpselector tp_sel;
1581
1582 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1583 pjsip_regc_set_transport(acc->regc, &tp_sel);
1584 }
1585
1586
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001587 /* Set credentials
1588 */
1589 if (acc->cred_cnt) {
1590 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1591 }
1592
Benny Prijono48ab2b72007-11-08 09:24:30 +00001593 /* Set authentication preference */
1594 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1595
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001596 /* Set route-set
1597 */
1598 if (!pj_list_empty(&acc->route_set)) {
1599 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1600 }
1601
Benny Prijono8fc6de02006-11-11 21:25:55 +00001602 /* Add other request headers. */
1603 if (pjsua_var.ua_cfg.user_agent.slen) {
1604 pjsip_hdr hdr_list;
1605 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1606 pjsip_generic_string_hdr *h;
1607
Benny Prijono8fc6de02006-11-11 21:25:55 +00001608 pj_list_init(&hdr_list);
1609
1610 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1611 &pjsua_var.ua_cfg.user_agent);
1612 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1613
1614 pjsip_regc_add_headers(acc->regc, &hdr_list);
1615 }
1616
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001617 pj_pool_release(pool);
1618
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001619 return PJ_SUCCESS;
1620}
1621
1622
1623/*
1624 * Update registration or perform unregistration.
1625 */
1626PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1627 pj_bool_t renew)
1628{
1629 pj_status_t status = 0;
1630 pjsip_tx_data *tdata = 0;
1631
1632 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1633 PJ_EINVAL);
1634 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1635
1636 PJSUA_LOCK();
1637
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001638 /* Cancel any re-registration timer */
1639 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1640
1641 /* Reset pointer to registration transport */
1642 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1643
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001644 if (renew) {
1645 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001646 status = pjsua_regc_init(acc_id);
1647 if (status != PJ_SUCCESS) {
1648 pjsua_perror(THIS_FILE, "Unable to create registration",
1649 status);
1650 goto on_return;
1651 }
1652 }
1653 if (!pjsua_var.acc[acc_id].regc) {
1654 status = PJ_EINVALIDOP;
1655 goto on_return;
1656 }
1657
1658 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1659 &tdata);
1660
Benny Prijono28f673a2007-10-15 07:04:59 +00001661 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1662 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1663 pjsip_authorization_hdr *h;
1664 char *uri;
1665 int d;
1666
1667 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1668 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1669 uri, acc->cfg.reg_uri.slen+10);
1670 pj_assert(d > 0);
1671
1672 h = pjsip_authorization_hdr_create(tdata->pool);
1673 h->scheme = pj_str("Digest");
1674 h->credential.digest.username = acc->cred[0].username;
1675 h->credential.digest.realm = acc->srv_domain;
1676 h->credential.digest.uri = pj_str(uri);
1677 h->credential.digest.algorithm = pj_str("md5");
1678
1679 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1680 }
1681
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001682 } else {
1683 if (pjsua_var.acc[acc_id].regc == NULL) {
1684 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1685 status = PJ_EINVALIDOP;
1686 goto on_return;
1687 }
Benny Prijono166d5022010-02-10 14:24:48 +00001688
1689 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
1690
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001691 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1692 }
1693
Benny Prijono56315612006-07-18 14:39:40 +00001694 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001695 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001696 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001697 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001698
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001699 /* Update pointer to registration transport */
1700 if (status == PJ_SUCCESS) {
1701 pjsip_regc_info reg_info;
1702
1703 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
1704 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
1705 }
1706
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001707 if (status != PJ_SUCCESS) {
1708 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1709 status);
1710 } else {
1711 PJ_LOG(3,(THIS_FILE, "%s sent",
1712 (renew? "Registration" : "Unregistration")));
1713 }
1714
1715on_return:
1716 PJSUA_UNLOCK();
1717 return status;
1718}
1719
1720
1721/*
1722 * Get account information.
1723 */
1724PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1725 pjsua_acc_info *info)
1726{
1727 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1728 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1729
1730 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001731 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001732
Benny Prijonoac623b32006-07-03 15:19:31 +00001733 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001734
1735 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1736 PJ_EINVAL);
1737 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1738
1739 PJSUA_LOCK();
1740
1741 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1742 PJSUA_UNLOCK();
1743 return PJ_EINVALIDOP;
1744 }
1745
1746 info->id = acc_id;
1747 info->is_default = (pjsua_var.default_acc == acc_id);
1748 info->acc_uri = acc_cfg->id;
1749 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1750 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001751 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1752 if (info->rpid.note.slen)
1753 info->online_status_text = info->rpid.note;
1754 else if (info->online_status)
1755 info->online_status_text = pj_str("Online");
1756 else
1757 info->online_status_text = pj_str("Offline");
1758
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001759 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001760 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001761 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1762 info->status_text = pj_str(info->buf_);
1763 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001764 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001765 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001766 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1767 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001768 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001769 info->status_text = pj_str("not registered");
1770 }
1771 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001772 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001773 info->status_text = pj_str("In Progress");
1774 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001775 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001776 info->status_text = pj_str("does not register");
1777 }
1778
1779 if (acc->regc) {
1780 pjsip_regc_info regc_info;
1781 pjsip_regc_get_info(acc->regc, &regc_info);
1782 info->expires = regc_info.next_reg;
1783 } else {
1784 info->expires = -1;
1785 }
1786
1787 PJSUA_UNLOCK();
1788
1789 return PJ_SUCCESS;
1790
1791}
1792
1793
1794/*
1795 * Enum accounts all account ids.
1796 */
1797PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1798 unsigned *count )
1799{
1800 unsigned i, c;
1801
1802 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1803
1804 PJSUA_LOCK();
1805
1806 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1807 if (!pjsua_var.acc[i].valid)
1808 continue;
1809 ids[c] = i;
1810 ++c;
1811 }
1812
1813 *count = c;
1814
1815 PJSUA_UNLOCK();
1816
1817 return PJ_SUCCESS;
1818}
1819
1820
1821/*
1822 * Enum accounts info.
1823 */
1824PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1825 unsigned *count )
1826{
1827 unsigned i, c;
1828
1829 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1830
1831 PJSUA_LOCK();
1832
1833 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1834 if (!pjsua_var.acc[i].valid)
1835 continue;
1836
1837 pjsua_acc_get_info(i, &info[c]);
1838 ++c;
1839 }
1840
1841 *count = c;
1842
1843 PJSUA_UNLOCK();
1844
1845 return PJ_SUCCESS;
1846}
1847
1848
1849/*
1850 * This is an internal function to find the most appropriate account to
1851 * used to reach to the specified URL.
1852 */
1853PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1854{
1855 pj_str_t tmp;
1856 pjsip_uri *uri;
1857 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001858 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001859 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001860
1861 PJSUA_LOCK();
1862
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001863 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001864
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001865 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001866
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001867 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001868 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001869 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001870 PJSUA_UNLOCK();
1871 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001872 }
1873
1874 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1875 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1876 {
1877 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001878 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1879 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001880 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001881 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001882 break;
1883 }
1884
Benny Prijono093d3022006-09-24 00:07:11 +00001885 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001886 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001887 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001888 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001889 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001890 }
1891
1892 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001893 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001894 PJSUA_UNLOCK();
1895 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001896 }
1897
Benny Prijonoa1e69682007-05-11 15:14:34 +00001898 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001899
Benny Prijonob4a17c92006-07-10 14:40:21 +00001900 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001901 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1902 unsigned acc_id = pjsua_var.acc_ids[i];
1903 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1904 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1905 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001906 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001907 PJSUA_UNLOCK();
1908 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001909 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001910 }
1911
Benny Prijonob4a17c92006-07-10 14:40:21 +00001912 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001913 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1914 unsigned acc_id = pjsua_var.acc_ids[i];
1915 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1916 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001917 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001918 PJSUA_UNLOCK();
1919 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001920 }
1921 }
1922
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001923
Benny Prijono093d3022006-09-24 00:07:11 +00001924 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001925 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001926 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001927 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001928}
1929
1930
1931/*
1932 * This is an internal function to find the most appropriate account to be
1933 * used to handle incoming calls.
1934 */
1935PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1936{
1937 pjsip_uri *uri;
1938 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001939 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001940
Benny Prijono371cf0a2007-06-19 00:35:37 +00001941 /* Check that there's at least one account configured */
1942 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1943
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001944 uri = rdata->msg_info.to->uri;
1945
1946 /* Just return default account if To URI is not SIP: */
1947 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1948 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1949 {
1950 return pjsua_var.default_acc;
1951 }
1952
1953
1954 PJSUA_LOCK();
1955
1956 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1957
1958 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001959 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1960 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001961 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1962
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001963 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001964 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001965 {
1966 /* Match ! */
1967 PJSUA_UNLOCK();
1968 return acc_id;
1969 }
1970 }
1971
Benny Prijono093d3022006-09-24 00:07:11 +00001972 /* No matching account, try match domain part only. */
1973 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1974 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001975 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1976
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001977 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001978 /* Match ! */
1979 PJSUA_UNLOCK();
1980 return acc_id;
1981 }
1982 }
1983
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001984 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001985 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1986 unsigned acc_id = pjsua_var.acc_ids[i];
1987 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1988
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001989 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1990
1991 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1992 pjsip_transport_type_e type;
1993 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1994 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1995 type = PJSIP_TRANSPORT_UDP;
1996
1997 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1998 continue;
1999 }
2000
Benny Prijono093d3022006-09-24 00:07:11 +00002001 /* Match ! */
2002 PJSUA_UNLOCK();
2003 return acc_id;
2004 }
2005 }
2006
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002007 /* Still no match, use default account */
2008 PJSUA_UNLOCK();
2009 return pjsua_var.default_acc;
2010}
2011
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002012
Benny Prijonofff245c2007-04-02 11:44:47 +00002013/*
2014 * Create arbitrary requests for this account.
2015 */
2016PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2017 const pjsip_method *method,
2018 const pj_str_t *target,
2019 pjsip_tx_data **p_tdata)
2020{
2021 pjsip_tx_data *tdata;
2022 pjsua_acc *acc;
2023 pjsip_route_hdr *r;
2024 pj_status_t status;
2025
2026 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2027 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2028
2029 acc = &pjsua_var.acc[acc_id];
2030
2031 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2032 &acc->cfg.id, target,
2033 NULL, NULL, -1, NULL, &tdata);
2034 if (status != PJ_SUCCESS) {
2035 pjsua_perror(THIS_FILE, "Unable to create request", status);
2036 return status;
2037 }
2038
2039 /* Copy routeset */
2040 r = acc->route_set.next;
2041 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002042 pjsip_msg_add_hdr(tdata->msg,
2043 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002044 r = r->next;
2045 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002046
2047 /* If account is locked to specific transport, then set that transport to
2048 * the transmit data.
2049 */
2050 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2051 pjsip_tpselector tp_sel;
2052
2053 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2054 pjsip_tx_data_set_transport(tdata, &tp_sel);
2055 }
2056
Benny Prijonofff245c2007-04-02 11:44:47 +00002057 /* Done */
2058 *p_tdata = tdata;
2059 return PJ_SUCCESS;
2060}
2061
2062
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002063PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2064 pj_str_t *contact,
2065 pjsua_acc_id acc_id,
2066 const pj_str_t *suri)
2067{
2068 pjsua_acc *acc;
2069 pjsip_sip_uri *sip_uri;
2070 pj_status_t status;
2071 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2072 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002073 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002074 unsigned flag;
2075 int secure;
2076 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002077 const char *beginquote, *endquote;
2078 char transport_param[32];
2079
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002080
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002081 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002082 acc = &pjsua_var.acc[acc_id];
2083
Benny Prijonof75eceb2007-03-23 19:09:54 +00002084 /* If force_contact is configured, then use use it */
2085 if (acc->cfg.force_contact.slen) {
2086 *contact = acc->cfg.force_contact;
2087 return PJ_SUCCESS;
2088 }
2089
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002090 /* If route-set is configured for the account, then URI is the
2091 * first entry of the route-set.
2092 */
2093 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002094 sip_uri = (pjsip_sip_uri*)
2095 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002096 } else {
2097 pj_str_t tmp;
2098 pjsip_uri *uri;
2099
2100 pj_strdup_with_null(pool, &tmp, suri);
2101
2102 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2103 if (uri == NULL)
2104 return PJSIP_EINVALIDURI;
2105
2106 /* For non-SIP scheme, route set should be configured */
2107 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2108 return PJSIP_EINVALIDREQURI;
2109
Benny Prijono8c7a6172007-02-18 21:17:46 +00002110 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002111 }
2112
2113 /* Get transport type of the URI */
2114 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2115 tp_type = PJSIP_TRANSPORT_TLS;
2116 else if (sip_uri->transport_param.slen == 0) {
2117 tp_type = PJSIP_TRANSPORT_UDP;
2118 } else
2119 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2120
2121 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2122 return PJSIP_EUNSUPTRANSPORT;
2123
Benny Prijonod0bd4982007-12-02 15:40:52 +00002124 /* If destination URI specifies IPv6, then set transport type
2125 * to use IPv6 as well.
2126 */
Benny Prijono19b29372007-12-05 04:08:40 +00002127 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002128 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2129
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002130 flag = pjsip_transport_get_flag_from_type(tp_type);
2131 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2132
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002133 /* Init transport selector. */
2134 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2135
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002136 /* Get local address suitable to send request from */
2137 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002138 pool, tp_type, &tp_sel,
2139 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002140 if (status != PJ_SUCCESS)
2141 return status;
2142
Benny Prijonod0bd4982007-12-02 15:40:52 +00002143 /* Enclose IPv6 address in square brackets */
2144 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2145 beginquote = "[";
2146 endquote = "]";
2147 } else {
2148 beginquote = endquote = "";
2149 }
2150
2151 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002152 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002153 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2154 ";transport=%s",
2155 pjsip_transport_get_type_name(tp_type));
2156 } else {
2157 transport_param[0] = '\0';
2158 }
2159
2160
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002161 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002162 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002163 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002164 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002165 (int)acc->display.slen,
2166 acc->display.ptr,
2167 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002168 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002169 (int)acc->user_part.slen,
2170 acc->user_part.ptr,
2171 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002172 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002173 (int)local_addr.slen,
2174 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002175 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002176 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002177 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002178 (int)acc->cfg.contact_uri_params.slen,
2179 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002180 (int)acc->cfg.contact_params.slen,
2181 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002182
2183 return PJ_SUCCESS;
2184}
2185
2186
2187
2188PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2189 pj_str_t *contact,
2190 pjsua_acc_id acc_id,
2191 pjsip_rx_data *rdata )
2192{
2193 /*
2194 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2195 * If the request that initiated the dialog contained a SIPS URI
2196 * in the Request-URI or in the top Record-Route header field value,
2197 * if there was any, or the Contact header field if there was no
2198 * Record-Route header field, the Contact header field in the response
2199 * MUST be a SIPS URI.
2200 */
2201 pjsua_acc *acc;
2202 pjsip_sip_uri *sip_uri;
2203 pj_status_t status;
2204 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2205 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002206 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002207 unsigned flag;
2208 int secure;
2209 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002210 const char *beginquote, *endquote;
2211 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002212
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002213 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002214 acc = &pjsua_var.acc[acc_id];
2215
Benny Prijonof75eceb2007-03-23 19:09:54 +00002216 /* If force_contact is configured, then use use it */
2217 if (acc->cfg.force_contact.slen) {
2218 *contact = acc->cfg.force_contact;
2219 return PJ_SUCCESS;
2220 }
2221
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002222 /* If Record-Route is present, then URI is the top Record-Route. */
2223 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002224 sip_uri = (pjsip_sip_uri*)
2225 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002226 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002227 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002228 pjsip_contact_hdr *h_contact;
2229 pjsip_uri *uri = NULL;
2230
Benny Prijonoa330d452008-08-05 20:14:39 +00002231 /* Otherwise URI is Contact URI.
2232 * Iterate the Contact URI until we find sip: or sips: scheme.
2233 */
2234 do {
2235 h_contact = (pjsip_contact_hdr*)
2236 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2237 pos);
2238 if (h_contact) {
2239 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2240 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2241 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2242 {
2243 pos = (pjsip_hdr*)h_contact->next;
2244 if (pos == &rdata->msg_info.msg->hdr)
2245 h_contact = NULL;
2246 } else {
2247 break;
2248 }
2249 }
2250 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002251
2252
2253 /* Or if Contact URI is not present, take the remote URI from
2254 * the From URI.
2255 */
2256 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002257 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002258
2259
2260 /* Can only do sip/sips scheme at present. */
2261 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2262 return PJSIP_EINVALIDREQURI;
2263
Benny Prijono8c7a6172007-02-18 21:17:46 +00002264 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002265 }
2266
2267 /* Get transport type of the URI */
2268 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2269 tp_type = PJSIP_TRANSPORT_TLS;
2270 else if (sip_uri->transport_param.slen == 0) {
2271 tp_type = PJSIP_TRANSPORT_UDP;
2272 } else
2273 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002274
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002275 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2276 return PJSIP_EUNSUPTRANSPORT;
2277
Benny Prijonod0bd4982007-12-02 15:40:52 +00002278 /* If destination URI specifies IPv6, then set transport type
2279 * to use IPv6 as well.
2280 */
2281 if (pj_strchr(&sip_uri->host, ':'))
2282 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2283
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002284 flag = pjsip_transport_get_flag_from_type(tp_type);
2285 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2286
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002287 /* Init transport selector. */
2288 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2289
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002290 /* Get local address suitable to send request from */
2291 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002292 pool, tp_type, &tp_sel,
2293 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002294 if (status != PJ_SUCCESS)
2295 return status;
2296
Benny Prijonod0bd4982007-12-02 15:40:52 +00002297 /* Enclose IPv6 address in square brackets */
2298 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2299 beginquote = "[";
2300 endquote = "]";
2301 } else {
2302 beginquote = endquote = "";
2303 }
2304
2305 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002306 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002307 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2308 ";transport=%s",
2309 pjsip_transport_get_type_name(tp_type));
2310 } else {
2311 transport_param[0] = '\0';
2312 }
2313
2314
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002315 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002316 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002317 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002318 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002319 (int)acc->display.slen,
2320 acc->display.ptr,
2321 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002322 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002323 (int)acc->user_part.slen,
2324 acc->user_part.ptr,
2325 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002326 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002327 (int)local_addr.slen,
2328 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002329 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002330 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002331 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002332 (int)acc->cfg.contact_uri_params.slen,
2333 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002334 (int)acc->cfg.contact_params.slen,
2335 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002336
2337 return PJ_SUCCESS;
2338}
2339
2340
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002341PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2342 pjsua_transport_id tp_id)
2343{
2344 pjsua_acc *acc;
2345
2346 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2347 acc = &pjsua_var.acc[acc_id];
2348
Benny Prijonoa1e69682007-05-11 15:14:34 +00002349 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002350 PJ_EINVAL);
2351
2352 acc->cfg.transport_id = tp_id;
2353
2354 return PJ_SUCCESS;
2355}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002356
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002357
2358/* Auto re-registration timeout callback */
2359static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2360{
2361 pjsua_acc *acc;
2362 pj_status_t status;
2363
2364 PJ_UNUSED_ARG(th);
2365 acc = (pjsua_acc*) te->user_data;
2366 pj_assert(acc);
2367
2368 PJSUA_LOCK();
2369
2370 if (!acc->valid || !acc->auto_rereg.active)
2371 goto on_return;
2372
2373 /* Start re-registration */
2374 acc->auto_rereg.attempt_cnt++;
2375 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2376 if (status != PJ_SUCCESS)
2377 schedule_reregistration(acc);
2378
Nanang Izzuddin66580002010-04-14 08:12:08 +00002379on_return:
2380 PJSUA_UNLOCK();
2381}
2382
2383
2384/* Schedule reregistration for specified account. Note that the first
2385 * re-registration after a registration failure will be done immediately.
2386 * Also note that this function should be called within PJSUA mutex.
2387 */
2388static void schedule_reregistration(pjsua_acc *acc)
2389{
2390 pj_time_val delay;
2391
2392 pj_assert(acc && acc->valid && acc->cfg.reg_retry_interval);
2393
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002394 /* If configured, disconnect calls of this account after the first
2395 * reregistration attempt failed.
2396 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002397 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002398 {
2399 unsigned i, cnt;
2400
2401 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2402 if (pjsua_var.calls[i].acc_id == acc->index) {
2403 pjsua_call_hangup(i, 0, NULL, NULL);
2404 ++cnt;
2405 }
2406 }
2407
2408 if (cnt) {
2409 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2410 "after reregistration attempt failed",
2411 cnt, acc->index));
2412 }
2413 }
2414
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002415 /* Cancel any re-registration timer */
2416 pjsua_cancel_timer(&acc->auto_rereg.timer);
2417
2418 /* Update re-registration flag */
2419 acc->auto_rereg.active = PJ_TRUE;
2420
2421 /* Set up timer for reregistration */
2422 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2423 acc->auto_rereg.timer.user_data = acc;
2424
2425 /* Reregistration attempt. The first attempt will be done immediately. */
2426 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2427 delay.msec = 0;
2428 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2429}
2430
2431
2432/* Internal function to perform auto-reregistration on transport
2433 * connection/disconnection events.
2434 */
2435void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2436 pjsip_transport_state state,
2437 const pjsip_transport_state_info *info)
2438{
2439 unsigned i;
2440
2441 PJ_UNUSED_ARG(info);
2442
2443 /* Only care for transport disconnection events */
2444 if (state != PJSIP_TP_STATE_DISCONNECTED)
2445 return;
2446
2447 /* Shutdown this transport, to make sure that the transport manager
2448 * will create a new transport for reconnection.
2449 */
2450 pjsip_transport_shutdown(tp);
2451
2452 PJSUA_LOCK();
2453
2454 /* Enumerate accounts using this transport and perform actions
2455 * based on the transport state.
2456 */
2457 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2458 pjsua_acc *acc = &pjsua_var.acc[i];
2459
2460 /* Skip if this account is not valid OR auto re-registration
2461 * feature is disabled OR this transport is not used by this account.
2462 */
2463 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2464 tp != acc->auto_rereg.reg_tp)
2465 {
2466 continue;
2467 }
2468
2469 /* Schedule reregistration for this account */
2470 schedule_reregistration(acc);
2471 }
2472
2473 PJSUA_UNLOCK();
2474}