+ return ai;
+}
+
+// Free struct addrinfo list from get_known_addresses().
+static void free_known_addresses(struct addrinfo *ai) {
+ for(struct addrinfo *aip = ai, *next; aip; aip = next) {
+ next = aip->ai_next;
+ free(aip);
+ }
+}
+
+static bool get_recent(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ node_t *n = lookup_node(mesh, outgoing->name);
+
+ if(!n) {
+ return false;
+ }
+
+ outgoing->ai = get_known_addresses(n);
+ outgoing->aip = outgoing->ai;
+ return outgoing->aip;
+}
+
+static bool get_next_ai(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ if(!outgoing->ai) {
+ char *address = NULL;
+
+ if(get_config_string(outgoing->cfg, &address)) {
+ char *port;
+ char *space = strchr(address, ' ');
+
+ if(space) {
+ port = xstrdup(space + 1);
+ *space = 0;
+ } else {
+ if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port)) {
+ logger(mesh, MESHLINK_ERROR, "No Port known for %s", outgoing->name);
+ return false;
+ }
+ }
+
+ outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
+ free(port);
+ free(address);
+ }
+
+ outgoing->aip = outgoing->ai;
+ } else {
+ outgoing->aip = outgoing->aip->ai_next;
+ }
+
+ return outgoing->aip;
+}
+
+static bool get_next_cfg(meshlink_handle_t *mesh, outgoing_t *outgoing, char *variable) {
+ (void)mesh;
+
+ if(!outgoing->cfg) {
+ outgoing->cfg = lookup_config(outgoing->config_tree, variable);
+ } else {
+ outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
+ }
+
+ return outgoing->cfg;
+}
+
+static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ bool start = false;
+
+ if(outgoing->state == OUTGOING_START) {
+ start = true;
+ outgoing->state = OUTGOING_CANONICAL;
+ }
+
+ if(outgoing->state == OUTGOING_CANONICAL) {
+ while(outgoing->aip || get_next_cfg(mesh, outgoing, "CanonicalAddress")) {
+ if(get_next_ai(mesh, outgoing)) {
+ return true;
+ } else {
+ freeaddrinfo(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ }
+ }
+
+ outgoing->state = OUTGOING_RECENT;
+ }
+
+ if(outgoing->state == OUTGOING_RECENT) {
+ while(outgoing->aip || get_next_cfg(mesh, outgoing, "Address")) {
+ if(get_next_ai(mesh, outgoing)) {
+ return true;
+ } else {
+ freeaddrinfo(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ }
+ }
+
+ outgoing->state = OUTGOING_KNOWN;
+ }
+
+ if(outgoing->state == OUTGOING_KNOWN) {
+ if(!outgoing->aip) {
+ get_recent(mesh, outgoing);
+ } else {
+ outgoing->aip = outgoing->aip->ai_next;
+ }
+
+ if(outgoing->aip) {
+ return true;
+ } else {
+ free_known_addresses(outgoing->ai);
+ outgoing->ai = NULL;
+ outgoing->aip = NULL;
+ }
+
+ outgoing->state = OUTGOING_END;
+ }
+
+ if(start) {
+ outgoing->state = OUTGOING_NO_KNOWN_ADDRESSES;
+ }
+
+ return false;
+}
+
+bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+ struct addrinfo *proxyai = NULL;
+ int result;
+
+begin:
+
+ if(!get_next_outgoing_address(mesh, outgoing)) {
+ if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
+ logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->name);
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->name);
+ retry_outgoing(mesh, outgoing);
+ }
+
+ return false;
+ }
+
+ connection_t *c = new_connection();
+ c->outgoing = outgoing;
+
+ memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
+
+ char *hostname = sockaddr2hostname(&c->address);
+
+ logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->name, hostname);
+
+ if(!mesh->proxytype) {
+ c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
+ configure_tcp(c);
+ } else {
+ proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
+
+ if(!proxyai) {
+ free_connection(c);
+ free(hostname);
+ goto begin;
+ }
+
+ logger(mesh, MESHLINK_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
+ c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ configure_tcp(c);
+ }
+
+ if(c->socket == -1) {
+ logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
+ free_connection(c);
+ free(hostname);
+ goto begin;
+ }
+
+ free(hostname);
+
+#ifdef FD_CLOEXEC
+ fcntl(c->socket, F_SETFD, FD_CLOEXEC);