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,java.io.Closeable,java.lang.AutoCloseable
public final class PcapWriteHandler extends ChannelDuplexHandler implements java.io.Closeable
PcapWriteHandlercapturesByteBuffromSocketChannel/ServerChannelorDatagramPacketand writes it into PcapOutputStream.Things to keep in mind when using
PcapWriteHandlerwith 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
captureZeroBytetofalse.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPcapWriteHandler.BuilderBuilder forPcapWriteHandlerprivate static classPcapWriteHandler.ChannelTypeprivate static classPcapWriteHandler.WildcardAddressHolder-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private booleancaptureZeroBytetrueif we want to capture packets with zero bytes elsefalse.private PcapWriteHandler.ChannelTypechannelTypeType of the channel this handler is registered onprivate java.net.InetSocketAddresshandlerAddrAddress of the receiver of the connectionprivate java.net.InetSocketAddressinitiatorAddrAddress of the initiator of the connectionprivate booleanisServerPipelineSet totrueif this handler is registered on a server pipelineprivate InternalLoggerloggerLogger for logging eventsprivate java.io.OutputStreamoutputStreamOutputStreamwhere we'll write Pcap data.private PcapWriterpCapWriterPcapWriterInstanceprivate longreceiveSegmentNumberTCP Receiver Segment Number.private longsendSegmentNumberTCP Sender Segment Number.private booleansharedOutputStreamtrueif we want to synchronize on theOutputStreamwhile writing elsefalse.private java.util.concurrent.atomic.AtomicReference<State>stateCurrent of thisPcapWriteHandlerprivate booleanwritePcapGlobalHeader
-
Constructor Summary
Constructors Modifier Constructor Description privatePcapWriteHandler(PcapWriteHandler.Builder builder, java.io.OutputStream outputStream)PcapWriteHandler(java.io.OutputStream outputStream)Deprecated.UsePcapWriteHandler.Builderinstead.PcapWriteHandler(java.io.OutputStream outputStream, boolean captureZeroByte, boolean writePcapGlobalHeader)Deprecated.UsePcapWriteHandler.Builderinstead.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static PcapWriteHandler.Builderbuilder()voidchannelActive(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelRead(ChannelHandlerContext ctx, java.lang.Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidclose()ClosePcapWriterandOutputStream.private voidcompleteTCPWrite(java.net.InetSocketAddress srcAddr, java.net.InetSocketAddress dstAddr, ByteBuf tcpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx)Write TCP/IP L3 and L2 here.private voidcompleteUDPWrite(java.net.InetSocketAddress srcAddr, java.net.InetSocketAddress dstAddr, ByteBuf udpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx)Write UDP/IP L3 and L2 here.voidexceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause)CallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline.private static java.net.InetSocketAddressgetLocalAddress(Channel ch, java.net.InetSocketAddress remote)Get the local address of a channel.voidhandlerRemoved(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.private voidhandleTCP(ChannelHandlerContext ctx, java.lang.Object msg, boolean isWriteOperation)Handle TCP L4private voidhandleTcpPacket(ChannelHandlerContext ctx, ByteBuf packet, boolean isWriteOperation, ByteBufAllocator byteBufAllocator)private voidhandleUDP(ChannelHandlerContext ctx, java.lang.Object msg, boolean isWriteOperation)Handle UDP l4private static longincrementUintSegmentNumber(long sequenceNumber, int value)private voidinitializeIfNecessary(ChannelHandlerContext ctx)booleanisWriting()Returnstrueif thePcapWriteHandleris currently writing packets to theOutputStreamelse returnsfalse.private voidlogDiscard()private voidlogTCP(boolean isWriteOperation, int bytes, long sendSegmentNumber, long receiveSegmentNumber, java.net.InetSocketAddress srcAddr, java.net.InetSocketAddress dstAddr, boolean ackOnly)Logger for TCP(package private) voidmarkClosed()(package private) java.io.OutputStreamoutputStream()voidpause()Pause thePcapWriteHandlerfrom writing packets to theOutputStream.(package private) PcapWriterpCapWriter()voidresume()Resume thePcapWriteHandlerto writing packets to theOutputStream.(package private) booleansharedOutputStream()(package private) Statestate()java.lang.StringtoString()voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.static voidwriteGlobalHeader(java.io.OutputStream outputStream)Writes the Pcap Global Header to the providedOutputStream(package private) booleanwritePcapGlobalHeader()-
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 Detail
-
logger
private final InternalLogger logger
Logger for logging events
-
pCapWriter
private PcapWriter pCapWriter
PcapWriterInstance
-
outputStream
private final java.io.OutputStream outputStream
OutputStreamwhere we'll write Pcap data.
-
captureZeroByte
private final boolean captureZeroByte
trueif we want to capture packets with zero bytes elsefalse.
-
writePcapGlobalHeader
private final boolean writePcapGlobalHeader
-
sharedOutputStream
private final boolean sharedOutputStream
trueif we want to synchronize on theOutputStreamwhile writing elsefalse.
-
sendSegmentNumber
private long sendSegmentNumber
TCP 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 receiveSegmentNumber
TCP Receiver Segment Number. It'll start with 1 and keep incrementing with number of bytes read/sent and wrap at the uint32 max
-
channelType
private PcapWriteHandler.ChannelType channelType
Type of the channel this handler is registered on
-
initiatorAddr
private java.net.InetSocketAddress initiatorAddr
Address of the initiator of the connection
-
handlerAddr
private java.net.InetSocketAddress handlerAddr
Address of the receiver of the connection
-
isServerPipeline
private boolean isServerPipeline
Set totrueif this handler is registered on a server pipeline
-
state
private final java.util.concurrent.atomic.AtomicReference<State> state
Current of thisPcapWriteHandler
-
-
Constructor Detail
-
PcapWriteHandler
@Deprecated public PcapWriteHandler(java.io.OutputStream outputStream)
Deprecated.UsePcapWriteHandler.Builderinstead.Create newPcapWriteHandlerInstance.captureZeroByteis set tofalseandwritePcapGlobalHeaderis set totrue.- Parameters:
outputStream- OutputStream where Pcap data will be written. Callclose()to close this OutputStream.- Throws:
java.lang.NullPointerException- IfOutputStreamisnullthen we'll throw anNullPointerException
-
PcapWriteHandler
@Deprecated public PcapWriteHandler(java.io.OutputStream outputStream, boolean captureZeroByte, boolean writePcapGlobalHeader)Deprecated.UsePcapWriteHandler.Builderinstead.Create newPcapWriteHandlerInstance- Parameters:
outputStream- OutputStream where Pcap data will be written. Callclose()to close this OutputStream.captureZeroByte- Set totrueto enable capturing packets with empty (0 bytes) payload. Otherwise, if set tofalse, empty packets will be filtered out.writePcapGlobalHeader- Set totrueto 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:
java.lang.NullPointerException- IfOutputStreamisnullthen we'll throw anNullPointerException
-
PcapWriteHandler
private PcapWriteHandler(PcapWriteHandler.Builder builder, java.io.OutputStream outputStream)
-
-
Method Detail
-
writeGlobalHeader
public static void writeGlobalHeader(java.io.OutputStream outputStream) throws java.io.IOExceptionWrites the Pcap Global Header to the providedOutputStream- Parameters:
outputStream- OutputStream where Pcap data will be written.- Throws:
java.io.IOException- if there is an error writing to theOutputStream
-
initializeIfNecessary
private void initializeIfNecessary(ChannelHandlerContext ctx) throws java.lang.Exception
- Throws:
java.lang.Exception
-
channelActive
public void channelActive(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelActivein interfaceChannelInboundHandler- Overrides:
channelActivein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
handleTCP
private void handleTCP(ChannelHandlerContext ctx, java.lang.Object msg, boolean isWriteOperation)
Handle TCP L4- Parameters:
ctx-ChannelHandlerContextforByteBufallocation andfireExceptionCaughtmsg-Objectmust beByteBufelse it'll be discardedisWriteOperation- Settrueif 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(java.net.InetSocketAddress srcAddr, java.net.InetSocketAddress dstAddr, ByteBuf tcpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx)Write TCP/IP L3 and L2 here.- Parameters:
srcAddr-InetSocketAddressSource Address of this PacketdstAddr-InetSocketAddressDestination Address of this PackettcpBuf-ByteBufcontaining TCP L4 DatabyteBufAllocator-ByteBufAllocatorfor allocating bytes for TCP/IP L3 and L2 data.ctx-ChannelHandlerContextforfireExceptionCaught
-
incrementUintSegmentNumber
private static long incrementUintSegmentNumber(long sequenceNumber, int value)
-
handleUDP
private void handleUDP(ChannelHandlerContext ctx, java.lang.Object msg, boolean isWriteOperation)
Handle UDP l4- Parameters:
ctx-ChannelHandlerContextforlocalAddress/remoteAddress,ByteBufallocation andfireExceptionCaughtmsg-DatagramPacketorByteBuf
-
completeUDPWrite
private void completeUDPWrite(java.net.InetSocketAddress srcAddr, java.net.InetSocketAddress dstAddr, ByteBuf udpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx)Write UDP/IP L3 and L2 here.- Parameters:
srcAddr-InetSocketAddressSource Address of this PacketdstAddr-InetSocketAddressDestination Address of this PacketudpBuf-ByteBufcontaining UDP L4 DatabyteBufAllocator-ByteBufAllocatorfor allocating bytes for UDP/IP L3 and L2 data.ctx-ChannelHandlerContextforfireExceptionCaught
-
getLocalAddress
private static java.net.InetSocketAddress getLocalAddress(Channel ch, java.net.InetSocketAddress remote)
Get the local address of a channel. If the address is a wildcard address (0.0.0.0or::), 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
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaughtin interfaceChannelHandler- Specified by:
exceptionCaughtin interfaceChannelInboundHandler- Overrides:
exceptionCaughtin classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
logTCP
private void logTCP(boolean isWriteOperation, int bytes, long sendSegmentNumber, long receiveSegmentNumber, java.net.InetSocketAddress srcAddr, java.net.InetSocketAddress dstAddr, boolean ackOnly)Logger for TCP
-
outputStream
java.io.OutputStream outputStream()
-
sharedOutputStream
boolean sharedOutputStream()
-
writePcapGlobalHeader
boolean writePcapGlobalHeader()
-
isWriting
public boolean isWriting()
Returnstrueif thePcapWriteHandleris currently writing packets to theOutputStreamelse returnsfalse.
-
state
State state()
-
pause
public void pause()
Pause thePcapWriteHandlerfrom writing packets to theOutputStream.
-
resume
public void resume()
Resume thePcapWriteHandlerto writing packets to theOutputStream.
-
markClosed
void markClosed()
-
pCapWriter
PcapWriter pCapWriter()
-
logDiscard
private void logDiscard()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
close
public void close() throws java.io.IOExceptionClose
PcapWriterandOutputStream.Note: Calling this method does not close
PcapWriteHandler. Only Pcap Writes are closed.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException- IfOutputStream.close()throws an exception
-
builder
public static PcapWriteHandler.Builder builder()
-
-