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