xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/tools/crypto_message_printer_bin.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Dumps the contents of a QUIC crypto handshake message in a human readable
6 // format.
7 //
8 // Usage: crypto_message_printer_bin <hex of message>
9 
10 #include <iostream>
11 #include <string>
12 
13 #include "absl/strings/escaping.h"
14 #include "quiche/quic/core/crypto/crypto_framer.h"
15 #include "quiche/quic/core/quic_utils.h"
16 #include "quiche/common/platform/api/quiche_command_line_flags.h"
17 
18 using std::cerr;
19 using std::cout;
20 using std::endl;
21 
22 namespace quic {
23 
24 class CryptoMessagePrinter : public ::quic::CryptoFramerVisitorInterface {
25  public:
OnHandshakeMessage(const CryptoHandshakeMessage & message)26   void OnHandshakeMessage(const CryptoHandshakeMessage& message) override {
27     cout << message.DebugString() << endl;
28   }
29 
OnError(CryptoFramer * framer)30   void OnError(CryptoFramer* framer) override {
31     cerr << "Error code: " << framer->error() << endl;
32     cerr << "Error details: " << framer->error_detail() << endl;
33   }
34 };
35 
36 }  // namespace quic
37 
main(int argc,char * argv[])38 int main(int argc, char* argv[]) {
39   const char* usage = "Usage: crypto_message_printer <hex>";
40   std::vector<std::string> messages =
41       quiche::QuicheParseCommandLineFlags(usage, argc, argv);
42   if (messages.size() != 1) {
43     quiche::QuichePrintCommandLineFlagHelp(usage);
44     exit(0);
45   }
46 
47   quic::CryptoMessagePrinter printer;
48   quic::CryptoFramer framer;
49   framer.set_visitor(&printer);
50   framer.set_process_truncated_messages(true);
51   std::string input;
52   if (!absl::HexStringToBytes(messages[0], &input)) {
53     cerr << "Invalid hex string provided" << endl;
54     return 1;
55   }
56   if (!framer.ProcessInput(input)) {
57     return 1;
58   }
59   if (framer.InputBytesRemaining() != 0) {
60     cerr << "Input partially consumed. " << framer.InputBytesRemaining()
61          << " bytes remaining." << endl;
62     return 2;
63   }
64   return 0;
65 }
66