Package io.netty.handler.pcap
Class PcapWriteHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.pcap.PcapWriteHandler
-
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
,Closeable
,AutoCloseable
public final class PcapWriteHandler extends ChannelDuplexHandler implements Closeable
PcapWriteHandler
capturesByteBuf
fromSocketChannel
/ServerChannel
orDatagramPacket
and writes it into PcapOutputStream
.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
tofalse
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Constructor Summary
Constructors Constructor Description PcapWriteHandler(OutputStream outputStream)
Create newPcapWriteHandler
Instance.PcapWriteHandler(OutputStream outputStream, boolean captureZeroByte, boolean writePcapGlobalHeader)
Create newPcapWriteHandler
Instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelActive(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelRead(ChannelHandlerContext ctx, Object msg)
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
close()
ClosePcapWriter
andOutputStream
.void
exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
CallsChannelHandlerContext.fireExceptionCaught(Throwable)
to forward to the nextChannelHandler
in theChannelPipeline
.void
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.void
write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded
-
-
-
-
Constructor Detail
-
PcapWriteHandler
public PcapWriteHandler(OutputStream outputStream)
Create newPcapWriteHandler
Instance.captureZeroByte
is set tofalse
andwritePcapGlobalHeader
is set totrue
.- Parameters:
outputStream
- OutputStream where Pcap data will be written. Callclose()
to close this OutputStream.- Throws:
NullPointerException
- IfOutputStream
isnull
then we'll throw anNullPointerException
-
PcapWriteHandler
public PcapWriteHandler(OutputStream outputStream, boolean captureZeroByte, boolean writePcapGlobalHeader)
Create newPcapWriteHandler
Instance- Parameters:
outputStream
- OutputStream where Pcap data will be written. Callclose()
to close this OutputStream.captureZeroByte
- Set totrue
to enable capturing packets with empty (0 bytes) payload. Otherwise, if set tofalse
, empty packets will be filtered out.writePcapGlobalHeader
- Set totrue
to write Pcap Global Header on initialization. Otherwise, if set tofalse
, 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
- IfOutputStream
isnull
then we'll throw anNullPointerException
-
-
Method Detail
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelActive
in interfaceChannelInboundHandler
- Overrides:
channelActive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
write
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireExceptionCaught(Throwable)
to forward to the nextChannelHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
close
public void close() throws IOException
Close
PcapWriter
andOutputStream
.Note: Calling this method does not close
PcapWriteHandler
. Only Pcap Writes are closed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- IfOutputStream.close()
throws an exception
-
-