1
1

Return random ascii-string in make_cookie. (#582)

Having hostname and microsecond timestamp in the cookie is not
necessary.  Also fill test buffer with data from /dev/urandom
instead of using random().
Этот коммит содержится в:
Sami Farin 2017-05-21 21:30:18 +03:00 коммит произвёл Bruce A. Mah
родитель eae9391ffe
Коммит e7ab564cb5
3 изменённых файлов: 45 добавлений и 19 удалений

Просмотреть файл

@ -3169,9 +3169,6 @@ iperf_new_stream(struct iperf_test *test, int s)
free(sp); free(sp);
return NULL; return NULL;
} }
srandom(time(NULL));
for (i = 0; i < test->settings->blksize; ++i)
sp->buffer[i] = random();
/* Set socket */ /* Set socket */
sp->socket = s; sp->socket = s;
@ -3196,7 +3193,8 @@ iperf_new_stream(struct iperf_test *test, int s)
sp->diskfile_fd = -1; sp->diskfile_fd = -1;
/* Initialize stream */ /* Initialize stream */
if (iperf_init_stream(sp, test) < 0) { if ((readentropy(sp->buffer, test->settings->blksize) < 0) ||
(iperf_init_stream(sp, test) < 0)) {
close(sp->buffer_fd); close(sp->buffer_fd);
munmap(sp->buffer, sp->test->settings->blksize); munmap(sp->buffer, sp->test->settings->blksize);
free(sp->result); free(sp->result);

Просмотреть файл

@ -45,6 +45,37 @@
#include <errno.h> #include <errno.h>
#include "cjson.h" #include "cjson.h"
#include "iperf.h"
#include "iperf_api.h"
/*
* Read entropy from /dev/urandom
* Errors are fatal.
* Returns 0 on success.
*/
int readentropy(void *out, size_t outsize)
{
static FILE *frandom;
static const char rndfile[] = "/dev/urandom";
if (!outsize) return 0;
if (frandom == NULL) {
frandom = fopen(rndfile, "rb");
if (frandom == NULL) {
iperf_errexit(NULL, "error - failed to open %s: %s\n",
rndfile, strerror(errno));
}
setbuf(frandom, NULL);
}
if (fread(out, 1, outsize, frandom) != outsize) {
iperf_errexit(NULL, "error - failed to read %s: %s\n",
rndfile,
feof(frandom) ? "EOF" : strerror(errno));
}
return 0;
}
/* make_cookie /* make_cookie
* *
@ -53,27 +84,21 @@
* Iperf uses this function to create test "cookies" which * Iperf uses this function to create test "cookies" which
* server as unique test identifiers. These cookies are also * server as unique test identifiers. These cookies are also
* used for the authentication of stream connections. * used for the authentication of stream connections.
* Assumes cookie has size (COOKIE_SIZE + 1) char's.
*/ */
void void
make_cookie(char *cookie) make_cookie(char *cookie)
{ {
static int randomized = 0; unsigned char *out = (unsigned char*)cookie;
char hostname[500]; size_t pos;
struct timeval tv; static const unsigned char rndchars[] = "abcdefghijklmnopqrstuvwxyz234567";
char temp[1000];
if ( ! randomized ) readentropy(out, COOKIE_SIZE);
srandom((int) time(0) ^ getpid()); for (pos = 0; pos < (COOKIE_SIZE - 1); pos++) {
out[pos] = rndchars[out[pos] % (sizeof(rndchars) - 1)];
/* Generate a string based on hostname, time, randomness, and filler. */ }
(void) gethostname(hostname, sizeof(hostname)); out[pos] = '\0';
(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");
/* Now truncate it to 36 bytes and terminate. */
memcpy(cookie, temp, 36);
cookie[36] = '\0';
} }

Просмотреть файл

@ -29,6 +29,9 @@
#include "cjson.h" #include "cjson.h"
#include <sys/select.h> #include <sys/select.h>
#include <stddef.h>
int readentropy(void *out, size_t outsize);
void make_cookie(char *); void make_cookie(char *);