private static boolean containsAddress ( final InetAddress network , final InetAddress netmask , final InetAddress ip ) { LOGGER . debug ( "Checking IP address: [{}] in [{}] by [{}]" , ip , network , netmask ) ; val networkBytes = network . getAddress ( ) ; val netmaskBytes = netmask . getAddress ( ) ; val ipBytes = ip . getAddress ( ) ; /* check IPv4/v6-compatibility or parameters: */ if ( networkBytes . length != netmaskBytes . length || netmaskBytes . length != ipBytes . length ) { LOGGER . debug ( "Network address [{}], subnet mask [{}] and/or host address [{}]" + " have different sizes! (return false ...)" , network , netmask , ip ) ; return false ; } /* Check if the masked network and ip addresses match: */ for ( var i = 0 ; i < netmaskBytes . length ; i ++ ) { val mask = netmaskBytes [ i ] & HEX_RIGHT_SHIFT_COEFFICIENT ; if ( ( networkBytes [ i ] & mask ) != ( ipBytes [ i ] & mask ) ) { LOGGER . debug ( "[{}] is not in [{}]/[{}]" , ip , network , netmask ) ; return false ; } } LOGGER . debug ( "[{}] is in [{}]/[{}]" , ip , network , netmask ) ; return true ; }
Checks if a subnet contains a specific IP address .