Skip to content
Mark Bednarczyk edited this page Oct 21, 2024 · 1 revision

Welcome to the jnetpcap-sdk wiki!

Getting Started with jnetpcap

This guide will help you get started with jnetpcap, including installation using Maven and basic usage examples.

Table of Contents

  1. JDK Requirements
  2. Installation
  3. Basic Setup
  4. First Capture
  5. Next Steps

JDK Requirements

jNetPcap provides two versions to accommodate different JDK environments:

JDK 21 LTS Configuration

When using JDK 21 LTS, you'll need to enable preview features for specific modules:

Required Compiler Options

javac --enable-preview --release 21

Maven Configuration

Add the following to your pom.xml for JDK 21 support:

<properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <maven.compiler.release>21</maven.compiler.release>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>21</source>
                <target>21</target>
                <compilerArgs>
                    <arg>--enable-preview</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

Module-Specific Requirements

  • jnetruntime-api: Requires --enable-preview
  • jnetpcap-wrapper: Requires --enable-preview
  • All other modules: Standard JDK 21 compilation

JDK 22 Configuration

JDK 22 users can use jNetPcap without preview features:

Maven Configuration

<properties>
    <maven.compiler.source>22</maven.compiler.source>
    <maven.compiler.target>22</maven.compiler.target>
    <maven.compiler.release>22</maven.compiler.release>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>22</source>
                <target>22</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Module-Specific Requirements

  • All modules: Standard JDK 22 compilation (no special options required)

Running Applications

JDK 21

When running applications with JDK 21:

# For applications using jnetruntime-api or jnetpcap-wrapper
java --enable-preview -jar your-application.jar

# For applications using only other modules
java -jar your-application.jar

JDK 22

When running applications with JDK 22:

java -jar your-application.jar

Version Selection Guidelines

  1. JDK 21 LTS Version

    • Choose this if you need long-term support
    • Acceptable if preview features are not a concern
    • Required configuration is more complex
  2. JDK 22 Version

    • Choose this for simplified configuration
    • No preview features required
    • Latest Java features available
    • Shorter support lifecycle

[Previous Installation section and rest of the content remains the same...]

Installation

Maven Dependencies

To use jnetpcap in your project, you'll need to add the appropriate dependencies to your pom.xml file.

First, add the core protocol pack SDK parent:

<parent>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-sdk-parent</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</parent>

Then add the protocol pack API dependency:

<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-api</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

Protocol Packs

Depending on your needs, you can add additional protocol packs. Each pack provides support for specific protocols:

<!-- Core Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-core</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- VoIP Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-voip</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- Web Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-web</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- IoT Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-iot</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- Telco Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-telco</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- Database Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-database</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- Microsoft Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-microsoft</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- LTE Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-lte</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

<!-- 3G Protocol Pack -->
<dependency>
    <groupId>com.slytechs.jnet.protocol</groupId>
    <artifactId>protocol-pack-3g</artifactId>
    <version>0.10.0-SNAPSHOT</version>
</dependency>

Choose the protocol packs based on the protocols you need to work with. For example:

  • If you're working with VoIP applications, include the protocol-pack-voip
  • For web applications, include the protocol-pack-web
  • For telecom applications, include the protocol-pack-telco
  • For database protocol analysis, include the protocol-pack-database

Complete Example POM

Here's a complete example of a pom.xml file for a basic jnetpcap project:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <parent>
        <groupId>com.slytechs.jnet.protocol</groupId>
        <artifactId>protocol-pack-sdk-parent</artifactId>
        <version>0.10.0-SNAPSHOT</version>
    </parent>
    
    <artifactId>my-packet-analyzer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>My Packet Analyzer</name>
    
    <dependencies>
        <!-- Core Protocol Pack API -->
        <dependency>
            <groupId>com.slytechs.jnet.protocol</groupId>
            <artifactId>protocol-pack-api</artifactId>
            <version>0.10.0-SNAPSHOT</version>
        </dependency>
        
        <!-- Add specific protocol packs as needed -->
        <dependency>
            <groupId>com.slytechs.jnet.protocol</groupId>
            <artifactId>protocol-pack-core</artifactId>
            <version>0.10.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Basic Setup

After adding the dependencies, you can start using jnetpcap in your code:

import com.slytechs.jnet.protocol.NetPcap;
import com.slytechs.jnet.protocol.Packet;

public class BasicCapture {
    public static void main(String[] args) {
        NetPcap pcap = NetPcap.openOffline("capture.pcap");
        Packet packet = new Packet();
        
        while (pcap.nextEx(packet)) {
            System.out.println("Packet length: " + packet.length());
        }
        
        pcap.close();
    }
}