-void try_outgoing_connections(void)
-{
- static config_t *cfg = NULL;
- char *name;
- outgoing_t *outgoing;
-cp
- for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg))
- {
- get_config_string(cfg, &name);
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
- free(name);
- continue;
- }
-
- outgoing = xmalloc_and_zero(sizeof(*outgoing));
- outgoing->name = name;
- setup_outgoing_connection(outgoing);
- }
+static void free_outgoing(outgoing_t *outgoing) {
+ timeout_del(&outgoing->ev);
+
+ if(outgoing->ai)
+ freeaddrinfo(outgoing->ai);
+
+ if(outgoing->config_tree)
+ exit_configuration(&outgoing->config_tree);
+
+ if(outgoing->name)
+ free(outgoing->name);
+
+ free(outgoing);
+}
+
+void try_outgoing_connections(void) {
+ /* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */
+
+ if(!outgoing_list) {
+ outgoing_list = list_alloc((list_action_t)free_outgoing);
+ } else {
+ for list_each(outgoing_t, outgoing, outgoing_list)
+ outgoing->timeout = -1;
+ }
+
+ /* Make sure there is one outgoing_t in the list for each ConnectTo. */
+
+ for(config_t *cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
+ char *name;
+ get_config_string(cfg, &name);
+
+ if(!check_id(name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR,
+ "Invalid name for outgoing connection in %s line %d",
+ cfg->file, cfg->line);
+ free(name);
+ continue;
+ }
+
+ bool found = false;
+
+ for list_each(outgoing_t, outgoing, outgoing_list) {
+ if(!strcmp(outgoing->name, name)) {
+ found = true;
+ outgoing->timeout = 0;
+ break;
+ }
+ }
+
+ if(!found) {
+ outgoing_t *outgoing = xmalloc_and_zero(sizeof *outgoing);
+ outgoing->name = name;
+ list_insert_tail(outgoing_list, outgoing);
+ setup_outgoing_connection(outgoing);
+ }
+ }
+
+ /* Terminate any connections whose outgoing_t is to be deleted. */
+
+ for list_each(connection_t, c, connection_list) {
+ if(c->outgoing && c->outgoing->timeout == -1) {
+ c->outgoing = NULL;
+ logger(DEBUG_CONNECTIONS, LOG_INFO, "No more outgoing connection to %s", c->name);
+ terminate_connection(c, c->status.active);
+ }
+ }
+
+ /* Delete outgoing_ts for which there is no ConnectTo. */
+
+ for list_each(outgoing_t, outgoing, outgoing_list)
+ if(outgoing->timeout == -1)
+ list_delete_node(outgoing_list, node);