| 46 | } |
| 47 | |
| 48 | int main(int argc, char *argv[]) |
| 49 | { |
| 50 | const u8 *m; |
| 51 | bool (*printtlv)(const char *fieldname, const u8 **cursor, size_t *plen) = NULL; |
| 52 | bool (*printwire)(const u8 *msg) = printpeer_wire_message; |
| 53 | bool ok = true; |
| 54 | |
| 55 | common_setup(argv[0]); |
| 56 | setup_locale(); |
| 57 | |
| 58 | opt_register_noarg("--onion", opt_set_onionprint, &printwire, |
| 59 | "Decode an error message instead of a peer message"); |
| 60 | opt_register_arg("--tlv", opt_set_tlvname, NULL, &printtlv, |
| 61 | "Decode a TLV of this type instead of a peer message"); |
| 62 | opt_register_noarg("--list-tlvs", opt_list_tlvnames, NULL, |
| 63 | "List all --tlv names supported"); |
| 64 | opt_register_noarg("--help|-h", opt_usage_and_exit, |
| 65 | "[<hexmsg>]" |
| 66 | "Decode a lightning spec wire message from hex, or a series of messages from stdin", |
| 67 | "Print this message."); |
| 68 | |
| 69 | opt_parse(&argc, argv, opt_log_stderr_exit); |
| 70 | if (argc > 2) |
| 71 | opt_usage_and_exit("Too many arguments"); |
| 72 | |
| 73 | if (argc == 2) { |
| 74 | /* Arg is hex string */ |
| 75 | m = tal_hexdata(NULL, argv[1], strlen(argv[1])); |
| 76 | if (!m) |
| 77 | errx(1, "'%s' is not valid hex", argv[1]); |
| 78 | |
| 79 | if (printtlv) { |
| 80 | size_t len = tal_bytelen(m); |
| 81 | ok &= printtlv("", &m, &len); |
| 82 | } else { |
| 83 | ok &= printwire(m); |
| 84 | } |
| 85 | } else { |
| 86 | u8 *f = grab_fd_str(NULL, STDIN_FILENO); |
| 87 | size_t off = 0; |
| 88 | |
| 89 | while (off != tal_count(f)) { |
| 90 | be16 len; |
| 91 | |
| 92 | if (off + sizeof(len) > tal_count(f)) { |
| 93 | warnx("Truncated file"); |
| 94 | ok = false; |
| 95 | break; |
| 96 | } |
| 97 | memcpy(&len, f + off, sizeof(len)); |
| 98 | off += sizeof(len); |
| 99 | if (off + be16_to_cpu(len) > tal_count(f)) { |
| 100 | warnx("Truncated file"); |
| 101 | ok = false; |
| 102 | break; |
| 103 | } |
| 104 | m = tal_dup_arr(f, u8, f + off, be16_to_cpu(len), 0); |
| 105 | if (printtlv) { |
nothing calls this directly
no test coverage detected