Module org.jnetpcap
Package org.jnetpcap

Class PcapHeader

java.lang.Object
org.jnetpcap.PcapHeader

public final class PcapHeader extends Object
PcapHeader relies on preview features of the Java platform:
Programs can only use PcapHeader when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
A Pcap packet header also called a descriptor that precedes each packet. The pcap header is supplied natively by libpcap library and PcapHeader can bind to native memory without copy to access its fields.

PcapHeader supplies vital information about the captured packet. These fields disclaim the length of the data capture and if a packet was truncated with snaplen parameter on the Pcap handle, the packet length as originally seen on the wire. Additionally a timestamp of the instant when the packet was captured.

Author:
Sly Technologies Inc, repos@slytechs.com
See Also:
  • Constructor Details

    • PcapHeader

      public PcapHeader(ByteBuffer headerBuffer)
      Instantiates a new pcap header.
      Parameters:
      headerBuffer - the header buffer
    • PcapHeader

      public PcapHeader(int tvSec, int tvUsec, int captureLength, int wireLength)
      Instantiates a new pcap header.
      Parameters:
      tvSec - the tv sec
      tvUsec - the tv usec
      captureLength - the capture length
      wireLength - the wire length
    • PcapHeader

      public PcapHeader(MemorySegmentPREVIEW mseg)
      Instantiates a new pcap header.
      Parameters:
      mseg - the struct pcap_pkthdr memory address.
    • PcapHeader

      public PcapHeader(MemorySegmentPREVIEW mseg, boolean isSwapped)
      Instantiates a new pcap header.
      Parameters:
      mseg - the struct pcap_pkthdr memory address.
      isSwapped - the flag indicates that the header is referencing memory for an offline capture and that the header field byte may be swapped, as recorded based on the original capture system architecture.
    • PcapHeader

      public PcapHeader(MemorySegmentPREVIEW mseg, boolean isSwapped, boolean isCompactAbi)
      Instantiates a new pcap header.
      Parameters:
      mseg - the struct pcap_pkthdr memory address.
      isSwapped - the flag indicates that the header is referencing memory for an offline capture and that the header field byte may be swapped, as recorded based on the original capture system architecture. *
      isCompactAbi - if true, forces the pcap binary interface to use the compact form.
      See Also:
  • Method Details

    • captureLength

      public static int captureLength(ByteBuffer headerBuffer) throws PcapHeaderException.OutOfRangeException
      Packet capture length.
      Parameters:
      headerBuffer - the buffer containing pcap header contents
      Returns:
      the number of packet bytes captured
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • captureLength

      public static int captureLength(ByteBuffer headerBuffer, boolean isSwapped) throws PcapHeaderException.OutOfRangeException
      Packet capture length for offline file capture.
      Parameters:
      headerBuffer - the buffer containing pcap header contents
      isSwapped - for offline files which were captured according to the capturing system ABI, the bytes written maybe swapped for this system's architecture.
      Returns:
      the number of packet bytes captured
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • captureLength

      public static int captureLength(MemorySegmentPREVIEW headerSegment) throws PcapHeaderException.OutOfRangeException
      Packet capture length for offline file capture.
      Parameters:
      headerSegment - the memory segment containing pcap header contents
      Returns:
      the number of packet bytes captured
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • captureLength

      public static int captureLength(MemorySegmentPREVIEW headerSegment, boolean isSwapped) throws PcapHeaderException.OutOfRangeException
      Packet capture length for offline file capture.
      Parameters:
      headerSegment - the memory segment containing pcap header contents
      isSwapped - for offline files which were captured according to the capturing system ABI, the bytes written maybe swapped for this system's architecture.
      Returns:
      the number of packet bytes captured
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • wireLength

      public static int wireLength(ByteBuffer headerBuffer) throws PcapHeaderException.OutOfRangeException
      Packet wire length .
      Parameters:
      headerBuffer - the buffer containing pcap header contents
      Returns:
      the number of packet bytes originally seen on the network wire/wireless before truncation due to "snaplen" parameter if set.
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • wireLength

      public static int wireLength(ByteBuffer headerBuffer, boolean isSwapped) throws PcapHeaderException.OutOfRangeException
      Packet wire length for offline file capture.
      Parameters:
      headerBuffer - the buffer containing pcap header contents
      isSwapped - for offline files which were captured according to the capturing system ABI, the bytes written maybe swapped for this system's architecture.
      Returns:
      the number of packet bytes originally seen on the network wire/wireless before truncation due to "snaplen" parameter if set.
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • wireLength

      public static int wireLength(MemorySegmentPREVIEW headerSegment) throws PcapHeaderException.OutOfRangeException
      Packet wire length.
      Parameters:
      headerSegment - the memory segment containing pcap header contents
      Returns:
      the number of packet bytes originally seen on the network wire/wireless before truncation due to "snaplen" parameter if set.
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • wireLength

      public static int wireLength(MemorySegmentPREVIEW headerSegment, boolean isSwapped) throws PcapHeaderException.OutOfRangeException
      Packet wire length for offline file capture.
      Parameters:
      headerSegment - the memory segment containing pcap header contents
      isSwapped - for offline files which were captured according to the capturing system ABI, the bytes written maybe swapped for this system's architecture.
      Returns:
      the number of packet bytes originally seen on the network wire/wireless before truncation due to "snaplen" parameter if set.
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception, typically indicates invalid ABI (Application Binary Interface) setting which is platform and offline capture file dependent
    • asMemoryReference

      public MemorySegmentPREVIEW asMemoryReference()
      As memory reference.
      Returns:
      the memory address
    • asMemorySegment

      public MemorySegmentPREVIEW asMemorySegment()
      As memory segment.
      Returns:
      the memory segment
    • captureLength

      public int captureLength() throws PcapHeaderException.OutOfRangeException
      Capture length.
      Returns:
      the int
      Throws:
      PcapHeaderException.OutOfRangeException - the out of range exception
    • headerLength

      public int headerLength()
      The length of this header.
      Returns:
      the length of this header in bytes.
    • setNanoTimePrecision

      public PcapHeader setNanoTimePrecision(boolean nanoTime)
      Sets a flag if the timestamp for this header is calculated using nanosecond or the default microsecond precision.
      Parameters:
      nanoTime - if true, a call to timestamp() or toEpochMilli() will use nano second precision
      Returns:
      this pcap header
    • timestamp

      public long timestamp() throws PcapHeaderException
      Timestamp with 32-bit seconds (MSB bits) and 32-bit usecs (LSB bits) from a base of January 1, 1970.
      Returns:
      the long
      Throws:
      PcapHeaderException - the pcap header exception
    • timestamp

      public long timestamp(boolean nanoTime) throws PcapHeaderException
      Timestamp in either nano or milli second precision.
      Parameters:
      nanoTime - if true, timestamp is returned with nano second precision, otherwise millis is returned
      Returns:
      the timestamp value as 64-bit long
      Throws:
      PcapHeaderException - the pcap header exception
    • toEpochMilli

      public long toEpochMilli() throws PcapHeaderException
      To epoch milli.
      Returns:
      the long
      Throws:
      PcapHeaderException - the pcap header exception
    • toEpochMilli

      public long toEpochMilli(boolean nanoTime) throws PcapHeaderException
      To epoch milli.
      Parameters:
      nanoTime - the nano time
      Returns:
      the long
      Throws:
      PcapHeaderException - the pcap header exception
    • toString

      public String toString()
      To string.
      Overrides:
      toString in class Object
      Returns:
      the string
      See Also:
    • tvSec

      public long tvSec() throws PcapHeaderException
      Tv sec.
      Returns:
      the int
      Throws:
      PcapHeaderException - the pcap header exception
    • tvUsec

      public long tvUsec() throws PcapHeaderException
      Tv usec.
      Returns:
      the int
      Throws:
      PcapHeaderException - the pcap header exception
    • wireLength

      public int wireLength() throws PcapHeaderException
      Wire length.
      Returns:
      the int
      Throws:
      PcapHeaderException - the pcap header exception