G22.3033-101 Lab 2: TCP Proxy

Due date: Wednesday Feb. 1. Don't wait until the last minute.


In this lab you'll write a TCP Proxy using the same C++ asynchronous library as in the first lab. You'll learn how to write both client and server code in this lab.

A TCP proxy server is a server which acts as an intermediary between a client and another server, called the destination server. Clients establish connections to the TCP proxy server, which then establishes a connection to the destination server. The proxy server sends data received from the client to the destination server and forwards data received from the destination server to the client. Interestingly, the TCP proxy server is actually both a server and a client. It is a server to its client and a client to its destination server.

A TCP proxy server can be useful to get around services which restrict connections based on the network addresses. For example, the web page http://www.scs.cs.nyu.edu/G22.3033-010/restricted/ is only accessible from the G22.3033-010 test machines. If you try to access it from elsewhere, you will receive an access denied error. However, you can view this page from a web browser anywhere on the Internet by running a proxy server on one of the class machines. The web server will think it is serving the data to a web client on the machine running the proxy. However, the proxy is forwarding the data out of the class network, thus subverting the protection mechanism. (Note that while you can do this for the www.scs.cs.nyu server, you should not point a proxy at other restricted servers at NYU--this would violate NYU's network policy.)

The assignment

The proxy server you will build for this lab will be invoked at the command line as follows:

% ./tcpproxy destination-host destination-port listen-port

For example, to redirect all connections to port 3000 on your local machine to yahoo's web server, run:

% ./tcpproxy www.yahoo.com 80 3000 &

As another example, to view the restricted web page mentioned above, you might run the following command on machine class5:

% ./tcpproxy www.scs.cs.nyu.edu 80 4000 &
Then you can view the restricted web page by typing the URL http://class5.scs.cs.nyu.edu:4000/G22.3033-010/restricted/ into your browser window. The trailing slash on .../restricted/ is actually important important in this context, to avoid getting redirected to www.scs.cs.nyu.edu. Of course if someone is already using port 4000, you will need to choose another port.

The proxy server will accept connections from multiple clients and forward them using multiple connections to the server. No client or server should be able to hang the proxy server by refusing to read or write data on its connection. For instance, if one client suddenly stops reading from the socket to the proxy, other clients should not notice interruptions of service through the proxy. You will need asynchronous behavior, described in "Using TCP Through Sockets".

The proxy must also handle hung clients and servers. In particular, if one end keeps transmitting data but the the other stops reading, the proxy must not buffer an unlimited amount of data. Once the amount of buffered data in a given direction reaches some high water mark (e.g., 8K), the proxy must stop reading in that direction until the buffer drains. If the proxy has buffered data in one direction and is unable to write any of it for 10 seconds, it should abort both connection pairs.

Connection termination

The proxy must handle end-of-file conditions as transparently as possible. If it reads end-of-file from one socket, it should pass the condition along to the other socket (using shutdown) after writing any remaining buffered data. However, the proxy should continue to forward data in the other direction. The proxy should terminate a connection pair and close the file descriptors under the following two circumstances:

  1. The proxy has read an end-of-file (or experienced a read error other than EAGAIN) in both directions and has written all remaining buffered data.
  2. The proxy experiences a write error (other than EAGAIN) in either direction.
The reason for giving up more easily on write errors is that they signify some failure of the higher-level protocol. A read end-of-file can be a legitimate part of a protocol, whereas when a program writes data to the network, it indicates a serious problem if no one is there to read it.

Setting up your project directory

To set up a project directory for your tcp proxy, you can use the multifinger directory from the first lab as a template. First, clean the multifinger directory with the command gmake maintainer-clean, then copy it as follows:
% cd multifinger
% gmake maintainer-clean
test -z "aclocal.m4 install-sh mkinstalldirs    missing configure ...
rm -f config.h
This command is intended for maintainers to use;
it deletes files that may require special tools to rebuild.
rm -f config.status
% cd ..
% cp -pr multifinger tcpproxy
% cd tcpproxy
% rm multifinger.C multifinger.h
Now you must edit the autoconf and automake configuration files. First, edit configure.in. At the top, you fill find the line:
AM_INIT_AUTOMAKE(multifinger, 0.0)
Change multifinger to tcpproxy. (This changes the name of the software package when you make a distribution.)

Next, you must edit Makefile.am to change the program you are going to build and the source files to include. There are three lines of interest:

bin_PROGRAMS = multifinger
noinst_HEADERS = multifinger.h
multifinger_SOURCES = multifinger.C
Thus, your modified Makefile.am might contain the following lines:
bin_PROGRAMS = tcpproxy
noinst_HEADERS = 
tcpproxy_SOURCES = tcpproxy.C
And you would put the source to your TCP proxy in tcpproxy.C. Finally, when you are ready to compile, you can proceed as you did with multifinger:
% ./setup
+ chmod +x setup
+ aclocal
+ set +x
% setenv DEBUG -g
% ./configure --with-sfs=/home/c/class/lib/debug
creating cache ./config.cache
checking for a BSD compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
creating Makefile
creating config.h
% gmake


You should test your proxy to make sure that it continues to forward data even when some connections aren't responding. Here's one test you should be able to pass.

First, run the proxy and point it at www.scs.cs.nyu.edu's HTTP port:

% ./tcpproxy www.scs.cs.nyu.edu 80 1234
Now, in another window, use telnet to fetch /cgi-bin/big through the proxy:
% telnet 127.1 1234
Connected to localhost (
Escape character is '^]'.
GET /cgi-bin/big
Watch the data go by for a while, then interrupt the output by typing control-], after which telnet should stop and print telnet>. Now check that the proxy hasn't been hung because telnet isn't reading data; open another window and fetch something else:
% telnet 127.1 1234
Connected to localhost (
Escape character is '^]'.
GET /ok
You were able to fetch the data.
Connection closed by foreign host.
If you see "You were able to fetch the data," your program passes the test. Now try to access the restricted page from your web browser, for instance with the URL http://class5.scs.cs.nyu.edu:1234/G22.3033-010/restricted/.

Once your proxy passes some basic tests, you can test it with the automated program test-tcpproxy. You can find this program on the class machines in ~class/bin/test-tcpproxy, which should be in your path by default. Assuming your proxy is in ./tcpproxy, you can test it as follows:

% test-tcpproxy ./tcpproxy
Initializing random buffer...Done
trying to launch tcpproxy: ./tcpproxy localhost 1093 listen port 1088
Starting tests...
Phase 1: (client write,server echo)checking the integrity of returned data
1 simultaneous connections (with read/write), 200 total...Succeeded
20 simultaneous connections (with read/write), 200 total...Succeeded
70 simultaneous connections (with read/write), 200 total...Succeeded
Phase 2: testing client-write-only-and-server-read-only and vice versa
1 simultaneous connections(1 total) client write, server read...Succeeded
1 simultaneous connections(1 total) client read, server write...Succeeded
Phase 3: testing hanging server/hanging client
1 simultaneous connections(1 total) client read, server write...Succeeded
1 simultaneous connections(1 total) client read, server write...Succeeded
1 simultaneous connections(200 total) client read, server write...Succeeded
Proxy down
Your program should pass all phases of the tests.

How/What to hand in

Feb 1 - TCP proxy

You should submit a complete software distribution of your tcpproxy program. As in the previous lab, you should build a software distribution as follows:
% gmake distcheck
rm -rf tcpproxy-0.0
mkdir tcpproxy-0.0
chmod 777 tcpproxy-0.0
tcpproxy-0.0.tar.gz is ready for distribution
To turn in your distribution, copy the file tcpproxy-0.0.tar.gz to the directory ~class/handin/lab2/username where username is your username:
% cp tcpproxy-0.0.tar.gz ~class/handin/lab2/`logname`/
If you have any problems about submission, please contact dm@cs.nyu.edu. The lab is due before class on Wednesday, Feb 1st.


The following are useful references for socket programming (in addition to the class handout Using TCP through sockets):