cURL / Mailing Lists / curl-library / Single Mail


Re: round-robin DNS crap

From: Daniel Stenberg <>
Date: Sat, 14 Jul 2007 00:26:48 +0200 (CEST)

On Fri, 13 Jul 2007, Tor Arntsen wrote:

> I think the idea is sound. That wget patch includes a new option --random,
> but if something like this gets implemented in libcurl we would maybe not
> want this a an option at all: default should be to randomize. Maybe we
> could have an option to turn it off though.

A quick and simple "randomizer" that works fine in my test case app is
included below.

It struck me that perhaps the most annoying part of this hack is that when we
create a new list with a different sort order, we also break the ability to
use freeaddrinfo() on the "base" pointer and I'm even a bit worried that even
if we save the base one and use that for freeing the data, libc itself might
use the ai_next fields and thus go nuts if we modified them...

Meaning: Pain! And I need to figure out a nice way to deal with this.

struct addrinfo *scramble(struct addrinfo *ai)
   struct addrinfo *list[20];
   int count;
   int i;
   struct addrinfo *aip;

   /* count number of entries, and store a pointer to each struct */
   for(count=0, aip = ai; aip && count < 20; aip=aip->ai_next, count++)

   if(count < 2)
     /* nothing to work with */
     return ai;

   /* scramble the array by switching array entries */
   for(i=0; i<count/2; i++) {
     int second = i + (random() % (count - i));
     aip = list[i]; /* temp storage */

     /* swap places */
     list[i] = list[second];
     list[second] = aip;

   /* build a new linked list based on the current array order */
   for(i=0; i<count; i++) {
     aip->ai_next = (i==count-1)?NULL:list[i+1]; /* last one gets NULL */
   return list[0];

  Commercial curl and libcurl Technical Support:
Received on 2007-07-14