diff --git a/include/coap/prng.h b/include/coap/prng.h index 373dc178..1ed08055 100644 --- a/include/coap/prng.h +++ b/include/coap/prng.h @@ -61,6 +61,30 @@ lwip_prng_impl(unsigned char *buf, size_t len) { #define prng(Buf,Length) lwip_prng_impl((Buf), (Length)) #define prng_init(Value) +#elif defined(_WIN32) +#define prng_init(Value) +errno_t __cdecl rand_s( _Out_ unsigned int* _RandomValue ); + /** + * 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. + */ +COAP_STATIC_INLINE int +coap_prng_impl( unsigned char *buf, size_t len ) { + while ( len != 0 ) { + uint32_t r = 0; + size_t i; + if ( rand_s( &r ) != 0 ) + return 0; + for ( i = 0; i < len && i < 4; i++ ) { + *buf++ = (uint8_t)r; + r >>= 8; + } + len -= i; + } + return 1; +} + #else #include diff --git a/src/net.c b/src/net.c index 2a2ae2d2..90f91614 100644 --- a/src/net.c +++ b/src/net.c @@ -353,15 +353,13 @@ coap_new_context( #endif /* not WITH_CONTIKI */ coap_clock_init(); -#ifdef WITH_LWIP +#if defined(WITH_LWIP) prng_init(LWIP_RAND()); -#endif /* WITH_LWIP */ -#ifdef WITH_CONTIKI +#elif defined(WITH_CONTIKI) prng_init((ptrdiff_t)listen_addr ^ clock_offset); -#endif /* WITH_LWIP */ -#ifdef WITH_POSIX +#elif !defined(_WIN32) prng_init((unsigned long)listen_addr ^ clock_offset); -#endif /* WITH_POSIX */ +#endif #ifndef WITH_CONTIKI if (!c) {