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