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