Browse Source

socket: reorganize module structure, with one module per protocol.

Dario Nieuwenhuis 2 years ago
parent
commit
72a9ee46ce

+ 9 - 9
examples/benchmark.rs

@@ -13,7 +13,7 @@ use std::thread;
 
 use smoltcp::iface::{InterfaceBuilder, NeighborCache};
 use smoltcp::phy::{wait as phy_wait, Device, Medium};
-use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
+use smoltcp::socket::tcp;
 use smoltcp::time::{Duration, Instant};
 use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
 
@@ -85,13 +85,13 @@ fn main() {
 
     let neighbor_cache = NeighborCache::new(BTreeMap::new());
 
-    let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
+    let tcp1_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp1_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp1_socket = tcp::Socket::new(tcp1_rx_buffer, tcp1_tx_buffer);
 
-    let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
+    let tcp2_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp2_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp2_socket = tcp::Socket::new(tcp2_rx_buffer, tcp2_tx_buffer);
 
     let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
     let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)];
@@ -119,7 +119,7 @@ fn main() {
         }
 
         // tcp:1234: emit data
-        let socket = iface.get_socket::<TcpSocket>(tcp1_handle);
+        let socket = iface.get_socket::<tcp::Socket>(tcp1_handle);
         if !socket.is_open() {
             socket.listen(1234).unwrap();
         }
@@ -137,7 +137,7 @@ fn main() {
         }
 
         // tcp:1235: sink data
-        let socket = iface.get_socket::<TcpSocket>(tcp2_handle);
+        let socket = iface.get_socket::<tcp::Socket>(tcp2_handle);
         if !socket.is_open() {
             socket.listen(1235).unwrap();
         }

+ 6 - 6
examples/client.rs

@@ -7,7 +7,7 @@ use std::str::{self, FromStr};
 
 use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes};
 use smoltcp::phy::{wait as phy_wait, Device, Medium};
-use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
+use smoltcp::socket::tcp;
 use smoltcp::time::Instant;
 use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
 
@@ -30,9 +30,9 @@ fn main() {
 
     let neighbor_cache = NeighborCache::new(BTreeMap::new());
 
-    let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
-    let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
-    let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
+    let tcp_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
+    let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
+    let tcp_socket = tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer);
 
     let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
     let ip_addrs = [IpCidr::new(IpAddress::v4(192, 168, 69, 2), 24)];
@@ -54,7 +54,7 @@ fn main() {
 
     let tcp_handle = iface.add_socket(tcp_socket);
 
-    let (socket, cx) = iface.get_socket_and_context::<TcpSocket>(tcp_handle);
+    let (socket, cx) = iface.get_socket_and_context::<tcp::Socket>(tcp_handle);
     socket.connect(cx, (address, port), 49500).unwrap();
 
     let mut tcp_active = false;
@@ -67,7 +67,7 @@ fn main() {
             }
         }
 
-        let socket = iface.get_socket::<TcpSocket>(tcp_handle);
+        let socket = iface.get_socket::<tcp::Socket>(tcp_handle);
         if socket.is_active() && !tcp_active {
             debug!("connected");
         } else if !socket.is_active() && tcp_active {

+ 5 - 5
examples/dhcp_client.rs

@@ -6,7 +6,7 @@ use std::collections::BTreeMap;
 use std::os::unix::io::AsRawFd;
 
 use smoltcp::iface::{Interface, InterfaceBuilder, NeighborCache, Routes};
-use smoltcp::socket::{Dhcpv4Event, Dhcpv4Socket};
+use smoltcp::socket::dhcpv4;
 use smoltcp::time::Instant;
 use smoltcp::wire::{EthernetAddress, IpCidr, Ipv4Address, Ipv4Cidr};
 use smoltcp::{
@@ -44,7 +44,7 @@ fn main() {
     }
     let mut iface = builder.finalize();
 
-    let mut dhcp_socket = Dhcpv4Socket::new();
+    let mut dhcp_socket = dhcpv4::Socket::new();
 
     // Set a ridiculously short max lease time to show DHCP renews work properly.
     // This will cause the DHCP client to start renewing after 5 seconds, and give up the
@@ -60,10 +60,10 @@ fn main() {
             debug!("poll error: {}", e);
         }
 
-        let event = iface.get_socket::<Dhcpv4Socket>(dhcp_handle).poll();
+        let event = iface.get_socket::<dhcpv4::Socket>(dhcp_handle).poll();
         match event {
             None => {}
-            Some(Dhcpv4Event::Configured(config)) => {
+            Some(dhcpv4::Event::Configured(config)) => {
                 debug!("DHCP config acquired!");
 
                 debug!("IP address:      {}", config.address);
@@ -83,7 +83,7 @@ fn main() {
                     }
                 }
             }
-            Some(Dhcpv4Event::Deconfigured) => {
+            Some(dhcpv4::Event::Deconfigured) => {
                 debug!("DHCP lost config!");
                 set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
                 iface.routes_mut().remove_default_ipv4_route();

+ 4 - 4
examples/dns.rs

@@ -10,7 +10,7 @@ mod utils;
 use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes};
 use smoltcp::phy::Device;
 use smoltcp::phy::{wait as phy_wait, Medium};
-use smoltcp::socket::DnsSocket;
+use smoltcp::socket::dns;
 use smoltcp::time::Instant;
 use smoltcp::wire::{
     EthernetAddress, HardwareAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address,
@@ -39,7 +39,7 @@ fn main() {
         Ipv4Address::new(8, 8, 4, 4).into(),
         Ipv4Address::new(8, 8, 8, 8).into(),
     ];
-    let dns_socket = DnsSocket::new(servers, vec![]);
+    let dns_socket = dns::Socket::new(servers, vec![]);
 
     let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
     let src_ipv6 = IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1);
@@ -68,7 +68,7 @@ fn main() {
 
     let dns_handle = iface.add_socket(dns_socket);
 
-    let (socket, cx) = iface.get_socket_and_context::<DnsSocket>(dns_handle);
+    let (socket, cx) = iface.get_socket_and_context::<dns::Socket>(dns_handle);
     let query = socket.start_query(cx, name).unwrap();
 
     loop {
@@ -83,7 +83,7 @@ fn main() {
         }
 
         match iface
-            .get_socket::<DnsSocket>(dns_handle)
+            .get_socket::<dns::Socket>(dns_handle)
             .get_query_result(query)
         {
             Ok(addrs) => {

+ 5 - 5
examples/httpclient.rs

@@ -8,7 +8,7 @@ use url::Url;
 
 use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes};
 use smoltcp::phy::{wait as phy_wait, Device, Medium};
-use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
+use smoltcp::socket::tcp;
 use smoltcp::time::Instant;
 use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv6Address};
 
@@ -30,9 +30,9 @@ fn main() {
 
     let neighbor_cache = NeighborCache::new(BTreeMap::new());
 
-    let tcp_rx_buffer = TcpSocketBuffer::new(vec![0; 1024]);
-    let tcp_tx_buffer = TcpSocketBuffer::new(vec![0; 1024]);
-    let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
+    let tcp_rx_buffer = tcp::SocketBuffer::new(vec![0; 1024]);
+    let tcp_tx_buffer = tcp::SocketBuffer::new(vec![0; 1024]);
+    let tcp_socket = tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer);
 
     let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
     let ip_addrs = [
@@ -76,7 +76,7 @@ fn main() {
             }
         }
 
-        let (socket, cx) = iface.get_socket_and_context::<TcpSocket>(tcp_handle);
+        let (socket, cx) = iface.get_socket_and_context::<tcp::Socket>(tcp_handle);
 
         state = match state {
             State::Connect if !socket.is_active() => {

+ 9 - 9
examples/loopback.rs

@@ -11,7 +11,7 @@ use log::{debug, error, info};
 
 use smoltcp::iface::{InterfaceBuilder, NeighborCache};
 use smoltcp::phy::{Loopback, Medium};
-use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
+use smoltcp::socket::tcp;
 use smoltcp::time::{Duration, Instant};
 use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
 
@@ -100,17 +100,17 @@ fn main() {
         // when stack overflows.
         static mut TCP_SERVER_RX_DATA: [u8; 1024] = [0; 1024];
         static mut TCP_SERVER_TX_DATA: [u8; 1024] = [0; 1024];
-        let tcp_rx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_SERVER_RX_DATA[..] });
-        let tcp_tx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_SERVER_TX_DATA[..] });
-        TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer)
+        let tcp_rx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_SERVER_RX_DATA[..] });
+        let tcp_tx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_SERVER_TX_DATA[..] });
+        tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer)
     };
 
     let client_socket = {
         static mut TCP_CLIENT_RX_DATA: [u8; 1024] = [0; 1024];
         static mut TCP_CLIENT_TX_DATA: [u8; 1024] = [0; 1024];
-        let tcp_rx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_CLIENT_RX_DATA[..] });
-        let tcp_tx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_CLIENT_TX_DATA[..] });
-        TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer)
+        let tcp_rx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_CLIENT_RX_DATA[..] });
+        let tcp_tx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_CLIENT_TX_DATA[..] });
+        tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer)
     };
 
     let server_handle = iface.add_socket(server_socket);
@@ -127,7 +127,7 @@ fn main() {
             }
         }
 
-        let mut socket = iface.get_socket::<TcpSocket>(server_handle);
+        let mut socket = iface.get_socket::<tcp::Socket>(server_handle);
         if !socket.is_active() && !socket.is_listening() {
             if !did_listen {
                 debug!("listening");
@@ -145,7 +145,7 @@ fn main() {
             done = true;
         }
 
-        let (mut socket, cx) = iface.get_socket_and_context::<TcpSocket>(client_handle);
+        let (mut socket, cx) = iface.get_socket_and_context::<tcp::Socket>(client_handle);
         if !socket.is_open() {
             if !did_connect {
                 debug!("connecting");

+ 9 - 11
examples/multicast.rs

@@ -6,9 +6,7 @@ use std::os::unix::io::AsRawFd;
 
 use smoltcp::iface::{InterfaceBuilder, NeighborCache};
 use smoltcp::phy::wait as phy_wait;
-use smoltcp::socket::{
-    RawPacketMetadata, RawSocket, RawSocketBuffer, UdpPacketMetadata, UdpSocket, UdpSocketBuffer,
-};
+use smoltcp::socket::{raw, udp};
 use smoltcp::time::Instant;
 use smoltcp::wire::{
     EthernetAddress, IgmpPacket, IgmpRepr, IpAddress, IpCidr, IpProtocol, IpVersion, Ipv4Address,
@@ -50,10 +48,10 @@ fn main() {
         .unwrap();
 
     // Must fit at least one IGMP packet
-    let raw_rx_buffer = RawSocketBuffer::new(vec![RawPacketMetadata::EMPTY; 2], vec![0; 512]);
+    let raw_rx_buffer = raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; 2], vec![0; 512]);
     // Will not send IGMP
-    let raw_tx_buffer = RawSocketBuffer::new(vec![], vec![]);
-    let raw_socket = RawSocket::new(
+    let raw_tx_buffer = raw::PacketBuffer::new(vec![], vec![]);
+    let raw_socket = raw::Socket::new(
         IpVersion::Ipv4,
         IpProtocol::Igmp,
         raw_rx_buffer,
@@ -62,10 +60,10 @@ fn main() {
     let raw_handle = iface.add_socket(raw_socket);
 
     // Must fit mDNS payload of at least one packet
-    let udp_rx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY; 4], vec![0; 1024]);
+    let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY; 4], vec![0; 1024]);
     // Will not send mDNS
-    let udp_tx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 0]);
-    let udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
+    let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 0]);
+    let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
     let udp_handle = iface.add_socket(udp_socket);
 
     loop {
@@ -77,7 +75,7 @@ fn main() {
             }
         }
 
-        let socket = iface.get_socket::<RawSocket>(raw_handle);
+        let socket = iface.get_socket::<raw::Socket>(raw_handle);
 
         if socket.can_recv() {
             // For display purposes only - normally we wouldn't process incoming IGMP packets
@@ -94,7 +92,7 @@ fn main() {
             }
         }
 
-        let socket = iface.get_socket::<UdpSocket>(udp_handle);
+        let socket = iface.get_socket::<udp::Socket>(udp_handle);
         if !socket.is_open() {
             socket.bind(MDNS_PORT).unwrap()
         }

+ 6 - 6
examples/ping.rs

@@ -11,7 +11,7 @@ use std::str::FromStr;
 use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes};
 use smoltcp::phy::wait as phy_wait;
 use smoltcp::phy::Device;
-use smoltcp::socket::{IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBuffer};
+use smoltcp::socket::icmp;
 use smoltcp::wire::{
     EthernetAddress, Icmpv4Packet, Icmpv4Repr, Icmpv6Packet, Icmpv6Repr, IpAddress, IpCidr,
     Ipv4Address, Ipv6Address,
@@ -108,9 +108,9 @@ fn main() {
 
     let remote_addr = address;
 
-    let icmp_rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 256]);
-    let icmp_tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 256]);
-    let icmp_socket = IcmpSocket::new(icmp_rx_buffer, icmp_tx_buffer);
+    let icmp_rx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 256]);
+    let icmp_tx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 256]);
+    let icmp_socket = icmp::Socket::new(icmp_rx_buffer, icmp_tx_buffer);
 
     let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
     let src_ipv6 = IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1);
@@ -156,9 +156,9 @@ fn main() {
         }
 
         let timestamp = Instant::now();
-        let socket = iface.get_socket::<IcmpSocket>(icmp_handle);
+        let socket = iface.get_socket::<icmp::Socket>(icmp_handle);
         if !socket.is_open() {
-            socket.bind(IcmpEndpoint::Ident(ident)).unwrap();
+            socket.bind(icmp::Endpoint::Ident(ident)).unwrap();
             send_at = timestamp;
         }
 

+ 21 - 22
examples/server.rs

@@ -8,8 +8,7 @@ use std::str;
 
 use smoltcp::iface::{InterfaceBuilder, NeighborCache};
 use smoltcp::phy::{wait as phy_wait, Device, Medium};
-use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
-use smoltcp::socket::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
+use smoltcp::socket::{tcp, udp};
 use smoltcp::time::{Duration, Instant};
 use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
 
@@ -27,25 +26,25 @@ fn main() {
 
     let neighbor_cache = NeighborCache::new(BTreeMap::new());
 
-    let udp_rx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 64]);
-    let udp_tx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 128]);
-    let udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
+    let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 64]);
+    let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 128]);
+    let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
 
-    let tcp1_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
-    let tcp1_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
-    let tcp1_socket = TcpSocket::new(tcp1_rx_buffer, tcp1_tx_buffer);
+    let tcp1_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
+    let tcp1_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
+    let tcp1_socket = tcp::Socket::new(tcp1_rx_buffer, tcp1_tx_buffer);
 
-    let tcp2_rx_buffer = TcpSocketBuffer::new(vec![0; 64]);
-    let tcp2_tx_buffer = TcpSocketBuffer::new(vec![0; 128]);
-    let tcp2_socket = TcpSocket::new(tcp2_rx_buffer, tcp2_tx_buffer);
+    let tcp2_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
+    let tcp2_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
+    let tcp2_socket = tcp::Socket::new(tcp2_rx_buffer, tcp2_tx_buffer);
 
-    let tcp3_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp3_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp3_socket = TcpSocket::new(tcp3_rx_buffer, tcp3_tx_buffer);
+    let tcp3_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp3_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp3_socket = tcp::Socket::new(tcp3_rx_buffer, tcp3_tx_buffer);
 
-    let tcp4_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp4_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
-    let tcp4_socket = TcpSocket::new(tcp4_rx_buffer, tcp4_tx_buffer);
+    let tcp4_rx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp4_tx_buffer = tcp::SocketBuffer::new(vec![0; 65535]);
+    let tcp4_socket = tcp::Socket::new(tcp4_rx_buffer, tcp4_tx_buffer);
 
     let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
     let ip_addrs = [
@@ -80,7 +79,7 @@ fn main() {
         }
 
         // udp:6969: respond "hello"
-        let socket = iface.get_socket::<UdpSocket>(udp_handle);
+        let socket = iface.get_socket::<udp::Socket>(udp_handle);
         if !socket.is_open() {
             socket.bind(6969).unwrap()
         }
@@ -106,7 +105,7 @@ fn main() {
         }
 
         // tcp:6969: respond "hello"
-        let socket = iface.get_socket::<TcpSocket>(tcp1_handle);
+        let socket = iface.get_socket::<tcp::Socket>(tcp1_handle);
         if !socket.is_open() {
             socket.listen(6969).unwrap();
         }
@@ -119,7 +118,7 @@ fn main() {
         }
 
         // tcp:6970: echo with reverse
-        let socket = iface.get_socket::<TcpSocket>(tcp2_handle);
+        let socket = iface.get_socket::<tcp::Socket>(tcp2_handle);
         if !socket.is_open() {
             socket.listen(6970).unwrap()
         }
@@ -161,7 +160,7 @@ fn main() {
         }
 
         // tcp:6971: sinkhole
-        let socket = iface.get_socket::<TcpSocket>(tcp3_handle);
+        let socket = iface.get_socket::<tcp::Socket>(tcp3_handle);
         if !socket.is_open() {
             socket.listen(6971).unwrap();
             socket.set_keep_alive(Some(Duration::from_millis(1000)));
@@ -182,7 +181,7 @@ fn main() {
         }
 
         // tcp:6972: fountain
-        let socket = iface.get_socket::<TcpSocket>(tcp4_handle);
+        let socket = iface.get_socket::<tcp::Socket>(tcp4_handle);
         if !socket.is_open() {
             socket.listen(6972).unwrap()
         }

+ 5 - 5
examples/sixlowpan.rs

@@ -49,7 +49,7 @@ use std::str;
 
 use smoltcp::iface::{InterfaceBuilder, NeighborCache};
 use smoltcp::phy::{wait as phy_wait, Medium, RawSocket};
-use smoltcp::socket::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
+use smoltcp::socket::udp;
 use smoltcp::time::Instant;
 use smoltcp::wire::{Ieee802154Pan, IpAddress, IpCidr};
 
@@ -68,9 +68,9 @@ fn main() {
 
     let neighbor_cache = NeighborCache::new(BTreeMap::new());
 
-    let udp_rx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 64]);
-    let udp_tx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 128]);
-    let udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
+    let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 64]);
+    let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 128]);
+    let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
 
     let ieee802154_addr = smoltcp::wire::Ieee802154Address::Extended([
         0x1a, 0x0b, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
@@ -100,7 +100,7 @@ fn main() {
         }
 
         // udp:6969: respond "hello"
-        let socket = iface.get_socket::<UdpSocket>(udp_handle);
+        let socket = iface.get_socket::<udp::Socket>(udp_handle);
         if !socket.is_open() {
             socket.bind(6969).unwrap()
         }

+ 9 - 9
fuzz/fuzz_targets/tcp_headers.rs

@@ -2,7 +2,7 @@
 use libfuzzer_sys::fuzz_target;
 use smoltcp::iface::{InterfaceBuilder, NeighborCache};
 use smoltcp::phy::{Loopback, Medium};
-use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
+use smoltcp::socket::tcp;
 use smoltcp::time::{Duration, Instant};
 use smoltcp::wire::{EthernetAddress, EthernetFrame, EthernetProtocol};
 use smoltcp::wire::{IpAddress, IpCidr, Ipv4Packet, Ipv6Packet, TcpPacket};
@@ -145,17 +145,17 @@ fuzz_target!(|data: &[u8]| {
         // when stack overflows.
         static mut TCP_SERVER_RX_DATA: [u8; 1024] = [0; 1024];
         static mut TCP_SERVER_TX_DATA: [u8; 1024] = [0; 1024];
-        let tcp_rx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_SERVER_RX_DATA[..] });
-        let tcp_tx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_SERVER_TX_DATA[..] });
-        TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer)
+        let tcp_rx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_SERVER_RX_DATA[..] });
+        let tcp_tx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_SERVER_TX_DATA[..] });
+        tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer)
     };
 
     let client_socket = {
         static mut TCP_CLIENT_RX_DATA: [u8; 1024] = [0; 1024];
         static mut TCP_CLIENT_TX_DATA: [u8; 1024] = [0; 1024];
-        let tcp_rx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_CLIENT_RX_DATA[..] });
-        let tcp_tx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_CLIENT_TX_DATA[..] });
-        TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer)
+        let tcp_rx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_CLIENT_RX_DATA[..] });
+        let tcp_tx_buffer = tcp::SocketBuffer::new(unsafe { &mut TCP_CLIENT_TX_DATA[..] });
+        tcp::Socket::new(tcp_rx_buffer, tcp_tx_buffer)
     };
 
     let mut socket_set_entries: [_; 2] = Default::default();
@@ -170,7 +170,7 @@ fuzz_target!(|data: &[u8]| {
         let _ = iface.poll(&mut socket_set, clock.elapsed());
 
         {
-            let mut socket = socket_set.get::<TcpSocket>(server_handle);
+            let mut socket = socket_set.get::<tcp::Socket>(server_handle);
             if !socket.is_active() && !socket.is_listening() {
                 if !did_listen {
                     socket.listen(1234).unwrap();
@@ -185,7 +185,7 @@ fuzz_target!(|data: &[u8]| {
         }
 
         {
-            let mut socket = socket_set.get::<TcpSocket>(client_handle);
+            let mut socket = socket_set.get::<tcp::Socket>(client_handle);
             if !socket.is_open() {
                 if !did_connect {
                     socket

+ 42 - 46
src/iface/interface.rs

@@ -15,6 +15,10 @@ use crate::iface::Routes;
 use crate::iface::{NeighborAnswer, NeighborCache};
 use crate::phy::{ChecksumCapabilities, Device, DeviceCapabilities, Medium, RxToken, TxToken};
 use crate::rand::Rand;
+#[cfg(feature = "socket-dhcpv4")]
+use crate::socket::dhcpv4;
+#[cfg(feature = "socket-dns")]
+use crate::socket::dns;
 use crate::socket::*;
 use crate::time::{Duration, Instant};
 use crate::wire::*;
@@ -1392,7 +1396,7 @@ impl<'a> InterfaceInner<'a> {
                         // If it does not accept the packet, then send an ICMP message.
                         for udp_socket in sockets
                             .iter_mut()
-                            .filter_map(|i| UdpSocket::downcast(&mut i.socket))
+                            .filter_map(|i| udp::Socket::downcast(&mut i.socket))
                         {
                             if !udp_socket.accepts(self, &IpRepr::Ipv6(ipv6_repr), &udp_repr) {
                                 continue;
@@ -1517,7 +1521,7 @@ impl<'a> InterfaceInner<'a> {
         // Pass every IP packet to all raw sockets we have registered.
         for raw_socket in sockets
             .iter_mut()
-            .filter_map(|i| RawSocket::downcast(&mut i.socket))
+            .filter_map(|i| raw::Socket::downcast(&mut i.socket))
         {
             if !raw_socket.accepts(ip_repr) {
                 continue;
@@ -1643,7 +1647,7 @@ impl<'a> InterfaceInner<'a> {
                 {
                     if let Some(dhcp_socket) = sockets
                         .iter_mut()
-                        .filter_map(|i| Dhcpv4Socket::downcast(&mut i.socket))
+                        .filter_map(|i| dhcpv4::Socket::downcast(&mut i.socket))
                         .next()
                     {
                         let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
@@ -1822,7 +1826,7 @@ impl<'a> InterfaceInner<'a> {
         #[cfg(all(feature = "socket-icmp", feature = "proto-ipv6"))]
         for icmp_socket in _sockets
             .iter_mut()
-            .filter_map(|i| IcmpSocket::downcast(&mut i.socket))
+            .filter_map(|i| icmp::Socket::downcast(&mut i.socket))
         {
             if !icmp_socket.accepts(self, &ip_repr, &icmp_repr.into()) {
                 continue;
@@ -2009,7 +2013,7 @@ impl<'a> InterfaceInner<'a> {
         #[cfg(all(feature = "socket-icmp", feature = "proto-ipv4"))]
         for icmp_socket in _sockets
             .iter_mut()
-            .filter_map(|i| IcmpSocket::downcast(&mut i.socket))
+            .filter_map(|i| icmp::Socket::downcast(&mut i.socket))
         {
             if !icmp_socket.accepts(self, &ip_repr, &icmp_repr.into()) {
                 continue;
@@ -2137,7 +2141,7 @@ impl<'a> InterfaceInner<'a> {
         #[cfg(feature = "socket-udp")]
         for udp_socket in sockets
             .iter_mut()
-            .filter_map(|i| UdpSocket::downcast(&mut i.socket))
+            .filter_map(|i| udp::Socket::downcast(&mut i.socket))
         {
             if !udp_socket.accepts(self, &ip_repr, &udp_repr) {
                 continue;
@@ -2154,7 +2158,7 @@ impl<'a> InterfaceInner<'a> {
         #[cfg(feature = "socket-dns")]
         for dns_socket in sockets
             .iter_mut()
-            .filter_map(|i| DnsSocket::downcast(&mut i.socket))
+            .filter_map(|i| dns::Socket::downcast(&mut i.socket))
         {
             if !dns_socket.accepts(&ip_repr, &udp_repr) {
                 continue;
@@ -2212,7 +2216,7 @@ impl<'a> InterfaceInner<'a> {
 
         for tcp_socket in sockets
             .iter_mut()
-            .filter_map(|i| TcpSocket::downcast(&mut i.socket))
+            .filter_map(|i| tcp::Socket::downcast(&mut i.socket))
         {
             if !tcp_socket.accepts(self, &ip_repr, &tcp_repr) {
                 continue;
@@ -2232,7 +2236,7 @@ impl<'a> InterfaceInner<'a> {
             Ok(None)
         } else {
             // The packet wasn't handled by a socket, send a TCP RST packet.
-            Ok(Some(IpPacket::Tcp(TcpSocket::rst_reply(
+            Ok(Some(IpPacket::Tcp(tcp::Socket::rst_reply(
                 &ip_repr, &tcp_repr,
             ))))
         }
@@ -3092,17 +3096,16 @@ mod test {
     #[test]
     #[cfg(feature = "socket-udp")]
     fn test_handle_udp_broadcast() {
-        use crate::socket::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
         use crate::wire::IpEndpoint;
 
         static UDP_PAYLOAD: [u8; 5] = [0x48, 0x65, 0x6c, 0x6c, 0x6f];
 
         let mut iface = create_loopback();
 
-        let rx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 15]);
-        let tx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 15]);
+        let rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
+        let tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
 
-        let udp_socket = UdpSocket::new(rx_buffer, tx_buffer);
+        let udp_socket = udp::Socket::new(rx_buffer, tx_buffer);
 
         let mut udp_bytes = vec![0u8; 13];
         let mut packet = UdpPacket::new_unchecked(&mut udp_bytes);
@@ -3137,7 +3140,7 @@ mod test {
         });
 
         // Bind the socket to port 68
-        let socket = iface.get_socket::<UdpSocket>(socket_handle);
+        let socket = iface.get_socket::<udp::Socket>(socket_handle);
         assert_eq!(socket.bind(68), Ok(()));
         assert!(!socket.can_recv());
         assert!(socket.can_send());
@@ -3161,7 +3164,7 @@ mod test {
 
         // Make sure the payload to the UDP packet processed by process_udp is
         // appended to the bound sockets rx_buffer
-        let socket = iface.get_socket::<UdpSocket>(socket_handle);
+        let socket = iface.get_socket::<udp::Socket>(socket_handle);
         assert!(socket.can_recv());
         assert_eq!(
             socket.recv(),
@@ -3592,15 +3595,14 @@ mod test {
     #[test]
     #[cfg(all(feature = "socket-icmp", feature = "proto-ipv4"))]
     fn test_icmpv4_socket() {
-        use crate::socket::{IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBuffer};
         use crate::wire::Icmpv4Packet;
 
         let mut iface = create_loopback();
 
-        let rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 24]);
-        let tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 24]);
+        let rx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 24]);
+        let tx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 24]);
 
-        let icmpv4_socket = IcmpSocket::new(rx_buffer, tx_buffer);
+        let icmpv4_socket = icmp::Socket::new(rx_buffer, tx_buffer);
 
         let socket_handle = iface.add_socket(icmpv4_socket);
 
@@ -3608,9 +3610,9 @@ mod test {
         let seq_no = 0x5432;
         let echo_data = &[0xff; 16];
 
-        let socket = iface.get_socket::<IcmpSocket>(socket_handle);
+        let socket = iface.get_socket::<icmp::Socket>(socket_handle);
         // Bind to the ID 0x1234
-        assert_eq!(socket.bind(IcmpEndpoint::Ident(ident)), Ok(()));
+        assert_eq!(socket.bind(icmp::Endpoint::Ident(ident)), Ok(()));
 
         // Ensure the ident we bound to and the ident of the packet are the same.
         let mut bytes = [0xff; 24];
@@ -3634,7 +3636,7 @@ mod test {
 
         // Open a socket and ensure the packet is handled due to the listening
         // socket.
-        assert!(!iface.get_socket::<IcmpSocket>(socket_handle).can_recv());
+        assert!(!iface.get_socket::<icmp::Socket>(socket_handle).can_recv());
 
         // Confirm we still get EchoReply from `smoltcp` even with the ICMP socket listening
         let echo_reply = Icmpv4Repr::EchoReply {
@@ -3654,7 +3656,7 @@ mod test {
             Ok(Some(IpPacket::Icmpv4((ipv4_reply, echo_reply))))
         );
 
-        let socket = iface.get_socket::<IcmpSocket>(socket_handle);
+        let socket = iface.get_socket::<icmp::Socket>(socket_handle);
         assert!(socket.can_recv());
         assert_eq!(
             socket.recv(),
@@ -3852,19 +3854,18 @@ mod test {
     #[test]
     #[cfg(all(feature = "proto-ipv4", feature = "socket-raw"))]
     fn test_raw_socket_no_reply() {
-        use crate::socket::{RawPacketMetadata, RawSocket, RawSocketBuffer};
         use crate::wire::{IpVersion, Ipv4Packet, UdpPacket, UdpRepr};
 
         let mut iface = create_loopback();
 
         let packets = 1;
         let rx_buffer =
-            RawSocketBuffer::new(vec![RawPacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
-        let tx_buffer = RawSocketBuffer::new(
-            vec![RawPacketMetadata::EMPTY; packets],
+            raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
+        let tx_buffer = raw::PacketBuffer::new(
+            vec![raw::PacketMetadata::EMPTY; packets],
             vec![0; 48 * packets],
         );
-        let raw_socket = RawSocket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
+        let raw_socket = raw::Socket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
         iface.add_socket(raw_socket);
 
         let src_addr = Ipv4Address([127, 0, 0, 2]);
@@ -3921,19 +3922,18 @@ mod test {
     #[test]
     #[cfg(all(feature = "proto-ipv4", feature = "socket-raw"))]
     fn test_raw_socket_truncated_packet() {
-        use crate::socket::{RawPacketMetadata, RawSocket, RawSocketBuffer};
         use crate::wire::{IpVersion, Ipv4Packet, UdpPacket, UdpRepr};
 
         let mut iface = create_loopback();
 
         let packets = 1;
         let rx_buffer =
-            RawSocketBuffer::new(vec![RawPacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
-        let tx_buffer = RawSocketBuffer::new(
-            vec![RawPacketMetadata::EMPTY; packets],
+            raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
+        let tx_buffer = raw::PacketBuffer::new(
+            vec![raw::PacketMetadata::EMPTY; packets],
             vec![0; 48 * packets],
         );
-        let raw_socket = RawSocket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
+        let raw_socket = raw::Socket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
         iface.add_socket(raw_socket);
 
         let src_addr = Ipv4Address([127, 0, 0, 2]);
@@ -3994,35 +3994,31 @@ mod test {
     #[test]
     #[cfg(all(feature = "proto-ipv4", feature = "socket-raw", feature = "socket-udp"))]
     fn test_raw_socket_with_udp_socket() {
-        use crate::socket::{
-            RawPacketMetadata, RawSocket, RawSocketBuffer, UdpPacketMetadata, UdpSocket,
-            UdpSocketBuffer,
-        };
         use crate::wire::{IpEndpoint, IpVersion, Ipv4Packet, UdpPacket, UdpRepr};
 
         static UDP_PAYLOAD: [u8; 5] = [0x48, 0x65, 0x6c, 0x6c, 0x6f];
 
         let mut iface = create_loopback();
 
-        let udp_rx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 15]);
-        let udp_tx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 15]);
-        let udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
+        let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
+        let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
+        let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
         let udp_socket_handle = iface.add_socket(udp_socket);
 
         // Bind the socket to port 68
-        let socket = iface.get_socket::<UdpSocket>(udp_socket_handle);
+        let socket = iface.get_socket::<udp::Socket>(udp_socket_handle);
         assert_eq!(socket.bind(68), Ok(()));
         assert!(!socket.can_recv());
         assert!(socket.can_send());
 
         let packets = 1;
         let raw_rx_buffer =
-            RawSocketBuffer::new(vec![RawPacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
-        let raw_tx_buffer = RawSocketBuffer::new(
-            vec![RawPacketMetadata::EMPTY; packets],
+            raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
+        let raw_tx_buffer = raw::PacketBuffer::new(
+            vec![raw::PacketMetadata::EMPTY; packets],
             vec![0; 48 * packets],
         );
-        let raw_socket = RawSocket::new(
+        let raw_socket = raw::Socket::new(
             IpVersion::Ipv4,
             IpProtocol::Udp,
             raw_rx_buffer,
@@ -4080,7 +4076,7 @@ mod test {
         );
 
         // Make sure the UDP socket can still receive in presence of a Raw socket that handles UDP
-        let socket = iface.get_socket::<UdpSocket>(udp_socket_handle);
+        let socket = iface.get_socket::<udp::Socket>(udp_socket_handle);
         assert!(socket.can_recv());
         assert_eq!(
             socket.recv(),

+ 6 - 6
src/socket/dhcpv4.rs

@@ -110,7 +110,7 @@ pub enum Event {
 }
 
 #[derive(Debug)]
-pub struct Dhcpv4Socket {
+pub struct Socket {
     /// State of the DHCP client.
     state: ClientState,
     /// Set to true on config/state change, cleared back to false by the `config` function.
@@ -131,11 +131,11 @@ pub struct Dhcpv4Socket {
 /// The socket acquires an IP address configuration through DHCP autonomously.
 /// You must query the configuration with `.poll()` after every call to `Interface::poll()`,
 /// and apply the configuration to the `Interface`.
-impl Dhcpv4Socket {
+impl Socket {
     /// Create a DHCPv4 socket
     #[allow(clippy::new_without_default)]
     pub fn new() -> Self {
-        Dhcpv4Socket {
+        Socket {
             state: ClientState::Discovering(DiscoverState {
                 retry_at: Instant::from_millis(0),
             }),
@@ -561,12 +561,12 @@ mod test {
     // Helper functions
 
     struct TestSocket {
-        socket: Dhcpv4Socket,
+        socket: Socket,
         cx: Context<'static>,
     }
 
     impl Deref for TestSocket {
-        type Target = Dhcpv4Socket;
+        type Target = Socket;
         fn deref(&self) -> &Self::Target {
             &self.socket
         }
@@ -797,7 +797,7 @@ mod test {
     // Tests
 
     fn socket() -> TestSocket {
-        let mut s = Dhcpv4Socket::new();
+        let mut s = Socket::new();
         assert_eq!(s.poll(), Some(Event::Deconfigured));
         TestSocket {
             socket: s,

+ 5 - 11
src/socket/dns.rs

@@ -4,7 +4,7 @@ use core::task::Waker;
 use heapless::Vec;
 use managed::ManagedSlice;
 
-use crate::socket::{Context, PollAt, Socket};
+use crate::socket::{Context, PollAt};
 use crate::time::{Duration, Instant};
 use crate::wire::dns::{Flags, Opcode, Packet, Question, Rcode, Record, RecordData, Repr, Type};
 use crate::wire::{self, IpAddress, IpProtocol, IpRepr, UdpRepr};
@@ -78,7 +78,7 @@ pub struct QueryHandle(usize);
 /// A UDP socket is bound to a specific endpoint, and owns transmit and receive
 /// packet buffers.
 #[derive(Debug)]
-pub struct DnsSocket<'a> {
+pub struct Socket<'a> {
     servers: Vec<IpAddress, MAX_SERVER_COUNT>,
     queries: ManagedSlice<'a, Option<DnsQuery>>,
 
@@ -86,17 +86,17 @@ pub struct DnsSocket<'a> {
     hop_limit: Option<u8>,
 }
 
-impl<'a> DnsSocket<'a> {
+impl<'a> Socket<'a> {
     /// Create a DNS socket.
     ///
     /// # Panics
     ///
     /// Panics if `servers.len() > MAX_SERVER_COUNT`
-    pub fn new<Q>(servers: &[IpAddress], queries: Q) -> DnsSocket<'a>
+    pub fn new<Q>(servers: &[IpAddress], queries: Q) -> Socket<'a>
     where
         Q: Into<ManagedSlice<'a, Option<DnsQuery>>>,
     {
-        DnsSocket {
+        Socket {
             servers: Vec::from_slice(servers).unwrap(),
             queries: queries.into(),
             hop_limit: None,
@@ -502,12 +502,6 @@ impl<'a> DnsSocket<'a> {
     }
 }
 
-impl<'a> From<DnsSocket<'a>> for Socket<'a> {
-    fn from(val: DnsSocket<'a>) -> Self {
-        Socket::Dns(val)
-    }
-}
-
 fn eq_names<'a>(
     mut a: impl Iterator<Item = wire::Result<&'a [u8]>>,
     mut b: impl Iterator<Item = wire::Result<&'a [u8]>>,

+ 25 - 30
src/socket/icmp.rs

@@ -6,7 +6,6 @@ use crate::phy::ChecksumCapabilities;
 #[cfg(feature = "async")]
 use crate::socket::WakerRegistration;
 use crate::socket::{Context, PollAt};
-use crate::storage::{PacketBuffer, PacketMetadata};
 use crate::{Error, Result};
 
 use crate::wire::IcmpRepr;
@@ -46,10 +45,10 @@ impl Default for Endpoint {
 }
 
 /// An ICMP packet metadata.
-pub type IcmpPacketMetadata = PacketMetadata<IpAddress>;
+pub type PacketMetadata = crate::storage::PacketMetadata<IpAddress>;
 
 /// An ICMP packet ring buffer.
-pub type IcmpSocketBuffer<'a> = PacketBuffer<'a, IpAddress>;
+pub type PacketBuffer<'a> = crate::storage::PacketBuffer<'a, IpAddress>;
 
 /// A ICMP socket
 ///
@@ -61,9 +60,9 @@ pub type IcmpSocketBuffer<'a> = PacketBuffer<'a, IpAddress>;
 /// [IcmpEndpoint]: enum.IcmpEndpoint.html
 /// [bind]: #method.bind
 #[derive(Debug)]
-pub struct IcmpSocket<'a> {
-    rx_buffer: IcmpSocketBuffer<'a>,
-    tx_buffer: IcmpSocketBuffer<'a>,
+pub struct Socket<'a> {
+    rx_buffer: PacketBuffer<'a>,
+    tx_buffer: PacketBuffer<'a>,
     /// The endpoint this socket is communicating with
     endpoint: Endpoint,
     /// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
@@ -74,10 +73,10 @@ pub struct IcmpSocket<'a> {
     tx_waker: WakerRegistration,
 }
 
-impl<'a> IcmpSocket<'a> {
+impl<'a> Socket<'a> {
     /// Create an ICMP socket with the given buffers.
-    pub fn new(rx_buffer: IcmpSocketBuffer<'a>, tx_buffer: IcmpSocketBuffer<'a>) -> IcmpSocket<'a> {
-        IcmpSocket {
+    pub fn new(rx_buffer: PacketBuffer<'a>, tx_buffer: PacketBuffer<'a>) -> Socket<'a> {
+        Socket {
             rx_buffer: rx_buffer,
             tx_buffer: tx_buffer,
             endpoint: Default::default(),
@@ -167,18 +166,17 @@ impl<'a> IcmpSocket<'a> {
     /// diagnose connection problems.
     ///
     /// ```
-    /// # use smoltcp::socket::{Socket, IcmpSocket, IcmpSocketBuffer, IcmpPacketMetadata};
-    /// # let rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
-    /// # let tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
     /// use smoltcp::wire::IpListenEndpoint;
-    /// use smoltcp::socket::IcmpEndpoint;
+    /// use smoltcp::socket::icmp;
+    /// # let rx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 20]);
+    /// # let tx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 20]);
     ///
     /// let mut icmp_socket = // ...
-    /// # IcmpSocket::new(rx_buffer, tx_buffer);
+    /// # icmp::Socket::new(rx_buffer, tx_buffer);
     ///
     /// // Bind to ICMP error responses for UDP packets sent from port 53.
     /// let endpoint = IpListenEndpoint::from(53);
-    /// icmp_socket.bind(IcmpEndpoint::Udp(endpoint)).unwrap();
+    /// icmp_socket.bind(icmp::Endpoint::Udp(endpoint)).unwrap();
     /// ```
     ///
     /// ## Bind to a specific ICMP identifier:
@@ -189,16 +187,16 @@ impl<'a> IcmpSocket<'a> {
     /// messages.
     ///
     /// ```
-    /// # use smoltcp::socket::{Socket, IcmpSocket, IcmpSocketBuffer, IcmpPacketMetadata};
-    /// # let rx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
-    /// # let tx_buffer = IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY], vec![0; 20]);
-    /// use smoltcp::socket::IcmpEndpoint;
+    /// use smoltcp::wire::IpListenEndpoint;
+    /// use smoltcp::socket::icmp;
+    /// # let rx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 20]);
+    /// # let tx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 20]);
     ///
     /// let mut icmp_socket = // ...
-    /// # IcmpSocket::new(rx_buffer, tx_buffer);
+    /// # icmp::Socket::new(rx_buffer, tx_buffer);
     ///
     /// // Bind to ICMP messages with the ICMP identifier 0x1234
-    /// icmp_socket.bind(IcmpEndpoint::Ident(0x1234)).unwrap();
+    /// icmp_socket.bind(icmp::Endpoint::Ident(0x1234)).unwrap();
     /// ```
     ///
     /// [is_specified]: enum.IcmpEndpoint.html#method.is_specified
@@ -509,18 +507,15 @@ mod tests_common {
     pub use crate::phy::DeviceCapabilities;
     pub use crate::wire::IpAddress;
 
-    pub fn buffer(packets: usize) -> IcmpSocketBuffer<'static> {
-        IcmpSocketBuffer::new(
-            vec![IcmpPacketMetadata::EMPTY; packets],
-            vec![0; 66 * packets],
-        )
+    pub fn buffer(packets: usize) -> PacketBuffer<'static> {
+        PacketBuffer::new(vec![PacketMetadata::EMPTY; packets], vec![0; 66 * packets])
     }
 
     pub fn socket(
-        rx_buffer: IcmpSocketBuffer<'static>,
-        tx_buffer: IcmpSocketBuffer<'static>,
-    ) -> IcmpSocket<'static> {
-        IcmpSocket::new(rx_buffer, tx_buffer)
+        rx_buffer: PacketBuffer<'static>,
+        tx_buffer: PacketBuffer<'static>,
+    ) -> Socket<'static> {
+        Socket::new(rx_buffer, tx_buffer)
     }
 
     pub const LOCAL_PORT: u16 = 53;

+ 19 - 32
src/socket/mod.rs

@@ -15,34 +15,21 @@ use crate::iface::Context;
 use crate::time::Instant;
 
 #[cfg(feature = "socket-dhcpv4")]
-mod dhcpv4;
+pub mod dhcpv4;
 #[cfg(feature = "socket-dns")]
-mod dns;
+pub mod dns;
 #[cfg(feature = "socket-icmp")]
-mod icmp;
+pub mod icmp;
 #[cfg(feature = "socket-raw")]
-mod raw;
+pub mod raw;
 #[cfg(feature = "socket-tcp")]
-mod tcp;
+pub mod tcp;
 #[cfg(feature = "socket-udp")]
-mod udp;
+pub mod udp;
 
 #[cfg(feature = "async")]
 mod waker;
 
-#[cfg(feature = "socket-dhcpv4")]
-pub use self::dhcpv4::{Config as Dhcpv4Config, Dhcpv4Socket, Event as Dhcpv4Event};
-#[cfg(feature = "socket-dns")]
-pub use self::dns::{DnsQuery, DnsSocket, QueryHandle as DnsQueryHandle};
-#[cfg(feature = "socket-icmp")]
-pub use self::icmp::{Endpoint as IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBuffer};
-#[cfg(feature = "socket-raw")]
-pub use self::raw::{RawPacketMetadata, RawSocket, RawSocketBuffer};
-#[cfg(feature = "socket-tcp")]
-pub use self::tcp::{SocketBuffer as TcpSocketBuffer, State as TcpState, TcpSocket};
-#[cfg(feature = "socket-udp")]
-pub use self::udp::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
-
 #[cfg(feature = "async")]
 pub(crate) use self::waker::WakerRegistration;
 
@@ -62,7 +49,7 @@ pub(crate) enum PollAt {
 ///
 /// This enumeration abstracts the various types of sockets based on the IP protocol.
 /// To downcast a `Socket` value to a concrete socket, use the [AnySocket] trait,
-/// e.g. to get `UdpSocket`, call `UdpSocket::downcast(socket)`.
+/// e.g. to get `udp::Socket`, call `udp::Socket::downcast(socket)`.
 ///
 /// It is usually more convenient to use [SocketSet::get] instead.
 ///
@@ -71,17 +58,17 @@ pub(crate) enum PollAt {
 #[derive(Debug)]
 pub enum Socket<'a> {
     #[cfg(feature = "socket-raw")]
-    Raw(RawSocket<'a>),
+    Raw(raw::Socket<'a>),
     #[cfg(feature = "socket-icmp")]
-    Icmp(IcmpSocket<'a>),
+    Icmp(icmp::Socket<'a>),
     #[cfg(feature = "socket-udp")]
-    Udp(UdpSocket<'a>),
+    Udp(udp::Socket<'a>),
     #[cfg(feature = "socket-tcp")]
-    Tcp(TcpSocket<'a>),
+    Tcp(tcp::Socket<'a>),
     #[cfg(feature = "socket-dhcpv4")]
-    Dhcpv4(Dhcpv4Socket),
+    Dhcpv4(dhcpv4::Socket),
     #[cfg(feature = "socket-dns")]
-    Dns(DnsSocket<'a>),
+    Dns(dns::Socket<'a>),
 }
 
 impl<'a> Socket<'a> {
@@ -128,14 +115,14 @@ macro_rules! from_socket {
 }
 
 #[cfg(feature = "socket-raw")]
-from_socket!(RawSocket<'a>, Raw);
+from_socket!(raw::Socket<'a>, Raw);
 #[cfg(feature = "socket-icmp")]
-from_socket!(IcmpSocket<'a>, Icmp);
+from_socket!(icmp::Socket<'a>, Icmp);
 #[cfg(feature = "socket-udp")]
-from_socket!(UdpSocket<'a>, Udp);
+from_socket!(udp::Socket<'a>, Udp);
 #[cfg(feature = "socket-tcp")]
-from_socket!(TcpSocket<'a>, Tcp);
+from_socket!(tcp::Socket<'a>, Tcp);
 #[cfg(feature = "socket-dhcpv4")]
-from_socket!(Dhcpv4Socket, Dhcpv4);
+from_socket!(dhcpv4::Socket, Dhcpv4);
 #[cfg(feature = "socket-dns")]
-from_socket!(DnsSocket<'a>, Dns);
+from_socket!(dns::Socket<'a>, Dns);

+ 22 - 26
src/socket/raw.rs

@@ -7,7 +7,6 @@ use crate::phy::ChecksumCapabilities;
 use crate::socket::PollAt;
 #[cfg(feature = "async")]
 use crate::socket::WakerRegistration;
-use crate::storage::{PacketBuffer, PacketMetadata};
 use crate::{Error, Result};
 
 use crate::wire::{IpProtocol, IpRepr, IpVersion};
@@ -17,37 +16,37 @@ use crate::wire::{Ipv4Packet, Ipv4Repr};
 use crate::wire::{Ipv6Packet, Ipv6Repr};
 
 /// A UDP packet metadata.
-pub type RawPacketMetadata = PacketMetadata<()>;
+pub type PacketMetadata = crate::storage::PacketMetadata<()>;
 
 /// A UDP packet ring buffer.
-pub type RawSocketBuffer<'a> = PacketBuffer<'a, ()>;
+pub type PacketBuffer<'a> = crate::storage::PacketBuffer<'a, ()>;
 
 /// A raw IP socket.
 ///
 /// A raw socket is bound to a specific IP protocol, and owns
 /// transmit and receive packet buffers.
 #[derive(Debug)]
-pub struct RawSocket<'a> {
+pub struct Socket<'a> {
     ip_version: IpVersion,
     ip_protocol: IpProtocol,
-    rx_buffer: RawSocketBuffer<'a>,
-    tx_buffer: RawSocketBuffer<'a>,
+    rx_buffer: PacketBuffer<'a>,
+    tx_buffer: PacketBuffer<'a>,
     #[cfg(feature = "async")]
     rx_waker: WakerRegistration,
     #[cfg(feature = "async")]
     tx_waker: WakerRegistration,
 }
 
-impl<'a> RawSocket<'a> {
+impl<'a> Socket<'a> {
     /// Create a raw IP socket bound to the given IP version and datagram protocol,
     /// with the given buffers.
     pub fn new(
         ip_version: IpVersion,
         ip_protocol: IpProtocol,
-        rx_buffer: RawSocketBuffer<'a>,
-        tx_buffer: RawSocketBuffer<'a>,
-    ) -> RawSocket<'a> {
-        RawSocket {
+        rx_buffer: PacketBuffer<'a>,
+        tx_buffer: PacketBuffer<'a>,
+    ) -> Socket<'a> {
+        Socket {
             ip_version,
             ip_protocol,
             rx_buffer,
@@ -330,11 +329,8 @@ mod test {
     #[cfg(feature = "proto-ipv6")]
     use crate::wire::{Ipv6Address, Ipv6Repr};
 
-    fn buffer(packets: usize) -> RawSocketBuffer<'static> {
-        RawSocketBuffer::new(
-            vec![RawPacketMetadata::EMPTY; packets],
-            vec![0; 48 * packets],
-        )
+    fn buffer(packets: usize) -> PacketBuffer<'static> {
+        PacketBuffer::new(vec![PacketMetadata::EMPTY; packets], vec![0; 48 * packets])
     }
 
     #[cfg(feature = "proto-ipv4")]
@@ -342,10 +338,10 @@ mod test {
         use super::*;
 
         pub fn socket(
-            rx_buffer: RawSocketBuffer<'static>,
-            tx_buffer: RawSocketBuffer<'static>,
-        ) -> RawSocket<'static> {
-            RawSocket::new(
+            rx_buffer: PacketBuffer<'static>,
+            tx_buffer: PacketBuffer<'static>,
+        ) -> Socket<'static> {
+            Socket::new(
                 IpVersion::Ipv4,
                 IpProtocol::Unknown(IP_PROTO),
                 rx_buffer,
@@ -373,10 +369,10 @@ mod test {
         use super::*;
 
         pub fn socket(
-            rx_buffer: RawSocketBuffer<'static>,
-            tx_buffer: RawSocketBuffer<'static>,
-        ) -> RawSocket<'static> {
-            RawSocket::new(
+            rx_buffer: PacketBuffer<'static>,
+            tx_buffer: PacketBuffer<'static>,
+        ) -> Socket<'static> {
+            Socket::new(
                 IpVersion::Ipv6,
                 IpProtocol::Unknown(IP_PROTO),
                 rx_buffer,
@@ -615,7 +611,7 @@ mod test {
     fn test_doesnt_accept_wrong_proto() {
         #[cfg(feature = "proto-ipv4")]
         {
-            let socket = RawSocket::new(
+            let socket = Socket::new(
                 IpVersion::Ipv4,
                 IpProtocol::Unknown(ipv4_locals::IP_PROTO + 1),
                 buffer(1),
@@ -627,7 +623,7 @@ mod test {
         }
         #[cfg(feature = "proto-ipv6")]
         {
-            let socket = RawSocket::new(
+            let socket = Socket::new(
                 IpVersion::Ipv6,
                 IpProtocol::Unknown(ipv6_locals::IP_PROTO + 1),
                 buffer(1),

+ 8 - 8
src/socket/tcp.rs

@@ -326,7 +326,7 @@ impl Display for Tuple {
 /// accept several connections, as many sockets must be allocated, or any new connection
 /// attempts will be reset.
 #[derive(Debug)]
-pub struct TcpSocket<'a> {
+pub struct Socket<'a> {
     state: State,
     timer: Timer,
     rtte: RttEstimator,
@@ -401,10 +401,10 @@ pub struct TcpSocket<'a> {
 
 const DEFAULT_MSS: usize = 536;
 
-impl<'a> TcpSocket<'a> {
+impl<'a> Socket<'a> {
     #[allow(unused_comparisons)] // small usize platforms always pass rx_capacity check
     /// Create a socket using the given buffers.
-    pub fn new<T>(rx_buffer: T, tx_buffer: T) -> TcpSocket<'a>
+    pub fn new<T>(rx_buffer: T, tx_buffer: T) -> Socket<'a>
     where
         T: Into<SocketBuffer<'a>>,
     {
@@ -420,7 +420,7 @@ impl<'a> TcpSocket<'a> {
         }
         let rx_cap_log2 = mem::size_of::<usize>() * 8 - rx_capacity.leading_zeros() as usize;
 
-        TcpSocket {
+        Socket {
             state: State::Closed,
             timer: Timer::new(),
             rtte: RttEstimator::default(),
@@ -2224,7 +2224,7 @@ impl<'a> TcpSocket<'a> {
     }
 }
 
-impl<'a> fmt::Write for TcpSocket<'a> {
+impl<'a> fmt::Write for Socket<'a> {
     fn write_str(&mut self, slice: &str) -> fmt::Result {
         let slice = slice.as_bytes();
         if self.send_slice(slice) == Ok(slice.len()) {
@@ -2344,12 +2344,12 @@ mod test {
     // =========================================================================================//
 
     struct TestSocket {
-        socket: TcpSocket<'static>,
+        socket: Socket<'static>,
         cx: Context<'static>,
     }
 
     impl Deref for TestSocket {
-        type Target = TcpSocket<'static>;
+        type Target = Socket<'static>;
         fn deref(&self) -> &Self::Target {
             &self.socket
         }
@@ -2465,7 +2465,7 @@ mod test {
     fn socket_with_buffer_sizes(tx_len: usize, rx_len: usize) -> TestSocket {
         let rx_buffer = SocketBuffer::new(vec![0; rx_len]);
         let tx_buffer = SocketBuffer::new(vec![0; tx_len]);
-        let mut socket = TcpSocket::new(rx_buffer, tx_buffer);
+        let mut socket = Socket::new(rx_buffer, tx_buffer);
         socket.set_ack_delay(None);
         let cx = Context::mock();
         TestSocket { socket, cx }

+ 16 - 20
src/socket/udp.rs

@@ -6,25 +6,24 @@ use crate::iface::Context;
 use crate::socket::PollAt;
 #[cfg(feature = "async")]
 use crate::socket::WakerRegistration;
-use crate::storage::{PacketBuffer, PacketMetadata};
 use crate::wire::{IpEndpoint, IpListenEndpoint, IpProtocol, IpRepr, UdpRepr};
 use crate::{Error, Result};
 
 /// A UDP packet metadata.
-pub type UdpPacketMetadata = PacketMetadata<IpEndpoint>;
+pub type PacketMetadata = crate::storage::PacketMetadata<IpEndpoint>;
 
 /// A UDP packet ring buffer.
-pub type UdpSocketBuffer<'a> = PacketBuffer<'a, IpEndpoint>;
+pub type PacketBuffer<'a> = crate::storage::PacketBuffer<'a, IpEndpoint>;
 
 /// A User Datagram Protocol socket.
 ///
 /// A UDP socket is bound to a specific endpoint, and owns transmit and receive
 /// packet buffers.
 #[derive(Debug)]
-pub struct UdpSocket<'a> {
+pub struct Socket<'a> {
     endpoint: IpListenEndpoint,
-    rx_buffer: UdpSocketBuffer<'a>,
-    tx_buffer: UdpSocketBuffer<'a>,
+    rx_buffer: PacketBuffer<'a>,
+    tx_buffer: PacketBuffer<'a>,
     /// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
     hop_limit: Option<u8>,
     #[cfg(feature = "async")]
@@ -33,10 +32,10 @@ pub struct UdpSocket<'a> {
     tx_waker: WakerRegistration,
 }
 
-impl<'a> UdpSocket<'a> {
+impl<'a> Socket<'a> {
     /// Create an UDP socket with the given buffers.
-    pub fn new(rx_buffer: UdpSocketBuffer<'a>, tx_buffer: UdpSocketBuffer<'a>) -> UdpSocket<'a> {
-        UdpSocket {
+    pub fn new(rx_buffer: PacketBuffer<'a>, tx_buffer: PacketBuffer<'a>) -> Socket<'a> {
+        Socket {
             endpoint: IpListenEndpoint::default(),
             rx_buffer,
             tx_buffer,
@@ -401,18 +400,15 @@ mod test {
     use super::*;
     use crate::wire::{IpRepr, UdpRepr};
 
-    fn buffer(packets: usize) -> UdpSocketBuffer<'static> {
-        UdpSocketBuffer::new(
-            vec![UdpPacketMetadata::EMPTY; packets],
-            vec![0; 16 * packets],
-        )
+    fn buffer(packets: usize) -> PacketBuffer<'static> {
+        PacketBuffer::new(vec![PacketMetadata::EMPTY; packets], vec![0; 16 * packets])
     }
 
     fn socket(
-        rx_buffer: UdpSocketBuffer<'static>,
-        tx_buffer: UdpSocketBuffer<'static>,
-    ) -> UdpSocket<'static> {
-        UdpSocket::new(rx_buffer, tx_buffer)
+        rx_buffer: PacketBuffer<'static>,
+        tx_buffer: PacketBuffer<'static>,
+    ) -> Socket<'static> {
+        Socket::new(rx_buffer, tx_buffer)
     }
 
     const LOCAL_PORT: u16 = 53;
@@ -735,7 +731,7 @@ mod test {
 
     #[test]
     fn test_process_empty_payload() {
-        let recv_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY; 1], vec![]);
+        let recv_buffer = PacketBuffer::new(vec![PacketMetadata::EMPTY; 1], vec![]);
         let mut socket = socket(recv_buffer, buffer(0));
         let mut cx = Context::mock();
 
@@ -751,7 +747,7 @@ mod test {
 
     #[test]
     fn test_closing() {
-        let recv_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY; 1], vec![]);
+        let recv_buffer = PacketBuffer::new(vec![PacketMetadata::EMPTY; 1], vec![]);
         let mut socket = socket(recv_buffer, buffer(0));
         assert_eq!(socket.bind(LOCAL_PORT), Ok(()));