diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2019-04-03 10:09:02 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2019-04-03 10:09:02 +0200 |
commit | b66f10141b1062865bc7fa89c5e4ff0664665269 (patch) | |
tree | 8aa864545e7903c103c7d0c6ff3f799da81b88d0 /src | |
parent | f615ca91176a486b75bd3b060e4e264fb470c755 (diff) | |
parent | 1c04661dc9b11c6506e96a21e79b3587b4038a28 (diff) |
Merge branch 'master' into release
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/ptunnel.c | 3 | ||||
-rw-r--r-- | src/utils.c | 29 |
3 files changed, 28 insertions, 10 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 1d161a2..8d4787a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,10 +20,10 @@ endif if HAVE_ARC4RANDOM ptunnel_ng_CFLAGS += -DHAVE_ARC4RANDOM=1 -else -if HAVE_RANDOM -ptunnel_ng_CFLAGS += -DHAVE_RANDOM=1 endif + +if USE_CUSTOMRNG +ptunnel_ng_CFLAGS += -DUSE_CUSTOMRNG=1 endif ptunnel_ng_SOURCES = \ diff --git a/src/ptunnel.c b/src/ptunnel.c index 4463077..01e0ef5 100644 --- a/src/ptunnel.c +++ b/src/ptunnel.c @@ -136,6 +136,9 @@ int main(int argc, char *argv[]) { if (parse_options(argc, argv)) return -1; + /* Init ptunnel RNG */ + pt_random(); + #ifdef HAVE_PCAP if (opts.pcap && opts.udp) { pt_log(kLog_error, "Packet capture is not supported (or needed) when using UDP for transport.\n"); diff --git a/src/utils.c b/src/utils.c index 6188543..a65f947 100644 --- a/src/utils.c +++ b/src/utils.c @@ -53,12 +53,12 @@ #include <stdarg.h> #include <string.h> #include <time.h> -#include <assert.h> #ifdef HAVE_BSD_STDLIB_H #include <bsd/stdlib.h> #endif #ifndef WIN32 +#include <errno.h> #include <syslog.h> #include <sys/types.h> #include <sys/stat.h> @@ -97,7 +97,7 @@ void pt_log(int level, const char *fmt, ...) { } else #endif /* !WIN32 */ - fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args); + fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args); va_end(args); #ifndef WIN32 if (opts.log_file != stdout && !opts.use_syslog) @@ -160,13 +160,28 @@ int pt_random(void) { #ifdef HAVE_ARC4RANDOM return arc4random(); #else -#if defined(HAVE_RANDOM) && !defined(_WIN32) +#if defined(USE_CUSTOMRNG) && !defined(_WIN32) static int rng_fd = -1; + ssize_t bytes_read; int rnd_val; - if (rng_fd < 0) - rng_fd = open("/dev/random", O_RDONLY); - assert(rng_fd >= 0); - assert( read(rng_fd, &rnd_val, sizeof rnd_val) == sizeof rnd_val ); + if (rng_fd < 0) { + rng_fd = open(RNGDEV, O_RDONLY); + if (rng_fd < 0) { + pt_log(kLog_error, "FATAL: Could not open random device '%s': %s\n", + RNGDEV, strerror(errno)); + exit(EXIT_FAILURE); + } + } + bytes_read = read(rng_fd, &rnd_val, sizeof rnd_val); + if (bytes_read != sizeof rnd_val) { + if (bytes_read < 0) + pt_log(kLog_error, "FATAL: Read from random device failed: %s\n", + strerror(errno)); + else + pt_log(kLog_error, "FATAL: Read only %zd bytes (wanted %zd bytes)\n", + bytes_read, sizeof rnd_val); + exit(EXIT_FAILURE); + } return rnd_val; #else srand(time(0)); |