Class PcapWriteHandler

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, io.netty.channel.ChannelOutboundHandler, Closeable, AutoCloseable

    public final class PcapWriteHandler
    extends io.netty.channel.ChannelDuplexHandler
    implements Closeable

    PcapWriteHandler captures ByteBuf from SocketChannel / ServerChannel or DatagramPacket and writes it into Pcap OutputStream.

    Things to keep in mind when using PcapWriteHandler with TCP:

    • Whenever ChannelInboundHandlerAdapter.channelActive(ChannelHandlerContext) is called, a fake TCP 3-way handshake (SYN, SYN+ACK, ACK) is simulated as new connection in Pcap.
    • Whenever ChannelHandlerAdapter.handlerRemoved(ChannelHandlerContext) is called, a fake TCP 3-way handshake (FIN+ACK, FIN+ACK, ACK) is simulated as connection shutdown in Pcap.
    • Whenever ChannelInboundHandlerAdapter.exceptionCaught(ChannelHandlerContext, Throwable) is called, a fake TCP RST is sent to simulate connection Reset in Pcap.
    • ACK is sent each time data is send / received.
    • Zero Length Data Packets can cause TCP Double ACK error in Wireshark. To tackle this, set captureZeroByte to false.

    • Constructor Detail

      • PcapWriteHandler

        @Deprecated
        public PcapWriteHandler​(OutputStream outputStream,
                                boolean captureZeroByte,
                                boolean writePcapGlobalHeader)
        Deprecated.
        Create new PcapWriteHandler Instance
        Parameters:
        outputStream - OutputStream where Pcap data will be written. Call close() to close this OutputStream.
        captureZeroByte - Set to true to enable capturing packets with empty (0 bytes) payload. Otherwise, if set to false, empty packets will be filtered out.
        writePcapGlobalHeader - Set to true to write Pcap Global Header on initialization. Otherwise, if set to false, Pcap Global Header will not be written on initialization. This could when writing Pcap data on a existing file where Pcap Global Header is already present.
        Throws:
        NullPointerException - If OutputStream is null then we'll throw an NullPointerException
    • Method Detail

      • writeGlobalHeader

        public static void writeGlobalHeader​(OutputStream outputStream)
                                      throws IOException
        Writes the Pcap Global Header to the provided OutputStream
        Parameters:
        outputStream - OutputStream where Pcap data will be written.
        Throws:
        IOException - if there is an error writing to the OutputStream
      • channelActive

        public void channelActive​(io.netty.channel.ChannelHandlerContext ctx)
                           throws Exception
        Specified by:
        channelActive in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelActive in class io.netty.channel.ChannelInboundHandlerAdapter
        Throws:
        Exception
      • channelRead

        public void channelRead​(io.netty.channel.ChannelHandlerContext ctx,
                                Object msg)
                         throws Exception
        Specified by:
        channelRead in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelRead in class io.netty.channel.ChannelInboundHandlerAdapter
        Throws:
        Exception
      • write

        public void write​(io.netty.channel.ChannelHandlerContext ctx,
                          Object msg,
                          io.netty.channel.ChannelPromise promise)
                   throws Exception
        Specified by:
        write in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        write in class io.netty.channel.ChannelDuplexHandler
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • exceptionCaught

        public void exceptionCaught​(io.netty.channel.ChannelHandlerContext ctx,
                                    Throwable cause)
                             throws Exception
        Specified by:
        exceptionCaught in interface io.netty.channel.ChannelHandler
        Specified by:
        exceptionCaught in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        exceptionCaught in class io.netty.channel.ChannelInboundHandlerAdapter
        Throws:
        Exception
      • isWriting

        public boolean isWriting()
        Returns true if the PcapWriteHandler is currently writing packets to the OutputStream else returns false.