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