Top Description Inners Fields Constructors Methods
io.netty.handler.pcap

public final Class PcapWriteHandler

extends ChannelDuplexHandler
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
io.netty.buffer.ByteBuf, .ByteBufAllocator, io.netty.channel.Channel, .ChannelDuplexHandler, .ChannelHandlerContext, .ChannelInboundHandlerAdapter, .ChannelPromise, .ServerChannel, io.netty.channel.socket.DatagramChannel, .DatagramPacket, .ServerSocketChannel, .SocketChannel, io.netty.util.NetUtil, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.io.Closeable, .IOException, .OutputStream, java.net.Inet4Address, .Inet6Address, .InetAddress, .InetSocketAddress, .UnknownHostException, java.util.concurrent.atomic.AtomicReference

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

Things to keep in mind when using PcapWriteHandler with TCP:

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private static enum
private static class

Field Summary

Modifier and TypeField and Description
private final boolean
captureZeroByte

true if we want to capture packets with zero bytes else false.

private PcapWriteHandler.ChannelType
channelType

Type of the channel this handler is registered on

private InetSocketAddress
handlerAddr

Address of the receiver of the connection

private InetSocketAddress
initiatorAddr

Address of the initiator of the connection

private boolean
isServerPipeline

Set to true if this handler is registered on a server pipeline

private final InternalLogger
logger

Logger for logging events

private final OutputStream
outputStream

OutputStream where we'll write Pcap data.

private PcapWriter
private int
receiveSegmentNumber

TCP Receiver Segment Number.

private int
sendSegmentNumber

TCP Sender Segment Number.

private final boolean
sharedOutputStream

true if we want to synchronize on the OutputStream while writing else false.

private final AtomicReference<State>
state

Current of this PcapWriteHandler

private final boolean
writePcapGlobalHeader

true if we want to write Pcap Global Header on initialization of PcapWriter else false.

Constructor Summary

AccessConstructor and Description
public
PcapWriteHandler(OutputStream
OutputStream where Pcap data will be written. Call close() to close this OutputStream.
outputStream
)

Deprecated Use Builder instead.
Create new PcapWriteHandler Instance.
public
PcapWriteHandler(OutputStream
OutputStream where Pcap data will be written. Call close() to close this OutputStream.
outputStream
,
boolean
Set to true to enable capturing packets with empty (0 bytes) payload. Otherwise, if set to false, empty packets will be filtered out.
captureZeroByte
,
boolean
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.
writePcapGlobalHeader
)

Deprecated Use Builder instead.
Create new PcapWriteHandler Instance
private

Method Summary

Modifier and TypeMethod and Description
public static PcapWriteHandler.Builder
public void
public void
public void
close()

Implements java.io.Closeable.close.

Close PcapWriter and OutputStream.

private void
completeTCPWrite(InetSocketAddress
InetSocketAddress Source Address of this Packet
srcAddr
,
InetSocketAddress
InetSocketAddress Destination Address of this Packet
dstAddr
,
ByteBuf
ByteBuf containing TCP L4 Data
tcpBuf
,
ByteBufAllocator
ByteBufAllocator for allocating bytes for TCP/IP L3 and L2 data.
byteBufAllocator
,
ChannelHandlerContext
ChannelHandlerContext for fireExceptionCaught
ctx
)

Write TCP/IP L3 and L2 here.

private void
completeUDPWrite(InetSocketAddress
InetSocketAddress Source Address of this Packet
srcAddr
,
InetSocketAddress
InetSocketAddress Destination Address of this Packet
dstAddr
,
ByteBuf
ByteBuf containing UDP L4 Data
udpBuf
,
ByteBufAllocator
ByteBufAllocator for allocating bytes for UDP/IP L3 and L2 data.
byteBufAllocator
,
ChannelHandlerContext
ChannelHandlerContext for fireExceptionCaught
ctx
)

Write UDP/IP L3 and L2 here.

public void
private static InetSocketAddress

Returns:

The fixed local address
getLocalAddress
(Channel
The channel to get the local address from
ch
,
InetSocketAddress
The remote address
remote
)

Get the local address of a channel.

public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
private void
handleTCP(ChannelHandlerContext
ChannelHandlerContext for ByteBuf allocation and fireExceptionCaught
ctx
,
Object
Object must be ByteBuf else it'll be discarded
msg
,
boolean
Set true if we have to process packet when packets are being sent out else set false
isWriteOperation
)

Handle TCP L4

private void
handleUDP(ChannelHandlerContext
ChannelHandlerContext for localAddress / remoteAddress, ByteBuf allocation and fireExceptionCaught
ctx
,
Object msg)

Handle UDP l4

private void
public boolean
isWriting()

Returns true if the PcapWriteHandler is currently writing packets to the OutputStream else returns false.

private void
private void
logTCP(boolean isWriteOperation, int bytes, int sendSegmentNumber, int receiveSegmentNumber, InetSocketAddress srcAddr, InetSocketAddress dstAddr, boolean ackOnly)

Logger for TCP

pack-priv void
pack-priv OutputStream
public void
pause()

Pause the PcapWriteHandler from writing packets to the OutputStream.

pack-priv PcapWriter
public void
resume()

Resume the PcapWriteHandler to writing packets to the OutputStream.

pack-priv boolean
pack-priv State
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
public static void
writeGlobalHeader(OutputStream
OutputStream where Pcap data will be written.
outputStream
)

Writes the Pcap Global Header to the provided OutputStream

Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflushread

Field Detail

captureZeroByteback to summary
private final boolean captureZeroByte

true if we want to capture packets with zero bytes else false.

channelTypeback to summary
private PcapWriteHandler.ChannelType channelType

Type of the channel this handler is registered on

handlerAddrback to summary
private InetSocketAddress handlerAddr

Address of the receiver of the connection

initiatorAddrback to summary
private InetSocketAddress initiatorAddr

Address of the initiator of the connection

isServerPipelineback to summary
private boolean isServerPipeline

Set to true if this handler is registered on a server pipeline

loggerback to summary
private final InternalLogger logger

Logger for logging events

outputStreamback to summary
private final OutputStream outputStream

OutputStream where we'll write Pcap data.

pCapWriterback to summary
private PcapWriter pCapWriter

PcapWriter Instance

receiveSegmentNumberback to summary
private int receiveSegmentNumber

TCP Receiver Segment Number. It'll start with 1 and keep incrementing with number of bytes read/sent.

sendSegmentNumberback to summary
private int sendSegmentNumber

TCP Sender Segment Number. It'll start with 1 and keep incrementing with number of bytes read/sent.

sharedOutputStreamback to summary
private final boolean sharedOutputStream

true if we want to synchronize on the OutputStream while writing else false.

stateback to summary
private final AtomicReference<State> state

Current of this PcapWriteHandler

writePcapGlobalHeaderback to summary
private final boolean writePcapGlobalHeader

true if we want to write Pcap Global Header on initialization of PcapWriter else false.

Constructor Detail

PcapWriteHandlerback to summary
public PcapWriteHandler(OutputStream outputStream)

Deprecated

Use Builder instead.

Create new PcapWriteHandler Instance. captureZeroByte is set to false and writePcapGlobalHeader is set to true.

Parameters
outputStream:OutputStream

OutputStream where Pcap data will be written. Call close() to close this OutputStream.

Annotations
@Deprecated
Exceptions
NullPointerException:
If OutputStream is null then we'll throw an NullPointerException
PcapWriteHandlerback to summary
public PcapWriteHandler(OutputStream outputStream, boolean captureZeroByte, boolean writePcapGlobalHeader)

Deprecated

Use Builder instead.

Create new PcapWriteHandler Instance

Parameters
outputStream:OutputStream

OutputStream where Pcap data will be written. Call close() to close this OutputStream.

captureZeroByte:boolean

Set to true to enable capturing packets with empty (0 bytes) payload. Otherwise, if set to false, empty packets will be filtered out.

writePcapGlobalHeader:boolean

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.

Annotations
@Deprecated
Exceptions
NullPointerException:
If OutputStream is null then we'll throw an NullPointerException
PcapWriteHandlerback to summary
private PcapWriteHandler(PcapWriteHandler.Builder builder, OutputStream outputStream)

Method Detail

builderback to summary
public static PcapWriteHandler.Builder builder()
channelActiveback to summary
public void channelActive(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelActive.

Implements io.netty.channel.ChannelInboundHandler.channelActive.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelActive.

Calls ChannelHandlerContext#fireChannelActive() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Close PcapWriter and OutputStream.

Note

Calling this method does not close PcapWriteHandler. Only Pcap Writes are closed.

Annotations
@Override
Exceptions
IOException:
If OutputStream#close() throws an exception
completeTCPWriteback to summary
private void completeTCPWrite(InetSocketAddress srcAddr, InetSocketAddress dstAddr, ByteBuf tcpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx)

Write TCP/IP L3 and L2 here.

Parameters
srcAddr:InetSocketAddress

InetSocketAddress Source Address of this Packet

dstAddr:InetSocketAddress

InetSocketAddress Destination Address of this Packet

tcpBuf:ByteBuf

ByteBuf containing TCP L4 Data

byteBufAllocator:ByteBufAllocator

ByteBufAllocator for allocating bytes for TCP/IP L3 and L2 data.

ctx:ChannelHandlerContext

ChannelHandlerContext for fireExceptionCaught

completeUDPWriteback to summary
private void completeUDPWrite(InetSocketAddress srcAddr, InetSocketAddress dstAddr, ByteBuf udpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx)

Write UDP/IP L3 and L2 here.

Parameters
srcAddr:InetSocketAddress

InetSocketAddress Source Address of this Packet

dstAddr:InetSocketAddress

InetSocketAddress Destination Address of this Packet

udpBuf:ByteBuf

ByteBuf containing UDP L4 Data

byteBufAllocator:ByteBufAllocator

ByteBufAllocator for allocating bytes for UDP/IP L3 and L2 data.

ctx:ChannelHandlerContext

ChannelHandlerContext for fireExceptionCaught

exceptionCaughtback to summary
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.exceptionCaught.

Implements io.netty.channel.ChannelHandler.exceptionCaught, io.netty.channel.ChannelInboundHandler.exceptionCaught.

Doc from io.netty.channel.ChannelHandler.exceptionCaught.

Gets called if a Throwable was thrown.

Annotations
@Override
getLocalAddressback to summary
private static InetSocketAddress getLocalAddress(Channel ch, InetSocketAddress remote)

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 the remote, return the wildcard address of the remote's family instead.

Parameters
ch:Channel

The channel to get the local address from

remote:InetSocketAddress

The remote address

Returns:InetSocketAddress

The fixed local address

handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
handleTCPback to summary
private void handleTCP(ChannelHandlerContext ctx, Object msg, boolean isWriteOperation)

Handle TCP L4

Parameters
ctx:ChannelHandlerContext

ChannelHandlerContext for ByteBuf allocation and fireExceptionCaught

msg:Object

Object must be ByteBuf else it'll be discarded

isWriteOperation:boolean

Set true if we have to process packet when packets are being sent out else set false

handleUDPback to summary
private void handleUDP(ChannelHandlerContext ctx, Object msg)

Handle UDP l4

Parameters
ctx:ChannelHandlerContext

ChannelHandlerContext for localAddress / remoteAddress, ByteBuf allocation and fireExceptionCaught

msg:Object

DatagramPacket or ByteBuf

initializeIfNecessaryback to summary
private void initializeIfNecessary(ChannelHandlerContext ctx) throws Exception
isWritingback to summary
public boolean isWriting()

Returns true if the PcapWriteHandler is currently writing packets to the OutputStream else returns false.

logDiscardback to summary
private void logDiscard()
logTCPback to summary
private void logTCP(boolean isWriteOperation, int bytes, int sendSegmentNumber, int receiveSegmentNumber, InetSocketAddress srcAddr, InetSocketAddress dstAddr, boolean ackOnly)

Logger for TCP

markClosedback to summary
pack-priv void markClosed()
outputStreamback to summary
pack-priv OutputStream outputStream()
pauseback to summary
public void pause()

Pause the PcapWriteHandler from writing packets to the OutputStream.

pCapWriterback to summary
pack-priv PcapWriter pCapWriter()
resumeback to summary
public void resume()

Resume the PcapWriteHandler to writing packets to the OutputStream.

sharedOutputStreamback to summary
pack-priv boolean sharedOutputStream()
stateback to summary
pack-priv State state()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs

writeGlobalHeaderback to summary
public static void writeGlobalHeader(OutputStream outputStream) throws IOException

Writes the Pcap Global Header to the provided OutputStream

Parameters
outputStream:OutputStream

OutputStream where Pcap data will be written.

Exceptions
IOException:
if there is an error writing to the OutputStream
io.netty.handler.pcap back to summary

public final Class PcapWriteHandler.Builder

extends Object
Class Inheritance

Builder for PcapWriteHandler

Field Summary

Modifier and TypeField and Description
private boolean
private PcapWriteHandler.ChannelType
private InetSocketAddress
private InetSocketAddress
private boolean
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public PcapWriteHandler

Returns:

The handler.
build
(OutputStream
The output stream to write the pcap data to.
outputStream
)

Build the PcapWriteHandler.

public PcapWriteHandler.Builder

Returns:

this builder
captureZeroByte
(boolean
Whether to filter out empty packets.
captureZeroByte
)

Set to true to enable capturing packets with empty (0 bytes) payload.

public PcapWriteHandler.Builder

Returns:

this builder
forceTcpChannel
(InetSocketAddress
The address of the TCP server (handler)
serverAddress
,
InetSocketAddress
The address of the TCP client (initiator)
clientAddress
,
boolean
Whether the handler is part of the server channel
isServerPipeline
)

Force this handler to write data as if they were TCP packets, with the given connection metadata.

public PcapWriteHandler.Builder

Returns:

this builder
forceUdpChannel
(InetSocketAddress
The address of the UDP local
localAddress
,
InetSocketAddress
The address of the UDP remote
remoteAddress
)

Force this handler to write data as if they were UDP packets, with the given connection metadata.

public PcapWriteHandler.Builder

Returns:

this builder
sharedOutputStream
(boolean
Whether OutputStream is shared or not
sharedOutputStream
)

Set to true if multiple PcapWriteHandler instances will be writing to the same OutputStream concurrently, and write locking is required.

public PcapWriteHandler.Builder

Returns:

this builder
writePcapGlobalHeader
(boolean
Whether to write the pcap global header.
writePcapGlobalHeader
)

Set to true to write Pcap Global Header on initialization.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

captureZeroByteback to summary
private boolean captureZeroByte
channelTypeback to summary
private PcapWriteHandler.ChannelType channelType
handlerAddrback to summary
private InetSocketAddress handlerAddr
initiatorAddrback to summary
private InetSocketAddress initiatorAddr
isServerPipelineback to summary
private boolean isServerPipeline
sharedOutputStreamback to summary
private boolean sharedOutputStream
writePcapGlobalHeaderback to summary
private boolean writePcapGlobalHeader

Constructor Detail

Builderback to summary
private Builder()

Method Detail

buildback to summary
public PcapWriteHandler build(OutputStream outputStream)

Build the PcapWriteHandler.

Parameters
outputStream:OutputStream

The output stream to write the pcap data to.

Returns:PcapWriteHandler

The handler.

captureZeroByteback to summary
public PcapWriteHandler.Builder captureZeroByte(boolean captureZeroByte)

Set to true to enable capturing packets with empty (0 bytes) payload. Otherwise, if set to false, empty packets will be filtered out.

Parameters
captureZeroByte:boolean

Whether to filter out empty packets.

Returns:PcapWriteHandler.Builder

this builder

forceTcpChannelback to summary
public PcapWriteHandler.Builder forceTcpChannel(InetSocketAddress serverAddress, InetSocketAddress clientAddress, boolean isServerPipeline)

Force this handler to write data as if they were TCP packets, with the given connection metadata. If this method isn't called, we determine the metadata from the channel.

Parameters
serverAddress:InetSocketAddress

The address of the TCP server (handler)

clientAddress:InetSocketAddress

The address of the TCP client (initiator)

isServerPipeline:boolean

Whether the handler is part of the server channel

Returns:PcapWriteHandler.Builder

this builder

forceUdpChannelback to summary
public PcapWriteHandler.Builder forceUdpChannel(InetSocketAddress localAddress, InetSocketAddress remoteAddress)

Force this handler to write data as if they were UDP packets, with the given connection metadata. If this method isn't called, we determine the metadata from the channel.
Note that even if this method is called, the address information on DatagramPacket takes precedence if it is present.

Parameters
localAddress:InetSocketAddress

The address of the UDP local

remoteAddress:InetSocketAddress

The address of the UDP remote

Returns:PcapWriteHandler.Builder

this builder

sharedOutputStreamback to summary
public PcapWriteHandler.Builder sharedOutputStream(boolean sharedOutputStream)

Set to true if multiple PcapWriteHandler instances will be writing to the same OutputStream concurrently, and write locking is required. Otherwise, if set to false, no locking will be done. Additionally, close will not close the underlying OutputStream.

Note

it is probably an error to have both writePcapGlobalHeader and sharedOutputStream set to true at the same time.

Parameters
sharedOutputStream:boolean

Whether OutputStream is shared or not

Returns:PcapWriteHandler.Builder

this builder

writePcapGlobalHeaderback to summary
public PcapWriteHandler.Builder writePcapGlobalHeader(boolean 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.

Parameters
writePcapGlobalHeader:boolean

Whether to write the pcap global header.

Returns:PcapWriteHandler.Builder

this builder

io.netty.handler.pcap back to summary

private final Enum PcapWriteHandler.ChannelType

extends Enum<PcapWriteHandler.ChannelType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final PcapWriteHandler.ChannelType
public static final PcapWriteHandler.ChannelType

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static PcapWriteHandler.ChannelType
public static PcapWriteHandler.ChannelType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

TCPback to summary
public static final PcapWriteHandler.ChannelType TCP
UDPback to summary
public static final PcapWriteHandler.ChannelType UDP

Constructor Detail

ChannelTypeback to summary
private ChannelType()

Method Detail

valueOfback to summary
public static PcapWriteHandler.ChannelType valueOf(String name)
valuesback to summary
public static PcapWriteHandler.ChannelType[] values()
io.netty.handler.pcap back to summary

private final Class PcapWriteHandler.WildcardAddressHolder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv static final InetAddress
pack-priv static final InetAddress

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

wildcard4back to summary
pack-priv static final InetAddress wildcard4
wildcard6back to summary
pack-priv static final InetAddress wildcard6

Constructor Detail

WildcardAddressHolderback to summary
private WildcardAddressHolder()