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