//: RelayD.java

/** Very simple server that just relays whatever the client sends onto
  * another server and then sends the server response back to the
  * original client.  Basically a dumb Gateway or Firewall tunnel.
  * Developed for use with IBM's Desktop OnCall, which cannot traverse
  * a firewall without help.
  * @author Mark Crocker
  * @author http://www.markcrocker.com/~mcrocker/
  * @version 0.91 (beta) 2000-May-13
  * Derived from @see c15.MultiJabberServer from 'Thinking in Java,
  * 2nd ed.' by Bruce Eckel www.BruceEckel.com.  */

/* This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 */

import java.io.*;
import java.net.*;

public class RelayD { 

    /** Default port that Desktop On-Call appears to be on */
    private static final int LISTENPORT = 8082;
    /** Default ort that Desktop On-Call really is on at the real host */
    private static final int SENDPORT = 8082;
    /** Default true host that Desktop On-Call lives on */
    private static final String RELAYTOHOST = "192.168.0.2"; 

    public static void main(String[] args) 
	throws IOException, InterruptedException {

	int listenPort = LISTENPORT;
	int sendPort = SENDPORT;
	String relayToHost = RELAYTOHOST;

	// Should set-up a generic command line option parsing class.
	if(args.length == 3) {
	    listenPort = Integer.parseInt(args[0]);
	    sendPort = Integer.parseInt(args[1]);
	    relayToHost = args[2];
	} if(args[0].equals("-help")) {
	    System.err.println("Usage: RelayD listenPort sendPort relayToHost");
	    System.exit(1);
	}


	// Server port
	ServerSocket s = new ServerSocket(listenPort);
	System.out.println("Server started: " + s);


	try {
	    while(true) {
		// Blocks until a connection occurs:
		Socket socketS = s.accept();
		try {

		    // Relay port
		    InetAddress addr =  InetAddress.getByName(relayToHost);
		    System.out.println("relaying to addr = " + addr);
		    Socket socketR = new Socket(addr, sendPort);
		    System.out.println("Relay socket started = " + socketR);


		    new RelayOneWay(socketS, socketR);
		    new RelayOneWay(socketR, socketS);
		    // If it fails, close the socket,
		    // otherwise the thread will close it:
		} catch(IOException e) {
		    System.out.println("Error establishing sockets...");
		    socketS.close();
		    //socketR.close();
		}
	    }
	} finally {
	    s.close();
	    //socketR.close();
	}
    } 
} ///:~

/* Todo:
 * 1. Command Line options.
 * 2. Popup window to fill in if no command line options supplied.
 * 3. Logging.
 * 4. Quiet mode.
 * 5. Debug mode.
 * 6. Update documentatin and generate Javadocs.
 */