From 1782789a3da144e9ac071ef45d8a6f14d20fc7b6 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sun, 19 May 2024 13:29:52 +0200 Subject: [PATCH 1/2] remove typo and return the correct address of the connected peer --- src/fd/socket/tcp.rs | 1 - src/syscalls/socket.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/fd/socket/tcp.rs b/src/fd/socket/tcp.rs index f5b0eefd97..2649e94d15 100644 --- a/src/fd/socket/tcp.rs +++ b/src/fd/socket/tcp.rs @@ -345,7 +345,6 @@ impl ObjectInterface for Socket { } } - #[allow(dead_code)] fn getpeername(&self) -> Option { self.with(|socket| socket.remote_endpoint()) } diff --git a/src/syscalls/socket.rs b/src/syscalls/socket.rs index c020bdecc3..1ee8f82474 100644 --- a/src/syscalls/socket.rs +++ b/src/syscalls/socket.rs @@ -532,7 +532,7 @@ pub unsafe extern "C" fn sys_getpeername( obj.map_or_else( |e| -num::ToPrimitive::to_i32(&e).unwrap(), |v| { - if let Some(endpoint) = (*v).getsockname() { + if let Some(endpoint) = (*v).getpeername() { if !addr.is_null() && !addrlen.is_null() { let addrlen = unsafe { &mut *addrlen }; From 1a71b8f7e2f70d6a1898f568c072412333994b42 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sun, 19 May 2024 12:59:19 +0200 Subject: [PATCH 2/2] fix conversion from IpAddress::Ipv4 to in_addr IpAddress::Ipv4 contains alreads the IP address in big-endian formt. => no byte swapping needed With these changes, the demo httpd shows the correct address of the client. --- src/syscalls/socket.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/syscalls/socket.rs b/src/syscalls/socket.rs index 1ee8f82474..47a38ea012 100644 --- a/src/syscalls/socket.rs +++ b/src/syscalls/socket.rs @@ -103,7 +103,7 @@ impl From for IpListenEndpoint { if addr.sin_addr.s_addr == 0 { IpListenEndpoint { addr: None, port } } else { - let s_addr = addr.sin_addr.s_addr.to_be_bytes(); + let s_addr = addr.sin_addr.s_addr.to_ne_bytes(); let address = IpAddress::v4(s_addr[0], s_addr[1], s_addr[2], s_addr[3]); @@ -116,7 +116,7 @@ impl From for IpListenEndpoint { impl From for IpEndpoint { fn from(addr: sockaddr_in) -> IpEndpoint { let port = u16::from_be(addr.sin_port); - let s_addr = addr.sin_addr.s_addr.to_be_bytes(); + let s_addr = addr.sin_addr.s_addr.to_ne_bytes(); let address = IpAddress::v4(s_addr[0], s_addr[1], s_addr[2], s_addr[3]); IpEndpoint::from((address, port)) @@ -129,7 +129,7 @@ impl From for sockaddr_in { match endpoint.addr { IpAddress::Ipv4(ip) => { let sin_addr = in_addr { - s_addr: u32::from_be_bytes(ip.as_bytes().try_into().unwrap()), + s_addr: u32::from_ne_bytes(ip.as_bytes().try_into().unwrap()), }; Self {