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 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 4000Then 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.XXXis the public IP address of your Red Cloud instance, and that you have authorized network access on the proxy listen-port (-p 4000).
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.
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
poll within each thread to handle multiple connections.
semaphorefor this. Also, you must use
pthread_mutex_tfor accessing shared data structures. But, you should not abuse your mutexes. Minimize criticial sections as much as possible to achieve better performance.
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 firstname.lastname@example.org.XXX:~/ prompt> ssh -i ~/.euca/id-rsa-kp-kl568-test email@example.com.XXX # tar --no-same-owner -xzf tcp-proxy.tar.gz # cd tcp-proxy # make gcc -pthread -o tcp-proxy tcp-proxy.cIf you work as a root, it is very important you use the
--no-same-ownerflag for tar, since you are acting as
rooton 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
rooton your box). As a result, subsequent SSH connections will fail, since the
/roothome folder of user
roothas just been owned by a rogue user ID.
# cd ~/tcp-proxy # make dist prompt> scp -i ~/.euca/id-rsa-kp-kl568-test firstname.lastname@example.org.XXX:tcp-proxy/tcp-proxy.tar.gz .
tcp-proxy. To test it, type, for example:
# ./tcp-proxy www.yahoo.com 80 1234Now you should test your program using telnet. In the new window, run:
# telnet localhost 1234 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... 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 1234Now, in another window, use telnet to fetch /big through the proxy:
# telnet localhost 1234 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET /courses/2014fa/cs5413/labs/bigWatch 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 ``
zRETURN'', wait for 10 seconds, and fetch something else:
telnet> z Suspended # telnet localhost 1234 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... 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.
# 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.gzfile on CMS.
If you have any problems about submission, please contact the TAs.
man socketgives you the man page for the socket system call.
ulimitcommand before you will see the core files being created (e.g.
ulimit -c unlimited). You can examine the core files with
gdbin order to learn what went wrong --- this is an invaluable tool. You can start by typing
gdb program program.core, and then typing the gdb command
backtrace. GDB will in turn return a trace pointing to where your program has crashed.
This page was originally created by Tudor Marian.