Use From<IpEndpoint> for SockAddr (not TryFrom)

This commit is contained in:
Ruihan Li
2024-01-08 23:34:04 +08:00
committed by Tate, Hongliang Tian
parent b53980da25
commit 8b094454aa
4 changed files with 17 additions and 32 deletions

View File

@ -17,7 +17,7 @@ pub use any_socket::{
AnyBoundSocket, AnyUnboundSocket, RawTcpSocket, RawUdpSocket, RECV_BUF_LEN, SEND_BUF_LEN, AnyBoundSocket, AnyUnboundSocket, RawTcpSocket, RawUdpSocket, RECV_BUF_LEN, SEND_BUF_LEN,
}; };
pub use loopback::IfaceLoopback; pub use loopback::IfaceLoopback;
pub use smoltcp::wire::{EthernetAddress, IpAddress, IpEndpoint, IpListenEndpoint, Ipv4Address}; pub use smoltcp::wire::{EthernetAddress, IpAddress, IpEndpoint, Ipv4Address};
pub use util::{spawn_background_poll_thread, BindPortConfig}; pub use util::{spawn_background_poll_thread, BindPortConfig};
pub use virtio::IfaceVirtio; pub use virtio::IfaceVirtio;

View File

@ -115,7 +115,7 @@ impl DatagramSocket {
}; };
let (recv_bytes, remote_endpoint) = bound_datagram.try_recvfrom(buf, flags)?; let (recv_bytes, remote_endpoint) = bound_datagram.try_recvfrom(buf, flags)?;
bound_datagram.update_io_events(&self.pollee); bound_datagram.update_io_events(&self.pollee);
Ok((recv_bytes, remote_endpoint.try_into()?)) Ok((recv_bytes, remote_endpoint.into()))
} }
fn try_sendto( fn try_sendto(
@ -240,7 +240,7 @@ impl Socket for DatagramSocket {
let Inner::Bound(bound_datagram) = inner.as_ref() else { let Inner::Bound(bound_datagram) = inner.as_ref() else {
return_errno_with_message!(Errno::EINVAL, "the socket is not bound"); return_errno_with_message!(Errno::EINVAL, "the socket is not bound");
}; };
bound_datagram.local_endpoint().try_into() Ok(bound_datagram.local_endpoint().into())
} }
fn peer_addr(&self) -> Result<SocketAddr> { fn peer_addr(&self) -> Result<SocketAddr> {
@ -248,7 +248,7 @@ impl Socket for DatagramSocket {
let Inner::Bound(bound_datagram) = inner.as_ref() else { let Inner::Bound(bound_datagram) = inner.as_ref() else {
return_errno_with_message!(Errno::EINVAL, "the socket is not bound"); return_errno_with_message!(Errno::EINVAL, "the socket is not bound");
}; };
bound_datagram.remote_endpoint()?.try_into() Ok(bound_datagram.remote_endpoint()?.into())
} }
// FIXME: respect RecvFromFlags // FIXME: respect RecvFromFlags

View File

@ -177,7 +177,7 @@ impl StreamSocket {
let remote_endpoint = connected_stream.remote_endpoint(); let remote_endpoint = connected_stream.remote_endpoint();
let accepted_socket = Self::new_connected(connected_stream); let accepted_socket = Self::new_connected(connected_stream);
Ok((accepted_socket, remote_endpoint.try_into()?)) Ok((accepted_socket, remote_endpoint.into()))
} }
fn try_recvfrom(&self, buf: &mut [u8], flags: SendRecvFlags) -> Result<(usize, SocketAddr)> { fn try_recvfrom(&self, buf: &mut [u8], flags: SendRecvFlags) -> Result<(usize, SocketAddr)> {
@ -188,7 +188,7 @@ impl StreamSocket {
}; };
let recv_bytes = connected_stream.try_recvfrom(buf, flags)?; let recv_bytes = connected_stream.try_recvfrom(buf, flags)?;
connected_stream.update_io_events(&self.pollee); connected_stream.update_io_events(&self.pollee);
Ok((recv_bytes, connected_stream.remote_endpoint().try_into()?)) Ok((recv_bytes, connected_stream.remote_endpoint().into()))
} }
fn try_sendto(&self, buf: &[u8], flags: SendRecvFlags) -> Result<usize> { fn try_sendto(&self, buf: &[u8], flags: SendRecvFlags) -> Result<usize> {
@ -390,7 +390,7 @@ impl Socket for StreamSocket {
State::Listen(listen_stream) => listen_stream.local_endpoint(), State::Listen(listen_stream) => listen_stream.local_endpoint(),
State::Connected(connected_stream) => connected_stream.local_endpoint(), State::Connected(connected_stream) => connected_stream.local_endpoint(),
}; };
local_endpoint.try_into() Ok(local_endpoint.into())
} }
fn peer_addr(&self) -> Result<SocketAddr> { fn peer_addr(&self) -> Result<SocketAddr> {
@ -405,7 +405,7 @@ impl Socket for StreamSocket {
} }
State::Connected(connected_stream) => connected_stream.remote_endpoint(), State::Connected(connected_stream) => connected_stream.remote_endpoint(),
}; };
remote_endpoint.try_into() Ok(remote_endpoint.into())
} }
fn recvfrom(&self, buf: &mut [u8], flags: SendRecvFlags) -> Result<(usize, SocketAddr)> { fn recvfrom(&self, buf: &mut [u8], flags: SendRecvFlags) -> Result<(usize, SocketAddr)> {

View File

@ -2,7 +2,7 @@
use crate::{ use crate::{
net::{ net::{
iface::{IpAddress, IpEndpoint, IpListenEndpoint, Ipv4Address}, iface::{IpAddress, IpEndpoint, Ipv4Address},
socket::unix::UnixSocketAddr, socket::unix::UnixSocketAddr,
}, },
prelude::*, prelude::*,
@ -24,34 +24,19 @@ impl TryFrom<SocketAddr> for IpEndpoint {
match value { match value {
SocketAddr::IPv4(addr, port) => Ok(IpEndpoint::new(addr.into_address(), port)), SocketAddr::IPv4(addr, port) => Ok(IpEndpoint::new(addr.into_address(), port)),
_ => return_errno_with_message!( _ => return_errno_with_message!(
Errno::EINVAL, Errno::EAFNOSUPPORT,
"sock addr cannot be converted as IpEndpoint" "the address is in an unsupported address family"
), ),
} }
} }
} }
impl TryFrom<IpEndpoint> for SocketAddr { impl From<IpEndpoint> for SocketAddr {
type Error = Error; fn from(endpoint: IpEndpoint) -> Self {
fn try_from(endpoint: IpEndpoint) -> Result<Self> {
let port = endpoint.port; let port = endpoint.port;
let socket_addr = match endpoint.addr { match endpoint.addr {
IpAddress::Ipv4(addr) => SocketAddr::IPv4(addr, port), // TODO: support IPv6 IpAddress::Ipv4(addr) => SocketAddr::IPv4(addr, port),
}; // TODO: support IPv6
Ok(socket_addr)
} }
} }
impl TryFrom<IpListenEndpoint> for SocketAddr {
type Error = Error;
fn try_from(value: IpListenEndpoint) -> Result<Self> {
let port = value.port;
let socket_addr = match value.addr {
None => return_errno_with_message!(Errno::EINVAL, "address is unspecified"),
Some(IpAddress::Ipv4(address)) => SocketAddr::IPv4(address, port),
};
Ok(socket_addr)
}
} }