Move IpEndpoint to socket/ip/addr.rs

This commit is contained in:
Ruihan Li
2024-07-26 15:43:13 +08:00
committed by Tate, Hongliang Tian
parent b11628b9ce
commit d814603504
16 changed files with 69 additions and 55 deletions

View File

@ -1,7 +1,11 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use super::{Iface, IpAddress, IpEndpoint}; use super::Iface;
use crate::{events::Observer, prelude::*}; use crate::{
events::Observer,
net::socket::ip::{IpAddress, IpEndpoint},
prelude::*,
};
pub type RawTcpSocket = smoltcp::socket::tcp::Socket<'static>; pub type RawTcpSocket = smoltcp::socket::tcp::Socket<'static>;
pub type RawUdpSocket = smoltcp::socket::udp::Socket<'static>; pub type RawUdpSocket = smoltcp::socket::udp::Socket<'static>;

View File

@ -15,9 +15,9 @@ use super::{
any_socket::{AnyBoundSocketInner, AnyRawSocket, AnyUnboundSocket, SocketFamily}, any_socket::{AnyBoundSocketInner, AnyRawSocket, AnyUnboundSocket, SocketFamily},
time::get_network_timestamp, time::get_network_timestamp,
util::BindPortConfig, util::BindPortConfig,
AnyBoundSocket, Iface, Ipv4Address, AnyBoundSocket, Iface,
}; };
use crate::prelude::*; use crate::{net::socket::ip::Ipv4Address, prelude::*};
pub struct IfaceCommon { pub struct IfaceCommon {
interface: SpinLock<smoltcp::iface::Interface>, interface: SpinLock<smoltcp::iface::Interface>,

View File

@ -6,8 +6,11 @@ use smoltcp::{
wire::IpCidr, wire::IpCidr,
}; };
use super::{common::IfaceCommon, internal::IfaceInternal, Iface, IpAddress, Ipv4Address}; use super::{common::IfaceCommon, internal::IfaceInternal, Iface};
use crate::prelude::*; use crate::{
net::socket::ip::{IpAddress, Ipv4Address},
prelude::*,
};
pub const LOOPBACK_ADDRESS: IpAddress = { pub const LOOPBACK_ADDRESS: IpAddress = {
let ipv4_addr = Ipv4Address::new(127, 0, 0, 1); let ipv4_addr = Ipv4Address::new(127, 0, 0, 1);

View File

@ -17,10 +17,12 @@ 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, Ipv4Address}; pub use smoltcp::wire::EthernetAddress;
pub use util::{spawn_background_poll_thread, BindPortConfig}; pub use util::{spawn_background_poll_thread, BindPortConfig};
pub use virtio::IfaceVirtio; pub use virtio::IfaceVirtio;
use crate::net::socket::ip::Ipv4Address;
/// Network interface. /// Network interface.
/// ///
/// A network interface (abbreviated as iface) is a hardware or software component that connects a device or computer to a network. /// A network interface (abbreviated as iface) is a hardware or software component that connects a device or computer to a network.

View File

@ -0,0 +1,31 @@
// SPDX-License-Identifier: MPL-2.0
pub use smoltcp::wire::{IpAddress, IpEndpoint, Ipv4Address};
use crate::{net::socket::SocketAddr, prelude::*, return_errno_with_message};
pub type PortNum = u16;
impl TryFrom<SocketAddr> for IpEndpoint {
type Error = Error;
fn try_from(value: SocketAddr) -> Result<Self> {
match value {
SocketAddr::IPv4(addr, port) => Ok(IpEndpoint::new(addr.into_address(), port)),
_ => return_errno_with_message!(
Errno::EAFNOSUPPORT,
"the address is in an unsupported address family"
),
}
}
}
impl From<IpEndpoint> for SocketAddr {
fn from(endpoint: IpEndpoint) -> Self {
let port = endpoint.port;
match endpoint.addr {
IpAddress::Ipv4(addr) => SocketAddr::IPv4(addr, port),
// TODO: support IPv6
}
}
}

View File

@ -1,8 +1,9 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use super::{IpAddress, IpEndpoint};
use crate::{ use crate::{
net::{ net::{
iface::{AnyBoundSocket, AnyUnboundSocket, BindPortConfig, Iface, IpAddress, IpEndpoint}, iface::{AnyBoundSocket, AnyUnboundSocket, BindPortConfig, Iface},
IFACES, IFACES,
}, },
prelude::*, prelude::*,

View File

@ -2,10 +2,11 @@
use smoltcp::socket::udp::{RecvError, SendError}; use smoltcp::socket::udp::{RecvError, SendError};
use super::IpEndpoint;
use crate::{ use crate::{
events::IoEvents, events::IoEvents,
net::{ net::{
iface::{AnyBoundSocket, IpEndpoint, RawUdpSocket}, iface::{AnyBoundSocket, RawUdpSocket},
socket::util::send_recv_flags::SendRecvFlags, socket::util::send_recv_flags::SendRecvFlags,
}, },
prelude::*, prelude::*,

View File

@ -5,12 +5,11 @@ use core::sync::atomic::{AtomicBool, Ordering};
use takeable::Takeable; use takeable::Takeable;
use self::{bound::BoundDatagram, unbound::UnboundDatagram}; use self::{bound::BoundDatagram, unbound::UnboundDatagram};
use super::{common::get_ephemeral_endpoint, UNSPECIFIED_LOCAL_ENDPOINT}; use super::{common::get_ephemeral_endpoint, IpEndpoint, UNSPECIFIED_LOCAL_ENDPOINT};
use crate::{ use crate::{
events::{IoEvents, Observer}, events::{IoEvents, Observer},
fs::{file_handle::FileLike, utils::StatusFlags}, fs::{file_handle::FileLike, utils::StatusFlags},
net::{ net::{
iface::IpEndpoint,
poll_ifaces, poll_ifaces,
socket::{ socket::{
util::{ util::{

View File

@ -2,11 +2,11 @@
use alloc::sync::Weak; use alloc::sync::Weak;
use super::bound::BoundDatagram; use super::{bound::BoundDatagram, IpEndpoint};
use crate::{ use crate::{
events::{IoEvents, Observer}, events::{IoEvents, Observer},
net::{ net::{
iface::{AnyUnboundSocket, IpEndpoint, RawUdpSocket}, iface::{AnyUnboundSocket, RawUdpSocket},
socket::ip::common::bind_socket, socket::ip::common::bind_socket,
}, },
prelude::*, prelude::*,

View File

@ -1,11 +1,11 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use crate::net::iface::{IpAddress, IpEndpoint, Ipv4Address}; mod addr;
mod common; mod common;
mod datagram; mod datagram;
pub mod stream; pub mod stream;
pub use addr::{IpAddress, IpEndpoint, Ipv4Address, PortNum};
pub use datagram::DatagramSocket; pub use datagram::DatagramSocket;
pub use stream::StreamSocket; pub use stream::StreamSocket;

View File

@ -4,10 +4,11 @@ use alloc::sync::Weak;
use smoltcp::socket::tcp::{RecvError, SendError}; use smoltcp::socket::tcp::{RecvError, SendError};
use super::IpEndpoint;
use crate::{ use crate::{
events::{IoEvents, Observer}, events::{IoEvents, Observer},
net::{ net::{
iface::{AnyBoundSocket, IpEndpoint, RawTcpSocket}, iface::{AnyBoundSocket, RawTcpSocket},
socket::util::{send_recv_flags::SendRecvFlags, shutdown_cmd::SockShutdownCmd}, socket::util::{send_recv_flags::SendRecvFlags, shutdown_cmd::SockShutdownCmd},
}, },
prelude::*, prelude::*,

View File

@ -1,8 +1,8 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use super::{connected::ConnectedStream, init::InitStream}; use super::{connected::ConnectedStream, init::InitStream, IpEndpoint};
use crate::{ use crate::{
net::iface::{AnyBoundSocket, IpEndpoint, RawTcpSocket}, net::iface::{AnyBoundSocket, RawTcpSocket},
prelude::*, prelude::*,
process::signal::Pollee, process::signal::Pollee,
}; };

View File

@ -2,11 +2,11 @@
use alloc::sync::Weak; use alloc::sync::Weak;
use super::{connecting::ConnectingStream, listen::ListenStream}; use super::{connecting::ConnectingStream, listen::ListenStream, IpEndpoint};
use crate::{ use crate::{
events::{IoEvents, Observer}, events::{IoEvents, Observer},
net::{ net::{
iface::{AnyBoundSocket, AnyUnboundSocket, IpEndpoint}, iface::{AnyBoundSocket, AnyUnboundSocket},
socket::ip::common::{bind_socket, get_ephemeral_endpoint}, socket::ip::common::{bind_socket, get_ephemeral_endpoint},
}, },
prelude::*, prelude::*,

View File

@ -2,10 +2,10 @@
use smoltcp::socket::tcp::ListenError; use smoltcp::socket::tcp::ListenError;
use super::connected::ConnectedStream; use super::{connected::ConnectedStream, IpEndpoint};
use crate::{ use crate::{
events::IoEvents, events::IoEvents,
net::iface::{AnyBoundSocket, AnyUnboundSocket, BindPortConfig, IpEndpoint, RawTcpSocket}, net::iface::{AnyBoundSocket, AnyUnboundSocket, BindPortConfig, RawTcpSocket},
prelude::*, prelude::*,
process::signal::Pollee, process::signal::Pollee,
}; };

View File

@ -1,15 +1,14 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use crate::{ use crate::{
net::{ net::socket::{
iface::{IpAddress, IpEndpoint, Ipv4Address}, ip::{Ipv4Address, PortNum},
socket::{unix::UnixSocketAddr, vsock::addr::VsockSocketAddr}, unix::UnixSocketAddr,
vsock::addr::VsockSocketAddr,
}, },
prelude::*, prelude::*,
}; };
type PortNum = u16;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum SocketAddr { pub enum SocketAddr {
Unix(UnixSocketAddr), Unix(UnixSocketAddr),
@ -17,27 +16,3 @@ pub enum SocketAddr {
IPv6, IPv6,
Vsock(VsockSocketAddr), Vsock(VsockSocketAddr),
} }
impl TryFrom<SocketAddr> for IpEndpoint {
type Error = Error;
fn try_from(value: SocketAddr) -> Result<Self> {
match value {
SocketAddr::IPv4(addr, port) => Ok(IpEndpoint::new(addr.into_address(), port)),
_ => return_errno_with_message!(
Errno::EAFNOSUPPORT,
"the address is in an unsupported address family"
),
}
}
}
impl From<IpEndpoint> for SocketAddr {
fn from(endpoint: IpEndpoint) -> Self {
let port = endpoint.port;
match endpoint.addr {
IpAddress::Ipv4(addr) => SocketAddr::IPv4(addr, port),
// TODO: support IPv6
}
}
}

View File

@ -4,10 +4,7 @@
#![allow(unused_variables)] #![allow(unused_variables)]
use crate::{ use crate::{
net::{ net::socket::{ip::Ipv4Address, unix::UnixSocketAddr, vsock::VsockSocketAddr, SocketAddr},
iface::Ipv4Address,
socket::{unix::UnixSocketAddr, vsock::VsockSocketAddr, SocketAddr},
},
prelude::*, prelude::*,
util::{read_bytes_from_user, read_val_from_user, write_val_to_user}, util::{read_bytes_from_user, read_val_from_user, write_val_to_user},
}; };