| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ | 5 #ifndef NET_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ |
| 6 #define NET_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ | 6 #define NET_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <cstdint> | 9 #include <cstdint> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/strings/string_piece.h" | |
| 15 #include "net/quic/core/crypto/crypto_handshake_message.h" | 14 #include "net/quic/core/crypto/crypto_handshake_message.h" |
| 16 #include "net/quic/core/quic_packets.h" | 15 #include "net/quic/core/quic_packets.h" |
| 17 #include "net/quic/platform/api/quic_export.h" | 16 #include "net/quic/platform/api/quic_export.h" |
| 18 | 17 |
| 19 namespace net { | 18 namespace net { |
| 20 | 19 |
| 21 class CryptoFramer; | 20 class CryptoFramer; |
| 22 class QuicData; | 21 class QuicData; |
| 23 class QuicDataWriter; | 22 class QuicDataWriter; |
| 24 | 23 |
| 25 class QUIC_EXPORT_PRIVATE CryptoFramerVisitorInterface { | 24 class QUIC_EXPORT_PRIVATE CryptoFramerVisitorInterface { |
| 26 public: | 25 public: |
| 27 virtual ~CryptoFramerVisitorInterface() {} | 26 virtual ~CryptoFramerVisitorInterface() {} |
| 28 | 27 |
| 29 // Called if an error is detected. | 28 // Called if an error is detected. |
| 30 virtual void OnError(CryptoFramer* framer) = 0; | 29 virtual void OnError(CryptoFramer* framer) = 0; |
| 31 | 30 |
| 32 // Called when a complete handshake message has been parsed. | 31 // Called when a complete handshake message has been parsed. |
| 33 virtual void OnHandshakeMessage(const CryptoHandshakeMessage& message) = 0; | 32 virtual void OnHandshakeMessage(const CryptoHandshakeMessage& message) = 0; |
| 34 }; | 33 }; |
| 35 | 34 |
| 36 // A class for framing the crypto messages that are exchanged in a QUIC | 35 // A class for framing the crypto messages that are exchanged in a QUIC |
| 37 // session. | 36 // session. |
| 38 class QUIC_EXPORT_PRIVATE CryptoFramer { | 37 class QUIC_EXPORT_PRIVATE CryptoFramer { |
| 39 public: | 38 public: |
| 40 CryptoFramer(); | 39 CryptoFramer(); |
| 41 | 40 |
| 42 virtual ~CryptoFramer(); | 41 virtual ~CryptoFramer(); |
| 43 | 42 |
| 44 // ParseMessage parses exactly one message from the given StringPiece. If | 43 // ParseMessage parses exactly one message from the given QuicStringPiece. If |
| 45 // there is an error, the message is truncated, or the message has trailing | 44 // there is an error, the message is truncated, or the message has trailing |
| 46 // garbage then nullptr will be returned. | 45 // garbage then nullptr will be returned. |
| 47 static std::unique_ptr<CryptoHandshakeMessage> ParseMessage( | 46 static std::unique_ptr<CryptoHandshakeMessage> ParseMessage( |
| 48 base::StringPiece in); | 47 QuicStringPiece in); |
| 49 | 48 |
| 50 // Set callbacks to be called from the framer. A visitor must be set, or | 49 // Set callbacks to be called from the framer. A visitor must be set, or |
| 51 // else the framer will crash. It is acceptable for the visitor to do | 50 // else the framer will crash. It is acceptable for the visitor to do |
| 52 // nothing. If this is called multiple times, only the last visitor | 51 // nothing. If this is called multiple times, only the last visitor |
| 53 // will be used. |visitor| will be owned by the framer. | 52 // will be used. |visitor| will be owned by the framer. |
| 54 void set_visitor(CryptoFramerVisitorInterface* visitor) { | 53 void set_visitor(CryptoFramerVisitorInterface* visitor) { |
| 55 visitor_ = visitor; | 54 visitor_ = visitor; |
| 56 } | 55 } |
| 57 | 56 |
| 58 QuicErrorCode error() const { return error_; } | 57 QuicErrorCode error() const { return error_; } |
| 59 const std::string& error_detail() const { return error_detail_; } | 58 const std::string& error_detail() const { return error_detail_; } |
| 60 | 59 |
| 61 // Processes input data, which must be delivered in order. Returns | 60 // Processes input data, which must be delivered in order. Returns |
| 62 // false if there was an error, and true otherwise. | 61 // false if there was an error, and true otherwise. |
| 63 bool ProcessInput(base::StringPiece input); | 62 bool ProcessInput(QuicStringPiece input); |
| 64 | 63 |
| 65 // Returns the number of bytes of buffered input data remaining to be | 64 // Returns the number of bytes of buffered input data remaining to be |
| 66 // parsed. | 65 // parsed. |
| 67 size_t InputBytesRemaining() const { return buffer_.length(); } | 66 size_t InputBytesRemaining() const { return buffer_.length(); } |
| 68 | 67 |
| 69 // Returns a new QuicData owned by the caller that contains a serialized | 68 // Returns a new QuicData owned by the caller that contains a serialized |
| 70 // |message|, or nullptr if there was an error. | 69 // |message|, or nullptr if there was an error. |
| 71 static QuicData* ConstructHandshakeMessage( | 70 static QuicData* ConstructHandshakeMessage( |
| 72 const CryptoHandshakeMessage& message); | 71 const CryptoHandshakeMessage& message); |
| 73 | 72 |
| 74 private: | 73 private: |
| 75 // Clears per-message state. Does not clear the visitor. | 74 // Clears per-message state. Does not clear the visitor. |
| 76 void Clear(); | 75 void Clear(); |
| 77 | 76 |
| 78 // Process does does the work of |ProcessInput|, but returns an error code, | 77 // Process does does the work of |ProcessInput|, but returns an error code, |
| 79 // doesn't set error_ and doesn't call |visitor_->OnError()|. | 78 // doesn't set error_ and doesn't call |visitor_->OnError()|. |
| 80 QuicErrorCode Process(base::StringPiece input); | 79 QuicErrorCode Process(QuicStringPiece input); |
| 81 | 80 |
| 82 static bool WritePadTag(QuicDataWriter* writer, | 81 static bool WritePadTag(QuicDataWriter* writer, |
| 83 size_t pad_length, | 82 size_t pad_length, |
| 84 uint32_t* end_offset); | 83 uint32_t* end_offset); |
| 85 | 84 |
| 86 // Represents the current state of the parsing state machine. | 85 // Represents the current state of the parsing state machine. |
| 87 enum CryptoFramerState { | 86 enum CryptoFramerState { |
| 88 STATE_READING_TAG, | 87 STATE_READING_TAG, |
| 89 STATE_READING_NUM_ENTRIES, | 88 STATE_READING_NUM_ENTRIES, |
| 90 STATE_READING_TAGS_AND_LENGTHS, | 89 STATE_READING_TAGS_AND_LENGTHS, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 108 // tags_and_lengths_ contains the tags that are currently being parsed and | 107 // tags_and_lengths_ contains the tags that are currently being parsed and |
| 109 // their lengths. | 108 // their lengths. |
| 110 std::vector<std::pair<QuicTag, size_t>> tags_and_lengths_; | 109 std::vector<std::pair<QuicTag, size_t>> tags_and_lengths_; |
| 111 // Cumulative length of all values in the message currently being parsed. | 110 // Cumulative length of all values in the message currently being parsed. |
| 112 size_t values_len_; | 111 size_t values_len_; |
| 113 }; | 112 }; |
| 114 | 113 |
| 115 } // namespace net | 114 } // namespace net |
| 116 | 115 |
| 117 #endif // NET_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ | 116 #endif // NET_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ |
| OLD | NEW |