tlsdate: Improve randomized IP selection

Improve randomization code for selecting a random IP for a host.
Make it choose the initial element randomly and iterate
sequentially from there. This should prevent it from selecting
the same (possibly bad) element several times in a row.

Bug: b/185961371
Test: Manually
Change-Id: I0eb8cf5c5d0cbf95f7040b50aef8dcaf513e0225
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/tlsdate/+/2859898
Tested-by: Michael Ershov <miersh@google.com>
Commit-Queue: Michael Ershov <miersh@google.com>
Reviewed-by: Mattias Nissler <mnissler@chromium.org>
Reviewed-by: Pavol Marko <pmarko@chromium.org>
diff --git a/src/tlsdate-helper.c b/src/tlsdate-helper.c
index 7db5e3c..ca90e91 100644
--- a/src/tlsdate-helper.c
+++ b/src/tlsdate-helper.c
@@ -240,15 +240,17 @@
   if (err != 0 || !addr_infos)
     die ("getaddrinfo (%s): %s\n", host, gai_strerror (err));
 
+  // tlsdate is killed by a supervisor if it takes too long to finish. So it
+  // may not have time to try all the addresses. Selecting the start point
+  // randomly makes it try different addresses during different attempts, that
+  // is useful when some addresses are not accessible (e.g. first ones).
   int list_length = get_addrinfo_length (addr_infos);
+  int start_index = random () % list_length;
+
   for (int i = 0; i < list_length; ++i)
   {
-    // tlsdate is killed by a supervisor if it takes too long to finish. So it
-    // may not have time to try all the addresses. Selecting randomly makes
-    // it try different addresses during different attempts, that is usefull
-    // when some addresses are not accessible (e.g. first ones).
     struct addrinfo *current_addr_info =
-        get_addrinfo_element (addr_infos, random () % list_length);
+        get_addrinfo_element (addr_infos, (start_index + i) % list_length);
     if (!current_addr_info) {
       die ("attempted to use NULL addrinfo");
     }