Skip to content
/ tcp Public

A toy TCP implementation in Rust

Notifications You must be signed in to change notification settings

staceytay/tcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A simple TCP implementation in Rust

An attempt to learn Rust and TCP while at the Recurse Center. I also wrote about the experience of working on this here.

Design overview

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.

Things implemented

  • 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.

Things not implemented (yet?)

TODO

Setup

Dependencies

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.

Examples

$ cargo run --example http-get http://example.com

Credits and resources

About

A toy TCP implementation in Rust

Topics

Resources

Stars

Watchers

Forks

Languages