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