From efa64153a88efe938a7c6f22dee398fef85f6cb2 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 24 Feb 2016 14:28:42 +0100 Subject: [PATCH] prng.c: move platform-specific PRNG invocation into separate files This change introduces a new subtree in the library's directory structure. Platform-specific code now should be placed into the corresponding directory in src/platform/. To illustrate this, prng.h has been split into three distinct files, one for each platform (contiki, lwip, posix) and moved into the respective subdirectory. --- Makefile.am | 3 +- Makefile.libcoap | 4 +- configure.ac | 6 +++ examples/lwip/Makefile | 4 +- include/coap/prng.h | 76 +++---------------------------------- libcoap-1.map | 2 + libcoap-1.sym | 2 + src/net.c | 15 +++----- src/platform/contiki/prng.c | 33 ++++++++++++++++ src/platform/lwip/prng.c | 45 ++++++++++++++++++++++ src/platform/posix/prng.c | 25 ++++++++++++ 11 files changed, 131 insertions(+), 84 deletions(-) create mode 100644 src/platform/contiki/prng.c create mode 100644 src/platform/lwip/prng.c create mode 100644 src/platform/posix/prng.c diff --git a/Makefile.am b/Makefile.am index d6892cdb..c394d614 100644 --- a/Makefile.am +++ b/Makefile.am @@ -63,7 +63,8 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \ src/resource.c \ src/str.c \ src/subscribe.c \ - src/uri.c + src/uri.c \ + src/platform/@PLATFORM@/prng.c ## Define the list of public header files and their install location. ## The list defined here will be used within the include/Makefile.am diff --git a/Makefile.libcoap b/Makefile.libcoap index 122e0faa..99288f16 100644 --- a/Makefile.libcoap +++ b/Makefile.libcoap @@ -1,7 +1,7 @@ -libcoap_src = pdu.c net.c debug.c encode.c uri.c subscribe.c resource.c hashkey.c str.c option.c async.c block.c mem.c coap_io.c +libcoap_src = pdu.c net.c debug.c encode.c uri.c subscribe.c resource.c hashkey.c str.c option.c async.c block.c mem.c coap_io.c prng.c libcoap_dir := $(filter %libcoap,$(APPDS)) -vpath %c $(libcoap_dir)/src +vpath %c $(libcoap_dir)/src $(libcoap_dir)/src/platform/contiki # set include path for coap sources CFLAGS += -I$(libcoap_dir)/include/coap -DCOAP_RESOURCES_NOHASH diff --git a/configure.ac b/configure.ac index 31158d85..77a1345e 100644 --- a/configure.ac +++ b/configure.ac @@ -296,6 +296,7 @@ case $host in *-linux* | *-uclinux*) AC_MSG_RESULT([Linux]) ADDITIONAL_CFLAGS="-D_GNU_SOURCE -DWITH_POSIX" + PLATFORM=posix # Not yet needed but if some code definitions have to depend on the platform. #AC_DEFINE(OS_LINUX, 1, [Linux backend]) @@ -308,11 +309,13 @@ case $host in # set __EXTENSION__ to shut up feature test macros that restrict -std=c99 # to only C99 (and nothing newer) ADDITIONAL_CFLAGS="-D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=600 -D__EXTENSIONS__=1 -DWITH_POSIX" + PLATFORM=posix ;; *-darwin*) AC_MSG_RESULT([Darwin]) ADDITIONAL_CFLAGS="-D_GNU_SOURCE -DWITH_POSIX" + PLATFORM=posix AC_DEFINE(__APPLE_USE_RFC_3542, 1, [Define this to 1 for ancillary data on MacOS]) @@ -326,6 +329,9 @@ case $host in AC_MSG_ERROR([==> If you can provide patches to support your operating system please write to 'libcoap-developers@lists.sourceforge.net'.]) esac +# Export platform definition +AC_SUBST(PLATFORM) + # Exporting the PREDEFINED_CFLAGS definition PREDEFINED_CFLAGS=`echo $ADDITIONAL_CFLAGS | sed -e 's/-D//g'` AC_SUBST(PREDEFINED_CFLAGS) diff --git a/examples/lwip/Makefile b/examples/lwip/Makefile index 26b07247..47a68621 100644 --- a/examples/lwip/Makefile +++ b/examples/lwip/Makefile @@ -26,9 +26,9 @@ CFLAGS += -std=gnu99 CFLAGS += -I$(top_srcdir)/include/coap -vpath %.c $(top_srcdir)/src +vpath %.c $(top_srcdir)/src $(top_srcdir)/src/platform/lwip -COAPOBJS = net.o debug.o option.o resource.o hashkey.o pdu.o encode.o subscribe.o coap_io_lwip.o block.o uri.o +COAPOBJS = net.o debug.o option.o resource.o hashkey.o pdu.o encode.o subscribe.o coap_io_lwip.o block.o uri.o prng.o CFLAGS += -g3 -Wall -pedantic -Dopenbsd -DIPv4 -O0 -fpack-struct diff --git a/include/coap/prng.h b/include/coap/prng.h index da6d9534..3746159e 100644 --- a/include/coap/prng.h +++ b/include/coap/prng.h @@ -1,7 +1,7 @@ /* * prng.h -- Pseudo Random Numbers * - * Copyright (C) 2010-2011 Olaf Bergmann + * Copyright (C) 2010-2011,2016 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see README for terms * of use. @@ -15,91 +15,27 @@ #ifndef _COAP_PRNG_H_ #define _COAP_PRNG_H_ +#include + /** * @defgroup prng Pseudo Random Numbers * @{ */ -#if defined(WITH_POSIX) || (defined(WITH_LWIP) && !defined(LWIP_RAND)) -#include - /** * Fills \p buf with \p len random bytes. This is the default implementation for * prng(). You might want to change prng() to use a better PRNG on your specific * platform. */ -static inline int -coap_prng_impl(unsigned char *buf, size_t len) { - while (len--) - *buf++ = rand() & 0xFF; - return 1; -} -#endif /* WITH_POSIX */ +int coap_prng(unsigned char *buf, size_t len); -#ifdef WITH_CONTIKI -#include - -/** - * Fills \p buf with \p len random bytes. This is the default implementation for - * prng(). You might want to change prng() to use a better PRNG on your specific - * platform. - */ -static inline int -contiki_prng_impl(unsigned char *buf, size_t len) { - unsigned short v = random_rand(); - while (len > sizeof(v)) { - memcpy(buf, &v, sizeof(v)); - len -= sizeof(v); - buf += sizeof(v); - v = random_rand(); - } - - memcpy(buf, &v, len); - return 1; -} - -#define prng(Buf,Length) contiki_prng_impl((Buf), (Length)) -#define prng_init(Value) random_init((unsigned short)(Value)) -#endif /* WITH_CONTIKI */ - -#if defined(WITH_LWIP) && defined(LWIP_RAND) -static inline int -lwip_prng_impl(unsigned char *buf, size_t len) { - u32_t v = LWIP_RAND(); - while (len > sizeof(v)) { - memcpy(buf, &v, sizeof(v)); - len -= sizeof(v); - buf += sizeof(v); - v = LWIP_RAND(); - } - - memcpy(buf, &v, len); - return 1; -} - -#define prng(Buf,Length) lwip_prng_impl((Buf), (Length)) -#define prng_init(Value) - -#endif /* WITH_LWIP */ - -#ifndef prng -/** - * Fills \p Buf with \p Length bytes of random data. - * - * @hideinitializer - */ -#define prng(Buf,Length) coap_prng_impl((Buf), (Length)) -#endif - -#ifndef prng_init /** * Called to set the PRNG seed. You may want to re-define this to allow for a * better PRNG. * - * @hideinitializer + * @param seed Seed for platform-specific PRNG. */ -#define prng_init(Value) srand((unsigned long)(Value)) -#endif +void coap_prng_init(void *seed); /** @} */ diff --git a/libcoap-1.map b/libcoap-1.map index b065371d..bf67af3c 100644 --- a/libcoap-1.map +++ b/libcoap-1.map @@ -86,6 +86,8 @@ global: coap_print_addr; coap_print_link; coap_print_wellknown; + coap_prng; + coap_prng_init; coap_read; coap_register_async; coap_remove_async; diff --git a/libcoap-1.sym b/libcoap-1.sym index 7f465feb..15107525 100644 --- a/libcoap-1.sym +++ b/libcoap-1.sym @@ -84,6 +84,8 @@ coap_pop_next coap_print_addr coap_print_link coap_print_wellknown +coap_prng +coap_prng_init coap_read coap_register_async coap_remove_async diff --git a/src/net.c b/src/net.c index 19762199..98173113 100644 --- a/src/net.c +++ b/src/net.c @@ -349,14 +349,11 @@ coap_new_context( coap_clock_init(); #ifdef WITH_LWIP - prng_init(LWIP_RAND()); + coap_prng_init(LWIP_RAND()); #endif /* WITH_LWIP */ -#ifdef WITH_CONTIKI - prng_init((ptrdiff_t)listen_addr ^ clock_offset); -#endif /* WITH_LWIP */ -#ifdef WITH_POSIX - prng_init((unsigned long)listen_addr ^ clock_offset); -#endif /* WITH_POSIX */ +#if defined (WITH_CONTIKI) || defined (WITH_POSIX) + coap_prng_init((void *)((unsigned long)listen_addr ^ clock_offset)); +#endif /* WITH_CONTIKI || WITH_POSIX */ #ifndef WITH_CONTIKI if (!c) { @@ -377,7 +374,7 @@ coap_new_context( memset(c, 0, sizeof( coap_context_t ) ); /* initialize message id */ - prng((unsigned char *)&c->message_id, sizeof(unsigned short)); + coap_prng((unsigned char *)&c->message_id, sizeof(unsigned short)); c->endpoint = coap_new_endpoint(listen_addr, COAP_ENDPOINT_NOSEC); #ifdef WITH_LWIP @@ -720,7 +717,7 @@ coap_send_confirmed(coap_context_t *context, return COAP_INVALID_TID; } - prng((unsigned char *)&r,sizeof(r)); + coap_prng((unsigned char *)&r,sizeof(r)); /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ node->timeout = calc_timeout(r); diff --git a/src/platform/contiki/prng.c b/src/platform/contiki/prng.c new file mode 100644 index 00000000..a14c7b38 --- /dev/null +++ b/src/platform/contiki/prng.c @@ -0,0 +1,33 @@ +/* + * prng.h -- Pseudo Random Numbers + * + * Copyright (C) 2010-2011,2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#include + +#include + +#include "prng.h" + +int +coap_prng(unsigned char *buf, size_t len) { + unsigned short v = random_rand(); + while (len > sizeof(v)) { + memcpy(buf, &v, sizeof(v)); + len -= sizeof(v); + buf += sizeof(v); + v = random_rand(); + } + + memcpy(buf, &v, len); + return 1; +} + +void +coap_prng_init(void *seed) { + random_init((unsigned short)seed); +} diff --git a/src/platform/lwip/prng.c b/src/platform/lwip/prng.c new file mode 100644 index 00000000..a5e97bbd --- /dev/null +++ b/src/platform/lwip/prng.c @@ -0,0 +1,45 @@ +/* + * prng.c -- Pseudo Random Numbers + * + * Copyright (C) 2010-2011,2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#include "prng.h" + +#ifdef LWIP_RAND +int +coap_prng(unsigned char *buf, size_t len) { + u32_t v = LWIP_RAND(); + while (len > sizeof(v)) { + memcpy(buf, &v, sizeof(v)); + len -= sizeof(v); + buf += sizeof(v); + v = LWIP_RAND(); + } + + memcpy(buf, &v, len); + return 1; +} + +void +coap_prng_init(void *value) { +} +#else /* LWIP_RAND */ +#include +#include + +int +coap_prng(unsigned char *buf, size_t len) { + while (len--) + *buf++ = rand() & 0xFF; + return 1; +} + +void +coap_prng_init(void *value) { + srand((intptr_t)value); +} +#endif /* LWIP_RAND */ diff --git a/src/platform/posix/prng.c b/src/platform/posix/prng.c new file mode 100644 index 00000000..026019b9 --- /dev/null +++ b/src/platform/posix/prng.c @@ -0,0 +1,25 @@ +/* + * prng.c -- Pseudo Random Numbers + * + * Copyright (C) 2010-2011,2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#include +#include + +#include "prng.h" + +int +coap_prng(unsigned char *buf, size_t len) { + while (len--) + *buf++ = rand() & 0xFF; + return 1; +} + +void +coap_prng_init(void *value) { + srand((intptr_t)value); +}