blob: f559169724adc123d42be4e60a579e857ad2be2e [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 */
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]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000188 r = (pjsip_route_hdr*)
189 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000190 if (r == NULL) {
191 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
192 PJSIP_EINVALIDURI);
193 return PJSIP_EINVALIDURI;
194 }
195 pj_list_push_back(&acc->route_set, r);
196 }
197
198 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
199 pj_str_t hname = { "Route", 5};
200 pjsip_route_hdr *r;
201 pj_str_t tmp;
202
203 pj_strdup_with_null(pjsua_var.pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000204 r = (pjsip_route_hdr*)
205 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000206 if (r == NULL) {
207 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
208 PJ_EINVAL);
209 return PJ_EINVAL;
210 }
211 pj_list_push_back(&acc->route_set, r);
212 }
213
214
215 /* Concatenate credentials from account config and global config */
216 acc->cred_cnt = 0;
217 for (i=0; i<acc_cfg->cred_count; ++i) {
218 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
219 }
220 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
221 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
222 {
223 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
224 }
225
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000226 status = pjsua_pres_init_acc(acc_id);
227 if (status != PJ_SUCCESS)
228 return status;
229
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000230 /* Mark account as valid */
231 pjsua_var.acc[acc_id].valid = PJ_TRUE;
232
Benny Prijono093d3022006-09-24 00:07:11 +0000233 /* Insert account ID into account ID array, sorted by priority */
234 for (i=0; i<pjsua_var.acc_cnt; ++i) {
235 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
236 pjsua_var.acc[acc_id].cfg.priority)
237 {
238 break;
239 }
240 }
241 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
242 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000243
244 return PJ_SUCCESS;
245}
246
247
248/*
249 * Add a new account to pjsua.
250 */
251PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
252 pj_bool_t is_default,
253 pjsua_acc_id *p_acc_id)
254{
255 unsigned id;
256 pj_status_t status;
257
258 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
259 PJ_ETOOMANY);
260
261 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000262 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000263
264 PJSUA_LOCK();
265
266 /* Find empty account id. */
267 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
268 if (pjsua_var.acc[id].valid == PJ_FALSE)
269 break;
270 }
271
272 /* Expect to find a slot */
273 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
274 {PJSUA_UNLOCK(); return PJ_EBUG;});
275
276 /* Copy config */
277 copy_acc_config(pjsua_var.pool, &pjsua_var.acc[id].cfg, cfg);
278
279 /* Normalize registration timeout */
280 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
281 pjsua_var.acc[id].cfg.reg_timeout == 0)
282 {
283 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
284 }
285
286 status = initialize_acc(id);
287 if (status != PJ_SUCCESS) {
288 pjsua_perror(THIS_FILE, "Error adding account", status);
289 PJSUA_UNLOCK();
290 return status;
291 }
292
293 if (is_default)
294 pjsua_var.default_acc = id;
295
296 if (p_acc_id)
297 *p_acc_id = id;
298
299 pjsua_var.acc_cnt++;
300
301 PJSUA_UNLOCK();
302
303 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
304 (int)cfg->id.slen, cfg->id.ptr, id));
305
306 /* If accounts has registration enabled, start registration */
307 if (pjsua_var.acc[id].cfg.reg_uri.slen)
308 pjsua_acc_set_registration(id, PJ_TRUE);
309
310
311 return PJ_SUCCESS;
312}
313
314
315/*
316 * Add local account
317 */
318PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
319 pj_bool_t is_default,
320 pjsua_acc_id *p_acc_id)
321{
322 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000323 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000324 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000325
Benny Prijonoe93e2872006-06-28 16:46:49 +0000326 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000327 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
328 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000329
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000330 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000331 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000332
333 pjsua_acc_config_default(&cfg);
334
Benny Prijono093d3022006-09-24 00:07:11 +0000335 /* Lower the priority of local account */
336 --cfg.priority;
337
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000338 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000339 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
340 "<sip:%.*s:%d;transport=%s>",
341 (int)t->local_name.host.slen,
342 t->local_name.host.ptr,
343 t->local_name.port,
344 pjsip_transport_get_type_name(t->type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000345
346 cfg.id = pj_str(uri);
347
348 return pjsua_acc_add(&cfg, is_default, p_acc_id);
349}
350
351
352/*
353 * Delete account.
354 */
355PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
356{
Benny Prijono093d3022006-09-24 00:07:11 +0000357 unsigned i;
358
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000359 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
360 PJ_EINVAL);
361 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
362
363 PJSUA_LOCK();
364
365 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000366 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000367 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000368 if (pjsua_var.acc[acc_id].regc) {
369 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
370 }
Benny Prijono922933b2007-01-21 16:23:56 +0000371 pjsua_var.acc[acc_id].regc = NULL;
372 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000373
374 /* Delete server presence subscription */
375 pjsua_pres_delete_acc(acc_id);
376
377 /* Invalidate */
378 pjsua_var.acc[acc_id].valid = PJ_FALSE;
379
Benny Prijono093d3022006-09-24 00:07:11 +0000380 /* Remove from array */
381 for (i=0; i<pjsua_var.acc_cnt; ++i) {
382 if (pjsua_var.acc_ids[i] == acc_id)
383 break;
384 }
385 if (i != pjsua_var.acc_cnt) {
386 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
387 pjsua_var.acc_cnt, i);
388 --pjsua_var.acc_cnt;
389 }
390
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000391 /* Leave the calls intact, as I don't think calls need to
392 * access account once it's created
393 */
394
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395
396 PJSUA_UNLOCK();
397
398 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
399
400 return PJ_SUCCESS;
401}
402
403
404/*
405 * Modify account information.
406 */
407PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
408 const pjsua_acc_config *cfg)
409{
410 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000411 PJ_UNUSED_ARG(acc_id);
412 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 return PJ_EINVALIDOP;
414}
415
416
417/*
418 * Modify account's presence status to be advertised to remote/presence
419 * subscribers.
420 */
421PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
422 pj_bool_t is_online)
423{
424 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
425 PJ_EINVAL);
426 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
427
428 pjsua_var.acc[acc_id].online_status = is_online;
429 pjsua_pres_refresh();
430 return PJ_SUCCESS;
431}
432
433
434/*
435 * This callback is called by pjsip_regc when outgoing register
436 * request has completed.
437 */
438static void regc_cb(struct pjsip_regc_cbparam *param)
439{
440
Benny Prijonoa1e69682007-05-11 15:14:34 +0000441 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000442
443 PJSUA_LOCK();
444
445 /*
446 * Print registration status.
447 */
448 if (param->status!=PJ_SUCCESS) {
449 pjsua_perror(THIS_FILE, "SIP registration error",
450 param->status);
451 pjsip_regc_destroy(acc->regc);
452 acc->regc = NULL;
453
454 } else if (param->code < 0 || param->code >= 300) {
455 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
456 param->code,
457 (int)param->reason.slen, param->reason.ptr));
458 pjsip_regc_destroy(acc->regc);
459 acc->regc = NULL;
460
461 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
462
463 if (param->expiration < 1) {
464 pjsip_regc_destroy(acc->regc);
465 acc->regc = NULL;
466 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
467 pjsua_var.acc[acc->index].cfg.id.ptr));
468 } else {
469 PJ_LOG(3, (THIS_FILE,
470 "%s: registration success, status=%d (%.*s), "
471 "will re-register in %d seconds",
472 pjsua_var.acc[acc->index].cfg.id.ptr,
473 param->code,
474 (int)param->reason.slen, param->reason.ptr,
475 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000476
477 /* Send initial PUBLISH if it is enabled */
478 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
479 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000480 }
481
482 } else {
483 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
484 }
485
486 acc->reg_last_err = param->status;
487 acc->reg_last_code = param->code;
488
489 if (pjsua_var.ua_cfg.cb.on_reg_state)
490 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
491
492 PJSUA_UNLOCK();
493}
494
495
496/*
497 * Initialize client registration.
498 */
499static pj_status_t pjsua_regc_init(int acc_id)
500{
501 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000502 pj_str_t contact;
Benny Prijonodfb7d482006-10-18 20:35:14 +0000503 char contact_buf[1024];
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000504 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 pj_status_t status;
506
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000507 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 acc = &pjsua_var.acc[acc_id];
509
510 if (acc->cfg.reg_uri.slen == 0) {
511 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
512 return PJ_SUCCESS;
513 }
514
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000515 /* Destroy existing session, if any */
516 if (acc->regc) {
517 pjsip_regc_destroy(acc->regc);
518 acc->regc = NULL;
519 }
520
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000521 /* initialize SIP registration if registrar is configured */
522
523 status = pjsip_regc_create( pjsua_var.endpt,
524 acc, &regc_cb, &acc->regc);
525
526 if (status != PJ_SUCCESS) {
527 pjsua_perror(THIS_FILE, "Unable to create client registration",
528 status);
529 return status;
530 }
531
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000532 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
533 status = pjsua_acc_create_uac_contact( pool, &contact,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000534 acc_id, &acc->cfg.reg_uri);
535 if (status != PJ_SUCCESS) {
536 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
537 " for registration",
538 status);
539 return status;
540 }
541
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 status = pjsip_regc_init( acc->regc,
543 &acc->cfg.reg_uri,
544 &acc->cfg.id,
545 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000546 1, &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 acc->cfg.reg_timeout);
548 if (status != PJ_SUCCESS) {
549 pjsua_perror(THIS_FILE,
550 "Client registration initialization error",
551 status);
552 return status;
553 }
554
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000555 /* If account is locked to specific transport, then set transport to
556 * the client registration.
557 */
558 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
559 pjsip_tpselector tp_sel;
560
561 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
562 pjsip_regc_set_transport(acc->regc, &tp_sel);
563 }
564
565
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000566 /* Set credentials
567 */
568 if (acc->cred_cnt) {
569 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
570 }
571
572 /* Set route-set
573 */
574 if (!pj_list_empty(&acc->route_set)) {
575 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
576 }
577
Benny Prijono8fc6de02006-11-11 21:25:55 +0000578 /* Add other request headers. */
579 if (pjsua_var.ua_cfg.user_agent.slen) {
580 pjsip_hdr hdr_list;
581 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
582 pjsip_generic_string_hdr *h;
583
584 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
585 pj_list_init(&hdr_list);
586
587 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
588 &pjsua_var.ua_cfg.user_agent);
589 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
590
591 pjsip_regc_add_headers(acc->regc, &hdr_list);
592 }
593
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000594 return PJ_SUCCESS;
595}
596
597
598/*
599 * Update registration or perform unregistration.
600 */
601PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
602 pj_bool_t renew)
603{
604 pj_status_t status = 0;
605 pjsip_tx_data *tdata = 0;
606
607 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
608 PJ_EINVAL);
609 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
610
611 PJSUA_LOCK();
612
613 if (renew) {
614 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000615 status = pjsua_regc_init(acc_id);
616 if (status != PJ_SUCCESS) {
617 pjsua_perror(THIS_FILE, "Unable to create registration",
618 status);
619 goto on_return;
620 }
621 }
622 if (!pjsua_var.acc[acc_id].regc) {
623 status = PJ_EINVALIDOP;
624 goto on_return;
625 }
626
627 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
628 &tdata);
629
630 } else {
631 if (pjsua_var.acc[acc_id].regc == NULL) {
632 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
633 status = PJ_EINVALIDOP;
634 goto on_return;
635 }
636 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
637 }
638
Benny Prijono56315612006-07-18 14:39:40 +0000639 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +0000640 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000641 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +0000642 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000643
644 if (status != PJ_SUCCESS) {
645 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
646 status);
647 } else {
648 PJ_LOG(3,(THIS_FILE, "%s sent",
649 (renew? "Registration" : "Unregistration")));
650 }
651
652on_return:
653 PJSUA_UNLOCK();
654 return status;
655}
656
657
658/*
659 * Get account information.
660 */
661PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
662 pjsua_acc_info *info)
663{
664 pjsua_acc *acc = &pjsua_var.acc[acc_id];
665 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
666
667 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000668 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000669
Benny Prijonoac623b32006-07-03 15:19:31 +0000670 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000671
672 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
673 PJ_EINVAL);
674 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
675
676 PJSUA_LOCK();
677
678 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
679 PJSUA_UNLOCK();
680 return PJ_EINVALIDOP;
681 }
682
683 info->id = acc_id;
684 info->is_default = (pjsua_var.default_acc == acc_id);
685 info->acc_uri = acc_cfg->id;
686 info->has_registration = (acc->cfg.reg_uri.slen > 0);
687 info->online_status = acc->online_status;
688
689 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000690 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000691 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
692 info->status_text = pj_str(info->buf_);
693 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +0000694 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000695 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000696 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
697 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000698 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000699 info->status_text = pj_str("not registered");
700 }
701 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000702 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000703 info->status_text = pj_str("In Progress");
704 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000705 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000706 info->status_text = pj_str("does not register");
707 }
708
709 if (acc->regc) {
710 pjsip_regc_info regc_info;
711 pjsip_regc_get_info(acc->regc, &regc_info);
712 info->expires = regc_info.next_reg;
713 } else {
714 info->expires = -1;
715 }
716
717 PJSUA_UNLOCK();
718
719 return PJ_SUCCESS;
720
721}
722
723
724/*
725 * Enum accounts all account ids.
726 */
727PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
728 unsigned *count )
729{
730 unsigned i, c;
731
732 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
733
734 PJSUA_LOCK();
735
736 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
737 if (!pjsua_var.acc[i].valid)
738 continue;
739 ids[c] = i;
740 ++c;
741 }
742
743 *count = c;
744
745 PJSUA_UNLOCK();
746
747 return PJ_SUCCESS;
748}
749
750
751/*
752 * Enum accounts info.
753 */
754PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
755 unsigned *count )
756{
757 unsigned i, c;
758
759 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
760
761 PJSUA_LOCK();
762
763 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
764 if (!pjsua_var.acc[i].valid)
765 continue;
766
767 pjsua_acc_get_info(i, &info[c]);
768 ++c;
769 }
770
771 *count = c;
772
773 PJSUA_UNLOCK();
774
775 return PJ_SUCCESS;
776}
777
778
779/*
780 * This is an internal function to find the most appropriate account to
781 * used to reach to the specified URL.
782 */
783PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
784{
785 pj_str_t tmp;
786 pjsip_uri *uri;
787 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +0000788 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000789
790 PJSUA_LOCK();
791
792 PJ_TODO(dont_use_pjsua_pool);
793
794 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
795
796 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
797 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +0000798 PJSUA_UNLOCK();
799 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000800 }
801
802 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
803 !PJSIP_URI_SCHEME_IS_SIPS(uri))
804 {
805 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +0000806 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
807 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000808 continue;
Benny Prijono093d3022006-09-24 00:07:11 +0000809 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000810 break;
811 }
812
Benny Prijono093d3022006-09-24 00:07:11 +0000813 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000814 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +0000815 PJSUA_UNLOCK();
816 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000817 }
818
819 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +0000820 PJSUA_UNLOCK();
821 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000822 }
823
Benny Prijonoa1e69682007-05-11 15:14:34 +0000824 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000825
Benny Prijonob4a17c92006-07-10 14:40:21 +0000826 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +0000827 for (i=0; i<pjsua_var.acc_cnt; ++i) {
828 unsigned acc_id = pjsua_var.acc_ids[i];
829 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
830 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
831 {
832 PJSUA_UNLOCK();
833 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +0000834 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000835 }
836
Benny Prijonob4a17c92006-07-10 14:40:21 +0000837 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +0000838 for (i=0; i<pjsua_var.acc_cnt; ++i) {
839 unsigned acc_id = pjsua_var.acc_ids[i];
840 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
841 {
842 PJSUA_UNLOCK();
843 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000844 }
845 }
846
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000847
Benny Prijono093d3022006-09-24 00:07:11 +0000848 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000849 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +0000850 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000851}
852
853
854/*
855 * This is an internal function to find the most appropriate account to be
856 * used to handle incoming calls.
857 */
858PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
859{
860 pjsip_uri *uri;
861 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +0000862 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000863
864 uri = rdata->msg_info.to->uri;
865
866 /* Just return default account if To URI is not SIP: */
867 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
868 !PJSIP_URI_SCHEME_IS_SIPS(uri))
869 {
870 return pjsua_var.default_acc;
871 }
872
873
874 PJSUA_LOCK();
875
876 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
877
878 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +0000879 for (i=0; i < pjsua_var.acc_cnt; ++i) {
880 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000881 pjsua_acc *acc = &pjsua_var.acc[acc_id];
882
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000883 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +0000884 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000885 {
886 /* Match ! */
887 PJSUA_UNLOCK();
888 return acc_id;
889 }
890 }
891
Benny Prijono093d3022006-09-24 00:07:11 +0000892 /* No matching account, try match domain part only. */
893 for (i=0; i < pjsua_var.acc_cnt; ++i) {
894 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000895 pjsua_acc *acc = &pjsua_var.acc[acc_id];
896
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000897 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000898 /* Match ! */
899 PJSUA_UNLOCK();
900 return acc_id;
901 }
902 }
903
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000904 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +0000905 for (i=0; i < pjsua_var.acc_cnt; ++i) {
906 unsigned acc_id = pjsua_var.acc_ids[i];
907 pjsua_acc *acc = &pjsua_var.acc[acc_id];
908
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000909 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
910
911 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
912 pjsip_transport_type_e type;
913 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
914 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
915 type = PJSIP_TRANSPORT_UDP;
916
917 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
918 continue;
919 }
920
Benny Prijono093d3022006-09-24 00:07:11 +0000921 /* Match ! */
922 PJSUA_UNLOCK();
923 return acc_id;
924 }
925 }
926
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000927 /* Still no match, use default account */
928 PJSUA_UNLOCK();
929 return pjsua_var.default_acc;
930}
931
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000932
Benny Prijonofff245c2007-04-02 11:44:47 +0000933/*
934 * Create arbitrary requests for this account.
935 */
936PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
937 const pjsip_method *method,
938 const pj_str_t *target,
939 pjsip_tx_data **p_tdata)
940{
941 pjsip_tx_data *tdata;
942 pjsua_acc *acc;
943 pjsip_route_hdr *r;
944 pj_status_t status;
945
946 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
947 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
948
949 acc = &pjsua_var.acc[acc_id];
950
951 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
952 &acc->cfg.id, target,
953 NULL, NULL, -1, NULL, &tdata);
954 if (status != PJ_SUCCESS) {
955 pjsua_perror(THIS_FILE, "Unable to create request", status);
956 return status;
957 }
958
959 /* Copy routeset */
960 r = acc->route_set.next;
961 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000962 pjsip_msg_add_hdr(tdata->msg,
963 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +0000964 r = r->next;
965 }
966
967 /* Done */
968 *p_tdata = tdata;
969 return PJ_SUCCESS;
970}
971
972
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000973PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
974 pj_str_t *contact,
975 pjsua_acc_id acc_id,
976 const pj_str_t *suri)
977{
978 pjsua_acc *acc;
979 pjsip_sip_uri *sip_uri;
980 pj_status_t status;
981 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
982 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000983 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000984 unsigned flag;
985 int secure;
986 int local_port;
987
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000988 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000989 acc = &pjsua_var.acc[acc_id];
990
Benny Prijonof75eceb2007-03-23 19:09:54 +0000991 /* If force_contact is configured, then use use it */
992 if (acc->cfg.force_contact.slen) {
993 *contact = acc->cfg.force_contact;
994 return PJ_SUCCESS;
995 }
996
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000997 /* If route-set is configured for the account, then URI is the
998 * first entry of the route-set.
999 */
1000 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001001 sip_uri = (pjsip_sip_uri*)
1002 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001003 } else {
1004 pj_str_t tmp;
1005 pjsip_uri *uri;
1006
1007 pj_strdup_with_null(pool, &tmp, suri);
1008
1009 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1010 if (uri == NULL)
1011 return PJSIP_EINVALIDURI;
1012
1013 /* For non-SIP scheme, route set should be configured */
1014 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1015 return PJSIP_EINVALIDREQURI;
1016
Benny Prijono8c7a6172007-02-18 21:17:46 +00001017 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001018 }
1019
1020 /* Get transport type of the URI */
1021 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1022 tp_type = PJSIP_TRANSPORT_TLS;
1023 else if (sip_uri->transport_param.slen == 0) {
1024 tp_type = PJSIP_TRANSPORT_UDP;
1025 } else
1026 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1027
1028 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1029 return PJSIP_EUNSUPTRANSPORT;
1030
1031 flag = pjsip_transport_get_flag_from_type(tp_type);
1032 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1033
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001034 /* Init transport selector. */
1035 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1036
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001037 /* Get local address suitable to send request from */
1038 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001039 pool, tp_type, &tp_sel,
1040 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001041 if (status != PJ_SUCCESS)
1042 return status;
1043
1044 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001045 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001046 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1047 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1048 (int)acc->display.slen,
1049 acc->display.ptr,
1050 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001051 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001052 (int)acc->user_part.slen,
1053 acc->user_part.ptr,
1054 (acc->user_part.slen?"@":""),
1055 (int)local_addr.slen,
1056 local_addr.ptr,
1057 local_port,
1058 pjsip_transport_get_type_name(tp_type));
1059
1060 return PJ_SUCCESS;
1061}
1062
1063
1064
1065PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1066 pj_str_t *contact,
1067 pjsua_acc_id acc_id,
1068 pjsip_rx_data *rdata )
1069{
1070 /*
1071 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1072 * If the request that initiated the dialog contained a SIPS URI
1073 * in the Request-URI or in the top Record-Route header field value,
1074 * if there was any, or the Contact header field if there was no
1075 * Record-Route header field, the Contact header field in the response
1076 * MUST be a SIPS URI.
1077 */
1078 pjsua_acc *acc;
1079 pjsip_sip_uri *sip_uri;
1080 pj_status_t status;
1081 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1082 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001083 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001084 unsigned flag;
1085 int secure;
1086 int local_port;
1087
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001088 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001089 acc = &pjsua_var.acc[acc_id];
1090
Benny Prijonof75eceb2007-03-23 19:09:54 +00001091 /* If force_contact is configured, then use use it */
1092 if (acc->cfg.force_contact.slen) {
1093 *contact = acc->cfg.force_contact;
1094 return PJ_SUCCESS;
1095 }
1096
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001097 /* If Record-Route is present, then URI is the top Record-Route. */
1098 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001099 sip_uri = (pjsip_sip_uri*)
1100 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001101 } else {
1102 pjsip_contact_hdr *h_contact;
1103 pjsip_uri *uri = NULL;
1104
1105 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001106 h_contact = (pjsip_contact_hdr*)
1107 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001108 NULL);
1109 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001110 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001111
1112
1113 /* Or if Contact URI is not present, take the remote URI from
1114 * the From URI.
1115 */
1116 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001117 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001118
1119
1120 /* Can only do sip/sips scheme at present. */
1121 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1122 return PJSIP_EINVALIDREQURI;
1123
Benny Prijono8c7a6172007-02-18 21:17:46 +00001124 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001125 }
1126
1127 /* Get transport type of the URI */
1128 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1129 tp_type = PJSIP_TRANSPORT_TLS;
1130 else if (sip_uri->transport_param.slen == 0) {
1131 tp_type = PJSIP_TRANSPORT_UDP;
1132 } else
1133 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1134
1135 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1136 return PJSIP_EUNSUPTRANSPORT;
1137
1138 flag = pjsip_transport_get_flag_from_type(tp_type);
1139 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1140
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001141 /* Init transport selector. */
1142 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1143
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001144 /* Get local address suitable to send request from */
1145 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001146 pool, tp_type, &tp_sel,
1147 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001148 if (status != PJ_SUCCESS)
1149 return status;
1150
1151 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001152 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001153 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1154 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1155 (int)acc->display.slen,
1156 acc->display.ptr,
1157 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001158 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001159 (int)acc->user_part.slen,
1160 acc->user_part.ptr,
1161 (acc->user_part.slen?"@":""),
1162 (int)local_addr.slen,
1163 local_addr.ptr,
1164 local_port,
1165 pjsip_transport_get_type_name(tp_type));
1166
1167 return PJ_SUCCESS;
1168}
1169
1170
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001171PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1172 pjsua_transport_id tp_id)
1173{
1174 pjsua_acc *acc;
1175
1176 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1177 acc = &pjsua_var.acc[acc_id];
1178
Benny Prijonoa1e69682007-05-11 15:14:34 +00001179 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001180 PJ_EINVAL);
1181
1182 acc->cfg.transport_id = tp_id;
1183
1184 return PJ_SUCCESS;
1185}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001186