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