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