#include     "./include/iptrans.h"



extern void iptrans_timer();

/*@ignore@*/
void init_timer_interrupt(void)
{
  struct sigaction newact;
  newact.sa_handler =iptrans_timer;
  newact.sa_flags =0;
  sigemptyset(&newact.sa_mask);
  sigaction(SIGALRM,&newact,NULL);
}

void setup_interval_timer(void)
{
  struct itimerval value;
  value.it_interval.tv_sec =0;
  value.it_interval.tv_usec =10*1000;
  value.it_value.tv_sec = 0;
  value.it_value.tv_usec = 10*1000;
  setitimer(ITIMER_REAL,&value,NULL);
}

void init_timer()
{
  init_timer_interrupt();
  setup_interval_timer();
}

void interrupt_handler(int signo)
{
  CloseTcpConn();
}

int initSystem()
{
   struct sigaction act;
   
   //close(0);
   //close(1);
   //close(2);
   act.sa_handler = interrupt_handler;
   sigemptyset(&act.sa_mask);
   act.sa_flags = 0;
   if(sigaction(SIGPIPE,&act,NULL)<0)
    fprintf(stderr,"set signal failed\n");

   system("sysctl -w net.ipv4.tcp_keepalive_time=20");
   system("sysctl -w net.ipv4.tcp_keepalive_probes=2");

   return 1;
}

int
main(int argc, char *argv[])
{

int                 err_flag = FALSE;
int                 test_mode = FALSE;
int 			ret;
    SetDebugFlag();

    /* 
    ** Get option from command line.
    ** option 'd' is enable to run program with daemon mode.
    */
    while ((ret = getopt(argc, argv, "dt")) != -1)
    switch (ret)
    {
        case 'd':
            /* Set daemon mode and open a syslog */
            DaemonInit("IPTRANS", LOG_USER);
            break;
        case 't':
            test_mode = TRUE;   /* test mode: bind port 4950 to recvfrom */
            break;
        case '?':
            err_flag = TRUE;
    }
    if (err_flag) {
        log_msg("Usage: %s -d -t \n", argv[0]);
        log_msg("    -d  with daemon mode");
        log_msg("    -t  with test mode");
        exit(1);
    }
    
    debug_init();
    initSystem(); 
    iptrMainInit();
    init_timer();

    while(1){
     usleep(1);
    }
    exit(0);
}
/*@end@*/
