~ubuntu-branches/ubuntu/hardy/nast/hardy

« back to all changes in this revision

Viewing changes to ncurses/n_rst.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2004-02-17 22:14:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040217221421-f1h39tzviblbp2lh
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    nast
 
3
 
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 
 
18
*/
 
19
 
 
20
#include "n_nast.h"
 
21
 
 
22
#ifdef HAVE_LIBNCURSES
 
23
 
 
24
# define Rst 0
 
25
# define Fin 1
 
26
# define Syn 2
 
27
 
 
28
int r_add(u_long ip_src,u_long ip_dst,u_short sport,u_short dport, u_long seq, u_long ack, int flag);
 
29
int r_del(u_long ip_src,u_long ip_dst,u_short sport,u_short dport, int flag);
 
30
 
 
31
int r_nconn = 0;
 
32
int r_lines = 1;
 
33
 
 
34
int app = 0;
 
35
 
 
36
int rst_connection_db(char *dev,u_long ip_src,u_long ip_dst,u_short sport,u_short dport)
 
37
{
 
38
   char errbuf[256];
 
39
   struct libnet_ipv4_hdr *ip;
 
40
   struct libnet_tcp_hdr *tcp;
 
41
   pcap_t* descr;
 
42
   int k;
 
43
   
 
44
   logd = stdout;
 
45
   nmax = 2;
 
46
 
 
47
   if((descr=pcap_open_live(dev,BUFSIZ,1,0,errbuf)) == NULL)
 
48
     {
 
49
        w_error(1, "pcap_open_live: %s", errbuf);
 
50
     }
 
51
 
 
52
   offset=(device(dev,descr));
 
53
 
 
54
   for(k=0;k<30;k++)
 
55
     {
 
56
        memset(&c_inf[k], 0, sizeof(c_inf[k]));
 
57
     }
 
58
 
 
59
   init_scr();
 
60
 
 
61
   mvwprintw(winfo->win,0,2,"Source");
 
62
   mvwprintw(winfo->win,0,21,"Port");
 
63
   mvwprintw(winfo->win,0,34,"Destination");
 
64
   mvwprintw(winfo->win,0,55,"Port");
 
65
   mvwprintw(winfo->win,0,66,"State");
 
66
   SAFE_SCROLL_REFRESH(winfo);
 
67
 
 
68
   while(rst_glob!=0)
 
69
     {
 
70
 
 
71
        if ((packet = (u_char *) pcap_next (descr, &hdr))!=NULL)
 
72
          {
 
73
 
 
74
             ip = (struct libnet_ipv4_hdr *) (packet + offset);
 
75
             tcp = (struct libnet_tcp_hdr *) (packet + offset + LIBNET_IPV4_H);
 
76
 
 
77
             if (ip->ip_p == IPPROTO_TCP)
 
78
               {
 
79
                  if (!sport && !dport)
 
80
                    {
 
81
                       if ( ip->ip_src.s_addr == ip_src && ip->ip_dst.s_addr == ip_dst)
 
82
                         {
 
83
                            switch(tcp->th_flags)
 
84
                              {
 
85
                               case TH_SYN:
 
86
                                 r_add(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),Syn);
 
87
                                 break;
 
88
                               case (TH_SYN|TH_ACK):
 
89
                                 r_add(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
90
                                 break;
 
91
                               case TH_ACK:
 
92
                                 r_add(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
93
                                 break;
 
94
                               case TH_RST:
 
95
                                 r_del(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),Rst);
 
96
                                 break;
 
97
                               case (TH_ACK|TH_PUSH):
 
98
                                 r_add(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
99
                                 break;
 
100
                               case (TH_URG|TH_ACK):
 
101
                                 r_add(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
102
                                 break;
 
103
                               case (TH_FIN|TH_ACK):
 
104
                                 r_del(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),Fin);
 
105
                               case (TH_RST|TH_ACK):
 
106
                                 r_del(ip_src,ip_dst,htons(tcp->th_sport),htons(tcp->th_dport),Rst);
 
107
                                 break;
 
108
 
 
109
                               default:
 
110
                                 break;
 
111
                              }
 
112
                         }
 
113
                        /*caso rovescio */
 
114
                       else if ( ip->ip_src.s_addr == ip_dst && ip->ip_dst.s_addr == ip_src )
 
115
                         {
 
116
                            switch(tcp->th_flags)
 
117
                              {
 
118
                               case TH_SYN:
 
119
                                 r_add(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),Syn);
 
120
                                 break;
 
121
                               case (TH_SYN|TH_ACK):
 
122
                                 r_add(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
123
                                 break;
 
124
                               case TH_ACK:
 
125
                                 r_add(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
126
                                 break;
 
127
                               case TH_RST:
 
128
                                 r_del(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),Rst);
 
129
                                 break;
 
130
                               case (TH_ACK|TH_PUSH):
 
131
                                 r_add(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
132
                                 break;
 
133
                               case (TH_URG|TH_ACK):
 
134
                                 r_add(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),htonl (tcp->th_seq), htonl (tcp->th_ack),0);
 
135
                                 break;
 
136
                               case (TH_FIN|TH_ACK):
 
137
                                 r_del(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),Fin);
 
138
                               case (TH_RST|TH_ACK):
 
139
                                 r_del(ip_dst,ip_src,htons(tcp->th_sport),htons(tcp->th_dport),Rst);
 
140
                                 break;
 
141
 
 
142
                               default:
 
143
                                 break;
 
144
                              }
 
145
                         }
 
146
                    }
 
147
               }
 
148
 
 
149
          }
 
150
 
 
151
     }
 
152
 
 
153
   pcap_close(descr);
 
154
 
 
155
   return 0;
 
156
}
 
157
 
 
158
int r_add(u_long ip_src,u_long ip_dst,u_short sport,u_short dport, u_long seq, u_long ack, int flag)
 
159
{
 
160
   int i;
 
161
   for(i=0;i<30;i++)
 
162
     if((ip_src==c_inf[i].s_ip && ip_dst==c_inf[i].d_ip && sport==c_inf[i].s_port && dport==c_inf[i].d_port && c_inf[i].set) || (ip_src==c_inf[i].d_ip && ip_dst==c_inf[i].s_ip && sport==c_inf[i].d_port && dport==c_inf[i].s_port && c_inf[i].set))
 
163
       {
 
164
          c_inf[i].seq=seq;
 
165
          c_inf[i].ack=ack;
 
166
          sf[c_inf[i].pr].seq=seq;
 
167
          sf[c_inf[i].pr].ack=ack;
 
168
          return(0);
 
169
       }
 
170
 
 
171
   for(i=0;i<30;i++)/*cerco spazio vuoto*/
 
172
     {
 
173
        if(c_inf[i].s_ip)
 
174
          continue;
 
175
        else
 
176
          if(flag!=Syn)
 
177
            return(0);
 
178
        else
 
179
          {
 
180
             c_inf[i].s_ip = ip_src;
 
181
             c_inf[i].d_ip = ip_dst;
 
182
             c_inf[i].s_port = sport;
 
183
             c_inf[i].d_port = dport;
 
184
             c_inf[i].seq=seq;
 
185
             c_inf[i].ack=ack;
 
186
             c_inf[i].pr=app;
 
187
 
 
188
             mvwprintw(winfo->win,r_lines,2,"%s",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE));
 
189
             mvwprintw(winfo->win,r_lines,21,"%d",c_inf[i].s_port);
 
190
             mvwprintw(winfo->win,r_lines,34,"%s",libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE));
 
191
             mvwprintw(winfo->win,r_lines,55,"%d",c_inf[i].d_port);
 
192
             mvwprintw(winfo->win,r_lines,66,"Work");
 
193
             c_inf[i].lin=r_lines;
 
194
             SAFE_SCROLL_REFRESH(winfo);
 
195
 
 
196
             sprintf(sf[app].string,"%2s%12d%20s%14d         Work",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
197
             /* create the filter for tcp stream*/
 
198
             sprintf(sf[app].sfilter,"host %s and port %d and host %s and port %d",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
199
             sf[app].seq=c_inf[i].seq;
 
200
             sf[app].ack=c_inf[i].ack;
 
201
             sf[app].ip_src=c_inf[i].s_ip;
 
202
             sf[app].ip_dst=c_inf[i].d_ip;
 
203
             sf[app].s_port=c_inf[i].s_port;
 
204
             sf[app].d_port=c_inf[i].d_port;
 
205
 
 
206
             sf[i].cont=app;
 
207
             c_inf[i].set=1;
 
208
             r_nconn++;
 
209
             r_lines++;
 
210
             app++;
 
211
             ++nmax;
 
212
             return(1);
 
213
          }
 
214
     }
 
215
   return(0);
 
216
}
 
217
 
 
218
int r_del(u_long ip_src,u_long ip_dst,u_short sport,u_short dport, int flag)
 
219
{
 
220
   int i;
 
221
 
 
222
   for(i=0;i<30;i++)
 
223
     {
 
224
        if(ip_src==c_inf[i].s_ip && ip_dst==c_inf[i].d_ip && sport==c_inf[i].s_port && dport==c_inf[i].d_port)
 
225
          { if(flag){
 
226
             mvwprintw(winfo->win,c_inf[i].lin,2,"%s",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE));
 
227
             mvwprintw(winfo->win,c_inf[i].lin,21,"%d",c_inf[i].s_port);
 
228
             mvwprintw(winfo->win,c_inf[i].lin,34,"%s",libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE));
 
229
             mvwprintw(winfo->win,c_inf[i].lin,55,"%d",c_inf[i].d_port);
 
230
             mvwprintw(winfo->win,c_inf[i].lin,66,"Closed");
 
231
 
 
232
             sprintf(sf[sf[i].cont].string,"%2s%12d%20s%14d         Closed",
 
233
                     libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
234
          }
 
235
             else
 
236
               {
 
237
                  mvwprintw(winfo->win,c_inf[i].lin,2,"%s",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE));
 
238
                  mvwprintw(winfo->win,c_inf[i].lin,21,"%d",c_inf[i].s_port);
 
239
                  mvwprintw(winfo->win,c_inf[i].lin,34,"%s",libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE));
 
240
                  mvwprintw(winfo->win,c_inf[i].lin,55,"%d",c_inf[i].d_port);
 
241
                  mvwprintw(winfo->win,c_inf[i].lin,66,"Resetted");
 
242
 
 
243
                  sprintf(sf[sf[i].cont].string,    "%2s%12d%20s%13d          Resetted",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
244
               }
 
245
             sprintf(sf[sf[i].cont].sfilter,"host %s and port %d and host %s and port %d",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
246
             SAFE_SCROLL_REFRESH(winfo);
 
247
             //wrefresh(winfo->win);
 
248
             memset(&c_inf[i], 0, sizeof(c_inf[i]));
 
249
             r_nconn--;
 
250
 
 
251
          }
 
252
        else if(ip_src==c_inf[i].d_ip && ip_dst==c_inf[i].s_ip && sport==c_inf[i].d_port && dport==c_inf[i].s_port)
 
253
          { if(flag){
 
254
             mvwprintw(winfo->win,c_inf[i].lin,2,"%s",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE));
 
255
             mvwprintw(winfo->win,c_inf[i].lin,21,"%d",c_inf[i].s_port);
 
256
             mvwprintw(winfo->win,c_inf[i].lin,34,"%s",libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE));
 
257
             mvwprintw(winfo->win,c_inf[i].lin,55,"%d",c_inf[i].d_port);
 
258
             mvwprintw(winfo->win,c_inf[i].lin,66,"Closed");
 
259
 
 
260
             sprintf(sf[sf[i].cont].string,      "%2s%12d%20s%14d         Closed",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
261
          }
 
262
             else
 
263
               {
 
264
                  mvwprintw(winfo->win,c_inf[i].lin,2,"%s",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE));
 
265
                  mvwprintw(winfo->win,c_inf[i].lin,21,"%d",c_inf[i].s_port);
 
266
                  mvwprintw(winfo->win,c_inf[i].lin,34,"%s",libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE));
 
267
                  mvwprintw(winfo->win,c_inf[i].lin,55,"%d",c_inf[i].d_port);
 
268
                  mvwprintw(winfo->win,c_inf[i].lin,66,"Resetted");
 
269
 
 
270
                  sprintf(sf[sf[i].cont].string,      "%2s%12d%20s%13d         Resetted",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
271
               }
 
272
             sprintf(sf[sf[i].cont].sfilter,"host %s and port %d and host %s and port %d",libnet_addr2name4(c_inf[i].s_ip, LIBNET_DONT_RESOLVE),c_inf[i].s_port,libnet_addr2name4(c_inf[i].d_ip, LIBNET_DONT_RESOLVE),c_inf[i].d_port);
 
273
             SAFE_SCROLL_REFRESH(winfo);
 
274
             memset(&c_inf[i], 0, sizeof(c_inf[i]));
 
275
             r_nconn--;
 
276
 
 
277
          }
 
278
 
 
279
     }
 
280
   return -1;
 
281
}
 
282
 
 
283
int reset_conn(char *dev,u_long s_ip, u_long d_ip, u_short s_port, u_short d_port,u_long seq, u_long ack)
 
284
{
 
285
 
 
286
   char errbuf[256];
 
287
 
 
288
   libnet_t *l;
 
289
   u_short n;
 
290
 
 
291
   n_print("princ",3,1,0,"- Stoled SEQ (%lu) ACK (%lu)...\n", seq, ack);
 
292
 
 
293
   if ((l = libnet_init (LIBNET_RAW4, NULL, errbuf))==NULL)
 
294
     {
 
295
        w_error(1, "libnet_init: %s\n", errbuf);
 
296
     }
 
297
 
 
298
   if (libnet_build_tcp (s_port, d_port, seq, ack, TH_RST, 32767, 0, 0, LIBNET_TCP_H, NULL, 0, l, 0)==-1)
 
299
     {
 
300
        libnet_destroy (l);
 
301
        w_error(1, "Error building tcp header : %s\n" ,libnet_geterror(l));
 
302
     }
 
303
 
 
304
   if (libnet_build_ipv4 (LIBNET_TCP_H + LIBNET_IPV4_H, 0x08, 35320, 0, 64, IPPROTO_TCP, 0,s_ip , d_ip , NULL, 0, l, 0)==-1)
 
305
     {
 
306
        libnet_destroy (l);
 
307
        w_error(1, "Error building ip header : %s\n", libnet_geterror(l));
 
308
     }
 
309
 
 
310
   for (n = 0; n < 2 ; n++)
 
311
     if (libnet_write (l) == -1)
 
312
       {
 
313
          libnet_destroy(l);
 
314
          w_error(1, "Error writing packet on wire : %s\n", libnet_geterror(l));
 
315
       }
 
316
       
 
317
   n_print("princ",5,1,0,"- Creating and sending the packet...");
 
318
 
 
319
   libnet_destroy(l);
 
320
   wattron(princ->win,A_BOLD);
 
321
   n_print("princ",7,1,0,"- Connection has been resetted!!\n\n");
 
322
      wattroff(princ->win,A_BOLD);
 
323
 
 
324
   redrawscrollwin(princ,0);
 
325
   return (0);
 
326
}
 
327
 
 
328
#endif
 
329