blob: 2258cdd9c1f05c9eb8b861b1bd7e5202e377a0f8 [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:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000780 * - ka_interval is not zero in the account, and
781 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000782 *
783 * Previously we only enabled keep-alive when STUN is enabled, since
784 * we thought that keep-alive is only needed in Internet situation.
785 * But it has been discovered that Windows Firewall on WinXP also
786 * needs to be kept-alive, otherwise incoming packets will be dropped.
787 * So because of this, now keep-alive is always enabled for UDP,
788 * regardless of whether STUN is enabled or not.
789 *
790 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
791 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000792 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000793 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000794 acc->cfg.ka_interval == 0 ||
795 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
796 {
797 /* Keep alive is not necessary */
798 return;
799 }
800
801 /* Save transport and destination address. */
802 acc->ka_transport = param->rdata->tp_info.transport;
803 pjsip_transport_add_ref(acc->ka_transport);
804 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
805 param->rdata->pkt_info.src_addr_len);
806 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
807
808 /* Setup and start the timer */
809 acc->ka_timer.cb = &keep_alive_timer_cb;
810 acc->ka_timer.user_data = (void*)acc;
811
812 delay.sec = acc->cfg.ka_interval;
813 delay.msec = 0;
814 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
815 &delay);
816 if (status == PJ_SUCCESS) {
817 acc->ka_timer.id = PJ_TRUE;
818 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
819 "destination:%s:%d, interval:%ds",
820 acc->index,
821 param->rdata->pkt_info.src_name,
822 param->rdata->pkt_info.src_port,
823 acc->cfg.ka_interval));
824 } else {
825 acc->ka_timer.id = PJ_FALSE;
826 pjsip_transport_dec_ref(acc->ka_transport);
827 acc->ka_transport = NULL;
828 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
829 }
830 }
831}
832
833
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000834/*
835 * This callback is called by pjsip_regc when outgoing register
836 * request has completed.
837 */
838static void regc_cb(struct pjsip_regc_cbparam *param)
839{
840
Benny Prijonoa1e69682007-05-11 15:14:34 +0000841 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000842
Benny Prijono15b02302007-09-27 14:07:07 +0000843 if (param->regc != acc->regc)
844 return;
845
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000846 PJSUA_LOCK();
847
848 /*
849 * Print registration status.
850 */
851 if (param->status!=PJ_SUCCESS) {
852 pjsua_perror(THIS_FILE, "SIP registration error",
853 param->status);
854 pjsip_regc_destroy(acc->regc);
855 acc->regc = NULL;
856
Benny Prijonobddef2c2007-10-31 13:28:08 +0000857 /* Stop keep-alive timer if any. */
858 update_keep_alive(acc, PJ_FALSE, NULL);
859
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000860 } else if (param->code < 0 || param->code >= 300) {
861 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
862 param->code,
863 (int)param->reason.slen, param->reason.ptr));
864 pjsip_regc_destroy(acc->regc);
865 acc->regc = NULL;
866
Benny Prijonobddef2c2007-10-31 13:28:08 +0000867 /* Stop keep-alive timer if any. */
868 update_keep_alive(acc, PJ_FALSE, NULL);
869
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000870 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
871
872 if (param->expiration < 1) {
873 pjsip_regc_destroy(acc->regc);
874 acc->regc = NULL;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000875
876 /* Stop keep-alive timer if any. */
877 update_keep_alive(acc, PJ_FALSE, NULL);
878
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000879 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
880 pjsua_var.acc[acc->index].cfg.id.ptr));
881 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000882 /* Check NAT bound address */
883 if (acc_check_nat_addr(acc, param)) {
884 /* Update address, don't notify application yet */
885 PJSUA_UNLOCK();
886 return;
887 }
888
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000889 /* Check and update Service-Route header */
890 update_service_route(acc, param->rdata);
891
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000892 PJ_LOG(3, (THIS_FILE,
893 "%s: registration success, status=%d (%.*s), "
894 "will re-register in %d seconds",
895 pjsua_var.acc[acc->index].cfg.id.ptr,
896 param->code,
897 (int)param->reason.slen, param->reason.ptr,
898 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000899
Benny Prijonobddef2c2007-10-31 13:28:08 +0000900 /* Start keep-alive timer if necessary. */
901 update_keep_alive(acc, PJ_TRUE, param);
902
Benny Prijono8b6834f2007-02-24 13:29:22 +0000903 /* Send initial PUBLISH if it is enabled */
904 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
905 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000906 }
907
908 } else {
909 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
910 }
911
912 acc->reg_last_err = param->status;
913 acc->reg_last_code = param->code;
914
915 if (pjsua_var.ua_cfg.cb.on_reg_state)
916 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
917
918 PJSUA_UNLOCK();
919}
920
921
922/*
923 * Initialize client registration.
924 */
925static pj_status_t pjsua_regc_init(int acc_id)
926{
927 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000928 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000929 pj_status_t status;
930
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000931 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000932 acc = &pjsua_var.acc[acc_id];
933
934 if (acc->cfg.reg_uri.slen == 0) {
935 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
936 return PJ_SUCCESS;
937 }
938
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000939 /* Destroy existing session, if any */
940 if (acc->regc) {
941 pjsip_regc_destroy(acc->regc);
942 acc->regc = NULL;
943 }
944
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000945 /* initialize SIP registration if registrar is configured */
946
947 status = pjsip_regc_create( pjsua_var.endpt,
948 acc, &regc_cb, &acc->regc);
949
950 if (status != PJ_SUCCESS) {
951 pjsua_perror(THIS_FILE, "Unable to create client registration",
952 status);
953 return status;
954 }
955
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000956 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000957
958 if (acc->contact.slen == 0) {
959 pj_str_t tmp_contact;
960
961 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
962 acc_id, &acc->cfg.reg_uri);
963 if (status != PJ_SUCCESS) {
964 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
965 " for registration",
966 status);
967 pjsip_regc_destroy(acc->regc);
968 pj_pool_release(pool);
969 acc->regc = NULL;
970 return status;
971 }
972
973 pj_strdup_with_null(pjsua_var.pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000974 }
975
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000976 status = pjsip_regc_init( acc->regc,
977 &acc->cfg.reg_uri,
978 &acc->cfg.id,
979 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000980 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000981 acc->cfg.reg_timeout);
982 if (status != PJ_SUCCESS) {
983 pjsua_perror(THIS_FILE,
984 "Client registration initialization error",
985 status);
Benny Prijono19b29372007-12-05 04:08:40 +0000986 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000987 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +0000988 acc->regc = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000989 return status;
990 }
991
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000992 /* If account is locked to specific transport, then set transport to
993 * the client registration.
994 */
995 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
996 pjsip_tpselector tp_sel;
997
998 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
999 pjsip_regc_set_transport(acc->regc, &tp_sel);
1000 }
1001
1002
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001003 /* Set credentials
1004 */
1005 if (acc->cred_cnt) {
1006 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1007 }
1008
Benny Prijono48ab2b72007-11-08 09:24:30 +00001009 /* Set authentication preference */
1010 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1011
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001012 /* Set route-set
1013 */
1014 if (!pj_list_empty(&acc->route_set)) {
1015 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1016 }
1017
Benny Prijono8fc6de02006-11-11 21:25:55 +00001018 /* Add other request headers. */
1019 if (pjsua_var.ua_cfg.user_agent.slen) {
1020 pjsip_hdr hdr_list;
1021 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1022 pjsip_generic_string_hdr *h;
1023
Benny Prijono8fc6de02006-11-11 21:25:55 +00001024 pj_list_init(&hdr_list);
1025
1026 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1027 &pjsua_var.ua_cfg.user_agent);
1028 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1029
1030 pjsip_regc_add_headers(acc->regc, &hdr_list);
1031 }
1032
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001033 pj_pool_release(pool);
1034
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001035 return PJ_SUCCESS;
1036}
1037
1038
1039/*
1040 * Update registration or perform unregistration.
1041 */
1042PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1043 pj_bool_t renew)
1044{
1045 pj_status_t status = 0;
1046 pjsip_tx_data *tdata = 0;
1047
1048 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1049 PJ_EINVAL);
1050 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1051
1052 PJSUA_LOCK();
1053
1054 if (renew) {
1055 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001056 status = pjsua_regc_init(acc_id);
1057 if (status != PJ_SUCCESS) {
1058 pjsua_perror(THIS_FILE, "Unable to create registration",
1059 status);
1060 goto on_return;
1061 }
1062 }
1063 if (!pjsua_var.acc[acc_id].regc) {
1064 status = PJ_EINVALIDOP;
1065 goto on_return;
1066 }
1067
1068 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1069 &tdata);
1070
Benny Prijono28f673a2007-10-15 07:04:59 +00001071 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1072 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1073 pjsip_authorization_hdr *h;
1074 char *uri;
1075 int d;
1076
1077 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1078 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1079 uri, acc->cfg.reg_uri.slen+10);
1080 pj_assert(d > 0);
1081
1082 h = pjsip_authorization_hdr_create(tdata->pool);
1083 h->scheme = pj_str("Digest");
1084 h->credential.digest.username = acc->cred[0].username;
1085 h->credential.digest.realm = acc->srv_domain;
1086 h->credential.digest.uri = pj_str(uri);
1087 h->credential.digest.algorithm = pj_str("md5");
1088
1089 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1090 }
1091
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001092 } else {
1093 if (pjsua_var.acc[acc_id].regc == NULL) {
1094 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1095 status = PJ_EINVALIDOP;
1096 goto on_return;
1097 }
1098 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1099 }
1100
Benny Prijono56315612006-07-18 14:39:40 +00001101 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001102 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001103 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001104 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001105
1106 if (status != PJ_SUCCESS) {
1107 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1108 status);
1109 } else {
1110 PJ_LOG(3,(THIS_FILE, "%s sent",
1111 (renew? "Registration" : "Unregistration")));
1112 }
1113
1114on_return:
1115 PJSUA_UNLOCK();
1116 return status;
1117}
1118
1119
1120/*
1121 * Get account information.
1122 */
1123PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1124 pjsua_acc_info *info)
1125{
1126 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1127 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1128
1129 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001130 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001131
Benny Prijonoac623b32006-07-03 15:19:31 +00001132 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001133
1134 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1135 PJ_EINVAL);
1136 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1137
1138 PJSUA_LOCK();
1139
1140 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1141 PJSUA_UNLOCK();
1142 return PJ_EINVALIDOP;
1143 }
1144
1145 info->id = acc_id;
1146 info->is_default = (pjsua_var.default_acc == acc_id);
1147 info->acc_uri = acc_cfg->id;
1148 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1149 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001150 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1151 if (info->rpid.note.slen)
1152 info->online_status_text = info->rpid.note;
1153 else if (info->online_status)
1154 info->online_status_text = pj_str("Online");
1155 else
1156 info->online_status_text = pj_str("Offline");
1157
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001158 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001159 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001160 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1161 info->status_text = pj_str(info->buf_);
1162 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001163 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001164 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001165 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1166 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001167 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001168 info->status_text = pj_str("not registered");
1169 }
1170 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001171 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001172 info->status_text = pj_str("In Progress");
1173 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001174 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001175 info->status_text = pj_str("does not register");
1176 }
1177
1178 if (acc->regc) {
1179 pjsip_regc_info regc_info;
1180 pjsip_regc_get_info(acc->regc, &regc_info);
1181 info->expires = regc_info.next_reg;
1182 } else {
1183 info->expires = -1;
1184 }
1185
1186 PJSUA_UNLOCK();
1187
1188 return PJ_SUCCESS;
1189
1190}
1191
1192
1193/*
1194 * Enum accounts all account ids.
1195 */
1196PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1197 unsigned *count )
1198{
1199 unsigned i, c;
1200
1201 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1202
1203 PJSUA_LOCK();
1204
1205 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1206 if (!pjsua_var.acc[i].valid)
1207 continue;
1208 ids[c] = i;
1209 ++c;
1210 }
1211
1212 *count = c;
1213
1214 PJSUA_UNLOCK();
1215
1216 return PJ_SUCCESS;
1217}
1218
1219
1220/*
1221 * Enum accounts info.
1222 */
1223PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1224 unsigned *count )
1225{
1226 unsigned i, c;
1227
1228 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1229
1230 PJSUA_LOCK();
1231
1232 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1233 if (!pjsua_var.acc[i].valid)
1234 continue;
1235
1236 pjsua_acc_get_info(i, &info[c]);
1237 ++c;
1238 }
1239
1240 *count = c;
1241
1242 PJSUA_UNLOCK();
1243
1244 return PJ_SUCCESS;
1245}
1246
1247
1248/*
1249 * This is an internal function to find the most appropriate account to
1250 * used to reach to the specified URL.
1251 */
1252PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1253{
1254 pj_str_t tmp;
1255 pjsip_uri *uri;
1256 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001257 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001258
1259 PJSUA_LOCK();
1260
1261 PJ_TODO(dont_use_pjsua_pool);
1262
1263 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
1264
1265 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
1266 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +00001267 PJSUA_UNLOCK();
1268 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001269 }
1270
1271 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1272 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1273 {
1274 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001275 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1276 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001277 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001278 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001279 break;
1280 }
1281
Benny Prijono093d3022006-09-24 00:07:11 +00001282 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001283 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +00001284 PJSUA_UNLOCK();
1285 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001286 }
1287
1288 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +00001289 PJSUA_UNLOCK();
1290 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001291 }
1292
Benny Prijonoa1e69682007-05-11 15:14:34 +00001293 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001294
Benny Prijonob4a17c92006-07-10 14:40:21 +00001295 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001296 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1297 unsigned acc_id = pjsua_var.acc_ids[i];
1298 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1299 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1300 {
1301 PJSUA_UNLOCK();
1302 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001303 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001304 }
1305
Benny Prijonob4a17c92006-07-10 14:40:21 +00001306 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001307 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1308 unsigned acc_id = pjsua_var.acc_ids[i];
1309 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1310 {
1311 PJSUA_UNLOCK();
1312 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001313 }
1314 }
1315
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001316
Benny Prijono093d3022006-09-24 00:07:11 +00001317 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001318 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001319 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001320}
1321
1322
1323/*
1324 * This is an internal function to find the most appropriate account to be
1325 * used to handle incoming calls.
1326 */
1327PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1328{
1329 pjsip_uri *uri;
1330 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001331 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001332
Benny Prijono371cf0a2007-06-19 00:35:37 +00001333 /* Check that there's at least one account configured */
1334 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1335
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001336 uri = rdata->msg_info.to->uri;
1337
1338 /* Just return default account if To URI is not SIP: */
1339 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1340 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1341 {
1342 return pjsua_var.default_acc;
1343 }
1344
1345
1346 PJSUA_LOCK();
1347
1348 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1349
1350 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001351 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1352 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001353 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1354
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001355 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001356 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001357 {
1358 /* Match ! */
1359 PJSUA_UNLOCK();
1360 return acc_id;
1361 }
1362 }
1363
Benny Prijono093d3022006-09-24 00:07:11 +00001364 /* No matching account, try match domain part only. */
1365 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1366 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001367 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1368
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001369 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001370 /* Match ! */
1371 PJSUA_UNLOCK();
1372 return acc_id;
1373 }
1374 }
1375
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001376 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001377 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1378 unsigned acc_id = pjsua_var.acc_ids[i];
1379 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1380
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001381 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1382
1383 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1384 pjsip_transport_type_e type;
1385 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1386 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1387 type = PJSIP_TRANSPORT_UDP;
1388
1389 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1390 continue;
1391 }
1392
Benny Prijono093d3022006-09-24 00:07:11 +00001393 /* Match ! */
1394 PJSUA_UNLOCK();
1395 return acc_id;
1396 }
1397 }
1398
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001399 /* Still no match, use default account */
1400 PJSUA_UNLOCK();
1401 return pjsua_var.default_acc;
1402}
1403
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001404
Benny Prijonofff245c2007-04-02 11:44:47 +00001405/*
1406 * Create arbitrary requests for this account.
1407 */
1408PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1409 const pjsip_method *method,
1410 const pj_str_t *target,
1411 pjsip_tx_data **p_tdata)
1412{
1413 pjsip_tx_data *tdata;
1414 pjsua_acc *acc;
1415 pjsip_route_hdr *r;
1416 pj_status_t status;
1417
1418 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1419 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1420
1421 acc = &pjsua_var.acc[acc_id];
1422
1423 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1424 &acc->cfg.id, target,
1425 NULL, NULL, -1, NULL, &tdata);
1426 if (status != PJ_SUCCESS) {
1427 pjsua_perror(THIS_FILE, "Unable to create request", status);
1428 return status;
1429 }
1430
1431 /* Copy routeset */
1432 r = acc->route_set.next;
1433 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001434 pjsip_msg_add_hdr(tdata->msg,
1435 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001436 r = r->next;
1437 }
1438
1439 /* Done */
1440 *p_tdata = tdata;
1441 return PJ_SUCCESS;
1442}
1443
1444
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001445PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1446 pj_str_t *contact,
1447 pjsua_acc_id acc_id,
1448 const pj_str_t *suri)
1449{
1450 pjsua_acc *acc;
1451 pjsip_sip_uri *sip_uri;
1452 pj_status_t status;
1453 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1454 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001455 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001456 unsigned flag;
1457 int secure;
1458 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001459 const char *beginquote, *endquote;
1460 char transport_param[32];
1461
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001462
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001463 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001464 acc = &pjsua_var.acc[acc_id];
1465
Benny Prijonof75eceb2007-03-23 19:09:54 +00001466 /* If force_contact is configured, then use use it */
1467 if (acc->cfg.force_contact.slen) {
1468 *contact = acc->cfg.force_contact;
1469 return PJ_SUCCESS;
1470 }
1471
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001472 /* If route-set is configured for the account, then URI is the
1473 * first entry of the route-set.
1474 */
1475 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001476 sip_uri = (pjsip_sip_uri*)
1477 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001478 } else {
1479 pj_str_t tmp;
1480 pjsip_uri *uri;
1481
1482 pj_strdup_with_null(pool, &tmp, suri);
1483
1484 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1485 if (uri == NULL)
1486 return PJSIP_EINVALIDURI;
1487
1488 /* For non-SIP scheme, route set should be configured */
1489 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1490 return PJSIP_EINVALIDREQURI;
1491
Benny Prijono8c7a6172007-02-18 21:17:46 +00001492 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001493 }
1494
1495 /* Get transport type of the URI */
1496 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1497 tp_type = PJSIP_TRANSPORT_TLS;
1498 else if (sip_uri->transport_param.slen == 0) {
1499 tp_type = PJSIP_TRANSPORT_UDP;
1500 } else
1501 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1502
1503 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1504 return PJSIP_EUNSUPTRANSPORT;
1505
Benny Prijonod0bd4982007-12-02 15:40:52 +00001506 /* If destination URI specifies IPv6, then set transport type
1507 * to use IPv6 as well.
1508 */
Benny Prijono19b29372007-12-05 04:08:40 +00001509 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001510 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1511
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001512 flag = pjsip_transport_get_flag_from_type(tp_type);
1513 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1514
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001515 /* Init transport selector. */
1516 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1517
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001518 /* Get local address suitable to send request from */
1519 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001520 pool, tp_type, &tp_sel,
1521 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001522 if (status != PJ_SUCCESS)
1523 return status;
1524
Benny Prijonod0bd4982007-12-02 15:40:52 +00001525 /* Enclose IPv6 address in square brackets */
1526 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1527 beginquote = "[";
1528 endquote = "]";
1529 } else {
1530 beginquote = endquote = "";
1531 }
1532
1533 /* Don't add transport parameter if it's UDP */
1534 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1535 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1536 ";transport=%s",
1537 pjsip_transport_get_type_name(tp_type));
1538 } else {
1539 transport_param[0] = '\0';
1540 }
1541
1542
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001543 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001544 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001545 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001546 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001547 (int)acc->display.slen,
1548 acc->display.ptr,
1549 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001550 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001551 (int)acc->user_part.slen,
1552 acc->user_part.ptr,
1553 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001554 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001555 (int)local_addr.slen,
1556 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001557 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001558 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001559 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001560
1561 return PJ_SUCCESS;
1562}
1563
1564
1565
1566PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1567 pj_str_t *contact,
1568 pjsua_acc_id acc_id,
1569 pjsip_rx_data *rdata )
1570{
1571 /*
1572 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1573 * If the request that initiated the dialog contained a SIPS URI
1574 * in the Request-URI or in the top Record-Route header field value,
1575 * if there was any, or the Contact header field if there was no
1576 * Record-Route header field, the Contact header field in the response
1577 * MUST be a SIPS URI.
1578 */
1579 pjsua_acc *acc;
1580 pjsip_sip_uri *sip_uri;
1581 pj_status_t status;
1582 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1583 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001584 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001585 unsigned flag;
1586 int secure;
1587 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001588 const char *beginquote, *endquote;
1589 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001590
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001591 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001592 acc = &pjsua_var.acc[acc_id];
1593
Benny Prijonof75eceb2007-03-23 19:09:54 +00001594 /* If force_contact is configured, then use use it */
1595 if (acc->cfg.force_contact.slen) {
1596 *contact = acc->cfg.force_contact;
1597 return PJ_SUCCESS;
1598 }
1599
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001600 /* If Record-Route is present, then URI is the top Record-Route. */
1601 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001602 sip_uri = (pjsip_sip_uri*)
1603 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001604 } else {
1605 pjsip_contact_hdr *h_contact;
1606 pjsip_uri *uri = NULL;
1607
1608 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001609 h_contact = (pjsip_contact_hdr*)
1610 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001611 NULL);
1612 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001613 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001614
1615
1616 /* Or if Contact URI is not present, take the remote URI from
1617 * the From URI.
1618 */
1619 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001620 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001621
1622
1623 /* Can only do sip/sips scheme at present. */
1624 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1625 return PJSIP_EINVALIDREQURI;
1626
Benny Prijono8c7a6172007-02-18 21:17:46 +00001627 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001628 }
1629
1630 /* Get transport type of the URI */
1631 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1632 tp_type = PJSIP_TRANSPORT_TLS;
1633 else if (sip_uri->transport_param.slen == 0) {
1634 tp_type = PJSIP_TRANSPORT_UDP;
1635 } else
1636 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001637
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001638 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1639 return PJSIP_EUNSUPTRANSPORT;
1640
Benny Prijonod0bd4982007-12-02 15:40:52 +00001641 /* If destination URI specifies IPv6, then set transport type
1642 * to use IPv6 as well.
1643 */
1644 if (pj_strchr(&sip_uri->host, ':'))
1645 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1646
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001647 flag = pjsip_transport_get_flag_from_type(tp_type);
1648 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1649
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001650 /* Init transport selector. */
1651 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1652
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001653 /* Get local address suitable to send request from */
1654 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001655 pool, tp_type, &tp_sel,
1656 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001657 if (status != PJ_SUCCESS)
1658 return status;
1659
Benny Prijonod0bd4982007-12-02 15:40:52 +00001660 /* Enclose IPv6 address in square brackets */
1661 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1662 beginquote = "[";
1663 endquote = "]";
1664 } else {
1665 beginquote = endquote = "";
1666 }
1667
1668 /* Don't add transport parameter if it's UDP */
1669 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1670 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1671 ";transport=%s",
1672 pjsip_transport_get_type_name(tp_type));
1673 } else {
1674 transport_param[0] = '\0';
1675 }
1676
1677
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001678 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001679 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001680 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001681 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001682 (int)acc->display.slen,
1683 acc->display.ptr,
1684 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001685 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001686 (int)acc->user_part.slen,
1687 acc->user_part.ptr,
1688 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001689 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001690 (int)local_addr.slen,
1691 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001692 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001693 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001694 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001695
1696 return PJ_SUCCESS;
1697}
1698
1699
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001700PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1701 pjsua_transport_id tp_id)
1702{
1703 pjsua_acc *acc;
1704
1705 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1706 acc = &pjsua_var.acc[acc_id];
1707
Benny Prijonoa1e69682007-05-11 15:14:34 +00001708 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001709 PJ_EINVAL);
1710
1711 acc->cfg.transport_id = tp_id;
1712
1713 return PJ_SUCCESS;
1714}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001715