blob: 4262d4101fea5ae668af2cccb71aba1f87543500 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20#include <pjsua-lib/pjsua_internal.h>
21
22
23#define THIS_FILE "pjsua_acc.c"
24
25
26/*
27 * Get number of current accounts.
28 */
29PJ_DEF(unsigned) pjsua_acc_get_count(void)
30{
31 return pjsua_var.acc_cnt;
32}
33
34
35/*
36 * Check if the specified account ID is valid.
37 */
38PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
39{
Benny Prijonoa1e69682007-05-11 15:14:34 +000040 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041 pjsua_var.acc[acc_id].valid;
42}
43
44
45/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000046 * Set default account
47 */
48PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
49{
50 pjsua_var.default_acc = acc_id;
51 return PJ_SUCCESS;
52}
53
54
55/*
56 * Get default account.
57 */
58PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
59{
60 return pjsua_var.default_acc;
61}
62
63
64/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000065 * Copy account configuration.
66 */
Benny Prijonobddef2c2007-10-31 13:28:08 +000067PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool,
68 pjsua_acc_config *dst,
69 const pjsua_acc_config *src)
Benny Prijonoeebe9af2006-06-13 22:57:13 +000070{
71 unsigned i;
72
73 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
74
75 pj_strdup_with_null(pool, &dst->id, &src->id);
76 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000077 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Benny Prijonofe04fb52007-08-24 08:28:52 +000078 pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000079
80 dst->proxy_cnt = src->proxy_cnt;
81 for (i=0; i<src->proxy_cnt; ++i)
82 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
83
84 dst->reg_timeout = src->reg_timeout;
85 dst->cred_count = src->cred_count;
86
87 for (i=0; i<src->cred_count; ++i) {
88 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
89 }
Benny Prijonobddef2c2007-10-31 13:28:08 +000090
91 dst->ka_interval = src->ka_interval;
92 pj_strdup(pool, &dst->ka_data, &src->ka_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000093}
94
95
96/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000097 * Initialize a new account (after configuration is set).
98 */
99static pj_status_t initialize_acc(unsigned acc_id)
100{
101 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
102 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000103 pjsip_name_addr *name_addr;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000104 pjsip_sip_uri *sip_uri, *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000105 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000106 unsigned i;
107
108 /* Need to parse local_uri to get the elements: */
109
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000110 name_addr = (pjsip_name_addr*)
111 pjsip_parse_uri(pjsua_var.pool, acc_cfg->id.ptr,
112 acc_cfg->id.slen,
113 PJSIP_PARSE_URI_AS_NAMEADDR);
114 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000115 pjsua_perror(THIS_FILE, "Invalid local URI",
116 PJSIP_EINVALIDURI);
117 return PJSIP_EINVALIDURI;
118 }
119
120 /* Local URI MUST be a SIP or SIPS: */
121
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000122 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
123 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000124 {
125 pjsua_perror(THIS_FILE, "Invalid local URI",
126 PJSIP_EINVALIDSCHEME);
127 return PJSIP_EINVALIDSCHEME;
128 }
129
130
131 /* Get the SIP URI object: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000132 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000133
Benny Prijonob4a17c92006-07-10 14:40:21 +0000134
135 /* Parse registrar URI, if any */
136 if (acc_cfg->reg_uri.slen) {
137 pjsip_uri *reg_uri;
138
139 reg_uri = pjsip_parse_uri(pjsua_var.pool, acc_cfg->reg_uri.ptr,
140 acc_cfg->reg_uri.slen, 0);
141 if (reg_uri == NULL) {
142 pjsua_perror(THIS_FILE, "Invalid registrar URI",
143 PJSIP_EINVALIDURI);
144 return PJSIP_EINVALIDURI;
145 }
146
147 /* Registrar URI MUST be a SIP or SIPS: */
148 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
149 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
150 {
151 pjsua_perror(THIS_FILE, "Invalid registar URI",
152 PJSIP_EINVALIDSCHEME);
153 return PJSIP_EINVALIDSCHEME;
154 }
155
156 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
157
158 } else {
159 sip_reg_uri = NULL;
160 }
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
190 pj_strdup_with_null(pjsua_var.pool, &tmp,
191 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000192 r = (pjsip_route_hdr*)
193 pjsip_parse_hdr(pjsua_var.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
207 pj_strdup_with_null(pjsua_var.pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000208 r = (pjsip_route_hdr*)
209 pjsip_parse_hdr(pjsua_var.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{
259 unsigned id;
260 pj_status_t status;
261
262 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
263 PJ_ETOOMANY);
264
265 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000266 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000267
268 PJSUA_LOCK();
269
270 /* Find empty account id. */
271 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
272 if (pjsua_var.acc[id].valid == PJ_FALSE)
273 break;
274 }
275
276 /* Expect to find a slot */
277 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
278 {PJSUA_UNLOCK(); return PJ_EBUG;});
279
280 /* Copy config */
Benny Prijonobddef2c2007-10-31 13:28:08 +0000281 pjsua_acc_config_dup(pjsua_var.pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000282
283 /* Normalize registration timeout */
284 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
285 pjsua_var.acc[id].cfg.reg_timeout == 0)
286 {
287 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
288 }
289
290 status = initialize_acc(id);
291 if (status != PJ_SUCCESS) {
292 pjsua_perror(THIS_FILE, "Error adding account", status);
293 PJSUA_UNLOCK();
294 return status;
295 }
296
297 if (is_default)
298 pjsua_var.default_acc = id;
299
300 if (p_acc_id)
301 *p_acc_id = id;
302
303 pjsua_var.acc_cnt++;
304
305 PJSUA_UNLOCK();
306
307 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
308 (int)cfg->id.slen, cfg->id.ptr, id));
309
310 /* If accounts has registration enabled, start registration */
311 if (pjsua_var.acc[id].cfg.reg_uri.slen)
312 pjsua_acc_set_registration(id, PJ_TRUE);
313
314
315 return PJ_SUCCESS;
316}
317
318
319/*
320 * Add local account
321 */
322PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
323 pj_bool_t is_default,
324 pjsua_acc_id *p_acc_id)
325{
326 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000327 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000328 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329
Benny Prijonoe93e2872006-06-28 16:46:49 +0000330 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000331 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
332 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000333
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000334 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000335 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000336
337 pjsua_acc_config_default(&cfg);
338
Benny Prijono093d3022006-09-24 00:07:11 +0000339 /* Lower the priority of local account */
340 --cfg.priority;
341
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000342 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000343 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
344 "<sip:%.*s:%d;transport=%s>",
345 (int)t->local_name.host.slen,
346 t->local_name.host.ptr,
347 t->local_name.port,
348 pjsip_transport_get_type_name(t->type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000349
350 cfg.id = pj_str(uri);
351
352 return pjsua_acc_add(&cfg, is_default, p_acc_id);
353}
354
355
356/*
357 * Delete account.
358 */
359PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
360{
Benny Prijono093d3022006-09-24 00:07:11 +0000361 unsigned i;
362
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
364 PJ_EINVAL);
365 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
366
367 PJSUA_LOCK();
368
369 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000370 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000371 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000372 if (pjsua_var.acc[acc_id].regc) {
373 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
374 }
Benny Prijono922933b2007-01-21 16:23:56 +0000375 pjsua_var.acc[acc_id].regc = NULL;
376 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377
378 /* Delete server presence subscription */
379 pjsua_pres_delete_acc(acc_id);
380
381 /* Invalidate */
382 pjsua_var.acc[acc_id].valid = PJ_FALSE;
383
Benny Prijono093d3022006-09-24 00:07:11 +0000384 /* Remove from array */
385 for (i=0; i<pjsua_var.acc_cnt; ++i) {
386 if (pjsua_var.acc_ids[i] == acc_id)
387 break;
388 }
389 if (i != pjsua_var.acc_cnt) {
390 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
391 pjsua_var.acc_cnt, i);
392 --pjsua_var.acc_cnt;
393 }
394
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000395 /* Leave the calls intact, as I don't think calls need to
396 * access account once it's created
397 */
398
Benny Prijonofb2b3652007-06-28 07:15:03 +0000399 /* Update default account */
400 if (pjsua_var.default_acc == acc_id)
401 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000402
403 PJSUA_UNLOCK();
404
405 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
406
407 return PJ_SUCCESS;
408}
409
410
411/*
412 * Modify account information.
413 */
414PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
415 const pjsua_acc_config *cfg)
416{
417 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000418 PJ_UNUSED_ARG(acc_id);
419 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000420 return PJ_EINVALIDOP;
421}
422
423
424/*
425 * Modify account's presence status to be advertised to remote/presence
426 * subscribers.
427 */
428PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
429 pj_bool_t is_online)
430{
431 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
432 PJ_EINVAL);
433 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
434
435 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000436 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
437 pjsua_pres_update_acc(acc_id, PJ_FALSE);
438 return PJ_SUCCESS;
439}
440
441
442/*
443 * Set online status with extended information
444 */
445PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
446 pj_bool_t is_online,
447 const pjrpid_element *pr)
448{
449 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
450 PJ_EINVAL);
451 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
452
453 pjsua_var.acc[acc_id].online_status = is_online;
454 pjrpid_element_dup(pjsua_var.pool, &pjsua_var.acc[acc_id].rpid, pr);
455 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000456 return PJ_SUCCESS;
457}
458
459
Benny Prijono15b02302007-09-27 14:07:07 +0000460/* Update NAT address from the REGISTER response */
461static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
462 struct pjsip_regc_cbparam *param)
463{
464 pjsip_transport *tp;
465 const pj_str_t *via_addr;
466 int rport;
467 pjsip_via_hdr *via;
468
469 tp = param->rdata->tp_info.transport;
470
471 /* Only update if account is configured to auto-update */
472 if (acc->cfg.auto_update_nat == PJ_FALSE)
473 return PJ_FALSE;
474
475 /* Only update if registration uses UDP transport */
476 if (tp->key.type != PJSIP_TRANSPORT_UDP)
477 return PJ_FALSE;
478
479 /* Only update if STUN is enabled (for now) */
480 if (pjsua_var.ua_cfg.stun_domain.slen == 0 &&
481 pjsua_var.ua_cfg.stun_host.slen == 0)
482 {
483 return PJ_FALSE;
484 }
485
486 /* Get the received and rport info */
487 via = param->rdata->msg_info.via;
488 if (via->rport_param < 1) {
489 /* Remote doesn't support rport */
490 rport = via->sent_by.port;
491 } else
492 rport = via->rport_param;
493
494 if (via->recvd_param.slen != 0)
495 via_addr = &via->recvd_param;
496 else
497 via_addr = &via->sent_by.host;
498
499 /* Compare received and rport with transport published address */
500 if (tp->local_name.port == rport &&
501 pj_stricmp(&tp->local_name.host, via_addr)==0)
502 {
503 /* Address doesn't change */
504 return PJ_FALSE;
505 }
506
507 /* At this point we've detected that the address as seen by registrar.
508 * has changed.
509 */
510 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
511 "(%.*s:%d --> %.*s:%d). Updating registration..",
512 acc->index,
513 (int)tp->local_name.host.slen,
514 tp->local_name.host.ptr,
515 tp->local_name.port,
516 (int)via_addr->slen,
517 via_addr->ptr,
518 rport));
519
520 /* Unregister current contact */
521 pjsua_acc_set_registration(acc->index, PJ_FALSE);
522 if (acc->regc != NULL) {
523 pjsip_regc_destroy(acc->regc);
524 acc->regc = NULL;
525 }
526
527 /* Update transport address */
528 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
529 tp->local_name.port = rport;
530
531 /* Perform new registration */
532 pjsua_acc_set_registration(acc->index, PJ_TRUE);
533
534 return PJ_TRUE;
535}
536
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000537/* Check and update Service-Route header */
538void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
539{
540 pjsip_generic_string_hdr *hsr = NULL;
541 pjsip_route_hdr *hr, *h;
542 const pj_str_t HNAME = { "Service-Route", 13 };
543 const pj_str_t HROUTE = { "Route", 5 };
544 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000545 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000546
Benny Prijono2a67ea42007-10-25 02:51:33 +0000547 /* Skip processing is enable_service_route is not set */
548 if (!acc->cfg.enable_service_route)
549 return;
550
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000551 /* Find and parse Service-Route headers */
552 for (;;) {
553 char saved;
554 int parsed_len;
555
556 /* Find Service-Route header */
557 hsr = (pjsip_generic_string_hdr*)
558 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
559 if (!hsr)
560 break;
561
562 /* Parse as Route header since the syntax is similar. This may
563 * return more than one headers.
564 */
565 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
566 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
567 hr = (pjsip_route_hdr*)
568 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
569 hsr->hvalue.slen, &parsed_len);
570 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
571
572 if (hr == NULL) {
573 /* Error */
574 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
575 return;
576 }
577
578 /* Save each URI in the result */
579 h = hr;
580 do {
581 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
582 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
583 {
584 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
585 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
586 return;
587 }
588
589 uri[uri_cnt++] = h->name_addr.uri;
590 h = h->next;
591 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
592
593 if (h != hr) {
594 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
595 return;
596 }
597
598 /* Prepare to find next Service-Route header */
599 hsr = hsr->next;
600 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
601 break;
602 }
603
604 if (uri_cnt == 0)
605 return;
606
607 /*
608 * Update account's route set
609 */
610
611 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000612 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000613 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
614 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
615 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000616 i<rcnt;
617 ++i)
618 {
619 pjsip_route_hdr *prev = hr->prev;
620 pj_list_erase(hr);
621 hr = prev;
622 }
623 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000624
625 /* Then append the Service-Route URIs */
626 for (i=0; i<uri_cnt; ++i) {
627 hr = pjsip_route_hdr_create(pjsua_var.pool);
628 hr->name_addr.uri = pjsip_uri_clone(pjsua_var.pool, uri[i]);
629 pj_list_push_back(&acc->route_set, hr);
630 }
631
632 /* Done */
633
634 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
635 acc->index, uri_cnt));
636}
637
Benny Prijonobddef2c2007-10-31 13:28:08 +0000638
639/* Keep alive timer callback */
640static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
641{
642 pjsua_acc *acc;
643 pjsip_tpselector tp_sel;
644 pj_time_val delay;
645 pj_status_t status;
646
647 PJ_UNUSED_ARG(th);
648
649 PJSUA_LOCK();
650
651 te->id = PJ_FALSE;
652
653 acc = (pjsua_acc*) te->user_data;
654
655 /* Select the transport to send the packet */
656 pj_bzero(&tp_sel, sizeof(tp_sel));
657 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
658 tp_sel.u.transport = acc->ka_transport;
659
660 PJ_LOG(5,(THIS_FILE,
661 "Sending %d bytes keep-alive packet for acc %d to %s:%d",
662 acc->cfg.ka_data.slen, acc->index,
663 pj_inet_ntoa(acc->ka_target.ipv4.sin_addr),
664 pj_ntohs(acc->ka_target.ipv4.sin_port)));
665
666 /* Send raw packet */
667 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
668 PJSIP_TRANSPORT_UDP, &tp_sel,
669 NULL, acc->cfg.ka_data.ptr,
670 acc->cfg.ka_data.slen,
671 &acc->ka_target, acc->ka_target_len,
672 NULL, NULL);
673
674 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
675 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
676 }
677
678 /* Reschedule next timer */
679 delay.sec = acc->cfg.ka_interval;
680 delay.msec = 0;
681 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
682 if (status == PJ_SUCCESS) {
683 te->id = PJ_TRUE;
684 } else {
685 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
686 }
687
688 PJSUA_UNLOCK();
689}
690
691
692/* Update keep-alive for the account */
693static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
694 struct pjsip_regc_cbparam *param)
695{
696 /* In all cases, stop keep-alive timer if it's running. */
697 if (acc->ka_timer.id) {
698 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
699 acc->ka_timer.id = PJ_FALSE;
700
701 pjsip_transport_dec_ref(acc->ka_transport);
702 acc->ka_transport = NULL;
703 }
704
705 if (start) {
706 pj_time_val delay;
707 pj_status_t status;
708
709 /* Only do keep-alive if:
710 * - STUN is enabled in global config, and
711 * - ka_interval is not zero in the account, and
712 * - transport is UDP.
713 */
714 if (pjsua_var.stun_srv.ipv4.sin_family == 0 ||
715 acc->cfg.ka_interval == 0 ||
716 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
717 {
718 /* Keep alive is not necessary */
719 return;
720 }
721
722 /* Save transport and destination address. */
723 acc->ka_transport = param->rdata->tp_info.transport;
724 pjsip_transport_add_ref(acc->ka_transport);
725 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
726 param->rdata->pkt_info.src_addr_len);
727 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
728
729 /* Setup and start the timer */
730 acc->ka_timer.cb = &keep_alive_timer_cb;
731 acc->ka_timer.user_data = (void*)acc;
732
733 delay.sec = acc->cfg.ka_interval;
734 delay.msec = 0;
735 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
736 &delay);
737 if (status == PJ_SUCCESS) {
738 acc->ka_timer.id = PJ_TRUE;
739 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
740 "destination:%s:%d, interval:%ds",
741 acc->index,
742 param->rdata->pkt_info.src_name,
743 param->rdata->pkt_info.src_port,
744 acc->cfg.ka_interval));
745 } else {
746 acc->ka_timer.id = PJ_FALSE;
747 pjsip_transport_dec_ref(acc->ka_transport);
748 acc->ka_transport = NULL;
749 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
750 }
751 }
752}
753
754
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000755/*
756 * This callback is called by pjsip_regc when outgoing register
757 * request has completed.
758 */
759static void regc_cb(struct pjsip_regc_cbparam *param)
760{
761
Benny Prijonoa1e69682007-05-11 15:14:34 +0000762 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000763
Benny Prijono15b02302007-09-27 14:07:07 +0000764 if (param->regc != acc->regc)
765 return;
766
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000767 PJSUA_LOCK();
768
769 /*
770 * Print registration status.
771 */
772 if (param->status!=PJ_SUCCESS) {
773 pjsua_perror(THIS_FILE, "SIP registration error",
774 param->status);
775 pjsip_regc_destroy(acc->regc);
776 acc->regc = NULL;
777
Benny Prijonobddef2c2007-10-31 13:28:08 +0000778 /* Stop keep-alive timer if any. */
779 update_keep_alive(acc, PJ_FALSE, NULL);
780
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000781 } else if (param->code < 0 || param->code >= 300) {
782 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
783 param->code,
784 (int)param->reason.slen, param->reason.ptr));
785 pjsip_regc_destroy(acc->regc);
786 acc->regc = NULL;
787
Benny Prijonobddef2c2007-10-31 13:28:08 +0000788 /* Stop keep-alive timer if any. */
789 update_keep_alive(acc, PJ_FALSE, NULL);
790
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000791 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
792
793 if (param->expiration < 1) {
794 pjsip_regc_destroy(acc->regc);
795 acc->regc = NULL;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000796
797 /* Stop keep-alive timer if any. */
798 update_keep_alive(acc, PJ_FALSE, NULL);
799
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000800 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
801 pjsua_var.acc[acc->index].cfg.id.ptr));
802 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000803 /* Check NAT bound address */
804 if (acc_check_nat_addr(acc, param)) {
805 /* Update address, don't notify application yet */
806 PJSUA_UNLOCK();
807 return;
808 }
809
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000810 /* Check and update Service-Route header */
811 update_service_route(acc, param->rdata);
812
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000813 PJ_LOG(3, (THIS_FILE,
814 "%s: registration success, status=%d (%.*s), "
815 "will re-register in %d seconds",
816 pjsua_var.acc[acc->index].cfg.id.ptr,
817 param->code,
818 (int)param->reason.slen, param->reason.ptr,
819 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000820
Benny Prijonobddef2c2007-10-31 13:28:08 +0000821 /* Start keep-alive timer if necessary. */
822 update_keep_alive(acc, PJ_TRUE, param);
823
Benny Prijono8b6834f2007-02-24 13:29:22 +0000824 /* Send initial PUBLISH if it is enabled */
825 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
826 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000827 }
828
829 } else {
830 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
831 }
832
833 acc->reg_last_err = param->status;
834 acc->reg_last_code = param->code;
835
836 if (pjsua_var.ua_cfg.cb.on_reg_state)
837 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
838
839 PJSUA_UNLOCK();
840}
841
842
843/*
844 * Initialize client registration.
845 */
846static pj_status_t pjsua_regc_init(int acc_id)
847{
848 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000849 pj_str_t contact;
Benny Prijonodfb7d482006-10-18 20:35:14 +0000850 char contact_buf[1024];
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000851 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000852 pj_status_t status;
853
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000854 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000855 acc = &pjsua_var.acc[acc_id];
856
857 if (acc->cfg.reg_uri.slen == 0) {
858 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
859 return PJ_SUCCESS;
860 }
861
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000862 /* Destroy existing session, if any */
863 if (acc->regc) {
864 pjsip_regc_destroy(acc->regc);
865 acc->regc = NULL;
866 }
867
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000868 /* initialize SIP registration if registrar is configured */
869
870 status = pjsip_regc_create( pjsua_var.endpt,
871 acc, &regc_cb, &acc->regc);
872
873 if (status != PJ_SUCCESS) {
874 pjsua_perror(THIS_FILE, "Unable to create client registration",
875 status);
876 return status;
877 }
878
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000879 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
880 status = pjsua_acc_create_uac_contact( pool, &contact,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000881 acc_id, &acc->cfg.reg_uri);
882 if (status != PJ_SUCCESS) {
883 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
884 " for registration",
885 status);
886 return status;
887 }
888
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 status = pjsip_regc_init( acc->regc,
890 &acc->cfg.reg_uri,
891 &acc->cfg.id,
892 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000893 1, &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000894 acc->cfg.reg_timeout);
895 if (status != PJ_SUCCESS) {
896 pjsua_perror(THIS_FILE,
897 "Client registration initialization error",
898 status);
899 return status;
900 }
901
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000902 /* If account is locked to specific transport, then set transport to
903 * the client registration.
904 */
905 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
906 pjsip_tpselector tp_sel;
907
908 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
909 pjsip_regc_set_transport(acc->regc, &tp_sel);
910 }
911
912
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000913 /* Set credentials
914 */
915 if (acc->cred_cnt) {
916 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
917 }
918
919 /* Set route-set
920 */
921 if (!pj_list_empty(&acc->route_set)) {
922 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
923 }
924
Benny Prijono8fc6de02006-11-11 21:25:55 +0000925 /* Add other request headers. */
926 if (pjsua_var.ua_cfg.user_agent.slen) {
927 pjsip_hdr hdr_list;
928 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
929 pjsip_generic_string_hdr *h;
930
931 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
932 pj_list_init(&hdr_list);
933
934 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
935 &pjsua_var.ua_cfg.user_agent);
936 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
937
938 pjsip_regc_add_headers(acc->regc, &hdr_list);
939 }
940
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000941 return PJ_SUCCESS;
942}
943
944
945/*
946 * Update registration or perform unregistration.
947 */
948PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
949 pj_bool_t renew)
950{
951 pj_status_t status = 0;
952 pjsip_tx_data *tdata = 0;
953
954 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
955 PJ_EINVAL);
956 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
957
958 PJSUA_LOCK();
959
960 if (renew) {
961 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000962 status = pjsua_regc_init(acc_id);
963 if (status != PJ_SUCCESS) {
964 pjsua_perror(THIS_FILE, "Unable to create registration",
965 status);
966 goto on_return;
967 }
968 }
969 if (!pjsua_var.acc[acc_id].regc) {
970 status = PJ_EINVALIDOP;
971 goto on_return;
972 }
973
974 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
975 &tdata);
976
Benny Prijono28f673a2007-10-15 07:04:59 +0000977 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
978 pjsua_acc *acc = &pjsua_var.acc[acc_id];
979 pjsip_authorization_hdr *h;
980 char *uri;
981 int d;
982
983 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
984 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
985 uri, acc->cfg.reg_uri.slen+10);
986 pj_assert(d > 0);
987
988 h = pjsip_authorization_hdr_create(tdata->pool);
989 h->scheme = pj_str("Digest");
990 h->credential.digest.username = acc->cred[0].username;
991 h->credential.digest.realm = acc->srv_domain;
992 h->credential.digest.uri = pj_str(uri);
993 h->credential.digest.algorithm = pj_str("md5");
994
995 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
996 }
997
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000998 } else {
999 if (pjsua_var.acc[acc_id].regc == NULL) {
1000 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1001 status = PJ_EINVALIDOP;
1002 goto on_return;
1003 }
1004 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1005 }
1006
Benny Prijono56315612006-07-18 14:39:40 +00001007 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001008 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001009 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001010 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001011
1012 if (status != PJ_SUCCESS) {
1013 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1014 status);
1015 } else {
1016 PJ_LOG(3,(THIS_FILE, "%s sent",
1017 (renew? "Registration" : "Unregistration")));
1018 }
1019
1020on_return:
1021 PJSUA_UNLOCK();
1022 return status;
1023}
1024
1025
1026/*
1027 * Get account information.
1028 */
1029PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1030 pjsua_acc_info *info)
1031{
1032 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1033 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1034
1035 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001036 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001037
Benny Prijonoac623b32006-07-03 15:19:31 +00001038 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001039
1040 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1041 PJ_EINVAL);
1042 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1043
1044 PJSUA_LOCK();
1045
1046 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1047 PJSUA_UNLOCK();
1048 return PJ_EINVALIDOP;
1049 }
1050
1051 info->id = acc_id;
1052 info->is_default = (pjsua_var.default_acc == acc_id);
1053 info->acc_uri = acc_cfg->id;
1054 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1055 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001056 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1057 if (info->rpid.note.slen)
1058 info->online_status_text = info->rpid.note;
1059 else if (info->online_status)
1060 info->online_status_text = pj_str("Online");
1061 else
1062 info->online_status_text = pj_str("Offline");
1063
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001064 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001065 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001066 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1067 info->status_text = pj_str(info->buf_);
1068 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001069 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001070 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001071 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1072 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001073 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001074 info->status_text = pj_str("not registered");
1075 }
1076 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001077 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001078 info->status_text = pj_str("In Progress");
1079 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001080 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001081 info->status_text = pj_str("does not register");
1082 }
1083
1084 if (acc->regc) {
1085 pjsip_regc_info regc_info;
1086 pjsip_regc_get_info(acc->regc, &regc_info);
1087 info->expires = regc_info.next_reg;
1088 } else {
1089 info->expires = -1;
1090 }
1091
1092 PJSUA_UNLOCK();
1093
1094 return PJ_SUCCESS;
1095
1096}
1097
1098
1099/*
1100 * Enum accounts all account ids.
1101 */
1102PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1103 unsigned *count )
1104{
1105 unsigned i, c;
1106
1107 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1108
1109 PJSUA_LOCK();
1110
1111 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1112 if (!pjsua_var.acc[i].valid)
1113 continue;
1114 ids[c] = i;
1115 ++c;
1116 }
1117
1118 *count = c;
1119
1120 PJSUA_UNLOCK();
1121
1122 return PJ_SUCCESS;
1123}
1124
1125
1126/*
1127 * Enum accounts info.
1128 */
1129PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1130 unsigned *count )
1131{
1132 unsigned i, c;
1133
1134 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1135
1136 PJSUA_LOCK();
1137
1138 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1139 if (!pjsua_var.acc[i].valid)
1140 continue;
1141
1142 pjsua_acc_get_info(i, &info[c]);
1143 ++c;
1144 }
1145
1146 *count = c;
1147
1148 PJSUA_UNLOCK();
1149
1150 return PJ_SUCCESS;
1151}
1152
1153
1154/*
1155 * This is an internal function to find the most appropriate account to
1156 * used to reach to the specified URL.
1157 */
1158PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1159{
1160 pj_str_t tmp;
1161 pjsip_uri *uri;
1162 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001163 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001164
1165 PJSUA_LOCK();
1166
1167 PJ_TODO(dont_use_pjsua_pool);
1168
1169 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
1170
1171 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
1172 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +00001173 PJSUA_UNLOCK();
1174 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001175 }
1176
1177 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1178 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1179 {
1180 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001181 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1182 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001183 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001184 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001185 break;
1186 }
1187
Benny Prijono093d3022006-09-24 00:07:11 +00001188 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001189 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +00001190 PJSUA_UNLOCK();
1191 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001192 }
1193
1194 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +00001195 PJSUA_UNLOCK();
1196 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001197 }
1198
Benny Prijonoa1e69682007-05-11 15:14:34 +00001199 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001200
Benny Prijonob4a17c92006-07-10 14:40:21 +00001201 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001202 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1203 unsigned acc_id = pjsua_var.acc_ids[i];
1204 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1205 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1206 {
1207 PJSUA_UNLOCK();
1208 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001209 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001210 }
1211
Benny Prijonob4a17c92006-07-10 14:40:21 +00001212 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001213 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1214 unsigned acc_id = pjsua_var.acc_ids[i];
1215 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1216 {
1217 PJSUA_UNLOCK();
1218 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001219 }
1220 }
1221
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001222
Benny Prijono093d3022006-09-24 00:07:11 +00001223 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001224 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001225 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001226}
1227
1228
1229/*
1230 * This is an internal function to find the most appropriate account to be
1231 * used to handle incoming calls.
1232 */
1233PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1234{
1235 pjsip_uri *uri;
1236 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001237 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001238
Benny Prijono371cf0a2007-06-19 00:35:37 +00001239 /* Check that there's at least one account configured */
1240 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1241
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001242 uri = rdata->msg_info.to->uri;
1243
1244 /* Just return default account if To URI is not SIP: */
1245 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1246 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1247 {
1248 return pjsua_var.default_acc;
1249 }
1250
1251
1252 PJSUA_LOCK();
1253
1254 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1255
1256 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001257 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1258 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001259 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1260
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001261 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001262 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001263 {
1264 /* Match ! */
1265 PJSUA_UNLOCK();
1266 return acc_id;
1267 }
1268 }
1269
Benny Prijono093d3022006-09-24 00:07:11 +00001270 /* No matching account, try match domain part only. */
1271 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1272 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1274
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001275 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001276 /* Match ! */
1277 PJSUA_UNLOCK();
1278 return acc_id;
1279 }
1280 }
1281
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001282 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001283 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1284 unsigned acc_id = pjsua_var.acc_ids[i];
1285 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1286
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001287 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1288
1289 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1290 pjsip_transport_type_e type;
1291 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1292 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1293 type = PJSIP_TRANSPORT_UDP;
1294
1295 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1296 continue;
1297 }
1298
Benny Prijono093d3022006-09-24 00:07:11 +00001299 /* Match ! */
1300 PJSUA_UNLOCK();
1301 return acc_id;
1302 }
1303 }
1304
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 /* Still no match, use default account */
1306 PJSUA_UNLOCK();
1307 return pjsua_var.default_acc;
1308}
1309
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001310
Benny Prijonofff245c2007-04-02 11:44:47 +00001311/*
1312 * Create arbitrary requests for this account.
1313 */
1314PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1315 const pjsip_method *method,
1316 const pj_str_t *target,
1317 pjsip_tx_data **p_tdata)
1318{
1319 pjsip_tx_data *tdata;
1320 pjsua_acc *acc;
1321 pjsip_route_hdr *r;
1322 pj_status_t status;
1323
1324 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1325 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1326
1327 acc = &pjsua_var.acc[acc_id];
1328
1329 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1330 &acc->cfg.id, target,
1331 NULL, NULL, -1, NULL, &tdata);
1332 if (status != PJ_SUCCESS) {
1333 pjsua_perror(THIS_FILE, "Unable to create request", status);
1334 return status;
1335 }
1336
1337 /* Copy routeset */
1338 r = acc->route_set.next;
1339 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001340 pjsip_msg_add_hdr(tdata->msg,
1341 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001342 r = r->next;
1343 }
1344
1345 /* Done */
1346 *p_tdata = tdata;
1347 return PJ_SUCCESS;
1348}
1349
1350
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001351PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1352 pj_str_t *contact,
1353 pjsua_acc_id acc_id,
1354 const pj_str_t *suri)
1355{
1356 pjsua_acc *acc;
1357 pjsip_sip_uri *sip_uri;
1358 pj_status_t status;
1359 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1360 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001361 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001362 unsigned flag;
1363 int secure;
1364 int local_port;
1365
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001366 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001367 acc = &pjsua_var.acc[acc_id];
1368
Benny Prijonof75eceb2007-03-23 19:09:54 +00001369 /* If force_contact is configured, then use use it */
1370 if (acc->cfg.force_contact.slen) {
1371 *contact = acc->cfg.force_contact;
1372 return PJ_SUCCESS;
1373 }
1374
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001375 /* If route-set is configured for the account, then URI is the
1376 * first entry of the route-set.
1377 */
1378 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001379 sip_uri = (pjsip_sip_uri*)
1380 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001381 } else {
1382 pj_str_t tmp;
1383 pjsip_uri *uri;
1384
1385 pj_strdup_with_null(pool, &tmp, suri);
1386
1387 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1388 if (uri == NULL)
1389 return PJSIP_EINVALIDURI;
1390
1391 /* For non-SIP scheme, route set should be configured */
1392 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1393 return PJSIP_EINVALIDREQURI;
1394
Benny Prijono8c7a6172007-02-18 21:17:46 +00001395 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001396 }
1397
1398 /* Get transport type of the URI */
1399 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1400 tp_type = PJSIP_TRANSPORT_TLS;
1401 else if (sip_uri->transport_param.slen == 0) {
1402 tp_type = PJSIP_TRANSPORT_UDP;
1403 } else
1404 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1405
1406 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1407 return PJSIP_EUNSUPTRANSPORT;
1408
1409 flag = pjsip_transport_get_flag_from_type(tp_type);
1410 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1411
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001412 /* Init transport selector. */
1413 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1414
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001415 /* Get local address suitable to send request from */
1416 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001417 pool, tp_type, &tp_sel,
1418 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001419 if (status != PJ_SUCCESS)
1420 return status;
1421
1422 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001423 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001424 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1425 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1426 (int)acc->display.slen,
1427 acc->display.ptr,
1428 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001429 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001430 (int)acc->user_part.slen,
1431 acc->user_part.ptr,
1432 (acc->user_part.slen?"@":""),
1433 (int)local_addr.slen,
1434 local_addr.ptr,
1435 local_port,
1436 pjsip_transport_get_type_name(tp_type));
1437
1438 return PJ_SUCCESS;
1439}
1440
1441
1442
1443PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1444 pj_str_t *contact,
1445 pjsua_acc_id acc_id,
1446 pjsip_rx_data *rdata )
1447{
1448 /*
1449 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1450 * If the request that initiated the dialog contained a SIPS URI
1451 * in the Request-URI or in the top Record-Route header field value,
1452 * if there was any, or the Contact header field if there was no
1453 * Record-Route header field, the Contact header field in the response
1454 * MUST be a SIPS URI.
1455 */
1456 pjsua_acc *acc;
1457 pjsip_sip_uri *sip_uri;
1458 pj_status_t status;
1459 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1460 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001461 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001462 unsigned flag;
1463 int secure;
1464 int local_port;
1465
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001466 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001467 acc = &pjsua_var.acc[acc_id];
1468
Benny Prijonof75eceb2007-03-23 19:09:54 +00001469 /* If force_contact is configured, then use use it */
1470 if (acc->cfg.force_contact.slen) {
1471 *contact = acc->cfg.force_contact;
1472 return PJ_SUCCESS;
1473 }
1474
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001475 /* If Record-Route is present, then URI is the top Record-Route. */
1476 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001477 sip_uri = (pjsip_sip_uri*)
1478 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001479 } else {
1480 pjsip_contact_hdr *h_contact;
1481 pjsip_uri *uri = NULL;
1482
1483 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001484 h_contact = (pjsip_contact_hdr*)
1485 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001486 NULL);
1487 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001488 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001489
1490
1491 /* Or if Contact URI is not present, take the remote URI from
1492 * the From URI.
1493 */
1494 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001495 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001496
1497
1498 /* Can only do sip/sips scheme at present. */
1499 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1500 return PJSIP_EINVALIDREQURI;
1501
Benny Prijono8c7a6172007-02-18 21:17:46 +00001502 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001503 }
1504
1505 /* Get transport type of the URI */
1506 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1507 tp_type = PJSIP_TRANSPORT_TLS;
1508 else if (sip_uri->transport_param.slen == 0) {
1509 tp_type = PJSIP_TRANSPORT_UDP;
1510 } else
1511 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1512
1513 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1514 return PJSIP_EUNSUPTRANSPORT;
1515
1516 flag = pjsip_transport_get_flag_from_type(tp_type);
1517 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1518
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001519 /* Init transport selector. */
1520 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1521
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001522 /* Get local address suitable to send request from */
1523 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001524 pool, tp_type, &tp_sel,
1525 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001526 if (status != PJ_SUCCESS)
1527 return status;
1528
1529 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001530 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001531 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1532 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1533 (int)acc->display.slen,
1534 acc->display.ptr,
1535 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001536 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001537 (int)acc->user_part.slen,
1538 acc->user_part.ptr,
1539 (acc->user_part.slen?"@":""),
1540 (int)local_addr.slen,
1541 local_addr.ptr,
1542 local_port,
1543 pjsip_transport_get_type_name(tp_type));
1544
1545 return PJ_SUCCESS;
1546}
1547
1548
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001549PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1550 pjsua_transport_id tp_id)
1551{
1552 pjsua_acc *acc;
1553
1554 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1555 acc = &pjsua_var.acc[acc_id];
1556
Benny Prijonoa1e69682007-05-11 15:14:34 +00001557 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001558 PJ_EINVAL);
1559
1560 acc->cfg.transport_id = tp_id;
1561
1562 return PJ_SUCCESS;
1563}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001564