diff --git a/daemon/jsonrpc.c b/daemon/jsonrpc.c index b07fcb590..8672503b4 100644 --- a/daemon/jsonrpc.c +++ b/daemon/jsonrpc.c @@ -188,6 +188,7 @@ static const struct json_command *cmdlist[] = { &help_command, &stop_command, &getlog_command, + &connect_command, /* Developer/debugging options. */ &echo_command, }; diff --git a/daemon/jsonrpc.h b/daemon/jsonrpc.h index 2f3f30ce2..af68b3f99 100644 --- a/daemon/jsonrpc.h +++ b/daemon/jsonrpc.h @@ -43,5 +43,6 @@ void json_notify(struct json_connection *jcon, const char *result); void setup_jsonrpc(struct lightningd_state *state, const char *rpc_filename); /* Commands (from other files) */ +extern const struct json_command connect_command; #endif /* LIGHTNING_DAEMON_JSONRPC_H */ diff --git a/daemon/peer.c b/daemon/peer.c index 44f8da614..5f578eecd 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -1,3 +1,5 @@ +#include "dns.h" +#include "jsonrpc.h" #include "lightningd.h" #include "log.h" #include "peer.h" @@ -158,3 +160,36 @@ void setup_listeners(struct lightningd_state *state, unsigned int portnum) if (fd1 < 0 && fd2 < 0) fatal("Could not bind to a network address"); } + +static char *json_connect(struct json_connection *jcon, + const jsmntok_t *params, + struct json_result *response) +{ + jsmntok_t *host, *port; + const char *hoststr, *portstr; + + json_get_params(jcon->buffer, params, "host", &host, "port", &port, + NULL); + + if (!host || !port) + return "Need host and port"; + + hoststr = tal_strndup(response, jcon->buffer + host->start, + host->end - host->start); + portstr = tal_strndup(response, jcon->buffer + port->start, + port->end - port->start); + if (!dns_resolve_and_connect(jcon->state, hoststr, portstr, + peer_connected_out)) + return "DNS failed"; + + json_object_start(response, NULL); + json_object_end(response); + return NULL; +} + +const struct json_command connect_command = { + "connect", + json_connect, + "Connect to a {host} at {port}", + "Returns an empty result (meaning connection in progress)" +};