0
0
mirror of https://github.com/darkk/redsocks.git synced 2025-08-26 03:35:30 +00:00

Improve nonce generation: srand() should be called only during startup.

This commit is contained in:
Leonid Evdokimov 2011-02-11 01:28:51 +02:00
parent 8b0a9dcc6c
commit ebaeedead1
3 changed files with 9 additions and 2 deletions

View File

@ -210,7 +210,6 @@ static struct evbuffer *httpc_mkconnect(redsocks_client *client)
/* prepare an random string for cnounce */
char cnounce[17];
srand(time(0));
for (int i = 0; i < 16; i += 4)
sprintf(cnounce + i, "%02x", rand() & 65535);

View File

@ -321,7 +321,6 @@ static void httpr_relay_write_cb(struct bufferevent *buffev, void *_arg)
/* prepare an random string for cnounce */
char cnounce[17];
srand(time(0));
for (int i = 0; i < 16; i += 4)
sprintf(cnounce + i, "%04x", rand() & 65535);

9
main.c
View File

@ -46,6 +46,14 @@ static void terminate(int sig, short what, void *_arg)
log_error(LOG_WARNING, "event_loopbreak");
}
static void red_srand()
{
struct timeval tv;
gettimeofday(&tv, NULL);
// using tv_usec is a bit less predictable than tv_sec
srand(tv.tv_sec*1000000+tv.tv_usec);
}
int main(int argc, char **argv)
{
int error;
@ -56,6 +64,7 @@ int main(int argc, char **argv)
int opt;
int i;
red_srand();
while ((opt = getopt(argc, argv, "tc:p:")) != -1) {
switch (opt) {
case 't':