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