diff --git a/examples/coap-server.c b/examples/coap-server.c index 73c9d079..96e5a8b5 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -2303,6 +2303,7 @@ get_context(const char *node, const char *port) { case COAP_PROTO_DTLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS; break; case COAP_PROTO_TLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP; break; case COAP_PROTO_NONE: + case COAP_PROTO_LAST: default: break; } diff --git a/include/coap3/coap_dtls_internal.h b/include/coap3/coap_dtls_internal.h index 86bdfd8f..24fb31a5 100644 --- a/include/coap3/coap_dtls_internal.h +++ b/include/coap3/coap_dtls_internal.h @@ -271,7 +271,7 @@ int coap_dtls_hello(coap_session_t *coap_session, * * If this layer is properly established on invocation, then the next layer * must get called by calling - * coap_session_establish(session) + * session->lfunc[COAP_LAYER_TLS].establish(session) * (or done at any point when DTLS is established). * * @param session Session that the lower layer connect was done on. @@ -367,7 +367,7 @@ ssize_t coap_tls_read(coap_session_t *coap_session, * * If this layer is properly established on invocation, then the next layer * must get called by calling - * coap_session_establish(session) + * session->lfunc[COAP_LAYER_TLS].establish(session) * (or done at any point when TLS is established). * * @param session Session that the lower layer accept/connect was done on. diff --git a/include/coap3/coap_io_internal.h b/include/coap3/coap_io_internal.h index cf83e9f3..df19a0a2 100644 --- a/include/coap3/coap_io_internal.h +++ b/include/coap3/coap_io_internal.h @@ -30,6 +30,98 @@ struct uip_udp_conn; #include "net/gnrc.h" #endif /* RIOT_VERSION */ +typedef enum { + COAP_LAYER_SESSION, + COAP_LAYER_TLS, + COAP_LAYER_LAST +} coap_layer_t; + +/** + * Function read interface for layer data receiving. + * + * If a called lower layer returned value is 0 or less, this must get passed + * back to the caller. + * + * If the layer function consumes all the data (i.e. to handle the protocol + * layer requirements), then the function must return 0 to indicate no data. + * + * Otherwise data must get updated (limited by datalen) and the number of bytes + * available returned. + * + * Note: If the number of returned bytes is less that read in, then + * COAP_SOCKET_CAN_READ must be dropped from session->sock.flags. + * + * @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 error in errno). + * -2 Recieved ICMP unreachable. + */ +typedef ssize_t (*coap_layer_read_t)(coap_session_t *session, + uint8_t *data, size_t datalen); + +/** + * Function write interface for layer data sending. + * + * If a called lower layer returned value is 0 or less, this must get passed + * back to the caller. + * + * If the layer function cannot transmit any data (congestion control etc.), + * then the function must return 0 to indicate no data sent. + * + * It is possible that not all the data is sent (congestion control etc.), + * and bytes written is less that datalen. + * + * Note: If the number of returned bytes is less that to be written, + * COAP_SOCKET_WANT_WRITE must be added to session->sock.flags. + * + * @param session Session to receive data on. + * @param data The data to write out. + * @param datalen The maximum length of @p data. + * + * @return >=0 Number of bytes written. + * -1 Error error in errno). + */ +typedef ssize_t (*coap_layer_write_t)(coap_session_t *session, + const uint8_t *data, size_t datalen); +/** + * Function establish interface for layer establish handling. + * + * If this layer is properly established on invocation, then the next layer + * must get called by calling + * session->lfunc[_this_layer_].establish(session) + * (or done at any point when layer is established). + * If the establishment of a layer fails, then + * coap_session_disconnected(session, COAP_NACK_xxx_LAYER_FAILED) must be + * called. + * + * @param session Session being established + */ +typedef void (*coap_layer_establish_t)(coap_session_t *session); + +/** + * Function close interface for layer closing. + * + * When this layer is properly closed, then the next layer + * must get called by calling + * session->lfunc[_this_layer_].close(session) + * (or done at any point when layer is closed). + * + * @param session Session being closed. + */ +typedef void (*coap_layer_close_t)(coap_session_t *session); + +typedef struct { + coap_layer_read_t read; /* Get data from next layer (TCP) */ + coap_layer_write_t write; /* Output data to next layer */ + coap_layer_establish_t establish; /* Layer establish */ + coap_layer_close_t close; /* Connection close */ +} coap_layer_func_t; + +extern coap_layer_func_t coap_layers_coap[COAP_PROTO_LAST][COAP_LAYER_LAST]; + struct coap_socket_t { #if defined(WITH_LWIP) struct udp_pcb *pcb; @@ -48,6 +140,7 @@ struct coap_socket_t { coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening endpoint. */ #endif /* COAP_SERVER_SUPPORT */ + coap_layer_func_t lfunc[COAP_LAYER_LAST]; /* Layer functions to use */ }; /** @@ -89,13 +182,37 @@ coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr ); +/** + * Function interface to close off a socket. + * + * @param sock Socket to close. + * + */ void coap_socket_close(coap_socket_t *sock); -ssize_t -coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len); +/** + * Function interface for data stream sending off a socket. + * + * @param sock Socket to send data over. + * @param data The data to send. + * @param data_len The length of @p data. + * + * @return >=0 Number of bytes sent. + * -1 Error error in errno). + */ +ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len); -ssize_t -coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len); +/** + * Function interface for data stream receiving off a socket. + * + * @param sock Socket to receive data on. + * @param data The data to receive. + * @param data_len The maximum length of @p data. + * + * @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); /** * Epoll specific function to add the state of events that epoll is to track @@ -188,7 +305,6 @@ struct coap_packet_t { #ifdef WITH_CONTIKI void coap_start_io_process(void); void coap_stop_io_process(void); -#endif /* COAP_IO_INTERNAL_H_ */ +#endif /* WITH_CONTIKI */ #endif /* COAP_IO_INTERNAL_H_ */ - diff --git a/include/coap3/pdu.h b/include/coap3/pdu.h index 61fae22e..2edf4c76 100644 --- a/include/coap3/pdu.h +++ b/include/coap3/pdu.h @@ -305,6 +305,7 @@ typedef enum coap_proto_t { COAP_PROTO_DTLS, COAP_PROTO_TCP, COAP_PROTO_TLS, + COAP_PROTO_LAST } coap_proto_t; /** diff --git a/src/coap_dtls.c b/src/coap_dtls.c index f793f51e..323e317a 100644 --- a/src/coap_dtls.c +++ b/src/coap_dtls.c @@ -19,7 +19,6 @@ void coap_dtls_establish(coap_session_t *session) { - assert(session->proto == COAP_PROTO_DTLS); #if COAP_CLIENT_SUPPORT if (session->type == COAP_SESSION_TYPE_CLIENT) session->tls = coap_dtls_new_client_session(session); @@ -36,15 +35,15 @@ coap_dtls_establish(coap_session_t *session) { return; } coap_ticks(&session->last_rx_tx); - return; } void coap_dtls_close(coap_session_t *session) { - if (session->tls) + if (session->tls) { coap_dtls_free_session(session); - if (coap_netif_available(session)) - coap_netif_close(session); + session->tls = NULL; + } + session->sock.lfunc[COAP_LAYER_TLS].close(session); } #if !COAP_DISABLE_TCP @@ -52,7 +51,6 @@ void coap_tls_establish(coap_session_t *session) { int connected = 0; - assert(session->proto == COAP_PROTO_TLS); #if COAP_CLIENT_SUPPORT if (session->type == COAP_SESSION_TYPE_CLIENT) session->tls = coap_tls_new_client_session(session, &connected); @@ -66,21 +64,21 @@ coap_tls_establish(coap_session_t *session) { session->state = COAP_SESSION_STATE_HANDSHAKE; if (connected) { coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); - coap_session_establish(session); + session->sock.lfunc[COAP_LAYER_TLS].establish(session); } } else { coap_session_disconnected(session, COAP_NACK_TLS_LAYER_FAILED); return; } coap_ticks(&session->last_rx_tx); - return; } void coap_tls_close(coap_session_t *session) { - if (session->tls) + if (session->tls) { coap_tls_free_session(session); - if (coap_netif_available(session)) - coap_netif_close(session); + session->tls = NULL; + } + session->sock.lfunc[COAP_LAYER_TLS].close(session); } -#endif /* COAP_DISABLE_TCP */ +#endif /* !COAP_DISABLE_TCP */ diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index 82363d30..b2b740bf 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -1995,7 +1995,8 @@ coap_dgram_write(gnutls_transport_ptr_t context, const void *send_buffer, errno = ECONNRESET; return -1; } - result = (int)coap_netif_dgrm_write(c_session, send_buffer, send_buffer_length); + result = c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session, + send_buffer, send_buffer_length); if (result != (int)send_buffer_length) { coap_log_warn("coap_netif_dgrm_write failed (%zd != %zu)\n", result, send_buffer_length); @@ -2478,7 +2479,7 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data, coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); gnutls_transport_set_ptr(g_env->g_session, c_session); - coap_session_establish(c_session); + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); } ret = gnutls_record_recv(g_env->g_session, pdu, (int)sizeof(pdu)); if (ret > 0) { @@ -2652,7 +2653,7 @@ 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) { - ret = (int)coap_netif_strm_read(c_session, out, outl); + ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].read(c_session, out, outl); /* Translate layer returns into what GnuTLS expects */ if (ret == 0) { errno = EAGAIN; @@ -2673,7 +2674,7 @@ 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_netif_strm_write(c_session, in, inl); + ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session, in, inl); /* Translate layer what returns into what GnuTLS expects */ if (ret < 0) { if ((c_session->state == COAP_SESSION_STATE_CSM || @@ -2742,7 +2743,7 @@ coap_tls_new_client_session(coap_session_t *c_session, int *connected) { if (ret == 1) { *connected = 1; coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); - coap_session_establish(c_session); + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); } return g_env; @@ -2854,7 +2855,7 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, if (ret == 1) { coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); - coap_session_establish(c_session); + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); } else { ret = -1; } @@ -2899,7 +2900,7 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { if (ret == 1) { coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); - coap_session_establish(c_session); + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); } } if (c_session->state != COAP_SESSION_STATE_NONE && g_env->established) { diff --git a/src/coap_io.c b/src/coap_io.c index f423160b..7aa8eeb5 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -74,6 +74,61 @@ #endif /* IPV6_RECVPKTINFO */ #endif /* !(WITH_CONTIKI || RIOT_VERSION) */ +/* + * Layer index table. A whole protocol chunk gets copied into coap_socket_t. + * Each layer invokes the function defined at its layer to get the next layer + * (which could be above or below) to complete. + * + * The stack layers are (* managed by libcoap) + * Application + * CoAP * + * CoAP-Session * + * DTLS * + * Netif * + * Sockets * + * Network Stack + * + * dgrm read currently handled separately. + * strm read works down the layers. + * write works down the layers. + * establish is done after netif accept/connect completes by invoking SESSION + * and then works up the layers. + * close works down the layers + */ +coap_layer_func_t coap_layers_coap[COAP_PROTO_LAST][COAP_LAYER_LAST] = { + { /* COAP_PROTO_NONE */ + { NULL, NULL, NULL, NULL }, /* SESSION */ + { NULL, NULL, NULL, NULL } /* TLS */ + }, + { /* COAP_PROTO_UDP */ + { NULL, coap_netif_dgrm_write, coap_session_establish, coap_netif_close }, /* SESSION */ + { NULL, NULL, NULL, NULL } /* TLS */ + }, + { /* COAP_PROTO_DTLS */ + { NULL, coap_dtls_send, coap_dtls_establish, coap_dtls_close }, /* SESSION */ + { NULL, coap_netif_dgrm_write, coap_session_establish, coap_netif_close } /* TLS */ + }, +#if !COAP_DISABLE_TCP + { /* COAP_PROTO_TCP */ + { coap_netif_strm_read, coap_netif_strm_write, coap_session_establish, coap_netif_close }, /* SESSION */ + { NULL, NULL, NULL, NULL } /* TLS */ + }, + { /* COAP_PROTO_TLS */ + { coap_tls_read, coap_tls_write, coap_tls_establish, coap_tls_close }, /* SESSION */ + { coap_netif_strm_read, coap_netif_strm_write, coap_session_establish, coap_netif_close } /* TLS */ + }, +#else /* COAP_DISABLE_TCP */ + { /* COAP_PROTO_TCP */ + { NULL, NULL, NULL, NULL }, /* SESSION */ + { NULL, NULL, NULL, NULL } /* TLS */ + }, + { /* COAP_PROTO_TLS */ + { NULL, NULL, NULL, NULL }, /* SESSION */ + { NULL, NULL, NULL, NULL } /* TLS */ + }, +#endif /* COAP_DISABLE_TCP */ +}; + #if COAP_SERVER_SUPPORT coap_endpoint_t * coap_malloc_endpoint(void) { diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index a65cafdc..46fb20ed 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -229,8 +229,8 @@ coap_dgram_write(void *ctx, const unsigned char *send_buffer, errno = ECONNRESET; return -1; } - result = (int)coap_netif_dgrm_write(c_session, - send_buffer, send_buffer_length); + result = (int)c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session, + send_buffer, send_buffer_length); if (result != (ssize_t)send_buffer_length) { coap_log_warn("coap_netif_dgrm_write failed (%zd != %zu)\n", result, send_buffer_length); @@ -1411,7 +1411,7 @@ coap_sock_read(void *ctx, unsigned char *out, size_t outl) { coap_session_t *c_session = (coap_session_t *)ctx; if (out != NULL) { - ret = (int)coap_netif_strm_read(c_session, out, outl); + ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].read(c_session, out, outl); /* Translate layer returns into what MbedTLS expects */ if (ret == -1) { if (errno == ECONNRESET) { @@ -1439,7 +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_netif_strm_write(c_session, (const uint8_t *)in, inl); + ret = c_session->sock.lfunc[COAP_LAYER_TLS].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 || @@ -2062,7 +2064,7 @@ coap_dtls_receive(coap_session_t *c_session, if (c_session->state == COAP_SESSION_STATE_HANDSHAKE) { coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); - coap_session_establish(c_session); + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); } ret = mbedtls_ssl_read(&m_env->ssl, pdu, sizeof(pdu)); @@ -2255,7 +2257,7 @@ void *coap_tls_new_client_session(coap_session_t *c_session, if (ret == 1) { *connected = 1; coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); - coap_session_establish(c_session); + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); } return m_env; #endif /* MBEDTLS_SSL_CLI_C */ @@ -2357,9 +2359,8 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, if (ret == 1) { coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); - coap_session_establish(c_session); - } - else { + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); + } else { ret = -1; } } @@ -2405,7 +2406,7 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) if (ret == 1) { coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); - coap_session_establish(c_session); + c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session); } } diff --git a/src/coap_netif.c b/src/coap_netif.c index 6ed7c416..4ca23792 100644 --- a/src/coap_netif.c +++ b/src/coap_netif.c @@ -219,8 +219,8 @@ coap_netif_strm_read(coap_session_t *session, uint8_t *data, size_t datalen) { coap_log_debug("* %s: netif: received %zd bytes\n", coap_session_str(session), bytes_read); } else if (bytes_read == -1 && errno != EAGAIN) { - coap_log_debug( "* %s: netif: failed to receive any bytes (%s)\n", - coap_session_str(session), coap_socket_strerror()); + coap_log_debug( "* %s: netif: failed to receive any bytes (%s) state %d\n", + coap_session_str(session), coap_socket_strerror(), session->state); errno = keep_errno; } return bytes_read; @@ -257,7 +257,8 @@ coap_netif_strm_write(coap_session_t *session, const uint8_t *data, void coap_netif_close(coap_session_t *session) { - coap_socket_close(&session->sock); + if (coap_netif_available(session)) + coap_socket_close(&session->sock); } #if COAP_SERVER_SUPPORT @@ -265,5 +266,4 @@ void coap_netif_close_ep(coap_endpoint_t *endpoint) { coap_socket_close(&endpoint->sock); } - #endif /* COAP_SERVER_SUPPORT */ diff --git a/src/coap_openssl.c b/src/coap_openssl.c index e99b6124..6f0f8a96 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -367,7 +367,7 @@ coap_dgram_write(BIO *a, const char *in, int inl) { errno = ECONNRESET; return -1; } - ret = (int)coap_netif_dgrm_write(data->session, + ret = (int)data->session->sock.lfunc[COAP_LAYER_TLS].write(data->session, (const uint8_t *)in, inl); BIO_clear_retry_flags(a); @@ -718,7 +718,8 @@ coap_sock_read(BIO *a, char *out, int outl) { coap_session_t *session = (coap_session_t *)BIO_get_data(a); if (out != NULL) { - ret = (int)coap_netif_strm_read(session, (u_char *)out, outl); + ret =(int)session->sock.lfunc[COAP_LAYER_TLS].read(session, (u_char *)out, + outl); /* Translate layer returns into what OpenSSL expects */ if (ret == 0) { BIO_set_retry_read(a); @@ -741,7 +742,9 @@ 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_netif_strm_write(session, (const uint8_t *)in, inl); + ret = (int)session->sock.lfunc[COAP_LAYER_TLS].write(session, + (const uint8_t *)in, + inl); /* Translate layer what returns into what OpenSSL expects */ BIO_clear_retry_flags(a); if (ret == 0) { @@ -3215,7 +3218,7 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); - coap_session_establish(session); + session->sock.lfunc[COAP_LAYER_TLS].establish(session); } r = 0; } else { @@ -3470,7 +3473,7 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); - coap_session_establish(session); + session->sock.lfunc[COAP_LAYER_TLS].establish(session); } if (err == SSL_ERROR_WANT_READ) session->sock.flags |= COAP_SOCKET_WANT_READ; @@ -3498,7 +3501,7 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); - coap_session_establish(session); + session->sock.lfunc[COAP_LAYER_TLS].establish(session); } if (session->dtls_event >= 0) { @@ -3544,7 +3547,7 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); - coap_session_establish(session); + session->sock.lfunc[COAP_LAYER_TLS].establish(session); } if (err == SSL_ERROR_WANT_READ) session->sock.flags |= COAP_SOCKET_WANT_READ; @@ -3570,7 +3573,7 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); - coap_session_establish(session); + session->sock.lfunc[COAP_LAYER_TLS].establish(session); } if (session->dtls_event >= 0) { diff --git a/src/coap_session.c b/src/coap_session.c index 68af9a27..3227ac44 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -257,14 +257,9 @@ coap_session_mfree(coap_session_t *session) { if (session->partial_pdu) coap_delete_pdu(session->partial_pdu); - if (session->proto == COAP_PROTO_DTLS) - coap_dtls_close(session); -#if !COAP_DISABLE_TCP - else if (session->proto == COAP_PROTO_TLS) - coap_tls_close(session); -#endif /* !COAP_DISABLE_TCP */ - else if (coap_netif_available(session)) - coap_netif_close(session); + + session->sock.lfunc[COAP_LAYER_SESSION].close(session); + if (session->psk_identity) coap_delete_bin_const(session->psk_identity); if (session->psk_key) @@ -513,7 +508,8 @@ coap_mid_t coap_session_send_ping(coap_session_t *session) { return coap_send_internal(session, ping); } -void coap_session_connected(coap_session_t *session) { +void +coap_session_connected(coap_session_t *session) { if (session->state != COAP_SESSION_STATE_ESTABLISHED) { coap_log_debug("***%s: session connected\n", coap_session_str(session)); @@ -709,17 +705,7 @@ coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) { session->doing_first = 0; } #endif /* !COAP_DISABLE_TCP */ - if (session->tls) { - if (session->proto == COAP_PROTO_DTLS) - coap_dtls_close(session); -#if !COAP_DISABLE_TCP - else if (session->proto == COAP_PROTO_TLS) - coap_tls_close(session); -#endif /* !COAP_DISABLE_TCP */ - session->tls = NULL; - } else if (coap_netif_available(session)) { - coap_netif_close(session); - } + session->sock.lfunc[COAP_LAYER_SESSION].close(session); } #if COAP_SERVER_SUPPORT @@ -855,6 +841,8 @@ coap_endpoint_get_session(coap_endpoint_t *endpoint, packet->ifindex, endpoint->context, endpoint); if (session) { session->last_rx_tx = now; + memcpy(session->sock.lfunc, endpoint->sock.lfunc, + sizeof(session->sock.lfunc)); if (endpoint->proto == COAP_PROTO_UDP) session->state = COAP_SESSION_STATE_ESTABLISHED; else if (endpoint->proto == COAP_PROTO_DTLS) { @@ -917,6 +905,7 @@ coap_session_create_client(coap_context_t *ctx, default_port = COAPS_DEFAULT_PORT; break; case COAP_PROTO_NONE: + case COAP_PROTO_LAST: default: assert(0); return NULL; @@ -928,6 +917,8 @@ coap_session_create_client(coap_context_t *ctx, coap_session_reference(session); session->sock.session = session; + memcpy(&session->sock.lfunc, coap_layers_coap[proto], + sizeof(session->sock.lfunc)); if (COAP_PROTO_NOT_RELIABLE(proto)) { coap_session_t *s, *rtmp; @@ -987,33 +978,27 @@ error: return NULL; } -static coap_session_t * -coap_session_connect(coap_session_t *session) { - if (session->proto == COAP_PROTO_UDP) { - session->state = COAP_SESSION_STATE_ESTABLISHED; - } else if (session->proto == COAP_PROTO_DTLS) { - coap_dtls_establish(session); -#if !COAP_DISABLE_TCP - } else { - if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS) { - if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) { - session->state = COAP_SESSION_STATE_CONNECTING; - if (session->state != COAP_SESSION_STATE_ESTABLISHED && - session->state != COAP_SESSION_STATE_NONE && - COAP_PROTO_RELIABLE(session->proto) && - session->type == COAP_SESSION_TYPE_CLIENT) { - session->doing_first = 1; - } - } else if (session->proto == COAP_PROTO_TLS) { - coap_tls_establish(session); - } else { - coap_session_establish(session); - } - } -#endif /* !COAP_DISABLE_TCP */ +static void +coap_session_check_connect(coap_session_t *session) { + if (COAP_PROTO_NOT_RELIABLE(session->proto)) { + session->sock.lfunc[COAP_LAYER_SESSION].establish(session); } +#if !COAP_DISABLE_TCP + if (COAP_PROTO_RELIABLE(session->proto)) { + if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) { + session->state = COAP_SESSION_STATE_CONNECTING; + if (session->state != COAP_SESSION_STATE_ESTABLISHED && + session->state != COAP_SESSION_STATE_NONE && + session->type == COAP_SESSION_TYPE_CLIENT) { + session->doing_first = 1; + } + } else { + /* Initial connect worked immediately */ + session->sock.lfunc[COAP_LAYER_SESSION].establish(session); + } + } +#endif /* !COAP_DISABLE_TCP */ coap_ticks(&session->last_rx_tx); - return session; } #endif /* COAP_CLIENT_SUPPORT */ @@ -1027,50 +1012,28 @@ coap_session_establish(coap_session_t *session) { #endif /* !COAP_DISABLE_TCP */ } -#if COAP_SERVER_SUPPORT -#if !COAP_DISABLE_TCP -static coap_session_t * -coap_session_accept(coap_session_t *session) { - if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS) - coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session); - if (session->proto == COAP_PROTO_TCP) { - coap_session_establish(session); - } else if (session->proto == COAP_PROTO_TLS) { - coap_tls_establish(session); - } - return session; -} -#endif /* !COAP_DISABLE_TCP */ -#endif /* COAP_SERVER_SUPPORT */ - #if COAP_CLIENT_SUPPORT -coap_session_t *coap_new_client_session( - coap_context_t *ctx, - const coap_address_t *local_if, - const coap_address_t *server, - coap_proto_t proto -) { +coap_session_t * +coap_new_client_session(coap_context_t *ctx, + const coap_address_t *local_if, + const coap_address_t *server, + coap_proto_t proto) { coap_session_t *session = coap_session_create_client(ctx, local_if, server, proto); if (session) { coap_log_debug("***%s: session %p: created outgoing session\n", coap_session_str(session), (void *)session); - session = coap_session_connect(session); - if (session) - session->context = ctx; + coap_session_check_connect(session); } return session; } -coap_session_t *coap_new_client_session_psk( - coap_context_t *ctx, - const coap_address_t *local_if, - const coap_address_t *server, - coap_proto_t proto, - const char *identity, - const uint8_t *key, - unsigned key_len -) { +coap_session_t * +coap_new_client_session_psk(coap_context_t *ctx, + const coap_address_t *local_if, + const coap_address_t *server, + coap_proto_t proto, const char *identity, + const uint8_t *key, unsigned key_len) { coap_dtls_cpsk_t setup_data; memset (&setup_data, 0, sizeof(setup_data)); @@ -1090,13 +1053,12 @@ coap_session_t *coap_new_client_session_psk( proto, &setup_data); } -coap_session_t *coap_new_client_session_psk2( - coap_context_t *ctx, - const coap_address_t *local_if, - const coap_address_t *server, - coap_proto_t proto, - coap_dtls_cpsk_t *setup_data -) { +coap_session_t * +coap_new_client_session_psk2(coap_context_t *ctx, + const coap_address_t *local_if, + const coap_address_t *server, + coap_proto_t proto, + coap_dtls_cpsk_t *setup_data) { coap_session_t *session = coap_session_create_client(ctx, local_if, server, proto); @@ -1143,7 +1105,8 @@ coap_session_t *coap_new_client_session_psk2( } coap_log_debug("***%s: new outgoing session\n", coap_session_str(session)); - return coap_session_connect(session); + coap_session_check_connect(session); + return session; } #endif /* COAP_CLIENT_SUPPORT */ @@ -1265,13 +1228,11 @@ coap_session_get_psk_key(const coap_session_t *session) { } #if COAP_CLIENT_SUPPORT -coap_session_t *coap_new_client_session_pki( - coap_context_t *ctx, - const coap_address_t *local_if, - const coap_address_t *server, - coap_proto_t proto, - coap_dtls_pki_t* setup_data -) { +coap_session_t *coap_new_client_session_pki(coap_context_t *ctx, + const coap_address_t *local_if, + const coap_address_t *server, + coap_proto_t proto, + coap_dtls_pki_t* setup_data) { coap_session_t *session; if (coap_dtls_is_supported() || coap_tls_is_supported()) { @@ -1301,7 +1262,8 @@ coap_session_t *coap_new_client_session_pki( } coap_log_debug("***%s: new outgoing session\n", coap_session_str(session)); - return coap_session_connect(session); + coap_session_check_connect(session); + return session; } #endif /* ! COAP_CLIENT_SUPPORT */ @@ -1315,6 +1277,7 @@ coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep) { if (!session) goto error; + memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc)); if (!coap_netif_strm_accept(ep, session)) goto error; @@ -1330,11 +1293,10 @@ coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep) { if (session) { coap_log_debug("***%s: session %p: new incoming session\n", coap_session_str(session), (void *)session); - /* Returned session may already have been released and is now NULL */ - session = coap_session_accept(session); - if(session) { - coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); - } + coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session); + coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); + session->state = COAP_SESSION_STATE_CONNECTING; + session->sock.lfunc[COAP_LAYER_SESSION].establish(session); } return session; @@ -1451,6 +1413,7 @@ coap_proto_name(coap_proto_t proto) { case COAP_PROTO_TCP: return "TCP "; case COAP_PROTO_TLS: return "TLS "; case COAP_PROTO_NONE: + case COAP_PROTO_LAST: default: return "????" ; break; } return NULL; @@ -1499,6 +1462,7 @@ coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, co ep->context = context; ep->proto = proto; ep->sock.endpoint = ep; + memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc)); if (COAP_PROTO_NOT_RELIABLE(proto)) { if (!coap_netif_dgrm_listen(ep, listen_addr)) diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 7a6ce1a3..e2ff62b1 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -260,7 +260,8 @@ dtls_send_to_peer(struct dtls_context_t *dtls_context, coap_log_warn("dtls_send_to_peer: cannot find local interface\n"); return -3; } - return (int)coap_netif_dgrm_write(coap_session, data, len); + return (int)coap_session->sock.lfunc[COAP_LAYER_TLS].write(coap_session, + data, len); } static int diff --git a/src/net.c b/src/net.c index 024aae6b..48029e1a 100644 --- a/src/net.c +++ b/src/net.c @@ -761,31 +761,9 @@ coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) { ssize_t bytes_written = -1; assert(pdu->hdr_size > 0); - switch(session->proto) { - case COAP_PROTO_UDP: - bytes_written = coap_netif_dgrm_write(session, pdu->token - pdu->hdr_size, - pdu->used_size + pdu->hdr_size); - break; - case COAP_PROTO_DTLS: - bytes_written = coap_dtls_send(session, pdu->token - pdu->hdr_size, - pdu->used_size + pdu->hdr_size); - break; - case COAP_PROTO_TCP: -#if !COAP_DISABLE_TCP - 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: -#if !COAP_DISABLE_TCP - bytes_written = coap_tls_write(session, pdu->token - pdu->hdr_size, - pdu->used_size + pdu->hdr_size); -#endif /* !COAP_DISABLE_TCP */ - break; - case COAP_PROTO_NONE: - default: - break; - } + bytes_written = session->sock.lfunc[COAP_LAYER_SESSION].write(session, + pdu->token - pdu->hdr_size, + pdu->used_size + pdu->hdr_size); coap_show_pdu(COAP_LOG_DEBUG, pdu); return bytes_written; } @@ -798,54 +776,8 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { #if ! COAP_CLIENT_SUPPORT return -1; #else /* COAP_CLIENT_SUPPORT */ - if (session->proto == COAP_PROTO_DTLS && !session->tls) { - session->tls = coap_dtls_new_client_session(session); - if (session->tls) { - session->state = COAP_SESSION_STATE_HANDSHAKE; - return coap_session_delay_pdu(session, pdu, node); - } - coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session); + if (session->type != COAP_SESSION_TYPE_CLIENT) return -1; -#if !COAP_DISABLE_TCP - } else if(COAP_PROTO_RELIABLE(session->proto)) { - if (!coap_netif_strm_connect1(session, &session->addr_info.local, - &session->addr_info.remote, - session->proto == COAP_PROTO_TLS ? - COAPS_DEFAULT_PORT : - COAP_DEFAULT_PORT)) { - coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session); - return -1; - } - session->last_ping = 0; - session->last_pong = 0; - session->csm_tx = 0; - coap_ticks( &session->last_rx_tx ); - if ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) != 0) { - session->state = COAP_SESSION_STATE_CONNECTING; - return coap_session_delay_pdu(session, pdu, node); - } - coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session); - if (session->proto == COAP_PROTO_TLS) { - int connected = 0; - session->state = COAP_SESSION_STATE_HANDSHAKE; - session->tls = coap_tls_new_client_session(session, &connected); - if (session->tls) { - if (connected) { - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); - coap_session_establish(session); - } - return coap_session_delay_pdu(session, pdu, node); - } - coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session); - coap_session_disconnected(session, COAP_NACK_TLS_FAILED); - return -1; - } else { - coap_session_establish(session); - } -#endif /* !COAP_DISABLE_TCP */ - } else { - return -1; - } #endif /* COAP_CLIENT_SUPPORT */ } @@ -1624,34 +1556,16 @@ coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_p #if COAP_CLIENT_SUPPORT static void -coap_connect_session(coap_context_t *ctx, - coap_session_t *session, - coap_tick_t now) { - (void)ctx; +coap_connect_session(coap_session_t *session, coap_tick_t now) { #if COAP_DISABLE_TCP - (void)session; (void)now; + + session->sock.flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CAN_CONNECT); #else /* !COAP_DISABLE_TCP */ if (coap_netif_strm_connect2(session)) { session->last_rx_tx = now; coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session); - if (session->proto == COAP_PROTO_TCP) { - coap_session_establish(session); - } else if (session->proto == COAP_PROTO_TLS) { - int connected = 0; - session->state = COAP_SESSION_STATE_HANDSHAKE; - session->tls = coap_tls_new_client_session(session, &connected); - if (session->tls) { - if (connected) { - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, - session); - coap_session_establish(session); - } - } else { - coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session); - coap_session_disconnected(session, COAP_NACK_TLS_FAILED); - } - } + session->sock.lfunc[COAP_LAYER_SESSION].establish(session); } else { coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session); coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE); @@ -1671,32 +1585,9 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now coap_log_debug("** %s: mid=0x%x: transmitted after delay\n", coap_session_str(session), (int)q->pdu->mid); assert(session->partial_write < q->pdu->used_size + q->pdu->hdr_size); - switch (session->proto) { - case COAP_PROTO_TCP: -#if !COAP_DISABLE_TCP - bytes_written = coap_netif_strm_write( - session, + bytes_written = session->sock.lfunc[COAP_LAYER_SESSION].write(session, q->pdu->token - q->pdu->hdr_size + session->partial_write, - q->pdu->used_size + q->pdu->hdr_size - session->partial_write - ); -#endif /* !COAP_DISABLE_TCP */ - break; - case COAP_PROTO_TLS: -#if !COAP_DISABLE_TCP - 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 - ); -#endif /* !COAP_DISABLE_TCP */ - break; - case COAP_PROTO_NONE: - case COAP_PROTO_UDP: - case COAP_PROTO_DTLS: - default: - bytes_written = -1; - break; - } + q->pdu->used_size + q->pdu->hdr_size - session->partial_write); if (bytes_written > 0) session->last_rx_tx = now; if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size - session->partial_write) { @@ -1753,14 +1644,10 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) int retry; do { - if (session->proto == COAP_PROTO_TCP) - 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); + bytes_read = session->sock.lfunc[COAP_LAYER_SESSION].read(session, + packet->payload, + packet->length); if (bytes_read > 0) { - coap_log_debug("* %s: received %zd bytes\n", - coap_session_str(session), bytes_read); session->last_rx_tx = now; } p = packet->payload; @@ -1975,7 +1862,7 @@ coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { /* Make sure the session object is not deleted in one of the callbacks */ coap_session_reference(s); if ((s->sock.flags & COAP_SOCKET_CAN_CONNECT) != 0) { - coap_connect_session(ctx, s, now); + coap_connect_session(s, now); } if ((s->sock.flags & COAP_SOCKET_CAN_READ) != 0 && s->ref > 1) { coap_read_session(ctx, s, now); @@ -2050,7 +1937,7 @@ coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents if ((sock->flags & COAP_SOCKET_WANT_CONNECT) && (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) { sock->flags |= COAP_SOCKET_CAN_CONNECT; - coap_connect_session(session->context, session, now); + coap_connect_session(session, now); if (coap_netif_available(session) && !(sock->flags & COAP_SOCKET_WANT_WRITE)) { coap_epoll_ctl_mod(sock, EPOLLIN, __func__); diff --git a/src/pdu.c b/src/pdu.c index 523a6d55..6741ce83 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -1341,7 +1341,7 @@ coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto) { "coap_add_token: Token size too large. PDU ignored\n"); return 0; } - if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) { + if (COAP_PROTO_NOT_RELIABLE(proto)) { assert(pdu->max_hdr_size >= 4); if (pdu->max_hdr_size < 4) { coap_log_warn( @@ -1355,7 +1355,7 @@ coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto) { pdu->token[-2] = (uint8_t)(pdu->mid >> 8); pdu->token[-1] = (uint8_t)(pdu->mid); pdu->hdr_size = 4; - } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) { + } else if (COAP_PROTO_RELIABLE(proto)) { size_t len; assert(pdu->used_size >= pdu->e_token_length); if (pdu->used_size < pdu->e_token_length) {