CS 5413: High Performance Systems and Networking (Fall 2014)
Lab1, and 2- TCP proxy


The first programming project is meant to introduce you to socket programming, as well as the Unix software development environment (gcc, make, etc.) and pthread programming. You may find it useful to refer to this document for socket programming (Beej's Guide to Network Programming).

Your task will be to write a TCP Proxy. You'll learn how to write both client and server code in this lab.

A TCP proxy server is a server that 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://fireless.cs.cornell.edu/courses/2014fa/cs5413/restricted/ is only accessible from Red Cloud hosts. 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 Red Cloud instance 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.

The assignment

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

# ./tcp-proxy 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:

# ./tcp-proxy www.yahoo.com 80 3000 

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

# ./tcp-proxy fireless.cs.cornell.edu 80 4000 
Then you can view the restricted web page by typing the URL http://128.84.9.XXX:4000/courses/2014fa/cs5413/restricted/ into your browser window, provided that 128.84.9.XXX is the public IP address of your Red Cloud instance, and that you have authorized network access on the proxy listen-port (-p 4000).

Lab 1 (Due Sep 12, 1 week)

The proxy server will accept a single connection from a client and forward it using a single connection to the server. During the connection, the proxy will not accept any other connections.

Lab 2 (Due Sep 22, 2 weeks)

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. In order to do so, you will need to use multiple pthreads. However, in this lab, the maximum number of threads you can use is limited by five (One main thread that accepts connections, and four worker threads that handle active connections). Then, you will need to use select, or poll within each thread to handle multiple connections.


The proxy must behave as followings: Lastly, you must carefully handle memory operations. There must be no memory leaks, dangling pointers, buffer overflows, and any vulnerabilities that C might introduce. Any bug related to memory operations will be counted negatively to your credit.

Fetching and building the source

Start by downloading the skeletal code from CMS (tcp-proxy.tar.gz), then copy it (using scp/rsync) to your home directory on one of your Red Cloud instances (recall that prompt> denotes your own machine, while # denotes the Red Cloud instance). You may use the same image you created during Lab 0. You should be able to get the files from your local box to your instance and build like this:
prompt> scp -i ~/.euca/id-rsa-kp-kl568-test tcp-proxy.tar.gz root@128.84.9.XXX:~/
prompt> ssh -i ~/.euca/id-rsa-kp-kl568-test root@128.84.9.XXX
# tar --no-same-owner -xzf tcp-proxy.tar.gz
# cd tcp-proxy
# make
gcc -pthread -o tcp-proxy tcp-proxy.c 
If you work as a root, it is very important you use the --no-same-owner flag for tar, since you are acting as root on the Red Cloud machine, and otherwise tar will change permissions (sadly propagating up the entire /root/tcp-proxy path) to the user ID of your local machine (the machine the scp command was initiated from), which does not necessarily exist on the Red Cloud instance (unless you fancy being root on your box). As a result, subsequent SSH connections will fail, since the /root home folder of user root has just been owned by a rogue user ID.

First, make sure that you save your work before terminating your instance, either by placing it in a bucket, using a version control system like (CVS, SVN, Git, darcs, etc.), or simply fetching it back on your machine. To fetch it back on your machine, follow the steps:
# cd ~/tcp-proxy
# make dist
prompt> scp -i ~/.euca/id-rsa-kp-kl568-test root@128.84.9.XXX:tcp-proxy/tcp-proxy.tar.gz .

Do not include any files associated with version control systems in your tar ball.

That's it! You've now built tcp-proxy. To test it, type, for example:
# ./tcp-proxy www.yahoo.com 80 1234
Now you should test your program using telnet. In the new window, run:
# telnet localhost 1234
Trying ::1...
telnet: connect to address ::1: Connection refused
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
The message "Connected to localhost" says that your proxy accepted a TCP connection, but then immediately closed it, since the proxy is not fully implemented. You must finish implementing the proxy. You are free to use any basic C library, or you can design your own data structures. It is possible to complete the assignment without using any other external libraries or data structures.


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 fireless.cs.cornell.edu's HTTP port:

# ./tcp-proxy fireless.cs.cornell.edu 80 1234
Now, in another window, use telnet to fetch /big through the proxy:
# telnet localhost 1234
Trying ::1...
telnet: connect to address ::1: Connection refused
Connected to localhost.
Escape character is '^]'.
GET /courses/2014fa/cs5413/labs/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; suspend your telnet by typing ``z RETURN'', wait for 10 seconds, and fetch something else:
telnet> z

# telnet localhost 1234
Trying ::1...
telnet: connect to address ::1: Connection refused
Connected to localhost.
Escape character is '^]'.
GET /courses/2014fa/cs5413/labs/small
You got it!
Connection closed by foreign host.
If you see "You got it!," your program passes the test.

Now try to access the restricted page from your web browser with a URL like http://128.84.9.XXX:1234/courses/2014fa/cs5413/restricted/. Again, make sure 128.84.9.XXX is the IP address of your Red Cloud instance running your tcp-proxy, and that you have authorized access on port 1234 to it.

(Part 2) Next, lower the maximum number of allowed concurrent proxied connections to something like 2, and test by pointing your proxy to fireless.cs.cornell.edu, port 80, just like in the first test. Start by opening 3 telnet connections, but without issuing the HTTP GET. The third connection should not be accepted. Now issue GET /small in one of your two connected telnet prompts, and once you received the responce from the server your third connection should be accepted --- HTTP web servers orderly terminate the connection to indicating that the end of file was reached.

How/What to hand in

TCP proxy

You should submit two things: You should also build the software distribution with the make dist command, as follows:
# make dist
rm -fr .DS_Store *.tar.gz *.ps *.pdf *.o *.dSYM *~ tcp-proxy test-tcpproxy
tar -czf tcp-proxy.tar.gz ../tcp-proxy --exclude=tcp-proxy.tar.gz --exclude=".svn" 
To turn in your distribution, upload the tcp-proxy.tar.gz file on CMS.

If you have any problems about submission, please contact the TAs.

Useful tips

This page was originally created by Tudor Marian.