From 09222bb6661842fcf4e66f742bcb67bcd881d81d Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 1 Mar 2023 16:31:12 +0000 Subject: [PATCH] netif: Add in close --- include/coap3/coap_netif_internal.h | 23 +++++++++++++++++++++++ src/coap_netif.c | 22 ++++++++++++++++++++++ src/coap_session.c | 14 +++++++------- src/net.c | 4 ++-- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/include/coap3/coap_netif_internal.h b/include/coap3/coap_netif_internal.h index 7bc91f8d..b04929da 100644 --- a/include/coap3/coap_netif_internal.h +++ b/include/coap3/coap_netif_internal.h @@ -36,6 +36,15 @@ */ int coap_netif_available(coap_session_t *session); +/** + * Function interface to check whether netif for endpoint is still available. + * + * @param endpoint Endpoint to check against. + * + * @return 1 If netif is available, else 0. + */ +int coap_netif_available_ep(coap_endpoint_t *endpoint); + /** * Layer function interface for Netif datagram listem (udp). * @@ -133,6 +142,20 @@ ssize_t coap_netif_strm_read(coap_session_t *session, uint8_t *data, ssize_t coap_netif_strm_write(coap_session_t *session, const uint8_t *data, size_t datalen); +/** + * Layer function interface for Netif close for a session. + * + * @param session Session to do the netif close on. + */ +void coap_netif_close(coap_session_t *session); + +/** + * Layer function interface for Netif close for a endpoint. + * + * @param endpoint Endpoint to do the netif close on. + */ +void coap_netif_close_ep(coap_endpoint_t *endpoint); + /** @} */ #endif /* COAP_NETIF_INTERNAL_H */ diff --git a/src/coap_netif.c b/src/coap_netif.c index 7d5c52d2..51d52868 100644 --- a/src/coap_netif.c +++ b/src/coap_netif.c @@ -27,6 +27,15 @@ coap_netif_available(coap_session_t *session) { } #if COAP_SERVER_SUPPORT +/* + * return 1 netif still in use. + * 0 netif no longer available. + */ +int +coap_netif_available_ep(coap_endpoint_t *endpoint) { + return endpoint->sock.flags != COAP_SOCKET_EMPTY; +} + int coap_netif_dgrm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr) { @@ -194,3 +203,16 @@ coap_netif_strm_write(coap_session_t *session, const uint8_t *data, return bytes_written; } #endif /* COAP_DISABLE_TCP */ + +void +coap_netif_close(coap_session_t *session) { + coap_socket_close(&session->sock); +} + +#if COAP_SERVER_SUPPORT +void +coap_netif_close_ep(coap_endpoint_t *endpoint) { + coap_socket_close(&endpoint->sock); +} + +#endif /* COAP_SERVER_SUPPORT */ diff --git a/src/coap_session.c b/src/coap_session.c index 01d3496f..66514704 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -262,8 +262,8 @@ void coap_session_mfree(coap_session_t *session) { else if (session->proto == COAP_PROTO_TLS) coap_tls_free_session(session); #endif /* !COAP_DISABLE_TCP */ - if (session->sock.flags != COAP_SOCKET_EMPTY) - coap_socket_close(&session->sock); + if (coap_netif_available(session)) + coap_netif_close(session); if (session->psk_identity) coap_delete_bin_const(session->psk_identity); if (session->psk_key) @@ -678,8 +678,8 @@ void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reaso #if !COAP_DISABLE_TCP if (COAP_PROTO_RELIABLE(session->proto)) { - if (session->sock.flags != COAP_SOCKET_EMPTY) { - coap_socket_close(&session->sock); + if (coap_netif_available(session)) { + coap_netif_close(session); coap_handle_event(session->context, state == COAP_SESSION_STATE_CONNECTING ? COAP_EVENT_TCP_FAILED : COAP_EVENT_TCP_CLOSED, session); @@ -1579,7 +1579,7 @@ coap_free_endpoint(coap_endpoint_t *ep) { coap_session_free(session); } } - if (ep->sock.flags != COAP_SOCKET_EMPTY) { + if (coap_netif_available_ep(ep)) { /* * ep->sock.endpoint is set in coap_new_endpoint(). * ep->sock.session is never set. @@ -1587,13 +1587,13 @@ coap_free_endpoint(coap_endpoint_t *ep) { * session->sock.session is set for both clients and servers (when a * new session is accepted), but does not affect the endpoint. * - * So, it is safe to call coap_socket_close() after all the sessions + * So, it is safe to call coap_netif_close_ep() after all the sessions * have been freed above as we are only working with the endpoint sock. */ #ifdef COAP_EPOLL_SUPPORT assert(ep->sock.session == NULL); #endif /* COAP_EPOLL_SUPPORT */ - coap_socket_close(&ep->sock); + coap_netif_close_ep(ep); } if (ep->context && ep->context->endpoint) { diff --git a/src/net.c b/src/net.c index 26c7e347..1a311f4e 100644 --- a/src/net.c +++ b/src/net.c @@ -1089,7 +1089,7 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { #if COAP_CLIENT_SUPPORT if (session->type == COAP_SESSION_TYPE_CLIENT && - session->sock.flags == COAP_SOCKET_EMPTY) { + !coap_netif_available(session)) { coap_log_debug("coap_send: Socket closed\n"); coap_delete_pdu(pdu); return COAP_INVALID_MID; @@ -2049,7 +2049,7 @@ coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) { sock->flags |= COAP_SOCKET_CAN_CONNECT; coap_connect_session(session->context, session, now); - if (sock->flags != COAP_SOCKET_EMPTY && + if (coap_netif_available(session) && !(sock->flags & COAP_SOCKET_WANT_WRITE)) { coap_epoll_ctl_mod(sock, EPOLLIN, __func__); }