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