2021-10-09 07:53:29 +02:00
|
|
|
#include <channeld/channeld_wiregen.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/json_tok.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/param.h>
|
2021-10-09 07:53:29 +02:00
|
|
|
#include <lightningd/channel.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>
|
2021-10-09 07:53:29 +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;
|
|
|
|
}
|
|
|
|
|
2021-10-09 07:53:29 +02:00
|
|
|
void ping_reply(struct subd *channeld, const u8 *msg)
|
2017-04-12 18:10:10 +02:00
|
|
|
{
|
|
|
|
u16 totlen;
|
2021-10-09 07:53:29 +02:00
|
|
|
bool sent;
|
2018-09-11 21:57:11 +02:00
|
|
|
struct ping_command *pc;
|
2021-10-09 07:53:29 +02:00
|
|
|
struct channel *c = channeld->channel;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2021-10-09 07:53:29 +02:00
|
|
|
log_debug(channeld->log, "Got ping reply!");
|
|
|
|
pc = find_ping_cmd(channeld->ld, &c->peer->id);
|
|
|
|
if (!pc) {
|
|
|
|
log_broken(channeld->log, "Unexpected ping reply?");
|
|
|
|
return;
|
|
|
|
}
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2021-10-09 07:53:29 +02:00
|
|
|
if (!fromwire_channeld_ping_reply(msg, &sent, &totlen)) {
|
|
|
|
log_broken(channeld->log, "Malformed ping reply %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(pc->cmd, LIGHTNINGD,
|
|
|
|
"Bad reply message"));
|
2021-10-09 07:53:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sent)
|
|
|
|
was_pending(command_fail(pc->cmd, LIGHTNINGD,
|
|
|
|
"Ping already pending"));
|
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
|
|
|
{
|
2018-08-10 22:16:22 +02:00
|
|
|
unsigned int *len, *pongbytes;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *id;
|
2021-10-09 07:53:29 +02:00
|
|
|
struct peer *peer;
|
|
|
|
struct channel *channel;
|
|
|
|
u8 *msg;
|
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
|
|
|
}
|
|
|
|
|
2021-10-09 07:53:29 +02:00
|
|
|
peer = peer_by_id(cmd->ld, id);
|
|
|
|
if (!peer)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
|
|
|
|
|
|
|
|
channel = peer_active_channel(peer);
|
|
|
|
if (!channel || !channel->owner || channel->state != CHANNELD_NORMAL)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
|
|
|
|
|
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);
|
|
|
|
|
2021-10-09 07:53:29 +02:00
|
|
|
msg = towire_channeld_ping(NULL, *pongbytes, *len);
|
|
|
|
subd_send_msg(channel->owner, 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);
|