diff --git a/doc/APIReference.md b/doc/APIReference.md index 820d3b81..067f1b53 100644 --- a/doc/APIReference.md +++ b/doc/APIReference.md @@ -28,6 +28,7 @@ LibHTTP is often used as HTTP and HTTPS library inside a larger application. A * [`httplib_get_context( conn );`](api/httplib_get_context.md) * [`httplib_get_builtin_mime_type( file_name );`](api/httplib_get_builtin_mime_type.md) * [`httplib_get_option( ctx, name );`](api/httplib_get_option.md) +* [`httplib_get_random();`](api/httplib_get_random.md) * [`httplib_get_response_code_text( conn, response_code );`](api/httplib_get_response_code_text.md) * [`httplib_get_server_ports( ctx, size, ports );`](api/httplib_get_server_ports.md) * [`httplib_get_user_data( ctx );`](api/httplib_get_user_data.md) diff --git a/doc/api/httplib_get_random.md b/doc/api/httplib_get_random.md new file mode 100644 index 00000000..99f2686f --- /dev/null +++ b/doc/api/httplib_get_random.md @@ -0,0 +1,19 @@ +# LibHTTP API Reference + +### `httplib_get_random();` + +### Parameters + +*none* + +### Return Value + +| Type | Description | +| :--- | :--- | +|`uint64_t`| A 64 bit pseudo random value| + +### Description + +The function `httplib_get_random()` returns a 64 bit wide pseudo random value. The calculation uses a mix of two random generator functions and the volatile part of a high speed timer value, which makes the result of the function useable in many situations. The implementation is independent of pseudo random number generators provided by the operating system or compiler run time library and will therefore give a consistent performance independent on the platform used. + +### See Also diff --git a/include/libhttp.h b/include/libhttp.h index ba685992..17b9f776 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -987,6 +987,7 @@ LIBHTTP_API int httplib_atomic_dec( volatile int *addr ); LIBHTTP_API int httplib_atomic_inc( volatile int *addr ); LIBHTTP_API int httplib_base64_encode( const unsigned char *src, int src_len, char *dst, int dst_len ); LIBHTTP_API int httplib_closedir( DIR *dir ); +LIBHTTP_API uint64_t httplib_get_random( void ); LIBHTTP_API int httplib_kill( pid_t pid, int sig_num ); LIBHTTP_API int httplib_mkdir( const char *path, int mode ); LIBHTTP_API DIR * httplib_opendir( const char *name ); diff --git a/src/httplib_get_random.c b/src/httplib_get_random.c index 3a8d9fa2..c71bc418 100644 --- a/src/httplib_get_random.c +++ b/src/httplib_get_random.c @@ -29,14 +29,14 @@ #include "httplib_utils.h" /* - * uint64_t XX_httplib_get_random( void ); + * uint64_t httplib_get_random( void ); * - * The function XX_httplib_get_random() is a pseudo random generator which + * The function httplib_get_random() is a pseudo random generator which * combines two high resolution random generators and the nano second part * of the time to generate 64 bit random numbers. */ -uint64_t XX_httplib_get_random( void ) { +LIBHTTP_API uint64_t httplib_get_random( void ) { static uint64_t lfsr = 0; /* Linear feedback shift register */ static uint64_t lcg = 0; /* Linear congruential generator */ @@ -73,4 +73,4 @@ uint64_t XX_httplib_get_random( void ) { return (lfsr ^ lcg ^ (uint64_t)now.tv_nsec); -} /* XX_httplib_get_random */ +} /* httplib_get_random */ diff --git a/src/httplib_start.c b/src/httplib_start.c index 577db8c0..b2711348 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -65,7 +65,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks * Random number generator will initialize at the first call */ - ctx->auth_nonce_mask = (uint64_t)XX_httplib_get_random() ^ (uint64_t)(ptrdiff_t)(options); + ctx->auth_nonce_mask = httplib_get_random() ^ (uint64_t)(ptrdiff_t)(options); if ( httplib_atomic_inc( & XX_httplib_sTlsInit ) == 1 ) { diff --git a/src/httplib_utils.h b/src/httplib_utils.h index 2f9240f1..a0515af0 100644 --- a/src/httplib_utils.h +++ b/src/httplib_utils.h @@ -24,7 +24,6 @@ void XX_httplib_addenv( struct cgi_environment *env, PRINTF_FORMAT_STRING(const char *fmt), ... ) PRINTF_ARGS(2, 3); double XX_httplib_difftimespec( const struct timespec *ts_now, const struct timespec *ts_before ); -uint64_t XX_httplib_get_random( void ); void XX_httplib_gmt_time_string( char *buf, size_t buf_len, time_t *t ); int XX_httplib_inet_pton( int af, const char *src, void *dst, size_t dstlen ); int XX_httplib_lowercase( const char *s ); diff --git a/src/httplib_websocket_client_write.c b/src/httplib_websocket_client_write.c index 9ca4708d..0c8b1cd9 100644 --- a/src/httplib_websocket_client_write.c +++ b/src/httplib_websocket_client_write.c @@ -49,7 +49,7 @@ int httplib_websocket_client_write( struct httplib_connection *conn, int opcode, retval = -1; masked_data = httplib_malloc( ((dataLen + 7) / 4) * 4 ); - masking_key = (uint32_t) XX_httplib_get_random(); + masking_key = (uint32_t) httplib_get_random(); if ( masked_data == NULL ) {