Home / Function/ initializeIfNecessary() — netty Function Reference

initializeIfNecessary() — netty Function Reference

Architecture documentation for the initializeIfNecessary() function in PcapWriteHandler.java from the netty codebase.

Function java Buffer Allocators calls 2 called by 3

Entity Profile

Dependency Diagram

graph TD
  f413848a_cf63_e812_442c_2bc5760c526a["initializeIfNecessary()"]
  59f0289b_ecfd_de3c_3397_4cf08e2047d1["PcapWriteHandler"]
  f413848a_cf63_e812_442c_2bc5760c526a -->|defined in| 59f0289b_ecfd_de3c_3397_4cf08e2047d1
  70ae8233_1a41_b995_2a35_d08a52e5832c["channelActive()"]
  70ae8233_1a41_b995_2a35_d08a52e5832c -->|calls| f413848a_cf63_e812_442c_2bc5760c526a
  ad9aed33_ce6c_03a6_768d_de9fb726fba9["channelRead()"]
  ad9aed33_ce6c_03a6_768d_de9fb726fba9 -->|calls| f413848a_cf63_e812_442c_2bc5760c526a
  2fb87f56_9808_98f7_33ce_8ee757b86601["write()"]
  2fb87f56_9808_98f7_33ce_8ee757b86601 -->|calls| f413848a_cf63_e812_442c_2bc5760c526a
  8825312b_8c52_5ece_b828_64cd89c66a92["PcapWriter()"]
  f413848a_cf63_e812_442c_2bc5760c526a -->|calls| 8825312b_8c52_5ece_b828_64cd89c66a92
  bbfbdddd_98c4_8181_5429_a45dee375189["completeTCPWrite()"]
  f413848a_cf63_e812_442c_2bc5760c526a -->|calls| bbfbdddd_98c4_8181_5429_a45dee375189
  style f413848a_cf63_e812_442c_2bc5760c526a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

handler/src/main/java/io/netty/handler/pcap/PcapWriteHandler.java lines 202–269

    private void initializeIfNecessary(ChannelHandlerContext ctx) throws Exception {
        // If State is not 'INIT' then it means we're already initialized so then no need to initiaize again.
        if (state.get() != State.INIT) {
            return;
        }

        pCapWriter = new PcapWriter(this);

        if (channelType == null) {
            // infer channel type
            if (ctx.channel() instanceof SocketChannel) {
                channelType = ChannelType.TCP;

                // If Channel belongs to `SocketChannel` then we're handling TCP.
                // Capture correct `localAddress` and `remoteAddress`
                if (ctx.channel().parent() instanceof ServerSocketChannel) {
                    isServerPipeline = true;
                    initiatorAddr = (InetSocketAddress) ctx.channel().remoteAddress();
                    handlerAddr = getLocalAddress(ctx.channel(), initiatorAddr);
                } else {
                    isServerPipeline = false;
                    handlerAddr = (InetSocketAddress) ctx.channel().remoteAddress();
                    initiatorAddr = getLocalAddress(ctx.channel(), handlerAddr);
                }
            } else if (ctx.channel() instanceof DatagramChannel) {
                channelType = ChannelType.UDP;

                DatagramChannel datagramChannel = (DatagramChannel) ctx.channel();

                // If `DatagramChannel` is connected then we can get
                // `localAddress` and `remoteAddress` from Channel.
                if (datagramChannel.isConnected()) {
                    handlerAddr = (InetSocketAddress) ctx.channel().remoteAddress();
                    initiatorAddr = getLocalAddress(ctx.channel(), handlerAddr);
                }
            }
        }

        if (channelType == ChannelType.TCP) {
            logger.debug("Initiating Fake TCP 3-Way Handshake");

            ByteBuf tcpBuf = ctx.alloc().buffer();

            try {
                // Write SYN with Normal Source and Destination Address
                TCPPacket.writePacket(tcpBuf, null, 0, 0,
                                      initiatorAddr.getPort(), handlerAddr.getPort(), TCPPacket.TCPFlag.SYN);
                completeTCPWrite(initiatorAddr, handlerAddr, tcpBuf, ctx.alloc(), ctx);

                // Write SYN+ACK with Reversed Source and Destination Address
                TCPPacket.writePacket(tcpBuf, null, 0, 1,
                                      handlerAddr.getPort(), initiatorAddr.getPort(), TCPPacket.TCPFlag.SYN,
                                      TCPPacket.TCPFlag.ACK);
                completeTCPWrite(handlerAddr, initiatorAddr, tcpBuf, ctx.alloc(), ctx);

                // Write ACK with Normal Source and Destination Address
                TCPPacket.writePacket(tcpBuf, null, 1, 1, initiatorAddr.getPort(),
                                      handlerAddr.getPort(), TCPPacket.TCPFlag.ACK);
                completeTCPWrite(initiatorAddr, handlerAddr, tcpBuf, ctx.alloc(), ctx);
            } finally {
                tcpBuf.release();
            }

            logger.debug("Finished Fake TCP 3-Way Handshake");
        }

        state.set(State.WRITING);
    }

Domain

Subdomains

Frequently Asked Questions

What does initializeIfNecessary() do?
initializeIfNecessary() is a function in the netty codebase, defined in handler/src/main/java/io/netty/handler/pcap/PcapWriteHandler.java.
Where is initializeIfNecessary() defined?
initializeIfNecessary() is defined in handler/src/main/java/io/netty/handler/pcap/PcapWriteHandler.java at line 202.
What does initializeIfNecessary() call?
initializeIfNecessary() calls 2 function(s): PcapWriter, completeTCPWrite.
What calls initializeIfNecessary()?
initializeIfNecessary() is called by 3 function(s): channelActive, channelRead, write.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free