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_QUIC_FRAMER_H_ | |
6 #define NET_QUIC_QUIC_FRAMER_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/string_piece.h" | |
14 #include "net/base/net_export.h" | |
15 #include "net/base/ip_endpoint.h" | |
jar (doing other things)
2012/10/16 19:24:01
nit: alphebetize
Ryan Hamilton
2012/10/16 19:43:23
Done.
| |
16 #include "net/quic/crypto/quic_decrypter.h" | |
17 #include "net/quic/crypto/quic_encrypter.h" | |
18 | |
19 namespace net { | |
20 | |
21 class QuicEncrypter; | |
22 class QuicDecrypter; | |
23 class QuicFramer; | |
24 class QuicDataReader; | |
25 class QuicDataWriter; | |
26 | |
27 // Class that receives callbacks from the framer when packets | |
28 // are processed. | |
29 class NET_EXPORT_PRIVATE QuicFramerVisitorInterface { | |
30 public: | |
31 virtual ~QuicFramerVisitorInterface() {} | |
32 | |
33 // Called if an error is detected in the QUIC protocol. | |
34 virtual void OnError(QuicFramer* framer) = 0; | |
35 | |
36 // Called when a new packet has been recieved, before it | |
37 // has been validated or processed. | |
38 virtual void OnPacket(const IPEndPoint& peer_address) = 0; | |
39 | |
40 // Called when the header of a packet had been parsed. | |
41 // If OnPacketHeader returns false, framing for this packet will cease. | |
42 virtual bool OnPacketHeader(const QuicPacketHeader& header) = 0; | |
43 | |
44 // Called when a data packet is parsed that is part of an FEC group. | |
45 // |payload| is the non-encrypted FEC protected payload of the packet. | |
46 virtual void OnFecProtectedPayload(base::StringPiece payload) = 0; | |
47 | |
48 // Called when a StreamFragment has been parsed. | |
49 virtual void OnStreamFragment(const QuicStreamFragment& fragment) = 0; | |
50 | |
51 // Called when a AckFragment has been parsed. | |
52 virtual void OnAckFragment(const QuicAckFragment& fragment) = 0; | |
53 | |
54 // Called when a RstStreamFragment has been parsed. | |
55 virtual void OnRstStreamFragment( | |
56 const QuicRstStreamFragment& fragment) = 0; | |
57 | |
58 // Called when a ConnectionCloseFragment has been parsed. | |
59 virtual void OnConnectionCloseFragment( | |
60 const QuicConnectionCloseFragment& fragment) = 0; | |
61 | |
62 // Called when FEC data has been parsed. | |
63 virtual void OnFecData(const QuicFecData& fec) = 0; | |
64 | |
65 // Called when a packet has been completely processed. | |
66 virtual void OnPacketComplete() = 0; | |
67 }; | |
68 | |
69 class NET_EXPORT_PRIVATE QuicFecBuilderInterface { | |
70 public: | |
71 virtual ~QuicFecBuilderInterface() {} | |
72 | |
73 // Called when a data packet is constructed that is part of an FEC group. | |
74 // |payload| is the non-encrypted FEC protected payload of the packet. | |
75 virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader& header, | |
76 base::StringPiece payload) = 0; | |
77 }; | |
78 | |
79 // Class for parsing and constructing QUIC packets. Has a | |
80 // QuicFramerVisitorInterface that is called when packets are parsed. | |
81 // Also has a QuicFecBuilder that is called when packets are constructed | |
82 // in order to generate FEC data for subsequently building FEC packets. | |
83 class NET_EXPORT_PRIVATE QuicFramer { | |
84 public: | |
85 // Constructs a new framer that will own |decrypter| and |encrypter|. | |
86 QuicFramer(QuicDecrypter* decrypter, QuicEncrypter* encrypter); | |
87 | |
88 virtual ~QuicFramer(); | |
89 | |
90 // Set callbacks to be called from the framer. A visitor must be set, or | |
91 // else the framer will likely crash. It is acceptable for the visitor | |
92 // to do nothing. If this is called multiple times, only the last visitor | |
93 // will be used. | |
94 void set_visitor(QuicFramerVisitorInterface* visitor) { | |
95 visitor_ = visitor; | |
96 } | |
97 | |
98 // Set a builder to be called from the framer when building FEC protected | |
99 // packets. If this is called multiple times, only the last builder | |
100 // will be used. The builder need not be set. | |
101 void set_fec_builder(QuicFecBuilderInterface* builder) { | |
102 fec_builder_ = builder; | |
103 } | |
104 | |
105 QuicErrorCode error() const { | |
106 return error_; | |
107 } | |
108 | |
109 // Pass a UDP packet into the framer for parsing. | |
110 // Return true if the packet was processed succesfully. |packet| must be a | |
111 // single, complete UDP packet (not a fragment of a packet). This packet | |
112 // might be null padded past the end of the payload, which will be correctly | |
113 // ignored. | |
114 bool ProcessPacket(const IPEndPoint& client_address, | |
115 const QuicEncryptedPacket& packet); | |
116 | |
117 // Pass a data packet that was revived from FEC data into the framer | |
118 // for parsing. | |
119 // Return true if the packet was processed succesfully. |payload| must be | |
120 // the complete DECRYPTED payload of the revived packet. | |
121 bool ProcessRevivedPacket(const IPEndPoint& client_address, | |
122 const QuicPacketHeader& header, | |
123 base::StringPiece payload); | |
124 | |
125 // Creates a new QuicPacket populated with the fields in |header| and | |
126 // |fragments|. Assigns |*packet| to the address of the new object. | |
127 // Returns true upon success. | |
128 bool ConstructFragementDataPacket(const QuicPacketHeader& header, | |
129 const QuicFragments& fragments, | |
130 QuicPacket** packet); | |
131 | |
132 // Creates a new QuicPacket populated with the fields in |header| and | |
133 // |fec|. Assigns |*packet| to the address of the new object. | |
134 // Returns true upon success. | |
135 bool ConstructFecPacket(const QuicPacketHeader& header, | |
136 const QuicFecData& fec, | |
137 QuicPacket** packet); | |
138 | |
139 // Increments the retransmission count by one, and updates the authentication | |
140 // hash accordingly. | |
141 void IncrementRetransmitCount(QuicPacket* packet); | |
142 | |
143 uint8 GetRetransmitCount(QuicPacket* packet); | |
144 | |
145 void WriteTransmissionTime(QuicTransmissionTime time, QuicPacket* packet); | |
146 | |
147 // Returns a new encrypted packet, owned by the caller. | |
148 QuicEncryptedPacket* EncryptPacket(const QuicPacket& packet); | |
149 | |
150 // Returns the maximum length of plaintext that can be encrypted | |
151 // to ciphertext no larger than |ciphertext_size|. | |
152 size_t GetMaxPlaintextSize(size_t ciphertext_size); | |
153 | |
154 const std::string& detailed_error() { return detailed_error_; } | |
155 | |
156 private: | |
157 bool WritePacketHeader(const QuicPacketHeader& header, | |
158 QuicDataWriter* builder); | |
159 | |
160 bool ProcessPacketHeader(QuicPacketHeader* header, | |
161 const QuicEncryptedPacket& packet); | |
162 | |
163 bool ProcessFragmentData(); | |
164 bool ProcessStreamFragment(); | |
165 bool ProcessPDUFragment(); | |
166 bool ProcessAckFragment(QuicAckFragment* fragment); | |
167 bool ProcessRstStreamFragment(); | |
168 bool ProcessConnectionCloseFragment(); | |
169 | |
170 bool DecryptPayload(const QuicEncryptedPacket& packet); | |
171 | |
172 // Computes the wire size in bytes of the payload of |fragment|. | |
173 size_t ComputeFragmentPayloadLength(const QuicFragment& fragment); | |
174 | |
175 bool AppendStreamFragmentPayload( | |
176 const QuicStreamFragment& fragment, | |
177 QuicDataWriter* builder); | |
178 bool AppendAckFragmentPayload( | |
179 const QuicAckFragment& fragment, | |
180 QuicDataWriter* builder); | |
181 bool AppendRstStreamFragmentPayload( | |
182 const QuicRstStreamFragment& fragment, | |
183 QuicDataWriter* builder); | |
184 bool AppendConnectionCloseFragmentPayload( | |
185 const QuicConnectionCloseFragment& fragment, | |
186 QuicDataWriter* builder); | |
187 bool RaiseError(QuicErrorCode error); | |
188 | |
189 void set_error(QuicErrorCode error) { | |
190 error_ = error; | |
191 } | |
192 | |
193 void set_detailed_error(const char* error) { | |
194 detailed_error_ = error; | |
195 } | |
196 | |
197 std::string detailed_error_; | |
198 scoped_ptr<QuicDataReader> reader_; | |
199 QuicFramerVisitorInterface* visitor_; | |
200 QuicFecBuilderInterface* fec_builder_; | |
201 QuicErrorCode error_; | |
202 // Buffer containing decrypted payload data during parsing. | |
203 scoped_ptr<QuicData> decrypted_; | |
204 // Decrypter used to decrypt packets during parsing. | |
205 scoped_ptr<QuicDecrypter> decrypter_; | |
206 // Encrypter used to encrypt packets via EncryptPacket(). | |
207 scoped_ptr<QuicEncrypter> encrypter_; | |
208 | |
209 DISALLOW_COPY_AND_ASSIGN(QuicFramer); | |
210 }; | |
211 | |
212 } // namespace net | |
213 | |
214 #endif // NET_QUIC_QUIC_FRAMER_H_ | |
OLD | NEW |