blob: 925c71a968ab8381cfbcdba63e6146add0ece738 [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;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000754 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000755#endif
756
757 /* Global outbound proxy */
758 if (global_route_crc != acc->global_route_crc) {
759 unsigned i, rcnt;
760
761 /* Remove the outbound proxies from the route set */
762 rcnt = pj_list_size(&acc->route_set);
763 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
764 pjsip_route_hdr *r = acc->route_set.next;
765 pj_list_erase(r);
766 }
767
768 /* Insert the outbound proxies to the beginning of route set */
769 pj_list_merge_first(&acc->route_set, &global_route);
770
771 /* Update global route CRC */
772 acc->global_route_crc = global_route_crc;
773
774 update_reg = PJ_TRUE;
775 }
776
777 /* Account proxy */
778 if (local_route_crc != acc->local_route_crc) {
779 unsigned i;
780
781 /* Remove the current account proxies from the route set */
782 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
783 pjsip_route_hdr *r = acc->route_set.prev;
784 pj_list_erase(r);
785 }
786
787 /* Insert new proxy setting to the route set */
788 pj_list_merge_last(&acc->route_set, &local_route);
789
790 /* Update the proxy setting */
791 acc->cfg.proxy_cnt = cfg->proxy_cnt;
792 for (i = 0; i < cfg->proxy_cnt; ++i)
793 acc->cfg.proxy[i] = acc_proxy[i];
794
795 /* Update local route CRC */
796 acc->local_route_crc = local_route_crc;
797
798 update_reg = PJ_TRUE;
799 }
800
801 /* Credential info */
802 {
803 unsigned i;
804
805 /* Selective update credential info. */
806 for (i = 0; i < cfg->cred_count; ++i) {
807 unsigned j;
808 pjsip_cred_info ci;
809
810 /* Find if this credential is already listed */
811 for (j = i; j < acc->cfg.cred_count; ++i) {
812 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
813 &cfg->cred_info[i]) == 0)
814 {
815 /* Found, but different index/position, swap */
816 if (j != i) {
817 ci = acc->cfg.cred_info[i];
818 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
819 acc->cfg.cred_info[j] = ci;
820 }
821 break;
822 }
823 }
824
825 /* Not found, insert this */
826 if (j == acc->cfg.cred_count) {
827 /* If account credential is full, discard the last one. */
828 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
829 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
830 acc->cfg.cred_count, acc->cfg.cred_count-1);
831 acc->cfg.cred_count--;
832 }
833
834 /* Insert this */
835 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
836 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
837 acc->cfg.cred_count, i, &ci);
838 }
839 }
840 acc->cfg.cred_count = cfg->cred_count;
841
842 /* Concatenate credentials from account config and global config */
843 acc->cred_cnt = 0;
844 for (i=0; i<acc->cfg.cred_count; ++i) {
845 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
846 }
847 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
848 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
849 {
850 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
851 }
852 }
853
854 /* Authentication preference */
855 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
856 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
857 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
858 &cfg->auth_pref.algorithm);
859
860 /* Registration */
861 acc->cfg.reg_timeout = cfg->reg_timeout;
862 acc->cfg.unreg_timeout = cfg->unreg_timeout;
863 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
864 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
865 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
866
867 /* Normalize registration timeout */
868 if (acc->cfg.reg_uri.slen && acc->cfg.reg_timeout == 0)
869 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
870
871 /* Registrar URI */
872 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
873 if (cfg->reg_uri.slen) {
874 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
875 if (reg_sip_uri)
876 acc->srv_port = reg_sip_uri->port;
877 } else {
878 /* Unregister if registration was set */
879 if (acc->cfg.reg_uri.slen)
880 pjsua_acc_set_registration(acc->index, PJ_FALSE);
881 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
882 }
883 update_reg = PJ_TRUE;
884 }
885
886 /* Update registration */
887 if (update_reg) {
888 /* If accounts has registration enabled, start registration */
889 if (acc->cfg.reg_uri.slen)
890 pjsua_acc_set_registration(acc->index, PJ_TRUE);
891 else {
892 /* Otherwise subscribe to MWI, if it's enabled */
893 if (acc->cfg.mwi_enabled)
894 pjsua_start_mwi(acc);
895 }
896 }
897
898on_return:
899 PJSUA_UNLOCK();
900 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000901}
902
903
904/*
905 * Modify account's presence status to be advertised to remote/presence
906 * subscribers.
907 */
908PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
909 pj_bool_t is_online)
910{
911 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
912 PJ_EINVAL);
913 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
914
915 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000916 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
917 pjsua_pres_update_acc(acc_id, PJ_FALSE);
918 return PJ_SUCCESS;
919}
920
921
922/*
923 * Set online status with extended information
924 */
925PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
926 pj_bool_t is_online,
927 const pjrpid_element *pr)
928{
929 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
930 PJ_EINVAL);
931 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
932
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000933 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000934 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000935 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
936 PJSUA_UNLOCK();
937
Benny Prijono4461c7d2007-08-25 13:36:15 +0000938 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000939 return PJ_SUCCESS;
940}
941
Benny Prijono7f630432008-09-24 16:52:41 +0000942/* Check if IP is private IP address */
943static pj_bool_t is_private_ip(const pj_str_t *addr)
944{
945 const pj_str_t private_net[] =
946 {
947 { "10.", 3 },
948 { "127.", 4 },
949 { "172.16.", 7 },
950 { "192.168.", 8 }
951 };
952 unsigned i;
953
954 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
955 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
956 return PJ_TRUE;
957 }
958
959 return PJ_FALSE;
960}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000961
Benny Prijono15b02302007-09-27 14:07:07 +0000962/* Update NAT address from the REGISTER response */
963static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
964 struct pjsip_regc_cbparam *param)
965{
966 pjsip_transport *tp;
967 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000968 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000969 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000970 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000971 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +0000972 pj_sockaddr contact_addr;
973 pj_sockaddr recv_addr;
974 pj_status_t status;
975 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +0000976 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000977 pjsip_contact_hdr *contact_hdr;
978 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +0000979
980 tp = param->rdata->tp_info.transport;
981
982 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000983 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000984 return PJ_FALSE;
985
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000986#if 0
987 // Always update
988 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000989
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000990 /* For UDP, only update if STUN is enabled (for now).
991 * For TCP/TLS, always check.
992 */
993 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
994 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
995 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
996 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
997 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000998 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000999 /* Yes we will check */
1000 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001001 return PJ_FALSE;
1002 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001003#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001004
1005 /* Get the received and rport info */
1006 via = param->rdata->msg_info.via;
1007 if (via->rport_param < 1) {
1008 /* Remote doesn't support rport */
1009 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001010 if (rport==0) {
1011 pjsip_transport_type_e tp_type;
1012 tp_type = (pjsip_transport_type_e) tp->key.type;
1013 rport = pjsip_transport_get_default_port_for_type(tp_type);
1014 }
Benny Prijono15b02302007-09-27 14:07:07 +00001015 } else
1016 rport = via->rport_param;
1017
1018 if (via->recvd_param.slen != 0)
1019 via_addr = &via->recvd_param;
1020 else
1021 via_addr = &via->sent_by.host;
1022
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001023 /* Compare received and rport with the URI in our registration */
1024 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001025 contact_hdr = (pjsip_contact_hdr*)
1026 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1027 acc->contact.slen, NULL);
1028 pj_assert(contact_hdr != NULL);
1029 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001030 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001031 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001032
Benny Prijono24a21852008-04-14 04:04:30 +00001033 if (uri->port == 0) {
1034 pjsip_transport_type_e tp_type;
1035 tp_type = (pjsip_transport_type_e) tp->key.type;
1036 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1037 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001038
Benny Prijono84d24932009-06-04 15:51:39 +00001039 /* Convert IP address strings into sockaddr for comparison.
1040 * (http://trac.pjsip.org/repos/ticket/863)
1041 */
1042 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1043 &contact_addr);
1044 if (status == PJ_SUCCESS)
1045 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1046 &recv_addr);
1047 if (status == PJ_SUCCESS) {
1048 /* Compare the addresses as sockaddr according to the ticket above */
1049 matched = (uri->port == rport &&
1050 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1051 } else {
1052 /* Compare the addresses as string, as before */
1053 matched = (uri->port == rport &&
1054 pj_stricmp(&uri->host, via_addr)==0);
1055 }
1056
1057 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001058 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001059 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001060 return PJ_FALSE;
1061 }
1062
Benny Prijono7f630432008-09-24 16:52:41 +00001063 /* Get server IP */
1064 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1065
Benny Prijono15b02302007-09-27 14:07:07 +00001066 /* At this point we've detected that the address as seen by registrar.
1067 * has changed.
1068 */
Benny Prijono7f630432008-09-24 16:52:41 +00001069
1070 /* Do not switch if both Contact and server's IP address are
1071 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001072 * might have messed up with the SIP packets. See:
1073 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001074 *
1075 * This exception can be disabled by setting allow_contact_rewrite
1076 * to 2. In this case, the switch will always be done whenever there
1077 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001078 */
Benny Prijono247921b2008-09-26 22:06:11 +00001079 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1080 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001081 {
1082 /* Don't switch */
1083 pj_pool_release(pool);
1084 return PJ_FALSE;
1085 }
1086
Benny Prijono4f933762009-11-10 03:45:42 +00001087 /* Also don't switch if only the port number part is different, and
1088 * the Via received address is private.
1089 * See http://trac.pjsip.org/repos/ticket/864
1090 */
1091 if (acc->cfg.allow_contact_rewrite != 2 &&
1092 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1093 is_private_ip(via_addr))
1094 {
1095 /* Don't switch */
1096 pj_pool_release(pool);
1097 return PJ_FALSE;
1098 }
1099
Benny Prijono15b02302007-09-27 14:07:07 +00001100 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001101 "(%.*s:%d --> %.*s:%d). Updating registration "
1102 "(using method %d)",
Benny Prijono15b02302007-09-27 14:07:07 +00001103 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001104 (int)uri->host.slen,
1105 uri->host.ptr,
1106 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001107 (int)via_addr->slen,
1108 via_addr->ptr,
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001109 rport,
1110 acc->cfg.contact_rewrite_method));
Benny Prijono15b02302007-09-27 14:07:07 +00001111
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001112 pj_assert(acc->cfg.contact_rewrite_method == 1 ||
1113 acc->cfg.contact_rewrite_method == 2);
1114
1115 if (acc->cfg.contact_rewrite_method == 1) {
1116 /* Unregister current contact */
1117 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1118 if (acc->regc != NULL) {
1119 pjsip_regc_destroy(acc->regc);
1120 acc->regc = NULL;
1121 acc->contact.slen = 0;
1122 }
Benny Prijono15b02302007-09-27 14:07:07 +00001123 }
1124
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001125 /*
1126 * Build new Contact header
1127 */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001128 {
1129 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001130 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001131 int len;
1132
Benny Prijono8972bf02009-04-05 18:30:45 +00001133 /* Enclose IPv6 address in square brackets */
1134 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1135 beginquote = "[";
1136 endquote = "]";
1137 } else {
1138 beginquote = endquote = "";
1139 }
1140
Benny Prijono24a21852008-04-14 04:04:30 +00001141 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001142 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001143 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001144 (int)acc->user_part.slen,
1145 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001146 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001147 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001148 (int)via_addr->slen,
1149 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001150 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001151 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001152 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001153 (int)acc->cfg.contact_uri_params.slen,
1154 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00001155 (int)acc->cfg.contact_params.slen,
1156 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001157 if (len < 1) {
1158 PJ_LOG(1,(THIS_FILE, "URI too long"));
1159 pj_pool_release(pool);
1160 return PJ_FALSE;
1161 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001162 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001163
1164 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1165 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1166 tp->local_name.port = rport;
1167
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001168 }
1169
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001170 if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) {
1171 pjsip_regc_update_contact(acc->regc, 1, &acc->contact);
1172 }
Benny Prijono15b02302007-09-27 14:07:07 +00001173
1174 /* Perform new registration */
1175 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1176
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001177 pj_pool_release(pool);
1178
Benny Prijono15b02302007-09-27 14:07:07 +00001179 return PJ_TRUE;
1180}
1181
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001182/* Check and update Service-Route header */
1183void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1184{
1185 pjsip_generic_string_hdr *hsr = NULL;
1186 pjsip_route_hdr *hr, *h;
1187 const pj_str_t HNAME = { "Service-Route", 13 };
1188 const pj_str_t HROUTE = { "Route", 5 };
1189 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001190 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001191
1192 /* Find and parse Service-Route headers */
1193 for (;;) {
1194 char saved;
1195 int parsed_len;
1196
1197 /* Find Service-Route header */
1198 hsr = (pjsip_generic_string_hdr*)
1199 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1200 if (!hsr)
1201 break;
1202
1203 /* Parse as Route header since the syntax is similar. This may
1204 * return more than one headers.
1205 */
1206 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1207 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1208 hr = (pjsip_route_hdr*)
1209 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1210 hsr->hvalue.slen, &parsed_len);
1211 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1212
1213 if (hr == NULL) {
1214 /* Error */
1215 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1216 return;
1217 }
1218
1219 /* Save each URI in the result */
1220 h = hr;
1221 do {
1222 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1223 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1224 {
1225 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1226 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1227 return;
1228 }
1229
1230 uri[uri_cnt++] = h->name_addr.uri;
1231 h = h->next;
1232 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1233
1234 if (h != hr) {
1235 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1236 return;
1237 }
1238
1239 /* Prepare to find next Service-Route header */
1240 hsr = hsr->next;
1241 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1242 break;
1243 }
1244
1245 if (uri_cnt == 0)
1246 return;
1247
1248 /*
1249 * Update account's route set
1250 */
1251
1252 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001253 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001254 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1255 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1256 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001257 i<rcnt;
1258 ++i)
1259 {
1260 pjsip_route_hdr *prev = hr->prev;
1261 pj_list_erase(hr);
1262 hr = prev;
1263 }
1264 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001265
1266 /* Then append the Service-Route URIs */
1267 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001268 hr = pjsip_route_hdr_create(acc->pool);
1269 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001270 pj_list_push_back(&acc->route_set, hr);
1271 }
1272
1273 /* Done */
1274
1275 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1276 acc->index, uri_cnt));
1277}
1278
Benny Prijonobddef2c2007-10-31 13:28:08 +00001279
1280/* Keep alive timer callback */
1281static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1282{
1283 pjsua_acc *acc;
1284 pjsip_tpselector tp_sel;
1285 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001286 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001287 pj_status_t status;
1288
1289 PJ_UNUSED_ARG(th);
1290
1291 PJSUA_LOCK();
1292
1293 te->id = PJ_FALSE;
1294
1295 acc = (pjsua_acc*) te->user_data;
1296
1297 /* Select the transport to send the packet */
1298 pj_bzero(&tp_sel, sizeof(tp_sel));
1299 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1300 tp_sel.u.transport = acc->ka_transport;
1301
1302 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001303 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001304 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001305 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001306
1307 /* Send raw packet */
1308 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1309 PJSIP_TRANSPORT_UDP, &tp_sel,
1310 NULL, acc->cfg.ka_data.ptr,
1311 acc->cfg.ka_data.slen,
1312 &acc->ka_target, acc->ka_target_len,
1313 NULL, NULL);
1314
1315 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1316 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1317 }
1318
1319 /* Reschedule next timer */
1320 delay.sec = acc->cfg.ka_interval;
1321 delay.msec = 0;
1322 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1323 if (status == PJ_SUCCESS) {
1324 te->id = PJ_TRUE;
1325 } else {
1326 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1327 }
1328
1329 PJSUA_UNLOCK();
1330}
1331
1332
1333/* Update keep-alive for the account */
1334static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1335 struct pjsip_regc_cbparam *param)
1336{
1337 /* In all cases, stop keep-alive timer if it's running. */
1338 if (acc->ka_timer.id) {
1339 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1340 acc->ka_timer.id = PJ_FALSE;
1341
1342 pjsip_transport_dec_ref(acc->ka_transport);
1343 acc->ka_transport = NULL;
1344 }
1345
1346 if (start) {
1347 pj_time_val delay;
1348 pj_status_t status;
1349
1350 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001351 * - ka_interval is not zero in the account, and
1352 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001353 *
1354 * Previously we only enabled keep-alive when STUN is enabled, since
1355 * we thought that keep-alive is only needed in Internet situation.
1356 * But it has been discovered that Windows Firewall on WinXP also
1357 * needs to be kept-alive, otherwise incoming packets will be dropped.
1358 * So because of this, now keep-alive is always enabled for UDP,
1359 * regardless of whether STUN is enabled or not.
1360 *
1361 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1362 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001363 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001364 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001365 acc->cfg.ka_interval == 0 ||
1366 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1367 {
1368 /* Keep alive is not necessary */
1369 return;
1370 }
1371
1372 /* Save transport and destination address. */
1373 acc->ka_transport = param->rdata->tp_info.transport;
1374 pjsip_transport_add_ref(acc->ka_transport);
1375 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1376 param->rdata->pkt_info.src_addr_len);
1377 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1378
1379 /* Setup and start the timer */
1380 acc->ka_timer.cb = &keep_alive_timer_cb;
1381 acc->ka_timer.user_data = (void*)acc;
1382
1383 delay.sec = acc->cfg.ka_interval;
1384 delay.msec = 0;
1385 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1386 &delay);
1387 if (status == PJ_SUCCESS) {
1388 acc->ka_timer.id = PJ_TRUE;
1389 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1390 "destination:%s:%d, interval:%ds",
1391 acc->index,
1392 param->rdata->pkt_info.src_name,
1393 param->rdata->pkt_info.src_port,
1394 acc->cfg.ka_interval));
1395 } else {
1396 acc->ka_timer.id = PJ_FALSE;
1397 pjsip_transport_dec_ref(acc->ka_transport);
1398 acc->ka_transport = NULL;
1399 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1400 }
1401 }
1402}
1403
1404
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001405/*
1406 * This callback is called by pjsip_regc when outgoing register
1407 * request has completed.
1408 */
1409static void regc_cb(struct pjsip_regc_cbparam *param)
1410{
1411
Benny Prijonoa1e69682007-05-11 15:14:34 +00001412 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001413
Benny Prijono15b02302007-09-27 14:07:07 +00001414 if (param->regc != acc->regc)
1415 return;
1416
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001417 PJSUA_LOCK();
1418
1419 /*
1420 * Print registration status.
1421 */
1422 if (param->status!=PJ_SUCCESS) {
1423 pjsua_perror(THIS_FILE, "SIP registration error",
1424 param->status);
1425 pjsip_regc_destroy(acc->regc);
1426 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001427 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001428
Benny Prijonobddef2c2007-10-31 13:28:08 +00001429 /* Stop keep-alive timer if any. */
1430 update_keep_alive(acc, PJ_FALSE, NULL);
1431
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001432 } else if (param->code < 0 || param->code >= 300) {
1433 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1434 param->code,
1435 (int)param->reason.slen, param->reason.ptr));
1436 pjsip_regc_destroy(acc->regc);
1437 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001438 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001439
Benny Prijonobddef2c2007-10-31 13:28:08 +00001440 /* Stop keep-alive timer if any. */
1441 update_keep_alive(acc, PJ_FALSE, NULL);
1442
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001443 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1444
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001445 /* Update auto registration flag */
1446 acc->auto_rereg.active = PJ_FALSE;
1447 acc->auto_rereg.attempt_cnt = 0;
1448
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001449 if (param->expiration < 1) {
1450 pjsip_regc_destroy(acc->regc);
1451 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001452 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001453
1454 /* Stop keep-alive timer if any. */
1455 update_keep_alive(acc, PJ_FALSE, NULL);
1456
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001457 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1458 pjsua_var.acc[acc->index].cfg.id.ptr));
1459 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001460 /* Check NAT bound address */
1461 if (acc_check_nat_addr(acc, param)) {
1462 /* Update address, don't notify application yet */
1463 PJSUA_UNLOCK();
1464 return;
1465 }
1466
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001467 /* Check and update Service-Route header */
1468 update_service_route(acc, param->rdata);
1469
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001470 PJ_LOG(3, (THIS_FILE,
1471 "%s: registration success, status=%d (%.*s), "
1472 "will re-register in %d seconds",
1473 pjsua_var.acc[acc->index].cfg.id.ptr,
1474 param->code,
1475 (int)param->reason.slen, param->reason.ptr,
1476 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001477
Benny Prijonobddef2c2007-10-31 13:28:08 +00001478 /* Start keep-alive timer if necessary. */
1479 update_keep_alive(acc, PJ_TRUE, param);
1480
Benny Prijono8b6834f2007-02-24 13:29:22 +00001481 /* Send initial PUBLISH if it is enabled */
1482 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1483 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001484
1485 /* Subscribe to MWI, if it's enabled */
1486 if (acc->cfg.mwi_enabled)
1487 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001488 }
1489
1490 } else {
1491 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1492 }
1493
1494 acc->reg_last_err = param->status;
1495 acc->reg_last_code = param->code;
1496
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001497 /* Check if we need to auto retry registration. Basically, registration
1498 * failure codes triggering auto-retry are those of temporal failures
1499 * considered to be recoverable in relatively short term.
1500 */
1501 if (acc->cfg.reg_retry_interval &&
1502 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1503 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1504 param->code == PJSIP_SC_BAD_GATEWAY ||
1505 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1506 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1507 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1508 {
1509 schedule_reregistration(acc);
1510 }
1511
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00001512 if (pjsua_var.ua_cfg.cb.on_reg_state)
1513 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1514
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001515 PJSUA_UNLOCK();
1516}
1517
1518
1519/*
1520 * Initialize client registration.
1521 */
1522static pj_status_t pjsua_regc_init(int acc_id)
1523{
1524 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001525 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001526 pj_status_t status;
1527
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001528 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001529 acc = &pjsua_var.acc[acc_id];
1530
1531 if (acc->cfg.reg_uri.slen == 0) {
1532 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1533 return PJ_SUCCESS;
1534 }
1535
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001536 /* Destroy existing session, if any */
1537 if (acc->regc) {
1538 pjsip_regc_destroy(acc->regc);
1539 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001540 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001541 }
1542
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001543 /* initialize SIP registration if registrar is configured */
1544
1545 status = pjsip_regc_create( pjsua_var.endpt,
1546 acc, &regc_cb, &acc->regc);
1547
1548 if (status != PJ_SUCCESS) {
1549 pjsua_perror(THIS_FILE, "Unable to create client registration",
1550 status);
1551 return status;
1552 }
1553
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001554 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001555
1556 if (acc->contact.slen == 0) {
1557 pj_str_t tmp_contact;
1558
1559 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1560 acc_id, &acc->cfg.reg_uri);
1561 if (status != PJ_SUCCESS) {
1562 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1563 " for registration",
1564 status);
1565 pjsip_regc_destroy(acc->regc);
1566 pj_pool_release(pool);
1567 acc->regc = NULL;
1568 return status;
1569 }
1570
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001571 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001572 }
1573
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001574 status = pjsip_regc_init( acc->regc,
1575 &acc->cfg.reg_uri,
1576 &acc->cfg.id,
1577 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001578 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001579 acc->cfg.reg_timeout);
1580 if (status != PJ_SUCCESS) {
1581 pjsua_perror(THIS_FILE,
1582 "Client registration initialization error",
1583 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001584 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001585 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001586 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001587 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001588 return status;
1589 }
1590
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001591 /* If account is locked to specific transport, then set transport to
1592 * the client registration.
1593 */
1594 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1595 pjsip_tpselector tp_sel;
1596
1597 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1598 pjsip_regc_set_transport(acc->regc, &tp_sel);
1599 }
1600
1601
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001602 /* Set credentials
1603 */
1604 if (acc->cred_cnt) {
1605 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1606 }
1607
Benny Prijono48ab2b72007-11-08 09:24:30 +00001608 /* Set authentication preference */
1609 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1610
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001611 /* Set route-set
1612 */
1613 if (!pj_list_empty(&acc->route_set)) {
1614 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1615 }
1616
Benny Prijono8fc6de02006-11-11 21:25:55 +00001617 /* Add other request headers. */
1618 if (pjsua_var.ua_cfg.user_agent.slen) {
1619 pjsip_hdr hdr_list;
1620 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1621 pjsip_generic_string_hdr *h;
1622
Benny Prijono8fc6de02006-11-11 21:25:55 +00001623 pj_list_init(&hdr_list);
1624
1625 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1626 &pjsua_var.ua_cfg.user_agent);
1627 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1628
1629 pjsip_regc_add_headers(acc->regc, &hdr_list);
1630 }
1631
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001632 pj_pool_release(pool);
1633
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001634 return PJ_SUCCESS;
1635}
1636
1637
1638/*
1639 * Update registration or perform unregistration.
1640 */
1641PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1642 pj_bool_t renew)
1643{
1644 pj_status_t status = 0;
1645 pjsip_tx_data *tdata = 0;
1646
1647 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1648 PJ_EINVAL);
1649 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1650
1651 PJSUA_LOCK();
1652
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001653 /* Cancel any re-registration timer */
1654 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1655
1656 /* Reset pointer to registration transport */
1657 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1658
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001659 if (renew) {
1660 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001661 status = pjsua_regc_init(acc_id);
1662 if (status != PJ_SUCCESS) {
1663 pjsua_perror(THIS_FILE, "Unable to create registration",
1664 status);
1665 goto on_return;
1666 }
1667 }
1668 if (!pjsua_var.acc[acc_id].regc) {
1669 status = PJ_EINVALIDOP;
1670 goto on_return;
1671 }
1672
1673 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1674 &tdata);
1675
Benny Prijono28f673a2007-10-15 07:04:59 +00001676 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1677 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1678 pjsip_authorization_hdr *h;
1679 char *uri;
1680 int d;
1681
1682 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1683 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1684 uri, acc->cfg.reg_uri.slen+10);
1685 pj_assert(d > 0);
1686
1687 h = pjsip_authorization_hdr_create(tdata->pool);
1688 h->scheme = pj_str("Digest");
1689 h->credential.digest.username = acc->cred[0].username;
1690 h->credential.digest.realm = acc->srv_domain;
1691 h->credential.digest.uri = pj_str(uri);
1692 h->credential.digest.algorithm = pj_str("md5");
1693
1694 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1695 }
1696
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001697 } else {
1698 if (pjsua_var.acc[acc_id].regc == NULL) {
1699 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1700 status = PJ_EINVALIDOP;
1701 goto on_return;
1702 }
Benny Prijono166d5022010-02-10 14:24:48 +00001703
1704 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
1705
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001706 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1707 }
1708
Benny Prijono56315612006-07-18 14:39:40 +00001709 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001710 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001711 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001712 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001713
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001714 /* Update pointer to registration transport */
1715 if (status == PJ_SUCCESS) {
1716 pjsip_regc_info reg_info;
1717
1718 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
1719 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
1720 }
1721
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001722 if (status != PJ_SUCCESS) {
1723 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1724 status);
1725 } else {
1726 PJ_LOG(3,(THIS_FILE, "%s sent",
1727 (renew? "Registration" : "Unregistration")));
1728 }
1729
1730on_return:
1731 PJSUA_UNLOCK();
1732 return status;
1733}
1734
1735
1736/*
1737 * Get account information.
1738 */
1739PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1740 pjsua_acc_info *info)
1741{
1742 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1743 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1744
1745 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001746 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001747
Benny Prijonoac623b32006-07-03 15:19:31 +00001748 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001749
1750 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1751 PJ_EINVAL);
1752 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1753
1754 PJSUA_LOCK();
1755
1756 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1757 PJSUA_UNLOCK();
1758 return PJ_EINVALIDOP;
1759 }
1760
1761 info->id = acc_id;
1762 info->is_default = (pjsua_var.default_acc == acc_id);
1763 info->acc_uri = acc_cfg->id;
1764 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1765 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001766 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1767 if (info->rpid.note.slen)
1768 info->online_status_text = info->rpid.note;
1769 else if (info->online_status)
1770 info->online_status_text = pj_str("Online");
1771 else
1772 info->online_status_text = pj_str("Offline");
1773
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001774 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001775 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001776 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1777 info->status_text = pj_str(info->buf_);
1778 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001779 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001780 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001781 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1782 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001783 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001784 info->status_text = pj_str("not registered");
1785 }
1786 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001787 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001788 info->status_text = pj_str("In Progress");
1789 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001790 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001791 info->status_text = pj_str("does not register");
1792 }
1793
1794 if (acc->regc) {
1795 pjsip_regc_info regc_info;
1796 pjsip_regc_get_info(acc->regc, &regc_info);
1797 info->expires = regc_info.next_reg;
1798 } else {
1799 info->expires = -1;
1800 }
1801
1802 PJSUA_UNLOCK();
1803
1804 return PJ_SUCCESS;
1805
1806}
1807
1808
1809/*
1810 * Enum accounts all account ids.
1811 */
1812PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1813 unsigned *count )
1814{
1815 unsigned i, c;
1816
1817 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1818
1819 PJSUA_LOCK();
1820
1821 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1822 if (!pjsua_var.acc[i].valid)
1823 continue;
1824 ids[c] = i;
1825 ++c;
1826 }
1827
1828 *count = c;
1829
1830 PJSUA_UNLOCK();
1831
1832 return PJ_SUCCESS;
1833}
1834
1835
1836/*
1837 * Enum accounts info.
1838 */
1839PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1840 unsigned *count )
1841{
1842 unsigned i, c;
1843
1844 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1845
1846 PJSUA_LOCK();
1847
1848 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1849 if (!pjsua_var.acc[i].valid)
1850 continue;
1851
1852 pjsua_acc_get_info(i, &info[c]);
1853 ++c;
1854 }
1855
1856 *count = c;
1857
1858 PJSUA_UNLOCK();
1859
1860 return PJ_SUCCESS;
1861}
1862
1863
1864/*
1865 * This is an internal function to find the most appropriate account to
1866 * used to reach to the specified URL.
1867 */
1868PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1869{
1870 pj_str_t tmp;
1871 pjsip_uri *uri;
1872 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001873 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001874 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001875
1876 PJSUA_LOCK();
1877
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001878 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001879
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001880 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001881
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001882 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001883 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001884 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001885 PJSUA_UNLOCK();
1886 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001887 }
1888
1889 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1890 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1891 {
1892 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001893 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1894 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001895 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001896 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001897 break;
1898 }
1899
Benny Prijono093d3022006-09-24 00:07:11 +00001900 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001901 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001902 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001903 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001904 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001905 }
1906
1907 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001908 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001909 PJSUA_UNLOCK();
1910 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001911 }
1912
Benny Prijonoa1e69682007-05-11 15:14:34 +00001913 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001914
Benny Prijonob4a17c92006-07-10 14:40:21 +00001915 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001916 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1917 unsigned acc_id = pjsua_var.acc_ids[i];
1918 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1919 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1920 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001921 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001922 PJSUA_UNLOCK();
1923 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001924 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001925 }
1926
Benny Prijonob4a17c92006-07-10 14:40:21 +00001927 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001928 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1929 unsigned acc_id = pjsua_var.acc_ids[i];
1930 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1931 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001932 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001933 PJSUA_UNLOCK();
1934 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001935 }
1936 }
1937
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001938
Benny Prijono093d3022006-09-24 00:07:11 +00001939 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001940 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001941 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001942 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001943}
1944
1945
1946/*
1947 * This is an internal function to find the most appropriate account to be
1948 * used to handle incoming calls.
1949 */
1950PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1951{
1952 pjsip_uri *uri;
1953 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001954 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001955
Benny Prijono371cf0a2007-06-19 00:35:37 +00001956 /* Check that there's at least one account configured */
1957 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1958
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001959 uri = rdata->msg_info.to->uri;
1960
1961 /* Just return default account if To URI is not SIP: */
1962 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1963 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1964 {
1965 return pjsua_var.default_acc;
1966 }
1967
1968
1969 PJSUA_LOCK();
1970
1971 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1972
1973 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001974 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1975 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001976 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1977
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001978 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001979 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001980 {
1981 /* Match ! */
1982 PJSUA_UNLOCK();
1983 return acc_id;
1984 }
1985 }
1986
Benny Prijono093d3022006-09-24 00:07:11 +00001987 /* No matching account, try match domain part only. */
1988 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1989 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001990 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1991
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001992 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001993 /* Match ! */
1994 PJSUA_UNLOCK();
1995 return acc_id;
1996 }
1997 }
1998
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001999 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00002000 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2001 unsigned acc_id = pjsua_var.acc_ids[i];
2002 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2003
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002004 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
2005
2006 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2007 pjsip_transport_type_e type;
2008 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2009 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
2010 type = PJSIP_TRANSPORT_UDP;
2011
2012 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
2013 continue;
2014 }
2015
Benny Prijono093d3022006-09-24 00:07:11 +00002016 /* Match ! */
2017 PJSUA_UNLOCK();
2018 return acc_id;
2019 }
2020 }
2021
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002022 /* Still no match, use default account */
2023 PJSUA_UNLOCK();
2024 return pjsua_var.default_acc;
2025}
2026
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002027
Benny Prijonofff245c2007-04-02 11:44:47 +00002028/*
2029 * Create arbitrary requests for this account.
2030 */
2031PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2032 const pjsip_method *method,
2033 const pj_str_t *target,
2034 pjsip_tx_data **p_tdata)
2035{
2036 pjsip_tx_data *tdata;
2037 pjsua_acc *acc;
2038 pjsip_route_hdr *r;
2039 pj_status_t status;
2040
2041 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2042 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2043
2044 acc = &pjsua_var.acc[acc_id];
2045
2046 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2047 &acc->cfg.id, target,
2048 NULL, NULL, -1, NULL, &tdata);
2049 if (status != PJ_SUCCESS) {
2050 pjsua_perror(THIS_FILE, "Unable to create request", status);
2051 return status;
2052 }
2053
2054 /* Copy routeset */
2055 r = acc->route_set.next;
2056 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002057 pjsip_msg_add_hdr(tdata->msg,
2058 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002059 r = r->next;
2060 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002061
2062 /* If account is locked to specific transport, then set that transport to
2063 * the transmit data.
2064 */
2065 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2066 pjsip_tpselector tp_sel;
2067
2068 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2069 pjsip_tx_data_set_transport(tdata, &tp_sel);
2070 }
2071
Benny Prijonofff245c2007-04-02 11:44:47 +00002072 /* Done */
2073 *p_tdata = tdata;
2074 return PJ_SUCCESS;
2075}
2076
2077
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002078PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2079 pj_str_t *contact,
2080 pjsua_acc_id acc_id,
2081 const pj_str_t *suri)
2082{
2083 pjsua_acc *acc;
2084 pjsip_sip_uri *sip_uri;
2085 pj_status_t status;
2086 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2087 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002088 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002089 unsigned flag;
2090 int secure;
2091 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002092 const char *beginquote, *endquote;
2093 char transport_param[32];
2094
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002095
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002096 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002097 acc = &pjsua_var.acc[acc_id];
2098
Benny Prijonof75eceb2007-03-23 19:09:54 +00002099 /* If force_contact is configured, then use use it */
2100 if (acc->cfg.force_contact.slen) {
2101 *contact = acc->cfg.force_contact;
2102 return PJ_SUCCESS;
2103 }
2104
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002105 /* If route-set is configured for the account, then URI is the
2106 * first entry of the route-set.
2107 */
2108 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002109 sip_uri = (pjsip_sip_uri*)
2110 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002111 } else {
2112 pj_str_t tmp;
2113 pjsip_uri *uri;
2114
2115 pj_strdup_with_null(pool, &tmp, suri);
2116
2117 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2118 if (uri == NULL)
2119 return PJSIP_EINVALIDURI;
2120
2121 /* For non-SIP scheme, route set should be configured */
2122 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2123 return PJSIP_EINVALIDREQURI;
2124
Benny Prijono8c7a6172007-02-18 21:17:46 +00002125 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002126 }
2127
2128 /* Get transport type of the URI */
2129 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2130 tp_type = PJSIP_TRANSPORT_TLS;
2131 else if (sip_uri->transport_param.slen == 0) {
2132 tp_type = PJSIP_TRANSPORT_UDP;
2133 } else
2134 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2135
2136 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2137 return PJSIP_EUNSUPTRANSPORT;
2138
Benny Prijonod0bd4982007-12-02 15:40:52 +00002139 /* If destination URI specifies IPv6, then set transport type
2140 * to use IPv6 as well.
2141 */
Benny Prijono19b29372007-12-05 04:08:40 +00002142 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002143 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2144
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002145 flag = pjsip_transport_get_flag_from_type(tp_type);
2146 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2147
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002148 /* Init transport selector. */
2149 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2150
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002151 /* Get local address suitable to send request from */
2152 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002153 pool, tp_type, &tp_sel,
2154 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002155 if (status != PJ_SUCCESS)
2156 return status;
2157
Benny Prijonod0bd4982007-12-02 15:40:52 +00002158 /* Enclose IPv6 address in square brackets */
2159 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2160 beginquote = "[";
2161 endquote = "]";
2162 } else {
2163 beginquote = endquote = "";
2164 }
2165
2166 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002167 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002168 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2169 ";transport=%s",
2170 pjsip_transport_get_type_name(tp_type));
2171 } else {
2172 transport_param[0] = '\0';
2173 }
2174
2175
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002176 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002177 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002178 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002179 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002180 (int)acc->display.slen,
2181 acc->display.ptr,
2182 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002183 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002184 (int)acc->user_part.slen,
2185 acc->user_part.ptr,
2186 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002187 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002188 (int)local_addr.slen,
2189 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002190 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002191 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002192 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002193 (int)acc->cfg.contact_uri_params.slen,
2194 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002195 (int)acc->cfg.contact_params.slen,
2196 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002197
2198 return PJ_SUCCESS;
2199}
2200
2201
2202
2203PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2204 pj_str_t *contact,
2205 pjsua_acc_id acc_id,
2206 pjsip_rx_data *rdata )
2207{
2208 /*
2209 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2210 * If the request that initiated the dialog contained a SIPS URI
2211 * in the Request-URI or in the top Record-Route header field value,
2212 * if there was any, or the Contact header field if there was no
2213 * Record-Route header field, the Contact header field in the response
2214 * MUST be a SIPS URI.
2215 */
2216 pjsua_acc *acc;
2217 pjsip_sip_uri *sip_uri;
2218 pj_status_t status;
2219 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2220 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002221 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002222 unsigned flag;
2223 int secure;
2224 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002225 const char *beginquote, *endquote;
2226 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002227
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002228 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002229 acc = &pjsua_var.acc[acc_id];
2230
Benny Prijonof75eceb2007-03-23 19:09:54 +00002231 /* If force_contact is configured, then use use it */
2232 if (acc->cfg.force_contact.slen) {
2233 *contact = acc->cfg.force_contact;
2234 return PJ_SUCCESS;
2235 }
2236
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002237 /* If Record-Route is present, then URI is the top Record-Route. */
2238 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002239 sip_uri = (pjsip_sip_uri*)
2240 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002241 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002242 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002243 pjsip_contact_hdr *h_contact;
2244 pjsip_uri *uri = NULL;
2245
Benny Prijonoa330d452008-08-05 20:14:39 +00002246 /* Otherwise URI is Contact URI.
2247 * Iterate the Contact URI until we find sip: or sips: scheme.
2248 */
2249 do {
2250 h_contact = (pjsip_contact_hdr*)
2251 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2252 pos);
2253 if (h_contact) {
Benny Prijono8b33bba2010-06-02 03:03:43 +00002254 if (h_contact->uri)
2255 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2256 else
2257 uri = NULL;
2258 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2259 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
Benny Prijonoa330d452008-08-05 20:14:39 +00002260 {
2261 pos = (pjsip_hdr*)h_contact->next;
2262 if (pos == &rdata->msg_info.msg->hdr)
2263 h_contact = NULL;
2264 } else {
2265 break;
2266 }
2267 }
2268 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002269
2270
2271 /* Or if Contact URI is not present, take the remote URI from
2272 * the From URI.
2273 */
2274 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002275 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002276
2277
2278 /* Can only do sip/sips scheme at present. */
2279 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2280 return PJSIP_EINVALIDREQURI;
2281
Benny Prijono8c7a6172007-02-18 21:17:46 +00002282 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002283 }
2284
2285 /* Get transport type of the URI */
2286 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2287 tp_type = PJSIP_TRANSPORT_TLS;
2288 else if (sip_uri->transport_param.slen == 0) {
2289 tp_type = PJSIP_TRANSPORT_UDP;
2290 } else
2291 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002292
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002293 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2294 return PJSIP_EUNSUPTRANSPORT;
2295
Benny Prijonod0bd4982007-12-02 15:40:52 +00002296 /* If destination URI specifies IPv6, then set transport type
2297 * to use IPv6 as well.
2298 */
2299 if (pj_strchr(&sip_uri->host, ':'))
2300 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2301
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002302 flag = pjsip_transport_get_flag_from_type(tp_type);
2303 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2304
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002305 /* Init transport selector. */
2306 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2307
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002308 /* Get local address suitable to send request from */
2309 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002310 pool, tp_type, &tp_sel,
2311 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002312 if (status != PJ_SUCCESS)
2313 return status;
2314
Benny Prijonod0bd4982007-12-02 15:40:52 +00002315 /* Enclose IPv6 address in square brackets */
2316 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2317 beginquote = "[";
2318 endquote = "]";
2319 } else {
2320 beginquote = endquote = "";
2321 }
2322
2323 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002324 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002325 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2326 ";transport=%s",
2327 pjsip_transport_get_type_name(tp_type));
2328 } else {
2329 transport_param[0] = '\0';
2330 }
2331
2332
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002333 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002334 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002335 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002336 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002337 (int)acc->display.slen,
2338 acc->display.ptr,
2339 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002340 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002341 (int)acc->user_part.slen,
2342 acc->user_part.ptr,
2343 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002344 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002345 (int)local_addr.slen,
2346 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002347 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002348 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002349 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002350 (int)acc->cfg.contact_uri_params.slen,
2351 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002352 (int)acc->cfg.contact_params.slen,
2353 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002354
2355 return PJ_SUCCESS;
2356}
2357
2358
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002359PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2360 pjsua_transport_id tp_id)
2361{
2362 pjsua_acc *acc;
2363
2364 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2365 acc = &pjsua_var.acc[acc_id];
2366
Benny Prijonoa1e69682007-05-11 15:14:34 +00002367 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002368 PJ_EINVAL);
2369
2370 acc->cfg.transport_id = tp_id;
2371
2372 return PJ_SUCCESS;
2373}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002374
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002375
2376/* Auto re-registration timeout callback */
2377static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2378{
2379 pjsua_acc *acc;
2380 pj_status_t status;
2381
2382 PJ_UNUSED_ARG(th);
2383 acc = (pjsua_acc*) te->user_data;
2384 pj_assert(acc);
2385
2386 PJSUA_LOCK();
2387
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002388 /* Check if the reregistration timer is still valid, e.g: while waiting
2389 * timeout timer application might have deleted the account or disabled
2390 * the auto-reregistration.
2391 */
2392 if (!acc->valid || !acc->auto_rereg.active ||
2393 acc->cfg.reg_retry_interval == 0)
2394 {
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002395 goto on_return;
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002396 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002397
2398 /* Start re-registration */
2399 acc->auto_rereg.attempt_cnt++;
2400 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2401 if (status != PJ_SUCCESS)
2402 schedule_reregistration(acc);
2403
Nanang Izzuddin66580002010-04-14 08:12:08 +00002404on_return:
2405 PJSUA_UNLOCK();
2406}
2407
2408
2409/* Schedule reregistration for specified account. Note that the first
2410 * re-registration after a registration failure will be done immediately.
2411 * Also note that this function should be called within PJSUA mutex.
2412 */
2413static void schedule_reregistration(pjsua_acc *acc)
2414{
2415 pj_time_val delay;
2416
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002417 pj_assert(acc);
2418
2419 /* Validate the account and re-registration feature status */
2420 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
2421 return;
2422 }
Nanang Izzuddin66580002010-04-14 08:12:08 +00002423
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002424 /* If configured, disconnect calls of this account after the first
2425 * reregistration attempt failed.
2426 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002427 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002428 {
2429 unsigned i, cnt;
2430
2431 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2432 if (pjsua_var.calls[i].acc_id == acc->index) {
2433 pjsua_call_hangup(i, 0, NULL, NULL);
2434 ++cnt;
2435 }
2436 }
2437
2438 if (cnt) {
2439 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2440 "after reregistration attempt failed",
2441 cnt, acc->index));
2442 }
2443 }
2444
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002445 /* Cancel any re-registration timer */
2446 pjsua_cancel_timer(&acc->auto_rereg.timer);
2447
2448 /* Update re-registration flag */
2449 acc->auto_rereg.active = PJ_TRUE;
2450
2451 /* Set up timer for reregistration */
2452 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2453 acc->auto_rereg.timer.user_data = acc;
2454
2455 /* Reregistration attempt. The first attempt will be done immediately. */
2456 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2457 delay.msec = 0;
2458 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2459}
2460
2461
2462/* Internal function to perform auto-reregistration on transport
2463 * connection/disconnection events.
2464 */
2465void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2466 pjsip_transport_state state,
2467 const pjsip_transport_state_info *info)
2468{
2469 unsigned i;
2470
2471 PJ_UNUSED_ARG(info);
2472
2473 /* Only care for transport disconnection events */
2474 if (state != PJSIP_TP_STATE_DISCONNECTED)
2475 return;
2476
2477 /* Shutdown this transport, to make sure that the transport manager
2478 * will create a new transport for reconnection.
2479 */
2480 pjsip_transport_shutdown(tp);
2481
2482 PJSUA_LOCK();
2483
2484 /* Enumerate accounts using this transport and perform actions
2485 * based on the transport state.
2486 */
2487 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2488 pjsua_acc *acc = &pjsua_var.acc[i];
2489
2490 /* Skip if this account is not valid OR auto re-registration
2491 * feature is disabled OR this transport is not used by this account.
2492 */
2493 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2494 tp != acc->auto_rereg.reg_tp)
2495 {
2496 continue;
2497 }
2498
2499 /* Schedule reregistration for this account */
2500 schedule_reregistration(acc);
2501 }
2502
2503 PJSUA_UNLOCK();
2504}