home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / sniffit.0.3.5 / sn_interface.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  23.1 KB  |  885 lines

  1. /* Sniffit Interface source File                                          */
  2. /*   - by: Brecht Claerhout                                               */
  3.                      
  4. #include "sn_config.h" 
  5.  
  6. #ifdef INCLUDE_INTERFACE
  7. #include <signal.h>
  8. #include <unistd.h>
  9. #include <sys/ipc.h>
  10. #include <sys/shm.h>       
  11. #include "sn_curses.h" 
  12. #include "sn_defines.h" 
  13. #include "sn_structs.h"
  14. #include "sn_interface.h"
  15. #include "sn_generation.h"
  16.  
  17. /*** extern stuff ********/
  18. extern char *SHARED, *connection_data, *timing, *running_connections,
  19.                                                   *logged_connections;
  20. extern int *LISTlength, *DATAlength, memory_id;
  21. extern unsigned int  *TCP_nr_of_packets, *ICMP_nr_of_packets, *UDP_nr_of_packets; unsigned int  *IP_nr_of_packets;
  22. extern unsigned long *TCP_bytes_in_packets, *UDP_bytes_in_packets;  
  23.  
  24. extern struct snif_mask *mask;
  25. extern struct shared_logged_conn *log_conn;
  26. extern FILE *log_dev_stream;
  27. extern struct stat log_dev_stat;
  28.  
  29. extern volatile int LOGGING, screen_busy;
  30. extern char PACKET_INFO;
  31. extern int POINTpos, LISTpos;
  32. extern unsigned char COLOR_AVAIL;           
  33.  
  34. extern WINDOW *menu_window;
  35. extern struct box_window data_box, main_box, mask_box, packets_box;
  36. extern int Pid;                   
  37. extern char *logging_device;
  38.  
  39. /*** Screen Parameters ***/
  40. extern int MASK_WINDOW_ROWS, MASK_WINDOW_COLS, MAIN_WINDOW_ROWS, MAIN_WINDOW_COLS; 
  41. extern int INFO_WINDOW_ROWS, INFO_WINDOW_COLS, DATA_WINDOW_ROWS, DATA_WINDOW_COLS; 
  42. extern int INFO_WINDOW_X, INFO_WINDOW_Y, MASK_WINDOW_X, MASK_WINDOW_Y;
  43. extern int DATA_WINDOW_X, DATA_WINDOW_Y;
  44.  
  45.  
  46. /*** Sreen operations ***/
  47. void init_screen (void)
  48. {
  49. initscr();
  50. cbreak();
  51. noecho();
  52. nonl();
  53. clear();
  54. if(has_colors()==TRUE)    
  55.     {
  56.       COLOR_AVAIL=1;
  57.      start_color();
  58.      init_pair(WIN_COLOR_NORMAL,COLOR_WHITE,COLOR_BLUE);
  59.       init_pair(WIN_COLOR_POINT,COLOR_BLUE,COLOR_CYAN);
  60.       init_pair(WIN_COLOR_DATA,COLOR_BLUE,COLOR_CYAN);
  61.      init_pair(WIN_COLOR_INPUT,COLOR_BLUE,COLOR_CYAN);
  62.       init_pair(WIN_COLOR_MENU,COLOR_BLUE,COLOR_CYAN);
  63.       init_pair(WIN_COLOR_PACKET_INFO,COLOR_BLUE,COLOR_CYAN);
  64.       }
  65. else 
  66.     {
  67.     COLOR_AVAIL=0;
  68.     }
  69. MAIN_WINDOW_ROWS = LINES-5; MAIN_WINDOW_COLS = COLS;
  70.  
  71. MASK_WINDOW_ROWS = 4; MASK_WINDOW_COLS = COLS;
  72. MASK_WINDOW_X = 0; MASK_WINDOW_Y = LINES-5;
  73.  
  74. INFO_WINDOW_ROWS = 8; INFO_WINDOW_COLS = 35;
  75. INFO_WINDOW_X = 3; INFO_WINDOW_Y = MAIN_WINDOW_ROWS-INFO_WINDOW_ROWS-2;
  76.  
  77. DATA_WINDOW_ROWS = (MAIN_WINDOW_ROWS/3)*2; DATA_WINDOW_COLS = (MAIN_WINDOW_COLS/3)*2; 
  78. DATA_WINDOW_X = COLS-DATA_WINDOW_COLS-2; DATA_WINDOW_Y = 3; 
  79. exit_func(screen_exit);
  80. if( (COLS<80)||(LINES<18) )
  81.     exit(0);
  82. };
  83.  
  84. void f_box_window (struct box_window *Win, 
  85.                  int num_lines, int num_cols, int begy,int begx, int col_mode) 
  86. /*  col_mode : color selection   */
  87. {
  88. int i;
  89.  
  90. Win->main_window=newwin(num_lines,num_cols,begy,begx);
  91. Win->work_window=subwin(Win->main_window,num_lines-2,num_cols-2,begy+1,begx+1);
  92. if(COLOR_AVAIL)    
  93.     {
  94.     switch(col_mode)
  95.         {
  96.         case 0:
  97.               wattrset(Win->main_window,COLOR_PAIR(WIN_COLOR_NORMAL));
  98.               wattrset(Win->work_window,COLOR_PAIR(WIN_COLOR_NORMAL));
  99.             break;
  100.         case 1:
  101.              wattrset(Win->main_window,COLOR_PAIR(WIN_COLOR_PACKET_INFO));
  102.               wattrset(Win->work_window,COLOR_PAIR(WIN_COLOR_PACKET_INFO));
  103.             break;
  104.         default:break;
  105.         }
  106.       for(i=0;i<=(num_lines-2);i++)
  107.         {
  108.           wmove(Win->work_window,i,0);
  109.         whline(Win->work_window,' ',num_cols-2);
  110.         }
  111.       }
  112. keypad(Win->work_window,1);
  113. box(Win->main_window,ACS_VLINE,ACS_HLINE);
  114. mvwprintw(Win->main_window,0,3,"Sniffit %s",VERSION);
  115. wmove(Win->work_window,0,0);
  116. wnoutrefresh(Win->main_window);wnoutrefresh(Win->work_window);
  117. doupdate();
  118. }
  119.  
  120. void data_window (struct box_window *Win, struct box_window *P_Win,
  121.                  int num_lines, int num_cols, int begy,int begx,
  122.                  char *buffer, int listitem) 
  123. {
  124. int i=0, j=0;    
  125. struct shared_conn_data *conn;
  126.  
  127. conn = (struct shared_conn_data *) buffer;
  128. while((j<listitem)&&(i<(CONNECTION_CAPACITY+1)))
  129.     {
  130.       if(conn[i].connection[0]!=0)
  131.             j++;
  132.     i++;
  133.     }
  134. while( (conn[i].connection[0]==0)&&(i<(CONNECTION_CAPACITY+1)) )    
  135.     i++;
  136. if(i>=CONNECTION_CAPACITY+1) return;
  137. j=0;
  138.  
  139. Win->main_window=newwin(num_lines,num_cols,begy,begx);
  140. Win->work_window=subwin(Win->main_window,num_lines-5,num_cols-2,begy+1,begx+1);
  141. scrollok(Win->work_window,1);
  142. if(COLOR_AVAIL)    
  143.       wattrset(Win->main_window,COLOR_PAIR(WIN_COLOR_DATA));
  144.  
  145. box(Win->main_window,ACS_VLINE,ACS_HLINE);
  146. wmove(Win->main_window,num_lines-3,1);
  147. whline(Win->main_window,ACS_HLINE,num_cols-2);
  148. wmove(Win->main_window,num_lines-2,1);
  149. whline(Win->main_window,' ',num_cols-2);
  150. wmove(Win->main_window,num_lines-2,2);
  151. waddstr(Win->main_window, conn[i].connection);
  152. strcpy(log_conn->log_enter,conn[i].connection);
  153. wmove(Win->work_window,0,0);
  154. wnoutrefresh(Win->main_window);wnoutrefresh(Win->work_window);
  155. doupdate();
  156. }
  157.  
  158. void data_device (char *buffer, int listitem) 
  159. {
  160. int i=0, j=0;
  161. struct shared_conn_data *conn;
  162.  
  163. conn = (struct shared_conn_data *) buffer;
  164. while((j<listitem)&&(i<(CONNECTION_CAPACITY+1)))
  165.     {
  166.       if(conn[i].connection[0]!=0)
  167.             j++;
  168.     i++;
  169.     }
  170. while( (conn[i].connection[0]==0)&&(i<(CONNECTION_CAPACITY+1)) )    
  171.     i++;
  172. if(i>=CONNECTION_CAPACITY+1) return;
  173. strcpy(log_conn->log_enter, conn[i].connection);
  174. }
  175.  
  176. void mask_status (struct box_window *Work_win)
  177. {
  178. unsigned char *ad;
  179. int i;
  180.  
  181. wmove(Work_win->work_window,0,1);
  182. for(i=0;i<2;i++)
  183.     {wmove(Work_win->work_window,i,0);
  184.     whline(Work_win->work_window,' ',COLS-2);}
  185. wmove(Work_win->work_window,0,1);
  186. wprintw(Work_win->work_window,"Source IP     : ");
  187. ad=&(mask->source_ip);
  188. if(mask->source_ip==0)
  189.      wprintw(Work_win->work_window,"All");
  190. else wprintw(Work_win->work_window,"%u.%u.%u.%u",ad[0],ad[1],ad[2],ad[3]);
  191. wmove(Work_win->work_window,1,1);
  192. wprintw(Work_win->work_window,"Destination IP: ");
  193. ad=&(mask->destination_ip);
  194. if(mask->destination_ip==0)
  195.      wprintw(Work_win->work_window,"All");
  196. else wprintw(Work_win->work_window,"%u.%u.%u.%u",ad[0],ad[1],ad[2],ad[3]);
  197. wmove(Work_win->work_window,0,35);
  198. wprintw(Work_win->work_window,"Source PORT     : ");
  199. if(mask->source_port==0)
  200.       wprintw(Work_win->work_window,"All");
  201. else wprintw(Work_win->work_window,"%u",mask->source_port);
  202. wmove(Work_win->work_window,1,35);
  203. wprintw(Work_win->work_window,"Destination PORT: ");
  204. if(mask->destination_port==0)
  205.      wprintw(Work_win->work_window,"All");
  206. else wprintw(Work_win->work_window,"%u",mask->destination_port);
  207. wnoutrefresh(Work_win->main_window);
  208. wnoutrefresh(Work_win->work_window);
  209. doupdate();
  210. }
  211.  
  212. void fill_box_window (struct box_window *Work_win, char *buffer, 
  213.                       int begin_item, int boxlen, int rowlen)
  214.                                                  /* 0 is the first item  */
  215. {
  216. int i=0, j=0, line=0;
  217. struct shared_conn_data *conn;
  218.  
  219. conn = (struct shared_conn_data *) buffer;
  220. while((j<begin_item)&&(i<(CONNECTION_CAPACITY+1)))
  221.     {
  222.       if(conn[i].connection[0]!=0)
  223.             j++;
  224.     i++;
  225.     }
  226. if(i>=CONNECTION_CAPACITY+1) return;
  227. j=0;
  228.  
  229. while((line<boxlen)&& ((i+j)<CONNECTION_CAPACITY) )    
  230.     {
  231.       if(conn[i+j].connection[0] != 0)    
  232.         {
  233.         wmove(Work_win->work_window,line,0);
  234.         whline(Work_win->work_window,' ',rowlen);
  235.         if(strcmp(log_conn->log_enter,conn[i+j].connection) != 0) 
  236.                 wprintw(Work_win->work_window," %s",conn[i+j].connection);
  237.         else
  238.                 wprintw(Work_win->work_window," %s           *LOGGED*",
  239.                                 conn[i+j].connection);
  240.  
  241.             line++;
  242.             }
  243.       j++;
  244.       }
  245. for(i=line;i<boxlen;i++)    
  246.     {
  247.       wmove(Work_win->work_window,i,0);
  248.     whline(Work_win->work_window,' ',rowlen);
  249.       };
  250. wnoutrefresh(Work_win->work_window);
  251. }
  252.  
  253. void point_item (struct box_window *Work_win, char *buffer, 
  254.                  int item, int begin_item, int boxlen, int rowlen)
  255. {
  256. int i=0, j=0;
  257. struct shared_conn_data *conn;
  258.  
  259.  
  260. if(item<0) return;      /* POINTpos   0 = first item   -1 = no items */
  261.                         /* LISTlength 0 = 1            -1 = no items */
  262.                         /* DANGER - there should always be >=        */
  263.                         /*          connections than 'item'          */
  264. conn = (struct shared_conn_data *) buffer;
  265. while((j<item)&&(i<(CONNECTION_CAPACITY+1)))
  266.     {
  267.     if(conn[i].connection[0] !=0)
  268.             j++;
  269.     i++;
  270.     }
  271. while( (conn[i].connection[0]==0)&&(i<(CONNECTION_CAPACITY+1)) )    
  272.     i++;
  273. if(i>=CONNECTION_CAPACITY+1) return;
  274. j=0;
  275.  
  276. #ifdef DEBUG
  277.         debug_msg(conn[i].connection);
  278. #endif
  279.  
  280. if(COLOR_AVAIL!=0)
  281.     wattrset(Work_win->work_window,COLOR_PAIR(WIN_COLOR_POINT));
  282. else    wattron(Work_win->work_window,A_REVERSE);
  283.  
  284. wmove(Work_win->work_window,item-begin_item,0);
  285. whline(Work_win->work_window,' ',rowlen);
  286.  
  287. if(strcmp(log_conn->log_enter,conn[i].connection)!=0)
  288.     mvwprintw(Work_win->work_window,item-begin_item,0,
  289.                         " %s", conn[i].connection );
  290. else
  291.     mvwprintw(Work_win->work_window,item-begin_item,0,
  292.                             " %s           *LOGGED*",
  293.                                                  conn[i].connection );
  294.   
  295. wnoutrefresh(Work_win->work_window);
  296. if(COLOR_AVAIL!=0)
  297.     wattrset(Work_win->work_window,COLOR_PAIR(WIN_COLOR_NORMAL));
  298. else     wattroff(Work_win->work_window,A_REVERSE); 
  299. }
  300.  
  301. void forced_refresh (void)    
  302. {
  303. #ifdef DEBUG
  304. char debug_line[200];
  305. #endif
  306.  
  307. if((POINTpos<0)&&(*LISTlength>=0)) POINTpos=0;
  308. if((POINTpos>*LISTlength)&&(*LISTlength>=0)) POINTpos=*LISTlength; 
  309. if((POINTpos>*LISTlength)&&(*LISTlength<0)) POINTpos=-1; 
  310.  
  311. while(screen_busy!=0) {};    /* wait till screen operations stop */
  312. #ifdef DEBUG
  313. sprintf(debug_line,"FIX: POINTpos: %d  LISTlength: %d  LISTpos: %d\n",POINTpos,*LISTlength,LISTpos);
  314. debug_msg(debug_line);
  315. #endif
  316.  
  317. fill_box_window(&main_box, running_connections,LISTpos,
  318.                            MAIN_WINDOW_ROWS-2,MAIN_WINDOW_COLS-2);
  319. point_item(&main_box, running_connections, POINTpos,LISTpos,
  320.                            MAIN_WINDOW_ROWS-2,MAIN_WINDOW_COLS-2);
  321. if((LOGGING==1)&&(logging_device==NULL))    
  322.     {
  323.     touchwin(data_box.main_window);touchwin(data_box.work_window);
  324.       wnoutrefresh(data_box.main_window);wnoutrefresh(data_box.work_window);
  325.        }
  326. if(PACKET_INFO==1)    
  327.     {
  328.     touchwin(packets_box.main_window);touchwin(packets_box.work_window);
  329.       wnoutrefresh(packets_box.main_window);
  330.     wnoutrefresh(packets_box.work_window);
  331.     }
  332. doupdate();
  333. }
  334.  
  335. void menu_line (void)
  336. {
  337. int i;
  338.  
  339. if(menu_window==NULL)
  340.       menu_window=newwin (1,COLS,LINES-1,0);
  341. if(COLOR_AVAIL!=0)    
  342.     wattrset(menu_window,COLOR_PAIR(WIN_COLOR_MENU));
  343. else      wattron(menu_window,A_REVERSE);
  344.  
  345. wmove(menu_window,0,0);
  346. whline(menu_window,' ',COLS);
  347. mvwaddstr(menu_window,0,0,MENU);
  348. wnoutrefresh(menu_window);
  349. }
  350.  
  351. char *input_field(char *string, char *input, int flag)
  352. {
  353.     int i;
  354.     char dummy[500];
  355.     WINDOW *Work_txt, *Work_inp;
  356.  
  357. #ifdef DEBUG
  358.         debug_msg("IntAct: Input Field activated");
  359. #endif
  360.         Work_txt=newwin(1,COLS,LINES-1,0);
  361.         Work_inp=newwin(1,50,LINES-1,strlen(string));
  362.     
  363.     if(COLOR_AVAIL!=0)
  364.           {
  365.         wattrset(Work_inp,COLOR_PAIR(WIN_COLOR_INPUT));
  366.           wattrset(Work_txt,COLOR_PAIR(WIN_COLOR_NORMAL));
  367.         }
  368.     whline(Work_txt,' ',COLS);
  369.     whline(Work_inp,' ',50);
  370.     mvwaddstr(Work_txt,0,0,string);
  371.     while(screen_busy!=0) {};   
  372.     wnoutrefresh(Work_txt);wnoutrefresh(Work_inp);
  373.     doupdate();
  374.     echo();mvwgetstr(Work_inp,0,0,dummy);noecho();
  375.     if(input!=NULL)
  376.         {strcpy(input,dummy);}
  377.     delwin(Work_inp);delwin(Work_txt);
  378.     menu_line();
  379.     forced_refresh();   
  380. #ifdef DEBUG
  381.         debug_msg("IntAct: Input Field Ended");
  382. #endif
  383.     return input;
  384. }
  385.  
  386. void exec_mask (void)
  387. {
  388. LISTpos=0;
  389. POINTpos=-1;             /* otherwise we get never ending loop */
  390. clear_shared_mem(1);
  391. mask_status(&mask_box);
  392. if(LOGGING==1) stop_logging();
  393. forced_refresh();
  394. }
  395.  
  396. /* signaling */
  397.  
  398. void sig_blocking(char on_off, int sig)
  399. {
  400. sigset_t set;
  401.  
  402. sigemptyset(&set);sigaddset(&set,sig);
  403. if(on_off==1)
  404.     {sigprocmask(SIG_BLOCK,&set,NULL);}
  405. else    {sigprocmask(SIG_UNBLOCK,&set,NULL);}
  406. }
  407.  
  408. void set_signal (int signum, sig_hand new_action)
  409. {    
  410. struct sigaction new_sigusr;
  411. sigset_t sig_mask;
  412.  
  413. sigemptyset(&sig_mask);
  414. sigaddset(&sig_mask,SIGUSR1);
  415. sigaddset(&sig_mask,SIGALRM);
  416. new_sigusr.sa_handler=new_action;
  417. new_sigusr.sa_mask=sig_mask;    
  418. new_sigusr.sa_flags=0;
  419. sigaction(signum,&new_sigusr,NULL);
  420. }
  421.  
  422. void interaction (int sig)              /* invoked when data arrives */
  423. {
  424. int i;
  425. struct shared_conn_data *conn;
  426.  
  427.  
  428. /* timeout increase */
  429. conn = (struct shared_conn_data *) running_connections;
  430. for(i=0;i<CONNECTION_CAPACITY;i++)
  431.       if(conn[i].connection[0]!=0)
  432.         conn[i].timeout+=1;
  433.  
  434. if((LOGGING==1)&&(log_conn->log_enter[0]==0)) stop_logging();
  435. screen_busy=1;   
  436. if((LOGGING==1)&&(*DATAlength!=0))
  437.       {
  438.     if(logging_device==NULL)
  439.         {
  440.           for(i=0;i<*DATAlength;i++)
  441.                 waddch(data_box.work_window,
  442.                 isprint(*(connection_data+i))?
  443.                     *(connection_data+i):'.');
  444.         }
  445.     else    {
  446.           for(i=0;i<*DATAlength;i++) 
  447.                fputc(*(connection_data+i),log_dev_stream);
  448.         fflush(log_dev_stream);
  449.         }
  450.       *DATAlength=0;
  451.      }
  452. screen_busy=0;
  453. forced_refresh();
  454. set_signal(SIGUSR1,interaction); 
  455. }
  456.  
  457. void packet_info_handler (int signum)
  458. {
  459. #ifdef DEBUG
  460.         debug_msg("ALARM RANG");
  461. #endif
  462. screen_busy=1; 
  463. mvwprintw(packets_box.work_window,0,1,"IP packets/sec.  : % 12u",(*IP_nr_of_packets)/INFO_TIMER);
  464. mvwprintw(packets_box.work_window,1,1,"TCP packets/sec. : % 12u",(*TCP_nr_of_packets)/INFO_TIMER);
  465. mvwprintw(packets_box.work_window,2,1,"ICMP packets/sec.: % 12u",(*ICMP_nr_of_packets)/INFO_TIMER);
  466. mvwprintw(packets_box.work_window,3,1,"UDP packets/sec. : % 12u",(*UDP_nr_of_packets)/INFO_TIMER);
  467. mvwprintw(packets_box.work_window,4,1,"bytes/sec. (TCP) : % 12ld",(*TCP_bytes_in_packets)/INFO_TIMER);
  468. mvwprintw(packets_box.work_window,5,1,"bytes/sec. (UDP) : % 12ld",(*UDP_bytes_in_packets)/INFO_TIMER);
  469. screen_busy=0; 
  470.  
  471. forced_refresh();
  472. /* reinstall handler, reset alarm */
  473. *IP_nr_of_packets=0; 
  474. *TCP_nr_of_packets=*TCP_bytes_in_packets=0; 
  475. *ICMP_nr_of_packets=0;
  476. *UDP_nr_of_packets=*UDP_bytes_in_packets=0; 
  477. set_signal(SIGALRM, packet_info_handler);
  478. alarm(INFO_TIMER);
  479. }
  480.  
  481.  
  482. /* at/on_exit's  */
  483.  
  484. void child_exit (void)
  485. {
  486. kill(Pid,SIGKILL);
  487. };
  488.  
  489. void screen_exit (void)
  490. {
  491. clear();
  492. endwin();
  493. };
  494.  
  495. void mem_exit (void)
  496. {
  497. if(shmctl(memory_id,IPC_RMID,0)<0)
  498.       {perror("Sniffer Hartattack (you are fucked!) ");exit(0);};
  499. }
  500.  
  501. /* Some other stuff */
  502.  
  503. void stop_logging (void)
  504. {    
  505. LOGGING=0;
  506. log_conn->log_enter[0]=0;
  507. if(logging_device==NULL)
  508.     {delwin(data_box.work_window); delwin(data_box.main_window);}
  509. forced_refresh();
  510. }
  511.  
  512. void stop_packet_info (void)
  513. {    
  514. PACKET_INFO=0;
  515. alarm(0);
  516. delwin(packets_box.work_window), delwin(packets_box.main_window);
  517. forced_refresh();
  518. }
  519.  
  520. int add_itemlist(char *buffer, char *string)
  521. {
  522. int i, to_help, to_item;
  523. struct shared_conn_data *conn;
  524.  
  525. /*invoked every time a packet comes in */
  526.  
  527. conn = (struct shared_conn_data *) buffer;
  528. for(i=0;i<CONNECTION_CAPACITY;i++)
  529.     if(strcmp( conn[i].connection, string)==0)
  530.         {
  531.         conn[i].timeout=0;
  532.         return -1;
  533.         }
  534. for(i=0;i<CONNECTION_CAPACITY;i++)
  535.     if(conn[i].connection[0]==0)
  536.             {
  537.         strcpy(conn[i].connection, string);
  538.         conn[i].timeout=0;
  539.             (*LISTlength)++;
  540.         return i;
  541.             }
  542.  
  543. /* everything full - timeout procedure  */
  544. to_help=to_item=0;
  545. for(i=0;i<CONNECTION_CAPACITY;i++)
  546.     if(conn[i].timeout>to_help)
  547.         if(strcmp(conn[i].connection,log_conn->log_enter)!=0)
  548.             {
  549.             to_help=conn[i].timeout;
  550.             to_item=i;
  551.             }
  552. strcpy(conn[to_item].connection, string);
  553. conn[to_item].timeout=0;
  554. return to_item;
  555. }
  556.  
  557. int del_itemlist(char *buffer, char *string)
  558. {
  559. int i;
  560. struct shared_conn_data *conn;
  561.  
  562. conn = (struct shared_conn_data *) buffer;
  563. for(i=0;i<CONNECTION_CAPACITY;i++)
  564.     if(strcmp( conn[i].connection, string)==0)
  565.             {
  566.         conn[i].connection[0]=0;
  567.         conn[i].timeout=0;
  568.             (*LISTlength)--;
  569.             if(strcmp( log_conn->log_enter, string)==0) 
  570.             {log_conn->log_enter[0]=0;}
  571.         return i;
  572.             }
  573. return -1;
  574. }
  575.  
  576. void clear_shared_mem(char mode) 
  577.             /* mode = 0    all               */
  578.             /* mode = 1    keep mask         */
  579.             /*             keep packet count */
  580. {
  581. int i;
  582. struct shared_conn_data *conn;
  583.  
  584. *timing=1;
  585. *DATAlength=0;
  586. *LISTlength=-1;
  587. if(mode==0)
  588.   {
  589.   mask->source_ip=mask->destination_ip=mask->source_port=mask->destination_port=0;
  590.   *IP_nr_of_packets=0; 
  591.   *TCP_nr_of_packets=*TCP_bytes_in_packets=0; 
  592.   *ICMP_nr_of_packets=0;
  593.   *UDP_nr_of_packets=*UDP_bytes_in_packets=0; 
  594.   }
  595.  
  596. log_conn->log_enter[0]=0;
  597. conn = (struct shared_conn_data *) running_connections;
  598. for(i=0;i<CONNECTION_CAPACITY;i++)
  599.     {
  600.     conn[i].connection[0]=0;
  601.     conn[i].timeout=0;
  602.     } 
  603. *timing=0;
  604. };
  605.  
  606. void create_arguments(char *esource, char *es_port, char *edest, 
  607.                         char *ed_port, char *buffer, int item)
  608. {
  609. char e_dummy[CONN_NAMELEN];
  610. int i=0, j=0;
  611. struct shared_conn_data *conn;
  612.  
  613. if(item<0) return;    
  614. conn = (struct shared_conn_data *) buffer;
  615. while((j<item)&&(i<(CONNECTION_CAPACITY+1)))
  616.     {
  617.       if(conn[i].connection[0] !=0)
  618.                 j++;
  619.     i++;
  620.     }
  621. while( (conn[i].connection[0]==0)&&(i<(CONNECTION_CAPACITY+1)) )    
  622.     i++;
  623. if(i>=CONNECTION_CAPACITY+1) return;
  624. j=0;
  625.  
  626. strcpy(e_dummy,conn[i].connection);
  627. /* OLD STUFF -- Previous line format */
  628. /*
  629. strtok(e_dummy," ");
  630. strcpy(esource,strtok(NULL," "));
  631. strcpy(es_port,strtok(NULL," "));
  632. strtok(NULL," ");
  633. strcpy(edest,strtok(NULL," "));
  634. strcpy(ed_port,strtok(NULL," "));
  635. */
  636.  
  637. strcpy(esource,strtok(e_dummy," "));
  638. strcpy(es_port,strtok(NULL," "));
  639. strtok(NULL," ");
  640. strcpy(edest,strtok(NULL," "));
  641. strcpy(ed_port,strtok(NULL," "));
  642. #ifdef DEBUG
  643.     debug_msg(esource);
  644.     debug_msg(es_port);
  645.     debug_msg(edest);
  646.     debug_msg(ed_port);
  647. #endif
  648. }
  649.  
  650. /*** Main interface program */
  651.  
  652. void run_interface(void)
  653. {
  654. int i,key_hit;
  655. char dummy[100];
  656. char exec_s[20],exec_sp[20],exec_d[20],exec_dp[20];  
  657. struct generate_mask generate;    
  658.  
  659. POINTpos=-1;
  660. *LISTlength=-1;
  661. LISTpos=0;
  662. LOGGING=0;
  663. PACKET_INFO=0;
  664. screen_busy=0;
  665.  
  666.  
  667. set_signal (SIGCHLD, SIG_IGN); 
  668. set_signal(SIGUSR1,interaction);
  669.  
  670. init_screen();                /* The whole screen setup */
  671. f_box_window(&mask_box,MASK_WINDOW_ROWS,MASK_WINDOW_COLS,MASK_WINDOW_Y,MASK_WINDOW_X,0);
  672. mask_status(&mask_box);
  673. f_box_window(&main_box,MAIN_WINDOW_ROWS,MAIN_WINDOW_COLS,0,0,0);
  674. fill_box_window(&main_box, running_connections,LISTpos,
  675.                            MAIN_WINDOW_ROWS-2,MAIN_WINDOW_COLS-2);
  676. point_item(&main_box, running_connections,POINTpos,LISTpos,
  677.                            MAIN_WINDOW_ROWS-2,MAIN_WINDOW_COLS-2);
  678. menu_window=NULL;
  679. menu_line();
  680. doupdate();                      /* And..... draw it! */
  681.  
  682. while(1)
  683.       {
  684.      key_hit=wgetch(main_box.work_window);
  685. #ifdef DEBUG
  686.     debug_msg("IntAct: Key Hit Received");
  687. #endif
  688.     sig_blocking(1, SIGALRM);
  689.     sig_blocking(1, SIGUSR1);
  690.      switch(key_hit)
  691.             {
  692.             case KEY_DOWN:
  693.         case 'J':
  694.         case 'j':
  695.                    if(POINTpos>=*LISTlength) break;
  696.                    if( POINTpos<(LISTpos+(MAIN_WINDOW_ROWS-3)) )
  697.                      POINTpos++;
  698.                   else
  699.                     {if(LISTpos>=*LISTlength) break; 
  700.                 LISTpos++; POINTpos++;};
  701.             forced_refresh();
  702.                    break;
  703.            case KEY_UP:
  704.         case 'K':
  705.         case 'k':
  706.                   if(POINTpos==0) break;
  707.                    if(POINTpos>LISTpos)
  708.                      POINTpos--;
  709.                    else
  710.                      {if(LISTpos==0) break; 
  711.                 LISTpos--; POINTpos--;};
  712.             forced_refresh();
  713.                    break;
  714.             case ENTER:
  715.                    if(*LISTlength<0) break; 
  716.                    if(LOGGING==0)
  717.                     {
  718.                      if(logging_device==NULL)
  719.                 data_window(&data_box,&main_box,DATA_WINDOW_ROWS,
  720.                         DATA_WINDOW_COLS,DATA_WINDOW_Y, DATA_WINDOW_X,
  721.                         running_connections,POINTpos);
  722.                 else 
  723.                   data_device(running_connections,POINTpos);
  724.                      
  725.                 LOGGING=1;
  726.                      }
  727.                    else
  728.                      {
  729.                      stop_logging();
  730.                     if(logging_device==NULL)
  731.                         data_window(&data_box,&main_box,DATA_WINDOW_ROWS,
  732.                         DATA_WINDOW_COLS,DATA_WINDOW_Y, DATA_WINDOW_X,
  733.                         running_connections,POINTpos);
  734.                 else 
  735.                          data_device(running_connections,POINTpos);
  736.                 LOGGING=1;
  737.                      };
  738.                    break;
  739.             case 'N':
  740.             case 'n':
  741.                    if(PACKET_INFO==0)
  742.                 {
  743.                 f_box_window(&packets_box,INFO_WINDOW_ROWS,INFO_WINDOW_COLS,
  744.                             INFO_WINDOW_Y, INFO_WINDOW_X ,1);
  745.                 PACKET_INFO=1;
  746.                 *IP_nr_of_packets=0; 
  747.                 *TCP_nr_of_packets=*TCP_bytes_in_packets=0; 
  748.                 *ICMP_nr_of_packets=0;
  749.                 *UDP_nr_of_packets=*UDP_bytes_in_packets=0; 
  750.                 packet_info_handler(SIGALRM);
  751.                 }
  752.             else
  753.                 {stop_packet_info();}
  754.                    break;
  755. #ifdef GENERATION
  756.         case 'g':
  757.         case 'G':
  758.             input_field("Source IP for PKT Generation: ",dummy,1);
  759.             generate.source_ip=getaddrbyname(dummy); 
  760.             input_field("Source Port: ",dummy,1);
  761.             generate.source_port=atoi(dummy);
  762.  
  763.             input_field("Dest IP for PKT Generation: ",dummy,1);
  764.             generate.dest_ip=getaddrbyname(dummy); 
  765.             input_field("Dest Port: ",dummy,1);
  766.             generate.dest_port=atoi(dummy); 
  767.  
  768.             input_field("Number of PKTs: ",dummy,1);
  769.             generate.pkt_no=atoi(dummy); 
  770.             exec_generate(&generate); 
  771.             break;
  772. #endif    
  773.         case 'q':
  774.         case 'Q':
  775.             case KEY_F(10):
  776.                     if(LOGGING==1)
  777.                       {stop_logging();}
  778.                     else
  779.                 {kill(Pid,SIGKILL);exit(0);}
  780.             break;
  781.         case '1':
  782.         case KEY_F(1):
  783.                    input_field("Source Ip: ",dummy,0); 
  784.             mask->source_ip=getaddrbyname(dummy);
  785.             exec_mask();
  786.             break;
  787.         case '2':
  788.             case KEY_F(2):
  789.                    input_field("Destination Ip: ",dummy,0);
  790.             mask->destination_ip=getaddrbyname(dummy);
  791.             exec_mask();
  792.                    break;
  793.         case '3':
  794.                case KEY_F(3):
  795.                    input_field("Source Port: ",dummy,0);
  796.                         mask->source_port=atoi(dummy);
  797.             exec_mask();          
  798.              break;
  799.         case '4':
  800.             case KEY_F(4):
  801.                    input_field("Destination Port: ",dummy,0);
  802.                         mask->destination_port=atoi(dummy);
  803.             exec_mask();
  804.                    break;
  805.  
  806.         case '5':
  807.             case KEY_F(5):
  808.                    if(*LISTlength<0) break;        
  809.             if(access("./sniffit_key5", X_OK)<0) break;
  810.             create_arguments(exec_s,exec_sp,exec_d,
  811.                     exec_dp,running_connections,POINTpos);
  812.             if(fork()==0)     /* CHILD */
  813.                {
  814.                close(0); close(1); close(2);
  815.                 sig_blocking(0, SIGALRM);
  816.               sig_blocking(0, SIGUSR1);
  817.                set_signal(SIGALRM,SIG_DFL); 
  818.                set_signal(SIGUSR1,SIG_DFL); 
  819.               execl("./sniffit_key5","sniffit_key5",exec_s,exec_sp,exec_d,exec_dp,NULL);
  820.                   exit(0);
  821.                };            
  822.             break;
  823.         case '6':
  824.             case KEY_F(6):
  825.                    if(*LISTlength<0) break;        
  826.             if(access("./sniffit_key6", X_OK)<0) break;
  827.             create_arguments(exec_s,exec_sp,exec_d,
  828.                     exec_dp,running_connections,POINTpos);
  829.             if(fork()==0)     /* CHILD */
  830.                {
  831.                close(0); close(1); close(2);
  832.                 sig_blocking(0, SIGALRM);
  833.               sig_blocking(0, SIGUSR1);
  834.                set_signal(SIGALRM,SIG_DFL); 
  835.                set_signal(SIGUSR1,SIG_DFL); 
  836.                execl("./sniffit_key6","sniffit_key6",exec_s,exec_sp,exec_d,exec_dp,NULL);
  837.                   exit(0);
  838.                };    
  839.             break;
  840.         case '7':
  841.             case KEY_F(7):
  842.                    if(*LISTlength<0) break;        
  843.             if(access("./sniffit_key7", X_OK)<0) break;
  844.             create_arguments(exec_s,exec_sp,exec_d,
  845.                     exec_dp,running_connections,POINTpos);
  846.             if(fork()==0)     /* CHILD */
  847.                {
  848.                 sig_blocking(0, SIGALRM);
  849.               sig_blocking(0, SIGUSR1);
  850.                set_signal(SIGALRM,SIG_DFL); 
  851.                set_signal(SIGUSR1,SIG_DFL); 
  852.                close(0); close(1); close(2);
  853.                execl("./sniffit_key7","sniffit_key7",exec_s,exec_sp,exec_d,exec_dp,NULL);
  854.                   exit(0);
  855.                };            
  856.             break;
  857.         case '8':
  858.             case KEY_F(8):
  859.                    if(*LISTlength<0) break;        
  860.             if(access("./sniffit_key8", X_OK)<0) break;
  861.             create_arguments(exec_s,exec_sp,exec_d,
  862.                     exec_dp,running_connections,POINTpos);
  863.             if(fork()==0)     /* CHILD */
  864.                {
  865.                 sig_blocking(0, SIGALRM);
  866.               sig_blocking(0, SIGUSR1);
  867.                set_signal(SIGALRM,SIG_DFL); 
  868.                set_signal(SIGUSR1,SIG_DFL); 
  869.                close(0); close(1); close(2);
  870.                execl("./sniffit_key8","sniffit_key8",exec_s,exec_sp,exec_d,exec_dp,NULL);
  871.                   exit(0);
  872.                };            
  873.             break;
  874.         case 'r':
  875.         case 'R':           /* mask does an auto reset */
  876.             exec_mask();
  877.             break;
  878.             default: break;
  879.             }
  880.     sig_blocking(0, SIGALRM);
  881.     sig_blocking(0, SIGUSR1);
  882.       }
  883. };
  884. #endif
  885.