LocalNetworkGuard implements NetworkGuard { @Override public boolean isSecure(Channel channel, InetSocketAddress source) { byte[] networkBytes = IPAddressCompare.addressToByteArrayV4(this.network); byte[] subnetBytes = IPAddressCompare.addressToByteArrayV4(this.subnet); boolean secure = IPAddressCompare.isInRangeV4(networkBytes, subnetBytes, source.getAddress().getAddress()); if(!secure) { if (log.isTraceEnabled()) { log.trace("Dropped insecure packet [network=" + this.network + ", subnet=" + this.subnet + ", remote address=" + source.getAddress().getHostAddress() + "]"); } } return secure; } LocalNetworkGuard(String network, String subnet); @Override boolean isSecure(Channel channel, InetSocketAddress source);  }
@Test public void testSecureSource() { final String subnet = "255.255.255.0"; final String network = "192.168.1.0"; final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.22", 2727); final Channel channel = Mockito.mock(Channel.class); final NetworkGuard guard = new LocalNetworkGuard(network, subnet); final boolean secure = guard.isSecure(channel, remoteAddress); Assert.assertTrue(secure); }
@Test public void testInsecureSource() { final String subnet = "255.255.255.0"; final String network = "192.168.1.0"; final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.2.4", 2727); final Channel channel = Mockito.mock(Channel.class); final NetworkGuard guard = new LocalNetworkGuard(network, subnet); final boolean secure = guard.isSecure(channel, remoteAddress); Assert.assertFalse(secure); }