blob: ec2265213c74a7657f83daa830a69b8687af1990 [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 Prijono24a21852008-04-14 04:04:30 +0000522 if (rport==0) {
523 pjsip_transport_type_e tp_type;
524 tp_type = (pjsip_transport_type_e) tp->key.type;
525 rport = pjsip_transport_get_default_port_for_type(tp_type);
526 }
Benny Prijono15b02302007-09-27 14:07:07 +0000527 } else
528 rport = via->rport_param;
529
530 if (via->recvd_param.slen != 0)
531 via_addr = &via->recvd_param;
532 else
533 via_addr = &via->sent_by.host;
534
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000535 /* Compare received and rport with the URI in our registration */
536 pool = pjsua_pool_create("tmp", 512, 512);
537 uri = (pjsip_sip_uri*)
538 pjsip_parse_uri(pool, acc->contact.ptr, acc->contact.slen, 0);
539 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +0000540 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000541
Benny Prijono24a21852008-04-14 04:04:30 +0000542 if (uri->port == 0) {
543 pjsip_transport_type_e tp_type;
544 tp_type = (pjsip_transport_type_e) tp->key.type;
545 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
546 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000547
548 if (uri->port == rport &&
549 pj_stricmp(&uri->host, via_addr)==0)
Benny Prijono15b02302007-09-27 14:07:07 +0000550 {
551 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000552 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000553 return PJ_FALSE;
554 }
555
556 /* At this point we've detected that the address as seen by registrar.
557 * has changed.
558 */
559 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
560 "(%.*s:%d --> %.*s:%d). Updating registration..",
561 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000562 (int)uri->host.slen,
563 uri->host.ptr,
564 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000565 (int)via_addr->slen,
566 via_addr->ptr,
567 rport));
568
569 /* Unregister current contact */
570 pjsua_acc_set_registration(acc->index, PJ_FALSE);
571 if (acc->regc != NULL) {
572 pjsip_regc_destroy(acc->regc);
573 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000574 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +0000575 }
576
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000577 /* Update account's Contact header */
578 {
579 char *tmp;
580 int len;
581
Benny Prijono24a21852008-04-14 04:04:30 +0000582 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000583 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
584 "<sip:%.*s@%.*s:%d;transport=%s>",
585 (int)acc->user_part.slen,
586 acc->user_part.ptr,
587 (int)via_addr->slen,
588 via_addr->ptr,
589 rport,
590 tp->type_name);
591 if (len < 1) {
592 PJ_LOG(1,(THIS_FILE, "URI too long"));
593 pj_pool_release(pool);
594 return PJ_FALSE;
595 }
596 pj_strdup2(pjsua_var.pool, &acc->contact, tmp);
597 }
598
599 /* For UDP transport, if STUN is enabled then update the transport's
600 * published name as well.
601 */
602 if (tp->key.type==PJSIP_TRANSPORT_UDP &&
603 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
604 pjsua_var.ua_cfg.stun_host.slen != 0))
605 {
606 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
607 tp->local_name.port = rport;
608 }
Benny Prijono15b02302007-09-27 14:07:07 +0000609
610 /* Perform new registration */
611 pjsua_acc_set_registration(acc->index, PJ_TRUE);
612
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000613 pj_pool_release(pool);
614
Benny Prijono15b02302007-09-27 14:07:07 +0000615 return PJ_TRUE;
616}
617
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000618/* Check and update Service-Route header */
619void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
620{
621 pjsip_generic_string_hdr *hsr = NULL;
622 pjsip_route_hdr *hr, *h;
623 const pj_str_t HNAME = { "Service-Route", 13 };
624 const pj_str_t HROUTE = { "Route", 5 };
625 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000626 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000627
628 /* Find and parse Service-Route headers */
629 for (;;) {
630 char saved;
631 int parsed_len;
632
633 /* Find Service-Route header */
634 hsr = (pjsip_generic_string_hdr*)
635 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
636 if (!hsr)
637 break;
638
639 /* Parse as Route header since the syntax is similar. This may
640 * return more than one headers.
641 */
642 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
643 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
644 hr = (pjsip_route_hdr*)
645 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
646 hsr->hvalue.slen, &parsed_len);
647 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
648
649 if (hr == NULL) {
650 /* Error */
651 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
652 return;
653 }
654
655 /* Save each URI in the result */
656 h = hr;
657 do {
658 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
659 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
660 {
661 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
662 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
663 return;
664 }
665
666 uri[uri_cnt++] = h->name_addr.uri;
667 h = h->next;
668 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
669
670 if (h != hr) {
671 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
672 return;
673 }
674
675 /* Prepare to find next Service-Route header */
676 hsr = hsr->next;
677 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
678 break;
679 }
680
681 if (uri_cnt == 0)
682 return;
683
684 /*
685 * Update account's route set
686 */
687
688 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000689 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000690 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
691 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
692 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000693 i<rcnt;
694 ++i)
695 {
696 pjsip_route_hdr *prev = hr->prev;
697 pj_list_erase(hr);
698 hr = prev;
699 }
700 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000701
702 /* Then append the Service-Route URIs */
703 for (i=0; i<uri_cnt; ++i) {
704 hr = pjsip_route_hdr_create(pjsua_var.pool);
Benny Prijonof0f8fd12007-11-10 12:05:59 +0000705 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(pjsua_var.pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000706 pj_list_push_back(&acc->route_set, hr);
707 }
708
709 /* Done */
710
711 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
712 acc->index, uri_cnt));
713}
714
Benny Prijonobddef2c2007-10-31 13:28:08 +0000715
716/* Keep alive timer callback */
717static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
718{
719 pjsua_acc *acc;
720 pjsip_tpselector tp_sel;
721 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000722 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000723 pj_status_t status;
724
725 PJ_UNUSED_ARG(th);
726
727 PJSUA_LOCK();
728
729 te->id = PJ_FALSE;
730
731 acc = (pjsua_acc*) te->user_data;
732
733 /* Select the transport to send the packet */
734 pj_bzero(&tp_sel, sizeof(tp_sel));
735 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
736 tp_sel.u.transport = acc->ka_transport;
737
738 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000739 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000740 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000741 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000742
743 /* Send raw packet */
744 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
745 PJSIP_TRANSPORT_UDP, &tp_sel,
746 NULL, acc->cfg.ka_data.ptr,
747 acc->cfg.ka_data.slen,
748 &acc->ka_target, acc->ka_target_len,
749 NULL, NULL);
750
751 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
752 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
753 }
754
755 /* Reschedule next timer */
756 delay.sec = acc->cfg.ka_interval;
757 delay.msec = 0;
758 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
759 if (status == PJ_SUCCESS) {
760 te->id = PJ_TRUE;
761 } else {
762 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
763 }
764
765 PJSUA_UNLOCK();
766}
767
768
769/* Update keep-alive for the account */
770static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
771 struct pjsip_regc_cbparam *param)
772{
773 /* In all cases, stop keep-alive timer if it's running. */
774 if (acc->ka_timer.id) {
775 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
776 acc->ka_timer.id = PJ_FALSE;
777
778 pjsip_transport_dec_ref(acc->ka_transport);
779 acc->ka_transport = NULL;
780 }
781
782 if (start) {
783 pj_time_val delay;
784 pj_status_t status;
785
786 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000787 * - ka_interval is not zero in the account, and
788 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000789 *
790 * Previously we only enabled keep-alive when STUN is enabled, since
791 * we thought that keep-alive is only needed in Internet situation.
792 * But it has been discovered that Windows Firewall on WinXP also
793 * needs to be kept-alive, otherwise incoming packets will be dropped.
794 * So because of this, now keep-alive is always enabled for UDP,
795 * regardless of whether STUN is enabled or not.
796 *
797 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
798 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000799 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000800 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000801 acc->cfg.ka_interval == 0 ||
802 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
803 {
804 /* Keep alive is not necessary */
805 return;
806 }
807
808 /* Save transport and destination address. */
809 acc->ka_transport = param->rdata->tp_info.transport;
810 pjsip_transport_add_ref(acc->ka_transport);
811 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
812 param->rdata->pkt_info.src_addr_len);
813 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
814
815 /* Setup and start the timer */
816 acc->ka_timer.cb = &keep_alive_timer_cb;
817 acc->ka_timer.user_data = (void*)acc;
818
819 delay.sec = acc->cfg.ka_interval;
820 delay.msec = 0;
821 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
822 &delay);
823 if (status == PJ_SUCCESS) {
824 acc->ka_timer.id = PJ_TRUE;
825 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
826 "destination:%s:%d, interval:%ds",
827 acc->index,
828 param->rdata->pkt_info.src_name,
829 param->rdata->pkt_info.src_port,
830 acc->cfg.ka_interval));
831 } else {
832 acc->ka_timer.id = PJ_FALSE;
833 pjsip_transport_dec_ref(acc->ka_transport);
834 acc->ka_transport = NULL;
835 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
836 }
837 }
838}
839
840
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000841/*
842 * This callback is called by pjsip_regc when outgoing register
843 * request has completed.
844 */
845static void regc_cb(struct pjsip_regc_cbparam *param)
846{
847
Benny Prijonoa1e69682007-05-11 15:14:34 +0000848 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000849
Benny Prijono15b02302007-09-27 14:07:07 +0000850 if (param->regc != acc->regc)
851 return;
852
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000853 PJSUA_LOCK();
854
855 /*
856 * Print registration status.
857 */
858 if (param->status!=PJ_SUCCESS) {
859 pjsua_perror(THIS_FILE, "SIP registration error",
860 param->status);
861 pjsip_regc_destroy(acc->regc);
862 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000863 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000864
Benny Prijonobddef2c2007-10-31 13:28:08 +0000865 /* Stop keep-alive timer if any. */
866 update_keep_alive(acc, PJ_FALSE, NULL);
867
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000868 } else if (param->code < 0 || param->code >= 300) {
869 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
870 param->code,
871 (int)param->reason.slen, param->reason.ptr));
872 pjsip_regc_destroy(acc->regc);
873 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000874 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000875
Benny Prijonobddef2c2007-10-31 13:28:08 +0000876 /* Stop keep-alive timer if any. */
877 update_keep_alive(acc, PJ_FALSE, NULL);
878
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000879 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
880
881 if (param->expiration < 1) {
882 pjsip_regc_destroy(acc->regc);
883 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000884 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000885
886 /* Stop keep-alive timer if any. */
887 update_keep_alive(acc, PJ_FALSE, NULL);
888
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
890 pjsua_var.acc[acc->index].cfg.id.ptr));
891 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000892 /* Check NAT bound address */
893 if (acc_check_nat_addr(acc, param)) {
894 /* Update address, don't notify application yet */
895 PJSUA_UNLOCK();
896 return;
897 }
898
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000899 /* Check and update Service-Route header */
900 update_service_route(acc, param->rdata);
901
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000902 PJ_LOG(3, (THIS_FILE,
903 "%s: registration success, status=%d (%.*s), "
904 "will re-register in %d seconds",
905 pjsua_var.acc[acc->index].cfg.id.ptr,
906 param->code,
907 (int)param->reason.slen, param->reason.ptr,
908 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +0000909
Benny Prijonobddef2c2007-10-31 13:28:08 +0000910 /* Start keep-alive timer if necessary. */
911 update_keep_alive(acc, PJ_TRUE, param);
912
Benny Prijono8b6834f2007-02-24 13:29:22 +0000913 /* Send initial PUBLISH if it is enabled */
914 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
915 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000916 }
917
918 } else {
919 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
920 }
921
922 acc->reg_last_err = param->status;
923 acc->reg_last_code = param->code;
924
925 if (pjsua_var.ua_cfg.cb.on_reg_state)
926 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
927
928 PJSUA_UNLOCK();
929}
930
931
932/*
933 * Initialize client registration.
934 */
935static pj_status_t pjsua_regc_init(int acc_id)
936{
937 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000938 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000939 pj_status_t status;
940
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000941 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000942 acc = &pjsua_var.acc[acc_id];
943
944 if (acc->cfg.reg_uri.slen == 0) {
945 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
946 return PJ_SUCCESS;
947 }
948
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000949 /* Destroy existing session, if any */
950 if (acc->regc) {
951 pjsip_regc_destroy(acc->regc);
952 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000953 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +0000954 }
955
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000956 /* initialize SIP registration if registrar is configured */
957
958 status = pjsip_regc_create( pjsua_var.endpt,
959 acc, &regc_cb, &acc->regc);
960
961 if (status != PJ_SUCCESS) {
962 pjsua_perror(THIS_FILE, "Unable to create client registration",
963 status);
964 return status;
965 }
966
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000967 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000968
969 if (acc->contact.slen == 0) {
970 pj_str_t tmp_contact;
971
972 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
973 acc_id, &acc->cfg.reg_uri);
974 if (status != PJ_SUCCESS) {
975 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
976 " for registration",
977 status);
978 pjsip_regc_destroy(acc->regc);
979 pj_pool_release(pool);
980 acc->regc = NULL;
981 return status;
982 }
983
984 pj_strdup_with_null(pjsua_var.pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000985 }
986
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000987 status = pjsip_regc_init( acc->regc,
988 &acc->cfg.reg_uri,
989 &acc->cfg.id,
990 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000991 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 acc->cfg.reg_timeout);
993 if (status != PJ_SUCCESS) {
994 pjsua_perror(THIS_FILE,
995 "Client registration initialization error",
996 status);
Benny Prijono19b29372007-12-05 04:08:40 +0000997 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000998 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +0000999 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001000 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001001 return status;
1002 }
1003
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001004 /* If account is locked to specific transport, then set transport to
1005 * the client registration.
1006 */
1007 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1008 pjsip_tpselector tp_sel;
1009
1010 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1011 pjsip_regc_set_transport(acc->regc, &tp_sel);
1012 }
1013
1014
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001015 /* Set credentials
1016 */
1017 if (acc->cred_cnt) {
1018 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1019 }
1020
Benny Prijono48ab2b72007-11-08 09:24:30 +00001021 /* Set authentication preference */
1022 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1023
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001024 /* Set route-set
1025 */
1026 if (!pj_list_empty(&acc->route_set)) {
1027 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1028 }
1029
Benny Prijono8fc6de02006-11-11 21:25:55 +00001030 /* Add other request headers. */
1031 if (pjsua_var.ua_cfg.user_agent.slen) {
1032 pjsip_hdr hdr_list;
1033 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1034 pjsip_generic_string_hdr *h;
1035
Benny Prijono8fc6de02006-11-11 21:25:55 +00001036 pj_list_init(&hdr_list);
1037
1038 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1039 &pjsua_var.ua_cfg.user_agent);
1040 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1041
1042 pjsip_regc_add_headers(acc->regc, &hdr_list);
1043 }
1044
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001045 pj_pool_release(pool);
1046
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001047 return PJ_SUCCESS;
1048}
1049
1050
1051/*
1052 * Update registration or perform unregistration.
1053 */
1054PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1055 pj_bool_t renew)
1056{
1057 pj_status_t status = 0;
1058 pjsip_tx_data *tdata = 0;
1059
1060 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1061 PJ_EINVAL);
1062 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1063
1064 PJSUA_LOCK();
1065
1066 if (renew) {
1067 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001068 status = pjsua_regc_init(acc_id);
1069 if (status != PJ_SUCCESS) {
1070 pjsua_perror(THIS_FILE, "Unable to create registration",
1071 status);
1072 goto on_return;
1073 }
1074 }
1075 if (!pjsua_var.acc[acc_id].regc) {
1076 status = PJ_EINVALIDOP;
1077 goto on_return;
1078 }
1079
1080 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1081 &tdata);
1082
Benny Prijono28f673a2007-10-15 07:04:59 +00001083 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1084 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1085 pjsip_authorization_hdr *h;
1086 char *uri;
1087 int d;
1088
1089 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1090 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1091 uri, acc->cfg.reg_uri.slen+10);
1092 pj_assert(d > 0);
1093
1094 h = pjsip_authorization_hdr_create(tdata->pool);
1095 h->scheme = pj_str("Digest");
1096 h->credential.digest.username = acc->cred[0].username;
1097 h->credential.digest.realm = acc->srv_domain;
1098 h->credential.digest.uri = pj_str(uri);
1099 h->credential.digest.algorithm = pj_str("md5");
1100
1101 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1102 }
1103
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001104 } else {
1105 if (pjsua_var.acc[acc_id].regc == NULL) {
1106 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1107 status = PJ_EINVALIDOP;
1108 goto on_return;
1109 }
1110 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1111 }
1112
Benny Prijono56315612006-07-18 14:39:40 +00001113 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001114 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001115 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001116 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001117
1118 if (status != PJ_SUCCESS) {
1119 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1120 status);
1121 } else {
1122 PJ_LOG(3,(THIS_FILE, "%s sent",
1123 (renew? "Registration" : "Unregistration")));
1124 }
1125
1126on_return:
1127 PJSUA_UNLOCK();
1128 return status;
1129}
1130
1131
1132/*
1133 * Get account information.
1134 */
1135PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1136 pjsua_acc_info *info)
1137{
1138 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1139 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1140
1141 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001142 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001143
Benny Prijonoac623b32006-07-03 15:19:31 +00001144 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001145
1146 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1147 PJ_EINVAL);
1148 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1149
1150 PJSUA_LOCK();
1151
1152 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1153 PJSUA_UNLOCK();
1154 return PJ_EINVALIDOP;
1155 }
1156
1157 info->id = acc_id;
1158 info->is_default = (pjsua_var.default_acc == acc_id);
1159 info->acc_uri = acc_cfg->id;
1160 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1161 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001162 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1163 if (info->rpid.note.slen)
1164 info->online_status_text = info->rpid.note;
1165 else if (info->online_status)
1166 info->online_status_text = pj_str("Online");
1167 else
1168 info->online_status_text = pj_str("Offline");
1169
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001170 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001171 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001172 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1173 info->status_text = pj_str(info->buf_);
1174 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001175 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001176 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001177 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1178 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001179 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001180 info->status_text = pj_str("not registered");
1181 }
1182 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001183 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001184 info->status_text = pj_str("In Progress");
1185 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001186 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001187 info->status_text = pj_str("does not register");
1188 }
1189
1190 if (acc->regc) {
1191 pjsip_regc_info regc_info;
1192 pjsip_regc_get_info(acc->regc, &regc_info);
1193 info->expires = regc_info.next_reg;
1194 } else {
1195 info->expires = -1;
1196 }
1197
1198 PJSUA_UNLOCK();
1199
1200 return PJ_SUCCESS;
1201
1202}
1203
1204
1205/*
1206 * Enum accounts all account ids.
1207 */
1208PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1209 unsigned *count )
1210{
1211 unsigned i, c;
1212
1213 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1214
1215 PJSUA_LOCK();
1216
1217 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1218 if (!pjsua_var.acc[i].valid)
1219 continue;
1220 ids[c] = i;
1221 ++c;
1222 }
1223
1224 *count = c;
1225
1226 PJSUA_UNLOCK();
1227
1228 return PJ_SUCCESS;
1229}
1230
1231
1232/*
1233 * Enum accounts info.
1234 */
1235PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1236 unsigned *count )
1237{
1238 unsigned i, c;
1239
1240 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1241
1242 PJSUA_LOCK();
1243
1244 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1245 if (!pjsua_var.acc[i].valid)
1246 continue;
1247
1248 pjsua_acc_get_info(i, &info[c]);
1249 ++c;
1250 }
1251
1252 *count = c;
1253
1254 PJSUA_UNLOCK();
1255
1256 return PJ_SUCCESS;
1257}
1258
1259
1260/*
1261 * This is an internal function to find the most appropriate account to
1262 * used to reach to the specified URL.
1263 */
1264PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1265{
1266 pj_str_t tmp;
1267 pjsip_uri *uri;
1268 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001269 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001270
1271 PJSUA_LOCK();
1272
1273 PJ_TODO(dont_use_pjsua_pool);
1274
1275 pj_strdup_with_null(pjsua_var.pool, &tmp, url);
1276
1277 uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
1278 if (!uri) {
Benny Prijono093d3022006-09-24 00:07:11 +00001279 PJSUA_UNLOCK();
1280 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001281 }
1282
1283 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1284 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1285 {
1286 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001287 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1288 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001289 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001290 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001291 break;
1292 }
1293
Benny Prijono093d3022006-09-24 00:07:11 +00001294 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001295 /* Found rather matching account */
Benny Prijono093d3022006-09-24 00:07:11 +00001296 PJSUA_UNLOCK();
1297 return 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001298 }
1299
1300 /* Not found, use default account */
Benny Prijono093d3022006-09-24 00:07:11 +00001301 PJSUA_UNLOCK();
1302 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001303 }
1304
Benny Prijonoa1e69682007-05-11 15:14:34 +00001305 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001306
Benny Prijonob4a17c92006-07-10 14:40:21 +00001307 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001308 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1309 unsigned acc_id = pjsua_var.acc_ids[i];
1310 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1311 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1312 {
1313 PJSUA_UNLOCK();
1314 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001315 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001316 }
1317
Benny Prijonob4a17c92006-07-10 14:40:21 +00001318 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001319 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1320 unsigned acc_id = pjsua_var.acc_ids[i];
1321 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1322 {
1323 PJSUA_UNLOCK();
1324 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001325 }
1326 }
1327
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001328
Benny Prijono093d3022006-09-24 00:07:11 +00001329 /* Still no match, just use default account */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001330 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001331 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001332}
1333
1334
1335/*
1336 * This is an internal function to find the most appropriate account to be
1337 * used to handle incoming calls.
1338 */
1339PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1340{
1341 pjsip_uri *uri;
1342 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001343 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001344
Benny Prijono371cf0a2007-06-19 00:35:37 +00001345 /* Check that there's at least one account configured */
1346 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1347
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001348 uri = rdata->msg_info.to->uri;
1349
1350 /* Just return default account if To URI is not SIP: */
1351 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1352 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1353 {
1354 return pjsua_var.default_acc;
1355 }
1356
1357
1358 PJSUA_LOCK();
1359
1360 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1361
1362 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001363 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1364 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001365 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1366
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001367 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001368 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001369 {
1370 /* Match ! */
1371 PJSUA_UNLOCK();
1372 return acc_id;
1373 }
1374 }
1375
Benny Prijono093d3022006-09-24 00:07:11 +00001376 /* No matching account, try match domain part only. */
1377 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1378 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001379 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1380
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001381 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001382 /* Match ! */
1383 PJSUA_UNLOCK();
1384 return acc_id;
1385 }
1386 }
1387
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001388 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001389 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1390 unsigned acc_id = pjsua_var.acc_ids[i];
1391 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1392
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001393 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1394
1395 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1396 pjsip_transport_type_e type;
1397 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1398 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1399 type = PJSIP_TRANSPORT_UDP;
1400
1401 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1402 continue;
1403 }
1404
Benny Prijono093d3022006-09-24 00:07:11 +00001405 /* Match ! */
1406 PJSUA_UNLOCK();
1407 return acc_id;
1408 }
1409 }
1410
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001411 /* Still no match, use default account */
1412 PJSUA_UNLOCK();
1413 return pjsua_var.default_acc;
1414}
1415
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001416
Benny Prijonofff245c2007-04-02 11:44:47 +00001417/*
1418 * Create arbitrary requests for this account.
1419 */
1420PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1421 const pjsip_method *method,
1422 const pj_str_t *target,
1423 pjsip_tx_data **p_tdata)
1424{
1425 pjsip_tx_data *tdata;
1426 pjsua_acc *acc;
1427 pjsip_route_hdr *r;
1428 pj_status_t status;
1429
1430 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1431 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1432
1433 acc = &pjsua_var.acc[acc_id];
1434
1435 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1436 &acc->cfg.id, target,
1437 NULL, NULL, -1, NULL, &tdata);
1438 if (status != PJ_SUCCESS) {
1439 pjsua_perror(THIS_FILE, "Unable to create request", status);
1440 return status;
1441 }
1442
1443 /* Copy routeset */
1444 r = acc->route_set.next;
1445 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001446 pjsip_msg_add_hdr(tdata->msg,
1447 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001448 r = r->next;
1449 }
1450
1451 /* Done */
1452 *p_tdata = tdata;
1453 return PJ_SUCCESS;
1454}
1455
1456
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001457PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1458 pj_str_t *contact,
1459 pjsua_acc_id acc_id,
1460 const pj_str_t *suri)
1461{
1462 pjsua_acc *acc;
1463 pjsip_sip_uri *sip_uri;
1464 pj_status_t status;
1465 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1466 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001467 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001468 unsigned flag;
1469 int secure;
1470 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001471 const char *beginquote, *endquote;
1472 char transport_param[32];
1473
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001474
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001475 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001476 acc = &pjsua_var.acc[acc_id];
1477
Benny Prijonof75eceb2007-03-23 19:09:54 +00001478 /* If force_contact is configured, then use use it */
1479 if (acc->cfg.force_contact.slen) {
1480 *contact = acc->cfg.force_contact;
1481 return PJ_SUCCESS;
1482 }
1483
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001484 /* If route-set is configured for the account, then URI is the
1485 * first entry of the route-set.
1486 */
1487 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001488 sip_uri = (pjsip_sip_uri*)
1489 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001490 } else {
1491 pj_str_t tmp;
1492 pjsip_uri *uri;
1493
1494 pj_strdup_with_null(pool, &tmp, suri);
1495
1496 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1497 if (uri == NULL)
1498 return PJSIP_EINVALIDURI;
1499
1500 /* For non-SIP scheme, route set should be configured */
1501 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1502 return PJSIP_EINVALIDREQURI;
1503
Benny Prijono8c7a6172007-02-18 21:17:46 +00001504 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001505 }
1506
1507 /* Get transport type of the URI */
1508 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1509 tp_type = PJSIP_TRANSPORT_TLS;
1510 else if (sip_uri->transport_param.slen == 0) {
1511 tp_type = PJSIP_TRANSPORT_UDP;
1512 } else
1513 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1514
1515 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1516 return PJSIP_EUNSUPTRANSPORT;
1517
Benny Prijonod0bd4982007-12-02 15:40:52 +00001518 /* If destination URI specifies IPv6, then set transport type
1519 * to use IPv6 as well.
1520 */
Benny Prijono19b29372007-12-05 04:08:40 +00001521 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001522 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1523
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001524 flag = pjsip_transport_get_flag_from_type(tp_type);
1525 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1526
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001527 /* Init transport selector. */
1528 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1529
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001530 /* Get local address suitable to send request from */
1531 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001532 pool, tp_type, &tp_sel,
1533 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001534 if (status != PJ_SUCCESS)
1535 return status;
1536
Benny Prijonod0bd4982007-12-02 15:40:52 +00001537 /* Enclose IPv6 address in square brackets */
1538 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1539 beginquote = "[";
1540 endquote = "]";
1541 } else {
1542 beginquote = endquote = "";
1543 }
1544
1545 /* Don't add transport parameter if it's UDP */
1546 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1547 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1548 ";transport=%s",
1549 pjsip_transport_get_type_name(tp_type));
1550 } else {
1551 transport_param[0] = '\0';
1552 }
1553
1554
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001555 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001556 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001557 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001558 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001559 (int)acc->display.slen,
1560 acc->display.ptr,
1561 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001562 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001563 (int)acc->user_part.slen,
1564 acc->user_part.ptr,
1565 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001566 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001567 (int)local_addr.slen,
1568 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001569 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001570 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001571 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001572
1573 return PJ_SUCCESS;
1574}
1575
1576
1577
1578PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1579 pj_str_t *contact,
1580 pjsua_acc_id acc_id,
1581 pjsip_rx_data *rdata )
1582{
1583 /*
1584 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1585 * If the request that initiated the dialog contained a SIPS URI
1586 * in the Request-URI or in the top Record-Route header field value,
1587 * if there was any, or the Contact header field if there was no
1588 * Record-Route header field, the Contact header field in the response
1589 * MUST be a SIPS URI.
1590 */
1591 pjsua_acc *acc;
1592 pjsip_sip_uri *sip_uri;
1593 pj_status_t status;
1594 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1595 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001596 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001597 unsigned flag;
1598 int secure;
1599 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001600 const char *beginquote, *endquote;
1601 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001602
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001603 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001604 acc = &pjsua_var.acc[acc_id];
1605
Benny Prijonof75eceb2007-03-23 19:09:54 +00001606 /* If force_contact is configured, then use use it */
1607 if (acc->cfg.force_contact.slen) {
1608 *contact = acc->cfg.force_contact;
1609 return PJ_SUCCESS;
1610 }
1611
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001612 /* If Record-Route is present, then URI is the top Record-Route. */
1613 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001614 sip_uri = (pjsip_sip_uri*)
1615 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001616 } else {
1617 pjsip_contact_hdr *h_contact;
1618 pjsip_uri *uri = NULL;
1619
1620 /* Otherwise URI is Contact URI */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001621 h_contact = (pjsip_contact_hdr*)
1622 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001623 NULL);
1624 if (h_contact)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001625 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001626
1627
1628 /* Or if Contact URI is not present, take the remote URI from
1629 * the From URI.
1630 */
1631 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001632 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001633
1634
1635 /* Can only do sip/sips scheme at present. */
1636 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1637 return PJSIP_EINVALIDREQURI;
1638
Benny Prijono8c7a6172007-02-18 21:17:46 +00001639 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001640 }
1641
1642 /* Get transport type of the URI */
1643 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1644 tp_type = PJSIP_TRANSPORT_TLS;
1645 else if (sip_uri->transport_param.slen == 0) {
1646 tp_type = PJSIP_TRANSPORT_UDP;
1647 } else
1648 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001649
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001650 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1651 return PJSIP_EUNSUPTRANSPORT;
1652
Benny Prijonod0bd4982007-12-02 15:40:52 +00001653 /* If destination URI specifies IPv6, then set transport type
1654 * to use IPv6 as well.
1655 */
1656 if (pj_strchr(&sip_uri->host, ':'))
1657 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1658
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001659 flag = pjsip_transport_get_flag_from_type(tp_type);
1660 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1661
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001662 /* Init transport selector. */
1663 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1664
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001665 /* Get local address suitable to send request from */
1666 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001667 pool, tp_type, &tp_sel,
1668 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001669 if (status != PJ_SUCCESS)
1670 return status;
1671
Benny Prijonod0bd4982007-12-02 15:40:52 +00001672 /* Enclose IPv6 address in square brackets */
1673 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1674 beginquote = "[";
1675 endquote = "]";
1676 } else {
1677 beginquote = endquote = "";
1678 }
1679
1680 /* Don't add transport parameter if it's UDP */
1681 if ((tp_type & PJSIP_TRANSPORT_UDP) == 0) {
1682 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1683 ";transport=%s",
1684 pjsip_transport_get_type_name(tp_type));
1685 } else {
1686 transport_param[0] = '\0';
1687 }
1688
1689
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001690 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001691 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001692 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001693 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001694 (int)acc->display.slen,
1695 acc->display.ptr,
1696 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001697 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001698 (int)acc->user_part.slen,
1699 acc->user_part.ptr,
1700 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001701 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001702 (int)local_addr.slen,
1703 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001704 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001705 local_port,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001706 transport_param);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001707
1708 return PJ_SUCCESS;
1709}
1710
1711
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001712PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1713 pjsua_transport_id tp_id)
1714{
1715 pjsua_acc *acc;
1716
1717 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1718 acc = &pjsua_var.acc[acc_id];
1719
Benny Prijonoa1e69682007-05-11 15:14:34 +00001720 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001721 PJ_EINVAL);
1722
1723 acc->cfg.transport_id = tp_id;
1724
1725 return PJ_SUCCESS;
1726}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001727