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
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
tofalse
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Builder forPcapWriteHandler
private static enum
private static final class
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
true
if we want to capture packets with zero bytes elsefalse
.private PcapWriteHandler.ChannelType
Type of the channel this handler is registered onprivate InetSocketAddress
Address of the receiver of the connectionprivate InetSocketAddress
Address of the initiator of the connectionprivate boolean
Set totrue
if this handler is registered on a server pipelineprivate final InternalLogger
Logger for logging eventsprivate final OutputStream
OutputStream
where we'll write Pcap data.private PcapWriter
PcapWriter
Instanceprivate long
TCP Receiver Segment Number.private long
TCP Sender Segment Number.private final boolean
private final AtomicReference
<State> Current of thisPcapWriteHandler
private final boolean
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
PcapWriteHandler
(PcapWriteHandler.Builder builder, OutputStream outputStream) PcapWriteHandler
(OutputStream outputStream) Deprecated.PcapWriteHandler
(OutputStream outputStream, boolean captureZeroByte, boolean writePcapGlobalHeader) Deprecated.UsePcapWriteHandler.Builder
instead. -
Method Summary
Modifier and TypeMethodDescriptionstatic PcapWriteHandler.Builder
builder()
void
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
.private void
completeTCPWrite
(InetSocketAddress srcAddr, InetSocketAddress dstAddr, ByteBuf tcpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx) Write TCP/IP L3 and L2 here.private void
completeUDPWrite
(InetSocketAddress srcAddr, InetSocketAddress dstAddr, ByteBuf udpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx) Write UDP/IP L3 and L2 here.void
exceptionCaught
(ChannelHandlerContext ctx, Throwable cause) CallsChannelHandlerContext.fireExceptionCaught(Throwable)
to forward to the nextChannelHandler
in theChannelPipeline
.private static InetSocketAddress
getLocalAddress
(Channel ch, InetSocketAddress remote) Get the local address of a channel.void
Do nothing by default, sub-classes may override this method.private void
handleTCP
(ChannelHandlerContext ctx, Object msg, boolean isWriteOperation) Handle TCP L4private void
handleTcpPacket
(ChannelHandlerContext ctx, ByteBuf packet, boolean isWriteOperation, ByteBufAllocator byteBufAllocator) private void
handleUDP
(ChannelHandlerContext ctx, Object msg, boolean isWriteOperation) Handle UDP l4private static long
incrementUintSegmentNumber
(long sequenceNumber, int value) private void
boolean
Returnstrue
if thePcapWriteHandler
is currently writing packets to theOutputStream
else returnsfalse
.private void
private void
logTCP
(boolean isWriteOperation, int bytes, long sendSegmentNumber, long receiveSegmentNumber, InetSocketAddress srcAddr, InetSocketAddress dstAddr, boolean ackOnly) Logger for TCP(package private) void
(package private) OutputStream
void
pause()
Pause thePcapWriteHandler
from writing packets to theOutputStream
.(package private) PcapWriter
void
resume()
Resume thePcapWriteHandler
to writing packets to theOutputStream
.(package private) boolean
(package private) State
state()
toString()
void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.static void
writeGlobalHeader
(OutputStream outputStream) Writes the Pcap Global Header to the providedOutputStream
(package private) boolean
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, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded
-
Field Details
-
logger
Logger for logging events -
pCapWriter
PcapWriter
Instance -
outputStream
OutputStream
where we'll write Pcap data. -
captureZeroByte
private final boolean captureZeroBytetrue
if we want to capture packets with zero bytes elsefalse
. -
writePcapGlobalHeader
private final boolean writePcapGlobalHeader -
sendSegmentNumber
private long sendSegmentNumberTCP Sender Segment Number. It'll start with 1 and keep incrementing with number of bytes read/sent and wrap at the uint32 max. -
receiveSegmentNumber
private long receiveSegmentNumberTCP Receiver Segment Number. It'll start with 1 and keep incrementing with number of bytes read/sent and wrap at the uint32 max -
channelType
Type of the channel this handler is registered on -
initiatorAddr
Address of the initiator of the connection -
handlerAddr
Address of the receiver of the connection -
isServerPipeline
private boolean isServerPipelineSet totrue
if this handler is registered on a server pipeline -
state
Current of thisPcapWriteHandler
-
-
Constructor Details
-
PcapWriteHandler
Deprecated.UsePcapWriteHandler.Builder
instead.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
@Deprecated public PcapWriteHandler(OutputStream outputStream, boolean captureZeroByte, boolean writePcapGlobalHeader) Deprecated.UsePcapWriteHandler.Builder
instead.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
-
PcapWriteHandler
-
-
Method Details
-
writeGlobalHeader
Writes the Pcap Global Header to the providedOutputStream
- Parameters:
outputStream
- OutputStream where Pcap data will be written.- Throws:
IOException
- if there is an error writing to theOutputStream
-
initializeIfNecessary
- Throws:
Exception
-
channelActive
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
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
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
-
handleTCP
Handle TCP L4- Parameters:
ctx
-ChannelHandlerContext
forByteBuf
allocation andfireExceptionCaught
msg
-Object
must beByteBuf
else it'll be discardedisWriteOperation
- Settrue
if we have to process packet when packets are being sent out else setfalse
-
handleTcpPacket
private void handleTcpPacket(ChannelHandlerContext ctx, ByteBuf packet, boolean isWriteOperation, ByteBufAllocator byteBufAllocator) -
completeTCPWrite
private void completeTCPWrite(InetSocketAddress srcAddr, InetSocketAddress dstAddr, ByteBuf tcpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx) Write TCP/IP L3 and L2 here.- Parameters:
srcAddr
-InetSocketAddress
Source Address of this PacketdstAddr
-InetSocketAddress
Destination Address of this PackettcpBuf
-ByteBuf
containing TCP L4 DatabyteBufAllocator
-ByteBufAllocator
for allocating bytes for TCP/IP L3 and L2 data.ctx
-ChannelHandlerContext
forfireExceptionCaught
-
incrementUintSegmentNumber
private static long incrementUintSegmentNumber(long sequenceNumber, int value) -
handleUDP
Handle UDP l4- Parameters:
ctx
-ChannelHandlerContext
forlocalAddress
/remoteAddress
,ByteBuf
allocation andfireExceptionCaught
msg
-DatagramPacket
orByteBuf
-
completeUDPWrite
private void completeUDPWrite(InetSocketAddress srcAddr, InetSocketAddress dstAddr, ByteBuf udpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx) Write UDP/IP L3 and L2 here.- Parameters:
srcAddr
-InetSocketAddress
Source Address of this PacketdstAddr
-InetSocketAddress
Destination Address of this PacketudpBuf
-ByteBuf
containing UDP L4 DatabyteBufAllocator
-ByteBufAllocator
for allocating bytes for UDP/IP L3 and L2 data.ctx
-ChannelHandlerContext
forfireExceptionCaught
-
getLocalAddress
Get the local address of a channel. If the address is a wildcard address (0.0.0.0
or::
), and the address family does not match that of theremote
, return the wildcard address of theremote
's family instead.- Parameters:
ch
- The channel to get the local address fromremote
- The remote address- Returns:
- The fixed local address
-
handlerRemoved
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
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
-
logTCP
private void logTCP(boolean isWriteOperation, int bytes, long sendSegmentNumber, long receiveSegmentNumber, InetSocketAddress srcAddr, InetSocketAddress dstAddr, boolean ackOnly) Logger for TCP -
outputStream
OutputStream outputStream() -
writePcapGlobalHeader
boolean writePcapGlobalHeader() -
isWriting
public boolean isWriting()Returnstrue
if thePcapWriteHandler
is currently writing packets to theOutputStream
else returnsfalse
. -
state
State state() -
pause
public void pause()Pause thePcapWriteHandler
from writing packets to theOutputStream
. -
resume
public void resume()Resume thePcapWriteHandler
to writing packets to theOutputStream
. -
markClosed
void markClosed() -
pCapWriter
PcapWriter pCapWriter() -
logDiscard
private void logDiscard() -
toString
-
close
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
-
builder
-
PcapWriteHandler.Builder
instead.