New: I have written a short FAQ that answers some of the questions that popped up in the comments to the Slashdot article.

phpstack is a proof-of-concept TCP/IP stack and web server written entirely in the PHP scripting language. The TCP/IP stack implements only the very minimal requirements for being able to run the extremely simple web server. The web server contains no functionality whatsoever - but is has the ability to execute PHP scripts.

A picture of the demo server

Note: this code is not meant to be taken too seriously, it is just a quick and dirty proof-of-concept hack written in about 3 hours. If you are looking for a real TCP/IP stack please see lwIP or uIP.

The demo server setup: 193.10.67.151

The phpstack software is running on a demonstration server here. The server runs as a process on a PC running FreeBSD. The PC has a null-modem cable connected between its two serial ports. A SLIP interface routes IP packets over the serial cable and the phpstack software is listening on the other end of the cable. The PC is a 450 MHz Pentium-III with 386 megabytes of memory running FreeBSD 4.9. The serial cable operates at 115200 bits per second and the web server is able to push approximately 5 connections per second.

Note that the phpstack software does not run under the Apache web server, but as a stand-alone program.

The PHP language

The PHP language originally was originally developed for HTML processing for web applications. One of the killer features of PHP was (and still is!) the ability to embed the PHP code into HTML documents. The last few years, however, people have been using PHP for writing general purpose programs; programs for which PHP wasn't really intended. There are now PHP OpenGL wrappers, GUI libraries and even web and FTP servers written in PHP. The phpstack software extends this list down to the very lowest level of Internetworking: the TCP/IP stack.

The TCP/IP stack

All computers connected to the Internet runs a TCP/IP stack. The purpose of the TCP/IP stack is to let applications talk to each other over the Internet. The TCP/IP stack is hidden deeply inside the operating systems and most users and developers will not even come near the actual TCP/IP stack software. Many developers have, however, used sockets to reach the network from their programs - sockets are the interface to the TCP/IP stack on most operating systems. The socket layer hides most of the complexity of the TCP/IP stack from application programmers.

phpstack contains a very, very simple TCP/IP stack. The stack is tailored explicitly for the purpose of answering pings, incoming TCP requests, opening up a connection, and sending out responses to incoming data. This functionality is enough to be able to support a small web server. The stack does not handle two-way communication, nor does it have the ability to support long-running connections.

Because of the cut-down functionality, it is possible to optimize the TCP/IP stack so that it do not require any extra memory at all for each new connection. It is therefore possible to support an unlimited number of simultaneous connections. The only limit is the bandwidth of the incoming network connection.

The web server

The web server in phpstack is extremely simple. It listens to incoming connection requests and sends out an HTML page or a picture in response to the requests. It can only handle pages or pictures with a maximum size of 1460 bytes, because of the limited TCP/IP stack.

A picture of the phpstack code

To reduce the complexity of both the TCP/IP stack and the web server, the web server does not parse incoming requests. Instead, it uses the TCP port number of the incoming connection to distinguish between different pages or pictures.

Since the web server is written in PHP, it is easy to allow the served web pages to contain PHP code.

The code

The roughly 600 lines of code are available here and is provided under the 3-clause BSD license. phpstack is provided as a reference and a program that people might find entertaining and possibly educating.

Adam Dunkels