Introduction

Pcap provides a packet processing library for rapid development on top of JVM languages.

Hello Pcap!

"Hello pcap!" implementation in different JVM languages.

Java
Kotlin
Scala
Groovy
Clojure
JRuby
Jython
Java
var service = Service.Creator.create("PcapService");
try (var pcap = service.live(service.interfaces(), new DefaultLiveOptions())) {
pcap.loop(10, (args, header, buffer) -> {
System.out.println("Args : " + args);
System.out.println("Header : " + header);
System.out.println("Packet : " + buffer);
}, "Hello pcap!");
}
Kotlin
val service = Service.Creator.create("PcapService")
val pcap = service.live(service.interfaces(), DefaultLiveOptions())
pcap.loop(10, { args, header, buffer ->
println("Args : $args")
println("Header : $header")
println("Packet : $buffer")
}, "Hello pcap!")
pcap.close()
Scala
val service = Service.Creator.create("PcapService")
val pcap = service.live(service.interfaces(), new DefaultLiveOptions())
pcap.loop(10, (args: String, header: PacketHeader, buffer: PacketBuffer) => {
println("Args : ", args)
println("Header : ", header)
println("Packet : ", buffer)
}, "Hello pcap!")
pcap.close()
Groovy
def service = Service.Creator.create("PcapService")
def pcap = service.live(service.interfaces(), new DefaultLiveOptions())
pcap.loop(10, { args, header, buffer ->
println("Args : " + args)
println("Header : " + header)
println("Packet : " + buffer)
}, "Hello pcap!");
pcap.close()
Clojure
(def service (. Service$Creator (create "PcapService")))
(def pcap (.live service (.interfaces service) (DefaultLiveOptions.)))
(def handler (reify PacketHandler (gotPacket [this args header buffer]
(println "Args : " args)
(println "Header : " header)
(println "Buffer : " buffer))))
(.loop pcap 10 handler "Hello pcap!")
(.close pcap))
JRuby
class Callback
def gotPacket(args, header, buffer)
puts "Args : " + args
puts "Header : " + header.toString
puts "Buffer : " + buffer.toString
end
end
service = Service::Creator.create('PcapService')
pcap = service.live(service.interfaces(), DefaultLiveOptions.new)
pcap.loop(10, Callback.new, "Hello pcap!")
pcap.close()
Jython
class Callback(PacketHandler):
def gotPacket(self, args, hdr, buf):
print("Args : ", args)
print("Header : ", hdr)
print("Buffer : ", buf)
service = DefaultService()
pcap = service.live(service.interfaces(), DefaultLiveOptions())
pcap.loop(10, Callback(), "Hello pcap!")
pcap.close()

Versioning

Pcap is using semantic versioning: https://semver.org

Version

Status

Release Date

Static Analysis

1.0

Maintained

2020-12-24

Sonarcloud

1.1

Broken

2021-02-01

Sonarcloud

1.2

Maintained

2021-02-01

Sonarcloud

Supported platforms

  • Linux (Tested)

  • Mac OS (Tested)

  • Windows (Tested)

  • Other OSes that supported by both Libpcap and JNA (Untested).

Requirements

  • Java 7+

  • Libpcap for Unix or Npcap for Windows

Java module system with java 9+

Pcap can be used in modular java 9+ applications with specific module name listed below:

  • pcap-spi: pcap.spi

  • pcap-jdk7: pcap.jdk7

  • pcap-common: pcap.common

  • pcap-codec: pcap.codec

Disclimers

  • Every types, methods, or fields annotated with @Incubating might be changed or removed at any time.

  • Every types, methods, or fields annotated with @Incubating does not imply that the functionality being tested.