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#include "net/quic/quic_crypto_stream.h"
6
7#include <string>
8
9#include "base/strings/string_piece.h"
10#include "net/quic/crypto/crypto_handshake.h"
11#include "net/quic/quic_connection.h"
12#include "net/quic/quic_session.h"
13
14using std::string;
15using base::StringPiece;
16
17namespace net {
18
19QuicCryptoStream::QuicCryptoStream(QuicSession* session)
20    : ReliableQuicStream(kCryptoStreamId, session),
21      encryption_established_(false),
22      handshake_confirmed_(false) {
23  crypto_framer_.set_visitor(this);
24}
25
26void QuicCryptoStream::OnError(CryptoFramer* framer) {
27  DLOG(WARNING) << "Error processing crypto data: "
28                << QuicUtils::ErrorToString(framer->error());
29}
30
31void QuicCryptoStream::OnHandshakeMessage(
32    const CryptoHandshakeMessage& message) {
33  session()->OnCryptoHandshakeMessageReceived(message);
34}
35
36uint32 QuicCryptoStream::ProcessRawData(const char* data,
37                                        uint32 data_len) {
38  // Do not process handshake messages after the handshake is confirmed.
39  if (handshake_confirmed()) {
40    CloseConnection(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE);
41    return 0;
42  }
43  if (!crypto_framer_.ProcessInput(StringPiece(data, data_len))) {
44    CloseConnection(crypto_framer_.error());
45    return 0;
46  }
47  return data_len;
48}
49
50QuicPriority QuicCryptoStream::EffectivePriority() const {
51  return QuicUtils::HighestPriority();
52}
53
54void QuicCryptoStream::SendHandshakeMessage(
55    const CryptoHandshakeMessage& message) {
56  session()->OnCryptoHandshakeMessageSent(message);
57  const QuicData& data = message.GetSerialized();
58  // To make reasoning about crypto frames easier, we don't combine them with
59  // any other frames in a single packet.
60  session()->connection()->Flush();
61  // TODO(wtc): check the return value.
62  WriteOrBufferData(string(data.data(), data.length()), false);
63  session()->connection()->Flush();
64}
65
66const QuicCryptoNegotiatedParameters&
67QuicCryptoStream::crypto_negotiated_params() const {
68  return crypto_negotiated_params_;
69}
70
71}  // namespace net
72