2017-08-28 18:05:01 +02:00
|
|
|
#include <common/ping.h>
|
2018-02-22 11:01:52 +01:00
|
|
|
#include <common/status.h>
|
|
|
|
#include <common/version.h>
|
2020-08-31 03:13:25 +02:00
|
|
|
#include <wire/peer_wiregen.h>
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
bool check_ping_make_pong(const tal_t *ctx, const u8 *ping, u8 **pong)
|
|
|
|
{
|
|
|
|
u16 num_pong_bytes;
|
|
|
|
u8 *ignored;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_ping(ctx, ping, &num_pong_bytes, &ignored))
|
2017-04-12 18:10:10 +02:00
|
|
|
return false;
|
|
|
|
tal_free(ignored);
|
|
|
|
|
|
|
|
/* FIXME: */
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A node receiving a `ping` message:
|
|
|
|
* - SHOULD fail the channels if it has received significantly in
|
|
|
|
* excess of one `ping` per 30 seconds.
|
2017-04-12 18:10:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A node receiving a `ping` message:
|
|
|
|
*...
|
|
|
|
* - if `num_pong_bytes` is less than 65532:
|
|
|
|
* - MUST respond by sending a `pong` message, with `byteslen` equal
|
|
|
|
* to `num_pong_bytes`.
|
|
|
|
* - otherwise (`num_pong_bytes` is **not** less than 65532):
|
|
|
|
* - MUST ignore the `ping`.
|
2017-04-12 18:10:10 +02:00
|
|
|
*/
|
|
|
|
if (num_pong_bytes < 65532) {
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A node sending a `pong` message:
|
|
|
|
* - SHOULD set `ignored` to 0s.
|
|
|
|
* - MUST NOT set `ignored` to sensitive data such as secrets
|
|
|
|
* or portions of initialized memory.
|
|
|
|
*/
|
2017-04-12 18:10:10 +02:00
|
|
|
ignored = tal_arrz(ctx, u8, num_pong_bytes);
|
2018-02-22 11:01:52 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
/* Embed version */
|
|
|
|
strncpy((char *)ignored, version(), num_pong_bytes);
|
|
|
|
#endif
|
2017-04-12 18:10:10 +02:00
|
|
|
*pong = towire_pong(ctx, ignored);
|
|
|
|
tal_free(ignored);
|
2017-04-12 18:10:10 +02:00
|
|
|
} else
|
|
|
|
*pong = NULL;
|
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *make_ping(const tal_t *ctx, u16 num_pong_bytes, u16 padlen)
|
|
|
|
{
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A node sending a `ping` message:
|
|
|
|
* - SHOULD set `ignored` to 0s.
|
|
|
|
* - MUST NOT set `ignored` to sensitive data such as secrets or
|
|
|
|
* portions of initialized memory.
|
2017-04-12 18:10:10 +02:00
|
|
|
*/
|
|
|
|
u8 *ping, *ignored = tal_arrz(ctx, u8, padlen);
|
|
|
|
|
|
|
|
ping = towire_ping(ctx, num_pong_bytes, ignored);
|
|
|
|
tal_free(ignored);
|
|
|
|
return ping;
|
|
|
|
}
|
2018-02-22 11:01:52 +01:00
|
|
|
|
|
|
|
const char *got_pong(const u8 *pong, size_t *num_pings_outstanding)
|
|
|
|
{
|
|
|
|
u8 *ignored;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!fromwire_pong(pong, pong, &ignored))
|
|
|
|
return "Bad pong";
|
|
|
|
|
|
|
|
if (*num_pings_outstanding == 0)
|
|
|
|
return "Unexpected pong";
|
|
|
|
|
2018-07-28 08:00:16 +02:00
|
|
|
for (i = 0; i < tal_count(ignored); i++) {
|
2018-02-22 11:01:52 +01:00
|
|
|
if (ignored[i] < ' ' || ignored[i] == 127)
|
|
|
|
break;
|
|
|
|
}
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Got pong %zu bytes (%.*s...)",
|
2018-07-28 08:00:16 +02:00
|
|
|
tal_count(ignored), i, (char *)ignored);
|
2018-02-22 11:01:52 +01:00
|
|
|
|
|
|
|
(*num_pings_outstanding)--;
|
|
|
|
return NULL;
|
|
|
|
}
|