Skip to content

Commit a983c62

Browse files
committedSep 25, 2017
Allow disabling any of: raw, TCP or UDP sockets.
1 parent 440b6f5 commit a983c62

File tree

7 files changed

+87
-27
lines changed

7 files changed

+87
-27
lines changed
 

‎.travis.yml

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ matrix:
99
# actually test everything
1010
- rust: nightly
1111
env: FEATURES='default' MODE='test'
12-
- rust: nightly
13-
env: FEATURES='std' MODE='test'
14-
- rust: nightly
15-
env: FEATURES='alloc' MODE='build'
1612
- rust: nightly
1713
env: FEATURES='phy-raw_socket' MODE='build'
1814
- rust: nightly
1915
env: FEATURES='phy-tap_interface' MODE='build'
2016
- rust: nightly
21-
env: FEATURES='' MODE='build'
17+
env: FEATURES='socket-raw' MODE='build'
18+
- rust: nightly
19+
env: FEATURES='socket-udp' MODE='build'
20+
- rust: nightly
21+
env: FEATURES='socket-tcp' MODE='build'
22+
- rust: nightly
23+
env: FEATURES='socket-raw socket-udp socket-tcp' MODE='build'
24+
- rust: nightly
25+
env: FEATURES='socket-raw socket-udp socket-tcp std' MODE='build'
26+
- rust: nightly
27+
env: FEATURES='socket-raw socket-udp socket-tcp alloc' MODE='build'
2228
script:
2329
- cargo "$MODE" --no-default-features --features "$FEATURES"
2430
notifications:

‎Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ alloc = ["managed/alloc"]
2828
verbose = []
2929
"phy-raw_socket" = ["std", "libc"]
3030
"phy-tap_interface" = ["std", "libc"]
31-
default = ["std", "log", "phy-raw_socket", "phy-tap_interface"]
31+
"socket-raw" = []
32+
"socket-udp" = []
33+
"socket-tcp" = []
34+
default = ["std", "log",
35+
"phy-raw_socket", "phy-tap_interface",
36+
"socket-raw", "socket-udp", "socket-tcp"]
3237

3338
[[example]]
3439
name = "tcpdump"

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ Enable `smoltcp::phy::RawSocket` and `smoltcp::phy::TapInterface`, respectively.
127127

128128
These features are enabled by default.
129129

130+
### Features `socket-raw`, `socket-udp`, and `socket-tcp`
131+
132+
Enable `smoltcp::socket::RawSocket`, `smoltcp::socket::UdpSocket`,
133+
and `smoltcp::socket::TcpSocket`, respectively.
134+
135+
These features are enabled by default.
136+
130137
## Hosted usage examples
131138

132139
_smoltcp_, being a freestanding networking stack, needs to be able to transmit and receive

‎src/iface/ethernet.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ use wire::{ArpPacket, ArpRepr, ArpOperation};
1010
use wire::{Ipv4Packet, Ipv4Repr};
1111
use wire::{Icmpv4Packet, Icmpv4Repr, Icmpv4DstUnreachable};
1212
use wire::{IpAddress, IpProtocol, IpRepr};
13-
use wire::{UdpPacket, UdpRepr, TcpPacket, TcpRepr, TcpControl};
14-
use socket::{Socket, SocketSet, RawSocket, TcpSocket, UdpSocket, AsSocket};
13+
#[cfg(feature = "socket-udp")] use wire::{UdpPacket, UdpRepr};
14+
#[cfg(feature = "socket-tcp")] use wire::{TcpPacket, TcpRepr, TcpControl};
15+
use socket::{Socket, SocketSet, AsSocket};
16+
#[cfg(feature = "socket-raw")] use socket::RawSocket;
17+
#[cfg(feature = "socket-udp")] use socket::UdpSocket;
18+
#[cfg(feature = "socket-tcp")] use socket::TcpSocket;
1519
use super::ArpCache;
1620

1721
/// An Ethernet network interface.
@@ -30,8 +34,11 @@ enum Packet<'a> {
3034
None,
3135
Arp(ArpRepr),
3236
Icmpv4(Ipv4Repr, Icmpv4Repr<'a>),
37+
#[cfg(feature = "socket-raw")]
3338
Raw((IpRepr, &'a [u8])),
39+
#[cfg(feature = "socket-udp")]
3440
Udp((IpRepr, UdpRepr<'a>)),
41+
#[cfg(feature = "socket-tcp")]
3542
Tcp((IpRepr, TcpRepr<'a>))
3643
}
3744

@@ -169,22 +176,25 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
169176
let mut device_result = Ok(());
170177
let socket_result =
171178
match socket {
179+
#[cfg(feature = "socket-raw")]
172180
&mut Socket::Raw(ref mut socket) =>
173181
socket.dispatch(|response| {
174182
device_result = self.dispatch(timestamp, Packet::Raw(response));
175183
device_result
176184
}),
185+
#[cfg(feature = "socket-udp")]
177186
&mut Socket::Udp(ref mut socket) =>
178187
socket.dispatch(|response| {
179188
device_result = self.dispatch(timestamp, Packet::Udp(response));
180189
device_result
181190
}),
191+
#[cfg(feature = "socket-tcp")]
182192
&mut Socket::Tcp(ref mut socket) =>
183193
socket.dispatch(timestamp, &limits, |response| {
184194
device_result = self.dispatch(timestamp, Packet::Tcp(response));
185195
device_result
186196
}),
187-
&mut Socket::__Nonexhaustive => unreachable!()
197+
&mut Socket::__Nonexhaustive(_) => unreachable!()
188198
};
189199
match (device_result, socket_result) {
190200
(Err(Error::Unaddressable), _) => break, // no one to transmit to
@@ -285,8 +295,11 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
285295
let ip_repr = IpRepr::Ipv4(ipv4_repr);
286296
let ip_payload = ipv4_packet.payload();
287297

288-
// Pass every IP packet to all raw sockets we have registered.
298+
#[cfg(feature = "socket-raw")]
289299
let mut handled_by_raw_socket = false;
300+
301+
// Pass every IP packet to all raw sockets we have registered.
302+
#[cfg(feature = "socket-raw")]
290303
for raw_socket in sockets.iter_mut().filter_map(
291304
<Socket as AsSocket<RawSocket>>::try_as_socket) {
292305
if !raw_socket.accepts(&ip_repr) { continue }
@@ -309,12 +322,19 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
309322
match ipv4_repr.protocol {
310323
IpProtocol::Icmp =>
311324
Self::process_icmpv4(ipv4_repr, ip_payload),
325+
326+
#[cfg(feature = "socket-udp")]
312327
IpProtocol::Udp =>
313328
Self::process_udp(sockets, ip_repr, ip_payload),
329+
330+
#[cfg(feature = "socket-tcp")]
314331
IpProtocol::Tcp =>
315332
Self::process_tcp(sockets, timestamp, ip_repr, ip_payload),
333+
334+
#[cfg(feature = "socket-raw")]
316335
_ if handled_by_raw_socket =>
317336
Ok(Packet::None),
337+
318338
_ => {
319339
let icmp_reply_repr = Icmpv4Repr::DstUnreachable {
320340
reason: Icmpv4DstUnreachable::ProtoUnreachable,
@@ -362,6 +382,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
362382
}
363383
}
364384

385+
#[cfg(feature = "socket-udp")]
365386
fn process_udp<'frame>(sockets: &mut SocketSet,
366387
ip_repr: IpRepr, ip_payload: &'frame [u8]) ->
367388
Result<Packet<'frame>> {
@@ -403,6 +424,7 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
403424
}
404425
}
405426

427+
#[cfg(feature = "socket-tcp")]
406428
fn process_tcp<'frame>(sockets: &mut SocketSet, timestamp: u64,
407429
ip_repr: IpRepr, ip_payload: &'frame [u8]) ->
408430
Result<Packet<'frame>> {
@@ -454,17 +476,20 @@ impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
454476
icmpv4_repr.emit(&mut Icmpv4Packet::new(payload));
455477
})
456478
}
479+
#[cfg(feature = "socket-raw")]
457480
Packet::Raw((ip_repr, raw_packet)) => {
458481
self.dispatch_ip(timestamp, ip_repr, |_ip_repr, payload| {
459482
payload.copy_from_slice(raw_packet);
460483
})
461484
}
485+
#[cfg(feature = "socket-udp")]
462486
Packet::Udp((ip_repr, udp_repr)) => {
463487
self.dispatch_ip(timestamp, ip_repr, |ip_repr, payload| {
464488
udp_repr.emit(&mut UdpPacket::new(payload),
465489
&ip_repr.src_addr(), &ip_repr.dst_addr());
466490
})
467491
}
492+
#[cfg(feature = "socket-tcp")]
468493
Packet::Tcp((ip_repr, mut tcp_repr)) => {
469494
let limits = self.device.limits();
470495
self.dispatch_ip(timestamp, ip_repr, |ip_repr, payload| {

‎src/socket/mod.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@
1010
//! The interface implemented by this module uses explicit buffering: you decide on the good
1111
//! size for a buffer, allocate it, and let the networking stack use it.
1212
13+
use core::marker::PhantomData;
1314
use wire::IpRepr;
1415

15-
mod raw;
16-
mod udp;
17-
mod tcp;
16+
#[cfg(feature = "socket-raw")] mod raw;
17+
#[cfg(feature = "socket-udp")] mod udp;
18+
#[cfg(feature = "socket-tcp")] mod tcp;
1819
mod set;
1920

20-
pub use self::raw::PacketBuffer as RawPacketBuffer;
21-
pub use self::raw::SocketBuffer as RawSocketBuffer;
22-
pub use self::raw::RawSocket;
21+
#[cfg(feature = "socket-raw")]
22+
pub use self::raw::{PacketBuffer as RawPacketBuffer,
23+
SocketBuffer as RawSocketBuffer,
24+
RawSocket};
2325

24-
pub use self::udp::PacketBuffer as UdpPacketBuffer;
25-
pub use self::udp::SocketBuffer as UdpSocketBuffer;
26-
pub use self::udp::UdpSocket;
26+
#[cfg(feature = "socket-udp")]
27+
pub use self::udp::{PacketBuffer as UdpPacketBuffer,
28+
SocketBuffer as UdpSocketBuffer,
29+
UdpSocket};
2730

28-
pub use self::tcp::SocketBuffer as TcpSocketBuffer;
29-
pub use self::tcp::State as TcpState;
30-
pub use self::tcp::TcpSocket;
31+
#[cfg(feature = "socket-tcp")]
32+
pub use self::tcp::{SocketBuffer as TcpSocketBuffer,
33+
State as TcpState,
34+
TcpSocket};
3135

3236
pub use self::set::{Set as SocketSet, Item as SocketSetItem, Handle as SocketHandle};
3337
pub use self::set::{Iter as SocketSetIter, IterMut as SocketSetIterMut};
@@ -47,20 +51,26 @@ pub use self::set::{Iter as SocketSetIter, IterMut as SocketSetIterMut};
4751
/// since the lower layers treat the packet as an opaque octet sequence.
4852
#[derive(Debug)]
4953
pub enum Socket<'a, 'b: 'a> {
54+
#[cfg(feature = "socket-raw")]
5055
Raw(RawSocket<'a, 'b>),
56+
#[cfg(feature = "socket-udp")]
5157
Udp(UdpSocket<'a, 'b>),
58+
#[cfg(feature = "socket-tcp")]
5259
Tcp(TcpSocket<'a>),
5360
#[doc(hidden)]
54-
__Nonexhaustive
61+
__Nonexhaustive(PhantomData<(&'a (), &'b ())>)
5562
}
5663

5764
macro_rules! dispatch_socket {
5865
($self_:expr, |$socket:ident [$( $mut_:tt )*]| $code:expr) => ({
5966
match $self_ {
67+
#[cfg(feature = "socket-raw")]
6068
&$( $mut_ )* Socket::Raw(ref $( $mut_ )* $socket) => $code,
69+
#[cfg(feature = "socket-udp")]
6170
&$( $mut_ )* Socket::Udp(ref $( $mut_ )* $socket) => $code,
71+
#[cfg(feature = "socket-tcp")]
6272
&$( $mut_ )* Socket::Tcp(ref $( $mut_ )* $socket) => $code,
63-
&$( $mut_ )* Socket::__Nonexhaustive => unreachable!()
73+
&$( $mut_ )* Socket::__Nonexhaustive(_) => unreachable!()
6474
}
6575
})
6676
}
@@ -115,6 +125,9 @@ macro_rules! as_socket {
115125
}
116126
}
117127

128+
#[cfg(feature = "socket-raw")]
118129
as_socket!(RawSocket<'a, 'b>, Raw);
130+
#[cfg(feature = "socket-udp")]
119131
as_socket!(UdpSocket<'a, 'b>, Udp);
132+
#[cfg(feature = "socket-tcp")]
120133
as_socket!(TcpSocket<'a>, Tcp);

‎src/socket/set.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use managed::ManagedSlice;
22
use core::slice;
33

44
use super::Socket;
5-
use super::TcpState;
5+
#[cfg(feature = "socket-tcp")] use super::TcpState;
66

77
/// An item of a socket set.
88
///
@@ -68,7 +68,6 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
6868
return put(index, &mut sockets[index], socket)
6969
}
7070
}
71-
7271
}
7372

7473
/// Get a socket from the set by its handle.
@@ -139,17 +138,20 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
139138
let mut may_remove = false;
140139
if let &mut Some(Item { refs: 0, ref mut socket }) = item {
141140
match socket {
141+
#[cfg(feature = "socket-raw")]
142142
&mut Socket::Raw(_) =>
143143
may_remove = true,
144+
#[cfg(feature = "socket-udp")]
144145
&mut Socket::Udp(_) =>
145146
may_remove = true,
147+
#[cfg(feature = "socket-tcp")]
146148
&mut Socket::Tcp(ref mut socket) =>
147149
if socket.state() == TcpState::Closed {
148150
may_remove = true
149151
} else {
150152
socket.close()
151153
},
152-
&mut Socket::__Nonexhaustive => unreachable!()
154+
&mut Socket::__Nonexhaustive(_) => unreachable!()
153155
}
154156
}
155157
if may_remove {

‎src/storage/assembler.rs

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ impl Assembler {
9393
Assembler { contigs }
9494
}
9595

96+
/// FIXME(whitequark): remove this once I'm certain enough that the assembler works well.
97+
#[allow(dead_code)]
9698
pub(crate) fn total_size(&self) -> usize {
9799
self.contigs
98100
.iter()

1 commit comments

Comments
 (1)

whitequark commented on Sep 25, 2017

@whitequark
ContributorAuthor
Please sign in to comment.