From 52cfd56c659a2276fbb8e49328334b56e838c3ea Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 1 Mar 2023 14:42:15 +0000 Subject: [PATCH] netif: Add in stream read write --- include/coap3/coap_netif_internal.h | 30 +++++++ src/coap_gnutls.c | 75 +++++++---------- src/coap_io.c | 11 +++ src/coap_mbedtls.c | 120 +++++++++++----------------- src/coap_netif.c | 51 ++++++++++++ src/coap_notls.c | 24 ++++-- src/coap_openssl.c | 60 +++++++++----- src/coap_session.c | 13 --- src/coap_tinydtls.c | 26 ++++-- src/net.c | 15 ++-- 10 files changed, 251 insertions(+), 174 deletions(-) diff --git a/include/coap3/coap_netif_internal.h b/include/coap3/coap_netif_internal.h index 08419f11..6a5f7674 100644 --- a/include/coap3/coap_netif_internal.h +++ b/include/coap3/coap_netif_internal.h @@ -77,6 +77,36 @@ ssize_t coap_netif_dgrm_read(coap_session_t *session, coap_packet_t *packet); ssize_t coap_netif_dgrm_write(coap_session_t *session, const uint8_t *data, size_t datalen); +/** + * Function interface for layer data stream receiving. This function returns + * the number of bytes that have been read, or -1 on error. + * + * @param session Session to receive data on. + * @param data The data to receive. + * @param datalen The maximum length of @p data. + * + * @return >=0 Number of bytes read. + * -1 Error of some sort (see errno). + */ +ssize_t coap_netif_strm_read(coap_session_t *session, uint8_t *data, + size_t datalen); + +/** + * Function interface for netif stream data transmission. This function returns + * the number of bytes that have been transmitted, or a value less than zero + * on error. The number of bytes written may be less than datalen because of + * congestion control. + * + * @param session Session to send data on. + * @param data The data to send. + * @param datalen The actual length of @p data. + * + * @return The number of bytes written on success, or a value + * less than zero on error. + */ +ssize_t coap_netif_strm_write(coap_session_t *session, + const uint8_t *data, size_t datalen); + /** @} */ #endif /* COAP_NETIF_INTERNAL_H */ diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index adce4551..3a225eca 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -2632,8 +2632,9 @@ unsigned int coap_dtls_get_overhead(coap_session_t *c_session COAP_UNUSED) { #if !COAP_DISABLE_TCP /* + * strm * return +ve data amount - * 0 no more + * 0 connection closed * -1 error (error in errno) */ static ssize_t @@ -2642,32 +2643,23 @@ coap_sock_read(gnutls_transport_ptr_t context, void *out, size_t outl) { coap_session_t *c_session = (coap_session_t *)context; if (out != NULL) { -#ifdef _WIN32 - ret = recv(c_session->sock.fd, (char *)out, (int)outl, 0); -#else - ret = recv(c_session->sock.fd, out, outl, 0); -#endif - if (ret > 0) { - coap_log_debug("* %s: received %d bytes\n", - coap_session_str(c_session), ret); - } else if (ret < 0 && errno != EAGAIN) { - coap_log_debug( "* %s: failed to receive any bytes (%s)\n", - coap_session_str(c_session), coap_socket_strerror()); + ret = (int)coap_netif_strm_read(c_session, out, outl); + /* Translate layer returns into what GnuTLS expects */ + if (ret == -1) { + if (errno == ECONNRESET) { + /* graceful shutdown */ + return 0; + } + } else if (ret == 0) { + errno = EAGAIN; + ret = -1; } - if (ret == 0) { - /* graceful shutdown */ - c_session->sock.flags &= ~COAP_SOCKET_CAN_READ; - return 0; - } else if (ret == COAP_SOCKET_ERROR) - c_session->sock.flags &= ~COAP_SOCKET_CAN_READ; - else if (ret < (ssize_t)outl) - c_session->sock.flags &= ~COAP_SOCKET_CAN_READ; - return ret; } return ret; } /* + * strm * return +ve data amount * 0 no more * -1 error (error in errno) @@ -2677,11 +2669,9 @@ coap_sock_write(gnutls_transport_ptr_t context, const void *in, size_t inl) { int ret = 0; coap_session_t *c_session = (coap_session_t *)context; - ret = (int)coap_socket_write(&c_session->sock, in, inl); - if (ret > 0) { - coap_log_debug("* %s: sent %d bytes\n", - coap_session_str(c_session), ret); - } else if (ret < 0) { + ret = (int)coap_netif_strm_write(c_session, in, inl); + /* Translate layer what returns into what GnuTLS expects */ + if (ret < 0) { if ((c_session->state == COAP_SESSION_STATE_CSM || c_session->state == COAP_SESSION_STATE_HANDSHAKE) && (errno == EPIPE || errno == ECONNRESET)) { @@ -2807,14 +2797,13 @@ void coap_tls_free_session(coap_session_t *c_session) { } /* - * return +ve data amount - * 0 no more - * -1 error (error in errno) + * strm + * return +ve Number of bytes written. + * -1 Error (error in errno). */ -ssize_t coap_tls_write(coap_session_t *c_session, - const uint8_t *data, - size_t data_len -) { +ssize_t +coap_tls_write(coap_session_t *c_session, const uint8_t *data, + size_t data_len) { int ret; coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; @@ -2833,14 +2822,12 @@ ssize_t coap_tls_write(coap_session_t *c_session, case GNUTLS_E_PULL_ERROR: case GNUTLS_E_PREMATURE_TERMINATION: c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; - ret = -1; break; case GNUTLS_E_FATAL_ALERT_RECEIVED: /* Stop the sending of an alert on closedown */ g_env->sent_alert = 1; log_last_alert(c_session, g_env->g_session); c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; - ret = -1; break; default: coap_log_warn( @@ -2882,19 +2869,19 @@ ssize_t coap_tls_write(coap_session_t *c_session, } /* - * return +ve data amount - * 0 no more - * -1 error (error in errno) + * strm + * return >=0 Number of bytes read. + * -1 Error (error in errno). */ -ssize_t coap_tls_read(coap_session_t *c_session, - uint8_t *data, - size_t data_len -) { +ssize_t +coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; int ret = -1; - if (!g_env) + if (!g_env) { + errno = ENXIO; return -1; + } c_session->dtls_event = -1; if (!g_env->established && !g_env->sent_alert) { @@ -2924,12 +2911,10 @@ ssize_t coap_tls_read(coap_session_t *c_session, g_env->sent_alert = 1; log_last_alert(c_session, g_env->g_session); c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; - ret = -1; break; case GNUTLS_E_WARNING_ALERT_RECEIVED: log_last_alert(c_session, g_env->g_session); c_session->dtls_event = COAP_EVENT_DTLS_ERROR; - ret = 0; break; default: coap_log_warn( diff --git a/src/coap_io.c b/src/coap_io.c index b73ff693..c99c4159 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -491,6 +491,12 @@ coap_win_error_to_errno(void) { } #endif /* _WIN32 */ +/* + * strm + * return +ve Number of bytes written. + * 0 No data written. + * -1 Error (error in errno). + */ ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) { ssize_t r; @@ -547,6 +553,11 @@ coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) { return r; } +/* + * strm + * return >=0 Number of bytes read. + * -1 Error (error in errno). + */ ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) { ssize_t r; diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 1913fe9e..23df80d7 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -1400,65 +1400,36 @@ mbedtls_debug_out(void *ctx COAP_UNUSED, int level, #if !COAP_DISABLE_TCP /* + * strm * return +ve data amount * 0 no more * -ve Mbed TLS error */ static int coap_sock_read(void *ctx, unsigned char *out, size_t outl) { - ssize_t ret = MBEDTLS_ERR_SSL_CONN_EOF; + int ret = MBEDTLS_ERR_SSL_CONN_EOF; coap_session_t *c_session = (coap_session_t *)ctx; if (out != NULL) { -#ifdef _WIN32 - ret = recv(c_session->sock.fd, (char *)out, (int)outl, 0); -#else - ret = recv(c_session->sock.fd, out, outl, 0); -#endif - if (ret > 0) { - coap_log_debug("* %s: received %zd bytes\n", - coap_session_str(c_session), ret); - } else if (ret < 0 && errno != EAGAIN) { - coap_log_debug( "* %s: failed to receive any bytes (%s)\n", - coap_session_str(c_session), coap_socket_strerror()); - } - - if (ret == 0) { - /* graceful shutdown */ - c_session->sock.flags &= ~COAP_SOCKET_CAN_READ; - ret = MBEDTLS_ERR_SSL_CONN_EOF; - } - else if (ret == COAP_SOCKET_ERROR) { -#ifdef _WIN32 - int lasterror = WSAGetLastError(); - - if (lasterror == WSAEWOULDBLOCK) { - ret = MBEDTLS_ERR_SSL_WANT_READ; - } - else if (lasterror == WSAECONNRESET) { - ret = MBEDTLS_ERR_NET_CONN_RESET; - } -#else - if (errno == EAGAIN || errno == EINTR) { - ret = MBEDTLS_ERR_SSL_WANT_READ; - } - else if (errno == EPIPE || errno == ECONNRESET) { - ret = MBEDTLS_ERR_NET_CONN_RESET; - } -#endif - else { + ret = (int)coap_netif_strm_read(c_session, out, outl); + /* Translate layer returns into what MbedTLS expects */ + if (ret == -1) { + if (errno == ECONNRESET) { + /* graceful shutdown */ + ret = MBEDTLS_ERR_SSL_CONN_EOF; + } else { ret = MBEDTLS_ERR_NET_RECV_FAILED; } - c_session->sock.flags &= ~COAP_SOCKET_CAN_READ; - } - else if (ret < (ssize_t)outl) { - c_session->sock.flags &= ~COAP_SOCKET_CAN_READ; + } else if (ret == 0) { + errno = EAGAIN; + ret = MBEDTLS_ERR_SSL_WANT_READ; } } - return (int)ret; + return ret; } /* + * strm * return +ve data amount * 0 no more * -ve Mbed TLS error @@ -1468,11 +1439,9 @@ coap_sock_write(void *context, const unsigned char *in, size_t inl) { int ret = 0; coap_session_t *c_session = (coap_session_t *)context; - ret = (int)coap_socket_write(&c_session->sock, in, inl); - if (ret > 0) { - coap_log_debug("* %s: sent %d bytes\n", - coap_session_str(c_session), ret); - } else if (ret < 0) { + ret = (int)coap_netif_strm_write(c_session, (const uint8_t *)in, inl); + /* Translate layer what returns into what MbedTLS expects */ + if (ret < 0) { if ((c_session->state == COAP_SESSION_STATE_CSM || c_session->state == COAP_SESSION_STATE_HANDSHAKE) && (errno == EPIPE || errno == ECONNRESET)) { @@ -2286,11 +2255,12 @@ void *coap_tls_new_client_session(coap_session_t *c_session, #endif /* COAP_CLIENT_SUPPORT */ #if COAP_SERVER_SUPPORT -void *coap_tls_new_server_session(coap_session_t *c_session COAP_UNUSED, - int *connected COAP_UNUSED) -{ +void * +coap_tls_new_server_session(coap_session_t *c_session, int *connected) { #if !defined(MBEDTLS_SSL_SRV_C) (void)c_session; + (void)connected; + coap_log_emerg("coap_tls_new_server_session:" " libcoap not compiled for Server Mode for Mbed TLS" " - update Mbed TLS to include Server Mode\n"); @@ -2301,6 +2271,10 @@ void *coap_tls_new_server_session(coap_session_t *c_session COAP_UNUSED, COAP_PROTO_TLS); int ret; + *connected = 0; + if (!m_env) + return NULL; + c_session->tls = m_env; ret = do_mbedtls_handshake(c_session, m_env); if (ret == 1) { @@ -2318,15 +2292,13 @@ void coap_tls_free_session(coap_session_t *c_session) } /* - * return +ve data amount - * 0 no more - * -1 error (error in errno) + * strm + * return +ve Number of bytes written. + * -1 Error (error in errno). */ -ssize_t coap_tls_write(coap_session_t *c_session, - const uint8_t *data, - size_t data_len - ) -{ +ssize_t +coap_tls_write(coap_session_t *c_session, const uint8_t *data, + size_t data_len) { int ret = 0; coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls; size_t amount_sent = 0; @@ -2334,24 +2306,27 @@ ssize_t coap_tls_write(coap_session_t *c_session, assert(m_env != NULL); if (!m_env) { + errno = ENXIO; return -1; } c_session->dtls_event = -1; if (m_env->established) { while (amount_sent < data_len) { - ret = mbedtls_ssl_write(&m_env->ssl, - (const unsigned char*)&data[amount_sent], + ret = mbedtls_ssl_write(&m_env->ssl, &data[amount_sent], data_len - amount_sent); if (ret <= 0) { switch (ret) { case MBEDTLS_ERR_SSL_WANT_READ: case MBEDTLS_ERR_SSL_WANT_WRITE: - ret = 0; + if (amount_sent) + ret = amount_sent; + else + ret = 0; + c_session->sock.flags |= COAP_SOCKET_WANT_WRITE; break; case MBEDTLS_ERR_NET_CONN_RESET: case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE: c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; - ret = -1; break; default: coap_log_warn( @@ -2394,21 +2369,21 @@ ssize_t coap_tls_write(coap_session_t *c_session, } /* - * return +ve data amount - * 0 no more - * -1 error (error in errno) + * strm + * return >=0 Number of bytes read. + * -1 Error (error in errno). */ -ssize_t coap_tls_read(coap_session_t *c_session, - uint8_t *data, - size_t data_len - ) +ssize_t +coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { int ret = -1; coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls; - if (!m_env) + if (!m_env) { + errno = ENXIO; return -1; + } c_session->dtls_event = -1; @@ -2434,7 +2409,6 @@ ssize_t coap_tls_read(coap_session_t *c_session, /* Stop the sending of an alert on closedown */ m_env->sent_alert = 1; c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; - ret = -1; break; case MBEDTLS_ERR_SSL_WANT_READ: errno = EAGAIN; @@ -2448,6 +2422,8 @@ ssize_t coap_tls_read(coap_session_t *c_session, ret = -1; break; } + } else if (ret < (int)data_len) { + c_session->sock.flags &= ~COAP_SOCKET_CAN_READ; } } diff --git a/src/coap_netif.c b/src/coap_netif.c index 8a91000c..3aa51cce 100644 --- a/src/coap_netif.c +++ b/src/coap_netif.c @@ -118,3 +118,54 @@ coap_netif_dgrm_read(coap_session_t *session, coap_packet_t *packet) { } return bytes_read; } + +#if !COAP_DISABLE_TCP +/* + * strm + * return >=0 Number of bytes read. + * -1 Error (error in errno). + */ +ssize_t +coap_netif_strm_read(coap_session_t *session, uint8_t *data, size_t datalen) { + ssize_t bytes_read = coap_socket_read(&session->sock, data, datalen); + int keep_errno = errno; + + if (bytes_read >= 0) { + coap_log_debug("* %s: received %zd bytes\n", + coap_session_str(session), bytes_read); + } else if (bytes_read == -1 && errno != EAGAIN) { + coap_log_debug( "* %s: failed to receive any bytes (%s)\n", + coap_session_str(session), coap_socket_strerror()); + errno = keep_errno; + } + return bytes_read; +} + +/* + * strm + * return +ve Number of bytes written. + * -1 Error (error in errno). + */ +ssize_t +coap_netif_strm_write(coap_session_t *session, const uint8_t *data, + size_t datalen) { + ssize_t bytes_written = coap_socket_write(&session->sock, data, datalen); + int keep_errno = errno; + + if (bytes_written <= 0) { + coap_log_debug( "* %s: failed to send %zd bytes (%s) state %d\n", + coap_session_str(session), datalen, + coap_socket_strerror(), session->state); + } else if (bytes_written == (ssize_t)datalen) { + coap_ticks(&session->last_rx_tx); + coap_log_debug("* %s: sent %zd bytes\n", + coap_session_str(session), datalen); + } else { + coap_ticks(&session->last_rx_tx); + coap_log_debug("* %s: sent %zd bytes of %zd\n", + coap_session_str(session), bytes_written, datalen); + errno = keep_errno; + } + return bytes_written; +} +#endif /* COAP_DISABLE_TCP */ diff --git a/src/coap_notls.c b/src/coap_notls.c index 600d8b57..eadc37c5 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -232,17 +232,27 @@ void *coap_tls_new_server_session(coap_session_t *session COAP_UNUSED, int *conn void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED) { } -ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, +/* + * strm + * return +ve Number of bytes written. + * -1 Error (error in errno). + */ +ssize_t +coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, - size_t data_len COAP_UNUSED -) { + size_t data_len COAP_UNUSED) { return -1; } -ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, - uint8_t *data COAP_UNUSED, - size_t data_len COAP_UNUSED -) { +/* + * strm + * return >=0 Number of bytes read. + * -1 Error (error in errno). + */ +ssize_t +coap_tls_read(coap_session_t *session COAP_UNUSED, + uint8_t *data COAP_UNUSED, + size_t data_len COAP_UNUSED) { return -1; } diff --git a/src/coap_openssl.c b/src/coap_openssl.c index a5e0bc26..93fa465b 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -706,12 +706,20 @@ static int coap_sock_destroy(BIO *a) { return 1; } -static int coap_sock_read(BIO *a, char *out, int outl) { +/* + * strm + * return +ve data amount + * 0 no more + * -1 error + */ +static int +coap_sock_read(BIO *a, char *out, int outl) { int ret = 0; coap_session_t *session = (coap_session_t *)BIO_get_data(a); if (out != NULL) { - ret = (int)coap_socket_read(&session->sock, (uint8_t*)out, (size_t)outl); + ret = (int)coap_netif_strm_read(session, (u_char *)out, outl); + /* Translate layer returns into what OpenSSL expects */ if (ret == 0) { BIO_set_retry_read(a); ret = -1; @@ -722,11 +730,19 @@ static int coap_sock_read(BIO *a, char *out, int outl) { return ret; } -static int coap_sock_write(BIO *a, const char *in, int inl) { +/* + * strm + * return +ve data amount + * 0 no more + * -1 error (error in errno) + */ +static int +coap_sock_write(BIO *a, const char *in, int inl) { int ret = 0; coap_session_t *session = (coap_session_t *)BIO_get_data(a); - ret = (int)coap_socket_write(&session->sock, (const uint8_t*)in, (size_t)inl); + ret = (int)coap_netif_strm_write(session, (const uint8_t *)in, inl); + /* Translate layer what returns into what OpenSSL expects */ BIO_clear_retry_flags(a); if (ret == 0) { BIO_set_retry_read(a); @@ -750,11 +766,6 @@ static int coap_sock_write(BIO *a, const char *in, int inl) { */ ret = inl; } - else { - coap_log_debug( "* %s: failed to send %d bytes (%s) state %d\n", - coap_session_str(session), inl, coap_socket_strerror(), - session->state); - } } } return ret; @@ -3344,7 +3355,8 @@ error: #endif /* COAP_CLIENT_SUPPORT */ #if COAP_SERVER_SUPPORT -void *coap_tls_new_server_session(coap_session_t *session, int *connected) { +void * +coap_tls_new_server_session(coap_session_t *session, int *connected) { BIO *bio = NULL; SSL *ssl = NULL; coap_tls_context_t *tls = &((coap_openssl_context_t *)session->context->dtls_context)->tls; @@ -3423,10 +3435,13 @@ void coap_tls_free_session(coap_session_t *session) { } } -ssize_t coap_tls_write(coap_session_t *session, - const uint8_t *data, - size_t data_len -) { +/* + * strm + * return +ve Number of bytes written. + * -1 Error (error in errno). + */ +ssize_t +coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { SSL *ssl = (SSL *)session->tls; int r, in_init; @@ -3448,7 +3463,7 @@ ssize_t coap_tls_write(coap_session_t *session, } if (err == SSL_ERROR_WANT_READ) session->sock.flags |= COAP_SOCKET_WANT_READ; - if (err == SSL_ERROR_WANT_WRITE) { + else if (err == SSL_ERROR_WANT_WRITE) { session->sock.flags |= COAP_SOCKET_WANT_WRITE; #ifdef COAP_EPOLL_SUPPORT coap_epoll_ctl_mod(&session->sock, @@ -3489,15 +3504,20 @@ ssize_t coap_tls_write(coap_session_t *session, return r; } -ssize_t coap_tls_read(coap_session_t *session, - uint8_t *data, - size_t data_len -) { +/* + * strm + * return >=0 Number of bytes read. + * -1 Error (error in errno). + */ +ssize_t +coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { SSL *ssl = (SSL *)session->tls; int r, in_init; - if (ssl == NULL) + if (ssl == NULL) { + errno = ENXIO; return -1; + } in_init = !SSL_is_init_finished(ssl); session->dtls_event = -1; diff --git a/src/coap_session.c b/src/coap_session.c index 02552d48..cc2337f7 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -408,19 +408,6 @@ void coap_session_set_mtu(coap_session_t *session, unsigned mtu) { } } -ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen) { - ssize_t bytes_written = coap_socket_write(&session->sock, data, datalen); - if (bytes_written > 0) { - coap_ticks(&session->last_rx_tx); - coap_log_debug("* %s: sent %zd bytes\n", - coap_session_str(session), bytes_written); - } else if (bytes_written < 0) { - coap_log_debug("* %s: failed to send %zd bytes\n", - coap_session_str(session), datalen ); - } - return bytes_written; -} - ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 41728db4..7a6ce1a3 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -1385,17 +1385,29 @@ void *coap_tls_new_server_session(coap_session_t *session COAP_UNUSED, int *conn void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED) { } -ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, - const uint8_t *data COAP_UNUSED, - size_t data_len COAP_UNUSED +/* + * strm + * return +ve Number of bytes written. + * -1 Error (error in errno). + */ +ssize_t +coap_tls_write(coap_session_t *session COAP_UNUSED, + const uint8_t *data COAP_UNUSED, + size_t data_len COAP_UNUSED ) { return -1; } -ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, - uint8_t *data COAP_UNUSED, - size_t data_len COAP_UNUSED -) { +/* + * strm + * return >=0 Number of bytes read. + * -1 Error (error in errno). + */ +ssize_t +coap_tls_read(coap_session_t *session COAP_UNUSED, + uint8_t *data COAP_UNUSED, + size_t data_len COAP_UNUSED) { + errno = ENODEV; return -1; } #endif /* !COAP_DISABLE_TCP */ diff --git a/src/net.c b/src/net.c index 7d589356..c103685e 100644 --- a/src/net.c +++ b/src/net.c @@ -772,8 +772,8 @@ coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) { break; case COAP_PROTO_TCP: #if !COAP_DISABLE_TCP - bytes_written = coap_session_write(session, pdu->token - pdu->hdr_size, - pdu->used_size + pdu->hdr_size); + bytes_written = coap_netif_strm_write(session, pdu->token - pdu->hdr_size, + pdu->used_size + pdu->hdr_size); #endif /* !COAP_DISABLE_TCP */ break; case COAP_PROTO_TLS: @@ -1676,7 +1676,7 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now switch (session->proto) { case COAP_PROTO_TCP: #if !COAP_DISABLE_TCP - bytes_written = coap_session_write( + bytes_written = coap_netif_strm_write( session, q->pdu->token - q->pdu->hdr_size + session->partial_write, q->pdu->used_size + q->pdu->hdr_size - session->partial_write @@ -1742,15 +1742,10 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) if (bytes_read == -2) /* Reset the session back to startup defaults */ coap_session_disconnected(session, COAP_NACK_ICMP_ISSUE); - else - coap_log_warn("* %s: read error\n", - coap_session_str(session)); } else if (bytes_read > 0) { session->last_rx_tx = now; memcpy(&session->addr_info, &packet->addr_info, sizeof(session->addr_info)); - coap_log_debug("* %s: received %zd bytes\n", - coap_session_str(session), bytes_read); coap_handle_dgram_for_proto(ctx, session, packet); } #if !COAP_DISABLE_TCP @@ -1761,8 +1756,8 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) do { if (session->proto == COAP_PROTO_TCP) - bytes_read = coap_socket_read(&session->sock, packet->payload, - packet->length); + bytes_read = coap_netif_strm_read(session, packet->payload, + packet->length); else if (session->proto == COAP_PROTO_TLS) bytes_read = coap_tls_read(session, packet->payload, packet->length); if (bytes_read > 0) {