[libc] Avoid use of random() in third_party/iperf
random() is implemented as i++. Instead, call zx_cprng_draw() to get
randomness, as it would be nice to remove random() from libc.
ZX-3307 #comment [libc] Avoid use of random() in third_party/iperf
Test: CQ
Change-Id: I066f0eca90d61e0900ac8c7e2495cb448918aac6
diff --git a/src/iperf_api.c b/src/iperf_api.c
index a3fe299..635e71c 100755
--- a/src/iperf_api.c
+++ b/src/iperf_api.c
@@ -63,6 +63,8 @@
#include <sys/cpuset.h>
#endif /* HAVE_CPUSET_SETAFFINITY */
+#include <zircon/syscalls.h>
+
#include "net.h"
#include "iperf.h"
#include "iperf_api.h"
@@ -2798,9 +2800,7 @@
free(sp);
return NULL;
}
- srandom(time(NULL));
- for (i = 0; i < test->settings->blksize; ++i)
- sp->buffer[i] = random();
+ zx_cprng_draw(sp->buffer, test->settings->blksize);
/* Set socket */
sp->socket = s;
diff --git a/src/iperf_util.c b/src/iperf_util.c
index f185115..b493ff7 100644
--- a/src/iperf_util.c
+++ b/src/iperf_util.c
@@ -46,6 +46,8 @@
#include <time.h>
#include <errno.h>
+#include <zircon/syscalls.h>
+
#include "cjson.h"
/* make_cookie
@@ -65,13 +67,14 @@
struct timeval tv;
char temp[1000];
- if ( ! randomized )
- srandom((int) time(0) ^ getpid());
+ unsigned long int rand1, rand2;
+ zx_cprng_draw(&rand1, sizeof(rand1));
+ zx_cprng_draw(&rand2, sizeof(rand2));
/* Generate a string based on hostname, time, randomness, and filler. */
(void) gethostname(hostname, sizeof(hostname));
(void) gettimeofday(&tv, 0);
- (void) snprintf(temp, sizeof(temp), "%s.%ld.%06ld.%08lx%08lx.%s", hostname, (unsigned long int) tv.tv_sec, (unsigned long int) tv.tv_usec, (unsigned long int) random(), (unsigned long int) random(), "1234567890123456789012345678901234567890");
+ (void) snprintf(temp, sizeof(temp), "%s.%ld.%06ld.%08lx%08lx.%s", hostname, (unsigned long int) tv.tv_sec, (unsigned long int) tv.tv_usec, rand1, rand2, "1234567890123456789012345678901234567890");
/* Now truncate it to 36 bytes and terminate. */
memcpy(cookie, temp, 36);