blob: 482d6bbe286275bebc30e4e8356bfd113833c2ed [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20#include <pjsua-lib/pjsua_internal.h>
21
22
23#define THIS_FILE "pjsua_acc.c"
24
25
26/*
27 * Get number of current accounts.
28 */
29PJ_DEF(unsigned) pjsua_acc_get_count(void)
30{
31 return pjsua_var.acc_cnt;
32}
33
34
35/*
36 * Check if the specified account ID is valid.
37 */
38PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id)
39{
Benny Prijonoa1e69682007-05-11 15:14:34 +000040 return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) &&
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041 pjsua_var.acc[acc_id].valid;
42}
43
44
45/*
Benny Prijono21b9ad92006-08-15 13:11:22 +000046 * Set default account
47 */
48PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id)
49{
50 pjsua_var.default_acc = acc_id;
51 return PJ_SUCCESS;
52}
53
54
55/*
56 * Get default account.
57 */
58PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void)
59{
60 return pjsua_var.default_acc;
61}
62
63
64/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000065 * Copy account configuration.
66 */
67static void copy_acc_config(pj_pool_t *pool,
68 pjsua_acc_config *dst,
69 const pjsua_acc_config *src)
70{
71 unsigned i;
72
73 pj_memcpy(dst, src, sizeof(pjsua_acc_config));
74
75 pj_strdup_with_null(pool, &dst->id, &src->id);
76 pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
Benny Prijonob4a17c92006-07-10 14:40:21 +000077 pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
Benny 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 }
90}
91
92
93/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000094 * Initialize a new account (after configuration is set).
95 */
96static pj_status_t initialize_acc(unsigned acc_id)
97{
98 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
99 pjsua_acc *acc = &pjsua_var.acc[acc_id];
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000100 pjsip_name_addr *name_addr;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000101 pjsip_sip_uri *sip_uri, *sip_reg_uri;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000102 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103 unsigned i;
104
105 /* Need to parse local_uri to get the elements: */
106
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000107 name_addr = (pjsip_name_addr*)
108 pjsip_parse_uri(pjsua_var.pool, acc_cfg->id.ptr,
109 acc_cfg->id.slen,
110 PJSIP_PARSE_URI_AS_NAMEADDR);
111 if (name_addr == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000112 pjsua_perror(THIS_FILE, "Invalid local URI",
113 PJSIP_EINVALIDURI);
114 return PJSIP_EINVALIDURI;
115 }
116
117 /* Local URI MUST be a SIP or SIPS: */
118
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000119 if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) &&
120 !PJSIP_URI_SCHEME_IS_SIPS(name_addr))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000121 {
122 pjsua_perror(THIS_FILE, "Invalid local URI",
123 PJSIP_EINVALIDSCHEME);
124 return PJSIP_EINVALIDSCHEME;
125 }
126
127
128 /* Get the SIP URI object: */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000129 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000130
Benny Prijonob4a17c92006-07-10 14:40:21 +0000131
132 /* Parse registrar URI, if any */
133 if (acc_cfg->reg_uri.slen) {
134 pjsip_uri *reg_uri;
135
136 reg_uri = pjsip_parse_uri(pjsua_var.pool, acc_cfg->reg_uri.ptr,
137 acc_cfg->reg_uri.slen, 0);
138 if (reg_uri == NULL) {
139 pjsua_perror(THIS_FILE, "Invalid registrar URI",
140 PJSIP_EINVALIDURI);
141 return PJSIP_EINVALIDURI;
142 }
143
144 /* Registrar URI MUST be a SIP or SIPS: */
145 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
146 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
147 {
148 pjsua_perror(THIS_FILE, "Invalid registar URI",
149 PJSIP_EINVALIDSCHEME);
150 return PJSIP_EINVALIDSCHEME;
151 }
152
153 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
154
155 } else {
156 sip_reg_uri = NULL;
157 }
158
159
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000160 /* Save the user and domain part. These will be used when finding an
161 * account for incoming requests.
162 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000163 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000164 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000165 acc->srv_domain = sip_uri->host;
166 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000167
Benny Prijonob4a17c92006-07-10 14:40:21 +0000168 if (sip_reg_uri) {
169 acc->srv_port = sip_reg_uri->port;
170 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000171
172 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000173 //if (acc_cfg->contact.slen == 0) {
174 // acc_cfg->contact = acc_cfg->id;
175 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000176
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000177 /* Build account route-set from outbound proxies and route set from
178 * account configuration.
179 */
180 pj_list_init(&acc->route_set);
181
182 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
183 pj_str_t hname = { "Route", 5};
184 pjsip_route_hdr *r;
185 pj_str_t tmp;
186
187 pj_strdup_with_null(pjsua_var.pool, &tmp,
188 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000189 r = (pjsip_route_hdr*)
190 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191 if (r == NULL) {
192 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
193 PJSIP_EINVALIDURI);
194 return PJSIP_EINVALIDURI;
195 }
196 pj_list_push_back(&acc->route_set, r);
197 }
198
199 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
200 pj_str_t hname = { "Route", 5};
201 pjsip_route_hdr *r;
202 pj_str_t tmp;
203
204 pj_strdup_with_null(pjsua_var.pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000205 r = (pjsip_route_hdr*)
206 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000207 if (r == NULL) {
208 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
209 PJ_EINVAL);
210 return PJ_EINVAL;
211 }
212 pj_list_push_back(&acc->route_set, r);
213 }
214
215
216 /* Concatenate credentials from account config and global config */
217 acc->cred_cnt = 0;
218 for (i=0; i<acc_cfg->cred_count; ++i) {
219 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
220 }
221 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
222 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
223 {
224 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
225 }
226
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000227 status = pjsua_pres_init_acc(acc_id);
228 if (status != PJ_SUCCESS)
229 return status;
230
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000231 /* Mark account as valid */
232 pjsua_var.acc[acc_id].valid = PJ_TRUE;
233
Benny Prijono093d3022006-09-24 00:07:11 +0000234 /* Insert account ID into account ID array, sorted by priority */
235 for (i=0; i<pjsua_var.acc_cnt; ++i) {
236 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
237 pjsua_var.acc[acc_id].cfg.priority)
238 {
239 break;
240 }
241 }
242 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
243 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244
245 return PJ_SUCCESS;
246}
247
248
249/*
250 * Add a new account to pjsua.
251 */
252PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
253 pj_bool_t is_default,
254 pjsua_acc_id *p_acc_id)
255{
256 unsigned id;
257 pj_status_t status;
258
259 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
260 PJ_ETOOMANY);
261
262 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000263 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000264
265 PJSUA_LOCK();
266
267 /* Find empty account id. */
268 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
269 if (pjsua_var.acc[id].valid == PJ_FALSE)
270 break;
271 }
272
273 /* Expect to find a slot */
274 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
275 {PJSUA_UNLOCK(); return PJ_EBUG;});
276
277 /* Copy config */
278 copy_acc_config(pjsua_var.pool, &pjsua_var.acc[id].cfg, cfg);
279
280 /* Normalize registration timeout */
281 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
282 pjsua_var.acc[id].cfg.reg_timeout == 0)
283 {
284 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
285 }
286
287 status = initialize_acc(id);
288 if (status != PJ_SUCCESS) {
289 pjsua_perror(THIS_FILE, "Error adding account", status);
290 PJSUA_UNLOCK();
291 return status;
292 }
293
294 if (is_default)
295 pjsua_var.default_acc = id;
296
297 if (p_acc_id)
298 *p_acc_id = id;
299
300 pjsua_var.acc_cnt++;
301
302 PJSUA_UNLOCK();
303
304 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
305 (int)cfg->id.slen, cfg->id.ptr, id));
306
307 /* If accounts has registration enabled, start registration */
308 if (pjsua_var.acc[id].cfg.reg_uri.slen)
309 pjsua_acc_set_registration(id, PJ_TRUE);
310
311
312 return PJ_SUCCESS;
313}
314
315
316/*
317 * Add local account
318 */
319PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
320 pj_bool_t is_default,
321 pjsua_acc_id *p_acc_id)
322{
323 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000324 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000325 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000326
Benny Prijonoe93e2872006-06-28 16:46:49 +0000327 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000328 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
329 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000330
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000331 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000332 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000333
334 pjsua_acc_config_default(&cfg);
335
Benny Prijono093d3022006-09-24 00:07:11 +0000336 /* Lower the priority of local account */
337 --cfg.priority;
338
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000339 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000340 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
341 "<sip:%.*s:%d;transport=%s>",
342 (int)t->local_name.host.slen,
343 t->local_name.host.ptr,
344 t->local_name.port,
345 pjsip_transport_get_type_name(t->type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000346
347 cfg.id = pj_str(uri);
348
349 return pjsua_acc_add(&cfg, is_default, p_acc_id);
350}
351
352
353/*
354 * Delete account.
355 */
356PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
357{
Benny Prijono093d3022006-09-24 00:07:11 +0000358 unsigned i;
359
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000360 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
361 PJ_EINVAL);
362 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
363
364 PJSUA_LOCK();
365
366 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000367 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000369 if (pjsua_var.acc[acc_id].regc) {
370 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
371 }
Benny Prijono922933b2007-01-21 16:23:56 +0000372 pjsua_var.acc[acc_id].regc = NULL;
373 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000374
375 /* Delete server presence subscription */
376 pjsua_pres_delete_acc(acc_id);
377
378 /* Invalidate */
379 pjsua_var.acc[acc_id].valid = PJ_FALSE;
380
Benny Prijono093d3022006-09-24 00:07:11 +0000381 /* Remove from array */
382 for (i=0; i<pjsua_var.acc_cnt; ++i) {
383 if (pjsua_var.acc_ids[i] == acc_id)
384 break;
385 }
386 if (i != pjsua_var.acc_cnt) {
387 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
388 pjsua_var.acc_cnt, i);
389 --pjsua_var.acc_cnt;
390 }
391
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000392 /* Leave the calls intact, as I don't think calls need to
393 * access account once it's created
394 */
395
Benny Prijonofb2b3652007-06-28 07:15:03 +0000396 /* Update default account */
397 if (pjsua_var.default_acc == acc_id)
398 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000399
400 PJSUA_UNLOCK();
401
402 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
403
404 return PJ_SUCCESS;
405}
406
407
408/*
409 * Modify account information.
410 */
411PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
412 const pjsua_acc_config *cfg)
413{
414 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000415 PJ_UNUSED_ARG(acc_id);
416 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 return PJ_EINVALIDOP;
418}
419
420
421/*
422 * Modify account's presence status to be advertised to remote/presence
423 * subscribers.
424 */
425PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
426 pj_bool_t is_online)
427{
428 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
429 PJ_EINVAL);
430 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
431
432 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000433 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
434 pjsua_pres_update_acc(acc_id, PJ_FALSE);
435 return PJ_SUCCESS;
436}
437
438
439/*
440 * Set online status with extended information
441 */
442PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
443 pj_bool_t is_online,
444 const pjrpid_element *pr)
445{
446 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
447 PJ_EINVAL);
448 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
449
450 pjsua_var.acc[acc_id].online_status = is_online;
451 pjrpid_element_dup(pjsua_var.pool, &pjsua_var.acc[acc_id].rpid, pr);
452 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000453 return PJ_SUCCESS;
454}
455
456
Benny Prijono15b02302007-09-27 14:07:07 +0000457/* Update NAT address from the REGISTER response */
458static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
459 struct pjsip_regc_cbparam *param)
460{
461 pjsip_transport *tp;
462 const pj_str_t *via_addr;
463 int rport;
464 pjsip_via_hdr *via;
465
466 tp = param->rdata->tp_info.transport;
467
468 /* Only update if account is configured to auto-update */
469 if (acc->cfg.auto_update_nat == PJ_FALSE)
470 return PJ_FALSE;
471
472 /* Only update if registration uses UDP transport */
473 if (tp->key.type != PJSIP_TRANSPORT_UDP)
474 return PJ_FALSE;
475
476 /* Only update if STUN is enabled (for now) */
477 if (pjsua_var.ua_cfg.stun_domain.slen == 0 &&
478 pjsua_var.ua_cfg.stun_host.slen == 0)
479 {
480 return PJ_FALSE;
481 }
482
483 /* Get the received and rport info */
484 via = param->rdata->msg_info.via;
485 if (via->rport_param < 1) {
486 /* Remote doesn't support rport */
487 rport = via->sent_by.port;
488 } else
489 rport = via->rport_param;
490
491 if (via->recvd_param.slen != 0)
492 via_addr = &via->recvd_param;
493 else
494 via_addr = &via->sent_by.host;
495
496 /* Compare received and rport with transport published address */
497 if (tp->local_name.port == rport &&
498 pj_stricmp(&tp->local_name.host, via_addr)==0)
499 {
500 /* Address doesn't change */
501 return PJ_FALSE;
502 }
503
504 /* At this point we've detected that the address as seen by registrar.
505 * has changed.
506 */
507 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
508 "(%.*s:%d --> %.*s:%d). Updating registration..",
509 acc->index,
510 (int)tp->local_name.host.slen,
511 tp->local_name.host.ptr,
512 tp->local_name.port,
513 (int)via_addr->slen,
514 via_addr->ptr,
515 rport));
516
517 /* Unregister current contact */
518 pjsua_acc_set_registration(acc->index, PJ_FALSE);
519 if (acc->regc != NULL) {
520 pjsip_regc_destroy(acc->regc);
521 acc->regc = NULL;
522 }
523
524 /* Update transport address */
525 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
526 tp->local_name.port = rport;
527
528 /* Perform new registration */
529 pjsua_acc_set_registration(acc->index, PJ_TRUE);
530
531 return PJ_TRUE;
532}
533
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534/*
535 * This callback is called by pjsip_regc when outgoing register
536 * request has completed.
537 */
538static void regc_cb(struct pjsip_regc_cbparam *param)
539{
540
Benny Prijonoa1e69682007-05-11 15:14:34 +0000541 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542
Benny Prijono15b02302007-09-27 14:07:07 +0000543 if (param->regc != acc->regc)
544 return;
545
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000546 PJSUA_LOCK();
547
548 /*
549 * Print registration status.
550 */
551 if (param->status!=PJ_SUCCESS) {
552 pjsua_perror(THIS_FILE, "SIP registration error",
553 param->status);
554 pjsip_regc_destroy(acc->regc);
555 acc->regc = NULL;
556
557 } else if (param->code < 0 || param->code >= 300) {
558 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
559 param->code,
560 (int)param->reason.slen, param->reason.ptr));
561 pjsip_regc_destroy(acc->regc);
562 acc->regc = NULL;
563
564 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
565
566 if (param->expiration < 1) {
567 pjsip_regc_destroy(acc->regc);
568 acc->regc = NULL;
569 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
570 pjsua_var.acc[acc->index].cfg.id.ptr));
571 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000572 /* Check NAT bound address */
573 if (acc_check_nat_addr(acc, param)) {
574 /* Update address, don't notify application yet */
575 PJSUA_UNLOCK();
576 return;
577 }
578
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000579 PJ_LOG(3, (THIS_FILE,
580 "%s: registration success, status=%d (%.*s), "
581 "will re-register in %d seconds",
582 pjsua_var.acc[acc->index].cfg.id.ptr,
583 param->code,
584 (int)param->reason.slen, param->reason.ptr,
585 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000586
587 /* Send initial PUBLISH if it is enabled */
588 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
589 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000590 }
591
592 } else {
593 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
594 }
595
596 acc->reg_last_err = param->status;
597 acc->reg_last_code = param->code;
598
599 if (pjsua_var.ua_cfg.cb.on_reg_state)
600 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
601
602 PJSUA_UNLOCK();
603}
604
605
606/*
607 * Initialize client registration.
608 */
609static pj_status_t pjsua_regc_init(int acc_id)
610{
611 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000612 pj_str_t contact;
Benny Prijonodfb7d482006-10-18 20:35:14 +0000613 char contact_buf[1024];
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000614 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000615 pj_status_t status;
616
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000617 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000618 acc = &pjsua_var.acc[acc_id];
619
620 if (acc->cfg.reg_uri.slen == 0) {
621 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
622 return PJ_SUCCESS;
623 }
624
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000625 /* Destroy existing session, if any */
626 if (acc->regc) {
627 pjsip_regc_destroy(acc->regc);
628 acc->regc = NULL;
629 }
630
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000631 /* initialize SIP registration if registrar is configured */
632
633 status = pjsip_regc_create( pjsua_var.endpt,
634 acc, &regc_cb, &acc->regc);
635
636 if (status != PJ_SUCCESS) {
637 pjsua_perror(THIS_FILE, "Unable to create client registration",
638 status);
639 return status;
640 }
641
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000642 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
643 status = pjsua_acc_create_uac_contact( pool, &contact,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000644 acc_id, &acc->cfg.reg_uri);
645 if (status != PJ_SUCCESS) {
646 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
647 " for registration",
648 status);
649 return status;
650 }
651
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000652 status = pjsip_regc_init( acc->regc,
653 &acc->cfg.reg_uri,
654 &acc->cfg.id,
655 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000656 1, &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000657 acc->cfg.reg_timeout);
658 if (status != PJ_SUCCESS) {
659 pjsua_perror(THIS_FILE,
660 "Client registration initialization error",
661 status);
662 return status;
663 }
664
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000665 /* If account is locked to specific transport, then set transport to
666 * the client registration.
667 */
668 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
669 pjsip_tpselector tp_sel;
670
671 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
672 pjsip_regc_set_transport(acc->regc, &tp_sel);
673 }
674
675
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000676 /* Set credentials
677 */
678 if (acc->cred_cnt) {
679 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
680 }
681
682 /* Set route-set
683 */
684 if (!pj_list_empty(&acc->route_set)) {
685 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
686 }
687
Benny Prijono8fc6de02006-11-11 21:25:55 +0000688 /* Add other request headers. */
689 if (pjsua_var.ua_cfg.user_agent.slen) {
690 pjsip_hdr hdr_list;
691 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
692 pjsip_generic_string_hdr *h;
693
694 pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf));
695 pj_list_init(&hdr_list);
696
697 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
698 &pjsua_var.ua_cfg.user_agent);
699 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
700
701 pjsip_regc_add_headers(acc->regc, &hdr_list);
702 }
703
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000704 return PJ_SUCCESS;
705}
706
707
708/*
709 * Update registration or perform unregistration.
710 */
711PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
712 pj_bool_t renew)
713{
714 pj_status_t status = 0;
715 pjsip_tx_data *tdata = 0;
716
717 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
718 PJ_EINVAL);
719 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
720
721 PJSUA_LOCK();
722
723 if (renew) {
724 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000725 status = pjsua_regc_init(acc_id);
726 if (status != PJ_SUCCESS) {
727 pjsua_perror(THIS_FILE, "Unable to create registration",
728 status);
729 goto on_return;
730 }
731 }
732 if (!pjsua_var.acc[acc_id].regc) {
733 status = PJ_EINVALIDOP;
734 goto on_return;
735 }
736
737 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
738 &tdata);
739
Benny Prijono28f673a2007-10-15 07:04:59 +0000740 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
741 pjsua_acc *acc = &pjsua_var.acc[acc_id];
742 pjsip_authorization_hdr *h;
743 char *uri;
744 int d;
745
746 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
747 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
748 uri, acc->cfg.reg_uri.slen+10);
749 pj_assert(d > 0);
750
751 h = pjsip_authorization_hdr_create(tdata->pool);
752 h->scheme = pj_str("Digest");
753 h->credential.digest.username = acc->cred[0].username;
754 h->credential.digest.realm = acc->srv_domain;
755 h->credential.digest.uri = pj_str(uri);
756 h->credential.digest.algorithm = pj_str("md5");
757
758 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
759 }
760
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000761 } else {
762 if (pjsua_var.acc[acc_id].regc == NULL) {
763 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
764 status = PJ_EINVALIDOP;
765 goto on_return;
766 }
767 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
768 }
769
Benny Prijono56315612006-07-18 14:39:40 +0000770 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +0000771 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000772 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +0000773 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000774
775 if (status != PJ_SUCCESS) {
776 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
777 status);
778 } else {
779 PJ_LOG(3,(THIS_FILE, "%s sent",
780 (renew? "Registration" : "Unregistration")));
781 }
782
783on_return:
784 PJSUA_UNLOCK();
785 return status;
786}
787
788
789/*
790 * Get account information.
791 */
792PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
793 pjsua_acc_info *info)
794{
795 pjsua_acc *acc = &pjsua_var.acc[acc_id];
796 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
797
798 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000799 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000800
Benny Prijonoac623b32006-07-03 15:19:31 +0000801 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000802
803 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
804 PJ_EINVAL);
805 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
806
807 PJSUA_LOCK();
808
809 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
810 PJSUA_UNLOCK();
811 return PJ_EINVALIDOP;
812 }
813
814 info->id = acc_id;
815 info->is_default = (pjsua_var.default_acc == acc_id);
816 info->acc_uri = acc_cfg->id;
817 info->has_registration = (acc->cfg.reg_uri.slen > 0);
818 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000819 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
820 if (info->rpid.note.slen)
821 info->online_status_text = info->rpid.note;
822 else if (info->online_status)
823 info->online_status_text = pj_str("Online");
824 else
825 info->online_status_text = pj_str("Offline");
826
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000827 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000828 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000829 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
830 info->status_text = pj_str(info->buf_);
831 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +0000832 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000833 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000834 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
835 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000836 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000837 info->status_text = pj_str("not registered");
838 }
839 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000840 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000841 info->status_text = pj_str("In Progress");
842 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +0000843 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000844 info->status_text = pj_str("does not register");
845 }
846
847 if (acc->regc) {
848 pjsip_regc_info regc_info;
849 pjsip_regc_get_info(acc->regc, &regc_info);
850 info->expires = regc_info.next_reg;
851 } else {
852 info->expires = -1;
853 }
854
855 PJSUA_UNLOCK();
856
857 return PJ_SUCCESS;
858
859}
860
861
862/*
863 * Enum accounts all account ids.
864 */
865PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
866 unsigned *count )
867{
868 unsigned i, c;
869
870 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
871
872 PJSUA_LOCK();
873
874 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
875 if (!pjsua_var.acc[i].valid)
876 continue;
877 ids[c] = i;
878 ++c;
879 }
880
881 *count = c;
882
883 PJSUA_UNLOCK();
884
885 return PJ_SUCCESS;
886}
887
888
889/*
890 * Enum accounts info.
891 */
892PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
893 unsigned *count )
894{
895 unsigned i, c;
896
897 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
898
899 PJSUA_LOCK();
900
901 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
902 if (!pjsua_var.acc[i].valid)
903 continue;
904
905 pjsua_acc_get_info(i, &info[c]);
906 ++c;
907 }
908
909 *count = c;
910
911 PJSUA_UNLOCK();
912
913 return PJ_SUCCESS;
914}
915
916
917/*
918 * This is an internal function to find the most appropriate account to
919 * used to reach to the specified URL.
920 */
921PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
922{
923 pj_str_t tmp;
924 pjsip_uri *uri;
925 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +0000926 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000927
928 PJSUA_LOCK();
929
930 PJ_TODO(dont_use_pjsua_pool);
931
932 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
933
934 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
935 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +0000936 PJSUA_UNLOCK();
937 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000938 }
939
940 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
941 !PJSIP_URI_SCHEME_IS_SIPS(uri))
942 {
943 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +0000944 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
945 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000946 continue;
Benny Prijono093d3022006-09-24 00:07:11 +0000947 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000948 break;
949 }
950
Benny Prijono093d3022006-09-24 00:07:11 +0000951 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000952 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +0000953 PJSUA_UNLOCK();
954 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000955 }
956
957 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +0000958 PJSUA_UNLOCK();
959 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000960 }
961
Benny Prijonoa1e69682007-05-11 15:14:34 +0000962 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000963
Benny Prijonob4a17c92006-07-10 14:40:21 +0000964 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +0000965 for (i=0; i<pjsua_var.acc_cnt; ++i) {
966 unsigned acc_id = pjsua_var.acc_ids[i];
967 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
968 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
969 {
970 PJSUA_UNLOCK();
971 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +0000972 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000973 }
974
Benny Prijonob4a17c92006-07-10 14:40:21 +0000975 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +0000976 for (i=0; i<pjsua_var.acc_cnt; ++i) {
977 unsigned acc_id = pjsua_var.acc_ids[i];
978 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
979 {
980 PJSUA_UNLOCK();
981 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000982 }
983 }
984
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000985
Benny Prijono093d3022006-09-24 00:07:11 +0000986 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000987 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +0000988 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000989}
990
991
992/*
993 * This is an internal function to find the most appropriate account to be
994 * used to handle incoming calls.
995 */
996PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
997{
998 pjsip_uri *uri;
999 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001000 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001001
Benny Prijono371cf0a2007-06-19 00:35:37 +00001002 /* Check that there's at least one account configured */
1003 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1004
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001005 uri = rdata->msg_info.to->uri;
1006
1007 /* Just return default account if To URI is not SIP: */
1008 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1009 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1010 {
1011 return pjsua_var.default_acc;
1012 }
1013
1014
1015 PJSUA_LOCK();
1016
1017 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1018
1019 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001020 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1021 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001022 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1023
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001024 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001025 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001026 {
1027 /* Match ! */
1028 PJSUA_UNLOCK();
1029 return acc_id;
1030 }
1031 }
1032
Benny Prijono093d3022006-09-24 00:07:11 +00001033 /* No matching account, try match domain part only. */
1034 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1035 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001036 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1037
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001038 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001039 /* Match ! */
1040 PJSUA_UNLOCK();
1041 return acc_id;
1042 }
1043 }
1044
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001045 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001046 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1047 unsigned acc_id = pjsua_var.acc_ids[i];
1048 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1049
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001050 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1051
1052 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1053 pjsip_transport_type_e type;
1054 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1055 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1056 type = PJSIP_TRANSPORT_UDP;
1057
1058 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1059 continue;
1060 }
1061
Benny Prijono093d3022006-09-24 00:07:11 +00001062 /* Match ! */
1063 PJSUA_UNLOCK();
1064 return acc_id;
1065 }
1066 }
1067
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001068 /* Still no match, use default account */
1069 PJSUA_UNLOCK();
1070 return pjsua_var.default_acc;
1071}
1072
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001073
Benny Prijonofff245c2007-04-02 11:44:47 +00001074/*
1075 * Create arbitrary requests for this account.
1076 */
1077PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1078 const pjsip_method *method,
1079 const pj_str_t *target,
1080 pjsip_tx_data **p_tdata)
1081{
1082 pjsip_tx_data *tdata;
1083 pjsua_acc *acc;
1084 pjsip_route_hdr *r;
1085 pj_status_t status;
1086
1087 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1088 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1089
1090 acc = &pjsua_var.acc[acc_id];
1091
1092 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1093 &acc->cfg.id, target,
1094 NULL, NULL, -1, NULL, &tdata);
1095 if (status != PJ_SUCCESS) {
1096 pjsua_perror(THIS_FILE, "Unable to create request", status);
1097 return status;
1098 }
1099
1100 /* Copy routeset */
1101 r = acc->route_set.next;
1102 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001103 pjsip_msg_add_hdr(tdata->msg,
1104 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001105 r = r->next;
1106 }
1107
1108 /* Done */
1109 *p_tdata = tdata;
1110 return PJ_SUCCESS;
1111}
1112
1113
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001114PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1115 pj_str_t *contact,
1116 pjsua_acc_id acc_id,
1117 const pj_str_t *suri)
1118{
1119 pjsua_acc *acc;
1120 pjsip_sip_uri *sip_uri;
1121 pj_status_t status;
1122 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1123 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001124 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001125 unsigned flag;
1126 int secure;
1127 int local_port;
1128
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001129 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001130 acc = &pjsua_var.acc[acc_id];
1131
Benny Prijonof75eceb2007-03-23 19:09:54 +00001132 /* If force_contact is configured, then use use it */
1133 if (acc->cfg.force_contact.slen) {
1134 *contact = acc->cfg.force_contact;
1135 return PJ_SUCCESS;
1136 }
1137
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001138 /* If route-set is configured for the account, then URI is the
1139 * first entry of the route-set.
1140 */
1141 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001142 sip_uri = (pjsip_sip_uri*)
1143 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001144 } else {
1145 pj_str_t tmp;
1146 pjsip_uri *uri;
1147
1148 pj_strdup_with_null(pool, &tmp, suri);
1149
1150 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1151 if (uri == NULL)
1152 return PJSIP_EINVALIDURI;
1153
1154 /* For non-SIP scheme, route set should be configured */
1155 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1156 return PJSIP_EINVALIDREQURI;
1157
Benny Prijono8c7a6172007-02-18 21:17:46 +00001158 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001159 }
1160
1161 /* Get transport type of the URI */
1162 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1163 tp_type = PJSIP_TRANSPORT_TLS;
1164 else if (sip_uri->transport_param.slen == 0) {
1165 tp_type = PJSIP_TRANSPORT_UDP;
1166 } else
1167 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1168
1169 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1170 return PJSIP_EUNSUPTRANSPORT;
1171
1172 flag = pjsip_transport_get_flag_from_type(tp_type);
1173 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1174
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001175 /* Init transport selector. */
1176 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1177
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001178 /* Get local address suitable to send request from */
1179 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001180 pool, tp_type, &tp_sel,
1181 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001182 if (status != PJ_SUCCESS)
1183 return status;
1184
1185 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001186 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001187 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1188 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1189 (int)acc->display.slen,
1190 acc->display.ptr,
1191 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001192 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001193 (int)acc->user_part.slen,
1194 acc->user_part.ptr,
1195 (acc->user_part.slen?"@":""),
1196 (int)local_addr.slen,
1197 local_addr.ptr,
1198 local_port,
1199 pjsip_transport_get_type_name(tp_type));
1200
1201 return PJ_SUCCESS;
1202}
1203
1204
1205
1206PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1207 pj_str_t *contact,
1208 pjsua_acc_id acc_id,
1209 pjsip_rx_data *rdata )
1210{
1211 /*
1212 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1213 * If the request that initiated the dialog contained a SIPS URI
1214 * in the Request-URI or in the top Record-Route header field value,
1215 * if there was any, or the Contact header field if there was no
1216 * Record-Route header field, the Contact header field in the response
1217 * MUST be a SIPS URI.
1218 */
1219 pjsua_acc *acc;
1220 pjsip_sip_uri *sip_uri;
1221 pj_status_t status;
1222 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1223 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001224 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001225 unsigned flag;
1226 int secure;
1227 int local_port;
1228
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001229 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001230 acc = &pjsua_var.acc[acc_id];
1231
Benny Prijonof75eceb2007-03-23 19:09:54 +00001232 /* If force_contact is configured, then use use it */
1233 if (acc->cfg.force_contact.slen) {
1234 *contact = acc->cfg.force_contact;
1235 return PJ_SUCCESS;
1236 }
1237
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001238 /* If Record-Route is present, then URI is the top Record-Route. */
1239 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001240 sip_uri = (pjsip_sip_uri*)
1241 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001242 } else {
1243 pjsip_contact_hdr *h_contact;
1244 pjsip_uri *uri = NULL;
1245
1246 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001247 h_contact = (pjsip_contact_hdr*)
1248 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001249 NULL);
1250 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001251 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001252
1253
1254 /* Or if Contact URI is not present, take the remote URI from
1255 * the From URI.
1256 */
1257 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001258 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001259
1260
1261 /* Can only do sip/sips scheme at present. */
1262 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1263 return PJSIP_EINVALIDREQURI;
1264
Benny Prijono8c7a6172007-02-18 21:17:46 +00001265 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001266 }
1267
1268 /* Get transport type of the URI */
1269 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1270 tp_type = PJSIP_TRANSPORT_TLS;
1271 else if (sip_uri->transport_param.slen == 0) {
1272 tp_type = PJSIP_TRANSPORT_UDP;
1273 } else
1274 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1275
1276 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1277 return PJSIP_EUNSUPTRANSPORT;
1278
1279 flag = pjsip_transport_get_flag_from_type(tp_type);
1280 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1281
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001282 /* Init transport selector. */
1283 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1284
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001285 /* Get local address suitable to send request from */
1286 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001287 pool, tp_type, &tp_sel,
1288 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001289 if (status != PJ_SUCCESS)
1290 return status;
1291
1292 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001293 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001294 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1295 "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>",
1296 (int)acc->display.slen,
1297 acc->display.ptr,
1298 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001299 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001300 (int)acc->user_part.slen,
1301 acc->user_part.ptr,
1302 (acc->user_part.slen?"@":""),
1303 (int)local_addr.slen,
1304 local_addr.ptr,
1305 local_port,
1306 pjsip_transport_get_type_name(tp_type));
1307
1308 return PJ_SUCCESS;
1309}
1310
1311
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001312PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1313 pjsua_transport_id tp_id)
1314{
1315 pjsua_acc *acc;
1316
1317 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1318 acc = &pjsua_var.acc[acc_id];
1319
Benny Prijonoa1e69682007-05-11 15:14:34 +00001320 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001321 PJ_EINVAL);
1322
1323 acc->cfg.transport_id = tp_id;
1324
1325 return PJ_SUCCESS;
1326}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001327