Miniweb is a proof-of-concept implementation of the TCP/IP protocol stack together with a webserver that uses around 30 bytes of RAM. The TCP/IP stack and the webserver uses around 30 bytes of RAM. The code is written in C and constitutes around 400 lines, comments removed. It should be possible to further minimize the code size and memory usage. Note: this code is not intended for actual use, it is only meant for educational purposes! Miniweb is just a proof of concept that shows that it is indeed possible to implement a TCP/IP stack and a web server using very small amounts of RAM. For a full-scale TCP/IP stack, refer to uIP or lwIP. Miniweb was written by Adam Dunkels at the Swedish Institute of Computer Science.
In order to reduce RAM usage, Miniweb stores its web pages as precomputed IP/TCP packets complete with headers. When sending the packets, only a few fields are updated and the checksums adjusted. Incoming packets are processed one byte at a time and therefore there is no need to buffer the data. The checksum is computed when each byte is received and if the checksum is found to be incorrect when the entire packet has been received, the contents of the packet is not used for further processing.
There are two variants of Miniweb, a stateless variant and a statefull variant. The stateless variant is a bit smaller and uses less RAM, but does not provide the same functionality as the statefull variant.
The stateless Miniweb keeps only minimal state information. Instead of keeping connection state, the stateless Miniweb uses the acknowledgement numbers of the incomming ACKs to decide which TCP segment to send. The IP address, port number and TCP sequence number of the last received ACK is also saved so that if an ACK is lost, the corresponding segment can be retransmitted. This means that if an ACK in another "connection" arrives before the retransmission occurs, the state of the previous connection is lost, and the retransmission will never occur. This connection will hang until the application in the other end sends data.
It it important to note that this approach of course is totally and utterly wrong; a system using this approach should not be connected to an actual network, let alone the global Internet!
To make matters worse:
|
If you have a FreeBSD box you can test Miniweb directly. Linux users need to download the universal tun/tap network interface and compile it into their kernel.
Download the code and do:
gunzip -c miniweb.tar.gz | tar xvf - cd miniweb gmake su ./miniweb
Now you should have the Miniweb TCP/IP stack and web server running on IP address 192.168.0.2. You can try it out by clicking on the link. Note that it isn't possible to use ping to reach the TCP/IP stack.
To inspect the packets that are sent to and from Miniweb, run
tcpdump -l -n -S -i tun0
Some links to other small TCP/IP stacks plus web servers using the same ideas that Miniweb uses:
$Date: 2005/11/11 12:21:48 $