From 2466853075b9edb7b3116696c635314c80a40016 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 7 Aug 2018 13:57:06 +0100 Subject: [PATCH] openssl.c: Global dtls_event variable causing confusion when events happen dtls_event is a static variable, declared outside of all the functions which tracks (D)TLS errors for generating events etc. If there are multiple SSL sessions being set up to the same server (as per Happy Eyeballs for the DOTS protocol) it is possible to asynchronously get dtls_event updated and the wrong SSL session then acts on it. Make dtls_event a part of coap_session_t to maintain seperation. Preset dtls_event to -1 in coap_make_session(). Update coap_event_handler() to have the 3rd parameter as coap_session_t *session instead of void *arg as the parameter is always session. Updated coap_event_handler_t to have session as the 3rd parameter. Add in new registration handler coap_register_event_handler() to be consistent with all the other coap_register_XXX_handler()s. Consequently, coap_set_event_handler() and coap_clear_enent_handler() are deprecated. Update coap_handler(3) to include new coap_register_event_handler(). (PR #183 event definitions are included presumptive on #183 getting merged) --- include/coap/coap_event.h | 27 ++++++++++++++-- include/coap/coap_session.h | 3 +- include/coap/net.h | 4 +-- libcoap-2.map | 1 + libcoap-2.sym | 1 + man/coap_handler.txt.in | 59 ++++++++++++++++++++++++++++------ src/coap_event.c | 13 +++++++- src/coap_openssl.c | 64 ++++++++++++++++++++----------------- src/coap_session.c | 1 + src/net.c | 4 +-- 10 files changed, 130 insertions(+), 47 deletions(-) diff --git a/include/coap/coap_event.h b/include/coap/coap_event.h index a72a4360..fb85102e 100644 --- a/include/coap/coap_event.h +++ b/include/coap/coap_event.h @@ -10,11 +10,14 @@ #ifndef _COAP_EVENT_H_ #define _COAP_EVENT_H_ +#include "libcoap.h" + struct coap_context_t; +struct coap_session_t; /** * @defgroup events Event API - * API functions for event deliver from lower-layer library functions. + * API functions for event delivery from lower-layer library functions. * @{ */ @@ -38,8 +41,8 @@ struct coap_context_t; * passed as the third argument. */ typedef int (*coap_event_handler_t)(struct coap_context_t *, - coap_event_t, - void *); + coap_event_t event, + struct coap_session_t *session); /** * Registers the function @p hnd as callback for events from the given @@ -47,16 +50,34 @@ typedef int (*coap_event_handler_t)(struct coap_context_t *, * registered with @p context will be overwritten by this operation. * * @param context The CoAP context to register the event handler with. + * @param hnd The event handler to be registered. @c NULL if to be + * de-registered. + */ +void coap_register_event_handler(struct coap_context_t *context, + coap_event_handler_t hnd); + +/** + * Registers the function @p hnd as callback for events from the given + * CoAP context @p context. Any event handler that has previously been + * registered with @p context will be overwritten by this operation. + * + * @deprecated Use coap_register_event_handler() instead. + * + * @param context The CoAP context to register the event handler with. * @param hnd The event handler to be registered. */ +COAP_DEPRECATED void coap_set_event_handler(struct coap_context_t *context, coap_event_handler_t hnd); /** * Clears the event handler registered with @p context. * + * @deprecated Use coap_register_event_handler() instead with NULL for hnd. + * * @param context The CoAP context whose event handler is to be removed. */ +COAP_DEPRECATED void coap_clear_event_handler(struct coap_context_t *context); /** @} */ diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 00fc50f5..3cf05e9f 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -84,7 +84,8 @@ typedef struct coap_session_t { unsigned int max_retransmit; /**< maximum re-transmit count (default 4) */ coap_fixed_point_t ack_timeout; /**< timeout waiting for ack (default 2 secs) */ coap_fixed_point_t ack_random_factor; /**< ack random factor backoff (default 1.5) */ - unsigned int dtls_timeout_count; /**handle_event = hnd; +} + +void +coap_set_event_handler(struct coap_context_t *context, + coap_event_handler_t hnd) { context->handle_event = hnd; } diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 062e61f2..af90333d 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -294,8 +294,6 @@ static unsigned coap_dtls_psk_server_callback(SSL *ssl, const char *identity, un return (unsigned)session->context->get_server_psk(session, (const uint8_t*)identity, identity_len, (uint8_t*)buf, max_len); } -static int dtls_event = 0; - static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) { coap_session_t *session = (coap_session_t*)SSL_get_app_data(ssl); const char *pstr; @@ -314,9 +312,13 @@ static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) { } else if (where & SSL_CB_ALERT) { pstr = (where & SSL_CB_READ) ? "read" : "write"; if (dtls_log_level >= LOG_INFO) - coap_log(LOG_INFO, "* %s: SSL3 alert %s:%s:%s\n", coap_session_str(session), pstr, SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret)); + coap_log(LOG_INFO, "* %s: SSL3 alert %s:%s:%s\n", + coap_session_str(session), + pstr, + SSL_alert_type_string_long(ret), + SSL_alert_desc_string_long(ret)); if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) - dtls_event = COAP_EVENT_DTLS_ERROR; + session->dtls_event = COAP_EVENT_DTLS_ERROR; } else if (where & SSL_CB_EXIT) { if (ret == 0) { if (dtls_log_level >= LOG_WARNING) { @@ -339,7 +341,7 @@ static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) { } if (where == SSL_CB_HANDSHAKE_START && SSL_get_state(ssl) == TLS_ST_OK) - dtls_event = COAP_EVENT_DTLS_RENEGOTIATE; + session->dtls_event = COAP_EVENT_DTLS_RENEGOTIATE; } static int coap_sock_create(BIO *a) { @@ -1489,7 +1491,7 @@ int coap_dtls_send(coap_session_t *session, assert(ssl != NULL); - dtls_event = -1; + session->dtls_event = -1; r = SSL_write(ssl, data, (int)data_len); if (r <= 0) { @@ -1499,16 +1501,17 @@ int coap_dtls_send(coap_session_t *session, } else { coap_log(LOG_WARNING, "coap_dtls_send: cannot send PDU\n"); if (err == SSL_ERROR_ZERO_RETURN) - dtls_event = COAP_EVENT_DTLS_CLOSED; + session->dtls_event = COAP_EVENT_DTLS_CLOSED; else if (err == SSL_ERROR_SSL) - dtls_event = COAP_EVENT_DTLS_ERROR; + session->dtls_event = COAP_EVENT_DTLS_ERROR; r = -1; } } - if (dtls_event >= 0) { - coap_handle_event(session->context, dtls_event, session); - if (dtls_event == COAP_EVENT_DTLS_ERROR || dtls_event == COAP_EVENT_DTLS_CLOSED) { + if (session->dtls_event >= 0) { + coap_handle_event(session->context, session->dtls_event, session); + if (session->dtls_event == COAP_EVENT_DTLS_ERROR || + session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); r = -1; } @@ -1587,7 +1590,7 @@ int coap_dtls_receive(coap_session_t *session, ssl_data->pdu = data; ssl_data->pdu_len = (unsigned)data_len; - dtls_event = -1; + session->dtls_event = -1; r = SSL_read(ssl, pdu, (int)sizeof(pdu)); if (r > 0) { return coap_handle_dgram(session->context, session, pdu, (size_t)r); @@ -1601,14 +1604,15 @@ int coap_dtls_receive(coap_session_t *session, r = 0; } else { if (err == SSL_ERROR_ZERO_RETURN) /* Got a close notify alert from the remote side */ - dtls_event = COAP_EVENT_DTLS_CLOSED; + session->dtls_event = COAP_EVENT_DTLS_CLOSED; else if (err == SSL_ERROR_SSL) - dtls_event = COAP_EVENT_DTLS_ERROR; + session->dtls_event = COAP_EVENT_DTLS_ERROR; r = -1; } - if (dtls_event >= 0) { - coap_handle_event(session->context, dtls_event, session); - if (dtls_event == COAP_EVENT_DTLS_ERROR || dtls_event == COAP_EVENT_DTLS_CLOSED) { + if (session->dtls_event >= 0) { + coap_handle_event(session->context, session->dtls_event, session); + if (session->dtls_event == COAP_EVENT_DTLS_ERROR || + session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); r = -1; } @@ -1791,7 +1795,7 @@ ssize_t coap_tls_write(coap_session_t *session, return -1; in_init = !SSL_is_init_finished(ssl); - dtls_event = -1; + session->dtls_event = -1; r = SSL_write(ssl, data, (int)data_len); if (r <= 0) { @@ -1809,9 +1813,9 @@ ssize_t coap_tls_write(coap_session_t *session, } else { coap_log(LOG_WARNING, "*** %s: coap_tls_write: cannot send PDU\n", coap_session_str(session)); if (err == SSL_ERROR_ZERO_RETURN) - dtls_event = COAP_EVENT_DTLS_CLOSED; + session->dtls_event = COAP_EVENT_DTLS_CLOSED; else if (err == SSL_ERROR_SSL) - dtls_event = COAP_EVENT_DTLS_ERROR; + session->dtls_event = COAP_EVENT_DTLS_ERROR; r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { @@ -1819,9 +1823,10 @@ ssize_t coap_tls_write(coap_session_t *session, coap_session_send_csm(session); } - if (dtls_event >= 0) { - coap_handle_event(session->context, dtls_event, session); - if (dtls_event == COAP_EVENT_DTLS_ERROR || dtls_event == COAP_EVENT_DTLS_CLOSED) { + if (session->dtls_event >= 0) { + coap_handle_event(session->context, session->dtls_event, session); + if (session->dtls_event == COAP_EVENT_DTLS_ERROR || + session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); r = -1; } @@ -1841,7 +1846,7 @@ ssize_t coap_tls_read(coap_session_t *session, return -1; in_init = !SSL_is_init_finished(ssl); - dtls_event = -1; + session->dtls_event = -1; r = SSL_read(ssl, data, (int)data_len); if (r <= 0) { int err = SSL_get_error(ssl, r); @@ -1857,9 +1862,9 @@ ssize_t coap_tls_read(coap_session_t *session, r = 0; } else { if (err == SSL_ERROR_ZERO_RETURN) /* Got a close notify alert from the remote side */ - dtls_event = COAP_EVENT_DTLS_CLOSED; + session->dtls_event = COAP_EVENT_DTLS_CLOSED; else if (err == SSL_ERROR_SSL) - dtls_event = COAP_EVENT_DTLS_ERROR; + session->dtls_event = COAP_EVENT_DTLS_ERROR; r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { @@ -1867,9 +1872,10 @@ ssize_t coap_tls_read(coap_session_t *session, coap_session_send_csm(session); } - if (dtls_event >= 0) { - coap_handle_event(session->context, dtls_event, session); - if (dtls_event == COAP_EVENT_DTLS_ERROR || dtls_event == COAP_EVENT_DTLS_CLOSED) { + if (session->dtls_event >= 0) { + coap_handle_event(session->context, session->dtls_event, session); + if (session->dtls_event == COAP_EVENT_DTLS_ERROR || + session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); r = -1; } diff --git a/src/coap_session.c b/src/coap_session.c index 4600fdd8..251a253b 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -135,6 +135,7 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, session->max_retransmit = COAP_DEFAULT_MAX_RETRANSMIT; session->ack_timeout = COAP_DEFAULT_ACK_TIMEOUT; session->ack_random_factor = COAP_DEFAULT_ACK_RANDOM_FACTOR; + session->dtls_event = -1; /* initialize message id */ prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid)); diff --git a/src/net.c b/src/net.c index fbb3107c..2a4b6327 100644 --- a/src/net.c +++ b/src/net.c @@ -2212,11 +2212,11 @@ cleanup: } int -coap_handle_event(coap_context_t *context, coap_event_t event, void *data) { +coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session) { coap_log(LOG_DEBUG, "*** EVENT: 0x%04x\n", event); if (context->handle_event) { - return context->handle_event(context, event, data); + return context->handle_event(context, event, session); } else { return 0; }