NetUtil implements NetworkAddressService { public static String getIpv4NetBroadcastAddress(String ipAddressString, short prefix) { String errorString = "IP '" + ipAddressString + "' is not a valid IPv4 address"; if (!isValidIPConfig(ipAddressString)) { throw new IllegalArgumentException(errorString); } if (prefix < 1 || prefix > 32) { throw new IllegalArgumentException("Prefix '" + prefix + "' is out of bounds (1-32)"); } try { byte[] addr = InetAddress.getByName(ipAddressString).getAddress(); byte[] netmask = InetAddress.getByName(networkPrefixLengthToNetmask(prefix)).getAddress(); byte[] broadcast = new byte[] { (byte) (~netmask[0] | addr[0]), (byte) (~netmask[1] | addr[1]), (byte) (~netmask[2] | addr[2]), (byte) (~netmask[3] | addr[3]) }; return InetAddress.getByAddress(broadcast).getHostAddress(); } catch (UnknownHostException ex) { throw new IllegalArgumentException(errorString); } } @Activate  NetUtil(final @Reference SafeCaller safeCaller); @Modified synchronized void modified(Map<String, Object> config); @Override @Nullable String getPrimaryIpv4HostAddress(); @Override boolean isUseOnlyOneAddress(); @Override boolean isUseIPv6(); @Override void addNetworkAddressChangeListener(NetworkAddressChangeListener listener); @Override void removeNetworkAddressChangeListener(NetworkAddressChangeListener listener); static List<String> getAllBroadcastAddresses(); @Override @Nullable String getConfiguredBroadcastAddress(); static Collection<CidrAddress> getAllInterfaceAddresses(); static String networkPrefixLengthToNetmask(int prefixLength); static String getIpv4NetAddress(String ipAddressString, short netMask); static String getIpv4NetBroadcastAddress(String ipAddressString, short prefix); static boolean isValidIPConfig(String ipAddress); static final int POLL_INTERVAL_SECONDS; }
@Test public void testBroadcastAddress() { String broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.0.1", (short) 24); assertThat(broadcast, is("192.168.0.255")); broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.23.5", (short) 24); assertThat(broadcast, is("192.168.23.255")); broadcast = NetUtil.getIpv4NetBroadcastAddress("172.16.42.23", (short) 16); assertThat(broadcast, is("172.16.255.255")); broadcast = NetUtil.getIpv4NetBroadcastAddress("10.8.13.5", (short) 8); assertThat(broadcast, is("10.255.255.255")); broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.5.8", (short) 23); assertThat(broadcast, is("192.168.5.255")); broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.5.8", (short) 27); assertThat(broadcast, is("192.168.5.31")); broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.5.8", (short) 29); assertThat(broadcast, is("192.168.5.15")); try { broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.5.8", (short) 33); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("Prefix '33' is out of bounds (1-32)")); } try { broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.58", (short) 24); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("IP '192.168.58' is not a valid IPv4 address")); } try { broadcast = NetUtil.getIpv4NetBroadcastAddress("192.168.58", (short) 24); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("IP '192.168.58' is not a valid IPv4 address")); } try { broadcast = NetUtil.getIpv4NetBroadcastAddress("SOME_TEXT", (short) 42); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("IP 'SOME_TEXT' is not a valid IPv4 address")); } }