Package: irssi
Version: 0.8.10
Severity: normal
Tags: patch

When connecting to a network by using a DNS hostname that has more than
one IP address ("a rotation"), irssi always picks the same host from
that rotation over multiple connects and reconnects.

The problem was thought to be fixed with r4077 "When looking up IP
addresses, return random IP instead of the first one." but it turns out
the issue still exists.

|       /* if there are multiple addresses, return random one */
|       use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4;
|       use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6;

The problem is that irssi does never initialize the random number
generator, and that it appears to fork for DNS resolving.  Anyway, the
random number generator always is in the same (unitialized) state when
asked to pick a random entry from the table.

The enclosed patch fixes the issue by calling srand(time(NULL)) before
rand().  I realize that time(NULL) is not very random, but we don't need
good randomness and rand() wouldn't provide that anyway.

Cheers,
Peter
-- 
                           |  .''`.  ** Debian GNU/Linux **
      Peter Palfrader      | : :' :      The  universal
 http://www.palfrader.org/ | `. `'      Operating System
                           |   `-    http://www.debian.org/
Index: src/core/network.c
===================================================================
--- src/core/network.c  (revision 4285)
+++ src/core/network.c  (working copy)
@@ -408,6 +408,7 @@
        union sockaddr_union *so;
        struct addrinfo hints, *ai, *ailist;
        int ret, count_v4, count_v6, use_v4, use_v6;
+       static int rand_initialized = 0;
 #else
        struct hostent *hp;
        int count;
@@ -439,6 +440,10 @@
        if (count_v4 == 0 && count_v6 == 0)
                return HOST_NOT_FOUND; /* shouldn't happen? */
 
+       if (!rand_initialized) {
+               rand_initialized = 1;
+               srand(time(NULL));
+       }
        /* if there are multiple addresses, return random one */
        use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4;
        use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6;

Reply via email to