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