+void randomized_alarm(int seconds)
+{
+ unsigned char r;
+ RAND_pseudo_bytes(&r, 1);
+ alarm((seconds * (int)r) / 128 + 1);
+}
+
+/* This function is severely fucked up.
+ We want to redesign it so the following rules apply:
+
+ - Try all ConnectTo's in a row:
+ - if a connect() fails, try next one immediately,
+ - if it works, wait 5 seconds or so.
+ - If none of them were succesful, increase delay and retry.
+ - If all were succesful, don't try anymore.
+*/
+
+RETSIGTYPE
+try_outgoing_connections(int a)
+{
+ static config_t *cfg = NULL;
+ static int retry = 0;
+ char *name;
+cp
+ if(!cfg)
+ cfg = lookup_config(config_tree, "ConnectTo");
+
+ if(!cfg)
+ return;
+
+ while(cfg)
+ {
+ get_config_string(cfg, &name);
+ cfg = lookup_config_next(config_tree, cfg); /* Next time skip to next ConnectTo line */
+
+ if(check_id(name))
+ {
+ syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
+ continue;
+ }
+
+ if(setup_outgoing_connection(name)) /* function returns 0 when there are no problems */
+ retry = 1;
+
+ }
+
+ get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout);
+
+ if(retry)
+ {
+ seconds_till_retry += 5;
+ if(seconds_till_retry > maxtimeout) /* Don't wait more than MAXTIMEOUT seconds. */
+ seconds_till_retry = maxtimeout;
+
+ syslog(LOG_ERR, _("Failed to setup all outgoing connections, will retry in %d seconds"),
+ seconds_till_retry);
+
+ /* Randomize timeout to avoid global synchronisation effects */
+ randomized_alarm(seconds_till_retry);
+ }
+ else
+ {
+ seconds_till_retry = 5;
+ }
+cp
+}
+