Prepare the SSL modules for using timer callbacks
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
index 227476a..b14c13e 100644
--- a/library/ssl_tls.c
+++ b/library/ssl_tls.c
@@ -81,12 +81,14 @@
/*
* Start a timer.
* Passing millisecs = 0 cancels a running timer.
- * The timer is already running iff time_limit != 0.
*/
static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
{
- ssl->time_limit = millisecs;
- mbedtls_timing_get_timer( &ssl->time_info, 1 );
+ if( ssl->f_set_timer == NULL )
+ return;
+
+ MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
+ ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
}
/*
@@ -94,11 +96,11 @@
*/
static int ssl_check_timer( mbedtls_ssl_context *ssl )
{
- if( ssl->time_limit != 0 &&
- mbedtls_timing_get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
- {
+ if( ssl->f_get_timer == NULL )
+ return( -2 );
+
+ if( ssl->f_get_timer( ssl->p_timer ) == 2 )
return( -1 );
- }
return( 0 );
}
@@ -2210,6 +2212,14 @@
{
uint32_t timeout;
+ /* Just to be sure */
+ if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
+ {
+ MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
+ "mbedtls_ssl_set_timer_cb() for DTLS" ) );
+ return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
+ }
+
/*
* The point is, we need to always read a full datagram at once, so we
* sometimes read more then requested, and handle the additional data.
@@ -2265,8 +2275,6 @@
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
}
- MBEDTLS_SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
-
/*
* Don't even try to read if time's out already.
* This avoids by-passing the timer when repeatedly receiving messages
@@ -4967,6 +4975,10 @@
void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
{
memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
+
+ /* Temporary WIP */
+ mbedtls_ssl_set_timer_cb( ssl, &ssl->WIP_timer,
+ mbedtls_timing_set_delay, mbedtls_timing_get_delay );
}
/*
@@ -5260,6 +5272,16 @@
conf->read_timeout = timeout;
}
+void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
+ void *p_timer,
+ void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms),
+ int (*f_get_timer)(void *) )
+{
+ ssl->p_timer = p_timer;
+ ssl->f_set_timer = f_set_timer;
+ ssl->f_get_timer = f_get_timer;
+}
+
#if defined(MBEDTLS_SSL_SRV_C)
void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
void *p_cache,
@@ -6040,7 +6062,7 @@
{
#if defined(MBEDTLS_SSL_PROTO_DTLS)
/* Start timer if not already running */
- if( ssl->time_limit == 0 )
+ if( ssl->f_get_timer( ssl->p_timer ) == -1 )
ssl_set_timer( ssl, ssl->conf->read_timeout );
#endif