ccie18643 / PyTCP

PyTCP is a fully functional TCP/IP stack written in Python. It supports TCP stream-based transport with reliable packet delivery based on a sliding window mechanism and basic congestion control. It also supports IPv6/ICMPv6 protocols with SLAAC address configuration. It operates as a user space program attached to the Linux TAP interface.
GNU General Public License v3.0
345 stars 35 forks source link
arp ethernet icmp ip ipv4 ipv6 linux network python tcp udp

PyTCP

The TCP/IP stack written in Python


GitHub release OS Supported Versions GitHub License CI

GitHub watchers GitHub forks GitHub stars


PyTCP is a fully functional TCP/IP stack written in Python. It supports TCP stream-based transport with reliable packet delivery based on a sliding window mechanism and basic congestion control. It also supports IPv6/ICMPv6 protocols with SLAAC address configuration. It operates as a user space program attached to the Linux TAP interface. It has implemented simple routing and can send and receive traffic over a local network and the Internet.

Version 2.7, unlike its predecessors, contains the PyTCP stack code in the form of a library so that it can be easily imported and used by external code. This should make the user experience smoother and eventually provide the full ability to replace the standard Linux stack calls (e.g., socket library) with the PyTCP calls in any 3rd party application.

This project initially started as a purely educational effort aimed at improving my Python skills and refreshing my network knowledge as part of the preparation for the Network Engineer role at Facebook. Since then, it has become more like a 'pet project' which I dedicate some of my time on a somewhat irregular basis. However, a couple of updates are usually added to it every month or two.

I welcome any contributions and help from anyone interested in network programming. Any input is appreciated. Also, remember that some stack features may be implemented only partially (as needed for stack operation). They may be implemented in a sub-optimal fashion or not 100% RFC-compliant way (due to lack of time), or they may contain bug(s) that I still need to fix.

Please feel free to check my two other related projects:


Principle of operation and the test setup

The PyTCP stack depends on the Linux TAP interface. The TAP interface is a virtual interface that, on the network end, can be 'plugged' into existing virtual network infrastructure via either Linux bridge or Open vSwitch. On the internal end, the TAP interface can be used like any other NIC by programatically sending and receiving packets to/from it.

If you wish to test the PyTCP stack in your local network, I'd suggest creating the following network setup that will allow you to connect both the Linux kernel (essentially your Linux OS) and the PyTCP stack to your local network at the same time.

<INTERNET> <---> [ROUTER] <---> (eth0)-[Linux bridge]-(br0) <---> [Linux TCP/IP stack]
                                            |
                                            |--(tap7) <---> [PyTCP TCP/IP stack]

After the example program (either client or service) starts the stack, it can comunicate with it via simplified BSD Sockets like API interface. There is also the possibility of sending packets directly by calling one of the _*_phtx() methods from PacketHandler class.


Clonning PyTCP from the GitHub repository

In most cases, PyTCP should be cloned directly from the GitHub repository, as this type of installation provides full development and testing environment.

git clone http://github.com/ccie18643/PyTCP

After cloning, we can run one of the included examples:

To fine-tune various stack operational parameters, please edit the pytcp/config.py file accordingly.


Installing PyTCP from the PyPi repository

PyTCP can also be installed as a regular module from the PyPi repository.

python -m pip install PyTCP

After installation, please ensure the TAP interface is operational and added to the bridge.

sudo ip tuntap add name tap7 mode tap
sudo ip link set dev tap7 up
sudo brctl addbr br0
sudo brctl addif br0 tap7

The PyTCP stack can be imported and started using the following code. It starts the stack subsystems and autoconfigures both the IPv4 and IPv6 protocol addresses using DHCPv4 and IPv6 SLAAC, respectively.

from pytcp import TcpIpStack
stack = TcpIpStack(interface="tap7")
stack.start()

The stack subsystems run in their own threads. After starting, the stack gives control back to the user code and can be stopped using the following call.

stack.stop()

Features

Already implemented:

To be implemented:


Examples

Several ping packets and two monkeys were delivered via TCP over the IPv6 protocol.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


IPv6 Neighbor Discovery / Duplicate Address Detection / Address Auto Configuration.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


TCP Fast Retransmit in action after lost TX packet.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


Out-of-order queue in action during RX packet loss event

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


TCP Finite State Machine - stack is running TCP Echo service.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


TCP Finite State Machine - stack is running TCP Echo client.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


Pre-parse packet sanity checks in action.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


ARP Probe/Announcement mechanism.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output


ARP resolution and handling ping packets.

Sample PyTCP log output Sample PyTCP log output


IP fragmentation.

Sample PyTCP log output Sample PyTCP log output Sample PyTCP log output