+// Find edges pointing to this node, and use them to build a list of unique, known addresses.
+static struct addrinfo *get_known_addresses(node_t *n) {
+ struct addrinfo *ai = NULL;
+
+ for splay_each(edge_t, e, n->edge_tree) {
+ if(!e->reverse) {
+ continue;
+ }
+
+ bool found = false;
+
+ for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
+ if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) {
+ found = true;
+ break;
+ }
+ }
+
+ if(found) {
+ continue;
+ }
+
+ // Create a new struct addrinfo, and put it at the head of the list.
+ struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(e->reverse->address.sa));
+ nai->ai_next = ai;
+ ai = nai;
+
+ ai->ai_family = e->reverse->address.sa.sa_family;
+ ai->ai_socktype = SOCK_STREAM;
+ ai->ai_protocol = IPPROTO_TCP;
+ ai->ai_addrlen = SALEN(e->reverse->address.sa);
+ ai->ai_addr = (struct sockaddr *)(nai + 1);
+ memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen);
+ }
+
+ 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;
+ }