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
|
|
|
|
2022-03-23 03:44:38 +01:00
|
|
|
static char *opt_set_tlvname(const char *arg,
|
|
|
|
bool (**printwire)(const char *fieldname,
|
|
|
|
const u8 **cursor,
|
|
|
|
size_t *plen))
|
|
|
|
{
|
|
|
|
for (size_t i = 0; tlvs_printpeer_wire_byname[i].name; i++) {
|
|
|
|
if (streq(arg, tlvs_printpeer_wire_byname[i].name)) {
|
|
|
|
*printwire = tlvs_printpeer_wire_byname[i].print;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; tlvs_printonion_wire_byname[i].name; i++) {
|
|
|
|
if (streq(arg, tlvs_printonion_wire_byname[i].name)) {
|
|
|
|
*printwire = tlvs_printonion_wire_byname[i].print;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "Unknown tlv name";
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *opt_set_onionprint(bool (**printwire)(const u8 *msg))
|
|
|
|
{
|
|
|
|
*printwire = printonion_wire_message;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-23 03:44:38 +01:00
|
|
|
static char *opt_list_tlvnames(void *unused)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; tlvs_printpeer_wire_byname[i].name; i++)
|
|
|
|
printf(" %s\n", tlvs_printpeer_wire_byname[i].name);
|
|
|
|
|
|
|
|
for (size_t i = 0; tlvs_printonion_wire_byname[i].name; i++)
|
|
|
|
printf(" %s\n", tlvs_printonion_wire_byname[i].name);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:34:47 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const u8 *m;
|
2022-03-23 03:44:38 +01:00
|
|
|
bool (*printtlv)(const char *fieldname, const u8 **cursor, size_t *plen) = NULL;
|
|
|
|
bool (*printwire)(const u8 *msg) = printpeer_wire_message;
|
2022-03-23 00:31:14 +01:00
|
|
|
bool ok = true;
|
|
|
|
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2022-03-23 03:44:38 +01:00
|
|
|
opt_register_noarg("--onion", opt_set_onionprint, &printwire,
|
2018-07-26 23:20:37 +02:00
|
|
|
"Decode an error message instead of a peer message");
|
2022-03-23 03:44:38 +01:00
|
|
|
opt_register_arg("--tlv", opt_set_tlvname, NULL, &printtlv,
|
|
|
|
"Decode a TLV of this type instead of a peer message");
|
2022-03-23 03:44:38 +01:00
|
|
|
opt_register_noarg("--list-tlvs", opt_list_tlvnames, NULL,
|
|
|
|
"List all --tlv names supported");
|
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
|
|
|
|
2022-03-23 03:44:38 +01:00
|
|
|
if (printtlv) {
|
|
|
|
size_t len = tal_bytelen(m);
|
|
|
|
ok &= printtlv("", &m, &len);
|
|
|
|
} else {
|
|
|
|
ok &= printwire(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);
|
2022-03-23 03:44:38 +01:00
|
|
|
if (printtlv) {
|
2022-07-25 09:00:09 +02:00
|
|
|
size_t mlen = tal_bytelen(m);
|
|
|
|
ok &= printtlv("", &m, &mlen);
|
2022-03-23 03:44:38 +01:00
|
|
|
} else {
|
|
|
|
ok &= printwire(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
|
|
|
}
|