blob: e5b18ed11bafe16808f376e08cee23b61cf32b6d [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;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000487 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000488 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000489 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000490 pjsip_via_hdr *via;
491
492 tp = param->rdata->tp_info.transport;
493
494 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000495 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000496 return PJ_FALSE;
497
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000498#if 0
499 // Always update
500 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000501
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000502 /* For UDP, only update if STUN is enabled (for now).
503 * For TCP/TLS, always check.
504 */
505 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
506 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
507 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
508 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
509 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000510 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000511 /* Yes we will check */
512 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000513 return PJ_FALSE;
514 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000515#endif
Benny Prijono15b02302007-09-27 14:07:07 +0000516
517 /* Get the received and rport info */
518 via = param->rdata->msg_info.via;
519 if (via->rport_param < 1) {
520 /* Remote doesn't support rport */
521 rport = via->sent_by.port;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000522 if (rport==0)
523 rport = pjsip_transport_get_default_port_for_type(tp->key.type);
Benny Prijono15b02302007-09-27 14:07:07 +0000524 } else
525 rport = via->rport_param;
526
527 if (via->recvd_param.slen != 0)
528 via_addr = &via->recvd_param;
529 else
530 via_addr = &via->sent_by.host;
531
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000532 /* Compare received and rport with the URI in our registration */
533 pool = pjsua_pool_create("tmp", 512, 512);
534 uri = (pjsip_sip_uri*)
535 pjsip_parse_uri(pool, acc->contact.ptr, acc->contact.slen, 0);
536 pj_assert(uri != NULL);
537 uri = pjsip_uri_get_uri(uri);
538
539 if (uri->port == 0)
540 uri->port = pjsip_transport_get_default_port_for_type(tp->key.type);
541
542 if (uri->port == rport &&
543 pj_stricmp(&uri->host, via_addr)==0)
Benny Prijono15b02302007-09-27 14:07:07 +0000544 {
545 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000546 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000547 return PJ_FALSE;
548 }
549
550 /* At this point we've detected that the address as seen by registrar.
551 * has changed.
552 */
553 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
554 "(%.*s:%d --> %.*s:%d). Updating registration..",
555 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000556 (int)uri->host.slen,
557 uri->host.ptr,
558 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000559 (int)via_addr->slen,
560 via_addr->ptr,
561 rport));
562
563 /* Unregister current contact */
564 pjsua_acc_set_registration(acc->index, PJ_FALSE);
565 if (acc->regc != NULL) {
566 pjsip_regc_destroy(acc->regc);
567 acc->regc = NULL;
568 }
569
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000570 /* Update account's Contact header */
571 {
572 char *tmp;
573 int len;
574
575 tmp = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
576 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
577 "<sip:%.*s@%.*s:%d;transport=%s>",
578 (int)acc->user_part.slen,
579 acc->user_part.ptr,
580 (int)via_addr->slen,
581 via_addr->ptr,
582 rport,
583 tp->type_name);
584 if (len < 1) {
585 PJ_LOG(1,(THIS_FILE, "URI too long"));
586 pj_pool_release(pool);
587 return PJ_FALSE;
588 }
589 pj_strdup2(pjsua_var.pool, &acc->contact, tmp);
590 }
591
592 /* For UDP transport, if STUN is enabled then update the transport's
593 * published name as well.
594 */
595 if (tp->key.type==PJSIP_TRANSPORT_UDP &&
596 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
597 pjsua_var.ua_cfg.stun_host.slen != 0))
598 {
599 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
600 tp->local_name.port = rport;
601 }
Benny Prijono15b02302007-09-27 14:07:07 +0000602
603 /* Perform new registration */
604 pjsua_acc_set_registration(acc->index, PJ_TRUE);
605
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000606 pj_pool_release(pool);
607
Benny Prijono15b02302007-09-27 14:07:07 +0000608 return PJ_TRUE;
609}
610
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000611/* Check and update Service-Route header */
612void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
613{
614 pjsip_generic_string_hdr *hsr = NULL;
615 pjsip_route_hdr *hr, *h;
616 const pj_str_t HNAME = { "Service-Route", 13 };
617 const pj_str_t HROUTE = { "Route", 5 };
618 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000619 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000620
621 /* Find and parse Service-Route headers */
622 for (;;) {
623 char saved;
624 int parsed_len;
625
626 /* Find Service-Route header */
627 hsr = (pjsip_generic_string_hdr*)
628 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
629 if (!hsr)
630 break;
631
632 /* Parse as Route header since the syntax is similar. This may
633 * return more than one headers.
634 */
635 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
636 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
637 hr = (pjsip_route_hdr*)
638 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
639 hsr->hvalue.slen, &parsed_len);
640 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
641
642 if (hr == NULL) {
643 /* Error */
644 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
645 return;
646 }
647
648 /* Save each URI in the result */
649 h = hr;
650 do {
651 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
652 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
653 {
654 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
655 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
656 return;
657 }
658
659 uri[uri_cnt++] = h->name_addr.uri;
660 h = h->next;
661 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
662
663 if (h != hr) {
664 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
665 return;
666 }
667
668 /* Prepare to find next Service-Route header */
669 hsr = hsr->next;
670 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
671 break;
672 }
673
674 if (uri_cnt == 0)
675 return;
676
677 /*
678 * Update account's route set
679 */
680
681 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000682 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000683 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
684 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
685 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000686 i<rcnt;
687 ++i)
688 {
689 pjsip_route_hdr *prev = hr->prev;
690 pj_list_erase(hr);
691 hr = prev;
692 }
693 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000694
695 /* Then append the Service-Route URIs */
696 for (i=0; i<uri_cnt; ++i) {
697 hr = pjsip_route_hdr_create(pjsua_var.pool);
Benny Prijonof0f8fd12007-11-10 12:05:59 +0000698 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(pjsua_var.pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000699 pj_list_push_back(&acc->route_set, hr);
700 }
701
702 /* Done */
703
704 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
705 acc->index, uri_cnt));
706}
707
Benny Prijonobddef2c2007-10-31 13:28:08 +0000708
709/* Keep alive timer callback */
710static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
711{
712 pjsua_acc *acc;
713 pjsip_tpselector tp_sel;
714 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000715 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000716 pj_status_t status;
717
718 PJ_UNUSED_ARG(th);
719
720 PJSUA_LOCK();
721
722 te->id = PJ_FALSE;
723
724 acc = (pjsua_acc*) te->user_data;
725
726 /* Select the transport to send the packet */
727 pj_bzero(&tp_sel, sizeof(tp_sel));
728 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
729 tp_sel.u.transport = acc->ka_transport;
730
731 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000732 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000733 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000734 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000735
736 /* Send raw packet */
737 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
738 PJSIP_TRANSPORT_UDP, &tp_sel,
739 NULL, acc->cfg.ka_data.ptr,
740 acc->cfg.ka_data.slen,
741 &acc->ka_target, acc->ka_target_len,
742 NULL, NULL);
743
744 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
745 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
746 }
747
748 /* Reschedule next timer */
749 delay.sec = acc->cfg.ka_interval;
750 delay.msec = 0;
751 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
752 if (status == PJ_SUCCESS) {
753 te->id = PJ_TRUE;
754 } else {
755 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
756 }
757
758 PJSUA_UNLOCK();
759}
760
761
762/* Update keep-alive for the account */
763static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
764 struct pjsip_regc_cbparam *param)
765{
766 /* In all cases, stop keep-alive timer if it's running. */
767 if (acc->ka_timer.id) {
768 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
769 acc->ka_timer.id = PJ_FALSE;
770
771 pjsip_transport_dec_ref(acc->ka_transport);
772 acc->ka_transport = NULL;
773 }
774
775 if (start) {
776 pj_time_val delay;
777 pj_status_t status;
778
779 /* Only do keep-alive if:
780 * - STUN is enabled in global config, and
781 * - ka_interval is not zero in the account, and
782 * - transport is UDP.
783 */
784 if (pjsua_var.stun_srv.ipv4.sin_family == 0 ||
785 acc->cfg.ka_interval == 0 ||
786 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
787 {
788 /* Keep alive is not necessary */
789 return;
790 }
791
792 /* Save transport and destination address. */
793 acc->ka_transport = param->rdata->tp_info.transport;
794 pjsip_transport_add_ref(acc->ka_transport);
795 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
796 param->rdata->pkt_info.src_addr_len);
797 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
798
799 /* Setup and start the timer */
800 acc->ka_timer.cb = &keep_alive_timer_cb;
801 acc->ka_timer.user_data = (void*)acc;
802
803 delay.sec = acc->cfg.ka_interval;
804 delay.msec = 0;
805 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
806 &delay);
807 if (status == PJ_SUCCESS) {
808 acc->ka_timer.id = PJ_TRUE;
809 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
810 "destination:%s:%d, interval:%ds",
811 acc->index,
812 param->rdata->pkt_info.src_name,
813 param->rdata->pkt_info.src_port,
814 acc->cfg.ka_interval));
815 } else {
816 acc->ka_timer.id = PJ_FALSE;
817 pjsip_transport_dec_ref(acc->ka_transport);
818 acc->ka_transport = NULL;
819 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
820 }
821 }
822}
823
824
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000825/*
826 * This callback is called by pjsip_regc when outgoing register
827 * request has completed.
828 */
829static void regc_cb(struct pjsip_regc_cbparam *param)
830{
831
Benny Prijonoa1e69682007-05-11 15:14:34 +0000832 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000833
Benny Prijono15b02302007-09-27 14:07:07 +0000834 if (param->regc != acc->regc)
835 return;
836
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000837 PJSUA_LOCK();
838
839 /*
840 * Print registration status.
841 */
842 if (param->status!=PJ_SUCCESS) {
843 pjsua_perror(THIS_FILE, "SIP registration error",
844 param->status);
845 pjsip_regc_destroy(acc->regc);
846 acc->regc = NULL;
847
Benny Prijonobddef2c2007-10-31 13:28:08 +0000848 /* Stop keep-alive timer if any. */
849 update_keep_alive(acc, PJ_FALSE, NULL);
850
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000851 } else if (param->code < 0 || param->code >= 300) {
852 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
853 param->code,
854 (int)param->reason.slen, param->reason.ptr));
855 pjsip_regc_destroy(acc->regc);
856 acc->regc = NULL;
857
Benny Prijonobddef2c2007-10-31 13:28:08 +0000858 /* Stop keep-alive timer if any. */
859 update_keep_alive(acc, PJ_FALSE, NULL);
860
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000861 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
862
863 if (param->expiration < 1) {
864 pjsip_regc_destroy(acc->regc);
865 acc->regc = NULL;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000866
867 /* Stop keep-alive timer if any. */
868 update_keep_alive(acc, PJ_FALSE, NULL);
869
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000870 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
871 pjsua_var.acc[acc->index].cfg.id.ptr));
872 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000873 /* Check NAT bound address */
874 if (acc_check_nat_addr(acc, param)) {
875 /* Update address, don't notify application yet */
876 PJSUA_UNLOCK();
877 return;
878 }
879
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000880 /* Check and update Service-Route header */
881 update_service_route(acc, param->rdata);
882
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000883 PJ_LOG(3, (THIS_FILE,
884 "%s: registration success, status=%d (%.*s), "
885 "will re-register in %d seconds",
886 pjsua_var.acc[acc->index].cfg.id.ptr,
887 param->code,
888 (int)param->reason.slen, param->reason.ptr,
889 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000890
Benny Prijonobddef2c2007-10-31 13:28:08 +0000891 /* Start keep-alive timer if necessary. */
892 update_keep_alive(acc, PJ_TRUE, param);
893
Benny Prijono8b6834f2007-02-24 13:29:22 +0000894 /* Send initial PUBLISH if it is enabled */
895 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
896 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000897 }
898
899 } else {
900 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
901 }
902
903 acc->reg_last_err = param->status;
904 acc->reg_last_code = param->code;
905
906 if (pjsua_var.ua_cfg.cb.on_reg_state)
907 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
908
909 PJSUA_UNLOCK();
910}
911
912
913/*
914 * Initialize client registration.
915 */
916static pj_status_t pjsua_regc_init(int acc_id)
917{
918 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000919 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000920 pj_status_t status;
921
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000922 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000923 acc = &pjsua_var.acc[acc_id];
924
925 if (acc->cfg.reg_uri.slen == 0) {
926 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
927 return PJ_SUCCESS;
928 }
929
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000930 /* Destroy existing session, if any */
931 if (acc->regc) {
932 pjsip_regc_destroy(acc->regc);
933 acc->regc = NULL;
934 }
935
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000936 /* initialize SIP registration if registrar is configured */
937
938 status = pjsip_regc_create( pjsua_var.endpt,
939 acc, &regc_cb, &acc->regc);
940
941 if (status != PJ_SUCCESS) {
942 pjsua_perror(THIS_FILE, "Unable to create client registration",
943 status);
944 return status;
945 }
946
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000947 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000948
949 if (acc->contact.slen == 0) {
950 pj_str_t tmp_contact;
951
952 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
953 acc_id, &acc->cfg.reg_uri);
954 if (status != PJ_SUCCESS) {
955 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
956 " for registration",
957 status);
958 pjsip_regc_destroy(acc->regc);
959 pj_pool_release(pool);
960 acc->regc = NULL;
961 return status;
962 }
963
964 pj_strdup_with_null(pjsua_var.pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000965 }
966
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000967 status = pjsip_regc_init( acc->regc,
968 &acc->cfg.reg_uri,
969 &acc->cfg.id,
970 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000971 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000972 acc->cfg.reg_timeout);
973 if (status != PJ_SUCCESS) {
974 pjsua_perror(THIS_FILE,
975 "Client registration initialization error",
976 status);
Benny Prijono19b29372007-12-05 04:08:40 +0000977 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000978 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +0000979 acc->regc = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000980 return status;
981 }
982
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000983 /* If account is locked to specific transport, then set transport to
984 * the client registration.
985 */
986 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
987 pjsip_tpselector tp_sel;
988
989 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
990 pjsip_regc_set_transport(acc->regc, &tp_sel);
991 }
992
993
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000994 /* Set credentials
995 */
996 if (acc->cred_cnt) {
997 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
998 }
999
Benny Prijono48ab2b72007-11-08 09:24:30 +00001000 /* Set authentication preference */
1001 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1002
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001003 /* Set route-set
1004 */
1005 if (!pj_list_empty(&acc->route_set)) {
1006 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1007 }
1008
Benny Prijono8fc6de02006-11-11 21:25:55 +00001009 /* Add other request headers. */
1010 if (pjsua_var.ua_cfg.user_agent.slen) {
1011 pjsip_hdr hdr_list;
1012 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1013 pjsip_generic_string_hdr *h;
1014
Benny Prijono8fc6de02006-11-11 21:25:55 +00001015 pj_list_init(&hdr_list);
1016
1017 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1018 &pjsua_var.ua_cfg.user_agent);
1019 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1020
1021 pjsip_regc_add_headers(acc->regc, &hdr_list);
1022 }
1023
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001024 pj_pool_release(pool);
1025
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001026 return PJ_SUCCESS;
1027}
1028
1029
1030/*
1031 * Update registration or perform unregistration.
1032 */
1033PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1034 pj_bool_t renew)
1035{
1036 pj_status_t status = 0;
1037 pjsip_tx_data *tdata = 0;
1038
1039 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1040 PJ_EINVAL);
1041 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1042
1043 PJSUA_LOCK();
1044
1045 if (renew) {
1046 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001047 status = pjsua_regc_init(acc_id);
1048 if (status != PJ_SUCCESS) {
1049 pjsua_perror(THIS_FILE, "Unable to create registration",
1050 status);
1051 goto on_return;
1052 }
1053 }
1054 if (!pjsua_var.acc[acc_id].regc) {
1055 status = PJ_EINVALIDOP;
1056 goto on_return;
1057 }
1058
1059 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1060 &tdata);
1061
Benny Prijono28f673a2007-10-15 07:04:59 +00001062 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1063 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1064 pjsip_authorization_hdr *h;
1065 char *uri;
1066 int d;
1067
1068 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1069 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1070 uri, acc->cfg.reg_uri.slen+10);
1071 pj_assert(d > 0);
1072
1073 h = pjsip_authorization_hdr_create(tdata->pool);
1074 h->scheme = pj_str("Digest");
1075 h->credential.digest.username = acc->cred[0].username;
1076 h->credential.digest.realm = acc->srv_domain;
1077 h->credential.digest.uri = pj_str(uri);
1078 h->credential.digest.algorithm = pj_str("md5");
1079
1080 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1081 }
1082
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001083 } else {
1084 if (pjsua_var.acc[acc_id].regc == NULL) {
1085 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1086 status = PJ_EINVALIDOP;
1087 goto on_return;
1088 }
1089 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1090 }
1091
Benny Prijono56315612006-07-18 14:39:40 +00001092 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001093 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001094 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001095 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001096
1097 if (status != PJ_SUCCESS) {
1098 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1099 status);
1100 } else {
1101 PJ_LOG(3,(THIS_FILE, "%s sent",
1102 (renew? "Registration" : "Unregistration")));
1103 }
1104
1105on_return:
1106 PJSUA_UNLOCK();
1107 return status;
1108}
1109
1110
1111/*
1112 * Get account information.
1113 */
1114PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1115 pjsua_acc_info *info)
1116{
1117 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1118 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1119
1120 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001121 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001122
Benny Prijonoac623b32006-07-03 15:19:31 +00001123 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001124
1125 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1126 PJ_EINVAL);
1127 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1128
1129 PJSUA_LOCK();
1130
1131 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1132 PJSUA_UNLOCK();
1133 return PJ_EINVALIDOP;
1134 }
1135
1136 info->id = acc_id;
1137 info->is_default = (pjsua_var.default_acc == acc_id);
1138 info->acc_uri = acc_cfg->id;
1139 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1140 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001141 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1142 if (info->rpid.note.slen)
1143 info->online_status_text = info->rpid.note;
1144 else if (info->online_status)
1145 info->online_status_text = pj_str("Online");
1146 else
1147 info->online_status_text = pj_str("Offline");
1148
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001149 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001150 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001151 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1152 info->status_text = pj_str(info->buf_);
1153 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001154 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001155 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001156 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1157 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001158 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001159 info->status_text = pj_str("not registered");
1160 }
1161 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001162 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001163 info->status_text = pj_str("In Progress");
1164 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001165 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001166 info->status_text = pj_str("does not register");
1167 }
1168
1169 if (acc->regc) {
1170 pjsip_regc_info regc_info;
1171 pjsip_regc_get_info(acc->regc, &regc_info);
1172 info->expires = regc_info.next_reg;
1173 } else {
1174 info->expires = -1;
1175 }
1176
1177 PJSUA_UNLOCK();
1178
1179 return PJ_SUCCESS;
1180
1181}
1182
1183
1184/*
1185 * Enum accounts all account ids.
1186 */
1187PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1188 unsigned *count )
1189{
1190 unsigned i, c;
1191
1192 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1193
1194 PJSUA_LOCK();
1195
1196 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1197 if (!pjsua_var.acc[i].valid)
1198 continue;
1199 ids[c] = i;
1200 ++c;
1201 }
1202
1203 *count = c;
1204
1205 PJSUA_UNLOCK();
1206
1207 return PJ_SUCCESS;
1208}
1209
1210
1211/*
1212 * Enum accounts info.
1213 */
1214PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1215 unsigned *count )
1216{
1217 unsigned i, c;
1218
1219 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1220
1221 PJSUA_LOCK();
1222
1223 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1224 if (!pjsua_var.acc[i].valid)
1225 continue;
1226
1227 pjsua_acc_get_info(i, &info[c]);
1228 ++c;
1229 }
1230
1231 *count = c;
1232
1233 PJSUA_UNLOCK();
1234
1235 return PJ_SUCCESS;
1236}
1237
1238
1239/*
1240 * This is an internal function to find the most appropriate account to
1241 * used to reach to the specified URL.
1242 */
1243PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1244{
1245 pj_str_t tmp;
1246 pjsip_uri *uri;
1247 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001248 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001249
1250 PJSUA_LOCK();
1251
1252 PJ_TODO(dont_use_pjsua_pool);
1253
1254 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
1255
1256 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
1257 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +00001258 PJSUA_UNLOCK();
1259 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001260 }
1261
1262 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1263 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1264 {
1265 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001266 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1267 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001268 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001269 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001270 break;
1271 }
1272
Benny Prijono093d3022006-09-24 00:07:11 +00001273 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001274 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +00001275 PJSUA_UNLOCK();
1276 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001277 }
1278
1279 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +00001280 PJSUA_UNLOCK();
1281 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001282 }
1283
Benny Prijonoa1e69682007-05-11 15:14:34 +00001284 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001285
Benny Prijonob4a17c92006-07-10 14:40:21 +00001286 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001287 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1288 unsigned acc_id = pjsua_var.acc_ids[i];
1289 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1290 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1291 {
1292 PJSUA_UNLOCK();
1293 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001294 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001295 }
1296
Benny Prijonob4a17c92006-07-10 14:40:21 +00001297 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001298 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1299 unsigned acc_id = pjsua_var.acc_ids[i];
1300 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1301 {
1302 PJSUA_UNLOCK();
1303 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001304 }
1305 }
1306
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001307
Benny Prijono093d3022006-09-24 00:07:11 +00001308 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001309 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001310 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001311}
1312
1313
1314/*
1315 * This is an internal function to find the most appropriate account to be
1316 * used to handle incoming calls.
1317 */
1318PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1319{
1320 pjsip_uri *uri;
1321 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001322 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323
Benny Prijono371cf0a2007-06-19 00:35:37 +00001324 /* Check that there's at least one account configured */
1325 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1326
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001327 uri = rdata->msg_info.to->uri;
1328
1329 /* Just return default account if To URI is not SIP: */
1330 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1331 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1332 {
1333 return pjsua_var.default_acc;
1334 }
1335
1336
1337 PJSUA_LOCK();
1338
1339 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1340
1341 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001342 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1343 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001344 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1345
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001346 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001347 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001348 {
1349 /* Match ! */
1350 PJSUA_UNLOCK();
1351 return acc_id;
1352 }
1353 }
1354
Benny Prijono093d3022006-09-24 00:07:11 +00001355 /* No matching account, try match domain part only. */
1356 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1357 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001358 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1359
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001360 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001361 /* Match ! */
1362 PJSUA_UNLOCK();
1363 return acc_id;
1364 }
1365 }
1366
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001367 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001368 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1369 unsigned acc_id = pjsua_var.acc_ids[i];
1370 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1371
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001372 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1373
1374 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1375 pjsip_transport_type_e type;
1376 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1377 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1378 type = PJSIP_TRANSPORT_UDP;
1379
1380 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1381 continue;
1382 }
1383
Benny Prijono093d3022006-09-24 00:07:11 +00001384 /* Match ! */
1385 PJSUA_UNLOCK();
1386 return acc_id;
1387 }
1388 }
1389
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001390 /* Still no match, use default account */
1391 PJSUA_UNLOCK();
1392 return pjsua_var.default_acc;
1393}
1394
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001395
Benny Prijonofff245c2007-04-02 11:44:47 +00001396/*
1397 * Create arbitrary requests for this account.
1398 */
1399PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1400 const pjsip_method *method,
1401 const pj_str_t *target,
1402 pjsip_tx_data **p_tdata)
1403{
1404 pjsip_tx_data *tdata;
1405 pjsua_acc *acc;
1406 pjsip_route_hdr *r;
1407 pj_status_t status;
1408
1409 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1410 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1411
1412 acc = &pjsua_var.acc[acc_id];
1413
1414 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1415 &acc->cfg.id, target,
1416 NULL, NULL, -1, NULL, &tdata);
1417 if (status != PJ_SUCCESS) {
1418 pjsua_perror(THIS_FILE, "Unable to create request", status);
1419 return status;
1420 }
1421
1422 /* Copy routeset */
1423 r = acc->route_set.next;
1424 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001425 pjsip_msg_add_hdr(tdata->msg,
1426 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001427 r = r->next;
1428 }
1429
1430 /* Done */
1431 *p_tdata = tdata;
1432 return PJ_SUCCESS;
1433}
1434
1435
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001436PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1437 pj_str_t *contact,
1438 pjsua_acc_id acc_id,
1439 const pj_str_t *suri)
1440{
1441 pjsua_acc *acc;
1442 pjsip_sip_uri *sip_uri;
1443 pj_status_t status;
1444 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1445 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001446 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001447 unsigned flag;
1448 int secure;
1449 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001450 const char *beginquote, *endquote;
1451 char transport_param[32];
1452
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001453
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001454 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001455 acc = &pjsua_var.acc[acc_id];
1456
Benny Prijonof75eceb2007-03-23 19:09:54 +00001457 /* If force_contact is configured, then use use it */
1458 if (acc->cfg.force_contact.slen) {
1459 *contact = acc->cfg.force_contact;
1460 return PJ_SUCCESS;
1461 }
1462
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001463 /* If route-set is configured for the account, then URI is the
1464 * first entry of the route-set.
1465 */
1466 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001467 sip_uri = (pjsip_sip_uri*)
1468 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001469 } else {
1470 pj_str_t tmp;
1471 pjsip_uri *uri;
1472
1473 pj_strdup_with_null(pool, &tmp, suri);
1474
1475 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1476 if (uri == NULL)
1477 return PJSIP_EINVALIDURI;
1478
1479 /* For non-SIP scheme, route set should be configured */
1480 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1481 return PJSIP_EINVALIDREQURI;
1482
Benny Prijono8c7a6172007-02-18 21:17:46 +00001483 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001484 }
1485
1486 /* Get transport type of the URI */
1487 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1488 tp_type = PJSIP_TRANSPORT_TLS;
1489 else if (sip_uri->transport_param.slen == 0) {
1490 tp_type = PJSIP_TRANSPORT_UDP;
1491 } else
1492 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1493
1494 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1495 return PJSIP_EUNSUPTRANSPORT;
1496
Benny Prijonod0bd4982007-12-02 15:40:52 +00001497 /* If destination URI specifies IPv6, then set transport type
1498 * to use IPv6 as well.
1499 */
Benny Prijono19b29372007-12-05 04:08:40 +00001500 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001501 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1502
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001503 flag = pjsip_transport_get_flag_from_type(tp_type);
1504 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1505
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001506 /* Init transport selector. */
1507 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1508
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001509 /* Get local address suitable to send request from */
1510 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001511 pool, tp_type, &tp_sel,
1512 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001513 if (status != PJ_SUCCESS)
1514 return status;
1515
Benny Prijonod0bd4982007-12-02 15:40:52 +00001516 /* Enclose IPv6 address in square brackets */
1517 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1518 beginquote = "[";
1519 endquote = "]";
1520 } else {
1521 beginquote = endquote = "";
1522 }
1523
1524 /* Don't add transport parameter if it's UDP */
1525 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1526 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1527 ";transport=%s",
1528 pjsip_transport_get_type_name(tp_type));
1529 } else {
1530 transport_param[0] = '\0';
1531 }
1532
1533
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001534 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001535 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001536 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001537 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001538 (int)acc->display.slen,
1539 acc->display.ptr,
1540 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001541 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001542 (int)acc->user_part.slen,
1543 acc->user_part.ptr,
1544 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001545 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001546 (int)local_addr.slen,
1547 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001548 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001549 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001550 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001551
1552 return PJ_SUCCESS;
1553}
1554
1555
1556
1557PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1558 pj_str_t *contact,
1559 pjsua_acc_id acc_id,
1560 pjsip_rx_data *rdata )
1561{
1562 /*
1563 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1564 * If the request that initiated the dialog contained a SIPS URI
1565 * in the Request-URI or in the top Record-Route header field value,
1566 * if there was any, or the Contact header field if there was no
1567 * Record-Route header field, the Contact header field in the response
1568 * MUST be a SIPS URI.
1569 */
1570 pjsua_acc *acc;
1571 pjsip_sip_uri *sip_uri;
1572 pj_status_t status;
1573 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1574 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001575 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001576 unsigned flag;
1577 int secure;
1578 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001579 const char *beginquote, *endquote;
1580 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001581
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001582 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001583 acc = &pjsua_var.acc[acc_id];
1584
Benny Prijonof75eceb2007-03-23 19:09:54 +00001585 /* If force_contact is configured, then use use it */
1586 if (acc->cfg.force_contact.slen) {
1587 *contact = acc->cfg.force_contact;
1588 return PJ_SUCCESS;
1589 }
1590
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001591 /* If Record-Route is present, then URI is the top Record-Route. */
1592 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001593 sip_uri = (pjsip_sip_uri*)
1594 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001595 } else {
1596 pjsip_contact_hdr *h_contact;
1597 pjsip_uri *uri = NULL;
1598
1599 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001600 h_contact = (pjsip_contact_hdr*)
1601 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001602 NULL);
1603 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001604 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001605
1606
1607 /* Or if Contact URI is not present, take the remote URI from
1608 * the From URI.
1609 */
1610 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001611 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001612
1613
1614 /* Can only do sip/sips scheme at present. */
1615 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1616 return PJSIP_EINVALIDREQURI;
1617
Benny Prijono8c7a6172007-02-18 21:17:46 +00001618 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001619 }
1620
1621 /* Get transport type of the URI */
1622 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1623 tp_type = PJSIP_TRANSPORT_TLS;
1624 else if (sip_uri->transport_param.slen == 0) {
1625 tp_type = PJSIP_TRANSPORT_UDP;
1626 } else
1627 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001628
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001629 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1630 return PJSIP_EUNSUPTRANSPORT;
1631
Benny Prijonod0bd4982007-12-02 15:40:52 +00001632 /* If destination URI specifies IPv6, then set transport type
1633 * to use IPv6 as well.
1634 */
1635 if (pj_strchr(&sip_uri->host, ':'))
1636 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1637
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001638 flag = pjsip_transport_get_flag_from_type(tp_type);
1639 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1640
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001641 /* Init transport selector. */
1642 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1643
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001644 /* Get local address suitable to send request from */
1645 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001646 pool, tp_type, &tp_sel,
1647 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001648 if (status != PJ_SUCCESS)
1649 return status;
1650
Benny Prijonod0bd4982007-12-02 15:40:52 +00001651 /* Enclose IPv6 address in square brackets */
1652 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1653 beginquote = "[";
1654 endquote = "]";
1655 } else {
1656 beginquote = endquote = "";
1657 }
1658
1659 /* Don't add transport parameter if it's UDP */
1660 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1661 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1662 ";transport=%s",
1663 pjsip_transport_get_type_name(tp_type));
1664 } else {
1665 transport_param[0] = '\0';
1666 }
1667
1668
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001669 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001670 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001671 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001672 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001673 (int)acc->display.slen,
1674 acc->display.ptr,
1675 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001676 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001677 (int)acc->user_part.slen,
1678 acc->user_part.ptr,
1679 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001680 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001681 (int)local_addr.slen,
1682 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001683 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001684 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001685 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001686
1687 return PJ_SUCCESS;
1688}
1689
1690
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001691PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1692 pjsua_transport_id tp_id)
1693{
1694 pjsua_acc *acc;
1695
1696 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1697 acc = &pjsua_var.acc[acc_id];
1698
Benny Prijonoa1e69682007-05-11 15:14:34 +00001699 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001700 PJ_EINVAL);
1701
1702 acc->cfg.transport_id = tp_id;
1703
1704 return PJ_SUCCESS;
1705}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001706