Pcap
  • Introduction
  • Getting Started
    • Dependency management
    • Installing Libpcap or Npcap
    • Run as non root user
    • Logging
    • Obtaining the device list
    • Opening an adapter and capturing the packets
    • Capturing the packets without the callback
    • Capture statistics
    • Filtering the traffic
    • Write the packets to a capture file
    • Read packets from a capture file.
    • Sending packets
    • I/O Multiplexing
    • Restricted Method
  • Developer Guide
    • Branches to look
    • Build from Source
    • Notes
  • Packet Structure
    • Packet Header
    • Packet Buffer
  • Packet Codec
    • Using packet codec
    • Adding protocol support
  • Others
  • Thanks to
  • Fork me on Github
Powered by GitBook
On this page

Was this helpful?

  1. Packet Codec

Using packet codec

How to use packet codec

Example of working with packet codec.

PacketBuffer packetBuffer = ..;

Ethernet ethernet = packetBuffer.cast(Ethernet.class);
System.out.println(ethernet);
if (ethernet.type() == Ip4.TYPE) {
  Ip4 ip4 = packetBuffer.readerIndex(ethernet.size()).cast(Ip4.class);
  System.out.println(ip4);
  if (ip4.protocol() == Tcp.TYPE) {
    Tcp tcp = packetBuffer.readerIndex(ethernet.size() + ip4.size()).cast(Tcp.class);
    System.out.println(tcp);
  } else if (ip4.protocol() == Udp.TYPE) {
    Udp udp = packetBuffer.readerIndex(ethernet.size() + ip4.size()).cast(Udp.class);
    System.out.println(udp);
  }
} else if (ethernet.type() == Ip6.TYPE) {
  Ip6 ip6 = packetBuffer.readerIndex(ethernet.size()).cast(Ip6.class);
  System.out.println(ip6);
  if (ip6.nextHeader() == Tcp.TYPE) {
    Tcp tcp = packetBuffer.readerIndex(ethernet.size() + ip6.size()).cast(Tcp.class);
    System.out.println(tcp);
  } else if (ip6.nextHeader() == Udp.TYPE) {
    Udp udp = packetBuffer.readerIndex(ethernet.size() + ip6.size()).cast(Udp.class);
    System.out.println(udp);
  }
}
PreviousPacket CodecNextAdding protocol support

Last updated 4 years ago

Was this helpful?