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