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 <utility> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/logging.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/strings/string_piece.h" 15 #include "net/base/net_export.h" 16 #include "net/quic/crypto/crypto_handshake.h" 17 #include "net/quic/crypto/crypto_protocol.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( 36 const CryptoHandshakeMessage& message) = 0; 37 }; 38 39 // A class for framing the crypto messages that are exchanged in a QUIC 40 // session. 41 class NET_EXPORT_PRIVATE CryptoFramer { 42 public: 43 CryptoFramer(); 44 45 virtual ~CryptoFramer(); 46 47 // ParseMessage parses exactly one message from the given StringPiece. If 48 // there is an error, the message is truncated, or the message has trailing 49 // garbage then NULL will be returned. 50 static CryptoHandshakeMessage* ParseMessage(base::StringPiece in); 51 52 // Set callbacks to be called from the framer. A visitor must be set, or 53 // else the framer will crash. It is acceptable for the visitor to do 54 // nothing. If this is called multiple times, only the last visitor 55 // will be used. |visitor| will be owned by the framer. 56 void set_visitor(CryptoFramerVisitorInterface* visitor) { 57 visitor_ = visitor; 58 } 59 60 QuicErrorCode error() const { return error_; } 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 NULL 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* 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 // Number of entires in the message currently being parsed. 106 uint16 num_entries_; 107 // tags_and_lengths_ contains the tags that are currently being parsed and 108 // their lengths. 109 std::vector<std::pair<QuicTag, size_t> > tags_and_lengths_; 110 // Cumulative length of all values in the message currently being parsed. 111 size_t values_len_; 112 }; 113 114 } // namespace net 115 116 #endif // NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ 117