blob: f2853fa6280e0d60403bf7af3494b7c4ae64ee37 [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
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000162 /* Save the user and domain part. These will be used when finding an
163 * account for incoming requests.
164 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000165 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000166 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000167 acc->srv_domain = sip_uri->host;
168 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000169
Benny Prijonob4a17c92006-07-10 14:40:21 +0000170 if (sip_reg_uri) {
171 acc->srv_port = sip_reg_uri->port;
172 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173
174 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000175 //if (acc_cfg->contact.slen == 0) {
176 // acc_cfg->contact = acc_cfg->id;
177 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000179 /* Build account route-set from outbound proxies and route set from
180 * account configuration.
181 */
182 pj_list_init(&acc->route_set);
183
184 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
185 pj_str_t hname = { "Route", 5};
186 pjsip_route_hdr *r;
187 pj_str_t tmp;
188
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000189 pj_strdup_with_null(acc->pool, &tmp,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000190 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000191 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000192 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193 if (r == NULL) {
194 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
195 PJSIP_EINVALIDURI);
196 return PJSIP_EINVALIDURI;
197 }
198 pj_list_push_back(&acc->route_set, r);
199 }
200
201 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
202 pj_str_t hname = { "Route", 5};
203 pjsip_route_hdr *r;
204 pj_str_t tmp;
205
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000206 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000207 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000208 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000209 if (r == NULL) {
210 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
211 PJ_EINVAL);
212 return PJ_EINVAL;
213 }
214 pj_list_push_back(&acc->route_set, r);
215 }
216
217
218 /* Concatenate credentials from account config and global config */
219 acc->cred_cnt = 0;
220 for (i=0; i<acc_cfg->cred_count; ++i) {
221 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
222 }
223 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
224 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
225 {
226 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
227 }
228
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000229 status = pjsua_pres_init_acc(acc_id);
230 if (status != PJ_SUCCESS)
231 return status;
232
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233 /* Mark account as valid */
234 pjsua_var.acc[acc_id].valid = PJ_TRUE;
235
Benny Prijono093d3022006-09-24 00:07:11 +0000236 /* Insert account ID into account ID array, sorted by priority */
237 for (i=0; i<pjsua_var.acc_cnt; ++i) {
238 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
239 pjsua_var.acc[acc_id].cfg.priority)
240 {
241 break;
242 }
243 }
244 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
245 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000246
247 return PJ_SUCCESS;
248}
249
250
251/*
252 * Add a new account to pjsua.
253 */
254PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
255 pj_bool_t is_default,
256 pjsua_acc_id *p_acc_id)
257{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000258 pjsua_acc *acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000259 unsigned id;
260 pj_status_t status;
261
262 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
263 PJ_ETOOMANY);
264
265 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000266 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000267
268 PJSUA_LOCK();
269
270 /* Find empty account id. */
271 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
272 if (pjsua_var.acc[id].valid == PJ_FALSE)
273 break;
274 }
275
276 /* Expect to find a slot */
277 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
278 {PJSUA_UNLOCK(); return PJ_EBUG;});
279
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000280 acc = &pjsua_var.acc[id];
281
282 /* Create pool for this account. */
283 if (acc->pool)
284 pj_pool_reset(acc->pool);
285 else
286 acc->pool = pjsua_pool_create("acc%p", 512, 256);
287
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000288 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000289 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000290
291 /* Normalize registration timeout */
292 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
293 pjsua_var.acc[id].cfg.reg_timeout == 0)
294 {
295 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
296 }
297
298 status = initialize_acc(id);
299 if (status != PJ_SUCCESS) {
300 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000301 pj_pool_release(acc->pool);
302 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000303 PJSUA_UNLOCK();
304 return status;
305 }
306
307 if (is_default)
308 pjsua_var.default_acc = id;
309
310 if (p_acc_id)
311 *p_acc_id = id;
312
313 pjsua_var.acc_cnt++;
314
315 PJSUA_UNLOCK();
316
317 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
318 (int)cfg->id.slen, cfg->id.ptr, id));
319
320 /* If accounts has registration enabled, start registration */
321 if (pjsua_var.acc[id].cfg.reg_uri.slen)
322 pjsua_acc_set_registration(id, PJ_TRUE);
323
324
325 return PJ_SUCCESS;
326}
327
328
329/*
330 * Add local account
331 */
332PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
333 pj_bool_t is_default,
334 pjsua_acc_id *p_acc_id)
335{
336 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000337 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000338 const char *beginquote, *endquote;
339 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000340 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000341
Benny Prijonoe93e2872006-06-28 16:46:49 +0000342 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000343 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
344 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000345
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000346 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000347 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000348
349 pjsua_acc_config_default(&cfg);
350
Benny Prijono093d3022006-09-24 00:07:11 +0000351 /* Lower the priority of local account */
352 --cfg.priority;
353
Benny Prijonod0bd4982007-12-02 15:40:52 +0000354 /* Enclose IPv6 address in square brackets */
355 if (t->type & PJSIP_TRANSPORT_IPV6) {
356 beginquote = "[";
357 endquote = "]";
358 } else {
359 beginquote = endquote = "";
360 }
361
362 /* Don't add transport parameter if it's UDP */
363 if ((t->type & PJSIP_TRANSPORT_UDP) == 0) {
364 pj_ansi_snprintf(transport_param, sizeof(transport_param),
365 ";transport=%s",
366 pjsip_transport_get_type_name(t->type));
367 } else {
368 transport_param[0] = '\0';
369 }
370
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000371 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000372 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000373 "<sip:%s%.*s%s:%d%s>",
374 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000375 (int)t->local_name.host.slen,
376 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000377 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000378 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000379 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000380
381 cfg.id = pj_str(uri);
382
383 return pjsua_acc_add(&cfg, is_default, p_acc_id);
384}
385
386
387/*
Benny Prijono705e7842008-07-21 18:12:51 +0000388 * Set arbitrary data to be associated with the account.
389 */
390PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
391 void *user_data)
392{
393 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
394 PJ_EINVAL);
395 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
396
397 PJSUA_LOCK();
398
399 pjsua_var.acc[acc_id].cfg.user_data = user_data;
400
401 PJSUA_UNLOCK();
402
403 return PJ_SUCCESS;
404}
405
406
407/*
408 * Retrieve arbitrary data associated with the account.
409 */
410PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
411{
412 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
413 NULL);
414 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
415
416 return pjsua_var.acc[acc_id].cfg.user_data;
417}
418
419
420/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000421 * Delete account.
422 */
423PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
424{
Benny Prijono093d3022006-09-24 00:07:11 +0000425 unsigned i;
426
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000427 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
428 PJ_EINVAL);
429 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
430
431 PJSUA_LOCK();
432
433 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000434 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000435 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000436 if (pjsua_var.acc[acc_id].regc) {
437 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
438 }
Benny Prijono922933b2007-01-21 16:23:56 +0000439 pjsua_var.acc[acc_id].regc = NULL;
440 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000441
442 /* Delete server presence subscription */
443 pjsua_pres_delete_acc(acc_id);
444
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000445 /* Release account pool */
446 if (pjsua_var.acc[acc_id].pool) {
447 pj_pool_release(pjsua_var.acc[acc_id].pool);
448 pjsua_var.acc[acc_id].pool = NULL;
449 }
450
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000451 /* Invalidate */
452 pjsua_var.acc[acc_id].valid = PJ_FALSE;
453
Benny Prijono093d3022006-09-24 00:07:11 +0000454 /* Remove from array */
455 for (i=0; i<pjsua_var.acc_cnt; ++i) {
456 if (pjsua_var.acc_ids[i] == acc_id)
457 break;
458 }
459 if (i != pjsua_var.acc_cnt) {
460 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
461 pjsua_var.acc_cnt, i);
462 --pjsua_var.acc_cnt;
463 }
464
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000465 /* Leave the calls intact, as I don't think calls need to
466 * access account once it's created
467 */
468
Benny Prijonofb2b3652007-06-28 07:15:03 +0000469 /* Update default account */
470 if (pjsua_var.default_acc == acc_id)
471 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000472
473 PJSUA_UNLOCK();
474
475 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
476
477 return PJ_SUCCESS;
478}
479
480
481/*
482 * Modify account information.
483 */
484PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
485 const pjsua_acc_config *cfg)
486{
487 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000488 PJ_UNUSED_ARG(acc_id);
489 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000490 return PJ_EINVALIDOP;
491}
492
493
494/*
495 * Modify account's presence status to be advertised to remote/presence
496 * subscribers.
497 */
498PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
499 pj_bool_t is_online)
500{
501 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
502 PJ_EINVAL);
503 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
504
505 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000506 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
507 pjsua_pres_update_acc(acc_id, PJ_FALSE);
508 return PJ_SUCCESS;
509}
510
511
512/*
513 * Set online status with extended information
514 */
515PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
516 pj_bool_t is_online,
517 const pjrpid_element *pr)
518{
519 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
520 PJ_EINVAL);
521 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
522
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000523 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000524 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000525 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
526 PJSUA_UNLOCK();
527
Benny Prijono4461c7d2007-08-25 13:36:15 +0000528 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 return PJ_SUCCESS;
530}
531
532
Benny Prijono15b02302007-09-27 14:07:07 +0000533/* Update NAT address from the REGISTER response */
534static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
535 struct pjsip_regc_cbparam *param)
536{
537 pjsip_transport *tp;
538 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000539 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000540 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000541 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000542 pjsip_via_hdr *via;
543
544 tp = param->rdata->tp_info.transport;
545
546 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000547 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000548 return PJ_FALSE;
549
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000550#if 0
551 // Always update
552 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000553
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000554 /* For UDP, only update if STUN is enabled (for now).
555 * For TCP/TLS, always check.
556 */
557 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
558 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
559 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
560 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
561 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000562 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000563 /* Yes we will check */
564 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000565 return PJ_FALSE;
566 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000567#endif
Benny Prijono15b02302007-09-27 14:07:07 +0000568
569 /* Get the received and rport info */
570 via = param->rdata->msg_info.via;
571 if (via->rport_param < 1) {
572 /* Remote doesn't support rport */
573 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +0000574 if (rport==0) {
575 pjsip_transport_type_e tp_type;
576 tp_type = (pjsip_transport_type_e) tp->key.type;
577 rport = pjsip_transport_get_default_port_for_type(tp_type);
578 }
Benny Prijono15b02302007-09-27 14:07:07 +0000579 } else
580 rport = via->rport_param;
581
582 if (via->recvd_param.slen != 0)
583 via_addr = &via->recvd_param;
584 else
585 via_addr = &via->sent_by.host;
586
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000587 /* Compare received and rport with the URI in our registration */
588 pool = pjsua_pool_create("tmp", 512, 512);
589 uri = (pjsip_sip_uri*)
590 pjsip_parse_uri(pool, acc->contact.ptr, acc->contact.slen, 0);
591 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +0000592 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000593
Benny Prijono24a21852008-04-14 04:04:30 +0000594 if (uri->port == 0) {
595 pjsip_transport_type_e tp_type;
596 tp_type = (pjsip_transport_type_e) tp->key.type;
597 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
598 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000599
600 if (uri->port == rport &&
601 pj_stricmp(&uri->host, via_addr)==0)
Benny Prijono15b02302007-09-27 14:07:07 +0000602 {
603 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000604 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000605 return PJ_FALSE;
606 }
607
608 /* At this point we've detected that the address as seen by registrar.
609 * has changed.
610 */
611 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
612 "(%.*s:%d --> %.*s:%d). Updating registration..",
613 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000614 (int)uri->host.slen,
615 uri->host.ptr,
616 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000617 (int)via_addr->slen,
618 via_addr->ptr,
619 rport));
620
621 /* Unregister current contact */
622 pjsua_acc_set_registration(acc->index, PJ_FALSE);
623 if (acc->regc != NULL) {
624 pjsip_regc_destroy(acc->regc);
625 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000626 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +0000627 }
628
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000629 /* Update account's Contact header */
630 {
631 char *tmp;
632 int len;
633
Benny Prijono24a21852008-04-14 04:04:30 +0000634 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000635 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijono83088f32008-04-22 18:33:55 +0000636 "<sip:%.*s%s%.*s:%d;transport=%s>",
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000637 (int)acc->user_part.slen,
638 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +0000639 (acc->user_part.slen? "@" : ""),
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000640 (int)via_addr->slen,
641 via_addr->ptr,
642 rport,
643 tp->type_name);
644 if (len < 1) {
645 PJ_LOG(1,(THIS_FILE, "URI too long"));
646 pj_pool_release(pool);
647 return PJ_FALSE;
648 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +0000649 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000650 }
651
652 /* For UDP transport, if STUN is enabled then update the transport's
653 * published name as well.
654 */
655 if (tp->key.type==PJSIP_TRANSPORT_UDP &&
656 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
657 pjsua_var.ua_cfg.stun_host.slen != 0))
658 {
659 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
660 tp->local_name.port = rport;
661 }
Benny Prijono15b02302007-09-27 14:07:07 +0000662
663 /* Perform new registration */
664 pjsua_acc_set_registration(acc->index, PJ_TRUE);
665
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000666 pj_pool_release(pool);
667
Benny Prijono15b02302007-09-27 14:07:07 +0000668 return PJ_TRUE;
669}
670
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000671/* Check and update Service-Route header */
672void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
673{
674 pjsip_generic_string_hdr *hsr = NULL;
675 pjsip_route_hdr *hr, *h;
676 const pj_str_t HNAME = { "Service-Route", 13 };
677 const pj_str_t HROUTE = { "Route", 5 };
678 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000679 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000680
681 /* Find and parse Service-Route headers */
682 for (;;) {
683 char saved;
684 int parsed_len;
685
686 /* Find Service-Route header */
687 hsr = (pjsip_generic_string_hdr*)
688 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
689 if (!hsr)
690 break;
691
692 /* Parse as Route header since the syntax is similar. This may
693 * return more than one headers.
694 */
695 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
696 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
697 hr = (pjsip_route_hdr*)
698 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
699 hsr->hvalue.slen, &parsed_len);
700 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
701
702 if (hr == NULL) {
703 /* Error */
704 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
705 return;
706 }
707
708 /* Save each URI in the result */
709 h = hr;
710 do {
711 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
712 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
713 {
714 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
715 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
716 return;
717 }
718
719 uri[uri_cnt++] = h->name_addr.uri;
720 h = h->next;
721 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
722
723 if (h != hr) {
724 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
725 return;
726 }
727
728 /* Prepare to find next Service-Route header */
729 hsr = hsr->next;
730 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
731 break;
732 }
733
734 if (uri_cnt == 0)
735 return;
736
737 /*
738 * Update account's route set
739 */
740
741 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000742 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000743 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
744 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
745 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000746 i<rcnt;
747 ++i)
748 {
749 pjsip_route_hdr *prev = hr->prev;
750 pj_list_erase(hr);
751 hr = prev;
752 }
753 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000754
755 /* Then append the Service-Route URIs */
756 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000757 hr = pjsip_route_hdr_create(acc->pool);
758 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000759 pj_list_push_back(&acc->route_set, hr);
760 }
761
762 /* Done */
763
764 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
765 acc->index, uri_cnt));
766}
767
Benny Prijonobddef2c2007-10-31 13:28:08 +0000768
769/* Keep alive timer callback */
770static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
771{
772 pjsua_acc *acc;
773 pjsip_tpselector tp_sel;
774 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000775 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000776 pj_status_t status;
777
778 PJ_UNUSED_ARG(th);
779
780 PJSUA_LOCK();
781
782 te->id = PJ_FALSE;
783
784 acc = (pjsua_acc*) te->user_data;
785
786 /* Select the transport to send the packet */
787 pj_bzero(&tp_sel, sizeof(tp_sel));
788 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
789 tp_sel.u.transport = acc->ka_transport;
790
791 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000792 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000793 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000794 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000795
796 /* Send raw packet */
797 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
798 PJSIP_TRANSPORT_UDP, &tp_sel,
799 NULL, acc->cfg.ka_data.ptr,
800 acc->cfg.ka_data.slen,
801 &acc->ka_target, acc->ka_target_len,
802 NULL, NULL);
803
804 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
805 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
806 }
807
808 /* Reschedule next timer */
809 delay.sec = acc->cfg.ka_interval;
810 delay.msec = 0;
811 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
812 if (status == PJ_SUCCESS) {
813 te->id = PJ_TRUE;
814 } else {
815 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
816 }
817
818 PJSUA_UNLOCK();
819}
820
821
822/* Update keep-alive for the account */
823static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
824 struct pjsip_regc_cbparam *param)
825{
826 /* In all cases, stop keep-alive timer if it's running. */
827 if (acc->ka_timer.id) {
828 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
829 acc->ka_timer.id = PJ_FALSE;
830
831 pjsip_transport_dec_ref(acc->ka_transport);
832 acc->ka_transport = NULL;
833 }
834
835 if (start) {
836 pj_time_val delay;
837 pj_status_t status;
838
839 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000840 * - ka_interval is not zero in the account, and
841 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000842 *
843 * Previously we only enabled keep-alive when STUN is enabled, since
844 * we thought that keep-alive is only needed in Internet situation.
845 * But it has been discovered that Windows Firewall on WinXP also
846 * needs to be kept-alive, otherwise incoming packets will be dropped.
847 * So because of this, now keep-alive is always enabled for UDP,
848 * regardless of whether STUN is enabled or not.
849 *
850 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
851 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000852 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000853 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000854 acc->cfg.ka_interval == 0 ||
855 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
856 {
857 /* Keep alive is not necessary */
858 return;
859 }
860
861 /* Save transport and destination address. */
862 acc->ka_transport = param->rdata->tp_info.transport;
863 pjsip_transport_add_ref(acc->ka_transport);
864 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
865 param->rdata->pkt_info.src_addr_len);
866 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
867
868 /* Setup and start the timer */
869 acc->ka_timer.cb = &keep_alive_timer_cb;
870 acc->ka_timer.user_data = (void*)acc;
871
872 delay.sec = acc->cfg.ka_interval;
873 delay.msec = 0;
874 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
875 &delay);
876 if (status == PJ_SUCCESS) {
877 acc->ka_timer.id = PJ_TRUE;
878 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
879 "destination:%s:%d, interval:%ds",
880 acc->index,
881 param->rdata->pkt_info.src_name,
882 param->rdata->pkt_info.src_port,
883 acc->cfg.ka_interval));
884 } else {
885 acc->ka_timer.id = PJ_FALSE;
886 pjsip_transport_dec_ref(acc->ka_transport);
887 acc->ka_transport = NULL;
888 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
889 }
890 }
891}
892
893
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000894/*
895 * This callback is called by pjsip_regc when outgoing register
896 * request has completed.
897 */
898static void regc_cb(struct pjsip_regc_cbparam *param)
899{
900
Benny Prijonoa1e69682007-05-11 15:14:34 +0000901 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000902
Benny Prijono15b02302007-09-27 14:07:07 +0000903 if (param->regc != acc->regc)
904 return;
905
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000906 PJSUA_LOCK();
907
908 /*
909 * Print registration status.
910 */
911 if (param->status!=PJ_SUCCESS) {
912 pjsua_perror(THIS_FILE, "SIP registration error",
913 param->status);
914 pjsip_regc_destroy(acc->regc);
915 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000916 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000917
Benny Prijonobddef2c2007-10-31 13:28:08 +0000918 /* Stop keep-alive timer if any. */
919 update_keep_alive(acc, PJ_FALSE, NULL);
920
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000921 } else if (param->code < 0 || param->code >= 300) {
922 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
923 param->code,
924 (int)param->reason.slen, param->reason.ptr));
925 pjsip_regc_destroy(acc->regc);
926 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000927 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000928
Benny Prijonobddef2c2007-10-31 13:28:08 +0000929 /* Stop keep-alive timer if any. */
930 update_keep_alive(acc, PJ_FALSE, NULL);
931
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000932 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
933
934 if (param->expiration < 1) {
935 pjsip_regc_destroy(acc->regc);
936 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000937 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000938
939 /* Stop keep-alive timer if any. */
940 update_keep_alive(acc, PJ_FALSE, NULL);
941
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000942 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
943 pjsua_var.acc[acc->index].cfg.id.ptr));
944 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000945 /* Check NAT bound address */
946 if (acc_check_nat_addr(acc, param)) {
947 /* Update address, don't notify application yet */
948 PJSUA_UNLOCK();
949 return;
950 }
951
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000952 /* Check and update Service-Route header */
953 update_service_route(acc, param->rdata);
954
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000955 PJ_LOG(3, (THIS_FILE,
956 "%s: registration success, status=%d (%.*s), "
957 "will re-register in %d seconds",
958 pjsua_var.acc[acc->index].cfg.id.ptr,
959 param->code,
960 (int)param->reason.slen, param->reason.ptr,
961 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000962
Benny Prijonobddef2c2007-10-31 13:28:08 +0000963 /* Start keep-alive timer if necessary. */
964 update_keep_alive(acc, PJ_TRUE, param);
965
Benny Prijono8b6834f2007-02-24 13:29:22 +0000966 /* Send initial PUBLISH if it is enabled */
967 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
968 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000969 }
970
971 } else {
972 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
973 }
974
975 acc->reg_last_err = param->status;
976 acc->reg_last_code = param->code;
977
978 if (pjsua_var.ua_cfg.cb.on_reg_state)
979 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
980
981 PJSUA_UNLOCK();
982}
983
984
985/*
986 * Initialize client registration.
987 */
988static pj_status_t pjsua_regc_init(int acc_id)
989{
990 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000991 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 pj_status_t status;
993
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000994 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000995 acc = &pjsua_var.acc[acc_id];
996
997 if (acc->cfg.reg_uri.slen == 0) {
998 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
999 return PJ_SUCCESS;
1000 }
1001
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001002 /* Destroy existing session, if any */
1003 if (acc->regc) {
1004 pjsip_regc_destroy(acc->regc);
1005 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001006 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001007 }
1008
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001009 /* initialize SIP registration if registrar is configured */
1010
1011 status = pjsip_regc_create( pjsua_var.endpt,
1012 acc, &regc_cb, &acc->regc);
1013
1014 if (status != PJ_SUCCESS) {
1015 pjsua_perror(THIS_FILE, "Unable to create client registration",
1016 status);
1017 return status;
1018 }
1019
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001020 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001021
1022 if (acc->contact.slen == 0) {
1023 pj_str_t tmp_contact;
1024
1025 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1026 acc_id, &acc->cfg.reg_uri);
1027 if (status != PJ_SUCCESS) {
1028 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1029 " for registration",
1030 status);
1031 pjsip_regc_destroy(acc->regc);
1032 pj_pool_release(pool);
1033 acc->regc = NULL;
1034 return status;
1035 }
1036
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001037 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001038 }
1039
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001040 status = pjsip_regc_init( acc->regc,
1041 &acc->cfg.reg_uri,
1042 &acc->cfg.id,
1043 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001044 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 acc->cfg.reg_timeout);
1046 if (status != PJ_SUCCESS) {
1047 pjsua_perror(THIS_FILE,
1048 "Client registration initialization error",
1049 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001050 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001051 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001052 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001053 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001054 return status;
1055 }
1056
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001057 /* If account is locked to specific transport, then set transport to
1058 * the client registration.
1059 */
1060 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1061 pjsip_tpselector tp_sel;
1062
1063 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1064 pjsip_regc_set_transport(acc->regc, &tp_sel);
1065 }
1066
1067
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001068 /* Set credentials
1069 */
1070 if (acc->cred_cnt) {
1071 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1072 }
1073
Benny Prijono48ab2b72007-11-08 09:24:30 +00001074 /* Set authentication preference */
1075 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1076
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001077 /* Set route-set
1078 */
1079 if (!pj_list_empty(&acc->route_set)) {
1080 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1081 }
1082
Benny Prijono8fc6de02006-11-11 21:25:55 +00001083 /* Add other request headers. */
1084 if (pjsua_var.ua_cfg.user_agent.slen) {
1085 pjsip_hdr hdr_list;
1086 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1087 pjsip_generic_string_hdr *h;
1088
Benny Prijono8fc6de02006-11-11 21:25:55 +00001089 pj_list_init(&hdr_list);
1090
1091 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1092 &pjsua_var.ua_cfg.user_agent);
1093 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1094
1095 pjsip_regc_add_headers(acc->regc, &hdr_list);
1096 }
1097
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001098 pj_pool_release(pool);
1099
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001100 return PJ_SUCCESS;
1101}
1102
1103
1104/*
1105 * Update registration or perform unregistration.
1106 */
1107PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1108 pj_bool_t renew)
1109{
1110 pj_status_t status = 0;
1111 pjsip_tx_data *tdata = 0;
1112
1113 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1114 PJ_EINVAL);
1115 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1116
1117 PJSUA_LOCK();
1118
1119 if (renew) {
1120 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001121 status = pjsua_regc_init(acc_id);
1122 if (status != PJ_SUCCESS) {
1123 pjsua_perror(THIS_FILE, "Unable to create registration",
1124 status);
1125 goto on_return;
1126 }
1127 }
1128 if (!pjsua_var.acc[acc_id].regc) {
1129 status = PJ_EINVALIDOP;
1130 goto on_return;
1131 }
1132
1133 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1134 &tdata);
1135
Benny Prijono28f673a2007-10-15 07:04:59 +00001136 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1137 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1138 pjsip_authorization_hdr *h;
1139 char *uri;
1140 int d;
1141
1142 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1143 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1144 uri, acc->cfg.reg_uri.slen+10);
1145 pj_assert(d > 0);
1146
1147 h = pjsip_authorization_hdr_create(tdata->pool);
1148 h->scheme = pj_str("Digest");
1149 h->credential.digest.username = acc->cred[0].username;
1150 h->credential.digest.realm = acc->srv_domain;
1151 h->credential.digest.uri = pj_str(uri);
1152 h->credential.digest.algorithm = pj_str("md5");
1153
1154 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1155 }
1156
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001157 } else {
1158 if (pjsua_var.acc[acc_id].regc == NULL) {
1159 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1160 status = PJ_EINVALIDOP;
1161 goto on_return;
1162 }
1163 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1164 }
1165
Benny Prijono56315612006-07-18 14:39:40 +00001166 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001167 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001168 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001169 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001170
1171 if (status != PJ_SUCCESS) {
1172 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1173 status);
1174 } else {
1175 PJ_LOG(3,(THIS_FILE, "%s sent",
1176 (renew? "Registration" : "Unregistration")));
1177 }
1178
1179on_return:
1180 PJSUA_UNLOCK();
1181 return status;
1182}
1183
1184
1185/*
1186 * Get account information.
1187 */
1188PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1189 pjsua_acc_info *info)
1190{
1191 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1192 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1193
1194 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001195 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001196
Benny Prijonoac623b32006-07-03 15:19:31 +00001197 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001198
1199 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1200 PJ_EINVAL);
1201 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1202
1203 PJSUA_LOCK();
1204
1205 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1206 PJSUA_UNLOCK();
1207 return PJ_EINVALIDOP;
1208 }
1209
1210 info->id = acc_id;
1211 info->is_default = (pjsua_var.default_acc == acc_id);
1212 info->acc_uri = acc_cfg->id;
1213 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1214 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001215 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1216 if (info->rpid.note.slen)
1217 info->online_status_text = info->rpid.note;
1218 else if (info->online_status)
1219 info->online_status_text = pj_str("Online");
1220 else
1221 info->online_status_text = pj_str("Offline");
1222
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001223 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001224 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001225 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1226 info->status_text = pj_str(info->buf_);
1227 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001228 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001229 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001230 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1231 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001232 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001233 info->status_text = pj_str("not registered");
1234 }
1235 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001236 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001237 info->status_text = pj_str("In Progress");
1238 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001239 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001240 info->status_text = pj_str("does not register");
1241 }
1242
1243 if (acc->regc) {
1244 pjsip_regc_info regc_info;
1245 pjsip_regc_get_info(acc->regc, &regc_info);
1246 info->expires = regc_info.next_reg;
1247 } else {
1248 info->expires = -1;
1249 }
1250
1251 PJSUA_UNLOCK();
1252
1253 return PJ_SUCCESS;
1254
1255}
1256
1257
1258/*
1259 * Enum accounts all account ids.
1260 */
1261PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1262 unsigned *count )
1263{
1264 unsigned i, c;
1265
1266 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1267
1268 PJSUA_LOCK();
1269
1270 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1271 if (!pjsua_var.acc[i].valid)
1272 continue;
1273 ids[c] = i;
1274 ++c;
1275 }
1276
1277 *count = c;
1278
1279 PJSUA_UNLOCK();
1280
1281 return PJ_SUCCESS;
1282}
1283
1284
1285/*
1286 * Enum accounts info.
1287 */
1288PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1289 unsigned *count )
1290{
1291 unsigned i, c;
1292
1293 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1294
1295 PJSUA_LOCK();
1296
1297 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1298 if (!pjsua_var.acc[i].valid)
1299 continue;
1300
1301 pjsua_acc_get_info(i, &info[c]);
1302 ++c;
1303 }
1304
1305 *count = c;
1306
1307 PJSUA_UNLOCK();
1308
1309 return PJ_SUCCESS;
1310}
1311
1312
1313/*
1314 * This is an internal function to find the most appropriate account to
1315 * used to reach to the specified URL.
1316 */
1317PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1318{
1319 pj_str_t tmp;
1320 pjsip_uri *uri;
1321 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001322 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001323 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001324
1325 PJSUA_LOCK();
1326
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001327 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001328
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001329 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001330
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001331 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001332 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001333 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001334 PJSUA_UNLOCK();
1335 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001336 }
1337
1338 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1339 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1340 {
1341 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001342 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1343 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001344 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001345 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001346 break;
1347 }
1348
Benny Prijono093d3022006-09-24 00:07:11 +00001349 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001350 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001351 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001352 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001353 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001354 }
1355
1356 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001357 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001358 PJSUA_UNLOCK();
1359 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001360 }
1361
Benny Prijonoa1e69682007-05-11 15:14:34 +00001362 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001363
Benny Prijonob4a17c92006-07-10 14:40:21 +00001364 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001365 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1366 unsigned acc_id = pjsua_var.acc_ids[i];
1367 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1368 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1369 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001370 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001371 PJSUA_UNLOCK();
1372 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001373 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001374 }
1375
Benny Prijonob4a17c92006-07-10 14:40:21 +00001376 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001377 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1378 unsigned acc_id = pjsua_var.acc_ids[i];
1379 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1380 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001381 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001382 PJSUA_UNLOCK();
1383 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001384 }
1385 }
1386
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001387
Benny Prijono093d3022006-09-24 00:07:11 +00001388 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001389 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001390 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001391 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001392}
1393
1394
1395/*
1396 * This is an internal function to find the most appropriate account to be
1397 * used to handle incoming calls.
1398 */
1399PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1400{
1401 pjsip_uri *uri;
1402 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001403 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001404
Benny Prijono371cf0a2007-06-19 00:35:37 +00001405 /* Check that there's at least one account configured */
1406 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1407
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001408 uri = rdata->msg_info.to->uri;
1409
1410 /* Just return default account if To URI is not SIP: */
1411 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1412 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1413 {
1414 return pjsua_var.default_acc;
1415 }
1416
1417
1418 PJSUA_LOCK();
1419
1420 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1421
1422 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001423 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1424 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001425 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1426
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001427 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001428 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001429 {
1430 /* Match ! */
1431 PJSUA_UNLOCK();
1432 return acc_id;
1433 }
1434 }
1435
Benny Prijono093d3022006-09-24 00:07:11 +00001436 /* No matching account, try match domain part only. */
1437 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1438 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001439 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1440
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001441 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001442 /* Match ! */
1443 PJSUA_UNLOCK();
1444 return acc_id;
1445 }
1446 }
1447
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001448 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001449 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1450 unsigned acc_id = pjsua_var.acc_ids[i];
1451 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1452
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001453 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1454
1455 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1456 pjsip_transport_type_e type;
1457 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1458 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1459 type = PJSIP_TRANSPORT_UDP;
1460
1461 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1462 continue;
1463 }
1464
Benny Prijono093d3022006-09-24 00:07:11 +00001465 /* Match ! */
1466 PJSUA_UNLOCK();
1467 return acc_id;
1468 }
1469 }
1470
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001471 /* Still no match, use default account */
1472 PJSUA_UNLOCK();
1473 return pjsua_var.default_acc;
1474}
1475
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001476
Benny Prijonofff245c2007-04-02 11:44:47 +00001477/*
1478 * Create arbitrary requests for this account.
1479 */
1480PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1481 const pjsip_method *method,
1482 const pj_str_t *target,
1483 pjsip_tx_data **p_tdata)
1484{
1485 pjsip_tx_data *tdata;
1486 pjsua_acc *acc;
1487 pjsip_route_hdr *r;
1488 pj_status_t status;
1489
1490 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1491 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1492
1493 acc = &pjsua_var.acc[acc_id];
1494
1495 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1496 &acc->cfg.id, target,
1497 NULL, NULL, -1, NULL, &tdata);
1498 if (status != PJ_SUCCESS) {
1499 pjsua_perror(THIS_FILE, "Unable to create request", status);
1500 return status;
1501 }
1502
1503 /* Copy routeset */
1504 r = acc->route_set.next;
1505 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001506 pjsip_msg_add_hdr(tdata->msg,
1507 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001508 r = r->next;
1509 }
1510
1511 /* Done */
1512 *p_tdata = tdata;
1513 return PJ_SUCCESS;
1514}
1515
1516
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001517PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1518 pj_str_t *contact,
1519 pjsua_acc_id acc_id,
1520 const pj_str_t *suri)
1521{
1522 pjsua_acc *acc;
1523 pjsip_sip_uri *sip_uri;
1524 pj_status_t status;
1525 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1526 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001527 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001528 unsigned flag;
1529 int secure;
1530 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001531 const char *beginquote, *endquote;
1532 char transport_param[32];
1533
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001534
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001535 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001536 acc = &pjsua_var.acc[acc_id];
1537
Benny Prijonof75eceb2007-03-23 19:09:54 +00001538 /* If force_contact is configured, then use use it */
1539 if (acc->cfg.force_contact.slen) {
1540 *contact = acc->cfg.force_contact;
1541 return PJ_SUCCESS;
1542 }
1543
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001544 /* If route-set is configured for the account, then URI is the
1545 * first entry of the route-set.
1546 */
1547 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001548 sip_uri = (pjsip_sip_uri*)
1549 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001550 } else {
1551 pj_str_t tmp;
1552 pjsip_uri *uri;
1553
1554 pj_strdup_with_null(pool, &tmp, suri);
1555
1556 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1557 if (uri == NULL)
1558 return PJSIP_EINVALIDURI;
1559
1560 /* For non-SIP scheme, route set should be configured */
1561 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1562 return PJSIP_EINVALIDREQURI;
1563
Benny Prijono8c7a6172007-02-18 21:17:46 +00001564 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001565 }
1566
1567 /* Get transport type of the URI */
1568 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1569 tp_type = PJSIP_TRANSPORT_TLS;
1570 else if (sip_uri->transport_param.slen == 0) {
1571 tp_type = PJSIP_TRANSPORT_UDP;
1572 } else
1573 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1574
1575 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1576 return PJSIP_EUNSUPTRANSPORT;
1577
Benny Prijonod0bd4982007-12-02 15:40:52 +00001578 /* If destination URI specifies IPv6, then set transport type
1579 * to use IPv6 as well.
1580 */
Benny Prijono19b29372007-12-05 04:08:40 +00001581 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001582 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1583
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001584 flag = pjsip_transport_get_flag_from_type(tp_type);
1585 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1586
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001587 /* Init transport selector. */
1588 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1589
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001590 /* Get local address suitable to send request from */
1591 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001592 pool, tp_type, &tp_sel,
1593 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001594 if (status != PJ_SUCCESS)
1595 return status;
1596
Benny Prijonod0bd4982007-12-02 15:40:52 +00001597 /* Enclose IPv6 address in square brackets */
1598 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1599 beginquote = "[";
1600 endquote = "]";
1601 } else {
1602 beginquote = endquote = "";
1603 }
1604
1605 /* Don't add transport parameter if it's UDP */
1606 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1607 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1608 ";transport=%s",
1609 pjsip_transport_get_type_name(tp_type));
1610 } else {
1611 transport_param[0] = '\0';
1612 }
1613
1614
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001615 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001616 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001617 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001618 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001619 (int)acc->display.slen,
1620 acc->display.ptr,
1621 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001622 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001623 (int)acc->user_part.slen,
1624 acc->user_part.ptr,
1625 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001626 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001627 (int)local_addr.slen,
1628 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001629 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001630 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001631 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001632
1633 return PJ_SUCCESS;
1634}
1635
1636
1637
1638PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1639 pj_str_t *contact,
1640 pjsua_acc_id acc_id,
1641 pjsip_rx_data *rdata )
1642{
1643 /*
1644 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1645 * If the request that initiated the dialog contained a SIPS URI
1646 * in the Request-URI or in the top Record-Route header field value,
1647 * if there was any, or the Contact header field if there was no
1648 * Record-Route header field, the Contact header field in the response
1649 * MUST be a SIPS URI.
1650 */
1651 pjsua_acc *acc;
1652 pjsip_sip_uri *sip_uri;
1653 pj_status_t status;
1654 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1655 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001656 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001657 unsigned flag;
1658 int secure;
1659 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001660 const char *beginquote, *endquote;
1661 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001662
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001663 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001664 acc = &pjsua_var.acc[acc_id];
1665
Benny Prijonof75eceb2007-03-23 19:09:54 +00001666 /* If force_contact is configured, then use use it */
1667 if (acc->cfg.force_contact.slen) {
1668 *contact = acc->cfg.force_contact;
1669 return PJ_SUCCESS;
1670 }
1671
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001672 /* If Record-Route is present, then URI is the top Record-Route. */
1673 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001674 sip_uri = (pjsip_sip_uri*)
1675 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001676 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00001677 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001678 pjsip_contact_hdr *h_contact;
1679 pjsip_uri *uri = NULL;
1680
Benny Prijonoa330d452008-08-05 20:14:39 +00001681 /* Otherwise URI is Contact URI.
1682 * Iterate the Contact URI until we find sip: or sips: scheme.
1683 */
1684 do {
1685 h_contact = (pjsip_contact_hdr*)
1686 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
1687 pos);
1688 if (h_contact) {
1689 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
1690 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1691 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1692 {
1693 pos = (pjsip_hdr*)h_contact->next;
1694 if (pos == &rdata->msg_info.msg->hdr)
1695 h_contact = NULL;
1696 } else {
1697 break;
1698 }
1699 }
1700 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001701
1702
1703 /* Or if Contact URI is not present, take the remote URI from
1704 * the From URI.
1705 */
1706 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001707 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001708
1709
1710 /* Can only do sip/sips scheme at present. */
1711 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1712 return PJSIP_EINVALIDREQURI;
1713
Benny Prijono8c7a6172007-02-18 21:17:46 +00001714 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001715 }
1716
1717 /* Get transport type of the URI */
1718 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1719 tp_type = PJSIP_TRANSPORT_TLS;
1720 else if (sip_uri->transport_param.slen == 0) {
1721 tp_type = PJSIP_TRANSPORT_UDP;
1722 } else
1723 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001724
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001725 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1726 return PJSIP_EUNSUPTRANSPORT;
1727
Benny Prijonod0bd4982007-12-02 15:40:52 +00001728 /* If destination URI specifies IPv6, then set transport type
1729 * to use IPv6 as well.
1730 */
1731 if (pj_strchr(&sip_uri->host, ':'))
1732 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1733
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001734 flag = pjsip_transport_get_flag_from_type(tp_type);
1735 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1736
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001737 /* Init transport selector. */
1738 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1739
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001740 /* Get local address suitable to send request from */
1741 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001742 pool, tp_type, &tp_sel,
1743 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001744 if (status != PJ_SUCCESS)
1745 return status;
1746
Benny Prijonod0bd4982007-12-02 15:40:52 +00001747 /* Enclose IPv6 address in square brackets */
1748 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1749 beginquote = "[";
1750 endquote = "]";
1751 } else {
1752 beginquote = endquote = "";
1753 }
1754
1755 /* Don't add transport parameter if it's UDP */
1756 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1757 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1758 ";transport=%s",
1759 pjsip_transport_get_type_name(tp_type));
1760 } else {
1761 transport_param[0] = '\0';
1762 }
1763
1764
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001765 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001766 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001767 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001768 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001769 (int)acc->display.slen,
1770 acc->display.ptr,
1771 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001772 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001773 (int)acc->user_part.slen,
1774 acc->user_part.ptr,
1775 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001776 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001777 (int)local_addr.slen,
1778 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001779 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001780 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001781 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001782
1783 return PJ_SUCCESS;
1784}
1785
1786
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001787PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1788 pjsua_transport_id tp_id)
1789{
1790 pjsua_acc *acc;
1791
1792 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1793 acc = &pjsua_var.acc[acc_id];
1794
Benny Prijonoa1e69682007-05-11 15:14:34 +00001795 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001796 PJ_EINVAL);
1797
1798 acc->cfg.transport_id = tp_id;
1799
1800 return PJ_SUCCESS;
1801}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001802