diff --git a/examples/coap-client.c b/examples/coap-client.c index b95211ddd3..f783f151ed 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -345,10 +345,13 @@ static void nack_handler(coap_session_t *session COAP_UNUSED, const coap_pdu_t *sent, const coap_nack_reason_t reason, - const coap_mid_t id COAP_UNUSED) { - coap_bin_const_t token = coap_pdu_get_token(sent); - if (!track_check_token(&token)) { - coap_log_err("nack_handler: Unexpected token\n"); + const coap_mid_t mid COAP_UNUSED) { + if (sent) { + coap_bin_const_t token = coap_pdu_get_token(sent); + + if (!track_check_token(&token)) { + coap_log_err("nack_handler: Unexpected token\n"); + } } switch (reason) { diff --git a/examples/coap-server.c b/examples/coap-server.c index 192f227bde..9cf33ee3f4 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -1720,7 +1720,7 @@ static void proxy_nack_handler(coap_session_t *session, const coap_pdu_t *sent COAP_UNUSED, const coap_nack_reason_t reason, - const coap_mid_t id COAP_UNUSED) { + const coap_mid_t mid COAP_UNUSED) { switch (reason) { case COAP_NACK_TOO_MANY_RETRIES: diff --git a/src/coap_session.c b/src/coap_session.c index 5caeeee4cd..69bd90fe94 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -859,17 +859,33 @@ coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) { if (reason == COAP_NACK_ICMP_ISSUE) { if (session->context->nack_handler) { + int sent_nack = 0; coap_queue_t *q = session->context->sendqueue; while (q) { if (q->session == session) { + /* Take the first one */ coap_bin_const_t token = q->pdu->actual_token; coap_check_update_token(session, q->pdu); session->context->nack_handler(session, q->pdu, reason, q->id); coap_update_token(q->pdu, token.length, token.s); + sent_nack = 1; + break; } q = q->next; } +#if COAP_CLIENT_SUPPORT + if (!sent_nack && session->lg_crcv) { + /* Take the first one */ + session->context->nack_handler(session, &session->lg_crcv->pdu, reason, + session->lg_crcv->pdu.mid); + sent_nack = 1; + } +#endif /* COAP_CLIENT_SUPPORT */ + if (!sent_nack) { + /* Unable to determine which request ICMP issue was for */ + session->context->nack_handler(session, NULL, reason, 0); + } } coap_log_debug("***%s: session issue (%s)\n", coap_session_str(session), coap_nack_name(reason));