jpcap
Class Jpcap

java.lang.Object
  |
  +--jpcap.Jpcap

public class Jpcap
extends java.lang.Object

This class is used to capture packets.

Sample program

 import jpcap.*;

 class Tcpdump implements JpcapHandler
 {
   public void handlePacket(Packet packet){
     System.out.println(packet);
   }

   public static void main(String[] args) throws java.io.IOException{
     Jpcap jpcap=Jpcap.openDevice(args[0],1000,true,20);
     jpcap.processPacket(-1,new Tcpdump());
   }
 }
 


Field Summary
 int dropped_packets
          Number of dropped packets
 int received_packets
          Number of received packets
 
Method Summary
 void close()
          Closes the opened interface of dump file.
static java.lang.String[] getDeviceDescription()
          Returns the descriptions of the interfaces (For Windows only)
static java.lang.String[] getDeviceList()
          Returns the names of the interfaces that can be used for capturing.
 java.lang.String getErrorMessage()
          Returns an error message
 Packet getPacket()
          Returns a captured packet.
static java.lang.String lookupDevice()
          Returns the names of the interfaces that can be used for capturing.
 int loopPacket(int count, JpcapHandler handler)
          Captures the specified number of packets consecutively.
static Jpcap openDevice(java.lang.String device, int snaplen, boolean promisc, int to_ms)
          Initializes the network interface, and returns an instance of this class.
static Jpcap openFile(java.lang.String filename)
          Opens a dump file created by tcpdump or Ethereal, and returns an instance of this class.
 int processPacket(int count, JpcapHandler handler)
          Captures the specified number of packets consecutively.
 void setFilter(java.lang.String condition, boolean optimize)
          Sets a filter.
 void updateStat()
          Updates received_packets and dropped_packets.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

received_packets

public int received_packets
Number of received packets

See Also:
updateStat()

dropped_packets

public int dropped_packets
Number of dropped packets

See Also:
updateStat()
Method Detail

openDevice

public static Jpcap openDevice(java.lang.String device,
                               int snaplen,
                               boolean promisc,
                               int to_ms)
                        throws java.io.IOException
Initializes the network interface, and returns an instance of this class.

Parameters:
device - Name of the network interface
snaplen - Max number of bytes captured at once
promisc - If true, the inferface becomes promiscuous mode
to_ms - Timeout of processPacket()
Returns:
an instance of this class Jpcap.
Throws:
java.io.IOException - Raised when the specified interface cannot be opened

openFile

public static Jpcap openFile(java.lang.String filename)
                      throws java.io.IOException
Opens a dump file created by tcpdump or Ethereal, and returns an instance of this class.

Parameters:
filename - File name of the dump file
Returns:
an instance of this class Jpcap
Throws:
java.io.IOException - If the file cannot be opened

lookupDevice

public static java.lang.String lookupDevice()
Returns the names of the interfaces that can be used for capturing.

Returns:
List of the interface names

getDeviceList

public static java.lang.String[] getDeviceList()
Returns the names of the interfaces that can be used for capturing.

Returns:
List of the interface names

getDeviceDescription

public static java.lang.String[] getDeviceDescription()
Returns the descriptions of the interfaces (For Windows only)

On Windows, the interface name is represented as a difficult string (e.g. \Device\Packet_{6E05D...}) This method returns the description of each interface that is easier to understand (e.g. 3com EtherLinkII).

Returns:
Descriptions of the interfaces

getPacket

public Packet getPacket()
Returns a captured packet.

Returns:
a captured packet

processPacket

public int processPacket(int count,
                         JpcapHandler handler)
Captures the specified number of packets consecutively.

Parameters:
count - Number of packets to be captured
You can specify -1 to capture packets parmanently until timeour, EOF or an error occurs.
handler - an instnace of JpcapHandler that analyzes the captured packets
Returns:
Number of captured packets

loopPacket

public int loopPacket(int count,
                      JpcapHandler handler)
Captures the specified number of packets consecutively.

Unlike processPacket(), this method ignores the timeout.

Parameters:
count - Number of packets to be captured
You can specify -1 to capture packets parmanently until EOF or an error occurs.
handler - an instnace of JpcapHandler that analyzes the captured packets
Returns:
Number of captured packets

setFilter

public void setFilter(java.lang.String condition,
                      boolean optimize)
Sets a filter. This filter is same as tcpdump.

Parameters:
condition - a string representation of the filter
optimize - If true, the filter is optimized

updateStat

public void updateStat()
Updates received_packets and dropped_packets.


getErrorMessage

public java.lang.String getErrorMessage()
Returns an error message

Returns:
error message

close

public void close()
Closes the opened interface of dump file.