2020-08-25 03:33:16 +02:00
|
|
|
#include <channeld/channeld_wiregen.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
|
|
|
#include <common/param.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/sphinx.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2020-08-25 04:05:45 +02:00
|
|
|
#include <gossipd/gossipd_wiregen.h>
|
2017-04-12 18:10:10 +02:00
|
|
|
#include <lightningd/htlc_end.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <lightningd/json.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2017-04-12 18:10:10 +02:00
|
|
|
#include <lightningd/lightningd.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/log.h>
|
2017-04-12 18:10:10 +02:00
|
|
|
#include <lightningd/peer_control.h>
|
2018-09-11 21:57:11 +02:00
|
|
|
#include <lightningd/ping.h>
|
2017-04-12 18:10:10 +02:00
|
|
|
#include <lightningd/subd.h>
|
|
|
|
|
2018-09-11 21:57:11 +02:00
|
|
|
struct ping_command {
|
|
|
|
struct list_node list;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2018-09-11 21:57:11 +02:00
|
|
|
struct command *cmd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ping_command *find_ping_cmd(struct lightningd *ld,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id)
|
2018-09-11 21:57:11 +02:00
|
|
|
{
|
|
|
|
struct ping_command *i;
|
|
|
|
|
|
|
|
list_for_each(&ld->ping_commands, i, list) {
|
2019-04-08 11:58:32 +02:00
|
|
|
if (node_id_eq(id, &i->id))
|
2018-09-11 21:57:11 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_ping_command(struct ping_command *pc)
|
|
|
|
{
|
|
|
|
list_del(&pc->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ping_command *new_ping_command(const tal_t *ctx,
|
|
|
|
struct lightningd *ld,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *peer_id,
|
2018-09-11 21:57:11 +02:00
|
|
|
struct command *cmd)
|
|
|
|
{
|
|
|
|
struct ping_command *pc = tal(ctx, struct ping_command);
|
|
|
|
|
|
|
|
pc->id = *peer_id;
|
|
|
|
pc->cmd = cmd;
|
|
|
|
list_add_tail(&ld->ping_commands, &pc->list);
|
|
|
|
tal_add_destructor(pc, destroy_ping_command);
|
|
|
|
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ping_reply(struct subd *subd, const u8 *msg)
|
2017-04-12 18:10:10 +02:00
|
|
|
{
|
|
|
|
u16 totlen;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
bool ok, sent = true;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2018-09-11 21:57:11 +02:00
|
|
|
struct ping_command *pc;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
log_debug(subd->ld->log, "Got ping reply!");
|
2020-08-25 04:05:45 +02:00
|
|
|
ok = fromwire_gossipd_ping_reply(msg, &id, &sent, &totlen);
|
2018-09-11 21:57:11 +02:00
|
|
|
|
|
|
|
pc = find_ping_cmd(subd->ld, &id);
|
|
|
|
assert(pc);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
if (!ok)
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(pc->cmd, LIGHTNINGD,
|
|
|
|
"Bad reply message"));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
else if (!sent)
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(pc->cmd, LIGHTNINGD, "Unknown peer"));
|
2017-04-12 18:10:10 +02:00
|
|
|
else {
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *response = json_stream_success(pc->cmd);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
json_add_num(response, "totlen", totlen);
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_success(pc->cmd, response));
|
2017-04-12 18:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_ping(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2017-04-12 18:10:10 +02:00
|
|
|
{
|
|
|
|
u8 *msg;
|
2018-08-10 22:16:22 +02:00
|
|
|
unsigned int *len, *pongbytes;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *id;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2018-07-16 22:48:38 +02:00
|
|
|
if (!param(cmd, buffer, params,
|
2019-04-08 11:58:32 +02:00
|
|
|
p_req("id", param_node_id, &id),
|
2018-12-16 05:50:06 +01:00
|
|
|
p_opt_def("len", param_number, &len, 128),
|
|
|
|
p_opt_def("pongbytes", param_number, &pongbytes, 128),
|
2018-07-16 22:48:38 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2018-02-20 07:07:18 +01:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* 1. `type`: a 2-byte big-endian field indicating the type of message
|
2018-06-17 12:13:44 +02:00
|
|
|
* 2. `payload`: ...
|
2018-02-20 07:07:18 +01:00
|
|
|
* The size of the message is required by the transport layer to fit
|
|
|
|
* into a 2-byte unsigned int; therefore, the maximum possible size is
|
|
|
|
* 65535 bytes.
|
|
|
|
*...
|
|
|
|
* 1. type: 18 (`ping`)
|
|
|
|
* 2. data:
|
2019-07-16 01:20:37 +02:00
|
|
|
* * [`u16`:`num_pong_bytes`]
|
|
|
|
* * [`u16`:`byteslen`]
|
|
|
|
* * [`byteslen*byte`:`ignored`]
|
2018-02-20 07:07:18 +01:00
|
|
|
*/
|
2018-08-10 22:16:22 +02:00
|
|
|
if (*len > 65535 - 2 - 2 - 2) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"%u would result in oversize ping", *len);
|
2018-02-20 07:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that > 65531 is valid: it means "no pong reply" */
|
2018-08-10 22:16:22 +02:00
|
|
|
if (*pongbytes > 65535) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"pongbytes %u > 65535", *pongbytes);
|
2018-02-20 07:07:18 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 21:57:11 +02:00
|
|
|
/* parent is cmd, so when we complete cmd, we free this. */
|
|
|
|
new_ping_command(cmd, cmd->ld, id, cmd);
|
|
|
|
|
2018-07-24 02:26:43 +02:00
|
|
|
/* gossipd handles all pinging, even if it's in another daemon. */
|
2020-08-25 04:05:45 +02:00
|
|
|
msg = towire_gossipd_ping(NULL, id, *pongbytes, *len);
|
2018-09-11 21:57:11 +02:00
|
|
|
subd_send_msg(cmd->ld->gossip, take(msg));
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_still_pending(cmd);
|
2017-04-12 18:10:10 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:29:30 +02:00
|
|
|
static const struct json_command ping_command = {
|
|
|
|
"ping",
|
2019-05-22 16:08:16 +02:00
|
|
|
"network",
|
2018-08-09 02:29:30 +02:00
|
|
|
json_ping,
|
2018-09-10 13:59:12 +02:00
|
|
|
"Send peer {id} a ping of length {len} (default 128) asking for {pongbytes} (default 128)"
|
2017-04-12 18:10:10 +02:00
|
|
|
};
|
2018-08-09 02:29:30 +02:00
|
|
|
AUTODATA(json_command, &ping_command);
|