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);
  }
}

Last updated