net.sourceforge.jpcap.capture
Interface PacketCaptureCapable

All Known Implementing Classes:
PacketCapture, PacketCaptureSimulator

public interface PacketCaptureCapable

This is the packet capture interface. It is implemented by both the packet capture system (PacketCapture) and the simulator (PacketCaptureSimulator).

The interface has two major components: methods that a client uses to register for packet events and methods that a client calls in order to setup and initiate packet capture.


Field Summary
static int DEFAULT_SNAPLEN
          Snapshot length.
static int DEFAULT_TIMEOUT
          Default capture timeout in milliseconds.
 
Method Summary
 void addPacketListener(PacketListener objListener)
          Register a packet object listener with this capture system.
 void addRawPacketListener(RawPacketListener rawListener)
          Register a raw packet listener with this capture system.
 void capture(int count)
          Capture packets.
 void close()
          Close the capture device.
 java.lang.String findDevice()
          Detect a network device suitable for packet capture.
 int getLinkLayerType()
          Fetch the link layer type for the specified device.
 int getNetmask(java.lang.String device)
          Fetch the network mask for the specified device.
 int getNetwork(java.lang.String device)
          Fetch the network number for the specified device.
 int getSnapshotLength()
          Get the snapshot length given that network device is open.
 CaptureStatistics getStatistics()
          Fetch statistics on captured packets.
 void open(java.lang.String device, boolean promiscuous)
          Open a network device for data capture.
 void open(java.lang.String device, int snaplen, boolean promiscuous, int timeout)
          Open a network device for data capture.
 void openOffline(java.lang.String fileName)
          Open a tcpdump-formatted savefile.
 void removePacketListener(PacketListener objListener)
          Deregister a packet object listener from this capture system.
 void removeRawPacketListener(RawPacketListener rawListener)
          Deregister a raw packet listener from this capture system.
 void setFilter(java.lang.String filterExpression, boolean optimize)
          Create, compile and activate a filter from a filter expression.
 

Field Detail

DEFAULT_SNAPLEN

public static final int DEFAULT_SNAPLEN
Snapshot length. Maximum number of bytes per packet to capture. For IPv4, 96 bytes guarantees that at least the headers of most packet types and protocols will get captured. For IPv6, 68 is a better value?

See Also:
Constant Field Values

DEFAULT_TIMEOUT

public static final int DEFAULT_TIMEOUT
Default capture timeout in milliseconds.

See Also:
Constant Field Values
Method Detail

open

public void open(java.lang.String device,
                 boolean promiscuous)
          throws CaptureDeviceOpenException
Open a network device for data capture. Throws an exception if the device name specified is invalid. Uses default values for the capture timeout and snaplen.

Parameters:
device - the name of the network device. Examples of valid network devices on linux are 'eth0' and 'ppp0'.
promiscuous - whether or not the device should be opened in promiscuous mode.
Throws:
CaptureDeviceOpenException

open

public void open(java.lang.String device,
                 int snaplen,
                 boolean promiscuous,
                 int timeout)
          throws CaptureDeviceOpenException
Open a network device for data capture.

Parameters:
device - the name of the network device. Examples of valid network devices on linux are 'eth0' and 'ppp0'.
snaplen - the 'snapshot' length. Defines the maximum number of bytes to save from each captured packet.
promiscuous - whether or not the device should be opened in promiscuous mode.
timeout - the packet capture timeout in milliseconds.
Throws:
CaptureDeviceOpenException

openOffline

public void openOffline(java.lang.String fileName)
                 throws CaptureFileOpenException
Open a tcpdump-formatted savefile.

Parameters:
fileName - the name of the savefile.
Throws:
CaptureFileOpenException

setFilter

public void setFilter(java.lang.String filterExpression,
                      boolean optimize)
               throws InvalidFilterException
Create, compile and activate a filter from a filter expression.

Parameters:
filterExpression - the filter expression. For example, the expression "host techno" would filter only packets sent or arriving at the host named techno.
optimize - whether or not the resulting bpf code is optimized internally by libpcap.
Throws:
InvalidFilterException

capture

public void capture(int count)
             throws CapturePacketException
Capture packets.

Parameters:
count - the number of packets to capture. If count is negative, capture will block forever, unless an exception is thrown.
Throws:
CapturePacketException

getStatistics

public CaptureStatistics getStatistics()
Fetch statistics on captured packets. This method should not be called unless capture() was previously called.

Returns:
packet capture statistics.

close

public void close()
Close the capture device.


findDevice

public java.lang.String findDevice()
                            throws CaptureDeviceNotFoundException
Detect a network device suitable for packet capture.

Returns:
a string describing the network device. if no device can be found, null is returned.
Throws:
CaptureDeviceNotFoundException

getNetwork

public int getNetwork(java.lang.String device)
               throws CaptureConfigurationException
Fetch the network number for the specified device.

Parameters:
device - the name of the network device.
Returns:
the network address
Throws:
CaptureConfigurationException

getNetmask

public int getNetmask(java.lang.String device)
               throws CaptureConfigurationException
Fetch the network mask for the specified device.

Parameters:
device - the name of the network device.
Returns:
the netmask address
Throws:
CaptureConfigurationException

getLinkLayerType

public int getLinkLayerType()
                     throws CaptureConfigurationException
Fetch the link layer type for the specified device.

Returns:
the link layer type code.
Throws:
CaptureConfigurationException

getSnapshotLength

public int getSnapshotLength()
Get the snapshot length given that network device is open.

Returns:
the packet snapshot length.

addRawPacketListener

public void addRawPacketListener(RawPacketListener rawListener)
Register a raw packet listener with this capture system.

Parameters:
rawListener - the raw packet listener to add to the notification list.

removeRawPacketListener

public void removeRawPacketListener(RawPacketListener rawListener)
Deregister a raw packet listener from this capture system.

Parameters:
rawListener - the raw packet listener to remove from the notification list.

addPacketListener

public void addPacketListener(PacketListener objListener)
Register a packet object listener with this capture system.

Parameters:
objListener - the packet listener to add to the notification list.

removePacketListener

public void removePacketListener(PacketListener objListener)
Deregister a packet object listener from this capture system.

Parameters:
objListener - the packet listener to remove from the notification list.