-
Notifications
You must be signed in to change notification settings - Fork 99
server: enable holding back acceptor alerts #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -70,7 +70,8 @@ impl TlsAcceptor { | |||||||||||
| pub struct LazyConfigAcceptor<IO> { | ||||||||||||
| acceptor: rustls::server::Acceptor, | ||||||||||||
| io: Option<IO>, | ||||||||||||
| alert: Option<(rustls::Error, AcceptedAlert)>, | ||||||||||||
| alert: Option<AlertState>, | ||||||||||||
| send_alert: bool, | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| impl<IO> LazyConfigAcceptor<IO> | ||||||||||||
|
|
@@ -83,9 +84,33 @@ where | |||||||||||
| acceptor, | ||||||||||||
| io: Some(io), | ||||||||||||
| alert: None, | ||||||||||||
| send_alert: true, | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Configure whether to send a TLS alert on failure. | ||||||||||||
| pub fn send_alert(mut self, send: bool) -> Self { | ||||||||||||
| self.send_alert = send; | ||||||||||||
| self | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Writes a stored alert, consuming the alert (if any) and IO. | ||||||||||||
| pub async fn write_alert(&mut self) -> io::Result<()> { | ||||||||||||
| let Some(alert) = self.take_alert() else { | ||||||||||||
| return Ok(()); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| let Some(io) = self.take_io() else { | ||||||||||||
| return Ok(()); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| WritingAlert { | ||||||||||||
| io, | ||||||||||||
| alert: Some(alert), | ||||||||||||
| } | ||||||||||||
| .await | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Takes back the client connection. Will return `None` if called more than once or if the | ||||||||||||
| /// connection has been accepted. | ||||||||||||
| /// | ||||||||||||
|
|
@@ -130,6 +155,14 @@ where | |||||||||||
| pub fn take_io(&mut self) -> Option<IO> { | ||||||||||||
| self.io.take() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| pub fn take_alert(&mut self) -> Option<AcceptedAlert> { | ||||||||||||
| match self.alert.take() { | ||||||||||||
| Some(AlertState::Sending(_, alert)) => Some(alert), | ||||||||||||
| Some(AlertState::Saved(alert)) => Some(alert), | ||||||||||||
| None => None, | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| impl<IO> Future for LazyConfigAcceptor<IO> | ||||||||||||
|
|
@@ -151,17 +184,17 @@ where | |||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| if let Some((err, mut alert)) = this.alert.take() { | ||||||||||||
| if let Some(AlertState::Sending(err, mut alert)) = this.alert.take() { | ||||||||||||
| match alert.write(&mut SyncWriteAdapter { io, cx }) { | ||||||||||||
| Err(e) if e.kind() == io::ErrorKind::WouldBlock => { | ||||||||||||
| this.alert = Some((err, alert)); | ||||||||||||
| this.alert = Some(AlertState::Sending(err, alert)); | ||||||||||||
| return Poll::Pending; | ||||||||||||
| } | ||||||||||||
| Ok(0) | Err(_) => { | ||||||||||||
| return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidData, err))) | ||||||||||||
| } | ||||||||||||
| Ok(_) => { | ||||||||||||
| this.alert = Some((err, alert)); | ||||||||||||
| this.alert = Some(AlertState::Sending(err, alert)); | ||||||||||||
| continue; | ||||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
@@ -181,9 +214,49 @@ where | |||||||||||
| return Poll::Ready(Ok(StartHandshake { accepted, io })); | ||||||||||||
| } | ||||||||||||
| Ok(None) => {} | ||||||||||||
| Err((err, alert)) => { | ||||||||||||
| this.alert = Some((err, alert)); | ||||||||||||
| Err((err, alert)) => match this.send_alert { | ||||||||||||
| true => this.alert = Some(AlertState::Sending(err, alert)), | ||||||||||||
| false => { | ||||||||||||
| this.alert = Some(AlertState::Saved(alert)); | ||||||||||||
| return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidData, err))); | ||||||||||||
| } | ||||||||||||
| }, | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| enum AlertState { | ||||||||||||
| Sending(rustls::Error, AcceptedAlert), | ||||||||||||
| Saved(AcceptedAlert), | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| struct WritingAlert<IO> { | ||||||||||||
| io: IO, | ||||||||||||
| alert: Option<AcceptedAlert>, | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| impl<IO> Future for WritingAlert<IO> | ||||||||||||
| where | ||||||||||||
| IO: AsyncRead + AsyncWrite + Unpin, | ||||||||||||
| { | ||||||||||||
| type Output = Result<(), io::Error>; | ||||||||||||
| fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||||||||||||
| let this = self.get_mut(); | ||||||||||||
| let io = &mut this.io; | ||||||||||||
| loop { | ||||||||||||
| let Some(mut alert) = this.alert.take() else { | ||||||||||||
| return Poll::Ready(Ok(())); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| match alert.write(&mut SyncWriteAdapter { io, cx }) { | ||||||||||||
| Ok(0) => return Poll::Ready(Ok(())), | ||||||||||||
| Ok(_) => continue, | ||||||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure this is wrong. In case of a partial write, just calling (Presumably it would be exceedingly rare to get a partial write, but still...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this works and is tested. The test uses a duplex stream with a capacity of Looking at the alert.write() function, it does seem to consume the data it wrote such that multiple calls to the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, for some reason it worked on my branch but not the PR now. let me see what changed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This fixes the issue and aligns with how LazyConfigAcceptor writes the alert |
||||||||||||
| Err(e) if e.kind() == io::ErrorKind::WouldBlock => { | ||||||||||||
| this.alert = Some(alert); | ||||||||||||
| return Poll::Pending; | ||||||||||||
| } | ||||||||||||
| Err(e) => return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidData, e))), | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@howardjohn what is the purpose of this? It doesn't make sense to me. If send_alert is true, we've sent the alert before this is reachable. If send_alert is false, I think you can only call this without taking out either the io or the alert, and there's no way you can actually check what the server received, I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is meant to be set before you poll, not after. We could also instead do
new_without_alert()or equivilent?(just a note, this was from your initial commit not my testing change)