blob: 12d7af4f0558053e0cfcf4e89547888b8924b1e8 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 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*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000111 pjsip_parse_uri(acc->pool, acc_cfg->id.ptr,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000112 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
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000139 reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000140 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
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000162 /* Save the user and domain part. These will be used when finding an
163 * account for incoming requests.
164 */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000165 acc->display = name_addr->display;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000166 acc->user_part = sip_uri->user;
Benny Prijonob4a17c92006-07-10 14:40:21 +0000167 acc->srv_domain = sip_uri->host;
168 acc->srv_port = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000169
Benny Prijonob4a17c92006-07-10 14:40:21 +0000170 if (sip_reg_uri) {
171 acc->srv_port = sip_reg_uri->port;
172 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173
174 /* Create Contact header if not present. */
Benny Prijonob4a17c92006-07-10 14:40:21 +0000175 //if (acc_cfg->contact.slen == 0) {
176 // acc_cfg->contact = acc_cfg->id;
177 //}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000179 /* Build account route-set from outbound proxies and route set from
180 * account configuration.
181 */
182 pj_list_init(&acc->route_set);
183
184 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
185 pj_str_t hname = { "Route", 5};
186 pjsip_route_hdr *r;
187 pj_str_t tmp;
188
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000189 pj_strdup_with_null(acc->pool, &tmp,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000190 &pjsua_var.ua_cfg.outbound_proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000191 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000192 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193 if (r == NULL) {
194 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
195 PJSIP_EINVALIDURI);
196 return PJSIP_EINVALIDURI;
197 }
198 pj_list_push_back(&acc->route_set, r);
199 }
200
201 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
202 pj_str_t hname = { "Route", 5};
203 pjsip_route_hdr *r;
204 pj_str_t tmp;
205
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000206 pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000207 r = (pjsip_route_hdr*)
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000208 pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000209 if (r == NULL) {
210 pjsua_perror(THIS_FILE, "Invalid URI in account route set",
211 PJ_EINVAL);
212 return PJ_EINVAL;
213 }
214 pj_list_push_back(&acc->route_set, r);
215 }
216
217
218 /* Concatenate credentials from account config and global config */
219 acc->cred_cnt = 0;
220 for (i=0; i<acc_cfg->cred_count; ++i) {
221 acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i];
222 }
223 for (i=0; i<pjsua_var.ua_cfg.cred_count &&
224 acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i)
225 {
226 acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i];
227 }
228
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000229 status = pjsua_pres_init_acc(acc_id);
230 if (status != PJ_SUCCESS)
231 return status;
232
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233 /* Mark account as valid */
234 pjsua_var.acc[acc_id].valid = PJ_TRUE;
235
Benny Prijono093d3022006-09-24 00:07:11 +0000236 /* Insert account ID into account ID array, sorted by priority */
237 for (i=0; i<pjsua_var.acc_cnt; ++i) {
238 if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
239 pjsua_var.acc[acc_id].cfg.priority)
240 {
241 break;
242 }
243 }
244 pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
245 pjsua_var.acc_cnt, i, &acc_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000246
247 return PJ_SUCCESS;
248}
249
250
251/*
252 * Add a new account to pjsua.
253 */
254PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg,
255 pj_bool_t is_default,
256 pjsua_acc_id *p_acc_id)
257{
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000258 pjsua_acc *acc;
Benny Prijono91d06b62008-09-20 12:16:56 +0000259 unsigned i, id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000260 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
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000280 acc = &pjsua_var.acc[id];
281
282 /* Create pool for this account. */
283 if (acc->pool)
284 pj_pool_reset(acc->pool);
285 else
286 acc->pool = pjsua_pool_create("acc%p", 512, 256);
287
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000288 /* Copy config */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000289 pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000290
291 /* Normalize registration timeout */
292 if (pjsua_var.acc[id].cfg.reg_uri.slen &&
293 pjsua_var.acc[id].cfg.reg_timeout == 0)
294 {
295 pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL;
296 }
297
Benny Prijono91d06b62008-09-20 12:16:56 +0000298 /* Check the route URI's and force loose route if required */
299 for (i=0; i<acc->cfg.proxy_cnt; ++i) {
300 status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]);
301 if (status != PJ_SUCCESS)
302 return status;
303 }
304
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000305 status = initialize_acc(id);
306 if (status != PJ_SUCCESS) {
307 pjsua_perror(THIS_FILE, "Error adding account", status);
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000308 pj_pool_release(acc->pool);
309 acc->pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000310 PJSUA_UNLOCK();
311 return status;
312 }
313
314 if (is_default)
315 pjsua_var.default_acc = id;
316
317 if (p_acc_id)
318 *p_acc_id = id;
319
320 pjsua_var.acc_cnt++;
321
322 PJSUA_UNLOCK();
323
324 PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d",
325 (int)cfg->id.slen, cfg->id.ptr, id));
326
327 /* If accounts has registration enabled, start registration */
328 if (pjsua_var.acc[id].cfg.reg_uri.slen)
329 pjsua_acc_set_registration(id, PJ_TRUE);
330
331
332 return PJ_SUCCESS;
333}
334
335
336/*
337 * Add local account
338 */
339PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid,
340 pj_bool_t is_default,
341 pjsua_acc_id *p_acc_id)
342{
343 pjsua_acc_config cfg;
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000344 pjsua_transport_data *t = &pjsua_var.tpdata[tid];
Benny Prijonod0bd4982007-12-02 15:40:52 +0000345 const char *beginquote, *endquote;
346 char transport_param[32];
Benny Prijonoe85bc412006-07-29 20:29:24 +0000347 char uri[PJSIP_MAX_URL_SIZE];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000348
Benny Prijonoe93e2872006-06-28 16:46:49 +0000349 /* ID must be valid */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000350 PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
351 PJ_EINVAL);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000352
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000353 /* Transport must be valid */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000354 PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355
356 pjsua_acc_config_default(&cfg);
357
Benny Prijono093d3022006-09-24 00:07:11 +0000358 /* Lower the priority of local account */
359 --cfg.priority;
360
Benny Prijonod0bd4982007-12-02 15:40:52 +0000361 /* Enclose IPv6 address in square brackets */
362 if (t->type & PJSIP_TRANSPORT_IPV6) {
363 beginquote = "[";
364 endquote = "]";
365 } else {
366 beginquote = endquote = "";
367 }
368
369 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +0000370 if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +0000371 pj_ansi_snprintf(transport_param, sizeof(transport_param),
372 ";transport=%s",
373 pjsip_transport_get_type_name(t->type));
374 } else {
375 transport_param[0] = '\0';
376 }
377
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378 /* Build URI for the account */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000379 pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000380 "<sip:%s%.*s%s:%d%s>",
381 beginquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000382 (int)t->local_name.host.slen,
383 t->local_name.host.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000384 endquote,
Benny Prijonoe85bc412006-07-29 20:29:24 +0000385 t->local_name.port,
Benny Prijonod0bd4982007-12-02 15:40:52 +0000386 transport_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387
388 cfg.id = pj_str(uri);
389
390 return pjsua_acc_add(&cfg, is_default, p_acc_id);
391}
392
393
394/*
Benny Prijono705e7842008-07-21 18:12:51 +0000395 * Set arbitrary data to be associated with the account.
396 */
397PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id,
398 void *user_data)
399{
400 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
401 PJ_EINVAL);
402 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
403
404 PJSUA_LOCK();
405
406 pjsua_var.acc[acc_id].cfg.user_data = user_data;
407
408 PJSUA_UNLOCK();
409
410 return PJ_SUCCESS;
411}
412
413
414/*
415 * Retrieve arbitrary data associated with the account.
416 */
417PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id)
418{
419 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
420 NULL);
421 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL);
422
423 return pjsua_var.acc[acc_id].cfg.user_data;
424}
425
426
427/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000428 * Delete account.
429 */
430PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
431{
Benny Prijono093d3022006-09-24 00:07:11 +0000432 unsigned i;
433
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000434 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
435 PJ_EINVAL);
436 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
437
438 PJSUA_LOCK();
439
440 /* Delete registration */
Benny Prijono922933b2007-01-21 16:23:56 +0000441 if (pjsua_var.acc[acc_id].regc != NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000442 pjsua_acc_set_registration(acc_id, PJ_FALSE);
Benny Prijonoe68e99f2007-06-06 00:28:10 +0000443 if (pjsua_var.acc[acc_id].regc) {
444 pjsip_regc_destroy(pjsua_var.acc[acc_id].regc);
445 }
Benny Prijono922933b2007-01-21 16:23:56 +0000446 pjsua_var.acc[acc_id].regc = NULL;
447 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000448
449 /* Delete server presence subscription */
450 pjsua_pres_delete_acc(acc_id);
451
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000452 /* Release account pool */
453 if (pjsua_var.acc[acc_id].pool) {
454 pj_pool_release(pjsua_var.acc[acc_id].pool);
455 pjsua_var.acc[acc_id].pool = NULL;
456 }
457
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000458 /* Invalidate */
459 pjsua_var.acc[acc_id].valid = PJ_FALSE;
Benny Prijono978de6e2008-09-15 14:56:05 +0000460 pjsua_var.acc[acc_id].contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000461
Benny Prijono093d3022006-09-24 00:07:11 +0000462 /* Remove from array */
463 for (i=0; i<pjsua_var.acc_cnt; ++i) {
464 if (pjsua_var.acc_ids[i] == acc_id)
465 break;
466 }
467 if (i != pjsua_var.acc_cnt) {
468 pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
469 pjsua_var.acc_cnt, i);
470 --pjsua_var.acc_cnt;
471 }
472
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000473 /* Leave the calls intact, as I don't think calls need to
474 * access account once it's created
475 */
476
Benny Prijonofb2b3652007-06-28 07:15:03 +0000477 /* Update default account */
478 if (pjsua_var.default_acc == acc_id)
479 pjsua_var.default_acc = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000480
481 PJSUA_UNLOCK();
482
483 PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id));
484
485 return PJ_SUCCESS;
486}
487
488
489/*
490 * Modify account information.
491 */
492PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id,
493 const pjsua_acc_config *cfg)
494{
495 PJ_TODO(pjsua_acc_modify);
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000496 PJ_UNUSED_ARG(acc_id);
497 PJ_UNUSED_ARG(cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 return PJ_EINVALIDOP;
499}
500
501
502/*
503 * Modify account's presence status to be advertised to remote/presence
504 * subscribers.
505 */
506PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id,
507 pj_bool_t is_online)
508{
509 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
510 PJ_EINVAL);
511 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
512
513 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijono4461c7d2007-08-25 13:36:15 +0000514 pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element));
515 pjsua_pres_update_acc(acc_id, PJ_FALSE);
516 return PJ_SUCCESS;
517}
518
519
520/*
521 * Set online status with extended information
522 */
523PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id,
524 pj_bool_t is_online,
525 const pjrpid_element *pr)
526{
527 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
528 PJ_EINVAL);
529 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
530
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000531 PJSUA_LOCK();
Benny Prijono4461c7d2007-08-25 13:36:15 +0000532 pjsua_var.acc[acc_id].online_status = is_online;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000533 pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr);
534 PJSUA_UNLOCK();
535
Benny Prijono4461c7d2007-08-25 13:36:15 +0000536 pjsua_pres_update_acc(acc_id, PJ_TRUE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 return PJ_SUCCESS;
538}
539
Benny Prijono7f630432008-09-24 16:52:41 +0000540/* Check if IP is private IP address */
541static pj_bool_t is_private_ip(const pj_str_t *addr)
542{
543 const pj_str_t private_net[] =
544 {
545 { "10.", 3 },
546 { "127.", 4 },
547 { "172.16.", 7 },
548 { "192.168.", 8 }
549 };
550 unsigned i;
551
552 for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) {
553 if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0)
554 return PJ_TRUE;
555 }
556
557 return PJ_FALSE;
558}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559
Benny Prijono15b02302007-09-27 14:07:07 +0000560/* Update NAT address from the REGISTER response */
561static pj_bool_t acc_check_nat_addr(pjsua_acc *acc,
562 struct pjsip_regc_cbparam *param)
563{
564 pjsip_transport *tp;
565 const pj_str_t *via_addr;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000566 pj_pool_t *pool;
Benny Prijono15b02302007-09-27 14:07:07 +0000567 int rport;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000568 pjsip_sip_uri *uri;
Benny Prijono15b02302007-09-27 14:07:07 +0000569 pjsip_via_hdr *via;
Benny Prijono7f630432008-09-24 16:52:41 +0000570 pj_str_t srv_ip;
Benny Prijono15b02302007-09-27 14:07:07 +0000571
572 tp = param->rdata->tp_info.transport;
573
574 /* Only update if account is configured to auto-update */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000575 if (acc->cfg.allow_contact_rewrite == PJ_FALSE)
Benny Prijono15b02302007-09-27 14:07:07 +0000576 return PJ_FALSE;
577
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000578#if 0
579 // Always update
580 // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html
Benny Prijono15b02302007-09-27 14:07:07 +0000581
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000582 /* For UDP, only update if STUN is enabled (for now).
583 * For TCP/TLS, always check.
584 */
585 if ((tp->key.type == PJSIP_TRANSPORT_UDP &&
586 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
587 (pjsua_var.ua_cfg.stun_host.slen != 0)) ||
588 (tp->key.type == PJSIP_TRANSPORT_TCP) ||
589 (tp->key.type == PJSIP_TRANSPORT_TLS))
Benny Prijono15b02302007-09-27 14:07:07 +0000590 {
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000591 /* Yes we will check */
592 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000593 return PJ_FALSE;
594 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000595#endif
Benny Prijono15b02302007-09-27 14:07:07 +0000596
597 /* Get the received and rport info */
598 via = param->rdata->msg_info.via;
599 if (via->rport_param < 1) {
600 /* Remote doesn't support rport */
601 rport = via->sent_by.port;
Benny Prijono24a21852008-04-14 04:04:30 +0000602 if (rport==0) {
603 pjsip_transport_type_e tp_type;
604 tp_type = (pjsip_transport_type_e) tp->key.type;
605 rport = pjsip_transport_get_default_port_for_type(tp_type);
606 }
Benny Prijono15b02302007-09-27 14:07:07 +0000607 } else
608 rport = via->rport_param;
609
610 if (via->recvd_param.slen != 0)
611 via_addr = &via->recvd_param;
612 else
613 via_addr = &via->sent_by.host;
614
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000615 /* Compare received and rport with the URI in our registration */
616 pool = pjsua_pool_create("tmp", 512, 512);
617 uri = (pjsip_sip_uri*)
618 pjsip_parse_uri(pool, acc->contact.ptr, acc->contact.slen, 0);
619 pj_assert(uri != NULL);
Benny Prijono24a21852008-04-14 04:04:30 +0000620 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000621
Benny Prijono24a21852008-04-14 04:04:30 +0000622 if (uri->port == 0) {
623 pjsip_transport_type_e tp_type;
624 tp_type = (pjsip_transport_type_e) tp->key.type;
625 uri->port = pjsip_transport_get_default_port_for_type(tp_type);
626 }
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000627
628 if (uri->port == rport &&
629 pj_stricmp(&uri->host, via_addr)==0)
Benny Prijono15b02302007-09-27 14:07:07 +0000630 {
631 /* Address doesn't change */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000632 pj_pool_release(pool);
Benny Prijono15b02302007-09-27 14:07:07 +0000633 return PJ_FALSE;
634 }
635
Benny Prijono7f630432008-09-24 16:52:41 +0000636 /* Get server IP */
637 srv_ip = pj_str(param->rdata->pkt_info.src_name);
638
Benny Prijono15b02302007-09-27 14:07:07 +0000639 /* At this point we've detected that the address as seen by registrar.
640 * has changed.
641 */
Benny Prijono7f630432008-09-24 16:52:41 +0000642
643 /* Do not switch if both Contact and server's IP address are
644 * public but response contains private IP. A NAT in the middle
645 * might have messed up with the SIP packets.
Benny Prijono247921b2008-09-26 22:06:11 +0000646 *
647 * This exception can be disabled by setting allow_contact_rewrite
648 * to 2. In this case, the switch will always be done whenever there
649 * is difference in the IP address in the response.
Benny Prijono7f630432008-09-24 16:52:41 +0000650 */
Benny Prijono247921b2008-09-26 22:06:11 +0000651 if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) &&
652 !is_private_ip(&srv_ip) && is_private_ip(via_addr))
Benny Prijono7f630432008-09-24 16:52:41 +0000653 {
654 /* Don't switch */
655 pj_pool_release(pool);
656 return PJ_FALSE;
657 }
658
Benny Prijono15b02302007-09-27 14:07:07 +0000659 PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d "
660 "(%.*s:%d --> %.*s:%d). Updating registration..",
661 acc->index,
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000662 (int)uri->host.slen,
663 uri->host.ptr,
664 uri->port,
Benny Prijono15b02302007-09-27 14:07:07 +0000665 (int)via_addr->slen,
666 via_addr->ptr,
667 rport));
668
669 /* Unregister current contact */
670 pjsua_acc_set_registration(acc->index, PJ_FALSE);
671 if (acc->regc != NULL) {
672 pjsip_regc_destroy(acc->regc);
673 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000674 acc->contact.slen = 0;
Benny Prijono15b02302007-09-27 14:07:07 +0000675 }
676
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000677 /* Update account's Contact header */
678 {
679 char *tmp;
680 int len;
681
Benny Prijono24a21852008-04-14 04:04:30 +0000682 tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000683 len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
Benny Prijono30fe4852008-12-10 16:54:16 +0000684 "<sip:%.*s%s%.*s:%d;transport=%s%.*s>",
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000685 (int)acc->user_part.slen,
686 acc->user_part.ptr,
Benny Prijono83088f32008-04-22 18:33:55 +0000687 (acc->user_part.slen? "@" : ""),
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000688 (int)via_addr->slen,
689 via_addr->ptr,
690 rport,
Benny Prijono30fe4852008-12-10 16:54:16 +0000691 tp->type_name,
692 (int)acc->cfg.contact_params.slen,
693 acc->cfg.contact_params.ptr);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000694 if (len < 1) {
695 PJ_LOG(1,(THIS_FILE, "URI too long"));
696 pj_pool_release(pool);
697 return PJ_FALSE;
698 }
Benny Prijono3d9b4b62008-07-14 17:55:40 +0000699 pj_strdup2_with_null(acc->pool, &acc->contact, tmp);
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000700 }
701
702 /* For UDP transport, if STUN is enabled then update the transport's
703 * published name as well.
704 */
705 if (tp->key.type==PJSIP_TRANSPORT_UDP &&
706 (pjsua_var.ua_cfg.stun_domain.slen != 0 ||
707 pjsua_var.ua_cfg.stun_host.slen != 0))
708 {
709 pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr);
710 tp->local_name.port = rport;
711 }
Benny Prijono15b02302007-09-27 14:07:07 +0000712
713 /* Perform new registration */
714 pjsua_acc_set_registration(acc->index, PJ_TRUE);
715
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000716 pj_pool_release(pool);
717
Benny Prijono15b02302007-09-27 14:07:07 +0000718 return PJ_TRUE;
719}
720
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000721/* Check and update Service-Route header */
722void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata)
723{
724 pjsip_generic_string_hdr *hsr = NULL;
725 pjsip_route_hdr *hr, *h;
726 const pj_str_t HNAME = { "Service-Route", 13 };
727 const pj_str_t HROUTE = { "Route", 5 };
728 pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES];
Benny Prijonof020ab22007-10-18 15:28:33 +0000729 unsigned i, uri_cnt = 0, rcnt;
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000730
731 /* Find and parse Service-Route headers */
732 for (;;) {
733 char saved;
734 int parsed_len;
735
736 /* Find Service-Route header */
737 hsr = (pjsip_generic_string_hdr*)
738 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr);
739 if (!hsr)
740 break;
741
742 /* Parse as Route header since the syntax is similar. This may
743 * return more than one headers.
744 */
745 saved = hsr->hvalue.ptr[hsr->hvalue.slen];
746 hsr->hvalue.ptr[hsr->hvalue.slen] = '\0';
747 hr = (pjsip_route_hdr*)
748 pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr,
749 hsr->hvalue.slen, &parsed_len);
750 hsr->hvalue.ptr[hsr->hvalue.slen] = saved;
751
752 if (hr == NULL) {
753 /* Error */
754 PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header"));
755 return;
756 }
757
758 /* Save each URI in the result */
759 h = hr;
760 do {
761 if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) &&
762 !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri))
763 {
764 PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s",
765 (int)hsr->hvalue.slen, hsr->hvalue.ptr));
766 return;
767 }
768
769 uri[uri_cnt++] = h->name_addr.uri;
770 h = h->next;
771 } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri));
772
773 if (h != hr) {
774 PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers"));
775 return;
776 }
777
778 /* Prepare to find next Service-Route header */
779 hsr = hsr->next;
780 if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr)
781 break;
782 }
783
784 if (uri_cnt == 0)
785 return;
786
787 /*
788 * Update account's route set
789 */
790
791 /* First remove all routes which are not the outbound proxies */
Benny Prijonof020ab22007-10-18 15:28:33 +0000792 rcnt = pj_list_size(&acc->route_set);
Benny Prijono2568c742007-11-03 09:29:52 +0000793 if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) {
794 for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt,
795 hr=acc->route_set.prev;
Benny Prijonof020ab22007-10-18 15:28:33 +0000796 i<rcnt;
797 ++i)
798 {
799 pjsip_route_hdr *prev = hr->prev;
800 pj_list_erase(hr);
801 hr = prev;
802 }
803 }
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000804
805 /* Then append the Service-Route URIs */
806 for (i=0; i<uri_cnt; ++i) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000807 hr = pjsip_route_hdr_create(acc->pool);
808 hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]);
Benny Prijonoa2a2d412007-10-18 05:54:02 +0000809 pj_list_push_back(&acc->route_set, hr);
810 }
811
812 /* Done */
813
814 PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)",
815 acc->index, uri_cnt));
816}
817
Benny Prijonobddef2c2007-10-31 13:28:08 +0000818
819/* Keep alive timer callback */
820static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te)
821{
822 pjsua_acc *acc;
823 pjsip_tpselector tp_sel;
824 pj_time_val delay;
Benny Prijonodb4eb812007-12-09 05:30:47 +0000825 char addrtxt[PJ_INET6_ADDRSTRLEN];
Benny Prijonobddef2c2007-10-31 13:28:08 +0000826 pj_status_t status;
827
828 PJ_UNUSED_ARG(th);
829
830 PJSUA_LOCK();
831
832 te->id = PJ_FALSE;
833
834 acc = (pjsua_acc*) te->user_data;
835
836 /* Select the transport to send the packet */
837 pj_bzero(&tp_sel, sizeof(tp_sel));
838 tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT;
839 tp_sel.u.transport = acc->ka_transport;
840
841 PJ_LOG(5,(THIS_FILE,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000842 "Sending %d bytes keep-alive packet for acc %d to %s",
Benny Prijonobddef2c2007-10-31 13:28:08 +0000843 acc->cfg.ka_data.slen, acc->index,
Benny Prijonodb4eb812007-12-09 05:30:47 +0000844 pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3)));
Benny Prijonobddef2c2007-10-31 13:28:08 +0000845
846 /* Send raw packet */
847 status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
848 PJSIP_TRANSPORT_UDP, &tp_sel,
849 NULL, acc->cfg.ka_data.ptr,
850 acc->cfg.ka_data.slen,
851 &acc->ka_target, acc->ka_target_len,
852 NULL, NULL);
853
854 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
855 pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status);
856 }
857
858 /* Reschedule next timer */
859 delay.sec = acc->cfg.ka_interval;
860 delay.msec = 0;
861 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay);
862 if (status == PJ_SUCCESS) {
863 te->id = PJ_TRUE;
864 } else {
865 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
866 }
867
868 PJSUA_UNLOCK();
869}
870
871
872/* Update keep-alive for the account */
873static void update_keep_alive(pjsua_acc *acc, pj_bool_t start,
874 struct pjsip_regc_cbparam *param)
875{
876 /* In all cases, stop keep-alive timer if it's running. */
877 if (acc->ka_timer.id) {
878 pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer);
879 acc->ka_timer.id = PJ_FALSE;
880
881 pjsip_transport_dec_ref(acc->ka_transport);
882 acc->ka_transport = NULL;
883 }
884
885 if (start) {
886 pj_time_val delay;
887 pj_status_t status;
888
889 /* Only do keep-alive if:
Benny Prijonobddef2c2007-10-31 13:28:08 +0000890 * - ka_interval is not zero in the account, and
891 * - transport is UDP.
Benny Prijono7fff9f92008-04-04 10:50:21 +0000892 *
893 * Previously we only enabled keep-alive when STUN is enabled, since
894 * we thought that keep-alive is only needed in Internet situation.
895 * But it has been discovered that Windows Firewall on WinXP also
896 * needs to be kept-alive, otherwise incoming packets will be dropped.
897 * So because of this, now keep-alive is always enabled for UDP,
898 * regardless of whether STUN is enabled or not.
899 *
900 * Note that this applies only for UDP. For TCP/TLS, the keep-alive
901 * is done by the transport layer.
Benny Prijonobddef2c2007-10-31 13:28:08 +0000902 */
Benny Prijono7fff9f92008-04-04 10:50:21 +0000903 if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/
Benny Prijonobddef2c2007-10-31 13:28:08 +0000904 acc->cfg.ka_interval == 0 ||
905 param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP)
906 {
907 /* Keep alive is not necessary */
908 return;
909 }
910
911 /* Save transport and destination address. */
912 acc->ka_transport = param->rdata->tp_info.transport;
913 pjsip_transport_add_ref(acc->ka_transport);
914 pj_memcpy(&acc->ka_target, &param->rdata->pkt_info.src_addr,
915 param->rdata->pkt_info.src_addr_len);
916 acc->ka_target_len = param->rdata->pkt_info.src_addr_len;
917
918 /* Setup and start the timer */
919 acc->ka_timer.cb = &keep_alive_timer_cb;
920 acc->ka_timer.user_data = (void*)acc;
921
922 delay.sec = acc->cfg.ka_interval;
923 delay.msec = 0;
924 status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer,
925 &delay);
926 if (status == PJ_SUCCESS) {
927 acc->ka_timer.id = PJ_TRUE;
928 PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, "
929 "destination:%s:%d, interval:%ds",
930 acc->index,
931 param->rdata->pkt_info.src_name,
932 param->rdata->pkt_info.src_port,
933 acc->cfg.ka_interval));
934 } else {
935 acc->ka_timer.id = PJ_FALSE;
936 pjsip_transport_dec_ref(acc->ka_transport);
937 acc->ka_transport = NULL;
938 pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status);
939 }
940 }
941}
942
943
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000944/*
945 * This callback is called by pjsip_regc when outgoing register
946 * request has completed.
947 */
948static void regc_cb(struct pjsip_regc_cbparam *param)
949{
950
Benny Prijonoa1e69682007-05-11 15:14:34 +0000951 pjsua_acc *acc = (pjsua_acc*) param->token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000952
Benny Prijono15b02302007-09-27 14:07:07 +0000953 if (param->regc != acc->regc)
954 return;
955
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000956 PJSUA_LOCK();
957
958 /*
959 * Print registration status.
960 */
961 if (param->status!=PJ_SUCCESS) {
962 pjsua_perror(THIS_FILE, "SIP registration error",
963 param->status);
964 pjsip_regc_destroy(acc->regc);
965 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000966 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000967
Benny Prijonobddef2c2007-10-31 13:28:08 +0000968 /* Stop keep-alive timer if any. */
969 update_keep_alive(acc, PJ_FALSE, NULL);
970
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000971 } else if (param->code < 0 || param->code >= 300) {
972 PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)",
973 param->code,
974 (int)param->reason.slen, param->reason.ptr));
975 pjsip_regc_destroy(acc->regc);
976 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000977 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000978
Benny Prijonobddef2c2007-10-31 13:28:08 +0000979 /* Stop keep-alive timer if any. */
980 update_keep_alive(acc, PJ_FALSE, NULL);
981
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000982 } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) {
983
984 if (param->expiration < 1) {
985 pjsip_regc_destroy(acc->regc);
986 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +0000987 acc->contact.slen = 0;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000988
989 /* Stop keep-alive timer if any. */
990 update_keep_alive(acc, PJ_FALSE, NULL);
991
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 PJ_LOG(3,(THIS_FILE, "%s: unregistration success",
993 pjsua_var.acc[acc->index].cfg.id.ptr));
994 } else {
Benny Prijono15b02302007-09-27 14:07:07 +0000995 /* Check NAT bound address */
996 if (acc_check_nat_addr(acc, param)) {
997 /* Update address, don't notify application yet */
998 PJSUA_UNLOCK();
999 return;
1000 }
1001
Benny Prijonoa2a2d412007-10-18 05:54:02 +00001002 /* Check and update Service-Route header */
1003 update_service_route(acc, param->rdata);
1004
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001005 PJ_LOG(3, (THIS_FILE,
1006 "%s: registration success, status=%d (%.*s), "
1007 "will re-register in %d seconds",
1008 pjsua_var.acc[acc->index].cfg.id.ptr,
1009 param->code,
1010 (int)param->reason.slen, param->reason.ptr,
1011 param->expiration));
Benny Prijono8b6834f2007-02-24 13:29:22 +00001012
Benny Prijonobddef2c2007-10-31 13:28:08 +00001013 /* Start keep-alive timer if necessary. */
1014 update_keep_alive(acc, PJ_TRUE, param);
1015
Benny Prijono8b6834f2007-02-24 13:29:22 +00001016 /* Send initial PUBLISH if it is enabled */
1017 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
1018 pjsua_pres_init_publish_acc(acc->index);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001019 }
1020
1021 } else {
1022 PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code));
1023 }
1024
1025 acc->reg_last_err = param->status;
1026 acc->reg_last_code = param->code;
1027
1028 if (pjsua_var.ua_cfg.cb.on_reg_state)
1029 (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index);
1030
1031 PJSUA_UNLOCK();
1032}
1033
1034
1035/*
1036 * Initialize client registration.
1037 */
1038static pj_status_t pjsua_regc_init(int acc_id)
1039{
1040 pjsua_acc *acc;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001041 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001042 pj_status_t status;
1043
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001044 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 acc = &pjsua_var.acc[acc_id];
1046
1047 if (acc->cfg.reg_uri.slen == 0) {
1048 PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified"));
1049 return PJ_SUCCESS;
1050 }
1051
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001052 /* Destroy existing session, if any */
1053 if (acc->regc) {
1054 pjsip_regc_destroy(acc->regc);
1055 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001056 acc->contact.slen = 0;
Benny Prijonoe1a8bad2006-10-13 17:45:38 +00001057 }
1058
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001059 /* initialize SIP registration if registrar is configured */
1060
1061 status = pjsip_regc_create( pjsua_var.endpt,
1062 acc, &regc_cb, &acc->regc);
1063
1064 if (status != PJ_SUCCESS) {
1065 pjsua_perror(THIS_FILE, "Unable to create client registration",
1066 status);
1067 return status;
1068 }
1069
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001070 pool = pjsua_pool_create("tmpregc", 512, 512);
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001071
1072 if (acc->contact.slen == 0) {
1073 pj_str_t tmp_contact;
1074
1075 status = pjsua_acc_create_uac_contact( pool, &tmp_contact,
1076 acc_id, &acc->cfg.reg_uri);
1077 if (status != PJ_SUCCESS) {
1078 pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header"
1079 " for registration",
1080 status);
1081 pjsip_regc_destroy(acc->regc);
1082 pj_pool_release(pool);
1083 acc->regc = NULL;
1084 return status;
1085 }
1086
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001087 pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001088 }
1089
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001090 status = pjsip_regc_init( acc->regc,
1091 &acc->cfg.reg_uri,
1092 &acc->cfg.id,
1093 &acc->cfg.id,
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001094 1, &acc->contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001095 acc->cfg.reg_timeout);
1096 if (status != PJ_SUCCESS) {
1097 pjsua_perror(THIS_FILE,
1098 "Client registration initialization error",
1099 status);
Benny Prijono19b29372007-12-05 04:08:40 +00001100 pjsip_regc_destroy(acc->regc);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001101 pj_pool_release(pool);
Benny Prijono19b29372007-12-05 04:08:40 +00001102 acc->regc = NULL;
Benny Prijonoddaaf6a2008-04-15 10:37:19 +00001103 acc->contact.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001104 return status;
1105 }
1106
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001107 /* If account is locked to specific transport, then set transport to
1108 * the client registration.
1109 */
1110 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
1111 pjsip_tpselector tp_sel;
1112
1113 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1114 pjsip_regc_set_transport(acc->regc, &tp_sel);
1115 }
1116
1117
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001118 /* Set credentials
1119 */
1120 if (acc->cred_cnt) {
1121 pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred);
1122 }
1123
Benny Prijono48ab2b72007-11-08 09:24:30 +00001124 /* Set authentication preference */
1125 pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref);
1126
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001127 /* Set route-set
1128 */
1129 if (!pj_list_empty(&acc->route_set)) {
1130 pjsip_regc_set_route_set( acc->regc, &acc->route_set );
1131 }
1132
Benny Prijono8fc6de02006-11-11 21:25:55 +00001133 /* Add other request headers. */
1134 if (pjsua_var.ua_cfg.user_agent.slen) {
1135 pjsip_hdr hdr_list;
1136 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1137 pjsip_generic_string_hdr *h;
1138
Benny Prijono8fc6de02006-11-11 21:25:55 +00001139 pj_list_init(&hdr_list);
1140
1141 h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT,
1142 &pjsua_var.ua_cfg.user_agent);
1143 pj_list_push_back(&hdr_list, (pjsip_hdr*)h);
1144
1145 pjsip_regc_add_headers(acc->regc, &hdr_list);
1146 }
1147
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001148 pj_pool_release(pool);
1149
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001150 return PJ_SUCCESS;
1151}
1152
1153
1154/*
1155 * Update registration or perform unregistration.
1156 */
1157PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id,
1158 pj_bool_t renew)
1159{
1160 pj_status_t status = 0;
1161 pjsip_tx_data *tdata = 0;
1162
1163 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1164 PJ_EINVAL);
1165 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1166
1167 PJSUA_LOCK();
1168
1169 if (renew) {
1170 if (pjsua_var.acc[acc_id].regc == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001171 status = pjsua_regc_init(acc_id);
1172 if (status != PJ_SUCCESS) {
1173 pjsua_perror(THIS_FILE, "Unable to create registration",
1174 status);
1175 goto on_return;
1176 }
1177 }
1178 if (!pjsua_var.acc[acc_id].regc) {
1179 status = PJ_EINVALIDOP;
1180 goto on_return;
1181 }
1182
1183 status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1,
1184 &tdata);
1185
Benny Prijono28f673a2007-10-15 07:04:59 +00001186 if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) {
1187 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1188 pjsip_authorization_hdr *h;
1189 char *uri;
1190 int d;
1191
1192 uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10);
1193 d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri,
1194 uri, acc->cfg.reg_uri.slen+10);
1195 pj_assert(d > 0);
1196
1197 h = pjsip_authorization_hdr_create(tdata->pool);
1198 h->scheme = pj_str("Digest");
1199 h->credential.digest.username = acc->cred[0].username;
1200 h->credential.digest.realm = acc->srv_domain;
1201 h->credential.digest.uri = pj_str(uri);
1202 h->credential.digest.algorithm = pj_str("md5");
1203
1204 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h);
1205 }
1206
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001207 } else {
1208 if (pjsua_var.acc[acc_id].regc == NULL) {
1209 PJ_LOG(3,(THIS_FILE, "Currently not registered"));
1210 status = PJ_EINVALIDOP;
1211 goto on_return;
1212 }
1213 status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
1214 }
1215
Benny Prijono56315612006-07-18 14:39:40 +00001216 if (status == PJ_SUCCESS) {
Benny Prijono8fc6de02006-11-11 21:25:55 +00001217 //pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001218 status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata );
Benny Prijono56315612006-07-18 14:39:40 +00001219 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001220
1221 if (status != PJ_SUCCESS) {
1222 pjsua_perror(THIS_FILE, "Unable to create/send REGISTER",
1223 status);
1224 } else {
1225 PJ_LOG(3,(THIS_FILE, "%s sent",
1226 (renew? "Registration" : "Unregistration")));
1227 }
1228
1229on_return:
1230 PJSUA_UNLOCK();
1231 return status;
1232}
1233
1234
1235/*
1236 * Get account information.
1237 */
1238PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id,
1239 pjsua_acc_info *info)
1240{
1241 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1242 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1243
1244 PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL);
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001245 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001246
Benny Prijonoac623b32006-07-03 15:19:31 +00001247 pj_bzero(info, sizeof(pjsua_acc_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001248
1249 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1250 PJ_EINVAL);
1251 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1252
1253 PJSUA_LOCK();
1254
1255 if (pjsua_var.acc[acc_id].valid == PJ_FALSE) {
1256 PJSUA_UNLOCK();
1257 return PJ_EINVALIDOP;
1258 }
1259
1260 info->id = acc_id;
1261 info->is_default = (pjsua_var.default_acc == acc_id);
1262 info->acc_uri = acc_cfg->id;
1263 info->has_registration = (acc->cfg.reg_uri.slen > 0);
1264 info->online_status = acc->online_status;
Benny Prijono4461c7d2007-08-25 13:36:15 +00001265 pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element));
1266 if (info->rpid.note.slen)
1267 info->online_status_text = info->rpid.note;
1268 else if (info->online_status)
1269 info->online_status_text = pj_str("Online");
1270 else
1271 info->online_status_text = pj_str("Offline");
1272
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273 if (acc->reg_last_err) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001274 info->status = (pjsip_status_code) acc->reg_last_err;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001275 pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_));
1276 info->status_text = pj_str(info->buf_);
1277 } else if (acc->reg_last_code) {
Benny Prijono6f979412006-06-15 12:25:46 +00001278 if (info->has_registration) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001279 info->status = (pjsip_status_code) acc->reg_last_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001280 info->status_text = *pjsip_get_status_text(acc->reg_last_code);
1281 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001282 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001283 info->status_text = pj_str("not registered");
1284 }
1285 } else if (acc->cfg.reg_uri.slen) {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001286 info->status = PJSIP_SC_TRYING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001287 info->status_text = pj_str("In Progress");
1288 } else {
Benny Prijonoba5926a2007-05-02 11:29:37 +00001289 info->status = (pjsip_status_code) 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001290 info->status_text = pj_str("does not register");
1291 }
1292
1293 if (acc->regc) {
1294 pjsip_regc_info regc_info;
1295 pjsip_regc_get_info(acc->regc, &regc_info);
1296 info->expires = regc_info.next_reg;
1297 } else {
1298 info->expires = -1;
1299 }
1300
1301 PJSUA_UNLOCK();
1302
1303 return PJ_SUCCESS;
1304
1305}
1306
1307
1308/*
1309 * Enum accounts all account ids.
1310 */
1311PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[],
1312 unsigned *count )
1313{
1314 unsigned i, c;
1315
1316 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
1317
1318 PJSUA_LOCK();
1319
1320 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1321 if (!pjsua_var.acc[i].valid)
1322 continue;
1323 ids[c] = i;
1324 ++c;
1325 }
1326
1327 *count = c;
1328
1329 PJSUA_UNLOCK();
1330
1331 return PJ_SUCCESS;
1332}
1333
1334
1335/*
1336 * Enum accounts info.
1337 */
1338PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[],
1339 unsigned *count )
1340{
1341 unsigned i, c;
1342
1343 PJ_ASSERT_RETURN(info && *count, PJ_EINVAL);
1344
1345 PJSUA_LOCK();
1346
1347 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1348 if (!pjsua_var.acc[i].valid)
1349 continue;
1350
1351 pjsua_acc_get_info(i, &info[c]);
1352 ++c;
1353 }
1354
1355 *count = c;
1356
1357 PJSUA_UNLOCK();
1358
1359 return PJ_SUCCESS;
1360}
1361
1362
1363/*
1364 * This is an internal function to find the most appropriate account to
1365 * used to reach to the specified URL.
1366 */
1367PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url)
1368{
1369 pj_str_t tmp;
1370 pjsip_uri *uri;
1371 pjsip_sip_uri *sip_uri;
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001372 pj_pool_t *tmp_pool;
Benny Prijono093d3022006-09-24 00:07:11 +00001373 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001374
1375 PJSUA_LOCK();
1376
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001377 tmp_pool = pjsua_pool_create("tmpacc10", 256, 256);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001378
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001379 pj_strdup_with_null(tmp_pool, &tmp, url);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001380
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001381 uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001382 if (!uri) {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001383 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001384 PJSUA_UNLOCK();
1385 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001386 }
1387
1388 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1389 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1390 {
1391 /* Return the first account with proxy */
Benny Prijono093d3022006-09-24 00:07:11 +00001392 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1393 if (!pjsua_var.acc[i].valid)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001394 continue;
Benny Prijono093d3022006-09-24 00:07:11 +00001395 if (!pj_list_empty(&pjsua_var.acc[i].route_set))
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001396 break;
1397 }
1398
Benny Prijono093d3022006-09-24 00:07:11 +00001399 if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001400 /* Found rather matching account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001401 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001402 PJSUA_UNLOCK();
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001403 return i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001404 }
1405
1406 /* Not found, use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001407 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001408 PJSUA_UNLOCK();
1409 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001410 }
1411
Benny Prijonoa1e69682007-05-11 15:14:34 +00001412 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001413
Benny Prijonob4a17c92006-07-10 14:40:21 +00001414 /* Find matching domain AND port */
Benny Prijono093d3022006-09-24 00:07:11 +00001415 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1416 unsigned acc_id = pjsua_var.acc_ids[i];
1417 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
1418 pjsua_var.acc[acc_id].srv_port == sip_uri->port)
1419 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001420 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001421 PJSUA_UNLOCK();
1422 return acc_id;
Benny Prijono21b9ad92006-08-15 13:11:22 +00001423 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001424 }
1425
Benny Prijonob4a17c92006-07-10 14:40:21 +00001426 /* If no match, try to match the domain part only */
Benny Prijono093d3022006-09-24 00:07:11 +00001427 for (i=0; i<pjsua_var.acc_cnt; ++i) {
1428 unsigned acc_id = pjsua_var.acc_ids[i];
1429 if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
1430 {
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001431 pj_pool_release(tmp_pool);
Benny Prijono093d3022006-09-24 00:07:11 +00001432 PJSUA_UNLOCK();
1433 return acc_id;
Benny Prijonob4a17c92006-07-10 14:40:21 +00001434 }
1435 }
1436
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001437
Benny Prijono093d3022006-09-24 00:07:11 +00001438 /* Still no match, just use default account */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001439 pj_pool_release(tmp_pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001440 PJSUA_UNLOCK();
Benny Prijono093d3022006-09-24 00:07:11 +00001441 return pjsua_var.default_acc;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001442}
1443
1444
1445/*
1446 * This is an internal function to find the most appropriate account to be
1447 * used to handle incoming calls.
1448 */
1449PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata)
1450{
1451 pjsip_uri *uri;
1452 pjsip_sip_uri *sip_uri;
Benny Prijono093d3022006-09-24 00:07:11 +00001453 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001454
Benny Prijono371cf0a2007-06-19 00:35:37 +00001455 /* Check that there's at least one account configured */
1456 PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc);
1457
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001458 uri = rdata->msg_info.to->uri;
1459
1460 /* Just return default account if To URI is not SIP: */
1461 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1462 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1463 {
1464 return pjsua_var.default_acc;
1465 }
1466
1467
1468 PJSUA_LOCK();
1469
1470 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
1471
1472 /* Find account which has matching username and domain. */
Benny Prijono093d3022006-09-24 00:07:11 +00001473 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1474 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001475 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1476
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001477 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
Benny Prijonob4a17c92006-07-10 14:40:21 +00001478 pj_stricmp(&acc->srv_domain, &sip_uri->host)==0)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001479 {
1480 /* Match ! */
1481 PJSUA_UNLOCK();
1482 return acc_id;
1483 }
1484 }
1485
Benny Prijono093d3022006-09-24 00:07:11 +00001486 /* No matching account, try match domain part only. */
1487 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1488 unsigned acc_id = pjsua_var.acc_ids[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001489 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1490
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001491 if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001492 /* Match ! */
1493 PJSUA_UNLOCK();
1494 return acc_id;
1495 }
1496 }
1497
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001498 /* No matching account, try match user part (and transport type) only. */
Benny Prijono093d3022006-09-24 00:07:11 +00001499 for (i=0; i < pjsua_var.acc_cnt; ++i) {
1500 unsigned acc_id = pjsua_var.acc_ids[i];
1501 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1502
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001503 if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
1504
1505 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1506 pjsip_transport_type_e type;
1507 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1508 if (type == PJSIP_TRANSPORT_UNSPECIFIED)
1509 type = PJSIP_TRANSPORT_UDP;
1510
1511 if (pjsua_var.tpdata[acc->cfg.transport_id].type != type)
1512 continue;
1513 }
1514
Benny Prijono093d3022006-09-24 00:07:11 +00001515 /* Match ! */
1516 PJSUA_UNLOCK();
1517 return acc_id;
1518 }
1519 }
1520
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001521 /* Still no match, use default account */
1522 PJSUA_UNLOCK();
1523 return pjsua_var.default_acc;
1524}
1525
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001526
Benny Prijonofff245c2007-04-02 11:44:47 +00001527/*
1528 * Create arbitrary requests for this account.
1529 */
1530PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id,
1531 const pjsip_method *method,
1532 const pj_str_t *target,
1533 pjsip_tx_data **p_tdata)
1534{
1535 pjsip_tx_data *tdata;
1536 pjsua_acc *acc;
1537 pjsip_route_hdr *r;
1538 pj_status_t status;
1539
1540 PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL);
1541 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1542
1543 acc = &pjsua_var.acc[acc_id];
1544
1545 status = pjsip_endpt_create_request(pjsua_var.endpt, method, target,
1546 &acc->cfg.id, target,
1547 NULL, NULL, -1, NULL, &tdata);
1548 if (status != PJ_SUCCESS) {
1549 pjsua_perror(THIS_FILE, "Unable to create request", status);
1550 return status;
1551 }
1552
1553 /* Copy routeset */
1554 r = acc->route_set.next;
1555 while (r != &acc->route_set) {
Benny Prijonoa1e69682007-05-11 15:14:34 +00001556 pjsip_msg_add_hdr(tdata->msg,
1557 (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r));
Benny Prijonofff245c2007-04-02 11:44:47 +00001558 r = r->next;
1559 }
1560
1561 /* Done */
1562 *p_tdata = tdata;
1563 return PJ_SUCCESS;
1564}
1565
1566
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001567PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool,
1568 pj_str_t *contact,
1569 pjsua_acc_id acc_id,
1570 const pj_str_t *suri)
1571{
1572 pjsua_acc *acc;
1573 pjsip_sip_uri *sip_uri;
1574 pj_status_t status;
1575 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1576 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001577 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001578 unsigned flag;
1579 int secure;
1580 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001581 const char *beginquote, *endquote;
1582 char transport_param[32];
1583
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001584
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001585 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001586 acc = &pjsua_var.acc[acc_id];
1587
Benny Prijonof75eceb2007-03-23 19:09:54 +00001588 /* If force_contact is configured, then use use it */
1589 if (acc->cfg.force_contact.slen) {
1590 *contact = acc->cfg.force_contact;
1591 return PJ_SUCCESS;
1592 }
1593
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001594 /* If route-set is configured for the account, then URI is the
1595 * first entry of the route-set.
1596 */
1597 if (!pj_list_empty(&acc->route_set)) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001598 sip_uri = (pjsip_sip_uri*)
1599 pjsip_uri_get_uri(acc->route_set.next->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001600 } else {
1601 pj_str_t tmp;
1602 pjsip_uri *uri;
1603
1604 pj_strdup_with_null(pool, &tmp, suri);
1605
1606 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1607 if (uri == NULL)
1608 return PJSIP_EINVALIDURI;
1609
1610 /* For non-SIP scheme, route set should be configured */
1611 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1612 return PJSIP_EINVALIDREQURI;
1613
Benny Prijono8c7a6172007-02-18 21:17:46 +00001614 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001615 }
1616
1617 /* Get transport type of the URI */
1618 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1619 tp_type = PJSIP_TRANSPORT_TLS;
1620 else if (sip_uri->transport_param.slen == 0) {
1621 tp_type = PJSIP_TRANSPORT_UDP;
1622 } else
1623 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1624
1625 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1626 return PJSIP_EUNSUPTRANSPORT;
1627
Benny Prijonod0bd4982007-12-02 15:40:52 +00001628 /* If destination URI specifies IPv6, then set transport type
1629 * to use IPv6 as well.
1630 */
Benny Prijono19b29372007-12-05 04:08:40 +00001631 if (pj_strchr(&sip_uri->host, ':'))
Benny Prijonod0bd4982007-12-02 15:40:52 +00001632 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1633
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001634 flag = pjsip_transport_get_flag_from_type(tp_type);
1635 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1636
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001637 /* Init transport selector. */
1638 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1639
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001640 /* Get local address suitable to send request from */
1641 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001642 pool, tp_type, &tp_sel,
1643 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001644 if (status != PJ_SUCCESS)
1645 return status;
1646
Benny Prijonod0bd4982007-12-02 15:40:52 +00001647 /* Enclose IPv6 address in square brackets */
1648 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1649 beginquote = "[";
1650 endquote = "]";
1651 } else {
1652 beginquote = endquote = "";
1653 }
1654
1655 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00001656 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00001657 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1658 ";transport=%s",
1659 pjsip_transport_get_type_name(tp_type));
1660 } else {
1661 transport_param[0] = '\0';
1662 }
1663
1664
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001665 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001666 contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001667 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijono30fe4852008-12-10 16:54:16 +00001668 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001669 (int)acc->display.slen,
1670 acc->display.ptr,
1671 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001672 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001673 (int)acc->user_part.slen,
1674 acc->user_part.ptr,
1675 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001676 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001677 (int)local_addr.slen,
1678 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001679 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001680 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00001681 transport_param,
1682 (int)acc->cfg.contact_params.slen,
1683 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001684
1685 return PJ_SUCCESS;
1686}
1687
1688
1689
1690PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool,
1691 pj_str_t *contact,
1692 pjsua_acc_id acc_id,
1693 pjsip_rx_data *rdata )
1694{
1695 /*
1696 * Section 12.1.1, paragraph about using SIPS URI in Contact.
1697 * If the request that initiated the dialog contained a SIPS URI
1698 * in the Request-URI or in the top Record-Route header field value,
1699 * if there was any, or the Contact header field if there was no
1700 * Record-Route header field, the Contact header field in the response
1701 * MUST be a SIPS URI.
1702 */
1703 pjsua_acc *acc;
1704 pjsip_sip_uri *sip_uri;
1705 pj_status_t status;
1706 pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED;
1707 pj_str_t local_addr;
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001708 pjsip_tpselector tp_sel;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001709 unsigned flag;
1710 int secure;
1711 int local_port;
Benny Prijonod0bd4982007-12-02 15:40:52 +00001712 const char *beginquote, *endquote;
1713 char transport_param[32];
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001714
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001715 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001716 acc = &pjsua_var.acc[acc_id];
1717
Benny Prijonof75eceb2007-03-23 19:09:54 +00001718 /* If force_contact is configured, then use use it */
1719 if (acc->cfg.force_contact.slen) {
1720 *contact = acc->cfg.force_contact;
1721 return PJ_SUCCESS;
1722 }
1723
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001724 /* If Record-Route is present, then URI is the top Record-Route. */
1725 if (rdata->msg_info.record_route) {
Benny Prijono9c1528f2007-02-10 19:22:25 +00001726 sip_uri = (pjsip_sip_uri*)
1727 pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001728 } else {
Benny Prijonoa330d452008-08-05 20:14:39 +00001729 pjsip_hdr *pos = NULL;
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001730 pjsip_contact_hdr *h_contact;
1731 pjsip_uri *uri = NULL;
1732
Benny Prijonoa330d452008-08-05 20:14:39 +00001733 /* Otherwise URI is Contact URI.
1734 * Iterate the Contact URI until we find sip: or sips: scheme.
1735 */
1736 do {
1737 h_contact = (pjsip_contact_hdr*)
1738 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
1739 pos);
1740 if (h_contact) {
1741 uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
1742 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
1743 !PJSIP_URI_SCHEME_IS_SIPS(uri))
1744 {
1745 pos = (pjsip_hdr*)h_contact->next;
1746 if (pos == &rdata->msg_info.msg->hdr)
1747 h_contact = NULL;
1748 } else {
1749 break;
1750 }
1751 }
1752 } while (h_contact);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001753
1754
1755 /* Or if Contact URI is not present, take the remote URI from
1756 * the From URI.
1757 */
1758 if (uri == NULL)
Benny Prijonoa1e69682007-05-11 15:14:34 +00001759 uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001760
1761
1762 /* Can only do sip/sips scheme at present. */
1763 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
1764 return PJSIP_EINVALIDREQURI;
1765
Benny Prijono8c7a6172007-02-18 21:17:46 +00001766 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001767 }
1768
1769 /* Get transport type of the URI */
1770 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri))
1771 tp_type = PJSIP_TRANSPORT_TLS;
1772 else if (sip_uri->transport_param.slen == 0) {
1773 tp_type = PJSIP_TRANSPORT_UDP;
1774 } else
1775 tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
Benny Prijonod0bd4982007-12-02 15:40:52 +00001776
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001777 if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED)
1778 return PJSIP_EUNSUPTRANSPORT;
1779
Benny Prijonod0bd4982007-12-02 15:40:52 +00001780 /* If destination URI specifies IPv6, then set transport type
1781 * to use IPv6 as well.
1782 */
1783 if (pj_strchr(&sip_uri->host, ':'))
1784 tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6);
1785
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001786 flag = pjsip_transport_get_flag_from_type(tp_type);
1787 secure = (flag & PJSIP_TRANSPORT_SECURE) != 0;
1788
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001789 /* Init transport selector. */
1790 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
1791
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001792 /* Get local address suitable to send request from */
1793 status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001794 pool, tp_type, &tp_sel,
1795 &local_addr, &local_port);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001796 if (status != PJ_SUCCESS)
1797 return status;
1798
Benny Prijonod0bd4982007-12-02 15:40:52 +00001799 /* Enclose IPv6 address in square brackets */
1800 if (tp_type & PJSIP_TRANSPORT_IPV6) {
1801 beginquote = "[";
1802 endquote = "]";
1803 } else {
1804 beginquote = endquote = "";
1805 }
1806
1807 /* Don't add transport parameter if it's UDP */
Benny Prijono4c82c1e2008-10-16 08:14:51 +00001808 if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) {
Benny Prijonod0bd4982007-12-02 15:40:52 +00001809 pj_ansi_snprintf(transport_param, sizeof(transport_param),
1810 ";transport=%s",
1811 pjsip_transport_get_type_name(tp_type));
1812 } else {
1813 transport_param[0] = '\0';
1814 }
1815
1816
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001817 /* Create the contact header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001818 contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001819 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
Benny Prijono30fe4852008-12-10 16:54:16 +00001820 "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>",
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001821 (int)acc->display.slen,
1822 acc->display.ptr,
1823 (acc->display.slen?" " : ""),
Benny Prijono8058a622007-06-08 04:37:05 +00001824 (secure ? PJSUA_SECURE_SCHEME : "sip"),
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001825 (int)acc->user_part.slen,
1826 acc->user_part.ptr,
1827 (acc->user_part.slen?"@":""),
Benny Prijonod0bd4982007-12-02 15:40:52 +00001828 beginquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001829 (int)local_addr.slen,
1830 local_addr.ptr,
Benny Prijonod0bd4982007-12-02 15:40:52 +00001831 endquote,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001832 local_port,
Benny Prijono30fe4852008-12-10 16:54:16 +00001833 transport_param,
1834 (int)acc->cfg.contact_params.slen,
1835 acc->cfg.contact_params.ptr);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001836
1837 return PJ_SUCCESS;
1838}
1839
1840
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001841PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id,
1842 pjsua_transport_id tp_id)
1843{
1844 pjsua_acc *acc;
1845
1846 PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL);
1847 acc = &pjsua_var.acc[acc_id];
1848
Benny Prijonoa1e69682007-05-11 15:14:34 +00001849 PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001850 PJ_EINVAL);
1851
1852 acc->cfg.transport_id = tp_id;
1853
1854 return PJ_SUCCESS;
1855}
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001856