|
преди 7 години | |
---|---|---|
examples | преди 7 години | |
src | преди 7 години | |
.gitignore | преди 8 години | |
.travis.yml | преди 7 години | |
Cargo.toml | преди 7 години | |
LICENSE-0BSD.txt | преди 8 години | |
README.md | преди 7 години |
smoltcp is a standalone, event-driven TCP/IP stack that is designed for bare-metal, real-time systems. Its design goals are simplicity and robustness. Its design anti-goals include complicated compile-time computations, such as macro or type tricks, even at cost of performance degradation.
smoltcp does not need heap allocation at all, is extensively documented, and compiles on stable Rust 1.18 and later.
smoltcp is missing many widely deployed features, whether by design or simply because no one implemented them yet. To set expectations right, both implemented and omitted features are listed.
The only supported medium is Ethernet.
The only supported internetworking protocol is IPv4.
The UDP protocol is supported over IPv4.
The TCP protocol is supported over IPv4. Server and client sockets are supported.
To use the smoltcp library in your project, add the following to Cargo.toml
:
[dependencies]
smoltcp = "0.3"
The default configuration assumes a hosted environment, for ease of evaluation. You probably want to disable default features and configure them one by one:
[dependencies]
smoltcp = { version = "0.3", default-features = false, features = ["..."] }
std
The std
feature enables use of objects and slices owned by the networking stack through a
dependency on std::boxed::Box
and std::vec::Vec
.
This feature is enabled by default.
raw_socket
and tap_interface
Enable smoltcp::phy::RawSocket
and smoltcp::phy::TapInterface
, respectively.
These features are enabled by default.
alloc
The alloc
feature enables use of objects owned by the networking stack through a dependency
on alloc::boxed::Box
. This only works on nightly rustc.
collections
The collections
feature enables use of slices owned by the networking stack through a dependency
on collections::vec::Vec
. This only works on nightly rustc.
log
The log
feature enables logging of events within the networking stack through
the log crate. The events are emitted with the TRACE log level.
This feature is enabled by default.
verbose
The verbose
feature enables logging of events where the logging itself may incur very high
overhead. For example, emitting a log line every time an application reads or writes as little
as 1 octet from a socket is likely to overwhelm the application logic unless a BufReader
or BufWriter
is used, which are of course not available on heap-less systems.
This feature is disabled by default.
smoltcp, being a freestanding networking stack, needs to be able to transmit and receive raw frames. For testing purposes, we will use a regular OS, and run smoltcp in a userspace process. Only Linux is supported (right now).
On *nix OSes, transmiting and receiving raw frames normally requires superuser privileges, but on Linux it is possible to create a persistent tap interface that can be manipulated by a specific user:
sudo ip tuntap add name tap0 mode tap user $USER
sudo ip link set tap0 up
sudo ip addr add 192.168.69.100/24 dev tap0
In order to demonstrate the response of smoltcp to adverse network conditions, all examples implement fault injection, available through command-line options:
--drop-chance
option randomly drops packets, with given probability in percents.--corrupt-chance
option randomly mutates one octet in a packet, with given
probability in percents.--size-limit
option drops packets larger than specified size.--tx-rate-limit
and --rx-rate-limit
options set the amount of tokens for
a token bucket rate limiter, in packets per bucket.--shaping-interval
option sets the refill interval of a token bucket rate limiter,
in milliseconds.A good starting value for --drop-chance
and --corrupt-chance
is 15%. A good starting
value for --?x-rate-limit
is 4 and --shaping-interval
is 50 ms.
examples/tcpdump.rs is a tiny clone of the tcpdump utility.
Unlike the rest of the examples, it uses raw sockets, and so it can be used on regular interfaces,
e.g. eth0
or wlan0
, as well as the tap0
interface we've created above.
Read its source code, then run it as:
cargo build --example tcpdump
sudo ./target/debug/tcpdump eth0
examples/server.rs emulates a network host that can respond to requests.
The host is assigned the hardware address 02-00-00-00-00-01
and IPv4 address 192.168.69.1
.
Read its source code, then run it as:
cargo run --example server -- tap0
It responds to:
ping 192.168.69.1
);socat stdio udp4-connect:192.168.69.1:6969 <<<"abcdefg"
),
where it will respond "yo dawg" to any incoming packet;socat stdio tcp4-connect:192.168.69.1:6969
),
where it will respond "yo dawg" to any incoming connection and immediately close it;socat stdio tcp4-connect:192.168.69.1:6970 <<<"abcdefg"
),
where it will respond with reversed chunks of the input indefinitely.cat /dev/urandom | socat stdio tcp4-connect:192.168.69.1:6971
),
which will be ignored.The buffers are only 64 bytes long, for convenience of testing resource exhaustion conditions.
examples/client.rs emulates a network host that can initiate requests.
The host is assigned the hardware address 02-00-00-00-00-02
and IPv4 address 192.168.69.2
.
Read its source code, then run it as:
cargo run --example client -- tap0 ADDRESS PORT
It connects to the given address (not a hostname) and port (e.g. socat stdio tcp4-listen 1234
),
and will respond with reversed chunks of the input indefinitely.
examples/ping.rs implements a minimal version of the ping
utility using raw sockets.
The host is assigned the hardware address 02-00-00-00-00-02
and IPv4 address 192.168.69.1
.
Read its source code, then run it as:
cargo run --example ping -- tap0 ADDRESS
It sends a series of 4 ICMP ECHO_REQUEST packets to the given address at one second intervals and prints out a status line on each valid ECHO_RESPONSE received.
The first ECHO_REQUEST packet is expected to be lost since arp_cache is empty after startup; the ECHO_REQUEST packet is dropped and an ARP request is sent instead.
Currently, netmasks are not implemented, and so the only address this example can reach
is the other endpoint of the tap interface, 192.168.1.100
. It cannot reach itself because
packets entering a tap interface do not loop back.
smoltcp is distributed under the terms of 0-clause BSD license.
See LICENSE-0BSD for details.