Class Pcap

java.lang.Object
pcap.api.internal.Pcap
All Implemented Interfaces:
java.lang.AutoCloseable, pcap.spi.Pcap

@Inclubating
public class Pcap
extends java.lang.Object
implements pcap.spi.Pcap
Pcap handle.
Author:
Ardika Rommy Sanjaya
  • Constructor Summary

    Constructors 
    Constructor Description
    Pcap​(java.foreign.memory.Pointer<pcap_mapping.pcap> pcap)  
    Pcap​(java.foreign.memory.Pointer<pcap_mapping.pcap> pcap, int netmask)  
  • Method Summary

    Modifier and Type Method Description
    void breakLoop()
    Force a loop(int, PacketHandler, Object) call to return And throw BreakException on loop(int, PacketHandler, Object).
    void close()
    pcap.spi.Dumper dumpOpen​(java.lang.String file)
    Open Dumper handler.
    pcap.spi.Dumper dumpOpenAppend​(java.lang.String file)
    Append packet buffer on existing pcap file.
    <T> void loop​(int count, pcap.spi.PacketHandler<T> handler, T args)
    Process packets from a live PcapLive or PcapOffline.
    void send​(java.nio.ByteBuffer buffer)
    Sends a raw packet through the network interface.
    void send​(java.nio.ByteBuffer buffer, int size)
    Sends a raw packet through the network interface.
    void setFilter​(java.lang.String filter, boolean optimize)
    BPF packet filter.
    pcap.spi.Status status()
    Represent packet statistics from the start of the run to the time of the call.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • dumpOpen

      public pcap.spi.Dumper dumpOpen​(java.lang.String file) throws pcap.spi.exception.ErrorException
      Open Dumper handler.
      Specified by:
      dumpOpen in interface pcap.spi.Pcap
      Parameters:
      file - location of capture file will saved.
      Returns:
      returns Pcap Dumper handle.
      Throws:
      pcap.spi.exception.ErrorException
    • dumpOpenAppend

      public pcap.spi.Dumper dumpOpenAppend​(java.lang.String file) throws pcap.spi.exception.ErrorException
      Append packet buffer on existing pcap file.
      Specified by:
      dumpOpenAppend in interface pcap.spi.Pcap
      Parameters:
      file - location of saved file.
      Returns:
      returns Pcap Dumper handle.
      Throws:
      pcap.spi.exception.ErrorException - generic error.
    • setFilter

      public void setFilter​(java.lang.String filter, boolean optimize) throws pcap.spi.exception.ErrorException
      BPF packet filter.
      Specified by:
      setFilter in interface pcap.spi.Pcap
      Parameters:
      filter - filter expression.
      optimize - true for optimized filter, false otherwise.
      Throws:
      pcap.spi.exception.ErrorException - generic error.
    • loop

      public <T> void loop​(int count, pcap.spi.PacketHandler<T> handler, T args) throws pcap.spi.exception.error.BreakException, pcap.spi.exception.ErrorException
      Process packets from a live PcapLive or PcapOffline.
      Specified by:
      loop in interface pcap.spi.Pcap
      Type Parameters:
      T - args type.
      Parameters:
      count - maximum number of packets to process before returning. A value of -1 or 0 for cnt is equivalent to infinity, so that packets are processed until another ending condition occurs.
      handler - PacketHandler callback function.
      args - user args.
      Throws:
      pcap.spi.exception.error.BreakException - breakLoop() is called.
      pcap.spi.exception.ErrorException - Generic error.
    • status

      public pcap.spi.Status status() throws pcap.spi.exception.ErrorException
      Represent packet statistics from the start of the run to the time of the call.

      Supported only on live captures, not on PcapOffline; no statistics are stored in PcapOffline so no statistics are available when reading from a PcapOffline

      Specified by:
      status in interface pcap.spi.Pcap
      Returns:
      returns Status on success.
      Throws:
      pcap.spi.exception.ErrorException - There is an error or if this Pcap doesn't support packet statistics.
    • breakLoop

      public void breakLoop()
      Force a loop(int, PacketHandler, Object) call to return And throw BreakException on loop(int, PacketHandler, Object).
      Specified by:
      breakLoop in interface pcap.spi.Pcap
    • send

      public void send​(java.nio.ByteBuffer buffer) throws pcap.spi.exception.ErrorException
      Sends a raw packet through the network interface.
      Parameters:
      buffer - the data of the packet, including the link-layer header.
      Throws:
      pcap.spi.exception.ErrorException - generic error.
    • send

      public void send​(java.nio.ByteBuffer buffer, int size) throws pcap.spi.exception.ErrorException
      Sends a raw packet through the network interface.
      Specified by:
      send in interface pcap.spi.Pcap
      Parameters:
      buffer - the data of the packet, including the link-layer header.
      size - the number of bytes in the packet.
      Throws:
      pcap.spi.exception.ErrorException - generic error.
    • close

      public void close()
      Close PcapLive or PcapOffline. Note: BPF handle will closed automaticly.
      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface pcap.spi.Pcap