Skip to content

Commit

Permalink
initialize rand seed with nano-second granularity
Browse files Browse the repository at this point in the history
in scenarios where one is to spin up several processes with the same
proxy list in random mode, all processes started in the same second
would pick the same proxy due to using the same srand() seed.

closes rofl0r#380
  • Loading branch information
rofl0r committed May 14, 2021
1 parent 6af2686 commit 092d704
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ check_compile 'whether we have pipe2() and O_CLOEXEC' "-DHAVE_PIPE2" \
check_compile 'whether we have SOCK_CLOEXEC' "-DHAVE_SOCK_CLOEXEC" \
'#define _GNU_SOURCE\n#include <sys/socket.h>\nint main() {\nreturn socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);}'

check_compile 'whether we have clock_gettime' "-DHAVE_CLOCK_GETTIME" \
'#define _GNU_SOURCE\n#include <time.h>\nint main() {\nstruct timespec now;clock_gettime(CLOCK_REALTIME, &now);\nreturn now.tv_sec ^ now.tv_nsec;}'

check_define __APPLE__ && {
mac_detected=true
check_define __x86_64__ && mac_64=true
Expand Down
12 changes: 11 additions & 1 deletion src/libproxychains.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,18 @@ static void setup_hooks(void) {
static int close_fds[16];
static int close_fds_cnt = 0;

static unsigned get_rand_seed(void) {
#ifdef HAVE_CLOCK_GETTIME
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
return now.tv_sec ^ now.tv_nsec;
#else
return time(NULL);
#endif
}

static void do_init(void) {
srand(time(NULL));
srand(get_rand_seed());
core_initialize();

/* read the config file */
Expand Down

0 comments on commit 092d704

Please sign in to comment.