Skip to content

Commit ffbf610

Browse files
authoredApr 20, 2022
feat(server): add AddrStream::local_addr() (#2816)
Expose local address of tcp connection in AddrStream. Closes #2773
1 parent d2c945e commit ffbf610

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed
 

‎src/server/tcp.rs

+23-7
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,8 @@ 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+
let local_addr = socket.local_addr()?;
122+
return Poll::Ready(Ok(AddrStream::new(socket, remote_addr, local_addr)));
122123
}
123124
Err(e) => {
124125
// Connection errors can be ignored directly, continue by
@@ -174,9 +175,12 @@ impl Accept for AddrIncoming {
174175
/// The timeout is useful to handle resource exhaustion errors like ENFILE
175176
/// and EMFILE. Otherwise, could enter into tight loop.
176177
fn is_connection_error(e: &io::Error) -> bool {
177-
matches!(e.kind(), io::ErrorKind::ConnectionRefused
178-
| io::ErrorKind::ConnectionAborted
179-
| io::ErrorKind::ConnectionReset)
178+
matches!(
179+
e.kind(),
180+
io::ErrorKind::ConnectionRefused
181+
| io::ErrorKind::ConnectionAborted
182+
| io::ErrorKind::ConnectionReset
183+
)
180184
}
181185

182186
impl fmt::Debug for AddrIncoming {
@@ -207,14 +211,20 @@ mod addr_stream {
207211
#[pin]
208212
inner: TcpStream,
209213
pub(super) remote_addr: SocketAddr,
214+
pub(super) local_addr: SocketAddr
210215
}
211216
}
212217

213218
impl AddrStream {
214-
pub(super) fn new(tcp: TcpStream, addr: SocketAddr) -> AddrStream {
219+
pub(super) fn new(
220+
tcp: TcpStream,
221+
remote_addr: SocketAddr,
222+
local_addr: SocketAddr,
223+
) -> AddrStream {
215224
AddrStream {
216225
inner: tcp,
217-
remote_addr: addr,
226+
remote_addr,
227+
local_addr,
218228
}
219229
}
220230

@@ -224,6 +234,12 @@ mod addr_stream {
224234
self.remote_addr
225235
}
226236

237+
/// Returns the local address of this connection.
238+
#[inline]
239+
pub fn local_addr(&self) -> SocketAddr {
240+
self.local_addr
241+
}
242+
227243
/// Consumes the AddrStream and returns the underlying IO object
228244
#[inline]
229245
pub fn into_inner(self) -> TcpStream {

0 commit comments

Comments
 (0)
Please sign in to comment.