Debian Codegraph @

src/rusttls/stream.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use futures_core::ready;
2 use futures_io::{AsyncRead, AsyncWrite};
3 #[cfg(feature = "early-data")]
4 use rustls::client::WriteEarlyData;
5 use rustls::{ClientConnection, IoState, Reader, ServerConnection, Writer};
6 use std::io::{self, Read, Write};
7 use std::marker::Unpin;
8 use std::pin::Pin;
9 use std::task::{Context, Poll};
10
11 pub struct Stream<'a, IO> {
12 pub io: &'a mut IO,
13 pub conn: Conn<'a>,
14 pub eof: bool,
15 }
16
17 pub(crate) enum Conn<'a> {
18 Client(&'a mut ClientConnection),
19 Server(&'a mut ServerConnection),
20 }
21
22 impl Conn<'_> {
23 pub(crate) fn is_handshaking(&self) -> bool {
24 match self {
25 Conn::Client(c) => c.is_handshaking(),
26 Conn::Server(c) => c.is_handshaking(),
27 }
28 }
29
30 pub(crate) fn wants_write(&self) -> bool {
31 match self {
32 Conn::Client(c) => c.wants_write(),
33 Conn::Server(c) => c.wants_write(),
34 }
35 }
36
37 pub(crate) fn wants_read(&self) -> bool {
38 match self {
39 Conn::Client(c) => c.wants_read(),
40 Conn::Server(c) => c.wants_read(),
41 }
42 }
43
44 pub(crate) fn write_tls(&mut self, wr: &mut dyn io::Write) -> Result<usize, io::Error> {
45 match self {
46 Conn::Client(c) => c.write_tls(wr),
47 Conn::Server(c) => c.write_tls(wr),
48 }
49 }
50
51 pub(crate) fn reader(&mut self) -> Reader {
52 match self {
53 Conn::Client(c) => c.reader(),
54 Conn::Server(c) => c.reader(),
55 }
56 }
57
58 pub(crate) fn writer(&mut self) -> Writer {
59 match self {
60 Conn::Client(c) => c.writer(),
61 Conn::Server(c) => c.writer(),
62 }
63 }
64
65 pub(crate) fn send_close_notify(&mut self) {
66 match self {
67 Conn::Client(c) => c.send_close_notify(),
68 Conn::Server(c) => c.send_close_notify(),
69 }
70 }
71
72 pub(crate) fn read_tls(&mut self, rd: &mut dyn io::Read) -> Result<usize, io::Error> {
73 match self {
74 Conn::Client(c) => c.read_tls(rd),
75 Conn::Server(c) => c.read_tls(rd),
76 }
77 }
78
79 pub(crate) fn process_new_packets(&mut self) -> Result<IoState, rustls::Error> {
80 match self {
81 Conn::Client(c) => c.process_new_packets(),
82 Conn::Server(c) => c.process_new_packets(),
83 }
84 }
85
86 #[cfg(feature = "early-data")]
87 pub(crate) fn is_early_data_accepted(&self) -> bool {
88 match self {
89 Conn::Client(c) => c.is_early_data_accepted(),
90 Conn::Server(_) => false,
91 }
92 }
93
94 #[cfg(feature = "early-data")]
95 pub(crate) fn client_early_data(&mut self) -> Option<WriteEarlyData<'_>> {
96 match self {
97 Conn::Client(c) => c.early_data(),
98 Conn::Server(_) => None,
99 }
100 }
101 }
102
103 impl<'a> From<&'a mut ClientConnection> for Conn<'a> {
104 fn from(conn: &'a mut ClientConnection) -> Self {
105 Conn::Client(conn)
106 }
107 }
108
109 impl<'a> From<&'a mut ServerConnection> for Conn<'a> {
110 fn from(conn: &'a mut ServerConnection) -> Self {
111 Conn::Server(conn)
112 }
113 }
114
115 trait WriteTls<IO: AsyncWrite> {
116 fn write_tls(&mut self, cx: &mut Context) -> io::Result<usize>;
117 }
118
119 #[derive(Clone, Copy)]
120 enum Focus {
121 Empty,
122 Readable,
123 Writable,
124 }
125
126 impl<'a, IO: AsyncRead + AsyncWrite + Unpin> Stream<'a, IO> {
127 pub fn new(io: &'a mut IO, conn: impl Into<Conn<'a>>) -> Self {
128 Stream {
129 io,
130 conn: conn.into(),
131 // The state so far is only used to detect EOF, so either Stream
132 // or EarlyData state should both be all right.
133 eof: false,
134 }
135 }
136
137 pub fn set_eof(mut self, eof: bool) -> Self {
138 self.eof = eof;
139 self
140 }
141
142 pub fn as_mut_pin(&mut self) -> Pin<&mut Self> {
143 Pin::new(self)
144 }
145
146 pub fn complete_io(&mut self, cx: &mut Context) -> Poll<io::Result<(usize, usize)>> {
147 self.complete_inner_io(cx, Focus::Empty)
148 }
149
150 fn complete_read_io(&mut self, cx: &mut Context) -> Poll<io::Result<usize>> {
151 struct Reader<'a, 'b, T> {
152 io: &'a mut T,
153 cx: &'a mut Context<'b>,
154 }
155
156 impl<'a, 'b, T: AsyncRead + Unpin> Read for Reader<'a, 'b, T> {
157 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
158 match Pin::new(&mut self.io).poll_read(self.cx, buf) {
159 Poll::Ready(result) => result,
160 Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
161 }
162 }
163 }
164
165 let mut reader = Reader { io: self.io, cx };
166
167 let n = match self.conn.read_tls(&mut reader) {
168 Ok(n) => n,
169 Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => return Poll::Pending,
170 Err(err) => return Poll::Ready(Err(err)),
171 };
172
173 self.conn.process_new_packets().map_err(|err| {
174 // In case we have an alert to send describing this error,
175 // try a last-gasp write -- but don't predate the primary
176 // error.
177 let _ = self.write_tls(cx);
178
179 io::Error::new(io::ErrorKind::InvalidData, err)
180 })?;
181
182 Poll::Ready(Ok(n))
183 }
184
185 fn complete_write_io(&mut self, cx: &mut Context) -> Poll<io::Result<usize>> {
186 match self.write_tls(cx) {
187 Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
188 result => Poll::Ready(result),
189 }
190 }
191
192 fn complete_inner_io(
193 &mut self,
194 cx: &mut Context,
195 focus: Focus,
196 ) -> Poll<io::Result<(usize, usize)>> {
197 let mut wrlen = 0;
198 let mut rdlen = 0;
199
200 loop {
201 let mut write_would_block = false;
202 let mut read_would_block = false;
203
204 while self.conn.wants_write() {
205 match self.complete_write_io(cx) {
206 Poll::Ready(Ok(n)) => wrlen += n,
207 Poll::Pending => {
208 write_would_block = true;
209 break;
210 }
211 Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
212 }
213 }
214
215 if !self.eof && self.conn.wants_read() {
216 match self.complete_read_io(cx) {
217 Poll::Ready(Ok(0)) => self.eof = true,
218 Poll::Ready(Ok(n)) => rdlen += n,
219 Poll::Pending => read_would_block = true,
220 Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
221 }
222 }
223
224 let would_block = match focus {
225 Focus::Empty => write_would_block || read_would_block,
226 Focus::Readable => read_would_block,
227 Focus::Writable => write_would_block,
228 };
229
230 match (self.eof, self.conn.is_handshaking(), would_block) {
231 (true, true, _) => {
232 let err = io::Error::new(io::ErrorKind::UnexpectedEof, "tls handshake eof");
233 return Poll::Ready(Err(err));
234 }
235 (_, false, true) => {
236 let would_block = match focus {
237 Focus::Empty => rdlen == 0 && wrlen == 0,
238 Focus::Readable => rdlen == 0,
239 Focus::Writable => wrlen == 0,
240 };
241
242 return if would_block {
243 Poll::Pending
244 } else {
245 Poll::Ready(Ok((rdlen, wrlen)))
246 };
247 }
248 (_, false, _) => return Poll::Ready(Ok((rdlen, wrlen))),
249 (_, true, true) => return Poll::Pending,
250 (..) => (),
251 }
252 }
253 }
254 }
255
256 impl<'a, IO: AsyncRead + AsyncWrite + Unpin> WriteTls<IO> for Stream<'a, IO> {
257 fn write_tls(&mut self, cx: &mut Context) -> io::Result<usize> {
258 // TODO writev
259
260 struct Writer<'a, 'b, T> {
261 io: &'a mut T,
262 cx: &'a mut Context<'b>,
263 }
264
265 impl<'a, 'b, T: AsyncWrite + Unpin> Write for Writer<'a, 'b, T> {
266 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
267 match Pin::new(&mut self.io).poll_write(self.cx, buf) {
268 Poll::Ready(result) => result,
269 Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
270 }
271 }
272
273 fn flush(&mut self) -> io::Result<()> {
274 match Pin::new(&mut self.io).poll_flush(self.cx) {
275 Poll::Ready(result) => result,
276 Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
277 }
278 }
279 }
280
281 let mut writer = Writer { io: self.io, cx };
282 self.conn.write_tls(&mut writer)
283 }
284 }
285
286 impl<'a, IO: AsyncRead + AsyncWrite + Unpin> AsyncRead for Stream<'a, IO> {
287 fn poll_read(
288 self: Pin<&mut Self>,
289 cx: &mut Context,
290 buf: &mut [u8],
291 ) -> Poll<io::Result<usize>> {
292 let this = self.get_mut();
293
294 while !this.eof && this.conn.wants_read() {
295 match this.complete_inner_io(cx, Focus::Readable) {
296 Poll::Ready(Ok((0, _))) => break,
297 Poll::Ready(Ok(_)) => (),
298 Poll::Pending => return Poll::Pending,
299 Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
300 }
301 }
302
303 let mut reader = this.conn.reader();
304 match reader.read(buf) {
305 Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
306 Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => {
307 this.eof = true;
308 Poll::Ready(Err(err))
309 }
310 result => Poll::Ready(result),
311 }
312 }
313 }
314
315 impl<'a, IO: AsyncRead + AsyncWrite + Unpin> AsyncWrite for Stream<'a, IO> {
316 fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
317 let this = self.get_mut();
318
319 let len = match this.conn.writer().write(buf) {
320 Ok(n) => n,
321 Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => return Poll::Pending,
322 Err(err) => return Poll::Ready(Err(err)),
323 };
324 while this.conn.wants_write() {
325 match this.complete_inner_io(cx, Focus::Writable) {
326 Poll::Ready(Ok(_)) => (),
327 Poll::Pending if len != 0 => break,
328 Poll::Pending => return Poll::Pending,
329 Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
330 }
331 }
332
333 if len != 0 || buf.is_empty() {
334 Poll::Ready(Ok(len))
335 } else {
336 // not write zero
337 match this.conn.writer().write(buf) {
338 Ok(0) => Poll::Pending,
339 Ok(n) => Poll::Ready(Ok(n)),
340 Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
341 Err(err) => Poll::Ready(Err(err)),
342 }
343 }
344 }
345
346 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
347 let this = self.get_mut();
348
349 this.conn.writer().flush()?;
350 while this.conn.wants_write() {
351 ready!(this.complete_inner_io(cx, Focus::Writable))?;
352 }
353 Pin::new(&mut this.io).poll_flush(cx)
354 }
355
356 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
357 let this = self.get_mut();
358
359 while this.conn.wants_write() {
360 ready!(this.complete_inner_io(cx, Focus::Writable))?;
361 }
362 Pin::new(&mut this.io).poll_close(cx)
363 }
364 }
365
366 #[cfg(all(test, feature = "client"))]
367 #[path = "test_stream.rs"]
368 mod test_stream;