|
From: Peter P. <pr...@us...> - 2006-11-22 18:51:09
|
Update of /cvsroot/pyxida/Pyxida/lib/jpcap-0.5.1-lib/src/java/jpcap In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13312/lib/jpcap-0.5.1-lib/src/java/jpcap Added Files: JpcapCaptor.java NetworkInterface.java PacketReceiver.java JpcapInstance.java package.html JpcapSender.java NetworkInterfaceAddress.java JpcapWriter.java Log Message: Added remaining Jpcap files (java sources + docs) --- NEW FILE: JpcapInstance.java --- package jpcap; abstract class JpcapInstance { protected static final int MAX_NUMBER_OF_INSTANCE = 255; protected static boolean[] instanciatedFlag = new boolean[MAX_NUMBER_OF_INSTANCE]; protected int ID; protected int reserveID(){ ID = -1; for (int i = 0; i < MAX_NUMBER_OF_INSTANCE; i++) if (!instanciatedFlag[i]) { ID = i; instanciatedFlag[i] = true; break; } return ID; } protected void unreserveID(){ instanciatedFlag[ID]=false; } } --- NEW FILE: JpcapWriter.java --- package jpcap; import jpcap.packet.Packet; /** This class is used to save the captured packets into a file. */ public class JpcapWriter { private native String nativeOpenDumpFile(String filename,int ID); private JpcapWriter(JpcapCaptor jpcap,String filename) throws java.io.IOException{ String ret=nativeOpenDumpFile(filename,jpcap.ID); if(ret!=null){ //error throw new java.io.IOException(ret); } } /** Opens a file to save the captured packets. * @param jpcap instance of Jpcap that was used to capture (load) packets * @param filename filename * @throws IOException If the file cannot be opened */ public static JpcapWriter openDumpFile(JpcapCaptor jpcap,String filename) throws java.io.IOException{ return new JpcapWriter(jpcap,filename); } /** Closes the opened file. */ public native void close(); /** Saves a packet into the file. * @param packet Packet to be saved */ public native void writePacket(Packet packet); static{ System.loadLibrary("jpcap"); } } --- NEW FILE: JpcapCaptor.java --- package jpcap; import jpcap.packet.Packet; /** * This class is used to capture packets or read packets from a captured file. */ public class JpcapCaptor extends JpcapInstance{ /** * Number of received packets * * @see #updateStat() */ public int received_packets; /** * Number of dropped packets * * @see #updateStat() */ public int dropped_packets; private native String nativeOpenLive(String device, int snaplen, int promisc, int to_ms); private native String nativeOpenOffline(String filename); private native void nativeClose(); private JpcapCaptor() throws java.io.IOException { if (reserveID()<0) throw new java.io.IOException("Unable to open a device: " + MAX_NUMBER_OF_INSTANCE + " devices are already opened."); } /** * Returns the interfaces that can be used for capturing. * * @return List of Interface objects */ public static native NetworkInterface[] getDeviceList(); /** * Initializes the network interface, and returns an instance of this class. * * @return an instance of this class Jpcap. * @param intrface * The network interface to capture packets * @param snaplen * Max number of bytes captured at once * @param promisc * If true, the inferface becomes promiscuous mode * @param to_ms * Timeout of * {@link #processPacket(int,PacketReceiver) processPacket()} * @exception java.io.IOException * Raised when the specified interface cannot be opened */ public static JpcapCaptor openDevice(NetworkInterface intrface, int snaplen, boolean promisc, int to_ms) throws java.io.IOException { JpcapCaptor jpcap = new JpcapCaptor(); String ret = jpcap.nativeOpenLive(intrface.name, snaplen, (promisc ? 1 : 0), to_ms); if (ret != null) // error throw new java.io.IOException(ret); return jpcap; } /** * Opens a dump file created by tcpdump or Ethereal, and returns an instance * of this class. * * @param filename * File name of the dump file * @exception java.io.IOException * If the file cannot be opened * @return an instance of this class Jpcap */ public static JpcapCaptor openFile(String filename) throws java.io.IOException { JpcapCaptor jpcap = new JpcapCaptor(); String ret = jpcap.nativeOpenOffline(filename); if (ret != null) // error throw new java.io.IOException(ret); return jpcap; } /** Closes the opened interface of dump file. */ public void close() { System.out.println("close:"+ID); nativeClose(); unreserveID(); } /** * Returns a captured packet. * * @return a captured packet */ public native Packet getPacket(); /** * Captures the specified number of packets consecutively. * * @param count * Number of packets to be captured<BR> * You can specify -1 to capture packets parmanently until * timeour, EOF or an error occurs. * @param handler * an instnace of JpcapHandler that analyzes the captured packets * @return Number of captured packets */ public native int processPacket(int count, PacketReceiver handler); /** * Captures the specified number of packets consecutively. * <P> * * Unlike processPacket(), this method ignores the timeout. * * @param count * Number of packets to be captured<BR> * You can specify -1 to capture packets parmanently until EOF or * an error occurs. * @param handler * an instnace of JpcapHandler that analyzes the captured packets * @return Number of captured packets */ public native int loopPacket(int count, PacketReceiver handler); /** * Captures a group of packets. * <P> * * Unlike processPacket(), this method may return before collecting the * number of packets specified by count. Also, in "non-blocking" mode, this * method returns immediately even if it didn't capture any packet. * loopPacket() and processPacket() do not support "non-blocking" mode. * * @param count * Number of packets to be captured<BR> * You can specify -1 to capture packets parmanently until EOF or * an error occurs. * @param handler * an instnace of JpcapHandler that analyzes the captured packets * @return Number of captured packets */ public native int dispatchPacket(int count, PacketReceiver handler); /** * Sets/unsets "non-blocking" mode * @param nonblocking TRUE to set "non-blocking" mode. FALSE to set "blocking" mode */ public native void setNonBlockingMode(boolean nonblocking); /** * Checks if the current setting is in "non-blocking" mode or not. * @return TRUE if it is in "non-blocking" mode. FALSE otherwise. */ public native boolean isNonBlockinMode(); /** * Set a flag that will force processPacket() and loopPacket() to return * rather than looping. * */ public native void breakLoop(); /** * Sets a filter. This filter is same as tcpdump. * * @param condition * a string representation of the filter * @param optimize * If true, the filter is optimized */ public native void setFilter(String condition, boolean optimize); /** * Updates {@link #received_packets received_packets} and * {@link #dropped_packets dropped_packets}. */ public native void updateStat(); /** * Returns an error message * * @return error message */ public native String getErrorMessage(); /** * Obtains an instance of JpcapSender that uses the same interface to send packets. * You can use this method only if you opened an interface with openDevice() method. * @return returns an instance of JpcapSender */ public JpcapSender getJpcapSenderInstance(){ return new JpcapSender(ID); } static { System.loadLibrary("jpcap"); } } --- NEW FILE: JpcapSender.java --- package jpcap; import jpcap.packet.Packet; /** This class is used to send a packet. */ public class JpcapSender extends JpcapInstance { private native String nativeOpenDevice(String device); private native void nativeSendPacket(Packet packet); private native void nativeCloseDevice(); private native void nativeOpenRawSocket(); private native void nativeSendPacketViaRawSocket(Packet packet); private native void nativeCloseRawSocket(); private static final int RAW_SOCKET_ID=99999; private JpcapSender() throws java.io.IOException { if (reserveID() < 0) throw new java.io.IOException("Unable to open a device: " + MAX_NUMBER_OF_INSTANCE + " devices are already opened."); } JpcapSender(int ID){ this.ID=ID; } /** * Initializes a network interface for sending a packet, and returns an * instance of this class. * * @param device * Interface for sending a packet * @throws IOException * Raised when initialization of the interface failed * @return intstance of this class (JpcapSender) */ public static JpcapSender openDevice(NetworkInterface device) throws java.io.IOException { JpcapSender sender = new JpcapSender(); String ret=sender.nativeOpenDevice(device.name); if(ret==null) return sender; else throw new java.io.IOException(ret); } /** * Open a raw IP socket to send a packet.<BR> * When sending a packet via a raw socket, the datalink header of the packet is ignored * (= automatically generated by OS).<P> * Note: the implementation and behavior of a raw socket may vary in different OS. * Also, you can only open one raw socket at a time. * * @throws IOException * Raised when initialization of the interface failed * @return intstance of this class (JpcapSender) */ public static JpcapSender openRawSocket() throws java.io.IOException { JpcapSender sender = new JpcapSender(); sender.nativeOpenRawSocket(); sender.ID=RAW_SOCKET_ID; return sender; } /** Closes the interface. */ public void close() { if(ID==RAW_SOCKET_ID) nativeCloseRawSocket(); else nativeCloseDevice(); unreserveID(); } /** * Sends a packet. * <P> * If this JpcapSender instance was created by openDevice(), you need to set * the Datalink layer's header (e.g., Ethernet header) of the packet. <P> * * If this JpcapSender instance was created by openRawSocket(), you can only * send IP packets, but you may not need to set the Datalink layer's header * of the IP packets you want to send.<BR> * Note: the implementation and behavior of a raw socket may vary in different OS. * For example, in Windows 2000/XP, you need to manually set the datalink/IP headers * of a packet. * @param packet Packet to be sent */ public void sendPacket(Packet packet){ if(ID==RAW_SOCKET_ID) nativeSendPacketViaRawSocket(packet); else nativeSendPacket(packet); } } --- NEW FILE: NetworkInterface.java --- package jpcap; /** * This class represents a network interface. * @author kfujii */ public class NetworkInterface { /** Name of the network interface */ public String name; /** Description about the network interface (e.g., "3Com ..."). May be null.*/ public String description; /** TRUE if this is a loopback interface */ public boolean loopback; /** Name of the datalink of the network interface*/ public String datalink_name; /** Description about the datalink of the network interface. May be null. */ public String datalink_description; /** Ethernet MAC address of the network interface */ public byte[] mac_address; /** Network addresses assigned the network interface. May be null if it is a non-IP (e.g. NetBios) address. */ public NetworkInterfaceAddress[] addresses; public NetworkInterface(String name,String description,boolean loopback, String datalink_name,String datalink_description,byte[] mac,NetworkInterfaceAddress[] addresses){ this.name=name; this.description=description; this.loopback=loopback; this.datalink_name=datalink_name; this.datalink_description=datalink_description; this.mac_address=mac; this.addresses=addresses; } } --- NEW FILE: PacketReceiver.java --- package jpcap; import jpcap.packet.Packet; /** This interface is used to define a method to analyze the captured packets, * which is used in JpcapCaptor.handlePacket() or JpcapCaptor.processPacket() * @see JpcapCaptor#processPacket(int,PacketReceiver) * @see JpcapCaptor#loopPacket(int,PacketReceiver) */ public interface PacketReceiver { /** Analyzes a packet.<BR> * <BR> * This method is called everytime a packet is captured. * @param p A packet to be analyzed */ public void receivePacket(Packet p); } --- NEW FILE: package.html --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head> <title>jpcap package</title> </head> <body bgcolor="white"> Provides classes and interfaces for capturing packets from a network interface, sending packets to a network interface, reading packets from a file, and writing packets to a local file. </body> </html> --- NEW FILE: NetworkInterfaceAddress.java --- package jpcap; import java.net.InetAddress; import java.net.UnknownHostException; /** * This class represents a network address assigned to a network interface. * @author kfujii */ public class NetworkInterfaceAddress{ /** Address of the network interface */ public InetAddress address; /** Subnet mask of the network interface */ public InetAddress subnet; /** Broadcast address of the network interface. May be null. */ public InetAddress broadcast; /** Destination address of the network interface (for P2P connection). May be null. */ public InetAddress destination; public NetworkInterfaceAddress(byte[] address,byte[] subnet,byte[] broadcast,byte[] destination){ try{ if(address!=null) this.address=InetAddress.getByAddress(address); if(subnet!=null) this.subnet=InetAddress.getByAddress(subnet); if(broadcast!=null) this.broadcast=InetAddress.getByAddress(broadcast); if(destination!=null) this.destination=InetAddress.getByAddress(destination); }catch(UnknownHostException e){ } } } |