Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +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 | 268ca61 | 2006-02-07 12:34:11 +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 | */ |
Benny Prijono | 4f9f64e | 2006-02-27 00:00:30 +0000 | [diff] [blame] | 19 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua_internal.h> |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 21 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 22 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 23 | #define THIS_FILE "pjsua_core.c" |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 24 | |
| 25 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 26 | /* PJSUA application instance. */ |
| 27 | struct pjsua_data pjsua_var; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 28 | |
| 29 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 30 | /* Display error */ |
| 31 | PJ_DEF(void) pjsua_perror( const char *sender, const char *title, |
| 32 | pj_status_t status) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 33 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 34 | char errmsg[PJ_ERR_MSG_SIZE]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 35 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 36 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 37 | PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status)); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 41 | static void init_data() |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 42 | { |
| 43 | unsigned i; |
| 44 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 45 | pj_bzero(&pjsua_var, sizeof(pjsua_var)); |
| 46 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 47 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) |
| 48 | pjsua_var.acc[i].index = i; |
| 49 | |
| 50 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i) |
| 51 | pjsua_var.tpdata[i].index = i; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 52 | |
| 53 | pjsua_var.stun_status = PJ_EUNKNOWN; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 54 | } |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 55 | |
| 56 | |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 57 | PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg) |
| 58 | { |
| 59 | pj_bzero(cfg, sizeof(*cfg)); |
| 60 | |
| 61 | cfg->msg_logging = PJ_TRUE; |
| 62 | cfg->level = 5; |
| 63 | cfg->console_level = 4; |
| 64 | cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME | |
| 65 | PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE; |
| 66 | } |
| 67 | |
| 68 | PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool, |
| 69 | pjsua_logging_config *dst, |
| 70 | const pjsua_logging_config *src) |
| 71 | { |
| 72 | pj_memcpy(dst, src, sizeof(*src)); |
| 73 | pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename); |
| 74 | } |
| 75 | |
| 76 | PJ_DEF(void) pjsua_config_default(pjsua_config *cfg) |
| 77 | { |
| 78 | pj_bzero(cfg, sizeof(*cfg)); |
| 79 | |
| 80 | cfg->max_calls = 4; |
| 81 | cfg->thread_cnt = 1; |
| 82 | } |
| 83 | |
| 84 | PJ_DEF(void) pjsip_cred_dup( pj_pool_t *pool, |
| 85 | pjsip_cred_info *dst, |
| 86 | const pjsip_cred_info *src) |
| 87 | { |
| 88 | pj_strdup_with_null(pool, &dst->realm, &src->realm); |
| 89 | pj_strdup_with_null(pool, &dst->scheme, &src->scheme); |
| 90 | pj_strdup_with_null(pool, &dst->username, &src->username); |
| 91 | pj_strdup_with_null(pool, &dst->data, &src->data); |
| 92 | } |
| 93 | |
| 94 | PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool, |
| 95 | pjsua_config *dst, |
| 96 | const pjsua_config *src) |
| 97 | { |
| 98 | unsigned i; |
| 99 | |
| 100 | pj_memcpy(dst, src, sizeof(*src)); |
| 101 | |
| 102 | for (i=0; i<src->outbound_proxy_cnt; ++i) { |
| 103 | pj_strdup_with_null(pool, &dst->outbound_proxy[i], |
| 104 | &src->outbound_proxy[i]); |
| 105 | } |
| 106 | |
| 107 | for (i=0; i<src->cred_count; ++i) { |
| 108 | pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]); |
| 109 | } |
| 110 | |
| 111 | pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent); |
| 112 | pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain); |
| 113 | pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host); |
| 114 | pj_strdup_with_null(pool, &dst->stun_relay_host, &src->stun_relay_host); |
| 115 | } |
| 116 | |
| 117 | PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data) |
| 118 | { |
| 119 | pj_bzero(msg_data, sizeof(*msg_data)); |
| 120 | pj_list_init(&msg_data->hdr_list); |
| 121 | } |
| 122 | |
| 123 | PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg) |
| 124 | { |
| 125 | pj_bzero(cfg, sizeof(*cfg)); |
| 126 | pjsip_tls_setting_default(&cfg->tls_setting); |
| 127 | } |
| 128 | |
| 129 | PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool, |
| 130 | pjsua_transport_config *dst, |
| 131 | const pjsua_transport_config *src) |
| 132 | { |
| 133 | PJ_UNUSED_ARG(pool); |
| 134 | pj_memcpy(dst, src, sizeof(*src)); |
| 135 | } |
| 136 | |
| 137 | PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg) |
| 138 | { |
| 139 | pj_bzero(cfg, sizeof(*cfg)); |
| 140 | |
| 141 | cfg->reg_timeout = PJSUA_REG_INTERVAL; |
| 142 | cfg->transport_id = PJSUA_INVALID_ID; |
Benny Prijono | 15b0230 | 2007-09-27 14:07:07 +0000 | [diff] [blame] | 143 | cfg->auto_update_nat = PJ_TRUE; |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg) |
| 147 | { |
| 148 | pj_bzero(cfg, sizeof(*cfg)); |
| 149 | } |
| 150 | |
| 151 | PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg) |
| 152 | { |
| 153 | pj_bzero(cfg, sizeof(*cfg)); |
| 154 | |
| 155 | cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE; |
| 156 | cfg->max_media_ports = 32; |
| 157 | cfg->has_ioqueue = PJ_TRUE; |
| 158 | cfg->thread_cnt = 1; |
| 159 | cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY; |
| 160 | cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE; |
| 161 | cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN; |
| 162 | cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1; |
| 163 | } |
| 164 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 165 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 166 | /***************************************************************************** |
| 167 | * This is a very simple PJSIP module, whose sole purpose is to display |
| 168 | * incoming and outgoing messages to log. This module will have priority |
| 169 | * higher than transport layer, which means: |
| 170 | * |
| 171 | * - incoming messages will come to this module first before reaching |
| 172 | * transaction layer. |
| 173 | * |
| 174 | * - outgoing messages will come to this module last, after the message |
| 175 | * has been 'printed' to contiguous buffer by transport layer and |
| 176 | * appropriate transport instance has been decided for this message. |
| 177 | * |
| 178 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 179 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 180 | /* Notification on incoming messages */ |
| 181 | static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata) |
| 182 | { |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 183 | PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n" |
| 184 | "%.*s\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 185 | "--end msg--", |
| 186 | rdata->msg_info.len, |
| 187 | pjsip_rx_data_get_info(rdata), |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 188 | rdata->tp_info.transport->type_name, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 189 | rdata->pkt_info.src_name, |
| 190 | rdata->pkt_info.src_port, |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 191 | (int)rdata->msg_info.len, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 192 | rdata->msg_info.msg_buf)); |
| 193 | |
| 194 | /* Always return false, otherwise messages will not get processed! */ |
| 195 | return PJ_FALSE; |
| 196 | } |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 197 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 198 | /* Notification on outgoing messages */ |
| 199 | static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata) |
| 200 | { |
| 201 | |
| 202 | /* Important note: |
| 203 | * tp_info field is only valid after outgoing messages has passed |
| 204 | * transport layer. So don't try to access tp_info when the module |
| 205 | * has lower priority than transport layer. |
| 206 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 207 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 208 | PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n" |
| 209 | "%.*s\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 210 | "--end msg--", |
| 211 | (tdata->buf.cur - tdata->buf.start), |
| 212 | pjsip_tx_data_get_info(tdata), |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 213 | tdata->tp_info.transport->type_name, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 214 | tdata->tp_info.dst_name, |
| 215 | tdata->tp_info.dst_port, |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 216 | (int)(tdata->buf.cur - tdata->buf.start), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 217 | tdata->buf.start)); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 218 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 219 | /* Always return success, otherwise message will not get sent! */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 220 | return PJ_SUCCESS; |
| 221 | } |
| 222 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 223 | /* The module instance. */ |
| 224 | static pjsip_module pjsua_msg_logger = |
| 225 | { |
| 226 | NULL, NULL, /* prev, next. */ |
| 227 | { "mod-pjsua-log", 13 }, /* Name. */ |
| 228 | -1, /* Id */ |
| 229 | PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */ |
| 230 | NULL, /* load() */ |
| 231 | NULL, /* start() */ |
| 232 | NULL, /* stop() */ |
| 233 | NULL, /* unload() */ |
| 234 | &logging_on_rx_msg, /* on_rx_request() */ |
| 235 | &logging_on_rx_msg, /* on_rx_response() */ |
| 236 | &logging_on_tx_msg, /* on_tx_request. */ |
| 237 | &logging_on_tx_msg, /* on_tx_response() */ |
| 238 | NULL, /* on_tsx_state() */ |
| 239 | |
| 240 | }; |
| 241 | |
| 242 | |
| 243 | /***************************************************************************** |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 244 | * Another simple module to handle incoming OPTIONS request |
| 245 | */ |
| 246 | |
| 247 | /* Notification on incoming request */ |
| 248 | static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata) |
| 249 | { |
| 250 | pjsip_tx_data *tdata; |
| 251 | pjsip_response_addr res_addr; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 252 | pjmedia_sock_info skinfo; |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 253 | pjmedia_sdp_session *sdp; |
| 254 | const pjsip_hdr *cap_hdr; |
| 255 | pj_status_t status; |
| 256 | |
| 257 | /* Only want to handle OPTIONS requests */ |
| 258 | if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 259 | pjsip_get_options_method()) != 0) |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 260 | { |
| 261 | return PJ_FALSE; |
| 262 | } |
| 263 | |
| 264 | /* Create basic response. */ |
| 265 | status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL, |
| 266 | &tdata); |
| 267 | if (status != PJ_SUCCESS) { |
| 268 | pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status); |
| 269 | return PJ_TRUE; |
| 270 | } |
| 271 | |
| 272 | /* Add Allow header */ |
| 273 | cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL); |
| 274 | if (cap_hdr) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 275 | pjsip_msg_add_hdr(tdata->msg, |
| 276 | (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Add Accept header */ |
| 280 | cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL); |
| 281 | if (cap_hdr) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 282 | pjsip_msg_add_hdr(tdata->msg, |
| 283 | (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Add Supported header */ |
| 287 | cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL); |
| 288 | if (cap_hdr) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 289 | pjsip_msg_add_hdr(tdata->msg, |
| 290 | (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* Add Allow-Events header from the evsub module */ |
| 294 | cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL); |
| 295 | if (cap_hdr) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 296 | pjsip_msg_add_hdr(tdata->msg, |
| 297 | (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* Add User-Agent header */ |
| 301 | if (pjsua_var.ua_cfg.user_agent.slen) { |
| 302 | const pj_str_t USER_AGENT = { "User-Agent", 10}; |
| 303 | pjsip_hdr *h; |
| 304 | |
| 305 | h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, |
| 306 | &USER_AGENT, |
| 307 | &pjsua_var.ua_cfg.user_agent); |
| 308 | pjsip_msg_add_hdr(tdata->msg, h); |
| 309 | } |
| 310 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 311 | /* Get media socket info */ |
| 312 | pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &skinfo); |
| 313 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 314 | /* Add SDP body, using call0's RTP address */ |
| 315 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1, |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 316 | &skinfo, &sdp); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 317 | if (status == PJ_SUCCESS) { |
| 318 | pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body); |
| 319 | } |
| 320 | |
| 321 | /* Send response statelessly */ |
| 322 | pjsip_get_response_addr(tdata->pool, rdata, &res_addr); |
| 323 | status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL); |
| 324 | if (status != PJ_SUCCESS) |
| 325 | pjsip_tx_data_dec_ref(tdata); |
| 326 | |
| 327 | return PJ_TRUE; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /* The module instance. */ |
| 332 | static pjsip_module pjsua_options_handler = |
| 333 | { |
| 334 | NULL, NULL, /* prev, next. */ |
| 335 | { "mod-pjsua-options", 17 }, /* Name. */ |
| 336 | -1, /* Id */ |
| 337 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 338 | NULL, /* load() */ |
| 339 | NULL, /* start() */ |
| 340 | NULL, /* stop() */ |
| 341 | NULL, /* unload() */ |
| 342 | &options_on_rx_request, /* on_rx_request() */ |
| 343 | NULL, /* on_rx_response() */ |
| 344 | NULL, /* on_tx_request. */ |
| 345 | NULL, /* on_tx_response() */ |
| 346 | NULL, /* on_tsx_state() */ |
| 347 | |
| 348 | }; |
| 349 | |
| 350 | |
| 351 | /***************************************************************************** |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 352 | * These two functions are the main callbacks registered to PJSIP stack |
| 353 | * to receive SIP request and response messages that are outside any |
| 354 | * dialogs and any transactions. |
| 355 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Handler for receiving incoming requests. |
| 359 | * |
| 360 | * This handler serves multiple purposes: |
| 361 | * - it receives requests outside dialogs. |
| 362 | * - it receives requests inside dialogs, when the requests are |
| 363 | * unhandled by other dialog usages. Example of these |
| 364 | * requests are: MESSAGE. |
| 365 | */ |
| 366 | static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata) |
| 367 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 368 | pj_bool_t processed = PJ_FALSE; |
| 369 | |
| 370 | PJSUA_LOCK(); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 371 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 372 | if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 373 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 374 | processed = pjsua_call_on_incoming(rdata); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 377 | PJSUA_UNLOCK(); |
| 378 | |
| 379 | return processed; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | |
| 383 | /* |
| 384 | * Handler for receiving incoming responses. |
| 385 | * |
| 386 | * This handler serves multiple purposes: |
| 387 | * - it receives strayed responses (i.e. outside any dialog and |
| 388 | * outside any transactions). |
| 389 | * - it receives responses coming to a transaction, when pjsua |
| 390 | * module is set as transaction user for the transaction. |
| 391 | * - it receives responses inside a dialog, when these responses |
| 392 | * are unhandled by other dialog usages. |
| 393 | */ |
| 394 | static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata) |
| 395 | { |
| 396 | PJ_UNUSED_ARG(rdata); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 397 | return PJ_FALSE; |
| 398 | } |
| 399 | |
| 400 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 401 | /***************************************************************************** |
| 402 | * Logging. |
| 403 | */ |
| 404 | |
| 405 | /* Log callback */ |
| 406 | static void log_writer(int level, const char *buffer, int len) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 407 | { |
Benny Prijono | 572d485 | 2006-11-23 21:50:02 +0000 | [diff] [blame] | 408 | /* Write to file, stdout or application callback. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 409 | |
| 410 | if (pjsua_var.log_file) { |
| 411 | pj_ssize_t size = len; |
| 412 | pj_file_write(pjsua_var.log_file, buffer, &size); |
Benny Prijono | 980ca0a | 2007-04-03 17:55:08 +0000 | [diff] [blame] | 413 | /* This will slow things down considerably! Don't do it! |
| 414 | pj_file_flush(pjsua_var.log_file); |
| 415 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Benny Prijono | 572d485 | 2006-11-23 21:50:02 +0000 | [diff] [blame] | 418 | if (level <= (int)pjsua_var.log_cfg.console_level) { |
| 419 | if (pjsua_var.log_cfg.cb) |
| 420 | (*pjsua_var.log_cfg.cb)(level, buffer, len); |
| 421 | else |
| 422 | pj_log_write(level, buffer, len); |
| 423 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | /* |
| 428 | * Application can call this function at any time (after pjsua_create(), of |
| 429 | * course) to change logging settings. |
| 430 | */ |
| 431 | PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg) |
| 432 | { |
| 433 | pj_status_t status; |
| 434 | |
| 435 | /* Save config. */ |
| 436 | pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg); |
| 437 | |
| 438 | /* Redirect log function to ours */ |
| 439 | pj_log_set_log_func( &log_writer ); |
| 440 | |
Benny Prijono | 9cb09a2 | 2007-08-30 09:35:10 +0000 | [diff] [blame] | 441 | /* Set decor */ |
| 442 | pj_log_set_decor(pjsua_var.log_cfg.decor); |
| 443 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 444 | /* Close existing file, if any */ |
| 445 | if (pjsua_var.log_file) { |
| 446 | pj_file_close(pjsua_var.log_file); |
| 447 | pjsua_var.log_file = NULL; |
| 448 | } |
| 449 | |
| 450 | /* If output log file is desired, create the file: */ |
| 451 | if (pjsua_var.log_cfg.log_filename.slen) { |
| 452 | |
| 453 | status = pj_file_open(pjsua_var.pool, |
| 454 | pjsua_var.log_cfg.log_filename.ptr, |
| 455 | PJ_O_WRONLY, |
| 456 | &pjsua_var.log_file); |
| 457 | |
| 458 | if (status != PJ_SUCCESS) { |
| 459 | pjsua_perror(THIS_FILE, "Error creating log file", status); |
| 460 | return status; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* Unregister msg logging if it's previously registered */ |
| 465 | if (pjsua_msg_logger.id >= 0) { |
| 466 | pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger); |
| 467 | pjsua_msg_logger.id = -1; |
| 468 | } |
| 469 | |
| 470 | /* Enable SIP message logging */ |
| 471 | if (pjsua_var.log_cfg.msg_logging) |
| 472 | pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger); |
| 473 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 474 | return PJ_SUCCESS; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /***************************************************************************** |
| 479 | * PJSUA Base API. |
| 480 | */ |
| 481 | |
| 482 | /* Worker thread function. */ |
| 483 | static int worker_thread(void *arg) |
| 484 | { |
| 485 | enum { TIMEOUT = 10 }; |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 486 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 487 | PJ_UNUSED_ARG(arg); |
| 488 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 489 | while (!pjsua_var.thread_quit_flag) { |
| 490 | int count; |
| 491 | |
| 492 | count = pjsua_handle_events(TIMEOUT); |
| 493 | if (count < 0) |
| 494 | pj_thread_sleep(TIMEOUT); |
| 495 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 500 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 501 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 502 | * Instantiate pjsua application. |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 503 | */ |
| 504 | PJ_DEF(pj_status_t) pjsua_create(void) |
| 505 | { |
| 506 | pj_status_t status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 507 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 508 | /* Init pjsua data */ |
| 509 | init_data(); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 510 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 511 | /* Set default logging settings */ |
| 512 | pjsua_logging_config_default(&pjsua_var.log_cfg); |
| 513 | |
| 514 | /* Init PJLIB: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 515 | status = pj_init(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 516 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 517 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 518 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 519 | /* Init PJLIB-UTIL: */ |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 520 | status = pjlib_util_init(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 521 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 522 | |
Benny Prijono | ec92134 | 2007-03-24 13:00:30 +0000 | [diff] [blame] | 523 | /* Init PJNATH */ |
| 524 | status = pjnath_init(); |
| 525 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 526 | |
Benny Prijono | 094d3ad | 2006-11-21 08:41:00 +0000 | [diff] [blame] | 527 | /* Set default sound device ID */ |
| 528 | pjsua_var.cap_dev = pjsua_var.play_dev = -1; |
| 529 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 530 | /* Init caching pool. */ |
Benny Prijono | 106f5b7 | 2007-08-30 13:49:33 +0000 | [diff] [blame] | 531 | pj_caching_pool_init(&pjsua_var.cp, NULL, 0); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 532 | |
| 533 | /* Create memory pool for application. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 534 | pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000); |
| 535 | |
| 536 | PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 537 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 538 | /* Create mutex */ |
| 539 | status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua", |
| 540 | &pjsua_var.mutex); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 541 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 542 | pjsua_perror(THIS_FILE, "Unable to create mutex", status); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 543 | return status; |
| 544 | } |
| 545 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 546 | /* Must create SIP endpoint to initialize SIP parser. The parser |
| 547 | * is needed for example when application needs to call pjsua_verify_url(). |
Benny Prijono | b8c2518 | 2006-04-29 08:31:09 +0000 | [diff] [blame] | 548 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 549 | status = pjsip_endpt_create(&pjsua_var.cp.factory, |
| 550 | pj_gethostname()->ptr, |
| 551 | &pjsua_var.endpt); |
| 552 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 3987915 | 2006-02-23 02:09:10 +0000 | [diff] [blame] | 553 | |
| 554 | |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 555 | return PJ_SUCCESS; |
| 556 | } |
| 557 | |
| 558 | |
| 559 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 560 | * Initialize pjsua with the specified settings. All the settings are |
| 561 | * optional, and the default values will be used when the config is not |
| 562 | * specified. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 563 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 564 | PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg, |
| 565 | const pjsua_logging_config *log_cfg, |
| 566 | const pjsua_media_config *media_cfg) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 567 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 568 | pjsua_config default_cfg; |
| 569 | pjsua_media_config default_media_cfg; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 570 | const pj_str_t STR_OPTIONS = { "OPTIONS", 7 }; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 571 | pj_status_t status; |
| 572 | |
| 573 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 574 | /* Create default configurations when the config is not supplied */ |
| 575 | |
| 576 | if (ua_cfg == NULL) { |
| 577 | pjsua_config_default(&default_cfg); |
| 578 | ua_cfg = &default_cfg; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 581 | if (media_cfg == NULL) { |
| 582 | pjsua_media_config_default(&default_media_cfg); |
| 583 | media_cfg = &default_media_cfg; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 586 | /* Initialize logging first so that info/errors can be captured */ |
| 587 | if (log_cfg) { |
| 588 | status = pjsua_reconfigure_logging(log_cfg); |
| 589 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 592 | /* If nameserver is configured, create DNS resolver instance and |
| 593 | * set it to be used by SIP resolver. |
| 594 | */ |
| 595 | if (ua_cfg->nameserver_count) { |
| 596 | #if PJSIP_HAS_RESOLVER |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 597 | unsigned i; |
| 598 | |
| 599 | /* Create DNS resolver */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 600 | status = pjsip_endpt_create_resolver(pjsua_var.endpt, |
| 601 | &pjsua_var.resolver); |
Benny Prijono | 318f384 | 2007-05-15 20:27:08 +0000 | [diff] [blame] | 602 | if (status != PJ_SUCCESS) { |
| 603 | pjsua_perror(THIS_FILE, "Error creating resolver", status); |
| 604 | return status; |
| 605 | } |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 606 | |
| 607 | /* Configure nameserver for the DNS resolver */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 608 | status = pj_dns_resolver_set_ns(pjsua_var.resolver, |
| 609 | ua_cfg->nameserver_count, |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 610 | ua_cfg->nameserver, NULL); |
| 611 | if (status != PJ_SUCCESS) { |
| 612 | pjsua_perror(THIS_FILE, "Error setting nameserver", status); |
| 613 | return status; |
| 614 | } |
| 615 | |
| 616 | /* Set this DNS resolver to be used by the SIP resolver */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 617 | status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver); |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 618 | if (status != PJ_SUCCESS) { |
| 619 | pjsua_perror(THIS_FILE, "Error setting DNS resolver", status); |
| 620 | return status; |
| 621 | } |
| 622 | |
| 623 | /* Print nameservers */ |
| 624 | for (i=0; i<ua_cfg->nameserver_count; ++i) { |
| 625 | PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added", |
| 626 | (int)ua_cfg->nameserver[i].slen, |
| 627 | ua_cfg->nameserver[i].ptr)); |
| 628 | } |
| 629 | #else |
| 630 | PJ_LOG(2,(THIS_FILE, |
| 631 | "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)")); |
| 632 | #endif |
| 633 | } |
| 634 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 635 | /* Init SIP UA: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 636 | |
| 637 | /* Initialize transaction layer: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 638 | status = pjsip_tsx_layer_init_module(pjsua_var.endpt); |
| 639 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 640 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 641 | |
| 642 | /* Initialize UA layer module: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 643 | status = pjsip_ua_init_module( pjsua_var.endpt, NULL ); |
| 644 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 645 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 646 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 647 | /* Initialize Replaces support. */ |
| 648 | status = pjsip_replaces_init_module( pjsua_var.endpt ); |
| 649 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 650 | |
| 651 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 652 | /* Initialize and register PJSUA application module. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 653 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 654 | const pjsip_module mod_initializer = |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 655 | { |
| 656 | NULL, NULL, /* prev, next. */ |
| 657 | { "mod-pjsua", 9 }, /* Name. */ |
| 658 | -1, /* Id */ |
| 659 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 660 | NULL, /* load() */ |
| 661 | NULL, /* start() */ |
| 662 | NULL, /* stop() */ |
| 663 | NULL, /* unload() */ |
| 664 | &mod_pjsua_on_rx_request, /* on_rx_request() */ |
| 665 | &mod_pjsua_on_rx_response, /* on_rx_response() */ |
| 666 | NULL, /* on_tx_request. */ |
| 667 | NULL, /* on_tx_response() */ |
| 668 | NULL, /* on_tsx_state() */ |
| 669 | }; |
| 670 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 671 | pjsua_var.mod = mod_initializer; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 672 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 673 | status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod); |
| 674 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 677 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 678 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 679 | /* Initialize PJSUA call subsystem: */ |
| 680 | status = pjsua_call_subsys_init(ua_cfg); |
| 681 | if (status != PJ_SUCCESS) |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 682 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 683 | |
| 684 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 685 | /* Start resolving STUN server */ |
| 686 | status = pjsua_resolve_stun_server(PJ_FALSE); |
| 687 | if (status != PJ_SUCCESS && status != PJ_EPENDING) { |
| 688 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 689 | return status; |
| 690 | } |
| 691 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 692 | /* Initialize PJSUA media subsystem */ |
| 693 | status = pjsua_media_subsys_init(media_cfg); |
| 694 | if (status != PJ_SUCCESS) |
| 695 | goto on_error; |
| 696 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 697 | |
| 698 | /* Init core SIMPLE module : */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 699 | status = pjsip_evsub_init_module(pjsua_var.endpt); |
| 700 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 701 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 702 | |
| 703 | /* Init presence module: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 704 | status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance()); |
| 705 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 706 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 707 | /* Init PUBLISH module */ |
| 708 | pjsip_publishc_init_module(pjsua_var.endpt); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 709 | |
| 710 | /* Init xfer/REFER module */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 711 | status = pjsip_xfer_init_module( pjsua_var.endpt ); |
| 712 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 713 | |
| 714 | /* Init pjsua presence handler: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 715 | status = pjsua_pres_init(); |
| 716 | if (status != PJ_SUCCESS) |
| 717 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 718 | |
| 719 | /* Init out-of-dialog MESSAGE request handler. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 720 | status = pjsua_im_init(); |
| 721 | if (status != PJ_SUCCESS) |
| 722 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 723 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 724 | /* Register OPTIONS handler */ |
| 725 | pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler); |
| 726 | |
| 727 | /* Add OPTIONS in Allow header */ |
| 728 | pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW, |
| 729 | NULL, 1, &STR_OPTIONS); |
| 730 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 731 | /* Start worker thread if needed. */ |
| 732 | if (pjsua_var.ua_cfg.thread_cnt) { |
| 733 | unsigned i; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 734 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 735 | if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread)) |
| 736 | pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 737 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 738 | for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) { |
| 739 | status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread, |
| 740 | NULL, 0, 0, &pjsua_var.thread[i]); |
| 741 | if (status != PJ_SUCCESS) |
| 742 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 743 | } |
Benny Prijono | f521eb0 | 2006-08-06 23:07:25 +0000 | [diff] [blame] | 744 | PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created", |
| 745 | pjsua_var.ua_cfg.thread_cnt)); |
| 746 | } else { |
| 747 | PJ_LOG(4,(THIS_FILE, "No SIP worker threads created")); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 750 | /* Done! */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 751 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 752 | PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized", |
Benny Prijono | 106f5b7 | 2007-08-30 13:49:33 +0000 | [diff] [blame] | 753 | pj_get_version(), PJ_OS_NAME)); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 754 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 755 | return PJ_SUCCESS; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 756 | |
| 757 | on_error: |
| 758 | pjsua_destroy(); |
| 759 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 763 | /* Sleep with polling */ |
| 764 | static void busy_sleep(unsigned msec) |
| 765 | { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 766 | #if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 767 | /* Ideally we shouldn't call pj_thread_sleep() and rather |
| 768 | * CActiveScheduler::WaitForAnyRequest() here, but that will |
| 769 | * drag in Symbian header and it doesn't look pretty. |
| 770 | */ |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 771 | pj_thread_sleep(msec); |
| 772 | #else |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 773 | pj_time_val timeout, now; |
| 774 | |
| 775 | pj_gettimeofday(&timeout); |
| 776 | timeout.msec += msec; |
| 777 | pj_time_val_normalize(&timeout); |
| 778 | |
| 779 | do { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 780 | while (pjsua_handle_events(10) > 0) |
| 781 | ; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 782 | pj_gettimeofday(&now); |
| 783 | } while (PJ_TIME_VAL_LT(now, timeout)); |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 784 | #endif |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 787 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 788 | /* |
| 789 | * Callback function to receive notification from the resolver |
| 790 | * when the resolution process completes. |
| 791 | */ |
| 792 | static void stun_dns_srv_resolver_cb(void *user_data, |
| 793 | pj_status_t status, |
| 794 | const pj_dns_srv_record *rec) |
| 795 | { |
| 796 | unsigned i; |
| 797 | |
| 798 | PJ_UNUSED_ARG(user_data); |
| 799 | |
| 800 | pjsua_var.stun_status = status; |
| 801 | |
| 802 | if (status != PJ_SUCCESS) { |
| 803 | /* DNS SRV resolution failed. If stun_host is specified, resolve |
| 804 | * it with gethostbyname() |
| 805 | */ |
| 806 | if (pjsua_var.ua_cfg.stun_host.slen) { |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 807 | pj_str_t str_host, str_port; |
| 808 | int port; |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 809 | pj_hostent he; |
| 810 | |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 811 | str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':'); |
| 812 | if (str_port.ptr != NULL) { |
| 813 | str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr; |
| 814 | str_host.slen = (str_port.ptr - str_host.ptr); |
| 815 | str_port.ptr++; |
| 816 | str_port.slen = pjsua_var.ua_cfg.stun_host.slen - |
| 817 | str_host.slen - 1; |
| 818 | port = (int)pj_strtoul(&str_port); |
| 819 | if (port < 1 || port > 65535) { |
| 820 | pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL); |
| 821 | pjsua_var.stun_status = PJ_EINVAL; |
| 822 | return; |
| 823 | } |
| 824 | } else { |
| 825 | str_host = pjsua_var.ua_cfg.stun_host; |
| 826 | port = 3478; |
| 827 | } |
| 828 | |
| 829 | pjsua_var.stun_status = pj_gethostbyname(&str_host, &he); |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 830 | |
| 831 | if (pjsua_var.stun_status == PJ_SUCCESS) { |
| 832 | pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0); |
| 833 | pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr; |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 834 | pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port); |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 835 | |
| 836 | PJ_LOG(3,(THIS_FILE, |
| 837 | "STUN server %.*s resolved, address is %s:%d", |
| 838 | (int)pjsua_var.ua_cfg.stun_host.slen, |
| 839 | pjsua_var.ua_cfg.stun_host.ptr, |
| 840 | pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr), |
| 841 | (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port))); |
| 842 | } |
| 843 | } else { |
| 844 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 845 | |
| 846 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 847 | PJ_LOG(1,(THIS_FILE, |
| 848 | "DNS SRV resolution failed for STUN server %.*s: %s", |
| 849 | (int)pjsua_var.ua_cfg.stun_domain.slen, |
| 850 | pjsua_var.ua_cfg.stun_domain.ptr, |
| 851 | errmsg)); |
| 852 | } |
| 853 | return; |
| 854 | } |
| 855 | |
Benny Prijono | f5afa92 | 2007-06-11 16:51:18 +0000 | [diff] [blame] | 856 | pj_assert(rec->count != 0 && rec->entry[0].server.addr_count != 0); |
| 857 | pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, |
| 858 | rec->entry[0].port); |
| 859 | pjsua_var.stun_srv.ipv4.sin_addr.s_addr = |
| 860 | rec->entry[0].server.addr[0].s_addr; |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 861 | |
| 862 | PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):", |
| 863 | (int)pjsua_var.ua_cfg.stun_domain.slen, |
| 864 | pjsua_var.ua_cfg.stun_domain.ptr, |
| 865 | rec->count)); |
| 866 | |
| 867 | for (i=0; i<rec->count; ++i) { |
| 868 | PJ_LOG(3,(THIS_FILE, |
| 869 | " %d: prio=%d, weight=%d %s:%d", |
| 870 | i, rec->entry[i].priority, rec->entry[i].weight, |
Benny Prijono | f5afa92 | 2007-06-11 16:51:18 +0000 | [diff] [blame] | 871 | pj_inet_ntoa(rec->entry[i].server.addr[0]), |
| 872 | (int)rec->entry[i].port)); |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | } |
| 876 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 877 | /* |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 878 | * Resolve STUN server. |
| 879 | */ |
| 880 | pj_status_t pjsua_resolve_stun_server(pj_bool_t wait) |
| 881 | { |
| 882 | if (pjsua_var.stun_status == PJ_EUNKNOWN) { |
| 883 | /* Initialize STUN configuration */ |
| 884 | pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0, |
| 885 | pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 886 | pjsip_endpt_get_timer_heap(pjsua_var.endpt)); |
| 887 | |
| 888 | /* Start STUN server resolution */ |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 889 | |
| 890 | pjsua_var.stun_status = PJ_EPENDING; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 891 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 892 | /* If stun_domain is specified, resolve STUN servers with DNS |
| 893 | * SRV resolution. |
| 894 | */ |
| 895 | if (pjsua_var.ua_cfg.stun_domain.slen) { |
| 896 | pj_str_t res_type; |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 897 | pj_status_t status; |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 898 | |
| 899 | /* Fail if resolver is not configured */ |
| 900 | if (pjsua_var.resolver == NULL) { |
| 901 | PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when " |
| 902 | "stun_domain is specified")); |
| 903 | pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS; |
| 904 | return PJLIB_UTIL_EDNSNONS; |
| 905 | } |
| 906 | res_type = pj_str("_stun._udp"); |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 907 | status = |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 908 | pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type, |
| 909 | 3478, pjsua_var.pool, pjsua_var.resolver, |
Benny Prijono | f5afa92 | 2007-06-11 16:51:18 +0000 | [diff] [blame] | 910 | 0, NULL, &stun_dns_srv_resolver_cb, NULL); |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 911 | if (status != PJ_SUCCESS) { |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 912 | pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution", |
| 913 | pjsua_var.stun_status); |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 914 | pjsua_var.stun_status = status; |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 915 | return pjsua_var.stun_status; |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 916 | } else { |
| 917 | pjsua_var.stun_status = PJ_EPENDING; |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | /* Otherwise if stun_host is specified, resolve STUN server with |
| 921 | * gethostbyname(). |
| 922 | */ |
| 923 | else if (pjsua_var.ua_cfg.stun_host.slen) { |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 924 | pj_str_t str_host, str_port; |
| 925 | int port; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 926 | pj_hostent he; |
| 927 | |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 928 | str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':'); |
| 929 | if (str_port.ptr != NULL) { |
| 930 | str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr; |
| 931 | str_host.slen = (str_port.ptr - str_host.ptr); |
| 932 | str_port.ptr++; |
| 933 | str_port.slen = pjsua_var.ua_cfg.stun_host.slen - |
| 934 | str_host.slen - 1; |
| 935 | port = (int)pj_strtoul(&str_port); |
| 936 | if (port < 1 || port > 65535) { |
| 937 | pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL); |
| 938 | pjsua_var.stun_status = PJ_EINVAL; |
| 939 | return pjsua_var.stun_status; |
| 940 | } |
| 941 | } else { |
| 942 | str_host = pjsua_var.ua_cfg.stun_host; |
| 943 | port = 3478; |
| 944 | } |
| 945 | |
| 946 | |
| 947 | pjsua_var.stun_status = pj_gethostbyname(&str_host, &he); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 948 | |
| 949 | if (pjsua_var.stun_status == PJ_SUCCESS) { |
| 950 | pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0); |
| 951 | pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr; |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 952 | pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port); |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 953 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 954 | PJ_LOG(3,(THIS_FILE, |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 955 | "STUN server %.*s resolved, address is %s:%d", |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 956 | (int)pjsua_var.ua_cfg.stun_host.slen, |
| 957 | pjsua_var.ua_cfg.stun_host.ptr, |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 958 | pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr), |
| 959 | (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port))); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 960 | } |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 961 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 962 | } |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 963 | /* Otherwise disable STUN. */ |
| 964 | else { |
| 965 | pjsua_var.stun_status = PJ_SUCCESS; |
| 966 | } |
| 967 | |
| 968 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 969 | return pjsua_var.stun_status; |
| 970 | |
| 971 | } else if (pjsua_var.stun_status == PJ_EPENDING) { |
| 972 | /* STUN server resolution has been started, wait for the |
| 973 | * result. |
| 974 | */ |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 975 | if (wait) { |
| 976 | while (pjsua_var.stun_status == PJ_EPENDING) |
| 977 | pjsua_handle_events(10); |
| 978 | } |
| 979 | |
| 980 | return pjsua_var.stun_status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 981 | |
| 982 | } else { |
| 983 | /* STUN server has been resolved, return the status */ |
| 984 | return pjsua_var.stun_status; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 989 | * Destroy pjsua. |
| 990 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 991 | PJ_DEF(pj_status_t) pjsua_destroy(void) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 992 | { |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 993 | int i; /* Must be signed */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 994 | |
| 995 | /* Signal threads to quit: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 996 | pjsua_var.thread_quit_flag = 1; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 997 | |
| 998 | /* Wait worker threads to quit: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 999 | for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) { |
| 1000 | if (pjsua_var.thread[i]) { |
| 1001 | pj_thread_join(pjsua_var.thread[i]); |
| 1002 | pj_thread_destroy(pjsua_var.thread[i]); |
| 1003 | pjsua_var.thread[i] = NULL; |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1004 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1005 | } |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1006 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1007 | if (pjsua_var.endpt) { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1008 | /* Terminate all calls. */ |
| 1009 | pjsua_call_hangup_all(); |
| 1010 | |
| 1011 | /* Terminate all presence subscriptions. */ |
| 1012 | pjsua_pres_shutdown(); |
| 1013 | |
| 1014 | /* Unregister, if required: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1015 | for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1016 | if (!pjsua_var.acc[i].valid) |
| 1017 | continue; |
| 1018 | |
| 1019 | if (pjsua_var.acc[i].regc) { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1020 | pjsua_acc_set_registration(i, PJ_FALSE); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* Wait for some time to allow unregistration to complete: */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1025 | PJ_LOG(4,(THIS_FILE, "Shutting down...")); |
| 1026 | busy_sleep(1000); |
| 1027 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1028 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1029 | /* Destroy media */ |
| 1030 | pjsua_media_subsys_destroy(); |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 1031 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1032 | /* Destroy endpoint. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1033 | if (pjsua_var.endpt) { |
| 1034 | pjsip_endpt_destroy(pjsua_var.endpt); |
| 1035 | pjsua_var.endpt = NULL; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1036 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1037 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1038 | /* Destroy mutex */ |
| 1039 | if (pjsua_var.mutex) { |
| 1040 | pj_mutex_destroy(pjsua_var.mutex); |
| 1041 | pjsua_var.mutex = NULL; |
| 1042 | } |
| 1043 | |
| 1044 | /* Destroy pool and pool factory. */ |
| 1045 | if (pjsua_var.pool) { |
| 1046 | pj_pool_release(pjsua_var.pool); |
| 1047 | pjsua_var.pool = NULL; |
| 1048 | pj_caching_pool_destroy(&pjsua_var.cp); |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 1049 | |
| 1050 | PJ_LOG(4,(THIS_FILE, "PJSUA destroyed...")); |
| 1051 | |
| 1052 | /* End logging */ |
| 1053 | if (pjsua_var.log_file) { |
| 1054 | pj_file_close(pjsua_var.log_file); |
| 1055 | pjsua_var.log_file = NULL; |
| 1056 | } |
| 1057 | |
| 1058 | /* Shutdown PJLIB */ |
| 1059 | pj_shutdown(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1060 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1061 | |
Benny Prijono | f762ee7 | 2006-12-01 11:14:37 +0000 | [diff] [blame] | 1062 | /* Clear pjsua_var */ |
| 1063 | pj_bzero(&pjsua_var, sizeof(pjsua_var)); |
| 1064 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1065 | /* Done. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1066 | return PJ_SUCCESS; |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | /** |
| 1071 | * Application is recommended to call this function after all initialization |
| 1072 | * is done, so that the library can do additional checking set up |
| 1073 | * additional |
| 1074 | * |
| 1075 | * @return PJ_SUCCESS on success, or the appropriate error code. |
| 1076 | */ |
| 1077 | PJ_DEF(pj_status_t) pjsua_start(void) |
| 1078 | { |
| 1079 | pj_status_t status; |
| 1080 | |
| 1081 | status = pjsua_call_subsys_start(); |
| 1082 | if (status != PJ_SUCCESS) |
| 1083 | return status; |
| 1084 | |
| 1085 | status = pjsua_media_subsys_start(); |
| 1086 | if (status != PJ_SUCCESS) |
| 1087 | return status; |
| 1088 | |
| 1089 | status = pjsua_pres_start(); |
| 1090 | if (status != PJ_SUCCESS) |
| 1091 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1092 | |
| 1093 | return PJ_SUCCESS; |
| 1094 | } |
| 1095 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1096 | |
| 1097 | /** |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1098 | * Poll pjsua for events, and if necessary block the caller thread for |
| 1099 | * the specified maximum interval (in miliseconds). |
| 1100 | */ |
| 1101 | PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout) |
| 1102 | { |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 1103 | #if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 1104 | /* Ideally we shouldn't call pj_thread_sleep() and rather |
| 1105 | * CActiveScheduler::WaitForAnyRequest() here, but that will |
| 1106 | * drag in Symbian header and it doesn't look pretty. |
| 1107 | */ |
| 1108 | pj_thread_sleep(msec_timeout); |
| 1109 | return msec_timeout; |
| 1110 | #else |
| 1111 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1112 | unsigned count = 0; |
| 1113 | pj_time_val tv; |
| 1114 | pj_status_t status; |
| 1115 | |
| 1116 | tv.sec = 0; |
| 1117 | tv.msec = msec_timeout; |
| 1118 | pj_time_val_normalize(&tv); |
| 1119 | |
| 1120 | status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count); |
| 1121 | |
| 1122 | if (status != PJ_SUCCESS) |
| 1123 | return -status; |
| 1124 | |
| 1125 | return count; |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 1126 | #endif |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | /* |
| 1131 | * Create memory pool. |
| 1132 | */ |
| 1133 | PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size, |
| 1134 | pj_size_t increment) |
| 1135 | { |
| 1136 | /* Pool factory is thread safe, no need to lock */ |
| 1137 | return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment, |
| 1138 | NULL); |
| 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | /* |
| 1143 | * Internal function to get SIP endpoint instance of pjsua, which is |
| 1144 | * needed for example to register module, create transports, etc. |
| 1145 | * Probably is only valid after #pjsua_init() is called. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1146 | */ |
| 1147 | PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void) |
| 1148 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1149 | return pjsua_var.endpt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1152 | /* |
| 1153 | * Internal function to get media endpoint instance. |
| 1154 | * Only valid after #pjsua_init() is called. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1155 | */ |
| 1156 | PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void) |
| 1157 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1158 | return pjsua_var.med_endpt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Benny Prijono | 97b8717 | 2006-08-24 14:25:14 +0000 | [diff] [blame] | 1161 | /* |
| 1162 | * Internal function to get PJSUA pool factory. |
| 1163 | */ |
| 1164 | PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void) |
| 1165 | { |
| 1166 | return &pjsua_var.cp.factory; |
| 1167 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1168 | |
| 1169 | /***************************************************************************** |
| 1170 | * PJSUA SIP Transport API. |
| 1171 | */ |
| 1172 | |
| 1173 | /* |
| 1174 | * Create and initialize SIP socket (and possibly resolve public |
| 1175 | * address via STUN, depending on config). |
| 1176 | */ |
| 1177 | static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr, |
| 1178 | int port, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1179 | pj_sock_t *p_sock, |
| 1180 | pj_sockaddr_in *p_pub_addr) |
| 1181 | { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1182 | char ip_addr[32]; |
| 1183 | pj_str_t stun_srv; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1184 | pj_sock_t sock; |
| 1185 | pj_status_t status; |
| 1186 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1187 | /* Make sure STUN server resolution has completed */ |
| 1188 | status = pjsua_resolve_stun_server(PJ_TRUE); |
| 1189 | if (status != PJ_SUCCESS) { |
| 1190 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 1191 | return status; |
| 1192 | } |
| 1193 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 1194 | status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1195 | if (status != PJ_SUCCESS) { |
| 1196 | pjsua_perror(THIS_FILE, "socket() error", status); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1197 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Benny Prijono | f90ef4f | 2007-02-12 14:59:57 +0000 | [diff] [blame] | 1200 | status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr), |
| 1201 | (pj_uint16_t)port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1202 | if (status != PJ_SUCCESS) { |
| 1203 | pjsua_perror(THIS_FILE, "bind() error", status); |
| 1204 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1205 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Benny Prijono | e347cb0 | 2007-02-14 14:36:13 +0000 | [diff] [blame] | 1208 | /* If port is zero, get the bound port */ |
| 1209 | if (port == 0) { |
| 1210 | pj_sockaddr_in bound_addr; |
| 1211 | int namelen = sizeof(bound_addr); |
| 1212 | status = pj_sock_getsockname(sock, &bound_addr, &namelen); |
| 1213 | if (status != PJ_SUCCESS) { |
| 1214 | pjsua_perror(THIS_FILE, "getsockname() error", status); |
| 1215 | pj_sock_close(sock); |
| 1216 | return status; |
| 1217 | } |
| 1218 | |
| 1219 | port = pj_ntohs(bound_addr.sin_port); |
| 1220 | } |
| 1221 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1222 | if (pjsua_var.stun_srv.addr.sa_family != 0) { |
| 1223 | pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr)); |
| 1224 | stun_srv = pj_str(ip_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1225 | } else { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1226 | stun_srv.slen = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /* Get the published address, either by STUN or by resolving |
| 1230 | * the name of local host. |
| 1231 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1232 | if (stun_srv.slen) { |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1233 | /* |
| 1234 | * STUN is specified, resolve the address with STUN. |
| 1235 | */ |
Benny Prijono | 14c2b86 | 2007-02-21 00:40:05 +0000 | [diff] [blame] | 1236 | status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock, |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 1237 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
| 1238 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1239 | p_pub_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1240 | if (status != PJ_SUCCESS) { |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame] | 1241 | pjsua_perror(THIS_FILE, "Error contacting STUN server", status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1242 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1243 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1246 | } else if (p_pub_addr->sin_addr.s_addr != 0) { |
| 1247 | /* |
| 1248 | * Public address is already specified, no need to resolve the |
| 1249 | * address, only set the port. |
| 1250 | */ |
Benny Prijono | e347cb0 | 2007-02-14 14:36:13 +0000 | [diff] [blame] | 1251 | if (p_pub_addr->sin_port == 0) |
| 1252 | p_pub_addr->sin_port = pj_htons((pj_uint16_t)port); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1253 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1254 | } else { |
| 1255 | |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 1256 | pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1257 | |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 1258 | status = pj_gethostip(&p_pub_addr->sin_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1259 | if (status != PJ_SUCCESS) { |
Benny Prijono | b43bad7 | 2007-01-20 05:11:08 +0000 | [diff] [blame] | 1260 | pjsua_perror(THIS_FILE, "Unable to get local host IP", status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1261 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1262 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 1265 | p_pub_addr->sin_family = pj_AF_INET(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1266 | p_pub_addr->sin_port = pj_htons((pj_uint16_t)port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | *p_sock = sock; |
| 1270 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1271 | PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d", |
| 1272 | pj_inet_ntoa(p_pub_addr->sin_addr), |
| 1273 | (int)pj_ntohs(p_pub_addr->sin_port))); |
| 1274 | |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1275 | return PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | /* |
| 1280 | * Create SIP transport. |
| 1281 | */ |
| 1282 | PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type, |
| 1283 | const pjsua_transport_config *cfg, |
| 1284 | pjsua_transport_id *p_id) |
| 1285 | { |
| 1286 | pjsip_transport *tp; |
| 1287 | unsigned id; |
| 1288 | pj_status_t status; |
| 1289 | |
| 1290 | PJSUA_LOCK(); |
| 1291 | |
| 1292 | /* Find empty transport slot */ |
| 1293 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1294 | if (pjsua_var.tpdata[id].data.ptr == NULL) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 1299 | status = PJ_ETOOMANY; |
| 1300 | pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 1301 | goto on_return; |
| 1302 | } |
| 1303 | |
| 1304 | /* Create the transport */ |
| 1305 | if (type == PJSIP_TRANSPORT_UDP) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1306 | /* |
| 1307 | * Create UDP transport. |
| 1308 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1309 | pjsua_transport_config config; |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1310 | pj_sock_t sock = PJ_INVALID_SOCKET; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1311 | pj_sockaddr_in bound_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1312 | pj_sockaddr_in pub_addr; |
| 1313 | pjsip_host_port addr_name; |
| 1314 | |
| 1315 | /* Supply default config if it's not specified */ |
| 1316 | if (cfg == NULL) { |
| 1317 | pjsua_transport_config_default(&config); |
| 1318 | cfg = &config; |
| 1319 | } |
| 1320 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1321 | /* Initialize bound address, if any */ |
| 1322 | bound_addr.sin_addr.s_addr = PJ_INADDR_ANY; |
| 1323 | if (cfg->bound_addr.slen) { |
| 1324 | status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr); |
| 1325 | if (status != PJ_SUCCESS) { |
| 1326 | pjsua_perror(THIS_FILE, |
| 1327 | "Unable to resolve transport bound address", |
| 1328 | status); |
| 1329 | goto on_return; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | /* Initialize the public address from the config, if any */ |
| 1334 | pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port); |
| 1335 | if (cfg->public_addr.slen) { |
| 1336 | status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr); |
| 1337 | if (status != PJ_SUCCESS) { |
| 1338 | pjsua_perror(THIS_FILE, |
| 1339 | "Unable to resolve transport public address", |
| 1340 | status); |
| 1341 | goto on_return; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /* Create the socket and possibly resolve the address with STUN |
| 1346 | * (only when public address is not specified). |
| 1347 | */ |
| 1348 | status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port, |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1349 | &sock, &pub_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1350 | if (status != PJ_SUCCESS) |
| 1351 | goto on_return; |
| 1352 | |
| 1353 | addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr)); |
| 1354 | addr_name.port = pj_ntohs(pub_addr.sin_port); |
| 1355 | |
| 1356 | /* Create UDP transport */ |
| 1357 | status = pjsip_udp_transport_attach( pjsua_var.endpt, sock, |
| 1358 | &addr_name, 1, |
| 1359 | &tp); |
| 1360 | if (status != PJ_SUCCESS) { |
| 1361 | pjsua_perror(THIS_FILE, "Error creating SIP UDP transport", |
| 1362 | status); |
| 1363 | pj_sock_close(sock); |
| 1364 | goto on_return; |
| 1365 | } |
| 1366 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1367 | |
| 1368 | /* Save the transport */ |
| 1369 | pjsua_var.tpdata[id].type = type; |
| 1370 | pjsua_var.tpdata[id].local_name = tp->local_name; |
| 1371 | pjsua_var.tpdata[id].data.tp = tp; |
| 1372 | |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 1373 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0 |
| 1374 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1375 | } else if (type == PJSIP_TRANSPORT_TCP) { |
| 1376 | /* |
| 1377 | * Create TCP transport. |
| 1378 | */ |
| 1379 | pjsua_transport_config config; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1380 | pjsip_host_port a_name; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1381 | pjsip_tpfactory *tcp; |
| 1382 | pj_sockaddr_in local_addr; |
| 1383 | |
| 1384 | /* Supply default config if it's not specified */ |
| 1385 | if (cfg == NULL) { |
| 1386 | pjsua_transport_config_default(&config); |
| 1387 | cfg = &config; |
| 1388 | } |
| 1389 | |
| 1390 | /* Init local address */ |
| 1391 | pj_sockaddr_in_init(&local_addr, 0, 0); |
| 1392 | |
| 1393 | if (cfg->port) |
| 1394 | local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port); |
| 1395 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1396 | if (cfg->bound_addr.slen) { |
| 1397 | status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr); |
| 1398 | if (status != PJ_SUCCESS) { |
| 1399 | pjsua_perror(THIS_FILE, |
| 1400 | "Unable to resolve transport bound address", |
| 1401 | status); |
| 1402 | goto on_return; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /* Init published name */ |
| 1407 | pj_bzero(&a_name, sizeof(pjsip_host_port)); |
| 1408 | if (cfg->public_addr.slen) |
| 1409 | a_name.host = cfg->public_addr; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1410 | |
| 1411 | /* Create the TCP transport */ |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1412 | status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr, |
| 1413 | &a_name, 1, &tcp); |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1414 | |
| 1415 | if (status != PJ_SUCCESS) { |
| 1416 | pjsua_perror(THIS_FILE, "Error creating SIP TCP listener", |
| 1417 | status); |
| 1418 | goto on_return; |
| 1419 | } |
| 1420 | |
| 1421 | /* Save the transport */ |
| 1422 | pjsua_var.tpdata[id].type = type; |
| 1423 | pjsua_var.tpdata[id].local_name = tcp->addr_name; |
| 1424 | pjsua_var.tpdata[id].data.factory = tcp; |
| 1425 | |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 1426 | #endif /* PJ_HAS_TCP */ |
| 1427 | |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1428 | #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0 |
| 1429 | } else if (type == PJSIP_TRANSPORT_TLS) { |
| 1430 | /* |
| 1431 | * Create TLS transport. |
| 1432 | */ |
Benny Prijono | f3bbc13 | 2006-12-25 06:43:59 +0000 | [diff] [blame] | 1433 | /* |
| 1434 | * Create TCP transport. |
| 1435 | */ |
| 1436 | pjsua_transport_config config; |
| 1437 | pjsip_host_port a_name; |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1438 | pjsip_tpfactory *tls; |
Benny Prijono | f3bbc13 | 2006-12-25 06:43:59 +0000 | [diff] [blame] | 1439 | pj_sockaddr_in local_addr; |
| 1440 | |
| 1441 | /* Supply default config if it's not specified */ |
| 1442 | if (cfg == NULL) { |
| 1443 | pjsua_transport_config_default(&config); |
| 1444 | config.port = 5061; |
| 1445 | cfg = &config; |
| 1446 | } |
| 1447 | |
| 1448 | /* Init local address */ |
| 1449 | pj_sockaddr_in_init(&local_addr, 0, 0); |
| 1450 | |
| 1451 | if (cfg->port) |
| 1452 | local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port); |
| 1453 | |
| 1454 | if (cfg->bound_addr.slen) { |
| 1455 | status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr); |
| 1456 | if (status != PJ_SUCCESS) { |
| 1457 | pjsua_perror(THIS_FILE, |
| 1458 | "Unable to resolve transport bound address", |
| 1459 | status); |
| 1460 | goto on_return; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | /* Init published name */ |
| 1465 | pj_bzero(&a_name, sizeof(pjsip_host_port)); |
| 1466 | if (cfg->public_addr.slen) |
| 1467 | a_name.host = cfg->public_addr; |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1468 | |
| 1469 | status = pjsip_tls_transport_start(pjsua_var.endpt, |
Benny Prijono | f3bbc13 | 2006-12-25 06:43:59 +0000 | [diff] [blame] | 1470 | &cfg->tls_setting, |
| 1471 | &local_addr, &a_name, 1, &tls); |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1472 | if (status != PJ_SUCCESS) { |
| 1473 | pjsua_perror(THIS_FILE, "Error creating SIP TLS listener", |
| 1474 | status); |
| 1475 | goto on_return; |
| 1476 | } |
| 1477 | |
| 1478 | /* Save the transport */ |
| 1479 | pjsua_var.tpdata[id].type = type; |
| 1480 | pjsua_var.tpdata[id].local_name = tls->addr_name; |
| 1481 | pjsua_var.tpdata[id].data.factory = tls; |
| 1482 | #endif |
| 1483 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1484 | } else { |
| 1485 | status = PJSIP_EUNSUPTRANSPORT; |
| 1486 | pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 1487 | goto on_return; |
| 1488 | } |
| 1489 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1490 | |
| 1491 | /* Return the ID */ |
| 1492 | if (p_id) *p_id = id; |
| 1493 | |
| 1494 | status = PJ_SUCCESS; |
| 1495 | |
| 1496 | on_return: |
| 1497 | |
| 1498 | PJSUA_UNLOCK(); |
| 1499 | |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1500 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | |
| 1504 | /* |
| 1505 | * Register transport that has been created by application. |
| 1506 | */ |
| 1507 | PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp, |
| 1508 | pjsua_transport_id *p_id) |
| 1509 | { |
| 1510 | unsigned id; |
| 1511 | |
| 1512 | PJSUA_LOCK(); |
| 1513 | |
| 1514 | /* Find empty transport slot */ |
| 1515 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1516 | if (pjsua_var.tpdata[id].data.ptr == NULL) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1517 | break; |
| 1518 | } |
| 1519 | |
| 1520 | if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 1521 | pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY); |
| 1522 | PJSUA_UNLOCK(); |
| 1523 | return PJ_ETOOMANY; |
| 1524 | } |
| 1525 | |
| 1526 | /* Save the transport */ |
Benny Prijono | d17a5e9 | 2007-05-29 11:51:45 +0000 | [diff] [blame] | 1527 | pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1528 | pjsua_var.tpdata[id].local_name = tp->local_name; |
| 1529 | pjsua_var.tpdata[id].data.tp = tp; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1530 | |
| 1531 | /* Return the ID */ |
| 1532 | if (p_id) *p_id = id; |
| 1533 | |
| 1534 | PJSUA_UNLOCK(); |
| 1535 | |
| 1536 | return PJ_SUCCESS; |
| 1537 | } |
| 1538 | |
| 1539 | |
| 1540 | /* |
| 1541 | * Enumerate all transports currently created in the system. |
| 1542 | */ |
| 1543 | PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[], |
| 1544 | unsigned *p_count ) |
| 1545 | { |
| 1546 | unsigned i, count; |
| 1547 | |
| 1548 | PJSUA_LOCK(); |
| 1549 | |
| 1550 | for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count; |
| 1551 | ++i) |
| 1552 | { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1553 | if (!pjsua_var.tpdata[i].data.ptr) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1554 | continue; |
| 1555 | |
| 1556 | id[count++] = i; |
| 1557 | } |
| 1558 | |
| 1559 | *p_count = count; |
| 1560 | |
| 1561 | PJSUA_UNLOCK(); |
| 1562 | |
| 1563 | return PJ_SUCCESS; |
| 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | /* |
| 1568 | * Get information about transports. |
| 1569 | */ |
| 1570 | PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id, |
| 1571 | pjsua_transport_info *info) |
| 1572 | { |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1573 | pjsua_transport_data *t = &pjsua_var.tpdata[id]; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1574 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1575 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1576 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1577 | |
| 1578 | /* Make sure id is in range. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1579 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 1580 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1583 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1584 | |
| 1585 | PJSUA_LOCK(); |
| 1586 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1587 | if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) { |
| 1588 | |
| 1589 | pjsip_transport *tp = t->data.tp; |
| 1590 | |
| 1591 | if (tp == NULL) { |
| 1592 | PJSUA_UNLOCK(); |
| 1593 | return PJ_EINVALIDOP; |
| 1594 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1595 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1596 | info->id = id; |
Benny Prijono | d17a5e9 | 2007-05-29 11:51:45 +0000 | [diff] [blame] | 1597 | info->type = (pjsip_transport_type_e) tp->key.type; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1598 | info->type_name = pj_str(tp->type_name); |
| 1599 | info->info = pj_str(tp->info); |
| 1600 | info->flag = tp->flag; |
| 1601 | info->addr_len = tp->addr_len; |
| 1602 | info->local_addr = tp->local_addr; |
| 1603 | info->local_name = tp->local_name; |
| 1604 | info->usage_count = pj_atomic_get(tp->ref_cnt); |
| 1605 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1606 | status = PJ_SUCCESS; |
| 1607 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1608 | } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) { |
| 1609 | |
| 1610 | pjsip_tpfactory *factory = t->data.factory; |
| 1611 | |
| 1612 | if (factory == NULL) { |
| 1613 | PJSUA_UNLOCK(); |
| 1614 | return PJ_EINVALIDOP; |
| 1615 | } |
| 1616 | |
| 1617 | info->id = id; |
| 1618 | info->type = t->type; |
| 1619 | info->type_name = pj_str("TCP"); |
| 1620 | info->info = pj_str("TCP transport"); |
| 1621 | info->flag = factory->flag; |
| 1622 | info->addr_len = sizeof(factory->local_addr); |
| 1623 | info->local_addr = factory->local_addr; |
| 1624 | info->local_name = factory->addr_name; |
| 1625 | info->usage_count = 0; |
| 1626 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1627 | status = PJ_SUCCESS; |
| 1628 | |
| 1629 | } else { |
| 1630 | pj_assert(!"Unsupported transport"); |
| 1631 | status = PJ_EINVALIDOP; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1634 | |
| 1635 | PJSUA_UNLOCK(); |
| 1636 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1637 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | |
| 1641 | /* |
| 1642 | * Disable a transport or re-enable it. |
| 1643 | */ |
| 1644 | PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id, |
| 1645 | pj_bool_t enabled) |
| 1646 | { |
| 1647 | /* Make sure id is in range. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1648 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 1649 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1650 | |
| 1651 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1652 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1653 | |
| 1654 | |
| 1655 | /* To be done!! */ |
| 1656 | PJ_TODO(pjsua_transport_set_enable); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1657 | PJ_UNUSED_ARG(enabled); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1658 | |
| 1659 | return PJ_EINVALIDOP; |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | /* |
| 1664 | * Close the transport. |
| 1665 | */ |
| 1666 | PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id, |
| 1667 | pj_bool_t force ) |
| 1668 | { |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1669 | pj_status_t status; |
| 1670 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1671 | /* Make sure id is in range. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1672 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 1673 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1674 | |
| 1675 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1676 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1677 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1678 | /* Note: destroy() may not work if there are objects still referencing |
| 1679 | * the transport. |
| 1680 | */ |
| 1681 | if (force) { |
| 1682 | switch (pjsua_var.tpdata[id].type) { |
| 1683 | case PJSIP_TRANSPORT_UDP: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1684 | status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp); |
| 1685 | if (status != PJ_SUCCESS) |
| 1686 | return status; |
| 1687 | status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp); |
| 1688 | if (status != PJ_SUCCESS) |
| 1689 | return status; |
| 1690 | break; |
| 1691 | |
| 1692 | case PJSIP_TRANSPORT_TLS: |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1693 | case PJSIP_TRANSPORT_TCP: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1694 | /* This will close the TCP listener, but existing TCP/TLS |
| 1695 | * connections (if any) will still linger |
| 1696 | */ |
| 1697 | status = (*pjsua_var.tpdata[id].data.factory->destroy) |
| 1698 | (pjsua_var.tpdata[id].data.factory); |
| 1699 | if (status != PJ_SUCCESS) |
| 1700 | return status; |
| 1701 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1702 | break; |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1703 | |
Benny Prijono | 1ebd614 | 2006-10-19 15:48:02 +0000 | [diff] [blame] | 1704 | default: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1705 | return PJ_EINVAL; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | } else { |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1709 | /* If force is not specified, transports will be closed at their |
| 1710 | * convenient time. However this will leak PJSUA-API transport |
| 1711 | * descriptors as PJSUA-API wouldn't know when exactly the |
| 1712 | * transport is closed thus it can't cleanup PJSUA transport |
| 1713 | * descriptor. |
| 1714 | */ |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1715 | switch (pjsua_var.tpdata[id].type) { |
| 1716 | case PJSIP_TRANSPORT_UDP: |
| 1717 | return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp); |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1718 | case PJSIP_TRANSPORT_TLS: |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1719 | case PJSIP_TRANSPORT_TCP: |
| 1720 | return (*pjsua_var.tpdata[id].data.factory->destroy) |
| 1721 | (pjsua_var.tpdata[id].data.factory); |
Benny Prijono | 1ebd614 | 2006-10-19 15:48:02 +0000 | [diff] [blame] | 1722 | default: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1723 | return PJ_EINVAL; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1724 | } |
| 1725 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1726 | |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1727 | /* Cleanup pjsua data when force is applied */ |
| 1728 | if (force) { |
| 1729 | pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED; |
| 1730 | pjsua_var.tpdata[id].data.ptr = NULL; |
| 1731 | } |
| 1732 | |
| 1733 | return PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | |
| 1737 | /* |
| 1738 | * Add additional headers etc in msg_data specified by application |
| 1739 | * when sending requests. |
| 1740 | */ |
| 1741 | void pjsua_process_msg_data(pjsip_tx_data *tdata, |
| 1742 | const pjsua_msg_data *msg_data) |
| 1743 | { |
| 1744 | pj_bool_t allow_body; |
| 1745 | const pjsip_hdr *hdr; |
| 1746 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1747 | /* Always add User-Agent */ |
| 1748 | if (pjsua_var.ua_cfg.user_agent.slen && |
| 1749 | tdata->msg->type == PJSIP_REQUEST_MSG) |
| 1750 | { |
| 1751 | const pj_str_t STR_USER_AGENT = { "User-Agent", 10 }; |
| 1752 | pjsip_hdr *h; |
| 1753 | h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool, |
| 1754 | &STR_USER_AGENT, |
| 1755 | &pjsua_var.ua_cfg.user_agent); |
| 1756 | pjsip_msg_add_hdr(tdata->msg, h); |
| 1757 | } |
| 1758 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1759 | if (!msg_data) |
| 1760 | return; |
| 1761 | |
| 1762 | hdr = msg_data->hdr_list.next; |
| 1763 | while (hdr && hdr != &msg_data->hdr_list) { |
| 1764 | pjsip_hdr *new_hdr; |
| 1765 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1766 | new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1767 | pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 1768 | |
| 1769 | hdr = hdr->next; |
| 1770 | } |
| 1771 | |
| 1772 | allow_body = (tdata->msg->body == NULL); |
| 1773 | |
| 1774 | if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) { |
| 1775 | pjsip_media_type ctype; |
| 1776 | pjsip_msg_body *body; |
| 1777 | |
| 1778 | pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype); |
| 1779 | body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype, |
| 1780 | &msg_data->msg_body); |
| 1781 | tdata->msg->body = body; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | |
| 1786 | /* |
| 1787 | * Add route_set to outgoing requests |
| 1788 | */ |
| 1789 | void pjsua_set_msg_route_set( pjsip_tx_data *tdata, |
| 1790 | const pjsip_route_hdr *route_set ) |
| 1791 | { |
| 1792 | const pjsip_route_hdr *r; |
| 1793 | |
| 1794 | r = route_set->next; |
| 1795 | while (r != route_set) { |
| 1796 | pjsip_route_hdr *new_r; |
| 1797 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1798 | new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1799 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r); |
| 1800 | |
| 1801 | r = r->next; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | /* |
| 1807 | * Simple version of MIME type parsing (it doesn't support parameters) |
| 1808 | */ |
| 1809 | void pjsua_parse_media_type( pj_pool_t *pool, |
| 1810 | const pj_str_t *mime, |
| 1811 | pjsip_media_type *media_type) |
| 1812 | { |
| 1813 | pj_str_t tmp; |
| 1814 | char *pos; |
| 1815 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1816 | pj_bzero(media_type, sizeof(*media_type)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1817 | |
| 1818 | pj_strdup_with_null(pool, &tmp, mime); |
| 1819 | |
| 1820 | pos = pj_strchr(&tmp, '/'); |
| 1821 | if (pos) { |
| 1822 | media_type->type.ptr = tmp.ptr; |
| 1823 | media_type->type.slen = (pos-tmp.ptr); |
| 1824 | media_type->subtype.ptr = pos+1; |
| 1825 | media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1; |
| 1826 | } else { |
| 1827 | media_type->type = tmp; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | /* |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1833 | * Internal function to init transport selector from transport id. |
| 1834 | */ |
| 1835 | void pjsua_init_tpselector(pjsua_transport_id tp_id, |
| 1836 | pjsip_tpselector *sel) |
| 1837 | { |
| 1838 | pjsua_transport_data *tpdata; |
| 1839 | unsigned flag; |
| 1840 | |
| 1841 | pj_bzero(sel, sizeof(*sel)); |
| 1842 | if (tp_id == PJSUA_INVALID_ID) |
| 1843 | return; |
| 1844 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1845 | pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata)); |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1846 | tpdata = &pjsua_var.tpdata[tp_id]; |
| 1847 | |
| 1848 | flag = pjsip_transport_get_flag_from_type(tpdata->type); |
| 1849 | |
| 1850 | if (flag & PJSIP_TRANSPORT_DATAGRAM) { |
| 1851 | sel->type = PJSIP_TPSELECTOR_TRANSPORT; |
| 1852 | sel->u.transport = tpdata->data.tp; |
| 1853 | } else { |
| 1854 | sel->type = PJSIP_TPSELECTOR_LISTENER; |
| 1855 | sel->u.listener = tpdata->data.factory; |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1861 | * Verify that valid SIP url is given. |
| 1862 | */ |
| 1863 | PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url) |
| 1864 | { |
| 1865 | pjsip_uri *p; |
| 1866 | pj_pool_t *pool; |
| 1867 | char *url; |
| 1868 | int len = (c_url ? pj_ansi_strlen(c_url) : 0); |
| 1869 | |
| 1870 | if (!len) return -1; |
| 1871 | |
| 1872 | pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL); |
| 1873 | if (!pool) return -1; |
| 1874 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1875 | url = (char*) pj_pool_alloc(pool, len+1); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1876 | pj_ansi_strcpy(url, c_url); |
| 1877 | |
| 1878 | p = pjsip_parse_uri(pool, url, len, 0); |
| 1879 | if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0) |
| 1880 | p = NULL; |
| 1881 | |
| 1882 | pj_pool_release(pool); |
| 1883 | return p ? 0 : -1; |
| 1884 | } |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 1885 | |
| 1886 | |
| 1887 | /* |
| 1888 | * This is a utility function to dump the stack states to log, using |
| 1889 | * verbosity level 3. |
| 1890 | */ |
| 1891 | PJ_DEF(void) pjsua_dump(pj_bool_t detail) |
| 1892 | { |
| 1893 | unsigned old_decor; |
| 1894 | unsigned i; |
| 1895 | char buf[1024]; |
| 1896 | |
| 1897 | PJ_LOG(3,(THIS_FILE, "Start dumping application states:")); |
| 1898 | |
| 1899 | old_decor = pj_log_get_decor(); |
| 1900 | pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR)); |
| 1901 | |
| 1902 | if (detail) |
| 1903 | pj_dump_config(); |
| 1904 | |
| 1905 | pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail); |
| 1906 | |
| 1907 | pjmedia_endpt_dump(pjsua_get_pjmedia_endpt()); |
| 1908 | |
| 1909 | PJ_LOG(3,(THIS_FILE, "Dumping media transports:")); |
| 1910 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 1911 | pjsua_call *call = &pjsua_var.calls[i]; |
| 1912 | pjmedia_sock_info skinfo; |
| 1913 | |
Benny Prijono | 4f093d2 | 2007-04-09 08:54:32 +0000 | [diff] [blame] | 1914 | /* MSVC complains about skinfo not being initialized */ |
| 1915 | pj_bzero(&skinfo, sizeof(skinfo)); |
| 1916 | |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 1917 | pjmedia_transport_get_info(call->med_tp, &skinfo); |
| 1918 | |
| 1919 | PJ_LOG(3,(THIS_FILE, " %s: %s:%d", |
| 1920 | (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"), |
| 1921 | pj_inet_ntoa(skinfo.rtp_addr_name.sin_addr), |
| 1922 | (int)pj_ntohs(skinfo.rtp_addr_name.sin_port))); |
| 1923 | } |
| 1924 | |
| 1925 | pjsip_tsx_layer_dump(detail); |
| 1926 | pjsip_ua_dump(detail); |
| 1927 | |
| 1928 | |
| 1929 | /* Dump all invite sessions: */ |
| 1930 | PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:")); |
| 1931 | |
| 1932 | if (pjsua_call_get_count() == 0) { |
| 1933 | |
| 1934 | PJ_LOG(3,(THIS_FILE, " - no sessions -")); |
| 1935 | |
| 1936 | } else { |
| 1937 | unsigned i; |
| 1938 | |
| 1939 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 1940 | if (pjsua_call_is_active(i)) { |
| 1941 | pjsua_call_dump(i, detail, buf, sizeof(buf), " "); |
| 1942 | PJ_LOG(3,(THIS_FILE, "%s", buf)); |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | /* Dump presence status */ |
| 1948 | pjsua_pres_dump(detail); |
| 1949 | |
| 1950 | pj_log_set_decor(old_decor); |
| 1951 | PJ_LOG(3,(THIS_FILE, "Dump complete")); |
| 1952 | } |
| 1953 | |