From 52dcc5201b0a40660aa08c74c2b08275ccd4fb5a Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 30 Nov 2022 14:25:51 +0000 Subject: [PATCH] Reliable PDUs: coap_show_pdu(): Do not show all the non-reliable headers --- include/coap3/coap_pdu_internal.h | 1 + man/coap_logging.txt.in | 4 ++++ src/coap_debug.c | 19 ++++++++++++++++--- src/coap_session.c | 5 ++++- src/net.c | 4 ++++ src/pdu.c | 1 + 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/include/coap3/coap_pdu_internal.h b/include/coap3/coap_pdu_internal.h index 303194d5..e87331c1 100644 --- a/include/coap3/coap_pdu_internal.h +++ b/include/coap3/coap_pdu_internal.h @@ -155,6 +155,7 @@ struct coap_pdu_t { size_t body_total; /**< Holds body data total size */ coap_lg_xmit_t *lg_xmit; /**< Holds ptr to lg_xmit if sending a set of blocks */ + coap_session_t *session; /**< Session responsible for PDU or NULL */ }; /** diff --git a/man/coap_logging.txt.in b/man/coap_logging.txt.in index 0a0fa1d4..527a1976 100644 --- a/man/coap_logging.txt.in +++ b/man/coap_logging.txt.in @@ -275,6 +275,10 @@ The *coap_show_pdu*() function is used to decode the _pdu_, outputting as appropriate for logging _level_. Where the output goes is dependent on *coap_set_show_pdu_output*(). +*NOTE:* If _pdu_ has not been associated with a CoAP session (i.e. not a +received PDU or coap_send() not yet called), then the output assumes that +this _pdu_ is of type unreliable. + *Function: coap_endpoint_str()* The *coap_endpoint_str*() function returns a description string of the diff --git a/src/coap_debug.c b/src/coap_debug.c index 718aafbc..5f0fbf7a 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -724,9 +724,22 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { coap_mutex_lock(&static_show_pdu_mutex); #endif /* COAP_CONSTRAINED_STACK */ - snprintf(outbuf, sizeof(outbuf), "v:%d t:%s c:%s i:%04x {", - COAP_DEFAULT_VERSION, msg_type_string(pdu->type), - msg_code_string(pdu->code), pdu->mid); + if (!pdu->session || COAP_PROTO_NOT_RELIABLE(pdu->session->proto)) { + snprintf(outbuf, sizeof(outbuf), "v:%d t:%s c:%s i:%04x {", + COAP_DEFAULT_VERSION, msg_type_string(pdu->type), + msg_code_string(pdu->code), pdu->mid); + } else if (pdu->session->proto == COAP_PROTO_WS || + pdu->session->proto == COAP_PROTO_WSS) { + if (pdu->type != COAP_MESSAGE_CON) + coap_log_alert("WebSocket: type != CON\n"); + snprintf(outbuf, sizeof(outbuf), "v:WebSocket c:%s {", + msg_code_string(pdu->code)); + } else { + if (pdu->type != COAP_MESSAGE_CON) + coap_log_alert("Reliable: type != CON\n"); + snprintf(outbuf, sizeof(outbuf), "v:Reliable c:%s {", + msg_code_string(pdu->code)); + } for (i = 0; i < pdu->actual_token.length; i++) { outbuflen = strlen(outbuf); diff --git a/src/coap_session.c b/src/coap_session.c index 58aa361f..7ec3a585 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -479,7 +479,10 @@ coap_session_send_csm(coap_session_t *session) { ) { coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE); } else { - ssize_t bytes_written = coap_session_send_pdu(session, pdu); + ssize_t bytes_written; + + pdu->session = session; + bytes_written = coap_session_send_pdu(session, pdu); if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) { coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE); } else { diff --git a/src/net.c b/src/net.c index 348814a8..8a2d257d 100644 --- a/src/net.c +++ b/src/net.c @@ -1027,6 +1027,7 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { assert(pdu); + pdu->session = session; #if COAP_CLIENT_SUPPORT if (session->type == COAP_SESSION_TYPE_CLIENT && !coap_netif_available(session)) { @@ -1236,6 +1237,7 @@ coap_send_internal(coap_session_t *session, coap_pdu_t *pdu) { ssize_t bytes_written; coap_opt_iterator_t opt_iter; + pdu->session = session; if (pdu->code == COAP_RESPONSE_CODE(508)) { /* * Need to prepend our IP identifier to the data as per @@ -2909,6 +2911,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu resp = 500; goto fail_response; } + response->session = session; #if COAP_ASYNC_SUPPORT /* If handling a separate response, need CON, not ACK response */ if (async && pdu->type == COAP_MESSAGE_CON) @@ -3333,6 +3336,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, #endif /* COAP_OSCORE_SUPPORT */ int is_ext_token_rst; + pdu->session = session; coap_show_pdu(COAP_LOG_DEBUG, pdu); memset(&opt_filter, 0, sizeof(coap_opt_filter_t)); diff --git a/src/pdu.c b/src/pdu.c index ca0700e1..3ff1123d 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -61,6 +61,7 @@ coap_pdu_clear(coap_pdu_t *pdu, size_t size) { pdu->body_offset = 0; pdu->body_total = 0; pdu->lg_xmit = NULL; + pdu->session = NULL; } #ifdef WITH_LWIP