blob: 46a6dd1c829f5590e35def9c7fe1769d9dff193e [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 Prijono91d06b62008-09-20 12:16:56 +0000259 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000260 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
Benny Prijono91d06b62008-09-20 12:16:56 +0000298 /* Check the route URI's and force loose route if required */
299 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
300 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
301 if (status != PJ_SUCCESS)
302 return status;
303 }
304
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000305 status = initialize_acc(id);
306 if (status != PJ_SUCCESS) {
307 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000308 pj_pool_release(acc->pool);
309 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000310 PJSUA_UNLOCK();
311 return status;
312 }
313
314 if (is_default)
315 pjsua_var.default_acc = id;
316
317 if (p_acc_id)
318 *p_acc_id = id;
319
320 pjsua_var.acc_cnt++;
321
322 PJSUA_UNLOCK();
323
324 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
325 (int)cfg->id.slen, cfg->id.ptr, id));
326
327 /* If accounts has registration enabled, start registration */
328 if (pjsua_var.acc[id].cfg.reg_uri.slen)
329 pjsua_acc_set_registration(id, PJ_TRUE);
330
331
332 return PJ_SUCCESS;
333}
334
335
336/*
337 * Add local account
338 */
339PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
340 pj_bool_t is_default,
341 pjsua_acc_id *p_acc_id)
342{
343 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000344 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000345 const char *beginquote, *endquote;
346 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000347 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000348
Benny Prijonoe93e2872006-06-28 16:46:49 +0000349 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000350 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
351 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000352
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000353 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000354 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355
356 pjsua_acc_config_default(&cfg);
357
Benny Prijono093d3022006-09-24 00:07:11 +0000358 /* Lower the priority of local account */
359 --cfg.priority;
360
Benny Prijonod0bd4982007-12-02 15:40:52 +0000361 /* Enclose IPv6 address in square brackets */
362 if (t->type & PJSIP_TRANSPORT_IPV6) {
363 beginquote = "[";
364 endquote = "]";
365 } else {
366 beginquote = endquote = "";
367 }
368
369 /* Don't add transport parameter if it's UDP */
370 if ((t->type & PJSIP_TRANSPORT_UDP) == 0) {
371 pj_ansi_snprintf(transport_param, sizeof(transport_param),
372 ";transport=%s",
373 pjsip_transport_get_type_name(t->type));
374 } else {
375 transport_param[0] = '\0';
376 }
377
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000379 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000380 "<sip:%s%.*s%s:%d%s>",
381 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000382 (int)t->local_name.host.slen,
383 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000384 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000385 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000386 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387
388 cfg.id = pj_str(uri);
389
390 return pjsua_acc_add(&cfg, is_default, p_acc_id);
391}
392
393
394/*
Benny Prijono705e7842008-07-21 18:12:51 +0000395 * Set arbitrary data to be associated with the account.
396 */
397PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
398 void *user_data)
399{
400 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
401 PJ_EINVAL);
402 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
403
404 PJSUA_LOCK();
405
406 pjsua_var.acc[acc_id].cfg.user_data = user_data;
407
408 PJSUA_UNLOCK();
409
410 return PJ_SUCCESS;
411}
412
413
414/*
415 * Retrieve arbitrary data associated with the account.
416 */
417PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
418{
419 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
420 NULL);
421 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
422
423 return pjsua_var.acc[acc_id].cfg.user_data;
424}
425
426
427/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000428 * Delete account.
429 */
430PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
431{
Benny Prijono093d3022006-09-24 00:07:11 +0000432 unsigned i;
433
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000434 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
435 PJ_EINVAL);
436 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
437
438 PJSUA_LOCK();
439
440 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000441 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000442 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000443 if (pjsua_var.acc[acc_id].regc) {
444 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
445 }
Benny Prijono922933b2007-01-21 16:23:56 +0000446 pjsua_var.acc[acc_id].regc = NULL;
447 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000448
449 /* Delete server presence subscription */
450 pjsua_pres_delete_acc(acc_id);
451
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000452 /* Release account pool */
453 if (pjsua_var.acc[acc_id].pool) {
454 pj_pool_release(pjsua_var.acc[acc_id].pool);
455 pjsua_var.acc[acc_id].pool = NULL;
456 }
457
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000458 /* Invalidate */
459 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000460 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000461
Benny Prijono093d3022006-09-24 00:07:11 +0000462 /* Remove from array */
463 for (i=0; i<pjsua_var.acc_cnt; ++i) {
464 if (pjsua_var.acc_ids[i] == acc_id)
465 break;
466 }
467 if (i != pjsua_var.acc_cnt) {
468 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
469 pjsua_var.acc_cnt, i);
470 --pjsua_var.acc_cnt;
471 }
472
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000473 /* Leave the calls intact, as I don't think calls need to
474 * access account once it's created
475 */
476
Benny Prijonofb2b3652007-06-28 07:15:03 +0000477 /* Update default account */
478 if (pjsua_var.default_acc == acc_id)
479 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000480
481 PJSUA_UNLOCK();
482
483 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
484
485 return PJ_SUCCESS;
486}
487
488
489/*
490 * Modify account information.
491 */
492PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
493 const pjsua_acc_config *cfg)
494{
495 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000496 PJ_UNUSED_ARG(acc_id);
497 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 return PJ_EINVALIDOP;
499}
500
501
502/*
503 * Modify account's presence status to be advertised to remote/presence
504 * subscribers.
505 */
506PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
507 pj_bool_t is_online)
508{
509 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
510 PJ_EINVAL);
511 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
512
513 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000514 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
515 pjsua_pres_update_acc(acc_id, PJ_FALSE);
516 return PJ_SUCCESS;
517}
518
519
520/*
521 * Set online status with extended information
522 */
523PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
524 pj_bool_t is_online,
525 const pjrpid_element *pr)
526{
527 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
528 PJ_EINVAL);
529 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
530
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000531 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000532 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000533 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
534 PJSUA_UNLOCK();
535
Benny Prijono4461c7d2007-08-25 13:36:15 +0000536 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 return PJ_SUCCESS;
538}
539
Benny Prijono7f630432008-09-24 16:52:41 +0000540/* Check if IP is private IP address */
541static pj_bool_t is_private_ip(const pj_str_t *addr)
542{
543 const pj_str_t private_net[] =
544 {
545 { "10.", 3 },
546 { "127.", 4 },
547 { "172.16.", 7 },
548 { "192.168.", 8 }
549 };
550 unsigned i;
551
552 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
553 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
554 return PJ_TRUE;
555 }
556
557 return PJ_FALSE;
558}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559
Benny Prijono15b02302007-09-27 14:07:07 +0000560/* Update NAT address from the REGISTER response */
561static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
562 struct pjsip_regc_cbparam *param)
563{
564 pjsip_transport *tp;
565 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000566 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000567 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000568 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000569 pjsip_via_hdr *via;
Benny Prijono7f630432008-09-24 16:52:41 +0000570 pj_str_t srv_ip;
Benny Prijono15b02302007-09-27 14:07:07 +0000571
572 tp = param->rdata->tp_info.transport;
573
574 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000575 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000576 return PJ_FALSE;
577
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000578#if 0
579 // Always update
580 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000581
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000582 /* For UDP, only update if STUN is enabled (for now).
583 * For TCP/TLS, always check.
584 */
585 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
586 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
587 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
588 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
589 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000590 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000591 /* Yes we will check */
592 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000593 return PJ_FALSE;
594 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000595#endif
Benny Prijono15b02302007-09-27 14:07:07 +0000596
597 /* Get the received and rport info */
598 via = param->rdata->msg_info.via;
599 if (via->rport_param < 1) {
600 /* Remote doesn't support rport */
601 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +0000602 if (rport==0) {
603 pjsip_transport_type_e tp_type;
604 tp_type = (pjsip_transport_type_e) tp->key.type;
605 rport = pjsip_transport_get_default_port_for_type(tp_type);
606 }
Benny Prijono15b02302007-09-27 14:07:07 +0000607 } else
608 rport = via->rport_param;
609
610 if (via->recvd_param.slen != 0)
611 via_addr = &via->recvd_param;
612 else
613 via_addr = &via->sent_by.host;
614
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000615 /* Compare received and rport with the URI in our registration */
616 pool = pjsua_pool_create("tmp", 512, 512);
617 uri = (pjsip_sip_uri*)
618 pjsip_parse_uri(pool, acc->contact.ptr, acc->contact.slen, 0);
619 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +0000620 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000621
Benny Prijono24a21852008-04-14 04:04:30 +0000622 if (uri->port == 0) {
623 pjsip_transport_type_e tp_type;
624 tp_type = (pjsip_transport_type_e) tp->key.type;
625 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
626 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000627
628 if (uri->port == rport &&
629 pj_stricmp(&uri->host, via_addr)==0)
Benny Prijono15b02302007-09-27 14:07:07 +0000630 {
631 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000632 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000633 return PJ_FALSE;
634 }
635
Benny Prijono7f630432008-09-24 16:52:41 +0000636 /* Get server IP */
637 srv_ip = pj_str(param->rdata->pkt_info.src_name);
638
Benny Prijono15b02302007-09-27 14:07:07 +0000639 /* At this point we've detected that the address as seen by registrar.
640 * has changed.
641 */
Benny Prijono7f630432008-09-24 16:52:41 +0000642
643 /* Do not switch if both Contact and server's IP address are
644 * public but response contains private IP. A NAT in the middle
645 * might have messed up with the SIP packets.
Benny Prijono247921b2008-09-26 22:06:11 +0000646 *
647 * This exception can be disabled by setting allow_contact_rewrite
648 * to 2. In this case, the switch will always be done whenever there
649 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +0000650 */
Benny Prijono247921b2008-09-26 22:06:11 +0000651 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
652 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +0000653 {
654 /* Don't switch */
655 pj_pool_release(pool);
656 return PJ_FALSE;
657 }
658
Benny Prijono15b02302007-09-27 14:07:07 +0000659 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
660 "(%.*s:%d --> %.*s:%d). Updating registration..",
661 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000662 (int)uri->host.slen,
663 uri->host.ptr,
664 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000665 (int)via_addr->slen,
666 via_addr->ptr,
667 rport));
668
669 /* Unregister current contact */
670 pjsua_acc_set_registration(acc->index, PJ_FALSE);
671 if (acc->regc != NULL) {
672 pjsip_regc_destroy(acc->regc);
673 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000674 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +0000675 }
676
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000677 /* Update account's Contact header */
678 {
679 char *tmp;
680 int len;
681
Benny Prijono24a21852008-04-14 04:04:30 +0000682 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000683 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijono83088f32008-04-22 18:33:55 +0000684 "<sip:%.*s%s%.*s:%d;transport=%s>",
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000685 (int)acc->user_part.slen,
686 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +0000687 (acc->user_part.slen? "@" : ""),
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000688 (int)via_addr->slen,
689 via_addr->ptr,
690 rport,
691 tp->type_name);
692 if (len < 1) {
693 PJ_LOG(1,(THIS_FILE, "URI too long"));
694 pj_pool_release(pool);
695 return PJ_FALSE;
696 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +0000697 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000698 }
699
700 /* For UDP transport, if STUN is enabled then update the transport's
701 * published name as well.
702 */
703 if (tp->key.type==PJSIP_TRANSPORT_UDP &&
704 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
705 pjsua_var.ua_cfg.stun_host.slen != 0))
706 {
707 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
708 tp->local_name.port = rport;
709 }
Benny Prijono15b02302007-09-27 14:07:07 +0000710
711 /* Perform new registration */
712 pjsua_acc_set_registration(acc->index, PJ_TRUE);
713
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000714 pj_pool_release(pool);
715
Benny Prijono15b02302007-09-27 14:07:07 +0000716 return PJ_TRUE;
717}
718
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000719/* Check and update Service-Route header */
720void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
721{
722 pjsip_generic_string_hdr *hsr = NULL;
723 pjsip_route_hdr *hr, *h;
724 const pj_str_t HNAME = { "Service-Route", 13 };
725 const pj_str_t HROUTE = { "Route", 5 };
726 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000727 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000728
729 /* Find and parse Service-Route headers */
730 for (;;) {
731 char saved;
732 int parsed_len;
733
734 /* Find Service-Route header */
735 hsr = (pjsip_generic_string_hdr*)
736 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
737 if (!hsr)
738 break;
739
740 /* Parse as Route header since the syntax is similar. This may
741 * return more than one headers.
742 */
743 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
744 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
745 hr = (pjsip_route_hdr*)
746 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
747 hsr->hvalue.slen, &parsed_len);
748 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
749
750 if (hr == NULL) {
751 /* Error */
752 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
753 return;
754 }
755
756 /* Save each URI in the result */
757 h = hr;
758 do {
759 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
760 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
761 {
762 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
763 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
764 return;
765 }
766
767 uri[uri_cnt++] = h->name_addr.uri;
768 h = h->next;
769 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
770
771 if (h != hr) {
772 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
773 return;
774 }
775
776 /* Prepare to find next Service-Route header */
777 hsr = hsr->next;
778 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
779 break;
780 }
781
782 if (uri_cnt == 0)
783 return;
784
785 /*
786 * Update account's route set
787 */
788
789 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000790 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000791 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
792 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
793 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000794 i<rcnt;
795 ++i)
796 {
797 pjsip_route_hdr *prev = hr->prev;
798 pj_list_erase(hr);
799 hr = prev;
800 }
801 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000802
803 /* Then append the Service-Route URIs */
804 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000805 hr = pjsip_route_hdr_create(acc->pool);
806 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000807 pj_list_push_back(&acc->route_set, hr);
808 }
809
810 /* Done */
811
812 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
813 acc->index, uri_cnt));
814}
815
Benny Prijonobddef2c2007-10-31 13:28:08 +0000816
817/* Keep alive timer callback */
818static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
819{
820 pjsua_acc *acc;
821 pjsip_tpselector tp_sel;
822 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000823 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000824 pj_status_t status;
825
826 PJ_UNUSED_ARG(th);
827
828 PJSUA_LOCK();
829
830 te->id = PJ_FALSE;
831
832 acc = (pjsua_acc*) te->user_data;
833
834 /* Select the transport to send the packet */
835 pj_bzero(&tp_sel, sizeof(tp_sel));
836 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
837 tp_sel.u.transport = acc->ka_transport;
838
839 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000840 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000841 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000842 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000843
844 /* Send raw packet */
845 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
846 PJSIP_TRANSPORT_UDP, &tp_sel,
847 NULL, acc->cfg.ka_data.ptr,
848 acc->cfg.ka_data.slen,
849 &acc->ka_target, acc->ka_target_len,
850 NULL, NULL);
851
852 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
853 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
854 }
855
856 /* Reschedule next timer */
857 delay.sec = acc->cfg.ka_interval;
858 delay.msec = 0;
859 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
860 if (status == PJ_SUCCESS) {
861 te->id = PJ_TRUE;
862 } else {
863 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
864 }
865
866 PJSUA_UNLOCK();
867}
868
869
870/* Update keep-alive for the account */
871static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
872 struct pjsip_regc_cbparam *param)
873{
874 /* In all cases, stop keep-alive timer if it's running. */
875 if (acc->ka_timer.id) {
876 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
877 acc->ka_timer.id = PJ_FALSE;
878
879 pjsip_transport_dec_ref(acc->ka_transport);
880 acc->ka_transport = NULL;
881 }
882
883 if (start) {
884 pj_time_val delay;
885 pj_status_t status;
886
887 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000888 * - ka_interval is not zero in the account, and
889 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000890 *
891 * Previously we only enabled keep-alive when STUN is enabled, since
892 * we thought that keep-alive is only needed in Internet situation.
893 * But it has been discovered that Windows Firewall on WinXP also
894 * needs to be kept-alive, otherwise incoming packets will be dropped.
895 * So because of this, now keep-alive is always enabled for UDP,
896 * regardless of whether STUN is enabled or not.
897 *
898 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
899 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000900 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000901 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000902 acc->cfg.ka_interval == 0 ||
903 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
904 {
905 /* Keep alive is not necessary */
906 return;
907 }
908
909 /* Save transport and destination address. */
910 acc->ka_transport = param->rdata->tp_info.transport;
911 pjsip_transport_add_ref(acc->ka_transport);
912 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
913 param->rdata->pkt_info.src_addr_len);
914 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
915
916 /* Setup and start the timer */
917 acc->ka_timer.cb = &keep_alive_timer_cb;
918 acc->ka_timer.user_data = (void*)acc;
919
920 delay.sec = acc->cfg.ka_interval;
921 delay.msec = 0;
922 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
923 &delay);
924 if (status == PJ_SUCCESS) {
925 acc->ka_timer.id = PJ_TRUE;
926 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
927 "destination:%s:%d, interval:%ds",
928 acc->index,
929 param->rdata->pkt_info.src_name,
930 param->rdata->pkt_info.src_port,
931 acc->cfg.ka_interval));
932 } else {
933 acc->ka_timer.id = PJ_FALSE;
934 pjsip_transport_dec_ref(acc->ka_transport);
935 acc->ka_transport = NULL;
936 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
937 }
938 }
939}
940
941
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000942/*
943 * This callback is called by pjsip_regc when outgoing register
944 * request has completed.
945 */
946static void regc_cb(struct pjsip_regc_cbparam *param)
947{
948
Benny Prijonoa1e69682007-05-11 15:14:34 +0000949 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000950
Benny Prijono15b02302007-09-27 14:07:07 +0000951 if (param->regc != acc->regc)
952 return;
953
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000954 PJSUA_LOCK();
955
956 /*
957 * Print registration status.
958 */
959 if (param->status!=PJ_SUCCESS) {
960 pjsua_perror(THIS_FILE, "SIP registration error",
961 param->status);
962 pjsip_regc_destroy(acc->regc);
963 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000964 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000965
Benny Prijonobddef2c2007-10-31 13:28:08 +0000966 /* Stop keep-alive timer if any. */
967 update_keep_alive(acc, PJ_FALSE, NULL);
968
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000969 } else if (param->code < 0 || param->code >= 300) {
970 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
971 param->code,
972 (int)param->reason.slen, param->reason.ptr));
973 pjsip_regc_destroy(acc->regc);
974 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000975 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000976
Benny Prijonobddef2c2007-10-31 13:28:08 +0000977 /* Stop keep-alive timer if any. */
978 update_keep_alive(acc, PJ_FALSE, NULL);
979
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000980 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
981
982 if (param->expiration < 1) {
983 pjsip_regc_destroy(acc->regc);
984 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000985 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000986
987 /* Stop keep-alive timer if any. */
988 update_keep_alive(acc, PJ_FALSE, NULL);
989
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000990 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
991 pjsua_var.acc[acc->index].cfg.id.ptr));
992 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000993 /* Check NAT bound address */
994 if (acc_check_nat_addr(acc, param)) {
995 /* Update address, don't notify application yet */
996 PJSUA_UNLOCK();
997 return;
998 }
999
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001000 /* Check and update Service-Route header */
1001 update_service_route(acc, param->rdata);
1002
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001003 PJ_LOG(3, (THIS_FILE,
1004 "%s: registration success, status=%d (%.*s), "
1005 "will re-register in %d seconds",
1006 pjsua_var.acc[acc->index].cfg.id.ptr,
1007 param->code,
1008 (int)param->reason.slen, param->reason.ptr,
1009 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001010
Benny Prijonobddef2c2007-10-31 13:28:08 +00001011 /* Start keep-alive timer if necessary. */
1012 update_keep_alive(acc, PJ_TRUE, param);
1013
Benny Prijono8b6834f2007-02-24 13:29:22 +00001014 /* Send initial PUBLISH if it is enabled */
1015 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1016 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001017 }
1018
1019 } else {
1020 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1021 }
1022
1023 acc->reg_last_err = param->status;
1024 acc->reg_last_code = param->code;
1025
1026 if (pjsua_var.ua_cfg.cb.on_reg_state)
1027 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1028
1029 PJSUA_UNLOCK();
1030}
1031
1032
1033/*
1034 * Initialize client registration.
1035 */
1036static pj_status_t pjsua_regc_init(int acc_id)
1037{
1038 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001039 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001040 pj_status_t status;
1041
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001042 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001043 acc = &pjsua_var.acc[acc_id];
1044
1045 if (acc->cfg.reg_uri.slen == 0) {
1046 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1047 return PJ_SUCCESS;
1048 }
1049
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001050 /* Destroy existing session, if any */
1051 if (acc->regc) {
1052 pjsip_regc_destroy(acc->regc);
1053 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001054 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001055 }
1056
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001057 /* initialize SIP registration if registrar is configured */
1058
1059 status = pjsip_regc_create( pjsua_var.endpt,
1060 acc, &regc_cb, &acc->regc);
1061
1062 if (status != PJ_SUCCESS) {
1063 pjsua_perror(THIS_FILE, "Unable to create client registration",
1064 status);
1065 return status;
1066 }
1067
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001068 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001069
1070 if (acc->contact.slen == 0) {
1071 pj_str_t tmp_contact;
1072
1073 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1074 acc_id, &acc->cfg.reg_uri);
1075 if (status != PJ_SUCCESS) {
1076 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1077 " for registration",
1078 status);
1079 pjsip_regc_destroy(acc->regc);
1080 pj_pool_release(pool);
1081 acc->regc = NULL;
1082 return status;
1083 }
1084
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001085 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001086 }
1087
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001088 status = pjsip_regc_init( acc->regc,
1089 &acc->cfg.reg_uri,
1090 &acc->cfg.id,
1091 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001092 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001093 acc->cfg.reg_timeout);
1094 if (status != PJ_SUCCESS) {
1095 pjsua_perror(THIS_FILE,
1096 "Client registration initialization error",
1097 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001098 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001099 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001100 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001101 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001102 return status;
1103 }
1104
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001105 /* If account is locked to specific transport, then set transport to
1106 * the client registration.
1107 */
1108 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1109 pjsip_tpselector tp_sel;
1110
1111 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1112 pjsip_regc_set_transport(acc->regc, &tp_sel);
1113 }
1114
1115
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001116 /* Set credentials
1117 */
1118 if (acc->cred_cnt) {
1119 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1120 }
1121
Benny Prijono48ab2b72007-11-08 09:24:30 +00001122 /* Set authentication preference */
1123 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1124
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001125 /* Set route-set
1126 */
1127 if (!pj_list_empty(&acc->route_set)) {
1128 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1129 }
1130
Benny Prijono8fc6de02006-11-11 21:25:55 +00001131 /* Add other request headers. */
1132 if (pjsua_var.ua_cfg.user_agent.slen) {
1133 pjsip_hdr hdr_list;
1134 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1135 pjsip_generic_string_hdr *h;
1136
Benny Prijono8fc6de02006-11-11 21:25:55 +00001137 pj_list_init(&hdr_list);
1138
1139 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1140 &pjsua_var.ua_cfg.user_agent);
1141 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1142
1143 pjsip_regc_add_headers(acc->regc, &hdr_list);
1144 }
1145
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001146 pj_pool_release(pool);
1147
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001148 return PJ_SUCCESS;
1149}
1150
1151
1152/*
1153 * Update registration or perform unregistration.
1154 */
1155PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1156 pj_bool_t renew)
1157{
1158 pj_status_t status = 0;
1159 pjsip_tx_data *tdata = 0;
1160
1161 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1162 PJ_EINVAL);
1163 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1164
1165 PJSUA_LOCK();
1166
1167 if (renew) {
1168 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001169 status = pjsua_regc_init(acc_id);
1170 if (status != PJ_SUCCESS) {
1171 pjsua_perror(THIS_FILE, "Unable to create registration",
1172 status);
1173 goto on_return;
1174 }
1175 }
1176 if (!pjsua_var.acc[acc_id].regc) {
1177 status = PJ_EINVALIDOP;
1178 goto on_return;
1179 }
1180
1181 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1182 &tdata);
1183
Benny Prijono28f673a2007-10-15 07:04:59 +00001184 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1185 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1186 pjsip_authorization_hdr *h;
1187 char *uri;
1188 int d;
1189
1190 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1191 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1192 uri, acc->cfg.reg_uri.slen+10);
1193 pj_assert(d > 0);
1194
1195 h = pjsip_authorization_hdr_create(tdata->pool);
1196 h->scheme = pj_str("Digest");
1197 h->credential.digest.username = acc->cred[0].username;
1198 h->credential.digest.realm = acc->srv_domain;
1199 h->credential.digest.uri = pj_str(uri);
1200 h->credential.digest.algorithm = pj_str("md5");
1201
1202 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1203 }
1204
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001205 } else {
1206 if (pjsua_var.acc[acc_id].regc == NULL) {
1207 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1208 status = PJ_EINVALIDOP;
1209 goto on_return;
1210 }
1211 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1212 }
1213
Benny Prijono56315612006-07-18 14:39:40 +00001214 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001215 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001216 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001217 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001218
1219 if (status != PJ_SUCCESS) {
1220 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1221 status);
1222 } else {
1223 PJ_LOG(3,(THIS_FILE, "%s sent",
1224 (renew? "Registration" : "Unregistration")));
1225 }
1226
1227on_return:
1228 PJSUA_UNLOCK();
1229 return status;
1230}
1231
1232
1233/*
1234 * Get account information.
1235 */
1236PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1237 pjsua_acc_info *info)
1238{
1239 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1240 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1241
1242 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001243 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001244
Benny Prijonoac623b32006-07-03 15:19:31 +00001245 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001246
1247 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1248 PJ_EINVAL);
1249 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1250
1251 PJSUA_LOCK();
1252
1253 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1254 PJSUA_UNLOCK();
1255 return PJ_EINVALIDOP;
1256 }
1257
1258 info->id = acc_id;
1259 info->is_default = (pjsua_var.default_acc == acc_id);
1260 info->acc_uri = acc_cfg->id;
1261 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1262 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001263 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1264 if (info->rpid.note.slen)
1265 info->online_status_text = info->rpid.note;
1266 else if (info->online_status)
1267 info->online_status_text = pj_str("Online");
1268 else
1269 info->online_status_text = pj_str("Offline");
1270
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001271 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001272 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1274 info->status_text = pj_str(info->buf_);
1275 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001276 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001277 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001278 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1279 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001280 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001281 info->status_text = pj_str("not registered");
1282 }
1283 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001284 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001285 info->status_text = pj_str("In Progress");
1286 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001287 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001288 info->status_text = pj_str("does not register");
1289 }
1290
1291 if (acc->regc) {
1292 pjsip_regc_info regc_info;
1293 pjsip_regc_get_info(acc->regc, &regc_info);
1294 info->expires = regc_info.next_reg;
1295 } else {
1296 info->expires = -1;
1297 }
1298
1299 PJSUA_UNLOCK();
1300
1301 return PJ_SUCCESS;
1302
1303}
1304
1305
1306/*
1307 * Enum accounts all account ids.
1308 */
1309PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1310 unsigned *count )
1311{
1312 unsigned i, c;
1313
1314 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1315
1316 PJSUA_LOCK();
1317
1318 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1319 if (!pjsua_var.acc[i].valid)
1320 continue;
1321 ids[c] = i;
1322 ++c;
1323 }
1324
1325 *count = c;
1326
1327 PJSUA_UNLOCK();
1328
1329 return PJ_SUCCESS;
1330}
1331
1332
1333/*
1334 * Enum accounts info.
1335 */
1336PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1337 unsigned *count )
1338{
1339 unsigned i, c;
1340
1341 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1342
1343 PJSUA_LOCK();
1344
1345 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1346 if (!pjsua_var.acc[i].valid)
1347 continue;
1348
1349 pjsua_acc_get_info(i, &info[c]);
1350 ++c;
1351 }
1352
1353 *count = c;
1354
1355 PJSUA_UNLOCK();
1356
1357 return PJ_SUCCESS;
1358}
1359
1360
1361/*
1362 * This is an internal function to find the most appropriate account to
1363 * used to reach to the specified URL.
1364 */
1365PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1366{
1367 pj_str_t tmp;
1368 pjsip_uri *uri;
1369 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001370 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001371 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001372
1373 PJSUA_LOCK();
1374
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001375 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001376
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001377 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001378
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001379 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001380 if (!uri) {
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 pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001384 }
1385
1386 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1387 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1388 {
1389 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001390 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1391 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001392 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001393 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001394 break;
1395 }
1396
Benny Prijono093d3022006-09-24 00:07:11 +00001397 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001398 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001399 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001400 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001401 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001402 }
1403
1404 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001405 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001406 PJSUA_UNLOCK();
1407 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001408 }
1409
Benny Prijonoa1e69682007-05-11 15:14:34 +00001410 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001411
Benny Prijonob4a17c92006-07-10 14:40:21 +00001412 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001413 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1414 unsigned acc_id = pjsua_var.acc_ids[i];
1415 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1416 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1417 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001418 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001419 PJSUA_UNLOCK();
1420 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001421 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001422 }
1423
Benny Prijonob4a17c92006-07-10 14:40:21 +00001424 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001425 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1426 unsigned acc_id = pjsua_var.acc_ids[i];
1427 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1428 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001429 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001430 PJSUA_UNLOCK();
1431 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001432 }
1433 }
1434
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001435
Benny Prijono093d3022006-09-24 00:07:11 +00001436 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001437 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001438 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001439 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001440}
1441
1442
1443/*
1444 * This is an internal function to find the most appropriate account to be
1445 * used to handle incoming calls.
1446 */
1447PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1448{
1449 pjsip_uri *uri;
1450 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001451 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001452
Benny Prijono371cf0a2007-06-19 00:35:37 +00001453 /* Check that there's at least one account configured */
1454 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1455
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001456 uri = rdata->msg_info.to->uri;
1457
1458 /* Just return default account if To URI is not SIP: */
1459 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1460 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1461 {
1462 return pjsua_var.default_acc;
1463 }
1464
1465
1466 PJSUA_LOCK();
1467
1468 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1469
1470 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001471 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1472 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001473 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1474
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001475 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001476 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001477 {
1478 /* Match ! */
1479 PJSUA_UNLOCK();
1480 return acc_id;
1481 }
1482 }
1483
Benny Prijono093d3022006-09-24 00:07:11 +00001484 /* No matching account, try match domain part only. */
1485 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1486 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001487 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1488
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001489 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001490 /* Match ! */
1491 PJSUA_UNLOCK();
1492 return acc_id;
1493 }
1494 }
1495
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001496 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001497 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1498 unsigned acc_id = pjsua_var.acc_ids[i];
1499 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1500
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001501 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1502
1503 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1504 pjsip_transport_type_e type;
1505 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1506 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1507 type = PJSIP_TRANSPORT_UDP;
1508
1509 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1510 continue;
1511 }
1512
Benny Prijono093d3022006-09-24 00:07:11 +00001513 /* Match ! */
1514 PJSUA_UNLOCK();
1515 return acc_id;
1516 }
1517 }
1518
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001519 /* Still no match, use default account */
1520 PJSUA_UNLOCK();
1521 return pjsua_var.default_acc;
1522}
1523
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001524
Benny Prijonofff245c2007-04-02 11:44:47 +00001525/*
1526 * Create arbitrary requests for this account.
1527 */
1528PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1529 const pjsip_method *method,
1530 const pj_str_t *target,
1531 pjsip_tx_data **p_tdata)
1532{
1533 pjsip_tx_data *tdata;
1534 pjsua_acc *acc;
1535 pjsip_route_hdr *r;
1536 pj_status_t status;
1537
1538 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1539 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1540
1541 acc = &pjsua_var.acc[acc_id];
1542
1543 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1544 &acc->cfg.id, target,
1545 NULL, NULL, -1, NULL, &tdata);
1546 if (status != PJ_SUCCESS) {
1547 pjsua_perror(THIS_FILE, "Unable to create request", status);
1548 return status;
1549 }
1550
1551 /* Copy routeset */
1552 r = acc->route_set.next;
1553 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001554 pjsip_msg_add_hdr(tdata->msg,
1555 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001556 r = r->next;
1557 }
1558
1559 /* Done */
1560 *p_tdata = tdata;
1561 return PJ_SUCCESS;
1562}
1563
1564
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001565PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1566 pj_str_t *contact,
1567 pjsua_acc_id acc_id,
1568 const pj_str_t *suri)
1569{
1570 pjsua_acc *acc;
1571 pjsip_sip_uri *sip_uri;
1572 pj_status_t status;
1573 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1574 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001575 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001576 unsigned flag;
1577 int secure;
1578 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001579 const char *beginquote, *endquote;
1580 char transport_param[32];
1581
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001582
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001583 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001584 acc = &pjsua_var.acc[acc_id];
1585
Benny Prijonof75eceb2007-03-23 19:09:54 +00001586 /* If force_contact is configured, then use use it */
1587 if (acc->cfg.force_contact.slen) {
1588 *contact = acc->cfg.force_contact;
1589 return PJ_SUCCESS;
1590 }
1591
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001592 /* If route-set is configured for the account, then URI is the
1593 * first entry of the route-set.
1594 */
1595 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001596 sip_uri = (pjsip_sip_uri*)
1597 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001598 } else {
1599 pj_str_t tmp;
1600 pjsip_uri *uri;
1601
1602 pj_strdup_with_null(pool, &tmp, suri);
1603
1604 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1605 if (uri == NULL)
1606 return PJSIP_EINVALIDURI;
1607
1608 /* For non-SIP scheme, route set should be configured */
1609 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1610 return PJSIP_EINVALIDREQURI;
1611
Benny Prijono8c7a6172007-02-18 21:17:46 +00001612 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001613 }
1614
1615 /* Get transport type of the URI */
1616 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1617 tp_type = PJSIP_TRANSPORT_TLS;
1618 else if (sip_uri->transport_param.slen == 0) {
1619 tp_type = PJSIP_TRANSPORT_UDP;
1620 } else
1621 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1622
1623 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1624 return PJSIP_EUNSUPTRANSPORT;
1625
Benny Prijonod0bd4982007-12-02 15:40:52 +00001626 /* If destination URI specifies IPv6, then set transport type
1627 * to use IPv6 as well.
1628 */
Benny Prijono19b29372007-12-05 04:08:40 +00001629 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001630 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1631
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001632 flag = pjsip_transport_get_flag_from_type(tp_type);
1633 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1634
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001635 /* Init transport selector. */
1636 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1637
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001638 /* Get local address suitable to send request from */
1639 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001640 pool, tp_type, &tp_sel,
1641 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001642 if (status != PJ_SUCCESS)
1643 return status;
1644
Benny Prijonod0bd4982007-12-02 15:40:52 +00001645 /* Enclose IPv6 address in square brackets */
1646 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1647 beginquote = "[";
1648 endquote = "]";
1649 } else {
1650 beginquote = endquote = "";
1651 }
1652
1653 /* Don't add transport parameter if it's UDP */
1654 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1655 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1656 ";transport=%s",
1657 pjsip_transport_get_type_name(tp_type));
1658 } else {
1659 transport_param[0] = '\0';
1660 }
1661
1662
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001663 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001664 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001665 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001666 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001667 (int)acc->display.slen,
1668 acc->display.ptr,
1669 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001670 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001671 (int)acc->user_part.slen,
1672 acc->user_part.ptr,
1673 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001674 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001675 (int)local_addr.slen,
1676 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001677 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001678 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001679 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001680
1681 return PJ_SUCCESS;
1682}
1683
1684
1685
1686PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1687 pj_str_t *contact,
1688 pjsua_acc_id acc_id,
1689 pjsip_rx_data *rdata )
1690{
1691 /*
1692 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1693 * If the request that initiated the dialog contained a SIPS URI
1694 * in the Request-URI or in the top Record-Route header field value,
1695 * if there was any, or the Contact header field if there was no
1696 * Record-Route header field, the Contact header field in the response
1697 * MUST be a SIPS URI.
1698 */
1699 pjsua_acc *acc;
1700 pjsip_sip_uri *sip_uri;
1701 pj_status_t status;
1702 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1703 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001704 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001705 unsigned flag;
1706 int secure;
1707 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001708 const char *beginquote, *endquote;
1709 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001710
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001711 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001712 acc = &pjsua_var.acc[acc_id];
1713
Benny Prijonof75eceb2007-03-23 19:09:54 +00001714 /* If force_contact is configured, then use use it */
1715 if (acc->cfg.force_contact.slen) {
1716 *contact = acc->cfg.force_contact;
1717 return PJ_SUCCESS;
1718 }
1719
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001720 /* If Record-Route is present, then URI is the top Record-Route. */
1721 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001722 sip_uri = (pjsip_sip_uri*)
1723 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001724 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00001725 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001726 pjsip_contact_hdr *h_contact;
1727 pjsip_uri *uri = NULL;
1728
Benny Prijonoa330d452008-08-05 20:14:39 +00001729 /* Otherwise URI is Contact URI.
1730 * Iterate the Contact URI until we find sip: or sips: scheme.
1731 */
1732 do {
1733 h_contact = (pjsip_contact_hdr*)
1734 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
1735 pos);
1736 if (h_contact) {
1737 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
1738 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1739 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1740 {
1741 pos = (pjsip_hdr*)h_contact->next;
1742 if (pos == &rdata->msg_info.msg->hdr)
1743 h_contact = NULL;
1744 } else {
1745 break;
1746 }
1747 }
1748 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001749
1750
1751 /* Or if Contact URI is not present, take the remote URI from
1752 * the From URI.
1753 */
1754 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001755 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001756
1757
1758 /* Can only do sip/sips scheme at present. */
1759 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1760 return PJSIP_EINVALIDREQURI;
1761
Benny Prijono8c7a6172007-02-18 21:17:46 +00001762 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001763 }
1764
1765 /* Get transport type of the URI */
1766 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1767 tp_type = PJSIP_TRANSPORT_TLS;
1768 else if (sip_uri->transport_param.slen == 0) {
1769 tp_type = PJSIP_TRANSPORT_UDP;
1770 } else
1771 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001772
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001773 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1774 return PJSIP_EUNSUPTRANSPORT;
1775
Benny Prijonod0bd4982007-12-02 15:40:52 +00001776 /* If destination URI specifies IPv6, then set transport type
1777 * to use IPv6 as well.
1778 */
1779 if (pj_strchr(&sip_uri->host, ':'))
1780 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1781
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001782 flag = pjsip_transport_get_flag_from_type(tp_type);
1783 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1784
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001785 /* Init transport selector. */
1786 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1787
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001788 /* Get local address suitable to send request from */
1789 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001790 pool, tp_type, &tp_sel,
1791 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001792 if (status != PJ_SUCCESS)
1793 return status;
1794
Benny Prijonod0bd4982007-12-02 15:40:52 +00001795 /* Enclose IPv6 address in square brackets */
1796 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1797 beginquote = "[";
1798 endquote = "]";
1799 } else {
1800 beginquote = endquote = "";
1801 }
1802
1803 /* Don't add transport parameter if it's UDP */
1804 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1805 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1806 ";transport=%s",
1807 pjsip_transport_get_type_name(tp_type));
1808 } else {
1809 transport_param[0] = '\0';
1810 }
1811
1812
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001813 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001814 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001815 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001816 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001817 (int)acc->display.slen,
1818 acc->display.ptr,
1819 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001820 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001821 (int)acc->user_part.slen,
1822 acc->user_part.ptr,
1823 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001824 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001825 (int)local_addr.slen,
1826 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001827 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001828 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001829 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001830
1831 return PJ_SUCCESS;
1832}
1833
1834
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001835PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1836 pjsua_transport_id tp_id)
1837{
1838 pjsua_acc *acc;
1839
1840 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1841 acc = &pjsua_var.acc[acc_id];
1842
Benny Prijonoa1e69682007-05-11 15:14:34 +00001843 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001844 PJ_EINVAL);
1845
1846 acc->cfg.transport_id = tp_id;
1847
1848 return PJ_SUCCESS;
1849}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001850