blob: 892d63c4e088b1cd4210abb415477c878ac308b2 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 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*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000111 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000112 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
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000139 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000140 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
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000190 pj_strdup_with_null(acc->pool, &tmp,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000192 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000193 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000194 if (r == NULL) {
195 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
196 PJSIP_EINVALIDURI);
197 return PJSIP_EINVALIDURI;
198 }
199 pj_list_push_back(&acc->route_set, r);
200 }
201
202 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
203 pj_str_t hname = { "Route", 5};
204 pjsip_route_hdr *r;
205 pj_str_t tmp;
206
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000207 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000208 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000209 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000210 if (r == NULL) {
211 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
212 PJ_EINVAL);
213 return PJ_EINVAL;
214 }
215 pj_list_push_back(&acc->route_set, r);
216 }
217
218
219 /* Concatenate credentials from account config and global config */
220 acc->cred_cnt = 0;
221 for (i=0; i<acc_cfg->cred_count; ++i) {
222 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
223 }
224 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
225 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
226 {
227 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
228 }
229
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000230 status = pjsua_pres_init_acc(acc_id);
231 if (status != PJ_SUCCESS)
232 return status;
233
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234 /* Mark account as valid */
235 pjsua_var.acc[acc_id].valid = PJ_TRUE;
236
Benny Prijono093d3022006-09-24 00:07:11 +0000237 /* Insert account ID into account ID array, sorted by priority */
238 for (i=0; i<pjsua_var.acc_cnt; ++i) {
239 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
240 pjsua_var.acc[acc_id].cfg.priority)
241 {
242 break;
243 }
244 }
245 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
246 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000247
248 return PJ_SUCCESS;
249}
250
251
252/*
253 * Add a new account to pjsua.
254 */
255PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
256 pj_bool_t is_default,
257 pjsua_acc_id *p_acc_id)
258{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000259 pjsua_acc *acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000260 unsigned id;
261 pj_status_t status;
262
263 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
264 PJ_ETOOMANY);
265
266 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000267 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000268
269 PJSUA_LOCK();
270
271 /* Find empty account id. */
272 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
273 if (pjsua_var.acc[id].valid == PJ_FALSE)
274 break;
275 }
276
277 /* Expect to find a slot */
278 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
279 {PJSUA_UNLOCK(); return PJ_EBUG;});
280
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000281 acc = &pjsua_var.acc[id];
282
283 /* Create pool for this account. */
284 if (acc->pool)
285 pj_pool_reset(acc->pool);
286 else
287 acc->pool = pjsua_pool_create("acc%p", 512, 256);
288
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000289 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000290 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000291
292 /* Normalize registration timeout */
293 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
294 pjsua_var.acc[id].cfg.reg_timeout == 0)
295 {
296 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
297 }
298
299 status = initialize_acc(id);
300 if (status != PJ_SUCCESS) {
301 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000302 pj_pool_release(acc->pool);
303 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000304 PJSUA_UNLOCK();
305 return status;
306 }
307
308 if (is_default)
309 pjsua_var.default_acc = id;
310
311 if (p_acc_id)
312 *p_acc_id = id;
313
314 pjsua_var.acc_cnt++;
315
316 PJSUA_UNLOCK();
317
318 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
319 (int)cfg->id.slen, cfg->id.ptr, id));
320
321 /* If accounts has registration enabled, start registration */
322 if (pjsua_var.acc[id].cfg.reg_uri.slen)
323 pjsua_acc_set_registration(id, PJ_TRUE);
324
325
326 return PJ_SUCCESS;
327}
328
329
330/*
331 * Add local account
332 */
333PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
334 pj_bool_t is_default,
335 pjsua_acc_id *p_acc_id)
336{
337 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000338 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000339 const char *beginquote, *endquote;
340 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000341 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000342
Benny Prijonoe93e2872006-06-28 16:46:49 +0000343 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000344 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
345 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000346
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000347 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000348 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000349
350 pjsua_acc_config_default(&cfg);
351
Benny Prijono093d3022006-09-24 00:07:11 +0000352 /* Lower the priority of local account */
353 --cfg.priority;
354
Benny Prijonod0bd4982007-12-02 15:40:52 +0000355 /* Enclose IPv6 address in square brackets */
356 if (t->type & PJSIP_TRANSPORT_IPV6) {
357 beginquote = "[";
358 endquote = "]";
359 } else {
360 beginquote = endquote = "";
361 }
362
363 /* Don't add transport parameter if it's UDP */
364 if ((t->type & PJSIP_TRANSPORT_UDP) == 0) {
365 pj_ansi_snprintf(transport_param, sizeof(transport_param),
366 ";transport=%s",
367 pjsip_transport_get_type_name(t->type));
368 } else {
369 transport_param[0] = '\0';
370 }
371
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000373 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000374 "<sip:%s%.*s%s:%d%s>",
375 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000376 (int)t->local_name.host.slen,
377 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000378 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000379 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000380 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381
382 cfg.id = pj_str(uri);
383
384 return pjsua_acc_add(&cfg, is_default, p_acc_id);
385}
386
387
388/*
389 * Delete account.
390 */
391PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
392{
Benny Prijono093d3022006-09-24 00:07:11 +0000393 unsigned i;
394
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
396 PJ_EINVAL);
397 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
398
399 PJSUA_LOCK();
400
401 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000402 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000403 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000404 if (pjsua_var.acc[acc_id].regc) {
405 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
406 }
Benny Prijono922933b2007-01-21 16:23:56 +0000407 pjsua_var.acc[acc_id].regc = NULL;
408 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000409
410 /* Delete server presence subscription */
411 pjsua_pres_delete_acc(acc_id);
412
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000413 /* Release account pool */
414 if (pjsua_var.acc[acc_id].pool) {
415 pj_pool_release(pjsua_var.acc[acc_id].pool);
416 pjsua_var.acc[acc_id].pool = NULL;
417 }
418
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000419 /* Invalidate */
420 pjsua_var.acc[acc_id].valid = PJ_FALSE;
421
Benny Prijono093d3022006-09-24 00:07:11 +0000422 /* Remove from array */
423 for (i=0; i<pjsua_var.acc_cnt; ++i) {
424 if (pjsua_var.acc_ids[i] == acc_id)
425 break;
426 }
427 if (i != pjsua_var.acc_cnt) {
428 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
429 pjsua_var.acc_cnt, i);
430 --pjsua_var.acc_cnt;
431 }
432
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000433 /* Leave the calls intact, as I don't think calls need to
434 * access account once it's created
435 */
436
Benny Prijonofb2b3652007-06-28 07:15:03 +0000437 /* Update default account */
438 if (pjsua_var.default_acc == acc_id)
439 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000440
441 PJSUA_UNLOCK();
442
443 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
444
445 return PJ_SUCCESS;
446}
447
448
449/*
450 * Modify account information.
451 */
452PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
453 const pjsua_acc_config *cfg)
454{
455 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000456 PJ_UNUSED_ARG(acc_id);
457 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000458 return PJ_EINVALIDOP;
459}
460
461
462/*
463 * Modify account's presence status to be advertised to remote/presence
464 * subscribers.
465 */
466PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
467 pj_bool_t is_online)
468{
469 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
470 PJ_EINVAL);
471 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
472
473 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000474 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
475 pjsua_pres_update_acc(acc_id, PJ_FALSE);
476 return PJ_SUCCESS;
477}
478
479
480/*
481 * Set online status with extended information
482 */
483PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
484 pj_bool_t is_online,
485 const pjrpid_element *pr)
486{
487 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
488 PJ_EINVAL);
489 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
490
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000491 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000492 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000493 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
494 PJSUA_UNLOCK();
495
Benny Prijono4461c7d2007-08-25 13:36:15 +0000496 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000497 return PJ_SUCCESS;
498}
499
500
Benny Prijono15b02302007-09-27 14:07:07 +0000501/* Update NAT address from the REGISTER response */
502static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
503 struct pjsip_regc_cbparam *param)
504{
505 pjsip_transport *tp;
506 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000507 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000508 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000509 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000510 pjsip_via_hdr *via;
511
512 tp = param->rdata->tp_info.transport;
513
514 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000515 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000516 return PJ_FALSE;
517
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000518#if 0
519 // Always update
520 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000521
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000522 /* For UDP, only update if STUN is enabled (for now).
523 * For TCP/TLS, always check.
524 */
525 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
526 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
527 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
528 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
529 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000530 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000531 /* Yes we will check */
532 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000533 return PJ_FALSE;
534 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000535#endif
Benny Prijono15b02302007-09-27 14:07:07 +0000536
537 /* Get the received and rport info */
538 via = param->rdata->msg_info.via;
539 if (via->rport_param < 1) {
540 /* Remote doesn't support rport */
541 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +0000542 if (rport==0) {
543 pjsip_transport_type_e tp_type;
544 tp_type = (pjsip_transport_type_e) tp->key.type;
545 rport = pjsip_transport_get_default_port_for_type(tp_type);
546 }
Benny Prijono15b02302007-09-27 14:07:07 +0000547 } else
548 rport = via->rport_param;
549
550 if (via->recvd_param.slen != 0)
551 via_addr = &via->recvd_param;
552 else
553 via_addr = &via->sent_by.host;
554
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000555 /* Compare received and rport with the URI in our registration */
556 pool = pjsua_pool_create("tmp", 512, 512);
557 uri = (pjsip_sip_uri*)
558 pjsip_parse_uri(pool, acc->contact.ptr, acc->contact.slen, 0);
559 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +0000560 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000561
Benny Prijono24a21852008-04-14 04:04:30 +0000562 if (uri->port == 0) {
563 pjsip_transport_type_e tp_type;
564 tp_type = (pjsip_transport_type_e) tp->key.type;
565 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
566 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000567
568 if (uri->port == rport &&
569 pj_stricmp(&uri->host, via_addr)==0)
Benny Prijono15b02302007-09-27 14:07:07 +0000570 {
571 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000572 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000573 return PJ_FALSE;
574 }
575
576 /* At this point we've detected that the address as seen by registrar.
577 * has changed.
578 */
579 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
580 "(%.*s:%d --> %.*s:%d). Updating registration..",
581 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000582 (int)uri->host.slen,
583 uri->host.ptr,
584 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000585 (int)via_addr->slen,
586 via_addr->ptr,
587 rport));
588
589 /* Unregister current contact */
590 pjsua_acc_set_registration(acc->index, PJ_FALSE);
591 if (acc->regc != NULL) {
592 pjsip_regc_destroy(acc->regc);
593 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000594 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +0000595 }
596
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000597 /* Update account's Contact header */
598 {
599 char *tmp;
600 int len;
601
Benny Prijono24a21852008-04-14 04:04:30 +0000602 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000603 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijono83088f32008-04-22 18:33:55 +0000604 "<sip:%.*s%s%.*s:%d;transport=%s>",
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000605 (int)acc->user_part.slen,
606 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +0000607 (acc->user_part.slen? "@" : ""),
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000608 (int)via_addr->slen,
609 via_addr->ptr,
610 rport,
611 tp->type_name);
612 if (len < 1) {
613 PJ_LOG(1,(THIS_FILE, "URI too long"));
614 pj_pool_release(pool);
615 return PJ_FALSE;
616 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +0000617 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000618 }
619
620 /* For UDP transport, if STUN is enabled then update the transport's
621 * published name as well.
622 */
623 if (tp->key.type==PJSIP_TRANSPORT_UDP &&
624 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
625 pjsua_var.ua_cfg.stun_host.slen != 0))
626 {
627 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
628 tp->local_name.port = rport;
629 }
Benny Prijono15b02302007-09-27 14:07:07 +0000630
631 /* Perform new registration */
632 pjsua_acc_set_registration(acc->index, PJ_TRUE);
633
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000634 pj_pool_release(pool);
635
Benny Prijono15b02302007-09-27 14:07:07 +0000636 return PJ_TRUE;
637}
638
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000639/* Check and update Service-Route header */
640void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
641{
642 pjsip_generic_string_hdr *hsr = NULL;
643 pjsip_route_hdr *hr, *h;
644 const pj_str_t HNAME = { "Service-Route", 13 };
645 const pj_str_t HROUTE = { "Route", 5 };
646 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000647 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000648
649 /* Find and parse Service-Route headers */
650 for (;;) {
651 char saved;
652 int parsed_len;
653
654 /* Find Service-Route header */
655 hsr = (pjsip_generic_string_hdr*)
656 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
657 if (!hsr)
658 break;
659
660 /* Parse as Route header since the syntax is similar. This may
661 * return more than one headers.
662 */
663 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
664 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
665 hr = (pjsip_route_hdr*)
666 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
667 hsr->hvalue.slen, &parsed_len);
668 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
669
670 if (hr == NULL) {
671 /* Error */
672 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
673 return;
674 }
675
676 /* Save each URI in the result */
677 h = hr;
678 do {
679 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
680 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
681 {
682 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
683 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
684 return;
685 }
686
687 uri[uri_cnt++] = h->name_addr.uri;
688 h = h->next;
689 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
690
691 if (h != hr) {
692 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
693 return;
694 }
695
696 /* Prepare to find next Service-Route header */
697 hsr = hsr->next;
698 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
699 break;
700 }
701
702 if (uri_cnt == 0)
703 return;
704
705 /*
706 * Update account's route set
707 */
708
709 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000710 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000711 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
712 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
713 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000714 i<rcnt;
715 ++i)
716 {
717 pjsip_route_hdr *prev = hr->prev;
718 pj_list_erase(hr);
719 hr = prev;
720 }
721 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000722
723 /* Then append the Service-Route URIs */
724 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000725 hr = pjsip_route_hdr_create(acc->pool);
726 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000727 pj_list_push_back(&acc->route_set, hr);
728 }
729
730 /* Done */
731
732 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
733 acc->index, uri_cnt));
734}
735
Benny Prijonobddef2c2007-10-31 13:28:08 +0000736
737/* Keep alive timer callback */
738static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
739{
740 pjsua_acc *acc;
741 pjsip_tpselector tp_sel;
742 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000743 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000744 pj_status_t status;
745
746 PJ_UNUSED_ARG(th);
747
748 PJSUA_LOCK();
749
750 te->id = PJ_FALSE;
751
752 acc = (pjsua_acc*) te->user_data;
753
754 /* Select the transport to send the packet */
755 pj_bzero(&tp_sel, sizeof(tp_sel));
756 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
757 tp_sel.u.transport = acc->ka_transport;
758
759 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000760 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000761 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000762 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000763
764 /* Send raw packet */
765 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
766 PJSIP_TRANSPORT_UDP, &tp_sel,
767 NULL, acc->cfg.ka_data.ptr,
768 acc->cfg.ka_data.slen,
769 &acc->ka_target, acc->ka_target_len,
770 NULL, NULL);
771
772 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
773 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
774 }
775
776 /* Reschedule next timer */
777 delay.sec = acc->cfg.ka_interval;
778 delay.msec = 0;
779 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
780 if (status == PJ_SUCCESS) {
781 te->id = PJ_TRUE;
782 } else {
783 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
784 }
785
786 PJSUA_UNLOCK();
787}
788
789
790/* Update keep-alive for the account */
791static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
792 struct pjsip_regc_cbparam *param)
793{
794 /* In all cases, stop keep-alive timer if it's running. */
795 if (acc->ka_timer.id) {
796 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
797 acc->ka_timer.id = PJ_FALSE;
798
799 pjsip_transport_dec_ref(acc->ka_transport);
800 acc->ka_transport = NULL;
801 }
802
803 if (start) {
804 pj_time_val delay;
805 pj_status_t status;
806
807 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000808 * - ka_interval is not zero in the account, and
809 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000810 *
811 * Previously we only enabled keep-alive when STUN is enabled, since
812 * we thought that keep-alive is only needed in Internet situation.
813 * But it has been discovered that Windows Firewall on WinXP also
814 * needs to be kept-alive, otherwise incoming packets will be dropped.
815 * So because of this, now keep-alive is always enabled for UDP,
816 * regardless of whether STUN is enabled or not.
817 *
818 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
819 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000820 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000821 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000822 acc->cfg.ka_interval == 0 ||
823 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
824 {
825 /* Keep alive is not necessary */
826 return;
827 }
828
829 /* Save transport and destination address. */
830 acc->ka_transport = param->rdata->tp_info.transport;
831 pjsip_transport_add_ref(acc->ka_transport);
832 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
833 param->rdata->pkt_info.src_addr_len);
834 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
835
836 /* Setup and start the timer */
837 acc->ka_timer.cb = &keep_alive_timer_cb;
838 acc->ka_timer.user_data = (void*)acc;
839
840 delay.sec = acc->cfg.ka_interval;
841 delay.msec = 0;
842 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
843 &delay);
844 if (status == PJ_SUCCESS) {
845 acc->ka_timer.id = PJ_TRUE;
846 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
847 "destination:%s:%d, interval:%ds",
848 acc->index,
849 param->rdata->pkt_info.src_name,
850 param->rdata->pkt_info.src_port,
851 acc->cfg.ka_interval));
852 } else {
853 acc->ka_timer.id = PJ_FALSE;
854 pjsip_transport_dec_ref(acc->ka_transport);
855 acc->ka_transport = NULL;
856 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
857 }
858 }
859}
860
861
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000862/*
863 * This callback is called by pjsip_regc when outgoing register
864 * request has completed.
865 */
866static void regc_cb(struct pjsip_regc_cbparam *param)
867{
868
Benny Prijonoa1e69682007-05-11 15:14:34 +0000869 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000870
Benny Prijono15b02302007-09-27 14:07:07 +0000871 if (param->regc != acc->regc)
872 return;
873
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000874 PJSUA_LOCK();
875
876 /*
877 * Print registration status.
878 */
879 if (param->status!=PJ_SUCCESS) {
880 pjsua_perror(THIS_FILE, "SIP registration error",
881 param->status);
882 pjsip_regc_destroy(acc->regc);
883 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000884 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000885
Benny Prijonobddef2c2007-10-31 13:28:08 +0000886 /* Stop keep-alive timer if any. */
887 update_keep_alive(acc, PJ_FALSE, NULL);
888
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 } else if (param->code < 0 || param->code >= 300) {
890 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
891 param->code,
892 (int)param->reason.slen, param->reason.ptr));
893 pjsip_regc_destroy(acc->regc);
894 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000895 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000896
Benny Prijonobddef2c2007-10-31 13:28:08 +0000897 /* Stop keep-alive timer if any. */
898 update_keep_alive(acc, PJ_FALSE, NULL);
899
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000900 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
901
902 if (param->expiration < 1) {
903 pjsip_regc_destroy(acc->regc);
904 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000905 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000906
907 /* Stop keep-alive timer if any. */
908 update_keep_alive(acc, PJ_FALSE, NULL);
909
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000910 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
911 pjsua_var.acc[acc->index].cfg.id.ptr));
912 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000913 /* Check NAT bound address */
914 if (acc_check_nat_addr(acc, param)) {
915 /* Update address, don't notify application yet */
916 PJSUA_UNLOCK();
917 return;
918 }
919
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000920 /* Check and update Service-Route header */
921 update_service_route(acc, param->rdata);
922
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000923 PJ_LOG(3, (THIS_FILE,
924 "%s: registration success, status=%d (%.*s), "
925 "will re-register in %d seconds",
926 pjsua_var.acc[acc->index].cfg.id.ptr,
927 param->code,
928 (int)param->reason.slen, param->reason.ptr,
929 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000930
Benny Prijonobddef2c2007-10-31 13:28:08 +0000931 /* Start keep-alive timer if necessary. */
932 update_keep_alive(acc, PJ_TRUE, param);
933
Benny Prijono8b6834f2007-02-24 13:29:22 +0000934 /* Send initial PUBLISH if it is enabled */
935 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
936 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000937 }
938
939 } else {
940 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
941 }
942
943 acc->reg_last_err = param->status;
944 acc->reg_last_code = param->code;
945
946 if (pjsua_var.ua_cfg.cb.on_reg_state)
947 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
948
949 PJSUA_UNLOCK();
950}
951
952
953/*
954 * Initialize client registration.
955 */
956static pj_status_t pjsua_regc_init(int acc_id)
957{
958 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000959 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000960 pj_status_t status;
961
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000962 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000963 acc = &pjsua_var.acc[acc_id];
964
965 if (acc->cfg.reg_uri.slen == 0) {
966 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
967 return PJ_SUCCESS;
968 }
969
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000970 /* Destroy existing session, if any */
971 if (acc->regc) {
972 pjsip_regc_destroy(acc->regc);
973 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000974 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000975 }
976
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000977 /* initialize SIP registration if registrar is configured */
978
979 status = pjsip_regc_create( pjsua_var.endpt,
980 acc, &regc_cb, &acc->regc);
981
982 if (status != PJ_SUCCESS) {
983 pjsua_perror(THIS_FILE, "Unable to create client registration",
984 status);
985 return status;
986 }
987
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000988 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000989
990 if (acc->contact.slen == 0) {
991 pj_str_t tmp_contact;
992
993 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
994 acc_id, &acc->cfg.reg_uri);
995 if (status != PJ_SUCCESS) {
996 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
997 " for registration",
998 status);
999 pjsip_regc_destroy(acc->regc);
1000 pj_pool_release(pool);
1001 acc->regc = NULL;
1002 return status;
1003 }
1004
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001005 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001006 }
1007
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001008 status = pjsip_regc_init( acc->regc,
1009 &acc->cfg.reg_uri,
1010 &acc->cfg.id,
1011 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001012 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001013 acc->cfg.reg_timeout);
1014 if (status != PJ_SUCCESS) {
1015 pjsua_perror(THIS_FILE,
1016 "Client registration initialization error",
1017 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001018 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001019 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001020 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001021 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001022 return status;
1023 }
1024
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001025 /* If account is locked to specific transport, then set transport to
1026 * the client registration.
1027 */
1028 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1029 pjsip_tpselector tp_sel;
1030
1031 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1032 pjsip_regc_set_transport(acc->regc, &tp_sel);
1033 }
1034
1035
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001036 /* Set credentials
1037 */
1038 if (acc->cred_cnt) {
1039 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1040 }
1041
Benny Prijono48ab2b72007-11-08 09:24:30 +00001042 /* Set authentication preference */
1043 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1044
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 /* Set route-set
1046 */
1047 if (!pj_list_empty(&acc->route_set)) {
1048 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1049 }
1050
Benny Prijono8fc6de02006-11-11 21:25:55 +00001051 /* Add other request headers. */
1052 if (pjsua_var.ua_cfg.user_agent.slen) {
1053 pjsip_hdr hdr_list;
1054 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1055 pjsip_generic_string_hdr *h;
1056
Benny Prijono8fc6de02006-11-11 21:25:55 +00001057 pj_list_init(&hdr_list);
1058
1059 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1060 &pjsua_var.ua_cfg.user_agent);
1061 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1062
1063 pjsip_regc_add_headers(acc->regc, &hdr_list);
1064 }
1065
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001066 pj_pool_release(pool);
1067
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001068 return PJ_SUCCESS;
1069}
1070
1071
1072/*
1073 * Update registration or perform unregistration.
1074 */
1075PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1076 pj_bool_t renew)
1077{
1078 pj_status_t status = 0;
1079 pjsip_tx_data *tdata = 0;
1080
1081 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1082 PJ_EINVAL);
1083 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1084
1085 PJSUA_LOCK();
1086
1087 if (renew) {
1088 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089 status = pjsua_regc_init(acc_id);
1090 if (status != PJ_SUCCESS) {
1091 pjsua_perror(THIS_FILE, "Unable to create registration",
1092 status);
1093 goto on_return;
1094 }
1095 }
1096 if (!pjsua_var.acc[acc_id].regc) {
1097 status = PJ_EINVALIDOP;
1098 goto on_return;
1099 }
1100
1101 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1102 &tdata);
1103
Benny Prijono28f673a2007-10-15 07:04:59 +00001104 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1105 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1106 pjsip_authorization_hdr *h;
1107 char *uri;
1108 int d;
1109
1110 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1111 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1112 uri, acc->cfg.reg_uri.slen+10);
1113 pj_assert(d > 0);
1114
1115 h = pjsip_authorization_hdr_create(tdata->pool);
1116 h->scheme = pj_str("Digest");
1117 h->credential.digest.username = acc->cred[0].username;
1118 h->credential.digest.realm = acc->srv_domain;
1119 h->credential.digest.uri = pj_str(uri);
1120 h->credential.digest.algorithm = pj_str("md5");
1121
1122 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1123 }
1124
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001125 } else {
1126 if (pjsua_var.acc[acc_id].regc == NULL) {
1127 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1128 status = PJ_EINVALIDOP;
1129 goto on_return;
1130 }
1131 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1132 }
1133
Benny Prijono56315612006-07-18 14:39:40 +00001134 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001135 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001136 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001137 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001138
1139 if (status != PJ_SUCCESS) {
1140 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1141 status);
1142 } else {
1143 PJ_LOG(3,(THIS_FILE, "%s sent",
1144 (renew? "Registration" : "Unregistration")));
1145 }
1146
1147on_return:
1148 PJSUA_UNLOCK();
1149 return status;
1150}
1151
1152
1153/*
1154 * Get account information.
1155 */
1156PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1157 pjsua_acc_info *info)
1158{
1159 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1160 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1161
1162 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001163 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001164
Benny Prijonoac623b32006-07-03 15:19:31 +00001165 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001166
1167 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1168 PJ_EINVAL);
1169 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1170
1171 PJSUA_LOCK();
1172
1173 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1174 PJSUA_UNLOCK();
1175 return PJ_EINVALIDOP;
1176 }
1177
1178 info->id = acc_id;
1179 info->is_default = (pjsua_var.default_acc == acc_id);
1180 info->acc_uri = acc_cfg->id;
1181 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1182 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001183 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1184 if (info->rpid.note.slen)
1185 info->online_status_text = info->rpid.note;
1186 else if (info->online_status)
1187 info->online_status_text = pj_str("Online");
1188 else
1189 info->online_status_text = pj_str("Offline");
1190
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001191 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001192 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001193 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1194 info->status_text = pj_str(info->buf_);
1195 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001196 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001197 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001198 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1199 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001200 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001201 info->status_text = pj_str("not registered");
1202 }
1203 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001204 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001205 info->status_text = pj_str("In Progress");
1206 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001207 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001208 info->status_text = pj_str("does not register");
1209 }
1210
1211 if (acc->regc) {
1212 pjsip_regc_info regc_info;
1213 pjsip_regc_get_info(acc->regc, &regc_info);
1214 info->expires = regc_info.next_reg;
1215 } else {
1216 info->expires = -1;
1217 }
1218
1219 PJSUA_UNLOCK();
1220
1221 return PJ_SUCCESS;
1222
1223}
1224
1225
1226/*
1227 * Enum accounts all account ids.
1228 */
1229PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1230 unsigned *count )
1231{
1232 unsigned i, c;
1233
1234 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1235
1236 PJSUA_LOCK();
1237
1238 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1239 if (!pjsua_var.acc[i].valid)
1240 continue;
1241 ids[c] = i;
1242 ++c;
1243 }
1244
1245 *count = c;
1246
1247 PJSUA_UNLOCK();
1248
1249 return PJ_SUCCESS;
1250}
1251
1252
1253/*
1254 * Enum accounts info.
1255 */
1256PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1257 unsigned *count )
1258{
1259 unsigned i, c;
1260
1261 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1262
1263 PJSUA_LOCK();
1264
1265 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1266 if (!pjsua_var.acc[i].valid)
1267 continue;
1268
1269 pjsua_acc_get_info(i, &info[c]);
1270 ++c;
1271 }
1272
1273 *count = c;
1274
1275 PJSUA_UNLOCK();
1276
1277 return PJ_SUCCESS;
1278}
1279
1280
1281/*
1282 * This is an internal function to find the most appropriate account to
1283 * used to reach to the specified URL.
1284 */
1285PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1286{
1287 pj_str_t tmp;
1288 pjsip_uri *uri;
1289 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001290 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001291 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001292
1293 PJSUA_LOCK();
1294
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001295 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001296
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001297 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001298
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001299 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001300 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001301 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001302 PJSUA_UNLOCK();
1303 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001304 }
1305
1306 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1307 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1308 {
1309 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001310 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1311 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001312 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001313 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001314 break;
1315 }
1316
Benny Prijono093d3022006-09-24 00:07:11 +00001317 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001318 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001319 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001320 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001321 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001322 }
1323
1324 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001325 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001326 PJSUA_UNLOCK();
1327 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001328 }
1329
Benny Prijonoa1e69682007-05-11 15:14:34 +00001330 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001331
Benny Prijonob4a17c92006-07-10 14:40:21 +00001332 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001333 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1334 unsigned acc_id = pjsua_var.acc_ids[i];
1335 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1336 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1337 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001338 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001339 PJSUA_UNLOCK();
1340 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001341 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001342 }
1343
Benny Prijonob4a17c92006-07-10 14:40:21 +00001344 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001345 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1346 unsigned acc_id = pjsua_var.acc_ids[i];
1347 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1348 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001349 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001350 PJSUA_UNLOCK();
1351 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001352 }
1353 }
1354
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001355
Benny Prijono093d3022006-09-24 00:07:11 +00001356 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001357 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001358 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001359 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001360}
1361
1362
1363/*
1364 * This is an internal function to find the most appropriate account to be
1365 * used to handle incoming calls.
1366 */
1367PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1368{
1369 pjsip_uri *uri;
1370 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001371 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001372
Benny Prijono371cf0a2007-06-19 00:35:37 +00001373 /* Check that there's at least one account configured */
1374 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1375
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001376 uri = rdata->msg_info.to->uri;
1377
1378 /* Just return default account if To URI is not SIP: */
1379 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1380 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1381 {
1382 return pjsua_var.default_acc;
1383 }
1384
1385
1386 PJSUA_LOCK();
1387
1388 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1389
1390 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001391 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1392 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001393 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1394
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001395 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001396 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001397 {
1398 /* Match ! */
1399 PJSUA_UNLOCK();
1400 return acc_id;
1401 }
1402 }
1403
Benny Prijono093d3022006-09-24 00:07:11 +00001404 /* No matching account, try match domain part only. */
1405 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1406 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001407 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1408
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001409 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001410 /* Match ! */
1411 PJSUA_UNLOCK();
1412 return acc_id;
1413 }
1414 }
1415
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001416 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001417 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1418 unsigned acc_id = pjsua_var.acc_ids[i];
1419 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1420
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001421 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1422
1423 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1424 pjsip_transport_type_e type;
1425 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1426 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1427 type = PJSIP_TRANSPORT_UDP;
1428
1429 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1430 continue;
1431 }
1432
Benny Prijono093d3022006-09-24 00:07:11 +00001433 /* Match ! */
1434 PJSUA_UNLOCK();
1435 return acc_id;
1436 }
1437 }
1438
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001439 /* Still no match, use default account */
1440 PJSUA_UNLOCK();
1441 return pjsua_var.default_acc;
1442}
1443
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001444
Benny Prijonofff245c2007-04-02 11:44:47 +00001445/*
1446 * Create arbitrary requests for this account.
1447 */
1448PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1449 const pjsip_method *method,
1450 const pj_str_t *target,
1451 pjsip_tx_data **p_tdata)
1452{
1453 pjsip_tx_data *tdata;
1454 pjsua_acc *acc;
1455 pjsip_route_hdr *r;
1456 pj_status_t status;
1457
1458 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1459 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1460
1461 acc = &pjsua_var.acc[acc_id];
1462
1463 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1464 &acc->cfg.id, target,
1465 NULL, NULL, -1, NULL, &tdata);
1466 if (status != PJ_SUCCESS) {
1467 pjsua_perror(THIS_FILE, "Unable to create request", status);
1468 return status;
1469 }
1470
1471 /* Copy routeset */
1472 r = acc->route_set.next;
1473 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001474 pjsip_msg_add_hdr(tdata->msg,
1475 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001476 r = r->next;
1477 }
1478
1479 /* Done */
1480 *p_tdata = tdata;
1481 return PJ_SUCCESS;
1482}
1483
1484
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001485PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1486 pj_str_t *contact,
1487 pjsua_acc_id acc_id,
1488 const pj_str_t *suri)
1489{
1490 pjsua_acc *acc;
1491 pjsip_sip_uri *sip_uri;
1492 pj_status_t status;
1493 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1494 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001495 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001496 unsigned flag;
1497 int secure;
1498 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001499 const char *beginquote, *endquote;
1500 char transport_param[32];
1501
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001502
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001503 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001504 acc = &pjsua_var.acc[acc_id];
1505
Benny Prijonof75eceb2007-03-23 19:09:54 +00001506 /* If force_contact is configured, then use use it */
1507 if (acc->cfg.force_contact.slen) {
1508 *contact = acc->cfg.force_contact;
1509 return PJ_SUCCESS;
1510 }
1511
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001512 /* If route-set is configured for the account, then URI is the
1513 * first entry of the route-set.
1514 */
1515 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001516 sip_uri = (pjsip_sip_uri*)
1517 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001518 } else {
1519 pj_str_t tmp;
1520 pjsip_uri *uri;
1521
1522 pj_strdup_with_null(pool, &tmp, suri);
1523
1524 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1525 if (uri == NULL)
1526 return PJSIP_EINVALIDURI;
1527
1528 /* For non-SIP scheme, route set should be configured */
1529 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1530 return PJSIP_EINVALIDREQURI;
1531
Benny Prijono8c7a6172007-02-18 21:17:46 +00001532 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001533 }
1534
1535 /* Get transport type of the URI */
1536 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1537 tp_type = PJSIP_TRANSPORT_TLS;
1538 else if (sip_uri->transport_param.slen == 0) {
1539 tp_type = PJSIP_TRANSPORT_UDP;
1540 } else
1541 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1542
1543 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1544 return PJSIP_EUNSUPTRANSPORT;
1545
Benny Prijonod0bd4982007-12-02 15:40:52 +00001546 /* If destination URI specifies IPv6, then set transport type
1547 * to use IPv6 as well.
1548 */
Benny Prijono19b29372007-12-05 04:08:40 +00001549 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001550 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1551
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001552 flag = pjsip_transport_get_flag_from_type(tp_type);
1553 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1554
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001555 /* Init transport selector. */
1556 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1557
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001558 /* Get local address suitable to send request from */
1559 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001560 pool, tp_type, &tp_sel,
1561 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001562 if (status != PJ_SUCCESS)
1563 return status;
1564
Benny Prijonod0bd4982007-12-02 15:40:52 +00001565 /* Enclose IPv6 address in square brackets */
1566 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1567 beginquote = "[";
1568 endquote = "]";
1569 } else {
1570 beginquote = endquote = "";
1571 }
1572
1573 /* Don't add transport parameter if it's UDP */
1574 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1575 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1576 ";transport=%s",
1577 pjsip_transport_get_type_name(tp_type));
1578 } else {
1579 transport_param[0] = '\0';
1580 }
1581
1582
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001583 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001584 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001585 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001586 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001587 (int)acc->display.slen,
1588 acc->display.ptr,
1589 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001590 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001591 (int)acc->user_part.slen,
1592 acc->user_part.ptr,
1593 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001594 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001595 (int)local_addr.slen,
1596 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001597 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001598 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001599 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001600
1601 return PJ_SUCCESS;
1602}
1603
1604
1605
1606PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1607 pj_str_t *contact,
1608 pjsua_acc_id acc_id,
1609 pjsip_rx_data *rdata )
1610{
1611 /*
1612 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1613 * If the request that initiated the dialog contained a SIPS URI
1614 * in the Request-URI or in the top Record-Route header field value,
1615 * if there was any, or the Contact header field if there was no
1616 * Record-Route header field, the Contact header field in the response
1617 * MUST be a SIPS URI.
1618 */
1619 pjsua_acc *acc;
1620 pjsip_sip_uri *sip_uri;
1621 pj_status_t status;
1622 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1623 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001624 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001625 unsigned flag;
1626 int secure;
1627 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001628 const char *beginquote, *endquote;
1629 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001630
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001631 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001632 acc = &pjsua_var.acc[acc_id];
1633
Benny Prijonof75eceb2007-03-23 19:09:54 +00001634 /* If force_contact is configured, then use use it */
1635 if (acc->cfg.force_contact.slen) {
1636 *contact = acc->cfg.force_contact;
1637 return PJ_SUCCESS;
1638 }
1639
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001640 /* If Record-Route is present, then URI is the top Record-Route. */
1641 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001642 sip_uri = (pjsip_sip_uri*)
1643 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001644 } else {
1645 pjsip_contact_hdr *h_contact;
1646 pjsip_uri *uri = NULL;
1647
1648 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001649 h_contact = (pjsip_contact_hdr*)
1650 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001651 NULL);
1652 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001653 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001654
1655
1656 /* Or if Contact URI is not present, take the remote URI from
1657 * the From URI.
1658 */
1659 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001660 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001661
1662
1663 /* Can only do sip/sips scheme at present. */
1664 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1665 return PJSIP_EINVALIDREQURI;
1666
Benny Prijono8c7a6172007-02-18 21:17:46 +00001667 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001668 }
1669
1670 /* Get transport type of the URI */
1671 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1672 tp_type = PJSIP_TRANSPORT_TLS;
1673 else if (sip_uri->transport_param.slen == 0) {
1674 tp_type = PJSIP_TRANSPORT_UDP;
1675 } else
1676 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001677
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001678 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1679 return PJSIP_EUNSUPTRANSPORT;
1680
Benny Prijonod0bd4982007-12-02 15:40:52 +00001681 /* If destination URI specifies IPv6, then set transport type
1682 * to use IPv6 as well.
1683 */
1684 if (pj_strchr(&sip_uri->host, ':'))
1685 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1686
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001687 flag = pjsip_transport_get_flag_from_type(tp_type);
1688 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1689
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001690 /* Init transport selector. */
1691 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1692
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001693 /* Get local address suitable to send request from */
1694 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001695 pool, tp_type, &tp_sel,
1696 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001697 if (status != PJ_SUCCESS)
1698 return status;
1699
Benny Prijonod0bd4982007-12-02 15:40:52 +00001700 /* Enclose IPv6 address in square brackets */
1701 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1702 beginquote = "[";
1703 endquote = "]";
1704 } else {
1705 beginquote = endquote = "";
1706 }
1707
1708 /* Don't add transport parameter if it's UDP */
1709 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1710 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1711 ";transport=%s",
1712 pjsip_transport_get_type_name(tp_type));
1713 } else {
1714 transport_param[0] = '\0';
1715 }
1716
1717
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001718 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001719 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001720 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001721 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001722 (int)acc->display.slen,
1723 acc->display.ptr,
1724 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001725 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001726 (int)acc->user_part.slen,
1727 acc->user_part.ptr,
1728 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001729 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001730 (int)local_addr.slen,
1731 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001732 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001733 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001734 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001735
1736 return PJ_SUCCESS;
1737}
1738
1739
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001740PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1741 pjsua_transport_id tp_id)
1742{
1743 pjsua_acc *acc;
1744
1745 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1746 acc = &pjsua_var.acc[acc_id];
1747
Benny Prijonoa1e69682007-05-11 15:14:34 +00001748 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001749 PJ_EINVAL);
1750
1751 acc->cfg.transport_id = tp_id;
1752
1753 return PJ_SUCCESS;
1754}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001755