net: coap: Add internal init function to seed message_id

Randomly generating ID the first time coap_next_id() is called is more
in accordance with CoAP recommendations (see
https://tools.ietf.org/html/draft-ietf-core-coap-18, section 4.4)

"It is strongly recommended that the initial value of the
variable (e.g., on startup) be randomized, in order to make successful
off-path attacks on the protocol less likely."

Doing this in a dedicated init function is the cleanest and most
idiomatic approach. This init function is not exposed publically which
means it will be called only once, by the network stack init procedure.

Signed-off-by: Benjamin Lindqvist <benjamin.lindqvist@endian.se>
This commit is contained in:
Benjamin Lindqvist 2019-09-17 13:42:46 +02:00 committed by Jukka Rissanen
commit 81ccbd96c9
4 changed files with 46 additions and 6 deletions

View file

@ -357,12 +357,7 @@ u8_t *coap_next_token(void);
*
* @return a new message id
*/
static inline u16_t coap_next_id(void)
{
static u16_t message_id;
return ++message_id;
}
u16_t coap_next_id(void);
/**
* @brief Return the values associated with the option of value @a