From 2633479bd770dd4195053cc5e2a74cb7bbe8089e Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Tue, 29 Mar 2016 15:34:34 +0200 Subject: [PATCH] net.c: moved DTLS-specific code to coap_send_impl() coap_send_impl() now decides if outbound data is passed to coap_dtls_send() or to the registered network_send callback function. For coap_send_confirmed() this implies that retransmitted PDUs are passed to coap_dtls_send() as well, causing retransmissions on the DTLS layer when the session is established. As long as the DTLS handshake is not complete, coap_dtls_send() caches the outbound application data for this particular PDU hence avoiding duplicate messages. --- src/net.c | 60 +++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/net.c b/src/net.c index 421fbd30..20a874cf 100644 --- a/src/net.c +++ b/src/net.c @@ -569,6 +569,11 @@ coap_send_ack(coap_context_t *context, return result; } +static int +is_dtls(const coap_endpoint_t *endpoint) { + return (endpoint->flags & COAP_ENDPOINT_DTLS) != 0; +} + #if defined(WITH_POSIX) || defined(WITH_CONTIKI) static coap_tid_t coap_send_impl(coap_context_t *context, @@ -588,8 +593,29 @@ coap_send_impl(coap_context_t *context, return COAP_DROPPED_RESPONSE; } - bytes_written = context->network_send(context, local_interface, dst, - (unsigned char *)pdu->hdr, pdu->length); + /* Pass to DTLS module for sending if local_interface is for secure + * communication. */ + if (is_dtls(local_interface)) { + struct coap_dtls_session_t *session; + + session = coap_dtls_get_session(context, local_interface, dst); + if (session == NULL) { + coap_log(LOG_WARNING, "coap_send: no DTLS session available\n"); + goto finish; + } else { + bytes_written = coap_dtls_send(context, session, pdu); + /* If coap_dtls_send() has returned a negative value we need to + * output a custom error message. Otherwise, we end this branch + * to create a valid transaction id. */ + if (bytes_written < 0) { + coap_log(LOG_WARNING, "coap_send: error sending data over DTLS\n"); + goto finish; + } + } + } else { + bytes_written = context->network_send(context, local_interface, dst, + (unsigned char *)pdu->hdr, pdu->length); + } if (bytes_written >= 0) { coap_transaction_id(dst, pdu, &id); @@ -597,6 +623,7 @@ coap_send_impl(coap_context_t *context, coap_log(LOG_CRIT, "coap_send_impl: %s\n", strerror(errno)); } + finish: return id; } #endif /* WITH_POSIX */ @@ -632,41 +659,12 @@ coap_send_impl(coap_context_t *context, } #endif /* WITH_LWIP */ -static int -is_dtls(const coap_endpoint_t *endpoint) { - return (endpoint->flags & COAP_ENDPOINT_DTLS) != 0; -} - coap_tid_t coap_send(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu) { - /* FIXME: the following DTLS-specific case should probably go into - * coap_send_impl() with proper treatment of CON messages. */ - if (is_dtls(local_interface)) { - struct coap_dtls_session_t *session; - coap_tid_t id = COAP_INVALID_TID; - - session = coap_dtls_get_session(context, local_interface, dst); - if (session == NULL) { - coap_log(LOG_WARNING, "coap_send: no DTLS session available\n"); - return COAP_INVALID_TID; - } else { - int res = coap_dtls_send(context, session, pdu); - if (res < 0) { - coap_log(LOG_WARNING, "coap_send: error sending data over DTLS\n"); - goto finish; - } else { - coap_transaction_id(dst, pdu, &id); - /* goto finish */ - } - } - finish: - return id; - } - return coap_send_impl(context, local_interface, dst, pdu); }