1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-07 16:02:55 +03:00

Made httplib_get_random public function

This commit is contained in:
Lammert Bies
2016-12-26 01:31:47 +01:00
parent 074e9a9a8c
commit 928247e542
7 changed files with 27 additions and 7 deletions

View File

@@ -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_context( conn );`](api/httplib_get_context.md)
* [`httplib_get_builtin_mime_type( file_name );`](api/httplib_get_builtin_mime_type.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_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_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_server_ports( ctx, size, ports );`](api/httplib_get_server_ports.md)
* [`httplib_get_user_data( ctx );`](api/httplib_get_user_data.md) * [`httplib_get_user_data( ctx );`](api/httplib_get_user_data.md)

View File

@@ -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

View File

@@ -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_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_base64_encode( const unsigned char *src, int src_len, char *dst, int dst_len );
LIBHTTP_API int httplib_closedir( DIR *dir ); 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_kill( pid_t pid, int sig_num );
LIBHTTP_API int httplib_mkdir( const char *path, int mode ); LIBHTTP_API int httplib_mkdir( const char *path, int mode );
LIBHTTP_API DIR * httplib_opendir( const char *name ); LIBHTTP_API DIR * httplib_opendir( const char *name );

View File

@@ -29,14 +29,14 @@
#include "httplib_utils.h" #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 * combines two high resolution random generators and the nano second part
* of the time to generate 64 bit random numbers. * 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 lfsr = 0; /* Linear feedback shift register */
static uint64_t lcg = 0; /* Linear congruential generator */ 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); return (lfsr ^ lcg ^ (uint64_t)now.tv_nsec);
} /* XX_httplib_get_random */ } /* httplib_get_random */

View File

@@ -65,7 +65,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks
* Random number generator will initialize at the first call * 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 ) { if ( httplib_atomic_inc( & XX_httplib_sTlsInit ) == 1 ) {

View File

@@ -24,7 +24,6 @@
void XX_httplib_addenv( struct cgi_environment *env, PRINTF_FORMAT_STRING(const char *fmt), ... ) PRINTF_ARGS(2, 3); 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 ); 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 ); 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_inet_pton( int af, const char *src, void *dst, size_t dstlen );
int XX_httplib_lowercase( const char *s ); int XX_httplib_lowercase( const char *s );

View File

@@ -49,7 +49,7 @@ int httplib_websocket_client_write( struct httplib_connection *conn, int opcode,
retval = -1; retval = -1;
masked_data = httplib_malloc( ((dataLen + 7) / 4) * 4 ); 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 ) { if ( masked_data == NULL ) {