jpcap
Class Jpcap

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

public class Jpcap
extends java.lang.Object

Main class of package jpcap.
Capturing and Sending packets are done through the instance of this class.

Sample tcpdump-like capturing 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=new Jpcap(args[0],1000,true,20);
     jpcap.processPacket(-1,new Tcpdump());
   }
 }
 


Inner Class Summary
 class Jpcap.JpcapInfo
          Class for keeping capture info.
 
Field Summary
 int dropped_packets
          Number of packets dropped
 int received_packets
          Number of packets received
 
Constructor Summary
Jpcap(java.lang.String filename)
          Opens the dump file created by tcpdump and creates the instance of this class.
Jpcap(java.lang.String device, int snaplen, boolean promisc, int to_ms)
          Initializes the specified network interface and creates the instance of this class.
 
Method Summary
 void close()
          Close the interface or dump file which is currently opened.
 java.lang.String getErrorMessage()
          Returns the error message which was caused by the error while capturing.
 Jpcap.JpcapInfo getJpcapInfo()
          Return JpcapInfo of this Jpcap instance.
 Packet getPacket()
          Captures one packet.
static java.lang.String lookupDevice()
          Returns the name of the device which can be used for capturing.
 void openRawSocket()
          Initializes the sending socket.
This method must be called before sending packets.
 int processPacket(int count, JpcapHandler handler)
          Captures the specified number of packets consecutively.
 void sendPacket(IPPacket packet)
          Sends a IP packet.

Currently only TCP/UDP/ICMP over IPv4 are supported.
For ICMP, only echo packets can be sent.
 void setFilter(java.lang.String condition, boolean optimize)
          Sets up the capturing filter.
 void updateStat()
          Updates received_packets and dropped_packets
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

received_packets

public int received_packets
Number of packets received
See Also:
updateStat()

dropped_packets

public int dropped_packets
Number of packets dropped
See Also:
updateStat()
Constructor Detail

Jpcap

public Jpcap(java.lang.String device,
             int snaplen,
             boolean promisc,
             int to_ms)
      throws java.io.IOException
Initializes the specified network interface and creates the instance of this class.
Parameters:
device - Name of the interface (eg: hme0,eth0)
snaplen - Maximum number of bytes captured at once
promisc - The specified interface goes promiscuous mode if true
to_ms - Time out of processPacket()
Throws:
java.io.IOException - If the speficied interface couldn't be open

Jpcap

public Jpcap(java.lang.String filename)
      throws java.io.IOException
Opens the dump file created by tcpdump and creates the instance of this class.
Parameters:
filename - Name of dump file
Throws:
java.io.IOException - If the specified file couldn't open
Method Detail

lookupDevice

public static java.lang.String lookupDevice()
Returns the name of the device which can be used for capturing.
Returns:
Name of the found device

getPacket

public Packet getPacket()
Captures one packet.

processPacket

public int processPacket(int count,
                         JpcapHandler handler)
Captures the specified number of packets consecutively.
Parameters:
count - Maximum number of packets processing
-1 means to process packets unlimitedly.
handler - The captures packets are passed to this JpcapHandler object to analyze
Returns:
Number of packet captured

setFilter

public void setFilter(java.lang.String condition,
                      boolean optimize)
Sets up the capturing filter.
Parameters:
condition - String which represents the condition of filtering. (See the document of tcpdump)
optimize - Filter is optimized if true

updateStat

public void updateStat()
Updates received_packets and dropped_packets

openRawSocket

public void openRawSocket()
Initializes the sending socket.
This method must be called before sending packets.

sendPacket

public void sendPacket(IPPacket packet)
Sends a IP packet.

Currently only TCP/UDP/ICMP over IPv4 are supported.
For ICMP, only echo packets can be sent.
Parameters:
packet - IP packet to be sent

getErrorMessage

public java.lang.String getErrorMessage()
Returns the error message which was caused by the error while capturing.

close

public void close()
Close the interface or dump file which is currently opened.

getJpcapInfo

public Jpcap.JpcapInfo getJpcapInfo()
Return JpcapInfo of this Jpcap instance.