-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);
- }
+#ifndef HAVE_MINGW
+/*
+ accept a new UNIX socket connection
+*/
+void handle_new_unix_connection(void *data, int flags) {
+ io_t *io = data;
+ connection_t *c;
+ sockaddr_t sa;
+ int fd;
+ socklen_t len = sizeof sa;
+
+ fd = accept(io->fd, &sa.sa, &len);
+
+ if(fd < 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
+ return;
+ }
+
+ sockaddrunmap(&sa);
+
+ c = new_connection();
+ c->name = xstrdup("<control>");
+ c->address = sa;
+ c->hostname = xstrdup("localhost port unix");
+ c->socket = fd;
+ c->last_ping_time = now.tv_sec;
+
+ logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection from %s", c->hostname);
+
+ io_add(&c->io, handle_meta_io, c, c->socket, IO_READ);
+
+ connection_add(c);
+
+ c->allow_request = ID;
+
+ send_id(c);
+}
+#endif
+
+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;
+ }
+
+ if(!strcmp(name, myself->name)) {
+ 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 = xzalloc(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->edge);
+ }
+ }
+
+ /* 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);