Home / Class/ Ip4SubnetFilterRule Class — netty Architecture

Ip4SubnetFilterRule Class — netty Architecture

Architecture documentation for the Ip4SubnetFilterRule class in IpSubnetFilterRule.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  dff893ee_a16f_1b3d_a2b1_b004425749de["Ip4SubnetFilterRule"]
  1cb34468_689d_d5ec_e45e_875dc8626c30["IpSubnetFilterRule.java"]
  dff893ee_a16f_1b3d_a2b1_b004425749de -->|defined in| 1cb34468_689d_d5ec_e45e_875dc8626c30
  c4185464_2099_2a9e_b108_9e389b3838a1["Ip4SubnetFilterRule()"]
  dff893ee_a16f_1b3d_a2b1_b004425749de -->|method| c4185464_2099_2a9e_b108_9e389b3838a1
  46a14e55_9861_790d_f9fd_791b6716de9a["matches()"]
  dff893ee_a16f_1b3d_a2b1_b004425749de -->|method| 46a14e55_9861_790d_f9fd_791b6716de9a
  c2ae2cd5_3646_3434_82b1_67603e8e5864["IpFilterRuleType()"]
  dff893ee_a16f_1b3d_a2b1_b004425749de -->|method| c2ae2cd5_3646_3434_82b1_67603e8e5864
  6ba924f7_e4be_2e1b_ecde_3c4b4a42862d["prefixToSubnetMask()"]
  dff893ee_a16f_1b3d_a2b1_b004425749de -->|method| 6ba924f7_e4be_2e1b_ecde_3c4b4a42862d

Relationship Graph

Source Code

handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java lines 163–208

    static final class Ip4SubnetFilterRule implements IpFilterRule {

        private final int networkAddress;
        private final int subnetMask;
        private final IpFilterRuleType ruleType;

        private Ip4SubnetFilterRule(Inet4Address ipAddress, int cidrPrefix, IpFilterRuleType ruleType) {
            if (cidrPrefix < 0 || cidrPrefix > 32) {
                throw new IllegalArgumentException(String.format("IPv4 requires the subnet prefix to be in range of "
                        + "[0,32]. The prefix was: %d", cidrPrefix));
            }

            subnetMask = prefixToSubnetMask(cidrPrefix);
            networkAddress = NetUtil.ipv4AddressToInt(ipAddress) & subnetMask;
            this.ruleType = ruleType;
        }

        @Override
        public boolean matches(InetSocketAddress remoteAddress) {
            final InetAddress inetAddress = remoteAddress.getAddress();
            if (inetAddress instanceof Inet4Address) {
                int ipAddress = NetUtil.ipv4AddressToInt((Inet4Address) inetAddress);
                return (ipAddress & subnetMask) == networkAddress;
            }
            return false;
        }

        @Override
        public IpFilterRuleType ruleType() {
            return ruleType;
        }

        private static int prefixToSubnetMask(int cidrPrefix) {
            /*
             * Perform the shift on a long and downcast it to int afterwards.
             * This is necessary to handle a cidrPrefix of zero correctly.
             * The left shift operator on an int only uses the five least
             * significant bits of the right-hand operand. Thus -1 << 32 evaluates
             * to -1 instead of 0. The left shift operator applied on a long
             * uses the six least significant bits.
             *
             * Also see https://github.com/netty/netty/issues/2767
             */
            return (int) (-1L << 32 - cidrPrefix);
        }
    }

Frequently Asked Questions

What is the Ip4SubnetFilterRule class?
Ip4SubnetFilterRule is a class in the netty codebase, defined in handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java.
Where is Ip4SubnetFilterRule defined?
Ip4SubnetFilterRule is defined in handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java at line 163.

Analyze Your Own Codebase

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

Try Supermodel Free