blob: 82ff609cd204366e0fa177ddcce499814ea54760 [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{
40 return acc_id>=0 && acc_id<PJ_ARRAY_SIZE(pjsua_var.acc) &&
41 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 */
67static void copy_acc_config(pj_pool_t *pool,
68 pjsua_acc_config *dst,
69 const pjsua_acc_config *src)
70{
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 Prijonoeebe9af2006-06-13 22:57:13 +000078
79 dst->proxy_cnt = src->proxy_cnt;
80 for (i=0; i<src->proxy_cnt; ++i)
81 pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]);
82
83 dst->reg_timeout = src->reg_timeout;
84 dst->cred_count = src->cred_count;
85
86 for (i=0; i<src->cred_count; ++i) {
87 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
88 }
89}
90
91
92/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000093 * Initialize a new account (after configuration is set).
94 */
95static pj_status_t initialize_acc(unsigned acc_id)
96{
97 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
98 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +000099 pjsip_name_addr *name_addr;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000100 pjsip_sip_uri *sip_uri, *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000101 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000102 unsigned i;
103
104 /* Need to parse local_uri to get the elements: */
105
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000106 name_addr = (pjsip_name_addr*)
107 pjsip_parse_uri(pjsua_var.pool, acc_cfg->id.ptr,
108 acc_cfg->id.slen,
109 PJSIP_PARSE_URI_AS_NAMEADDR);
110 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111 pjsua_perror(THIS_FILE, "Invalid local URI",
112 PJSIP_EINVALIDURI);
113 return PJSIP_EINVALIDURI;
114 }
115
116 /* Local URI MUST be a SIP or SIPS: */
117
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000118 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
119 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000120 {
121 pjsua_perror(THIS_FILE, "Invalid local URI",
122 PJSIP_EINVALIDSCHEME);
123 return PJSIP_EINVALIDSCHEME;
124 }
125
126
127 /* Get the SIP URI object: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000128 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000129
Benny Prijonob4a17c92006-07-10 14:40:21 +0000130
131 /* Parse registrar URI, if any */
132 if (acc_cfg->reg_uri.slen) {
133 pjsip_uri *reg_uri;
134
135 reg_uri = pjsip_parse_uri(pjsua_var.pool, acc_cfg->reg_uri.ptr,
136 acc_cfg->reg_uri.slen, 0);
137 if (reg_uri == NULL) {
138 pjsua_perror(THIS_FILE, "Invalid registrar URI",
139 PJSIP_EINVALIDURI);
140 return PJSIP_EINVALIDURI;
141 }
142
143 /* Registrar URI MUST be a SIP or SIPS: */
144 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
145 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
146 {
147 pjsua_perror(THIS_FILE, "Invalid registar URI",
148 PJSIP_EINVALIDSCHEME);
149 return PJSIP_EINVALIDSCHEME;
150 }
151
152 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
153
154 } else {
155 sip_reg_uri = NULL;
156 }
157
158
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000159 /* Save the user and domain part. These will be used when finding an
160 * account for incoming requests.
161 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000162 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000163 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000164 acc->srv_domain = sip_uri->host;
165 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000166
Benny Prijonob4a17c92006-07-10 14:40:21 +0000167 if (sip_reg_uri) {
168 acc->srv_port = sip_reg_uri->port;
169 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000170
171 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000172 //if (acc_cfg->contact.slen == 0) {
173 // acc_cfg->contact = acc_cfg->id;
174 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000175
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000176 /* Build account route-set from outbound proxies and route set from
177 * account configuration.
178 */
179 pj_list_init(&acc->route_set);
180
181 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
182 pj_str_t hname = { "Route", 5};
183 pjsip_route_hdr *r;
184 pj_str_t tmp;
185
186 pj_strdup_with_null(pjsua_var.pool, &tmp,
187 &pjsua_var.ua_cfg.outbound_proxy[i]);
188 r = pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
189 if (r == NULL) {
190 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
191 PJSIP_EINVALIDURI);
192 return PJSIP_EINVALIDURI;
193 }
194 pj_list_push_back(&acc->route_set, r);
195 }
196
197 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
198 pj_str_t hname = { "Route", 5};
199 pjsip_route_hdr *r;
200 pj_str_t tmp;
201
202 pj_strdup_with_null(pjsua_var.pool, &tmp, &acc_cfg->proxy[i]);
203 r = pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
204 if (r == NULL) {
205 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
206 PJ_EINVAL);
207 return PJ_EINVAL;
208 }
209 pj_list_push_back(&acc->route_set, r);
210 }
211
212
213 /* Concatenate credentials from account config and global config */
214 acc->cred_cnt = 0;
215 for (i=0; i<acc_cfg->cred_count; ++i) {
216 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
217 }
218 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
219 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
220 {
221 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
222 }
223
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000224 status = pjsua_pres_init_acc(acc_id);
225 if (status != PJ_SUCCESS)
226 return status;
227
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228 /* Mark account as valid */
229 pjsua_var.acc[acc_id].valid = PJ_TRUE;
230
Benny Prijono093d3022006-09-24 00:07:11 +0000231 /* Insert account ID into account ID array, sorted by priority */
232 for (i=0; i<pjsua_var.acc_cnt; ++i) {
233 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
234 pjsua_var.acc[acc_id].cfg.priority)
235 {
236 break;
237 }
238 }
239 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
240 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000241
242 return PJ_SUCCESS;
243}
244
245
246/*
247 * Add a new account to pjsua.
248 */
249PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
250 pj_bool_t is_default,
251 pjsua_acc_id *p_acc_id)
252{
253 unsigned id;
254 pj_status_t status;
255
256 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
257 PJ_ETOOMANY);
258
259 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000260 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000261
262 PJSUA_LOCK();
263
264 /* Find empty account id. */
265 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
266 if (pjsua_var.acc[id].valid == PJ_FALSE)
267 break;
268 }
269
270 /* Expect to find a slot */
271 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
272 {PJSUA_UNLOCK(); return PJ_EBUG;});
273
274 /* Copy config */
275 copy_acc_config(pjsua_var.pool, &pjsua_var.acc[id].cfg, cfg);
276
277 /* Normalize registration timeout */
278 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
279 pjsua_var.acc[id].cfg.reg_timeout == 0)
280 {
281 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
282 }
283
284 status = initialize_acc(id);
285 if (status != PJ_SUCCESS) {
286 pjsua_perror(THIS_FILE, "Error adding account", status);
287 PJSUA_UNLOCK();
288 return status;
289 }
290
291 if (is_default)
292 pjsua_var.default_acc = id;
293
294 if (p_acc_id)
295 *p_acc_id = id;
296
297 pjsua_var.acc_cnt++;
298
299 PJSUA_UNLOCK();
300
301 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
302 (int)cfg->id.slen, cfg->id.ptr, id));
303
304 /* If accounts has registration enabled, start registration */
305 if (pjsua_var.acc[id].cfg.reg_uri.slen)
306 pjsua_acc_set_registration(id, PJ_TRUE);
307
308
309 return PJ_SUCCESS;
310}
311
312
313/*
314 * Add local account
315 */
316PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
317 pj_bool_t is_default,
318 pjsua_acc_id *p_acc_id)
319{
320 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000321 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000322 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000323
Benny Prijonoe93e2872006-06-28 16:46:49 +0000324 /* ID must be valid */
325 PJ_ASSERT_RETURN(tid>=0 && tid<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
326
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000327 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000328 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329
330 pjsua_acc_config_default(&cfg);
331
Benny Prijono093d3022006-09-24 00:07:11 +0000332 /* Lower the priority of local account */
333 --cfg.priority;
334
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000335 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000336 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
337 "<sip:%.*s:%d;transport=%s>",
338 (int)t->local_name.host.slen,
339 t->local_name.host.ptr,
340 t->local_name.port,
341 pjsip_transport_get_type_name(t->type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000342
343 cfg.id = pj_str(uri);
344
345 return pjsua_acc_add(&cfg, is_default, p_acc_id);
346}
347
348
349/*
350 * Delete account.
351 */
352PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
353{
Benny Prijono093d3022006-09-24 00:07:11 +0000354 unsigned i;
355
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000356 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
357 PJ_EINVAL);
358 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
359
360 PJSUA_LOCK();
361
362 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000363 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000364 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijono922933b2007-01-21 16:23:56 +0000365 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
366 pjsua_var.acc[acc_id].regc = NULL;
367 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368
369 /* Delete server presence subscription */
370 pjsua_pres_delete_acc(acc_id);
371
372 /* Invalidate */
373 pjsua_var.acc[acc_id].valid = PJ_FALSE;
374
Benny Prijono093d3022006-09-24 00:07:11 +0000375 /* Remove from array */
376 for (i=0; i<pjsua_var.acc_cnt; ++i) {
377 if (pjsua_var.acc_ids[i] == acc_id)
378 break;
379 }
380 if (i != pjsua_var.acc_cnt) {
381 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
382 pjsua_var.acc_cnt, i);
383 --pjsua_var.acc_cnt;
384 }
385
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000386 /* Leave the calls intact, as I don't think calls need to
387 * access account once it's created
388 */
389
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390
391 PJSUA_UNLOCK();
392
393 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
394
395 return PJ_SUCCESS;
396}
397
398
399/*
400 * Modify account information.
401 */
402PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
403 const pjsua_acc_config *cfg)
404{
405 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000406 PJ_UNUSED_ARG(acc_id);
407 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000408 return PJ_EINVALIDOP;
409}
410
411
412/*
413 * Modify account's presence status to be advertised to remote/presence
414 * subscribers.
415 */
416PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
417 pj_bool_t is_online)
418{
419 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
420 PJ_EINVAL);
421 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
422
423 pjsua_var.acc[acc_id].online_status = is_online;
424 pjsua_pres_refresh();
425 return PJ_SUCCESS;
426}
427
428
429/*
430 * This callback is called by pjsip_regc when outgoing register
431 * request has completed.
432 */
433static void regc_cb(struct pjsip_regc_cbparam *param)
434{
435
436 pjsua_acc *acc = param->token;
437
438 PJSUA_LOCK();
439
440 /*
441 * Print registration status.
442 */
443 if (param->status!=PJ_SUCCESS) {
444 pjsua_perror(THIS_FILE, "SIP registration error",
445 param->status);
446 pjsip_regc_destroy(acc->regc);
447 acc->regc = NULL;
448
449 } else if (param->code < 0 || param->code >= 300) {
450 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
451 param->code,
452 (int)param->reason.slen, param->reason.ptr));
453 pjsip_regc_destroy(acc->regc);
454 acc->regc = NULL;
455
456 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
457
458 if (param->expiration < 1) {
459 pjsip_regc_destroy(acc->regc);
460 acc->regc = NULL;
461 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
462 pjsua_var.acc[acc->index].cfg.id.ptr));
463 } else {
464 PJ_LOG(3, (THIS_FILE,
465 "%s: registration success, status=%d (%.*s), "
466 "will re-register in %d seconds",
467 pjsua_var.acc[acc->index].cfg.id.ptr,
468 param->code,
469 (int)param->reason.slen, param->reason.ptr,
470 param->expiration));
471 }
472
473 } else {
474 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
475 }
476
477 acc->reg_last_err = param->status;
478 acc->reg_last_code = param->code;
479
480 if (pjsua_var.ua_cfg.cb.on_reg_state)
481 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
482
483 PJSUA_UNLOCK();
484}
485
486
487/*
488 * Initialize client registration.
489 */
490static pj_status_t pjsua_regc_init(int acc_id)
491{
492 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000493 pj_str_t contact;
Benny Prijonodfb7d482006-10-18 20:35:14 +0000494 char contact_buf[1024];
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000495 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496 pj_status_t status;
497
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000498 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000499 acc = &pjsua_var.acc[acc_id];
500
501 if (acc->cfg.reg_uri.slen == 0) {
502 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
503 return PJ_SUCCESS;
504 }
505
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000506 /* Destroy existing session, if any */
507 if (acc->regc) {
508 pjsip_regc_destroy(acc->regc);
509 acc->regc = NULL;
510 }
511
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000512 /* initialize SIP registration if registrar is configured */
513
514 status = pjsip_regc_create( pjsua_var.endpt,
515 acc, &regc_cb, &acc->regc);
516
517 if (status != PJ_SUCCESS) {
518 pjsua_perror(THIS_FILE, "Unable to create client registration",
519 status);
520 return status;
521 }
522
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000523 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
524 status = pjsua_acc_create_uac_contact( pool, &contact,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000525 acc_id, &acc->cfg.reg_uri);
526 if (status != PJ_SUCCESS) {
527 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
528 " for registration",
529 status);
530 return status;
531 }
532
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000533 status = pjsip_regc_init( acc->regc,
534 &acc->cfg.reg_uri,
535 &acc->cfg.id,
536 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000537 1, &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 acc->cfg.reg_timeout);
539 if (status != PJ_SUCCESS) {
540 pjsua_perror(THIS_FILE,
541 "Client registration initialization error",
542 status);
543 return status;
544 }
545
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000546 /* If account is locked to specific transport, then set transport to
547 * the client registration.
548 */
549 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
550 pjsip_tpselector tp_sel;
551
552 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
553 pjsip_regc_set_transport(acc->regc, &tp_sel);
554 }
555
556
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000557 /* Set credentials
558 */
559 if (acc->cred_cnt) {
560 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
561 }
562
563 /* Set route-set
564 */
565 if (!pj_list_empty(&acc->route_set)) {
566 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
567 }
568
Benny Prijono8fc6de02006-11-11 21:25:55 +0000569 /* Add other request headers. */
570 if (pjsua_var.ua_cfg.user_agent.slen) {
571 pjsip_hdr hdr_list;
572 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
573 pjsip_generic_string_hdr *h;
574
575 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
576 pj_list_init(&hdr_list);
577
578 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
579 &pjsua_var.ua_cfg.user_agent);
580 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
581
582 pjsip_regc_add_headers(acc->regc, &hdr_list);
583 }
584
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000585 return PJ_SUCCESS;
586}
587
588
589/*
590 * Update registration or perform unregistration.
591 */
592PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
593 pj_bool_t renew)
594{
595 pj_status_t status = 0;
596 pjsip_tx_data *tdata = 0;
597
598 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
599 PJ_EINVAL);
600 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
601
602 PJSUA_LOCK();
603
604 if (renew) {
605 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000606 status = pjsua_regc_init(acc_id);
607 if (status != PJ_SUCCESS) {
608 pjsua_perror(THIS_FILE, "Unable to create registration",
609 status);
610 goto on_return;
611 }
612 }
613 if (!pjsua_var.acc[acc_id].regc) {
614 status = PJ_EINVALIDOP;
615 goto on_return;
616 }
617
618 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
619 &tdata);
620
621 } else {
622 if (pjsua_var.acc[acc_id].regc == NULL) {
623 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
624 status = PJ_EINVALIDOP;
625 goto on_return;
626 }
627 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
628 }
629
Benny Prijono56315612006-07-18 14:39:40 +0000630 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +0000631 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000632 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +0000633 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000634
635 if (status != PJ_SUCCESS) {
636 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
637 status);
638 } else {
639 PJ_LOG(3,(THIS_FILE, "%s sent",
640 (renew? "Registration" : "Unregistration")));
641 }
642
643on_return:
644 PJSUA_UNLOCK();
645 return status;
646}
647
648
649/*
650 * Get account information.
651 */
652PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
653 pjsua_acc_info *info)
654{
655 pjsua_acc *acc = &pjsua_var.acc[acc_id];
656 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
657
658 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000659 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000660
Benny Prijonoac623b32006-07-03 15:19:31 +0000661 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000662
663 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
664 PJ_EINVAL);
665 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
666
667 PJSUA_LOCK();
668
669 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
670 PJSUA_UNLOCK();
671 return PJ_EINVALIDOP;
672 }
673
674 info->id = acc_id;
675 info->is_default = (pjsua_var.default_acc == acc_id);
676 info->acc_uri = acc_cfg->id;
677 info->has_registration = (acc->cfg.reg_uri.slen > 0);
678 info->online_status = acc->online_status;
679
680 if (acc->reg_last_err) {
681 info->status = acc->reg_last_err;
682 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
683 info->status_text = pj_str(info->buf_);
684 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +0000685 if (info->has_registration) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000686 info->status = acc->reg_last_code;
687 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
688 } else {
689 info->status = 0;
690 info->status_text = pj_str("not registered");
691 }
692 } else if (acc->cfg.reg_uri.slen) {
693 info->status = 100;
694 info->status_text = pj_str("In Progress");
695 } else {
696 info->status = 0;
697 info->status_text = pj_str("does not register");
698 }
699
700 if (acc->regc) {
701 pjsip_regc_info regc_info;
702 pjsip_regc_get_info(acc->regc, &regc_info);
703 info->expires = regc_info.next_reg;
704 } else {
705 info->expires = -1;
706 }
707
708 PJSUA_UNLOCK();
709
710 return PJ_SUCCESS;
711
712}
713
714
715/*
716 * Enum accounts all account ids.
717 */
718PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
719 unsigned *count )
720{
721 unsigned i, c;
722
723 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
724
725 PJSUA_LOCK();
726
727 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
728 if (!pjsua_var.acc[i].valid)
729 continue;
730 ids[c] = i;
731 ++c;
732 }
733
734 *count = c;
735
736 PJSUA_UNLOCK();
737
738 return PJ_SUCCESS;
739}
740
741
742/*
743 * Enum accounts info.
744 */
745PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
746 unsigned *count )
747{
748 unsigned i, c;
749
750 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
751
752 PJSUA_LOCK();
753
754 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
755 if (!pjsua_var.acc[i].valid)
756 continue;
757
758 pjsua_acc_get_info(i, &info[c]);
759 ++c;
760 }
761
762 *count = c;
763
764 PJSUA_UNLOCK();
765
766 return PJ_SUCCESS;
767}
768
769
770/*
771 * This is an internal function to find the most appropriate account to
772 * used to reach to the specified URL.
773 */
774PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
775{
776 pj_str_t tmp;
777 pjsip_uri *uri;
778 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +0000779 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000780
781 PJSUA_LOCK();
782
783 PJ_TODO(dont_use_pjsua_pool);
784
785 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
786
787 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
788 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +0000789 PJSUA_UNLOCK();
790 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000791 }
792
793 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
794 !PJSIP_URI_SCHEME_IS_SIPS(uri))
795 {
796 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +0000797 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
798 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000799 continue;
Benny Prijono093d3022006-09-24 00:07:11 +0000800 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000801 break;
802 }
803
Benny Prijono093d3022006-09-24 00:07:11 +0000804 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000805 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +0000806 PJSUA_UNLOCK();
807 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000808 }
809
810 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +0000811 PJSUA_UNLOCK();
812 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000813 }
814
815 sip_uri = pjsip_uri_get_uri(uri);
816
Benny Prijonob4a17c92006-07-10 14:40:21 +0000817 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +0000818 for (i=0; i<pjsua_var.acc_cnt; ++i) {
819 unsigned acc_id = pjsua_var.acc_ids[i];
820 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
821 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
822 {
823 PJSUA_UNLOCK();
824 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +0000825 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000826 }
827
Benny Prijonob4a17c92006-07-10 14:40:21 +0000828 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +0000829 for (i=0; i<pjsua_var.acc_cnt; ++i) {
830 unsigned acc_id = pjsua_var.acc_ids[i];
831 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
832 {
833 PJSUA_UNLOCK();
834 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000835 }
836 }
837
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000838
Benny Prijono093d3022006-09-24 00:07:11 +0000839 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000840 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +0000841 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000842}
843
844
845/*
846 * This is an internal function to find the most appropriate account to be
847 * used to handle incoming calls.
848 */
849PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
850{
851 pjsip_uri *uri;
852 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +0000853 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000854
855 uri = rdata->msg_info.to->uri;
856
857 /* Just return default account if To URI is not SIP: */
858 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
859 !PJSIP_URI_SCHEME_IS_SIPS(uri))
860 {
861 return pjsua_var.default_acc;
862 }
863
864
865 PJSUA_LOCK();
866
867 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
868
869 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +0000870 for (i=0; i < pjsua_var.acc_cnt; ++i) {
871 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000872 pjsua_acc *acc = &pjsua_var.acc[acc_id];
873
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000874 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +0000875 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000876 {
877 /* Match ! */
878 PJSUA_UNLOCK();
879 return acc_id;
880 }
881 }
882
Benny Prijono093d3022006-09-24 00:07:11 +0000883 /* No matching account, try match domain part only. */
884 for (i=0; i < pjsua_var.acc_cnt; ++i) {
885 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000886 pjsua_acc *acc = &pjsua_var.acc[acc_id];
887
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000888 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 /* Match ! */
890 PJSUA_UNLOCK();
891 return acc_id;
892 }
893 }
894
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000895 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +0000896 for (i=0; i < pjsua_var.acc_cnt; ++i) {
897 unsigned acc_id = pjsua_var.acc_ids[i];
898 pjsua_acc *acc = &pjsua_var.acc[acc_id];
899
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000900 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
901
902 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
903 pjsip_transport_type_e type;
904 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
905 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
906 type = PJSIP_TRANSPORT_UDP;
907
908 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
909 continue;
910 }
911
Benny Prijono093d3022006-09-24 00:07:11 +0000912 /* Match ! */
913 PJSUA_UNLOCK();
914 return acc_id;
915 }
916 }
917
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000918 /* Still no match, use default account */
919 PJSUA_UNLOCK();
920 return pjsua_var.default_acc;
921}
922
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000923
924PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
925 pj_str_t *contact,
926 pjsua_acc_id acc_id,
927 const pj_str_t *suri)
928{
929 pjsua_acc *acc;
930 pjsip_sip_uri *sip_uri;
931 pj_status_t status;
932 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
933 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000934 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000935 unsigned flag;
936 int secure;
937 int local_port;
938
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000939 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000940 acc = &pjsua_var.acc[acc_id];
941
942 /* If route-set is configured for the account, then URI is the
943 * first entry of the route-set.
944 */
945 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +0000946 sip_uri = (pjsip_sip_uri*)
947 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000948 } else {
949 pj_str_t tmp;
950 pjsip_uri *uri;
951
952 pj_strdup_with_null(pool, &tmp, suri);
953
954 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
955 if (uri == NULL)
956 return PJSIP_EINVALIDURI;
957
958 /* For non-SIP scheme, route set should be configured */
959 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
960 return PJSIP_EINVALIDREQURI;
961
Benny Prijono8c7a6172007-02-18 21:17:46 +0000962 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000963 }
964
965 /* Get transport type of the URI */
966 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
967 tp_type = PJSIP_TRANSPORT_TLS;
968 else if (sip_uri->transport_param.slen == 0) {
969 tp_type = PJSIP_TRANSPORT_UDP;
970 } else
971 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
972
973 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
974 return PJSIP_EUNSUPTRANSPORT;
975
976 flag = pjsip_transport_get_flag_from_type(tp_type);
977 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
978
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000979 /* Init transport selector. */
980 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
981
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000982 /* Get local address suitable to send request from */
983 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000984 pool, tp_type, &tp_sel,
985 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000986 if (status != PJ_SUCCESS)
987 return status;
988
989 /* Create the contact header */
990 contact->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
991 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
992 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
993 (int)acc->display.slen,
994 acc->display.ptr,
995 (acc->display.slen?" " : ""),
996 (secure ? "sips" : "sip"),
997 (int)acc->user_part.slen,
998 acc->user_part.ptr,
999 (acc->user_part.slen?"@":""),
1000 (int)local_addr.slen,
1001 local_addr.ptr,
1002 local_port,
1003 pjsip_transport_get_type_name(tp_type));
1004
1005 return PJ_SUCCESS;
1006}
1007
1008
1009
1010PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1011 pj_str_t *contact,
1012 pjsua_acc_id acc_id,
1013 pjsip_rx_data *rdata )
1014{
1015 /*
1016 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1017 * If the request that initiated the dialog contained a SIPS URI
1018 * in the Request-URI or in the top Record-Route header field value,
1019 * if there was any, or the Contact header field if there was no
1020 * Record-Route header field, the Contact header field in the response
1021 * MUST be a SIPS URI.
1022 */
1023 pjsua_acc *acc;
1024 pjsip_sip_uri *sip_uri;
1025 pj_status_t status;
1026 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1027 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001028 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001029 unsigned flag;
1030 int secure;
1031 int local_port;
1032
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001033 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001034 acc = &pjsua_var.acc[acc_id];
1035
1036 /* If Record-Route is present, then URI is the top Record-Route. */
1037 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001038 sip_uri = (pjsip_sip_uri*)
1039 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001040 } else {
1041 pjsip_contact_hdr *h_contact;
1042 pjsip_uri *uri = NULL;
1043
1044 /* Otherwise URI is Contact URI */
1045 h_contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
1046 NULL);
1047 if (h_contact)
1048 uri = pjsip_uri_get_uri(h_contact->uri);
1049
1050
1051 /* Or if Contact URI is not present, take the remote URI from
1052 * the From URI.
1053 */
1054 if (uri == NULL)
1055 uri = pjsip_uri_get_uri(rdata->msg_info.from->uri);
1056
1057
1058 /* Can only do sip/sips scheme at present. */
1059 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1060 return PJSIP_EINVALIDREQURI;
1061
Benny Prijono8c7a6172007-02-18 21:17:46 +00001062 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001063 }
1064
1065 /* Get transport type of the URI */
1066 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1067 tp_type = PJSIP_TRANSPORT_TLS;
1068 else if (sip_uri->transport_param.slen == 0) {
1069 tp_type = PJSIP_TRANSPORT_UDP;
1070 } else
1071 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1072
1073 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1074 return PJSIP_EUNSUPTRANSPORT;
1075
1076 flag = pjsip_transport_get_flag_from_type(tp_type);
1077 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1078
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001079 /* Init transport selector. */
1080 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1081
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001082 /* Get local address suitable to send request from */
1083 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001084 pool, tp_type, &tp_sel,
1085 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001086 if (status != PJ_SUCCESS)
1087 return status;
1088
1089 /* Create the contact header */
1090 contact->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1091 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1092 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1093 (int)acc->display.slen,
1094 acc->display.ptr,
1095 (acc->display.slen?" " : ""),
1096 (secure ? "sips" : "sip"),
1097 (int)acc->user_part.slen,
1098 acc->user_part.ptr,
1099 (acc->user_part.slen?"@":""),
1100 (int)local_addr.slen,
1101 local_addr.ptr,
1102 local_port,
1103 pjsip_transport_get_type_name(tp_type));
1104
1105 return PJ_SUCCESS;
1106}
1107
1108
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001109PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1110 pjsua_transport_id tp_id)
1111{
1112 pjsua_acc *acc;
1113
1114 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1115 acc = &pjsua_var.acc[acc_id];
1116
1117 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata),
1118 PJ_EINVAL);
1119
1120 acc->cfg.transport_id = tp_id;
1121
1122 return PJ_SUCCESS;
1123}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001124