Skip to content

Commit dfbf35d

Browse files
committed
fix(server): use local address in AddrStream
Expose local address of tcp connection in AddrStream for the purpose mentioned in #2773.
1 parent d2c945e commit dfbf35d

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/server/tcp.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl AddrIncoming {
107107

108108
loop {
109109
match ready!(self.listener.poll_accept(cx)) {
110-
Ok((socket, addr)) => {
110+
Ok((socket, remote_addr)) => {
111111
if let Some(dur) = self.tcp_keepalive_timeout {
112112
let socket = socket2::SockRef::from(&socket);
113113
let conf = socket2::TcpKeepalive::new().with_time(dur);
@@ -118,7 +118,11 @@ impl AddrIncoming {
118118
if let Err(e) = socket.set_nodelay(self.tcp_nodelay) {
119119
trace!("error trying to set TCP nodelay: {}", e);
120120
}
121-
return Poll::Ready(Ok(AddrStream::new(socket, addr)));
121+
return Poll::Ready(Ok(AddrStream::new(
122+
socket,
123+
remote_addr,
124+
socket.local_addr()?,
125+
)));
122126
}
123127
Err(e) => {
124128
// Connection errors can be ignored directly, continue by
@@ -174,9 +178,12 @@ impl Accept for AddrIncoming {
174178
/// The timeout is useful to handle resource exhaustion errors like ENFILE
175179
/// and EMFILE. Otherwise, could enter into tight loop.
176180
fn is_connection_error(e: &io::Error) -> bool {
177-
matches!(e.kind(), io::ErrorKind::ConnectionRefused
178-
| io::ErrorKind::ConnectionAborted
179-
| io::ErrorKind::ConnectionReset)
181+
matches!(
182+
e.kind(),
183+
io::ErrorKind::ConnectionRefused
184+
| io::ErrorKind::ConnectionAborted
185+
| io::ErrorKind::ConnectionReset
186+
)
180187
}
181188

182189
impl fmt::Debug for AddrIncoming {
@@ -207,14 +214,20 @@ mod addr_stream {
207214
#[pin]
208215
inner: TcpStream,
209216
pub(super) remote_addr: SocketAddr,
217+
pub(super) local_addr: SocketAddr
210218
}
211219
}
212220

213221
impl AddrStream {
214-
pub(super) fn new(tcp: TcpStream, addr: SocketAddr) -> AddrStream {
222+
pub(super) fn new(
223+
tcp: TcpStream,
224+
remote_addr: SocketAddr,
225+
local_addr: SocketAddr,
226+
) -> AddrStream {
215227
AddrStream {
216228
inner: tcp,
217-
remote_addr: addr,
229+
remote_addr,
230+
local_addr,
218231
}
219232
}
220233

@@ -224,6 +237,12 @@ mod addr_stream {
224237
self.remote_addr
225238
}
226239

240+
/// Returns the local address of this connection.
241+
#[inline]
242+
pub fn local_addr(&self) -> SocketAddr {
243+
self.local_addr
244+
}
245+
227246
/// Consumes the AddrStream and returns the underlying IO object
228247
#[inline]
229248
pub fn into_inner(self) -> TcpStream {

0 commit comments

Comments
 (0)