+static void handle_meta_io(void *data, int flags) {
+ connection_t *c = data;
+
+ if(c->status.connecting) {
+ /* The event loop does not protect against spurious events. Verify that we are actually connected. */
+ if (connect(c->socket, &c->address.sa, sizeof(c->address)) == 0)
+ logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): redundant connect() unexpectedly succeeded", c->name, c->hostname);
+ else if (!sockisconn(sockerrno)) {
+ if (!sockalready(sockerrno)) {
+ logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while checking connection status for %s (%s): %s", c->name, c->hostname, sockstrerror(sockerrno));
+ terminate_connection(c, false);
+ }
+ return;
+ }
+
+ c->status.connecting = false;
+
+ int result;
+ socklen_t len = sizeof result;
+ getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
+
+ if(!result)
+ finish_connecting(c);
+ else {
+ logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(result));
+ terminate_connection(c, false);
+ return;
+ }
+ }
+
+ if(flags & IO_WRITE)
+ handle_meta_write(c);
+ else
+ handle_meta_connection_data(c);
+}
+
+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);