kaitoy / pcap4j

A Java library for capturing, crafting, and sending packets.
https://www.pcap4j.org/
Other
1.16k stars 309 forks source link
capture-packets java java-library jna libpcap npcap pcap-library sending-packets winpcap

Japanese

Pcap4J

Logos

Slack Maven Central

Build Status CircleCI Build status Coverage Status Code Quality: Java Total Alerts

Pcap4J

Pcap4J is a Java library for capturing, crafting and sending packets. Pcap4J wraps a native packet capture library (libpcap, WinPcap, or Npcap) via JNA and provides you Java-Oriented APIs.

Contents

Download

Pcap4J is available on the Maven Central Repository.

Why Pcap4J was born

I have been developing an SNMP network simulator (SNeO, available at the link below) by Java, which needed to capture packets and I found the pcap was useful for it. Although there are some implementations of the pcap such as libpcap (for UNIX) and WinPcap (for Windows), because they are both native libraries, a Java wrapper library was necessary in order to use them for SNeO. I researched it and found three Java wrapper libraries for pcap: jpcap, jNetPcap, and Jpcap. But both jpcap and jNetPcap were unsuitable for SNeO because they seemed to be designed for mainly capturing packets and not to be useful for making and sending packets so much. On the other hand, Jpcap looked useful for making and sending packets. But it had a defect in capturing ICMP packets and its development seemed to be stopped long ago. That's why I started developing Pcap4j.

Features

How to use

System requirements

Dependencies

Pcap4j 1.1.0 or older needs Java 5.0+. Pcap4j 1.2.0 or newer needs Java 6.0+. And also a pcap native library (libpcap 1.0.0+, WinPcap 3.0+, or Npcap), jna, slf4j-api, and an implementation of logger for slf4j are required. I'm using the following libraries for the test.

Platforms

I tested Pcap4j on the following OSes with x86 or x64 processors.

And tomute tested Pcap4j on Mac OS X. The report is here. Thank you, tomute!

I hope Pcap4j can run on the other OSes supported by both JNA and libpcap.

Others

Pcap4J needs administrator/root privileges. Or, if on Linux, you can run Pcap4J with a non-root user by granting capabilities CAP_NET_RAW and CAP_NET_ADMIN to your java command by the following command: setcap cap_net_raw,cap_net_admin=eip /path/to/java

Documents

The latest JavaDoc is here. Each version's JavaDoc is on the Maven Central Repository.

Refer to here for information about Pcap4J modules.

Because Pcap4J is a wrapper of a pcap native library, the following documents help you to understand how to use Pcap4J.

You can learn how to write Pcap4J programs from samples.

Learn more about Pcap4j from the following documents:

How to run samples

See the following examples:

If you want to run a sample in pcap4j-sample on Eclipse, add pcap4j-packetfactory-static or pcap4j-packetfactory-propertiesbased project to the top of User Entries in Classpath tab of the Run Configuration for the sample.

How to use in Maven project

Add a dependency to the pom.xml as like below:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <dependencies>
    <dependency>
      <groupId>org.pcap4j</groupId>
      <artifactId>pcap4j-core</artifactId>
      <version>1.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.pcap4j</groupId>
      <artifactId>pcap4j-packetfactory-static</artifactId>
      <version>1.8.2</version>
    </dependency>
       ...
  </dependencies>
  ...
</project>

About native library loading

By default, Pcap4j loads the native libraries on the following conditions:

You can use the following Java system properties to change the default behavior.

WinPcap or Npcap

There are two native pcap libraries for Windows; WinPcap and Npcap.

The development of WinPcap has stopped since version 4.1.3 (libpcap 1.0.0 base) was released on 3/8/2013, while Npcap is still being developed. So, you should pick Npcap if you want to use new features or so.

Pcap4J can load WinPcap without tricks because it's installed in %SystemRoot%\System32\.

On the other hand, because Npcap is installed in %SystemRoot%\System32\Npcap\ by default, you need to do either of the following so that Pcap4J can load it:

Docker

A Docker image for Pcap4J on CentOS is available at Docker Hub.

Download it by docker pull kaitoy/pcap4j and execute docker run kaitoy/pcap4j:latest to start capturing packets from eth0 on the container.

This image is built everytime a commit is made on the Git repositry.

How to build

  1. Install libpcap, WinPcap, or Npcap:

    Install WinPcap (if Windows) or libpcap (if Linux/UNIX). It's needed for the unit tests which are run during a build.

  2. Install JDK:

    Download and install JDK 9, 10, or 11, and set the environment variable JAVA_HOME properly.

  3. Add the JDK to Maven toolchains:

    Create toolchains.xml describing the JDK installed at the previous step and put it into ~/.m2/. toolchains.xml is like below:

    <?xml version="1.0" encoding="UTF-8"?>
    <toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
      <toolchain>
        <type>jdk</type>
        <provides>
          <version>11</version>
        </provides>
        <configuration>
          <jdkHome>/path/to/jdk-11</jdkHome>
        </configuration>
      </toolchain>
    </toolchains>
  4. Install Git:

    Download Git and install it. This step is optional.

  5. Clone the Pcap4J repository:

    If you installed Git, execute the following command: git clone git@github.com:kaitoy/pcap4j.git
    Otherwise, download the repository as a zip ball and extract it.

  6. Build:

    Open a command prompt/a terminal, cd to the project root directory, and execute ./mvnw install. Note Administrator/root privileges are needed for the unit tests.

Contributing Code

  1. Fork this repository.

  2. Create a branch from v1 branch.

  3. Write code.

    • Please refer to This PR as an example when adding protocol support.
    • This project follows Google Java Style Guide. Execute the following command to format your code: mvnw com.coveo:fmt-maven-plugin:format
  4. Send a PR from the branch.

License

LICENSE

Contacts

Kaito Yamada (kaitoy@pcap4j.org)