rand.c (apr-1.6.5.tar.bz2) | : | rand.c (apr-1.7.0.tar.bz2) | ||
---|---|---|---|---|
skipping to change at line 46 | skipping to change at line 46 | |||
#include <sys/un.h> | #include <sys/un.h> | |||
#endif | #endif | |||
#if defined(HAVE_UUID_H) | #if defined(HAVE_UUID_H) | |||
#include <uuid.h> | #include <uuid.h> | |||
#elif defined(HAVE_UUID_UUID_H) | #elif defined(HAVE_UUID_UUID_H) | |||
#include <uuid/uuid.h> | #include <uuid/uuid.h> | |||
#elif defined(HAVE_SYS_UUID_H) | #elif defined(HAVE_SYS_UUID_H) | |||
#include <sys/uuid.h> | #include <sys/uuid.h> | |||
#endif | #endif | |||
#if defined(SYS_RANDOM) | ||||
#if defined(HAVE_SYS_RANDOM_H) && \ | ||||
defined(HAVE_GETRANDOM) | ||||
#include <sys/random.h> | ||||
#define USE_GETRANDOM | ||||
#elif defined(HAVE_SYS_SYSCALL_H) && \ | ||||
defined(HAVE_LINUX_RANDOM_H) && \ | ||||
defined(HAVE_DECL_SYS_GETRANDOM) && \ | ||||
HAVE_DECL_SYS_GETRANDOM | ||||
#ifndef _GNU_SOURCE | ||||
#define _GNU_SOURCE | ||||
#endif | ||||
#include <unistd.h> | ||||
#include <sys/syscall.h> | ||||
#include <linux/random.h> | ||||
#define getrandom(buf, buflen, flags) \ | ||||
syscall(SYS_getrandom, (buf), (buflen), (flags)) | ||||
#define USE_GETRANDOM | ||||
#endif /* HAVE_SYS_RANDOM_H */ | ||||
#endif /* SYS_RANDOM */ | ||||
#ifndef SHUT_RDWR | #ifndef SHUT_RDWR | |||
#define SHUT_RDWR 2 | #define SHUT_RDWR 2 | |||
#endif | #endif | |||
#if APR_HAS_OS_UUID | #if APR_HAS_OS_UUID | |||
#if defined(HAVE_UUID_CREATE) | #if defined(HAVE_UUID_CREATE) | |||
APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data) | APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data) | |||
{ | { | |||
skipping to change at line 90 | skipping to change at line 115 | |||
} | } | |||
#endif | #endif | |||
#endif /* APR_HAS_OS_UUID */ | #endif /* APR_HAS_OS_UUID */ | |||
#if APR_HAS_RANDOM | #if APR_HAS_RANDOM | |||
APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf, | APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf, | |||
apr_size_t length) | apr_size_t length) | |||
{ | { | |||
#ifdef DEV_RANDOM | #if defined(HAVE_EGD) | |||
int fd = -1; | ||||
/* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then | ||||
* gives EOF, so reading 'length' bytes may require opening the | ||||
* device several times. */ | ||||
do { | ||||
apr_ssize_t rc; | ||||
if (fd == -1) | ||||
if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1) | ||||
return errno; | ||||
do { | ||||
rc = read(fd, buf, length); | ||||
} while (rc == -1 && errno == EINTR); | ||||
if (rc < 0) { | ||||
int errnum = errno; | ||||
close(fd); | ||||
return errnum; | ||||
} | ||||
else if (rc == 0) { | ||||
close(fd); | ||||
fd = -1; /* force open() again */ | ||||
} | ||||
else { | ||||
buf += rc; | ||||
length -= rc; | ||||
} | ||||
} while (length > 0); | ||||
close(fd); | ||||
#elif defined(OS2) | ||||
static UCHAR randbyte(); | ||||
unsigned int idx; | ||||
for (idx=0; idx<length; idx++) | ||||
buf[idx] = randbyte(); | ||||
#elif defined(HAVE_EGD) | ||||
/* use EGD-compatible socket daemon (such as EGD or PRNGd). | /* use EGD-compatible socket daemon (such as EGD or PRNGd). | |||
* message format: | * message format: | |||
* 0x00 (get entropy level) | * 0x00 (get entropy level) | |||
* 0xMM (msb) 0xmm 0xll 0xLL (lsb) | * 0xMM (msb) 0xmm 0xll 0xLL (lsb) | |||
* 0x01 (read entropy nonblocking) 0xNN (bytes requested) | * 0x01 (read entropy nonblocking) 0xNN (bytes requested) | |||
* 0xMM (bytes granted) MM bytes | * 0xMM (bytes granted) MM bytes | |||
* 0x02 (read entropy blocking) 0xNN (bytes desired) | * 0x02 (read entropy blocking) 0xNN (bytes desired) | |||
* [block] NN bytes | * [block] NN bytes | |||
* 0x03 (write entropy) 0xMM 0xLL (bits of entropy) 0xNN (bytes of data) | * 0x03 (write entropy) 0xMM 0xLL (bits of entropy) 0xNN (bytes of data) | |||
* NN bytes | * NN bytes | |||
skipping to change at line 227 | skipping to change at line 211 | |||
close(egd_socket); | close(egd_socket); | |||
} | } | |||
if (length > 0) { | if (length > 0) { | |||
/* We must have iterated through the list of sockets, | /* We must have iterated through the list of sockets, | |||
* and no go. Return the errno. | * and no go. Return the errno. | |||
*/ | */ | |||
return bad_errno; | return bad_errno; | |||
} | } | |||
#elif defined(SYS_RANDOM) && defined(USE_GETRANDOM) | ||||
do { | ||||
int rc; | ||||
rc = getrandom(buf, length, 0); | ||||
if (rc == -1) { | ||||
if (errno == EINTR) { | ||||
continue; | ||||
} | ||||
return errno; | ||||
} | ||||
buf += rc; | ||||
length -= rc; | ||||
} while (length > 0); | ||||
#elif defined(SYS_RANDOM) && defined(HAVE_ARC4RANDOM_BUF) | ||||
arc4random_buf(buf, length); | ||||
#elif defined(DEV_RANDOM) | ||||
int fd = -1; | ||||
/* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then | ||||
* gives EOF, so reading 'length' bytes may require opening the | ||||
* device several times. */ | ||||
do { | ||||
apr_ssize_t rc; | ||||
if (fd == -1) | ||||
if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1) | ||||
return errno; | ||||
do { | ||||
rc = read(fd, buf, length); | ||||
} while (rc == -1 && errno == EINTR); | ||||
if (rc < 0) { | ||||
int errnum = errno; | ||||
close(fd); | ||||
return errnum; | ||||
} | ||||
else if (rc == 0) { | ||||
close(fd); | ||||
fd = -1; /* force open() again */ | ||||
} | ||||
else { | ||||
buf += rc; | ||||
length -= rc; | ||||
} | ||||
} while (length > 0); | ||||
close(fd); | ||||
#elif defined(OS2) | ||||
static UCHAR randbyte(); | ||||
unsigned int idx; | ||||
for (idx=0; idx<length; idx++) | ||||
buf[idx] = randbyte(); | ||||
#elif defined(HAVE_TRUERAND) /* use truerand */ | #elif defined(HAVE_TRUERAND) /* use truerand */ | |||
extern int randbyte(void); /* from the truerand library */ | extern int randbyte(void); /* from the truerand library */ | |||
unsigned int idx; | unsigned int idx; | |||
/* this will increase the startup time of the server, unfortunately... | /* this will increase the startup time of the server, unfortunately... | |||
* (generating 20 bytes takes about 8 seconds) | * (generating 20 bytes takes about 8 seconds) | |||
*/ | */ | |||
for (idx=0; idx<length; idx++) | for (idx=0; idx<length; idx++) | |||
buf[idx] = (unsigned char) randbyte(); | buf[idx] = (unsigned char) randbyte(); | |||
#else | ||||
#error APR_HAS_RANDOM defined with no implementation | ||||
#endif /* DEV_RANDOM */ | #endif /* DEV_RANDOM */ | |||
return APR_SUCCESS; | return APR_SUCCESS; | |||
} | } | |||
#undef STR | #undef STR | |||
#undef XSTR | #undef XSTR | |||
#ifdef OS2 | #ifdef OS2 | |||
#include "randbyte_os2.inc" | #include "randbyte_os2.inc" | |||
End of changes. 4 change blocks. | ||||
42 lines changed or deleted | 94 lines changed or added |