+ 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) {
+ if(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) {
+ if(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)) {
+ if(get_next_ai(mesh, outgoing)) {
+ 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;