2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2018-06-28 03:34:47 +02:00
|
|
|
#include <ccan/err/err.h>
|
2018-07-26 23:20:37 +02:00
|
|
|
#include <ccan/opt/opt.h>
|
2018-09-21 02:29:48 +02:00
|
|
|
#include <ccan/tal/grab_file/grab_file.h>
|
|
|
|
#include <unistd.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#if EXPERIMENTAL_FEATURES
|
2020-10-22 02:57:07 +02:00
|
|
|
#include <wire/onion_exp_printgen.h>
|
|
|
|
#include <wire/peer_exp_printgen.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#else
|
2020-10-22 02:57:07 +02:00
|
|
|
#include <wire/onion_printgen.h>
|
|
|
|
#include <wire/peer_printgen.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#endif
|
2018-02-01 01:08:37 +01:00
|
|
|
|
2018-06-28 03:34:47 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const u8 *m;
|
2018-07-26 23:20:37 +02:00
|
|
|
bool onion = false;
|
2019-04-05 05:06:49 +02:00
|
|
|
char *tlv_name = NULL;
|
2022-03-23 00:31:14 +01:00
|
|
|
bool ok = true;
|
|
|
|
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2018-07-26 23:20:37 +02:00
|
|
|
opt_register_noarg("--onion", opt_set_bool, &onion,
|
|
|
|
"Decode an error message instead of a peer message");
|
2019-04-05 05:06:49 +02:00
|
|
|
opt_register_arg("--tlv", opt_set_charp, opt_show_charp,
|
|
|
|
&tlv_name,
|
|
|
|
"Deocde a TLV of this type instead of a peer message");
|
2018-07-26 23:20:37 +02:00
|
|
|
opt_register_noarg("--help|-h", opt_usage_and_exit,
|
2018-09-21 02:29:48 +02:00
|
|
|
"[<hexmsg>]"
|
|
|
|
"Decode a lightning spec wire message from hex, or a series of messages from stdin",
|
2018-07-26 23:20:37 +02:00
|
|
|
"Print this message.");
|
|
|
|
|
|
|
|
opt_parse(&argc, argv, opt_log_stderr_exit);
|
2018-09-21 02:29:48 +02:00
|
|
|
if (argc > 2)
|
|
|
|
opt_usage_and_exit("Too many arguments");
|
2018-06-28 03:34:47 +02:00
|
|
|
|
2018-09-21 02:29:48 +02:00
|
|
|
if (argc == 2) {
|
|
|
|
/* Arg is hex string */
|
|
|
|
m = tal_hexdata(NULL, argv[1], strlen(argv[1]));
|
|
|
|
if (!m)
|
|
|
|
errx(1, "'%s' is not valid hex", argv[1]);
|
2018-06-28 03:34:47 +02:00
|
|
|
|
2018-09-21 02:29:48 +02:00
|
|
|
if (onion)
|
2019-04-05 05:06:49 +02:00
|
|
|
if (tlv_name)
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printonion_wire_tlv_message(tlv_name, m);
|
2019-04-05 05:06:49 +02:00
|
|
|
else
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printonion_wire_message(m);
|
2019-04-05 05:06:49 +02:00
|
|
|
else if (tlv_name)
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printpeer_wire_tlv_message(tlv_name, m);
|
2018-09-21 02:29:48 +02:00
|
|
|
else
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printpeer_wire_message(m);
|
2018-09-21 02:29:48 +02:00
|
|
|
} else {
|
|
|
|
u8 *f = grab_fd(NULL, STDIN_FILENO);
|
|
|
|
size_t off = 0;
|
|
|
|
|
|
|
|
while (off != tal_count(f)) {
|
|
|
|
be16 len;
|
|
|
|
|
|
|
|
if (off + sizeof(len) > tal_count(f)) {
|
|
|
|
warnx("Truncated file");
|
2022-03-23 00:31:14 +01:00
|
|
|
ok = false;
|
2018-09-21 02:29:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy(&len, f + off, sizeof(len));
|
|
|
|
off += sizeof(len);
|
|
|
|
if (off + be16_to_cpu(len) > tal_count(f)) {
|
|
|
|
warnx("Truncated file");
|
2022-03-23 00:31:14 +01:00
|
|
|
ok = false;
|
2018-09-21 02:29:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
m = tal_dup_arr(f, u8, f + off, be16_to_cpu(len), 0);
|
|
|
|
if (onion)
|
2019-04-05 05:06:49 +02:00
|
|
|
if (tlv_name)
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printonion_wire_tlv_message(tlv_name, m);
|
2019-04-05 05:06:49 +02:00
|
|
|
else
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printonion_wire_message(m);
|
2019-04-05 05:06:49 +02:00
|
|
|
else if (tlv_name)
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printpeer_wire_tlv_message(tlv_name, m);
|
2018-09-21 02:29:48 +02:00
|
|
|
else
|
2022-03-23 00:31:14 +01:00
|
|
|
ok &= printpeer_wire_message(m);
|
2018-09-21 02:29:48 +02:00
|
|
|
off += be16_to_cpu(len);
|
|
|
|
tal_free(m);
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 19:01:34 +01:00
|
|
|
printf("\n");
|
2022-03-23 00:31:14 +01:00
|
|
|
|
|
|
|
return ok ? 0 : 1;
|
2018-02-01 01:08:37 +01:00
|
|
|
}
|