int listening_socket ;
int connections ;
int display, pane, ip ;
string destination ;
int connect_port ;


    int baric ;


void display_close_request(int display)
{
  quit() ;
}

void socket_closed(int handle)
{
  string message ;

  message="Sockets " ;
  string_cat(message, int_to_string(handle)) ;
  string_cat(message, " closed.") ;
  text_to_display(display, 0, message, "") ;
}

void reverse_lookup_complete(int handle; int event; string name; int ip)
{
  string message ;

  message="Reverse_lookup: " ;
  string_cat(message, name) ;
  text_to_display(display, 0, message, "") ;
}

void data_on_proxy(int from; int to; string data)
{
  text_to_display(display, 0, data, "") ;
}

void socket_connected(int handle; int new; int wherefrom; int port)
{
  int socket ;
  string message ;

  connections=connections+1 ;

  message="Incoming connection from " ;
  string_cat(message, int_to_dotted_ip(wherefrom)) ;
  text_to_display(display, 0, message, "") ;

  start_reverse_lookup(ip, 0) ;
  if (ip)
  {
    text_to_display(display, 0, int_to_dotted_ip(ip), "") ;
    socket=create_socket(ip, connect_port) ;
  }
  else
  {
    text_to_display(display, 0, "Invalid IP", "") ;
  }

  if (socket >= 0)
  {
    message="Outgoing connection successful, commencing proxy (sockets " ;
    string_cat(message, int_to_string(new)) ;
    string_cat(message, " and ") ;
    string_cat(message, int_to_string(socket)) ;
    string_cat(message, ")") ;
    text_to_display(display, 0, message, "") ;
    define_sockets_to_proxy(new, socket, 1) ;
  }
  else
  {
    text_to_display(display, 0, "Outoing connection unsuccessful, closing incoming too", "") ;
    close_socket(new) ;
  }
}

void lookup_complete(int handle; int reason; int addr)
{
  if (reason=1)
  {
    int port ;
    string message ;

    ip=addr ;
    port=1235 ;
    listening_socket=create_listening_socket(port) ;
    if (listening_socket < 0)
    {
      string error ;
      error="Unable to bind port " ;
      string_cat(error, int_to_string(port)) ;
      string_cat(error, ". Is something else using it?") ;
      report_error(error) ;
      quit() ;
    }
    connections=0 ;

    message="proxying connections to " ;
    string_cat(message, destination) ;
    string_cat(message, " on port ") ;
    string_cat(message, int_to_string(connect_port)) ;

    text_to_display(display, 0, message, "") ;
    text_to_display(display, 0, "Now listening for connections", "") ;
  }
  else
  {
    text_to_display(display, 0, "\0300FF00Lookup failed", "") ;
  }
}

int click(int _display; int pane; int icon; int x; int y; int buttons)
{
  string line ;

  line=int_to_string(_display) ;

  string_cat(line, ", ") ;
  string_cat(line, int_to_string(pane)) ;

  string_cat(line, ", ") ;
  string_cat(line, int_to_string(icon)) ;

  string_cat(line, ", ") ;
  string_cat(line, int_to_string(buttons)) ;
  text_to_display(display, 0, line, "") ;
  if (buttons=1)
  {
    hide_display(display) ;
  }
  if (buttons=4)
  {
    raise_display(display) ;
  }
  return 0 ;
}

void main()
{
  int version ;

  version=init("Proxy", "Generic proxy", "Sham Gardner", "0.00 (November 1997)", 0) ;

  display=open_display(80, 20, 50, "Proxy") ;
  pane        =create_pane(display, 1072, 200, 1072, 200, 0, 16*13, 0, 0) ;


  destination="localhost" ;
  connect_port=1234 ;
  start_lookup(destination, 0) ;
  baric=bar_icon(-1, 68, 68, "directory") ;
}
