Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/act365/net/sniffer/Sniffer.java


1   /*
2     * JSocket Wrench
3     * 
4     * Copyright (C) act365.com October 2003
5     * 
6     * Web site: http://www.act365.com/wrench
7     * E-mail: developers@act365.com
8     * 
9     * The JSocket Wrench library adds support for low-level Internet protocols
10    * to the Java programming language.
11    * 
12    * This program is free software; you can redistribute it and/or modify it 
13    * under the terms of the GNU General Public License as published by the Free 
14    * Software Foundation; either version 2 of the License, or (at your option) 
15    * any later version.
16    *  
17    * This program is distributed in the hope that it will be useful, 
18    * but WITHOUT ANY WARRANTY; without even the implied warranty of 
19    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
20    * Public License for more details.
21    * 
22    * You should have received a copy of the GNU General Public License along with 
23    * this program; if not, write to the Free Software Foundation, Inc., 
24    * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25    */
26  
27  package com.act365.net.sniffer;
28  
29  import com.act365.net.*;
30  import com.act365.net.ip.*;
31  import com.act365.net.udp.*;
32  import com.act365.net.tcp.*;
33  
34  import java.net.*;
35  
36  /**
37   * Listens for received packets that implement a given protocol and writes
38   * them to the screen.
39   */
40  
41  public class Sniffer {
42  
43    /**
44     * Usage: <code>Sniffer -p protocol</code>
45     * <p><code>protocol</code> is an optional argument. When selected, only
46     * packets with the given protocol will be displayed. The default is TCP.
47     */
48    
49    public static void main(String[] args) {
50  
51      String protocollabel = "TCP";
52  
53      int i = 0;
54  
55      while( i < args.length) {
56            if( args[ i ].equals("-p") && i < args.length - 1 ){
57              protocollabel = args[ ++ i ];
58            } else {
59              System.err.println("Syntax: Sniffer -p protocol");
60              System.exit( 1 );
61            }
62            ++ i ;
63      }
64      
65      try {
66  
67        new SocketWrenchSession();
68  
69              SocketUtils.setProtocol( protocollabel );
70              
71        byte[] buffer = new byte[512];
72  
73        DatagramPacket packet;
74    
75        DatagramSocket socket = new DatagramSocket();
76  
77        IP4Message ipmessage;
78  
79        while (true) {
80  
81          packet = new DatagramPacket(buffer, buffer.length);
82  
83          socket.receive(packet);
84  
85          ipmessage =  IP4Reader.read(packet.getData(), packet.getLength(), true);
86  
87          switch (ipmessage.protocol) {
88            case SocketConstants.IPPROTO_UDP :
89  
90              UDPReader.read(
91                ipmessage.data,
92                0,
93                ipmessage.data.length,
94                true,
95                ipmessage.source,
96                ipmessage.destination);
97              break;
98  
99            case SocketConstants.IPPROTO_TCP :
100 
101             TCPReader.read(
102               ipmessage.data,
103               0,
104               ipmessage.data.length,
105               true,
106               ipmessage.source,
107               ipmessage.destination);
108             break;
109 
110           case SocketConstants.IPPROTO_ICMP :
111           default :
112             }
113 
114         System.out.println(
115           packet.getLength()
116             + " bytes received from "
117             + packet.getAddress());
118 
119         SocketUtils.dump(
120           System.out,
121           packet.getData(),
122           0,
123           packet.getLength());
124       }
125     } catch (Exception e) {
126       System.err.println(e.getMessage());
127     }
128   }
129 }