summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Habets <thomas@habets.se>2019-12-20 14:55:59 +0000
committerThomas Habets <thomas@habets.se>2019-12-20 14:55:59 +0000
commit4a3fba54b11c4c137b7685b7055f061aa982bc13 (patch)
tree0bbb98706d075c1e647d2e17818d42d72a83bd8c
parent24860ea35d94ae53c0c978e0757449b6ede7e623 (diff)
downloadarping-4a3fba54b11c4c137b7685b7055f061aa982bc13.tar.gz
Remove use of random() where possible
-rw-r--r--src/arping.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/arping.c b/src/arping.c
index 2f00da9..1611acb 100644
--- a/src/arping.c
+++ b/src/arping.c
@@ -233,6 +233,26 @@ xgetrandom(void *buf, const size_t buflen, const unsigned int flags)
#endif
}
+static long int
+xrandom() {
+ const int maxtry = 10;
+ for (int c = 0; c < maxtry; c++) {
+ long int ret;
+ const ssize_t rc = xgetrandom(&ret, sizeof(ret), 0);
+ if (rc == -1) {
+ fprintf(stderr, "arping: failed to get random bytes: %s\n", strerror(errno));
+ continue;
+ }
+ if (sizeof(ret) != rc) {
+ fprintf(stderr, "arping: got too few random bytes %d, want %d\n", rc, sizeof(ret));
+ continue;
+ }
+ return ret;
+ }
+ fprintf(stderr, "arping: failed to get random bytes after %d tries\n", maxtry);
+ exit(1);
+}
+
/**
* If possible, chroot.
*
@@ -2213,7 +2233,7 @@ arping_main(int argc, char **argv)
} else { /* PINGMAC */
int c;
for (c = 0; (maxcount < 0 || c < maxcount) && !time_to_die; c++) {
- pingmac_send(random(), c);
+ pingmac_send(xrandom(), c);
const uint32_t w = wait_time(deadline, packetwait);
if (w == 0) {
break;