blob: 4bfe98c1990abb3e269c569a4d9ae58eeadf264f [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
27/*
28 * Get number of current accounts.
29 */
30PJ_DEF(unsigned) pjsua_acc_get_count(void)
31{
32 return pjsua_var.acc_cnt;
33}
34
35
36/*
37 * Check if the specified account ID is valid.
38 */
39PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
40{
Benny Prijonoa1e69682007-05-11 15:14:34 +000041 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000042 pjsua_var.acc[acc_id].valid;
43}
44
45
46/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000047 * Set default account
48 */
49PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
50{
51 pjsua_var.default_acc = acc_id;
52 return PJ_SUCCESS;
53}
54
55
56/*
57 * Get default account.
58 */
59PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
60{
61 return pjsua_var.default_acc;
62}
63
64
65/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000066 * Copy account configuration.
67 */
Benny Prijonobddef2c2007-10-31 13:28:08 +000068PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
69 pjsua_acc_config *dst,
70 const pjsua_acc_config *src)
Benny Prijonoeebe9af2006-06-13 22:57:13 +000071{
72 unsigned i;
73
74 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
75
76 pj_strdup_with_null(pool, &dst->id, &src->id);
77 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000078 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Benny Prijonofe04fb52007-08-24 08:28:52 +000079 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000080
81 dst->proxy_cnt = src->proxy_cnt;
82 for (i=0; i<src->proxy_cnt; ++i)
83 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
84
85 dst->reg_timeout = src->reg_timeout;
86 dst->cred_count = src->cred_count;
87
88 for (i=0; i<src->cred_count; ++i) {
89 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
90 }
Benny Prijonobddef2c2007-10-31 13:28:08 +000091
92 dst->ka_interval = src->ka_interval;
93 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000094}
95
96
97/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000098 * Initialize a new account (after configuration is set).
99 */
100static pj_status_t initialize_acc(unsigned acc_id)
101{
102 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
103 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000104 pjsip_name_addr *name_addr;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000105 pjsip_sip_uri *sip_uri, *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000106 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000107 unsigned i;
108
109 /* Need to parse local_uri to get the elements: */
110
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000111 name_addr = (pjsip_name_addr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000112 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000113 acc_cfg->id.slen,
114 PJSIP_PARSE_URI_AS_NAMEADDR);
115 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000116 pjsua_perror(THIS_FILE, "Invalid local URI",
117 PJSIP_EINVALIDURI);
118 return PJSIP_EINVALIDURI;
119 }
120
121 /* Local URI MUST be a SIP or SIPS: */
122
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000123 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
124 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000125 {
126 pjsua_perror(THIS_FILE, "Invalid local URI",
127 PJSIP_EINVALIDSCHEME);
128 return PJSIP_EINVALIDSCHEME;
129 }
130
131
132 /* Get the SIP URI object: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000133 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000134
Benny Prijonob4a17c92006-07-10 14:40:21 +0000135
136 /* Parse registrar URI, if any */
137 if (acc_cfg->reg_uri.slen) {
138 pjsip_uri *reg_uri;
139
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000140 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000141 acc_cfg->reg_uri.slen, 0);
142 if (reg_uri == NULL) {
143 pjsua_perror(THIS_FILE, "Invalid registrar URI",
144 PJSIP_EINVALIDURI);
145 return PJSIP_EINVALIDURI;
146 }
147
148 /* Registrar URI MUST be a SIP or SIPS: */
149 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
150 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
151 {
152 pjsua_perror(THIS_FILE, "Invalid registar URI",
153 PJSIP_EINVALIDSCHEME);
154 return PJSIP_EINVALIDSCHEME;
155 }
156
157 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
158
159 } else {
160 sip_reg_uri = NULL;
161 }
162
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000163 /* Save the user and domain part. These will be used when finding an
164 * account for incoming requests.
165 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000166 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000167 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000168 acc->srv_domain = sip_uri->host;
169 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000170
Benny Prijonob4a17c92006-07-10 14:40:21 +0000171 if (sip_reg_uri) {
172 acc->srv_port = sip_reg_uri->port;
173 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174
175 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000176 //if (acc_cfg->contact.slen == 0) {
177 // acc_cfg->contact = acc_cfg->id;
178 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000179
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000180 /* Build account route-set from outbound proxies and route set from
181 * account configuration.
182 */
183 pj_list_init(&acc->route_set);
184
185 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
186 pj_str_t hname = { "Route", 5};
187 pjsip_route_hdr *r;
188 pj_str_t tmp;
189
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000190 pj_strdup_with_null(acc->pool, &tmp,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000192 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000193 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000194 if (r == NULL) {
195 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
196 PJSIP_EINVALIDURI);
197 return PJSIP_EINVALIDURI;
198 }
199 pj_list_push_back(&acc->route_set, r);
200 }
201
202 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
203 pj_str_t hname = { "Route", 5};
204 pjsip_route_hdr *r;
205 pj_str_t tmp;
206
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000207 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000208 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000209 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000210 if (r == NULL) {
211 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
212 PJ_EINVAL);
213 return PJ_EINVAL;
214 }
215 pj_list_push_back(&acc->route_set, r);
216 }
217
218
219 /* Concatenate credentials from account config and global config */
220 acc->cred_cnt = 0;
221 for (i=0; i<acc_cfg->cred_count; ++i) {
222 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
223 }
224 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
225 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
226 {
227 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
228 }
229
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000230 status = pjsua_pres_init_acc(acc_id);
231 if (status != PJ_SUCCESS)
232 return status;
233
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234 /* Mark account as valid */
235 pjsua_var.acc[acc_id].valid = PJ_TRUE;
236
Benny Prijono093d3022006-09-24 00:07:11 +0000237 /* Insert account ID into account ID array, sorted by priority */
238 for (i=0; i<pjsua_var.acc_cnt; ++i) {
239 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
240 pjsua_var.acc[acc_id].cfg.priority)
241 {
242 break;
243 }
244 }
245 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
246 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000247
248 return PJ_SUCCESS;
249}
250
251
252/*
253 * Add a new account to pjsua.
254 */
255PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
256 pj_bool_t is_default,
257 pjsua_acc_id *p_acc_id)
258{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000259 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000260 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000261 pj_status_t status;
262
263 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
264 PJ_ETOOMANY);
265
266 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000267 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000268
269 PJSUA_LOCK();
270
271 /* Find empty account id. */
272 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
273 if (pjsua_var.acc[id].valid == PJ_FALSE)
274 break;
275 }
276
277 /* Expect to find a slot */
278 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
279 {PJSUA_UNLOCK(); return PJ_EBUG;});
280
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000281 acc = &pjsua_var.acc[id];
282
283 /* Create pool for this account. */
284 if (acc->pool)
285 pj_pool_reset(acc->pool);
286 else
287 acc->pool = pjsua_pool_create("acc%p", 512, 256);
288
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000289 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000290 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000291
292 /* Normalize registration timeout */
293 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
294 pjsua_var.acc[id].cfg.reg_timeout == 0)
295 {
296 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
297 }
298
Benny Prijono91d06b62008-09-20 12:16:56 +0000299 /* Check the route URI's and force loose route if required */
300 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
301 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
302 if (status != PJ_SUCCESS)
303 return status;
304 }
305
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000306 status = initialize_acc(id);
307 if (status != PJ_SUCCESS) {
308 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000309 pj_pool_release(acc->pool);
310 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000311 PJSUA_UNLOCK();
312 return status;
313 }
314
315 if (is_default)
316 pjsua_var.default_acc = id;
317
318 if (p_acc_id)
319 *p_acc_id = id;
320
321 pjsua_var.acc_cnt++;
322
323 PJSUA_UNLOCK();
324
325 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
326 (int)cfg->id.slen, cfg->id.ptr, id));
327
328 /* If accounts has registration enabled, start registration */
329 if (pjsua_var.acc[id].cfg.reg_uri.slen)
330 pjsua_acc_set_registration(id, PJ_TRUE);
331
332
333 return PJ_SUCCESS;
334}
335
336
337/*
338 * Add local account
339 */
340PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
341 pj_bool_t is_default,
342 pjsua_acc_id *p_acc_id)
343{
344 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000345 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000346 const char *beginquote, *endquote;
347 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000348 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000349
Benny Prijonoe93e2872006-06-28 16:46:49 +0000350 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000351 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
352 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000353
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000354 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000355 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000356
357 pjsua_acc_config_default(&cfg);
358
Benny Prijono093d3022006-09-24 00:07:11 +0000359 /* Lower the priority of local account */
360 --cfg.priority;
361
Benny Prijonod0bd4982007-12-02 15:40:52 +0000362 /* Enclose IPv6 address in square brackets */
363 if (t->type & PJSIP_TRANSPORT_IPV6) {
364 beginquote = "[";
365 endquote = "]";
366 } else {
367 beginquote = endquote = "";
368 }
369
370 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000371 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000372 pj_ansi_snprintf(transport_param, sizeof(transport_param),
373 ";transport=%s",
374 pjsip_transport_get_type_name(t->type));
375 } else {
376 transport_param[0] = '\0';
377 }
378
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000379 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000380 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000381 "<sip:%s%.*s%s:%d%s>",
382 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000383 (int)t->local_name.host.slen,
384 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000385 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000386 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000387 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000388
389 cfg.id = pj_str(uri);
390
391 return pjsua_acc_add(&cfg, is_default, p_acc_id);
392}
393
394
395/*
Benny Prijono705e7842008-07-21 18:12:51 +0000396 * Set arbitrary data to be associated with the account.
397 */
398PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
399 void *user_data)
400{
401 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
402 PJ_EINVAL);
403 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
404
405 PJSUA_LOCK();
406
407 pjsua_var.acc[acc_id].cfg.user_data = user_data;
408
409 PJSUA_UNLOCK();
410
411 return PJ_SUCCESS;
412}
413
414
415/*
416 * Retrieve arbitrary data associated with the account.
417 */
418PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
419{
420 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
421 NULL);
422 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
423
424 return pjsua_var.acc[acc_id].cfg.user_data;
425}
426
427
428/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000429 * Delete account.
430 */
431PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
432{
Benny Prijono093d3022006-09-24 00:07:11 +0000433 unsigned i;
434
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000435 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
436 PJ_EINVAL);
437 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
438
439 PJSUA_LOCK();
440
441 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000442 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000443 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000444 if (pjsua_var.acc[acc_id].regc) {
445 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
446 }
Benny Prijono922933b2007-01-21 16:23:56 +0000447 pjsua_var.acc[acc_id].regc = NULL;
448 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000449
450 /* Delete server presence subscription */
451 pjsua_pres_delete_acc(acc_id);
452
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000453 /* Release account pool */
454 if (pjsua_var.acc[acc_id].pool) {
455 pj_pool_release(pjsua_var.acc[acc_id].pool);
456 pjsua_var.acc[acc_id].pool = NULL;
457 }
458
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000459 /* Invalidate */
460 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000461 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000462
Benny Prijono093d3022006-09-24 00:07:11 +0000463 /* Remove from array */
464 for (i=0; i<pjsua_var.acc_cnt; ++i) {
465 if (pjsua_var.acc_ids[i] == acc_id)
466 break;
467 }
468 if (i != pjsua_var.acc_cnt) {
469 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
470 pjsua_var.acc_cnt, i);
471 --pjsua_var.acc_cnt;
472 }
473
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000474 /* Leave the calls intact, as I don't think calls need to
475 * access account once it's created
476 */
477
Benny Prijonofb2b3652007-06-28 07:15:03 +0000478 /* Update default account */
479 if (pjsua_var.default_acc == acc_id)
480 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000481
482 PJSUA_UNLOCK();
483
484 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
485
486 return PJ_SUCCESS;
487}
488
489
490/*
491 * Modify account information.
492 */
493PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
494 const pjsua_acc_config *cfg)
495{
496 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000497 PJ_UNUSED_ARG(acc_id);
498 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000499 return PJ_EINVALIDOP;
500}
501
502
503/*
504 * Modify account's presence status to be advertised to remote/presence
505 * subscribers.
506 */
507PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
508 pj_bool_t is_online)
509{
510 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
511 PJ_EINVAL);
512 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
513
514 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000515 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
516 pjsua_pres_update_acc(acc_id, PJ_FALSE);
517 return PJ_SUCCESS;
518}
519
520
521/*
522 * Set online status with extended information
523 */
524PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
525 pj_bool_t is_online,
526 const pjrpid_element *pr)
527{
528 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
529 PJ_EINVAL);
530 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
531
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000532 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000533 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000534 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
535 PJSUA_UNLOCK();
536
Benny Prijono4461c7d2007-08-25 13:36:15 +0000537 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 return PJ_SUCCESS;
539}
540
Benny Prijono7f630432008-09-24 16:52:41 +0000541/* Check if IP is private IP address */
542static pj_bool_t is_private_ip(const pj_str_t *addr)
543{
544 const pj_str_t private_net[] =
545 {
546 { "10.", 3 },
547 { "127.", 4 },
548 { "172.16.", 7 },
549 { "192.168.", 8 }
550 };
551 unsigned i;
552
553 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
554 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
555 return PJ_TRUE;
556 }
557
558 return PJ_FALSE;
559}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000560
Benny Prijono15b02302007-09-27 14:07:07 +0000561/* Update NAT address from the REGISTER response */
562static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
563 struct pjsip_regc_cbparam *param)
564{
565 pjsip_transport *tp;
566 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000567 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000568 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000569 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000570 pjsip_via_hdr *via;
Benny Prijono84d24932009-06-04 15:51:39 +0000571 pj_sockaddr contact_addr;
572 pj_sockaddr recv_addr;
573 pj_status_t status;
574 pj_bool_t matched;
Benny Prijono7f630432008-09-24 16:52:41 +0000575 pj_str_t srv_ip;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000576 pjsip_contact_hdr *contact_hdr;
577 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijono15b02302007-09-27 14:07:07 +0000578
579 tp = param->rdata->tp_info.transport;
580
581 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000582 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000583 return PJ_FALSE;
584
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000585#if 0
586 // Always update
587 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000588
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000589 /* For UDP, only update if STUN is enabled (for now).
590 * For TCP/TLS, always check.
591 */
592 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
593 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
594 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
595 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
596 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000597 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000598 /* Yes we will check */
599 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000600 return PJ_FALSE;
601 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000602#endif
Benny Prijono15b02302007-09-27 14:07:07 +0000603
604 /* Get the received and rport info */
605 via = param->rdata->msg_info.via;
606 if (via->rport_param < 1) {
607 /* Remote doesn't support rport */
608 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +0000609 if (rport==0) {
610 pjsip_transport_type_e tp_type;
611 tp_type = (pjsip_transport_type_e) tp->key.type;
612 rport = pjsip_transport_get_default_port_for_type(tp_type);
613 }
Benny Prijono15b02302007-09-27 14:07:07 +0000614 } else
615 rport = via->rport_param;
616
617 if (via->recvd_param.slen != 0)
618 via_addr = &via->recvd_param;
619 else
620 via_addr = &via->sent_by.host;
621
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000622 /* Compare received and rport with the URI in our registration */
623 pool = pjsua_pool_create("tmp", 512, 512);
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000624 contact_hdr = (pjsip_contact_hdr*)
625 pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr,
626 acc->contact.slen, NULL);
627 pj_assert(contact_hdr != NULL);
628 uri = (pjsip_sip_uri*) contact_hdr->uri;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000629 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +0000630 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000631
Benny Prijono24a21852008-04-14 04:04:30 +0000632 if (uri->port == 0) {
633 pjsip_transport_type_e tp_type;
634 tp_type = (pjsip_transport_type_e) tp->key.type;
635 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
636 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000637
Benny Prijono84d24932009-06-04 15:51:39 +0000638 /* Convert IP address strings into sockaddr for comparison.
639 * (http://trac.pjsip.org/repos/ticket/863)
640 */
641 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host,
642 &contact_addr);
643 if (status == PJ_SUCCESS)
644 status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr,
645 &recv_addr);
646 if (status == PJ_SUCCESS) {
647 /* Compare the addresses as sockaddr according to the ticket above */
648 matched = (uri->port == rport &&
649 pj_sockaddr_cmp(&contact_addr, &recv_addr)==0);
650 } else {
651 /* Compare the addresses as string, as before */
652 matched = (uri->port == rport &&
653 pj_stricmp(&uri->host, via_addr)==0);
654 }
655
656 if (matched) {
Benny Prijono15b02302007-09-27 14:07:07 +0000657 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000658 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000659 return PJ_FALSE;
660 }
661
Benny Prijono7f630432008-09-24 16:52:41 +0000662 /* Get server IP */
663 srv_ip = pj_str(param->rdata->pkt_info.src_name);
664
Benny Prijono15b02302007-09-27 14:07:07 +0000665 /* At this point we've detected that the address as seen by registrar.
666 * has changed.
667 */
Benny Prijono7f630432008-09-24 16:52:41 +0000668
669 /* Do not switch if both Contact and server's IP address are
670 * public but response contains private IP. A NAT in the middle
Benny Prijono44e42e12009-06-03 08:37:24 +0000671 * might have messed up with the SIP packets. See:
672 * http://trac.pjsip.org/repos/ticket/643
Benny Prijono247921b2008-09-26 22:06:11 +0000673 *
674 * This exception can be disabled by setting allow_contact_rewrite
675 * to 2. In this case, the switch will always be done whenever there
676 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +0000677 */
Benny Prijono247921b2008-09-26 22:06:11 +0000678 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
679 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +0000680 {
681 /* Don't switch */
682 pj_pool_release(pool);
683 return PJ_FALSE;
684 }
685
Benny Prijono15b02302007-09-27 14:07:07 +0000686 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
687 "(%.*s:%d --> %.*s:%d). Updating registration..",
688 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000689 (int)uri->host.slen,
690 uri->host.ptr,
691 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000692 (int)via_addr->slen,
693 via_addr->ptr,
694 rport));
695
696 /* Unregister current contact */
697 pjsua_acc_set_registration(acc->index, PJ_FALSE);
698 if (acc->regc != NULL) {
699 pjsip_regc_destroy(acc->regc);
700 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000701 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +0000702 }
703
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000704 /* Update account's Contact header */
705 {
706 char *tmp;
Benny Prijono8972bf02009-04-05 18:30:45 +0000707 const char *beginquote, *endquote;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000708 int len;
709
Benny Prijono8972bf02009-04-05 18:30:45 +0000710 /* Enclose IPv6 address in square brackets */
711 if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
712 beginquote = "[";
713 endquote = "]";
714 } else {
715 beginquote = endquote = "";
716 }
717
Benny Prijono24a21852008-04-14 04:04:30 +0000718 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000719 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +0000720 "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s>%.*s",
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000721 (int)acc->user_part.slen,
722 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +0000723 (acc->user_part.slen? "@" : ""),
Benny Prijono8972bf02009-04-05 18:30:45 +0000724 beginquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000725 (int)via_addr->slen,
726 via_addr->ptr,
Benny Prijono8972bf02009-04-05 18:30:45 +0000727 endquote,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000728 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +0000729 tp->type_name,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +0000730 (int)acc->cfg.contact_uri_params.slen,
731 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +0000732 (int)acc->cfg.contact_params.slen,
733 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000734 if (len < 1) {
735 PJ_LOG(1,(THIS_FILE, "URI too long"));
736 pj_pool_release(pool);
737 return PJ_FALSE;
738 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +0000739 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000740 }
741
742 /* For UDP transport, if STUN is enabled then update the transport's
743 * published name as well.
744 */
745 if (tp->key.type==PJSIP_TRANSPORT_UDP &&
746 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
747 pjsua_var.ua_cfg.stun_host.slen != 0))
748 {
749 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
750 tp->local_name.port = rport;
751 }
Benny Prijono15b02302007-09-27 14:07:07 +0000752
753 /* Perform new registration */
754 pjsua_acc_set_registration(acc->index, PJ_TRUE);
755
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000756 pj_pool_release(pool);
757
Benny Prijono15b02302007-09-27 14:07:07 +0000758 return PJ_TRUE;
759}
760
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000761/* Check and update Service-Route header */
762void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
763{
764 pjsip_generic_string_hdr *hsr = NULL;
765 pjsip_route_hdr *hr, *h;
766 const pj_str_t HNAME = { "Service-Route", 13 };
767 const pj_str_t HROUTE = { "Route", 5 };
768 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000769 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000770
771 /* Find and parse Service-Route headers */
772 for (;;) {
773 char saved;
774 int parsed_len;
775
776 /* Find Service-Route header */
777 hsr = (pjsip_generic_string_hdr*)
778 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
779 if (!hsr)
780 break;
781
782 /* Parse as Route header since the syntax is similar. This may
783 * return more than one headers.
784 */
785 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
786 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
787 hr = (pjsip_route_hdr*)
788 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
789 hsr->hvalue.slen, &parsed_len);
790 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
791
792 if (hr == NULL) {
793 /* Error */
794 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
795 return;
796 }
797
798 /* Save each URI in the result */
799 h = hr;
800 do {
801 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
802 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
803 {
804 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
805 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
806 return;
807 }
808
809 uri[uri_cnt++] = h->name_addr.uri;
810 h = h->next;
811 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
812
813 if (h != hr) {
814 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
815 return;
816 }
817
818 /* Prepare to find next Service-Route header */
819 hsr = hsr->next;
820 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
821 break;
822 }
823
824 if (uri_cnt == 0)
825 return;
826
827 /*
828 * Update account's route set
829 */
830
831 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000832 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000833 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
834 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
835 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000836 i<rcnt;
837 ++i)
838 {
839 pjsip_route_hdr *prev = hr->prev;
840 pj_list_erase(hr);
841 hr = prev;
842 }
843 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000844
845 /* Then append the Service-Route URIs */
846 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000847 hr = pjsip_route_hdr_create(acc->pool);
848 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000849 pj_list_push_back(&acc->route_set, hr);
850 }
851
852 /* Done */
853
854 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
855 acc->index, uri_cnt));
856}
857
Benny Prijonobddef2c2007-10-31 13:28:08 +0000858
859/* Keep alive timer callback */
860static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
861{
862 pjsua_acc *acc;
863 pjsip_tpselector tp_sel;
864 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000865 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000866 pj_status_t status;
867
868 PJ_UNUSED_ARG(th);
869
870 PJSUA_LOCK();
871
872 te->id = PJ_FALSE;
873
874 acc = (pjsua_acc*) te->user_data;
875
876 /* Select the transport to send the packet */
877 pj_bzero(&tp_sel, sizeof(tp_sel));
878 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
879 tp_sel.u.transport = acc->ka_transport;
880
881 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000882 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000883 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000884 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000885
886 /* Send raw packet */
887 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
888 PJSIP_TRANSPORT_UDP, &tp_sel,
889 NULL, acc->cfg.ka_data.ptr,
890 acc->cfg.ka_data.slen,
891 &acc->ka_target, acc->ka_target_len,
892 NULL, NULL);
893
894 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
895 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
896 }
897
898 /* Reschedule next timer */
899 delay.sec = acc->cfg.ka_interval;
900 delay.msec = 0;
901 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
902 if (status == PJ_SUCCESS) {
903 te->id = PJ_TRUE;
904 } else {
905 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
906 }
907
908 PJSUA_UNLOCK();
909}
910
911
912/* Update keep-alive for the account */
913static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
914 struct pjsip_regc_cbparam *param)
915{
916 /* In all cases, stop keep-alive timer if it's running. */
917 if (acc->ka_timer.id) {
918 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
919 acc->ka_timer.id = PJ_FALSE;
920
921 pjsip_transport_dec_ref(acc->ka_transport);
922 acc->ka_transport = NULL;
923 }
924
925 if (start) {
926 pj_time_val delay;
927 pj_status_t status;
928
929 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000930 * - ka_interval is not zero in the account, and
931 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000932 *
933 * Previously we only enabled keep-alive when STUN is enabled, since
934 * we thought that keep-alive is only needed in Internet situation.
935 * But it has been discovered that Windows Firewall on WinXP also
936 * needs to be kept-alive, otherwise incoming packets will be dropped.
937 * So because of this, now keep-alive is always enabled for UDP,
938 * regardless of whether STUN is enabled or not.
939 *
940 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
941 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000942 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000943 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000944 acc->cfg.ka_interval == 0 ||
945 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
946 {
947 /* Keep alive is not necessary */
948 return;
949 }
950
951 /* Save transport and destination address. */
952 acc->ka_transport = param->rdata->tp_info.transport;
953 pjsip_transport_add_ref(acc->ka_transport);
954 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
955 param->rdata->pkt_info.src_addr_len);
956 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
957
958 /* Setup and start the timer */
959 acc->ka_timer.cb = &keep_alive_timer_cb;
960 acc->ka_timer.user_data = (void*)acc;
961
962 delay.sec = acc->cfg.ka_interval;
963 delay.msec = 0;
964 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
965 &delay);
966 if (status == PJ_SUCCESS) {
967 acc->ka_timer.id = PJ_TRUE;
968 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
969 "destination:%s:%d, interval:%ds",
970 acc->index,
971 param->rdata->pkt_info.src_name,
972 param->rdata->pkt_info.src_port,
973 acc->cfg.ka_interval));
974 } else {
975 acc->ka_timer.id = PJ_FALSE;
976 pjsip_transport_dec_ref(acc->ka_transport);
977 acc->ka_transport = NULL;
978 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
979 }
980 }
981}
982
983
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000984/*
985 * This callback is called by pjsip_regc when outgoing register
986 * request has completed.
987 */
988static void regc_cb(struct pjsip_regc_cbparam *param)
989{
990
Benny Prijonoa1e69682007-05-11 15:14:34 +0000991 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992
Benny Prijono15b02302007-09-27 14:07:07 +0000993 if (param->regc != acc->regc)
994 return;
995
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000996 PJSUA_LOCK();
997
998 /*
999 * Print registration status.
1000 */
1001 if (param->status!=PJ_SUCCESS) {
1002 pjsua_perror(THIS_FILE, "SIP registration error",
1003 param->status);
1004 pjsip_regc_destroy(acc->regc);
1005 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001006 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001007
Benny Prijonobddef2c2007-10-31 13:28:08 +00001008 /* Stop keep-alive timer if any. */
1009 update_keep_alive(acc, PJ_FALSE, NULL);
1010
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001011 } else if (param->code < 0 || param->code >= 300) {
1012 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
1013 param->code,
1014 (int)param->reason.slen, param->reason.ptr));
1015 pjsip_regc_destroy(acc->regc);
1016 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001017 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001018
Benny Prijonobddef2c2007-10-31 13:28:08 +00001019 /* Stop keep-alive timer if any. */
1020 update_keep_alive(acc, PJ_FALSE, NULL);
1021
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001022 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
1023
1024 if (param->expiration < 1) {
1025 pjsip_regc_destroy(acc->regc);
1026 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001027 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +00001028
1029 /* Stop keep-alive timer if any. */
1030 update_keep_alive(acc, PJ_FALSE, NULL);
1031
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001032 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
1033 pjsua_var.acc[acc->index].cfg.id.ptr));
1034 } else {
Benny Prijono15b02302007-09-27 14:07:07 +00001035 /* Check NAT bound address */
1036 if (acc_check_nat_addr(acc, param)) {
1037 /* Update address, don't notify application yet */
1038 PJSUA_UNLOCK();
1039 return;
1040 }
1041
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001042 /* Check and update Service-Route header */
1043 update_service_route(acc, param->rdata);
1044
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 PJ_LOG(3, (THIS_FILE,
1046 "%s: registration success, status=%d (%.*s), "
1047 "will re-register in %d seconds",
1048 pjsua_var.acc[acc->index].cfg.id.ptr,
1049 param->code,
1050 (int)param->reason.slen, param->reason.ptr,
1051 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001052
Benny Prijonobddef2c2007-10-31 13:28:08 +00001053 /* Start keep-alive timer if necessary. */
1054 update_keep_alive(acc, PJ_TRUE, param);
1055
Benny Prijono8b6834f2007-02-24 13:29:22 +00001056 /* Send initial PUBLISH if it is enabled */
1057 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1058 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001059 }
1060
1061 } else {
1062 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1063 }
1064
1065 acc->reg_last_err = param->status;
1066 acc->reg_last_code = param->code;
1067
1068 if (pjsua_var.ua_cfg.cb.on_reg_state)
1069 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1070
1071 PJSUA_UNLOCK();
1072}
1073
1074
1075/*
1076 * Initialize client registration.
1077 */
1078static pj_status_t pjsua_regc_init(int acc_id)
1079{
1080 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001081 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001082 pj_status_t status;
1083
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001084 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001085 acc = &pjsua_var.acc[acc_id];
1086
1087 if (acc->cfg.reg_uri.slen == 0) {
1088 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1089 return PJ_SUCCESS;
1090 }
1091
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001092 /* Destroy existing session, if any */
1093 if (acc->regc) {
1094 pjsip_regc_destroy(acc->regc);
1095 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001096 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001097 }
1098
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001099 /* initialize SIP registration if registrar is configured */
1100
1101 status = pjsip_regc_create( pjsua_var.endpt,
1102 acc, &regc_cb, &acc->regc);
1103
1104 if (status != PJ_SUCCESS) {
1105 pjsua_perror(THIS_FILE, "Unable to create client registration",
1106 status);
1107 return status;
1108 }
1109
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001110 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001111
1112 if (acc->contact.slen == 0) {
1113 pj_str_t tmp_contact;
1114
1115 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1116 acc_id, &acc->cfg.reg_uri);
1117 if (status != PJ_SUCCESS) {
1118 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1119 " for registration",
1120 status);
1121 pjsip_regc_destroy(acc->regc);
1122 pj_pool_release(pool);
1123 acc->regc = NULL;
1124 return status;
1125 }
1126
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001127 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001128 }
1129
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001130 status = pjsip_regc_init( acc->regc,
1131 &acc->cfg.reg_uri,
1132 &acc->cfg.id,
1133 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001134 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001135 acc->cfg.reg_timeout);
1136 if (status != PJ_SUCCESS) {
1137 pjsua_perror(THIS_FILE,
1138 "Client registration initialization error",
1139 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001140 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001141 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001142 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001143 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001144 return status;
1145 }
1146
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001147 /* If account is locked to specific transport, then set transport to
1148 * the client registration.
1149 */
1150 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1151 pjsip_tpselector tp_sel;
1152
1153 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1154 pjsip_regc_set_transport(acc->regc, &tp_sel);
1155 }
1156
1157
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001158 /* Set credentials
1159 */
1160 if (acc->cred_cnt) {
1161 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1162 }
1163
Benny Prijono48ab2b72007-11-08 09:24:30 +00001164 /* Set authentication preference */
1165 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1166
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001167 /* Set route-set
1168 */
1169 if (!pj_list_empty(&acc->route_set)) {
1170 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1171 }
1172
Benny Prijono8fc6de02006-11-11 21:25:55 +00001173 /* Add other request headers. */
1174 if (pjsua_var.ua_cfg.user_agent.slen) {
1175 pjsip_hdr hdr_list;
1176 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1177 pjsip_generic_string_hdr *h;
1178
Benny Prijono8fc6de02006-11-11 21:25:55 +00001179 pj_list_init(&hdr_list);
1180
1181 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1182 &pjsua_var.ua_cfg.user_agent);
1183 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1184
1185 pjsip_regc_add_headers(acc->regc, &hdr_list);
1186 }
1187
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001188 pj_pool_release(pool);
1189
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001190 return PJ_SUCCESS;
1191}
1192
1193
1194/*
1195 * Update registration or perform unregistration.
1196 */
1197PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1198 pj_bool_t renew)
1199{
1200 pj_status_t status = 0;
1201 pjsip_tx_data *tdata = 0;
1202
1203 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1204 PJ_EINVAL);
1205 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1206
1207 PJSUA_LOCK();
1208
1209 if (renew) {
1210 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001211 status = pjsua_regc_init(acc_id);
1212 if (status != PJ_SUCCESS) {
1213 pjsua_perror(THIS_FILE, "Unable to create registration",
1214 status);
1215 goto on_return;
1216 }
1217 }
1218 if (!pjsua_var.acc[acc_id].regc) {
1219 status = PJ_EINVALIDOP;
1220 goto on_return;
1221 }
1222
1223 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1224 &tdata);
1225
Benny Prijono28f673a2007-10-15 07:04:59 +00001226 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1227 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1228 pjsip_authorization_hdr *h;
1229 char *uri;
1230 int d;
1231
1232 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1233 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1234 uri, acc->cfg.reg_uri.slen+10);
1235 pj_assert(d > 0);
1236
1237 h = pjsip_authorization_hdr_create(tdata->pool);
1238 h->scheme = pj_str("Digest");
1239 h->credential.digest.username = acc->cred[0].username;
1240 h->credential.digest.realm = acc->srv_domain;
1241 h->credential.digest.uri = pj_str(uri);
1242 h->credential.digest.algorithm = pj_str("md5");
1243
1244 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1245 }
1246
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001247 } else {
1248 if (pjsua_var.acc[acc_id].regc == NULL) {
1249 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1250 status = PJ_EINVALIDOP;
1251 goto on_return;
1252 }
1253 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1254 }
1255
Benny Prijono56315612006-07-18 14:39:40 +00001256 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001257 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001258 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001259 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001260
1261 if (status != PJ_SUCCESS) {
1262 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1263 status);
1264 } else {
1265 PJ_LOG(3,(THIS_FILE, "%s sent",
1266 (renew? "Registration" : "Unregistration")));
1267 }
1268
1269on_return:
1270 PJSUA_UNLOCK();
1271 return status;
1272}
1273
1274
1275/*
1276 * Get account information.
1277 */
1278PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1279 pjsua_acc_info *info)
1280{
1281 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1282 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1283
1284 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001285 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001286
Benny Prijonoac623b32006-07-03 15:19:31 +00001287 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001288
1289 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1290 PJ_EINVAL);
1291 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1292
1293 PJSUA_LOCK();
1294
1295 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1296 PJSUA_UNLOCK();
1297 return PJ_EINVALIDOP;
1298 }
1299
1300 info->id = acc_id;
1301 info->is_default = (pjsua_var.default_acc == acc_id);
1302 info->acc_uri = acc_cfg->id;
1303 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1304 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001305 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1306 if (info->rpid.note.slen)
1307 info->online_status_text = info->rpid.note;
1308 else if (info->online_status)
1309 info->online_status_text = pj_str("Online");
1310 else
1311 info->online_status_text = pj_str("Offline");
1312
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001313 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001314 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001315 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1316 info->status_text = pj_str(info->buf_);
1317 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001318 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001319 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001320 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1321 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001322 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323 info->status_text = pj_str("not registered");
1324 }
1325 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001326 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001327 info->status_text = pj_str("In Progress");
1328 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001329 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001330 info->status_text = pj_str("does not register");
1331 }
1332
1333 if (acc->regc) {
1334 pjsip_regc_info regc_info;
1335 pjsip_regc_get_info(acc->regc, &regc_info);
1336 info->expires = regc_info.next_reg;
1337 } else {
1338 info->expires = -1;
1339 }
1340
1341 PJSUA_UNLOCK();
1342
1343 return PJ_SUCCESS;
1344
1345}
1346
1347
1348/*
1349 * Enum accounts all account ids.
1350 */
1351PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1352 unsigned *count )
1353{
1354 unsigned i, c;
1355
1356 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1357
1358 PJSUA_LOCK();
1359
1360 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1361 if (!pjsua_var.acc[i].valid)
1362 continue;
1363 ids[c] = i;
1364 ++c;
1365 }
1366
1367 *count = c;
1368
1369 PJSUA_UNLOCK();
1370
1371 return PJ_SUCCESS;
1372}
1373
1374
1375/*
1376 * Enum accounts info.
1377 */
1378PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1379 unsigned *count )
1380{
1381 unsigned i, c;
1382
1383 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1384
1385 PJSUA_LOCK();
1386
1387 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1388 if (!pjsua_var.acc[i].valid)
1389 continue;
1390
1391 pjsua_acc_get_info(i, &info[c]);
1392 ++c;
1393 }
1394
1395 *count = c;
1396
1397 PJSUA_UNLOCK();
1398
1399 return PJ_SUCCESS;
1400}
1401
1402
1403/*
1404 * This is an internal function to find the most appropriate account to
1405 * used to reach to the specified URL.
1406 */
1407PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1408{
1409 pj_str_t tmp;
1410 pjsip_uri *uri;
1411 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001412 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001413 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001414
1415 PJSUA_LOCK();
1416
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001417 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001418
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001419 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001420
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001421 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001422 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001423 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001424 PJSUA_UNLOCK();
1425 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001426 }
1427
1428 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1429 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1430 {
1431 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001432 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1433 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001434 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001435 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001436 break;
1437 }
1438
Benny Prijono093d3022006-09-24 00:07:11 +00001439 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001440 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001441 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001442 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001443 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001444 }
1445
1446 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001447 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001448 PJSUA_UNLOCK();
1449 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001450 }
1451
Benny Prijonoa1e69682007-05-11 15:14:34 +00001452 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001453
Benny Prijonob4a17c92006-07-10 14:40:21 +00001454 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001455 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1456 unsigned acc_id = pjsua_var.acc_ids[i];
1457 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1458 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1459 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001460 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001461 PJSUA_UNLOCK();
1462 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001463 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001464 }
1465
Benny Prijonob4a17c92006-07-10 14:40:21 +00001466 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001467 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1468 unsigned acc_id = pjsua_var.acc_ids[i];
1469 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1470 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001471 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001472 PJSUA_UNLOCK();
1473 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001474 }
1475 }
1476
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001477
Benny Prijono093d3022006-09-24 00:07:11 +00001478 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001479 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001480 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001481 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001482}
1483
1484
1485/*
1486 * This is an internal function to find the most appropriate account to be
1487 * used to handle incoming calls.
1488 */
1489PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1490{
1491 pjsip_uri *uri;
1492 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001493 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001494
Benny Prijono371cf0a2007-06-19 00:35:37 +00001495 /* Check that there's at least one account configured */
1496 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1497
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001498 uri = rdata->msg_info.to->uri;
1499
1500 /* Just return default account if To URI is not SIP: */
1501 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1502 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1503 {
1504 return pjsua_var.default_acc;
1505 }
1506
1507
1508 PJSUA_LOCK();
1509
1510 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1511
1512 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001513 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1514 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001515 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1516
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001517 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001518 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001519 {
1520 /* Match ! */
1521 PJSUA_UNLOCK();
1522 return acc_id;
1523 }
1524 }
1525
Benny Prijono093d3022006-09-24 00:07:11 +00001526 /* No matching account, try match domain part only. */
1527 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1528 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001529 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1530
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001531 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001532 /* Match ! */
1533 PJSUA_UNLOCK();
1534 return acc_id;
1535 }
1536 }
1537
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001538 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001539 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1540 unsigned acc_id = pjsua_var.acc_ids[i];
1541 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1542
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001543 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1544
1545 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1546 pjsip_transport_type_e type;
1547 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1548 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1549 type = PJSIP_TRANSPORT_UDP;
1550
1551 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1552 continue;
1553 }
1554
Benny Prijono093d3022006-09-24 00:07:11 +00001555 /* Match ! */
1556 PJSUA_UNLOCK();
1557 return acc_id;
1558 }
1559 }
1560
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001561 /* Still no match, use default account */
1562 PJSUA_UNLOCK();
1563 return pjsua_var.default_acc;
1564}
1565
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001566
Benny Prijonofff245c2007-04-02 11:44:47 +00001567/*
1568 * Create arbitrary requests for this account.
1569 */
1570PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1571 const pjsip_method *method,
1572 const pj_str_t *target,
1573 pjsip_tx_data **p_tdata)
1574{
1575 pjsip_tx_data *tdata;
1576 pjsua_acc *acc;
1577 pjsip_route_hdr *r;
1578 pj_status_t status;
1579
1580 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1581 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1582
1583 acc = &pjsua_var.acc[acc_id];
1584
1585 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1586 &acc->cfg.id, target,
1587 NULL, NULL, -1, NULL, &tdata);
1588 if (status != PJ_SUCCESS) {
1589 pjsua_perror(THIS_FILE, "Unable to create request", status);
1590 return status;
1591 }
1592
1593 /* Copy routeset */
1594 r = acc->route_set.next;
1595 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001596 pjsip_msg_add_hdr(tdata->msg,
1597 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001598 r = r->next;
1599 }
1600
1601 /* Done */
1602 *p_tdata = tdata;
1603 return PJ_SUCCESS;
1604}
1605
1606
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001607PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1608 pj_str_t *contact,
1609 pjsua_acc_id acc_id,
1610 const pj_str_t *suri)
1611{
1612 pjsua_acc *acc;
1613 pjsip_sip_uri *sip_uri;
1614 pj_status_t status;
1615 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1616 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001617 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001618 unsigned flag;
1619 int secure;
1620 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001621 const char *beginquote, *endquote;
1622 char transport_param[32];
1623
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001624
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001625 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001626 acc = &pjsua_var.acc[acc_id];
1627
Benny Prijonof75eceb2007-03-23 19:09:54 +00001628 /* If force_contact is configured, then use use it */
1629 if (acc->cfg.force_contact.slen) {
1630 *contact = acc->cfg.force_contact;
1631 return PJ_SUCCESS;
1632 }
1633
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001634 /* If route-set is configured for the account, then URI is the
1635 * first entry of the route-set.
1636 */
1637 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001638 sip_uri = (pjsip_sip_uri*)
1639 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001640 } else {
1641 pj_str_t tmp;
1642 pjsip_uri *uri;
1643
1644 pj_strdup_with_null(pool, &tmp, suri);
1645
1646 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1647 if (uri == NULL)
1648 return PJSIP_EINVALIDURI;
1649
1650 /* For non-SIP scheme, route set should be configured */
1651 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1652 return PJSIP_EINVALIDREQURI;
1653
Benny Prijono8c7a6172007-02-18 21:17:46 +00001654 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001655 }
1656
1657 /* Get transport type of the URI */
1658 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1659 tp_type = PJSIP_TRANSPORT_TLS;
1660 else if (sip_uri->transport_param.slen == 0) {
1661 tp_type = PJSIP_TRANSPORT_UDP;
1662 } else
1663 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1664
1665 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1666 return PJSIP_EUNSUPTRANSPORT;
1667
Benny Prijonod0bd4982007-12-02 15:40:52 +00001668 /* If destination URI specifies IPv6, then set transport type
1669 * to use IPv6 as well.
1670 */
Benny Prijono19b29372007-12-05 04:08:40 +00001671 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001672 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1673
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001674 flag = pjsip_transport_get_flag_from_type(tp_type);
1675 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1676
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001677 /* Init transport selector. */
1678 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1679
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001680 /* Get local address suitable to send request from */
1681 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001682 pool, tp_type, &tp_sel,
1683 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001684 if (status != PJ_SUCCESS)
1685 return status;
1686
Benny Prijonod0bd4982007-12-02 15:40:52 +00001687 /* Enclose IPv6 address in square brackets */
1688 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1689 beginquote = "[";
1690 endquote = "]";
1691 } else {
1692 beginquote = endquote = "";
1693 }
1694
1695 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00001696 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00001697 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1698 ";transport=%s",
1699 pjsip_transport_get_type_name(tp_type));
1700 } else {
1701 transport_param[0] = '\0';
1702 }
1703
1704
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001705 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001706 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001707 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001708 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001709 (int)acc->display.slen,
1710 acc->display.ptr,
1711 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001712 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001713 (int)acc->user_part.slen,
1714 acc->user_part.ptr,
1715 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001716 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001717 (int)local_addr.slen,
1718 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001719 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001720 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00001721 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001722 (int)acc->cfg.contact_uri_params.slen,
1723 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00001724 (int)acc->cfg.contact_params.slen,
1725 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001726
1727 return PJ_SUCCESS;
1728}
1729
1730
1731
1732PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1733 pj_str_t *contact,
1734 pjsua_acc_id acc_id,
1735 pjsip_rx_data *rdata )
1736{
1737 /*
1738 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1739 * If the request that initiated the dialog contained a SIPS URI
1740 * in the Request-URI or in the top Record-Route header field value,
1741 * if there was any, or the Contact header field if there was no
1742 * Record-Route header field, the Contact header field in the response
1743 * MUST be a SIPS URI.
1744 */
1745 pjsua_acc *acc;
1746 pjsip_sip_uri *sip_uri;
1747 pj_status_t status;
1748 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1749 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001750 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001751 unsigned flag;
1752 int secure;
1753 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001754 const char *beginquote, *endquote;
1755 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001756
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001757 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001758 acc = &pjsua_var.acc[acc_id];
1759
Benny Prijonof75eceb2007-03-23 19:09:54 +00001760 /* If force_contact is configured, then use use it */
1761 if (acc->cfg.force_contact.slen) {
1762 *contact = acc->cfg.force_contact;
1763 return PJ_SUCCESS;
1764 }
1765
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001766 /* If Record-Route is present, then URI is the top Record-Route. */
1767 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001768 sip_uri = (pjsip_sip_uri*)
1769 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001770 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00001771 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001772 pjsip_contact_hdr *h_contact;
1773 pjsip_uri *uri = NULL;
1774
Benny Prijonoa330d452008-08-05 20:14:39 +00001775 /* Otherwise URI is Contact URI.
1776 * Iterate the Contact URI until we find sip: or sips: scheme.
1777 */
1778 do {
1779 h_contact = (pjsip_contact_hdr*)
1780 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
1781 pos);
1782 if (h_contact) {
1783 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
1784 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1785 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1786 {
1787 pos = (pjsip_hdr*)h_contact->next;
1788 if (pos == &rdata->msg_info.msg->hdr)
1789 h_contact = NULL;
1790 } else {
1791 break;
1792 }
1793 }
1794 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001795
1796
1797 /* Or if Contact URI is not present, take the remote URI from
1798 * the From URI.
1799 */
1800 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001801 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001802
1803
1804 /* Can only do sip/sips scheme at present. */
1805 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1806 return PJSIP_EINVALIDREQURI;
1807
Benny Prijono8c7a6172007-02-18 21:17:46 +00001808 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001809 }
1810
1811 /* Get transport type of the URI */
1812 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1813 tp_type = PJSIP_TRANSPORT_TLS;
1814 else if (sip_uri->transport_param.slen == 0) {
1815 tp_type = PJSIP_TRANSPORT_UDP;
1816 } else
1817 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001818
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001819 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1820 return PJSIP_EUNSUPTRANSPORT;
1821
Benny Prijonod0bd4982007-12-02 15:40:52 +00001822 /* If destination URI specifies IPv6, then set transport type
1823 * to use IPv6 as well.
1824 */
1825 if (pj_strchr(&sip_uri->host, ':'))
1826 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1827
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001828 flag = pjsip_transport_get_flag_from_type(tp_type);
1829 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1830
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001831 /* Init transport selector. */
1832 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1833
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001834 /* Get local address suitable to send request from */
1835 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001836 pool, tp_type, &tp_sel,
1837 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001838 if (status != PJ_SUCCESS)
1839 return status;
1840
Benny Prijonod0bd4982007-12-02 15:40:52 +00001841 /* Enclose IPv6 address in square brackets */
1842 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1843 beginquote = "[";
1844 endquote = "]";
1845 } else {
1846 beginquote = endquote = "";
1847 }
1848
1849 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00001850 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00001851 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1852 ";transport=%s",
1853 pjsip_transport_get_type_name(tp_type));
1854 } else {
1855 transport_param[0] = '\0';
1856 }
1857
1858
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001859 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001860 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001861 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001862 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001863 (int)acc->display.slen,
1864 acc->display.ptr,
1865 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001866 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001867 (int)acc->user_part.slen,
1868 acc->user_part.ptr,
1869 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001870 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001871 (int)local_addr.slen,
1872 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001873 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001874 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00001875 transport_param,
Nanang Izzuddine2c7e852009-08-04 14:36:17 +00001876 (int)acc->cfg.contact_uri_params.slen,
1877 acc->cfg.contact_uri_params.ptr,
Benny Prijono30fe4852008-12-10 16:54:16 +00001878 (int)acc->cfg.contact_params.slen,
1879 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001880
1881 return PJ_SUCCESS;
1882}
1883
1884
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001885PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1886 pjsua_transport_id tp_id)
1887{
1888 pjsua_acc *acc;
1889
1890 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1891 acc = &pjsua_var.acc[acc_id];
1892
Benny Prijonoa1e69682007-05-11 15:14:34 +00001893 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001894 PJ_EINVAL);
1895
1896 acc->cfg.transport_id = tp_id;
1897
1898 return PJ_SUCCESS;
1899}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001900