| 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 #include "net/quic/crypto/crypto_framer.h" | 5 #include "net/quic/crypto/crypto_framer.h" |
| 6 | 6 |
| 7 #include "net/quic/crypto/crypto_protocol.h" | 7 #include "net/quic/crypto/crypto_protocol.h" |
| 8 #include "net/quic/quic_data_reader.h" | 8 #include "net/quic/quic_data_reader.h" |
| 9 #include "net/quic/quic_data_writer.h" | 9 #include "net/quic/quic_data_writer.h" |
| 10 | 10 |
| 11 using base::StringPiece; | 11 using base::StringPiece; |
| 12 using std::make_pair; | 12 using std::make_pair; |
| 13 using std::pair; | 13 using std::pair; |
| 14 using std::vector; | 14 using std::vector; |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const size_t kQuicTagSize = sizeof(uint32); | 20 const size_t kQuicTagSize = sizeof(uint32); |
| 21 const size_t kCryptoEndOffsetSize = sizeof(uint32); | 21 const size_t kCryptoEndOffsetSize = sizeof(uint32); |
| 22 const size_t kNumEntriesSize = sizeof(uint16); | 22 const size_t kNumEntriesSize = sizeof(uint16); |
| 23 | 23 |
| 24 // OneShotVisitor is a framer visitor that records a single handshake message. | 24 // OneShotVisitor is a framer visitor that records a single handshake message. |
| 25 class OneShotVisitor : public CryptoFramerVisitorInterface { | 25 class OneShotVisitor : public CryptoFramerVisitorInterface { |
| 26 public: | 26 public: |
| 27 explicit OneShotVisitor(CryptoHandshakeMessage* out) | 27 OneShotVisitor() : error_(false) {} |
| 28 : out_(out), | |
| 29 error_(false), | |
| 30 done_(false) { | |
| 31 } | |
| 32 | 28 |
| 33 virtual void OnError(CryptoFramer* framer) OVERRIDE { error_ = true; } | 29 virtual void OnError(CryptoFramer* framer) OVERRIDE { error_ = true; } |
| 34 | 30 |
| 35 virtual void OnHandshakeMessage( | 31 virtual void OnHandshakeMessage( |
| 36 const CryptoHandshakeMessage& message) OVERRIDE { | 32 const CryptoHandshakeMessage& message) OVERRIDE { |
| 37 *out_ = message; | 33 out_.reset(new CryptoHandshakeMessage(message)); |
| 38 done_ = true; | |
| 39 } | 34 } |
| 40 | 35 |
| 41 bool error() const { return error_; } | 36 bool error() const { return error_; } |
| 42 | 37 |
| 43 bool done() const { return done_; } | 38 CryptoHandshakeMessage* release() { return out_.release(); } |
| 44 | 39 |
| 45 private: | 40 private: |
| 46 CryptoHandshakeMessage* const out_; | 41 scoped_ptr<CryptoHandshakeMessage> out_; |
| 47 bool error_; | 42 bool error_; |
| 48 bool done_; | |
| 49 }; | 43 }; |
| 50 | 44 |
| 51 } // namespace | 45 } // namespace |
| 52 | 46 |
| 53 CryptoFramer::CryptoFramer() | 47 CryptoFramer::CryptoFramer() |
| 54 : visitor_(NULL), | 48 : visitor_(NULL), |
| 55 num_entries_(0), | 49 num_entries_(0), |
| 56 values_len_(0) { | 50 values_len_(0) { |
| 57 Clear(); | 51 Clear(); |
| 58 } | 52 } |
| 59 | 53 |
| 60 CryptoFramer::~CryptoFramer() {} | 54 CryptoFramer::~CryptoFramer() {} |
| 61 | 55 |
| 62 // static | 56 // static |
| 63 CryptoHandshakeMessage* CryptoFramer::ParseMessage(StringPiece in) { | 57 CryptoHandshakeMessage* CryptoFramer::ParseMessage(StringPiece in) { |
| 64 scoped_ptr<CryptoHandshakeMessage> msg(new CryptoHandshakeMessage); | 58 OneShotVisitor visitor; |
| 65 OneShotVisitor visitor(msg.get()); | |
| 66 CryptoFramer framer; | 59 CryptoFramer framer; |
| 67 | 60 |
| 68 framer.set_visitor(&visitor); | 61 framer.set_visitor(&visitor); |
| 69 if (!framer.ProcessInput(in) || visitor.error() || !visitor.done() || | 62 if (!framer.ProcessInput(in) || visitor.error() || |
| 70 framer.InputBytesRemaining()) { | 63 framer.InputBytesRemaining()) { |
| 71 return NULL; | 64 return NULL; |
| 72 } | 65 } |
| 73 | 66 |
| 74 return msg.release(); | 67 return visitor.release(); |
| 75 } | 68 } |
| 76 | 69 |
| 77 bool CryptoFramer::ProcessInput(StringPiece input) { | 70 bool CryptoFramer::ProcessInput(StringPiece input) { |
| 78 DCHECK_EQ(QUIC_NO_ERROR, error_); | 71 DCHECK_EQ(QUIC_NO_ERROR, error_); |
| 79 if (error_ != QUIC_NO_ERROR) { | 72 if (error_ != QUIC_NO_ERROR) { |
| 80 return false; | 73 return false; |
| 81 } | 74 } |
| 82 error_ = Process(input); | 75 error_ = Process(input); |
| 83 if (error_ != QUIC_NO_ERROR) { | 76 if (error_ != QUIC_NO_ERROR) { |
| 84 visitor_->OnError(this); | 77 visitor_->OnError(this); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 281 } |
| 289 *end_offset += pad_length; | 282 *end_offset += pad_length; |
| 290 if (!writer->WriteUInt32(*end_offset)) { | 283 if (!writer->WriteUInt32(*end_offset)) { |
| 291 DCHECK(false) << "Failed to write end offset."; | 284 DCHECK(false) << "Failed to write end offset."; |
| 292 return false; | 285 return false; |
| 293 } | 286 } |
| 294 return true; | 287 return true; |
| 295 } | 288 } |
| 296 | 289 |
| 297 } // namespace net | 290 } // namespace net |
| OLD | NEW |