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.
This commit is contained in:
Olaf Bergmann committed 2016-02-26 18:26:03 +01:00
1 parent 2d6f52686c
commit efa64153a8
11 files changed
+131 -84

No files matched your search

+6 -70
View File
@@ -1,7 +1,7 @@
/*
* prng.h -- Pseudo Random Numbers
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2010-2011,2016 Olaf Bergmann <bergmann@tzi.org>
*
* 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 <stddef.h>
/**
* @defgroup prng Pseudo Random Numbers
* @{
*/
#if defined(WITH_POSIX) || (defined(WITH_LWIP) && !defined(LWIP_RAND))
#include <stdlib.h>
/**
* 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 <string.h>
/**
* 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);
/** @} */