Comp 343 program 2: port forwarder

Due: Friday, April 30

This program is for those registered as CS undergraduates. That is, you are registered as an undergrad, and are not in the criminal-justice minor (those students, for whom Comp 271 is not required, will write a paper instead).

When started on host foohost with the command line
    java forwarder 3333 outhost 44
then every time a connection is made to foohose:3333, a new connection is made to outhost:44 and two copier threads are created to copy the data between the two connections (one copier thread for each direction). The net result is that it appears to the user that a connection to foohost:3333 is actually a connection to outhost:44. No timeouts are needed, though thread creation is necessary.

You are to print a line or two for each connection, indicating the original source (host,port) for the incoming connection, and the port used for the outbound forwarded connection. Note that this has a practical security implication, in that it may be your only warning that someone else is using your forwarder! A suggested improvement is to check the incoming TCP source and reject connections from hosts other than the one you're using to test this.

Thread creation is demonstrated in the threaded stalk server file. I'm also giving you the Copier class (as an inner class, defined in forwarder.java), that is thread-ready and which takes two sockets from and to and arranges to copy from the from socket to the to socket. To set up the copying, you first have the two sockets, s1 (the inbound socket from the accept() call) and s2 (the new second connection you create). You then create two Copier objects, one inbound = Copier(s1,s2) and one outbound = Copier(s2,s1), and then start both the threads:
     
    new Thread(inbound).start();
    new Thread(outbound).start();

Here the inbound Copier object handles data from the initiating host (foohost) to outhost, and the outbound Copier object handles the reverse. Note that after these threads have been created, your main program can return to the accept() call to wait for more inbound connections. In this sense, forwarder acts like the threaded stalk server, tstalks.java.

To get started, use forwarder.java.

A good way to test your program, if you're doing development on your own workstation, is to start with
    java forwarder 3333 www.luc.edu 80
Then fire up a web browser and point it at localhost:3333. You should get Loyola's web page. If you're working on a linux server, remotely, say infinity.cs.luc.edu (or random.cs.luc.edu), then start the command above on infinity and then point your browser at infinity.cs.luc.edu:3333. You may have to change the port number if you're working in a shared environment.

Do not leave your forwarder running longer than you need to test it.