net.c: Delay sending ACK until after response_handler() is called

In handle_response() the ACK is sent before calling ctx->response_handler() so
if the response_handler() wants to send RST the server ignores it as the ACK
has already been received.

By updating coap_response_handler_t to return a value, this can be used to
send the ACK (return of COAP_RESPONSE_OK) or a RST (return of COAP_RESPONSE_FAIL)
after ctx->response_handler() is called.

include/coap2/net.h:
man/coap_handler.txt.in:

Create a new enum coap_response_t for use as the return value for
coap_response_handler_t.
Update coap_response_handler_t and associated documentation.

src/net.c:

Send CoAP ACK or RST depending on return value of ctx->response_handler().

examples/client.c:

Update message_handler() to use the new coap_response_handler_t return values
(coap_response_t).
This commit is contained in:
Jon Shallow committed 2021-02-24 09:37:32 +00:00
1 parent b0e47d71d0
commit 14e75a54b8
5 files changed
+61 -34

No files matched your search

+10 -8
View File
@@ -316,9 +316,9 @@ nack_handler(coap_context_t *context UNUSED_PARAM,
/*
* Response handler used for coap_send_large() responses
*/
static void
static coap_response_t
message_handler(struct coap_context_t *ctx UNUSED_PARAM,
coap_session_t *session,
coap_session_t *session UNUSED_PARAM,
coap_pdu_t *sent,
coap_pdu_t *received,
const coap_tid_t id UNUSED_PARAM) {
@@ -339,14 +339,16 @@ message_handler(struct coap_context_t *ctx UNUSED_PARAM,
if (!check_token(received)) {
/* drop if this was just some message, or send RST in case of notification */
if (!sent && (received->type == COAP_MESSAGE_CON ||
received->type == COAP_MESSAGE_NON))
coap_send_rst(session, received);
return;
received->type == COAP_MESSAGE_NON)) {
/* Cause a CoAP RST to be sent */
return COAP_RESPONSE_FAIL;
}
return COAP_RESPONSE_OK;
}
if (received->type == COAP_MESSAGE_RST) {
coap_log(LOG_INFO, "got RST\n");
return;
return COAP_RESPONSE_OK;
}
/* output the received data, if any */
@@ -387,7 +389,7 @@ message_handler(struct coap_context_t *ctx UNUSED_PARAM,
else {
doing_getting_block = 0;
}
return;
return COAP_RESPONSE_OK;
}
} else { /* no 2.05 */
@@ -410,7 +412,7 @@ message_handler(struct coap_context_t *ctx UNUSED_PARAM,
/* our job is done, we can exit at any time */
ready = doing_observe ? coap_check_option(received,
COAP_OPTION_OBSERVE, &opt_iter) == NULL : 1;
return;
return COAP_RESPONSE_OK;
}
static void
+6 -6
View File
@@ -1541,7 +1541,7 @@ proxy_event_handler(coap_context_t *ctx UNUSED_PARAM,
return 0;
}
static void
static coap_response_t
proxy_message_handler(struct coap_context_t *ctx UNUSED_PARAM,
coap_session_t *session,
coap_pdu_t *sent UNUSED_PARAM,
@@ -1572,7 +1572,7 @@ proxy_message_handler(struct coap_context_t *ctx UNUSED_PARAM,
}
if (i == proxy_list_count) {
coap_log(LOG_DEBUG, "Unknown proxy ongoing session response received\n");
return;
return COAP_RESPONSE_OK;
}
coap_log(LOG_DEBUG, "** process upstream incoming %d.%02d response:\n",
@@ -1590,10 +1590,10 @@ proxy_message_handler(struct coap_context_t *ctx UNUSED_PARAM,
size, data, offset, total);
if (!proxy_entry->body_data) {
coap_log(LOG_DEBUG, "body build memory error\n");
return;
return COAP_RESPONSE_OK;
}
if (offset + size != total)
return;
return COAP_RESPONSE_OK;
data = proxy_entry->body_data->s;
size = proxy_entry->body_data->length;
}
@@ -1606,7 +1606,7 @@ proxy_message_handler(struct coap_context_t *ctx UNUSED_PARAM,
coap_session_max_pdu_size(incoming));
if (!pdu) {
coap_log(LOG_DEBUG, "Failed to create ongoing proxy response PDU\n");
return;
return COAP_RESPONSE_OK;
}
if (!coap_add_token(pdu, received->token_length, received->token)) {
@@ -1659,7 +1659,7 @@ proxy_message_handler(struct coap_context_t *ctx UNUSED_PARAM,
coap_show_pdu(LOG_INFO, pdu);
coap_send_large(incoming, pdu);
return;
return COAP_RESPONSE_OK;
}
static void