blob: 53090c4a1f6cfcaa69fb8ba2800ad5c4bbf810b0 [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
547 /* Find and parse Service-Route headers */
548 for (;;) {
549 char saved;
550 int parsed_len;
551
552 /* Find Service-Route header */
553 hsr = (pjsip_generic_string_hdr*)
554 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
555 if (!hsr)
556 break;
557
558 /* Parse as Route header since the syntax is similar. This may
559 * return more than one headers.
560 */
561 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
562 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
563 hr = (pjsip_route_hdr*)
564 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
565 hsr->hvalue.slen, &parsed_len);
566 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
567
568 if (hr == NULL) {
569 /* Error */
570 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
571 return;
572 }
573
574 /* Save each URI in the result */
575 h = hr;
576 do {
577 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
578 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
579 {
580 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
581 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
582 return;
583 }
584
585 uri[uri_cnt++] = h->name_addr.uri;
586 h = h->next;
587 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
588
589 if (h != hr) {
590 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
591 return;
592 }
593
594 /* Prepare to find next Service-Route header */
595 hsr = hsr->next;
596 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
597 break;
598 }
599
600 if (uri_cnt == 0)
601 return;
602
603 /*
604 * Update account's route set
605 */
606
607 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000608 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000609 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
610 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
611 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000612 i<rcnt;
613 ++i)
614 {
615 pjsip_route_hdr *prev = hr->prev;
616 pj_list_erase(hr);
617 hr = prev;
618 }
619 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000620
621 /* Then append the Service-Route URIs */
622 for (i=0; i<uri_cnt; ++i) {
623 hr = pjsip_route_hdr_create(pjsua_var.pool);
Benny Prijonof0f8fd12007-11-10 12:05:59 +0000624 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(pjsua_var.pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000625 pj_list_push_back(&acc->route_set, hr);
626 }
627
628 /* Done */
629
630 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
631 acc->index, uri_cnt));
632}
633
Benny Prijonobddef2c2007-10-31 13:28:08 +0000634
635/* Keep alive timer callback */
636static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
637{
638 pjsua_acc *acc;
639 pjsip_tpselector tp_sel;
640 pj_time_val delay;
641 pj_status_t status;
642
643 PJ_UNUSED_ARG(th);
644
645 PJSUA_LOCK();
646
647 te->id = PJ_FALSE;
648
649 acc = (pjsua_acc*) te->user_data;
650
651 /* Select the transport to send the packet */
652 pj_bzero(&tp_sel, sizeof(tp_sel));
653 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
654 tp_sel.u.transport = acc->ka_transport;
655
656 PJ_LOG(5,(THIS_FILE,
657 "Sending %d bytes keep-alive packet for acc %d to %s:%d",
658 acc->cfg.ka_data.slen, acc->index,
659 pj_inet_ntoa(acc->ka_target.ipv4.sin_addr),
660 pj_ntohs(acc->ka_target.ipv4.sin_port)));
661
662 /* Send raw packet */
663 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
664 PJSIP_TRANSPORT_UDP, &tp_sel,
665 NULL, acc->cfg.ka_data.ptr,
666 acc->cfg.ka_data.slen,
667 &acc->ka_target, acc->ka_target_len,
668 NULL, NULL);
669
670 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
671 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
672 }
673
674 /* Reschedule next timer */
675 delay.sec = acc->cfg.ka_interval;
676 delay.msec = 0;
677 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
678 if (status == PJ_SUCCESS) {
679 te->id = PJ_TRUE;
680 } else {
681 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
682 }
683
684 PJSUA_UNLOCK();
685}
686
687
688/* Update keep-alive for the account */
689static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
690 struct pjsip_regc_cbparam *param)
691{
692 /* In all cases, stop keep-alive timer if it's running. */
693 if (acc->ka_timer.id) {
694 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
695 acc->ka_timer.id = PJ_FALSE;
696
697 pjsip_transport_dec_ref(acc->ka_transport);
698 acc->ka_transport = NULL;
699 }
700
701 if (start) {
702 pj_time_val delay;
703 pj_status_t status;
704
705 /* Only do keep-alive if:
706 * - STUN is enabled in global config, and
707 * - ka_interval is not zero in the account, and
708 * - transport is UDP.
709 */
710 if (pjsua_var.stun_srv.ipv4.sin_family == 0 ||
711 acc->cfg.ka_interval == 0 ||
712 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
713 {
714 /* Keep alive is not necessary */
715 return;
716 }
717
718 /* Save transport and destination address. */
719 acc->ka_transport = param->rdata->tp_info.transport;
720 pjsip_transport_add_ref(acc->ka_transport);
721 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
722 param->rdata->pkt_info.src_addr_len);
723 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
724
725 /* Setup and start the timer */
726 acc->ka_timer.cb = &keep_alive_timer_cb;
727 acc->ka_timer.user_data = (void*)acc;
728
729 delay.sec = acc->cfg.ka_interval;
730 delay.msec = 0;
731 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
732 &delay);
733 if (status == PJ_SUCCESS) {
734 acc->ka_timer.id = PJ_TRUE;
735 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
736 "destination:%s:%d, interval:%ds",
737 acc->index,
738 param->rdata->pkt_info.src_name,
739 param->rdata->pkt_info.src_port,
740 acc->cfg.ka_interval));
741 } else {
742 acc->ka_timer.id = PJ_FALSE;
743 pjsip_transport_dec_ref(acc->ka_transport);
744 acc->ka_transport = NULL;
745 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
746 }
747 }
748}
749
750
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000751/*
752 * This callback is called by pjsip_regc when outgoing register
753 * request has completed.
754 */
755static void regc_cb(struct pjsip_regc_cbparam *param)
756{
757
Benny Prijonoa1e69682007-05-11 15:14:34 +0000758 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000759
Benny Prijono15b02302007-09-27 14:07:07 +0000760 if (param->regc != acc->regc)
761 return;
762
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000763 PJSUA_LOCK();
764
765 /*
766 * Print registration status.
767 */
768 if (param->status!=PJ_SUCCESS) {
769 pjsua_perror(THIS_FILE, "SIP registration error",
770 param->status);
771 pjsip_regc_destroy(acc->regc);
772 acc->regc = NULL;
773
Benny Prijonobddef2c2007-10-31 13:28:08 +0000774 /* Stop keep-alive timer if any. */
775 update_keep_alive(acc, PJ_FALSE, NULL);
776
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000777 } else if (param->code < 0 || param->code >= 300) {
778 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
779 param->code,
780 (int)param->reason.slen, param->reason.ptr));
781 pjsip_regc_destroy(acc->regc);
782 acc->regc = NULL;
783
Benny Prijonobddef2c2007-10-31 13:28:08 +0000784 /* Stop keep-alive timer if any. */
785 update_keep_alive(acc, PJ_FALSE, NULL);
786
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000787 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
788
789 if (param->expiration < 1) {
790 pjsip_regc_destroy(acc->regc);
791 acc->regc = NULL;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000792
793 /* Stop keep-alive timer if any. */
794 update_keep_alive(acc, PJ_FALSE, NULL);
795
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000796 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
797 pjsua_var.acc[acc->index].cfg.id.ptr));
798 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000799 /* Check NAT bound address */
800 if (acc_check_nat_addr(acc, param)) {
801 /* Update address, don't notify application yet */
802 PJSUA_UNLOCK();
803 return;
804 }
805
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000806 /* Check and update Service-Route header */
807 update_service_route(acc, param->rdata);
808
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000809 PJ_LOG(3, (THIS_FILE,
810 "%s: registration success, status=%d (%.*s), "
811 "will re-register in %d seconds",
812 pjsua_var.acc[acc->index].cfg.id.ptr,
813 param->code,
814 (int)param->reason.slen, param->reason.ptr,
815 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000816
Benny Prijonobddef2c2007-10-31 13:28:08 +0000817 /* Start keep-alive timer if necessary. */
818 update_keep_alive(acc, PJ_TRUE, param);
819
Benny Prijono8b6834f2007-02-24 13:29:22 +0000820 /* Send initial PUBLISH if it is enabled */
821 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
822 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000823 }
824
825 } else {
826 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
827 }
828
829 acc->reg_last_err = param->status;
830 acc->reg_last_code = param->code;
831
832 if (pjsua_var.ua_cfg.cb.on_reg_state)
833 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
834
835 PJSUA_UNLOCK();
836}
837
838
839/*
840 * Initialize client registration.
841 */
842static pj_status_t pjsua_regc_init(int acc_id)
843{
844 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000845 pj_str_t contact;
Benny Prijonodfb7d482006-10-18 20:35:14 +0000846 char contact_buf[1024];
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000847 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000848 pj_status_t status;
849
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000850 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000851 acc = &pjsua_var.acc[acc_id];
852
853 if (acc->cfg.reg_uri.slen == 0) {
854 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
855 return PJ_SUCCESS;
856 }
857
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000858 /* Destroy existing session, if any */
859 if (acc->regc) {
860 pjsip_regc_destroy(acc->regc);
861 acc->regc = NULL;
862 }
863
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000864 /* initialize SIP registration if registrar is configured */
865
866 status = pjsip_regc_create( pjsua_var.endpt,
867 acc, &regc_cb, &acc->regc);
868
869 if (status != PJ_SUCCESS) {
870 pjsua_perror(THIS_FILE, "Unable to create client registration",
871 status);
872 return status;
873 }
874
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000875 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
876 status = pjsua_acc_create_uac_contact( pool, &contact,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000877 acc_id, &acc->cfg.reg_uri);
878 if (status != PJ_SUCCESS) {
879 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
880 " for registration",
881 status);
882 return status;
883 }
884
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000885 status = pjsip_regc_init( acc->regc,
886 &acc->cfg.reg_uri,
887 &acc->cfg.id,
888 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000889 1, &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000890 acc->cfg.reg_timeout);
891 if (status != PJ_SUCCESS) {
892 pjsua_perror(THIS_FILE,
893 "Client registration initialization error",
894 status);
895 return status;
896 }
897
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000898 /* If account is locked to specific transport, then set transport to
899 * the client registration.
900 */
901 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
902 pjsip_tpselector tp_sel;
903
904 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
905 pjsip_regc_set_transport(acc->regc, &tp_sel);
906 }
907
908
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000909 /* Set credentials
910 */
911 if (acc->cred_cnt) {
912 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
913 }
914
Benny Prijono48ab2b72007-11-08 09:24:30 +0000915 /* Set authentication preference */
916 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
917
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000918 /* Set route-set
919 */
920 if (!pj_list_empty(&acc->route_set)) {
921 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
922 }
923
Benny Prijono8fc6de02006-11-11 21:25:55 +0000924 /* Add other request headers. */
925 if (pjsua_var.ua_cfg.user_agent.slen) {
926 pjsip_hdr hdr_list;
927 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
928 pjsip_generic_string_hdr *h;
929
930 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
931 pj_list_init(&hdr_list);
932
933 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
934 &pjsua_var.ua_cfg.user_agent);
935 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
936
937 pjsip_regc_add_headers(acc->regc, &hdr_list);
938 }
939
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000940 return PJ_SUCCESS;
941}
942
943
944/*
945 * Update registration or perform unregistration.
946 */
947PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
948 pj_bool_t renew)
949{
950 pj_status_t status = 0;
951 pjsip_tx_data *tdata = 0;
952
953 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
954 PJ_EINVAL);
955 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
956
957 PJSUA_LOCK();
958
959 if (renew) {
960 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000961 status = pjsua_regc_init(acc_id);
962 if (status != PJ_SUCCESS) {
963 pjsua_perror(THIS_FILE, "Unable to create registration",
964 status);
965 goto on_return;
966 }
967 }
968 if (!pjsua_var.acc[acc_id].regc) {
969 status = PJ_EINVALIDOP;
970 goto on_return;
971 }
972
973 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
974 &tdata);
975
Benny Prijono28f673a2007-10-15 07:04:59 +0000976 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
977 pjsua_acc *acc = &pjsua_var.acc[acc_id];
978 pjsip_authorization_hdr *h;
979 char *uri;
980 int d;
981
982 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
983 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
984 uri, acc->cfg.reg_uri.slen+10);
985 pj_assert(d > 0);
986
987 h = pjsip_authorization_hdr_create(tdata->pool);
988 h->scheme = pj_str("Digest");
989 h->credential.digest.username = acc->cred[0].username;
990 h->credential.digest.realm = acc->srv_domain;
991 h->credential.digest.uri = pj_str(uri);
992 h->credential.digest.algorithm = pj_str("md5");
993
994 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
995 }
996
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000997 } else {
998 if (pjsua_var.acc[acc_id].regc == NULL) {
999 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1000 status = PJ_EINVALIDOP;
1001 goto on_return;
1002 }
1003 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1004 }
1005
Benny Prijono56315612006-07-18 14:39:40 +00001006 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001007 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001008 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001009 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001010
1011 if (status != PJ_SUCCESS) {
1012 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1013 status);
1014 } else {
1015 PJ_LOG(3,(THIS_FILE, "%s sent",
1016 (renew? "Registration" : "Unregistration")));
1017 }
1018
1019on_return:
1020 PJSUA_UNLOCK();
1021 return status;
1022}
1023
1024
1025/*
1026 * Get account information.
1027 */
1028PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1029 pjsua_acc_info *info)
1030{
1031 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1032 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1033
1034 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001035 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001036
Benny Prijonoac623b32006-07-03 15:19:31 +00001037 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001038
1039 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1040 PJ_EINVAL);
1041 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1042
1043 PJSUA_LOCK();
1044
1045 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1046 PJSUA_UNLOCK();
1047 return PJ_EINVALIDOP;
1048 }
1049
1050 info->id = acc_id;
1051 info->is_default = (pjsua_var.default_acc == acc_id);
1052 info->acc_uri = acc_cfg->id;
1053 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1054 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001055 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1056 if (info->rpid.note.slen)
1057 info->online_status_text = info->rpid.note;
1058 else if (info->online_status)
1059 info->online_status_text = pj_str("Online");
1060 else
1061 info->online_status_text = pj_str("Offline");
1062
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001063 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001064 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1066 info->status_text = pj_str(info->buf_);
1067 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001068 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001069 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001070 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1071 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001072 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001073 info->status_text = pj_str("not registered");
1074 }
1075 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001076 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001077 info->status_text = pj_str("In Progress");
1078 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001079 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001080 info->status_text = pj_str("does not register");
1081 }
1082
1083 if (acc->regc) {
1084 pjsip_regc_info regc_info;
1085 pjsip_regc_get_info(acc->regc, &regc_info);
1086 info->expires = regc_info.next_reg;
1087 } else {
1088 info->expires = -1;
1089 }
1090
1091 PJSUA_UNLOCK();
1092
1093 return PJ_SUCCESS;
1094
1095}
1096
1097
1098/*
1099 * Enum accounts all account ids.
1100 */
1101PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1102 unsigned *count )
1103{
1104 unsigned i, c;
1105
1106 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1107
1108 PJSUA_LOCK();
1109
1110 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1111 if (!pjsua_var.acc[i].valid)
1112 continue;
1113 ids[c] = i;
1114 ++c;
1115 }
1116
1117 *count = c;
1118
1119 PJSUA_UNLOCK();
1120
1121 return PJ_SUCCESS;
1122}
1123
1124
1125/*
1126 * Enum accounts info.
1127 */
1128PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1129 unsigned *count )
1130{
1131 unsigned i, c;
1132
1133 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1134
1135 PJSUA_LOCK();
1136
1137 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1138 if (!pjsua_var.acc[i].valid)
1139 continue;
1140
1141 pjsua_acc_get_info(i, &info[c]);
1142 ++c;
1143 }
1144
1145 *count = c;
1146
1147 PJSUA_UNLOCK();
1148
1149 return PJ_SUCCESS;
1150}
1151
1152
1153/*
1154 * This is an internal function to find the most appropriate account to
1155 * used to reach to the specified URL.
1156 */
1157PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1158{
1159 pj_str_t tmp;
1160 pjsip_uri *uri;
1161 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001162 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001163
1164 PJSUA_LOCK();
1165
1166 PJ_TODO(dont_use_pjsua_pool);
1167
1168 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
1169
1170 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
1171 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +00001172 PJSUA_UNLOCK();
1173 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001174 }
1175
1176 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1177 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1178 {
1179 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001180 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1181 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001182 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001183 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001184 break;
1185 }
1186
Benny Prijono093d3022006-09-24 00:07:11 +00001187 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001188 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +00001189 PJSUA_UNLOCK();
1190 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001191 }
1192
1193 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +00001194 PJSUA_UNLOCK();
1195 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001196 }
1197
Benny Prijonoa1e69682007-05-11 15:14:34 +00001198 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001199
Benny Prijonob4a17c92006-07-10 14:40:21 +00001200 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001201 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1202 unsigned acc_id = pjsua_var.acc_ids[i];
1203 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1204 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1205 {
1206 PJSUA_UNLOCK();
1207 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001208 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001209 }
1210
Benny Prijonob4a17c92006-07-10 14:40:21 +00001211 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001212 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1213 unsigned acc_id = pjsua_var.acc_ids[i];
1214 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1215 {
1216 PJSUA_UNLOCK();
1217 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001218 }
1219 }
1220
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001221
Benny Prijono093d3022006-09-24 00:07:11 +00001222 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001223 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001224 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001225}
1226
1227
1228/*
1229 * This is an internal function to find the most appropriate account to be
1230 * used to handle incoming calls.
1231 */
1232PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1233{
1234 pjsip_uri *uri;
1235 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001236 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001237
Benny Prijono371cf0a2007-06-19 00:35:37 +00001238 /* Check that there's at least one account configured */
1239 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1240
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001241 uri = rdata->msg_info.to->uri;
1242
1243 /* Just return default account if To URI is not SIP: */
1244 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1245 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1246 {
1247 return pjsua_var.default_acc;
1248 }
1249
1250
1251 PJSUA_LOCK();
1252
1253 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1254
1255 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001256 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1257 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001258 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1259
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001260 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001261 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001262 {
1263 /* Match ! */
1264 PJSUA_UNLOCK();
1265 return acc_id;
1266 }
1267 }
1268
Benny Prijono093d3022006-09-24 00:07:11 +00001269 /* No matching account, try match domain part only. */
1270 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1271 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001272 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1273
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001274 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001275 /* Match ! */
1276 PJSUA_UNLOCK();
1277 return acc_id;
1278 }
1279 }
1280
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001281 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001282 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1283 unsigned acc_id = pjsua_var.acc_ids[i];
1284 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1285
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001286 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1287
1288 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1289 pjsip_transport_type_e type;
1290 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1291 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1292 type = PJSIP_TRANSPORT_UDP;
1293
1294 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1295 continue;
1296 }
1297
Benny Prijono093d3022006-09-24 00:07:11 +00001298 /* Match ! */
1299 PJSUA_UNLOCK();
1300 return acc_id;
1301 }
1302 }
1303
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001304 /* Still no match, use default account */
1305 PJSUA_UNLOCK();
1306 return pjsua_var.default_acc;
1307}
1308
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001309
Benny Prijonofff245c2007-04-02 11:44:47 +00001310/*
1311 * Create arbitrary requests for this account.
1312 */
1313PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1314 const pjsip_method *method,
1315 const pj_str_t *target,
1316 pjsip_tx_data **p_tdata)
1317{
1318 pjsip_tx_data *tdata;
1319 pjsua_acc *acc;
1320 pjsip_route_hdr *r;
1321 pj_status_t status;
1322
1323 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1324 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1325
1326 acc = &pjsua_var.acc[acc_id];
1327
1328 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1329 &acc->cfg.id, target,
1330 NULL, NULL, -1, NULL, &tdata);
1331 if (status != PJ_SUCCESS) {
1332 pjsua_perror(THIS_FILE, "Unable to create request", status);
1333 return status;
1334 }
1335
1336 /* Copy routeset */
1337 r = acc->route_set.next;
1338 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001339 pjsip_msg_add_hdr(tdata->msg,
1340 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001341 r = r->next;
1342 }
1343
1344 /* Done */
1345 *p_tdata = tdata;
1346 return PJ_SUCCESS;
1347}
1348
1349
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001350PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1351 pj_str_t *contact,
1352 pjsua_acc_id acc_id,
1353 const pj_str_t *suri)
1354{
1355 pjsua_acc *acc;
1356 pjsip_sip_uri *sip_uri;
1357 pj_status_t status;
1358 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1359 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001360 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001361 unsigned flag;
1362 int secure;
1363 int local_port;
1364
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001365 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001366 acc = &pjsua_var.acc[acc_id];
1367
Benny Prijonof75eceb2007-03-23 19:09:54 +00001368 /* If force_contact is configured, then use use it */
1369 if (acc->cfg.force_contact.slen) {
1370 *contact = acc->cfg.force_contact;
1371 return PJ_SUCCESS;
1372 }
1373
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001374 /* If route-set is configured for the account, then URI is the
1375 * first entry of the route-set.
1376 */
1377 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001378 sip_uri = (pjsip_sip_uri*)
1379 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001380 } else {
1381 pj_str_t tmp;
1382 pjsip_uri *uri;
1383
1384 pj_strdup_with_null(pool, &tmp, suri);
1385
1386 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1387 if (uri == NULL)
1388 return PJSIP_EINVALIDURI;
1389
1390 /* For non-SIP scheme, route set should be configured */
1391 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1392 return PJSIP_EINVALIDREQURI;
1393
Benny Prijono8c7a6172007-02-18 21:17:46 +00001394 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001395 }
1396
1397 /* Get transport type of the URI */
1398 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1399 tp_type = PJSIP_TRANSPORT_TLS;
1400 else if (sip_uri->transport_param.slen == 0) {
1401 tp_type = PJSIP_TRANSPORT_UDP;
1402 } else
1403 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1404
1405 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1406 return PJSIP_EUNSUPTRANSPORT;
1407
1408 flag = pjsip_transport_get_flag_from_type(tp_type);
1409 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1410
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001411 /* Init transport selector. */
1412 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1413
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001414 /* Get local address suitable to send request from */
1415 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001416 pool, tp_type, &tp_sel,
1417 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001418 if (status != PJ_SUCCESS)
1419 return status;
1420
1421 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001422 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001423 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1424 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1425 (int)acc->display.slen,
1426 acc->display.ptr,
1427 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001428 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001429 (int)acc->user_part.slen,
1430 acc->user_part.ptr,
1431 (acc->user_part.slen?"@":""),
1432 (int)local_addr.slen,
1433 local_addr.ptr,
1434 local_port,
1435 pjsip_transport_get_type_name(tp_type));
1436
1437 return PJ_SUCCESS;
1438}
1439
1440
1441
1442PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1443 pj_str_t *contact,
1444 pjsua_acc_id acc_id,
1445 pjsip_rx_data *rdata )
1446{
1447 /*
1448 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1449 * If the request that initiated the dialog contained a SIPS URI
1450 * in the Request-URI or in the top Record-Route header field value,
1451 * if there was any, or the Contact header field if there was no
1452 * Record-Route header field, the Contact header field in the response
1453 * MUST be a SIPS URI.
1454 */
1455 pjsua_acc *acc;
1456 pjsip_sip_uri *sip_uri;
1457 pj_status_t status;
1458 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1459 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001460 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001461 unsigned flag;
1462 int secure;
1463 int local_port;
1464
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001465 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001466 acc = &pjsua_var.acc[acc_id];
1467
Benny Prijonof75eceb2007-03-23 19:09:54 +00001468 /* If force_contact is configured, then use use it */
1469 if (acc->cfg.force_contact.slen) {
1470 *contact = acc->cfg.force_contact;
1471 return PJ_SUCCESS;
1472 }
1473
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001474 /* If Record-Route is present, then URI is the top Record-Route. */
1475 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001476 sip_uri = (pjsip_sip_uri*)
1477 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001478 } else {
1479 pjsip_contact_hdr *h_contact;
1480 pjsip_uri *uri = NULL;
1481
1482 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001483 h_contact = (pjsip_contact_hdr*)
1484 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001485 NULL);
1486 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001487 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001488
1489
1490 /* Or if Contact URI is not present, take the remote URI from
1491 * the From URI.
1492 */
1493 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001494 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001495
1496
1497 /* Can only do sip/sips scheme at present. */
1498 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1499 return PJSIP_EINVALIDREQURI;
1500
Benny Prijono8c7a6172007-02-18 21:17:46 +00001501 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001502 }
1503
1504 /* Get transport type of the URI */
1505 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1506 tp_type = PJSIP_TRANSPORT_TLS;
1507 else if (sip_uri->transport_param.slen == 0) {
1508 tp_type = PJSIP_TRANSPORT_UDP;
1509 } else
1510 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1511
1512 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1513 return PJSIP_EUNSUPTRANSPORT;
1514
1515 flag = pjsip_transport_get_flag_from_type(tp_type);
1516 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1517
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001518 /* Init transport selector. */
1519 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1520
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001521 /* Get local address suitable to send request from */
1522 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001523 pool, tp_type, &tp_sel,
1524 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001525 if (status != PJ_SUCCESS)
1526 return status;
1527
1528 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001529 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001530 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1531 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1532 (int)acc->display.slen,
1533 acc->display.ptr,
1534 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001535 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001536 (int)acc->user_part.slen,
1537 acc->user_part.ptr,
1538 (acc->user_part.slen?"@":""),
1539 (int)local_addr.slen,
1540 local_addr.ptr,
1541 local_port,
1542 pjsip_transport_get_type_name(tp_type));
1543
1544 return PJ_SUCCESS;
1545}
1546
1547
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001548PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1549 pjsua_transport_id tp_id)
1550{
1551 pjsua_acc *acc;
1552
1553 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1554 acc = &pjsua_var.acc[acc_id];
1555
Benny Prijonoa1e69682007-05-11 15:14:34 +00001556 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001557 PJ_EINVAL);
1558
1559 acc->cfg.transport_id = tp_id;
1560
1561 return PJ_SUCCESS;
1562}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001563