From 58766a137e2bcdfd3cfb80026207186aa6929c29 Mon Sep 17 00:00:00 2001 From: jcmichelou Date: Mon, 18 Sep 2017 16:47:06 +0200 Subject: [PATCH] coap_dtls.h, openssl: Add function prototypes for TLS support. --- include/coap/coap_dtls.h | 33 +++++++++++++++++++++++---------- src/coap_notls.c | 16 +++++++++++----- src/coap_openssl.c | 16 +++++++++++----- src/net.c | 9 ++++----- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index db658141..232ef50e 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -1,7 +1,8 @@ /* - * coap_dtls.h -- Datagram Transport Layer Support for libcoap + * coap_dtls.h -- (Datagram) Transport Layer Support for libcoap * * Copyright (C) 2016 Olaf Bergmann + * Copyright (C) 2017 Jean-Claude Michelou * * This file is part of the CoAP library libcoap. Please see README for terms * of use. @@ -155,20 +156,32 @@ int coap_dtls_hello(coap_session_t *session, unsigned int coap_dtls_get_overhead(coap_session_t *session); /** - * Send data to a TLS peer. + * Send data to a TLS peer, with implicit flush. * * @param session The CoAP session * @param data pointer to data * @param size number of bytes to send - * @param flush set to non-zero if the message should be sent now - * @return 0 if this would be blocking, -1 if there is an error or the number of cleartext bytes sent + * @return 0 if this should be retried, -1 if there is an error or + * the number of cleartext bytes sent. */ -int coap_tls_send(coap_session_t *session, - const uint8_t *data, - size_t data_len, - int flush); - - +ssize_t coap_tls_write(coap_session_t *session, + const uint8_t *data, + size_t data_len + ); + +/** + * Read some data from a TLS peer. + * + * @param session The CoAP session + * @param data pointer to data + * @param size maximum number of bytes to read + * @return 0 if this should be retried, -1 if there is an error or + * the number of cleartext bytes read. + */ +ssize_t coap_tls_read(coap_session_t *session, + uint8_t *data, + size_t data_len + ); /** @} */ diff --git a/src/coap_notls.c b/src/coap_notls.c index cf32d05f..73862d96 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -110,11 +110,17 @@ unsigned int coap_dtls_get_overhead(coap_session_t *session UNUSED) { return 0; } -int coap_tls_send(coap_session_t *session, - const uint8_t *data, - size_t data_len, - int flush ) -{ +ssize_t coap_tls_write(coap_session_t *session UNUSED, + const uint8_t *data UNUSED, + size_t data_len UNUSED +) { + return -1; +} + +ssize_t coap_tls_read(coap_session_t *session UNUSED, + uint8_t *data UNUSED, + size_t data_len UNUSED +) { return -1; } diff --git a/src/coap_openssl.c b/src/coap_openssl.c index fbd4fc12..558fc137 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -675,11 +675,17 @@ unsigned int coap_dtls_get_overhead(coap_session_t *session) { return overhead; } -int coap_tls_send(coap_session_t *session, - const uint8_t *data, - size_t data_len, - int flush ) -{ +ssize_t coap_tls_write(coap_session_t *session, + const uint8_t *data, + size_t data_len +) { + return -1; +} + +ssize_t coap_tls_read(coap_session_t *session, + uint8_t *data, + size_t data_len +) { return -1; } diff --git a/src/net.c b/src/net.c index 12b72d51..9d8b1930 100644 --- a/src/net.c +++ b/src/net.c @@ -647,8 +647,8 @@ coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) { pdu->token - pdu->hdr_size, pdu->used_size + pdu->hdr_size); break; case COAP_PROTO_TLS: - bytes_written = coap_tls_send(session, pdu->token - pdu->hdr_size, - pdu->used_size + pdu->hdr_size, 1); + bytes_written = coap_tls_write(session, pdu->token - pdu->hdr_size, + pdu->used_size + pdu->hdr_size); break; default: break; @@ -1025,11 +1025,10 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now ); break; case COAP_PROTO_TLS: - bytes_written = coap_tls_send( + bytes_written = coap_tls_write( session, q->pdu->token - q->pdu->hdr_size - session->partial_write, - q->pdu->used_size + q->pdu->hdr_size - session->partial_write, - 0 + q->pdu->used_size + q->pdu->hdr_size - session->partial_write ); break; }