-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_WINDOWS
+/*
+ accept a new UNIX socket connection
+*/
+void handle_new_unix_connection(void *data, int flags) {
+ (void)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;
+}
+#endif
+
+void try_outgoing_connections(void) {
+ /* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */
+
+ 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->node->name, name)) {
+ found = true;
+ outgoing->timeout = 0;
+ break;
+ }
+ }
+
+ if(!found) {
+ outgoing_t *outgoing = xzalloc(sizeof(*outgoing));
+ node_t *n = lookup_node(name);
+
+ if(!n) {
+ n = new_node();
+ n->name = xstrdup(name);
+ node_add(n);
+ }
+
+ outgoing->node = n;
+ list_insert_tail(&outgoing_list, outgoing);
+ setup_outgoing_connection(outgoing, true);
+ }
+
+ free(name);
+ }
+
+ /* 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);
+ }