|
|||||||||
| PREV NEXT | FRAMES NO FRAMES | ||||||||
See:
Description
| Packages | |
|---|---|
| org.jnetpcap | Core libpcap functionality available on all platforms. |
| org.jnetpcap.nio | Native memory and IO management classes. |
| org.jnetpcap.packet | Packet decoding framework. |
| org.jnetpcap.packet.analysis | Protocol analysis support. |
| org.jnetpcap.packet.annotate | Annotation interfaces for header definitions. |
| org.jnetpcap.packet.format | Formatting classes for JPacket and JHeader objects. |
| org.jnetpcap.packet.structure | Packet, Header, Field, Binding and Scanner primitives that describe their structure. |
| org.jnetpcap.protocol | Network protocols and header definitions. |
| org.jnetpcap.protocol.application | Application protocol suite. |
| org.jnetpcap.protocol.lan | LAN protocol suite. |
| org.jnetpcap.protocol.network | Network protocol suite. |
| org.jnetpcap.protocol.tcpip | Tcp/Ip protocol suite. |
| org.jnetpcap.protocol.vpn | VPN protocol suite. |
| org.jnetpcap.protocol.wan | WAN protocol suite. |
| org.jnetpcap.util | Various support utility methods. |
| org.jnetpcap.util.config | SDK configuration using properties |
| org.jnetpcap.util.resolver | Address to human label resolvers. |
| org.jnetpcap.winpcap | WinPcap extensions to libpcap avialable on a limited set of platforms. |
StringBuffer
instead of
StringBuilder
or
Appendable
which are found in the JRE 1.5 jar file. Functionality wise the two APIs
and supplied jar files are identical in every other respect.
jNetPcap is a java wrapper around libpcap and WinPcap native libraries found on various unix and windows platforms. jNetPcap exposes the functionality as a java programming interface (API) which this documentation describes.
Feature highlights:
java.nio.ByteBuffer
object with not data copies.setBuffer for changing
buffer size on windows platform and others.StringBuilder
buffer and no java exceptions are thrown outside the usual
mismatched/invalid arguments.
Since jNetPcap is very closely mapped to native libpcap library functions, users are encouraged to read libpcap documentation at libpcap on wikipedia and visit libpcap's homepage at http://tcpdump.org. You can also visit jNetPcap project website for more information and a user guide with tutorials at http://jnetpcap.org .
Pcap (which stands for Packet Capture) is an application programming interface for packet capturing from a live network interface. The implementation of pcap for Unix-like systems is known as libpcap; the Windows port of libpcap is called WinPcap. libpcap and WinPcap may be used by a program to capture packets traveling over a network and, in newer versions, to transmit packets on a network at the link layer, as well as to get a list of network interfaces that can be used with libpcap or WinPcap. libpcap and WinPcap are the packet capture and filtering engines of many open source and commercial network tools, including protocol analyzers, network monitors, network intrusion detection systems, packet sniffers, traffic generators and network testers. The pcap API is designed for use from C and C++, so, for other languages such as scripting languages, Java, and .NET languages, a wrapper is generally used. (Credit: wikipedia.org)
org.jnetpcap
) while also providing the
WinPcap
extra functionality as an extension (
org.jnetpcap.winpcap
). The main classes which implement
libpcap
and
WinPcap
functionality are:
org.jnetpcap.Pcap class - core libpcap
methods available on all platformsorg.jnetpcap.winpcap.winpcap class - extensions
based on WinPcap library typically only available on windows
based systemPcap
object, the object contains a memory pointer to a C pcap_t structure.
When any non-static method call on the java class, will use the stored
reference to the native C structure to execute the requested function.
Same thing applies to all other structures such as
PcapIf
and the remaining. They are all peered and retain a memory reference to
their corresponding C structure. For safety purposes and java
protections, the reader is not allowed to access these C structures
directly and all the comparible
libpcap
library functions are provided as java methods. Therefore the is a very
close relationshipt between each java object and its corresponding
native C strucutre, the same applies to
libpcap
functions and their corresponding java methods.
The native libpcap library is not multithread safe. It does not support reentrant function calls from multiple threads. jNetPcap wrapper does not provide any addition multithreading support than what is provided by libpcap itself.
It is however safe to interact with various Pcap
objects from multiple threads, as long as access is externally
synchronized.
Pcap.loop()
using 2 threadsThe following example demonstrates, using pseudo code, how to capture packets from network interface in one thread while providing control of the capture session from another thread. The example specifically shows the neccessary synchronization needed to break a capture loop from another thread and closing the capture session. Thread #1 is a control thread and Thread #2 is the packet capture loop thread.
Thread #1 - control thread_start Thread #2 _receive Pcap object reference from Thread #2 _issue Pcap.breakLoop call _wait for Thread #2 to exit _issue Pcap.close call _goto startThread #2 - open live capture and loop thread
_issue Pcap.findAllDevs and retrieve all network devices _issue Pcap.openLive using one of the network devices _create PcapHandler, a callback object for the Pcap.loop, user data is Pcap object _issue Pcap.loop call with our PcapHandler, Thread #2 control is passed to libpcap // In Thread #2 libpcap supplies buffers to our PcapHandler // On first call to PcapHandler, we exchange Pcap object with Thread #1 // One possible implementation of the exchange is java.util.concurrent.Exchanger
In the above example, once Thread #1 issues a Pcap.breakLoop()
call, the loop in Thread #2 may not terminate immediately. Exactly how
the loop terminates is native libpcap library dependent. You
can see Pcap.breakLoop()
javadoc page for more specific implementation details. Thread #1 must
then wait for Thread #2 to break out of the loop and gracefully exit.
The wait can be accomplished using java.lang.Thread.join()
method on Thread #2 object reference. Once Thread #2 exits, it is safe
to call Pcap.close() on the exchanged Pcap
object.
Note #1: it is imperative to wait for Thread #2 to exit,
or some other way of synchronization, to ensure that Thread #2 has
broken out of the Pcap.loop() otherwise a premature call to
Pcap.close() while Thread #2 is still in the loop will
cause a coredump and the entire Java VM to crash. The coredump or crash
stems from the fact that libpcap is not multithreaded and a
single threaded execution is assumed. In a single thread, it is
impossible to issue a Pcap.close() at the same time Pcap.loop()
is still executing, since a Pcap.loop() is a blocking call.
Note #2:As a convenience, starting with version 1.2,
jNetPcap provides 2 methods to run loops in a background thread. The
methods are found in the Pcap and PcapUtils
classes. The names are dispatchInBackground() and loopInBackground().
These methods return a PcapTask handle which allows the
user to interact with the background thread such as aquire error codes
and issue breakLoop safely to terminate any background
tasks nicely.
WinPcap.isSupported()
which will return a boolean value. Returned value of
true
means that the extension is supported otherwise
false
.
WinPcap
extension can not be used on a platform that does not support the
extension. Any method used with
WinPcap
on an unsupported platform, will throw an immediate unchecked
PcapExtensionNotAvailableException
, to indicate that this extension is not available on this particular
platform. Therefore, it is very important to always do a check first
before relying on an extension function (i.e. org.jnetpcap.winpcap.)
org.jnetpcap.packet
package and provides limited decoding capabilities. A
JPacketHandler
dispatcher handler is used with
Pcap.loop(), Pcap.dispatch()
methods which receives JPacket objects. JPackets are fully decoded
packets and provide a packet API that allows the user to query which
protocol headers exist in the packet and access those headers. Each
header then provides a method for each network header field found in the
header and any logic that goes into decoding that protocol header.
Various formatters are also provided which allow a packet to be reformatted to various textual forms such as a plain text that is human readable and less human readable Xml format which is great for computer applications.
jNetPcap is supplied with a core set of protocol headers, but the list is not complete. The API provides a mechanism for extending and adding additional protocols to the framework. Protocol header contributions are welcome.
Currently no other extensions are supported, but can easily be added once suitable candidates are found such as AirPcap from http://cacetech.com , as long licensing terms permit it.
Note: currently there are no plans to support AirPcap as an extension. The name was arbitrarily chosen as an example of another libpcap extension.Here is a little introduction to other java wrappers (unrelated to jNetPcap), in order to clear up the confusion between various like sounding projects in relation to a java wrapper for native libpcap. The jNetPcap is completely independent project from all other java libpcap wrappers. The names of the projects may sound similar but the implementation and goals of each project were much different.
Two non related projects that jNetPcap team is aware of are both named the same jPcap and they both provide two different APIs and implementations. Please see each project's website for more detail:
(Note: for the most upto date list of all wrappers for libpcap library look at Wikipedia webpage for libpcap .)
Both of jPcap projects provide some kind of packet decoding facility, but are limited on the libpcap features they expose. Starting with release 1.2, jNetPcap also provides limited packet decoding capabilities. If you require higher level API that is object oriented, follows java programming paradigm, provides full packet decoding and capture file manipulation, we also recommend jNetStream , a sister project, that is built on top of jNetPcap .
|
|||||||||
| PREV NEXT | FRAMES NO FRAMES | ||||||||