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