diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c index a07c056..4fdd04a 100644 --- a/res/res_rtp_asterisk.c +++ b/res/res_rtp_asterisk.c @@ -2101,6 +2101,8 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s SSL_set_accept_state(dtls->ssl); } + ast_mutex_lock(&dtls->lock); + dtls_srtp_check_pending(instance, rtp, rtcp); BIO_write(dtls->read_bio, buf, len); @@ -2111,6 +2113,7 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s unsigned long error = ERR_get_error(); ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p' due to reason '%s', terminating\n", instance, ERR_reason_error_string(error)); + ast_mutex_unlock(&dtls->lock); return -1; } @@ -2125,11 +2128,11 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s } } else { /* Since we've sent additional traffic start the timeout timer for retransmission */ - ast_mutex_lock(&dtls->lock); dtls_srtp_start_timeout_timer(instance, rtp, rtcp); - ast_mutex_unlock(&dtls->lock); } + ast_mutex_unlock(&dtls->lock); + return res; } #endif @@ -4678,6 +4681,9 @@ static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp) static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr) { struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); +#ifdef HAVE_OPENSSL_SRTP + struct dtls_details *dtls; +#endif if (rtp->rtcp) { ast_debug(1, "Setting RTCP address on RTP instance '%p'\n", instance); @@ -4695,6 +4701,26 @@ static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct rtp_learning_seq_init(&rtp->rtp_source_learn, rtp->seqno); } +#ifdef HAVE_OPENSSL_SRTP + /* Trigger pending outbound DTLS packets received before the address was set. Avoid unnecessary locking + * by checking if we're passive. Without this, we only send the pending packets once a new SSL packet is + * received in __rtp_recvfrom. + */ + dtls = &rtp->dtls; + if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) { + ast_mutex_lock(&dtls->lock); + dtls_srtp_check_pending(instance, rtp, 0); + ast_mutex_unlock(&dtls->lock); + } + + dtls = &rtp->rtcp->dtls; + if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) { + ast_mutex_lock(&dtls->lock); + dtls_srtp_check_pending(instance, rtp, 1); + ast_mutex_unlock(&dtls->lock); + } +#endif + return; }