diff options
author | Guillaume Nault <g.nault@alphalink.fr> | 2013-04-09 21:43:04 +0200 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2013-04-17 09:06:35 +0400 |
commit | b041db6c60fb80846ad6b6770adc3971e41b2194 (patch) | |
tree | d1d2b3b8b5dbae57360357bf8b0ad4064abc01a3 /accel-pppd | |
parent | 76f30e4e2d664d3c10f988b8d9660b478718c777 (diff) | |
download | accel-ppp-b041db6c60fb80846ad6b6770adc3971e41b2194.tar.gz accel-ppp-b041db6c60fb80846ad6b6770adc3971e41b2194.zip |
utils: Add random buffer generation function
Define and export the u_randbuf() function that fills a buffer with
random data.
Convert L2TP's challenge generation code for using it.
Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Diffstat (limited to 'accel-pppd')
-rw-r--r-- | accel-pppd/ctrl/l2tp/l2tp.c | 27 | ||||
-rw-r--r-- | accel-pppd/utils.c | 30 | ||||
-rw-r--r-- | accel-pppd/utils.h | 1 |
3 files changed, 40 insertions, 18 deletions
diff --git a/accel-pppd/ctrl/l2tp/l2tp.c b/accel-pppd/ctrl/l2tp/l2tp.c index 267a47f..c6ba82f 100644 --- a/accel-pppd/ctrl/l2tp/l2tp.c +++ b/accel-pppd/ctrl/l2tp/l2tp.c @@ -223,8 +223,7 @@ static int l2tp_tunnel_genchall(uint16_t chall_len, struct l2tp_packet_t *pack) { void *ptr = NULL; - size_t urandlen; - ssize_t rdlen; + int err; if (chall_len == 0 || conf_secret == NULL || conf_secret_len == 0) { @@ -248,26 +247,18 @@ static int l2tp_tunnel_genchall(uint16_t chall_len, conn->challenge_len = chall_len; } - for (urandlen = 0; urandlen < chall_len; urandlen += rdlen) { - rdlen = read(urandom_fd, conn->challenge + urandlen, - chall_len - urandlen); - if (rdlen < 0) { - if (errno == EINTR) - rdlen = 0; - else { - log_tunnel(log_error, conn, - "impossible to generate Challenge:" - " reading from urandom failed: %s\n", - strerror(errno)); - goto err; - } - } else if (rdlen == 0) { + if (u_randbuf(conn->challenge, chall_len, &err) < 0) { + if (err) + log_tunnel(log_error, conn, + "impossible to generate Challenge:" + " reading from urandom failed: %s\n", + strerror(err)); + else log_tunnel(log_error, conn, "impossible to generate Challenge:" " end of file reached while reading" " from urandom\n"); - goto err; - } + goto err; } if (l2tp_packet_add_octets(pack, Challenge, conn->challenge, diff --git a/accel-pppd/utils.c b/accel-pppd/utils.c index a6a76c9..45e8709 100644 --- a/accel-pppd/utils.c +++ b/accel-pppd/utils.c @@ -1,12 +1,15 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include "triton.h" #include "utils.h" #include "memdebug.h" +extern int urandom_fd; + void __export u_inet_ntoa(in_addr_t addr, char *str) { sprintf(str, "%i.%i.%i.%i", addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff); @@ -30,3 +33,30 @@ int __export u_readlong(long int *dst, const char *src, return 0; } } + +int __export u_randbuf(void *buf, size_t buf_len, int *err) +{ + uint8_t *u8buf = buf; + ssize_t rd_len; + + while (buf_len) { + rd_len = read(urandom_fd, u8buf, buf_len); + if (rd_len < 0) { + if (errno == EINTR) + rd_len = 0; + else { + if (err) + *err = errno; + return -1; + } + } else if (rd_len == 0) { + if (err) + *err = 0; + return -1; + } + u8buf += rd_len; + buf_len -= rd_len; + } + + return 0; +} diff --git a/accel-pppd/utils.h b/accel-pppd/utils.h index bb1a00a..be62f6a 100644 --- a/accel-pppd/utils.h +++ b/accel-pppd/utils.h @@ -5,5 +5,6 @@ void u_inet_ntoa(in_addr_t, char *str); int u_readlong(long int *dst, const char *src, long int min, long int max); +int u_randbuf(void *buf, size_t buf_len, int *err); #endif |