From 3c84f5c012358a62ade3ab227168ee17336772e9 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 10 Jun 2015 17:46:53 +0200 Subject: [PATCH] net.c, pdu.h: ensure that valid transaction ids are positive The calculation of transaction ids did not ensure that the result is always greater or equal zero. To avoid conflicts with the error codes returned by coap_send(), the calculation now masks out the lower half. --- include/coap/pdu.h | 9 +++++++-- src/net.c | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/coap/pdu.h b/include/coap/pdu.h index f6d6f441..57b68d09 100644 --- a/include/coap/pdu.h +++ b/include/coap/pdu.h @@ -155,9 +155,14 @@ char *coap_response_phrase(unsigned char code); * use an unallocated type here and hope for the best. */ #define COAP_MEDIATYPE_ANY 0xff /* any media type */ -/* CoAP transaction id */ -/*typedef unsigned short coap_tid_t; */ +/** + * coap_tid_t is used to store CoAP transaction id, i.e. a hash value + * built from the remote transport address and the message id of a + * CoAP PDU. Valid transaction ids are greater or equal zero. + */ typedef int coap_tid_t; + +/** Indicates an invalid transaction id. */ #define COAP_INVALID_TID -1 #ifdef WORDS_BIGENDIAN diff --git a/src/net.c b/src/net.c index db385fe5..bc8e5f86 100644 --- a/src/net.c +++ b/src/net.c @@ -523,7 +523,7 @@ coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_hash((const unsigned char *)&pdu->hdr->id, sizeof(unsigned short), h); - *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]); + *id = (((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3])) & INT_MAX; } coap_tid_t