blob: cd1b5931063abb41fe2706dd3331286cffe7c938 [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 */
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*)
111 pjsip_parse_uri(pjsua_var.pool, acc_cfg->id.ptr,
112 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
139 reg_uri = pjsip_parse_uri(pjsua_var.pool, acc_cfg->reg_uri.ptr,
140 acc_cfg->reg_uri.slen, 0);
141 if (reg_uri == NULL) {
142 pjsua_perror(THIS_FILE, "Invalid registrar URI",
143 PJSIP_EINVALIDURI);
144 return PJSIP_EINVALIDURI;
145 }
146
147 /* Registrar URI MUST be a SIP or SIPS: */
148 if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) &&
149 !PJSIP_URI_SCHEME_IS_SIPS(reg_uri))
150 {
151 pjsua_perror(THIS_FILE, "Invalid registar URI",
152 PJSIP_EINVALIDSCHEME);
153 return PJSIP_EINVALIDSCHEME;
154 }
155
156 sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
157
158 } else {
159 sip_reg_uri = NULL;
160 }
161
162
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000163 /* Save the user and domain part. These will be used when finding an
164 * account for incoming requests.
165 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000166 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000167 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000168 acc->srv_domain = sip_uri->host;
169 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000170
Benny Prijonob4a17c92006-07-10 14:40:21 +0000171 if (sip_reg_uri) {
172 acc->srv_port = sip_reg_uri->port;
173 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174
175 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000176 //if (acc_cfg->contact.slen == 0) {
177 // acc_cfg->contact = acc_cfg->id;
178 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000179
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000180 /* Build account route-set from outbound proxies and route set from
181 * account configuration.
182 */
183 pj_list_init(&acc->route_set);
184
185 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
186 pj_str_t hname = { "Route", 5};
187 pjsip_route_hdr *r;
188 pj_str_t tmp;
189
190 pj_strdup_with_null(pjsua_var.pool, &tmp,
191 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000192 r = (pjsip_route_hdr*)
193 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000194 if (r == NULL) {
195 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
196 PJSIP_EINVALIDURI);
197 return PJSIP_EINVALIDURI;
198 }
199 pj_list_push_back(&acc->route_set, r);
200 }
201
202 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
203 pj_str_t hname = { "Route", 5};
204 pjsip_route_hdr *r;
205 pj_str_t tmp;
206
207 pj_strdup_with_null(pjsua_var.pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000208 r = (pjsip_route_hdr*)
209 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000210 if (r == NULL) {
211 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
212 PJ_EINVAL);
213 return PJ_EINVAL;
214 }
215 pj_list_push_back(&acc->route_set, r);
216 }
217
218
219 /* Concatenate credentials from account config and global config */
220 acc->cred_cnt = 0;
221 for (i=0; i<acc_cfg->cred_count; ++i) {
222 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
223 }
224 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
225 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
226 {
227 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
228 }
229
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000230 status = pjsua_pres_init_acc(acc_id);
231 if (status != PJ_SUCCESS)
232 return status;
233
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234 /* Mark account as valid */
235 pjsua_var.acc[acc_id].valid = PJ_TRUE;
236
Benny Prijono093d3022006-09-24 00:07:11 +0000237 /* Insert account ID into account ID array, sorted by priority */
238 for (i=0; i<pjsua_var.acc_cnt; ++i) {
239 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
240 pjsua_var.acc[acc_id].cfg.priority)
241 {
242 break;
243 }
244 }
245 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
246 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000247
248 return PJ_SUCCESS;
249}
250
251
252/*
253 * Add a new account to pjsua.
254 */
255PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
256 pj_bool_t is_default,
257 pjsua_acc_id *p_acc_id)
258{
259 unsigned id;
260 pj_status_t status;
261
262 PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc),
263 PJ_ETOOMANY);
264
265 /* Must have a transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000266 PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000267
268 PJSUA_LOCK();
269
270 /* Find empty account id. */
271 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) {
272 if (pjsua_var.acc[id].valid == PJ_FALSE)
273 break;
274 }
275
276 /* Expect to find a slot */
277 PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc),
278 {PJSUA_UNLOCK(); return PJ_EBUG;});
279
280 /* Copy config */
Benny Prijonobddef2c2007-10-31 13:28:08 +0000281 pjsua_acc_config_dup(pjsua_var.pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000282
283 /* Normalize registration timeout */
284 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
285 pjsua_var.acc[id].cfg.reg_timeout == 0)
286 {
287 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
288 }
289
290 status = initialize_acc(id);
291 if (status != PJ_SUCCESS) {
292 pjsua_perror(THIS_FILE, "Error adding account", status);
293 PJSUA_UNLOCK();
294 return status;
295 }
296
297 if (is_default)
298 pjsua_var.default_acc = id;
299
300 if (p_acc_id)
301 *p_acc_id = id;
302
303 pjsua_var.acc_cnt++;
304
305 PJSUA_UNLOCK();
306
307 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
308 (int)cfg->id.slen, cfg->id.ptr, id));
309
310 /* If accounts has registration enabled, start registration */
311 if (pjsua_var.acc[id].cfg.reg_uri.slen)
312 pjsua_acc_set_registration(id, PJ_TRUE);
313
314
315 return PJ_SUCCESS;
316}
317
318
319/*
320 * Add local account
321 */
322PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
323 pj_bool_t is_default,
324 pjsua_acc_id *p_acc_id)
325{
326 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000327 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000328 const char *beginquote, *endquote;
329 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000330 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000331
Benny Prijonoe93e2872006-06-28 16:46:49 +0000332 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000333 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
334 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000335
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000336 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000337 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000338
339 pjsua_acc_config_default(&cfg);
340
Benny Prijono093d3022006-09-24 00:07:11 +0000341 /* Lower the priority of local account */
342 --cfg.priority;
343
Benny Prijonod0bd4982007-12-02 15:40:52 +0000344 /* Enclose IPv6 address in square brackets */
345 if (t->type & PJSIP_TRANSPORT_IPV6) {
346 beginquote = "[";
347 endquote = "]";
348 } else {
349 beginquote = endquote = "";
350 }
351
352 /* Don't add transport parameter if it's UDP */
353 if ((t->type & PJSIP_TRANSPORT_UDP) == 0) {
354 pj_ansi_snprintf(transport_param, sizeof(transport_param),
355 ";transport=%s",
356 pjsip_transport_get_type_name(t->type));
357 } else {
358 transport_param[0] = '\0';
359 }
360
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000361 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000362 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000363 "<sip:%s%.*s%s:%d%s>",
364 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000365 (int)t->local_name.host.slen,
366 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000367 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000368 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000369 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000370
371 cfg.id = pj_str(uri);
372
373 return pjsua_acc_add(&cfg, is_default, p_acc_id);
374}
375
376
377/*
378 * Delete account.
379 */
380PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
381{
Benny Prijono093d3022006-09-24 00:07:11 +0000382 unsigned i;
383
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
385 PJ_EINVAL);
386 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
387
388 PJSUA_LOCK();
389
390 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000391 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000393 if (pjsua_var.acc[acc_id].regc) {
394 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
395 }
Benny Prijono922933b2007-01-21 16:23:56 +0000396 pjsua_var.acc[acc_id].regc = NULL;
397 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000398
399 /* Delete server presence subscription */
400 pjsua_pres_delete_acc(acc_id);
401
402 /* Invalidate */
403 pjsua_var.acc[acc_id].valid = PJ_FALSE;
404
Benny Prijono093d3022006-09-24 00:07:11 +0000405 /* Remove from array */
406 for (i=0; i<pjsua_var.acc_cnt; ++i) {
407 if (pjsua_var.acc_ids[i] == acc_id)
408 break;
409 }
410 if (i != pjsua_var.acc_cnt) {
411 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
412 pjsua_var.acc_cnt, i);
413 --pjsua_var.acc_cnt;
414 }
415
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000416 /* Leave the calls intact, as I don't think calls need to
417 * access account once it's created
418 */
419
Benny Prijonofb2b3652007-06-28 07:15:03 +0000420 /* Update default account */
421 if (pjsua_var.default_acc == acc_id)
422 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000423
424 PJSUA_UNLOCK();
425
426 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
427
428 return PJ_SUCCESS;
429}
430
431
432/*
433 * Modify account information.
434 */
435PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
436 const pjsua_acc_config *cfg)
437{
438 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000439 PJ_UNUSED_ARG(acc_id);
440 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000441 return PJ_EINVALIDOP;
442}
443
444
445/*
446 * Modify account's presence status to be advertised to remote/presence
447 * subscribers.
448 */
449PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
450 pj_bool_t is_online)
451{
452 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
453 PJ_EINVAL);
454 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
455
456 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000457 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
458 pjsua_pres_update_acc(acc_id, PJ_FALSE);
459 return PJ_SUCCESS;
460}
461
462
463/*
464 * Set online status with extended information
465 */
466PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
467 pj_bool_t is_online,
468 const pjrpid_element *pr)
469{
470 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
471 PJ_EINVAL);
472 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
473
474 pjsua_var.acc[acc_id].online_status = is_online;
475 pjrpid_element_dup(pjsua_var.pool, &pjsua_var.acc[acc_id].rpid, pr);
476 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000477 return PJ_SUCCESS;
478}
479
480
Benny Prijono15b02302007-09-27 14:07:07 +0000481/* Update NAT address from the REGISTER response */
482static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
483 struct pjsip_regc_cbparam *param)
484{
485 pjsip_transport *tp;
486 const pj_str_t *via_addr;
487 int rport;
488 pjsip_via_hdr *via;
489
490 tp = param->rdata->tp_info.transport;
491
492 /* Only update if account is configured to auto-update */
493 if (acc->cfg.auto_update_nat == PJ_FALSE)
494 return PJ_FALSE;
495
496 /* Only update if registration uses UDP transport */
497 if (tp->key.type != PJSIP_TRANSPORT_UDP)
498 return PJ_FALSE;
499
500 /* Only update if STUN is enabled (for now) */
501 if (pjsua_var.ua_cfg.stun_domain.slen == 0 &&
502 pjsua_var.ua_cfg.stun_host.slen == 0)
503 {
504 return PJ_FALSE;
505 }
506
507 /* Get the received and rport info */
508 via = param->rdata->msg_info.via;
509 if (via->rport_param < 1) {
510 /* Remote doesn't support rport */
511 rport = via->sent_by.port;
512 } else
513 rport = via->rport_param;
514
515 if (via->recvd_param.slen != 0)
516 via_addr = &via->recvd_param;
517 else
518 via_addr = &via->sent_by.host;
519
520 /* Compare received and rport with transport published address */
521 if (tp->local_name.port == rport &&
522 pj_stricmp(&tp->local_name.host, via_addr)==0)
523 {
524 /* Address doesn't change */
525 return PJ_FALSE;
526 }
527
528 /* At this point we've detected that the address as seen by registrar.
529 * has changed.
530 */
531 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
532 "(%.*s:%d --> %.*s:%d). Updating registration..",
533 acc->index,
534 (int)tp->local_name.host.slen,
535 tp->local_name.host.ptr,
536 tp->local_name.port,
537 (int)via_addr->slen,
538 via_addr->ptr,
539 rport));
540
541 /* Unregister current contact */
542 pjsua_acc_set_registration(acc->index, PJ_FALSE);
543 if (acc->regc != NULL) {
544 pjsip_regc_destroy(acc->regc);
545 acc->regc = NULL;
546 }
547
548 /* Update transport address */
549 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
550 tp->local_name.port = rport;
551
552 /* Perform new registration */
553 pjsua_acc_set_registration(acc->index, PJ_TRUE);
554
555 return PJ_TRUE;
556}
557
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000558/* Check and update Service-Route header */
559void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
560{
561 pjsip_generic_string_hdr *hsr = NULL;
562 pjsip_route_hdr *hr, *h;
563 const pj_str_t HNAME = { "Service-Route", 13 };
564 const pj_str_t HROUTE = { "Route", 5 };
565 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000566 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000567
568 /* Find and parse Service-Route headers */
569 for (;;) {
570 char saved;
571 int parsed_len;
572
573 /* Find Service-Route header */
574 hsr = (pjsip_generic_string_hdr*)
575 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
576 if (!hsr)
577 break;
578
579 /* Parse as Route header since the syntax is similar. This may
580 * return more than one headers.
581 */
582 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
583 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
584 hr = (pjsip_route_hdr*)
585 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
586 hsr->hvalue.slen, &parsed_len);
587 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
588
589 if (hr == NULL) {
590 /* Error */
591 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
592 return;
593 }
594
595 /* Save each URI in the result */
596 h = hr;
597 do {
598 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
599 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
600 {
601 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
602 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
603 return;
604 }
605
606 uri[uri_cnt++] = h->name_addr.uri;
607 h = h->next;
608 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
609
610 if (h != hr) {
611 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
612 return;
613 }
614
615 /* Prepare to find next Service-Route header */
616 hsr = hsr->next;
617 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
618 break;
619 }
620
621 if (uri_cnt == 0)
622 return;
623
624 /*
625 * Update account's route set
626 */
627
628 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000629 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000630 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
631 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
632 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000633 i<rcnt;
634 ++i)
635 {
636 pjsip_route_hdr *prev = hr->prev;
637 pj_list_erase(hr);
638 hr = prev;
639 }
640 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000641
642 /* Then append the Service-Route URIs */
643 for (i=0; i<uri_cnt; ++i) {
644 hr = pjsip_route_hdr_create(pjsua_var.pool);
Benny Prijonof0f8fd12007-11-10 12:05:59 +0000645 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(pjsua_var.pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000646 pj_list_push_back(&acc->route_set, hr);
647 }
648
649 /* Done */
650
651 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
652 acc->index, uri_cnt));
653}
654
Benny Prijonobddef2c2007-10-31 13:28:08 +0000655
656/* Keep alive timer callback */
657static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
658{
659 pjsua_acc *acc;
660 pjsip_tpselector tp_sel;
661 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000662 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000663 pj_status_t status;
664
665 PJ_UNUSED_ARG(th);
666
667 PJSUA_LOCK();
668
669 te->id = PJ_FALSE;
670
671 acc = (pjsua_acc*) te->user_data;
672
673 /* Select the transport to send the packet */
674 pj_bzero(&tp_sel, sizeof(tp_sel));
675 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
676 tp_sel.u.transport = acc->ka_transport;
677
678 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000679 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000680 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000681 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000682
683 /* Send raw packet */
684 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
685 PJSIP_TRANSPORT_UDP, &tp_sel,
686 NULL, acc->cfg.ka_data.ptr,
687 acc->cfg.ka_data.slen,
688 &acc->ka_target, acc->ka_target_len,
689 NULL, NULL);
690
691 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
692 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
693 }
694
695 /* Reschedule next timer */
696 delay.sec = acc->cfg.ka_interval;
697 delay.msec = 0;
698 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
699 if (status == PJ_SUCCESS) {
700 te->id = PJ_TRUE;
701 } else {
702 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
703 }
704
705 PJSUA_UNLOCK();
706}
707
708
709/* Update keep-alive for the account */
710static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
711 struct pjsip_regc_cbparam *param)
712{
713 /* In all cases, stop keep-alive timer if it's running. */
714 if (acc->ka_timer.id) {
715 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
716 acc->ka_timer.id = PJ_FALSE;
717
718 pjsip_transport_dec_ref(acc->ka_transport);
719 acc->ka_transport = NULL;
720 }
721
722 if (start) {
723 pj_time_val delay;
724 pj_status_t status;
725
726 /* Only do keep-alive if:
727 * - STUN is enabled in global config, and
728 * - ka_interval is not zero in the account, and
729 * - transport is UDP.
730 */
731 if (pjsua_var.stun_srv.ipv4.sin_family == 0 ||
732 acc->cfg.ka_interval == 0 ||
733 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
734 {
735 /* Keep alive is not necessary */
736 return;
737 }
738
739 /* Save transport and destination address. */
740 acc->ka_transport = param->rdata->tp_info.transport;
741 pjsip_transport_add_ref(acc->ka_transport);
742 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
743 param->rdata->pkt_info.src_addr_len);
744 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
745
746 /* Setup and start the timer */
747 acc->ka_timer.cb = &keep_alive_timer_cb;
748 acc->ka_timer.user_data = (void*)acc;
749
750 delay.sec = acc->cfg.ka_interval;
751 delay.msec = 0;
752 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
753 &delay);
754 if (status == PJ_SUCCESS) {
755 acc->ka_timer.id = PJ_TRUE;
756 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
757 "destination:%s:%d, interval:%ds",
758 acc->index,
759 param->rdata->pkt_info.src_name,
760 param->rdata->pkt_info.src_port,
761 acc->cfg.ka_interval));
762 } else {
763 acc->ka_timer.id = PJ_FALSE;
764 pjsip_transport_dec_ref(acc->ka_transport);
765 acc->ka_transport = NULL;
766 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
767 }
768 }
769}
770
771
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000772/*
773 * This callback is called by pjsip_regc when outgoing register
774 * request has completed.
775 */
776static void regc_cb(struct pjsip_regc_cbparam *param)
777{
778
Benny Prijonoa1e69682007-05-11 15:14:34 +0000779 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000780
Benny Prijono15b02302007-09-27 14:07:07 +0000781 if (param->regc != acc->regc)
782 return;
783
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000784 PJSUA_LOCK();
785
786 /*
787 * Print registration status.
788 */
789 if (param->status!=PJ_SUCCESS) {
790 pjsua_perror(THIS_FILE, "SIP registration error",
791 param->status);
792 pjsip_regc_destroy(acc->regc);
793 acc->regc = NULL;
794
Benny Prijonobddef2c2007-10-31 13:28:08 +0000795 /* Stop keep-alive timer if any. */
796 update_keep_alive(acc, PJ_FALSE, NULL);
797
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000798 } else if (param->code < 0 || param->code >= 300) {
799 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
800 param->code,
801 (int)param->reason.slen, param->reason.ptr));
802 pjsip_regc_destroy(acc->regc);
803 acc->regc = NULL;
804
Benny Prijonobddef2c2007-10-31 13:28:08 +0000805 /* Stop keep-alive timer if any. */
806 update_keep_alive(acc, PJ_FALSE, NULL);
807
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000808 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
809
810 if (param->expiration < 1) {
811 pjsip_regc_destroy(acc->regc);
812 acc->regc = NULL;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000813
814 /* Stop keep-alive timer if any. */
815 update_keep_alive(acc, PJ_FALSE, NULL);
816
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000817 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
818 pjsua_var.acc[acc->index].cfg.id.ptr));
819 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000820 /* Check NAT bound address */
821 if (acc_check_nat_addr(acc, param)) {
822 /* Update address, don't notify application yet */
823 PJSUA_UNLOCK();
824 return;
825 }
826
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000827 /* Check and update Service-Route header */
828 update_service_route(acc, param->rdata);
829
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000830 PJ_LOG(3, (THIS_FILE,
831 "%s: registration success, status=%d (%.*s), "
832 "will re-register in %d seconds",
833 pjsua_var.acc[acc->index].cfg.id.ptr,
834 param->code,
835 (int)param->reason.slen, param->reason.ptr,
836 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000837
Benny Prijonobddef2c2007-10-31 13:28:08 +0000838 /* Start keep-alive timer if necessary. */
839 update_keep_alive(acc, PJ_TRUE, param);
840
Benny Prijono8b6834f2007-02-24 13:29:22 +0000841 /* Send initial PUBLISH if it is enabled */
842 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
843 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000844 }
845
846 } else {
847 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
848 }
849
850 acc->reg_last_err = param->status;
851 acc->reg_last_code = param->code;
852
853 if (pjsua_var.ua_cfg.cb.on_reg_state)
854 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
855
856 PJSUA_UNLOCK();
857}
858
859
860/*
861 * Initialize client registration.
862 */
863static pj_status_t pjsua_regc_init(int acc_id)
864{
865 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000866 pj_str_t contact;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000867 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000868 pj_status_t status;
869
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000870 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000871 acc = &pjsua_var.acc[acc_id];
872
873 if (acc->cfg.reg_uri.slen == 0) {
874 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
875 return PJ_SUCCESS;
876 }
877
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000878 /* Destroy existing session, if any */
879 if (acc->regc) {
880 pjsip_regc_destroy(acc->regc);
881 acc->regc = NULL;
882 }
883
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000884 /* initialize SIP registration if registrar is configured */
885
886 status = pjsip_regc_create( pjsua_var.endpt,
887 acc, &regc_cb, &acc->regc);
888
889 if (status != PJ_SUCCESS) {
890 pjsua_perror(THIS_FILE, "Unable to create client registration",
891 status);
892 return status;
893 }
894
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000895 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000896 status = pjsua_acc_create_uac_contact( pool, &contact,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000897 acc_id, &acc->cfg.reg_uri);
898 if (status != PJ_SUCCESS) {
899 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
900 " for registration",
901 status);
Benny Prijono19b29372007-12-05 04:08:40 +0000902 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000903 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +0000904 acc->regc = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000905 return status;
906 }
907
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000908 status = pjsip_regc_init( acc->regc,
909 &acc->cfg.reg_uri,
910 &acc->cfg.id,
911 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000912 1, &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000913 acc->cfg.reg_timeout);
914 if (status != PJ_SUCCESS) {
915 pjsua_perror(THIS_FILE,
916 "Client registration initialization error",
917 status);
Benny Prijono19b29372007-12-05 04:08:40 +0000918 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000919 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +0000920 acc->regc = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000921 return status;
922 }
923
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000924 /* If account is locked to specific transport, then set transport to
925 * the client registration.
926 */
927 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
928 pjsip_tpselector tp_sel;
929
930 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
931 pjsip_regc_set_transport(acc->regc, &tp_sel);
932 }
933
934
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000935 /* Set credentials
936 */
937 if (acc->cred_cnt) {
938 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
939 }
940
Benny Prijono48ab2b72007-11-08 09:24:30 +0000941 /* Set authentication preference */
942 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
943
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000944 /* Set route-set
945 */
946 if (!pj_list_empty(&acc->route_set)) {
947 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
948 }
949
Benny Prijono8fc6de02006-11-11 21:25:55 +0000950 /* Add other request headers. */
951 if (pjsua_var.ua_cfg.user_agent.slen) {
952 pjsip_hdr hdr_list;
953 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
954 pjsip_generic_string_hdr *h;
955
Benny Prijono8fc6de02006-11-11 21:25:55 +0000956 pj_list_init(&hdr_list);
957
958 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
959 &pjsua_var.ua_cfg.user_agent);
960 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
961
962 pjsip_regc_add_headers(acc->regc, &hdr_list);
963 }
964
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000965 pj_pool_release(pool);
966
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000967 return PJ_SUCCESS;
968}
969
970
971/*
972 * Update registration or perform unregistration.
973 */
974PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
975 pj_bool_t renew)
976{
977 pj_status_t status = 0;
978 pjsip_tx_data *tdata = 0;
979
980 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
981 PJ_EINVAL);
982 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
983
984 PJSUA_LOCK();
985
986 if (renew) {
987 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000988 status = pjsua_regc_init(acc_id);
989 if (status != PJ_SUCCESS) {
990 pjsua_perror(THIS_FILE, "Unable to create registration",
991 status);
992 goto on_return;
993 }
994 }
995 if (!pjsua_var.acc[acc_id].regc) {
996 status = PJ_EINVALIDOP;
997 goto on_return;
998 }
999
1000 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1001 &tdata);
1002
Benny Prijono28f673a2007-10-15 07:04:59 +00001003 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1004 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1005 pjsip_authorization_hdr *h;
1006 char *uri;
1007 int d;
1008
1009 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1010 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1011 uri, acc->cfg.reg_uri.slen+10);
1012 pj_assert(d > 0);
1013
1014 h = pjsip_authorization_hdr_create(tdata->pool);
1015 h->scheme = pj_str("Digest");
1016 h->credential.digest.username = acc->cred[0].username;
1017 h->credential.digest.realm = acc->srv_domain;
1018 h->credential.digest.uri = pj_str(uri);
1019 h->credential.digest.algorithm = pj_str("md5");
1020
1021 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1022 }
1023
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001024 } else {
1025 if (pjsua_var.acc[acc_id].regc == NULL) {
1026 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1027 status = PJ_EINVALIDOP;
1028 goto on_return;
1029 }
1030 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1031 }
1032
Benny Prijono56315612006-07-18 14:39:40 +00001033 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001034 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001035 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001036 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001037
1038 if (status != PJ_SUCCESS) {
1039 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1040 status);
1041 } else {
1042 PJ_LOG(3,(THIS_FILE, "%s sent",
1043 (renew? "Registration" : "Unregistration")));
1044 }
1045
1046on_return:
1047 PJSUA_UNLOCK();
1048 return status;
1049}
1050
1051
1052/*
1053 * Get account information.
1054 */
1055PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1056 pjsua_acc_info *info)
1057{
1058 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1059 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1060
1061 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001062 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001063
Benny Prijonoac623b32006-07-03 15:19:31 +00001064 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065
1066 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1067 PJ_EINVAL);
1068 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1069
1070 PJSUA_LOCK();
1071
1072 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1073 PJSUA_UNLOCK();
1074 return PJ_EINVALIDOP;
1075 }
1076
1077 info->id = acc_id;
1078 info->is_default = (pjsua_var.default_acc == acc_id);
1079 info->acc_uri = acc_cfg->id;
1080 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1081 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001082 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1083 if (info->rpid.note.slen)
1084 info->online_status_text = info->rpid.note;
1085 else if (info->online_status)
1086 info->online_status_text = pj_str("Online");
1087 else
1088 info->online_status_text = pj_str("Offline");
1089
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001090 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001091 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001092 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1093 info->status_text = pj_str(info->buf_);
1094 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001095 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001096 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001097 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1098 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001099 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001100 info->status_text = pj_str("not registered");
1101 }
1102 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001103 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001104 info->status_text = pj_str("In Progress");
1105 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001106 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001107 info->status_text = pj_str("does not register");
1108 }
1109
1110 if (acc->regc) {
1111 pjsip_regc_info regc_info;
1112 pjsip_regc_get_info(acc->regc, &regc_info);
1113 info->expires = regc_info.next_reg;
1114 } else {
1115 info->expires = -1;
1116 }
1117
1118 PJSUA_UNLOCK();
1119
1120 return PJ_SUCCESS;
1121
1122}
1123
1124
1125/*
1126 * Enum accounts all account ids.
1127 */
1128PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1129 unsigned *count )
1130{
1131 unsigned i, c;
1132
1133 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1134
1135 PJSUA_LOCK();
1136
1137 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1138 if (!pjsua_var.acc[i].valid)
1139 continue;
1140 ids[c] = i;
1141 ++c;
1142 }
1143
1144 *count = c;
1145
1146 PJSUA_UNLOCK();
1147
1148 return PJ_SUCCESS;
1149}
1150
1151
1152/*
1153 * Enum accounts info.
1154 */
1155PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1156 unsigned *count )
1157{
1158 unsigned i, c;
1159
1160 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1161
1162 PJSUA_LOCK();
1163
1164 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1165 if (!pjsua_var.acc[i].valid)
1166 continue;
1167
1168 pjsua_acc_get_info(i, &info[c]);
1169 ++c;
1170 }
1171
1172 *count = c;
1173
1174 PJSUA_UNLOCK();
1175
1176 return PJ_SUCCESS;
1177}
1178
1179
1180/*
1181 * This is an internal function to find the most appropriate account to
1182 * used to reach to the specified URL.
1183 */
1184PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1185{
1186 pj_str_t tmp;
1187 pjsip_uri *uri;
1188 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001189 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001190
1191 PJSUA_LOCK();
1192
1193 PJ_TODO(dont_use_pjsua_pool);
1194
1195 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
1196
1197 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
1198 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +00001199 PJSUA_UNLOCK();
1200 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001201 }
1202
1203 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1204 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1205 {
1206 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001207 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1208 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001209 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001210 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001211 break;
1212 }
1213
Benny Prijono093d3022006-09-24 00:07:11 +00001214 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001215 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +00001216 PJSUA_UNLOCK();
1217 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001218 }
1219
1220 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +00001221 PJSUA_UNLOCK();
1222 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001223 }
1224
Benny Prijonoa1e69682007-05-11 15:14:34 +00001225 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001226
Benny Prijonob4a17c92006-07-10 14:40:21 +00001227 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001228 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1229 unsigned acc_id = pjsua_var.acc_ids[i];
1230 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1231 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1232 {
1233 PJSUA_UNLOCK();
1234 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001235 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001236 }
1237
Benny Prijonob4a17c92006-07-10 14:40:21 +00001238 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001239 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1240 unsigned acc_id = pjsua_var.acc_ids[i];
1241 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1242 {
1243 PJSUA_UNLOCK();
1244 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001245 }
1246 }
1247
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001248
Benny Prijono093d3022006-09-24 00:07:11 +00001249 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001250 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001251 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001252}
1253
1254
1255/*
1256 * This is an internal function to find the most appropriate account to be
1257 * used to handle incoming calls.
1258 */
1259PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1260{
1261 pjsip_uri *uri;
1262 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001263 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001264
Benny Prijono371cf0a2007-06-19 00:35:37 +00001265 /* Check that there's at least one account configured */
1266 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1267
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001268 uri = rdata->msg_info.to->uri;
1269
1270 /* Just return default account if To URI is not SIP: */
1271 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1272 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1273 {
1274 return pjsua_var.default_acc;
1275 }
1276
1277
1278 PJSUA_LOCK();
1279
1280 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1281
1282 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001283 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1284 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001285 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1286
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001287 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001288 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001289 {
1290 /* Match ! */
1291 PJSUA_UNLOCK();
1292 return acc_id;
1293 }
1294 }
1295
Benny Prijono093d3022006-09-24 00:07:11 +00001296 /* No matching account, try match domain part only. */
1297 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1298 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001299 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1300
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001301 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001302 /* Match ! */
1303 PJSUA_UNLOCK();
1304 return acc_id;
1305 }
1306 }
1307
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001308 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001309 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1310 unsigned acc_id = pjsua_var.acc_ids[i];
1311 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1312
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001313 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1314
1315 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1316 pjsip_transport_type_e type;
1317 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1318 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1319 type = PJSIP_TRANSPORT_UDP;
1320
1321 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1322 continue;
1323 }
1324
Benny Prijono093d3022006-09-24 00:07:11 +00001325 /* Match ! */
1326 PJSUA_UNLOCK();
1327 return acc_id;
1328 }
1329 }
1330
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001331 /* Still no match, use default account */
1332 PJSUA_UNLOCK();
1333 return pjsua_var.default_acc;
1334}
1335
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001336
Benny Prijonofff245c2007-04-02 11:44:47 +00001337/*
1338 * Create arbitrary requests for this account.
1339 */
1340PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1341 const pjsip_method *method,
1342 const pj_str_t *target,
1343 pjsip_tx_data **p_tdata)
1344{
1345 pjsip_tx_data *tdata;
1346 pjsua_acc *acc;
1347 pjsip_route_hdr *r;
1348 pj_status_t status;
1349
1350 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1351 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1352
1353 acc = &pjsua_var.acc[acc_id];
1354
1355 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1356 &acc->cfg.id, target,
1357 NULL, NULL, -1, NULL, &tdata);
1358 if (status != PJ_SUCCESS) {
1359 pjsua_perror(THIS_FILE, "Unable to create request", status);
1360 return status;
1361 }
1362
1363 /* Copy routeset */
1364 r = acc->route_set.next;
1365 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001366 pjsip_msg_add_hdr(tdata->msg,
1367 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001368 r = r->next;
1369 }
1370
1371 /* Done */
1372 *p_tdata = tdata;
1373 return PJ_SUCCESS;
1374}
1375
1376
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001377PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1378 pj_str_t *contact,
1379 pjsua_acc_id acc_id,
1380 const pj_str_t *suri)
1381{
1382 pjsua_acc *acc;
1383 pjsip_sip_uri *sip_uri;
1384 pj_status_t status;
1385 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1386 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001387 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001388 unsigned flag;
1389 int secure;
1390 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001391 const char *beginquote, *endquote;
1392 char transport_param[32];
1393
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001394
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001395 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001396 acc = &pjsua_var.acc[acc_id];
1397
Benny Prijonof75eceb2007-03-23 19:09:54 +00001398 /* If force_contact is configured, then use use it */
1399 if (acc->cfg.force_contact.slen) {
1400 *contact = acc->cfg.force_contact;
1401 return PJ_SUCCESS;
1402 }
1403
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001404 /* If route-set is configured for the account, then URI is the
1405 * first entry of the route-set.
1406 */
1407 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001408 sip_uri = (pjsip_sip_uri*)
1409 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001410 } else {
1411 pj_str_t tmp;
1412 pjsip_uri *uri;
1413
1414 pj_strdup_with_null(pool, &tmp, suri);
1415
1416 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1417 if (uri == NULL)
1418 return PJSIP_EINVALIDURI;
1419
1420 /* For non-SIP scheme, route set should be configured */
1421 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1422 return PJSIP_EINVALIDREQURI;
1423
Benny Prijono8c7a6172007-02-18 21:17:46 +00001424 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001425 }
1426
1427 /* Get transport type of the URI */
1428 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1429 tp_type = PJSIP_TRANSPORT_TLS;
1430 else if (sip_uri->transport_param.slen == 0) {
1431 tp_type = PJSIP_TRANSPORT_UDP;
1432 } else
1433 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1434
1435 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1436 return PJSIP_EUNSUPTRANSPORT;
1437
Benny Prijonod0bd4982007-12-02 15:40:52 +00001438 /* If destination URI specifies IPv6, then set transport type
1439 * to use IPv6 as well.
1440 */
Benny Prijono19b29372007-12-05 04:08:40 +00001441 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001442 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1443
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001444 flag = pjsip_transport_get_flag_from_type(tp_type);
1445 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1446
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001447 /* Init transport selector. */
1448 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1449
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001450 /* Get local address suitable to send request from */
1451 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001452 pool, tp_type, &tp_sel,
1453 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001454 if (status != PJ_SUCCESS)
1455 return status;
1456
Benny Prijonod0bd4982007-12-02 15:40:52 +00001457 /* Enclose IPv6 address in square brackets */
1458 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1459 beginquote = "[";
1460 endquote = "]";
1461 } else {
1462 beginquote = endquote = "";
1463 }
1464
1465 /* Don't add transport parameter if it's UDP */
1466 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1467 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1468 ";transport=%s",
1469 pjsip_transport_get_type_name(tp_type));
1470 } else {
1471 transport_param[0] = '\0';
1472 }
1473
1474
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001475 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001476 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001477 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001478 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001479 (int)acc->display.slen,
1480 acc->display.ptr,
1481 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001482 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001483 (int)acc->user_part.slen,
1484 acc->user_part.ptr,
1485 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001486 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001487 (int)local_addr.slen,
1488 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001489 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001490 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001491 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001492
1493 return PJ_SUCCESS;
1494}
1495
1496
1497
1498PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1499 pj_str_t *contact,
1500 pjsua_acc_id acc_id,
1501 pjsip_rx_data *rdata )
1502{
1503 /*
1504 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1505 * If the request that initiated the dialog contained a SIPS URI
1506 * in the Request-URI or in the top Record-Route header field value,
1507 * if there was any, or the Contact header field if there was no
1508 * Record-Route header field, the Contact header field in the response
1509 * MUST be a SIPS URI.
1510 */
1511 pjsua_acc *acc;
1512 pjsip_sip_uri *sip_uri;
1513 pj_status_t status;
1514 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1515 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001516 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001517 unsigned flag;
1518 int secure;
1519 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001520 const char *beginquote, *endquote;
1521 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001522
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001523 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001524 acc = &pjsua_var.acc[acc_id];
1525
Benny Prijonof75eceb2007-03-23 19:09:54 +00001526 /* If force_contact is configured, then use use it */
1527 if (acc->cfg.force_contact.slen) {
1528 *contact = acc->cfg.force_contact;
1529 return PJ_SUCCESS;
1530 }
1531
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001532 /* If Record-Route is present, then URI is the top Record-Route. */
1533 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001534 sip_uri = (pjsip_sip_uri*)
1535 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001536 } else {
1537 pjsip_contact_hdr *h_contact;
1538 pjsip_uri *uri = NULL;
1539
1540 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001541 h_contact = (pjsip_contact_hdr*)
1542 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001543 NULL);
1544 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001545 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001546
1547
1548 /* Or if Contact URI is not present, take the remote URI from
1549 * the From URI.
1550 */
1551 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001552 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001553
1554
1555 /* Can only do sip/sips scheme at present. */
1556 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1557 return PJSIP_EINVALIDREQURI;
1558
Benny Prijono8c7a6172007-02-18 21:17:46 +00001559 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001560 }
1561
1562 /* Get transport type of the URI */
1563 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1564 tp_type = PJSIP_TRANSPORT_TLS;
1565 else if (sip_uri->transport_param.slen == 0) {
1566 tp_type = PJSIP_TRANSPORT_UDP;
1567 } else
1568 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001569
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001570 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1571 return PJSIP_EUNSUPTRANSPORT;
1572
Benny Prijonod0bd4982007-12-02 15:40:52 +00001573 /* If destination URI specifies IPv6, then set transport type
1574 * to use IPv6 as well.
1575 */
1576 if (pj_strchr(&sip_uri->host, ':'))
1577 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1578
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001579 flag = pjsip_transport_get_flag_from_type(tp_type);
1580 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1581
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001582 /* Init transport selector. */
1583 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1584
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001585 /* Get local address suitable to send request from */
1586 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001587 pool, tp_type, &tp_sel,
1588 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001589 if (status != PJ_SUCCESS)
1590 return status;
1591
Benny Prijonod0bd4982007-12-02 15:40:52 +00001592 /* Enclose IPv6 address in square brackets */
1593 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1594 beginquote = "[";
1595 endquote = "]";
1596 } else {
1597 beginquote = endquote = "";
1598 }
1599
1600 /* Don't add transport parameter if it's UDP */
1601 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1602 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1603 ";transport=%s",
1604 pjsip_transport_get_type_name(tp_type));
1605 } else {
1606 transport_param[0] = '\0';
1607 }
1608
1609
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001610 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001611 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001612 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001613 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001614 (int)acc->display.slen,
1615 acc->display.ptr,
1616 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001617 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001618 (int)acc->user_part.slen,
1619 acc->user_part.ptr,
1620 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001621 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001622 (int)local_addr.slen,
1623 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001624 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001625 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001626 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001627
1628 return PJ_SUCCESS;
1629}
1630
1631
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001632PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1633 pjsua_transport_id tp_id)
1634{
1635 pjsua_acc *acc;
1636
1637 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1638 acc = &pjsua_var.acc[acc_id];
1639
Benny Prijonoa1e69682007-05-11 15:14:34 +00001640 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001641 PJ_EINVAL);
1642
1643 acc->cfg.transport_id = tp_id;
1644
1645 return PJ_SUCCESS;
1646}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001647