blob: e1bfc404968f97e320835341ac09ef573ee3e633 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjsua-lib/pjsua.h>
21#include <pjsua-lib/pjsua_internal.h>
22
23
24#define THIS_FILE "pjsua_acc.c"
25
26
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +000027static void schedule_reregistration(pjsua_acc *acc);
28
Benny Prijonoeebe9af2006-06-13 22:57:13 +000029/*
30 * Get number of current accounts.
31 */
32PJ_DEF(unsigned) pjsua_acc_get_count(void)
33{
34 return pjsua_var.acc_cnt;
35}
36
37
38/*
39 * Check if the specified account ID is valid.
40 */
41PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
42{
Benny Prijonoa1e69682007-05-11 15:14:34 +000043 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000044 pjsua_var.acc[acc_id].valid;
45}
46
47
48/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000049 * Set default account
50 */
51PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
52{
53 pjsua_var.default_acc = acc_id;
54 return PJ_SUCCESS;
55}
56
57
58/*
59 * Get default account.
60 */
61PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
62{
63 return pjsua_var.default_acc;
64}
65
66
67/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000068 * Copy account configuration.
69 */
Benny Prijonobddef2c2007-10-31 13:28:08 +000070PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
71 pjsua_acc_config *dst,
72 const pjsua_acc_config *src)
Benny Prijonoeebe9af2006-06-13 22:57:13 +000073{
74 unsigned i;
75
76 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
77
78 pj_strdup_with_null(pool, &dst->id, &src->id);
79 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000080 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Benny Prijonofe04fb52007-08-24 08:28:52 +000081 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000082
83 dst->proxy_cnt = src->proxy_cnt;
84 for (i=0; i<src->proxy_cnt; ++i)
85 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
86
87 dst->reg_timeout = src->reg_timeout;
88 dst->cred_count = src->cred_count;
89
90 for (i=0; i<src->cred_count; ++i) {
91 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
92 }
Benny Prijonobddef2c2007-10-31 13:28:08 +000093
94 dst->ka_interval = src->ka_interval;
95 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000096}
97
98
99/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000100 * Initialize a new account (after configuration is set).
101 */
102static pj_status_t initialize_acc(unsigned acc_id)
103{
104 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
105 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000106 pjsip_name_addr *name_addr;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000107 pjsip_sip_uri *sip_uri, *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000108 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000109 unsigned i;
110
111 /* Need to parse local_uri to get the elements: */
112
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000113 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000114 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000115 acc_cfg->id.slen,
116 PJSIP_PARSE_URI_AS_NAMEADDR);
117 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000118 pjsua_perror(THIS_FILE, "Invalid local URI",
119 PJSIP_EINVALIDURI);
120 return PJSIP_EINVALIDURI;
121 }
122
123 /* Local URI MUST be a SIP or SIPS: */
124
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000125 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
126 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000127 {
128 pjsua_perror(THIS_FILE, "Invalid local URI",
129 PJSIP_EINVALIDSCHEME);
130 return PJSIP_EINVALIDSCHEME;
131 }
132
133
134 /* Get the SIP URI object: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000135 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000136
Benny Prijonob4a17c92006-07-10 14:40:21 +0000137
138 /* Parse registrar URI, if any */
139 if (acc_cfg->reg_uri.slen) {
140 pjsip_uri *reg_uri;
141
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000142 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000143 acc_cfg->reg_uri.slen, 0);
144 if (reg_uri == NULL) {
145 pjsua_perror(THIS_FILE, "Invalid registrar URI",
146 PJSIP_EINVALIDURI);
147 return PJSIP_EINVALIDURI;
148 }
149
150 /* Registrar URI MUST be a SIP or SIPS: */
151 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
152 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
153 {
154 pjsua_perror(THIS_FILE, "Invalid registar URI",
155 PJSIP_EINVALIDSCHEME);
156 return PJSIP_EINVALIDSCHEME;
157 }
158
159 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
160
161 } else {
162 sip_reg_uri = NULL;
163 }
164
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000165 /* Save the user and domain part. These will be used when finding an
166 * account for incoming requests.
167 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000168 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000169 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000170 acc->srv_domain = sip_uri->host;
171 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000172
Benny Prijonob4a17c92006-07-10 14:40:21 +0000173 if (sip_reg_uri) {
174 acc->srv_port = sip_reg_uri->port;
175 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000176
177 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000178 //if (acc_cfg->contact.slen == 0) {
179 // acc_cfg->contact = acc_cfg->id;
180 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 /* Build account route-set from outbound proxies and route set from
183 * account configuration.
184 */
185 pj_list_init(&acc->route_set);
186
187 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
188 pj_str_t hname = { "Route", 5};
189 pjsip_route_hdr *r;
190 pj_str_t tmp;
191
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000192 pj_strdup_with_null(acc->pool, &tmp,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000194 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000195 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000196 if (r == NULL) {
197 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
198 PJSIP_EINVALIDURI);
199 return PJSIP_EINVALIDURI;
200 }
201 pj_list_push_back(&acc->route_set, r);
202 }
203
204 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
205 pj_str_t hname = { "Route", 5};
206 pjsip_route_hdr *r;
207 pj_str_t tmp;
208
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000209 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000210 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000211 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000212 if (r == NULL) {
213 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
214 PJ_EINVAL);
215 return PJ_EINVAL;
216 }
217 pj_list_push_back(&acc->route_set, r);
218 }
219
220
221 /* Concatenate credentials from account config and global config */
222 acc->cred_cnt = 0;
223 for (i=0; i<acc_cfg->cred_count; ++i) {
224 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
225 }
226 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
227 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
228 {
229 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
230 }
231
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000232 status = pjsua_pres_init_acc(acc_id);
233 if (status != PJ_SUCCESS)
234 return status;
235
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000236 /* Mark account as valid */
237 pjsua_var.acc[acc_id].valid = PJ_TRUE;
238
Benny Prijono093d3022006-09-24 00:07:11 +0000239 /* Insert account ID into account ID array, sorted by priority */
240 for (i=0; i<pjsua_var.acc_cnt; ++i) {
241 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
242 pjsua_var.acc[acc_id].cfg.priority)
243 {
244 break;
245 }
246 }
247 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
248 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000249
250 return PJ_SUCCESS;
251}
252
253
254/*
255 * Add a new account to pjsua.
256 */
257PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
258 pj_bool_t is_default,
259 pjsua_acc_id *p_acc_id)
260{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000261 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000262 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000263 pj_status_t status;
264
265 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
266 PJ_ETOOMANY);
267
268 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000269 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270
271 PJSUA_LOCK();
272
273 /* Find empty account id. */
274 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
275 if (pjsua_var.acc[id].valid == PJ_FALSE)
276 break;
277 }
278
279 /* Expect to find a slot */
280 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
281 {PJSUA_UNLOCK(); return PJ_EBUG;});
282
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000283 acc = &pjsua_var.acc[id];
284
285 /* Create pool for this account. */
286 if (acc->pool)
287 pj_pool_reset(acc->pool);
288 else
289 acc->pool = pjsua_pool_create("acc%p", 512, 256);
290
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000291 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000292 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000293
294 /* Normalize registration timeout */
295 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
296 pjsua_var.acc[id].cfg.reg_timeout == 0)
297 {
298 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
299 }
300
Benny Prijono91d06b62008-09-20 12:16:56 +0000301 /* Check the route URI's and force loose route if required */
302 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
303 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
304 if (status != PJ_SUCCESS)
305 return status;
306 }
307
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000308 status = initialize_acc(id);
309 if (status != PJ_SUCCESS) {
310 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000311 pj_pool_release(acc->pool);
312 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000313 PJSUA_UNLOCK();
314 return status;
315 }
316
317 if (is_default)
318 pjsua_var.default_acc = id;
319
320 if (p_acc_id)
321 *p_acc_id = id;
322
323 pjsua_var.acc_cnt++;
324
325 PJSUA_UNLOCK();
326
327 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
328 (int)cfg->id.slen, cfg->id.ptr, id));
329
330 /* If accounts has registration enabled, start registration */
331 if (pjsua_var.acc[id].cfg.reg_uri.slen)
332 pjsua_acc_set_registration(id, PJ_TRUE);
Benny Prijono4dd961b2009-10-26 11:21:37 +0000333 else {
334 /* Otherwise subscribe to MWI, if it's enabled */
335 if (pjsua_var.acc[id].cfg.mwi_enabled)
336 pjsua_start_mwi(&pjsua_var.acc[id]);
337 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000338
339 return PJ_SUCCESS;
340}
341
342
343/*
344 * Add local account
345 */
346PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
347 pj_bool_t is_default,
348 pjsua_acc_id *p_acc_id)
349{
350 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000351 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000352 const char *beginquote, *endquote;
353 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000354 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355
Benny Prijonoe93e2872006-06-28 16:46:49 +0000356 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000357 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
358 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000359
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000360 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000361 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000362
363 pjsua_acc_config_default(&cfg);
364
Benny Prijono093d3022006-09-24 00:07:11 +0000365 /* Lower the priority of local account */
366 --cfg.priority;
367
Benny Prijonod0bd4982007-12-02 15:40:52 +0000368 /* Enclose IPv6 address in square brackets */
369 if (t->type & PJSIP_TRANSPORT_IPV6) {
370 beginquote = "[";
371 endquote = "]";
372 } else {
373 beginquote = endquote = "";
374 }
375
376 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000377 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000378 pj_ansi_snprintf(transport_param, sizeof(transport_param),
379 ";transport=%s",
380 pjsip_transport_get_type_name(t->type));
381 } else {
382 transport_param[0] = '\0';
383 }
384
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000385 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000386 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000387 "<sip:%s%.*s%s:%d%s>",
388 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000389 (int)t->local_name.host.slen,
390 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000391 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000392 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000393 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000394
395 cfg.id = pj_str(uri);
396
397 return pjsua_acc_add(&cfg, is_default, p_acc_id);
398}
399
400
401/*
Benny Prijono705e7842008-07-21 18:12:51 +0000402 * Set arbitrary data to be associated with the account.
403 */
404PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
405 void *user_data)
406{
407 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
408 PJ_EINVAL);
409 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
410
411 PJSUA_LOCK();
412
413 pjsua_var.acc[acc_id].cfg.user_data = user_data;
414
415 PJSUA_UNLOCK();
416
417 return PJ_SUCCESS;
418}
419
420
421/*
422 * Retrieve arbitrary data associated with the account.
423 */
424PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
425{
426 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
427 NULL);
428 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
429
430 return pjsua_var.acc[acc_id].cfg.user_data;
431}
432
433
434/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000435 * Delete account.
436 */
437PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
438{
Benny Prijono093d3022006-09-24 00:07:11 +0000439 unsigned i;
440
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000441 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
442 PJ_EINVAL);
443 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
444
445 PJSUA_LOCK();
446
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000447 /* Cancel any re-registration timer */
448 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
449
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000450 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000451 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000452 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000453 if (pjsua_var.acc[acc_id].regc) {
454 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
455 }
Benny Prijono922933b2007-01-21 16:23:56 +0000456 pjsua_var.acc[acc_id].regc = NULL;
457 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000458
459 /* Delete server presence subscription */
460 pjsua_pres_delete_acc(acc_id);
461
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000462 /* Release account pool */
463 if (pjsua_var.acc[acc_id].pool) {
464 pj_pool_release(pjsua_var.acc[acc_id].pool);
465 pjsua_var.acc[acc_id].pool = NULL;
466 }
467
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000468 /* Invalidate */
469 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000470 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471
Benny Prijono093d3022006-09-24 00:07:11 +0000472 /* Remove from array */
473 for (i=0; i<pjsua_var.acc_cnt; ++i) {
474 if (pjsua_var.acc_ids[i] == acc_id)
475 break;
476 }
477 if (i != pjsua_var.acc_cnt) {
478 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
479 pjsua_var.acc_cnt, i);
480 --pjsua_var.acc_cnt;
481 }
482
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000483 /* Leave the calls intact, as I don't think calls need to
484 * access account once it's created
485 */
486
Benny Prijonofb2b3652007-06-28 07:15:03 +0000487 /* Update default account */
488 if (pjsua_var.default_acc == acc_id)
489 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000490
491 PJSUA_UNLOCK();
492
493 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
494
495 return PJ_SUCCESS;
496}
497
498
499/*
500 * Modify account information.
501 */
502PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
503 const pjsua_acc_config *cfg)
504{
505 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000506 PJ_UNUSED_ARG(acc_id);
507 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 return PJ_EINVALIDOP;
509}
510
511
512/*
513 * Modify account's presence status to be advertised to remote/presence
514 * subscribers.
515 */
516PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
517 pj_bool_t is_online)
518{
519 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
520 PJ_EINVAL);
521 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
522
523 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000524 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
525 pjsua_pres_update_acc(acc_id, PJ_FALSE);
526 return PJ_SUCCESS;
527}
528
529
530/*
531 * Set online status with extended information
532 */
533PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
534 pj_bool_t is_online,
535 const pjrpid_element *pr)
536{
537 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
538 PJ_EINVAL);
539 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
540
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000541 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000542 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000543 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
544 PJSUA_UNLOCK();
545
Benny Prijono4461c7d2007-08-25 13:36:15 +0000546 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 return PJ_SUCCESS;
548}
549
Benny Prijono7f630432008-09-24 16:52:41 +0000550/* Check if IP is private IP address */
551static pj_bool_t is_private_ip(const pj_str_t *addr)
552{
553 const pj_str_t private_net[] =
554 {
555 { "10.", 3 },
556 { "127.", 4 },
557 { "172.16.", 7 },
558 { "192.168.", 8 }
559 };
560 unsigned i;
561
562 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
563 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
564 return PJ_TRUE;
565 }
566
567 return PJ_FALSE;
568}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000569
Benny Prijono15b02302007-09-27 14:07:07 +0000570/* Update NAT address from the REGISTER response */
571static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
572 struct pjsip_regc_cbparam *param)
573{
574 pjsip_transport *tp;
575 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000576 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000577 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000578 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000579 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +0000580 pj_sockaddr contact_addr;
581 pj_sockaddr recv_addr;
582 pj_status_t status;
583 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +0000584 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000585 pjsip_contact_hdr *contact_hdr;
586 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +0000587
588 tp = param->rdata->tp_info.transport;
589
590 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000591 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000592 return PJ_FALSE;
593
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000594#if 0
595 // Always update
596 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000597
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000598 /* For UDP, only update if STUN is enabled (for now).
599 * For TCP/TLS, always check.
600 */
601 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
602 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
603 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
604 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
605 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000606 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000607 /* Yes we will check */
608 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000609 return PJ_FALSE;
610 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000611#endif
Benny Prijono15b02302007-09-27 14:07:07 +0000612
613 /* Get the received and rport info */
614 via = param->rdata->msg_info.via;
615 if (via->rport_param < 1) {
616 /* Remote doesn't support rport */
617 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +0000618 if (rport==0) {
619 pjsip_transport_type_e tp_type;
620 tp_type = (pjsip_transport_type_e) tp->key.type;
621 rport = pjsip_transport_get_default_port_for_type(tp_type);
622 }
Benny Prijono15b02302007-09-27 14:07:07 +0000623 } else
624 rport = via->rport_param;
625
626 if (via->recvd_param.slen != 0)
627 via_addr = &via->recvd_param;
628 else
629 via_addr = &via->sent_by.host;
630
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000631 /* Compare received and rport with the URI in our registration */
632 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000633 contact_hdr = (pjsip_contact_hdr*)
634 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
635 acc->contact.slen, NULL);
636 pj_assert(contact_hdr != NULL);
637 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000638 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +0000639 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000640
Benny Prijono24a21852008-04-14 04:04:30 +0000641 if (uri->port == 0) {
642 pjsip_transport_type_e tp_type;
643 tp_type = (pjsip_transport_type_e) tp->key.type;
644 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
645 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000646
Benny Prijono84d24932009-06-04 15:51:39 +0000647 /* Convert IP address strings into sockaddr for comparison.
648 * (http://trac.pjsip.org/repos/ticket/863)
649 */
650 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
651 &contact_addr);
652 if (status == PJ_SUCCESS)
653 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
654 &recv_addr);
655 if (status == PJ_SUCCESS) {
656 /* Compare the addresses as sockaddr according to the ticket above */
657 matched = (uri->port == rport &&
658 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
659 } else {
660 /* Compare the addresses as string, as before */
661 matched = (uri->port == rport &&
662 pj_stricmp(&uri->host, via_addr)==0);
663 }
664
665 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +0000666 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000667 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000668 return PJ_FALSE;
669 }
670
Benny Prijono7f630432008-09-24 16:52:41 +0000671 /* Get server IP */
672 srv_ip = pj_str(param->rdata->pkt_info.src_name);
673
Benny Prijono15b02302007-09-27 14:07:07 +0000674 /* At this point we've detected that the address as seen by registrar.
675 * has changed.
676 */
Benny Prijono7f630432008-09-24 16:52:41 +0000677
678 /* Do not switch if both Contact and server's IP address are
679 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +0000680 * might have messed up with the SIP packets. See:
681 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +0000682 *
683 * This exception can be disabled by setting allow_contact_rewrite
684 * to 2. In this case, the switch will always be done whenever there
685 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +0000686 */
Benny Prijono247921b2008-09-26 22:06:11 +0000687 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
688 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +0000689 {
690 /* Don't switch */
691 pj_pool_release(pool);
692 return PJ_FALSE;
693 }
694
Benny Prijono4f933762009-11-10 03:45:42 +0000695 /* Also don't switch if only the port number part is different, and
696 * the Via received address is private.
697 * See http://trac.pjsip.org/repos/ticket/864
698 */
699 if (acc->cfg.allow_contact_rewrite != 2 &&
700 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 &&
701 is_private_ip(via_addr))
702 {
703 /* Don't switch */
704 pj_pool_release(pool);
705 return PJ_FALSE;
706 }
707
Benny Prijono15b02302007-09-27 14:07:07 +0000708 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
709 "(%.*s:%d --> %.*s:%d). Updating registration..",
710 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000711 (int)uri->host.slen,
712 uri->host.ptr,
713 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000714 (int)via_addr->slen,
715 via_addr->ptr,
716 rport));
717
718 /* Unregister current contact */
719 pjsua_acc_set_registration(acc->index, PJ_FALSE);
720 if (acc->regc != NULL) {
721 pjsip_regc_destroy(acc->regc);
722 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000723 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +0000724 }
725
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000726 /* Update account's Contact header */
727 {
728 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +0000729 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000730 int len;
731
Benny Prijono8972bf02009-04-05 18:30:45 +0000732 /* Enclose IPv6 address in square brackets */
733 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
734 beginquote = "[";
735 endquote = "]";
736 } else {
737 beginquote = endquote = "";
738 }
739
Benny Prijono24a21852008-04-14 04:04:30 +0000740 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000741 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +0000742 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000743 (int)acc->user_part.slen,
744 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +0000745 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +0000746 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000747 (int)via_addr->slen,
748 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +0000749 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000750 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +0000751 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +0000752 (int)acc->cfg.contact_uri_params.slen,
753 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +0000754 (int)acc->cfg.contact_params.slen,
755 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000756 if (len < 1) {
757 PJ_LOG(1,(THIS_FILE, "URI too long"));
758 pj_pool_release(pool);
759 return PJ_FALSE;
760 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +0000761 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000762 }
763
Benny Prijono4f933762009-11-10 03:45:42 +0000764 /* Always update, by http://trac.pjsip.org/repos/ticket/864. */
765 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
766 tp->local_name.port = rport;
Benny Prijono15b02302007-09-27 14:07:07 +0000767
768 /* Perform new registration */
769 pjsua_acc_set_registration(acc->index, PJ_TRUE);
770
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000771 pj_pool_release(pool);
772
Benny Prijono15b02302007-09-27 14:07:07 +0000773 return PJ_TRUE;
774}
775
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000776/* Check and update Service-Route header */
777void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
778{
779 pjsip_generic_string_hdr *hsr = NULL;
780 pjsip_route_hdr *hr, *h;
781 const pj_str_t HNAME = { "Service-Route", 13 };
782 const pj_str_t HROUTE = { "Route", 5 };
783 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000784 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000785
786 /* Find and parse Service-Route headers */
787 for (;;) {
788 char saved;
789 int parsed_len;
790
791 /* Find Service-Route header */
792 hsr = (pjsip_generic_string_hdr*)
793 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
794 if (!hsr)
795 break;
796
797 /* Parse as Route header since the syntax is similar. This may
798 * return more than one headers.
799 */
800 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
801 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
802 hr = (pjsip_route_hdr*)
803 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
804 hsr->hvalue.slen, &parsed_len);
805 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
806
807 if (hr == NULL) {
808 /* Error */
809 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
810 return;
811 }
812
813 /* Save each URI in the result */
814 h = hr;
815 do {
816 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
817 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
818 {
819 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
820 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
821 return;
822 }
823
824 uri[uri_cnt++] = h->name_addr.uri;
825 h = h->next;
826 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
827
828 if (h != hr) {
829 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
830 return;
831 }
832
833 /* Prepare to find next Service-Route header */
834 hsr = hsr->next;
835 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
836 break;
837 }
838
839 if (uri_cnt == 0)
840 return;
841
842 /*
843 * Update account's route set
844 */
845
846 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000847 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000848 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
849 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
850 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000851 i<rcnt;
852 ++i)
853 {
854 pjsip_route_hdr *prev = hr->prev;
855 pj_list_erase(hr);
856 hr = prev;
857 }
858 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000859
860 /* Then append the Service-Route URIs */
861 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000862 hr = pjsip_route_hdr_create(acc->pool);
863 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000864 pj_list_push_back(&acc->route_set, hr);
865 }
866
867 /* Done */
868
869 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
870 acc->index, uri_cnt));
871}
872
Benny Prijonobddef2c2007-10-31 13:28:08 +0000873
874/* Keep alive timer callback */
875static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
876{
877 pjsua_acc *acc;
878 pjsip_tpselector tp_sel;
879 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000880 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000881 pj_status_t status;
882
883 PJ_UNUSED_ARG(th);
884
885 PJSUA_LOCK();
886
887 te->id = PJ_FALSE;
888
889 acc = (pjsua_acc*) te->user_data;
890
891 /* Select the transport to send the packet */
892 pj_bzero(&tp_sel, sizeof(tp_sel));
893 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
894 tp_sel.u.transport = acc->ka_transport;
895
896 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000897 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000898 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000899 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000900
901 /* Send raw packet */
902 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
903 PJSIP_TRANSPORT_UDP, &tp_sel,
904 NULL, acc->cfg.ka_data.ptr,
905 acc->cfg.ka_data.slen,
906 &acc->ka_target, acc->ka_target_len,
907 NULL, NULL);
908
909 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
910 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
911 }
912
913 /* Reschedule next timer */
914 delay.sec = acc->cfg.ka_interval;
915 delay.msec = 0;
916 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
917 if (status == PJ_SUCCESS) {
918 te->id = PJ_TRUE;
919 } else {
920 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
921 }
922
923 PJSUA_UNLOCK();
924}
925
926
927/* Update keep-alive for the account */
928static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
929 struct pjsip_regc_cbparam *param)
930{
931 /* In all cases, stop keep-alive timer if it's running. */
932 if (acc->ka_timer.id) {
933 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
934 acc->ka_timer.id = PJ_FALSE;
935
936 pjsip_transport_dec_ref(acc->ka_transport);
937 acc->ka_transport = NULL;
938 }
939
940 if (start) {
941 pj_time_val delay;
942 pj_status_t status;
943
944 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000945 * - ka_interval is not zero in the account, and
946 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000947 *
948 * Previously we only enabled keep-alive when STUN is enabled, since
949 * we thought that keep-alive is only needed in Internet situation.
950 * But it has been discovered that Windows Firewall on WinXP also
951 * needs to be kept-alive, otherwise incoming packets will be dropped.
952 * So because of this, now keep-alive is always enabled for UDP,
953 * regardless of whether STUN is enabled or not.
954 *
955 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
956 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000957 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000958 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000959 acc->cfg.ka_interval == 0 ||
960 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
961 {
962 /* Keep alive is not necessary */
963 return;
964 }
965
966 /* Save transport and destination address. */
967 acc->ka_transport = param->rdata->tp_info.transport;
968 pjsip_transport_add_ref(acc->ka_transport);
969 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
970 param->rdata->pkt_info.src_addr_len);
971 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
972
973 /* Setup and start the timer */
974 acc->ka_timer.cb = &keep_alive_timer_cb;
975 acc->ka_timer.user_data = (void*)acc;
976
977 delay.sec = acc->cfg.ka_interval;
978 delay.msec = 0;
979 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
980 &delay);
981 if (status == PJ_SUCCESS) {
982 acc->ka_timer.id = PJ_TRUE;
983 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
984 "destination:%s:%d, interval:%ds",
985 acc->index,
986 param->rdata->pkt_info.src_name,
987 param->rdata->pkt_info.src_port,
988 acc->cfg.ka_interval));
989 } else {
990 acc->ka_timer.id = PJ_FALSE;
991 pjsip_transport_dec_ref(acc->ka_transport);
992 acc->ka_transport = NULL;
993 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
994 }
995 }
996}
997
998
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000999/*
1000 * This callback is called by pjsip_regc when outgoing register
1001 * request has completed.
1002 */
1003static void regc_cb(struct pjsip_regc_cbparam *param)
1004{
1005
Benny Prijonoa1e69682007-05-11 15:14:34 +00001006 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001007
Benny Prijono15b02302007-09-27 14:07:07 +00001008 if (param->regc != acc->regc)
1009 return;
1010
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001011 PJSUA_LOCK();
1012
1013 /*
1014 * Print registration status.
1015 */
1016 if (param->status!=PJ_SUCCESS) {
1017 pjsua_perror(THIS_FILE, "SIP registration error",
1018 param->status);
1019 pjsip_regc_destroy(acc->regc);
1020 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001021 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001022
Benny Prijonobddef2c2007-10-31 13:28:08 +00001023 /* Stop keep-alive timer if any. */
1024 update_keep_alive(acc, PJ_FALSE, NULL);
1025
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001026 } else if (param->code < 0 || param->code >= 300) {
1027 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1028 param->code,
1029 (int)param->reason.slen, param->reason.ptr));
1030 pjsip_regc_destroy(acc->regc);
1031 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001032 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001033
Benny Prijonobddef2c2007-10-31 13:28:08 +00001034 /* Stop keep-alive timer if any. */
1035 update_keep_alive(acc, PJ_FALSE, NULL);
1036
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001037 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1038
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001039 /* Update auto registration flag */
1040 acc->auto_rereg.active = PJ_FALSE;
1041 acc->auto_rereg.attempt_cnt = 0;
1042
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001043 if (param->expiration < 1) {
1044 pjsip_regc_destroy(acc->regc);
1045 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001046 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001047
1048 /* Stop keep-alive timer if any. */
1049 update_keep_alive(acc, PJ_FALSE, NULL);
1050
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001051 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1052 pjsua_var.acc[acc->index].cfg.id.ptr));
1053 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001054 /* Check NAT bound address */
1055 if (acc_check_nat_addr(acc, param)) {
1056 /* Update address, don't notify application yet */
1057 PJSUA_UNLOCK();
1058 return;
1059 }
1060
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001061 /* Check and update Service-Route header */
1062 update_service_route(acc, param->rdata);
1063
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001064 PJ_LOG(3, (THIS_FILE,
1065 "%s: registration success, status=%d (%.*s), "
1066 "will re-register in %d seconds",
1067 pjsua_var.acc[acc->index].cfg.id.ptr,
1068 param->code,
1069 (int)param->reason.slen, param->reason.ptr,
1070 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001071
Benny Prijonobddef2c2007-10-31 13:28:08 +00001072 /* Start keep-alive timer if necessary. */
1073 update_keep_alive(acc, PJ_TRUE, param);
1074
Benny Prijono8b6834f2007-02-24 13:29:22 +00001075 /* Send initial PUBLISH if it is enabled */
1076 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1077 pjsua_pres_init_publish_acc(acc->index);
Benny Prijono4dd961b2009-10-26 11:21:37 +00001078
1079 /* Subscribe to MWI, if it's enabled */
1080 if (acc->cfg.mwi_enabled)
1081 pjsua_start_mwi(acc);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001082 }
1083
1084 } else {
1085 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1086 }
1087
1088 acc->reg_last_err = param->status;
1089 acc->reg_last_code = param->code;
1090
1091 if (pjsua_var.ua_cfg.cb.on_reg_state)
1092 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1093
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001094 /* Check if we need to auto retry registration. Basically, registration
1095 * failure codes triggering auto-retry are those of temporal failures
1096 * considered to be recoverable in relatively short term.
1097 */
1098 if (acc->cfg.reg_retry_interval &&
1099 (param->code == PJSIP_SC_REQUEST_TIMEOUT ||
1100 param->code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
1101 param->code == PJSIP_SC_BAD_GATEWAY ||
1102 param->code == PJSIP_SC_SERVICE_UNAVAILABLE ||
1103 param->code == PJSIP_SC_SERVER_TIMEOUT ||
1104 PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */
1105 {
1106 schedule_reregistration(acc);
1107 }
1108
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001109 PJSUA_UNLOCK();
1110}
1111
1112
1113/*
1114 * Initialize client registration.
1115 */
1116static pj_status_t pjsua_regc_init(int acc_id)
1117{
1118 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001119 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001120 pj_status_t status;
1121
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001122 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001123 acc = &pjsua_var.acc[acc_id];
1124
1125 if (acc->cfg.reg_uri.slen == 0) {
1126 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1127 return PJ_SUCCESS;
1128 }
1129
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001130 /* Destroy existing session, if any */
1131 if (acc->regc) {
1132 pjsip_regc_destroy(acc->regc);
1133 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001134 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001135 }
1136
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001137 /* initialize SIP registration if registrar is configured */
1138
1139 status = pjsip_regc_create( pjsua_var.endpt,
1140 acc, &regc_cb, &acc->regc);
1141
1142 if (status != PJ_SUCCESS) {
1143 pjsua_perror(THIS_FILE, "Unable to create client registration",
1144 status);
1145 return status;
1146 }
1147
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001148 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001149
1150 if (acc->contact.slen == 0) {
1151 pj_str_t tmp_contact;
1152
1153 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1154 acc_id, &acc->cfg.reg_uri);
1155 if (status != PJ_SUCCESS) {
1156 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1157 " for registration",
1158 status);
1159 pjsip_regc_destroy(acc->regc);
1160 pj_pool_release(pool);
1161 acc->regc = NULL;
1162 return status;
1163 }
1164
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001165 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001166 }
1167
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001168 status = pjsip_regc_init( acc->regc,
1169 &acc->cfg.reg_uri,
1170 &acc->cfg.id,
1171 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001172 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001173 acc->cfg.reg_timeout);
1174 if (status != PJ_SUCCESS) {
1175 pjsua_perror(THIS_FILE,
1176 "Client registration initialization error",
1177 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001178 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001179 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001180 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001181 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001182 return status;
1183 }
1184
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001185 /* If account is locked to specific transport, then set transport to
1186 * the client registration.
1187 */
1188 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1189 pjsip_tpselector tp_sel;
1190
1191 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1192 pjsip_regc_set_transport(acc->regc, &tp_sel);
1193 }
1194
1195
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001196 /* Set credentials
1197 */
1198 if (acc->cred_cnt) {
1199 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1200 }
1201
Benny Prijono48ab2b72007-11-08 09:24:30 +00001202 /* Set authentication preference */
1203 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1204
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001205 /* Set route-set
1206 */
1207 if (!pj_list_empty(&acc->route_set)) {
1208 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1209 }
1210
Benny Prijono8fc6de02006-11-11 21:25:55 +00001211 /* Add other request headers. */
1212 if (pjsua_var.ua_cfg.user_agent.slen) {
1213 pjsip_hdr hdr_list;
1214 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1215 pjsip_generic_string_hdr *h;
1216
Benny Prijono8fc6de02006-11-11 21:25:55 +00001217 pj_list_init(&hdr_list);
1218
1219 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1220 &pjsua_var.ua_cfg.user_agent);
1221 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1222
1223 pjsip_regc_add_headers(acc->regc, &hdr_list);
1224 }
1225
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001226 pj_pool_release(pool);
1227
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001228 return PJ_SUCCESS;
1229}
1230
1231
1232/*
1233 * Update registration or perform unregistration.
1234 */
1235PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1236 pj_bool_t renew)
1237{
1238 pj_status_t status = 0;
1239 pjsip_tx_data *tdata = 0;
1240
1241 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1242 PJ_EINVAL);
1243 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1244
1245 PJSUA_LOCK();
1246
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001247 /* Cancel any re-registration timer */
1248 pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer);
1249
1250 /* Reset pointer to registration transport */
1251 pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL;
1252
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001253 if (renew) {
1254 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001255 status = pjsua_regc_init(acc_id);
1256 if (status != PJ_SUCCESS) {
1257 pjsua_perror(THIS_FILE, "Unable to create registration",
1258 status);
1259 goto on_return;
1260 }
1261 }
1262 if (!pjsua_var.acc[acc_id].regc) {
1263 status = PJ_EINVALIDOP;
1264 goto on_return;
1265 }
1266
1267 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1268 &tdata);
1269
Benny Prijono28f673a2007-10-15 07:04:59 +00001270 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1271 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1272 pjsip_authorization_hdr *h;
1273 char *uri;
1274 int d;
1275
1276 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1277 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1278 uri, acc->cfg.reg_uri.slen+10);
1279 pj_assert(d > 0);
1280
1281 h = pjsip_authorization_hdr_create(tdata->pool);
1282 h->scheme = pj_str("Digest");
1283 h->credential.digest.username = acc->cred[0].username;
1284 h->credential.digest.realm = acc->srv_domain;
1285 h->credential.digest.uri = pj_str(uri);
1286 h->credential.digest.algorithm = pj_str("md5");
1287
1288 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1289 }
1290
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001291 } else {
1292 if (pjsua_var.acc[acc_id].regc == NULL) {
1293 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1294 status = PJ_EINVALIDOP;
1295 goto on_return;
1296 }
Benny Prijono166d5022010-02-10 14:24:48 +00001297
1298 pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
1299
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001300 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1301 }
1302
Benny Prijono56315612006-07-18 14:39:40 +00001303 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001304 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001306 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001307
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001308 /* Update pointer to registration transport */
1309 if (status == PJ_SUCCESS) {
1310 pjsip_regc_info reg_info;
1311
1312 pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, &reg_info);
1313 pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport;
1314 }
1315
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001316 if (status != PJ_SUCCESS) {
1317 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1318 status);
1319 } else {
1320 PJ_LOG(3,(THIS_FILE, "%s sent",
1321 (renew? "Registration" : "Unregistration")));
1322 }
1323
1324on_return:
1325 PJSUA_UNLOCK();
1326 return status;
1327}
1328
1329
1330/*
1331 * Get account information.
1332 */
1333PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1334 pjsua_acc_info *info)
1335{
1336 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1337 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1338
1339 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001340 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001341
Benny Prijonoac623b32006-07-03 15:19:31 +00001342 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001343
1344 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1345 PJ_EINVAL);
1346 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1347
1348 PJSUA_LOCK();
1349
1350 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1351 PJSUA_UNLOCK();
1352 return PJ_EINVALIDOP;
1353 }
1354
1355 info->id = acc_id;
1356 info->is_default = (pjsua_var.default_acc == acc_id);
1357 info->acc_uri = acc_cfg->id;
1358 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1359 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001360 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1361 if (info->rpid.note.slen)
1362 info->online_status_text = info->rpid.note;
1363 else if (info->online_status)
1364 info->online_status_text = pj_str("Online");
1365 else
1366 info->online_status_text = pj_str("Offline");
1367
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001368 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001369 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001370 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1371 info->status_text = pj_str(info->buf_);
1372 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001373 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001374 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001375 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1376 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001377 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001378 info->status_text = pj_str("not registered");
1379 }
1380 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001381 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001382 info->status_text = pj_str("In Progress");
1383 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001384 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001385 info->status_text = pj_str("does not register");
1386 }
1387
1388 if (acc->regc) {
1389 pjsip_regc_info regc_info;
1390 pjsip_regc_get_info(acc->regc, &regc_info);
1391 info->expires = regc_info.next_reg;
1392 } else {
1393 info->expires = -1;
1394 }
1395
1396 PJSUA_UNLOCK();
1397
1398 return PJ_SUCCESS;
1399
1400}
1401
1402
1403/*
1404 * Enum accounts all account ids.
1405 */
1406PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1407 unsigned *count )
1408{
1409 unsigned i, c;
1410
1411 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1412
1413 PJSUA_LOCK();
1414
1415 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1416 if (!pjsua_var.acc[i].valid)
1417 continue;
1418 ids[c] = i;
1419 ++c;
1420 }
1421
1422 *count = c;
1423
1424 PJSUA_UNLOCK();
1425
1426 return PJ_SUCCESS;
1427}
1428
1429
1430/*
1431 * Enum accounts info.
1432 */
1433PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1434 unsigned *count )
1435{
1436 unsigned i, c;
1437
1438 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1439
1440 PJSUA_LOCK();
1441
1442 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1443 if (!pjsua_var.acc[i].valid)
1444 continue;
1445
1446 pjsua_acc_get_info(i, &info[c]);
1447 ++c;
1448 }
1449
1450 *count = c;
1451
1452 PJSUA_UNLOCK();
1453
1454 return PJ_SUCCESS;
1455}
1456
1457
1458/*
1459 * This is an internal function to find the most appropriate account to
1460 * used to reach to the specified URL.
1461 */
1462PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1463{
1464 pj_str_t tmp;
1465 pjsip_uri *uri;
1466 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001467 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001468 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001469
1470 PJSUA_LOCK();
1471
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001472 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001473
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001474 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001475
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001476 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001477 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001478 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001479 PJSUA_UNLOCK();
1480 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001481 }
1482
1483 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1484 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1485 {
1486 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001487 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1488 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001489 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001490 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001491 break;
1492 }
1493
Benny Prijono093d3022006-09-24 00:07:11 +00001494 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001495 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001496 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001497 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001498 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001499 }
1500
1501 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001502 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001503 PJSUA_UNLOCK();
1504 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001505 }
1506
Benny Prijonoa1e69682007-05-11 15:14:34 +00001507 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001508
Benny Prijonob4a17c92006-07-10 14:40:21 +00001509 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001510 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1511 unsigned acc_id = pjsua_var.acc_ids[i];
1512 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1513 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1514 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001515 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001516 PJSUA_UNLOCK();
1517 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001518 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001519 }
1520
Benny Prijonob4a17c92006-07-10 14:40:21 +00001521 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001522 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1523 unsigned acc_id = pjsua_var.acc_ids[i];
1524 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1525 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001526 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001527 PJSUA_UNLOCK();
1528 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001529 }
1530 }
1531
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001532
Benny Prijono093d3022006-09-24 00:07:11 +00001533 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001534 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001535 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001536 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001537}
1538
1539
1540/*
1541 * This is an internal function to find the most appropriate account to be
1542 * used to handle incoming calls.
1543 */
1544PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1545{
1546 pjsip_uri *uri;
1547 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001548 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001549
Benny Prijono371cf0a2007-06-19 00:35:37 +00001550 /* Check that there's at least one account configured */
1551 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1552
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001553 uri = rdata->msg_info.to->uri;
1554
1555 /* Just return default account if To URI is not SIP: */
1556 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1557 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1558 {
1559 return pjsua_var.default_acc;
1560 }
1561
1562
1563 PJSUA_LOCK();
1564
1565 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1566
1567 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001568 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1569 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001570 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1571
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001572 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001573 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001574 {
1575 /* Match ! */
1576 PJSUA_UNLOCK();
1577 return acc_id;
1578 }
1579 }
1580
Benny Prijono093d3022006-09-24 00:07:11 +00001581 /* No matching account, try match domain part only. */
1582 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1583 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001584 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1585
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001586 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001587 /* Match ! */
1588 PJSUA_UNLOCK();
1589 return acc_id;
1590 }
1591 }
1592
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001593 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001594 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1595 unsigned acc_id = pjsua_var.acc_ids[i];
1596 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1597
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001598 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1599
1600 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1601 pjsip_transport_type_e type;
1602 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1603 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1604 type = PJSIP_TRANSPORT_UDP;
1605
1606 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1607 continue;
1608 }
1609
Benny Prijono093d3022006-09-24 00:07:11 +00001610 /* Match ! */
1611 PJSUA_UNLOCK();
1612 return acc_id;
1613 }
1614 }
1615
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001616 /* Still no match, use default account */
1617 PJSUA_UNLOCK();
1618 return pjsua_var.default_acc;
1619}
1620
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001621
Benny Prijonofff245c2007-04-02 11:44:47 +00001622/*
1623 * Create arbitrary requests for this account.
1624 */
1625PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1626 const pjsip_method *method,
1627 const pj_str_t *target,
1628 pjsip_tx_data **p_tdata)
1629{
1630 pjsip_tx_data *tdata;
1631 pjsua_acc *acc;
1632 pjsip_route_hdr *r;
1633 pj_status_t status;
1634
1635 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1636 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1637
1638 acc = &pjsua_var.acc[acc_id];
1639
1640 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1641 &acc->cfg.id, target,
1642 NULL, NULL, -1, NULL, &tdata);
1643 if (status != PJ_SUCCESS) {
1644 pjsua_perror(THIS_FILE, "Unable to create request", status);
1645 return status;
1646 }
1647
1648 /* Copy routeset */
1649 r = acc->route_set.next;
1650 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001651 pjsip_msg_add_hdr(tdata->msg,
1652 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001653 r = r->next;
1654 }
Benny Prijonoe7911562009-12-14 11:13:45 +00001655
1656 /* If account is locked to specific transport, then set that transport to
1657 * the transmit data.
1658 */
1659 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1660 pjsip_tpselector tp_sel;
1661
1662 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
1663 pjsip_tx_data_set_transport(tdata, &tp_sel);
1664 }
1665
Benny Prijonofff245c2007-04-02 11:44:47 +00001666 /* Done */
1667 *p_tdata = tdata;
1668 return PJ_SUCCESS;
1669}
1670
1671
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001672PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1673 pj_str_t *contact,
1674 pjsua_acc_id acc_id,
1675 const pj_str_t *suri)
1676{
1677 pjsua_acc *acc;
1678 pjsip_sip_uri *sip_uri;
1679 pj_status_t status;
1680 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1681 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001682 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001683 unsigned flag;
1684 int secure;
1685 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001686 const char *beginquote, *endquote;
1687 char transport_param[32];
1688
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001689
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001690 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001691 acc = &pjsua_var.acc[acc_id];
1692
Benny Prijonof75eceb2007-03-23 19:09:54 +00001693 /* If force_contact is configured, then use use it */
1694 if (acc->cfg.force_contact.slen) {
1695 *contact = acc->cfg.force_contact;
1696 return PJ_SUCCESS;
1697 }
1698
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001699 /* If route-set is configured for the account, then URI is the
1700 * first entry of the route-set.
1701 */
1702 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001703 sip_uri = (pjsip_sip_uri*)
1704 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001705 } else {
1706 pj_str_t tmp;
1707 pjsip_uri *uri;
1708
1709 pj_strdup_with_null(pool, &tmp, suri);
1710
1711 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1712 if (uri == NULL)
1713 return PJSIP_EINVALIDURI;
1714
1715 /* For non-SIP scheme, route set should be configured */
1716 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1717 return PJSIP_EINVALIDREQURI;
1718
Benny Prijono8c7a6172007-02-18 21:17:46 +00001719 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001720 }
1721
1722 /* Get transport type of the URI */
1723 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1724 tp_type = PJSIP_TRANSPORT_TLS;
1725 else if (sip_uri->transport_param.slen == 0) {
1726 tp_type = PJSIP_TRANSPORT_UDP;
1727 } else
1728 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1729
1730 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1731 return PJSIP_EUNSUPTRANSPORT;
1732
Benny Prijonod0bd4982007-12-02 15:40:52 +00001733 /* If destination URI specifies IPv6, then set transport type
1734 * to use IPv6 as well.
1735 */
Benny Prijono19b29372007-12-05 04:08:40 +00001736 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001737 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1738
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001739 flag = pjsip_transport_get_flag_from_type(tp_type);
1740 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1741
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001742 /* Init transport selector. */
1743 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1744
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001745 /* Get local address suitable to send request from */
1746 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001747 pool, tp_type, &tp_sel,
1748 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001749 if (status != PJ_SUCCESS)
1750 return status;
1751
Benny Prijonod0bd4982007-12-02 15:40:52 +00001752 /* Enclose IPv6 address in square brackets */
1753 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1754 beginquote = "[";
1755 endquote = "]";
1756 } else {
1757 beginquote = endquote = "";
1758 }
1759
1760 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00001761 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00001762 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1763 ";transport=%s",
1764 pjsip_transport_get_type_name(tp_type));
1765 } else {
1766 transport_param[0] = '\0';
1767 }
1768
1769
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001770 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001771 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001772 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001773 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001774 (int)acc->display.slen,
1775 acc->display.ptr,
1776 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001777 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001778 (int)acc->user_part.slen,
1779 acc->user_part.ptr,
1780 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001781 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001782 (int)local_addr.slen,
1783 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001784 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001785 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00001786 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001787 (int)acc->cfg.contact_uri_params.slen,
1788 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00001789 (int)acc->cfg.contact_params.slen,
1790 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001791
1792 return PJ_SUCCESS;
1793}
1794
1795
1796
1797PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1798 pj_str_t *contact,
1799 pjsua_acc_id acc_id,
1800 pjsip_rx_data *rdata )
1801{
1802 /*
1803 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1804 * If the request that initiated the dialog contained a SIPS URI
1805 * in the Request-URI or in the top Record-Route header field value,
1806 * if there was any, or the Contact header field if there was no
1807 * Record-Route header field, the Contact header field in the response
1808 * MUST be a SIPS URI.
1809 */
1810 pjsua_acc *acc;
1811 pjsip_sip_uri *sip_uri;
1812 pj_status_t status;
1813 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1814 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001815 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001816 unsigned flag;
1817 int secure;
1818 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001819 const char *beginquote, *endquote;
1820 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001821
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001822 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001823 acc = &pjsua_var.acc[acc_id];
1824
Benny Prijonof75eceb2007-03-23 19:09:54 +00001825 /* If force_contact is configured, then use use it */
1826 if (acc->cfg.force_contact.slen) {
1827 *contact = acc->cfg.force_contact;
1828 return PJ_SUCCESS;
1829 }
1830
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001831 /* If Record-Route is present, then URI is the top Record-Route. */
1832 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001833 sip_uri = (pjsip_sip_uri*)
1834 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001835 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00001836 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001837 pjsip_contact_hdr *h_contact;
1838 pjsip_uri *uri = NULL;
1839
Benny Prijonoa330d452008-08-05 20:14:39 +00001840 /* Otherwise URI is Contact URI.
1841 * Iterate the Contact URI until we find sip: or sips: scheme.
1842 */
1843 do {
1844 h_contact = (pjsip_contact_hdr*)
1845 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
1846 pos);
1847 if (h_contact) {
1848 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
1849 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1850 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1851 {
1852 pos = (pjsip_hdr*)h_contact->next;
1853 if (pos == &rdata->msg_info.msg->hdr)
1854 h_contact = NULL;
1855 } else {
1856 break;
1857 }
1858 }
1859 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001860
1861
1862 /* Or if Contact URI is not present, take the remote URI from
1863 * the From URI.
1864 */
1865 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001866 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001867
1868
1869 /* Can only do sip/sips scheme at present. */
1870 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1871 return PJSIP_EINVALIDREQURI;
1872
Benny Prijono8c7a6172007-02-18 21:17:46 +00001873 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001874 }
1875
1876 /* Get transport type of the URI */
1877 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1878 tp_type = PJSIP_TRANSPORT_TLS;
1879 else if (sip_uri->transport_param.slen == 0) {
1880 tp_type = PJSIP_TRANSPORT_UDP;
1881 } else
1882 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001883
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001884 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1885 return PJSIP_EUNSUPTRANSPORT;
1886
Benny Prijonod0bd4982007-12-02 15:40:52 +00001887 /* If destination URI specifies IPv6, then set transport type
1888 * to use IPv6 as well.
1889 */
1890 if (pj_strchr(&sip_uri->host, ':'))
1891 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1892
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001893 flag = pjsip_transport_get_flag_from_type(tp_type);
1894 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1895
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001896 /* Init transport selector. */
1897 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1898
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001899 /* Get local address suitable to send request from */
1900 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001901 pool, tp_type, &tp_sel,
1902 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001903 if (status != PJ_SUCCESS)
1904 return status;
1905
Benny Prijonod0bd4982007-12-02 15:40:52 +00001906 /* Enclose IPv6 address in square brackets */
1907 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1908 beginquote = "[";
1909 endquote = "]";
1910 } else {
1911 beginquote = endquote = "";
1912 }
1913
1914 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00001915 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00001916 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1917 ";transport=%s",
1918 pjsip_transport_get_type_name(tp_type));
1919 } else {
1920 transport_param[0] = '\0';
1921 }
1922
1923
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001924 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001925 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001926 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001927 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001928 (int)acc->display.slen,
1929 acc->display.ptr,
1930 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001931 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001932 (int)acc->user_part.slen,
1933 acc->user_part.ptr,
1934 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001935 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001936 (int)local_addr.slen,
1937 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001938 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001939 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00001940 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001941 (int)acc->cfg.contact_uri_params.slen,
1942 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00001943 (int)acc->cfg.contact_params.slen,
1944 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001945
1946 return PJ_SUCCESS;
1947}
1948
1949
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001950PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1951 pjsua_transport_id tp_id)
1952{
1953 pjsua_acc *acc;
1954
1955 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1956 acc = &pjsua_var.acc[acc_id];
1957
Benny Prijonoa1e69682007-05-11 15:14:34 +00001958 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001959 PJ_EINVAL);
1960
1961 acc->cfg.transport_id = tp_id;
1962
1963 return PJ_SUCCESS;
1964}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001965
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001966
1967/* Auto re-registration timeout callback */
1968static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
1969{
1970 pjsua_acc *acc;
1971 pj_status_t status;
1972
1973 PJ_UNUSED_ARG(th);
1974 acc = (pjsua_acc*) te->user_data;
1975 pj_assert(acc);
1976
1977 PJSUA_LOCK();
1978
1979 if (!acc->valid || !acc->auto_rereg.active)
1980 goto on_return;
1981
1982 /* Start re-registration */
1983 acc->auto_rereg.attempt_cnt++;
1984 status = pjsua_acc_set_registration(acc->index, PJ_TRUE);
1985 if (status != PJ_SUCCESS)
1986 schedule_reregistration(acc);
1987
Nanang Izzuddin66580002010-04-14 08:12:08 +00001988on_return:
1989 PJSUA_UNLOCK();
1990}
1991
1992
1993/* Schedule reregistration for specified account. Note that the first
1994 * re-registration after a registration failure will be done immediately.
1995 * Also note that this function should be called within PJSUA mutex.
1996 */
1997static void schedule_reregistration(pjsua_acc *acc)
1998{
1999 pj_time_val delay;
2000
2001 pj_assert(acc && acc->valid && acc->cfg.reg_retry_interval);
2002
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002003 /* If configured, disconnect calls of this account after the first
2004 * reregistration attempt failed.
2005 */
Nanang Izzuddin66580002010-04-14 08:12:08 +00002006 if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1)
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002007 {
2008 unsigned i, cnt;
2009
2010 for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) {
2011 if (pjsua_var.calls[i].acc_id == acc->index) {
2012 pjsua_call_hangup(i, 0, NULL, NULL);
2013 ++cnt;
2014 }
2015 }
2016
2017 if (cnt) {
2018 PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d "
2019 "after reregistration attempt failed",
2020 cnt, acc->index));
2021 }
2022 }
2023
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00002024 /* Cancel any re-registration timer */
2025 pjsua_cancel_timer(&acc->auto_rereg.timer);
2026
2027 /* Update re-registration flag */
2028 acc->auto_rereg.active = PJ_TRUE;
2029
2030 /* Set up timer for reregistration */
2031 acc->auto_rereg.timer.cb = &auto_rereg_timer_cb;
2032 acc->auto_rereg.timer.user_data = acc;
2033
2034 /* Reregistration attempt. The first attempt will be done immediately. */
2035 delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0;
2036 delay.msec = 0;
2037 pjsua_schedule_timer(&acc->auto_rereg.timer, &delay);
2038}
2039
2040
2041/* Internal function to perform auto-reregistration on transport
2042 * connection/disconnection events.
2043 */
2044void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
2045 pjsip_transport_state state,
2046 const pjsip_transport_state_info *info)
2047{
2048 unsigned i;
2049
2050 PJ_UNUSED_ARG(info);
2051
2052 /* Only care for transport disconnection events */
2053 if (state != PJSIP_TP_STATE_DISCONNECTED)
2054 return;
2055
2056 /* Shutdown this transport, to make sure that the transport manager
2057 * will create a new transport for reconnection.
2058 */
2059 pjsip_transport_shutdown(tp);
2060
2061 PJSUA_LOCK();
2062
2063 /* Enumerate accounts using this transport and perform actions
2064 * based on the transport state.
2065 */
2066 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2067 pjsua_acc *acc = &pjsua_var.acc[i];
2068
2069 /* Skip if this account is not valid OR auto re-registration
2070 * feature is disabled OR this transport is not used by this account.
2071 */
2072 if (!acc->valid || !acc->cfg.reg_retry_interval ||
2073 tp != acc->auto_rereg.reg_tp)
2074 {
2075 continue;
2076 }
2077
2078 /* Schedule reregistration for this account */
2079 schedule_reregistration(acc);
2080 }
2081
2082 PJSUA_UNLOCK();
2083}