blob: 027b88dea52b9a5c1c8d2b7f4a31b4576151baea [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
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +000094 pj_list_init(&dst->reg_hdr_list);
95 if (!pj_list_empty(&src->reg_hdr_list)) {
96 const pjsip_hdr *hdr;
97
98 hdr = src->reg_hdr_list.next;
99 while (hdr != &src->reg_hdr_list) {
100 pj_list_push_back(&dst->reg_hdr_list, pjsip_hdr_clone(pool, hdr));
101 hdr = hdr->next;
102 }
103 }
104
Benny Prijonobddef2c2007-10-31 13:28:08 +0000105 dst->ka_interval = src->ka_interval;
106 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000107}
108
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000109/*
110 * Calculate CRC of proxy list.
111 */
112static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt)
113{
114 pj_crc32_context ctx;
115 unsigned i;
116
117 pj_crc32_init(&ctx);
118 for (i=0; i<cnt; ++i) {
119 pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen);
120 }
121
122 return pj_crc32_final(&ctx);
123}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000124
125/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000126 * Initialize a new account (after configuration is set).
127 */
128static pj_status_t initialize_acc(unsigned acc_id)
129{
130 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
131 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000132 pjsip_name_addr *name_addr;
Benny Prijonoc7545782010-09-28 07:43:18 +0000133 pjsip_sip_uri *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000134 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000135 unsigned i;
136
137 /* Need to parse local_uri to get the elements: */
138
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000139 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000140 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000141 acc_cfg->id.slen,
142 PJSIP_PARSE_URI_AS_NAMEADDR);
143 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000144 pjsua_perror(THIS_FILE, "Invalid local URI",
145 PJSIP_EINVALIDURI);
146 return PJSIP_EINVALIDURI;
147 }
148
149 /* Local URI MUST be a SIP or SIPS: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000150 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
151 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000152 {
Benny Prijonoc7545782010-09-28 07:43:18 +0000153 acc->display = name_addr->display;
154 acc->user_part = name_addr->display;
155 acc->srv_domain = pj_str("");
156 acc->srv_port = 0;
157 } else {
158 pjsip_sip_uri *sip_uri;
159
160 /* Get the SIP URI object: */
161 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
162
163 /* Save the user and domain part. These will be used when finding an
164 * account for incoming requests.
165 */
166 acc->display = name_addr->display;
167 acc->user_part = sip_uri->user;
168 acc->srv_domain = sip_uri->host;
169 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000170 }
171
172
Benny Prijonob4a17c92006-07-10 14:40:21 +0000173 /* Parse registrar URI, if any */
174 if (acc_cfg->reg_uri.slen) {
175 pjsip_uri *reg_uri;
176
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000177 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000178 acc_cfg->reg_uri.slen, 0);
179 if (reg_uri == NULL) {
180 pjsua_perror(THIS_FILE, "Invalid registrar URI",
181 PJSIP_EINVALIDURI);
182 return PJSIP_EINVALIDURI;
183 }
184
185 /* Registrar URI MUST be a SIP or SIPS: */
186 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
187 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
188 {
189 pjsua_perror(THIS_FILE, "Invalid registar URI",
190 PJSIP_EINVALIDSCHEME);
191 return PJSIP_EINVALIDSCHEME;
192 }
193
194 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
195
196 } else {
197 sip_reg_uri = NULL;
198 }
199
Benny Prijonob4a17c92006-07-10 14:40:21 +0000200 if (sip_reg_uri) {
201 acc->srv_port = sip_reg_uri->port;
202 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000203
204 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000205 //if (acc_cfg->contact.slen == 0) {
206 // acc_cfg->contact = acc_cfg->id;
207 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000208
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000209 /* Build account route-set from outbound proxies and route set from
210 * account configuration.
211 */
212 pj_list_init(&acc->route_set);
213
Benny Prijono29c8ca32010-06-22 06:02:13 +0000214 if (!pj_list_empty(&pjsua_var.outbound_proxy)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000215 pjsip_route_hdr *r;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000216
Benny Prijono29c8ca32010-06-22 06:02:13 +0000217 r = pjsua_var.outbound_proxy.next;
218 while (r != &pjsua_var.outbound_proxy) {
219 pj_list_push_back(&acc->route_set,
220 pjsip_hdr_shallow_clone(acc->pool, r));
221 r = r->next;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000222 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000223 }
224
225 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
226 pj_str_t hname = { "Route", 5};
227 pjsip_route_hdr *r;
228 pj_str_t tmp;
229
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000230 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000231 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000232 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233 if (r == NULL) {
234 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
235 PJ_EINVAL);
236 return PJ_EINVAL;
237 }
238 pj_list_push_back(&acc->route_set, r);
239 }
240
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000241 /* Concatenate credentials from account config and global config */
242 acc->cred_cnt = 0;
243 for (i=0; i<acc_cfg->cred_count; ++i) {
244 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
245 }
246 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
247 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
248 {
249 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
250 }
251
Benny Prijono07fe2302010-06-24 12:33:18 +0000252 /* If ICE is enabled, add "+sip.ice" media feature tag in account's
253 * contact params.
254 */
255#if PJSUA_ADD_ICE_TAGS
256 if (pjsua_var.media_cfg.enable_ice) {
257 unsigned new_len;
258 pj_str_t new_prm;
259
260 new_len = acc_cfg->contact_params.slen + 10;
261 new_prm.ptr = (char*)pj_pool_alloc(acc->pool, new_len);
262 pj_strcpy(&new_prm, &acc_cfg->contact_params);
263 pj_strcat2(&new_prm, ";+sip.ice");
264 acc_cfg->contact_params = new_prm;
265 }
266#endif
267
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000268 status = pjsua_pres_init_acc(acc_id);
269 if (status != PJ_SUCCESS)
270 return status;
271
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000272 /* Mark account as valid */
273 pjsua_var.acc[acc_id].valid = PJ_TRUE;
274
Benny Prijono093d3022006-09-24 00:07:11 +0000275 /* Insert account ID into account ID array, sorted by priority */
276 for (i=0; i<pjsua_var.acc_cnt; ++i) {
277 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
278 pjsua_var.acc[acc_id].cfg.priority)
279 {
280 break;
281 }
282 }
283 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
284 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000285
286 return PJ_SUCCESS;
287}
288
289
290/*
291 * Add a new account to pjsua.
292 */
293PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
294 pj_bool_t is_default,
295 pjsua_acc_id *p_acc_id)
296{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000297 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000298 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000299 pj_status_t status;
300
301 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
302 PJ_ETOOMANY);
303
304 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000305 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000306
307 PJSUA_LOCK();
308
309 /* Find empty account id. */
310 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
311 if (pjsua_var.acc[id].valid == PJ_FALSE)
312 break;
313 }
314
315 /* Expect to find a slot */
316 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
317 {PJSUA_UNLOCK(); return PJ_EBUG;});
318
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000319 acc = &pjsua_var.acc[id];
320
321 /* Create pool for this account. */
322 if (acc->pool)
323 pj_pool_reset(acc->pool);
324 else
325 acc->pool = pjsua_pool_create("acc%p", 512, 256);
326
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000327 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000328 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329
330 /* Normalize registration timeout */
331 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
332 pjsua_var.acc[id].cfg.reg_timeout == 0)
333 {
334 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
335 }
336
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000337 /* Get CRC of account proxy setting */
338 acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt);
339
340 /* Get CRC of global outbound proxy setting */
341 acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
342 pjsua_var.ua_cfg.outbound_proxy_cnt);
343
Benny Prijono91d06b62008-09-20 12:16:56 +0000344 /* Check the route URI's and force loose route if required */
345 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
346 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
347 if (status != PJ_SUCCESS)
348 return status;
349 }
350
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000351 status = initialize_acc(id);
352 if (status != PJ_SUCCESS) {
353 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000354 pj_pool_release(acc->pool);
355 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000356 PJSUA_UNLOCK();
357 return status;
358 }
359
360 if (is_default)
361 pjsua_var.default_acc = id;
362
363 if (p_acc_id)
364 *p_acc_id = id;
365
366 pjsua_var.acc_cnt++;
367
368 PJSUA_UNLOCK();
369
370 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
371 (int)cfg->id.slen, cfg->id.ptr, id));
372
373 /* If accounts has registration enabled, start registration */
374 if (pjsua_var.acc[id].cfg.reg_uri.slen)
375 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000376 else {
377 /* Otherwise subscribe to MWI, if it's enabled */
378 if (pjsua_var.acc[id].cfg.mwi_enabled)
379 pjsua_start_mwi(&pjsua_var.acc[id]);
380 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381
382 return PJ_SUCCESS;
383}
384
385
386/*
387 * Add local account
388 */
389PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
390 pj_bool_t is_default,
391 pjsua_acc_id *p_acc_id)
392{
393 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000394 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000395 const char *beginquote, *endquote;
396 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000397 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000398
Benny Prijonoe93e2872006-06-28 16:46:49 +0000399 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000400 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
401 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000402
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000403 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000404 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000405
406 pjsua_acc_config_default(&cfg);
407
Benny Prijono093d3022006-09-24 00:07:11 +0000408 /* Lower the priority of local account */
409 --cfg.priority;
410
Benny Prijonod0bd4982007-12-02 15:40:52 +0000411 /* Enclose IPv6 address in square brackets */
412 if (t->type & PJSIP_TRANSPORT_IPV6) {
413 beginquote = "[";
414 endquote = "]";
415 } else {
416 beginquote = endquote = "";
417 }
418
419 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000420 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000421 pj_ansi_snprintf(transport_param, sizeof(transport_param),
422 ";transport=%s",
423 pjsip_transport_get_type_name(t->type));
424 } else {
425 transport_param[0] = '\0';
426 }
427
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000428 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000429 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000430 "<sip:%s%.*s%s:%d%s>",
431 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000432 (int)t->local_name.host.slen,
433 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000434 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000435 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000436 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000437
438 cfg.id = pj_str(uri);
439
440 return pjsua_acc_add(&cfg, is_default, p_acc_id);
441}
442
443
444/*
Benny Prijono705e7842008-07-21 18:12:51 +0000445 * Set arbitrary data to be associated with the account.
446 */
447PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
448 void *user_data)
449{
450 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
451 PJ_EINVAL);
452 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
453
454 PJSUA_LOCK();
455
456 pjsua_var.acc[acc_id].cfg.user_data = user_data;
457
458 PJSUA_UNLOCK();
459
460 return PJ_SUCCESS;
461}
462
463
464/*
465 * Retrieve arbitrary data associated with the account.
466 */
467PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
468{
469 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
470 NULL);
471 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
472
473 return pjsua_var.acc[acc_id].cfg.user_data;
474}
475
476
477/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000478 * Delete account.
479 */
480PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
481{
Benny Prijono093d3022006-09-24 00:07:11 +0000482 unsigned i;
483
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000484 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
485 PJ_EINVAL);
486 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
487
488 PJSUA_LOCK();
489
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000490 /* Cancel any re-registration timer */
491 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
492
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000493 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000494 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000495 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000496 if (pjsua_var.acc[acc_id].regc) {
497 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
498 }
Benny Prijono922933b2007-01-21 16:23:56 +0000499 pjsua_var.acc[acc_id].regc = NULL;
500 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000501
502 /* Delete server presence subscription */
503 pjsua_pres_delete_acc(acc_id);
504
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000505 /* Release account pool */
506 if (pjsua_var.acc[acc_id].pool) {
507 pj_pool_release(pjsua_var.acc[acc_id].pool);
508 pjsua_var.acc[acc_id].pool = NULL;
509 }
510
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 /* Invalidate */
512 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000513 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514
Benny Prijono093d3022006-09-24 00:07:11 +0000515 /* Remove from array */
516 for (i=0; i<pjsua_var.acc_cnt; ++i) {
517 if (pjsua_var.acc_ids[i] == acc_id)
518 break;
519 }
520 if (i != pjsua_var.acc_cnt) {
521 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
522 pjsua_var.acc_cnt, i);
523 --pjsua_var.acc_cnt;
524 }
525
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000526 /* Leave the calls intact, as I don't think calls need to
527 * access account once it's created
528 */
529
Benny Prijonofb2b3652007-06-28 07:15:03 +0000530 /* Update default account */
531 if (pjsua_var.default_acc == acc_id)
532 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000533
534 PJSUA_UNLOCK();
535
536 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
537
538 return PJ_SUCCESS;
539}
540
541
542/*
543 * Modify account information.
544 */
545PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
546 const pjsua_acc_config *cfg)
547{
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000548 pjsua_acc *acc;
549 pjsip_name_addr *id_name_addr = NULL;
550 pjsip_sip_uri *id_sip_uri = NULL;
551 pjsip_sip_uri *reg_sip_uri = NULL;
552 pj_uint32_t local_route_crc, global_route_crc;
553 pjsip_route_hdr global_route;
554 pjsip_route_hdr local_route;
555 pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES];
556 pj_bool_t update_reg = PJ_FALSE;
557 pj_status_t status = PJ_SUCCESS;
558
559 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
560 PJ_EINVAL);
561
562 PJSUA_LOCK();
563
564 acc = &pjsua_var.acc[acc_id];
565 if (!acc->valid) {
566 status = PJ_EINVAL;
567 goto on_return;
568 }
569
570 /* == Validate first == */
571
572 /* Account id */
573 if (pj_strcmp(&acc->cfg.id, &cfg->id)) {
574 /* Need to parse id to get the elements: */
575 id_name_addr = (pjsip_name_addr*)
576 pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen,
577 PJSIP_PARSE_URI_AS_NAMEADDR);
578 if (id_name_addr == NULL) {
579 status = PJSIP_EINVALIDURI;
580 pjsua_perror(THIS_FILE, "Invalid local URI", status);
581 goto on_return;
582 }
583
584 /* URI MUST be a SIP or SIPS: */
585 if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) &&
586 !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr))
587 {
588 status = PJSIP_EINVALIDSCHEME;
589 pjsua_perror(THIS_FILE, "Invalid local URI", status);
590 goto on_return;
591 }
592
593 /* Get the SIP URI object: */
594 id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr);
595 }
596
597 /* Registrar URI */
598 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) {
599 pjsip_uri *reg_uri;
600
601 /* Need to parse reg_uri to get the elements: */
602 reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr,
603 cfg->reg_uri.slen, 0);
604 if (reg_uri == NULL) {
605 status = PJSIP_EINVALIDURI;
606 pjsua_perror(THIS_FILE, "Invalid registrar URI", status);
607 goto on_return;
608 }
609
610 /* Registrar URI MUST be a SIP or SIPS: */
611 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
612 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
613 {
614 status = PJSIP_EINVALIDSCHEME;
615 pjsua_perror(THIS_FILE, "Invalid registar URI", status);
616 goto on_return;
617 }
618
619 reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
620 }
621
622 /* Global outbound proxy */
623 global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy,
624 pjsua_var.ua_cfg.outbound_proxy_cnt);
625 if (global_route_crc != acc->global_route_crc) {
626 pjsip_route_hdr *r;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000627
Benny Prijono29c8ca32010-06-22 06:02:13 +0000628 /* Copy from global outbound proxies */
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000629 pj_list_init(&global_route);
Benny Prijono29c8ca32010-06-22 06:02:13 +0000630 r = pjsua_var.outbound_proxy.next;
631 while (r != &pjsua_var.outbound_proxy) {
632 pj_list_push_back(&global_route,
633 pjsip_hdr_shallow_clone(acc->pool, r));
634 r = r->next;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000635 }
636 }
637
638 /* Account proxy */
639 local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt);
640 if (local_route_crc != acc->local_route_crc) {
641 pjsip_route_hdr *r;
642 unsigned i;
643
644 /* Validate the local route and save it to temporary var */
645 pj_list_init(&local_route);
646 for (i=0; i<cfg->proxy_cnt; ++i) {
647 pj_str_t hname = { "Route", 5};
648
649 pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]);
650 status = normalize_route_uri(acc->pool, &acc_proxy[i]);
651 if (status != PJ_SUCCESS)
652 goto on_return;
653 r = (pjsip_route_hdr*)
654 pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr,
655 acc_proxy[i].slen, NULL);
656 if (r == NULL) {
657 status = PJSIP_EINVALIDURI;
658 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
659 status);
660 goto on_return;
661 }
662
663 pj_list_push_back(&local_route, r);
664 }
665 }
666
667
668 /* == Apply the new config == */
669
670 /* Account ID. */
671 if (id_name_addr && id_sip_uri) {
672 pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id);
673 acc->display = id_name_addr->display;
674 acc->user_part = id_sip_uri->user;
675 acc->srv_domain = id_sip_uri->host;
676 acc->srv_port = 0;
677 update_reg = PJ_TRUE;
678 }
679
680 /* User data */
681 acc->cfg.user_data = cfg->user_data;
682
683 /* Priority */
684 if (acc->cfg.priority != cfg->priority) {
685 unsigned i;
686
687 acc->cfg.priority = cfg->priority;
688
689 /* Resort accounts priority */
690 for (i=0; i<pjsua_var.acc_cnt; ++i) {
691 if (pjsua_var.acc_ids[i] == acc_id)
692 break;
693 }
694 pj_assert(i < pjsua_var.acc_cnt);
695 pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id),
696 pjsua_var.acc_cnt, i);
697 for (i=0; i<pjsua_var.acc_cnt; ++i) {
698 if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
699 acc->cfg.priority)
700 {
701 break;
702 }
703 }
704 pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id),
705 pjsua_var.acc_cnt, i, &acc_id);
706 }
707
708 /* MWI */
709 acc->cfg.mwi_enabled = cfg->mwi_enabled;
710
711 /* PIDF tuple ID */
712 if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id))
713 pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id,
714 &cfg->pidf_tuple_id);
715
716 /* Publish */
717 acc->cfg.publish_opt = cfg->publish_opt;
718 acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec;
719 if (acc->cfg.publish_enabled != cfg->publish_enabled) {
720 acc->cfg.publish_enabled = cfg->publish_enabled;
721 if (!acc->cfg.publish_enabled)
722 pjsua_pres_unpublish(acc);
723 else
724 update_reg = PJ_TRUE;
725 }
726
727 /* Force contact URI */
728 if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) {
729 pj_strdup_with_null(acc->pool, &acc->cfg.force_contact,
730 &cfg->force_contact);
731 update_reg = PJ_TRUE;
732 }
733
734 /* Contact param */
735 if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) {
736 pj_strdup_with_null(acc->pool, &acc->cfg.contact_params,
737 &cfg->contact_params);
738 update_reg = PJ_TRUE;
739 }
740
741 /* Contact URI params */
742 if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) {
743 pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params,
744 &cfg->contact_uri_params);
745 update_reg = PJ_TRUE;
746 }
747
748 /* Reliable provisional response */
749 acc->cfg.require_100rel = cfg->require_100rel;
750
751 /* Session timer */
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000752 acc->cfg.use_timer = cfg->use_timer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000753 acc->cfg.timer_setting = cfg->timer_setting;
754
755 /* Transport and keep-alive */
756 if (acc->cfg.transport_id != cfg->transport_id) {
757 acc->cfg.transport_id = cfg->transport_id;
758 update_reg = PJ_TRUE;
759 }
760 acc->cfg.ka_interval = cfg->ka_interval;
761 if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data))
762 pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data);
763#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
764 acc->cfg.use_srtp = cfg->use_srtp;
765 acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000766 acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer;
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000767#endif
768
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000769#if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
770 acc->cfg.use_stream_ka = cfg->use_stream_ka;
771#endif
772
Nanang Izzuddinc3ea16a2010-04-20 14:36:38 +0000773 /* Global outbound proxy */
774 if (global_route_crc != acc->global_route_crc) {
775 unsigned i, rcnt;
776
777 /* Remove the outbound proxies from the route set */
778 rcnt = pj_list_size(&acc->route_set);
779 for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) {
780 pjsip_route_hdr *r = acc->route_set.next;
781 pj_list_erase(r);
782 }
783
784 /* Insert the outbound proxies to the beginning of route set */
785 pj_list_merge_first(&acc->route_set, &global_route);
786
787 /* Update global route CRC */
788 acc->global_route_crc = global_route_crc;
789
790 update_reg = PJ_TRUE;
791 }
792
793 /* Account proxy */
794 if (local_route_crc != acc->local_route_crc) {
795 unsigned i;
796
797 /* Remove the current account proxies from the route set */
798 for (i=0; i < acc->cfg.proxy_cnt; ++i) {
799 pjsip_route_hdr *r = acc->route_set.prev;
800 pj_list_erase(r);
801 }
802
803 /* Insert new proxy setting to the route set */
804 pj_list_merge_last(&acc->route_set, &local_route);
805
806 /* Update the proxy setting */
807 acc->cfg.proxy_cnt = cfg->proxy_cnt;
808 for (i = 0; i < cfg->proxy_cnt; ++i)
809 acc->cfg.proxy[i] = acc_proxy[i];
810
811 /* Update local route CRC */
812 acc->local_route_crc = local_route_crc;
813
814 update_reg = PJ_TRUE;
815 }
816
817 /* Credential info */
818 {
819 unsigned i;
820
821 /* Selective update credential info. */
822 for (i = 0; i < cfg->cred_count; ++i) {
823 unsigned j;
824 pjsip_cred_info ci;
825
826 /* Find if this credential is already listed */
827 for (j = i; j < acc->cfg.cred_count; ++i) {
828 if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j],
829 &cfg->cred_info[i]) == 0)
830 {
831 /* Found, but different index/position, swap */
832 if (j != i) {
833 ci = acc->cfg.cred_info[i];
834 acc->cfg.cred_info[i] = acc->cfg.cred_info[j];
835 acc->cfg.cred_info[j] = ci;
836 }
837 break;
838 }
839 }
840
841 /* Not found, insert this */
842 if (j == acc->cfg.cred_count) {
843 /* If account credential is full, discard the last one. */
844 if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) {
845 pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info),
846 acc->cfg.cred_count, acc->cfg.cred_count-1);
847 acc->cfg.cred_count--;
848 }
849
850 /* Insert this */
851 pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]);
852 pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info),
853 acc->cfg.cred_count, i, &ci);
854 }
855 }
856 acc->cfg.cred_count = cfg->cred_count;
857
858 /* Concatenate credentials from account config and global config */
859 acc->cred_cnt = 0;
860 for (i=0; i<acc->cfg.cred_count; ++i) {
861 acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i];
862 }
863 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
864 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
865 {
866 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
867 }
868 }
869
870 /* Authentication preference */
871 acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth;
872 if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm))
873 pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm,
874 &cfg->auth_pref.algorithm);
875
876 /* Registration */
877 acc->cfg.reg_timeout = cfg->reg_timeout;
878 acc->cfg.unreg_timeout = cfg->unreg_timeout;
879 acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite;
880 acc->cfg.reg_retry_interval = cfg->reg_retry_interval;
881 acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail;
882
883 /* Normalize registration timeout */
884 if (acc->cfg.reg_uri.slen && acc->cfg.reg_timeout == 0)
885 acc->cfg.reg_timeout = PJSUA_REG_INTERVAL;
886
887 /* Registrar URI */
888 if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) {
889 if (cfg->reg_uri.slen) {
890 pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri);
891 if (reg_sip_uri)
892 acc->srv_port = reg_sip_uri->port;
893 } else {
894 /* Unregister if registration was set */
895 if (acc->cfg.reg_uri.slen)
896 pjsua_acc_set_registration(acc->index, PJ_FALSE);
897 pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri));
898 }
899 update_reg = PJ_TRUE;
900 }
901
902 /* Update registration */
903 if (update_reg) {
904 /* If accounts has registration enabled, start registration */
905 if (acc->cfg.reg_uri.slen)
906 pjsua_acc_set_registration(acc->index, PJ_TRUE);
907 else {
908 /* Otherwise subscribe to MWI, if it's enabled */
909 if (acc->cfg.mwi_enabled)
910 pjsua_start_mwi(acc);
911 }
912 }
913
914on_return:
915 PJSUA_UNLOCK();
916 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000917}
918
919
920/*
921 * Modify account's presence status to be advertised to remote/presence
922 * subscribers.
923 */
924PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
925 pj_bool_t is_online)
926{
927 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
928 PJ_EINVAL);
929 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
930
931 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000932 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
933 pjsua_pres_update_acc(acc_id, PJ_FALSE);
934 return PJ_SUCCESS;
935}
936
937
938/*
939 * Set online status with extended information
940 */
941PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
942 pj_bool_t is_online,
943 const pjrpid_element *pr)
944{
945 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
946 PJ_EINVAL);
947 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
948
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000949 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000950 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000951 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
952 PJSUA_UNLOCK();
953
Benny Prijono4461c7d2007-08-25 13:36:15 +0000954 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000955 return PJ_SUCCESS;
956}
957
Benny Prijono7f630432008-09-24 16:52:41 +0000958/* Check if IP is private IP address */
959static pj_bool_t is_private_ip(const pj_str_t *addr)
960{
961 const pj_str_t private_net[] =
962 {
963 { "10.", 3 },
964 { "127.", 4 },
965 { "172.16.", 7 },
966 { "192.168.", 8 }
967 };
968 unsigned i;
969
970 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
971 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
972 return PJ_TRUE;
973 }
974
975 return PJ_FALSE;
976}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000977
Benny Prijono15b02302007-09-27 14:07:07 +0000978/* Update NAT address from the REGISTER response */
979static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
980 struct pjsip_regc_cbparam *param)
981{
982 pjsip_transport *tp;
983 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000984 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000985 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000986 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000987 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +0000988 pj_sockaddr contact_addr;
989 pj_sockaddr recv_addr;
990 pj_status_t status;
991 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +0000992 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000993 pjsip_contact_hdr *contact_hdr;
994 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +0000995
996 tp = param->rdata->tp_info.transport;
997
998 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000999 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +00001000 return PJ_FALSE;
1001
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001002#if 0
1003 // Always update
1004 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +00001005
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001006 /* For UDP, only update if STUN is enabled (for now).
1007 * For TCP/TLS, always check.
1008 */
1009 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
1010 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
1011 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
1012 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
1013 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +00001014 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001015 /* Yes we will check */
1016 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001017 return PJ_FALSE;
1018 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001019#endif
Benny Prijono15b02302007-09-27 14:07:07 +00001020
1021 /* Get the received and rport info */
1022 via = param->rdata->msg_info.via;
1023 if (via->rport_param < 1) {
1024 /* Remote doesn't support rport */
1025 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +00001026 if (rport==0) {
1027 pjsip_transport_type_e tp_type;
1028 tp_type = (pjsip_transport_type_e) tp->key.type;
1029 rport = pjsip_transport_get_default_port_for_type(tp_type);
1030 }
Benny Prijono15b02302007-09-27 14:07:07 +00001031 } else
1032 rport = via->rport_param;
1033
1034 if (via->recvd_param.slen != 0)
1035 via_addr = &via->recvd_param;
1036 else
1037 via_addr = &via->sent_by.host;
1038
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001039 /* Compare received and rport with the URI in our registration */
1040 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +00001041 contact_hdr = (pjsip_contact_hdr*)
1042 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
1043 acc->contact.slen, NULL);
1044 pj_assert(contact_hdr != NULL);
1045 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001046 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +00001047 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001048
Benny Prijono24a21852008-04-14 04:04:30 +00001049 if (uri->port == 0) {
1050 pjsip_transport_type_e tp_type;
1051 tp_type = (pjsip_transport_type_e) tp->key.type;
1052 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1053 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001054
Benny Prijono84d24932009-06-04 15:51:39 +00001055 /* Convert IP address strings into sockaddr for comparison.
1056 * (http://trac.pjsip.org/repos/ticket/863)
1057 */
1058 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
1059 &contact_addr);
1060 if (status == PJ_SUCCESS)
1061 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
1062 &recv_addr);
1063 if (status == PJ_SUCCESS) {
1064 /* Compare the addresses as sockaddr according to the ticket above */
1065 matched = (uri->port == rport &&
1066 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
1067 } else {
1068 /* Compare the addresses as string, as before */
1069 matched = (uri->port == rport &&
1070 pj_stricmp(&uri->host, via_addr)==0);
1071 }
1072
1073 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +00001074 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001075 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +00001076 return PJ_FALSE;
1077 }
1078
Benny Prijono7f630432008-09-24 16:52:41 +00001079 /* Get server IP */
1080 srv_ip = pj_str(param->rdata->pkt_info.src_name);
1081
Benny Prijono15b02302007-09-27 14:07:07 +00001082 /* At this point we've detected that the address as seen by registrar.
1083 * has changed.
1084 */
Benny Prijono7f630432008-09-24 16:52:41 +00001085
1086 /* Do not switch if both Contact and server's IP address are
1087 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +00001088 * might have messed up with the SIP packets. See:
1089 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +00001090 *
1091 * This exception can be disabled by setting allow_contact_rewrite
1092 * to 2. In this case, the switch will always be done whenever there
1093 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +00001094 */
Benny Prijono247921b2008-09-26 22:06:11 +00001095 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
1096 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +00001097 {
1098 /* Don't switch */
1099 pj_pool_release(pool);
1100 return PJ_FALSE;
1101 }
1102
Benny Prijono4f933762009-11-10 03:45:42 +00001103 /* Also don't switch if only the port number part is different, and
1104 * the Via received address is private.
1105 * See http://trac.pjsip.org/repos/ticket/864
1106 */
1107 if (acc->cfg.allow_contact_rewrite != 2 &&
1108 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
1109 is_private_ip(via_addr))
1110 {
1111 /* Don't switch */
1112 pj_pool_release(pool);
1113 return PJ_FALSE;
1114 }
1115
Benny Prijono15b02302007-09-27 14:07:07 +00001116 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001117 "(%.*s:%d --> %.*s:%d). Updating registration "
1118 "(using method %d)",
Benny Prijono15b02302007-09-27 14:07:07 +00001119 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001120 (int)uri->host.slen,
1121 uri->host.ptr,
1122 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +00001123 (int)via_addr->slen,
1124 via_addr->ptr,
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001125 rport,
1126 acc->cfg.contact_rewrite_method));
Benny Prijono15b02302007-09-27 14:07:07 +00001127
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001128 pj_assert(acc->cfg.contact_rewrite_method == 1 ||
1129 acc->cfg.contact_rewrite_method == 2);
1130
1131 if (acc->cfg.contact_rewrite_method == 1) {
1132 /* Unregister current contact */
1133 pjsua_acc_set_registration(acc->index, PJ_FALSE);
1134 if (acc->regc != NULL) {
1135 pjsip_regc_destroy(acc->regc);
1136 acc->regc = NULL;
1137 acc->contact.slen = 0;
1138 }
Benny Prijono15b02302007-09-27 14:07:07 +00001139 }
1140
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001141 /*
1142 * Build new Contact header
1143 */
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001144 {
1145 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +00001146 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001147 int len;
1148
Benny Prijono8972bf02009-04-05 18:30:45 +00001149 /* Enclose IPv6 address in square brackets */
1150 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1151 beginquote = "[";
1152 endquote = "]";
1153 } else {
1154 beginquote = endquote = "";
1155 }
1156
Benny Prijono24a21852008-04-14 04:04:30 +00001157 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001158 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001159 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001160 (int)acc->user_part.slen,
1161 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +00001162 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +00001163 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001164 (int)via_addr->slen,
1165 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +00001166 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001167 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +00001168 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001169 (int)acc->cfg.contact_uri_params.slen,
1170 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00001171 (int)acc->cfg.contact_params.slen,
1172 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001173 if (len < 1) {
1174 PJ_LOG(1,(THIS_FILE, "URI too long"));
1175 pj_pool_release(pool);
1176 return PJ_FALSE;
1177 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +00001178 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001179
1180 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
1181 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
1182 tp->local_name.port = rport;
1183
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001184 }
1185
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +00001186 if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) {
1187 pjsip_regc_update_contact(acc->regc, 1, &acc->contact);
1188 }
Benny Prijono15b02302007-09-27 14:07:07 +00001189
1190 /* Perform new registration */
1191 pjsua_acc_set_registration(acc->index, PJ_TRUE);
1192
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001193 pj_pool_release(pool);
1194
Benny Prijono15b02302007-09-27 14:07:07 +00001195 return PJ_TRUE;
1196}
1197
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001198/* Check and update Service-Route header */
1199void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
1200{
1201 pjsip_generic_string_hdr *hsr = NULL;
1202 pjsip_route_hdr *hr, *h;
1203 const pj_str_t HNAME = { "Service-Route", 13 };
1204 const pj_str_t HROUTE = { "Route", 5 };
1205 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +00001206 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001207
1208 /* Find and parse Service-Route headers */
1209 for (;;) {
1210 char saved;
1211 int parsed_len;
1212
1213 /* Find Service-Route header */
1214 hsr = (pjsip_generic_string_hdr*)
1215 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
1216 if (!hsr)
1217 break;
1218
1219 /* Parse as Route header since the syntax is similar. This may
1220 * return more than one headers.
1221 */
1222 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
1223 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
1224 hr = (pjsip_route_hdr*)
1225 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
1226 hsr->hvalue.slen, &parsed_len);
1227 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
1228
1229 if (hr == NULL) {
1230 /* Error */
1231 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
1232 return;
1233 }
1234
1235 /* Save each URI in the result */
1236 h = hr;
1237 do {
1238 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
1239 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
1240 {
1241 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
1242 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
1243 return;
1244 }
1245
1246 uri[uri_cnt++] = h->name_addr.uri;
1247 h = h->next;
1248 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
1249
1250 if (h != hr) {
1251 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
1252 return;
1253 }
1254
1255 /* Prepare to find next Service-Route header */
1256 hsr = hsr->next;
1257 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
1258 break;
1259 }
1260
1261 if (uri_cnt == 0)
1262 return;
1263
1264 /*
1265 * Update account's route set
1266 */
1267
1268 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +00001269 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +00001270 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
1271 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
1272 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +00001273 i<rcnt;
1274 ++i)
1275 {
1276 pjsip_route_hdr *prev = hr->prev;
1277 pj_list_erase(hr);
1278 hr = prev;
1279 }
1280 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001281
1282 /* Then append the Service-Route URIs */
1283 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001284 hr = pjsip_route_hdr_create(acc->pool);
1285 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001286 pj_list_push_back(&acc->route_set, hr);
1287 }
1288
1289 /* Done */
1290
1291 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
1292 acc->index, uri_cnt));
1293}
1294
Benny Prijonobddef2c2007-10-31 13:28:08 +00001295
1296/* Keep alive timer callback */
1297static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1298{
1299 pjsua_acc *acc;
1300 pjsip_tpselector tp_sel;
1301 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +00001302 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +00001303 pj_status_t status;
1304
1305 PJ_UNUSED_ARG(th);
1306
1307 PJSUA_LOCK();
1308
1309 te->id = PJ_FALSE;
1310
1311 acc = (pjsua_acc*) te->user_data;
1312
1313 /* Select the transport to send the packet */
1314 pj_bzero(&tp_sel, sizeof(tp_sel));
1315 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
1316 tp_sel.u.transport = acc->ka_transport;
1317
1318 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001319 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +00001320 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +00001321 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +00001322
1323 /* Send raw packet */
1324 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
1325 PJSIP_TRANSPORT_UDP, &tp_sel,
1326 NULL, acc->cfg.ka_data.ptr,
1327 acc->cfg.ka_data.slen,
1328 &acc->ka_target, acc->ka_target_len,
1329 NULL, NULL);
1330
1331 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1332 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
1333 }
1334
1335 /* Reschedule next timer */
1336 delay.sec = acc->cfg.ka_interval;
1337 delay.msec = 0;
1338 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
1339 if (status == PJ_SUCCESS) {
1340 te->id = PJ_TRUE;
1341 } else {
1342 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1343 }
1344
1345 PJSUA_UNLOCK();
1346}
1347
1348
1349/* Update keep-alive for the account */
1350static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
1351 struct pjsip_regc_cbparam *param)
1352{
1353 /* In all cases, stop keep-alive timer if it's running. */
1354 if (acc->ka_timer.id) {
1355 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
1356 acc->ka_timer.id = PJ_FALSE;
1357
1358 pjsip_transport_dec_ref(acc->ka_transport);
1359 acc->ka_transport = NULL;
1360 }
1361
1362 if (start) {
1363 pj_time_val delay;
1364 pj_status_t status;
1365
1366 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +00001367 * - ka_interval is not zero in the account, and
1368 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +00001369 *
1370 * Previously we only enabled keep-alive when STUN is enabled, since
1371 * we thought that keep-alive is only needed in Internet situation.
1372 * But it has been discovered that Windows Firewall on WinXP also
1373 * needs to be kept-alive, otherwise incoming packets will be dropped.
1374 * So because of this, now keep-alive is always enabled for UDP,
1375 * regardless of whether STUN is enabled or not.
1376 *
1377 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
1378 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +00001379 */
Benny Prijono7fff9f92008-04-04 10:50:21 +00001380 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +00001381 acc->cfg.ka_interval == 0 ||
1382 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
1383 {
1384 /* Keep alive is not necessary */
1385 return;
1386 }
1387
1388 /* Save transport and destination address. */
1389 acc->ka_transport = param->rdata->tp_info.transport;
1390 pjsip_transport_add_ref(acc->ka_transport);
1391 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
1392 param->rdata->pkt_info.src_addr_len);
1393 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
1394
1395 /* Setup and start the timer */
1396 acc->ka_timer.cb = &keep_alive_timer_cb;
1397 acc->ka_timer.user_data = (void*)acc;
1398
1399 delay.sec = acc->cfg.ka_interval;
1400 delay.msec = 0;
1401 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
1402 &delay);
1403 if (status == PJ_SUCCESS) {
1404 acc->ka_timer.id = PJ_TRUE;
1405 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
1406 "destination:%s:%d, interval:%ds",
1407 acc->index,
1408 param->rdata->pkt_info.src_name,
1409 param->rdata->pkt_info.src_port,
1410 acc->cfg.ka_interval));
1411 } else {
1412 acc->ka_timer.id = PJ_FALSE;
1413 pjsip_transport_dec_ref(acc->ka_transport);
1414 acc->ka_transport = NULL;
1415 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
1416 }
1417 }
1418}
1419
1420
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001421/*
1422 * This callback is called by pjsip_regc when outgoing register
1423 * request has completed.
1424 */
1425static void regc_cb(struct pjsip_regc_cbparam *param)
1426{
1427
Benny Prijonoa1e69682007-05-11 15:14:34 +00001428 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001429
Benny Prijono15b02302007-09-27 14:07:07 +00001430 if (param->regc != acc->regc)
1431 return;
1432
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001433 PJSUA_LOCK();
1434
1435 /*
1436 * Print registration status.
1437 */
1438 if (param->status!=PJ_SUCCESS) {
1439 pjsua_perror(THIS_FILE, "SIP registration error",
1440 param->status);
1441 pjsip_regc_destroy(acc->regc);
1442 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001443 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001444
Benny Prijonobddef2c2007-10-31 13:28:08 +00001445 /* Stop keep-alive timer if any. */
1446 update_keep_alive(acc, PJ_FALSE, NULL);
1447
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001448 } else if (param->code < 0 || param->code >= 300) {
1449 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1450 param->code,
1451 (int)param->reason.slen, param->reason.ptr));
1452 pjsip_regc_destroy(acc->regc);
1453 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001454 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001455
Benny Prijonobddef2c2007-10-31 13:28:08 +00001456 /* Stop keep-alive timer if any. */
1457 update_keep_alive(acc, PJ_FALSE, NULL);
1458
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001459 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1460
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001461 /* Update auto registration flag */
1462 acc->auto_rereg.active = PJ_FALSE;
1463 acc->auto_rereg.attempt_cnt = 0;
1464
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001465 if (param->expiration < 1) {
1466 pjsip_regc_destroy(acc->regc);
1467 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001468 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001469
1470 /* Stop keep-alive timer if any. */
1471 update_keep_alive(acc, PJ_FALSE, NULL);
1472
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001473 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1474 pjsua_var.acc[acc->index].cfg.id.ptr));
1475 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001476 /* Check NAT bound address */
1477 if (acc_check_nat_addr(acc, param)) {
1478 /* Update address, don't notify application yet */
1479 PJSUA_UNLOCK();
1480 return;
1481 }
1482
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001483 /* Check and update Service-Route header */
1484 update_service_route(acc, param->rdata);
1485
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001486 PJ_LOG(3, (THIS_FILE,
1487 "%s: registration success, status=%d (%.*s), "
1488 "will re-register in %d seconds",
1489 pjsua_var.acc[acc->index].cfg.id.ptr,
1490 param->code,
1491 (int)param->reason.slen, param->reason.ptr,
1492 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001493
Benny Prijonobddef2c2007-10-31 13:28:08 +00001494 /* Start keep-alive timer if necessary. */
1495 update_keep_alive(acc, PJ_TRUE, param);
1496
Benny Prijono8b6834f2007-02-24 13:29:22 +00001497 /* Send initial PUBLISH if it is enabled */
1498 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1499 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001500
1501 /* Subscribe to MWI, if it's enabled */
1502 if (acc->cfg.mwi_enabled)
1503 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001504 }
1505
1506 } else {
1507 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1508 }
1509
1510 acc->reg_last_err = param->status;
1511 acc->reg_last_code = param->code;
1512
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001513 /* Check if we need to auto retry registration. Basically, registration
1514 * failure codes triggering auto-retry are those of temporal failures
1515 * considered to be recoverable in relatively short term.
1516 */
1517 if (acc->cfg.reg_retry_interval &&
1518 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1519 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1520 param->code == PJSIP_SC_BAD_GATEWAY ||
1521 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1522 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1523 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1524 {
1525 schedule_reregistration(acc);
1526 }
1527
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001528 /* Call the registration status callback */
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00001529
Nanang Izzuddin4ea1bcc2010-09-28 04:57:01 +00001530 if (pjsua_var.ua_cfg.cb.on_reg_state) {
1531 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1532 }
1533
1534 if (pjsua_var.ua_cfg.cb.on_reg_state2) {
1535 pjsua_reg_info reg_info;
1536
1537 reg_info.cbparam = param;
1538 (*pjsua_var.ua_cfg.cb.on_reg_state2)(acc->index, &reg_info);
1539 }
1540
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001541 PJSUA_UNLOCK();
1542}
1543
1544
1545/*
1546 * Initialize client registration.
1547 */
1548static pj_status_t pjsua_regc_init(int acc_id)
1549{
1550 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001551 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001552 pj_status_t status;
1553
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001554 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001555 acc = &pjsua_var.acc[acc_id];
1556
1557 if (acc->cfg.reg_uri.slen == 0) {
1558 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1559 return PJ_SUCCESS;
1560 }
1561
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001562 /* Destroy existing session, if any */
1563 if (acc->regc) {
1564 pjsip_regc_destroy(acc->regc);
1565 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001566 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001567 }
1568
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001569 /* initialize SIP registration if registrar is configured */
1570
1571 status = pjsip_regc_create( pjsua_var.endpt,
1572 acc, &regc_cb, &acc->regc);
1573
1574 if (status != PJ_SUCCESS) {
1575 pjsua_perror(THIS_FILE, "Unable to create client registration",
1576 status);
1577 return status;
1578 }
1579
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001580 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001581
1582 if (acc->contact.slen == 0) {
1583 pj_str_t tmp_contact;
1584
1585 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1586 acc_id, &acc->cfg.reg_uri);
1587 if (status != PJ_SUCCESS) {
1588 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1589 " for registration",
1590 status);
1591 pjsip_regc_destroy(acc->regc);
1592 pj_pool_release(pool);
1593 acc->regc = NULL;
1594 return status;
1595 }
1596
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001597 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001598 }
1599
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001600 status = pjsip_regc_init( acc->regc,
1601 &acc->cfg.reg_uri,
1602 &acc->cfg.id,
1603 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001604 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001605 acc->cfg.reg_timeout);
1606 if (status != PJ_SUCCESS) {
1607 pjsua_perror(THIS_FILE,
1608 "Client registration initialization error",
1609 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001610 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001611 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001612 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001613 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001614 return status;
1615 }
1616
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001617 /* If account is locked to specific transport, then set transport to
1618 * the client registration.
1619 */
1620 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1621 pjsip_tpselector tp_sel;
1622
1623 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1624 pjsip_regc_set_transport(acc->regc, &tp_sel);
1625 }
1626
1627
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001628 /* Set credentials
1629 */
1630 if (acc->cred_cnt) {
1631 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1632 }
1633
Benny Prijono48ab2b72007-11-08 09:24:30 +00001634 /* Set authentication preference */
1635 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1636
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001637 /* Set route-set
1638 */
Benny Prijono29c8ca32010-06-22 06:02:13 +00001639 if (acc->cfg.reg_use_proxy) {
1640 pjsip_route_hdr route_set;
1641 const pjsip_route_hdr *r;
1642
1643 pj_list_init(&route_set);
1644
1645 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) {
1646 r = pjsua_var.outbound_proxy.next;
1647 while (r != &pjsua_var.outbound_proxy) {
1648 pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r));
1649 r = r->next;
1650 }
1651 }
1652
1653 if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY &&
1654 acc->cfg.proxy_cnt)
1655 {
1656 int cnt = acc->cfg.proxy_cnt;
1657 pjsip_route_hdr *pos = route_set.prev;
1658 int i;
1659
1660 r = acc->route_set.prev;
1661 for (i=0; i<cnt; ++i) {
1662 pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r));
1663 r = r->prev;
1664 }
1665 }
1666
1667 if (!pj_list_empty(&route_set))
1668 pjsip_regc_set_route_set( acc->regc, &route_set );
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001669 }
1670
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +00001671 /* Add custom request headers specified in the account config */
1672 pjsip_regc_add_headers(acc->regc, &acc->cfg.reg_hdr_list);
1673
Benny Prijono8fc6de02006-11-11 21:25:55 +00001674 /* Add other request headers. */
1675 if (pjsua_var.ua_cfg.user_agent.slen) {
1676 pjsip_hdr hdr_list;
1677 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1678 pjsip_generic_string_hdr *h;
1679
Benny Prijono8fc6de02006-11-11 21:25:55 +00001680 pj_list_init(&hdr_list);
1681
1682 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1683 &pjsua_var.ua_cfg.user_agent);
1684 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1685
1686 pjsip_regc_add_headers(acc->regc, &hdr_list);
1687 }
1688
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001689 pj_pool_release(pool);
1690
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001691 return PJ_SUCCESS;
1692}
1693
1694
1695/*
1696 * Update registration or perform unregistration.
1697 */
1698PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1699 pj_bool_t renew)
1700{
1701 pj_status_t status = 0;
1702 pjsip_tx_data *tdata = 0;
1703
1704 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1705 PJ_EINVAL);
1706 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1707
1708 PJSUA_LOCK();
1709
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001710 /* Cancel any re-registration timer */
1711 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1712
1713 /* Reset pointer to registration transport */
1714 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1715
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001716 if (renew) {
1717 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001718 status = pjsua_regc_init(acc_id);
1719 if (status != PJ_SUCCESS) {
1720 pjsua_perror(THIS_FILE, "Unable to create registration",
1721 status);
1722 goto on_return;
1723 }
1724 }
1725 if (!pjsua_var.acc[acc_id].regc) {
1726 status = PJ_EINVALIDOP;
1727 goto on_return;
1728 }
1729
1730 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1731 &tdata);
1732
Benny Prijono28f673a2007-10-15 07:04:59 +00001733 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1734 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1735 pjsip_authorization_hdr *h;
1736 char *uri;
1737 int d;
1738
1739 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1740 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1741 uri, acc->cfg.reg_uri.slen+10);
1742 pj_assert(d > 0);
1743
1744 h = pjsip_authorization_hdr_create(tdata->pool);
1745 h->scheme = pj_str("Digest");
1746 h->credential.digest.username = acc->cred[0].username;
1747 h->credential.digest.realm = acc->srv_domain;
1748 h->credential.digest.uri = pj_str(uri);
1749 h->credential.digest.algorithm = pj_str("md5");
1750
1751 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1752 }
1753
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001754 } else {
1755 if (pjsua_var.acc[acc_id].regc == NULL) {
1756 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1757 status = PJ_EINVALIDOP;
1758 goto on_return;
1759 }
Benny Prijono166d5022010-02-10 14:24:48 +00001760
1761 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
1762
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001763 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1764 }
1765
Benny Prijono56315612006-07-18 14:39:40 +00001766 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001767 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001768 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001769 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001770
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001771 /* Update pointer to registration transport */
1772 if (status == PJ_SUCCESS) {
1773 pjsip_regc_info reg_info;
1774
1775 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
1776 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
1777 }
1778
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001779 if (status != PJ_SUCCESS) {
1780 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1781 status);
1782 } else {
1783 PJ_LOG(3,(THIS_FILE, "%s sent",
1784 (renew? "Registration" : "Unregistration")));
1785 }
1786
1787on_return:
1788 PJSUA_UNLOCK();
1789 return status;
1790}
1791
1792
1793/*
1794 * Get account information.
1795 */
1796PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1797 pjsua_acc_info *info)
1798{
1799 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1800 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1801
1802 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001803 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001804
Benny Prijonoac623b32006-07-03 15:19:31 +00001805 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001806
1807 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1808 PJ_EINVAL);
1809 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1810
1811 PJSUA_LOCK();
1812
1813 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1814 PJSUA_UNLOCK();
1815 return PJ_EINVALIDOP;
1816 }
1817
1818 info->id = acc_id;
1819 info->is_default = (pjsua_var.default_acc == acc_id);
1820 info->acc_uri = acc_cfg->id;
1821 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1822 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001823 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1824 if (info->rpid.note.slen)
1825 info->online_status_text = info->rpid.note;
1826 else if (info->online_status)
1827 info->online_status_text = pj_str("Online");
1828 else
1829 info->online_status_text = pj_str("Offline");
1830
Sauw Ming48f6dbf2010-09-07 05:10:25 +00001831 if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001832 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001833 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001834 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
Sauw Ming48f6dbf2010-09-07 05:10:25 +00001835 if (acc->reg_last_err)
1836 info->reg_last_err = acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001837 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001838 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001839 info->status_text = pj_str("not registered");
1840 }
1841 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001842 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001843 info->status_text = pj_str("In Progress");
1844 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001845 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001846 info->status_text = pj_str("does not register");
1847 }
1848
1849 if (acc->regc) {
1850 pjsip_regc_info regc_info;
1851 pjsip_regc_get_info(acc->regc, &regc_info);
1852 info->expires = regc_info.next_reg;
1853 } else {
1854 info->expires = -1;
1855 }
1856
1857 PJSUA_UNLOCK();
1858
1859 return PJ_SUCCESS;
1860
1861}
1862
1863
1864/*
1865 * Enum accounts all account ids.
1866 */
1867PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1868 unsigned *count )
1869{
1870 unsigned i, c;
1871
1872 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1873
1874 PJSUA_LOCK();
1875
1876 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1877 if (!pjsua_var.acc[i].valid)
1878 continue;
1879 ids[c] = i;
1880 ++c;
1881 }
1882
1883 *count = c;
1884
1885 PJSUA_UNLOCK();
1886
1887 return PJ_SUCCESS;
1888}
1889
1890
1891/*
1892 * Enum accounts info.
1893 */
1894PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1895 unsigned *count )
1896{
1897 unsigned i, c;
1898
1899 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1900
1901 PJSUA_LOCK();
1902
1903 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1904 if (!pjsua_var.acc[i].valid)
1905 continue;
1906
1907 pjsua_acc_get_info(i, &info[c]);
1908 ++c;
1909 }
1910
1911 *count = c;
1912
1913 PJSUA_UNLOCK();
1914
1915 return PJ_SUCCESS;
1916}
1917
1918
1919/*
1920 * This is an internal function to find the most appropriate account to
1921 * used to reach to the specified URL.
1922 */
1923PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1924{
1925 pj_str_t tmp;
1926 pjsip_uri *uri;
1927 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001928 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001929 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001930
1931 PJSUA_LOCK();
1932
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001933 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001934
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001935 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001936
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001937 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001938 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001939 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001940 PJSUA_UNLOCK();
1941 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001942 }
1943
1944 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1945 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1946 {
1947 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001948 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1949 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001950 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001951 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001952 break;
1953 }
1954
Benny Prijono093d3022006-09-24 00:07:11 +00001955 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001956 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001957 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001958 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001959 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001960 }
1961
1962 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001963 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001964 PJSUA_UNLOCK();
1965 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001966 }
1967
Benny Prijonoa1e69682007-05-11 15:14:34 +00001968 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001969
Benny Prijonob4a17c92006-07-10 14:40:21 +00001970 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001971 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1972 unsigned acc_id = pjsua_var.acc_ids[i];
1973 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1974 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1975 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001976 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001977 PJSUA_UNLOCK();
1978 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001979 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001980 }
1981
Benny Prijonob4a17c92006-07-10 14:40:21 +00001982 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001983 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1984 unsigned acc_id = pjsua_var.acc_ids[i];
1985 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1986 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001987 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001988 PJSUA_UNLOCK();
1989 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001990 }
1991 }
1992
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001993
Benny Prijono093d3022006-09-24 00:07:11 +00001994 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001995 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001996 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001997 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001998}
1999
2000
2001/*
2002 * This is an internal function to find the most appropriate account to be
2003 * used to handle incoming calls.
2004 */
2005PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
2006{
2007 pjsip_uri *uri;
2008 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00002009 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002010
Benny Prijono371cf0a2007-06-19 00:35:37 +00002011 /* Check that there's at least one account configured */
2012 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
2013
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002014 uri = rdata->msg_info.to->uri;
2015
2016 /* Just return default account if To URI is not SIP: */
2017 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2018 !PJSIP_URI_SCHEME_IS_SIPS(uri))
2019 {
2020 return pjsua_var.default_acc;
2021 }
2022
2023
2024 PJSUA_LOCK();
2025
2026 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
2027
2028 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00002029 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2030 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002031 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2032
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002033 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00002034 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002035 {
2036 /* Match ! */
2037 PJSUA_UNLOCK();
2038 return acc_id;
2039 }
2040 }
2041
Benny Prijono093d3022006-09-24 00:07:11 +00002042 /* No matching account, try match domain part only. */
2043 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2044 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002045 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2046
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002047 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002048 /* Match ! */
2049 PJSUA_UNLOCK();
2050 return acc_id;
2051 }
2052 }
2053
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002054 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00002055 for (i=0; i < pjsua_var.acc_cnt; ++i) {
2056 unsigned acc_id = pjsua_var.acc_ids[i];
2057 pjsua_acc *acc = &pjsua_var.acc[acc_id];
2058
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002059 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
2060
2061 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2062 pjsip_transport_type_e type;
2063 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2064 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
2065 type = PJSIP_TRANSPORT_UDP;
2066
2067 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
2068 continue;
2069 }
2070
Benny Prijono093d3022006-09-24 00:07:11 +00002071 /* Match ! */
2072 PJSUA_UNLOCK();
2073 return acc_id;
2074 }
2075 }
2076
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002077 /* Still no match, use default account */
2078 PJSUA_UNLOCK();
2079 return pjsua_var.default_acc;
2080}
2081
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002082
Benny Prijonofff245c2007-04-02 11:44:47 +00002083/*
2084 * Create arbitrary requests for this account.
2085 */
2086PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
2087 const pjsip_method *method,
2088 const pj_str_t *target,
2089 pjsip_tx_data **p_tdata)
2090{
2091 pjsip_tx_data *tdata;
2092 pjsua_acc *acc;
2093 pjsip_route_hdr *r;
2094 pj_status_t status;
2095
2096 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
2097 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2098
2099 acc = &pjsua_var.acc[acc_id];
2100
2101 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
2102 &acc->cfg.id, target,
2103 NULL, NULL, -1, NULL, &tdata);
2104 if (status != PJ_SUCCESS) {
2105 pjsua_perror(THIS_FILE, "Unable to create request", status);
2106 return status;
2107 }
2108
2109 /* Copy routeset */
2110 r = acc->route_set.next;
2111 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00002112 pjsip_msg_add_hdr(tdata->msg,
2113 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00002114 r = r->next;
2115 }
Benny Prijonoe7911562009-12-14 11:13:45 +00002116
2117 /* If account is locked to specific transport, then set that transport to
2118 * the transmit data.
2119 */
2120 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
2121 pjsip_tpselector tp_sel;
2122
2123 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2124 pjsip_tx_data_set_transport(tdata, &tp_sel);
2125 }
2126
Benny Prijonofff245c2007-04-02 11:44:47 +00002127 /* Done */
2128 *p_tdata = tdata;
2129 return PJ_SUCCESS;
2130}
2131
2132
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002133PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
2134 pj_str_t *contact,
2135 pjsua_acc_id acc_id,
2136 const pj_str_t *suri)
2137{
2138 pjsua_acc *acc;
2139 pjsip_sip_uri *sip_uri;
2140 pj_status_t status;
2141 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2142 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002143 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002144 unsigned flag;
2145 int secure;
2146 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002147 const char *beginquote, *endquote;
2148 char transport_param[32];
2149
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002150
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002151 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002152 acc = &pjsua_var.acc[acc_id];
2153
Benny Prijonof75eceb2007-03-23 19:09:54 +00002154 /* If force_contact is configured, then use use it */
2155 if (acc->cfg.force_contact.slen) {
2156 *contact = acc->cfg.force_contact;
2157 return PJ_SUCCESS;
2158 }
2159
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002160 /* If route-set is configured for the account, then URI is the
2161 * first entry of the route-set.
2162 */
2163 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002164 sip_uri = (pjsip_sip_uri*)
2165 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002166 } else {
2167 pj_str_t tmp;
2168 pjsip_uri *uri;
2169
2170 pj_strdup_with_null(pool, &tmp, suri);
2171
2172 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
2173 if (uri == NULL)
2174 return PJSIP_EINVALIDURI;
2175
2176 /* For non-SIP scheme, route set should be configured */
2177 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
Benny Prijonoc7545782010-09-28 07:43:18 +00002178 return PJSIP_ENOROUTESET;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002179
Benny Prijono8c7a6172007-02-18 21:17:46 +00002180 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002181 }
2182
2183 /* Get transport type of the URI */
2184 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2185 tp_type = PJSIP_TRANSPORT_TLS;
2186 else if (sip_uri->transport_param.slen == 0) {
2187 tp_type = PJSIP_TRANSPORT_UDP;
2188 } else
2189 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
2190
2191 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2192 return PJSIP_EUNSUPTRANSPORT;
2193
Benny Prijonod0bd4982007-12-02 15:40:52 +00002194 /* If destination URI specifies IPv6, then set transport type
2195 * to use IPv6 as well.
2196 */
Benny Prijono19b29372007-12-05 04:08:40 +00002197 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00002198 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2199
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002200 flag = pjsip_transport_get_flag_from_type(tp_type);
2201 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2202
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002203 /* Init transport selector. */
2204 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2205
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002206 /* Get local address suitable to send request from */
2207 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002208 pool, tp_type, &tp_sel,
2209 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002210 if (status != PJ_SUCCESS)
2211 return status;
2212
Benny Prijonod0bd4982007-12-02 15:40:52 +00002213 /* Enclose IPv6 address in square brackets */
2214 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2215 beginquote = "[";
2216 endquote = "]";
2217 } else {
2218 beginquote = endquote = "";
2219 }
2220
2221 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002222 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002223 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2224 ";transport=%s",
2225 pjsip_transport_get_type_name(tp_type));
2226 } else {
2227 transport_param[0] = '\0';
2228 }
2229
2230
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002231 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002232 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002233 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002234 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002235 (int)acc->display.slen,
2236 acc->display.ptr,
2237 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002238 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002239 (int)acc->user_part.slen,
2240 acc->user_part.ptr,
2241 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002242 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002243 (int)local_addr.slen,
2244 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002245 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002246 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002247 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002248 (int)acc->cfg.contact_uri_params.slen,
2249 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002250 (int)acc->cfg.contact_params.slen,
2251 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002252
2253 return PJ_SUCCESS;
2254}
2255
2256
2257
2258PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
2259 pj_str_t *contact,
2260 pjsua_acc_id acc_id,
2261 pjsip_rx_data *rdata )
2262{
2263 /*
2264 * Section 12.1.1, paragraph about using SIPS URI in Contact.
2265 * If the request that initiated the dialog contained a SIPS URI
2266 * in the Request-URI or in the top Record-Route header field value,
2267 * if there was any, or the Contact header field if there was no
2268 * Record-Route header field, the Contact header field in the response
2269 * MUST be a SIPS URI.
2270 */
2271 pjsua_acc *acc;
2272 pjsip_sip_uri *sip_uri;
2273 pj_status_t status;
2274 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
2275 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002276 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002277 unsigned flag;
2278 int secure;
2279 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00002280 const char *beginquote, *endquote;
2281 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002282
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002283 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002284 acc = &pjsua_var.acc[acc_id];
2285
Benny Prijonof75eceb2007-03-23 19:09:54 +00002286 /* If force_contact is configured, then use use it */
2287 if (acc->cfg.force_contact.slen) {
2288 *contact = acc->cfg.force_contact;
2289 return PJ_SUCCESS;
2290 }
2291
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002292 /* If Record-Route is present, then URI is the top Record-Route. */
2293 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00002294 sip_uri = (pjsip_sip_uri*)
2295 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002296 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00002297 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002298 pjsip_contact_hdr *h_contact;
2299 pjsip_uri *uri = NULL;
2300
Benny Prijonoa330d452008-08-05 20:14:39 +00002301 /* Otherwise URI is Contact URI.
2302 * Iterate the Contact URI until we find sip: or sips: scheme.
2303 */
2304 do {
2305 h_contact = (pjsip_contact_hdr*)
2306 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
2307 pos);
2308 if (h_contact) {
Benny Prijono8b33bba2010-06-02 03:03:43 +00002309 if (h_contact->uri)
2310 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
2311 else
2312 uri = NULL;
2313 if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
2314 !PJSIP_URI_SCHEME_IS_SIPS(uri)))
Benny Prijonoa330d452008-08-05 20:14:39 +00002315 {
2316 pos = (pjsip_hdr*)h_contact->next;
2317 if (pos == &rdata->msg_info.msg->hdr)
2318 h_contact = NULL;
2319 } else {
2320 break;
2321 }
2322 }
2323 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002324
2325
2326 /* Or if Contact URI is not present, take the remote URI from
2327 * the From URI.
2328 */
2329 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00002330 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002331
2332
2333 /* Can only do sip/sips scheme at present. */
2334 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
2335 return PJSIP_EINVALIDREQURI;
2336
Benny Prijono8c7a6172007-02-18 21:17:46 +00002337 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002338 }
2339
2340 /* Get transport type of the URI */
2341 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
2342 tp_type = PJSIP_TRANSPORT_TLS;
2343 else if (sip_uri->transport_param.slen == 0) {
2344 tp_type = PJSIP_TRANSPORT_UDP;
2345 } else
2346 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00002347
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002348 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
2349 return PJSIP_EUNSUPTRANSPORT;
2350
Benny Prijonod0bd4982007-12-02 15:40:52 +00002351 /* If destination URI specifies IPv6, then set transport type
2352 * to use IPv6 as well.
2353 */
2354 if (pj_strchr(&sip_uri->host, ':'))
2355 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
2356
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002357 flag = pjsip_transport_get_flag_from_type(tp_type);
2358 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
2359
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002360 /* Init transport selector. */
2361 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
2362
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002363 /* Get local address suitable to send request from */
2364 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002365 pool, tp_type, &tp_sel,
2366 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002367 if (status != PJ_SUCCESS)
2368 return status;
2369
Benny Prijonod0bd4982007-12-02 15:40:52 +00002370 /* Enclose IPv6 address in square brackets */
2371 if (tp_type & PJSIP_TRANSPORT_IPV6) {
2372 beginquote = "[";
2373 endquote = "]";
2374 } else {
2375 beginquote = endquote = "";
2376 }
2377
2378 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00002379 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00002380 pj_ansi_snprintf(transport_param, sizeof(transport_param),
2381 ";transport=%s",
2382 pjsip_transport_get_type_name(tp_type));
2383 } else {
2384 transport_param[0] = '\0';
2385 }
2386
2387
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002388 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002389 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002390 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002391 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002392 (int)acc->display.slen,
2393 acc->display.ptr,
2394 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00002395 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002396 (int)acc->user_part.slen,
2397 acc->user_part.ptr,
2398 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00002399 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002400 (int)local_addr.slen,
2401 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00002402 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002403 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00002404 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00002405 (int)acc->cfg.contact_uri_params.slen,
2406 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00002407 (int)acc->cfg.contact_params.slen,
2408 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002409
2410 return PJ_SUCCESS;
2411}
2412
2413
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002414PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
2415 pjsua_transport_id tp_id)
2416{
2417 pjsua_acc *acc;
2418
2419 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
2420 acc = &pjsua_var.acc[acc_id];
2421
Benny Prijonoa1e69682007-05-11 15:14:34 +00002422 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002423 PJ_EINVAL);
2424
2425 acc->cfg.transport_id = tp_id;
2426
2427 return PJ_SUCCESS;
2428}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002429
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002430
2431/* Auto re-registration timeout callback */
2432static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
2433{
2434 pjsua_acc *acc;
2435 pj_status_t status;
2436
2437 PJ_UNUSED_ARG(th);
2438 acc = (pjsua_acc*) te->user_data;
2439 pj_assert(acc);
2440
2441 PJSUA_LOCK();
2442
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002443 /* Check if the reregistration timer is still valid, e.g: while waiting
2444 * timeout timer application might have deleted the account or disabled
2445 * the auto-reregistration.
2446 */
2447 if (!acc->valid || !acc->auto_rereg.active ||
2448 acc->cfg.reg_retry_interval == 0)
2449 {
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002450 goto on_return;
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002451 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002452
2453 /* Start re-registration */
2454 acc->auto_rereg.attempt_cnt++;
2455 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
2456 if (status != PJ_SUCCESS)
2457 schedule_reregistration(acc);
2458
Nanang Izzuddin66580002010-04-14 08:12:08 +00002459on_return:
2460 PJSUA_UNLOCK();
2461}
2462
2463
2464/* Schedule reregistration for specified account. Note that the first
2465 * re-registration after a registration failure will be done immediately.
2466 * Also note that this function should be called within PJSUA mutex.
2467 */
2468static void schedule_reregistration(pjsua_acc *acc)
2469{
2470 pj_time_val delay;
2471
Nanang Izzuddinc71bed62010-05-26 15:04:43 +00002472 pj_assert(acc);
2473
2474 /* Validate the account and re-registration feature status */
2475 if (!acc->valid || acc->cfg.reg_retry_interval == 0) {
2476 return;
2477 }
Nanang Izzuddin66580002010-04-14 08:12:08 +00002478
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002479 /* If configured, disconnect calls of this account after the first
2480 * reregistration attempt failed.
2481 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002482 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002483 {
2484 unsigned i, cnt;
2485
2486 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2487 if (pjsua_var.calls[i].acc_id == acc->index) {
2488 pjsua_call_hangup(i, 0, NULL, NULL);
2489 ++cnt;
2490 }
2491 }
2492
2493 if (cnt) {
2494 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2495 "after reregistration attempt failed",
2496 cnt, acc->index));
2497 }
2498 }
2499
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002500 /* Cancel any re-registration timer */
2501 pjsua_cancel_timer(&acc->auto_rereg.timer);
2502
2503 /* Update re-registration flag */
2504 acc->auto_rereg.active = PJ_TRUE;
2505
2506 /* Set up timer for reregistration */
2507 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2508 acc->auto_rereg.timer.user_data = acc;
2509
2510 /* Reregistration attempt. The first attempt will be done immediately. */
2511 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2512 delay.msec = 0;
2513 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2514}
2515
2516
2517/* Internal function to perform auto-reregistration on transport
2518 * connection/disconnection events.
2519 */
2520void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2521 pjsip_transport_state state,
2522 const pjsip_transport_state_info *info)
2523{
2524 unsigned i;
2525
2526 PJ_UNUSED_ARG(info);
2527
2528 /* Only care for transport disconnection events */
2529 if (state != PJSIP_TP_STATE_DISCONNECTED)
2530 return;
2531
2532 /* Shutdown this transport, to make sure that the transport manager
2533 * will create a new transport for reconnection.
2534 */
2535 pjsip_transport_shutdown(tp);
2536
2537 PJSUA_LOCK();
2538
2539 /* Enumerate accounts using this transport and perform actions
2540 * based on the transport state.
2541 */
2542 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2543 pjsua_acc *acc = &pjsua_var.acc[i];
2544
2545 /* Skip if this account is not valid OR auto re-registration
2546 * feature is disabled OR this transport is not used by this account.
2547 */
2548 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2549 tp != acc->auto_rereg.reg_tp)
2550 {
2551 continue;
2552 }
2553
2554 /* Schedule reregistration for this account */
2555 schedule_reregistration(acc);
2556 }
2557
2558 PJSUA_UNLOCK();
2559}