Skip to content

A TCP/IP implementation over raw sockets as per RFC 793

Notifications You must be signed in to change notification settings

InfiniteVerma/cpp-tcpip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpp-tcpip

This project is a custom implementation of the TCP/IP protocol stack (as per RFC 793), simulating core TCP mechanisms such as the 3-way handshake and basic data transmission. It includes a user-space simulation where socket operations are handled by communicating with a kernel-like thread, which manages connection states and packet transmission.

Features

  • 3-Way Handshake: The implementation successfully establishes a TCP connection using the SYN, SYN-ACK, and ACK packets.
  • Data Transmission: Sending and receiving data between connected sockets is functional (currently using some workarounds).
  • User and Kernel Thread Simulation: Socket operations are initiated by the user thread and executed by a separate "kernel" thread using message queues for inter-thread communication.
2024-09-04.23-31-00.mp4

Current Status

Core mechanism of the TCP protocol are implemented (with some hacks) including connection establishment (3-way handshake) and data transfer. Details are mentioned below in Plan section.

Usage

Note: Currently, our 3-way handshake doesn't support timeout + retransmission so first run myserver, wait for it to reach LISTEN state and then run myclient to reach ESTABLISHED state.

  1. Clone the repository

  2. Add the two ips in lo loopback interface

sudo ip addr add 192.168.1.1/24 dev lo
sudo ip addr add 192.168.1.2/24 dev lo
  1. Build the project
make -j4
  1. Open two terminals and run below:
sudo ./myserver |tee server.log

and

sudo ./myclient |tee client.log

Plan

Continuously iterating on it since there's so much going on here :)

  • Read RFC793 few times
  • Boilerplate code
    • Make your own basic socket API calls
    • TCB basic code
  • Communication over raw sockets
    • IP Header class
    • Test and verify packet exchange
  • 3 way handshake init
    • Add TCP segment header
    • Basic FSM driver logic (using fn pointers?)
    • One way FSM
    • Payload -> TCP Segment + Payload -> IP Header + TCP Segment + Payload
  • Reorganize code before it gets out of hand
  • Make a different thread for TCP stuff
    • Redesign the socket design for 'userland' and 'tcp' (kernel) thread state
    • Communicate
    • Move socket logic to new thread
    • Implement timer logic
    • Stop the tcp thread gracefully
    • Verify the handshake that worked before
    • Stop threads once handshake is done for now
  • Proper handshake
    • Fix timer. Timeout should fail and after receiving ACK, delete timer
    • Implement it as designed in the "Event Processing" chapter in RFC
    • [?] Full state machine
    • Do 3 way handshake and maintain the state (of FSM) properly
  • Close socket and free resources
  • Send and receive data
    • Insert packet in a buffer and use a bitset with mutex
    • Retrieve packet in tcp thread
    • Add tcp/ip headers and send to socket
    • After ESTABLISHED, server needs to listen for data
    • For now, get the packet and dump it
  • Add debug logging
    • write to a logfile to keep it separate from user code
  • Sequence and ACK logic
  • Revisit the ACTION logic of FSM (should be more tightly coupled)
  • Revisit FSM hacking done in recvSegment
  • Retransmission queue
    • Test 3 way handshake with this
  • Add properly error codes
  • getISN from time module 2**32

End goal: Sending a file as a byte stream over this tcp/ip implementation

Gochas and lessons learned

  • Assigned a char* pointer from MyMsg into a packet but the MyMsg instance gets deleted so pointer now pointed to undefined area.
  • Assigned and mutated a local copy of an object and then was confused why the actual object wasn't updated.
  • Called join of the second thread in it's own context (instead of main thread) causing an exception

References

About

A TCP/IP implementation over raw sockets as per RFC 793

Topics

Resources

Stars

Watchers

Forks