OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #ifndef NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ | |
6 #define NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <utility> | |
12 #include <vector> | |
13 | |
14 #include "base/logging.h" | |
15 #include "base/strings/string_piece.h" | |
16 #include "net/base/net_export.h" | |
17 #include "net/quic/crypto/crypto_handshake_message.h" | |
18 #include "net/quic/quic_protocol.h" | |
19 | |
20 namespace net { | |
21 | |
22 class CryptoFramer; | |
23 class QuicData; | |
24 class QuicDataReader; | |
25 class QuicDataWriter; | |
26 | |
27 class NET_EXPORT_PRIVATE CryptoFramerVisitorInterface { | |
28 public: | |
29 virtual ~CryptoFramerVisitorInterface() {} | |
30 | |
31 // Called if an error is detected. | |
32 virtual void OnError(CryptoFramer* framer) = 0; | |
33 | |
34 // Called when a complete handshake message has been parsed. | |
35 virtual void OnHandshakeMessage(const CryptoHandshakeMessage& message) = 0; | |
36 }; | |
37 | |
38 // A class for framing the crypto messages that are exchanged in a QUIC | |
39 // session. | |
40 class NET_EXPORT_PRIVATE CryptoFramer { | |
41 public: | |
42 CryptoFramer(); | |
43 | |
44 virtual ~CryptoFramer(); | |
45 | |
46 // ParseMessage parses exactly one message from the given StringPiece. If | |
47 // there is an error, the message is truncated, or the message has trailing | |
48 // garbage then nullptr will be returned. | |
49 static CryptoHandshakeMessage* ParseMessage(base::StringPiece in); | |
50 | |
51 // Set callbacks to be called from the framer. A visitor must be set, or | |
52 // else the framer will crash. It is acceptable for the visitor to do | |
53 // nothing. If this is called multiple times, only the last visitor | |
54 // will be used. |visitor| will be owned by the framer. | |
55 void set_visitor(CryptoFramerVisitorInterface* visitor) { | |
56 visitor_ = visitor; | |
57 } | |
58 | |
59 QuicErrorCode error() const { return error_; } | |
60 const std::string& error_detail() const { return error_detail_; } | |
61 | |
62 // Processes input data, which must be delivered in order. Returns | |
63 // false if there was an error, and true otherwise. | |
64 bool ProcessInput(base::StringPiece input); | |
65 | |
66 // Returns the number of bytes of buffered input data remaining to be | |
67 // parsed. | |
68 size_t InputBytesRemaining() const { return buffer_.length(); } | |
69 | |
70 // Returns a new QuicData owned by the caller that contains a serialized | |
71 // |message|, or nullptr if there was an error. | |
72 static QuicData* ConstructHandshakeMessage( | |
73 const CryptoHandshakeMessage& message); | |
74 | |
75 private: | |
76 // Clears per-message state. Does not clear the visitor. | |
77 void Clear(); | |
78 | |
79 // Process does does the work of |ProcessInput|, but returns an error code, | |
80 // doesn't set error_ and doesn't call |visitor_->OnError()|. | |
81 QuicErrorCode Process(base::StringPiece input); | |
82 | |
83 static bool WritePadTag(QuicDataWriter* writer, | |
84 size_t pad_length, | |
85 uint32_t* end_offset); | |
86 | |
87 // Represents the current state of the parsing state machine. | |
88 enum CryptoFramerState { | |
89 STATE_READING_TAG, | |
90 STATE_READING_NUM_ENTRIES, | |
91 STATE_READING_TAGS_AND_LENGTHS, | |
92 STATE_READING_VALUES | |
93 }; | |
94 | |
95 // Visitor to invoke when messages are parsed. | |
96 CryptoFramerVisitorInterface* visitor_; | |
97 // Last error. | |
98 QuicErrorCode error_; | |
99 // Remaining unparsed data. | |
100 std::string buffer_; | |
101 // Current state of the parsing. | |
102 CryptoFramerState state_; | |
103 // The message currently being parsed. | |
104 CryptoHandshakeMessage message_; | |
105 // The issue which caused |error_| | |
106 std::string error_detail_; | |
107 // Number of entires in the message currently being parsed. | |
108 uint16_t num_entries_; | |
109 // tags_and_lengths_ contains the tags that are currently being parsed and | |
110 // their lengths. | |
111 std::vector<std::pair<QuicTag, size_t>> tags_and_lengths_; | |
112 // Cumulative length of all values in the message currently being parsed. | |
113 size_t values_len_; | |
114 }; | |
115 | |
116 } // namespace net | |
117 | |
118 #endif // NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ | |
OLD | NEW |