+ 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;
+}
+
+void 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->node->name);
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
+ retry_outgoing(mesh, outgoing);
+ }
+
+ return;
+ }
+
+ 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->node->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);
+ }