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