Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjsua-lib/pjsua.h> |
| 21 | #include <pjsua-lib/pjsua_internal.h> |
| 22 | |
| 23 | |
| 24 | #define THIS_FILE "pjsua_acc.c" |
| 25 | |
| 26 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 27 | static void schedule_reregistration(pjsua_acc *acc); |
| 28 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 29 | /* |
| 30 | * Get number of current accounts. |
| 31 | */ |
| 32 | PJ_DEF(unsigned) pjsua_acc_get_count(void) |
| 33 | { |
| 34 | return pjsua_var.acc_cnt; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * Check if the specified account ID is valid. |
| 40 | */ |
| 41 | PJ_DEF(pj_bool_t) pjsua_acc_is_valid(pjsua_acc_id acc_id) |
| 42 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 43 | return acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc) && |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 44 | pjsua_var.acc[acc_id].valid; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 49 | * Set default account |
| 50 | */ |
| 51 | PJ_DEF(pj_status_t) pjsua_acc_set_default(pjsua_acc_id acc_id) |
| 52 | { |
| 53 | pjsua_var.default_acc = acc_id; |
| 54 | return PJ_SUCCESS; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | * Get default account. |
| 60 | */ |
| 61 | PJ_DEF(pjsua_acc_id) pjsua_acc_get_default(void) |
| 62 | { |
| 63 | return pjsua_var.default_acc; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 68 | * Copy account configuration. |
| 69 | */ |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 70 | PJ_DEF(void) pjsua_acc_config_dup( pj_pool_t *pool, |
| 71 | pjsua_acc_config *dst, |
| 72 | const pjsua_acc_config *src) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 73 | { |
| 74 | unsigned i; |
| 75 | |
| 76 | pj_memcpy(dst, src, sizeof(pjsua_acc_config)); |
| 77 | |
| 78 | pj_strdup_with_null(pool, &dst->id, &src->id); |
| 79 | pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri); |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 80 | pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact); |
Benny Prijono | fe04fb5 | 2007-08-24 08:28:52 +0000 | [diff] [blame] | 81 | pj_strdup_with_null(pool, &dst->pidf_tuple_id, &src->pidf_tuple_id); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 82 | |
| 83 | dst->proxy_cnt = src->proxy_cnt; |
| 84 | for (i=0; i<src->proxy_cnt; ++i) |
| 85 | pj_strdup_with_null(pool, &dst->proxy[i], &src->proxy[i]); |
| 86 | |
| 87 | dst->reg_timeout = src->reg_timeout; |
| 88 | dst->cred_count = src->cred_count; |
| 89 | |
| 90 | for (i=0; i<src->cred_count; ++i) { |
| 91 | pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]); |
| 92 | } |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 93 | |
| 94 | dst->ka_interval = src->ka_interval; |
| 95 | pj_strdup(pool, &dst->ka_data, &src->ka_data); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Nanang Izzuddin | c3ea16a | 2010-04-20 14:36:38 +0000 | [diff] [blame] | 98 | /* |
| 99 | * Calculate CRC of proxy list. |
| 100 | */ |
| 101 | static pj_uint32_t calc_proxy_crc(const pj_str_t proxy[], pj_size_t cnt) |
| 102 | { |
| 103 | pj_crc32_context ctx; |
| 104 | unsigned i; |
| 105 | |
| 106 | pj_crc32_init(&ctx); |
| 107 | for (i=0; i<cnt; ++i) { |
| 108 | pj_crc32_update(&ctx, (pj_uint8_t*)proxy[i].ptr, proxy[i].slen); |
| 109 | } |
| 110 | |
| 111 | return pj_crc32_final(&ctx); |
| 112 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 113 | |
| 114 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 115 | * Initialize a new account (after configuration is set). |
| 116 | */ |
| 117 | static pj_status_t initialize_acc(unsigned acc_id) |
| 118 | { |
| 119 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 120 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 121 | pjsip_name_addr *name_addr; |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 122 | pjsip_sip_uri *sip_uri, *sip_reg_uri; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 123 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 124 | unsigned i; |
| 125 | |
| 126 | /* Need to parse local_uri to get the elements: */ |
| 127 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 128 | name_addr = (pjsip_name_addr*) |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 129 | pjsip_parse_uri(acc->pool, acc_cfg->id.ptr, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 130 | acc_cfg->id.slen, |
| 131 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 132 | if (name_addr == NULL) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 133 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 134 | PJSIP_EINVALIDURI); |
| 135 | return PJSIP_EINVALIDURI; |
| 136 | } |
| 137 | |
| 138 | /* Local URI MUST be a SIP or SIPS: */ |
| 139 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 140 | if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) && |
| 141 | !PJSIP_URI_SCHEME_IS_SIPS(name_addr)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 142 | { |
| 143 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 144 | PJSIP_EINVALIDSCHEME); |
| 145 | return PJSIP_EINVALIDSCHEME; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /* Get the SIP URI object: */ |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 150 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 151 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 152 | |
| 153 | /* Parse registrar URI, if any */ |
| 154 | if (acc_cfg->reg_uri.slen) { |
| 155 | pjsip_uri *reg_uri; |
| 156 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 157 | reg_uri = pjsip_parse_uri(acc->pool, acc_cfg->reg_uri.ptr, |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 158 | acc_cfg->reg_uri.slen, 0); |
| 159 | if (reg_uri == NULL) { |
| 160 | pjsua_perror(THIS_FILE, "Invalid registrar URI", |
| 161 | PJSIP_EINVALIDURI); |
| 162 | return PJSIP_EINVALIDURI; |
| 163 | } |
| 164 | |
| 165 | /* Registrar URI MUST be a SIP or SIPS: */ |
| 166 | if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) && |
| 167 | !PJSIP_URI_SCHEME_IS_SIPS(reg_uri)) |
| 168 | { |
| 169 | pjsua_perror(THIS_FILE, "Invalid registar URI", |
| 170 | PJSIP_EINVALIDSCHEME); |
| 171 | return PJSIP_EINVALIDSCHEME; |
| 172 | } |
| 173 | |
| 174 | sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri); |
| 175 | |
| 176 | } else { |
| 177 | sip_reg_uri = NULL; |
| 178 | } |
| 179 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 180 | /* Save the user and domain part. These will be used when finding an |
| 181 | * account for incoming requests. |
| 182 | */ |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 183 | acc->display = name_addr->display; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 184 | acc->user_part = sip_uri->user; |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 185 | acc->srv_domain = sip_uri->host; |
| 186 | acc->srv_port = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 187 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 188 | if (sip_reg_uri) { |
| 189 | acc->srv_port = sip_reg_uri->port; |
| 190 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 191 | |
| 192 | /* Create Contact header if not present. */ |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 193 | //if (acc_cfg->contact.slen == 0) { |
| 194 | // acc_cfg->contact = acc_cfg->id; |
| 195 | //} |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 196 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 197 | /* Build account route-set from outbound proxies and route set from |
| 198 | * account configuration. |
| 199 | */ |
| 200 | pj_list_init(&acc->route_set); |
| 201 | |
Benny Prijono | 29c8ca3 | 2010-06-22 06:02:13 +0000 | [diff] [blame^] | 202 | if (!pj_list_empty(&pjsua_var.outbound_proxy)) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 203 | pjsip_route_hdr *r; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 204 | |
Benny Prijono | 29c8ca3 | 2010-06-22 06:02:13 +0000 | [diff] [blame^] | 205 | r = pjsua_var.outbound_proxy.next; |
| 206 | while (r != &pjsua_var.outbound_proxy) { |
| 207 | pj_list_push_back(&acc->route_set, |
| 208 | pjsip_hdr_shallow_clone(acc->pool, r)); |
| 209 | r = r->next; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 210 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | for (i=0; i<acc_cfg->proxy_cnt; ++i) { |
| 214 | pj_str_t hname = { "Route", 5}; |
| 215 | pjsip_route_hdr *r; |
| 216 | pj_str_t tmp; |
| 217 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 218 | pj_strdup_with_null(acc->pool, &tmp, &acc_cfg->proxy[i]); |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 219 | r = (pjsip_route_hdr*) |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 220 | pjsip_parse_hdr(acc->pool, &hname, tmp.ptr, tmp.slen, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 221 | if (r == NULL) { |
| 222 | pjsua_perror(THIS_FILE, "Invalid URI in account route set", |
| 223 | PJ_EINVAL); |
| 224 | return PJ_EINVAL; |
| 225 | } |
| 226 | pj_list_push_back(&acc->route_set, r); |
| 227 | } |
| 228 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 229 | /* Concatenate credentials from account config and global config */ |
| 230 | acc->cred_cnt = 0; |
| 231 | for (i=0; i<acc_cfg->cred_count; ++i) { |
| 232 | acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i]; |
| 233 | } |
| 234 | for (i=0; i<pjsua_var.ua_cfg.cred_count && |
| 235 | acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i) |
| 236 | { |
| 237 | acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i]; |
| 238 | } |
| 239 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 240 | status = pjsua_pres_init_acc(acc_id); |
| 241 | if (status != PJ_SUCCESS) |
| 242 | return status; |
| 243 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 244 | /* Mark account as valid */ |
| 245 | pjsua_var.acc[acc_id].valid = PJ_TRUE; |
| 246 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 247 | /* Insert account ID into account ID array, sorted by priority */ |
| 248 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 249 | if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority < |
| 250 | pjsua_var.acc[acc_id].cfg.priority) |
| 251 | { |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]), |
| 256 | pjsua_var.acc_cnt, i, &acc_id); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 257 | |
| 258 | return PJ_SUCCESS; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
| 263 | * Add a new account to pjsua. |
| 264 | */ |
| 265 | PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg, |
| 266 | pj_bool_t is_default, |
| 267 | pjsua_acc_id *p_acc_id) |
| 268 | { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 269 | pjsua_acc *acc; |
Benny Prijono | 91d06b6 | 2008-09-20 12:16:56 +0000 | [diff] [blame] | 270 | unsigned i, id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 271 | pj_status_t status; |
| 272 | |
| 273 | PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc), |
| 274 | PJ_ETOOMANY); |
| 275 | |
| 276 | /* Must have a transport */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 277 | PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 278 | |
| 279 | PJSUA_LOCK(); |
| 280 | |
| 281 | /* Find empty account id. */ |
| 282 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) { |
| 283 | if (pjsua_var.acc[id].valid == PJ_FALSE) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | /* Expect to find a slot */ |
| 288 | PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc), |
| 289 | {PJSUA_UNLOCK(); return PJ_EBUG;}); |
| 290 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 291 | acc = &pjsua_var.acc[id]; |
| 292 | |
| 293 | /* Create pool for this account. */ |
| 294 | if (acc->pool) |
| 295 | pj_pool_reset(acc->pool); |
| 296 | else |
| 297 | acc->pool = pjsua_pool_create("acc%p", 512, 256); |
| 298 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 299 | /* Copy config */ |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 300 | pjsua_acc_config_dup(acc->pool, &pjsua_var.acc[id].cfg, cfg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 301 | |
| 302 | /* Normalize registration timeout */ |
| 303 | if (pjsua_var.acc[id].cfg.reg_uri.slen && |
| 304 | pjsua_var.acc[id].cfg.reg_timeout == 0) |
| 305 | { |
| 306 | pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL; |
| 307 | } |
| 308 | |
Nanang Izzuddin | c3ea16a | 2010-04-20 14:36:38 +0000 | [diff] [blame] | 309 | /* Get CRC of account proxy setting */ |
| 310 | acc->local_route_crc = calc_proxy_crc(acc->cfg.proxy, acc->cfg.proxy_cnt); |
| 311 | |
| 312 | /* Get CRC of global outbound proxy setting */ |
| 313 | acc->global_route_crc=calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy, |
| 314 | pjsua_var.ua_cfg.outbound_proxy_cnt); |
| 315 | |
Benny Prijono | 91d06b6 | 2008-09-20 12:16:56 +0000 | [diff] [blame] | 316 | /* Check the route URI's and force loose route if required */ |
| 317 | for (i=0; i<acc->cfg.proxy_cnt; ++i) { |
| 318 | status = normalize_route_uri(acc->pool, &acc->cfg.proxy[i]); |
| 319 | if (status != PJ_SUCCESS) |
| 320 | return status; |
| 321 | } |
| 322 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 323 | status = initialize_acc(id); |
| 324 | if (status != PJ_SUCCESS) { |
| 325 | pjsua_perror(THIS_FILE, "Error adding account", status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 326 | pj_pool_release(acc->pool); |
| 327 | acc->pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 328 | PJSUA_UNLOCK(); |
| 329 | return status; |
| 330 | } |
| 331 | |
| 332 | if (is_default) |
| 333 | pjsua_var.default_acc = id; |
| 334 | |
| 335 | if (p_acc_id) |
| 336 | *p_acc_id = id; |
| 337 | |
| 338 | pjsua_var.acc_cnt++; |
| 339 | |
| 340 | PJSUA_UNLOCK(); |
| 341 | |
| 342 | PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d", |
| 343 | (int)cfg->id.slen, cfg->id.ptr, id)); |
| 344 | |
| 345 | /* If accounts has registration enabled, start registration */ |
| 346 | if (pjsua_var.acc[id].cfg.reg_uri.slen) |
| 347 | pjsua_acc_set_registration(id, PJ_TRUE); |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 348 | else { |
| 349 | /* Otherwise subscribe to MWI, if it's enabled */ |
| 350 | if (pjsua_var.acc[id].cfg.mwi_enabled) |
| 351 | pjsua_start_mwi(&pjsua_var.acc[id]); |
| 352 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 353 | |
| 354 | return PJ_SUCCESS; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /* |
| 359 | * Add local account |
| 360 | */ |
| 361 | PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid, |
| 362 | pj_bool_t is_default, |
| 363 | pjsua_acc_id *p_acc_id) |
| 364 | { |
| 365 | pjsua_acc_config cfg; |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 366 | pjsua_transport_data *t = &pjsua_var.tpdata[tid]; |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 367 | const char *beginquote, *endquote; |
| 368 | char transport_param[32]; |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 369 | char uri[PJSIP_MAX_URL_SIZE]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 370 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 371 | /* ID must be valid */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 372 | PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 373 | PJ_EINVAL); |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 374 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 375 | /* Transport must be valid */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 376 | PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 377 | |
| 378 | pjsua_acc_config_default(&cfg); |
| 379 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 380 | /* Lower the priority of local account */ |
| 381 | --cfg.priority; |
| 382 | |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 383 | /* Enclose IPv6 address in square brackets */ |
| 384 | if (t->type & PJSIP_TRANSPORT_IPV6) { |
| 385 | beginquote = "["; |
| 386 | endquote = "]"; |
| 387 | } else { |
| 388 | beginquote = endquote = ""; |
| 389 | } |
| 390 | |
| 391 | /* Don't add transport parameter if it's UDP */ |
Benny Prijono | 4c82c1e | 2008-10-16 08:14:51 +0000 | [diff] [blame] | 392 | if (t->type!=PJSIP_TRANSPORT_UDP && t->type!=PJSIP_TRANSPORT_UDP6) { |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 393 | pj_ansi_snprintf(transport_param, sizeof(transport_param), |
| 394 | ";transport=%s", |
| 395 | pjsip_transport_get_type_name(t->type)); |
| 396 | } else { |
| 397 | transport_param[0] = '\0'; |
| 398 | } |
| 399 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 400 | /* Build URI for the account */ |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 401 | pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE, |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 402 | "<sip:%s%.*s%s:%d%s>", |
| 403 | beginquote, |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 404 | (int)t->local_name.host.slen, |
| 405 | t->local_name.host.ptr, |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 406 | endquote, |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 407 | t->local_name.port, |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 408 | transport_param); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 409 | |
| 410 | cfg.id = pj_str(uri); |
| 411 | |
| 412 | return pjsua_acc_add(&cfg, is_default, p_acc_id); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /* |
Benny Prijono | 705e784 | 2008-07-21 18:12:51 +0000 | [diff] [blame] | 417 | * Set arbitrary data to be associated with the account. |
| 418 | */ |
| 419 | PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id, |
| 420 | void *user_data) |
| 421 | { |
| 422 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 423 | PJ_EINVAL); |
| 424 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 425 | |
| 426 | PJSUA_LOCK(); |
| 427 | |
| 428 | pjsua_var.acc[acc_id].cfg.user_data = user_data; |
| 429 | |
| 430 | PJSUA_UNLOCK(); |
| 431 | |
| 432 | return PJ_SUCCESS; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | /* |
| 437 | * Retrieve arbitrary data associated with the account. |
| 438 | */ |
| 439 | PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id) |
| 440 | { |
| 441 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 442 | NULL); |
| 443 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL); |
| 444 | |
| 445 | return pjsua_var.acc[acc_id].cfg.user_data; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 450 | * Delete account. |
| 451 | */ |
| 452 | PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id) |
| 453 | { |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 454 | unsigned i; |
| 455 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 456 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 457 | PJ_EINVAL); |
| 458 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 459 | |
| 460 | PJSUA_LOCK(); |
| 461 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 462 | /* Cancel any re-registration timer */ |
| 463 | pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer); |
| 464 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 465 | /* Delete registration */ |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 466 | if (pjsua_var.acc[acc_id].regc != NULL) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 467 | pjsua_acc_set_registration(acc_id, PJ_FALSE); |
Benny Prijono | e68e99f | 2007-06-06 00:28:10 +0000 | [diff] [blame] | 468 | if (pjsua_var.acc[acc_id].regc) { |
| 469 | pjsip_regc_destroy(pjsua_var.acc[acc_id].regc); |
| 470 | } |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 471 | pjsua_var.acc[acc_id].regc = NULL; |
| 472 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 473 | |
| 474 | /* Delete server presence subscription */ |
| 475 | pjsua_pres_delete_acc(acc_id); |
| 476 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 477 | /* Release account pool */ |
| 478 | if (pjsua_var.acc[acc_id].pool) { |
| 479 | pj_pool_release(pjsua_var.acc[acc_id].pool); |
| 480 | pjsua_var.acc[acc_id].pool = NULL; |
| 481 | } |
| 482 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 483 | /* Invalidate */ |
| 484 | pjsua_var.acc[acc_id].valid = PJ_FALSE; |
Benny Prijono | 978de6e | 2008-09-15 14:56:05 +0000 | [diff] [blame] | 485 | pjsua_var.acc[acc_id].contact.slen = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 486 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 487 | /* Remove from array */ |
| 488 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 489 | if (pjsua_var.acc_ids[i] == acc_id) |
| 490 | break; |
| 491 | } |
| 492 | if (i != pjsua_var.acc_cnt) { |
| 493 | pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]), |
| 494 | pjsua_var.acc_cnt, i); |
| 495 | --pjsua_var.acc_cnt; |
| 496 | } |
| 497 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 498 | /* Leave the calls intact, as I don't think calls need to |
| 499 | * access account once it's created |
| 500 | */ |
| 501 | |
Benny Prijono | fb2b365 | 2007-06-28 07:15:03 +0000 | [diff] [blame] | 502 | /* Update default account */ |
| 503 | if (pjsua_var.default_acc == acc_id) |
| 504 | pjsua_var.default_acc = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 505 | |
| 506 | PJSUA_UNLOCK(); |
| 507 | |
| 508 | PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id)); |
| 509 | |
| 510 | return PJ_SUCCESS; |
| 511 | } |
| 512 | |
| 513 | |
| 514 | /* |
| 515 | * Modify account information. |
| 516 | */ |
| 517 | PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id, |
| 518 | const pjsua_acc_config *cfg) |
| 519 | { |
Nanang Izzuddin | c3ea16a | 2010-04-20 14:36:38 +0000 | [diff] [blame] | 520 | pjsua_acc *acc; |
| 521 | pjsip_name_addr *id_name_addr = NULL; |
| 522 | pjsip_sip_uri *id_sip_uri = NULL; |
| 523 | pjsip_sip_uri *reg_sip_uri = NULL; |
| 524 | pj_uint32_t local_route_crc, global_route_crc; |
| 525 | pjsip_route_hdr global_route; |
| 526 | pjsip_route_hdr local_route; |
| 527 | pj_str_t acc_proxy[PJSUA_ACC_MAX_PROXIES]; |
| 528 | pj_bool_t update_reg = PJ_FALSE; |
| 529 | pj_status_t status = PJ_SUCCESS; |
| 530 | |
| 531 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 532 | PJ_EINVAL); |
| 533 | |
| 534 | PJSUA_LOCK(); |
| 535 | |
| 536 | acc = &pjsua_var.acc[acc_id]; |
| 537 | if (!acc->valid) { |
| 538 | status = PJ_EINVAL; |
| 539 | goto on_return; |
| 540 | } |
| 541 | |
| 542 | /* == Validate first == */ |
| 543 | |
| 544 | /* Account id */ |
| 545 | if (pj_strcmp(&acc->cfg.id, &cfg->id)) { |
| 546 | /* Need to parse id to get the elements: */ |
| 547 | id_name_addr = (pjsip_name_addr*) |
| 548 | pjsip_parse_uri(acc->pool, cfg->id.ptr, cfg->id.slen, |
| 549 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 550 | if (id_name_addr == NULL) { |
| 551 | status = PJSIP_EINVALIDURI; |
| 552 | pjsua_perror(THIS_FILE, "Invalid local URI", status); |
| 553 | goto on_return; |
| 554 | } |
| 555 | |
| 556 | /* URI MUST be a SIP or SIPS: */ |
| 557 | if (!PJSIP_URI_SCHEME_IS_SIP(id_name_addr) && |
| 558 | !PJSIP_URI_SCHEME_IS_SIPS(id_name_addr)) |
| 559 | { |
| 560 | status = PJSIP_EINVALIDSCHEME; |
| 561 | pjsua_perror(THIS_FILE, "Invalid local URI", status); |
| 562 | goto on_return; |
| 563 | } |
| 564 | |
| 565 | /* Get the SIP URI object: */ |
| 566 | id_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(id_name_addr); |
| 567 | } |
| 568 | |
| 569 | /* Registrar URI */ |
| 570 | if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri) && cfg->reg_uri.slen) { |
| 571 | pjsip_uri *reg_uri; |
| 572 | |
| 573 | /* Need to parse reg_uri to get the elements: */ |
| 574 | reg_uri = pjsip_parse_uri(acc->pool, cfg->reg_uri.ptr, |
| 575 | cfg->reg_uri.slen, 0); |
| 576 | if (reg_uri == NULL) { |
| 577 | status = PJSIP_EINVALIDURI; |
| 578 | pjsua_perror(THIS_FILE, "Invalid registrar URI", status); |
| 579 | goto on_return; |
| 580 | } |
| 581 | |
| 582 | /* Registrar URI MUST be a SIP or SIPS: */ |
| 583 | if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) && |
| 584 | !PJSIP_URI_SCHEME_IS_SIPS(reg_uri)) |
| 585 | { |
| 586 | status = PJSIP_EINVALIDSCHEME; |
| 587 | pjsua_perror(THIS_FILE, "Invalid registar URI", status); |
| 588 | goto on_return; |
| 589 | } |
| 590 | |
| 591 | reg_sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri); |
| 592 | } |
| 593 | |
| 594 | /* Global outbound proxy */ |
| 595 | global_route_crc = calc_proxy_crc(pjsua_var.ua_cfg.outbound_proxy, |
| 596 | pjsua_var.ua_cfg.outbound_proxy_cnt); |
| 597 | if (global_route_crc != acc->global_route_crc) { |
| 598 | pjsip_route_hdr *r; |
Nanang Izzuddin | c3ea16a | 2010-04-20 14:36:38 +0000 | [diff] [blame] | 599 | |
Benny Prijono | 29c8ca3 | 2010-06-22 06:02:13 +0000 | [diff] [blame^] | 600 | /* Copy from global outbound proxies */ |
Nanang Izzuddin | c3ea16a | 2010-04-20 14:36:38 +0000 | [diff] [blame] | 601 | pj_list_init(&global_route); |
Benny Prijono | 29c8ca3 | 2010-06-22 06:02:13 +0000 | [diff] [blame^] | 602 | r = pjsua_var.outbound_proxy.next; |
| 603 | while (r != &pjsua_var.outbound_proxy) { |
| 604 | pj_list_push_back(&global_route, |
| 605 | pjsip_hdr_shallow_clone(acc->pool, r)); |
| 606 | r = r->next; |
Nanang Izzuddin | c3ea16a | 2010-04-20 14:36:38 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | /* Account proxy */ |
| 611 | local_route_crc = calc_proxy_crc(cfg->proxy, cfg->proxy_cnt); |
| 612 | if (local_route_crc != acc->local_route_crc) { |
| 613 | pjsip_route_hdr *r; |
| 614 | unsigned i; |
| 615 | |
| 616 | /* Validate the local route and save it to temporary var */ |
| 617 | pj_list_init(&local_route); |
| 618 | for (i=0; i<cfg->proxy_cnt; ++i) { |
| 619 | pj_str_t hname = { "Route", 5}; |
| 620 | |
| 621 | pj_strdup_with_null(acc->pool, &acc_proxy[i], &cfg->proxy[i]); |
| 622 | status = normalize_route_uri(acc->pool, &acc_proxy[i]); |
| 623 | if (status != PJ_SUCCESS) |
| 624 | goto on_return; |
| 625 | r = (pjsip_route_hdr*) |
| 626 | pjsip_parse_hdr(acc->pool, &hname, acc_proxy[i].ptr, |
| 627 | acc_proxy[i].slen, NULL); |
| 628 | if (r == NULL) { |
| 629 | status = PJSIP_EINVALIDURI; |
| 630 | pjsua_perror(THIS_FILE, "Invalid URI in account route set", |
| 631 | status); |
| 632 | goto on_return; |
| 633 | } |
| 634 | |
| 635 | pj_list_push_back(&local_route, r); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | |
| 640 | /* == Apply the new config == */ |
| 641 | |
| 642 | /* Account ID. */ |
| 643 | if (id_name_addr && id_sip_uri) { |
| 644 | pj_strdup_with_null(acc->pool, &acc->cfg.id, &cfg->id); |
| 645 | acc->display = id_name_addr->display; |
| 646 | acc->user_part = id_sip_uri->user; |
| 647 | acc->srv_domain = id_sip_uri->host; |
| 648 | acc->srv_port = 0; |
| 649 | update_reg = PJ_TRUE; |
| 650 | } |
| 651 | |
| 652 | /* User data */ |
| 653 | acc->cfg.user_data = cfg->user_data; |
| 654 | |
| 655 | /* Priority */ |
| 656 | if (acc->cfg.priority != cfg->priority) { |
| 657 | unsigned i; |
| 658 | |
| 659 | acc->cfg.priority = cfg->priority; |
| 660 | |
| 661 | /* Resort accounts priority */ |
| 662 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 663 | if (pjsua_var.acc_ids[i] == acc_id) |
| 664 | break; |
| 665 | } |
| 666 | pj_assert(i < pjsua_var.acc_cnt); |
| 667 | pj_array_erase(pjsua_var.acc_ids, sizeof(acc_id), |
| 668 | pjsua_var.acc_cnt, i); |
| 669 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 670 | if (pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority < |
| 671 | acc->cfg.priority) |
| 672 | { |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | pj_array_insert(pjsua_var.acc_ids, sizeof(acc_id), |
| 677 | pjsua_var.acc_cnt, i, &acc_id); |
| 678 | } |
| 679 | |
| 680 | /* MWI */ |
| 681 | acc->cfg.mwi_enabled = cfg->mwi_enabled; |
| 682 | |
| 683 | /* PIDF tuple ID */ |
| 684 | if (pj_strcmp(&acc->cfg.pidf_tuple_id, &cfg->pidf_tuple_id)) |
| 685 | pj_strdup_with_null(acc->pool, &acc->cfg.pidf_tuple_id, |
| 686 | &cfg->pidf_tuple_id); |
| 687 | |
| 688 | /* Publish */ |
| 689 | acc->cfg.publish_opt = cfg->publish_opt; |
| 690 | acc->cfg.unpublish_max_wait_time_msec = cfg->unpublish_max_wait_time_msec; |
| 691 | if (acc->cfg.publish_enabled != cfg->publish_enabled) { |
| 692 | acc->cfg.publish_enabled = cfg->publish_enabled; |
| 693 | if (!acc->cfg.publish_enabled) |
| 694 | pjsua_pres_unpublish(acc); |
| 695 | else |
| 696 | update_reg = PJ_TRUE; |
| 697 | } |
| 698 | |
| 699 | /* Force contact URI */ |
| 700 | if (pj_strcmp(&acc->cfg.force_contact, &cfg->force_contact)) { |
| 701 | pj_strdup_with_null(acc->pool, &acc->cfg.force_contact, |
| 702 | &cfg->force_contact); |
| 703 | update_reg = PJ_TRUE; |
| 704 | } |
| 705 | |
| 706 | /* Contact param */ |
| 707 | if (pj_strcmp(&acc->cfg.contact_params, &cfg->contact_params)) { |
| 708 | pj_strdup_with_null(acc->pool, &acc->cfg.contact_params, |
| 709 | &cfg->contact_params); |
| 710 | update_reg = PJ_TRUE; |
| 711 | } |
| 712 | |
| 713 | /* Contact URI params */ |
| 714 | if (pj_strcmp(&acc->cfg.contact_uri_params, &cfg->contact_uri_params)) { |
| 715 | pj_strdup_with_null(acc->pool, &acc->cfg.contact_uri_params, |
| 716 | &cfg->contact_uri_params); |
| 717 | update_reg = PJ_TRUE; |
| 718 | } |
| 719 | |
| 720 | /* Reliable provisional response */ |
| 721 | acc->cfg.require_100rel = cfg->require_100rel; |
| 722 | |
| 723 | /* Session timer */ |
| 724 | acc->cfg.require_timer = cfg->require_timer; |
| 725 | acc->cfg.timer_setting = cfg->timer_setting; |
| 726 | |
| 727 | /* Transport and keep-alive */ |
| 728 | if (acc->cfg.transport_id != cfg->transport_id) { |
| 729 | acc->cfg.transport_id = cfg->transport_id; |
| 730 | update_reg = PJ_TRUE; |
| 731 | } |
| 732 | acc->cfg.ka_interval = cfg->ka_interval; |
| 733 | if (pj_strcmp(&acc->cfg.ka_data, &cfg->ka_data)) |
| 734 | pj_strdup(acc->pool, &acc->cfg.ka_data, &cfg->ka_data); |
| 735 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
| 736 | acc->cfg.use_srtp = cfg->use_srtp; |
| 737 | acc->cfg.srtp_secure_signaling = cfg->srtp_secure_signaling; |
Nanang Izzuddin | d89cc3a | 2010-05-13 05:22:51 +0000 | [diff] [blame] | 738 | acc->cfg.srtp_optional_dup_offer = cfg->srtp_optional_dup_offer; |
Nanang Izzuddin | c3ea16a | 2010-04-20 14:36:38 +0000 | [diff] [blame] | 739 | #endif |
| 740 | |
| 741 | /* Global outbound proxy */ |
| 742 | if (global_route_crc != acc->global_route_crc) { |
| 743 | unsigned i, rcnt; |
| 744 | |
| 745 | /* Remove the outbound proxies from the route set */ |
| 746 | rcnt = pj_list_size(&acc->route_set); |
| 747 | for (i=0; i < rcnt - acc->cfg.proxy_cnt; ++i) { |
| 748 | pjsip_route_hdr *r = acc->route_set.next; |
| 749 | pj_list_erase(r); |
| 750 | } |
| 751 | |
| 752 | /* Insert the outbound proxies to the beginning of route set */ |
| 753 | pj_list_merge_first(&acc->route_set, &global_route); |
| 754 | |
| 755 | /* Update global route CRC */ |
| 756 | acc->global_route_crc = global_route_crc; |
| 757 | |
| 758 | update_reg = PJ_TRUE; |
| 759 | } |
| 760 | |
| 761 | /* Account proxy */ |
| 762 | if (local_route_crc != acc->local_route_crc) { |
| 763 | unsigned i; |
| 764 | |
| 765 | /* Remove the current account proxies from the route set */ |
| 766 | for (i=0; i < acc->cfg.proxy_cnt; ++i) { |
| 767 | pjsip_route_hdr *r = acc->route_set.prev; |
| 768 | pj_list_erase(r); |
| 769 | } |
| 770 | |
| 771 | /* Insert new proxy setting to the route set */ |
| 772 | pj_list_merge_last(&acc->route_set, &local_route); |
| 773 | |
| 774 | /* Update the proxy setting */ |
| 775 | acc->cfg.proxy_cnt = cfg->proxy_cnt; |
| 776 | for (i = 0; i < cfg->proxy_cnt; ++i) |
| 777 | acc->cfg.proxy[i] = acc_proxy[i]; |
| 778 | |
| 779 | /* Update local route CRC */ |
| 780 | acc->local_route_crc = local_route_crc; |
| 781 | |
| 782 | update_reg = PJ_TRUE; |
| 783 | } |
| 784 | |
| 785 | /* Credential info */ |
| 786 | { |
| 787 | unsigned i; |
| 788 | |
| 789 | /* Selective update credential info. */ |
| 790 | for (i = 0; i < cfg->cred_count; ++i) { |
| 791 | unsigned j; |
| 792 | pjsip_cred_info ci; |
| 793 | |
| 794 | /* Find if this credential is already listed */ |
| 795 | for (j = i; j < acc->cfg.cred_count; ++i) { |
| 796 | if (pjsip_cred_info_cmp(&acc->cfg.cred_info[j], |
| 797 | &cfg->cred_info[i]) == 0) |
| 798 | { |
| 799 | /* Found, but different index/position, swap */ |
| 800 | if (j != i) { |
| 801 | ci = acc->cfg.cred_info[i]; |
| 802 | acc->cfg.cred_info[i] = acc->cfg.cred_info[j]; |
| 803 | acc->cfg.cred_info[j] = ci; |
| 804 | } |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | /* Not found, insert this */ |
| 810 | if (j == acc->cfg.cred_count) { |
| 811 | /* If account credential is full, discard the last one. */ |
| 812 | if (acc->cfg.cred_count == PJ_ARRAY_SIZE(acc->cfg.cred_info)) { |
| 813 | pj_array_erase(acc->cfg.cred_info, sizeof(pjsip_cred_info), |
| 814 | acc->cfg.cred_count, acc->cfg.cred_count-1); |
| 815 | acc->cfg.cred_count--; |
| 816 | } |
| 817 | |
| 818 | /* Insert this */ |
| 819 | pjsip_cred_info_dup(acc->pool, &ci, &cfg->cred_info[i]); |
| 820 | pj_array_insert(acc->cfg.cred_info, sizeof(pjsip_cred_info), |
| 821 | acc->cfg.cred_count, i, &ci); |
| 822 | } |
| 823 | } |
| 824 | acc->cfg.cred_count = cfg->cred_count; |
| 825 | |
| 826 | /* Concatenate credentials from account config and global config */ |
| 827 | acc->cred_cnt = 0; |
| 828 | for (i=0; i<acc->cfg.cred_count; ++i) { |
| 829 | acc->cred[acc->cred_cnt++] = acc->cfg.cred_info[i]; |
| 830 | } |
| 831 | for (i=0; i<pjsua_var.ua_cfg.cred_count && |
| 832 | acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i) |
| 833 | { |
| 834 | acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i]; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* Authentication preference */ |
| 839 | acc->cfg.auth_pref.initial_auth = cfg->auth_pref.initial_auth; |
| 840 | if (pj_strcmp(&acc->cfg.auth_pref.algorithm, &cfg->auth_pref.algorithm)) |
| 841 | pj_strdup_with_null(acc->pool, &acc->cfg.auth_pref.algorithm, |
| 842 | &cfg->auth_pref.algorithm); |
| 843 | |
| 844 | /* Registration */ |
| 845 | acc->cfg.reg_timeout = cfg->reg_timeout; |
| 846 | acc->cfg.unreg_timeout = cfg->unreg_timeout; |
| 847 | acc->cfg.allow_contact_rewrite = cfg->allow_contact_rewrite; |
| 848 | acc->cfg.reg_retry_interval = cfg->reg_retry_interval; |
| 849 | acc->cfg.drop_calls_on_reg_fail = cfg->drop_calls_on_reg_fail; |
| 850 | |
| 851 | /* Normalize registration timeout */ |
| 852 | if (acc->cfg.reg_uri.slen && acc->cfg.reg_timeout == 0) |
| 853 | acc->cfg.reg_timeout = PJSUA_REG_INTERVAL; |
| 854 | |
| 855 | /* Registrar URI */ |
| 856 | if (pj_strcmp(&acc->cfg.reg_uri, &cfg->reg_uri)) { |
| 857 | if (cfg->reg_uri.slen) { |
| 858 | pj_strdup_with_null(acc->pool, &acc->cfg.reg_uri, &cfg->reg_uri); |
| 859 | if (reg_sip_uri) |
| 860 | acc->srv_port = reg_sip_uri->port; |
| 861 | } else { |
| 862 | /* Unregister if registration was set */ |
| 863 | if (acc->cfg.reg_uri.slen) |
| 864 | pjsua_acc_set_registration(acc->index, PJ_FALSE); |
| 865 | pj_bzero(&acc->cfg.reg_uri, sizeof(acc->cfg.reg_uri)); |
| 866 | } |
| 867 | update_reg = PJ_TRUE; |
| 868 | } |
| 869 | |
| 870 | /* Update registration */ |
| 871 | if (update_reg) { |
| 872 | /* If accounts has registration enabled, start registration */ |
| 873 | if (acc->cfg.reg_uri.slen) |
| 874 | pjsua_acc_set_registration(acc->index, PJ_TRUE); |
| 875 | else { |
| 876 | /* Otherwise subscribe to MWI, if it's enabled */ |
| 877 | if (acc->cfg.mwi_enabled) |
| 878 | pjsua_start_mwi(acc); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | on_return: |
| 883 | PJSUA_UNLOCK(); |
| 884 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | |
| 888 | /* |
| 889 | * Modify account's presence status to be advertised to remote/presence |
| 890 | * subscribers. |
| 891 | */ |
| 892 | PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id, |
| 893 | pj_bool_t is_online) |
| 894 | { |
| 895 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 896 | PJ_EINVAL); |
| 897 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 898 | |
| 899 | pjsua_var.acc[acc_id].online_status = is_online; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 900 | pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element)); |
| 901 | pjsua_pres_update_acc(acc_id, PJ_FALSE); |
| 902 | return PJ_SUCCESS; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | /* |
| 907 | * Set online status with extended information |
| 908 | */ |
| 909 | PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id, |
| 910 | pj_bool_t is_online, |
| 911 | const pjrpid_element *pr) |
| 912 | { |
| 913 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 914 | PJ_EINVAL); |
| 915 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 916 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 917 | PJSUA_LOCK(); |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 918 | pjsua_var.acc[acc_id].online_status = is_online; |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 919 | pjrpid_element_dup(pjsua_var.acc[acc_id].pool, &pjsua_var.acc[acc_id].rpid, pr); |
| 920 | PJSUA_UNLOCK(); |
| 921 | |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 922 | pjsua_pres_update_acc(acc_id, PJ_TRUE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 923 | return PJ_SUCCESS; |
| 924 | } |
| 925 | |
Benny Prijono | 7f63043 | 2008-09-24 16:52:41 +0000 | [diff] [blame] | 926 | /* Check if IP is private IP address */ |
| 927 | static pj_bool_t is_private_ip(const pj_str_t *addr) |
| 928 | { |
| 929 | const pj_str_t private_net[] = |
| 930 | { |
| 931 | { "10.", 3 }, |
| 932 | { "127.", 4 }, |
| 933 | { "172.16.", 7 }, |
| 934 | { "192.168.", 8 } |
| 935 | }; |
| 936 | unsigned i; |
| 937 | |
| 938 | for (i=0; i<PJ_ARRAY_SIZE(private_net); ++i) { |
| 939 | if (pj_strncmp(addr, &private_net[i], private_net[i].slen)==0) |
| 940 | return PJ_TRUE; |
| 941 | } |
| 942 | |
| 943 | return PJ_FALSE; |
| 944 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 945 | |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 946 | /* Update NAT address from the REGISTER response */ |
| 947 | static pj_bool_t acc_check_nat_addr(pjsua_acc *acc, |
| 948 | struct pjsip_regc_cbparam *param) |
| 949 | { |
| 950 | pjsip_transport *tp; |
| 951 | const pj_str_t *via_addr; |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 952 | pj_pool_t *pool; |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 953 | int rport; |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 954 | pjsip_sip_uri *uri; |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 955 | pjsip_via_hdr *via; |
Benny Prijono | 84d2493 | 2009-06-04 15:51:39 +0000 | [diff] [blame] | 956 | pj_sockaddr contact_addr; |
| 957 | pj_sockaddr recv_addr; |
| 958 | pj_status_t status; |
| 959 | pj_bool_t matched; |
Benny Prijono | 7f63043 | 2008-09-24 16:52:41 +0000 | [diff] [blame] | 960 | pj_str_t srv_ip; |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 961 | pjsip_contact_hdr *contact_hdr; |
| 962 | const pj_str_t STR_CONTACT = { "Contact", 7 }; |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 963 | |
| 964 | tp = param->rdata->tp_info.transport; |
| 965 | |
| 966 | /* Only update if account is configured to auto-update */ |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 967 | if (acc->cfg.allow_contact_rewrite == PJ_FALSE) |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 968 | return PJ_FALSE; |
| 969 | |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 970 | #if 0 |
| 971 | // Always update |
| 972 | // See http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-March/002178.html |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 973 | |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 974 | /* For UDP, only update if STUN is enabled (for now). |
| 975 | * For TCP/TLS, always check. |
| 976 | */ |
| 977 | if ((tp->key.type == PJSIP_TRANSPORT_UDP && |
| 978 | (pjsua_var.ua_cfg.stun_domain.slen != 0 || |
| 979 | (pjsua_var.ua_cfg.stun_host.slen != 0)) || |
| 980 | (tp->key.type == PJSIP_TRANSPORT_TCP) || |
| 981 | (tp->key.type == PJSIP_TRANSPORT_TLS)) |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 982 | { |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 983 | /* Yes we will check */ |
| 984 | } else { |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 985 | return PJ_FALSE; |
| 986 | } |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 987 | #endif |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 988 | |
| 989 | /* Get the received and rport info */ |
| 990 | via = param->rdata->msg_info.via; |
| 991 | if (via->rport_param < 1) { |
| 992 | /* Remote doesn't support rport */ |
| 993 | rport = via->sent_by.port; |
Benny Prijono | 24a2185 | 2008-04-14 04:04:30 +0000 | [diff] [blame] | 994 | if (rport==0) { |
| 995 | pjsip_transport_type_e tp_type; |
| 996 | tp_type = (pjsip_transport_type_e) tp->key.type; |
| 997 | rport = pjsip_transport_get_default_port_for_type(tp_type); |
| 998 | } |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 999 | } else |
| 1000 | rport = via->rport_param; |
| 1001 | |
| 1002 | if (via->recvd_param.slen != 0) |
| 1003 | via_addr = &via->recvd_param; |
| 1004 | else |
| 1005 | via_addr = &via->sent_by.host; |
| 1006 | |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1007 | /* Compare received and rport with the URI in our registration */ |
| 1008 | pool = pjsua_pool_create("tmp", 512, 512); |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 1009 | contact_hdr = (pjsip_contact_hdr*) |
| 1010 | pjsip_parse_hdr(pool, &STR_CONTACT, acc->contact.ptr, |
| 1011 | acc->contact.slen, NULL); |
| 1012 | pj_assert(contact_hdr != NULL); |
| 1013 | uri = (pjsip_sip_uri*) contact_hdr->uri; |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1014 | pj_assert(uri != NULL); |
Benny Prijono | 24a2185 | 2008-04-14 04:04:30 +0000 | [diff] [blame] | 1015 | uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri); |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1016 | |
Benny Prijono | 24a2185 | 2008-04-14 04:04:30 +0000 | [diff] [blame] | 1017 | if (uri->port == 0) { |
| 1018 | pjsip_transport_type_e tp_type; |
| 1019 | tp_type = (pjsip_transport_type_e) tp->key.type; |
| 1020 | uri->port = pjsip_transport_get_default_port_for_type(tp_type); |
| 1021 | } |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1022 | |
Benny Prijono | 84d2493 | 2009-06-04 15:51:39 +0000 | [diff] [blame] | 1023 | /* Convert IP address strings into sockaddr for comparison. |
| 1024 | * (http://trac.pjsip.org/repos/ticket/863) |
| 1025 | */ |
| 1026 | status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host, |
| 1027 | &contact_addr); |
| 1028 | if (status == PJ_SUCCESS) |
| 1029 | status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr, |
| 1030 | &recv_addr); |
| 1031 | if (status == PJ_SUCCESS) { |
| 1032 | /* Compare the addresses as sockaddr according to the ticket above */ |
| 1033 | matched = (uri->port == rport && |
| 1034 | pj_sockaddr_cmp(&contact_addr, &recv_addr)==0); |
| 1035 | } else { |
| 1036 | /* Compare the addresses as string, as before */ |
| 1037 | matched = (uri->port == rport && |
| 1038 | pj_stricmp(&uri->host, via_addr)==0); |
| 1039 | } |
| 1040 | |
| 1041 | if (matched) { |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1042 | /* Address doesn't change */ |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1043 | pj_pool_release(pool); |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1044 | return PJ_FALSE; |
| 1045 | } |
| 1046 | |
Benny Prijono | 7f63043 | 2008-09-24 16:52:41 +0000 | [diff] [blame] | 1047 | /* Get server IP */ |
| 1048 | srv_ip = pj_str(param->rdata->pkt_info.src_name); |
| 1049 | |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1050 | /* At this point we've detected that the address as seen by registrar. |
| 1051 | * has changed. |
| 1052 | */ |
Benny Prijono | 7f63043 | 2008-09-24 16:52:41 +0000 | [diff] [blame] | 1053 | |
| 1054 | /* Do not switch if both Contact and server's IP address are |
| 1055 | * public but response contains private IP. A NAT in the middle |
Benny Prijono | 44e42e1 | 2009-06-03 08:37:24 +0000 | [diff] [blame] | 1056 | * might have messed up with the SIP packets. See: |
| 1057 | * http://trac.pjsip.org/repos/ticket/643 |
Benny Prijono | 247921b | 2008-09-26 22:06:11 +0000 | [diff] [blame] | 1058 | * |
| 1059 | * This exception can be disabled by setting allow_contact_rewrite |
| 1060 | * to 2. In this case, the switch will always be done whenever there |
| 1061 | * is difference in the IP address in the response. |
Benny Prijono | 7f63043 | 2008-09-24 16:52:41 +0000 | [diff] [blame] | 1062 | */ |
Benny Prijono | 247921b | 2008-09-26 22:06:11 +0000 | [diff] [blame] | 1063 | if (acc->cfg.allow_contact_rewrite != 2 && !is_private_ip(&uri->host) && |
| 1064 | !is_private_ip(&srv_ip) && is_private_ip(via_addr)) |
Benny Prijono | 7f63043 | 2008-09-24 16:52:41 +0000 | [diff] [blame] | 1065 | { |
| 1066 | /* Don't switch */ |
| 1067 | pj_pool_release(pool); |
| 1068 | return PJ_FALSE; |
| 1069 | } |
| 1070 | |
Benny Prijono | 4f93376 | 2009-11-10 03:45:42 +0000 | [diff] [blame] | 1071 | /* Also don't switch if only the port number part is different, and |
| 1072 | * the Via received address is private. |
| 1073 | * See http://trac.pjsip.org/repos/ticket/864 |
| 1074 | */ |
| 1075 | if (acc->cfg.allow_contact_rewrite != 2 && |
| 1076 | pj_sockaddr_cmp(&contact_addr, &recv_addr)==0 && |
| 1077 | is_private_ip(via_addr)) |
| 1078 | { |
| 1079 | /* Don't switch */ |
| 1080 | pj_pool_release(pool); |
| 1081 | return PJ_FALSE; |
| 1082 | } |
| 1083 | |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1084 | PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d " |
Benny Prijono | c6d5fdc | 2010-06-20 08:58:26 +0000 | [diff] [blame] | 1085 | "(%.*s:%d --> %.*s:%d). Updating registration " |
| 1086 | "(using method %d)", |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1087 | acc->index, |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1088 | (int)uri->host.slen, |
| 1089 | uri->host.ptr, |
| 1090 | uri->port, |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1091 | (int)via_addr->slen, |
| 1092 | via_addr->ptr, |
Benny Prijono | c6d5fdc | 2010-06-20 08:58:26 +0000 | [diff] [blame] | 1093 | rport, |
| 1094 | acc->cfg.contact_rewrite_method)); |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1095 | |
Benny Prijono | c6d5fdc | 2010-06-20 08:58:26 +0000 | [diff] [blame] | 1096 | pj_assert(acc->cfg.contact_rewrite_method == 1 || |
| 1097 | acc->cfg.contact_rewrite_method == 2); |
| 1098 | |
| 1099 | if (acc->cfg.contact_rewrite_method == 1) { |
| 1100 | /* Unregister current contact */ |
| 1101 | pjsua_acc_set_registration(acc->index, PJ_FALSE); |
| 1102 | if (acc->regc != NULL) { |
| 1103 | pjsip_regc_destroy(acc->regc); |
| 1104 | acc->regc = NULL; |
| 1105 | acc->contact.slen = 0; |
| 1106 | } |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Benny Prijono | c6d5fdc | 2010-06-20 08:58:26 +0000 | [diff] [blame] | 1109 | /* |
| 1110 | * Build new Contact header |
| 1111 | */ |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1112 | { |
| 1113 | char *tmp; |
Benny Prijono | 8972bf0 | 2009-04-05 18:30:45 +0000 | [diff] [blame] | 1114 | const char *beginquote, *endquote; |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1115 | int len; |
| 1116 | |
Benny Prijono | 8972bf0 | 2009-04-05 18:30:45 +0000 | [diff] [blame] | 1117 | /* Enclose IPv6 address in square brackets */ |
| 1118 | if (tp->key.type & PJSIP_TRANSPORT_IPV6) { |
| 1119 | beginquote = "["; |
| 1120 | endquote = "]"; |
| 1121 | } else { |
| 1122 | beginquote = endquote = ""; |
| 1123 | } |
| 1124 | |
Benny Prijono | 24a2185 | 2008-04-14 04:04:30 +0000 | [diff] [blame] | 1125 | tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1126 | len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE, |
Nanang Izzuddin | e2c7e85 | 2009-08-04 14:36:17 +0000 | [diff] [blame] | 1127 | "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s>%.*s", |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1128 | (int)acc->user_part.slen, |
| 1129 | acc->user_part.ptr, |
Benny Prijono | 83088f3 | 2008-04-22 18:33:55 +0000 | [diff] [blame] | 1130 | (acc->user_part.slen? "@" : ""), |
Benny Prijono | 8972bf0 | 2009-04-05 18:30:45 +0000 | [diff] [blame] | 1131 | beginquote, |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1132 | (int)via_addr->slen, |
| 1133 | via_addr->ptr, |
Benny Prijono | 8972bf0 | 2009-04-05 18:30:45 +0000 | [diff] [blame] | 1134 | endquote, |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1135 | rport, |
Benny Prijono | 30fe485 | 2008-12-10 16:54:16 +0000 | [diff] [blame] | 1136 | tp->type_name, |
Nanang Izzuddin | e2c7e85 | 2009-08-04 14:36:17 +0000 | [diff] [blame] | 1137 | (int)acc->cfg.contact_uri_params.slen, |
| 1138 | acc->cfg.contact_uri_params.ptr, |
Benny Prijono | 30fe485 | 2008-12-10 16:54:16 +0000 | [diff] [blame] | 1139 | (int)acc->cfg.contact_params.slen, |
| 1140 | acc->cfg.contact_params.ptr); |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1141 | if (len < 1) { |
| 1142 | PJ_LOG(1,(THIS_FILE, "URI too long")); |
| 1143 | pj_pool_release(pool); |
| 1144 | return PJ_FALSE; |
| 1145 | } |
Benny Prijono | 3d9b4b6 | 2008-07-14 17:55:40 +0000 | [diff] [blame] | 1146 | pj_strdup2_with_null(acc->pool, &acc->contact, tmp); |
Benny Prijono | c6d5fdc | 2010-06-20 08:58:26 +0000 | [diff] [blame] | 1147 | |
| 1148 | /* Always update, by http://trac.pjsip.org/repos/ticket/864. */ |
| 1149 | pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr); |
| 1150 | tp->local_name.port = rport; |
| 1151 | |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Benny Prijono | c6d5fdc | 2010-06-20 08:58:26 +0000 | [diff] [blame] | 1154 | if (acc->cfg.contact_rewrite_method == 2 && acc->regc != NULL) { |
| 1155 | pjsip_regc_update_contact(acc->regc, 1, &acc->contact); |
| 1156 | } |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1157 | |
| 1158 | /* Perform new registration */ |
| 1159 | pjsua_acc_set_registration(acc->index, PJ_TRUE); |
| 1160 | |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1161 | pj_pool_release(pool); |
| 1162 | |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1163 | return PJ_TRUE; |
| 1164 | } |
| 1165 | |
Benny Prijono | a2a2d41 | 2007-10-18 05:54:02 +0000 | [diff] [blame] | 1166 | /* Check and update Service-Route header */ |
| 1167 | void update_service_route(pjsua_acc *acc, pjsip_rx_data *rdata) |
| 1168 | { |
| 1169 | pjsip_generic_string_hdr *hsr = NULL; |
| 1170 | pjsip_route_hdr *hr, *h; |
| 1171 | const pj_str_t HNAME = { "Service-Route", 13 }; |
| 1172 | const pj_str_t HROUTE = { "Route", 5 }; |
| 1173 | pjsip_uri *uri[PJSUA_ACC_MAX_PROXIES]; |
Benny Prijono | f020ab2 | 2007-10-18 15:28:33 +0000 | [diff] [blame] | 1174 | unsigned i, uri_cnt = 0, rcnt; |
Benny Prijono | a2a2d41 | 2007-10-18 05:54:02 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* Find and parse Service-Route headers */ |
| 1177 | for (;;) { |
| 1178 | char saved; |
| 1179 | int parsed_len; |
| 1180 | |
| 1181 | /* Find Service-Route header */ |
| 1182 | hsr = (pjsip_generic_string_hdr*) |
| 1183 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &HNAME, hsr); |
| 1184 | if (!hsr) |
| 1185 | break; |
| 1186 | |
| 1187 | /* Parse as Route header since the syntax is similar. This may |
| 1188 | * return more than one headers. |
| 1189 | */ |
| 1190 | saved = hsr->hvalue.ptr[hsr->hvalue.slen]; |
| 1191 | hsr->hvalue.ptr[hsr->hvalue.slen] = '\0'; |
| 1192 | hr = (pjsip_route_hdr*) |
| 1193 | pjsip_parse_hdr(rdata->tp_info.pool, &HROUTE, hsr->hvalue.ptr, |
| 1194 | hsr->hvalue.slen, &parsed_len); |
| 1195 | hsr->hvalue.ptr[hsr->hvalue.slen] = saved; |
| 1196 | |
| 1197 | if (hr == NULL) { |
| 1198 | /* Error */ |
| 1199 | PJ_LOG(1,(THIS_FILE, "Error parsing Service-Route header")); |
| 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | /* Save each URI in the result */ |
| 1204 | h = hr; |
| 1205 | do { |
| 1206 | if (!PJSIP_URI_SCHEME_IS_SIP(h->name_addr.uri) && |
| 1207 | !PJSIP_URI_SCHEME_IS_SIPS(h->name_addr.uri)) |
| 1208 | { |
| 1209 | PJ_LOG(1,(THIS_FILE,"Error: non SIP URI in Service-Route: %.*s", |
| 1210 | (int)hsr->hvalue.slen, hsr->hvalue.ptr)); |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | uri[uri_cnt++] = h->name_addr.uri; |
| 1215 | h = h->next; |
| 1216 | } while (h != hr && uri_cnt != PJ_ARRAY_SIZE(uri)); |
| 1217 | |
| 1218 | if (h != hr) { |
| 1219 | PJ_LOG(1,(THIS_FILE, "Error: too many Service-Route headers")); |
| 1220 | return; |
| 1221 | } |
| 1222 | |
| 1223 | /* Prepare to find next Service-Route header */ |
| 1224 | hsr = hsr->next; |
| 1225 | if ((void*)hsr == (void*)&rdata->msg_info.msg->hdr) |
| 1226 | break; |
| 1227 | } |
| 1228 | |
| 1229 | if (uri_cnt == 0) |
| 1230 | return; |
| 1231 | |
| 1232 | /* |
| 1233 | * Update account's route set |
| 1234 | */ |
| 1235 | |
| 1236 | /* First remove all routes which are not the outbound proxies */ |
Benny Prijono | f020ab2 | 2007-10-18 15:28:33 +0000 | [diff] [blame] | 1237 | rcnt = pj_list_size(&acc->route_set); |
Benny Prijono | 2568c74 | 2007-11-03 09:29:52 +0000 | [diff] [blame] | 1238 | if (rcnt != pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt) { |
| 1239 | for (i=pjsua_var.ua_cfg.outbound_proxy_cnt + acc->cfg.proxy_cnt, |
| 1240 | hr=acc->route_set.prev; |
Benny Prijono | f020ab2 | 2007-10-18 15:28:33 +0000 | [diff] [blame] | 1241 | i<rcnt; |
| 1242 | ++i) |
| 1243 | { |
| 1244 | pjsip_route_hdr *prev = hr->prev; |
| 1245 | pj_list_erase(hr); |
| 1246 | hr = prev; |
| 1247 | } |
| 1248 | } |
Benny Prijono | a2a2d41 | 2007-10-18 05:54:02 +0000 | [diff] [blame] | 1249 | |
| 1250 | /* Then append the Service-Route URIs */ |
| 1251 | for (i=0; i<uri_cnt; ++i) { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1252 | hr = pjsip_route_hdr_create(acc->pool); |
| 1253 | hr->name_addr.uri = (pjsip_uri*)pjsip_uri_clone(acc->pool, uri[i]); |
Benny Prijono | a2a2d41 | 2007-10-18 05:54:02 +0000 | [diff] [blame] | 1254 | pj_list_push_back(&acc->route_set, hr); |
| 1255 | } |
| 1256 | |
| 1257 | /* Done */ |
| 1258 | |
| 1259 | PJ_LOG(4,(THIS_FILE, "Service-Route updated for acc %d with %d URI(s)", |
| 1260 | acc->index, uri_cnt)); |
| 1261 | } |
| 1262 | |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1263 | |
| 1264 | /* Keep alive timer callback */ |
| 1265 | static void keep_alive_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te) |
| 1266 | { |
| 1267 | pjsua_acc *acc; |
| 1268 | pjsip_tpselector tp_sel; |
| 1269 | pj_time_val delay; |
Benny Prijono | db4eb81 | 2007-12-09 05:30:47 +0000 | [diff] [blame] | 1270 | char addrtxt[PJ_INET6_ADDRSTRLEN]; |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1271 | pj_status_t status; |
| 1272 | |
| 1273 | PJ_UNUSED_ARG(th); |
| 1274 | |
| 1275 | PJSUA_LOCK(); |
| 1276 | |
| 1277 | te->id = PJ_FALSE; |
| 1278 | |
| 1279 | acc = (pjsua_acc*) te->user_data; |
| 1280 | |
| 1281 | /* Select the transport to send the packet */ |
| 1282 | pj_bzero(&tp_sel, sizeof(tp_sel)); |
| 1283 | tp_sel.type = PJSIP_TPSELECTOR_TRANSPORT; |
| 1284 | tp_sel.u.transport = acc->ka_transport; |
| 1285 | |
| 1286 | PJ_LOG(5,(THIS_FILE, |
Benny Prijono | db4eb81 | 2007-12-09 05:30:47 +0000 | [diff] [blame] | 1287 | "Sending %d bytes keep-alive packet for acc %d to %s", |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1288 | acc->cfg.ka_data.slen, acc->index, |
Benny Prijono | db4eb81 | 2007-12-09 05:30:47 +0000 | [diff] [blame] | 1289 | pj_sockaddr_print(&acc->ka_target, addrtxt, sizeof(addrtxt),3))); |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1290 | |
| 1291 | /* Send raw packet */ |
| 1292 | status = pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(pjsua_var.endpt), |
| 1293 | PJSIP_TRANSPORT_UDP, &tp_sel, |
| 1294 | NULL, acc->cfg.ka_data.ptr, |
| 1295 | acc->cfg.ka_data.slen, |
| 1296 | &acc->ka_target, acc->ka_target_len, |
| 1297 | NULL, NULL); |
| 1298 | |
| 1299 | if (status != PJ_SUCCESS && status != PJ_EPENDING) { |
| 1300 | pjsua_perror(THIS_FILE, "Error sending keep-alive packet", status); |
| 1301 | } |
| 1302 | |
| 1303 | /* Reschedule next timer */ |
| 1304 | delay.sec = acc->cfg.ka_interval; |
| 1305 | delay.msec = 0; |
| 1306 | status = pjsip_endpt_schedule_timer(pjsua_var.endpt, te, &delay); |
| 1307 | if (status == PJ_SUCCESS) { |
| 1308 | te->id = PJ_TRUE; |
| 1309 | } else { |
| 1310 | pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status); |
| 1311 | } |
| 1312 | |
| 1313 | PJSUA_UNLOCK(); |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | /* Update keep-alive for the account */ |
| 1318 | static void update_keep_alive(pjsua_acc *acc, pj_bool_t start, |
| 1319 | struct pjsip_regc_cbparam *param) |
| 1320 | { |
| 1321 | /* In all cases, stop keep-alive timer if it's running. */ |
| 1322 | if (acc->ka_timer.id) { |
| 1323 | pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer); |
| 1324 | acc->ka_timer.id = PJ_FALSE; |
| 1325 | |
| 1326 | pjsip_transport_dec_ref(acc->ka_transport); |
| 1327 | acc->ka_transport = NULL; |
| 1328 | } |
| 1329 | |
| 1330 | if (start) { |
| 1331 | pj_time_val delay; |
| 1332 | pj_status_t status; |
| 1333 | |
| 1334 | /* Only do keep-alive if: |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1335 | * - ka_interval is not zero in the account, and |
| 1336 | * - transport is UDP. |
Benny Prijono | 7fff9f9 | 2008-04-04 10:50:21 +0000 | [diff] [blame] | 1337 | * |
| 1338 | * Previously we only enabled keep-alive when STUN is enabled, since |
| 1339 | * we thought that keep-alive is only needed in Internet situation. |
| 1340 | * But it has been discovered that Windows Firewall on WinXP also |
| 1341 | * needs to be kept-alive, otherwise incoming packets will be dropped. |
| 1342 | * So because of this, now keep-alive is always enabled for UDP, |
| 1343 | * regardless of whether STUN is enabled or not. |
| 1344 | * |
| 1345 | * Note that this applies only for UDP. For TCP/TLS, the keep-alive |
| 1346 | * is done by the transport layer. |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1347 | */ |
Benny Prijono | 7fff9f9 | 2008-04-04 10:50:21 +0000 | [diff] [blame] | 1348 | if (/*pjsua_var.stun_srv.ipv4.sin_family == 0 ||*/ |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1349 | acc->cfg.ka_interval == 0 || |
| 1350 | param->rdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP) |
| 1351 | { |
| 1352 | /* Keep alive is not necessary */ |
| 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | /* Save transport and destination address. */ |
| 1357 | acc->ka_transport = param->rdata->tp_info.transport; |
| 1358 | pjsip_transport_add_ref(acc->ka_transport); |
| 1359 | pj_memcpy(&acc->ka_target, ¶m->rdata->pkt_info.src_addr, |
| 1360 | param->rdata->pkt_info.src_addr_len); |
| 1361 | acc->ka_target_len = param->rdata->pkt_info.src_addr_len; |
| 1362 | |
| 1363 | /* Setup and start the timer */ |
| 1364 | acc->ka_timer.cb = &keep_alive_timer_cb; |
| 1365 | acc->ka_timer.user_data = (void*)acc; |
| 1366 | |
| 1367 | delay.sec = acc->cfg.ka_interval; |
| 1368 | delay.msec = 0; |
| 1369 | status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &acc->ka_timer, |
| 1370 | &delay); |
| 1371 | if (status == PJ_SUCCESS) { |
| 1372 | acc->ka_timer.id = PJ_TRUE; |
| 1373 | PJ_LOG(4,(THIS_FILE, "Keep-alive timer started for acc %d, " |
| 1374 | "destination:%s:%d, interval:%ds", |
| 1375 | acc->index, |
| 1376 | param->rdata->pkt_info.src_name, |
| 1377 | param->rdata->pkt_info.src_port, |
| 1378 | acc->cfg.ka_interval)); |
| 1379 | } else { |
| 1380 | acc->ka_timer.id = PJ_FALSE; |
| 1381 | pjsip_transport_dec_ref(acc->ka_transport); |
| 1382 | acc->ka_transport = NULL; |
| 1383 | pjsua_perror(THIS_FILE, "Error starting keep-alive timer", status); |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * This callback is called by pjsip_regc when outgoing register |
| 1391 | * request has completed. |
| 1392 | */ |
| 1393 | static void regc_cb(struct pjsip_regc_cbparam *param) |
| 1394 | { |
| 1395 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1396 | pjsua_acc *acc = (pjsua_acc*) param->token; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1397 | |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1398 | if (param->regc != acc->regc) |
| 1399 | return; |
| 1400 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1401 | PJSUA_LOCK(); |
| 1402 | |
| 1403 | /* |
| 1404 | * Print registration status. |
| 1405 | */ |
| 1406 | if (param->status!=PJ_SUCCESS) { |
| 1407 | pjsua_perror(THIS_FILE, "SIP registration error", |
| 1408 | param->status); |
| 1409 | pjsip_regc_destroy(acc->regc); |
| 1410 | acc->regc = NULL; |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1411 | acc->contact.slen = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1412 | |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1413 | /* Stop keep-alive timer if any. */ |
| 1414 | update_keep_alive(acc, PJ_FALSE, NULL); |
| 1415 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1416 | } else if (param->code < 0 || param->code >= 300) { |
| 1417 | PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)", |
| 1418 | param->code, |
| 1419 | (int)param->reason.slen, param->reason.ptr)); |
| 1420 | pjsip_regc_destroy(acc->regc); |
| 1421 | acc->regc = NULL; |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1422 | acc->contact.slen = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1423 | |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1424 | /* Stop keep-alive timer if any. */ |
| 1425 | update_keep_alive(acc, PJ_FALSE, NULL); |
| 1426 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1427 | } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) { |
| 1428 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 1429 | /* Update auto registration flag */ |
| 1430 | acc->auto_rereg.active = PJ_FALSE; |
| 1431 | acc->auto_rereg.attempt_cnt = 0; |
| 1432 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1433 | if (param->expiration < 1) { |
| 1434 | pjsip_regc_destroy(acc->regc); |
| 1435 | acc->regc = NULL; |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1436 | acc->contact.slen = 0; |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1437 | |
| 1438 | /* Stop keep-alive timer if any. */ |
| 1439 | update_keep_alive(acc, PJ_FALSE, NULL); |
| 1440 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1441 | PJ_LOG(3,(THIS_FILE, "%s: unregistration success", |
| 1442 | pjsua_var.acc[acc->index].cfg.id.ptr)); |
| 1443 | } else { |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 1444 | /* Check NAT bound address */ |
| 1445 | if (acc_check_nat_addr(acc, param)) { |
| 1446 | /* Update address, don't notify application yet */ |
| 1447 | PJSUA_UNLOCK(); |
| 1448 | return; |
| 1449 | } |
| 1450 | |
Benny Prijono | a2a2d41 | 2007-10-18 05:54:02 +0000 | [diff] [blame] | 1451 | /* Check and update Service-Route header */ |
| 1452 | update_service_route(acc, param->rdata); |
| 1453 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1454 | PJ_LOG(3, (THIS_FILE, |
| 1455 | "%s: registration success, status=%d (%.*s), " |
| 1456 | "will re-register in %d seconds", |
| 1457 | pjsua_var.acc[acc->index].cfg.id.ptr, |
| 1458 | param->code, |
| 1459 | (int)param->reason.slen, param->reason.ptr, |
| 1460 | param->expiration)); |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1461 | |
Benny Prijono | bddef2c | 2007-10-31 13:28:08 +0000 | [diff] [blame] | 1462 | /* Start keep-alive timer if necessary. */ |
| 1463 | update_keep_alive(acc, PJ_TRUE, param); |
| 1464 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 1465 | /* Send initial PUBLISH if it is enabled */ |
| 1466 | if (acc->cfg.publish_enabled && acc->publish_sess==NULL) |
| 1467 | pjsua_pres_init_publish_acc(acc->index); |
Benny Prijono | 4dd961b | 2009-10-26 11:21:37 +0000 | [diff] [blame] | 1468 | |
| 1469 | /* Subscribe to MWI, if it's enabled */ |
| 1470 | if (acc->cfg.mwi_enabled) |
| 1471 | pjsua_start_mwi(acc); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | } else { |
| 1475 | PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code)); |
| 1476 | } |
| 1477 | |
| 1478 | acc->reg_last_err = param->status; |
| 1479 | acc->reg_last_code = param->code; |
| 1480 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 1481 | /* Check if we need to auto retry registration. Basically, registration |
| 1482 | * failure codes triggering auto-retry are those of temporal failures |
| 1483 | * considered to be recoverable in relatively short term. |
| 1484 | */ |
| 1485 | if (acc->cfg.reg_retry_interval && |
| 1486 | (param->code == PJSIP_SC_REQUEST_TIMEOUT || |
| 1487 | param->code == PJSIP_SC_INTERNAL_SERVER_ERROR || |
| 1488 | param->code == PJSIP_SC_BAD_GATEWAY || |
| 1489 | param->code == PJSIP_SC_SERVICE_UNAVAILABLE || |
| 1490 | param->code == PJSIP_SC_SERVER_TIMEOUT || |
| 1491 | PJSIP_IS_STATUS_IN_CLASS(param->code, 600))) /* Global failure */ |
| 1492 | { |
| 1493 | schedule_reregistration(acc); |
| 1494 | } |
| 1495 | |
Nanang Izzuddin | c71bed6 | 2010-05-26 15:04:43 +0000 | [diff] [blame] | 1496 | if (pjsua_var.ua_cfg.cb.on_reg_state) |
| 1497 | (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index); |
| 1498 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1499 | PJSUA_UNLOCK(); |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | /* |
| 1504 | * Initialize client registration. |
| 1505 | */ |
| 1506 | static pj_status_t pjsua_regc_init(int acc_id) |
| 1507 | { |
| 1508 | pjsua_acc *acc; |
Benny Prijono | e1a8bad | 2006-10-13 17:45:38 +0000 | [diff] [blame] | 1509 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1510 | pj_status_t status; |
| 1511 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1512 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1513 | acc = &pjsua_var.acc[acc_id]; |
| 1514 | |
| 1515 | if (acc->cfg.reg_uri.slen == 0) { |
| 1516 | PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified")); |
| 1517 | return PJ_SUCCESS; |
| 1518 | } |
| 1519 | |
Benny Prijono | e1a8bad | 2006-10-13 17:45:38 +0000 | [diff] [blame] | 1520 | /* Destroy existing session, if any */ |
| 1521 | if (acc->regc) { |
| 1522 | pjsip_regc_destroy(acc->regc); |
| 1523 | acc->regc = NULL; |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1524 | acc->contact.slen = 0; |
Benny Prijono | e1a8bad | 2006-10-13 17:45:38 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1527 | /* initialize SIP registration if registrar is configured */ |
| 1528 | |
| 1529 | status = pjsip_regc_create( pjsua_var.endpt, |
| 1530 | acc, ®c_cb, &acc->regc); |
| 1531 | |
| 1532 | if (status != PJ_SUCCESS) { |
| 1533 | pjsua_perror(THIS_FILE, "Unable to create client registration", |
| 1534 | status); |
| 1535 | return status; |
| 1536 | } |
| 1537 | |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1538 | pool = pjsua_pool_create("tmpregc", 512, 512); |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1539 | |
| 1540 | if (acc->contact.slen == 0) { |
| 1541 | pj_str_t tmp_contact; |
| 1542 | |
| 1543 | status = pjsua_acc_create_uac_contact( pool, &tmp_contact, |
| 1544 | acc_id, &acc->cfg.reg_uri); |
| 1545 | if (status != PJ_SUCCESS) { |
| 1546 | pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header" |
| 1547 | " for registration", |
| 1548 | status); |
| 1549 | pjsip_regc_destroy(acc->regc); |
| 1550 | pj_pool_release(pool); |
| 1551 | acc->regc = NULL; |
| 1552 | return status; |
| 1553 | } |
| 1554 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1555 | pj_strdup_with_null(acc->pool, &acc->contact, &tmp_contact); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1558 | status = pjsip_regc_init( acc->regc, |
| 1559 | &acc->cfg.reg_uri, |
| 1560 | &acc->cfg.id, |
| 1561 | &acc->cfg.id, |
Benny Prijono | e8554ef | 2008-03-22 09:33:52 +0000 | [diff] [blame] | 1562 | 1, &acc->contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1563 | acc->cfg.reg_timeout); |
| 1564 | if (status != PJ_SUCCESS) { |
| 1565 | pjsua_perror(THIS_FILE, |
| 1566 | "Client registration initialization error", |
| 1567 | status); |
Benny Prijono | 19b2937 | 2007-12-05 04:08:40 +0000 | [diff] [blame] | 1568 | pjsip_regc_destroy(acc->regc); |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1569 | pj_pool_release(pool); |
Benny Prijono | 19b2937 | 2007-12-05 04:08:40 +0000 | [diff] [blame] | 1570 | acc->regc = NULL; |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1571 | acc->contact.slen = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1572 | return status; |
| 1573 | } |
| 1574 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1575 | /* If account is locked to specific transport, then set transport to |
| 1576 | * the client registration. |
| 1577 | */ |
| 1578 | if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) { |
| 1579 | pjsip_tpselector tp_sel; |
| 1580 | |
| 1581 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 1582 | pjsip_regc_set_transport(acc->regc, &tp_sel); |
| 1583 | } |
| 1584 | |
| 1585 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1586 | /* Set credentials |
| 1587 | */ |
| 1588 | if (acc->cred_cnt) { |
| 1589 | pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred); |
| 1590 | } |
| 1591 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 1592 | /* Set authentication preference */ |
| 1593 | pjsip_regc_set_prefs(acc->regc, &acc->cfg.auth_pref); |
| 1594 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1595 | /* Set route-set |
| 1596 | */ |
Benny Prijono | 29c8ca3 | 2010-06-22 06:02:13 +0000 | [diff] [blame^] | 1597 | if (acc->cfg.reg_use_proxy) { |
| 1598 | pjsip_route_hdr route_set; |
| 1599 | const pjsip_route_hdr *r; |
| 1600 | |
| 1601 | pj_list_init(&route_set); |
| 1602 | |
| 1603 | if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_OUTBOUND_PROXY) { |
| 1604 | r = pjsua_var.outbound_proxy.next; |
| 1605 | while (r != &pjsua_var.outbound_proxy) { |
| 1606 | pj_list_push_back(&route_set, pjsip_hdr_shallow_clone(pool, r)); |
| 1607 | r = r->next; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | if (acc->cfg.reg_use_proxy & PJSUA_REG_USE_ACC_PROXY && |
| 1612 | acc->cfg.proxy_cnt) |
| 1613 | { |
| 1614 | int cnt = acc->cfg.proxy_cnt; |
| 1615 | pjsip_route_hdr *pos = route_set.prev; |
| 1616 | int i; |
| 1617 | |
| 1618 | r = acc->route_set.prev; |
| 1619 | for (i=0; i<cnt; ++i) { |
| 1620 | pj_list_push_front(pos, pjsip_hdr_shallow_clone(pool, r)); |
| 1621 | r = r->prev; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | if (!pj_list_empty(&route_set)) |
| 1626 | pjsip_regc_set_route_set( acc->regc, &route_set ); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 1629 | /* Add other request headers. */ |
| 1630 | if (pjsua_var.ua_cfg.user_agent.slen) { |
| 1631 | pjsip_hdr hdr_list; |
| 1632 | const pj_str_t STR_USER_AGENT = { "User-Agent", 10 }; |
| 1633 | pjsip_generic_string_hdr *h; |
| 1634 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 1635 | pj_list_init(&hdr_list); |
| 1636 | |
| 1637 | h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT, |
| 1638 | &pjsua_var.ua_cfg.user_agent); |
| 1639 | pj_list_push_back(&hdr_list, (pjsip_hdr*)h); |
| 1640 | |
| 1641 | pjsip_regc_add_headers(acc->regc, &hdr_list); |
| 1642 | } |
| 1643 | |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1644 | pj_pool_release(pool); |
| 1645 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1646 | return PJ_SUCCESS; |
| 1647 | } |
| 1648 | |
| 1649 | |
| 1650 | /* |
| 1651 | * Update registration or perform unregistration. |
| 1652 | */ |
| 1653 | PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id, |
| 1654 | pj_bool_t renew) |
| 1655 | { |
| 1656 | pj_status_t status = 0; |
| 1657 | pjsip_tx_data *tdata = 0; |
| 1658 | |
| 1659 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 1660 | PJ_EINVAL); |
| 1661 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 1662 | |
| 1663 | PJSUA_LOCK(); |
| 1664 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 1665 | /* Cancel any re-registration timer */ |
| 1666 | pjsua_cancel_timer(&pjsua_var.acc[acc_id].auto_rereg.timer); |
| 1667 | |
| 1668 | /* Reset pointer to registration transport */ |
| 1669 | pjsua_var.acc[acc_id].auto_rereg.reg_tp = NULL; |
| 1670 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1671 | if (renew) { |
| 1672 | if (pjsua_var.acc[acc_id].regc == NULL) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1673 | status = pjsua_regc_init(acc_id); |
| 1674 | if (status != PJ_SUCCESS) { |
| 1675 | pjsua_perror(THIS_FILE, "Unable to create registration", |
| 1676 | status); |
| 1677 | goto on_return; |
| 1678 | } |
| 1679 | } |
| 1680 | if (!pjsua_var.acc[acc_id].regc) { |
| 1681 | status = PJ_EINVALIDOP; |
| 1682 | goto on_return; |
| 1683 | } |
| 1684 | |
| 1685 | status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1, |
| 1686 | &tdata); |
| 1687 | |
Benny Prijono | 28f673a | 2007-10-15 07:04:59 +0000 | [diff] [blame] | 1688 | if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) { |
| 1689 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1690 | pjsip_authorization_hdr *h; |
| 1691 | char *uri; |
| 1692 | int d; |
| 1693 | |
| 1694 | uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10); |
| 1695 | d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri, |
| 1696 | uri, acc->cfg.reg_uri.slen+10); |
| 1697 | pj_assert(d > 0); |
| 1698 | |
| 1699 | h = pjsip_authorization_hdr_create(tdata->pool); |
| 1700 | h->scheme = pj_str("Digest"); |
| 1701 | h->credential.digest.username = acc->cred[0].username; |
| 1702 | h->credential.digest.realm = acc->srv_domain; |
| 1703 | h->credential.digest.uri = pj_str(uri); |
| 1704 | h->credential.digest.algorithm = pj_str("md5"); |
| 1705 | |
| 1706 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h); |
| 1707 | } |
| 1708 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1709 | } else { |
| 1710 | if (pjsua_var.acc[acc_id].regc == NULL) { |
| 1711 | PJ_LOG(3,(THIS_FILE, "Currently not registered")); |
| 1712 | status = PJ_EINVALIDOP; |
| 1713 | goto on_return; |
| 1714 | } |
Benny Prijono | 166d502 | 2010-02-10 14:24:48 +0000 | [diff] [blame] | 1715 | |
| 1716 | pjsua_pres_unpublish(&pjsua_var.acc[acc_id]); |
| 1717 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1718 | status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata); |
| 1719 | } |
| 1720 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1721 | if (status == PJ_SUCCESS) { |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 1722 | //pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1723 | status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata ); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1724 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1725 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 1726 | /* Update pointer to registration transport */ |
| 1727 | if (status == PJ_SUCCESS) { |
| 1728 | pjsip_regc_info reg_info; |
| 1729 | |
| 1730 | pjsip_regc_get_info(pjsua_var.acc[acc_id].regc, ®_info); |
| 1731 | pjsua_var.acc[acc_id].auto_rereg.reg_tp = reg_info.transport; |
| 1732 | } |
| 1733 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1734 | if (status != PJ_SUCCESS) { |
| 1735 | pjsua_perror(THIS_FILE, "Unable to create/send REGISTER", |
| 1736 | status); |
| 1737 | } else { |
| 1738 | PJ_LOG(3,(THIS_FILE, "%s sent", |
| 1739 | (renew? "Registration" : "Unregistration"))); |
| 1740 | } |
| 1741 | |
| 1742 | on_return: |
| 1743 | PJSUA_UNLOCK(); |
| 1744 | return status; |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | /* |
| 1749 | * Get account information. |
| 1750 | */ |
| 1751 | PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id, |
| 1752 | pjsua_acc_info *info) |
| 1753 | { |
| 1754 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1755 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 1756 | |
| 1757 | PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL); |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1758 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1759 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1760 | pj_bzero(info, sizeof(pjsua_acc_info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1761 | |
| 1762 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 1763 | PJ_EINVAL); |
| 1764 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 1765 | |
| 1766 | PJSUA_LOCK(); |
| 1767 | |
| 1768 | if (pjsua_var.acc[acc_id].valid == PJ_FALSE) { |
| 1769 | PJSUA_UNLOCK(); |
| 1770 | return PJ_EINVALIDOP; |
| 1771 | } |
| 1772 | |
| 1773 | info->id = acc_id; |
| 1774 | info->is_default = (pjsua_var.default_acc == acc_id); |
| 1775 | info->acc_uri = acc_cfg->id; |
| 1776 | info->has_registration = (acc->cfg.reg_uri.slen > 0); |
| 1777 | info->online_status = acc->online_status; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 1778 | pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element)); |
| 1779 | if (info->rpid.note.slen) |
| 1780 | info->online_status_text = info->rpid.note; |
| 1781 | else if (info->online_status) |
| 1782 | info->online_status_text = pj_str("Online"); |
| 1783 | else |
| 1784 | info->online_status_text = pj_str("Offline"); |
| 1785 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1786 | if (acc->reg_last_err) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1787 | info->status = (pjsip_status_code) acc->reg_last_err; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1788 | pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_)); |
| 1789 | info->status_text = pj_str(info->buf_); |
| 1790 | } else if (acc->reg_last_code) { |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 1791 | if (info->has_registration) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1792 | info->status = (pjsip_status_code) acc->reg_last_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1793 | info->status_text = *pjsip_get_status_text(acc->reg_last_code); |
| 1794 | } else { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1795 | info->status = (pjsip_status_code) 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1796 | info->status_text = pj_str("not registered"); |
| 1797 | } |
| 1798 | } else if (acc->cfg.reg_uri.slen) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1799 | info->status = PJSIP_SC_TRYING; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1800 | info->status_text = pj_str("In Progress"); |
| 1801 | } else { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1802 | info->status = (pjsip_status_code) 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1803 | info->status_text = pj_str("does not register"); |
| 1804 | } |
| 1805 | |
| 1806 | if (acc->regc) { |
| 1807 | pjsip_regc_info regc_info; |
| 1808 | pjsip_regc_get_info(acc->regc, ®c_info); |
| 1809 | info->expires = regc_info.next_reg; |
| 1810 | } else { |
| 1811 | info->expires = -1; |
| 1812 | } |
| 1813 | |
| 1814 | PJSUA_UNLOCK(); |
| 1815 | |
| 1816 | return PJ_SUCCESS; |
| 1817 | |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | /* |
| 1822 | * Enum accounts all account ids. |
| 1823 | */ |
| 1824 | PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[], |
| 1825 | unsigned *count ) |
| 1826 | { |
| 1827 | unsigned i, c; |
| 1828 | |
| 1829 | PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL); |
| 1830 | |
| 1831 | PJSUA_LOCK(); |
| 1832 | |
| 1833 | for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1834 | if (!pjsua_var.acc[i].valid) |
| 1835 | continue; |
| 1836 | ids[c] = i; |
| 1837 | ++c; |
| 1838 | } |
| 1839 | |
| 1840 | *count = c; |
| 1841 | |
| 1842 | PJSUA_UNLOCK(); |
| 1843 | |
| 1844 | return PJ_SUCCESS; |
| 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | /* |
| 1849 | * Enum accounts info. |
| 1850 | */ |
| 1851 | PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[], |
| 1852 | unsigned *count ) |
| 1853 | { |
| 1854 | unsigned i, c; |
| 1855 | |
| 1856 | PJ_ASSERT_RETURN(info && *count, PJ_EINVAL); |
| 1857 | |
| 1858 | PJSUA_LOCK(); |
| 1859 | |
| 1860 | for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1861 | if (!pjsua_var.acc[i].valid) |
| 1862 | continue; |
| 1863 | |
| 1864 | pjsua_acc_get_info(i, &info[c]); |
| 1865 | ++c; |
| 1866 | } |
| 1867 | |
| 1868 | *count = c; |
| 1869 | |
| 1870 | PJSUA_UNLOCK(); |
| 1871 | |
| 1872 | return PJ_SUCCESS; |
| 1873 | } |
| 1874 | |
| 1875 | |
| 1876 | /* |
| 1877 | * This is an internal function to find the most appropriate account to |
| 1878 | * used to reach to the specified URL. |
| 1879 | */ |
| 1880 | PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url) |
| 1881 | { |
| 1882 | pj_str_t tmp; |
| 1883 | pjsip_uri *uri; |
| 1884 | pjsip_sip_uri *sip_uri; |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1885 | pj_pool_t *tmp_pool; |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1886 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1887 | |
| 1888 | PJSUA_LOCK(); |
| 1889 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1890 | tmp_pool = pjsua_pool_create("tmpacc10", 256, 256); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1891 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1892 | pj_strdup_with_null(tmp_pool, &tmp, url); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1893 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1894 | uri = pjsip_parse_uri(tmp_pool, tmp.ptr, tmp.slen, 0); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1895 | if (!uri) { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1896 | pj_pool_release(tmp_pool); |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1897 | PJSUA_UNLOCK(); |
| 1898 | return pjsua_var.default_acc; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && |
| 1902 | !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 1903 | { |
| 1904 | /* Return the first account with proxy */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1905 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1906 | if (!pjsua_var.acc[i].valid) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1907 | continue; |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1908 | if (!pj_list_empty(&pjsua_var.acc[i].route_set)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1909 | break; |
| 1910 | } |
| 1911 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1912 | if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1913 | /* Found rather matching account */ |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1914 | pj_pool_release(tmp_pool); |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1915 | PJSUA_UNLOCK(); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1916 | return i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | /* Not found, use default account */ |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1920 | pj_pool_release(tmp_pool); |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1921 | PJSUA_UNLOCK(); |
| 1922 | return pjsua_var.default_acc; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1925 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1926 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1927 | /* Find matching domain AND port */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1928 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 1929 | unsigned acc_id = pjsua_var.acc_ids[i]; |
| 1930 | if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 && |
| 1931 | pjsua_var.acc[acc_id].srv_port == sip_uri->port) |
| 1932 | { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1933 | pj_pool_release(tmp_pool); |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1934 | PJSUA_UNLOCK(); |
| 1935 | return acc_id; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1936 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1939 | /* If no match, try to match the domain part only */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1940 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 1941 | unsigned acc_id = pjsua_var.acc_ids[i]; |
| 1942 | if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0) |
| 1943 | { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1944 | pj_pool_release(tmp_pool); |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1945 | PJSUA_UNLOCK(); |
| 1946 | return acc_id; |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1950 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1951 | /* Still no match, just use default account */ |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 1952 | pj_pool_release(tmp_pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1953 | PJSUA_UNLOCK(); |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1954 | return pjsua_var.default_acc; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | |
| 1958 | /* |
| 1959 | * This is an internal function to find the most appropriate account to be |
| 1960 | * used to handle incoming calls. |
| 1961 | */ |
| 1962 | PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata) |
| 1963 | { |
| 1964 | pjsip_uri *uri; |
| 1965 | pjsip_sip_uri *sip_uri; |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1966 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1967 | |
Benny Prijono | 371cf0a | 2007-06-19 00:35:37 +0000 | [diff] [blame] | 1968 | /* Check that there's at least one account configured */ |
| 1969 | PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc); |
| 1970 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1971 | uri = rdata->msg_info.to->uri; |
| 1972 | |
| 1973 | /* Just return default account if To URI is not SIP: */ |
| 1974 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && |
| 1975 | !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 1976 | { |
| 1977 | return pjsua_var.default_acc; |
| 1978 | } |
| 1979 | |
| 1980 | |
| 1981 | PJSUA_LOCK(); |
| 1982 | |
| 1983 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 1984 | |
| 1985 | /* Find account which has matching username and domain. */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1986 | for (i=0; i < pjsua_var.acc_cnt; ++i) { |
| 1987 | unsigned acc_id = pjsua_var.acc_ids[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1988 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1989 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1990 | if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 && |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1991 | pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1992 | { |
| 1993 | /* Match ! */ |
| 1994 | PJSUA_UNLOCK(); |
| 1995 | return acc_id; |
| 1996 | } |
| 1997 | } |
| 1998 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1999 | /* No matching account, try match domain part only. */ |
| 2000 | for (i=0; i < pjsua_var.acc_cnt; ++i) { |
| 2001 | unsigned acc_id = pjsua_var.acc_ids[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2002 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 2003 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2004 | if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2005 | /* Match ! */ |
| 2006 | PJSUA_UNLOCK(); |
| 2007 | return acc_id; |
| 2008 | } |
| 2009 | } |
| 2010 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2011 | /* No matching account, try match user part (and transport type) only. */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 2012 | for (i=0; i < pjsua_var.acc_cnt; ++i) { |
| 2013 | unsigned acc_id = pjsua_var.acc_ids[i]; |
| 2014 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 2015 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2016 | if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) { |
| 2017 | |
| 2018 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 2019 | pjsip_transport_type_e type; |
| 2020 | type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); |
| 2021 | if (type == PJSIP_TRANSPORT_UNSPECIFIED) |
| 2022 | type = PJSIP_TRANSPORT_UDP; |
| 2023 | |
| 2024 | if (pjsua_var.tpdata[acc->cfg.transport_id].type != type) |
| 2025 | continue; |
| 2026 | } |
| 2027 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 2028 | /* Match ! */ |
| 2029 | PJSUA_UNLOCK(); |
| 2030 | return acc_id; |
| 2031 | } |
| 2032 | } |
| 2033 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2034 | /* Still no match, use default account */ |
| 2035 | PJSUA_UNLOCK(); |
| 2036 | return pjsua_var.default_acc; |
| 2037 | } |
| 2038 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2039 | |
Benny Prijono | fff245c | 2007-04-02 11:44:47 +0000 | [diff] [blame] | 2040 | /* |
| 2041 | * Create arbitrary requests for this account. |
| 2042 | */ |
| 2043 | PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id, |
| 2044 | const pjsip_method *method, |
| 2045 | const pj_str_t *target, |
| 2046 | pjsip_tx_data **p_tdata) |
| 2047 | { |
| 2048 | pjsip_tx_data *tdata; |
| 2049 | pjsua_acc *acc; |
| 2050 | pjsip_route_hdr *r; |
| 2051 | pj_status_t status; |
| 2052 | |
| 2053 | PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL); |
| 2054 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
| 2055 | |
| 2056 | acc = &pjsua_var.acc[acc_id]; |
| 2057 | |
| 2058 | status = pjsip_endpt_create_request(pjsua_var.endpt, method, target, |
| 2059 | &acc->cfg.id, target, |
| 2060 | NULL, NULL, -1, NULL, &tdata); |
| 2061 | if (status != PJ_SUCCESS) { |
| 2062 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 2063 | return status; |
| 2064 | } |
| 2065 | |
| 2066 | /* Copy routeset */ |
| 2067 | r = acc->route_set.next; |
| 2068 | while (r != &acc->route_set) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2069 | pjsip_msg_add_hdr(tdata->msg, |
| 2070 | (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r)); |
Benny Prijono | fff245c | 2007-04-02 11:44:47 +0000 | [diff] [blame] | 2071 | r = r->next; |
| 2072 | } |
Benny Prijono | e791156 | 2009-12-14 11:13:45 +0000 | [diff] [blame] | 2073 | |
| 2074 | /* If account is locked to specific transport, then set that transport to |
| 2075 | * the transmit data. |
| 2076 | */ |
| 2077 | if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) { |
| 2078 | pjsip_tpselector tp_sel; |
| 2079 | |
| 2080 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 2081 | pjsip_tx_data_set_transport(tdata, &tp_sel); |
| 2082 | } |
| 2083 | |
Benny Prijono | fff245c | 2007-04-02 11:44:47 +0000 | [diff] [blame] | 2084 | /* Done */ |
| 2085 | *p_tdata = tdata; |
| 2086 | return PJ_SUCCESS; |
| 2087 | } |
| 2088 | |
| 2089 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2090 | PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool, |
| 2091 | pj_str_t *contact, |
| 2092 | pjsua_acc_id acc_id, |
| 2093 | const pj_str_t *suri) |
| 2094 | { |
| 2095 | pjsua_acc *acc; |
| 2096 | pjsip_sip_uri *sip_uri; |
| 2097 | pj_status_t status; |
| 2098 | pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED; |
| 2099 | pj_str_t local_addr; |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2100 | pjsip_tpselector tp_sel; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2101 | unsigned flag; |
| 2102 | int secure; |
| 2103 | int local_port; |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2104 | const char *beginquote, *endquote; |
| 2105 | char transport_param[32]; |
| 2106 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2107 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2108 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2109 | acc = &pjsua_var.acc[acc_id]; |
| 2110 | |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 2111 | /* If force_contact is configured, then use use it */ |
| 2112 | if (acc->cfg.force_contact.slen) { |
| 2113 | *contact = acc->cfg.force_contact; |
| 2114 | return PJ_SUCCESS; |
| 2115 | } |
| 2116 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2117 | /* If route-set is configured for the account, then URI is the |
| 2118 | * first entry of the route-set. |
| 2119 | */ |
| 2120 | if (!pj_list_empty(&acc->route_set)) { |
Benny Prijono | 9c1528f | 2007-02-10 19:22:25 +0000 | [diff] [blame] | 2121 | sip_uri = (pjsip_sip_uri*) |
| 2122 | pjsip_uri_get_uri(acc->route_set.next->name_addr.uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2123 | } else { |
| 2124 | pj_str_t tmp; |
| 2125 | pjsip_uri *uri; |
| 2126 | |
| 2127 | pj_strdup_with_null(pool, &tmp, suri); |
| 2128 | |
| 2129 | uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0); |
| 2130 | if (uri == NULL) |
| 2131 | return PJSIP_EINVALIDURI; |
| 2132 | |
| 2133 | /* For non-SIP scheme, route set should be configured */ |
| 2134 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 2135 | return PJSIP_EINVALIDREQURI; |
| 2136 | |
Benny Prijono | 8c7a617 | 2007-02-18 21:17:46 +0000 | [diff] [blame] | 2137 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /* Get transport type of the URI */ |
| 2141 | if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) |
| 2142 | tp_type = PJSIP_TRANSPORT_TLS; |
| 2143 | else if (sip_uri->transport_param.slen == 0) { |
| 2144 | tp_type = PJSIP_TRANSPORT_UDP; |
| 2145 | } else |
| 2146 | tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); |
| 2147 | |
| 2148 | if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED) |
| 2149 | return PJSIP_EUNSUPTRANSPORT; |
| 2150 | |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2151 | /* If destination URI specifies IPv6, then set transport type |
| 2152 | * to use IPv6 as well. |
| 2153 | */ |
Benny Prijono | 19b2937 | 2007-12-05 04:08:40 +0000 | [diff] [blame] | 2154 | if (pj_strchr(&sip_uri->host, ':')) |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2155 | tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6); |
| 2156 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2157 | flag = pjsip_transport_get_flag_from_type(tp_type); |
| 2158 | secure = (flag & PJSIP_TRANSPORT_SECURE) != 0; |
| 2159 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2160 | /* Init transport selector. */ |
| 2161 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 2162 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2163 | /* Get local address suitable to send request from */ |
| 2164 | status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt), |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2165 | pool, tp_type, &tp_sel, |
| 2166 | &local_addr, &local_port); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2167 | if (status != PJ_SUCCESS) |
| 2168 | return status; |
| 2169 | |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2170 | /* Enclose IPv6 address in square brackets */ |
| 2171 | if (tp_type & PJSIP_TRANSPORT_IPV6) { |
| 2172 | beginquote = "["; |
| 2173 | endquote = "]"; |
| 2174 | } else { |
| 2175 | beginquote = endquote = ""; |
| 2176 | } |
| 2177 | |
| 2178 | /* Don't add transport parameter if it's UDP */ |
Benny Prijono | 4c82c1e | 2008-10-16 08:14:51 +0000 | [diff] [blame] | 2179 | if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) { |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2180 | pj_ansi_snprintf(transport_param, sizeof(transport_param), |
| 2181 | ";transport=%s", |
| 2182 | pjsip_transport_get_type_name(tp_type)); |
| 2183 | } else { |
| 2184 | transport_param[0] = '\0'; |
| 2185 | } |
| 2186 | |
| 2187 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2188 | /* Create the contact header */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2189 | contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2190 | contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE, |
Nanang Izzuddin | e2c7e85 | 2009-08-04 14:36:17 +0000 | [diff] [blame] | 2191 | "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s", |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2192 | (int)acc->display.slen, |
| 2193 | acc->display.ptr, |
| 2194 | (acc->display.slen?" " : ""), |
Benny Prijono | 8058a62 | 2007-06-08 04:37:05 +0000 | [diff] [blame] | 2195 | (secure ? PJSUA_SECURE_SCHEME : "sip"), |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2196 | (int)acc->user_part.slen, |
| 2197 | acc->user_part.ptr, |
| 2198 | (acc->user_part.slen?"@":""), |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2199 | beginquote, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2200 | (int)local_addr.slen, |
| 2201 | local_addr.ptr, |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2202 | endquote, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2203 | local_port, |
Benny Prijono | 30fe485 | 2008-12-10 16:54:16 +0000 | [diff] [blame] | 2204 | transport_param, |
Nanang Izzuddin | e2c7e85 | 2009-08-04 14:36:17 +0000 | [diff] [blame] | 2205 | (int)acc->cfg.contact_uri_params.slen, |
| 2206 | acc->cfg.contact_uri_params.ptr, |
Benny Prijono | 30fe485 | 2008-12-10 16:54:16 +0000 | [diff] [blame] | 2207 | (int)acc->cfg.contact_params.slen, |
| 2208 | acc->cfg.contact_params.ptr); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2209 | |
| 2210 | return PJ_SUCCESS; |
| 2211 | } |
| 2212 | |
| 2213 | |
| 2214 | |
| 2215 | PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool, |
| 2216 | pj_str_t *contact, |
| 2217 | pjsua_acc_id acc_id, |
| 2218 | pjsip_rx_data *rdata ) |
| 2219 | { |
| 2220 | /* |
| 2221 | * Section 12.1.1, paragraph about using SIPS URI in Contact. |
| 2222 | * If the request that initiated the dialog contained a SIPS URI |
| 2223 | * in the Request-URI or in the top Record-Route header field value, |
| 2224 | * if there was any, or the Contact header field if there was no |
| 2225 | * Record-Route header field, the Contact header field in the response |
| 2226 | * MUST be a SIPS URI. |
| 2227 | */ |
| 2228 | pjsua_acc *acc; |
| 2229 | pjsip_sip_uri *sip_uri; |
| 2230 | pj_status_t status; |
| 2231 | pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED; |
| 2232 | pj_str_t local_addr; |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2233 | pjsip_tpselector tp_sel; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2234 | unsigned flag; |
| 2235 | int secure; |
| 2236 | int local_port; |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2237 | const char *beginquote, *endquote; |
| 2238 | char transport_param[32]; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2239 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2240 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2241 | acc = &pjsua_var.acc[acc_id]; |
| 2242 | |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 2243 | /* If force_contact is configured, then use use it */ |
| 2244 | if (acc->cfg.force_contact.slen) { |
| 2245 | *contact = acc->cfg.force_contact; |
| 2246 | return PJ_SUCCESS; |
| 2247 | } |
| 2248 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2249 | /* If Record-Route is present, then URI is the top Record-Route. */ |
| 2250 | if (rdata->msg_info.record_route) { |
Benny Prijono | 9c1528f | 2007-02-10 19:22:25 +0000 | [diff] [blame] | 2251 | sip_uri = (pjsip_sip_uri*) |
| 2252 | pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2253 | } else { |
Benny Prijono | a330d45 | 2008-08-05 20:14:39 +0000 | [diff] [blame] | 2254 | pjsip_hdr *pos = NULL; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2255 | pjsip_contact_hdr *h_contact; |
| 2256 | pjsip_uri *uri = NULL; |
| 2257 | |
Benny Prijono | a330d45 | 2008-08-05 20:14:39 +0000 | [diff] [blame] | 2258 | /* Otherwise URI is Contact URI. |
| 2259 | * Iterate the Contact URI until we find sip: or sips: scheme. |
| 2260 | */ |
| 2261 | do { |
| 2262 | h_contact = (pjsip_contact_hdr*) |
| 2263 | pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, |
| 2264 | pos); |
| 2265 | if (h_contact) { |
Benny Prijono | 8b33bba | 2010-06-02 03:03:43 +0000 | [diff] [blame] | 2266 | if (h_contact->uri) |
| 2267 | uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri); |
| 2268 | else |
| 2269 | uri = NULL; |
| 2270 | if (!uri || (!PJSIP_URI_SCHEME_IS_SIP(uri) && |
| 2271 | !PJSIP_URI_SCHEME_IS_SIPS(uri))) |
Benny Prijono | a330d45 | 2008-08-05 20:14:39 +0000 | [diff] [blame] | 2272 | { |
| 2273 | pos = (pjsip_hdr*)h_contact->next; |
| 2274 | if (pos == &rdata->msg_info.msg->hdr) |
| 2275 | h_contact = NULL; |
| 2276 | } else { |
| 2277 | break; |
| 2278 | } |
| 2279 | } |
| 2280 | } while (h_contact); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2281 | |
| 2282 | |
| 2283 | /* Or if Contact URI is not present, take the remote URI from |
| 2284 | * the From URI. |
| 2285 | */ |
| 2286 | if (uri == NULL) |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2287 | uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2288 | |
| 2289 | |
| 2290 | /* Can only do sip/sips scheme at present. */ |
| 2291 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 2292 | return PJSIP_EINVALIDREQURI; |
| 2293 | |
Benny Prijono | 8c7a617 | 2007-02-18 21:17:46 +0000 | [diff] [blame] | 2294 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | /* Get transport type of the URI */ |
| 2298 | if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) |
| 2299 | tp_type = PJSIP_TRANSPORT_TLS; |
| 2300 | else if (sip_uri->transport_param.slen == 0) { |
| 2301 | tp_type = PJSIP_TRANSPORT_UDP; |
| 2302 | } else |
| 2303 | tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2304 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2305 | if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED) |
| 2306 | return PJSIP_EUNSUPTRANSPORT; |
| 2307 | |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2308 | /* If destination URI specifies IPv6, then set transport type |
| 2309 | * to use IPv6 as well. |
| 2310 | */ |
| 2311 | if (pj_strchr(&sip_uri->host, ':')) |
| 2312 | tp_type = (pjsip_transport_type_e)(((int)tp_type) + PJSIP_TRANSPORT_IPV6); |
| 2313 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2314 | flag = pjsip_transport_get_flag_from_type(tp_type); |
| 2315 | secure = (flag & PJSIP_TRANSPORT_SECURE) != 0; |
| 2316 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2317 | /* Init transport selector. */ |
| 2318 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 2319 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2320 | /* Get local address suitable to send request from */ |
| 2321 | status = pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(pjsua_var.endpt), |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2322 | pool, tp_type, &tp_sel, |
| 2323 | &local_addr, &local_port); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2324 | if (status != PJ_SUCCESS) |
| 2325 | return status; |
| 2326 | |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2327 | /* Enclose IPv6 address in square brackets */ |
| 2328 | if (tp_type & PJSIP_TRANSPORT_IPV6) { |
| 2329 | beginquote = "["; |
| 2330 | endquote = "]"; |
| 2331 | } else { |
| 2332 | beginquote = endquote = ""; |
| 2333 | } |
| 2334 | |
| 2335 | /* Don't add transport parameter if it's UDP */ |
Benny Prijono | 4c82c1e | 2008-10-16 08:14:51 +0000 | [diff] [blame] | 2336 | if (tp_type!=PJSIP_TRANSPORT_UDP && tp_type!=PJSIP_TRANSPORT_UDP6) { |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2337 | pj_ansi_snprintf(transport_param, sizeof(transport_param), |
| 2338 | ";transport=%s", |
| 2339 | pjsip_transport_get_type_name(tp_type)); |
| 2340 | } else { |
| 2341 | transport_param[0] = '\0'; |
| 2342 | } |
| 2343 | |
| 2344 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2345 | /* Create the contact header */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2346 | contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2347 | contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE, |
Nanang Izzuddin | e2c7e85 | 2009-08-04 14:36:17 +0000 | [diff] [blame] | 2348 | "%.*s%s<%s:%.*s%s%s%.*s%s:%d%s%.*s>%.*s", |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2349 | (int)acc->display.slen, |
| 2350 | acc->display.ptr, |
| 2351 | (acc->display.slen?" " : ""), |
Benny Prijono | 8058a62 | 2007-06-08 04:37:05 +0000 | [diff] [blame] | 2352 | (secure ? PJSUA_SECURE_SCHEME : "sip"), |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2353 | (int)acc->user_part.slen, |
| 2354 | acc->user_part.ptr, |
| 2355 | (acc->user_part.slen?"@":""), |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2356 | beginquote, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2357 | (int)local_addr.slen, |
| 2358 | local_addr.ptr, |
Benny Prijono | d0bd498 | 2007-12-02 15:40:52 +0000 | [diff] [blame] | 2359 | endquote, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2360 | local_port, |
Benny Prijono | 30fe485 | 2008-12-10 16:54:16 +0000 | [diff] [blame] | 2361 | transport_param, |
Nanang Izzuddin | e2c7e85 | 2009-08-04 14:36:17 +0000 | [diff] [blame] | 2362 | (int)acc->cfg.contact_uri_params.slen, |
| 2363 | acc->cfg.contact_uri_params.ptr, |
Benny Prijono | 30fe485 | 2008-12-10 16:54:16 +0000 | [diff] [blame] | 2364 | (int)acc->cfg.contact_params.slen, |
| 2365 | acc->cfg.contact_params.ptr); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2366 | |
| 2367 | return PJ_SUCCESS; |
| 2368 | } |
| 2369 | |
| 2370 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2371 | PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id, |
| 2372 | pjsua_transport_id tp_id) |
| 2373 | { |
| 2374 | pjsua_acc *acc; |
| 2375 | |
| 2376 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
| 2377 | acc = &pjsua_var.acc[acc_id]; |
| 2378 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2379 | PJ_ASSERT_RETURN(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 2380 | PJ_EINVAL); |
| 2381 | |
| 2382 | acc->cfg.transport_id = tp_id; |
| 2383 | |
| 2384 | return PJ_SUCCESS; |
| 2385 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 2386 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 2387 | |
| 2388 | /* Auto re-registration timeout callback */ |
| 2389 | static void auto_rereg_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te) |
| 2390 | { |
| 2391 | pjsua_acc *acc; |
| 2392 | pj_status_t status; |
| 2393 | |
| 2394 | PJ_UNUSED_ARG(th); |
| 2395 | acc = (pjsua_acc*) te->user_data; |
| 2396 | pj_assert(acc); |
| 2397 | |
| 2398 | PJSUA_LOCK(); |
| 2399 | |
Nanang Izzuddin | c71bed6 | 2010-05-26 15:04:43 +0000 | [diff] [blame] | 2400 | /* Check if the reregistration timer is still valid, e.g: while waiting |
| 2401 | * timeout timer application might have deleted the account or disabled |
| 2402 | * the auto-reregistration. |
| 2403 | */ |
| 2404 | if (!acc->valid || !acc->auto_rereg.active || |
| 2405 | acc->cfg.reg_retry_interval == 0) |
| 2406 | { |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 2407 | goto on_return; |
Nanang Izzuddin | c71bed6 | 2010-05-26 15:04:43 +0000 | [diff] [blame] | 2408 | } |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 2409 | |
| 2410 | /* Start re-registration */ |
| 2411 | acc->auto_rereg.attempt_cnt++; |
| 2412 | status = pjsua_acc_set_registration(acc->index, PJ_TRUE); |
| 2413 | if (status != PJ_SUCCESS) |
| 2414 | schedule_reregistration(acc); |
| 2415 | |
Nanang Izzuddin | 6658000 | 2010-04-14 08:12:08 +0000 | [diff] [blame] | 2416 | on_return: |
| 2417 | PJSUA_UNLOCK(); |
| 2418 | } |
| 2419 | |
| 2420 | |
| 2421 | /* Schedule reregistration for specified account. Note that the first |
| 2422 | * re-registration after a registration failure will be done immediately. |
| 2423 | * Also note that this function should be called within PJSUA mutex. |
| 2424 | */ |
| 2425 | static void schedule_reregistration(pjsua_acc *acc) |
| 2426 | { |
| 2427 | pj_time_val delay; |
| 2428 | |
Nanang Izzuddin | c71bed6 | 2010-05-26 15:04:43 +0000 | [diff] [blame] | 2429 | pj_assert(acc); |
| 2430 | |
| 2431 | /* Validate the account and re-registration feature status */ |
| 2432 | if (!acc->valid || acc->cfg.reg_retry_interval == 0) { |
| 2433 | return; |
| 2434 | } |
Nanang Izzuddin | 6658000 | 2010-04-14 08:12:08 +0000 | [diff] [blame] | 2435 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 2436 | /* If configured, disconnect calls of this account after the first |
| 2437 | * reregistration attempt failed. |
| 2438 | */ |
Nanang Izzuddin | 6658000 | 2010-04-14 08:12:08 +0000 | [diff] [blame] | 2439 | if (acc->cfg.drop_calls_on_reg_fail && acc->auto_rereg.attempt_cnt >= 1) |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 2440 | { |
| 2441 | unsigned i, cnt; |
| 2442 | |
| 2443 | for (i = 0, cnt = 0; i < pjsua_var.ua_cfg.max_calls; ++i) { |
| 2444 | if (pjsua_var.calls[i].acc_id == acc->index) { |
| 2445 | pjsua_call_hangup(i, 0, NULL, NULL); |
| 2446 | ++cnt; |
| 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | if (cnt) { |
| 2451 | PJ_LOG(3, (THIS_FILE, "Disconnecting %d call(s) of account #%d " |
| 2452 | "after reregistration attempt failed", |
| 2453 | cnt, acc->index)); |
| 2454 | } |
| 2455 | } |
| 2456 | |
Nanang Izzuddin | 36dd5b6 | 2010-03-30 11:13:59 +0000 | [diff] [blame] | 2457 | /* Cancel any re-registration timer */ |
| 2458 | pjsua_cancel_timer(&acc->auto_rereg.timer); |
| 2459 | |
| 2460 | /* Update re-registration flag */ |
| 2461 | acc->auto_rereg.active = PJ_TRUE; |
| 2462 | |
| 2463 | /* Set up timer for reregistration */ |
| 2464 | acc->auto_rereg.timer.cb = &auto_rereg_timer_cb; |
| 2465 | acc->auto_rereg.timer.user_data = acc; |
| 2466 | |
| 2467 | /* Reregistration attempt. The first attempt will be done immediately. */ |
| 2468 | delay.sec = acc->auto_rereg.attempt_cnt? acc->cfg.reg_retry_interval : 0; |
| 2469 | delay.msec = 0; |
| 2470 | pjsua_schedule_timer(&acc->auto_rereg.timer, &delay); |
| 2471 | } |
| 2472 | |
| 2473 | |
| 2474 | /* Internal function to perform auto-reregistration on transport |
| 2475 | * connection/disconnection events. |
| 2476 | */ |
| 2477 | void pjsua_acc_on_tp_state_changed(pjsip_transport *tp, |
| 2478 | pjsip_transport_state state, |
| 2479 | const pjsip_transport_state_info *info) |
| 2480 | { |
| 2481 | unsigned i; |
| 2482 | |
| 2483 | PJ_UNUSED_ARG(info); |
| 2484 | |
| 2485 | /* Only care for transport disconnection events */ |
| 2486 | if (state != PJSIP_TP_STATE_DISCONNECTED) |
| 2487 | return; |
| 2488 | |
| 2489 | /* Shutdown this transport, to make sure that the transport manager |
| 2490 | * will create a new transport for reconnection. |
| 2491 | */ |
| 2492 | pjsip_transport_shutdown(tp); |
| 2493 | |
| 2494 | PJSUA_LOCK(); |
| 2495 | |
| 2496 | /* Enumerate accounts using this transport and perform actions |
| 2497 | * based on the transport state. |
| 2498 | */ |
| 2499 | for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2500 | pjsua_acc *acc = &pjsua_var.acc[i]; |
| 2501 | |
| 2502 | /* Skip if this account is not valid OR auto re-registration |
| 2503 | * feature is disabled OR this transport is not used by this account. |
| 2504 | */ |
| 2505 | if (!acc->valid || !acc->cfg.reg_retry_interval || |
| 2506 | tp != acc->auto_rereg.reg_tp) |
| 2507 | { |
| 2508 | continue; |
| 2509 | } |
| 2510 | |
| 2511 | /* Schedule reregistration for this account */ |
| 2512 | schedule_reregistration(acc); |
| 2513 | } |
| 2514 | |
| 2515 | PJSUA_UNLOCK(); |
| 2516 | } |