summaryrefslogtreecommitdiff
path: root/accel-pppd/utils.c
diff options
context:
space:
mode:
authorGuillaume Nault <g.nault@alphalink.fr>2013-04-09 21:43:04 +0200
committerDmitry Kozlov <xeb@mail.ru>2013-04-17 09:06:35 +0400
commitb041db6c60fb80846ad6b6770adc3971e41b2194 (patch)
treed1d2b3b8b5dbae57360357bf8b0ad4064abc01a3 /accel-pppd/utils.c
parent76f30e4e2d664d3c10f988b8d9660b478718c777 (diff)
downloadaccel-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/utils.c')
-rw-r--r--accel-pppd/utils.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/accel-pppd/utils.c b/accel-pppd/utils.c
index a6a76c93..45e8709c 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;
+}