#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include "sigtypes.h"
#include "user_lib.h"


////////////////////////////////////////////////////////////////////
//$ ssh -l vpn -L 10022:192.101.77.209:22 wormhole
//
//Then, in another terminal on the internal linux box:
//
//$ ssh -p 10022 -l wes01 -L 12345:127.0.0.1:10095 127.0.0.1
//
//That will give you a local port of 12345 for data.  To test:
//
//Run this program instead of telnet
//$ telnet 127.0.0.1 12345
////////////////////////////////////////////////////////////////////

static int isocket;
static void SigioHandler( int signal_a );
int main( int iargc_a, char* argv_a[] )
{
  UINT4 iconnect_stat;

  isocket = SocketConnectNoBlock(AF_INET, "127.0.0.1", 12345, SigioHandler, &iconnect_stat);

  printf("CONNECT STAT %d Port %d Socket %d\n", iconnect_stat, 12345, isocket);
  while(true)
  {
    sleep(5);
  }
}

static void SigioHandler( int signal_a )
{
  char data[8000];
  int ierror, isize;

  isize = recv( isocket, data, 7999, 0 );

  if (isize > 0)
  {
    /* We got some valid data here.  Add terminating NULL, then process */
    data[isize] = 0;
    printf("%s", data);
  }
  else if (isize < 0)
  {
    /* We an error here, signal all errors except EWOULDBLOCK */
    ierror = errno;
    if (ierror != EWOULDBLOCK)
        printf("Link Broken\n");
  }
  else
     printf("Link Broken\n");
}



