2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
2022-07-04 05:49:38 +02:00
|
|
|
#include <common/json_param.h>
|
2022-01-29 04:31:32 +01:00
|
|
|
#include <connectd/connectd_wiregen.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>
|
2017-04-12 18:10:10 +02:00
|
|
|
#include <lightningd/subd.h>
|
|
|
|
|
2022-01-29 04:31:32 +01:00
|
|
|
static void ping_reply(struct subd *connectd,
|
|
|
|
const u8 *msg, const int *fds,
|
|
|
|
struct command *cmd)
|
2017-04-12 18:10:10 +02:00
|
|
|
{
|
|
|
|
u16 totlen;
|
2021-10-09 07:53:29 +02:00
|
|
|
bool sent;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2022-01-29 04:31:32 +01:00
|
|
|
log_debug(connectd->log, "Got ping reply!");
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2022-01-29 04:31:32 +01:00
|
|
|
if (!fromwire_connectd_ping_reply(msg, &sent, &totlen)) {
|
|
|
|
log_broken(connectd->log, "Malformed ping reply %s",
|
2021-10-09 07:53:29 +02:00
|
|
|
tal_hex(tmpctx, msg));
|
2022-01-29 04:31:32 +01:00
|
|
|
was_pending(command_fail(cmd, LIGHTNINGD,
|
2018-12-16 05:53:06 +01:00
|
|
|
"Bad reply message"));
|
2021-10-09 07:53:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sent)
|
2022-01-29 04:31:32 +01:00
|
|
|
was_pending(command_fail(cmd, LIGHTNINGD,
|
2021-10-09 07:53:29 +02:00
|
|
|
"Ping already pending"));
|
2017-04-12 18:10:10 +02:00
|
|
|
else {
|
2022-01-29 04:31:32 +01:00
|
|
|
struct json_stream *response = json_stream_success(cmd);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
json_add_num(response, "totlen", totlen);
|
2022-01-29 04:31:32 +01:00
|
|
|
was_pending(command_success(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
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-29 04:31:32 +01:00
|
|
|
if (!peer_by_id(cmd->ld, id))
|
2021-10-09 07:53:29 +02:00
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
|
|
|
|
|
2022-01-29 04:31:32 +01:00
|
|
|
msg = towire_connectd_ping(NULL, id, *pongbytes, *len);
|
|
|
|
subd_req(cmd, cmd->ld->connectd, take(msg), -1, 0, ping_reply, cmd);
|
2021-10-09 07:53:29 +02:00
|
|
|
|
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);
|