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