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