diff --git a/common/jsonrpc_errors.h b/common/jsonrpc_errors.h index 86f58e765..c7de013dc 100644 --- a/common/jsonrpc_errors.h +++ b/common/jsonrpc_errors.h @@ -44,6 +44,10 @@ #define FUNDING_PEER_NOT_CONNECTED 305 #define FUNDING_UNKNOWN_PEER 306 +/* `connect` errors */ +#define CONNECT_NO_KNOWN_ADDRESS 400 +#define CONNECT_ALL_ADDRESSES_FAILED 401 + /* Errors from `invoice` command */ #define INVOICE_LABEL_ALREADY_EXISTS 900 #define INVOICE_PREIMAGE_ALREADY_EXISTS 901 diff --git a/connectd/connect_wire.csv b/connectd/connect_wire.csv index 7f80adae0..660453eae 100644 --- a/connectd/connect_wire.csv +++ b/connectd/connect_wire.csv @@ -44,6 +44,7 @@ msgdata,connectctl_connect_to_peer,addrhint,?wireaddr_internal, # Connectd->master: connect failed. msgtype,connectctl_connect_failed,2020 msgdata,connectctl_connect_failed,id,node_id, +msgdata,connectctl_connect_failed,failcode,u32, msgdata,connectctl_connect_failed,failreason,wirestring, msgdata,connectctl_connect_failed,seconds_to_delay,u32, msgdata,connectctl_connect_failed,addrhint,?wireaddr_internal, diff --git a/connectd/connectd.c b/connectd/connectd.c index 35f81ebb2..da6b8995b 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -565,22 +566,24 @@ static void connect_failed(struct daemon *daemon, const struct node_id *id, u32 seconds_waited, const struct wireaddr_internal *addrhint, + u32 errcode, const char *errfmt, ...) - PRINTF_FMT(5,6); + PRINTF_FMT(6,7); static void connect_failed(struct daemon *daemon, const struct node_id *id, u32 seconds_waited, const struct wireaddr_internal *addrhint, + u32 errcode, const char *errfmt, ...) { u8 *msg; va_list ap; - char *err; + char *errmsg; u32 wait_seconds; va_start(ap, errfmt); - err = tal_vfmt(tmpctx, errfmt, ap); + errmsg = tal_vfmt(tmpctx, errfmt, ap); va_end(ap); /* Wait twice as long to reconnect, between min and max. */ @@ -594,11 +597,11 @@ static void connect_failed(struct daemon *daemon, * happened. We leave it to lightningd to decide if it wants to try * again, with the wait_seconds as a hint of how long before * asking. */ - msg = towire_connectctl_connect_failed(NULL, id, err, wait_seconds, - addrhint); + msg = towire_connectctl_connect_failed(NULL, id, errcode, errmsg, + wait_seconds, addrhint); daemon_conn_send(daemon->master, take(msg)); - status_peer_debug(id, "Failed connected out: %s", err); + status_peer_debug(id, "Failed connected out: %s", errmsg); } /*~ This is the destructor for the (unsuccessful) connection. We accumulate @@ -717,7 +720,8 @@ static void try_connect_one_addr(struct connecting *connect) if (connect->addrnum == tal_count(connect->addrs)) { connect_failed(connect->daemon, &connect->id, connect->seconds_waited, - connect->addrhint, "%s", connect->errors); + connect->addrhint, CONNECT_ALL_ADDRESSES_FAILED, + "%s", connect->errors); tal_free(connect); return; } @@ -1428,6 +1432,7 @@ static void try_connect_peer(struct daemon *daemon, * to retry; an address may get gossiped or appear on the DNS seed. */ if (tal_count(addrs) == 0) { connect_failed(daemon, id, seconds_waited, addrhint, + CONNECT_NO_KNOWN_ADDRESS, "Unable to connect, no address known for peer"); return; } diff --git a/doc/lightning-connect.7 b/doc/lightning-connect.7 index 7bec09eb9..5c068c603 100644 --- a/doc/lightning-connect.7 +++ b/doc/lightning-connect.7 @@ -40,14 +40,41 @@ another node\. Once the peer is connected a channel can be opened with On success the peer \fIid\fR is returned\. +.SH ERRORS -The following error codes may occur: +On failure, one of the following errors will be returned: -.IP \[bu] --1: Catchall nonspecific error\. This may occur if the host is not -valid or there are problems communicating with the peer\. \fBconnect\fR -will make up to 10 attempts to connect to the peer before giving up\. +.nf +.RS +{ "code" : 400, "message" : "Unable to connect, no address known for peer" } + +.RE + +.fi + +If some addresses are known but connecting to all of them failed, the message +will contain details about the failures: + +.nf +.RS +{ "code" : 401, "message" : "..." } + + +.RE + +.fi + +If the given parameters are wrong: + +.nf +.RS +{ "code" : -32602, "message" : "..." } + + +.RE + +.fi .SH AUTHOR Rusty Russell \fI is mainly responsible\. diff --git a/doc/lightning-connect.7.md b/doc/lightning-connect.7.md index 6f21176d1..1483535a6 100644 --- a/doc/lightning-connect.7.md +++ b/doc/lightning-connect.7.md @@ -37,10 +37,21 @@ RETURN VALUE On success the peer *id* is returned. -The following error codes may occur: -- -1: Catchall nonspecific error. This may occur if the host is not - valid or there are problems communicating with the peer. **connect** - will make up to 10 attempts to connect to the peer before giving up. +ERRORS +------ + +On failure, one of the following errors will be returned: + + { "code" : 400, "message" : "Unable to connect, no address known for peer" } + +If some addresses are known but connecting to all of them failed, the message +will contain details about the failures: + + { "code" : 401, "message" : "..." } + +If the given parameters are wrong: + + { "code" : -32602, "message" : "..." } AUTHOR ------ diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index a18e2673d..bd74b5d78 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -231,21 +232,23 @@ void delay_then_reconnect(struct channel *channel, u32 seconds_delay, static void connect_failed(struct lightningd *ld, const u8 *msg) { struct node_id id; - char *err; + u32 errcode; + char *errmsg; struct connect *c; u32 seconds_to_delay; struct wireaddr_internal *addrhint; struct channel *channel; - if (!fromwire_connectctl_connect_failed(tmpctx, msg, &id, &err, - &seconds_to_delay, &addrhint)) + if (!fromwire_connectctl_connect_failed(tmpctx, msg, &id, &errcode, &errmsg, + &seconds_to_delay, &addrhint) || + errcode > INT_MAX) fatal("Connect gave bad CONNECTCTL_CONNECT_FAILED message %s", tal_hex(msg, msg)); /* We can have multiple connect commands: fail them all */ while ((c = find_connect(ld, &id)) != NULL) { /* They delete themselves from list */ - was_pending(command_fail(c->cmd, LIGHTNINGD, "%s", err)); + was_pending(command_fail(c->cmd, (int)errcode, "%s", errmsg)); } /* If we have an active channel, then reconnect. */