| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsua-lib/pjsua.h> |
| 20 |
#include <pjsua-lib/pjsua_internal.h> |
| 21 |
|
| 22 |
|
| 23 |
#define THIS_FILE "pjsua_core.c" |
| 24 |
|
| 25 |
#define DEFAULT_RTP_PORT 4000 |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static void resolve_stun_entry(pjsua_stun_resolve *sess); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct pjsua_data pjsua_var; |
| 34 |
|
| 35 |
|
| 36 |
PJ_DEF(struct pjsua_data*) pjsua_get_var(void) |
| 37 |
{ |
| 38 |
return &pjsua_var; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
PJ_DEF(void) pjsua_perror( const char *sender, const char *title, |
| 44 |
pj_status_t status) |
| 45 |
{ |
| 46 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 47 |
|
| 48 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 49 |
PJ_LOG(1,(sender, "%s: %s [status=%d]", title, errmsg, status)); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
static void init_data() |
| 54 |
{ |
| 55 |
unsigned i; |
| 56 |
|
| 57 |
pj_bzero(&pjsua_var, sizeof(pjsua_var)); |
| 58 |
|
| 59 |
for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) |
| 60 |
pjsua_var.acc[i].index = i; |
| 61 |
|
| 62 |
for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i) |
| 63 |
pjsua_var.tpdata[i].index = i; |
| 64 |
|
| 65 |
pjsua_var.stun_status = PJ_EUNKNOWN; |
| 66 |
pjsua_var.nat_status = PJ_EPENDING; |
| 67 |
pj_list_init(&pjsua_var.stun_res); |
| 68 |
pj_list_init(&pjsua_var.outbound_proxy); |
| 69 |
|
| 70 |
pjsua_config_default(&pjsua_var.ua_cfg); |
| 71 |
|
| 72 |
for (i=0; i<PJSUA_MAX_VID_WINS; ++i) { |
| 73 |
pjsua_vid_win_reset(i); |
| 74 |
} |
| 75 |
#if PJSUA_HAS_VIDEO |
| 76 |
for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.avi_player); ++i) { |
| 77 |
pjsua_reset_avi_player_data(i); |
| 78 |
} |
| 79 |
|
| 80 |
for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.avi_recorder); ++i) { |
| 81 |
pjsua_reset_avi_recorder_data(i); |
| 82 |
} |
| 83 |
#endif |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg) |
| 88 |
{ |
| 89 |
pj_bzero(cfg, sizeof(*cfg)); |
| 90 |
|
| 91 |
cfg->msg_logging = PJ_TRUE; |
| 92 |
cfg->level = 5; |
| 93 |
cfg->console_level = 4; |
| 94 |
cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME | |
| 95 |
PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE | |
| 96 |
PJ_LOG_HAS_SPACE | PJ_LOG_HAS_THREAD_SWC | |
| 97 |
PJ_LOG_HAS_INDENT; |
| 98 |
#if (defined(PJ_WIN32) && PJ_WIN32 != 0) || (defined(PJ_WIN64) && PJ_WIN64 != 0) |
| 99 |
cfg->decor |= PJ_LOG_HAS_COLOR; |
| 100 |
#endif |
| 101 |
} |
| 102 |
|
| 103 |
PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool, |
| 104 |
pjsua_logging_config *dst, |
| 105 |
const pjsua_logging_config *src) |
| 106 |
{ |
| 107 |
pj_memcpy(dst, src, sizeof(*src)); |
| 108 |
pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename); |
| 109 |
} |
| 110 |
|
| 111 |
PJ_DEF(void) pjsua_config_default(pjsua_config *cfg) |
| 112 |
{ |
| 113 |
pj_bzero(cfg, sizeof(*cfg)); |
| 114 |
|
| 115 |
cfg->max_calls = PJSUA_MAX_CALLS; |
| 116 |
cfg->thread_cnt = PJSUA_SEPARATE_WORKER_FOR_TIMER? 2 : 1; |
| 117 |
cfg->nat_type_in_sdp = 1; |
| 118 |
cfg->stun_ignore_failure = PJ_TRUE; |
| 119 |
cfg->force_lr = PJ_TRUE; |
| 120 |
cfg->enable_unsolicited_mwi = PJ_TRUE; |
| 121 |
cfg->use_srtp = PJSUA_DEFAULT_USE_SRTP; |
| 122 |
cfg->srtp_secure_signaling = PJSUA_DEFAULT_SRTP_SECURE_SIGNALING; |
| 123 |
cfg->hangup_forked_call = PJ_TRUE; |
| 124 |
|
| 125 |
cfg->use_timer = PJSUA_SIP_TIMER_OPTIONAL; |
| 126 |
cfg->use_siprec = PJSUA_SIP_SIPREC_INACTIVE; |
| 127 |
pjsip_timer_setting_default(&cfg->timer_setting); |
| 128 |
pjsua_srtp_opt_default(&cfg->srtp_opt); |
| 129 |
cfg->no_refer_sub = PJ_TRUE; |
| 130 |
} |
| 131 |
|
| 132 |
PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool, |
| 133 |
pjsua_config *dst, |
| 134 |
const pjsua_config *src) |
| 135 |
{ |
| 136 |
unsigned i; |
| 137 |
|
| 138 |
pj_memcpy(dst, src, sizeof(*src)); |
| 139 |
|
| 140 |
for (i=0; i<src->outbound_proxy_cnt; ++i) { |
| 141 |
pj_strdup_with_null(pool, &dst->outbound_proxy[i], |
| 142 |
&src->outbound_proxy[i]); |
| 143 |
} |
| 144 |
|
| 145 |
for (i=0; i<src->cred_count; ++i) { |
| 146 |
pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]); |
| 147 |
} |
| 148 |
|
| 149 |
pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent); |
| 150 |
pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain); |
| 151 |
pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host); |
| 152 |
|
| 153 |
for (i=0; i<src->stun_srv_cnt; ++i) { |
| 154 |
pj_strdup_with_null(pool, &dst->stun_srv[i], &src->stun_srv[i]); |
| 155 |
} |
| 156 |
|
| 157 |
pjsua_srtp_opt_dup(pool, &dst->srtp_opt, &src->srtp_opt, PJ_FALSE); |
| 158 |
} |
| 159 |
|
| 160 |
PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data) |
| 161 |
{ |
| 162 |
pj_bzero(msg_data, sizeof(*msg_data)); |
| 163 |
pj_list_init(&msg_data->hdr_list); |
| 164 |
pjsip_media_type_init(&msg_data->multipart_ctype, NULL, NULL); |
| 165 |
pj_list_init(&msg_data->multipart_parts); |
| 166 |
} |
| 167 |
|
| 168 |
PJ_DEF(pjsua_msg_data*) pjsua_msg_data_clone(pj_pool_t *pool, |
| 169 |
const pjsua_msg_data *rhs) |
| 170 |
{ |
| 171 |
pjsua_msg_data *msg_data; |
| 172 |
const pjsip_hdr *hdr; |
| 173 |
const pjsip_multipart_part *mpart; |
| 174 |
|
| 175 |
PJ_ASSERT_RETURN(pool && rhs, NULL); |
| 176 |
|
| 177 |
msg_data = PJ_POOL_ZALLOC_T(pool, pjsua_msg_data); |
| 178 |
PJ_ASSERT_RETURN(msg_data != NULL, NULL); |
| 179 |
|
| 180 |
pj_strdup(pool, &msg_data->target_uri, &rhs->target_uri); |
| 181 |
pj_strdup(pool, &msg_data->local_uri, &rhs->local_uri); |
| 182 |
pj_strdup(pool, &msg_data->contact_uri, &rhs->contact_uri); |
| 183 |
|
| 184 |
pj_list_init(&msg_data->hdr_list); |
| 185 |
hdr = rhs->hdr_list.next; |
| 186 |
while (hdr != &rhs->hdr_list) { |
| 187 |
pj_list_push_back(&msg_data->hdr_list, pjsip_hdr_clone(pool, hdr)); |
| 188 |
hdr = hdr->next; |
| 189 |
} |
| 190 |
|
| 191 |
pj_strdup(pool, &msg_data->content_type, &rhs->content_type); |
| 192 |
pj_strdup(pool, &msg_data->msg_body, &rhs->msg_body); |
| 193 |
|
| 194 |
pjsip_media_type_cp(pool, &msg_data->multipart_ctype, |
| 195 |
&rhs->multipart_ctype); |
| 196 |
|
| 197 |
pj_list_init(&msg_data->multipart_parts); |
| 198 |
mpart = rhs->multipart_parts.next; |
| 199 |
while (mpart != &rhs->multipart_parts) { |
| 200 |
pj_list_push_back(&msg_data->multipart_parts, |
| 201 |
pjsip_multipart_clone_part(pool, mpart)); |
| 202 |
mpart = mpart->next; |
| 203 |
} |
| 204 |
|
| 205 |
return msg_data; |
| 206 |
} |
| 207 |
|
| 208 |
PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg) |
| 209 |
{ |
| 210 |
pj_bzero(cfg, sizeof(*cfg)); |
| 211 |
pjsip_tls_setting_default(&cfg->tls_setting); |
| 212 |
} |
| 213 |
|
| 214 |
PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool, |
| 215 |
pjsua_transport_config *dst, |
| 216 |
const pjsua_transport_config *src) |
| 217 |
{ |
| 218 |
pj_memcpy(dst, src, sizeof(*src)); |
| 219 |
pj_strdup(pool, &dst->public_addr, &src->public_addr); |
| 220 |
pj_strdup(pool, &dst->bound_addr, &src->bound_addr); |
| 221 |
} |
| 222 |
|
| 223 |
PJ_DEF(void) pjsua_ice_config_from_media_config( pj_pool_t *pool, |
| 224 |
pjsua_ice_config *dst, |
| 225 |
const pjsua_media_config *src) |
| 226 |
{ |
| 227 |
PJ_UNUSED_ARG(pool); |
| 228 |
|
| 229 |
dst->enable_ice = src->enable_ice; |
| 230 |
dst->ice_max_host_cands = src->ice_max_host_cands; |
| 231 |
dst->ice_opt = src->ice_opt; |
| 232 |
dst->ice_no_rtcp = src->ice_no_rtcp; |
| 233 |
dst->ice_always_update = src->ice_always_update; |
| 234 |
} |
| 235 |
|
| 236 |
PJ_DEF(void) pjsua_ice_config_dup( pj_pool_t *pool, |
| 237 |
pjsua_ice_config *dst, |
| 238 |
const pjsua_ice_config *src) |
| 239 |
{ |
| 240 |
PJ_UNUSED_ARG(pool); |
| 241 |
pj_memcpy(dst, src, sizeof(*src)); |
| 242 |
} |
| 243 |
|
| 244 |
PJ_DEF(void) pjsua_turn_config_from_media_config(pj_pool_t *pool, |
| 245 |
pjsua_turn_config *dst, |
| 246 |
const pjsua_media_config *src) |
| 247 |
{ |
| 248 |
dst->enable_turn = src->enable_turn; |
| 249 |
dst->turn_conn_type = src->turn_conn_type; |
| 250 |
if (pool == NULL) { |
| 251 |
dst->turn_server = src->turn_server; |
| 252 |
dst->turn_auth_cred = src->turn_auth_cred; |
| 253 |
|
| 254 |
#if PJ_HAS_SSL_SOCK |
| 255 |
pj_memcpy(&dst->turn_tls_setting, &src->turn_tls_setting, |
| 256 |
sizeof(src->turn_tls_setting)); |
| 257 |
#endif |
| 258 |
} else { |
| 259 |
if (pj_stricmp(&dst->turn_server, &src->turn_server)) |
| 260 |
pj_strdup(pool, &dst->turn_server, &src->turn_server); |
| 261 |
pj_stun_auth_cred_dup(pool, &dst->turn_auth_cred, |
| 262 |
&src->turn_auth_cred); |
| 263 |
|
| 264 |
#if PJ_HAS_SSL_SOCK |
| 265 |
pj_turn_sock_tls_cfg_dup(pool, &dst->turn_tls_setting, |
| 266 |
&src->turn_tls_setting); |
| 267 |
#endif |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
PJ_DEF(void) pjsua_turn_config_dup(pj_pool_t *pool, |
| 272 |
pjsua_turn_config *dst, |
| 273 |
const pjsua_turn_config *src) |
| 274 |
{ |
| 275 |
pj_memcpy(dst, src, sizeof(*src)); |
| 276 |
if (pool) { |
| 277 |
pj_strdup(pool, &dst->turn_server, &src->turn_server); |
| 278 |
pj_stun_auth_cred_dup(pool, &dst->turn_auth_cred, |
| 279 |
&src->turn_auth_cred); |
| 280 |
|
| 281 |
#if PJ_HAS_SSL_SOCK |
| 282 |
pj_turn_sock_tls_cfg_dup(pool, &dst->turn_tls_setting, |
| 283 |
&src->turn_tls_setting); |
| 284 |
#endif |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
PJ_DEF(void) pjsua_srtp_opt_default(pjsua_srtp_opt *cfg) |
| 290 |
{ |
| 291 |
pj_bzero(cfg, sizeof(*cfg)); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
PJ_DEF(void) pjsua_srtp_opt_dup( pj_pool_t *pool, pjsua_srtp_opt *dst, |
| 296 |
const pjsua_srtp_opt *src, |
| 297 |
pj_bool_t check_str) |
| 298 |
{ |
| 299 |
pjsua_srtp_opt backup_dst; |
| 300 |
|
| 301 |
if (check_str) pj_memcpy(&backup_dst, dst, sizeof(*dst)); |
| 302 |
pj_memcpy(dst, src, sizeof(*src)); |
| 303 |
|
| 304 |
if (pool) { |
| 305 |
unsigned i; |
| 306 |
|
| 307 |
for (i = 0; i < src->crypto_count; i++) { |
| 308 |
if (!check_str || |
| 309 |
pj_stricmp(&backup_dst.crypto[i].key, &src->crypto[i].key)) |
| 310 |
{ |
| 311 |
pj_strdup(pool, &dst->crypto[i].key, &src->crypto[i].key); |
| 312 |
} else { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
dst->crypto[i].key = backup_dst.crypto[i].key; |
| 317 |
} |
| 318 |
if (!check_str || |
| 319 |
pj_stricmp(&backup_dst.crypto[i].name, &src->crypto[i].name)) |
| 320 |
{ |
| 321 |
pj_strdup(pool, &dst->crypto[i].name, &src->crypto[i].name); |
| 322 |
} else { |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
dst->crypto[i].name = backup_dst.crypto[i].name; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg) |
| 334 |
{ |
| 335 |
pjsua_media_config med_cfg; |
| 336 |
|
| 337 |
pj_bzero(cfg, sizeof(*cfg)); |
| 338 |
|
| 339 |
cfg->reg_timeout = PJSUA_REG_INTERVAL; |
| 340 |
cfg->reg_delay_before_refresh = PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH; |
| 341 |
cfg->unreg_timeout = PJSUA_UNREG_TIMEOUT; |
| 342 |
pjsip_publishc_opt_default(&cfg->publish_opt); |
| 343 |
cfg->unpublish_max_wait_time_msec = PJSUA_UNPUBLISH_MAX_WAIT_TIME_MSEC; |
| 344 |
cfg->transport_id = PJSUA_INVALID_ID; |
| 345 |
cfg->allow_contact_rewrite = PJ_TRUE; |
| 346 |
cfg->allow_via_rewrite = PJ_TRUE; |
| 347 |
cfg->require_100rel = pjsua_var.ua_cfg.require_100rel; |
| 348 |
cfg->use_timer = pjsua_var.ua_cfg.use_timer; |
| 349 |
cfg->timer_setting = pjsua_var.ua_cfg.timer_setting; |
| 350 |
cfg->use_siprec = pjsua_var.ua_cfg.use_siprec; |
| 351 |
cfg->lock_codec = 1; |
| 352 |
cfg->ka_interval = 15; |
| 353 |
cfg->ka_data = pj_str("\r\n"); |
| 354 |
cfg->txt_red_level = PJSUA_TXT_DEFAULT_REDUNDANCY_LEVEL; |
| 355 |
cfg->vid_cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV; |
| 356 |
cfg->vid_rend_dev = PJMEDIA_VID_DEFAULT_RENDER_DEV; |
| 357 |
#if PJMEDIA_HAS_VIDEO |
| 358 |
pjmedia_vid_stream_rc_config_default(&cfg->vid_stream_rc_cfg); |
| 359 |
pjmedia_vid_stream_sk_config_default(&cfg->vid_stream_sk_cfg); |
| 360 |
#endif |
| 361 |
pjsua_transport_config_default(&cfg->rtp_cfg); |
| 362 |
cfg->rtp_cfg.port = DEFAULT_RTP_PORT; |
| 363 |
pjmedia_rtcp_fb_setting_default(&cfg->rtcp_fb_cfg); |
| 364 |
|
| 365 |
pjsua_media_config_default(&med_cfg); |
| 366 |
pjsua_ice_config_from_media_config(NULL, &cfg->ice_cfg, &med_cfg); |
| 367 |
pjsua_turn_config_from_media_config(NULL, &cfg->turn_cfg, &med_cfg); |
| 368 |
|
| 369 |
cfg->use_srtp = pjsua_var.ua_cfg.use_srtp; |
| 370 |
cfg->srtp_secure_signaling = pjsua_var.ua_cfg.srtp_secure_signaling; |
| 371 |
cfg->srtp_optional_dup_offer = pjsua_var.ua_cfg.srtp_optional_dup_offer; |
| 372 |
cfg->srtp_opt = pjsua_var.ua_cfg.srtp_opt; |
| 373 |
cfg->reg_retry_interval = PJSUA_REG_RETRY_INTERVAL; |
| 374 |
cfg->reg_retry_random_interval = 10; |
| 375 |
cfg->contact_rewrite_method = PJSUA_CONTACT_REWRITE_METHOD; |
| 376 |
cfg->contact_use_src_port = PJ_TRUE; |
| 377 |
cfg->use_rfc5626 = PJ_TRUE; |
| 378 |
cfg->reg_use_proxy = PJSUA_REG_USE_OUTBOUND_PROXY | |
| 379 |
PJSUA_REG_USE_ACC_PROXY; |
| 380 |
#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0 |
| 381 |
cfg->use_stream_ka = (PJMEDIA_STREAM_ENABLE_KA != 0); |
| 382 |
pjmedia_stream_ka_config_default(&cfg->stream_ka_cfg); |
| 383 |
#endif |
| 384 |
pj_list_init(&cfg->reg_hdr_list); |
| 385 |
pj_list_init(&cfg->sub_hdr_list); |
| 386 |
cfg->call_hold_type = PJSUA_CALL_HOLD_TYPE_DEFAULT; |
| 387 |
cfg->register_on_acc_add = PJ_TRUE; |
| 388 |
cfg->mwi_expires = PJSIP_MWI_DEFAULT_EXPIRES; |
| 389 |
|
| 390 |
cfg->ipv6_sip_use = PJ_HAS_IPV6? PJSUA_IPV6_ENABLED_NO_PREFERENCE : |
| 391 |
PJSUA_IPV6_DISABLED; |
| 392 |
cfg->ipv6_media_use = PJ_HAS_IPV6? PJSUA_IPV6_ENABLED_PREFER_IPV4 : |
| 393 |
PJSUA_IPV6_DISABLED; |
| 394 |
|
| 395 |
cfg->media_stun_use = PJSUA_STUN_RETRY_ON_FAILURE; |
| 396 |
cfg->ip_change_cfg.shutdown_tp = PJ_TRUE; |
| 397 |
cfg->ip_change_cfg.hangup_calls = PJ_FALSE; |
| 398 |
cfg->ip_change_cfg.reinvite_flags = PJSUA_CALL_REINIT_MEDIA | |
| 399 |
PJSUA_CALL_UPDATE_CONTACT | |
| 400 |
PJSUA_CALL_UPDATE_VIA; |
| 401 |
cfg->enable_rtcp_xr = (PJMEDIA_HAS_RTCP_XR && PJMEDIA_STREAM_ENABLE_XR); |
| 402 |
cfg->use_shared_auth = PJ_FALSE; |
| 403 |
|
| 404 |
cfg->auto_repond_sip_message = PJ_TRUE; |
| 405 |
} |
| 406 |
|
| 407 |
PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg) |
| 408 |
{ |
| 409 |
pj_bzero(cfg, sizeof(*cfg)); |
| 410 |
cfg->acc_id = PJSUA_INVALID_ID; |
| 411 |
} |
| 412 |
|
| 413 |
PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg) |
| 414 |
{ |
| 415 |
const pj_sys_info *si = pj_get_sys_info(); |
| 416 |
pj_str_t dev_model = {"iPhone5", 7}; |
| 417 |
|
| 418 |
pj_bzero(cfg, sizeof(*cfg)); |
| 419 |
|
| 420 |
cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
if (pj_stristr(&si->machine, &dev_model) && |
| 426 |
((si->os_ver & 0xFF000000) >> 24) >= 7) |
| 427 |
{ |
| 428 |
cfg->snd_clock_rate = 44100; |
| 429 |
} else { |
| 430 |
cfg->snd_clock_rate = 0; |
| 431 |
} |
| 432 |
cfg->snd_use_sw_clock = PJSUA_DEFAULT_SND_USE_SW_CLOCK; |
| 433 |
cfg->channel_count = 1; |
| 434 |
cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME; |
| 435 |
cfg->max_media_ports = PJSUA_MAX_CONF_PORTS; |
| 436 |
cfg->conf_threads = PJMEDIA_CONF_THREADS; |
| 437 |
cfg->has_ioqueue = PJ_TRUE; |
| 438 |
cfg->thread_cnt = 1; |
| 439 |
cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY; |
| 440 |
cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE; |
| 441 |
cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN; |
| 442 |
cfg->snd_rec_latency = PJMEDIA_SND_DEFAULT_REC_LATENCY; |
| 443 |
cfg->snd_play_latency = PJMEDIA_SND_DEFAULT_PLAY_LATENCY; |
| 444 |
cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1; |
| 445 |
cfg->jb_discard_algo = PJMEDIA_JB_DISCARD_PROGRESSIVE; |
| 446 |
cfg->snd_auto_close_time = 1; |
| 447 |
|
| 448 |
cfg->ice_max_host_cands = -1; |
| 449 |
cfg->ice_always_update = PJ_TRUE; |
| 450 |
pj_ice_sess_options_default(&cfg->ice_opt); |
| 451 |
|
| 452 |
cfg->turn_conn_type = PJ_TURN_TP_UDP; |
| 453 |
#if PJ_HAS_SSL_SOCK |
| 454 |
pj_turn_sock_tls_cfg_default(&cfg->turn_tls_setting); |
| 455 |
#endif |
| 456 |
cfg->vid_preview_enable_native = PJ_TRUE; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata) |
| 475 |
{ |
| 476 |
char addr[PJ_INET6_ADDRSTRLEN+10]; |
| 477 |
pj_str_t input_str = pj_str(rdata->pkt_info.src_name); |
| 478 |
|
| 479 |
PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:\n" |
| 480 |
"%.*s\n" |
| 481 |
"--end msg--", |
| 482 |
rdata->msg_info.len, |
| 483 |
pjsip_rx_data_get_info(rdata), |
| 484 |
rdata->tp_info.transport->type_name, |
| 485 |
pj_addr_str_print(&input_str, |
| 486 |
rdata->pkt_info.src_port, |
| 487 |
addr, |
| 488 |
sizeof(addr), |
| 489 |
1), |
| 490 |
(int)rdata->msg_info.len, |
| 491 |
rdata->msg_info.msg_buf)); |
| 492 |
|
| 493 |
|
| 494 |
return PJ_FALSE; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata) |
| 499 |
{ |
| 500 |
char addr[PJ_INET6_ADDRSTRLEN+10]; |
| 501 |
pj_str_t input_str = pj_str(tdata->tp_info.dst_name); |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:\n" |
| 509 |
"%.*s\n" |
| 510 |
"--end msg--", |
| 511 |
(int)(tdata->buf.cur - tdata->buf.start), |
| 512 |
pjsip_tx_data_get_info(tdata), |
| 513 |
tdata->tp_info.transport->type_name, |
| 514 |
pj_addr_str_print(&input_str, |
| 515 |
tdata->tp_info.dst_port, |
| 516 |
addr, |
| 517 |
sizeof(addr), |
| 518 |
1), |
| 519 |
(int)(tdata->buf.cur - tdata->buf.start), |
| 520 |
tdata->buf.start)); |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
return PJ_SUCCESS; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
static pjsip_module pjsua_msg_logger = |
| 529 |
{ |
| 530 |
NULL, NULL, |
| 531 |
{ "mod-pjsua-log", 13 }, |
| 532 |
-1, |
| 533 |
PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1, |
| 534 |
NULL, |
| 535 |
NULL, |
| 536 |
NULL, |
| 537 |
NULL, |
| 538 |
&logging_on_rx_msg, |
| 539 |
&logging_on_rx_msg, |
| 540 |
&logging_on_tx_msg, |
| 541 |
&logging_on_tx_msg, |
| 542 |
NULL, |
| 543 |
|
| 544 |
}; |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata) |
| 553 |
{ |
| 554 |
pjsip_tx_data *tdata; |
| 555 |
pjsip_response_addr res_addr; |
| 556 |
const pjsip_hdr *cap_hdr; |
| 557 |
pj_status_t status; |
| 558 |
|
| 559 |
|
| 560 |
if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, |
| 561 |
pjsip_get_options_method()) != 0) |
| 562 |
{ |
| 563 |
return PJ_FALSE; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
if (pjsua_var.thread_quit_flag) { |
| 568 |
pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 569 |
PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL, |
| 570 |
NULL, NULL); |
| 571 |
return PJ_TRUE; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL, |
| 576 |
&tdata); |
| 577 |
if (status != PJ_SUCCESS) { |
| 578 |
pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status); |
| 579 |
return PJ_TRUE; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL); |
| 584 |
if (cap_hdr) { |
| 585 |
pjsip_msg_add_hdr(tdata->msg, |
| 586 |
(pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL); |
| 591 |
if (cap_hdr) { |
| 592 |
pjsip_msg_add_hdr(tdata->msg, |
| 593 |
(pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL); |
| 598 |
if (cap_hdr) { |
| 599 |
pjsip_msg_add_hdr(tdata->msg, |
| 600 |
(pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL); |
| 605 |
if (cap_hdr) { |
| 606 |
pjsip_msg_add_hdr(tdata->msg, |
| 607 |
(pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
if (pjsua_var.ua_cfg.user_agent.slen) { |
| 612 |
const pj_str_t USER_AGENT = { "User-Agent", 10}; |
| 613 |
pjsip_hdr *h; |
| 614 |
|
| 615 |
h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, |
| 616 |
&USER_AGENT, |
| 617 |
&pjsua_var.ua_cfg.user_agent); |
| 618 |
pjsip_msg_add_hdr(tdata->msg, h); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
#if DISABLED_FOR_TICKET_1185 |
| 623 |
if (pjsua_var.calls[0].med_tp) { |
| 624 |
pjmedia_transport_info tpinfo; |
| 625 |
pjmedia_sdp_session *sdp; |
| 626 |
|
| 627 |
pjmedia_transport_info_init(&tpinfo); |
| 628 |
pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo); |
| 629 |
|
| 630 |
|
| 631 |
status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1, |
| 632 |
&tpinfo.sock_info, &sdp); |
| 633 |
if (status == PJ_SUCCESS) { |
| 634 |
pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body); |
| 635 |
} |
| 636 |
} |
| 637 |
#endif |
| 638 |
|
| 639 |
|
| 640 |
pjsip_get_response_addr(tdata->pool, rdata, &res_addr); |
| 641 |
status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL); |
| 642 |
if (status != PJ_SUCCESS) |
| 643 |
pjsip_tx_data_dec_ref(tdata); |
| 644 |
|
| 645 |
return PJ_TRUE; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
static pjsip_module pjsua_options_handler = |
| 651 |
{ |
| 652 |
NULL, NULL, |
| 653 |
{ "mod-pjsua-options", 17 }, |
| 654 |
-1, |
| 655 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 656 |
NULL, |
| 657 |
NULL, |
| 658 |
NULL, |
| 659 |
NULL, |
| 660 |
&options_on_rx_request, |
| 661 |
NULL, |
| 662 |
NULL, |
| 663 |
NULL, |
| 664 |
NULL, |
| 665 |
|
| 666 |
}; |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata) |
| 685 |
{ |
| 686 |
pj_bool_t processed = PJ_FALSE; |
| 687 |
|
| 688 |
#if PJSUA_DETECT_MERGED_REQUESTS |
| 689 |
pjsip_transaction *tsx; |
| 690 |
if ((tsx = pjsip_tsx_detect_merged_requests(rdata)) != NULL) { |
| 691 |
|
| 692 |
pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 693 |
|
| 694 |
PJ_LOG(4, (THIS_FILE, "Merged request detected (%s) (%s): %s from %s:%d", |
| 695 |
dlg ? dlg->obj_name : "-no-dlg-", |
| 696 |
tsx->obj_name, |
| 697 |
pjsip_rx_data_get_info(rdata), |
| 698 |
rdata->pkt_info.src_name, |
| 699 |
rdata->pkt_info.src_port)); |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, |
| 705 |
&pjsip_ack_method) == 0) { |
| 706 |
return PJ_FALSE; |
| 707 |
} else { |
| 708 |
|
| 709 |
pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, |
| 710 |
PJSIP_SC_LOOP_DETECTED, NULL, |
| 711 |
NULL, NULL, NULL); |
| 712 |
return PJ_TRUE; |
| 713 |
} |
| 714 |
} |
| 715 |
#endif |
| 716 |
|
| 717 |
PJSUA_LOCK(); |
| 718 |
|
| 719 |
if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) { |
| 720 |
|
| 721 |
processed = pjsua_call_on_incoming(rdata); |
| 722 |
} |
| 723 |
|
| 724 |
PJSUA_UNLOCK(); |
| 725 |
|
| 726 |
return processed; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata) |
| 742 |
{ |
| 743 |
PJ_UNUSED_ARG(rdata); |
| 744 |
return PJ_FALSE; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
static void log_writer(int level, const char *buffer, int len) |
| 754 |
{ |
| 755 |
|
| 756 |
|
| 757 |
if (pjsua_var.log_file) { |
| 758 |
pj_ssize_t size = len; |
| 759 |
pj_file_write(pjsua_var.log_file, buffer, &size); |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
} |
| 764 |
|
| 765 |
if (level <= (int)pjsua_var.log_cfg.console_level) { |
| 766 |
if (pjsua_var.log_cfg.cb) |
| 767 |
(*pjsua_var.log_cfg.cb)(level, buffer, len); |
| 768 |
else |
| 769 |
pj_log_write(level, buffer, len); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg) |
| 779 |
{ |
| 780 |
pj_status_t status; |
| 781 |
|
| 782 |
PJ_ASSERT_RETURN(cfg, PJ_EINVAL); |
| 783 |
|
| 784 |
|
| 785 |
pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg); |
| 786 |
|
| 787 |
|
| 788 |
pj_log_set_log_func( &log_writer ); |
| 789 |
|
| 790 |
|
| 791 |
pj_log_set_decor(pjsua_var.log_cfg.decor); |
| 792 |
|
| 793 |
|
| 794 |
pj_log_set_level(pjsua_var.log_cfg.level); |
| 795 |
|
| 796 |
|
| 797 |
if (pjsua_var.log_file) { |
| 798 |
pj_file_close(pjsua_var.log_file); |
| 799 |
pjsua_var.log_file = NULL; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
if (pjsua_var.log_cfg.log_filename.slen) { |
| 804 |
unsigned flags = PJ_O_WRONLY | PJ_O_CLOEXEC; |
| 805 |
flags |= pjsua_var.log_cfg.log_file_flags; |
| 806 |
status = pj_file_open(pjsua_var.pool, |
| 807 |
pjsua_var.log_cfg.log_filename.ptr, |
| 808 |
flags, |
| 809 |
&pjsua_var.log_file); |
| 810 |
|
| 811 |
if (status != PJ_SUCCESS) { |
| 812 |
pjsua_perror(THIS_FILE, "Error creating log file", status); |
| 813 |
return status; |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
if (pjsua_msg_logger.id >= 0) { |
| 819 |
pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger); |
| 820 |
pjsua_msg_logger.id = -1; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
if (pjsua_var.log_cfg.msg_logging) { |
| 825 |
status = pjsip_endpt_register_module(pjsua_var.endpt, |
| 826 |
&pjsua_msg_logger); |
| 827 |
if (status != PJ_SUCCESS) |
| 828 |
return status; |
| 829 |
} |
| 830 |
|
| 831 |
return PJ_SUCCESS; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
#if PJSUA_SEPARATE_WORKER_FOR_TIMER |
| 840 |
|
| 841 |
|
| 842 |
static int worker_thread_timer(void *arg) |
| 843 |
{ |
| 844 |
pj_timer_heap_t *th; |
| 845 |
|
| 846 |
PJ_UNUSED_ARG(arg); |
| 847 |
|
| 848 |
th = pjsip_endpt_get_timer_heap(pjsua_var.endpt); |
| 849 |
while (!pjsua_var.thread_quit_flag) { |
| 850 |
pj_time_val timeout = {0, 0}; |
| 851 |
int c; |
| 852 |
|
| 853 |
c = pj_timer_heap_poll(th, &timeout); |
| 854 |
if (c == 0) { |
| 855 |
|
| 856 |
enum { MAX_SLEEP_MS = 100 }; |
| 857 |
if (PJ_TIME_VAL_MSEC(timeout) < MAX_SLEEP_MS) |
| 858 |
pj_thread_sleep(PJ_TIME_VAL_MSEC(timeout)); |
| 859 |
else |
| 860 |
pj_thread_sleep(MAX_SLEEP_MS); |
| 861 |
} |
| 862 |
} |
| 863 |
return 0; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
static int worker_thread_ioqueue(void *arg) |
| 868 |
{ |
| 869 |
pj_ioqueue_t *ioq; |
| 870 |
|
| 871 |
PJ_UNUSED_ARG(arg); |
| 872 |
|
| 873 |
ioq = pjsip_endpt_get_ioqueue(pjsua_var.endpt); |
| 874 |
while (!pjsua_var.thread_quit_flag) { |
| 875 |
pj_time_val timeout = {0, 100}; |
| 876 |
pj_ioqueue_poll(ioq, &timeout); |
| 877 |
} |
| 878 |
return 0; |
| 879 |
} |
| 880 |
|
| 881 |
#else |
| 882 |
|
| 883 |
|
| 884 |
static int worker_thread(void *arg) |
| 885 |
{ |
| 886 |
enum { TIMEOUT = 10 }; |
| 887 |
|
| 888 |
PJ_UNUSED_ARG(arg); |
| 889 |
|
| 890 |
while (!pjsua_var.thread_quit_flag) { |
| 891 |
int count; |
| 892 |
|
| 893 |
count = pjsua_handle_events(TIMEOUT); |
| 894 |
if (count < 0) |
| 895 |
pj_thread_sleep(TIMEOUT); |
| 896 |
} |
| 897 |
|
| 898 |
return 0; |
| 899 |
} |
| 900 |
|
| 901 |
#endif |
| 902 |
|
| 903 |
PJ_DEF(void) pjsua_stop_worker_threads(void) |
| 904 |
{ |
| 905 |
unsigned i; |
| 906 |
|
| 907 |
pjsua_var.thread_quit_flag = 1; |
| 908 |
|
| 909 |
|
| 910 |
for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) { |
| 911 |
if (pjsua_var.thread[i]) { |
| 912 |
pj_status_t status; |
| 913 |
status = pj_thread_join(pjsua_var.thread[i]); |
| 914 |
if (status != PJ_SUCCESS) { |
| 915 |
PJ_PERROR(4,(THIS_FILE, status, "Error joining worker thread")); |
| 916 |
pj_thread_sleep(1000); |
| 917 |
} |
| 918 |
pj_thread_destroy(pjsua_var.thread[i]); |
| 919 |
pjsua_var.thread[i] = NULL; |
| 920 |
} |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
static void init_random_seed(void) |
| 926 |
{ |
| 927 |
pj_sockaddr addr; |
| 928 |
const pj_str_t *hostname; |
| 929 |
pj_uint32_t pid; |
| 930 |
pj_time_val t; |
| 931 |
unsigned seed=0; |
| 932 |
|
| 933 |
|
| 934 |
hostname = pj_gethostname(); |
| 935 |
seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen); |
| 936 |
|
| 937 |
|
| 938 |
if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS) |
| 939 |
seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4); |
| 940 |
|
| 941 |
|
| 942 |
pj_gettimeofday(&t); |
| 943 |
seed = pj_hash_calc(seed, &t, sizeof(t)); |
| 944 |
|
| 945 |
|
| 946 |
pid = pj_getpid(); |
| 947 |
seed = pj_hash_calc(seed, &pid, sizeof(pid)); |
| 948 |
|
| 949 |
|
| 950 |
pj_srand(seed); |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
PJ_DEF(pj_status_t) pjsua_create(void) |
| 957 |
{ |
| 958 |
pj_status_t status; |
| 959 |
|
| 960 |
|
| 961 |
init_data(); |
| 962 |
|
| 963 |
|
| 964 |
pjsua_logging_config_default(&pjsua_var.log_cfg); |
| 965 |
|
| 966 |
|
| 967 |
status = pj_init(); |
| 968 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 969 |
|
| 970 |
pj_log_push_indent(); |
| 971 |
|
| 972 |
|
| 973 |
init_random_seed(); |
| 974 |
|
| 975 |
|
| 976 |
status = pjlib_util_init(); |
| 977 |
if (status != PJ_SUCCESS) { |
| 978 |
pj_log_pop_indent(); |
| 979 |
pjsua_perror(THIS_FILE, "Failed in initializing pjlib-util", status); |
| 980 |
pj_shutdown(); |
| 981 |
return status; |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
status = pjnath_init(); |
| 986 |
if (status != PJ_SUCCESS) { |
| 987 |
pj_log_pop_indent(); |
| 988 |
pjsua_perror(THIS_FILE, "Failed in initializing pjnath", status); |
| 989 |
pj_shutdown(); |
| 990 |
return status; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
pjsua_var.cap_dev = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV; |
| 995 |
pjsua_var.play_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; |
| 996 |
|
| 997 |
|
| 998 |
pjsua_var.vcap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV; |
| 999 |
pjsua_var.vrdr_dev = PJMEDIA_VID_DEFAULT_RENDER_DEV; |
| 1000 |
|
| 1001 |
|
| 1002 |
pj_caching_pool_init(&pjsua_var.cp, NULL, 0); |
| 1003 |
|
| 1004 |
|
| 1005 |
pjsua_var.pool = pjsua_pool_create("pjsua", PJSUA_POOL_LEN, PJSUA_POOL_INC); |
| 1006 |
pjsua_var.timer_pool = pjsua_pool_create("pjsua_timer", 500, 500); |
| 1007 |
if (pjsua_var.pool == NULL || pjsua_var.timer_pool == NULL) { |
| 1008 |
pj_log_pop_indent(); |
| 1009 |
status = PJ_ENOMEM; |
| 1010 |
pjsua_perror(THIS_FILE, "Unable to create pjsua/timer pool", status); |
| 1011 |
pj_shutdown(); |
| 1012 |
return status; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua", |
| 1017 |
&pjsua_var.mutex); |
| 1018 |
if (status != PJ_SUCCESS) { |
| 1019 |
pj_log_pop_indent(); |
| 1020 |
pjsua_perror(THIS_FILE, "Unable to create mutex", status); |
| 1021 |
pjsua_destroy(); |
| 1022 |
return status; |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
status = pjsip_endpt_create(&pjsua_var.cp.factory, |
| 1029 |
pj_gethostname()->ptr, |
| 1030 |
&pjsua_var.endpt); |
| 1031 |
if (status != PJ_SUCCESS) { |
| 1032 |
pj_log_pop_indent(); |
| 1033 |
pjsua_perror(THIS_FILE, "Unable to create endpoint", status); |
| 1034 |
pjsua_destroy(); |
| 1035 |
return status; |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
pj_list_init(&pjsua_var.active_timer_list); |
| 1040 |
pj_list_init(&pjsua_var.timer_list); |
| 1041 |
pj_list_init(&pjsua_var.event_list); |
| 1042 |
|
| 1043 |
|
| 1044 |
status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua_timer", |
| 1045 |
&pjsua_var.timer_mutex); |
| 1046 |
if (status != PJ_SUCCESS) { |
| 1047 |
pj_log_pop_indent(); |
| 1048 |
pjsua_perror(THIS_FILE, "Unable to create mutex", status); |
| 1049 |
pjsua_destroy(); |
| 1050 |
return status; |
| 1051 |
} |
| 1052 |
|
| 1053 |
pjsua_set_state(PJSUA_STATE_CREATED); |
| 1054 |
pj_log_pop_indent(); |
| 1055 |
return PJ_SUCCESS; |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
#if defined(PJNATH_HAS_UPNP) && (PJNATH_HAS_UPNP != 0) |
| 1060 |
|
| 1061 |
static void upnp_cb(pj_status_t status) |
| 1062 |
{ |
| 1063 |
pjsua_var.upnp_status = status; |
| 1064 |
} |
| 1065 |
#endif |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg, |
| 1073 |
const pjsua_logging_config *log_cfg, |
| 1074 |
const pjsua_media_config *media_cfg) |
| 1075 |
{ |
| 1076 |
pjsua_config default_cfg; |
| 1077 |
pjsua_media_config default_media_cfg; |
| 1078 |
const pj_str_t STR_OPTIONS = { "OPTIONS", 7 }; |
| 1079 |
pjsip_ua_init_param ua_init_param; |
| 1080 |
unsigned i; |
| 1081 |
pj_status_t status; |
| 1082 |
|
| 1083 |
pj_log_push_indent(); |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
if (ua_cfg == NULL) { |
| 1088 |
pjsua_config_default(&default_cfg); |
| 1089 |
ua_cfg = &default_cfg; |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (media_cfg == NULL) { |
| 1093 |
pjsua_media_config_default(&default_media_cfg); |
| 1094 |
media_cfg = &default_media_cfg; |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
if (log_cfg) { |
| 1099 |
status = pjsua_reconfigure_logging(log_cfg); |
| 1100 |
if (status != PJ_SUCCESS) |
| 1101 |
goto on_error; |
| 1102 |
} |
| 1103 |
|
| 1104 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 1105 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT != 0 |
| 1106 |
if (!(pj_get_sys_info()->flags & PJ_SYS_HAS_IOS_BG)) { |
| 1107 |
PJ_LOG(5, (THIS_FILE, "Device does not support " |
| 1108 |
"background mode")); |
| 1109 |
pj_activesock_enable_iphone_os_bg(PJ_FALSE); |
| 1110 |
} |
| 1111 |
#endif |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
if (ua_cfg->nameserver_count) { |
| 1117 |
#if PJSIP_HAS_RESOLVER |
| 1118 |
unsigned ii; |
| 1119 |
|
| 1120 |
|
| 1121 |
status = pjsip_endpt_create_resolver(pjsua_var.endpt, |
| 1122 |
&pjsua_var.resolver); |
| 1123 |
if (status != PJ_SUCCESS) { |
| 1124 |
pjsua_perror(THIS_FILE, "Error creating resolver", status); |
| 1125 |
goto on_error; |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
status = pj_dns_resolver_set_ns(pjsua_var.resolver, |
| 1130 |
ua_cfg->nameserver_count, |
| 1131 |
ua_cfg->nameserver, NULL); |
| 1132 |
if (status != PJ_SUCCESS) { |
| 1133 |
pjsua_perror(THIS_FILE, "Error setting nameserver", status); |
| 1134 |
goto on_error; |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver); |
| 1139 |
if (status != PJ_SUCCESS) { |
| 1140 |
pjsua_perror(THIS_FILE, "Error setting DNS resolver", status); |
| 1141 |
goto on_error; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
for (ii=0; ii<ua_cfg->nameserver_count; ++ii) { |
| 1146 |
PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added", |
| 1147 |
(int)ua_cfg->nameserver[ii].slen, |
| 1148 |
ua_cfg->nameserver[ii].ptr)); |
| 1149 |
} |
| 1150 |
#else |
| 1151 |
PJ_LOG(2,(THIS_FILE, |
| 1152 |
"DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)")); |
| 1153 |
#endif |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
status = pjsip_tsx_layer_init_module(pjsua_var.endpt); |
| 1160 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
pj_bzero(&ua_init_param, sizeof(ua_init_param)); |
| 1165 |
if (ua_cfg->hangup_forked_call) { |
| 1166 |
ua_init_param.on_dlg_forked = &on_dlg_forked; |
| 1167 |
} |
| 1168 |
status = pjsip_ua_init_module( pjsua_var.endpt, &ua_init_param); |
| 1169 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
status = pjsip_replaces_init_module( pjsua_var.endpt ); |
| 1174 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1175 |
|
| 1176 |
|
| 1177 |
status = pjsip_100rel_init_module(pjsua_var.endpt); |
| 1178 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1179 |
|
| 1180 |
|
| 1181 |
status = pjsip_timer_init_module(pjsua_var.endpt); |
| 1182 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1183 |
|
| 1184 |
#if PJSUA_HAS_SIPREC |
| 1185 |
|
| 1186 |
status = pjsip_siprec_init_module(pjsua_var.endpt); |
| 1187 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1188 |
#endif |
| 1189 |
|
| 1190 |
|
| 1191 |
{ |
| 1192 |
const pjsip_module mod_initializer = |
| 1193 |
{ |
| 1194 |
NULL, NULL, |
| 1195 |
{ "mod-pjsua", 9 }, |
| 1196 |
-1, |
| 1197 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 1198 |
NULL, |
| 1199 |
NULL, |
| 1200 |
NULL, |
| 1201 |
NULL, |
| 1202 |
&mod_pjsua_on_rx_request, |
| 1203 |
&mod_pjsua_on_rx_response, |
| 1204 |
NULL, |
| 1205 |
NULL, |
| 1206 |
NULL, |
| 1207 |
}; |
| 1208 |
|
| 1209 |
pjsua_var.mod = mod_initializer; |
| 1210 |
|
| 1211 |
status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod); |
| 1212 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
for (i=0; i<ua_cfg->outbound_proxy_cnt; ++i) { |
| 1217 |
pj_str_t tmp; |
| 1218 |
pj_str_t hname = { "Route", 5}; |
| 1219 |
pjsip_route_hdr *r; |
| 1220 |
|
| 1221 |
pj_strdup_with_null(pjsua_var.pool, &tmp, &ua_cfg->outbound_proxy[i]); |
| 1222 |
|
| 1223 |
r = (pjsip_route_hdr*) |
| 1224 |
pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr, |
| 1225 |
(unsigned)tmp.slen, NULL); |
| 1226 |
if (r == NULL) { |
| 1227 |
pjsua_perror(THIS_FILE, "Invalid outbound proxy URI", |
| 1228 |
PJSIP_EINVALIDURI); |
| 1229 |
status = PJSIP_EINVALIDURI; |
| 1230 |
goto on_error; |
| 1231 |
} |
| 1232 |
|
| 1233 |
if (pjsua_var.ua_cfg.force_lr) { |
| 1234 |
pjsip_sip_uri *sip_url; |
| 1235 |
if (!PJSIP_URI_SCHEME_IS_SIP(r->name_addr.uri) && |
| 1236 |
!PJSIP_URI_SCHEME_IS_SIPS(r->name_addr.uri)) |
| 1237 |
{ |
| 1238 |
status = PJSIP_EINVALIDSCHEME; |
| 1239 |
goto on_error; |
| 1240 |
} |
| 1241 |
sip_url = (pjsip_sip_uri*)r->name_addr.uri; |
| 1242 |
sip_url->lr_param = 1; |
| 1243 |
} |
| 1244 |
|
| 1245 |
pj_list_push_back(&pjsua_var.outbound_proxy, r); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
status = pjsua_call_subsys_init(ua_cfg); |
| 1251 |
if (status != PJ_SUCCESS) |
| 1252 |
goto on_error; |
| 1253 |
|
| 1254 |
|
| 1255 |
if (pjsua_var.ua_cfg.stun_srv_cnt==0) { |
| 1256 |
if (pjsua_var.ua_cfg.stun_domain.slen) { |
| 1257 |
pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] = |
| 1258 |
pjsua_var.ua_cfg.stun_domain; |
| 1259 |
} |
| 1260 |
if (pjsua_var.ua_cfg.stun_host.slen) { |
| 1261 |
pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] = |
| 1262 |
pjsua_var.ua_cfg.stun_host; |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
#if !PJ_HAS_IPV6 |
| 1267 |
pjsua_var.ua_cfg.stun_try_ipv6 = PJ_FALSE; |
| 1268 |
#endif |
| 1269 |
|
| 1270 |
|
| 1271 |
status = resolve_stun_server(PJ_FALSE, PJ_FALSE, 0); |
| 1272 |
if (status != PJ_SUCCESS && status != PJ_EPENDING) { |
| 1273 |
pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 1274 |
goto on_error; |
| 1275 |
} |
| 1276 |
|
| 1277 |
#if defined(PJNATH_HAS_UPNP) && (PJNATH_HAS_UPNP != 0) |
| 1278 |
|
| 1279 |
if (pjsua_var.ua_cfg.enable_upnp) { |
| 1280 |
pj_upnp_init_param param; |
| 1281 |
|
| 1282 |
pj_bzero(¶m, sizeof(param)); |
| 1283 |
param.factory = pjsua_var.pool->factory; |
| 1284 |
if (pjsua_var.ua_cfg.upnp_if_name.slen > 0) |
| 1285 |
param.if_name = pjsua_var.ua_cfg.upnp_if_name.ptr; |
| 1286 |
param.upnp_cb = &upnp_cb; |
| 1287 |
|
| 1288 |
pjsua_var.upnp_status = pj_upnp_init(¶m); |
| 1289 |
if (pjsua_var.upnp_status != PJ_SUCCESS) { |
| 1290 |
pjsua_perror(THIS_FILE, "Error initializing UPnP", |
| 1291 |
pjsua_var.upnp_status); |
| 1292 |
} |
| 1293 |
} |
| 1294 |
#endif |
| 1295 |
|
| 1296 |
|
| 1297 |
status = pjsua_media_subsys_init(media_cfg); |
| 1298 |
if (status != PJ_SUCCESS) |
| 1299 |
goto on_error; |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
status = pjsip_evsub_init_module(pjsua_var.endpt); |
| 1304 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance()); |
| 1309 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1310 |
|
| 1311 |
#if PJSUA_HAS_DLG_EVENT_PKG |
| 1312 |
status = pjsip_dlg_event_init_module( pjsua_var.endpt, pjsip_evsub_instance()); |
| 1313 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1314 |
#endif |
| 1315 |
|
| 1316 |
|
| 1317 |
status = pjsip_mwi_init_module(pjsua_var.endpt, pjsip_evsub_instance()); |
| 1318 |
|
| 1319 |
|
| 1320 |
pjsip_publishc_init_module(pjsua_var.endpt); |
| 1321 |
|
| 1322 |
|
| 1323 |
status = pjsip_xfer_init_module( pjsua_var.endpt ); |
| 1324 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1325 |
|
| 1326 |
|
| 1327 |
status = pjsua_pres_init(); |
| 1328 |
if (status != PJ_SUCCESS) |
| 1329 |
goto on_error; |
| 1330 |
|
| 1331 |
|
| 1332 |
status = pjsua_im_init(); |
| 1333 |
if (status != PJ_SUCCESS) |
| 1334 |
goto on_error; |
| 1335 |
|
| 1336 |
|
| 1337 |
status = pjsip_endpt_register_module(pjsua_var.endpt, |
| 1338 |
&pjsua_options_handler); |
| 1339 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1340 |
|
| 1341 |
|
| 1342 |
pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW, |
| 1343 |
NULL, 1, &STR_OPTIONS); |
| 1344 |
|
| 1345 |
|
| 1346 |
if (pjsua_var.ua_cfg.thread_cnt) { |
| 1347 |
unsigned ii; |
| 1348 |
|
| 1349 |
if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread)) |
| 1350 |
pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread); |
| 1351 |
|
| 1352 |
#if PJSUA_SEPARATE_WORKER_FOR_TIMER |
| 1353 |
if (pjsua_var.ua_cfg.thread_cnt < 2) |
| 1354 |
pjsua_var.ua_cfg.thread_cnt = 2; |
| 1355 |
#endif |
| 1356 |
|
| 1357 |
for (ii=0; ii<pjsua_var.ua_cfg.thread_cnt; ++ii) { |
| 1358 |
char tname[PJ_MAX_OBJ_NAME]; |
| 1359 |
|
| 1360 |
pj_ansi_snprintf(tname, sizeof(tname), "pjsua_%d", ii); |
| 1361 |
|
| 1362 |
#if PJSUA_SEPARATE_WORKER_FOR_TIMER |
| 1363 |
if (ii == 0) { |
| 1364 |
status = pj_thread_create(pjsua_var.pool, tname, |
| 1365 |
&worker_thread_timer, |
| 1366 |
NULL, 0, 0, &pjsua_var.thread[ii]); |
| 1367 |
} else { |
| 1368 |
status = pj_thread_create(pjsua_var.pool, tname, |
| 1369 |
&worker_thread_ioqueue, |
| 1370 |
NULL, 0, 0, &pjsua_var.thread[ii]); |
| 1371 |
} |
| 1372 |
#else |
| 1373 |
status = pj_thread_create(pjsua_var.pool, tname, &worker_thread, |
| 1374 |
NULL, 0, 0, &pjsua_var.thread[ii]); |
| 1375 |
#endif |
| 1376 |
if (status != PJ_SUCCESS) |
| 1377 |
goto on_error; |
| 1378 |
} |
| 1379 |
PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created", |
| 1380 |
pjsua_var.ua_cfg.thread_cnt)); |
| 1381 |
} else { |
| 1382 |
PJ_LOG(4,(THIS_FILE, "No SIP worker threads created")); |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized", |
| 1388 |
pj_get_version(), pj_get_sys_info()->info.ptr)); |
| 1389 |
|
| 1390 |
pjsua_set_state(PJSUA_STATE_INIT); |
| 1391 |
pj_log_pop_indent(); |
| 1392 |
return PJ_SUCCESS; |
| 1393 |
|
| 1394 |
on_error: |
| 1395 |
pj_log_pop_indent(); |
| 1396 |
return status; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
static void busy_sleep(unsigned msec) |
| 1402 |
{ |
| 1403 |
pj_time_val timeout, now; |
| 1404 |
|
| 1405 |
pj_gettickcount(&timeout); |
| 1406 |
timeout.msec += msec; |
| 1407 |
pj_time_val_normalize(&timeout); |
| 1408 |
|
| 1409 |
do { |
| 1410 |
int i; |
| 1411 |
i = msec / 10; |
| 1412 |
while (pjsua_handle_events(10) > 0 && i > 0) |
| 1413 |
--i; |
| 1414 |
pj_gettickcount(&now); |
| 1415 |
} while (PJ_TIME_VAL_LT(now, timeout)); |
| 1416 |
} |
| 1417 |
|
| 1418 |
static void stun_resolve_add_ref(pjsua_stun_resolve *sess) |
| 1419 |
{ |
| 1420 |
++sess->ref_cnt; |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
static void release_stun_session(pjsua_stun_resolve *sess) |
| 1425 |
{ |
| 1426 |
PJSUA_LOCK(); |
| 1427 |
pj_list_erase(sess); |
| 1428 |
PJSUA_UNLOCK(); |
| 1429 |
|
| 1430 |
pj_assert(sess->stun_sock==NULL); |
| 1431 |
pj_pool_release(sess->pool); |
| 1432 |
} |
| 1433 |
|
| 1434 |
static void destroy_stun_resolve_cb(pj_timer_heap_t *t, pj_timer_entry *e) |
| 1435 |
{ |
| 1436 |
pjsua_stun_resolve *sess = (pjsua_stun_resolve*)e->user_data; |
| 1437 |
PJ_UNUSED_ARG(t); |
| 1438 |
|
| 1439 |
release_stun_session(sess); |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
static void destroy_stun_resolve(pjsua_stun_resolve *sess, pj_bool_t forced) |
| 1444 |
{ |
| 1445 |
pj_time_val timeout = {0, 0}; |
| 1446 |
|
| 1447 |
if (sess->destroy_flag) |
| 1448 |
return; |
| 1449 |
|
| 1450 |
sess->destroy_flag = PJ_TRUE; |
| 1451 |
if (sess->stun_sock) { |
| 1452 |
pj_stun_sock_destroy(sess->stun_sock); |
| 1453 |
sess->stun_sock = NULL; |
| 1454 |
} |
| 1455 |
|
| 1456 |
if (pjsua_var.stun_status == PJ_EUNKNOWN || |
| 1457 |
pjsua_var.stun_status == PJ_EPENDING) |
| 1458 |
{ |
| 1459 |
pjsua_var.stun_status = PJNATH_ESTUNDESTROYED; |
| 1460 |
} |
| 1461 |
|
| 1462 |
if (forced) { |
| 1463 |
release_stun_session(sess); |
| 1464 |
} else { |
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
pj_timer_entry_init(&sess->timer, 0, (void*)sess, |
| 1471 |
&destroy_stun_resolve_cb); |
| 1472 |
pjsua_schedule_timer(&sess->timer, &timeout); |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
static void stun_resolve_dec_ref(pjsua_stun_resolve *sess) |
| 1477 |
{ |
| 1478 |
int ref_cnt = --sess->ref_cnt; |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
if ((ref_cnt > 0) || |
| 1483 |
(sess->blocking && (sess->waiter != pj_thread_this()))) |
| 1484 |
{ |
| 1485 |
return; |
| 1486 |
} |
| 1487 |
|
| 1488 |
destroy_stun_resolve(sess, PJ_FALSE); |
| 1489 |
} |
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
static void stun_resolve_complete(pjsua_stun_resolve *sess) |
| 1496 |
{ |
| 1497 |
pj_stun_resolve_result result; |
| 1498 |
|
| 1499 |
if (sess->has_result) |
| 1500 |
goto on_return; |
| 1501 |
|
| 1502 |
pj_bzero(&result, sizeof(result)); |
| 1503 |
result.token = sess->token; |
| 1504 |
result.status = sess->status; |
| 1505 |
result.name = sess->srv[sess->idx]; |
| 1506 |
result.index = sess->idx; |
| 1507 |
pj_memcpy(&result.addr, &sess->addr, sizeof(result.addr)); |
| 1508 |
sess->has_result = PJ_TRUE; |
| 1509 |
|
| 1510 |
if (result.status == PJ_SUCCESS) { |
| 1511 |
char addr[PJ_INET6_ADDRSTRLEN+10]; |
| 1512 |
pj_sockaddr_print(&result.addr, addr, sizeof(addr), 3); |
| 1513 |
PJ_LOG(4,(THIS_FILE, |
| 1514 |
"STUN resolution success, using %.*s, address is %s", |
| 1515 |
(int)sess->srv[sess->idx].slen, |
| 1516 |
sess->srv[sess->idx].ptr, |
| 1517 |
addr)); |
| 1518 |
} else { |
| 1519 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 1520 |
pj_strerror(result.status, errmsg, sizeof(errmsg)); |
| 1521 |
PJ_LOG(1,(THIS_FILE, "STUN resolution failed: %s", errmsg)); |
| 1522 |
} |
| 1523 |
|
| 1524 |
sess->cb(&result); |
| 1525 |
|
| 1526 |
on_return: |
| 1527 |
if (!sess->blocking) { |
| 1528 |
stun_resolve_dec_ref(sess); |
| 1529 |
} |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
static pj_bool_t test_stun_on_status(pj_stun_sock *stun_sock, |
| 1537 |
pj_stun_sock_op op, |
| 1538 |
pj_status_t status) |
| 1539 |
{ |
| 1540 |
pjsua_stun_resolve *sess; |
| 1541 |
|
| 1542 |
sess = (pjsua_stun_resolve*) pj_stun_sock_get_user_data(stun_sock); |
| 1543 |
pj_assert(stun_sock == sess->stun_sock); |
| 1544 |
|
| 1545 |
if (status != PJ_SUCCESS) { |
| 1546 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 1547 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 1548 |
|
| 1549 |
PJ_LOG(4,(THIS_FILE, "STUN resolution for %.*s failed: %s", |
| 1550 |
(int)sess->srv[sess->idx].slen, |
| 1551 |
sess->srv[sess->idx].ptr, errmsg)); |
| 1552 |
|
| 1553 |
if (op == PJ_STUN_SOCK_BINDING_OP && !sess->async_wait) { |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
return PJ_FALSE; |
| 1559 |
} |
| 1560 |
|
| 1561 |
pj_stun_sock_destroy(stun_sock); |
| 1562 |
sess->stun_sock = NULL; |
| 1563 |
|
| 1564 |
stun_resolve_add_ref(sess); |
| 1565 |
|
| 1566 |
if (pjsua_var.ua_cfg.stun_try_ipv6 && sess->af == pj_AF_INET()) { |
| 1567 |
sess->af = pj_AF_INET6(); |
| 1568 |
} else { |
| 1569 |
++sess->idx; |
| 1570 |
sess->af = pj_AF_INET(); |
| 1571 |
if (sess->idx >= sess->count) |
| 1572 |
sess->status = status; |
| 1573 |
} |
| 1574 |
|
| 1575 |
resolve_stun_entry(sess); |
| 1576 |
|
| 1577 |
stun_resolve_dec_ref(sess); |
| 1578 |
|
| 1579 |
return PJ_FALSE; |
| 1580 |
|
| 1581 |
} else if (op == PJ_STUN_SOCK_BINDING_OP) { |
| 1582 |
pj_stun_sock_info ssi; |
| 1583 |
|
| 1584 |
pj_stun_sock_get_info(stun_sock, &ssi); |
| 1585 |
pj_memcpy(&sess->addr, &ssi.srv_addr, sizeof(sess->addr)); |
| 1586 |
|
| 1587 |
stun_resolve_add_ref(sess); |
| 1588 |
|
| 1589 |
sess->status = PJ_SUCCESS; |
| 1590 |
pj_stun_sock_destroy(stun_sock); |
| 1591 |
sess->stun_sock = NULL; |
| 1592 |
|
| 1593 |
stun_resolve_complete(sess); |
| 1594 |
|
| 1595 |
stun_resolve_dec_ref(sess); |
| 1596 |
|
| 1597 |
return PJ_FALSE; |
| 1598 |
|
| 1599 |
} else |
| 1600 |
return PJ_TRUE; |
| 1601 |
|
| 1602 |
} |
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
static void resolve_stun_entry(pjsua_stun_resolve *sess) |
| 1609 |
{ |
| 1610 |
pj_status_t status = PJ_EUNKNOWN; |
| 1611 |
|
| 1612 |
|
| 1613 |
for (; sess->idx < sess->count; |
| 1614 |
(pjsua_var.ua_cfg.stun_try_ipv6 && sess->af == pj_AF_INET())? |
| 1615 |
sess->af = pj_AF_INET6(): (++sess->idx, sess->af = pj_AF_INET())) |
| 1616 |
{ |
| 1617 |
int af; |
| 1618 |
char target[64]; |
| 1619 |
pj_str_t hostpart; |
| 1620 |
pj_uint16_t port; |
| 1621 |
pj_stun_sock_cb stun_sock_cb; |
| 1622 |
|
| 1623 |
pj_assert(sess->idx < sess->count); |
| 1624 |
|
| 1625 |
if (pjsua_var.ua_cfg.stun_try_ipv6 && |
| 1626 |
pjsua_var.stun_opt != PJSUA_NAT64_DISABLED && |
| 1627 |
sess->af == pj_AF_INET()) |
| 1628 |
{ |
| 1629 |
|
| 1630 |
PJ_LOG(4,(THIS_FILE, "Skipping IPv4 resolution of STUN server " |
| 1631 |
"%s (%d of %d)", target, |
| 1632 |
sess->idx+1, sess->count)); |
| 1633 |
continue; |
| 1634 |
} |
| 1635 |
|
| 1636 |
pj_ansi_snprintf(target, sizeof(target), "%.*s", |
| 1637 |
(int)sess->srv[sess->idx].slen, |
| 1638 |
sess->srv[sess->idx].ptr); |
| 1639 |
|
| 1640 |
|
| 1641 |
status = pj_sockaddr_parse2(pj_AF_UNSPEC(), 0, &sess->srv[sess->idx], |
| 1642 |
&hostpart, &port, &af); |
| 1643 |
if (status != PJ_SUCCESS) { |
| 1644 |
PJ_LOG(2,(THIS_FILE, "Invalid STUN server entry %s", target)); |
| 1645 |
continue; |
| 1646 |
} |
| 1647 |
|
| 1648 |
|
| 1649 |
if (port == 0) |
| 1650 |
port = PJ_STUN_PORT; |
| 1651 |
|
| 1652 |
pj_assert(sess->stun_sock == NULL); |
| 1653 |
|
| 1654 |
PJ_LOG(4,(THIS_FILE, "Trying STUN server %s %s (%d of %d)..", |
| 1655 |
target, (sess->af == pj_AF_INET()? "IPv4": "IPv6"), |
| 1656 |
sess->idx+1, sess->count)); |
| 1657 |
|
| 1658 |
|
| 1659 |
pj_bzero(&stun_sock_cb, sizeof(stun_sock_cb)); |
| 1660 |
stun_sock_cb.on_status = &test_stun_on_status; |
| 1661 |
sess->async_wait = PJ_FALSE; |
| 1662 |
status = pj_stun_sock_create(&pjsua_var.stun_cfg, "stunresolve", |
| 1663 |
sess->af, &stun_sock_cb, |
| 1664 |
NULL, sess, &sess->stun_sock); |
| 1665 |
if (status != PJ_SUCCESS) { |
| 1666 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 1667 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 1668 |
PJ_LOG(4,(THIS_FILE, |
| 1669 |
"Error creating STUN socket for %s: %s", |
| 1670 |
target, errmsg)); |
| 1671 |
|
| 1672 |
continue; |
| 1673 |
} |
| 1674 |
|
| 1675 |
status = pj_stun_sock_start(sess->stun_sock, &hostpart, port, |
| 1676 |
pjsua_var.resolver); |
| 1677 |
if (status != PJ_SUCCESS) { |
| 1678 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 1679 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 1680 |
PJ_LOG(4,(THIS_FILE, |
| 1681 |
"Error starting STUN socket for %s: %s", |
| 1682 |
target, errmsg)); |
| 1683 |
|
| 1684 |
if (sess->stun_sock) { |
| 1685 |
pj_stun_sock_destroy(sess->stun_sock); |
| 1686 |
sess->stun_sock = NULL; |
| 1687 |
} |
| 1688 |
continue; |
| 1689 |
} |
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
sess->async_wait = PJ_TRUE; |
| 1695 |
return; |
| 1696 |
} |
| 1697 |
|
| 1698 |
if (sess->idx >= sess->count) { |
| 1699 |
|
| 1700 |
stun_resolve_add_ref(sess); |
| 1701 |
pj_assert(status != PJ_SUCCESS || sess->status != PJ_EPENDING); |
| 1702 |
if (sess->status == PJ_EPENDING) |
| 1703 |
sess->status = status; |
| 1704 |
stun_resolve_complete(sess); |
| 1705 |
stun_resolve_dec_ref(sess); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
PJ_DEF(pj_status_t) pjsua_update_stun_servers(unsigned count, pj_str_t srv[], |
| 1714 |
pj_bool_t wait) |
| 1715 |
{ |
| 1716 |
unsigned i; |
| 1717 |
pj_status_t status; |
| 1718 |
|
| 1719 |
PJ_ASSERT_RETURN(count && srv, PJ_EINVAL); |
| 1720 |
|
| 1721 |
PJSUA_LOCK(); |
| 1722 |
|
| 1723 |
pjsua_var.ua_cfg.stun_srv_cnt = count; |
| 1724 |
for (i = 0; i < count; i++) { |
| 1725 |
if (pj_strcmp(&pjsua_var.ua_cfg.stun_srv[i], &srv[i])) |
| 1726 |
pj_strdup(pjsua_var.pool, &pjsua_var.ua_cfg.stun_srv[i], &srv[i]); |
| 1727 |
} |
| 1728 |
pjsua_var.stun_status = PJ_EUNKNOWN; |
| 1729 |
|
| 1730 |
PJSUA_UNLOCK(); |
| 1731 |
|
| 1732 |
status = resolve_stun_server(wait, PJ_FALSE, 0); |
| 1733 |
if (wait == PJ_FALSE && status == PJ_EPENDING) |
| 1734 |
status = PJ_SUCCESS; |
| 1735 |
|
| 1736 |
return status; |
| 1737 |
} |
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
PJ_DEF(pj_status_t) pjsua_resolve_stun_servers( unsigned count, |
| 1744 |
pj_str_t srv[], |
| 1745 |
pj_bool_t wait, |
| 1746 |
void *token, |
| 1747 |
pj_stun_resolve_cb cb) |
| 1748 |
{ |
| 1749 |
pj_pool_t *pool; |
| 1750 |
pjsua_stun_resolve *sess; |
| 1751 |
pj_status_t status; |
| 1752 |
unsigned i, max_wait_ms; |
| 1753 |
pj_timestamp start, now; |
| 1754 |
|
| 1755 |
PJ_ASSERT_RETURN(count && srv && cb, PJ_EINVAL); |
| 1756 |
|
| 1757 |
pool = pjsua_pool_create("stunres", 256, 256); |
| 1758 |
if (!pool) |
| 1759 |
return PJ_ENOMEM; |
| 1760 |
|
| 1761 |
sess = PJ_POOL_ZALLOC_T(pool, pjsua_stun_resolve); |
| 1762 |
sess->pool = pool; |
| 1763 |
sess->token = token; |
| 1764 |
sess->cb = cb; |
| 1765 |
sess->count = count; |
| 1766 |
sess->blocking = wait; |
| 1767 |
sess->waiter = pj_thread_this(); |
| 1768 |
sess->status = PJ_EPENDING; |
| 1769 |
sess->af = pj_AF_INET(); |
| 1770 |
stun_resolve_add_ref(sess); |
| 1771 |
sess->srv = (pj_str_t*) pj_pool_calloc(pool, count, sizeof(pj_str_t)); |
| 1772 |
for (i=0; i<count; ++i) { |
| 1773 |
pj_strdup(pool, &sess->srv[i], &srv[i]); |
| 1774 |
} |
| 1775 |
|
| 1776 |
PJSUA_LOCK(); |
| 1777 |
pj_list_push_back(&pjsua_var.stun_res, sess); |
| 1778 |
PJSUA_UNLOCK(); |
| 1779 |
|
| 1780 |
resolve_stun_entry(sess); |
| 1781 |
|
| 1782 |
if (!wait) |
| 1783 |
return PJ_SUCCESS; |
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
max_wait_ms = count * pjsua_var.stun_cfg.rto_msec * (1 << 7); |
| 1790 |
pj_get_timestamp(&start); |
| 1791 |
|
| 1792 |
while ((sess->status == PJ_EPENDING) && (!sess->destroy_flag)) { |
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
if (pjsua_var.thread[0] == NULL || |
| 1798 |
(pj_thread_this() == pjsua_var.thread[0] && |
| 1799 |
pjsua_var.ua_cfg.thread_cnt == 1)) |
| 1800 |
{ |
| 1801 |
pjsua_handle_events(50); |
| 1802 |
} else { |
| 1803 |
pj_thread_sleep(20); |
| 1804 |
} |
| 1805 |
|
| 1806 |
pj_get_timestamp(&now); |
| 1807 |
if (pj_elapsed_msec(&start, &now) > max_wait_ms) |
| 1808 |
sess->status = PJ_ETIMEDOUT; |
| 1809 |
} |
| 1810 |
|
| 1811 |
status = sess->status; |
| 1812 |
stun_resolve_dec_ref(sess); |
| 1813 |
|
| 1814 |
return status; |
| 1815 |
} |
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
PJ_DEF(pj_status_t) pjsua_cancel_stun_resolution( void *token, |
| 1821 |
pj_bool_t notify_cb) |
| 1822 |
{ |
| 1823 |
pjsua_stun_resolve *sess; |
| 1824 |
unsigned cancelled_count = 0; |
| 1825 |
|
| 1826 |
PJSUA_LOCK(); |
| 1827 |
sess = pjsua_var.stun_res.next; |
| 1828 |
while (sess != &pjsua_var.stun_res) { |
| 1829 |
pjsua_stun_resolve *next = sess->next; |
| 1830 |
|
| 1831 |
if (sess->token == token) { |
| 1832 |
sess->has_result = PJ_TRUE; |
| 1833 |
sess->status = PJ_ECANCELLED; |
| 1834 |
if (notify_cb) { |
| 1835 |
pj_stun_resolve_result result; |
| 1836 |
|
| 1837 |
pj_bzero(&result, sizeof(result)); |
| 1838 |
result.token = token; |
| 1839 |
result.status = PJ_ECANCELLED; |
| 1840 |
|
| 1841 |
sess->cb(&result); |
| 1842 |
} |
| 1843 |
++cancelled_count; |
| 1844 |
} |
| 1845 |
|
| 1846 |
sess = next; |
| 1847 |
} |
| 1848 |
PJSUA_UNLOCK(); |
| 1849 |
|
| 1850 |
return cancelled_count ? PJ_SUCCESS : PJ_ENOTFOUND; |
| 1851 |
} |
| 1852 |
|
| 1853 |
static void internal_stun_resolve_cb(const pj_stun_resolve_result *result) |
| 1854 |
{ |
| 1855 |
pjsua_var.stun_status = result->status; |
| 1856 |
if ((result->status == PJ_SUCCESS) && (pjsua_var.ua_cfg.stun_srv_cnt>0)) { |
| 1857 |
pj_memcpy(&pjsua_var.stun_srv, &result->addr, sizeof(result->addr)); |
| 1858 |
pjsua_var.stun_srv_idx = result->index; |
| 1859 |
|
| 1860 |
|
| 1861 |
if (pjsua_var.nat_type == PJ_STUN_NAT_TYPE_UNKNOWN && |
| 1862 |
!pjsua_var.nat_in_progress && |
| 1863 |
pjsua_var.ua_cfg.nat_type_in_sdp) |
| 1864 |
{ |
| 1865 |
pjsua_detect_nat_type(); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
if (pjsua_var.ua_cfg.cb.on_stun_resolution_complete) |
| 1870 |
(*pjsua_var.ua_cfg.cb.on_stun_resolution_complete)(result); |
| 1871 |
} |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
pj_status_t resolve_stun_server(pj_bool_t wait, pj_bool_t retry_if_cur_error, |
| 1877 |
unsigned options) |
| 1878 |
{ |
| 1879 |
pjsua_var.stun_opt = options; |
| 1880 |
|
| 1881 |
|
| 1882 |
if (pjsua_var.stun_status != PJ_EPENDING && |
| 1883 |
pjsua_var.stun_status != PJ_SUCCESS && |
| 1884 |
retry_if_cur_error) |
| 1885 |
{ |
| 1886 |
pjsua_var.stun_status = PJ_EUNKNOWN; |
| 1887 |
} |
| 1888 |
|
| 1889 |
if (pjsua_var.stun_status == PJ_EUNKNOWN) { |
| 1890 |
pj_status_t status; |
| 1891 |
|
| 1892 |
|
| 1893 |
pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0, |
| 1894 |
pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 1895 |
pjsip_endpt_get_timer_heap(pjsua_var.endpt)); |
| 1896 |
|
| 1897 |
|
| 1898 |
if (pjsua_var.ua_cfg.stun_srv_cnt) { |
| 1899 |
pjsua_var.stun_status = PJ_EPENDING; |
| 1900 |
status = pjsua_resolve_stun_servers(pjsua_var.ua_cfg.stun_srv_cnt, |
| 1901 |
pjsua_var.ua_cfg.stun_srv, |
| 1902 |
wait, NULL, |
| 1903 |
&internal_stun_resolve_cb); |
| 1904 |
if (wait || status != PJ_SUCCESS) { |
| 1905 |
pjsua_var.stun_status = status; |
| 1906 |
} |
| 1907 |
} else { |
| 1908 |
pjsua_var.stun_status = PJ_SUCCESS; |
| 1909 |
} |
| 1910 |
|
| 1911 |
} else if (pjsua_var.stun_status == PJ_EPENDING) { |
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
if (wait) { |
| 1916 |
unsigned max_wait_ms; |
| 1917 |
pj_timestamp start, now; |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
max_wait_ms = pjsua_var.ua_cfg.stun_srv_cnt * |
| 1924 |
pjsua_var.stun_cfg.rto_msec * (1 << 7); |
| 1925 |
pj_get_timestamp(&start); |
| 1926 |
|
| 1927 |
while (pjsua_var.stun_status == PJ_EPENDING) { |
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
if (pjsua_var.thread[0] == NULL || |
| 1933 |
(pj_thread_this() == pjsua_var.thread[0] && |
| 1934 |
pjsua_var.ua_cfg.thread_cnt == 1)) |
| 1935 |
{ |
| 1936 |
pjsua_handle_events(10); |
| 1937 |
} else { |
| 1938 |
pj_thread_sleep(10); |
| 1939 |
} |
| 1940 |
|
| 1941 |
pj_get_timestamp(&now); |
| 1942 |
if (pj_elapsed_msec(&start, &now) > max_wait_ms) |
| 1943 |
return PJ_ETIMEDOUT; |
| 1944 |
} |
| 1945 |
} |
| 1946 |
} |
| 1947 |
|
| 1948 |
if (pjsua_var.stun_status != PJ_EPENDING && |
| 1949 |
pjsua_var.stun_status != PJ_SUCCESS && |
| 1950 |
pjsua_var.ua_cfg.stun_ignore_failure) |
| 1951 |
{ |
| 1952 |
PJ_LOG(2,(THIS_FILE, |
| 1953 |
"Ignoring STUN resolution failure (by setting)")); |
| 1954 |
|
| 1955 |
return PJ_SUCCESS; |
| 1956 |
} |
| 1957 |
|
| 1958 |
return pjsua_var.stun_status; |
| 1959 |
} |
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
PJ_DEF(pj_status_t) pjsua_destroy2(unsigned flags) |
| 1965 |
{ |
| 1966 |
int i; |
| 1967 |
|
| 1968 |
if (pjsua_var.endpt) { |
| 1969 |
PJ_LOG(4,(THIS_FILE, "Shutting down, flags=%d...", flags)); |
| 1970 |
} |
| 1971 |
|
| 1972 |
if (pjsua_var.state > PJSUA_STATE_NULL && |
| 1973 |
pjsua_var.state < PJSUA_STATE_CLOSING) |
| 1974 |
{ |
| 1975 |
pjsua_set_state(PJSUA_STATE_CLOSING); |
| 1976 |
} |
| 1977 |
|
| 1978 |
|
| 1979 |
pjsua_stop_worker_threads(); |
| 1980 |
|
| 1981 |
if (pjsua_var.endpt) { |
| 1982 |
unsigned max_wait; |
| 1983 |
|
| 1984 |
pj_log_push_indent(); |
| 1985 |
|
| 1986 |
|
| 1987 |
if (pjsua_var.calls) { |
| 1988 |
if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) { |
| 1989 |
pjsua_call_hangup_all(); |
| 1990 |
} else { |
| 1991 |
|
| 1992 |
for (i=0; i<(int)pjsua_var.ua_cfg.max_calls; ++i) { |
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
pjsua_media_channel_deinit(i); |
| 1997 |
} |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
|
| 2002 |
for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2003 |
if (!pjsua_var.acc[i].valid) |
| 2004 |
continue; |
| 2005 |
pjsua_var.acc[i].online_status = PJ_FALSE; |
| 2006 |
pj_bzero(&pjsua_var.acc[i].rpid, sizeof(pjrpid_element)); |
| 2007 |
} |
| 2008 |
|
| 2009 |
|
| 2010 |
pjsua_pres_shutdown(flags); |
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
max_wait = 100; |
| 2017 |
for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2018 |
if (!pjsua_var.acc[i].valid) |
| 2019 |
continue; |
| 2020 |
if (pjsua_var.acc[i].cfg.unpublish_max_wait_time_msec > max_wait) |
| 2021 |
max_wait = pjsua_var.acc[i].cfg.unpublish_max_wait_time_msec; |
| 2022 |
} |
| 2023 |
|
| 2024 |
|
| 2025 |
if (flags & PJSUA_DESTROY_NO_RX_MSG) { |
| 2026 |
max_wait = 0; |
| 2027 |
} |
| 2028 |
|
| 2029 |
|
| 2030 |
for (i=0; i<(int)(max_wait/50); ++i) { |
| 2031 |
unsigned j; |
| 2032 |
for (j=0; j<PJ_ARRAY_SIZE(pjsua_var.acc); ++j) { |
| 2033 |
if (!pjsua_var.acc[j].valid) |
| 2034 |
continue; |
| 2035 |
|
| 2036 |
if (pjsua_var.acc[j].publish_sess) |
| 2037 |
break; |
| 2038 |
} |
| 2039 |
if (j != PJ_ARRAY_SIZE(pjsua_var.acc)) |
| 2040 |
busy_sleep(50); |
| 2041 |
else |
| 2042 |
break; |
| 2043 |
} |
| 2044 |
|
| 2045 |
|
| 2046 |
for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2047 |
if (pjsua_var.acc[i].publish_sess) { |
| 2048 |
pjsip_publishc_destroy(pjsua_var.acc[i].publish_sess); |
| 2049 |
pjsua_var.acc[i].publish_sess = NULL; |
| 2050 |
} |
| 2051 |
} |
| 2052 |
|
| 2053 |
|
| 2054 |
for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2055 |
if (!pjsua_var.acc[i].valid) |
| 2056 |
continue; |
| 2057 |
|
| 2058 |
if (pjsua_var.acc[i].regc && (flags & PJSUA_DESTROY_NO_TX_MSG)==0) |
| 2059 |
{ |
| 2060 |
pjsua_acc_set_registration(i, PJ_FALSE); |
| 2061 |
} |
| 2062 |
#if PJ_HAS_SSL_SOCK |
| 2063 |
pj_turn_sock_tls_cfg_wipe_keys( |
| 2064 |
&pjsua_var.acc[i].cfg.turn_cfg.turn_tls_setting); |
| 2065 |
#endif |
| 2066 |
} |
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
max_wait = 100; |
| 2071 |
for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2072 |
if (!pjsua_var.acc[i].valid) |
| 2073 |
continue; |
| 2074 |
if (pjsua_var.acc[i].cfg.unreg_timeout > max_wait) |
| 2075 |
max_wait = pjsua_var.acc[i].cfg.unreg_timeout; |
| 2076 |
} |
| 2077 |
|
| 2078 |
|
| 2079 |
if (flags & PJSUA_DESTROY_NO_RX_MSG) { |
| 2080 |
max_wait = 0; |
| 2081 |
} |
| 2082 |
|
| 2083 |
|
| 2084 |
for (i=0; i<(int)(max_wait/50); ++i) { |
| 2085 |
unsigned j; |
| 2086 |
for (j=0; j<PJ_ARRAY_SIZE(pjsua_var.acc); ++j) { |
| 2087 |
if (!pjsua_var.acc[j].valid) |
| 2088 |
continue; |
| 2089 |
|
| 2090 |
if (pjsua_var.acc[j].regc) |
| 2091 |
break; |
| 2092 |
} |
| 2093 |
if (j != PJ_ARRAY_SIZE(pjsua_var.acc)) |
| 2094 |
busy_sleep(50); |
| 2095 |
else |
| 2096 |
break; |
| 2097 |
} |
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
if (i < 20 && (flags & PJSUA_DESTROY_NO_RX_MSG) == 0) { |
| 2104 |
busy_sleep(1000 - i*50); |
| 2105 |
} |
| 2106 |
|
| 2107 |
PJ_LOG(4,(THIS_FILE, "Destroying...")); |
| 2108 |
|
| 2109 |
|
| 2110 |
if (!pj_list_empty(&pjsua_var.stun_res)) { |
| 2111 |
pjsua_stun_resolve *sess = pjsua_var.stun_res.next; |
| 2112 |
while (sess != &pjsua_var.stun_res) { |
| 2113 |
pjsua_stun_resolve *next = sess->next; |
| 2114 |
destroy_stun_resolve(sess, PJ_TRUE); |
| 2115 |
sess = next; |
| 2116 |
} |
| 2117 |
} |
| 2118 |
|
| 2119 |
|
| 2120 |
for (i = 0; i < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata); i++) { |
| 2121 |
if (pjsua_var.tpdata[i].data.ptr) { |
| 2122 |
pjsip_transport_type_e tp_type; |
| 2123 |
|
| 2124 |
tp_type = pjsua_var.tpdata[i].type & ~PJSIP_TRANSPORT_IPV6; |
| 2125 |
if ((flags & PJSUA_DESTROY_NO_TX_MSG) && |
| 2126 |
tp_type == PJSIP_TRANSPORT_UDP && |
| 2127 |
pjsua_var.ua_cfg.enable_upnp && |
| 2128 |
pjsua_var.upnp_status == PJ_SUCCESS) |
| 2129 |
{ |
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
continue; |
| 2135 |
} |
| 2136 |
|
| 2137 |
pjsua_transport_close(i, PJ_FALSE); |
| 2138 |
} |
| 2139 |
} |
| 2140 |
|
| 2141 |
|
| 2142 |
pjsua_media_subsys_destroy(flags); |
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
pjsip_endpt_destroy(pjsua_var.endpt); |
| 2150 |
pjsua_var.endpt = NULL; |
| 2151 |
|
| 2152 |
|
| 2153 |
for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 2154 |
if (pjsua_var.buddy[i].pool) { |
| 2155 |
pj_pool_release(pjsua_var.buddy[i].pool); |
| 2156 |
pjsua_var.buddy[i].pool = NULL; |
| 2157 |
} |
| 2158 |
} |
| 2159 |
|
| 2160 |
|
| 2161 |
for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 2162 |
if (pjsua_var.acc[i].pool) { |
| 2163 |
pj_pool_release(pjsua_var.acc[i].pool); |
| 2164 |
pjsua_var.acc[i].pool = NULL; |
| 2165 |
} |
| 2166 |
} |
| 2167 |
} |
| 2168 |
|
| 2169 |
#if defined(PJNATH_HAS_UPNP) && (PJNATH_HAS_UPNP != 0) |
| 2170 |
|
| 2171 |
if (pjsua_var.ua_cfg.enable_upnp) { |
| 2172 |
pj_upnp_deinit(); |
| 2173 |
} |
| 2174 |
#endif |
| 2175 |
|
| 2176 |
|
| 2177 |
if (pjsua_var.mutex) { |
| 2178 |
pj_mutex_destroy(pjsua_var.mutex); |
| 2179 |
pjsua_var.mutex = NULL; |
| 2180 |
} |
| 2181 |
|
| 2182 |
if (pjsua_var.timer_mutex) { |
| 2183 |
pj_mutex_destroy(pjsua_var.timer_mutex); |
| 2184 |
pjsua_var.timer_mutex = NULL; |
| 2185 |
} |
| 2186 |
|
| 2187 |
|
| 2188 |
if (pjsua_var.timer_pool) { |
| 2189 |
pj_pool_release(pjsua_var.timer_pool); |
| 2190 |
pjsua_var.timer_pool = NULL; |
| 2191 |
} |
| 2192 |
if (pjsua_var.pool) { |
| 2193 |
pj_pool_release(pjsua_var.pool); |
| 2194 |
pjsua_var.pool = NULL; |
| 2195 |
pj_caching_pool_destroy(&pjsua_var.cp); |
| 2196 |
|
| 2197 |
pjsua_set_state(PJSUA_STATE_NULL); |
| 2198 |
|
| 2199 |
PJ_LOG(4,(THIS_FILE, "PJSUA destroyed...")); |
| 2200 |
|
| 2201 |
|
| 2202 |
if (pjsua_var.log_file) { |
| 2203 |
pj_file_close(pjsua_var.log_file); |
| 2204 |
pjsua_var.log_file = NULL; |
| 2205 |
} |
| 2206 |
|
| 2207 |
pj_log_pop_indent(); |
| 2208 |
|
| 2209 |
|
| 2210 |
pj_shutdown(); |
| 2211 |
} |
| 2212 |
|
| 2213 |
|
| 2214 |
pj_bzero(&pjsua_var, sizeof(pjsua_var)); |
| 2215 |
|
| 2216 |
|
| 2217 |
return PJ_SUCCESS; |
| 2218 |
} |
| 2219 |
|
| 2220 |
void pjsua_set_state(pjsua_state new_state) |
| 2221 |
{ |
| 2222 |
const char *state_name[] = { |
| 2223 |
"NULL", |
| 2224 |
"CREATED", |
| 2225 |
"INIT", |
| 2226 |
"STARTING", |
| 2227 |
"RUNNING", |
| 2228 |
"CLOSING" |
| 2229 |
}; |
| 2230 |
pjsua_state old_state = pjsua_var.state; |
| 2231 |
|
| 2232 |
pjsua_var.state = new_state; |
| 2233 |
PJ_LOG(4,(THIS_FILE, "PJSUA state changed: %s --> %s", |
| 2234 |
state_name[old_state], state_name[new_state])); |
| 2235 |
} |
| 2236 |
|
| 2237 |
|
| 2238 |
PJ_DEF(pjsua_state) pjsua_get_state(void) |
| 2239 |
{ |
| 2240 |
return pjsua_var.state; |
| 2241 |
} |
| 2242 |
|
| 2243 |
PJ_DEF(pj_status_t) pjsua_destroy(void) |
| 2244 |
{ |
| 2245 |
return pjsua_destroy2(0); |
| 2246 |
} |
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
PJ_DEF(pj_status_t) pjsua_start(void) |
| 2257 |
{ |
| 2258 |
pj_status_t status; |
| 2259 |
|
| 2260 |
pjsua_set_state(PJSUA_STATE_STARTING); |
| 2261 |
pj_log_push_indent(); |
| 2262 |
|
| 2263 |
status = pjsua_call_subsys_start(); |
| 2264 |
if (status != PJ_SUCCESS) |
| 2265 |
goto on_return; |
| 2266 |
|
| 2267 |
status = pjsua_media_subsys_start(); |
| 2268 |
if (status != PJ_SUCCESS) |
| 2269 |
goto on_return; |
| 2270 |
|
| 2271 |
status = pjsua_pres_start(); |
| 2272 |
if (status != PJ_SUCCESS) |
| 2273 |
goto on_return; |
| 2274 |
|
| 2275 |
pjsua_set_state(PJSUA_STATE_RUNNING); |
| 2276 |
|
| 2277 |
on_return: |
| 2278 |
pj_log_pop_indent(); |
| 2279 |
return status; |
| 2280 |
} |
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout) |
| 2288 |
{ |
| 2289 |
#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 2290 |
|
| 2291 |
return pj_symbianos_poll(-1, msec_timeout); |
| 2292 |
|
| 2293 |
#else |
| 2294 |
|
| 2295 |
unsigned count = 0; |
| 2296 |
pj_time_val tv; |
| 2297 |
pj_status_t status; |
| 2298 |
|
| 2299 |
tv.sec = 0; |
| 2300 |
tv.msec = msec_timeout; |
| 2301 |
pj_time_val_normalize(&tv); |
| 2302 |
|
| 2303 |
status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count); |
| 2304 |
|
| 2305 |
if (status != PJ_SUCCESS) |
| 2306 |
return -status; |
| 2307 |
|
| 2308 |
return count; |
| 2309 |
|
| 2310 |
#endif |
| 2311 |
} |
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size, |
| 2318 |
pj_size_t increment) |
| 2319 |
{ |
| 2320 |
|
| 2321 |
return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment, |
| 2322 |
NULL); |
| 2323 |
} |
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void) |
| 2332 |
{ |
| 2333 |
return pjsua_var.endpt; |
| 2334 |
} |
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void) |
| 2341 |
{ |
| 2342 |
return pjsua_var.med_endpt; |
| 2343 |
} |
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void) |
| 2349 |
{ |
| 2350 |
return &pjsua_var.cp.factory; |
| 2351 |
} |
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
|
| 2358 |
|
| 2359 |
|
| 2360 |
static const char *addr_string(const pj_sockaddr_t *addr) |
| 2361 |
{ |
| 2362 |
static char str[128]; |
| 2363 |
str[0] = '\0'; |
| 2364 |
pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family, |
| 2365 |
pj_sockaddr_get_addr(addr), |
| 2366 |
str, sizeof(str)); |
| 2367 |
return str; |
| 2368 |
} |
| 2369 |
|
| 2370 |
void pjsua_acc_on_tp_state_changed(pjsip_transport *tp, |
| 2371 |
pjsip_transport_state state, |
| 2372 |
const pjsip_transport_state_info *info); |
| 2373 |
|
| 2374 |
|
| 2375 |
static void on_tp_state_callback(pjsip_transport *tp, |
| 2376 |
pjsip_transport_state state, |
| 2377 |
const pjsip_transport_state_info *info) |
| 2378 |
{ |
| 2379 |
if (pjsua_var.ua_cfg.cb.on_transport_state) { |
| 2380 |
(*pjsua_var.ua_cfg.cb.on_transport_state)(tp, state, info); |
| 2381 |
} |
| 2382 |
if (pjsua_var.old_tp_cb) { |
| 2383 |
(*pjsua_var.old_tp_cb)(tp, state, info); |
| 2384 |
} |
| 2385 |
pjsua_acc_on_tp_state_changed(tp, state, info); |
| 2386 |
} |
| 2387 |
|
| 2388 |
|
| 2389 |
static void set_tp_state_cb() |
| 2390 |
{ |
| 2391 |
pjsip_tp_state_callback tpcb; |
| 2392 |
pjsip_tpmgr *tpmgr; |
| 2393 |
|
| 2394 |
tpmgr = pjsip_endpt_get_tpmgr(pjsua_var.endpt); |
| 2395 |
tpcb = pjsip_tpmgr_get_state_cb(tpmgr); |
| 2396 |
|
| 2397 |
if (tpcb != &on_tp_state_callback) { |
| 2398 |
pjsua_var.old_tp_cb = tpcb; |
| 2399 |
pjsip_tpmgr_set_state_cb(tpmgr, &on_tp_state_callback); |
| 2400 |
} |
| 2401 |
} |
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
static pj_status_t create_sip_udp_sock(int af, |
| 2408 |
const pjsua_transport_config *cfg, |
| 2409 |
pj_sock_t *p_sock, |
| 2410 |
pj_sockaddr *p_pub_addr) |
| 2411 |
{ |
| 2412 |
char stun_ip_addr[PJ_INET6_ADDRSTRLEN]; |
| 2413 |
unsigned port = cfg->port; |
| 2414 |
pj_str_t stun_srv; |
| 2415 |
pj_sock_t sock; |
| 2416 |
pj_sockaddr bind_addr; |
| 2417 |
pj_status_t status; |
| 2418 |
|
| 2419 |
|
| 2420 |
status = resolve_stun_server(PJ_TRUE, PJ_TRUE, 0); |
| 2421 |
if (status != PJ_SUCCESS) { |
| 2422 |
pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 2423 |
return status; |
| 2424 |
} |
| 2425 |
|
| 2426 |
|
| 2427 |
if (cfg->bound_addr.slen) { |
| 2428 |
status = pj_sockaddr_init(af, &bind_addr, &cfg->bound_addr, |
| 2429 |
(pj_uint16_t)port); |
| 2430 |
if (status != PJ_SUCCESS) { |
| 2431 |
pjsua_perror(THIS_FILE, |
| 2432 |
"Unable to resolve transport bound address", |
| 2433 |
status); |
| 2434 |
return status; |
| 2435 |
} |
| 2436 |
} else { |
| 2437 |
pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port); |
| 2438 |
} |
| 2439 |
|
| 2440 |
|
| 2441 |
status = pj_sock_socket(af, pj_SOCK_DGRAM() | pj_SOCK_CLOEXEC(), 0, &sock); |
| 2442 |
if (status != PJ_SUCCESS) { |
| 2443 |
pjsua_perror(THIS_FILE, "socket() error", status); |
| 2444 |
return status; |
| 2445 |
} |
| 2446 |
|
| 2447 |
|
| 2448 |
status = pj_sock_apply_qos2(sock, cfg->qos_type, |
| 2449 |
&cfg->qos_params, |
| 2450 |
2, THIS_FILE, "SIP UDP socket"); |
| 2451 |
|
| 2452 |
|
| 2453 |
if (cfg->sockopt_params.cnt) { |
| 2454 |
status = pj_sock_setsockopt_params(sock, &cfg->sockopt_params); |
| 2455 |
if (status != PJ_SUCCESS) { |
| 2456 |
pjsua_perror(THIS_FILE, "setsockopt) error", status); |
| 2457 |
} |
| 2458 |
} |
| 2459 |
|
| 2460 |
|
| 2461 |
status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr)); |
| 2462 |
if (status != PJ_SUCCESS) { |
| 2463 |
pjsua_perror(THIS_FILE, "bind() error", status); |
| 2464 |
pj_sock_close(sock); |
| 2465 |
return status; |
| 2466 |
} |
| 2467 |
|
| 2468 |
|
| 2469 |
if (port == 0) { |
| 2470 |
pj_sockaddr bound_addr; |
| 2471 |
int namelen = sizeof(bound_addr); |
| 2472 |
status = pj_sock_getsockname(sock, &bound_addr, &namelen); |
| 2473 |
if (status != PJ_SUCCESS) { |
| 2474 |
pjsua_perror(THIS_FILE, "getsockname() error", status); |
| 2475 |
pj_sock_close(sock); |
| 2476 |
return status; |
| 2477 |
} |
| 2478 |
|
| 2479 |
port = pj_sockaddr_get_port(&bound_addr); |
| 2480 |
} |
| 2481 |
|
| 2482 |
if (pjsua_var.stun_srv.addr.sa_family != 0) { |
| 2483 |
pj_sockaddr_print(&pjsua_var.stun_srv, |
| 2484 |
stun_ip_addr, sizeof(stun_ip_addr), 0); |
| 2485 |
stun_srv = pj_str(stun_ip_addr); |
| 2486 |
} else { |
| 2487 |
stun_srv.slen = 0; |
| 2488 |
} |
| 2489 |
|
| 2490 |
|
| 2491 |
|
| 2492 |
|
| 2493 |
status = PJ_SUCCESS; |
| 2494 |
if (pj_sockaddr_has_addr(p_pub_addr)) { |
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
if (pj_sockaddr_get_port(p_pub_addr) == 0) |
| 2500 |
pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port); |
| 2501 |
|
| 2502 |
} else if (stun_srv.slen && |
| 2503 |
(af == pj_AF_INET() || pjsua_var.ua_cfg.stun_try_ipv6)) |
| 2504 |
{ |
| 2505 |
pjstun_setting stun_opt; |
| 2506 |
|
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
|
| 2511 |
|
| 2512 |
pj_bzero(&stun_opt, sizeof(stun_opt)); |
| 2513 |
stun_opt.use_stun2 = pjsua_var.ua_cfg.stun_map_use_stun2; |
| 2514 |
stun_opt.af = pjsua_var.stun_srv.addr.sa_family; |
| 2515 |
stun_opt.srv1 = stun_opt.srv2 = stun_srv; |
| 2516 |
stun_opt.port1 = stun_opt.port2 = |
| 2517 |
pj_sockaddr_get_port(&pjsua_var.stun_srv); |
| 2518 |
status = pjstun_get_mapped_addr2(&pjsua_var.cp.factory, &stun_opt, |
| 2519 |
1, &sock, &p_pub_addr->ipv4); |
| 2520 |
if (status != PJ_SUCCESS) { |
| 2521 |
|
| 2522 |
pjsua_perror(THIS_FILE, "Error contacting STUN server", status); |
| 2523 |
|
| 2524 |
|
| 2525 |
if (!pjsua_var.ua_cfg.stun_ignore_failure) { |
| 2526 |
pj_sock_close(sock); |
| 2527 |
return status; |
| 2528 |
} |
| 2529 |
} |
| 2530 |
} |
| 2531 |
|
| 2532 |
#if defined(PJNATH_HAS_UPNP) && (PJNATH_HAS_UPNP != 0) |
| 2533 |
else if (pjsua_var.ua_cfg.enable_upnp && |
| 2534 |
pjsua_var.upnp_status == PJ_SUCCESS) |
| 2535 |
{ |
| 2536 |
status = pj_upnp_add_port_mapping(1, &sock, NULL, p_pub_addr); |
| 2537 |
if (status != PJ_SUCCESS) { |
| 2538 |
pjsua_perror(THIS_FILE, "Error adding UPnP port mapping", status); |
| 2539 |
} |
| 2540 |
} |
| 2541 |
#endif |
| 2542 |
|
| 2543 |
if (!pj_sockaddr_has_addr(p_pub_addr)) { |
| 2544 |
pj_bzero(p_pub_addr, sizeof(pj_sockaddr)); |
| 2545 |
|
| 2546 |
if (pj_sockaddr_has_addr(&bind_addr)) { |
| 2547 |
pj_sockaddr_copy_addr(p_pub_addr, &bind_addr); |
| 2548 |
} else { |
| 2549 |
status = pj_gethostip(af, p_pub_addr); |
| 2550 |
if (status != PJ_SUCCESS) { |
| 2551 |
pjsua_perror(THIS_FILE, "Unable to get local host IP", status); |
| 2552 |
pj_sock_close(sock); |
| 2553 |
return status; |
| 2554 |
} |
| 2555 |
} |
| 2556 |
|
| 2557 |
p_pub_addr->addr.sa_family = (pj_uint16_t)af; |
| 2558 |
pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port); |
| 2559 |
|
| 2560 |
if (stun_srv.slen && af != pj_AF_INET()) { |
| 2561 |
|
| 2562 |
PJ_PERROR(2, (THIS_FILE, PJ_EAFNOTSUP, |
| 2563 |
"Cannot use STUN for SIP UDP socket %s:%d", |
| 2564 |
addr_string(p_pub_addr), |
| 2565 |
(int)pj_sockaddr_get_port(p_pub_addr))); |
| 2566 |
} |
| 2567 |
|
| 2568 |
} |
| 2569 |
|
| 2570 |
*p_sock = sock; |
| 2571 |
|
| 2572 |
PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d", |
| 2573 |
addr_string(p_pub_addr), |
| 2574 |
(int)pj_sockaddr_get_port(p_pub_addr))); |
| 2575 |
|
| 2576 |
return PJ_SUCCESS; |
| 2577 |
} |
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type, |
| 2584 |
const pjsua_transport_config *cfg, |
| 2585 |
pjsua_transport_id *p_id) |
| 2586 |
{ |
| 2587 |
pjsip_transport *tp; |
| 2588 |
unsigned id; |
| 2589 |
pj_status_t status; |
| 2590 |
|
| 2591 |
PJSUA_LOCK(); |
| 2592 |
|
| 2593 |
|
| 2594 |
for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
| 2595 |
if (pjsua_var.tpdata[id].data.ptr == NULL) |
| 2596 |
break; |
| 2597 |
} |
| 2598 |
|
| 2599 |
if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 2600 |
status = PJ_ETOOMANY; |
| 2601 |
pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 2602 |
goto on_return; |
| 2603 |
} |
| 2604 |
|
| 2605 |
|
| 2606 |
if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) { |
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
pjsua_transport_config config; |
| 2611 |
char hostbuf[PJ_INET6_ADDRSTRLEN]; |
| 2612 |
pj_sock_t sock = PJ_INVALID_SOCKET; |
| 2613 |
pj_sockaddr pub_addr; |
| 2614 |
pjsip_host_port addr_name; |
| 2615 |
|
| 2616 |
|
| 2617 |
if (cfg == NULL) { |
| 2618 |
pjsua_transport_config_default(&config); |
| 2619 |
cfg = &config; |
| 2620 |
} |
| 2621 |
|
| 2622 |
|
| 2623 |
pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr, |
| 2624 |
NULL, (pj_uint16_t)cfg->port); |
| 2625 |
if (cfg->public_addr.slen) { |
| 2626 |
status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type), |
| 2627 |
&pub_addr, &cfg->public_addr); |
| 2628 |
if (status != PJ_SUCCESS) { |
| 2629 |
pjsua_perror(THIS_FILE, |
| 2630 |
"Unable to resolve transport public address", |
| 2631 |
status); |
| 2632 |
goto on_return; |
| 2633 |
} |
| 2634 |
} |
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
status = create_sip_udp_sock(pjsip_transport_type_get_af(type), |
| 2640 |
cfg, &sock, &pub_addr); |
| 2641 |
if (status != PJ_SUCCESS) |
| 2642 |
goto on_return; |
| 2643 |
|
| 2644 |
pj_ansi_strxcpy(hostbuf, addr_string(&pub_addr), sizeof(hostbuf)); |
| 2645 |
addr_name.host = pj_str(hostbuf); |
| 2646 |
addr_name.port = pj_sockaddr_get_port(&pub_addr); |
| 2647 |
|
| 2648 |
|
| 2649 |
status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock, |
| 2650 |
&addr_name, 1, &tp); |
| 2651 |
if (status != PJ_SUCCESS) { |
| 2652 |
pjsua_perror(THIS_FILE, "Error creating SIP UDP transport", |
| 2653 |
status); |
| 2654 |
pj_sock_close(sock); |
| 2655 |
goto on_return; |
| 2656 |
} |
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
pjsua_var.tpdata[id].type = type; |
| 2661 |
pjsua_var.tpdata[id].local_name = tp->local_name; |
| 2662 |
pjsua_var.tpdata[id].data.tp = tp; |
| 2663 |
pj_sockaddr_cp(&pjsua_var.tpdata[id].pub_addr, &pub_addr); |
| 2664 |
if (cfg->bound_addr.slen || cfg->public_addr.slen) |
| 2665 |
pjsua_var.tpdata[id].has_cfg_addr = PJ_TRUE; |
| 2666 |
|
| 2667 |
#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0 |
| 2668 |
|
| 2669 |
} else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) { |
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
pjsua_transport_config config; |
| 2674 |
pjsip_tpfactory *tcp; |
| 2675 |
pjsip_tcp_transport_cfg tcp_cfg; |
| 2676 |
int af; |
| 2677 |
|
| 2678 |
af = (type==PJSIP_TRANSPORT_TCP6) ? pj_AF_INET6() : pj_AF_INET(); |
| 2679 |
pjsip_tcp_transport_cfg_default(&tcp_cfg, af); |
| 2680 |
|
| 2681 |
|
| 2682 |
if (cfg == NULL) { |
| 2683 |
pjsua_transport_config_default(&config); |
| 2684 |
cfg = &config; |
| 2685 |
} |
| 2686 |
|
| 2687 |
|
| 2688 |
if (cfg->port) |
| 2689 |
pj_sockaddr_set_port(&tcp_cfg.bind_addr, (pj_uint16_t)cfg->port); |
| 2690 |
|
| 2691 |
if (cfg->bound_addr.slen) { |
| 2692 |
status = pj_sockaddr_set_str_addr(tcp_cfg.af, |
| 2693 |
&tcp_cfg.bind_addr, |
| 2694 |
&cfg->bound_addr); |
| 2695 |
if (status != PJ_SUCCESS) { |
| 2696 |
pjsua_perror(THIS_FILE, |
| 2697 |
"Unable to resolve transport bound address", |
| 2698 |
status); |
| 2699 |
goto on_return; |
| 2700 |
} |
| 2701 |
} |
| 2702 |
|
| 2703 |
|
| 2704 |
if (cfg->public_addr.slen) |
| 2705 |
tcp_cfg.addr_name.host = cfg->public_addr; |
| 2706 |
|
| 2707 |
|
| 2708 |
tcp_cfg.qos_type = cfg->qos_type; |
| 2709 |
pj_memcpy(&tcp_cfg.qos_params, &cfg->qos_params, |
| 2710 |
sizeof(cfg->qos_params)); |
| 2711 |
|
| 2712 |
|
| 2713 |
pj_memcpy(&tcp_cfg.sockopt_params, &cfg->sockopt_params, |
| 2714 |
sizeof(tcp_cfg.sockopt_params)); |
| 2715 |
|
| 2716 |
|
| 2717 |
status = pjsip_tcp_transport_start3(pjsua_var.endpt, &tcp_cfg, &tcp); |
| 2718 |
|
| 2719 |
if (status != PJ_SUCCESS) { |
| 2720 |
pjsua_perror(THIS_FILE, "Error creating SIP TCP listener", |
| 2721 |
status); |
| 2722 |
goto on_return; |
| 2723 |
} |
| 2724 |
|
| 2725 |
|
| 2726 |
pjsua_var.tpdata[id].type = type; |
| 2727 |
pjsua_var.tpdata[id].local_name = tcp->addr_name; |
| 2728 |
pjsua_var.tpdata[id].data.factory = tcp; |
| 2729 |
|
| 2730 |
#endif |
| 2731 |
|
| 2732 |
#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0 |
| 2733 |
} else if (type == PJSIP_TRANSPORT_TLS || type == PJSIP_TRANSPORT_TLS6) { |
| 2734 |
|
| 2735 |
|
| 2736 |
|
| 2737 |
pjsua_transport_config config; |
| 2738 |
pjsip_host_port a_name; |
| 2739 |
pjsip_tpfactory *tls; |
| 2740 |
pj_sockaddr local_addr; |
| 2741 |
int af; |
| 2742 |
|
| 2743 |
|
| 2744 |
if (cfg == NULL) { |
| 2745 |
pjsua_transport_config_default(&config); |
| 2746 |
config.port = 5061; |
| 2747 |
cfg = &config; |
| 2748 |
} |
| 2749 |
|
| 2750 |
|
| 2751 |
af = (type==PJSIP_TRANSPORT_TLS) ? pj_AF_INET() : pj_AF_INET6(); |
| 2752 |
pj_sockaddr_init(af, &local_addr, NULL, 0); |
| 2753 |
|
| 2754 |
if (cfg->port) |
| 2755 |
pj_sockaddr_set_port(&local_addr, (pj_uint16_t)cfg->port); |
| 2756 |
|
| 2757 |
if (cfg->bound_addr.slen) { |
| 2758 |
status = pj_sockaddr_set_str_addr(af, &local_addr, |
| 2759 |
&cfg->bound_addr); |
| 2760 |
if (status != PJ_SUCCESS) { |
| 2761 |
pjsua_perror(THIS_FILE, |
| 2762 |
"Unable to resolve transport bound address", |
| 2763 |
status); |
| 2764 |
goto on_return; |
| 2765 |
} |
| 2766 |
} |
| 2767 |
|
| 2768 |
|
| 2769 |
pj_bzero(&a_name, sizeof(pjsip_host_port)); |
| 2770 |
if (cfg->public_addr.slen) |
| 2771 |
a_name.host = cfg->public_addr; |
| 2772 |
|
| 2773 |
status = pjsip_tls_transport_start2(pjsua_var.endpt, &cfg->tls_setting, |
| 2774 |
&local_addr, &a_name, 1, &tls); |
| 2775 |
if (status != PJ_SUCCESS) { |
| 2776 |
pjsua_perror(THIS_FILE, "Error creating SIP TLS listener", |
| 2777 |
status); |
| 2778 |
goto on_return; |
| 2779 |
} |
| 2780 |
|
| 2781 |
|
| 2782 |
pjsua_var.tpdata[id].type = type; |
| 2783 |
pjsua_var.tpdata[id].local_name = tls->addr_name; |
| 2784 |
pjsua_var.tpdata[id].data.factory = tls; |
| 2785 |
#endif |
| 2786 |
|
| 2787 |
} else { |
| 2788 |
status = PJSIP_EUNSUPTRANSPORT; |
| 2789 |
pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 2790 |
goto on_return; |
| 2791 |
} |
| 2792 |
|
| 2793 |
|
| 2794 |
set_tp_state_cb(); |
| 2795 |
|
| 2796 |
|
| 2797 |
if (p_id) *p_id = id; |
| 2798 |
|
| 2799 |
status = PJ_SUCCESS; |
| 2800 |
|
| 2801 |
on_return: |
| 2802 |
|
| 2803 |
PJSUA_UNLOCK(); |
| 2804 |
|
| 2805 |
return status; |
| 2806 |
} |
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp, |
| 2813 |
pjsua_transport_id *p_id) |
| 2814 |
{ |
| 2815 |
unsigned id; |
| 2816 |
|
| 2817 |
PJSUA_LOCK(); |
| 2818 |
|
| 2819 |
|
| 2820 |
for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
| 2821 |
if (pjsua_var.tpdata[id].data.ptr == NULL) |
| 2822 |
break; |
| 2823 |
} |
| 2824 |
|
| 2825 |
if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 2826 |
pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY); |
| 2827 |
PJSUA_UNLOCK(); |
| 2828 |
return PJ_ETOOMANY; |
| 2829 |
} |
| 2830 |
|
| 2831 |
|
| 2832 |
pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type; |
| 2833 |
pjsua_var.tpdata[id].local_name = tp->local_name; |
| 2834 |
pjsua_var.tpdata[id].data.tp = tp; |
| 2835 |
|
| 2836 |
|
| 2837 |
set_tp_state_cb(); |
| 2838 |
|
| 2839 |
|
| 2840 |
if (p_id) *p_id = id; |
| 2841 |
|
| 2842 |
PJSUA_UNLOCK(); |
| 2843 |
|
| 2844 |
return PJ_SUCCESS; |
| 2845 |
} |
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
|
| 2851 |
PJ_DEF(pj_status_t) pjsua_tpfactory_register( pjsip_tpfactory *tf, |
| 2852 |
pjsua_transport_id *p_id) |
| 2853 |
{ |
| 2854 |
unsigned id; |
| 2855 |
|
| 2856 |
PJSUA_LOCK(); |
| 2857 |
|
| 2858 |
|
| 2859 |
for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
| 2860 |
if (pjsua_var.tpdata[id].data.ptr == NULL) |
| 2861 |
break; |
| 2862 |
} |
| 2863 |
|
| 2864 |
if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 2865 |
pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY); |
| 2866 |
PJSUA_UNLOCK(); |
| 2867 |
return PJ_ETOOMANY; |
| 2868 |
} |
| 2869 |
|
| 2870 |
|
| 2871 |
pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tf->type; |
| 2872 |
pjsua_var.tpdata[id].local_name = tf->addr_name; |
| 2873 |
pjsua_var.tpdata[id].data.factory = tf; |
| 2874 |
|
| 2875 |
|
| 2876 |
set_tp_state_cb(); |
| 2877 |
|
| 2878 |
|
| 2879 |
if (p_id) *p_id = id; |
| 2880 |
|
| 2881 |
PJSUA_UNLOCK(); |
| 2882 |
|
| 2883 |
return PJ_SUCCESS; |
| 2884 |
} |
| 2885 |
|
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[], |
| 2891 |
unsigned *p_count ) |
| 2892 |
{ |
| 2893 |
unsigned i, count; |
| 2894 |
|
| 2895 |
PJSUA_LOCK(); |
| 2896 |
|
| 2897 |
for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count; |
| 2898 |
++i) |
| 2899 |
{ |
| 2900 |
if (!pjsua_var.tpdata[i].data.ptr) |
| 2901 |
continue; |
| 2902 |
|
| 2903 |
id[count++] = i; |
| 2904 |
} |
| 2905 |
|
| 2906 |
*p_count = count; |
| 2907 |
|
| 2908 |
PJSUA_UNLOCK(); |
| 2909 |
|
| 2910 |
return PJ_SUCCESS; |
| 2911 |
} |
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id, |
| 2918 |
pjsua_transport_info *info) |
| 2919 |
{ |
| 2920 |
pjsua_transport_data *t = &pjsua_var.tpdata[id]; |
| 2921 |
pj_status_t status; |
| 2922 |
|
| 2923 |
pj_bzero(info, sizeof(*info)); |
| 2924 |
|
| 2925 |
|
| 2926 |
PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 2927 |
PJ_EINVAL); |
| 2928 |
|
| 2929 |
|
| 2930 |
PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
| 2931 |
|
| 2932 |
PJSUA_LOCK(); |
| 2933 |
|
| 2934 |
if ((t->type & ~PJSIP_TRANSPORT_IPV6) == PJSIP_TRANSPORT_UDP) { |
| 2935 |
|
| 2936 |
pjsip_transport *tp = t->data.tp; |
| 2937 |
|
| 2938 |
if (tp == NULL) { |
| 2939 |
PJSUA_UNLOCK(); |
| 2940 |
return PJ_EINVALIDOP; |
| 2941 |
} |
| 2942 |
|
| 2943 |
info->id = id; |
| 2944 |
info->type = (pjsip_transport_type_e) tp->key.type; |
| 2945 |
info->type_name = pj_str(tp->type_name); |
| 2946 |
info->info = pj_str(tp->info); |
| 2947 |
info->flag = tp->flag; |
| 2948 |
info->addr_len = tp->addr_len; |
| 2949 |
info->local_addr = tp->local_addr; |
| 2950 |
info->local_name = tp->local_name; |
| 2951 |
info->usage_count = pj_atomic_get(tp->ref_cnt); |
| 2952 |
|
| 2953 |
status = PJ_SUCCESS; |
| 2954 |
|
| 2955 |
} else if ((t->type & ~PJSIP_TRANSPORT_IPV6) == PJSIP_TRANSPORT_TCP || |
| 2956 |
(t->type & ~PJSIP_TRANSPORT_IPV6) == PJSIP_TRANSPORT_TLS) |
| 2957 |
{ |
| 2958 |
|
| 2959 |
pjsip_tpfactory *factory = t->data.factory; |
| 2960 |
|
| 2961 |
if (factory == NULL) { |
| 2962 |
PJSUA_UNLOCK(); |
| 2963 |
return PJ_EINVALIDOP; |
| 2964 |
} |
| 2965 |
|
| 2966 |
info->id = id; |
| 2967 |
info->type = t->type; |
| 2968 |
info->type_name = pj_str(factory->type_name); |
| 2969 |
info->info = pj_str(factory->info); |
| 2970 |
info->flag = factory->flag; |
| 2971 |
info->addr_len = sizeof(factory->local_addr); |
| 2972 |
info->local_addr = factory->local_addr; |
| 2973 |
info->local_name = factory->addr_name; |
| 2974 |
info->usage_count = 0; |
| 2975 |
|
| 2976 |
status = PJ_SUCCESS; |
| 2977 |
|
| 2978 |
} else { |
| 2979 |
pj_assert(!"Unsupported transport"); |
| 2980 |
status = PJ_EINVALIDOP; |
| 2981 |
} |
| 2982 |
|
| 2983 |
|
| 2984 |
PJSUA_UNLOCK(); |
| 2985 |
|
| 2986 |
return status; |
| 2987 |
} |
| 2988 |
|
| 2989 |
|
| 2990 |
#if 0 |
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id, |
| 2995 |
pj_bool_t enabled) |
| 2996 |
{ |
| 2997 |
|
| 2998 |
PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 2999 |
PJ_EINVAL); |
| 3000 |
|
| 3001 |
|
| 3002 |
PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
| 3003 |
|
| 3004 |
|
| 3005 |
|
| 3006 |
PJ_TODO(pjsua_transport_set_enable); |
| 3007 |
PJ_UNUSED_ARG(enabled); |
| 3008 |
|
| 3009 |
return PJ_EINVALIDOP; |
| 3010 |
} |
| 3011 |
#endif |
| 3012 |
|
| 3013 |
|
| 3014 |
|
| 3015 |
|
| 3016 |
|
| 3017 |
PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id, |
| 3018 |
pj_bool_t force ) |
| 3019 |
{ |
| 3020 |
pj_status_t status; |
| 3021 |
pjsip_transport_type_e tp_type; |
| 3022 |
|
| 3023 |
|
| 3024 |
PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 3025 |
PJ_EINVAL); |
| 3026 |
|
| 3027 |
|
| 3028 |
PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
| 3029 |
|
| 3030 |
tp_type = pjsua_var.tpdata[id].type & ~PJSIP_TRANSPORT_IPV6; |
| 3031 |
|
| 3032 |
if (force) { |
| 3033 |
|
| 3034 |
|
| 3035 |
|
| 3036 |
|
| 3037 |
PJ_LOG(1, (THIS_FILE, "pjsua_transport_close(force=PJ_TRUE) is " |
| 3038 |
"deprecated.")); |
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
|
| 3043 |
|
| 3044 |
} |
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
|
| 3049 |
switch (tp_type) { |
| 3050 |
case PJSIP_TRANSPORT_UDP: |
| 3051 |
#if defined(PJNATH_HAS_UPNP) && (PJNATH_HAS_UPNP != 0) |
| 3052 |
if (pjsua_var.ua_cfg.enable_upnp && |
| 3053 |
pjsua_var.upnp_status == PJ_SUCCESS) |
| 3054 |
{ |
| 3055 |
status = pj_upnp_del_port_mapping( |
| 3056 |
&pjsua_var.tpdata[id].pub_addr); |
| 3057 |
if (status != PJ_SUCCESS) { |
| 3058 |
pjsua_perror(THIS_FILE, "Error deleting pjsua_transport " |
| 3059 |
"UPnP port mapping", status); |
| 3060 |
} |
| 3061 |
} |
| 3062 |
#endif |
| 3063 |
|
| 3064 |
status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp); |
| 3065 |
break; |
| 3066 |
case PJSIP_TRANSPORT_TLS: |
| 3067 |
case PJSIP_TRANSPORT_TCP: |
| 3068 |
|
| 3069 |
|
| 3070 |
|
| 3071 |
status = (*pjsua_var.tpdata[id].data.factory->destroy) |
| 3072 |
(pjsua_var.tpdata[id].data.factory); |
| 3073 |
break; |
| 3074 |
default: |
| 3075 |
return PJ_EINVAL; |
| 3076 |
} |
| 3077 |
|
| 3078 |
|
| 3079 |
|
| 3080 |
|
| 3081 |
|
| 3082 |
if (status == PJ_SUCCESS) { |
| 3083 |
pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED; |
| 3084 |
pjsua_var.tpdata[id].data.ptr = NULL; |
| 3085 |
} |
| 3086 |
|
| 3087 |
return status; |
| 3088 |
} |
| 3089 |
|
| 3090 |
|
| 3091 |
PJ_DEF(pj_status_t) pjsua_transport_lis_start(pjsua_transport_id id, |
| 3092 |
const pjsua_transport_config *cfg) |
| 3093 |
{ |
| 3094 |
pj_status_t status = PJ_SUCCESS; |
| 3095 |
pjsip_transport_type_e tp_type; |
| 3096 |
|
| 3097 |
|
| 3098 |
PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 3099 |
PJ_EINVAL); |
| 3100 |
|
| 3101 |
|
| 3102 |
PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
| 3103 |
|
| 3104 |
tp_type = pjsua_var.tpdata[id].type & ~PJSIP_TRANSPORT_IPV6; |
| 3105 |
|
| 3106 |
if ((tp_type == PJSIP_TRANSPORT_TLS) || (tp_type == PJSIP_TRANSPORT_TCP)) { |
| 3107 |
pj_sockaddr bind_addr; |
| 3108 |
pjsip_host_port addr_name; |
| 3109 |
pjsip_tpfactory *factory = pjsua_var.tpdata[id].data.factory; |
| 3110 |
|
| 3111 |
int af = pjsip_transport_type_get_af(factory->type); |
| 3112 |
|
| 3113 |
if (cfg->port) |
| 3114 |
pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)cfg->port); |
| 3115 |
|
| 3116 |
if (cfg->bound_addr.slen) { |
| 3117 |
status = pj_sockaddr_set_str_addr(af, |
| 3118 |
&bind_addr, |
| 3119 |
&cfg->bound_addr); |
| 3120 |
if (status != PJ_SUCCESS) { |
| 3121 |
pjsua_perror(THIS_FILE, |
| 3122 |
"Unable to resolve transport bound address", |
| 3123 |
status); |
| 3124 |
return status; |
| 3125 |
} |
| 3126 |
} |
| 3127 |
|
| 3128 |
|
| 3129 |
pj_bzero(&addr_name, sizeof(pjsip_host_port)); |
| 3130 |
if (cfg->public_addr.slen) |
| 3131 |
addr_name.host = cfg->public_addr; |
| 3132 |
|
| 3133 |
if (tp_type == PJSIP_TRANSPORT_TCP) { |
| 3134 |
status = pjsip_tcp_transport_lis_start(factory, &bind_addr, |
| 3135 |
&addr_name); |
| 3136 |
} |
| 3137 |
#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0 |
| 3138 |
else { |
| 3139 |
status = pjsip_tls_transport_lis_start(factory, &bind_addr, |
| 3140 |
&addr_name); |
| 3141 |
} |
| 3142 |
#endif |
| 3143 |
} else if (tp_type == PJSIP_TRANSPORT_UDP) { |
| 3144 |
status = PJ_SUCCESS; |
| 3145 |
} else { |
| 3146 |
status = PJ_EINVAL; |
| 3147 |
} |
| 3148 |
return status; |
| 3149 |
} |
| 3150 |
|
| 3151 |
|
| 3152 |
PJ_DEF(pj_status_t) pjsua_transport_lis_restart(pjsua_transport_id id, |
| 3153 |
const pjsua_transport_config *cfg) |
| 3154 |
{ |
| 3155 |
pj_status_t status = PJ_SUCCESS; |
| 3156 |
pjsip_transport_type_e tp_type; |
| 3157 |
|
| 3158 |
pj_sockaddr bind_addr; |
| 3159 |
pjsip_host_port addr_name; |
| 3160 |
int af; |
| 3161 |
|
| 3162 |
|
| 3163 |
PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), |
| 3164 |
PJ_EINVAL); |
| 3165 |
|
| 3166 |
|
| 3167 |
PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
| 3168 |
|
| 3169 |
tp_type = pjsua_var.tpdata[id].type & ~PJSIP_TRANSPORT_IPV6; |
| 3170 |
|
| 3171 |
if ((tp_type == PJSIP_TRANSPORT_TLS) || (tp_type == PJSIP_TRANSPORT_TCP)) { |
| 3172 |
pjsip_tpfactory *factory = pjsua_var.tpdata[id].data.factory; |
| 3173 |
af = pjsip_transport_type_get_af(factory->type); |
| 3174 |
} else if (tp_type == PJSIP_TRANSPORT_UDP) { |
| 3175 |
pjsip_transport *transport = pjsua_var.tpdata[id].data.tp; |
| 3176 |
af = pjsip_transport_type_get_af(transport->key.type); |
| 3177 |
} else { |
| 3178 |
return PJ_EINVAL; |
| 3179 |
} |
| 3180 |
|
| 3181 |
|
| 3182 |
pj_sockaddr_init(af, &bind_addr, NULL, 0); |
| 3183 |
|
| 3184 |
if (cfg->port) |
| 3185 |
pj_sockaddr_set_port(&bind_addr, (pj_uint16_t)cfg->port); |
| 3186 |
|
| 3187 |
if (cfg->bound_addr.slen) { |
| 3188 |
status = pj_sockaddr_set_str_addr(af, |
| 3189 |
&bind_addr, |
| 3190 |
&cfg->bound_addr); |
| 3191 |
if (status != PJ_SUCCESS) { |
| 3192 |
pjsua_perror(THIS_FILE, |
| 3193 |
"Unable to resolve transport bound address", |
| 3194 |
status); |
| 3195 |
return status; |
| 3196 |
} |
| 3197 |
} |
| 3198 |
|
| 3199 |
|
| 3200 |
pj_bzero(&addr_name, sizeof(pjsip_host_port)); |
| 3201 |
if (cfg->public_addr.slen) |
| 3202 |
addr_name.host = cfg->public_addr; |
| 3203 |
|
| 3204 |
|
| 3205 |
if ((tp_type == PJSIP_TRANSPORT_TLS) || (tp_type == PJSIP_TRANSPORT_TCP)) { |
| 3206 |
pjsip_tpfactory *factory = pjsua_var.tpdata[id].data.factory; |
| 3207 |
|
| 3208 |
if (tp_type == PJSIP_TRANSPORT_TCP) { |
| 3209 |
status = pjsip_tcp_transport_restart(factory, &bind_addr, |
| 3210 |
&addr_name); |
| 3211 |
} |
| 3212 |
#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0 |
| 3213 |
else { |
| 3214 |
|
| 3215 |
status = pjsip_tls_transport_restart2(factory, &cfg->tls_setting, &bind_addr, |
| 3216 |
&addr_name); |
| 3217 |
} |
| 3218 |
#endif |
| 3219 |
} else if (tp_type == PJSIP_TRANSPORT_UDP) { |
| 3220 |
pjsip_transport *transport = pjsua_var.tpdata[id].data.tp; |
| 3221 |
|
| 3222 |
|
| 3223 |
status = pjsip_udp_transport_restart2(transport, |
| 3224 |
PJSIP_UDP_TRANSPORT_DESTROY_SOCKET, |
| 3225 |
PJ_INVALID_SOCKET, |
| 3226 |
&bind_addr, |
| 3227 |
cfg->public_addr.slen ? &addr_name : NULL); |
| 3228 |
} |
| 3229 |
|
| 3230 |
return status; |
| 3231 |
} |
| 3232 |
|
| 3233 |
|
| 3234 |
|
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
void pjsua_process_msg_data(pjsip_tx_data *tdata, |
| 3239 |
const pjsua_msg_data *msg_data) |
| 3240 |
{ |
| 3241 |
pj_bool_t allow_body; |
| 3242 |
const pjsip_hdr *hdr; |
| 3243 |
|
| 3244 |
|
| 3245 |
if (pjsua_var.ua_cfg.user_agent.slen && |
| 3246 |
tdata->msg->type == PJSIP_REQUEST_MSG) |
| 3247 |
{ |
| 3248 |
const pj_str_t STR_USER_AGENT = { "User-Agent", 10 }; |
| 3249 |
pjsip_hdr *h; |
| 3250 |
h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool, |
| 3251 |
&STR_USER_AGENT, |
| 3252 |
&pjsua_var.ua_cfg.user_agent); |
| 3253 |
pjsip_msg_add_hdr(tdata->msg, h); |
| 3254 |
} |
| 3255 |
|
| 3256 |
if (!msg_data) |
| 3257 |
return; |
| 3258 |
|
| 3259 |
hdr = msg_data->hdr_list.next; |
| 3260 |
while (hdr && hdr != &msg_data->hdr_list) { |
| 3261 |
pjsip_hdr *new_hdr; |
| 3262 |
|
| 3263 |
|
| 3264 |
|
| 3265 |
|
| 3266 |
if (hdr->type == PJSIP_H_MAX_FORWARDS) { |
| 3267 |
pjsip_max_fwd_hdr *orig_hdr; |
| 3268 |
|
| 3269 |
orig_hdr = pjsip_hdr_find(&tdata->msg->hdr, PJSIP_H_MAX_FORWARDS, |
| 3270 |
NULL); |
| 3271 |
if (orig_hdr != NULL) { |
| 3272 |
pj_uint32_t orig_value = orig_hdr->ivalue; |
| 3273 |
pj_uint32_t new_value = |
| 3274 |
((const pjsip_max_fwd_hdr*)hdr)->ivalue; |
| 3275 |
|
| 3276 |
orig_hdr->ivalue = new_value; |
| 3277 |
PJ_LOG(4, (THIS_FILE, |
| 3278 |
"Overriding Max-Forwards header value: %u -> %u", |
| 3279 |
orig_value, new_value)); |
| 3280 |
|
| 3281 |
hdr = hdr->next; |
| 3282 |
continue; |
| 3283 |
} |
| 3284 |
} |
| 3285 |
|
| 3286 |
new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr); |
| 3287 |
pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 3288 |
|
| 3289 |
hdr = hdr->next; |
| 3290 |
} |
| 3291 |
|
| 3292 |
allow_body = (tdata->msg->body == NULL); |
| 3293 |
|
| 3294 |
if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) { |
| 3295 |
pjsip_media_type ctype; |
| 3296 |
pjsip_msg_body *body; |
| 3297 |
|
| 3298 |
pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype); |
| 3299 |
body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype, |
| 3300 |
&msg_data->msg_body); |
| 3301 |
tdata->msg->body = body; |
| 3302 |
} |
| 3303 |
|
| 3304 |
|
| 3305 |
if (!pj_list_empty(&msg_data->multipart_parts) && |
| 3306 |
msg_data->multipart_ctype.type.slen) |
| 3307 |
{ |
| 3308 |
pjsip_msg_body *bodies; |
| 3309 |
pjsip_multipart_part *part; |
| 3310 |
pj_str_t *boundary = NULL; |
| 3311 |
|
| 3312 |
bodies = pjsip_multipart_create(tdata->pool, |
| 3313 |
&msg_data->multipart_ctype, |
| 3314 |
boundary); |
| 3315 |
part = msg_data->multipart_parts.next; |
| 3316 |
while (part != &msg_data->multipart_parts) { |
| 3317 |
pjsip_multipart_part *part_copy; |
| 3318 |
|
| 3319 |
part_copy = pjsip_multipart_clone_part(tdata->pool, part); |
| 3320 |
pjsip_multipart_add_part(tdata->pool, bodies, part_copy); |
| 3321 |
part = part->next; |
| 3322 |
} |
| 3323 |
|
| 3324 |
if (tdata->msg->body) { |
| 3325 |
part = pjsip_multipart_create_part(tdata->pool); |
| 3326 |
part->body = tdata->msg->body; |
| 3327 |
pjsip_multipart_add_part(tdata->pool, bodies, part); |
| 3328 |
|
| 3329 |
tdata->msg->body = NULL; |
| 3330 |
} |
| 3331 |
|
| 3332 |
tdata->msg->body = bodies; |
| 3333 |
} |
| 3334 |
} |
| 3335 |
|
| 3336 |
|
| 3337 |
|
| 3338 |
|
| 3339 |
|
| 3340 |
void pjsua_set_msg_route_set( pjsip_tx_data *tdata, |
| 3341 |
const pjsip_route_hdr *route_set ) |
| 3342 |
{ |
| 3343 |
const pjsip_route_hdr *r; |
| 3344 |
|
| 3345 |
r = route_set->next; |
| 3346 |
while (r != route_set) { |
| 3347 |
pjsip_route_hdr *new_r; |
| 3348 |
|
| 3349 |
new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r); |
| 3350 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r); |
| 3351 |
|
| 3352 |
r = r->next; |
| 3353 |
} |
| 3354 |
} |
| 3355 |
|
| 3356 |
|
| 3357 |
|
| 3358 |
|
| 3359 |
|
| 3360 |
void pjsua_parse_media_type( pj_pool_t *pool, |
| 3361 |
const pj_str_t *mime, |
| 3362 |
pjsip_media_type *media_type) |
| 3363 |
{ |
| 3364 |
pj_str_t tmp; |
| 3365 |
char *pos; |
| 3366 |
|
| 3367 |
pj_bzero(media_type, sizeof(*media_type)); |
| 3368 |
|
| 3369 |
pj_strdup_with_null(pool, &tmp, mime); |
| 3370 |
|
| 3371 |
pos = pj_strchr(&tmp, '/'); |
| 3372 |
if (pos) { |
| 3373 |
media_type->type.ptr = tmp.ptr; |
| 3374 |
media_type->type.slen = (pos-tmp.ptr); |
| 3375 |
media_type->subtype.ptr = pos+1; |
| 3376 |
media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1; |
| 3377 |
} else { |
| 3378 |
media_type->type = tmp; |
| 3379 |
} |
| 3380 |
} |
| 3381 |
|
| 3382 |
|
| 3383 |
|
| 3384 |
|
| 3385 |
|
| 3386 |
void pjsua_init_tpselector(pjsua_acc_id acc_id, |
| 3387 |
pjsip_tpselector *sel) |
| 3388 |
{ |
| 3389 |
pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 3390 |
|
| 3391 |
pj_bzero(sel, sizeof(*sel)); |
| 3392 |
|
| 3393 |
if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 3394 |
pjsua_transport_data *tpdata; |
| 3395 |
unsigned flag; |
| 3396 |
|
| 3397 |
PJ_ASSERT_RETURN(acc->cfg.transport_id >= 0 && |
| 3398 |
acc->cfg.transport_id < |
| 3399 |
(int)PJ_ARRAY_SIZE(pjsua_var.tpdata), ); |
| 3400 |
tpdata = &pjsua_var.tpdata[acc->cfg.transport_id]; |
| 3401 |
|
| 3402 |
flag = pjsip_transport_get_flag_from_type(tpdata->type); |
| 3403 |
|
| 3404 |
if (flag & PJSIP_TRANSPORT_DATAGRAM) { |
| 3405 |
sel->type = PJSIP_TPSELECTOR_TRANSPORT; |
| 3406 |
sel->u.transport = tpdata->data.tp; |
| 3407 |
} else { |
| 3408 |
sel->type = PJSIP_TPSELECTOR_LISTENER; |
| 3409 |
sel->u.listener = tpdata->data.factory; |
| 3410 |
} |
| 3411 |
} else if (acc->cfg.ipv6_sip_use != PJSUA_IPV6_ENABLED_NO_PREFERENCE) { |
| 3412 |
sel->type = PJSIP_TPSELECTOR_IP_VER; |
| 3413 |
sel->u.ip_ver = (pjsip_tpselector_ip_ver)acc->cfg.ipv6_sip_use; |
| 3414 |
} |
| 3415 |
} |
| 3416 |
|
| 3417 |
|
| 3418 |
PJ_DEF(void) pjsua_ip_change_param_default(pjsua_ip_change_param *param) |
| 3419 |
{ |
| 3420 |
pj_bzero(param, sizeof(*param)); |
| 3421 |
param->restart_listener = PJ_TRUE; |
| 3422 |
param->restart_lis_delay = PJSUA_TRANSPORT_RESTART_DELAY_TIME; |
| 3423 |
param->shutdown_transport = PJ_TRUE; |
| 3424 |
} |
| 3425 |
|
| 3426 |
|
| 3427 |
|
| 3428 |
static void nat_detect_cb(void *user_data, |
| 3429 |
const pj_stun_nat_detect_result *res) |
| 3430 |
{ |
| 3431 |
PJ_UNUSED_ARG(user_data); |
| 3432 |
|
| 3433 |
pjsua_var.nat_in_progress = PJ_FALSE; |
| 3434 |
pjsua_var.nat_status = res->status; |
| 3435 |
pjsua_var.nat_type = res->nat_type; |
| 3436 |
|
| 3437 |
if (pjsua_var.ua_cfg.cb.on_nat_detect) { |
| 3438 |
(*pjsua_var.ua_cfg.cb.on_nat_detect)(res); |
| 3439 |
} |
| 3440 |
} |
| 3441 |
|
| 3442 |
|
| 3443 |
|
| 3444 |
|
| 3445 |
|
| 3446 |
PJ_DEF(pj_status_t) pjsua_detect_nat_type() |
| 3447 |
{ |
| 3448 |
pj_status_t status; |
| 3449 |
|
| 3450 |
if (pjsua_var.nat_in_progress) |
| 3451 |
return PJ_SUCCESS; |
| 3452 |
|
| 3453 |
|
| 3454 |
status = resolve_stun_server(PJ_TRUE, PJ_TRUE, 0); |
| 3455 |
if (status != PJ_SUCCESS) { |
| 3456 |
pjsua_var.nat_status = status; |
| 3457 |
pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN; |
| 3458 |
return status; |
| 3459 |
} |
| 3460 |
|
| 3461 |
|
| 3462 |
if (pjsua_var.stun_srv.addr.sa_family == 0) { |
| 3463 |
pjsua_var.nat_status = PJNATH_ESTUNINSERVER; |
| 3464 |
return PJNATH_ESTUNINSERVER; |
| 3465 |
} |
| 3466 |
|
| 3467 |
status = pj_stun_detect_nat_type2(&pjsua_var.stun_srv, |
| 3468 |
&pjsua_var.stun_cfg, |
| 3469 |
NULL, &nat_detect_cb); |
| 3470 |
|
| 3471 |
if (status != PJ_SUCCESS) { |
| 3472 |
pjsua_var.nat_status = status; |
| 3473 |
pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN; |
| 3474 |
return status; |
| 3475 |
} |
| 3476 |
|
| 3477 |
pjsua_var.nat_in_progress = PJ_TRUE; |
| 3478 |
|
| 3479 |
return PJ_SUCCESS; |
| 3480 |
} |
| 3481 |
|
| 3482 |
|
| 3483 |
|
| 3484 |
|
| 3485 |
|
| 3486 |
PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type) |
| 3487 |
{ |
| 3488 |
*type = pjsua_var.nat_type; |
| 3489 |
return pjsua_var.nat_status; |
| 3490 |
} |
| 3491 |
|
| 3492 |
|
| 3493 |
|
| 3494 |
|
| 3495 |
PJ_DEF(pj_status_t) pjsua_verify_url(const char *c_url) |
| 3496 |
{ |
| 3497 |
pjsip_uri *p; |
| 3498 |
pj_pool_t *pool; |
| 3499 |
char *url; |
| 3500 |
pj_size_t len = (c_url ? pj_ansi_strlen(c_url) : 0); |
| 3501 |
|
| 3502 |
if (!len) return PJSIP_EINVALIDURI; |
| 3503 |
|
| 3504 |
pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL); |
| 3505 |
if (!pool) return PJ_ENOMEM; |
| 3506 |
|
| 3507 |
url = (char*) pj_pool_alloc(pool, len+1); |
| 3508 |
pj_ansi_strxcpy(url, c_url, len+1); |
| 3509 |
|
| 3510 |
p = pjsip_parse_uri(pool, url, len, 0); |
| 3511 |
|
| 3512 |
pj_pool_release(pool); |
| 3513 |
return p ? 0 : PJSIP_EINVALIDURI; |
| 3514 |
} |
| 3515 |
|
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url) |
| 3520 |
{ |
| 3521 |
pjsip_uri *p; |
| 3522 |
pj_pool_t *pool; |
| 3523 |
char *url; |
| 3524 |
pj_size_t len = (c_url ? pj_ansi_strlen(c_url) : 0); |
| 3525 |
|
| 3526 |
if (!len) return PJSIP_EINVALIDURI; |
| 3527 |
|
| 3528 |
pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL); |
| 3529 |
if (!pool) return PJ_ENOMEM; |
| 3530 |
|
| 3531 |
url = (char*) pj_pool_calloc(pool, 1, (unsigned)len+1); |
| 3532 |
pj_ansi_strxcpy(url, c_url, len+1); |
| 3533 |
|
| 3534 |
p = pjsip_parse_uri(pool, url, len, 0); |
| 3535 |
if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 && |
| 3536 |
pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0)) |
| 3537 |
{ |
| 3538 |
p = NULL; |
| 3539 |
} |
| 3540 |
|
| 3541 |
pj_pool_release(pool); |
| 3542 |
return p ? 0 : PJSIP_EINVALIDURI; |
| 3543 |
} |
| 3544 |
|
| 3545 |
|
| 3546 |
|
| 3547 |
|
| 3548 |
#if PJ_TIMER_DEBUG |
| 3549 |
PJ_DEF(pj_status_t) pjsua_schedule_timer_dbg( pj_timer_entry *entry, |
| 3550 |
const pj_time_val *delay, |
| 3551 |
const char *src_file, |
| 3552 |
int src_line) |
| 3553 |
{ |
| 3554 |
return pjsip_endpt_schedule_timer_dbg(pjsua_var.endpt, entry, delay, |
| 3555 |
src_file, src_line); |
| 3556 |
} |
| 3557 |
#else |
| 3558 |
PJ_DEF(pj_status_t) pjsua_schedule_timer( pj_timer_entry *entry, |
| 3559 |
const pj_time_val *delay) |
| 3560 |
{ |
| 3561 |
return pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, delay); |
| 3562 |
} |
| 3563 |
#endif |
| 3564 |
|
| 3565 |
|
| 3566 |
static void timer_cb( pj_timer_heap_t *th, |
| 3567 |
pj_timer_entry *entry) |
| 3568 |
{ |
| 3569 |
pjsua_timer_list *tmr = (pjsua_timer_list *)entry->user_data; |
| 3570 |
void (*cb)(void *user_data) = tmr->cb; |
| 3571 |
void *user_data = tmr->user_data; |
| 3572 |
|
| 3573 |
PJ_UNUSED_ARG(th); |
| 3574 |
|
| 3575 |
if (cb) |
| 3576 |
(*cb)(user_data); |
| 3577 |
|
| 3578 |
pj_mutex_lock(pjsua_var.timer_mutex); |
| 3579 |
pj_list_erase(tmr); |
| 3580 |
pj_list_push_back(&pjsua_var.timer_list, tmr); |
| 3581 |
pj_mutex_unlock(pjsua_var.timer_mutex); |
| 3582 |
} |
| 3583 |
|
| 3584 |
|
| 3585 |
|
| 3586 |
|
| 3587 |
#if PJ_TIMER_DEBUG |
| 3588 |
PJ_DEF(pj_status_t) pjsua_schedule_timer2_dbg( void (*cb)(void *user_data), |
| 3589 |
void *user_data, |
| 3590 |
unsigned msec_delay, |
| 3591 |
const char *src_file, |
| 3592 |
int src_line) |
| 3593 |
#else |
| 3594 |
PJ_DEF(pj_status_t) pjsua_schedule_timer2( void (*cb)(void *user_data), |
| 3595 |
void *user_data, |
| 3596 |
unsigned msec_delay) |
| 3597 |
#endif |
| 3598 |
{ |
| 3599 |
pjsua_timer_list *tmr = NULL; |
| 3600 |
pj_status_t status; |
| 3601 |
pj_time_val delay; |
| 3602 |
|
| 3603 |
pj_mutex_lock(pjsua_var.timer_mutex); |
| 3604 |
|
| 3605 |
if (pj_list_empty(&pjsua_var.timer_list)) { |
| 3606 |
tmr = PJ_POOL_ALLOC_T(pjsua_var.timer_pool, pjsua_timer_list); |
| 3607 |
} else { |
| 3608 |
tmr = pjsua_var.timer_list.next; |
| 3609 |
pj_list_erase(tmr); |
| 3610 |
} |
| 3611 |
pj_timer_entry_init(&tmr->entry, 0, tmr, timer_cb); |
| 3612 |
tmr->cb = cb; |
| 3613 |
tmr->user_data = user_data; |
| 3614 |
delay.sec = 0; |
| 3615 |
delay.msec = msec_delay; |
| 3616 |
|
| 3617 |
#if PJ_TIMER_DEBUG |
| 3618 |
status = pjsip_endpt_schedule_timer_dbg(pjsua_var.endpt, &tmr->entry, |
| 3619 |
&delay, src_file, src_line); |
| 3620 |
#else |
| 3621 |
status = pjsip_endpt_schedule_timer(pjsua_var.endpt, &tmr->entry, &delay); |
| 3622 |
#endif |
| 3623 |
if (status == PJ_SUCCESS) { |
| 3624 |
pj_list_push_back(&pjsua_var.active_timer_list, tmr); |
| 3625 |
} else { |
| 3626 |
pj_list_push_back(&pjsua_var.timer_list, tmr); |
| 3627 |
} |
| 3628 |
|
| 3629 |
pj_mutex_unlock(pjsua_var.timer_mutex); |
| 3630 |
|
| 3631 |
return status; |
| 3632 |
} |
| 3633 |
|
| 3634 |
|
| 3635 |
|
| 3636 |
|
| 3637 |
|
| 3638 |
PJ_DEF(void) pjsua_cancel_timer(pj_timer_entry *entry) |
| 3639 |
{ |
| 3640 |
pjsip_endpt_cancel_timer(pjsua_var.endpt, entry); |
| 3641 |
} |
| 3642 |
|
| 3643 |
|
| 3644 |
|
| 3645 |
|
| 3646 |
|
| 3647 |
pj_status_t normalize_route_uri(pj_pool_t *pool, pj_str_t *uri) |
| 3648 |
{ |
| 3649 |
pj_str_t tmp_uri; |
| 3650 |
pj_pool_t *tmp_pool; |
| 3651 |
pjsip_uri *uri_obj; |
| 3652 |
pjsip_sip_uri *sip_uri; |
| 3653 |
|
| 3654 |
tmp_pool = pjsua_pool_create("tmplr%p", 512, 512); |
| 3655 |
if (!tmp_pool) |
| 3656 |
return PJ_ENOMEM; |
| 3657 |
|
| 3658 |
pj_strdup_with_null(tmp_pool, &tmp_uri, uri); |
| 3659 |
|
| 3660 |
uri_obj = pjsip_parse_uri(tmp_pool, tmp_uri.ptr, tmp_uri.slen, 0); |
| 3661 |
if (!uri_obj) { |
| 3662 |
PJ_LOG(1,(THIS_FILE, "Invalid route URI: %.*s", |
| 3663 |
(int)uri->slen, uri->ptr)); |
| 3664 |
pj_pool_release(tmp_pool); |
| 3665 |
return PJSIP_EINVALIDURI; |
| 3666 |
} |
| 3667 |
|
| 3668 |
if (!PJSIP_URI_SCHEME_IS_SIP(uri_obj) && |
| 3669 |
!PJSIP_URI_SCHEME_IS_SIPS(uri_obj)) |
| 3670 |
{ |
| 3671 |
PJ_LOG(1,(THIS_FILE, "Route URI must be SIP URI: %.*s", |
| 3672 |
(int)uri->slen, uri->ptr)); |
| 3673 |
pj_pool_release(tmp_pool); |
| 3674 |
return PJSIP_EINVALIDSCHEME; |
| 3675 |
} |
| 3676 |
|
| 3677 |
sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri_obj); |
| 3678 |
|
| 3679 |
|
| 3680 |
if (!pjsua_var.ua_cfg.force_lr || sip_uri->lr_param) { |
| 3681 |
pj_pool_release(tmp_pool); |
| 3682 |
return PJ_SUCCESS; |
| 3683 |
} |
| 3684 |
|
| 3685 |
|
| 3686 |
sip_uri->lr_param = 1; |
| 3687 |
|
| 3688 |
|
| 3689 |
tmp_uri.ptr = (char*) pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE); |
| 3690 |
tmp_uri.slen = pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, uri_obj, |
| 3691 |
tmp_uri.ptr, PJSIP_MAX_URL_SIZE); |
| 3692 |
if (tmp_uri.slen < 1) { |
| 3693 |
PJ_LOG(1,(THIS_FILE, "Route URI is too long: %.*s", |
| 3694 |
(int)uri->slen, uri->ptr)); |
| 3695 |
pj_pool_release(tmp_pool); |
| 3696 |
return PJSIP_EURITOOLONG; |
| 3697 |
} |
| 3698 |
|
| 3699 |
|
| 3700 |
pj_strdup_with_null(pool, uri, &tmp_uri); |
| 3701 |
|
| 3702 |
pj_pool_release(tmp_pool); |
| 3703 |
return PJ_SUCCESS; |
| 3704 |
} |
| 3705 |
|
| 3706 |
|
| 3707 |
|
| 3708 |
|
| 3709 |
|
| 3710 |
PJ_DEF(void) pjsua_dump(pj_bool_t detail) |
| 3711 |
{ |
| 3712 |
unsigned old_decor; |
| 3713 |
unsigned i; |
| 3714 |
|
| 3715 |
PJ_LOG(3,(THIS_FILE, "Start dumping application states:")); |
| 3716 |
|
| 3717 |
old_decor = pj_log_get_decor(); |
| 3718 |
pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR)); |
| 3719 |
|
| 3720 |
if (detail) { |
| 3721 |
pj_dump_config(); |
| 3722 |
pjsip_dump_config(); |
| 3723 |
} |
| 3724 |
|
| 3725 |
pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail); |
| 3726 |
|
| 3727 |
pjmedia_endpt_dump(pjsua_get_pjmedia_endpt()); |
| 3728 |
|
| 3729 |
PJ_LOG(3,(THIS_FILE, "Dumping media transports:")); |
| 3730 |
for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 3731 |
pjsua_call *call = &pjsua_var.calls[i]; |
| 3732 |
pjsua_acc_config *acc_cfg; |
| 3733 |
pjmedia_transport *tp[PJSUA_MAX_CALL_MEDIA*2]; |
| 3734 |
unsigned tp_cnt = 0; |
| 3735 |
unsigned j; |
| 3736 |
|
| 3737 |
|
| 3738 |
for (j = 0; j < call->med_cnt; ++j) { |
| 3739 |
if (call->media[j].tp != NULL) |
| 3740 |
tp[tp_cnt++] = call->media[j].tp; |
| 3741 |
} |
| 3742 |
for (j = 0; j < call->med_prov_cnt; ++j) { |
| 3743 |
pjmedia_transport *med_tp = call->media_prov[j].tp; |
| 3744 |
if (med_tp) { |
| 3745 |
unsigned k; |
| 3746 |
pj_bool_t used = PJ_FALSE; |
| 3747 |
for (k = 0; k < tp_cnt; ++k) { |
| 3748 |
if (med_tp == tp[k]) { |
| 3749 |
used = PJ_TRUE; |
| 3750 |
break; |
| 3751 |
} |
| 3752 |
} |
| 3753 |
if (!used) |
| 3754 |
tp[tp_cnt++] = med_tp; |
| 3755 |
} |
| 3756 |
} |
| 3757 |
|
| 3758 |
acc_cfg = &pjsua_var.acc[call->acc_id].cfg; |
| 3759 |
|
| 3760 |
|
| 3761 |
for (j = 0; j < tp_cnt; ++j) { |
| 3762 |
pjmedia_transport_info tpinfo; |
| 3763 |
char addr_buf[80]; |
| 3764 |
|
| 3765 |
pjmedia_transport_info_init(&tpinfo); |
| 3766 |
pjmedia_transport_get_info(tp[j], &tpinfo); |
| 3767 |
PJ_LOG(3,(THIS_FILE, " %s: %s", |
| 3768 |
(acc_cfg->ice_cfg.enable_ice ? "ICE" : "UDP"), |
| 3769 |
pj_sockaddr_print(&tpinfo.sock_info.rtp_addr_name, |
| 3770 |
addr_buf, |
| 3771 |
sizeof(addr_buf), 3))); |
| 3772 |
} |
| 3773 |
} |
| 3774 |
|
| 3775 |
pjsip_tsx_layer_dump(detail); |
| 3776 |
pjsip_ua_dump(detail); |
| 3777 |
|
| 3778 |
|
| 3779 |
|
| 3780 |
#if 0 |
| 3781 |
|
| 3782 |
PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:")); |
| 3783 |
|
| 3784 |
if (pjsua_call_get_count() == 0) { |
| 3785 |
|
| 3786 |
PJ_LOG(3,(THIS_FILE, " - no sessions -")); |
| 3787 |
|
| 3788 |
} else { |
| 3789 |
unsigned i; |
| 3790 |
|
| 3791 |
for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 3792 |
if (pjsua_call_is_active(i)) { |
| 3793 |
|
| 3794 |
|
| 3795 |
|
| 3796 |
char buf[1024 * 3]; |
| 3797 |
unsigned call_dump_len; |
| 3798 |
unsigned part_len; |
| 3799 |
unsigned part_idx; |
| 3800 |
unsigned log_decor; |
| 3801 |
|
| 3802 |
pjsua_call_dump(i, detail, buf, sizeof(buf), " "); |
| 3803 |
call_dump_len = strlen(buf); |
| 3804 |
|
| 3805 |
log_decor = pj_log_get_decor(); |
| 3806 |
pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | |
| 3807 |
PJ_LOG_HAS_CR)); |
| 3808 |
PJ_LOG(3,(THIS_FILE, "\n")); |
| 3809 |
pj_log_set_decor(0); |
| 3810 |
|
| 3811 |
part_idx = 0; |
| 3812 |
part_len = PJ_LOG_MAX_SIZE-80; |
| 3813 |
while (part_idx < call_dump_len) { |
| 3814 |
char p_orig, *p; |
| 3815 |
|
| 3816 |
p = &buf[part_idx]; |
| 3817 |
if (part_idx + part_len > call_dump_len) |
| 3818 |
part_len = call_dump_len - part_idx; |
| 3819 |
p_orig = p[part_len]; |
| 3820 |
p[part_len] = '\0'; |
| 3821 |
PJ_LOG(3,(THIS_FILE, "%s", p)); |
| 3822 |
p[part_len] = p_orig; |
| 3823 |
part_idx += part_len; |
| 3824 |
} |
| 3825 |
pj_log_set_decor(log_decor); |
| 3826 |
} |
| 3827 |
} |
| 3828 |
} |
| 3829 |
#endif |
| 3830 |
|
| 3831 |
|
| 3832 |
pjsua_pres_dump(detail); |
| 3833 |
|
| 3834 |
pj_log_set_decor(old_decor); |
| 3835 |
PJ_LOG(3,(THIS_FILE, "Dump complete")); |
| 3836 |
} |
| 3837 |
|
| 3838 |
|
| 3839 |
|
| 3840 |
static void restart_listener_cb(void *user_data); |
| 3841 |
|
| 3842 |
|
| 3843 |
static pj_status_t handle_ip_change_on_acc() |
| 3844 |
{ |
| 3845 |
int i = 0; |
| 3846 |
pj_status_t status = PJ_SUCCESS; |
| 3847 |
pj_bool_t acc_done[PJSUA_MAX_ACC]; |
| 3848 |
|
| 3849 |
PJSUA_LOCK(); |
| 3850 |
|
| 3851 |
if (pjsua_var.acc_cnt == 0) { |
| 3852 |
PJ_LOG(3, (THIS_FILE, |
| 3853 |
"No account is set, IP change handling will stop")); |
| 3854 |
pjsua_acc_end_ip_change(NULL); |
| 3855 |
PJSUA_UNLOCK(); |
| 3856 |
return status; |
| 3857 |
} |
| 3858 |
|
| 3859 |
|
| 3860 |
for (; i < (int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 3861 |
pjsua_var.acc[i].ip_change_op = PJSUA_IP_CHANGE_OP_NULL; |
| 3862 |
acc_done[i] = PJ_FALSE; |
| 3863 |
} |
| 3864 |
|
| 3865 |
for (i = 0; i < (int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 3866 |
pj_bool_t shutdown_transport = PJ_FALSE; |
| 3867 |
pjsip_regc_info regc_info; |
| 3868 |
char acc_id[PJSUA_MAX_ACC * 4]; |
| 3869 |
pjsua_acc *acc = &pjsua_var.acc[i]; |
| 3870 |
pjsip_transport *transport = NULL; |
| 3871 |
pjsua_acc_id shut_acc_ids[PJSUA_MAX_ACC]; |
| 3872 |
unsigned shut_acc_cnt = 0; |
| 3873 |
|
| 3874 |
if (!acc->valid || (acc_done[i])) |
| 3875 |
continue; |
| 3876 |
|
| 3877 |
if (acc->regc) { |
| 3878 |
unsigned j = 0; |
| 3879 |
pj_status_t found_restart_tp_fail = PJ_FALSE; |
| 3880 |
|
| 3881 |
pjsip_regc_get_info(acc->regc, ®c_info); |
| 3882 |
|
| 3883 |
|
| 3884 |
for (; j < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++j) { |
| 3885 |
if (pjsua_var.tpdata[j].data.ptr != NULL && |
| 3886 |
pjsua_var.tpdata[j].restart_status != PJ_SUCCESS && |
| 3887 |
pjsua_var.tpdata[j].type == regc_info.transport->key.type) |
| 3888 |
{ |
| 3889 |
if ((pjsua_var.tpdata[j].data.factory |
| 3890 |
== regc_info.transport->factory) || |
| 3891 |
(pjsua_var.tpdata[j].data.tp |
| 3892 |
== regc_info.transport)) |
| 3893 |
{ |
| 3894 |
found_restart_tp_fail = PJ_TRUE; |
| 3895 |
break; |
| 3896 |
} |
| 3897 |
} |
| 3898 |
} |
| 3899 |
|
| 3900 |
if (found_restart_tp_fail) { |
| 3901 |
if (acc->ka_timer.id) { |
| 3902 |
pjsip_endpt_cancel_timer(pjsua_var.endpt, &acc->ka_timer); |
| 3903 |
acc->ka_timer.id = PJ_FALSE; |
| 3904 |
|
| 3905 |
if (acc->ka_transport) { |
| 3906 |
pjsip_transport_dec_ref(acc->ka_transport); |
| 3907 |
acc->ka_transport = NULL; |
| 3908 |
} |
| 3909 |
} |
| 3910 |
pjsua_acc_end_ip_change(acc); |
| 3911 |
continue; |
| 3912 |
} |
| 3913 |
|
| 3914 |
if ((regc_info.transport) && |
| 3915 |
((regc_info.transport->flag & PJSIP_TRANSPORT_DATAGRAM) == 0)) |
| 3916 |
{ |
| 3917 |
transport = regc_info.transport; |
| 3918 |
shutdown_transport = acc->cfg.ip_change_cfg.shutdown_tp; |
| 3919 |
shut_acc_ids[shut_acc_cnt++] = acc->index; |
| 3920 |
} |
| 3921 |
} else if (acc->cfg.reg_uri.slen && |
| 3922 |
acc->reg_last_code != PJSIP_SC_OK && |
| 3923 |
acc->reg_last_code != PJSIP_SC_REQUEST_TIMEOUT && |
| 3924 |
acc->reg_last_code != PJSIP_SC_INTERNAL_SERVER_ERROR && |
| 3925 |
acc->reg_last_code != PJSIP_SC_BAD_GATEWAY && |
| 3926 |
acc->reg_last_code != PJSIP_SC_SERVICE_UNAVAILABLE && |
| 3927 |
acc->reg_last_code != PJSIP_SC_SERVER_TIMEOUT && |
| 3928 |
acc->reg_last_code != PJSIP_SC_TEMPORARILY_UNAVAILABLE) |
| 3929 |
{ |
| 3930 |
PJ_LOG(3, (THIS_FILE, "Permanent registration failure, " |
| 3931 |
"IP change handling will stop for acc %d", acc->index)); |
| 3932 |
|
| 3933 |
pjsua_acc_end_ip_change(acc); |
| 3934 |
continue; |
| 3935 |
} |
| 3936 |
pj_ansi_snprintf(acc_id, sizeof(acc_id), "#%d", i); |
| 3937 |
|
| 3938 |
if (transport) { |
| 3939 |
int j = i + 1; |
| 3940 |
|
| 3941 |
|
| 3942 |
for (; j < (int)PJ_ARRAY_SIZE(pjsua_var.acc); ++j) { |
| 3943 |
pjsip_regc_info tmp_regc_info; |
| 3944 |
pjsua_acc *next_acc = &pjsua_var.acc[j]; |
| 3945 |
|
| 3946 |
if (!next_acc->valid || !next_acc->regc || |
| 3947 |
(next_acc->ip_change_op > PJSUA_IP_CHANGE_OP_NULL)) |
| 3948 |
{ |
| 3949 |
continue; |
| 3950 |
} |
| 3951 |
|
| 3952 |
pjsip_regc_get_info(next_acc->regc, &tmp_regc_info); |
| 3953 |
if (transport == tmp_regc_info.transport) { |
| 3954 |
char tmp_buf[32]; |
| 3955 |
|
| 3956 |
pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf), " #%d", j); |
| 3957 |
if (pj_ansi_strlen(acc_id) + pj_ansi_strlen(tmp_buf) < |
| 3958 |
sizeof(acc_id)) |
| 3959 |
{ |
| 3960 |
pj_ansi_strxcat(acc_id, tmp_buf, sizeof(acc_id)); |
| 3961 |
} |
| 3962 |
|
| 3963 |
shut_acc_ids[shut_acc_cnt++] = j; |
| 3964 |
if (!shutdown_transport) { |
| 3965 |
shutdown_transport = |
| 3966 |
next_acc->cfg.ip_change_cfg.shutdown_tp; |
| 3967 |
} |
| 3968 |
} |
| 3969 |
} |
| 3970 |
} |
| 3971 |
|
| 3972 |
if (shutdown_transport) { |
| 3973 |
unsigned j; |
| 3974 |
|
| 3975 |
PJ_LOG(3, (THIS_FILE, "Shutdown transport %s used by account %s " |
| 3976 |
"triggered by IP change", transport->obj_name, acc_id)); |
| 3977 |
|
| 3978 |
for (j = 0; j < shut_acc_cnt; ++j) { |
| 3979 |
pjsua_acc *tmp_acc = &pjsua_var.acc[shut_acc_ids[j]]; |
| 3980 |
tmp_acc->ip_change_op = PJSUA_IP_CHANGE_OP_ACC_SHUTDOWN_TP; |
| 3981 |
acc_done[shut_acc_ids[j]] = PJ_TRUE; |
| 3982 |
} |
| 3983 |
|
| 3984 |
status = pjsip_transport_shutdown2(transport, PJ_TRUE); |
| 3985 |
} else { |
| 3986 |
acc_done[i] = PJ_TRUE; |
| 3987 |
if (acc->cfg.allow_contact_rewrite && acc->cfg.reg_uri.slen) { |
| 3988 |
status = pjsua_acc_update_contact_on_ip_change(acc); |
| 3989 |
} else { |
| 3990 |
status = pjsua_acc_handle_call_on_ip_change(acc); |
| 3991 |
} |
| 3992 |
} |
| 3993 |
} |
| 3994 |
PJSUA_UNLOCK(); |
| 3995 |
return status; |
| 3996 |
} |
| 3997 |
|
| 3998 |
|
| 3999 |
static pj_status_t restart_listener(pjsua_transport_id id, |
| 4000 |
unsigned restart_lis_delay) |
| 4001 |
{ |
| 4002 |
pj_sockaddr bind_addr; |
| 4003 |
pjsua_transport_info tp_info; |
| 4004 |
pj_status_t status; |
| 4005 |
pj_bool_t tp_valid = PJ_TRUE; |
| 4006 |
pj_uint16_t saf; |
| 4007 |
|
| 4008 |
status = pjsua_transport_get_info(id, &tp_info); |
| 4009 |
if (status != PJ_SUCCESS) { |
| 4010 |
|
| 4011 |
tp_valid = PJ_FALSE; |
| 4012 |
goto on_return; |
| 4013 |
} |
| 4014 |
|
| 4015 |
saf = tp_info.local_addr.addr.sa_family; |
| 4016 |
pj_sockaddr_init(pjsip_transport_type_get_af(tp_info.type), |
| 4017 |
&bind_addr, |
| 4018 |
NULL, |
| 4019 |
(saf==PJ_AF_INET || saf==PJ_AF_INET6)? |
| 4020 |
pj_sockaddr_get_port(&tp_info.local_addr) : 0); |
| 4021 |
|
| 4022 |
switch (tp_info.type) { |
| 4023 |
case PJSIP_TRANSPORT_UDP: |
| 4024 |
case PJSIP_TRANSPORT_UDP6: |
| 4025 |
{ |
| 4026 |
unsigned num_locks = 0; |
| 4027 |
|
| 4028 |
|
| 4029 |
num_locks = PJSUA_RELEASE_LOCK(); |
| 4030 |
|
| 4031 |
status = pjsip_udp_transport_restart2( |
| 4032 |
pjsua_var.tpdata[id].data.tp, |
| 4033 |
PJSIP_UDP_TRANSPORT_DESTROY_SOCKET, |
| 4034 |
PJ_INVALID_SOCKET, |
| 4035 |
&bind_addr, |
| 4036 |
NULL); |
| 4037 |
|
| 4038 |
|
| 4039 |
for (;num_locks > 0; num_locks--) |
| 4040 |
PJSUA_LOCK(); |
| 4041 |
|
| 4042 |
} |
| 4043 |
break; |
| 4044 |
|
| 4045 |
#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0 |
| 4046 |
case PJSIP_TRANSPORT_TLS: |
| 4047 |
case PJSIP_TRANSPORT_TLS6: |
| 4048 |
status = pjsip_tls_transport_restart( |
| 4049 |
pjsua_var.tpdata[id].data.factory, |
| 4050 |
&bind_addr, |
| 4051 |
NULL); |
| 4052 |
break; |
| 4053 |
#endif |
| 4054 |
case PJSIP_TRANSPORT_TCP: |
| 4055 |
case PJSIP_TRANSPORT_TCP6: |
| 4056 |
status = pjsip_tcp_transport_restart( |
| 4057 |
pjsua_var.tpdata[id].data.factory, |
| 4058 |
&bind_addr, |
| 4059 |
NULL); |
| 4060 |
break; |
| 4061 |
|
| 4062 |
default: |
| 4063 |
status = PJ_EINVAL; |
| 4064 |
} |
| 4065 |
|
| 4066 |
PJ_PERROR(3,(THIS_FILE, status, "Listener %.*s restart", |
| 4067 |
(int)tp_info.info.slen, tp_info.info.ptr)); |
| 4068 |
|
| 4069 |
on_return: |
| 4070 |
|
| 4071 |
if (tp_valid && status != PJ_SUCCESS && (restart_lis_delay > 0)) { |
| 4072 |
|
| 4073 |
pjsua_schedule_timer2(&restart_listener_cb, |
| 4074 |
(void*)(pj_size_t)id, |
| 4075 |
restart_lis_delay); |
| 4076 |
|
| 4077 |
PJ_LOG(3,(THIS_FILE, "Retry listener %.*s restart in %d ms", |
| 4078 |
(int)tp_info.info.slen, tp_info.info.ptr, restart_lis_delay)); |
| 4079 |
|
| 4080 |
status = PJ_SUCCESS; |
| 4081 |
} else { |
| 4082 |
int i = 0; |
| 4083 |
pj_bool_t all_done = PJ_TRUE; |
| 4084 |
|
| 4085 |
pjsua_var.tpdata[id].is_restarting = PJ_FALSE; |
| 4086 |
pjsua_var.tpdata[id].restart_status = status; |
| 4087 |
if (pjsua_var.ua_cfg.cb.on_ip_change_progress) { |
| 4088 |
pjsua_ip_change_op_info info; |
| 4089 |
|
| 4090 |
pj_bzero(&info, sizeof(info)); |
| 4091 |
info.lis_restart.transport_id = id; |
| 4092 |
pjsua_var.ua_cfg.cb.on_ip_change_progress( |
| 4093 |
PJSUA_IP_CHANGE_OP_RESTART_LIS, |
| 4094 |
status, |
| 4095 |
&info); |
| 4096 |
} |
| 4097 |
|
| 4098 |
|
| 4099 |
for (; i < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i) { |
| 4100 |
if (pjsua_var.tpdata[i].data.ptr != NULL && |
| 4101 |
pjsua_var.tpdata[i].is_restarting) |
| 4102 |
{ |
| 4103 |
all_done = PJ_FALSE; |
| 4104 |
break; |
| 4105 |
} |
| 4106 |
} |
| 4107 |
if (all_done) |
| 4108 |
status = handle_ip_change_on_acc(); |
| 4109 |
} |
| 4110 |
return status; |
| 4111 |
} |
| 4112 |
|
| 4113 |
|
| 4114 |
static void restart_listener_cb(void *user_data) |
| 4115 |
{ |
| 4116 |
pjsua_transport_id transport_id = (pjsua_transport_id)(pj_size_t)user_data; |
| 4117 |
restart_listener(transport_id, 0); |
| 4118 |
} |
| 4119 |
|
| 4120 |
|
| 4121 |
static void ip_change_put_back_inv_config(void *user_data) |
| 4122 |
{ |
| 4123 |
PJ_UNUSED_ARG(user_data); |
| 4124 |
|
| 4125 |
PJ_LOG(4,(THIS_FILE,"IP change stops ignoring request timeout")); |
| 4126 |
pjsip_cfg()->endpt.keep_inv_after_tsx_timeout = PJ_FALSE; |
| 4127 |
} |
| 4128 |
|
| 4129 |
|
| 4130 |
PJ_DEF(pj_status_t) pjsua_handle_ip_change(const pjsua_ip_change_param *param) |
| 4131 |
{ |
| 4132 |
pj_status_t status = PJ_SUCCESS; |
| 4133 |
int i = 0; |
| 4134 |
|
| 4135 |
PJ_ASSERT_RETURN(param, PJ_EINVAL); |
| 4136 |
|
| 4137 |
for (; i < (int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 4138 |
if (pjsua_var.acc[i].valid && |
| 4139 |
pjsua_var.acc[i].ip_change_op != PJSUA_IP_CHANGE_OP_NULL && |
| 4140 |
pjsua_var.acc[i].ip_change_op != PJSUA_IP_CHANGE_OP_COMPLETED) |
| 4141 |
{ |
| 4142 |
PJ_LOG(2, (THIS_FILE, |
| 4143 |
"Previous IP address change handling still in progress")); |
| 4144 |
} |
| 4145 |
} |
| 4146 |
|
| 4147 |
PJ_LOG(3, (THIS_FILE, "Start handling IP address change")); |
| 4148 |
|
| 4149 |
|
| 4150 |
|
| 4151 |
|
| 4152 |
|
| 4153 |
if (!pjsip_cfg()->endpt.keep_inv_after_tsx_timeout) { |
| 4154 |
pjsip_cfg()->endpt.keep_inv_after_tsx_timeout = PJ_TRUE; |
| 4155 |
|
| 4156 |
|
| 4157 |
pjsua_schedule_timer2(&ip_change_put_back_inv_config, NULL, |
| 4158 |
pjsip_cfg()->tsx.td); |
| 4159 |
|
| 4160 |
PJ_LOG(4,(THIS_FILE,"IP change temporarily ignores request timeout")); |
| 4161 |
} |
| 4162 |
|
| 4163 |
|
| 4164 |
if (param->shutdown_transport) { |
| 4165 |
pjsip_tpmgr_shutdown_param sd_param; |
| 4166 |
|
| 4167 |
pjsip_tpmgr_shutdown_param_default(&sd_param); |
| 4168 |
sd_param.include_udp = PJ_FALSE; |
| 4169 |
|
| 4170 |
PJ_LOG(4,(THIS_FILE, "IP change shutting down transports..")); |
| 4171 |
PJSUA_LOCK(); |
| 4172 |
status = pjsip_tpmgr_shutdown_all( |
| 4173 |
pjsip_endpt_get_tpmgr(pjsua_var.endpt), |
| 4174 |
&sd_param); |
| 4175 |
PJSUA_UNLOCK(); |
| 4176 |
|
| 4177 |
|
| 4178 |
|
| 4179 |
|
| 4180 |
if (pjsua_var.ua_cfg.cb.on_ip_change_progress) { |
| 4181 |
pjsua_ip_change_op_info info; |
| 4182 |
|
| 4183 |
pj_bzero(&info, sizeof(info)); |
| 4184 |
pjsua_var.ua_cfg.cb.on_ip_change_progress( |
| 4185 |
PJSUA_IP_CHANGE_OP_SHUTDOWN_TP, |
| 4186 |
status, &info); |
| 4187 |
} |
| 4188 |
} |
| 4189 |
|
| 4190 |
if (param->restart_listener) { |
| 4191 |
PJSUA_LOCK(); |
| 4192 |
|
| 4193 |
|
| 4194 |
|
| 4195 |
for (i = 0; i < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i) { |
| 4196 |
if (pjsua_var.tpdata[i].data.ptr != NULL) { |
| 4197 |
pjsua_var.tpdata[i].is_restarting = PJ_TRUE; |
| 4198 |
pjsua_var.tpdata[i].restart_status = PJ_EUNKNOWN; |
| 4199 |
} |
| 4200 |
} |
| 4201 |
for (i = 0; i < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i) { |
| 4202 |
if (pjsua_var.tpdata[i].data.ptr != NULL) { |
| 4203 |
status = restart_listener(i, param->restart_lis_delay); |
| 4204 |
} |
| 4205 |
} |
| 4206 |
PJSUA_UNLOCK(); |
| 4207 |
} else { |
| 4208 |
for (i = 0; i < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i) { |
| 4209 |
if (pjsua_var.tpdata[i].data.ptr != NULL) { |
| 4210 |
pjsua_var.tpdata[i].restart_status = PJ_SUCCESS; |
| 4211 |
} |
| 4212 |
} |
| 4213 |
status = handle_ip_change_on_acc(); |
| 4214 |
} |
| 4215 |
|
| 4216 |
return status; |
| 4217 |
} |