Пример #1
0
int main(int argc, char **argv)
{
        struct event_base *base;
        int ret;
        base = event_base_new ();
        if (!base) {
                fprintf(stderr, "failed to create event base\n");
                return 1;
        }
        struct bufferevent *bev = bufferevent_socket_new (base, -1,
                                                          BEV_OPT_CLOSE_ON_FREE);
        if (!bev) {
                fprintf(stderr, "failed to create bufferevent\n");
                return 1;
        }
        ret = bufferevent_socket_connect_hostname (bev, NULL, AF_INET,
                                                   "localhost", 9876);
        if (ret) {
                perror("connect failed");
                return 1;
        }
        bufferevent_setcb (bev, read_cb, write_cb, event_cb, NULL);
        bufferevent_enable (bev, EV_READ|EV_WRITE);
        event_base_dispatch(base);
}
Пример #2
0
static
bool
cdsurvivalproxy_ClientConnect (CDServer* server, CDClient* client)
{
	CDPlugin* self = CD_GetPlugin(server->plugins, "survival.proxy");
	
	CDSurvivalProxyData* proxyData = (CDSurvivalProxyData*)CD_DynamicGet(self, "Proxy.data");
	
	CDBuffers* proxyBuffers = CD_WrapBuffers(
		bufferevent_socket_new(server->event.base, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE));
	CD_DynamicPut(client, "Client.proxyBuffers", (CDPointer) proxyBuffers);
	
	
	SLOG(server, LOG_INFO, "proxy client connect %s:%d", proxyData->hostname, proxyData->port);
	
	if (bufferevent_socket_connect_hostname(proxyBuffers->raw,
		proxyData->dnsBase,
		AF_UNSPEC,
		proxyData->hostname,
		proxyData->port) < 0) {
		
		CD_DestroyBuffers(proxyBuffers);
		return false;	
	}
	
	bufferevent_setcb(proxyBuffers->raw,
		(bufferevent_data_cb) cdsurvivalproxy_ProxyReadCallback,
		NULL,
		(bufferevent_event_cb) cdsurvivalproxy_ProxyErrorCallback,
		client);
	
	bufferevent_enable(proxyBuffers->raw, EV_READ | EV_WRITE);
	
	return true;
}
Пример #3
0
bufferevent * connect_by_host(struct event_base *base, const char * host,int port)
{
	bufferevent * bev =NULL;
	do 
	{
		struct evdns_base *dns_base = evdns_base_new(base, 1);
		char * fname = evdns_get_default_hosts_filename();
		if (0!=evdns_base_load_hosts(dns_base, fname))
		{
			printf("evdns_base_load_hosts failed! \n");
		}		
		if (fname)
			free(fname);

		bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
		if (NULL==bev)
		{
			printf("bufferevent_socket_new failed! \n");
			break;
		}
		printf("++++ bufferevent_socket_new %p \n", bev);

		bufferevent_setcb(bev, readcb, writecb, eventcb, NULL);
		bufferevent_enable(bev, EV_READ|EV_WRITE);

		connected =0;
		int rt =bufferevent_socket_connect_hostname(bev, NULL, AF_INET, host, port);
	} while (0);
	return bev;
}
Пример #4
0
static void
search_cb(LDAP *ld, LDAPMessage *msg, void *priv)
{
    struct imap_context *ctx = priv;
    struct bufferevent *server_bev;
    struct evbuffer *out;
    BerValue **servername = NULL;

    out = bufferevent_get_output(ctx->client_bev);
    if (msg) {
        servername = ldap_get_values_len(ld, msg, "mailhost");
    }

    // user not provisioned
    if (!servername || !*servername) {
        /*FIXME: need the full request to have somethng to send */
        evbuffer_add_printf(out, "some_tag " AUTH_FAILED_MSG CRLF);
        bufferevent_enable(ctx->client_bev, EV_READ);
        return;
    }

    server_bev = bufferevent_socket_new(ctx->driver->base, -1, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_enable(server_bev, EV_READ|EV_WRITE);
    bufferevent_socket_connect_hostname(server_bev, ctx->driver->dnsbase, AF_UNSPEC, servername[0]->bv_val, ctx->driver->config->default_port);
    bufferevent_setcb(server_bev, NULL, NULL, server_connect_cb, ctx);

    /*
    // copy over client data, CRLF in request has been skipped, so append that
    bufferevent_write(server_bev, req->line.bv_val, req->line.bv_len);
    bufferevent_write(server_bev, CRLF, 2);
    */

    ctx->server_bev = server_bev;
    ldap_value_free_len(servername);
}
Пример #5
0
int main(int argc, char **argv)
{
    struct event_base *base;
    struct evdns_base *dns_base;
    struct bufferevent *bev;

    if (argc != 3) {
        printf("Trivial HTTP 0.x client\n"
               "Syntax: %s [hostname] [resource]\n"
               "Example: %s www.google.com /\n", argv[0], argv[0]);
        return 1;
    }

    base = event_base_new();
    dns_base = evdns_base_new(base, 1);

    bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_setcb(bev, readcb, NULL, eventcb, base);
    bufferevent_enable(bev, EV_READ | EV_WRITE);
    evbuffer_add_printf(bufferevent_get_output(bev), "GET %s\r\n",
                argv[2]);
    bufferevent_socket_connect_hostname(bev, dns_base, AF_UNSPEC,
                        argv[1], 80);
    event_base_dispatch(base);
    return 0;
}
Пример #6
0
void TS3::telnetClientThread(TS3 * parent, std::string server)
{
	int port = 10011;
	parent->_base = event_base_new();
	parent->_dns_base = evdns_base_new(parent->_base, 1);

	evthread_use_pthreads();
	evthread_make_base_notifiable(parent->_base);

	parent->_msg_event = event_new(parent->_base, -1, EV_READ, sendTS3message, parent);
	event_add(parent->_msg_event, nullptr);

	parent->_bev = bufferevent_socket_new(parent->_base, -1, BEV_OPT_CLOSE_ON_FREE);
	bufferevent_setcb(parent->_bev, telnetMessage, nullptr, telnetEvent, parent);
	bufferevent_enable(parent->_bev, EV_READ|EV_WRITE);
	timeval t;
	t.tv_sec = 60;
	t.tv_usec = 0;
	bufferevent_set_timeouts(parent->_bev, &t, nullptr);
	bufferevent_socket_connect_hostname(
		parent->_bev, parent->_dns_base, AF_UNSPEC, server.c_str(), port);

	// FIXME needs a reliable restart mechanism
	auto result = event_base_dispatch(parent->_base);

	if (result == -1)
		LOG(ERROR) << "Failed to start event loop";
}
Пример #7
0
int
main(int argc, char ** argv) {
    struct evdns_base * dns_base;
    struct timeval      tv;

    if (parse_args(argc, argv) < 0) {
        exit(1);
    }

    tv.tv_sec  = sec;
    tv.tv_usec = usec;

    evbase     = event_base_new();
    dns_base   = evdns_base_new(evbase, 1);
    bev        = bufferevent_socket_new(evbase, -1, BEV_OPT_CLOSE_ON_FREE);
    sev        = evtimer_new(evbase, send_byte, bev);

    bufferevent_setcb(bev, readcb, NULL, eventcb, evbase);
    bufferevent_enable(bev, EV_READ | EV_WRITE);
    bufferevent_socket_connect_hostname(bev, dns_base, AF_UNSPEC, addr, port);

    event_base_loop(evbase, 0);

    while (fread(&fbyte, 1, 1, input_file) == 1) {
        fprintf(stdout, "sending: '%c' (%x)\n", isprint(fbyte) ? fbyte : ' ', fbyte);
    }

    return 0;
}
Пример #8
0
Файл: conn.c Проект: 0xffea/shim
int
conn_connect_bufferevent(struct bufferevent *bev, struct evdns_base *dns,
			 int family, const char *name, int port,
			 conn_connectcb conncb, void *arg)
{
	struct conninfo *info;
	int rv = -1;
	

	info = mem_calloc(1, sizeof(*info));
	info->bev = bev;
	info->on_connect = conncb;
	info->cbarg = arg;
	info->connecting = 1;
	info->socks = use_socks;

	bufferevent_setcb(bev, conn_readcb, NULL, conn_errorcb, info);
	if (use_socks != SOCKS_NONE) {
		info->host = mem_strdup(name);
		info->port = port;
		if (use_socks == SOCKS_4a) {
			rv = bufferevent_socket_connect(bev,
					(struct sockaddr*)&socks_addr,
					socks_addr_len);
			return rv;
		}
#ifndef DISABLE_DIRECT_CONNECTIONS
		else {
			struct evutil_addrinfo hint;
			char portstr[NI_MAXSERV];

			evutil_snprintf(portstr, sizeof(portstr), "%d", port);
			memset(&hint, 0, sizeof(hint));
			hint.ai_family = AF_INET;
			hint.ai_protocol = IPPROTO_TCP;
			hint.ai_socktype = SOCK_STREAM;

			evdns_getaddrinfo(dns, name, portstr, &hint,
				          socks_resolvecb, info);
			return 0;
		}
#endif
	}
#ifdef DISABLE_DIRECT_CONNECTIONS
	{
		const char *msg;
		msg = "Direct connections disabled, but I have no SOCKS 4a "
		      "proxy to connect to!";
		log_error("conn: %s", msg);
		finish_connection(info, 0, msg);
	}
#else
	rv =  bufferevent_socket_connect_hostname(bev, dns, family, name, port);
#endif

	return rv;
}
Пример #9
0
cTCPLinkImplPtr cTCPLinkImpl::Connect(const AString & a_Host, UInt16 a_Port, cTCPLink::cCallbacksPtr a_LinkCallbacks, cNetwork::cConnectCallbacksPtr a_ConnectCallbacks)
{
	ASSERT(a_LinkCallbacks != nullptr);
	ASSERT(a_ConnectCallbacks != nullptr);

	// Create a new link:
	cTCPLinkImplPtr res{new cTCPLinkImpl(a_LinkCallbacks)};  // Cannot use std::make_shared here, constructor is not accessible
	res->m_ConnectCallbacks = a_ConnectCallbacks;
	cNetworkSingleton::Get().AddLink(res);
	res->m_Callbacks->OnLinkCreated(res);
	res->Enable(res);

	// If a_Host is an IP address, schedule a connection immediately:
	sockaddr_storage sa;
	int salen = static_cast<int>(sizeof(sa));
	if (evutil_parse_sockaddr_port(a_Host.c_str(), reinterpret_cast<sockaddr *>(&sa), &salen) == 0)
	{
		// Insert the correct port:
		if (sa.ss_family == AF_INET6)
		{
			reinterpret_cast<sockaddr_in6 *>(&sa)->sin6_port = htons(a_Port);
		}
		else
		{
			reinterpret_cast<sockaddr_in *>(&sa)->sin_port = htons(a_Port);
		}

		// Queue the connect request:
		if (bufferevent_socket_connect(res->m_BufferEvent, reinterpret_cast<sockaddr *>(&sa), salen) == 0)
		{
			// Success
			return res;
		}
		// Failure
		cNetworkSingleton::Get().RemoveLink(res.get());
		return nullptr;
	}

	// a_Host is a hostname, connect after a lookup:
	if (bufferevent_socket_connect_hostname(res->m_BufferEvent, cNetworkSingleton::Get().GetDNSBase(), AF_UNSPEC, a_Host.c_str(), a_Port) == 0)
	{
		// Success
		return res;
	}
	// Failure
	cNetworkSingleton::Get().RemoveLink(res.get());
	return nullptr;
}
Пример #10
0
void TierClientConnectionReconnect(TierClientConnectionRef me){

	if(me->logReconnectMessage)
	{
		TCLOG("trying to reconnect");
		me->logReconnectMessage = NO;
	}
	if(me->be != NULL)
		bufferevent_free(me->be);
	me->be = bufferevent_socket_new(me->eventLoop, -1, BEV_OPT_CLOSE_ON_FREE);
	bufferevent_setcb(me->be, readCBWrapper, writeCBWrapper, eventCBWrapper, me);
	
	bufferevent_enable(me->be, EV_READ|EV_WRITE);
	bufferevent_socket_connect_hostname(me->be, me->dnsBase, AF_UNSPEC, CO(me->host), me->port);

	me->isConnecting = YES;
}
Пример #11
0
// connect to the remote server
static void s3http_client_connect (S3HttpClient *http)
{
    int port;
    AppConf *conf;
    
    if (http->connection_state == S3C_connecting)
        return;

    conf = application_get_conf (http->app);

    if (http->bev)
        bufferevent_free (http->bev);

    http->bev = bufferevent_socket_new (http->evbase, -1, 0);
    if (!http->bev) {
        LOG_err (HTTP_LOG, "Failed to create HTTP object!");
    }
    // XXX: 
    // bufferevent_set_timeouts (http->bev, 


    port = evhttp_uri_get_port (http->http_uri);
    // if no port is specified, libevent returns -1
    if (port == -1) {
        port = conf->http_port;
    }
    
    LOG_debug (HTTP_LOG, "Connecting to %s:%d .. %p",
        evhttp_uri_get_host (http->http_uri),
        port, http
    );

    http->connection_state = S3C_connecting;
    
    bufferevent_enable (http->bev, EV_WRITE);
    bufferevent_setcb (http->bev, 
        NULL, NULL, s3http_client_connection_event_cb,
        http
    );

    bufferevent_socket_connect_hostname (http->bev, http->dns_base, 
        AF_UNSPEC,
        evhttp_uri_get_host (http->http_uri),
        port
    );
}
Пример #12
0
/* Start connecting to the remote peer |host:port| */
static void initiate_connection(struct event_base *evbase, SSL_CTX *ssl_ctx,
                                const char *host, uint16_t port,
                                http2_session_data *session_data) {
  int rv;
  struct bufferevent *bev;
  SSL *ssl;

  ssl = create_ssl(ssl_ctx);
  bev = bufferevent_openssl_socket_new(
      evbase, -1, ssl, BUFFEREVENT_SSL_CONNECTING,
      BEV_OPT_DEFER_CALLBACKS | BEV_OPT_CLOSE_ON_FREE);
  bufferevent_enable(bev, EV_READ | EV_WRITE);
  bufferevent_setcb(bev, readcb, writecb, eventcb, session_data);
  rv = bufferevent_socket_connect_hostname(bev, session_data->dnsbase,
                                           AF_UNSPEC, host, port);

  if (rv != 0) {
    errx(1, "Could not connect to the remote host %s", host);
  }
  session_data->bev = bev;
}
Пример #13
0
void timer_callback(evutil_socket_t fd, short event, void* arg) {
  DEBUG(255, "timer_callback(%d, %d, %p);", fd, event, arg);
  struct server* server = arg;
  struct bufferevent* conn = bufferevent_socket_new(event_base, -1, BEV_OPT_CLOSE_ON_FREE);
  static const struct timeval timeout = { 10, 0 };
  bufferevent_set_timeouts(conn, &timeout, &timeout);
  bufferevent_socket_connect_hostname(conn, dns, AF_INET, server->hostname, server->port);
  bufferevent_setcb(conn, readcb, NULL, eventcb, arg);
  bufferevent_enable(conn, EV_READ);
  static const unsigned char HEADER[] = { 0x1E, 0x00 };
  bufferevent_write(conn, HEADER, (sizeof(HEADER) / sizeof(unsigned char)));
  u_int16_t hostlen = strlen(server->hostname);
  int i;
  for (i = sizeof(hostlen) - 1; i >= 0; i--)
    bufferevent_write(conn, &((unsigned char*)&hostlen)[i], 1);
  bufferevent_write(conn, server->hostname, hostlen);
  for (i = sizeof(server->port) - 1; i >= 0; i--)
    bufferevent_write(conn, &((unsigned char*)&server->port)[i], 1);
  static const unsigned char CLOSING[] = { 0x01, 0x01, 0x00 };
  bufferevent_write(conn, CLOSING, (sizeof(CLOSING) / sizeof(unsigned char)));
};
Пример #14
0
void ClientImpl::initiateConnection(boost::shared_ptr<PendingConnection> &pc) throw (voltdb::ConnectException, voltdb::LibEventException){


    std::stringstream ss;
    ss << "ClientImpl::initiateConnection to " << pc->m_hostname << ":" << pc->m_port;
    logMessage(ClientLogger::INFO, ss.str());
    struct bufferevent * bev = bufferevent_socket_new(m_base, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
    if (bev == NULL) {
        throw ConnectException();
    }
    FreeBEVOnFailure protector(bev);
    bufferevent_setcb(bev, authenticationReadCallback, NULL, authenticationEventCallback, pc.get());

    if (bufferevent_socket_connect_hostname(bev, NULL, AF_INET, pc->m_hostname.c_str(), pc->m_port) != 0) {

        ss.str("");
        ss << "!!!! ClientImpl::initiateConnection to " << pc->m_hostname << ":" << pc->m_port << " failed";
    	logMessage(ClientLogger::ERROR, ss.str());

        throw voltdb::LibEventException();
    }
    protector.success();
    }
Пример #15
0
int main(int argc, char* argv[])
{
    if (argc < 5) {
        printf("Usage: %s address port workload output\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    struct info *inf = info_new();

    struct evdns_base *dns_base;
    struct bufferevent *bev;
    struct event *ev_monitor;

    inf->base = event_base_new();

    dns_base = evdns_base_new(inf->base, 1);

    int port = atoi(argv[2]);

    bev = bufferevent_socket_new(inf->base, -1, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_setcb(bev, readcb, NULL, eventcb, inf);

    if (bufferevent_socket_connect_hostname(bev, dns_base, AF_INET, argv[1], port) < 0) {
        perror("connect failed.\n");
    }

    ev_monitor = event_new(inf->base, -1, EV_TIMEOUT|EV_PERSIST, monitor, inf);
    struct timeval one_second = {1, 0};
    event_add(ev_monitor, &one_second);

    // read_input(inf, argv[3]);
    inf->fp = fopen(argv[4], "w+");

    event_base_dispatch(inf->base);
    bufferevent_free(bev);
    info_free(inf);
    return 0;
}
Пример #16
0
void bev_connect(jsonrpc_server_t* server)
{
	if(!server) {
		ERR("Trying to connect null server\n");
		return;
	}

	INFO("Connecting to server %.*s:%d for conn %.*s.\n",
			STR(server->addr), server->port, STR(server->conn));

	server->bev = bufferevent_socket_new(
			global_ev_base,
			-1,
			BEV_OPT_CLOSE_ON_FREE);
	if(!(server->bev)) {
		ERR("Could not create bufferevent for  %.*s:%d\n", STR(server->addr), server->port);
		connect_failed(server);
		return;
	}

	bufferevent_setcb(
			server->bev,
			bev_read_cb,
			NULL,
			bev_connect_cb,
			server);
	bufferevent_enable(server->bev, EV_READ|EV_WRITE);
	if(bufferevent_socket_connect_hostname(
			server->bev,
			global_evdns_base,
			AF_UNSPEC,
			server->addr.s,
			server->port)<0) {
		WARN("Failed to connect to %.*s:%d\n", STR(server->addr), server->port);
		connect_failed(server);
	}
}
Пример #17
0
static void
test_bufferevent_connect_hostname(void *arg)
{
	struct basic_test_data *data = arg;
	struct evconnlistener *listener = NULL;
	struct bufferevent *be1=NULL, *be2=NULL, *be3=NULL, *be4=NULL, *be5=NULL;
	int be1_outcome=0, be2_outcome=0, be3_outcome=0, be4_outcome=0,
	    be5_outcome=0;
	struct evdns_base *dns=NULL;
	struct evdns_server_port *port=NULL;
	evutil_socket_t server_fd=-1;
	struct sockaddr_in sin;
	int listener_port=-1;
	ev_uint16_t dns_port=0;
	int n_accept=0, n_dns=0;
	char buf[128];

	be_connect_hostname_base = data->base;

	/* Bind an address and figure out what port it's on. */
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
	sin.sin_port = 0;
	listener = evconnlistener_new_bind(data->base, nil_accept_cb,
	    &n_accept,
	    LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC,
	    -1, (struct sockaddr *)&sin, sizeof(sin));
	listener_port = regress_get_socket_port(
		evconnlistener_get_fd(listener));

	port = regress_get_dnsserver(data->base, &dns_port, NULL,
	    be_getaddrinfo_server_cb, &n_dns);
	tt_assert(port);
	tt_int_op(dns_port, >=, 0);

	/* Start an evdns_base that uses the server as its resolver. */
	dns = evdns_base_new(data->base, 0);
	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", dns_port);
	evdns_base_nameserver_ip_add(dns, buf);

	/* Now, finally, at long last, launch the bufferevents.  One should do
	 * a failing lookup IP, one should do a successful lookup by IP,
	 * and one should do a successful lookup by hostname. */
	be1 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
	be2 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
	be3 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
	be4 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
	be5 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);

	bufferevent_setcb(be1, NULL, NULL, be_connect_hostname_event_cb,
	    &be1_outcome);
	bufferevent_setcb(be2, NULL, NULL, be_connect_hostname_event_cb,
	    &be2_outcome);
	bufferevent_setcb(be3, NULL, NULL, be_connect_hostname_event_cb,
	    &be3_outcome);
	bufferevent_setcb(be4, NULL, NULL, be_connect_hostname_event_cb,
	    &be4_outcome);
	bufferevent_setcb(be5, NULL, NULL, be_connect_hostname_event_cb,
	    &be5_outcome);

	/* Launch an async resolve that will fail. */
	tt_assert(!bufferevent_socket_connect_hostname(be1, dns, AF_INET,
		"nosuchplace.example.com", listener_port));
	/* Connect to the IP without resolving. */
	tt_assert(!bufferevent_socket_connect_hostname(be2, dns, AF_INET,
		"127.0.0.1", listener_port));
	/* Launch an async resolve that will succeed. */
	tt_assert(!bufferevent_socket_connect_hostname(be3, dns, AF_INET,
		"nobodaddy.example.com", listener_port));
	/* Use the blocking resolver.  This one will fail if your resolver
	 * can't resolve localhost to 127.0.0.1 */
	tt_assert(!bufferevent_socket_connect_hostname(be4, NULL, AF_INET,
		"localhost", listener_port));
	/* Use the blocking resolver with a nonexistent hostname. */
	tt_assert(!bufferevent_socket_connect_hostname(be5, NULL, AF_INET,
		"nonesuch.nowhere.example.com", 80));

	event_base_dispatch(data->base);

	tt_int_op(be1_outcome, ==, BEV_EVENT_ERROR);
	tt_int_op(be2_outcome, ==, BEV_EVENT_CONNECTED);
	tt_int_op(be3_outcome, ==, BEV_EVENT_CONNECTED);
	tt_int_op(be4_outcome, ==, BEV_EVENT_CONNECTED);
	tt_int_op(be5_outcome, ==, BEV_EVENT_ERROR);

	tt_int_op(n_accept, ==, 3);
	tt_int_op(n_dns, ==, 2);

end:
	if (listener)
		evconnlistener_free(listener);
	if (server_fd>=0)
		EVUTIL_CLOSESOCKET(server_fd);
	if (port)
                evdns_close_server_port(port);
	if (dns)
		evdns_base_free(dns, 0);
	if (be1)
		bufferevent_free(be1);
	if (be2)
		bufferevent_free(be2);
	if (be3)
		bufferevent_free(be3);
	if (be4)
		bufferevent_free(be4);
	if (be5)
		bufferevent_free(be5);
}
Пример #18
0
// This starts a new TCP/TLS connection to notblocked
// It is called both when a new local proxy connection is accepted
// and when we need to re-open an existing telex transport (state->local is reused)
void make_new_telex_conn(struct telex_state *state)
{
    struct telex_conf *conf = state->conf;

    HexDump(LOG_TRACE, state->name, "Opening telex id:", state->remote_conn_id, sizeof(state->remote_conn_id));

	state->remotetcp = bufferevent_socket_new(state->base, -1,
		BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
	if (!state->remotetcp) {
		LogError(state->name, "Could not create remote bufferevent socket");
		StateCleanup(&state);
		return;
	}
	_inc(BEV);

    // TODO: make nonblocking lookup?
    bufferevent_socket_connect_hostname(state->remotetcp, NULL, AF_INET, conf->notblocked_host, conf->notblocked_port);

    // After resolution...
    /*
    struct sockaddr_in sin;
    if (getpeername(bufferevent_getfd(state->remotetcp), (struct sockaddr *)&sin, (socklen_t*)sizeof(sin)) < 0) {
        perror("getpeername");
        LogError("proxy", "getpeername failed");
        StateCleanup(&state);
        return;
    }
    char ip_p[INET_ADDRSTRLEN];
    LogTrace(state->name, "Connecting to %s:%d",
             evutil_inet_ntop(AF_INET, server_ip, ip_p, sizeof(ip_p)), state->conf->notblocked_port);
    //bufferevent_socket_connect(state->remotetcp, ai->ai_addr, (int)ai->ai_addrlen);
    */

	if (ssl_new_telex(state) < 0) {
		ssl_log_errors(LOG_ERROR, state->name);
		LogError(state->name, "Could not create new telex SSL connection object");
		StateCleanup(&state);
		return;
	}
	_inc(SSL);

	state->remote = bufferevent_openssl_filter_new(state->base, state->remotetcp,
		state->ssl, BUFFEREVENT_SSL_CONNECTING, BEV_OPT_DEFER_CALLBACKS);
		// Not BEV_OPT_CLOSE_ON_FREE!
	if (!state->remote) {
		LogError(state->name, "Could not create remote SSL bufferevent filter");
		StateCleanup(&state);
		return;
	}
	_inc(BEV);

    // First, set our read_cb to something that receives the SPTelex init message
	bufferevent_setcb(state->remote, (bufferevent_data_cb)first_read_cb, NULL,
		(bufferevent_event_cb)event_cb, state);

    // Disable until SPTelex init msg
    bufferevent_disable(state->local, EV_READ|EV_WRITE);
	bufferevent_setcb(state->local, (bufferevent_data_cb)read_cb, NULL,
		(bufferevent_event_cb)event_cb, state);

    // Hmm...we should make a second one of these
    state->in_local = 0;
}