From 5c079b72d93e77a5a49110856e2d34b72f89d727 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 2 Aug 2017 21:36:22 +0200 Subject: [PATCH] coap_session.[hc]: make message id session-local The message id in the CoAP header is used for reliability when transferring a CoAP PDU over a non-reliable transport protocol such as UDP. For message de-duplication, it is useful to have a monotonously increasing number space within a session. Therefore, the context-global value has been moved to coap_session_t. The function coap_new_message_id() now takes a coap_session_t object as its argument. --- examples/client.c | 6 ++++-- examples/coap-server.c | 2 +- include/coap/coap_session.h | 1 + include/coap/net.h | 10 +++++----- src/coap_session.c | 4 ++++ src/resource.c | 2 +- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/client.c b/examples/client.c index 340fb90a..90725708 100644 --- a/examples/client.c +++ b/examples/client.c @@ -148,12 +148,13 @@ coap_new_request(coap_context_t *ctx, size_t length) { coap_pdu_t *pdu; coap_list_t *opt; + (void)ctx; if ( ! ( pdu = coap_new_pdu(session) ) ) return NULL; pdu->hdr->type = msgtype; - pdu->hdr->id = coap_new_message_id(ctx); + pdu->hdr->id = coap_new_message_id(session); pdu->hdr->code = m; pdu->hdr->token_length = (uint16_t)the_token.length; @@ -190,11 +191,12 @@ clear_obs(coap_context_t *ctx, coap_session_t *session) { coap_list_t *option; coap_tid_t tid = COAP_INVALID_TID; unsigned char buf[2]; + (void)ctx; /* create bare PDU w/o any option */ pdu = coap_pdu_init(msgtype, COAP_REQUEST_GET, - coap_new_message_id(ctx), + coap_new_message_id(session), coap_session_max_pdu_size(session)); if (!pdu) { diff --git a/examples/coap-server.c b/examples/coap-server.c index e69c4ce9..38486cff 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -266,7 +266,7 @@ check_async(coap_context_t *ctx, return; } - response->hdr->id = coap_new_message_id(ctx); + response->hdr->id = coap_new_message_id(async->session); if (async->tokenlen) coap_add_token(response, async->tokenlen, async->token); diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 405b6aee..b40552b1 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -60,6 +60,7 @@ typedef struct coap_session_t { struct coap_endpoint_t *endpoint; /**< session's endpoint */ struct coap_context_t *context; /**< session's context */ void *tls; /**< security parameters */ + uint16_t tx_mid; /**< the last message id that was used in this session */ struct coap_queue_t *sendqueue; /**< list of messages waiting to be sent */ coap_tick_t last_rx_tx; uint8_t *psk_identity; diff --git a/include/coap/net.h b/include/coap/net.h index 77ef149f..f8f1088f 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -215,13 +215,13 @@ void coap_context_set_psk( coap_context_t *ctx, const char *hint, * * @return Incremented message id in network byte order. */ -COAP_STATIC_INLINE unsigned short -coap_new_message_id(coap_context_t *context) { - context->message_id++; +COAP_STATIC_INLINE uint16_t +coap_new_message_id(coap_session_t *session) { + session->tx_mid++; #ifndef WITH_CONTIKI - return htons(context->message_id); + return htons(session->tx_mid); #else /* WITH_CONTIKI */ - return uip_htons(context->message_id); + return uip_htons(session->tx_mid); #endif } diff --git a/src/coap_session.c b/src/coap_session.c index 00df6d6c..44135155 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -68,6 +68,10 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addre coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n"); } } + + /* initialize message id */ + prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid)); + return session; } diff --git a/src/resource.c b/src/resource.c index 4afee298..fc85ac38 100644 --- a/src/resource.c +++ b/src/resource.c @@ -679,7 +679,7 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r) { token.length = obs->token_length; token.s = obs->token; - response->hdr->id = coap_new_message_id(context); + response->hdr->id = coap_new_message_id(obs->session); if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 && obs->non_cnt < COAP_OBS_MAX_NON) { response->hdr->type = COAP_MESSAGE_NON;