+bool do_outgoing_connection(outgoing_t *outgoing) {
+ char *address, *port, *space;
+ struct addrinfo *proxyai = NULL;
+ int result;
+
+begin:
+ if(!outgoing->ai) {
+ if(!outgoing->cfg) {
+ logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->name);
+ retry_outgoing(outgoing);
+ return false;
+ }
+
+ get_config_string(outgoing->cfg, &address);
+
+ space = strchr(address, ' ');
+ if(space) {
+ port = xstrdup(space + 1);
+ *space = 0;
+ } else {
+ if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port))
+ port = xstrdup("655");
+ }
+
+ outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
+ free(address);
+ free(port);
+
+ outgoing->aip = outgoing->ai;
+ outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg);
+ }
+
+ if(!outgoing->aip) {
+ if(outgoing->ai)
+ freeaddrinfo(outgoing->ai);
+ outgoing->ai = NULL;
+ goto begin;
+ }
+
+ connection_t *c = new_connection();
+ c->outgoing = outgoing;
+
+ memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
+ outgoing->aip = outgoing->aip->ai_next;
+
+ c->hostname = sockaddr2hostname(&c->address);
+
+ logger(DEBUG_CONNECTIONS, LOG_INFO, "Trying to connect to %s (%s)", outgoing->name, c->hostname);
+
+ if(!proxytype) {
+ c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
+ configure_tcp(c);
+ } else if(proxytype == PROXY_EXEC) {
+ do_outgoing_pipe(c, proxyhost);
+ } else {
+ proxyai = str2addrinfo(proxyhost, proxyport, SOCK_STREAM);
+ if(!proxyai) {
+ free_connection(c);
+ goto begin;
+ }
+ logger(DEBUG_CONNECTIONS, LOG_INFO, "Using proxy at %s port %s", proxyhost, proxyport);
+ c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ configure_tcp(c);
+ }