Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 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 | */ |
| 67 | static void copy_acc_config(pj_pool_t *pool, |
| 68 | pjsua_acc_config *dst, |
| 69 | const pjsua_acc_config *src) |
| 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 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 94 | * Initialize a new account (after configuration is set). |
| 95 | */ |
| 96 | static pj_status_t initialize_acc(unsigned acc_id) |
| 97 | { |
| 98 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 99 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 100 | pjsip_name_addr *name_addr; |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 101 | pjsip_sip_uri *sip_uri, *sip_reg_uri; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 102 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 103 | unsigned i; |
| 104 | |
| 105 | /* Need to parse local_uri to get the elements: */ |
| 106 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 107 | name_addr = (pjsip_name_addr*) |
| 108 | pjsip_parse_uri(pjsua_var.pool, acc_cfg->id.ptr, |
| 109 | acc_cfg->id.slen, |
| 110 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 111 | if (name_addr == NULL) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 112 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 113 | PJSIP_EINVALIDURI); |
| 114 | return PJSIP_EINVALIDURI; |
| 115 | } |
| 116 | |
| 117 | /* Local URI MUST be a SIP or SIPS: */ |
| 118 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 119 | if (!PJSIP_URI_SCHEME_IS_SIP(name_addr) && |
| 120 | !PJSIP_URI_SCHEME_IS_SIPS(name_addr)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 121 | { |
| 122 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 123 | PJSIP_EINVALIDSCHEME); |
| 124 | return PJSIP_EINVALIDSCHEME; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* Get the SIP URI object: */ |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 129 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(name_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 130 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 131 | |
| 132 | /* Parse registrar URI, if any */ |
| 133 | if (acc_cfg->reg_uri.slen) { |
| 134 | pjsip_uri *reg_uri; |
| 135 | |
| 136 | reg_uri = pjsip_parse_uri(pjsua_var.pool, acc_cfg->reg_uri.ptr, |
| 137 | acc_cfg->reg_uri.slen, 0); |
| 138 | if (reg_uri == NULL) { |
| 139 | pjsua_perror(THIS_FILE, "Invalid registrar URI", |
| 140 | PJSIP_EINVALIDURI); |
| 141 | return PJSIP_EINVALIDURI; |
| 142 | } |
| 143 | |
| 144 | /* Registrar URI MUST be a SIP or SIPS: */ |
| 145 | if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) && |
| 146 | !PJSIP_URI_SCHEME_IS_SIPS(reg_uri)) |
| 147 | { |
| 148 | pjsua_perror(THIS_FILE, "Invalid registar URI", |
| 149 | PJSIP_EINVALIDSCHEME); |
| 150 | return PJSIP_EINVALIDSCHEME; |
| 151 | } |
| 152 | |
| 153 | sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri); |
| 154 | |
| 155 | } else { |
| 156 | sip_reg_uri = NULL; |
| 157 | } |
| 158 | |
| 159 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 160 | /* Save the user and domain part. These will be used when finding an |
| 161 | * account for incoming requests. |
| 162 | */ |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 163 | acc->display = name_addr->display; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 164 | acc->user_part = sip_uri->user; |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 165 | acc->srv_domain = sip_uri->host; |
| 166 | acc->srv_port = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 167 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 168 | if (sip_reg_uri) { |
| 169 | acc->srv_port = sip_reg_uri->port; |
| 170 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 171 | |
| 172 | /* Create Contact header if not present. */ |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 173 | //if (acc_cfg->contact.slen == 0) { |
| 174 | // acc_cfg->contact = acc_cfg->id; |
| 175 | //} |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 176 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 177 | /* Build account route-set from outbound proxies and route set from |
| 178 | * account configuration. |
| 179 | */ |
| 180 | pj_list_init(&acc->route_set); |
| 181 | |
| 182 | for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) { |
| 183 | pj_str_t hname = { "Route", 5}; |
| 184 | pjsip_route_hdr *r; |
| 185 | pj_str_t tmp; |
| 186 | |
| 187 | pj_strdup_with_null(pjsua_var.pool, &tmp, |
| 188 | &pjsua_var.ua_cfg.outbound_proxy[i]); |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 189 | r = (pjsip_route_hdr*) |
| 190 | pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 191 | if (r == NULL) { |
| 192 | pjsua_perror(THIS_FILE, "Invalid outbound proxy URI", |
| 193 | PJSIP_EINVALIDURI); |
| 194 | return PJSIP_EINVALIDURI; |
| 195 | } |
| 196 | pj_list_push_back(&acc->route_set, r); |
| 197 | } |
| 198 | |
| 199 | for (i=0; i<acc_cfg->proxy_cnt; ++i) { |
| 200 | pj_str_t hname = { "Route", 5}; |
| 201 | pjsip_route_hdr *r; |
| 202 | pj_str_t tmp; |
| 203 | |
| 204 | pj_strdup_with_null(pjsua_var.pool, &tmp, &acc_cfg->proxy[i]); |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 205 | r = (pjsip_route_hdr*) |
| 206 | pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, tmp.slen, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 207 | if (r == NULL) { |
| 208 | pjsua_perror(THIS_FILE, "Invalid URI in account route set", |
| 209 | PJ_EINVAL); |
| 210 | return PJ_EINVAL; |
| 211 | } |
| 212 | pj_list_push_back(&acc->route_set, r); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* Concatenate credentials from account config and global config */ |
| 217 | acc->cred_cnt = 0; |
| 218 | for (i=0; i<acc_cfg->cred_count; ++i) { |
| 219 | acc->cred[acc->cred_cnt++] = acc_cfg->cred_info[i]; |
| 220 | } |
| 221 | for (i=0; i<pjsua_var.ua_cfg.cred_count && |
| 222 | acc->cred_cnt < PJ_ARRAY_SIZE(acc->cred); ++i) |
| 223 | { |
| 224 | acc->cred[acc->cred_cnt++] = pjsua_var.ua_cfg.cred_info[i]; |
| 225 | } |
| 226 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 227 | status = pjsua_pres_init_acc(acc_id); |
| 228 | if (status != PJ_SUCCESS) |
| 229 | return status; |
| 230 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 231 | /* Mark account as valid */ |
| 232 | pjsua_var.acc[acc_id].valid = PJ_TRUE; |
| 233 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 234 | /* Insert account ID into account ID array, sorted by priority */ |
| 235 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 236 | if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority < |
| 237 | pjsua_var.acc[acc_id].cfg.priority) |
| 238 | { |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]), |
| 243 | pjsua_var.acc_cnt, i, &acc_id); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 244 | |
| 245 | return PJ_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /* |
| 250 | * Add a new account to pjsua. |
| 251 | */ |
| 252 | PJ_DEF(pj_status_t) pjsua_acc_add( const pjsua_acc_config *cfg, |
| 253 | pj_bool_t is_default, |
| 254 | pjsua_acc_id *p_acc_id) |
| 255 | { |
| 256 | unsigned id; |
| 257 | pj_status_t status; |
| 258 | |
| 259 | PJ_ASSERT_RETURN(pjsua_var.acc_cnt < PJ_ARRAY_SIZE(pjsua_var.acc), |
| 260 | PJ_ETOOMANY); |
| 261 | |
| 262 | /* Must have a transport */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 263 | PJ_ASSERT_RETURN(pjsua_var.tpdata[0].data.ptr != NULL, PJ_EINVALIDOP); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 264 | |
| 265 | PJSUA_LOCK(); |
| 266 | |
| 267 | /* Find empty account id. */ |
| 268 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.acc); ++id) { |
| 269 | if (pjsua_var.acc[id].valid == PJ_FALSE) |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | /* Expect to find a slot */ |
| 274 | PJ_ASSERT_ON_FAIL( id < PJ_ARRAY_SIZE(pjsua_var.acc), |
| 275 | {PJSUA_UNLOCK(); return PJ_EBUG;}); |
| 276 | |
| 277 | /* Copy config */ |
| 278 | copy_acc_config(pjsua_var.pool, &pjsua_var.acc[id].cfg, cfg); |
| 279 | |
| 280 | /* Normalize registration timeout */ |
| 281 | if (pjsua_var.acc[id].cfg.reg_uri.slen && |
| 282 | pjsua_var.acc[id].cfg.reg_timeout == 0) |
| 283 | { |
| 284 | pjsua_var.acc[id].cfg.reg_timeout = PJSUA_REG_INTERVAL; |
| 285 | } |
| 286 | |
| 287 | status = initialize_acc(id); |
| 288 | if (status != PJ_SUCCESS) { |
| 289 | pjsua_perror(THIS_FILE, "Error adding account", status); |
| 290 | PJSUA_UNLOCK(); |
| 291 | return status; |
| 292 | } |
| 293 | |
| 294 | if (is_default) |
| 295 | pjsua_var.default_acc = id; |
| 296 | |
| 297 | if (p_acc_id) |
| 298 | *p_acc_id = id; |
| 299 | |
| 300 | pjsua_var.acc_cnt++; |
| 301 | |
| 302 | PJSUA_UNLOCK(); |
| 303 | |
| 304 | PJ_LOG(4,(THIS_FILE, "Account %.*s added with id %d", |
| 305 | (int)cfg->id.slen, cfg->id.ptr, id)); |
| 306 | |
| 307 | /* If accounts has registration enabled, start registration */ |
| 308 | if (pjsua_var.acc[id].cfg.reg_uri.slen) |
| 309 | pjsua_acc_set_registration(id, PJ_TRUE); |
| 310 | |
| 311 | |
| 312 | return PJ_SUCCESS; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /* |
| 317 | * Add local account |
| 318 | */ |
| 319 | PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid, |
| 320 | pj_bool_t is_default, |
| 321 | pjsua_acc_id *p_acc_id) |
| 322 | { |
| 323 | pjsua_acc_config cfg; |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 324 | pjsua_transport_data *t = &pjsua_var.tpdata[tid]; |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 325 | char uri[PJSIP_MAX_URL_SIZE]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 326 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 327 | /* ID must be valid */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 328 | PJ_ASSERT_RETURN(tid>=0 && tid<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 329 | PJ_EINVAL); |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 330 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 331 | /* Transport must be valid */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 332 | PJ_ASSERT_RETURN(t->data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 333 | |
| 334 | pjsua_acc_config_default(&cfg); |
| 335 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 336 | /* Lower the priority of local account */ |
| 337 | --cfg.priority; |
| 338 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 339 | /* Build URI for the account */ |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 340 | pj_ansi_snprintf(uri, PJSIP_MAX_URL_SIZE, |
| 341 | "<sip:%.*s:%d;transport=%s>", |
| 342 | (int)t->local_name.host.slen, |
| 343 | t->local_name.host.ptr, |
| 344 | t->local_name.port, |
| 345 | pjsip_transport_get_type_name(t->type)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 346 | |
| 347 | cfg.id = pj_str(uri); |
| 348 | |
| 349 | return pjsua_acc_add(&cfg, is_default, p_acc_id); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * Delete account. |
| 355 | */ |
| 356 | PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id) |
| 357 | { |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 358 | unsigned i; |
| 359 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 360 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 361 | PJ_EINVAL); |
| 362 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 363 | |
| 364 | PJSUA_LOCK(); |
| 365 | |
| 366 | /* Delete registration */ |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 367 | if (pjsua_var.acc[acc_id].regc != NULL) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 368 | pjsua_acc_set_registration(acc_id, PJ_FALSE); |
Benny Prijono | e68e99f | 2007-06-06 00:28:10 +0000 | [diff] [blame] | 369 | if (pjsua_var.acc[acc_id].regc) { |
| 370 | pjsip_regc_destroy(pjsua_var.acc[acc_id].regc); |
| 371 | } |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 372 | pjsua_var.acc[acc_id].regc = NULL; |
| 373 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 374 | |
| 375 | /* Delete server presence subscription */ |
| 376 | pjsua_pres_delete_acc(acc_id); |
| 377 | |
| 378 | /* Invalidate */ |
| 379 | pjsua_var.acc[acc_id].valid = PJ_FALSE; |
| 380 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 381 | /* Remove from array */ |
| 382 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 383 | if (pjsua_var.acc_ids[i] == acc_id) |
| 384 | break; |
| 385 | } |
| 386 | if (i != pjsua_var.acc_cnt) { |
| 387 | pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]), |
| 388 | pjsua_var.acc_cnt, i); |
| 389 | --pjsua_var.acc_cnt; |
| 390 | } |
| 391 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 392 | /* Leave the calls intact, as I don't think calls need to |
| 393 | * access account once it's created |
| 394 | */ |
| 395 | |
Benny Prijono | fb2b365 | 2007-06-28 07:15:03 +0000 | [diff] [blame] | 396 | /* Update default account */ |
| 397 | if (pjsua_var.default_acc == acc_id) |
| 398 | pjsua_var.default_acc = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 399 | |
| 400 | PJSUA_UNLOCK(); |
| 401 | |
| 402 | PJ_LOG(4,(THIS_FILE, "Account id %d deleted", acc_id)); |
| 403 | |
| 404 | return PJ_SUCCESS; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* |
| 409 | * Modify account information. |
| 410 | */ |
| 411 | PJ_DEF(pj_status_t) pjsua_acc_modify( pjsua_acc_id acc_id, |
| 412 | const pjsua_acc_config *cfg) |
| 413 | { |
| 414 | PJ_TODO(pjsua_acc_modify); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 415 | PJ_UNUSED_ARG(acc_id); |
| 416 | PJ_UNUSED_ARG(cfg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 417 | return PJ_EINVALIDOP; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /* |
| 422 | * Modify account's presence status to be advertised to remote/presence |
| 423 | * subscribers. |
| 424 | */ |
| 425 | PJ_DEF(pj_status_t) pjsua_acc_set_online_status( pjsua_acc_id acc_id, |
| 426 | pj_bool_t is_online) |
| 427 | { |
| 428 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 429 | PJ_EINVAL); |
| 430 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 431 | |
| 432 | pjsua_var.acc[acc_id].online_status = is_online; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 433 | pj_bzero(&pjsua_var.acc[acc_id].rpid, sizeof(pjrpid_element)); |
| 434 | pjsua_pres_update_acc(acc_id, PJ_FALSE); |
| 435 | return PJ_SUCCESS; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /* |
| 440 | * Set online status with extended information |
| 441 | */ |
| 442 | PJ_DEF(pj_status_t) pjsua_acc_set_online_status2( pjsua_acc_id acc_id, |
| 443 | pj_bool_t is_online, |
| 444 | const pjrpid_element *pr) |
| 445 | { |
| 446 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 447 | PJ_EINVAL); |
| 448 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 449 | |
| 450 | pjsua_var.acc[acc_id].online_status = is_online; |
| 451 | pjrpid_element_dup(pjsua_var.pool, &pjsua_var.acc[acc_id].rpid, pr); |
| 452 | pjsua_pres_update_acc(acc_id, PJ_TRUE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 453 | return PJ_SUCCESS; |
| 454 | } |
| 455 | |
| 456 | |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 457 | /* Update NAT address from the REGISTER response */ |
| 458 | static pj_bool_t acc_check_nat_addr(pjsua_acc *acc, |
| 459 | struct pjsip_regc_cbparam *param) |
| 460 | { |
| 461 | pjsip_transport *tp; |
| 462 | const pj_str_t *via_addr; |
| 463 | int rport; |
| 464 | pjsip_via_hdr *via; |
| 465 | |
| 466 | tp = param->rdata->tp_info.transport; |
| 467 | |
| 468 | /* Only update if account is configured to auto-update */ |
| 469 | if (acc->cfg.auto_update_nat == PJ_FALSE) |
| 470 | return PJ_FALSE; |
| 471 | |
| 472 | /* Only update if registration uses UDP transport */ |
| 473 | if (tp->key.type != PJSIP_TRANSPORT_UDP) |
| 474 | return PJ_FALSE; |
| 475 | |
| 476 | /* Only update if STUN is enabled (for now) */ |
| 477 | if (pjsua_var.ua_cfg.stun_domain.slen == 0 && |
| 478 | pjsua_var.ua_cfg.stun_host.slen == 0) |
| 479 | { |
| 480 | return PJ_FALSE; |
| 481 | } |
| 482 | |
| 483 | /* Get the received and rport info */ |
| 484 | via = param->rdata->msg_info.via; |
| 485 | if (via->rport_param < 1) { |
| 486 | /* Remote doesn't support rport */ |
| 487 | rport = via->sent_by.port; |
| 488 | } else |
| 489 | rport = via->rport_param; |
| 490 | |
| 491 | if (via->recvd_param.slen != 0) |
| 492 | via_addr = &via->recvd_param; |
| 493 | else |
| 494 | via_addr = &via->sent_by.host; |
| 495 | |
| 496 | /* Compare received and rport with transport published address */ |
| 497 | if (tp->local_name.port == rport && |
| 498 | pj_stricmp(&tp->local_name.host, via_addr)==0) |
| 499 | { |
| 500 | /* Address doesn't change */ |
| 501 | return PJ_FALSE; |
| 502 | } |
| 503 | |
| 504 | /* At this point we've detected that the address as seen by registrar. |
| 505 | * has changed. |
| 506 | */ |
| 507 | PJ_LOG(3,(THIS_FILE, "IP address change detected for account %d " |
| 508 | "(%.*s:%d --> %.*s:%d). Updating registration..", |
| 509 | acc->index, |
| 510 | (int)tp->local_name.host.slen, |
| 511 | tp->local_name.host.ptr, |
| 512 | tp->local_name.port, |
| 513 | (int)via_addr->slen, |
| 514 | via_addr->ptr, |
| 515 | rport)); |
| 516 | |
| 517 | /* Unregister current contact */ |
| 518 | pjsua_acc_set_registration(acc->index, PJ_FALSE); |
| 519 | if (acc->regc != NULL) { |
| 520 | pjsip_regc_destroy(acc->regc); |
| 521 | acc->regc = NULL; |
| 522 | } |
| 523 | |
| 524 | /* Update transport address */ |
| 525 | pj_strdup_with_null(tp->pool, &tp->local_name.host, via_addr); |
| 526 | tp->local_name.port = rport; |
| 527 | |
| 528 | /* Perform new registration */ |
| 529 | pjsua_acc_set_registration(acc->index, PJ_TRUE); |
| 530 | |
| 531 | return PJ_TRUE; |
| 532 | } |
| 533 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 534 | /* |
| 535 | * This callback is called by pjsip_regc when outgoing register |
| 536 | * request has completed. |
| 537 | */ |
| 538 | static void regc_cb(struct pjsip_regc_cbparam *param) |
| 539 | { |
| 540 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 541 | pjsua_acc *acc = (pjsua_acc*) param->token; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 542 | |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 543 | if (param->regc != acc->regc) |
| 544 | return; |
| 545 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 546 | PJSUA_LOCK(); |
| 547 | |
| 548 | /* |
| 549 | * Print registration status. |
| 550 | */ |
| 551 | if (param->status!=PJ_SUCCESS) { |
| 552 | pjsua_perror(THIS_FILE, "SIP registration error", |
| 553 | param->status); |
| 554 | pjsip_regc_destroy(acc->regc); |
| 555 | acc->regc = NULL; |
| 556 | |
| 557 | } else if (param->code < 0 || param->code >= 300) { |
| 558 | PJ_LOG(2, (THIS_FILE, "SIP registration failed, status=%d (%.*s)", |
| 559 | param->code, |
| 560 | (int)param->reason.slen, param->reason.ptr)); |
| 561 | pjsip_regc_destroy(acc->regc); |
| 562 | acc->regc = NULL; |
| 563 | |
| 564 | } else if (PJSIP_IS_STATUS_IN_CLASS(param->code, 200)) { |
| 565 | |
| 566 | if (param->expiration < 1) { |
| 567 | pjsip_regc_destroy(acc->regc); |
| 568 | acc->regc = NULL; |
| 569 | PJ_LOG(3,(THIS_FILE, "%s: unregistration success", |
| 570 | pjsua_var.acc[acc->index].cfg.id.ptr)); |
| 571 | } else { |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 572 | /* Check NAT bound address */ |
| 573 | if (acc_check_nat_addr(acc, param)) { |
| 574 | /* Update address, don't notify application yet */ |
| 575 | PJSUA_UNLOCK(); |
| 576 | return; |
| 577 | } |
| 578 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 579 | PJ_LOG(3, (THIS_FILE, |
| 580 | "%s: registration success, status=%d (%.*s), " |
| 581 | "will re-register in %d seconds", |
| 582 | pjsua_var.acc[acc->index].cfg.id.ptr, |
| 583 | param->code, |
| 584 | (int)param->reason.slen, param->reason.ptr, |
| 585 | param->expiration)); |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 586 | |
| 587 | /* Send initial PUBLISH if it is enabled */ |
| 588 | if (acc->cfg.publish_enabled && acc->publish_sess==NULL) |
| 589 | pjsua_pres_init_publish_acc(acc->index); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | } else { |
| 593 | PJ_LOG(4, (THIS_FILE, "SIP registration updated status=%d", param->code)); |
| 594 | } |
| 595 | |
| 596 | acc->reg_last_err = param->status; |
| 597 | acc->reg_last_code = param->code; |
| 598 | |
| 599 | if (pjsua_var.ua_cfg.cb.on_reg_state) |
| 600 | (*pjsua_var.ua_cfg.cb.on_reg_state)(acc->index); |
| 601 | |
| 602 | PJSUA_UNLOCK(); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /* |
| 607 | * Initialize client registration. |
| 608 | */ |
| 609 | static pj_status_t pjsua_regc_init(int acc_id) |
| 610 | { |
| 611 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 612 | pj_str_t contact; |
Benny Prijono | dfb7d48 | 2006-10-18 20:35:14 +0000 | [diff] [blame] | 613 | char contact_buf[1024]; |
Benny Prijono | e1a8bad | 2006-10-13 17:45:38 +0000 | [diff] [blame] | 614 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 615 | pj_status_t status; |
| 616 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 617 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 618 | acc = &pjsua_var.acc[acc_id]; |
| 619 | |
| 620 | if (acc->cfg.reg_uri.slen == 0) { |
| 621 | PJ_LOG(3,(THIS_FILE, "Registrar URI is not specified")); |
| 622 | return PJ_SUCCESS; |
| 623 | } |
| 624 | |
Benny Prijono | e1a8bad | 2006-10-13 17:45:38 +0000 | [diff] [blame] | 625 | /* Destroy existing session, if any */ |
| 626 | if (acc->regc) { |
| 627 | pjsip_regc_destroy(acc->regc); |
| 628 | acc->regc = NULL; |
| 629 | } |
| 630 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 631 | /* initialize SIP registration if registrar is configured */ |
| 632 | |
| 633 | status = pjsip_regc_create( pjsua_var.endpt, |
| 634 | acc, ®c_cb, &acc->regc); |
| 635 | |
| 636 | if (status != PJ_SUCCESS) { |
| 637 | pjsua_perror(THIS_FILE, "Unable to create client registration", |
| 638 | status); |
| 639 | return status; |
| 640 | } |
| 641 | |
Benny Prijono | e1a8bad | 2006-10-13 17:45:38 +0000 | [diff] [blame] | 642 | pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf)); |
| 643 | status = pjsua_acc_create_uac_contact( pool, &contact, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 644 | acc_id, &acc->cfg.reg_uri); |
| 645 | if (status != PJ_SUCCESS) { |
| 646 | pjsua_perror(THIS_FILE, "Unable to generate suitable Contact header" |
| 647 | " for registration", |
| 648 | status); |
| 649 | return status; |
| 650 | } |
| 651 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 652 | status = pjsip_regc_init( acc->regc, |
| 653 | &acc->cfg.reg_uri, |
| 654 | &acc->cfg.id, |
| 655 | &acc->cfg.id, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 656 | 1, &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 657 | acc->cfg.reg_timeout); |
| 658 | if (status != PJ_SUCCESS) { |
| 659 | pjsua_perror(THIS_FILE, |
| 660 | "Client registration initialization error", |
| 661 | status); |
| 662 | return status; |
| 663 | } |
| 664 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 665 | /* If account is locked to specific transport, then set transport to |
| 666 | * the client registration. |
| 667 | */ |
| 668 | if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) { |
| 669 | pjsip_tpselector tp_sel; |
| 670 | |
| 671 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 672 | pjsip_regc_set_transport(acc->regc, &tp_sel); |
| 673 | } |
| 674 | |
| 675 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 676 | /* Set credentials |
| 677 | */ |
| 678 | if (acc->cred_cnt) { |
| 679 | pjsip_regc_set_credentials( acc->regc, acc->cred_cnt, acc->cred); |
| 680 | } |
| 681 | |
| 682 | /* Set route-set |
| 683 | */ |
| 684 | if (!pj_list_empty(&acc->route_set)) { |
| 685 | pjsip_regc_set_route_set( acc->regc, &acc->route_set ); |
| 686 | } |
| 687 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 688 | /* Add other request headers. */ |
| 689 | if (pjsua_var.ua_cfg.user_agent.slen) { |
| 690 | pjsip_hdr hdr_list; |
| 691 | const pj_str_t STR_USER_AGENT = { "User-Agent", 10 }; |
| 692 | pjsip_generic_string_hdr *h; |
| 693 | |
| 694 | pool = pj_pool_create_on_buf(NULL, contact_buf, sizeof(contact_buf)); |
| 695 | pj_list_init(&hdr_list); |
| 696 | |
| 697 | h = pjsip_generic_string_hdr_create(pool, &STR_USER_AGENT, |
| 698 | &pjsua_var.ua_cfg.user_agent); |
| 699 | pj_list_push_back(&hdr_list, (pjsip_hdr*)h); |
| 700 | |
| 701 | pjsip_regc_add_headers(acc->regc, &hdr_list); |
| 702 | } |
| 703 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 704 | return PJ_SUCCESS; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /* |
| 709 | * Update registration or perform unregistration. |
| 710 | */ |
| 711 | PJ_DEF(pj_status_t) pjsua_acc_set_registration( pjsua_acc_id acc_id, |
| 712 | pj_bool_t renew) |
| 713 | { |
| 714 | pj_status_t status = 0; |
| 715 | pjsip_tx_data *tdata = 0; |
| 716 | |
| 717 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 718 | PJ_EINVAL); |
| 719 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 720 | |
| 721 | PJSUA_LOCK(); |
| 722 | |
| 723 | if (renew) { |
| 724 | if (pjsua_var.acc[acc_id].regc == NULL) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 725 | status = pjsua_regc_init(acc_id); |
| 726 | if (status != PJ_SUCCESS) { |
| 727 | pjsua_perror(THIS_FILE, "Unable to create registration", |
| 728 | status); |
| 729 | goto on_return; |
| 730 | } |
| 731 | } |
| 732 | if (!pjsua_var.acc[acc_id].regc) { |
| 733 | status = PJ_EINVALIDOP; |
| 734 | goto on_return; |
| 735 | } |
| 736 | |
| 737 | status = pjsip_regc_register(pjsua_var.acc[acc_id].regc, 1, |
| 738 | &tdata); |
| 739 | |
Benny Prijono | 28f673a | 2007-10-15 07:04:59 +0000 | [diff] [blame^] | 740 | if (0 && status == PJ_SUCCESS && pjsua_var.acc[acc_id].cred_cnt) { |
| 741 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 742 | pjsip_authorization_hdr *h; |
| 743 | char *uri; |
| 744 | int d; |
| 745 | |
| 746 | uri = (char*) pj_pool_alloc(tdata->pool, acc->cfg.reg_uri.slen+10); |
| 747 | d = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri, |
| 748 | uri, acc->cfg.reg_uri.slen+10); |
| 749 | pj_assert(d > 0); |
| 750 | |
| 751 | h = pjsip_authorization_hdr_create(tdata->pool); |
| 752 | h->scheme = pj_str("Digest"); |
| 753 | h->credential.digest.username = acc->cred[0].username; |
| 754 | h->credential.digest.realm = acc->srv_domain; |
| 755 | h->credential.digest.uri = pj_str(uri); |
| 756 | h->credential.digest.algorithm = pj_str("md5"); |
| 757 | |
| 758 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h); |
| 759 | } |
| 760 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 761 | } else { |
| 762 | if (pjsua_var.acc[acc_id].regc == NULL) { |
| 763 | PJ_LOG(3,(THIS_FILE, "Currently not registered")); |
| 764 | status = PJ_EINVALIDOP; |
| 765 | goto on_return; |
| 766 | } |
| 767 | status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata); |
| 768 | } |
| 769 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 770 | if (status == PJ_SUCCESS) { |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 771 | //pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 772 | status = pjsip_regc_send( pjsua_var.acc[acc_id].regc, tdata ); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 773 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 774 | |
| 775 | if (status != PJ_SUCCESS) { |
| 776 | pjsua_perror(THIS_FILE, "Unable to create/send REGISTER", |
| 777 | status); |
| 778 | } else { |
| 779 | PJ_LOG(3,(THIS_FILE, "%s sent", |
| 780 | (renew? "Registration" : "Unregistration"))); |
| 781 | } |
| 782 | |
| 783 | on_return: |
| 784 | PJSUA_UNLOCK(); |
| 785 | return status; |
| 786 | } |
| 787 | |
| 788 | |
| 789 | /* |
| 790 | * Get account information. |
| 791 | */ |
| 792 | PJ_DEF(pj_status_t) pjsua_acc_get_info( pjsua_acc_id acc_id, |
| 793 | pjsua_acc_info *info) |
| 794 | { |
| 795 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 796 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
| 797 | |
| 798 | PJ_ASSERT_RETURN(info != NULL, PJ_EINVAL); |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 799 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 800 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 801 | pj_bzero(info, sizeof(pjsua_acc_info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 802 | |
| 803 | PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
| 804 | PJ_EINVAL); |
| 805 | PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); |
| 806 | |
| 807 | PJSUA_LOCK(); |
| 808 | |
| 809 | if (pjsua_var.acc[acc_id].valid == PJ_FALSE) { |
| 810 | PJSUA_UNLOCK(); |
| 811 | return PJ_EINVALIDOP; |
| 812 | } |
| 813 | |
| 814 | info->id = acc_id; |
| 815 | info->is_default = (pjsua_var.default_acc == acc_id); |
| 816 | info->acc_uri = acc_cfg->id; |
| 817 | info->has_registration = (acc->cfg.reg_uri.slen > 0); |
| 818 | info->online_status = acc->online_status; |
Benny Prijono | 4461c7d | 2007-08-25 13:36:15 +0000 | [diff] [blame] | 819 | pj_memcpy(&info->rpid, &acc->rpid, sizeof(pjrpid_element)); |
| 820 | if (info->rpid.note.slen) |
| 821 | info->online_status_text = info->rpid.note; |
| 822 | else if (info->online_status) |
| 823 | info->online_status_text = pj_str("Online"); |
| 824 | else |
| 825 | info->online_status_text = pj_str("Offline"); |
| 826 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 827 | if (acc->reg_last_err) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 828 | info->status = (pjsip_status_code) acc->reg_last_err; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 829 | pj_strerror(acc->reg_last_err, info->buf_, sizeof(info->buf_)); |
| 830 | info->status_text = pj_str(info->buf_); |
| 831 | } else if (acc->reg_last_code) { |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 832 | if (info->has_registration) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 833 | info->status = (pjsip_status_code) acc->reg_last_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 834 | info->status_text = *pjsip_get_status_text(acc->reg_last_code); |
| 835 | } else { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 836 | info->status = (pjsip_status_code) 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 837 | info->status_text = pj_str("not registered"); |
| 838 | } |
| 839 | } else if (acc->cfg.reg_uri.slen) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 840 | info->status = PJSIP_SC_TRYING; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 841 | info->status_text = pj_str("In Progress"); |
| 842 | } else { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 843 | info->status = (pjsip_status_code) 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 844 | info->status_text = pj_str("does not register"); |
| 845 | } |
| 846 | |
| 847 | if (acc->regc) { |
| 848 | pjsip_regc_info regc_info; |
| 849 | pjsip_regc_get_info(acc->regc, ®c_info); |
| 850 | info->expires = regc_info.next_reg; |
| 851 | } else { |
| 852 | info->expires = -1; |
| 853 | } |
| 854 | |
| 855 | PJSUA_UNLOCK(); |
| 856 | |
| 857 | return PJ_SUCCESS; |
| 858 | |
| 859 | } |
| 860 | |
| 861 | |
| 862 | /* |
| 863 | * Enum accounts all account ids. |
| 864 | */ |
| 865 | PJ_DEF(pj_status_t) pjsua_enum_accs(pjsua_acc_id ids[], |
| 866 | unsigned *count ) |
| 867 | { |
| 868 | unsigned i, c; |
| 869 | |
| 870 | PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL); |
| 871 | |
| 872 | PJSUA_LOCK(); |
| 873 | |
| 874 | for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 875 | if (!pjsua_var.acc[i].valid) |
| 876 | continue; |
| 877 | ids[c] = i; |
| 878 | ++c; |
| 879 | } |
| 880 | |
| 881 | *count = c; |
| 882 | |
| 883 | PJSUA_UNLOCK(); |
| 884 | |
| 885 | return PJ_SUCCESS; |
| 886 | } |
| 887 | |
| 888 | |
| 889 | /* |
| 890 | * Enum accounts info. |
| 891 | */ |
| 892 | PJ_DEF(pj_status_t) pjsua_acc_enum_info( pjsua_acc_info info[], |
| 893 | unsigned *count ) |
| 894 | { |
| 895 | unsigned i, c; |
| 896 | |
| 897 | PJ_ASSERT_RETURN(info && *count, PJ_EINVAL); |
| 898 | |
| 899 | PJSUA_LOCK(); |
| 900 | |
| 901 | for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 902 | if (!pjsua_var.acc[i].valid) |
| 903 | continue; |
| 904 | |
| 905 | pjsua_acc_get_info(i, &info[c]); |
| 906 | ++c; |
| 907 | } |
| 908 | |
| 909 | *count = c; |
| 910 | |
| 911 | PJSUA_UNLOCK(); |
| 912 | |
| 913 | return PJ_SUCCESS; |
| 914 | } |
| 915 | |
| 916 | |
| 917 | /* |
| 918 | * This is an internal function to find the most appropriate account to |
| 919 | * used to reach to the specified URL. |
| 920 | */ |
| 921 | PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_outgoing(const pj_str_t *url) |
| 922 | { |
| 923 | pj_str_t tmp; |
| 924 | pjsip_uri *uri; |
| 925 | pjsip_sip_uri *sip_uri; |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 926 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 927 | |
| 928 | PJSUA_LOCK(); |
| 929 | |
| 930 | PJ_TODO(dont_use_pjsua_pool); |
| 931 | |
| 932 | pj_strdup_with_null(pjsua_var.pool, &tmp, url); |
| 933 | |
| 934 | uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0); |
| 935 | if (!uri) { |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 936 | PJSUA_UNLOCK(); |
| 937 | return pjsua_var.default_acc; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && |
| 941 | !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 942 | { |
| 943 | /* Return the first account with proxy */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 944 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 945 | if (!pjsua_var.acc[i].valid) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 946 | continue; |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 947 | if (!pj_list_empty(&pjsua_var.acc[i].route_set)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 948 | break; |
| 949 | } |
| 950 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 951 | if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 952 | /* Found rather matching account */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 953 | PJSUA_UNLOCK(); |
| 954 | return 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /* Not found, use default account */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 958 | PJSUA_UNLOCK(); |
| 959 | return pjsua_var.default_acc; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 962 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 963 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 964 | /* Find matching domain AND port */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 965 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 966 | unsigned acc_id = pjsua_var.acc_ids[i]; |
| 967 | if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 && |
| 968 | pjsua_var.acc[acc_id].srv_port == sip_uri->port) |
| 969 | { |
| 970 | PJSUA_UNLOCK(); |
| 971 | return acc_id; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 972 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 975 | /* If no match, try to match the domain part only */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 976 | for (i=0; i<pjsua_var.acc_cnt; ++i) { |
| 977 | unsigned acc_id = pjsua_var.acc_ids[i]; |
| 978 | if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0) |
| 979 | { |
| 980 | PJSUA_UNLOCK(); |
| 981 | return acc_id; |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 985 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 986 | /* Still no match, just use default account */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 987 | PJSUA_UNLOCK(); |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 988 | return pjsua_var.default_acc; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
| 992 | /* |
| 993 | * This is an internal function to find the most appropriate account to be |
| 994 | * used to handle incoming calls. |
| 995 | */ |
| 996 | PJ_DEF(pjsua_acc_id) pjsua_acc_find_for_incoming(pjsip_rx_data *rdata) |
| 997 | { |
| 998 | pjsip_uri *uri; |
| 999 | pjsip_sip_uri *sip_uri; |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1000 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1001 | |
Benny Prijono | 371cf0a | 2007-06-19 00:35:37 +0000 | [diff] [blame] | 1002 | /* Check that there's at least one account configured */ |
| 1003 | PJ_ASSERT_RETURN(pjsua_var.acc_cnt!=0, pjsua_var.default_acc); |
| 1004 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1005 | uri = rdata->msg_info.to->uri; |
| 1006 | |
| 1007 | /* Just return default account if To URI is not SIP: */ |
| 1008 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && |
| 1009 | !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 1010 | { |
| 1011 | return pjsua_var.default_acc; |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | PJSUA_LOCK(); |
| 1016 | |
| 1017 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 1018 | |
| 1019 | /* Find account which has matching username and domain. */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1020 | for (i=0; i < pjsua_var.acc_cnt; ++i) { |
| 1021 | unsigned acc_id = pjsua_var.acc_ids[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1022 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1023 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1024 | if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0 && |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1025 | pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1026 | { |
| 1027 | /* Match ! */ |
| 1028 | PJSUA_UNLOCK(); |
| 1029 | return acc_id; |
| 1030 | } |
| 1031 | } |
| 1032 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1033 | /* No matching account, try match domain part only. */ |
| 1034 | for (i=0; i < pjsua_var.acc_cnt; ++i) { |
| 1035 | unsigned acc_id = pjsua_var.acc_ids[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1036 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1037 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1038 | if (acc->valid && pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1039 | /* Match ! */ |
| 1040 | PJSUA_UNLOCK(); |
| 1041 | return acc_id; |
| 1042 | } |
| 1043 | } |
| 1044 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1045 | /* No matching account, try match user part (and transport type) only. */ |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1046 | for (i=0; i < pjsua_var.acc_cnt; ++i) { |
| 1047 | unsigned acc_id = pjsua_var.acc_ids[i]; |
| 1048 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 1049 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1050 | if (acc->valid && pj_stricmp(&acc->user_part, &sip_uri->user)==0) { |
| 1051 | |
| 1052 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 1053 | pjsip_transport_type_e type; |
| 1054 | type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); |
| 1055 | if (type == PJSIP_TRANSPORT_UNSPECIFIED) |
| 1056 | type = PJSIP_TRANSPORT_UDP; |
| 1057 | |
| 1058 | if (pjsua_var.tpdata[acc->cfg.transport_id].type != type) |
| 1059 | continue; |
| 1060 | } |
| 1061 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 1062 | /* Match ! */ |
| 1063 | PJSUA_UNLOCK(); |
| 1064 | return acc_id; |
| 1065 | } |
| 1066 | } |
| 1067 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1068 | /* Still no match, use default account */ |
| 1069 | PJSUA_UNLOCK(); |
| 1070 | return pjsua_var.default_acc; |
| 1071 | } |
| 1072 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1073 | |
Benny Prijono | fff245c | 2007-04-02 11:44:47 +0000 | [diff] [blame] | 1074 | /* |
| 1075 | * Create arbitrary requests for this account. |
| 1076 | */ |
| 1077 | PJ_DEF(pj_status_t) pjsua_acc_create_request(pjsua_acc_id acc_id, |
| 1078 | const pjsip_method *method, |
| 1079 | const pj_str_t *target, |
| 1080 | pjsip_tx_data **p_tdata) |
| 1081 | { |
| 1082 | pjsip_tx_data *tdata; |
| 1083 | pjsua_acc *acc; |
| 1084 | pjsip_route_hdr *r; |
| 1085 | pj_status_t status; |
| 1086 | |
| 1087 | PJ_ASSERT_RETURN(method && target && p_tdata, PJ_EINVAL); |
| 1088 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
| 1089 | |
| 1090 | acc = &pjsua_var.acc[acc_id]; |
| 1091 | |
| 1092 | status = pjsip_endpt_create_request(pjsua_var.endpt, method, target, |
| 1093 | &acc->cfg.id, target, |
| 1094 | NULL, NULL, -1, NULL, &tdata); |
| 1095 | if (status != PJ_SUCCESS) { |
| 1096 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 1097 | return status; |
| 1098 | } |
| 1099 | |
| 1100 | /* Copy routeset */ |
| 1101 | r = acc->route_set.next; |
| 1102 | while (r != &acc->route_set) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1103 | pjsip_msg_add_hdr(tdata->msg, |
| 1104 | (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, r)); |
Benny Prijono | fff245c | 2007-04-02 11:44:47 +0000 | [diff] [blame] | 1105 | r = r->next; |
| 1106 | } |
| 1107 | |
| 1108 | /* Done */ |
| 1109 | *p_tdata = tdata; |
| 1110 | return PJ_SUCCESS; |
| 1111 | } |
| 1112 | |
| 1113 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1114 | PJ_DEF(pj_status_t) pjsua_acc_create_uac_contact( pj_pool_t *pool, |
| 1115 | pj_str_t *contact, |
| 1116 | pjsua_acc_id acc_id, |
| 1117 | const pj_str_t *suri) |
| 1118 | { |
| 1119 | pjsua_acc *acc; |
| 1120 | pjsip_sip_uri *sip_uri; |
| 1121 | pj_status_t status; |
| 1122 | pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED; |
| 1123 | pj_str_t local_addr; |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1124 | pjsip_tpselector tp_sel; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1125 | unsigned flag; |
| 1126 | int secure; |
| 1127 | int local_port; |
| 1128 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1129 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1130 | acc = &pjsua_var.acc[acc_id]; |
| 1131 | |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 1132 | /* If force_contact is configured, then use use it */ |
| 1133 | if (acc->cfg.force_contact.slen) { |
| 1134 | *contact = acc->cfg.force_contact; |
| 1135 | return PJ_SUCCESS; |
| 1136 | } |
| 1137 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1138 | /* If route-set is configured for the account, then URI is the |
| 1139 | * first entry of the route-set. |
| 1140 | */ |
| 1141 | if (!pj_list_empty(&acc->route_set)) { |
Benny Prijono | 9c1528f | 2007-02-10 19:22:25 +0000 | [diff] [blame] | 1142 | sip_uri = (pjsip_sip_uri*) |
| 1143 | pjsip_uri_get_uri(acc->route_set.next->name_addr.uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1144 | } else { |
| 1145 | pj_str_t tmp; |
| 1146 | pjsip_uri *uri; |
| 1147 | |
| 1148 | pj_strdup_with_null(pool, &tmp, suri); |
| 1149 | |
| 1150 | uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0); |
| 1151 | if (uri == NULL) |
| 1152 | return PJSIP_EINVALIDURI; |
| 1153 | |
| 1154 | /* For non-SIP scheme, route set should be configured */ |
| 1155 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 1156 | return PJSIP_EINVALIDREQURI; |
| 1157 | |
Benny Prijono | 8c7a617 | 2007-02-18 21:17:46 +0000 | [diff] [blame] | 1158 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* Get transport type of the URI */ |
| 1162 | if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) |
| 1163 | tp_type = PJSIP_TRANSPORT_TLS; |
| 1164 | else if (sip_uri->transport_param.slen == 0) { |
| 1165 | tp_type = PJSIP_TRANSPORT_UDP; |
| 1166 | } else |
| 1167 | tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); |
| 1168 | |
| 1169 | if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED) |
| 1170 | return PJSIP_EUNSUPTRANSPORT; |
| 1171 | |
| 1172 | flag = pjsip_transport_get_flag_from_type(tp_type); |
| 1173 | secure = (flag & PJSIP_TRANSPORT_SECURE) != 0; |
| 1174 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1175 | /* Init transport selector. */ |
| 1176 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 1177 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1178 | /* Get local address suitable to send request from */ |
| 1179 | 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] | 1180 | pool, tp_type, &tp_sel, |
| 1181 | &local_addr, &local_port); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1182 | if (status != PJ_SUCCESS) |
| 1183 | return status; |
| 1184 | |
| 1185 | /* Create the contact header */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1186 | contact->ptr = (char*)pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1187 | contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE, |
| 1188 | "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>", |
| 1189 | (int)acc->display.slen, |
| 1190 | acc->display.ptr, |
| 1191 | (acc->display.slen?" " : ""), |
Benny Prijono | 8058a62 | 2007-06-08 04:37:05 +0000 | [diff] [blame] | 1192 | (secure ? PJSUA_SECURE_SCHEME : "sip"), |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1193 | (int)acc->user_part.slen, |
| 1194 | acc->user_part.ptr, |
| 1195 | (acc->user_part.slen?"@":""), |
| 1196 | (int)local_addr.slen, |
| 1197 | local_addr.ptr, |
| 1198 | local_port, |
| 1199 | pjsip_transport_get_type_name(tp_type)); |
| 1200 | |
| 1201 | return PJ_SUCCESS; |
| 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | PJ_DEF(pj_status_t) pjsua_acc_create_uas_contact( pj_pool_t *pool, |
| 1207 | pj_str_t *contact, |
| 1208 | pjsua_acc_id acc_id, |
| 1209 | pjsip_rx_data *rdata ) |
| 1210 | { |
| 1211 | /* |
| 1212 | * Section 12.1.1, paragraph about using SIPS URI in Contact. |
| 1213 | * If the request that initiated the dialog contained a SIPS URI |
| 1214 | * in the Request-URI or in the top Record-Route header field value, |
| 1215 | * if there was any, or the Contact header field if there was no |
| 1216 | * Record-Route header field, the Contact header field in the response |
| 1217 | * MUST be a SIPS URI. |
| 1218 | */ |
| 1219 | pjsua_acc *acc; |
| 1220 | pjsip_sip_uri *sip_uri; |
| 1221 | pj_status_t status; |
| 1222 | pjsip_transport_type_e tp_type = PJSIP_TRANSPORT_UNSPECIFIED; |
| 1223 | pj_str_t local_addr; |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1224 | pjsip_tpselector tp_sel; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1225 | unsigned flag; |
| 1226 | int secure; |
| 1227 | int local_port; |
| 1228 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1229 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1230 | acc = &pjsua_var.acc[acc_id]; |
| 1231 | |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 1232 | /* If force_contact is configured, then use use it */ |
| 1233 | if (acc->cfg.force_contact.slen) { |
| 1234 | *contact = acc->cfg.force_contact; |
| 1235 | return PJ_SUCCESS; |
| 1236 | } |
| 1237 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1238 | /* If Record-Route is present, then URI is the top Record-Route. */ |
| 1239 | if (rdata->msg_info.record_route) { |
Benny Prijono | 9c1528f | 2007-02-10 19:22:25 +0000 | [diff] [blame] | 1240 | sip_uri = (pjsip_sip_uri*) |
| 1241 | pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1242 | } else { |
| 1243 | pjsip_contact_hdr *h_contact; |
| 1244 | pjsip_uri *uri = NULL; |
| 1245 | |
| 1246 | /* Otherwise URI is Contact URI */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1247 | h_contact = (pjsip_contact_hdr*) |
| 1248 | pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1249 | NULL); |
| 1250 | if (h_contact) |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1251 | uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1252 | |
| 1253 | |
| 1254 | /* Or if Contact URI is not present, take the remote URI from |
| 1255 | * the From URI. |
| 1256 | */ |
| 1257 | if (uri == NULL) |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1258 | uri = (pjsip_uri*) pjsip_uri_get_uri(rdata->msg_info.from->uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1259 | |
| 1260 | |
| 1261 | /* Can only do sip/sips scheme at present. */ |
| 1262 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 1263 | return PJSIP_EINVALIDREQURI; |
| 1264 | |
Benny Prijono | 8c7a617 | 2007-02-18 21:17:46 +0000 | [diff] [blame] | 1265 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /* Get transport type of the URI */ |
| 1269 | if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) |
| 1270 | tp_type = PJSIP_TRANSPORT_TLS; |
| 1271 | else if (sip_uri->transport_param.slen == 0) { |
| 1272 | tp_type = PJSIP_TRANSPORT_UDP; |
| 1273 | } else |
| 1274 | tp_type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); |
| 1275 | |
| 1276 | if (tp_type == PJSIP_TRANSPORT_UNSPECIFIED) |
| 1277 | return PJSIP_EUNSUPTRANSPORT; |
| 1278 | |
| 1279 | flag = pjsip_transport_get_flag_from_type(tp_type); |
| 1280 | secure = (flag & PJSIP_TRANSPORT_SECURE) != 0; |
| 1281 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1282 | /* Init transport selector. */ |
| 1283 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 1284 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1285 | /* Get local address suitable to send request from */ |
| 1286 | 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] | 1287 | pool, tp_type, &tp_sel, |
| 1288 | &local_addr, &local_port); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1289 | if (status != PJ_SUCCESS) |
| 1290 | return status; |
| 1291 | |
| 1292 | /* Create the contact header */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1293 | contact->ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1294 | contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE, |
| 1295 | "%.*s%s<%s:%.*s%s%.*s:%d;transport=%s>", |
| 1296 | (int)acc->display.slen, |
| 1297 | acc->display.ptr, |
| 1298 | (acc->display.slen?" " : ""), |
Benny Prijono | 8058a62 | 2007-06-08 04:37:05 +0000 | [diff] [blame] | 1299 | (secure ? PJSUA_SECURE_SCHEME : "sip"), |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1300 | (int)acc->user_part.slen, |
| 1301 | acc->user_part.ptr, |
| 1302 | (acc->user_part.slen?"@":""), |
| 1303 | (int)local_addr.slen, |
| 1304 | local_addr.ptr, |
| 1305 | local_port, |
| 1306 | pjsip_transport_get_type_name(tp_type)); |
| 1307 | |
| 1308 | return PJ_SUCCESS; |
| 1309 | } |
| 1310 | |
| 1311 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1312 | PJ_DEF(pj_status_t) pjsua_acc_set_transport( pjsua_acc_id acc_id, |
| 1313 | pjsua_transport_id tp_id) |
| 1314 | { |
| 1315 | pjsua_acc *acc; |
| 1316 | |
| 1317 | PJ_ASSERT_RETURN(pjsua_acc_is_valid(acc_id), PJ_EINVAL); |
| 1318 | acc = &pjsua_var.acc[acc_id]; |
| 1319 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1320 | 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] | 1321 | PJ_EINVAL); |
| 1322 | |
| 1323 | acc->cfg.transport_id = tp_id; |
| 1324 | |
| 1325 | return PJ_SUCCESS; |
| 1326 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1327 | |