xref: /aosp_15_r20/external/cronet/net/tools/quic/crypto_message_printer_bin.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 
12 #include "base/command_line.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "net/third_party/quiche/src/quiche/quic/core/crypto/crypto_framer.h"
15 
16 using quic::Perspective;
17 using std::cerr;
18 using std::cout;
19 using std::endl;
20 
21 namespace net {
22 
23 class CryptoMessagePrinter : public quic::CryptoFramerVisitorInterface {
24  public:
25   explicit CryptoMessagePrinter() = default;
26 
OnHandshakeMessage(const quic::CryptoHandshakeMessage & message)27   void OnHandshakeMessage(
28       const quic::CryptoHandshakeMessage& message) override {
29     cout << message.DebugString() << endl;
30   }
31 
OnError(quic::CryptoFramer * framer)32   void OnError(quic::CryptoFramer* framer) override {
33     cerr << "Error code: " << framer->error() << endl;
34     cerr << "Error details: " << framer->error_detail() << endl;
35   }
36 
37 };
38 
39 }  // namespace net
40 
main(int argc,char * argv[])41 int main(int argc, char* argv[]) {
42   base::CommandLine::Init(argc, argv);
43 
44   if (argc != 1) {
45     cerr << "Usage: " << argv[0] << " <hex of message>\n";
46     return 1;
47   }
48 
49   net::CryptoMessagePrinter printer;
50   quic::CryptoFramer framer;
51   framer.set_visitor(&printer);
52   framer.set_process_truncated_messages(true);
53   std::string input;
54   if (!base::HexStringToString(argv[1], &input) ||
55       !framer.ProcessInput(input)) {
56     return 1;
57   }
58   if (framer.InputBytesRemaining() != 0) {
59     cerr << "Input partially consumed. " << framer.InputBytesRemaining()
60          << " bytes remaining." << endl;
61     return 2;
62   }
63   return 0;
64 }
65