An attempt to learn Rust and TCP while at the Recurse Center. I also wrote about the experience of working on this here.
API modeled after Rust's TcpStream
with read
and write
functions exposed
to clients. I also used the typestate
pattern to model the
different TCP states.
- Three-way handshake, sending and reading of packets, closing of TCP connection.
- Verification of received packet's checksum and sequence numbers.
- Waiting for ACK from remote host for packets sent.
TODO
This implementation was tested on a Linux VM and requires Linux to run.
# set up `tun0`
sudo ip link del tun0
sudo ip tuntap add name tun0 mode tun user $USER
sudo ip link set tun0 up
sudo ip addr add 192.0.2.1 peer 192.0.2.2 dev tun0
# set up NAT
sudo iptables -t nat -A POSTROUTING -s 192.0.2.2 -j MASQUERADE
sudo iptables -A FORWARD -i tun0 -s 192.0.2.2 -j ACCEPT
sudo iptables -A FORWARD -o tun0 -d 192.0.2.2 -j ACCEPT
sudo sysctl -w net.ipv4.ip_forward=1
Taken from How to send raw network packets in Python with tun/tap.
$ cargo run --example http-get http://example.com
- Beej's Guide to Network Programming
- How to send raw network packets in Python with tun/tap
- Julia Evans' Implementing TCP in a Weekend (beta)
- RFC1122: Requirements for Internet Hosts -- Communication Layers
- RFC2581: TCP Congestion Control
- RFC6056: Recommendations for Transport-Protocol Port Randomization
- RFC6335: Internet Assigned Numbers Authority (IANA) Procedures for the Management of the Service Name and Transport Protocol Port Number Registry
- Rust for Rustaceans, specifically Chapter 3 for introducing the typestate pattern.
- https://crates.io/crates/smoltcp
- https://github.com/WireGuard/wireguard-rs
- https://github.com/cloudflare/boringtun
- https://github.com/meh/rust-tun