Browse Source

connectd: rename 'struct reaching' to 'struct connecting'.

It reads better, and it's accurate: it only exists while we're trying to
connect to a peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
fee-tracking2
Rusty Russell 7 years ago
parent
commit
4de2b362f5
  1. 176
      connectd/connectd.c
  2. 5
      connectd/connectd.h
  3. 117
      connectd/tor.c
  4. 4
      connectd/tor.h

176
connectd/connectd.c

@ -57,7 +57,7 @@
#include <wire/wire_sync.h> #include <wire/wire_sync.h>
#include <zlib.h> #include <zlib.h>
#define CONNECT_MAX_REACH_ATTEMPTS 10 #define MAX_CONNECT_ATTEMPTS 10
#define HSM_FD 3 #define HSM_FD 3
#define GOSSIPCTL_FD 4 #define GOSSIPCTL_FD 4
@ -89,7 +89,7 @@ struct daemon {
struct pubkey_set peers; struct pubkey_set peers;
/* Peers we are trying to reach */ /* Peers we are trying to reach */
struct list_head reaching; struct list_head connecting;
/* Connection to main daemon. */ /* Connection to main daemon. */
struct daemon_conn master; struct daemon_conn master;
@ -114,9 +114,9 @@ struct daemon {
struct listen_fd *listen_fds; struct listen_fd *listen_fds;
}; };
/* Peers we're trying to reach. */ /* Peers we're trying to connect. */
struct reaching { struct connecting {
/* daemon->reaching */ /* daemon->connecting */
struct list_node list; struct list_node list;
struct daemon *daemon; struct daemon *daemon;
@ -142,7 +142,7 @@ struct reaching {
}; };
/* Mutual recursion */ /* Mutual recursion */
static void try_reach_one_addr(struct reaching *reach); static void try_connect_one_addr(struct connecting *connect);
/** /**
* Some ISP resolvers will reply with a dummy IP to queries that would otherwise * Some ISP resolvers will reply with a dummy IP to queries that would otherwise
@ -174,38 +174,33 @@ static bool broken_resolver(struct daemon *daemon)
return daemon->broken_resolver_response != NULL; return daemon->broken_resolver_response != NULL;
} }
static void destroy_reaching(struct reaching *reach) static void destroy_connecting(struct connecting *connect)
{ {
list_del_from(&reach->daemon->reaching, &reach->list); list_del_from(&connect->daemon->connecting, &connect->list);
} }
static struct reaching *find_reaching(struct daemon *daemon, static struct connecting *find_connecting(struct daemon *daemon,
const struct pubkey *id) const struct pubkey *id)
{ {
struct reaching *r; struct connecting *i;
list_for_each(&daemon->reaching, r, list) list_for_each(&daemon->connecting, i, list)
if (pubkey_eq(id, &r->id)) if (pubkey_eq(id, &i->id))
return r; return i;
return NULL; return NULL;
} }
static void reached_peer(struct io_conn *conn, static void connected_to_peer(struct daemon *daemon,
struct daemon *daemon, struct io_conn *conn,
const struct pubkey *id) const struct pubkey *id)
{ {
/* OK, we've reached the peer successfully, tell everyone. */
struct reaching *r = find_reaching(daemon, id);
if (!r)
return;
/* Don't call destroy_io_conn */ /* Don't call destroy_io_conn */
io_set_finish(conn, NULL, NULL); io_set_finish(conn, NULL, NULL);
/* Don't free conn with reach */ /* Don't free conn with connect */
tal_steal(daemon, conn); tal_steal(daemon, conn);
tal_free(r);
tal_free(find_connecting(daemon, id));
} }
static int get_gossipfd(struct daemon *daemon, static int get_gossipfd(struct daemon *daemon,
@ -299,7 +294,7 @@ struct io_plan *peer_connected(struct io_conn *conn,
return io_wait(conn, key, retry_peer_connected, r); return io_wait(conn, key, retry_peer_connected, r);
} }
reached_peer(conn, daemon, id); connected_to_peer(daemon, conn, id);
gossip_fd = get_gossipfd(daemon, id, lfeatures); gossip_fd = get_gossipfd(daemon, id, lfeatures);
@ -745,24 +740,24 @@ static struct io_plan *handshake_out_success(struct io_conn *conn,
const struct pubkey *id, const struct pubkey *id,
const struct wireaddr_internal *addr, const struct wireaddr_internal *addr,
const struct crypto_state *cs, const struct crypto_state *cs,
struct reaching *reach) struct connecting *connect)
{ {
reach->connstate = "Exchanging init messages"; connect->connstate = "Exchanging init messages";
status_trace("Connect OUT to %s", status_trace("Connect OUT to %s",
type_to_string(tmpctx, struct pubkey, id)); type_to_string(tmpctx, struct pubkey, id));
return peer_exchange_initmsg(conn, reach->daemon, cs, id, addr); return peer_exchange_initmsg(conn, connect->daemon, cs, id, addr);
} }
struct io_plan *connection_out(struct io_conn *conn, struct reaching *reach) struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect)
{ {
/* FIXME: Timeout */ /* FIXME: Timeout */
status_trace("Connected out for %s", status_trace("Connected out for %s",
type_to_string(tmpctx, struct pubkey, &reach->id)); type_to_string(tmpctx, struct pubkey, &connect->id));
reach->connstate = "Cryptographic handshake"; connect->connstate = "Cryptographic handshake";
return initiator_handshake(conn, &reach->daemon->id, &reach->id, return initiator_handshake(conn, &connect->daemon->id, &connect->id,
&reach->addrs[reach->addrnum], &connect->addrs[connect->addrnum],
handshake_out_success, reach); handshake_out_success, connect);
} }
static void PRINTF_FMT(5,6) static void PRINTF_FMT(5,6)
@ -798,21 +793,22 @@ static void PRINTF_FMT(5,6)
err); err);
} }
static void destroy_io_conn(struct io_conn *conn, struct reaching *reach) static void destroy_io_conn(struct io_conn *conn, struct connecting *connect)
{ {
tal_append_fmt(&reach->errors, tal_append_fmt(&connect->errors,
"%s: %s: %s. ", "%s: %s: %s. ",
type_to_string(tmpctx, struct wireaddr_internal, type_to_string(tmpctx, struct wireaddr_internal,
&reach->addrs[reach->addrnum]), &connect->addrs[connect->addrnum]),
reach->connstate, strerror(errno)); connect->connstate, strerror(errno));
reach->addrnum++; connect->addrnum++;
try_reach_one_addr(reach); try_connect_one_addr(connect);
} }
static struct io_plan *conn_init(struct io_conn *conn, struct reaching *reach) static struct io_plan *conn_init(struct io_conn *conn,
struct connecting *connect)
{ {
struct addrinfo *ai = NULL; struct addrinfo *ai = NULL;
const struct wireaddr_internal *addr = &reach->addrs[reach->addrnum]; const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
switch (addr->itype) { switch (addr->itype) {
case ADDR_INTERNAL_SOCKNAME: case ADDR_INTERNAL_SOCKNAME:
@ -820,15 +816,15 @@ static struct io_plan *conn_init(struct io_conn *conn, struct reaching *reach)
break; break;
case ADDR_INTERNAL_ALLPROTO: case ADDR_INTERNAL_ALLPROTO:
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Can't reach to all protocols"); "Can't connect to all protocols");
break; break;
case ADDR_INTERNAL_AUTOTOR: case ADDR_INTERNAL_AUTOTOR:
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Can't reach to autotor address"); "Can't connect to autotor address");
break; break;
case ADDR_INTERNAL_FORPROXY: case ADDR_INTERNAL_FORPROXY:
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Can't reach to forproxy address"); "Can't connect to forproxy address");
break; break;
case ADDR_INTERNAL_WIREADDR: case ADDR_INTERNAL_WIREADDR:
/* If it was a Tor address, we wouldn't be here. */ /* If it was a Tor address, we wouldn't be here. */
@ -837,16 +833,16 @@ static struct io_plan *conn_init(struct io_conn *conn, struct reaching *reach)
} }
assert(ai); assert(ai);
io_set_finish(conn, destroy_io_conn, reach); io_set_finish(conn, destroy_io_conn, connect);
return io_connect(conn, ai, connection_out, reach); return io_connect(conn, ai, connection_out, connect);
} }
static struct io_plan *conn_proxy_init(struct io_conn *conn, static struct io_plan *conn_proxy_init(struct io_conn *conn,
struct reaching *reach) struct connecting *connect)
{ {
const char *host = NULL; const char *host = NULL;
u16 port; u16 port;
const struct wireaddr_internal *addr = &reach->addrs[reach->addrnum]; const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
switch (addr->itype) { switch (addr->itype) {
case ADDR_INTERNAL_FORPROXY: case ADDR_INTERNAL_FORPROXY:
@ -865,10 +861,11 @@ static struct io_plan *conn_proxy_init(struct io_conn *conn,
if (!host) if (!host)
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Can't reach to %u address", addr->itype); "Can't connect to %u address", addr->itype);
io_set_finish(conn, destroy_io_conn, reach); io_set_finish(conn, destroy_io_conn, connect);
return io_tor_connect(conn, reach->daemon->proxyaddr, host, port, reach); return io_tor_connect(conn, connect->daemon->proxyaddr, host, port,
connect);
} }
static const char *seedname(const tal_t *ctx, const struct pubkey *id) static const char *seedname(const tal_t *ctx, const struct pubkey *id)
@ -933,16 +930,17 @@ static void add_gossip_addrs(struct wireaddr_internal **addrs,
} }
} }
static void try_reach_one_addr(struct reaching *reach) static void try_connect_one_addr(struct connecting *connect)
{ {
int fd, af; int fd, af;
bool use_proxy = reach->daemon->use_proxy_always; bool use_proxy = connect->daemon->use_proxy_always;
const struct wireaddr_internal *addr = &reach->addrs[reach->addrnum]; const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
if (reach->addrnum == tal_count(reach->addrs)) { if (connect->addrnum == tal_count(connect->addrs)) {
connect_failed(reach->daemon, &reach->id, reach->seconds_waited, connect_failed(connect->daemon, &connect->id,
reach->addrhint, "%s", reach->errors); connect->seconds_waited,
tal_free(reach); connect->addrhint, "%s", connect->errors);
tal_free(connect);
return; return;
} }
@ -957,10 +955,10 @@ static void try_reach_one_addr(struct reaching *reach)
break; break;
case ADDR_INTERNAL_ALLPROTO: case ADDR_INTERNAL_ALLPROTO:
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Can't reach ALLPROTO"); "Can't connect ALLPROTO");
case ADDR_INTERNAL_AUTOTOR: case ADDR_INTERNAL_AUTOTOR:
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Can't reach AUTOTOR"); "Can't connect AUTOTOR");
case ADDR_INTERNAL_FORPROXY: case ADDR_INTERNAL_FORPROXY:
use_proxy = true; use_proxy = true;
break; break;
@ -983,11 +981,11 @@ static void try_reach_one_addr(struct reaching *reach)
/* If we have to use proxy but we don't have one, we fail. */ /* If we have to use proxy but we don't have one, we fail. */
if (use_proxy) { if (use_proxy) {
if (!reach->daemon->proxyaddr) { if (!connect->daemon->proxyaddr) {
status_debug("Need proxy"); status_debug("Need proxy");
af = -1; af = -1;
} else } else
af = reach->daemon->proxyaddr->ai_family; af = connect->daemon->proxyaddr->ai_family;
} }
if (af == -1) { if (af == -1) {
@ -997,38 +995,38 @@ static void try_reach_one_addr(struct reaching *reach)
fd = socket(af, SOCK_STREAM, 0); fd = socket(af, SOCK_STREAM, 0);
if (fd < 0) { if (fd < 0) {
tal_append_fmt(&reach->errors, tal_append_fmt(&connect->errors,
"%s: opening %i socket gave %s. ", "%s: opening %i socket gave %s. ",
type_to_string(tmpctx, struct wireaddr_internal, type_to_string(tmpctx, struct wireaddr_internal,
addr), addr),
af, strerror(errno)); af, strerror(errno));
reach->addrnum++; connect->addrnum++;
try_reach_one_addr(reach); try_connect_one_addr(connect);
return; return;
} }
if (use_proxy) if (use_proxy)
io_new_conn(reach, fd, conn_proxy_init, reach); io_new_conn(connect, fd, conn_proxy_init, connect);
else else
io_new_conn(reach, fd, conn_init, reach); io_new_conn(connect, fd, conn_init, connect);
} }
/* Consumes addrhint if not NULL */ /* Consumes addrhint if not NULL */
static void try_reach_peer(struct daemon *daemon, static void try_connect_peer(struct daemon *daemon,
const struct pubkey *id, const struct pubkey *id,
u32 seconds_waited, u32 seconds_waited,
struct wireaddr_internal *addrhint) struct wireaddr_internal *addrhint)
{ {
struct wireaddr_internal *addrs; struct wireaddr_internal *addrs;
bool use_proxy = daemon->use_proxy_always; bool use_proxy = daemon->use_proxy_always;
struct reaching *reach; struct connecting *connect;
/* Already done? May happen with timer. */ /* Already done? May happen with timer. */
if (pubkey_set_get(&daemon->peers, id)) if (pubkey_set_get(&daemon->peers, id))
return; return;
/* If we're trying to reach it right now, that's OK. */ /* If we're trying to connect it right now, that's OK. */
if (find_reaching(daemon, id)) if (find_connecting(daemon, id))
return; return;
addrs = tal_arr(tmpctx, struct wireaddr_internal, 0); addrs = tal_arr(tmpctx, struct wireaddr_internal, 0);
@ -1058,19 +1056,19 @@ static void try_reach_peer(struct daemon *daemon,
} }
/* Start connecting to it */ /* Start connecting to it */
reach = tal(daemon, struct reaching); connect = tal(daemon, struct connecting);
reach->daemon = daemon; connect->daemon = daemon;
reach->id = *id; connect->id = *id;
reach->addrs = tal_steal(reach, addrs); connect->addrs = tal_steal(connect, addrs);
reach->addrnum = 0; connect->addrnum = 0;
reach->connstate = "Connection establishment"; connect->connstate = "Connection establishment";
reach->seconds_waited = seconds_waited; connect->seconds_waited = seconds_waited;
reach->addrhint = tal_steal(reach, addrhint); connect->addrhint = tal_steal(connect, addrhint);
reach->errors = tal_strdup(reach, ""); connect->errors = tal_strdup(connect, "");
list_add_tail(&daemon->reaching, &reach->list); list_add_tail(&daemon->connecting, &connect->list);
tal_add_destructor(reach, destroy_reaching); tal_add_destructor(connect, destroy_connecting);
try_reach_one_addr(reach); try_connect_one_addr(connect);
} }
static struct io_plan *connect_to_peer(struct io_conn *conn, static struct io_plan *connect_to_peer(struct io_conn *conn,
@ -1085,7 +1083,7 @@ static struct io_plan *connect_to_peer(struct io_conn *conn,
&addrhint)) &addrhint))
master_badmsg(WIRE_CONNECTCTL_CONNECT_TO_PEER, msg); master_badmsg(WIRE_CONNECTCTL_CONNECT_TO_PEER, msg);
try_reach_peer(daemon, &id, seconds_waited, addrhint); try_connect_peer(daemon, &id, seconds_waited, addrhint);
return daemon_conn_read_next(conn, &daemon->master); return daemon_conn_read_next(conn, &daemon->master);
} }
@ -1177,7 +1175,7 @@ int main(int argc, char *argv[])
daemon = tal(NULL, struct daemon); daemon = tal(NULL, struct daemon);
pubkey_set_init(&daemon->peers); pubkey_set_init(&daemon->peers);
list_head_init(&daemon->reaching); list_head_init(&daemon->connecting);
daemon->listen_fds = tal_arr(daemon, struct listen_fd, 0); daemon->listen_fds = tal_arr(daemon, struct listen_fd, 0);
/* stdin == control */ /* stdin == control */
daemon_conn_init(daemon, &daemon->master, STDIN_FILENO, recv_req, daemon_conn_init(daemon, &daemon->master, STDIN_FILENO, recv_req,

5
connectd/connectd.h

@ -5,12 +5,11 @@
#include <common/crypto_state.h> #include <common/crypto_state.h>
struct io_conn; struct io_conn;
struct peer; struct connecting;
struct reaching;
struct daemon; struct daemon;
/* Called by io_tor_connect once it has a connection out. */ /* Called by io_tor_connect once it has a connection out. */
struct io_plan *connection_out(struct io_conn *conn, struct reaching *reach); struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect);
/* Called by peer_exchange_initmsg if successful. */ /* Called by peer_exchange_initmsg if successful. */
struct io_plan *peer_connected(struct io_conn *conn, struct io_plan *peer_connected(struct io_conn *conn,

117
connectd/tor.c

@ -33,130 +33,131 @@
/* some crufts can not forward ipv6*/ /* some crufts can not forward ipv6*/
#undef BIND_FIRST_TO_IPV6 #undef BIND_FIRST_TO_IPV6
struct reaching_socks { struct connecting_socks {
u8 buffer[MAX_SIZE_OF_SOCKS5_REQ_OR_RESP]; u8 buffer[MAX_SIZE_OF_SOCKS5_REQ_OR_RESP];
size_t hlen; size_t hlen;
in_port_t port; in_port_t port;
char *host; char *host;
struct reaching *reach; struct connecting *connect;
}; };
static struct io_plan *connect_finish2(struct io_conn *conn, static struct io_plan *connect_finish2(struct io_conn *conn,
struct reaching_socks *reach) struct connecting_socks *connect)
{ {
status_io(LOG_IO_IN, "proxy", status_io(LOG_IO_IN, "proxy",
(reach->buffer + SIZE_OF_RESPONSE - SIZE_OF_IPV4_RESPONSE), (connect->buffer + SIZE_OF_RESPONSE - SIZE_OF_IPV4_RESPONSE),
SIZE_OF_IPV6_RESPONSE - SIZE_OF_RESPONSE - SIZE_OF_IPV4_RESPONSE); SIZE_OF_IPV6_RESPONSE - SIZE_OF_RESPONSE - SIZE_OF_IPV4_RESPONSE);
status_trace("Now try LN connect out for host %s", reach->host); status_trace("Now try LN connect out for host %s", connect->host);
return connection_out(conn, reach->reach); return connection_out(conn, connect->connect);
} }
static struct io_plan *connect_finish(struct io_conn *conn, static struct io_plan *connect_finish(struct io_conn *conn,
struct reaching_socks *reach) struct connecting_socks *connect)
{ {
status_io(LOG_IO_IN, "proxy", status_io(LOG_IO_IN, "proxy",
reach->buffer, SIZE_OF_IPV4_RESPONSE + SIZE_OF_RESPONSE); connect->buffer, SIZE_OF_IPV4_RESPONSE + SIZE_OF_RESPONSE);
if ( reach->buffer[1] == '\0') { if ( connect->buffer[1] == '\0') {
if ( reach->buffer[3] == SOCKS_TYP_IPV6) { if ( connect->buffer[3] == SOCKS_TYP_IPV6) {
return io_read(conn, return io_read(conn,
(reach->buffer + SIZE_OF_RESPONSE - (connect->buffer + SIZE_OF_RESPONSE -
SIZE_OF_IPV4_RESPONSE), SIZE_OF_IPV4_RESPONSE),
SIZE_OF_IPV6_RESPONSE - SIZE_OF_IPV6_RESPONSE -
SIZE_OF_RESPONSE - SIZE_OF_IPV4_RESPONSE, SIZE_OF_RESPONSE - SIZE_OF_IPV4_RESPONSE,
&connect_finish2, reach); &connect_finish2, connect);
} else if ( reach->buffer[3] == SOCKS_TYP_IPV4) { } else if ( connect->buffer[3] == SOCKS_TYP_IPV4) {
status_trace("Now try LN connect out for host %s", status_trace("Now try LN connect out for host %s",
reach->host); connect->host);
return connection_out(conn, reach->reach); return connection_out(conn, connect->connect);
} else { } else {
status_trace status_trace
("Tor connect out for host %s error invalid type return ", ("Tor connect out for host %s error invalid type return ",
reach->host); connect->host);
return io_close(conn); return io_close(conn);
} }
} else { } else {
status_trace("Tor connect out for host %s error: %x ", status_trace("Tor connect out for host %s error: %x ",
reach->host, reach->buffer[1]); connect->host, connect->buffer[1]);
return io_close(conn); return io_close(conn);
} }
} }
/* called when TOR responds */ /* called when TOR responds */
static struct io_plan *connect_out(struct io_conn *conn, static struct io_plan *connect_out(struct io_conn *conn,
struct reaching_socks *reach) struct connecting_socks *connect)
{ {
return io_read(conn, reach->buffer, return io_read(conn, connect->buffer,
SIZE_OF_IPV4_RESPONSE + SIZE_OF_RESPONSE, SIZE_OF_IPV4_RESPONSE + SIZE_OF_RESPONSE,
&connect_finish, reach); &connect_finish, connect);
} }
static struct io_plan *io_tor_connect_after_resp_to_connect(struct io_conn static struct io_plan *io_tor_connect_after_resp_to_connect(struct io_conn
*conn, *conn,
struct struct
reaching_socks connecting_socks
*reach) *connect)
{ {
status_io(LOG_IO_IN, "proxy", reach->buffer, 2); status_io(LOG_IO_IN, "proxy", connect->buffer, 2);
if (reach->buffer[1] == SOCKS_ERROR) { if (connect->buffer[1] == SOCKS_ERROR) {
status_trace("Connected out for %s error", reach->host); status_trace("Connected out for %s error", connect->host);
return io_close(conn); return io_close(conn);
} }
/* make the V5 request */ /* make the V5 request */
reach->hlen = strlen(reach->host); connect->hlen = strlen(connect->host);
reach->buffer[0] = SOCKS_V5; connect->buffer[0] = SOCKS_V5;
reach->buffer[1] = SOCKS_CONNECT; connect->buffer[1] = SOCKS_CONNECT;
reach->buffer[2] = 0; connect->buffer[2] = 0;
reach->buffer[3] = SOCKS_DOMAIN; connect->buffer[3] = SOCKS_DOMAIN;
reach->buffer[4] = reach->hlen; connect->buffer[4] = connect->hlen;
memcpy(reach->buffer + SOCK_REQ_V5_LEN, reach->host, reach->hlen); memcpy(connect->buffer + SOCK_REQ_V5_LEN, connect->host, connect->hlen);
memcpy(reach->buffer + SOCK_REQ_V5_LEN + strlen(reach->host), memcpy(connect->buffer + SOCK_REQ_V5_LEN + strlen(connect->host),
&(reach->port), sizeof reach->port); &(connect->port), sizeof connect->port);
status_io(LOG_IO_OUT, "proxy", reach->buffer, status_io(LOG_IO_OUT, "proxy", connect->buffer,
SOCK_REQ_V5_HEADER_LEN + reach->hlen); SOCK_REQ_V5_HEADER_LEN + connect->hlen);
return io_write(conn, reach->buffer, return io_write(conn, connect->buffer,
SOCK_REQ_V5_HEADER_LEN + reach->hlen, SOCK_REQ_V5_HEADER_LEN + connect->hlen,
connect_out, reach); connect_out, connect);
} }
static struct io_plan *io_tor_connect_after_req_to_connect(struct io_conn *conn, static struct io_plan *io_tor_connect_after_req_to_connect(struct io_conn *conn,
struct reaching_socks struct connecting_socks
*reach) *connect)
{ {
return io_read(conn, reach->buffer, 2, return io_read(conn, connect->buffer, 2,
&io_tor_connect_after_resp_to_connect, reach); &io_tor_connect_after_resp_to_connect, connect);
} }
static struct io_plan *io_tor_connect_do_req(struct io_conn *conn, static struct io_plan *io_tor_connect_do_req(struct io_conn *conn,
struct reaching_socks *reach) struct connecting_socks *connect)
{ {
/* make the init request */ /* make the init request */
reach->buffer[0] = SOCKS_V5; connect->buffer[0] = SOCKS_V5;
reach->buffer[1] = 1; connect->buffer[1] = 1;
reach->buffer[2] = SOCKS_NOAUTH; connect->buffer[2] = SOCKS_NOAUTH;
status_io(LOG_IO_OUT, "proxy", reach->buffer, SOCK_REQ_METH_LEN); status_io(LOG_IO_OUT, "proxy", connect->buffer, SOCK_REQ_METH_LEN);
return io_write(conn, reach->buffer, SOCK_REQ_METH_LEN, return io_write(conn, connect->buffer, SOCK_REQ_METH_LEN,
&io_tor_connect_after_req_to_connect, reach); &io_tor_connect_after_req_to_connect, connect);
} }
// called when we want to connect to TOR SOCKS5 // called when we want to connect to TOR SOCKS5
struct io_plan *io_tor_connect(struct io_conn *conn, struct io_plan *io_tor_connect(struct io_conn *conn,
const struct addrinfo *tor_proxyaddr, const struct addrinfo *tor_proxyaddr,
const char *host, u16 port, const char *host, u16 port,
struct reaching *reach) struct connecting *connect)
{ {
struct reaching_socks *reach_tor = tal(reach, struct reaching_socks); struct connecting_socks *connect_tor = tal(connect,
struct connecting_socks);
reach_tor->port = htons(port); connect_tor->port = htons(port);
reach_tor->host = tal_strdup(reach_tor, host); connect_tor->host = tal_strdup(connect_tor, host);
reach_tor->reach = reach; connect_tor->connect = connect;
return io_connect(conn, tor_proxyaddr, return io_connect(conn, tor_proxyaddr,
&io_tor_connect_do_req, reach_tor); &io_tor_connect_do_req, connect_tor);
} }

4
connectd/tor.h

@ -6,11 +6,11 @@
struct addrinfo; struct addrinfo;
struct wireaddr; struct wireaddr;
struct io_conn; struct io_conn;
struct reaching; struct connecting;
struct io_plan *io_tor_connect(struct io_conn *conn, struct io_plan *io_tor_connect(struct io_conn *conn,
const struct addrinfo *tor_proxyaddr, const struct addrinfo *tor_proxyaddr,
const char *host, u16 port, const char *host, u16 port,
struct reaching *reach); struct connecting *connect);
#endif /* LIGHTNING_CONNECTD_TOR_H */ #endif /* LIGHTNING_CONNECTD_TOR_H */

Loading…
Cancel
Save