mirror of
https://github.com/smoltcp-rs/smoltcp.git
synced 2025-10-02 15:15:05 +00:00
Return specific sockets from new
functions instead of Socket
.
* Add Into<Socket> implementations for sockets * Make SocketSet::add generic over Into<Socket>
This commit is contained in:
parent
f0c147a5ee
commit
c2d18ec071
@ -104,14 +104,14 @@ pub struct IcmpSocket<'a, 'b: 'a> {
|
|||||||
impl<'a, 'b> IcmpSocket<'a, 'b> {
|
impl<'a, 'b> IcmpSocket<'a, 'b> {
|
||||||
/// Create an ICMPv4 socket with the given buffers.
|
/// Create an ICMPv4 socket with the given buffers.
|
||||||
pub fn new(rx_buffer: SocketBuffer<'a, 'b>,
|
pub fn new(rx_buffer: SocketBuffer<'a, 'b>,
|
||||||
tx_buffer: SocketBuffer<'a, 'b>) -> Socket<'a, 'b> {
|
tx_buffer: SocketBuffer<'a, 'b>) -> IcmpSocket<'a, 'b> {
|
||||||
Socket::Icmp(IcmpSocket {
|
IcmpSocket {
|
||||||
meta: SocketMeta::default(),
|
meta: SocketMeta::default(),
|
||||||
rx_buffer: rx_buffer,
|
rx_buffer: rx_buffer,
|
||||||
tx_buffer: tx_buffer,
|
tx_buffer: tx_buffer,
|
||||||
endpoint: Endpoint::default(),
|
endpoint: Endpoint::default(),
|
||||||
hop_limit: None
|
hop_limit: None
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the socket handle.
|
/// Return the socket handle.
|
||||||
@ -170,10 +170,7 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|||||||
/// use smoltcp::socket::IcmpEndpoint;
|
/// use smoltcp::socket::IcmpEndpoint;
|
||||||
///
|
///
|
||||||
/// let mut icmp_socket = // ...
|
/// let mut icmp_socket = // ...
|
||||||
/// # match IcmpSocket::new(rx_buffer, tx_buffer) {
|
/// # IcmpSocket::new(rx_buffer, tx_buffer);
|
||||||
/// # Socket::Icmp(socket) => socket,
|
|
||||||
/// # _ => unreachable!()
|
|
||||||
/// # };
|
|
||||||
///
|
///
|
||||||
/// // Bind to ICMP error responses for UDP packets sent from port 53.
|
/// // Bind to ICMP error responses for UDP packets sent from port 53.
|
||||||
/// let endpoint = IpEndpoint::from(53);
|
/// let endpoint = IpEndpoint::from(53);
|
||||||
@ -194,10 +191,7 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|||||||
/// use smoltcp::socket::IcmpEndpoint;
|
/// use smoltcp::socket::IcmpEndpoint;
|
||||||
///
|
///
|
||||||
/// let mut icmp_socket = // ...
|
/// let mut icmp_socket = // ...
|
||||||
/// # match IcmpSocket::new(rx_buffer, tx_buffer) {
|
/// # IcmpSocket::new(rx_buffer, tx_buffer);
|
||||||
/// # Socket::Icmp(socket) => socket,
|
|
||||||
/// # _ => unreachable!()
|
|
||||||
/// # };
|
|
||||||
///
|
///
|
||||||
/// // Bind to ICMP messages with the ICMP identifier 0x1234
|
/// // Bind to ICMP messages with the ICMP identifier 0x1234
|
||||||
/// icmp_socket.bind(IcmpEndpoint::Ident(0x1234)).unwrap();
|
/// icmp_socket.bind(IcmpEndpoint::Ident(0x1234)).unwrap();
|
||||||
@ -360,6 +354,12 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Into<Socket<'a, 'b>> for IcmpSocket<'a, 'b> {
|
||||||
|
fn into(self) -> Socket<'a, 'b> {
|
||||||
|
Socket::Icmp(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use phy::DeviceCapabilities;
|
use phy::DeviceCapabilities;
|
||||||
@ -376,10 +376,7 @@ mod test {
|
|||||||
|
|
||||||
fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
||||||
tx_buffer: SocketBuffer<'static, 'static>) -> IcmpSocket<'static, 'static> {
|
tx_buffer: SocketBuffer<'static, 'static>) -> IcmpSocket<'static, 'static> {
|
||||||
match IcmpSocket::new(rx_buffer, tx_buffer) {
|
IcmpSocket::new(rx_buffer, tx_buffer)
|
||||||
Socket::Icmp(socket) => socket,
|
|
||||||
_ => unreachable!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const REMOTE_IPV4: Ipv4Address = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
|
const REMOTE_IPV4: Ipv4Address = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
|
||||||
|
@ -73,14 +73,14 @@ impl<'a, 'b> RawSocket<'a, 'b> {
|
|||||||
/// with the given buffers.
|
/// with the given buffers.
|
||||||
pub fn new(ip_version: IpVersion, ip_protocol: IpProtocol,
|
pub fn new(ip_version: IpVersion, ip_protocol: IpProtocol,
|
||||||
rx_buffer: SocketBuffer<'a, 'b>,
|
rx_buffer: SocketBuffer<'a, 'b>,
|
||||||
tx_buffer: SocketBuffer<'a, 'b>) -> Socket<'a, 'b> {
|
tx_buffer: SocketBuffer<'a, 'b>) -> RawSocket<'a, 'b> {
|
||||||
Socket::Raw(RawSocket {
|
RawSocket {
|
||||||
meta: SocketMeta::default(),
|
meta: SocketMeta::default(),
|
||||||
ip_version,
|
ip_version,
|
||||||
ip_protocol,
|
ip_protocol,
|
||||||
rx_buffer,
|
rx_buffer,
|
||||||
tx_buffer,
|
tx_buffer,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the socket handle.
|
/// Return the socket handle.
|
||||||
@ -251,6 +251,12 @@ impl<'a, 'b> RawSocket<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Into<Socket<'a, 'b>> for RawSocket<'a, 'b> {
|
||||||
|
fn into(self) -> Socket<'a, 'b> {
|
||||||
|
Socket::Raw(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use wire::IpRepr;
|
use wire::IpRepr;
|
||||||
@ -275,11 +281,8 @@ mod test {
|
|||||||
pub fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
pub fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
||||||
tx_buffer: SocketBuffer<'static, 'static>)
|
tx_buffer: SocketBuffer<'static, 'static>)
|
||||||
-> RawSocket<'static, 'static> {
|
-> RawSocket<'static, 'static> {
|
||||||
match RawSocket::new(IpVersion::Ipv4, IpProtocol::Unknown(IP_PROTO),
|
RawSocket::new(IpVersion::Ipv4, IpProtocol::Unknown(IP_PROTO),
|
||||||
rx_buffer, tx_buffer) {
|
rx_buffer, tx_buffer)
|
||||||
Socket::Raw(socket) => socket,
|
|
||||||
_ => unreachable!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const IP_PROTO: u8 = 63;
|
pub const IP_PROTO: u8 = 63;
|
||||||
@ -310,11 +313,8 @@ mod test {
|
|||||||
pub fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
pub fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
||||||
tx_buffer: SocketBuffer<'static, 'static>)
|
tx_buffer: SocketBuffer<'static, 'static>)
|
||||||
-> RawSocket<'static, 'static> {
|
-> RawSocket<'static, 'static> {
|
||||||
match RawSocket::new(IpVersion::Ipv6, IpProtocol::Unknown(IP_PROTO),
|
RawSocket::new(IpVersion::Ipv6, IpProtocol::Unknown(IP_PROTO),
|
||||||
rx_buffer, tx_buffer) {
|
rx_buffer, tx_buffer)
|
||||||
Socket::Raw(socket) => socket,
|
|
||||||
_ => unreachable!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const IP_PROTO: u8 = 63;
|
pub const IP_PROTO: u8 = 63;
|
||||||
@ -514,24 +514,16 @@ mod test {
|
|||||||
fn test_doesnt_accept_wrong_proto() {
|
fn test_doesnt_accept_wrong_proto() {
|
||||||
#[cfg(feature = "proto-ipv4")]
|
#[cfg(feature = "proto-ipv4")]
|
||||||
{
|
{
|
||||||
let socket = match RawSocket::new(IpVersion::Ipv4,
|
let socket = RawSocket::new(IpVersion::Ipv4,
|
||||||
IpProtocol::Unknown(ipv4_locals::IP_PROTO+1),
|
IpProtocol::Unknown(ipv4_locals::IP_PROTO+1), buffer(1), buffer(1));
|
||||||
buffer(1), buffer(1)) {
|
|
||||||
Socket::Raw(socket) => socket,
|
|
||||||
_ => unreachable!()
|
|
||||||
};
|
|
||||||
assert!(!socket.accepts(&ipv4_locals::HEADER_REPR));
|
assert!(!socket.accepts(&ipv4_locals::HEADER_REPR));
|
||||||
#[cfg(feature = "proto-ipv6")]
|
#[cfg(feature = "proto-ipv6")]
|
||||||
assert!(!socket.accepts(&ipv6_locals::HEADER_REPR));
|
assert!(!socket.accepts(&ipv6_locals::HEADER_REPR));
|
||||||
}
|
}
|
||||||
#[cfg(feature = "proto-ipv6")]
|
#[cfg(feature = "proto-ipv6")]
|
||||||
{
|
{
|
||||||
let socket = match RawSocket::new(IpVersion::Ipv6,
|
let socket = RawSocket::new(IpVersion::Ipv6,
|
||||||
IpProtocol::Unknown(ipv6_locals::IP_PROTO+1),
|
IpProtocol::Unknown(ipv6_locals::IP_PROTO+1), buffer(1), buffer(1));
|
||||||
buffer(1), buffer(1)) {
|
|
||||||
Socket::Raw(socket) => socket,
|
|
||||||
_ => unreachable!()
|
|
||||||
};
|
|
||||||
assert!(!socket.accepts(&ipv6_locals::HEADER_REPR));
|
assert!(!socket.accepts(&ipv6_locals::HEADER_REPR));
|
||||||
#[cfg(feature = "proto-ipv4")]
|
#[cfg(feature = "proto-ipv4")]
|
||||||
assert!(!socket.accepts(&ipv4_locals::HEADER_REPR));
|
assert!(!socket.accepts(&ipv4_locals::HEADER_REPR));
|
||||||
|
@ -47,7 +47,9 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
|||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// This function panics if the storage is fixed-size (not a `Vec`) and is full.
|
/// This function panics if the storage is fixed-size (not a `Vec`) and is full.
|
||||||
pub fn add(&mut self, socket: Socket<'b, 'c>) -> Handle {
|
pub fn add<T>(&mut self, socket: T) -> Handle
|
||||||
|
where T: Into<Socket<'b, 'c>>
|
||||||
|
{
|
||||||
fn put<'b, 'c>(index: usize, slot: &mut Option<Item<'b, 'c>>,
|
fn put<'b, 'c>(index: usize, slot: &mut Option<Item<'b, 'c>>,
|
||||||
mut socket: Socket<'b, 'c>) -> Handle {
|
mut socket: Socket<'b, 'c>) -> Handle {
|
||||||
net_trace!("[{}]: adding", index);
|
net_trace!("[{}]: adding", index);
|
||||||
@ -57,6 +59,8 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
|||||||
handle
|
handle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let socket = socket.into();
|
||||||
|
|
||||||
for (index, slot) in self.sockets.iter_mut().enumerate() {
|
for (index, slot) in self.sockets.iter_mut().enumerate() {
|
||||||
if slot.is_none() {
|
if slot.is_none() {
|
||||||
return put(index, slot, socket)
|
return put(index, slot, socket)
|
||||||
|
@ -230,7 +230,7 @@ const DEFAULT_MSS: usize = 536;
|
|||||||
|
|
||||||
impl<'a> TcpSocket<'a> {
|
impl<'a> TcpSocket<'a> {
|
||||||
/// Create a socket using the given buffers.
|
/// Create a socket using the given buffers.
|
||||||
pub fn new<T>(rx_buffer: T, tx_buffer: T) -> Socket<'a, 'static>
|
pub fn new<T>(rx_buffer: T, tx_buffer: T) -> TcpSocket<'a>
|
||||||
where T: Into<SocketBuffer<'a>> {
|
where T: Into<SocketBuffer<'a>> {
|
||||||
let (rx_buffer, tx_buffer) = (rx_buffer.into(), tx_buffer.into());
|
let (rx_buffer, tx_buffer) = (rx_buffer.into(), tx_buffer.into());
|
||||||
if rx_buffer.capacity() > <u16>::max_value() as usize {
|
if rx_buffer.capacity() > <u16>::max_value() as usize {
|
||||||
@ -238,7 +238,7 @@ impl<'a> TcpSocket<'a> {
|
|||||||
<u16>::max_value())
|
<u16>::max_value())
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::Tcp(TcpSocket {
|
TcpSocket {
|
||||||
meta: SocketMeta::default(),
|
meta: SocketMeta::default(),
|
||||||
state: State::Closed,
|
state: State::Closed,
|
||||||
timer: Timer::default(),
|
timer: Timer::default(),
|
||||||
@ -259,7 +259,7 @@ impl<'a> TcpSocket<'a> {
|
|||||||
remote_win_len: 0,
|
remote_win_len: 0,
|
||||||
remote_mss: DEFAULT_MSS,
|
remote_mss: DEFAULT_MSS,
|
||||||
remote_last_ts: None,
|
remote_last_ts: None,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the socket handle.
|
/// Return the socket handle.
|
||||||
@ -1495,6 +1495,12 @@ impl<'a> TcpSocket<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> Into<Socket<'a, 'static>> for TcpSocket<'a> {
|
||||||
|
fn into(self) -> Socket<'a, 'static> {
|
||||||
|
Socket::Tcp(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> fmt::Write for TcpSocket<'a> {
|
impl<'a> fmt::Write for TcpSocket<'a> {
|
||||||
fn write_str(&mut self, slice: &str) -> fmt::Result {
|
fn write_str(&mut self, slice: &str) -> fmt::Result {
|
||||||
let slice = slice.as_bytes();
|
let slice = slice.as_bytes();
|
||||||
@ -1680,10 +1686,7 @@ mod test {
|
|||||||
|
|
||||||
let rx_buffer = SocketBuffer::new(vec![0; 64]);
|
let rx_buffer = SocketBuffer::new(vec![0; 64]);
|
||||||
let tx_buffer = SocketBuffer::new(vec![0; 64]);
|
let tx_buffer = SocketBuffer::new(vec![0; 64]);
|
||||||
match TcpSocket::new(rx_buffer, tx_buffer) {
|
TcpSocket::new(rx_buffer, tx_buffer)
|
||||||
Socket::Tcp(socket) => socket,
|
|
||||||
_ => unreachable!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn socket_syn_received() -> TcpSocket<'static> {
|
fn socket_syn_received() -> TcpSocket<'static> {
|
||||||
|
@ -70,14 +70,14 @@ pub struct UdpSocket<'a, 'b: 'a> {
|
|||||||
impl<'a, 'b> UdpSocket<'a, 'b> {
|
impl<'a, 'b> UdpSocket<'a, 'b> {
|
||||||
/// Create an UDP socket with the given buffers.
|
/// Create an UDP socket with the given buffers.
|
||||||
pub fn new(rx_buffer: SocketBuffer<'a, 'b>,
|
pub fn new(rx_buffer: SocketBuffer<'a, 'b>,
|
||||||
tx_buffer: SocketBuffer<'a, 'b>) -> Socket<'a, 'b> {
|
tx_buffer: SocketBuffer<'a, 'b>) -> UdpSocket<'a, 'b> {
|
||||||
Socket::Udp(UdpSocket {
|
UdpSocket {
|
||||||
meta: SocketMeta::default(),
|
meta: SocketMeta::default(),
|
||||||
endpoint: IpEndpoint::default(),
|
endpoint: IpEndpoint::default(),
|
||||||
rx_buffer: rx_buffer,
|
rx_buffer: rx_buffer,
|
||||||
tx_buffer: tx_buffer,
|
tx_buffer: tx_buffer,
|
||||||
hop_limit: None
|
hop_limit: None
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the socket handle.
|
/// Return the socket handle.
|
||||||
@ -256,6 +256,12 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Into<Socket<'a, 'b>> for UdpSocket<'a, 'b> {
|
||||||
|
fn into(self) -> Socket<'a, 'b> {
|
||||||
|
Socket::Udp(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use wire::{IpAddress, IpRepr, UdpRepr};
|
use wire::{IpAddress, IpRepr, UdpRepr};
|
||||||
@ -277,10 +283,7 @@ mod test {
|
|||||||
fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
fn socket(rx_buffer: SocketBuffer<'static, 'static>,
|
||||||
tx_buffer: SocketBuffer<'static, 'static>)
|
tx_buffer: SocketBuffer<'static, 'static>)
|
||||||
-> UdpSocket<'static, 'static> {
|
-> UdpSocket<'static, 'static> {
|
||||||
match UdpSocket::new(rx_buffer, tx_buffer) {
|
UdpSocket::new(rx_buffer, tx_buffer)
|
||||||
Socket::Udp(socket) => socket,
|
|
||||||
_ => unreachable!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const LOCAL_PORT: u16 = 53;
|
const LOCAL_PORT: u16 = 53;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user