List of usage examples for java.net InterfaceAddress getNetworkPrefixLength
public short getNetworkPrefixLength()
From source file:Main.java
public static void printParameter(NetworkInterface ni) throws SocketException { System.out.println(" Name = " + ni.getName()); System.out.println(" Display Name = " + ni.getDisplayName()); System.out.println(" Is up = " + ni.isUp()); System.out.println(" Support multicast = " + ni.supportsMulticast()); System.out.println(" Is loopback = " + ni.isLoopback()); System.out.println(" Is virtual = " + ni.isVirtual()); System.out.println(" Is point to point = " + ni.isPointToPoint()); System.out.println(" Hardware address = " + ni.getHardwareAddress()); System.out.println(" MTU = " + ni.getMTU()); System.out.println("\nList of Interface Addresses:"); List<InterfaceAddress> list = ni.getInterfaceAddresses(); Iterator<InterfaceAddress> it = list.iterator(); while (it.hasNext()) { InterfaceAddress ia = it.next(); System.out.println(" Address = " + ia.getAddress()); System.out.println(" Broadcast = " + ia.getBroadcast()); System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength()); System.out.println(""); }// w ww.j ava2 s . com }
From source file:com.clustercontrol.repository.util.NodeSearchUtil.java
/** * Generate default IP for node search/create dialog *///from w w w. ja v a 2s .c o m public static String generateDefaultIp(String def, int hostAddress) { try { // Get IP InetAddress addr = Inet4Address.getLocalHost(); // Get subnet mask length int prefixLength = -1; NetworkInterface ni = NetworkInterface.getByInetAddress(addr); for (InterfaceAddress iaddr : ni.getInterfaceAddresses()) { if (iaddr.getAddress() instanceof Inet4Address) { prefixLength = iaddr.getNetworkPrefixLength(); break; } } if (-1 == prefixLength) { return def; } byte[] ipRaw = addr.getAddress(); // For ipv4 if (4 == ipRaw.length) { int counter = prefixLength; for (int i = 0; i < ipRaw.length; i++) { if (counter < 8) { byte mask = 0x00; for (int j = 0; j < counter; j++) { mask = (byte) (mask >> 1 | 0x80); } ipRaw[i] = (byte) (ipRaw[i] & mask); } counter -= 8; } // Re-format/round up hostAddress if (hostAddress < 0) { hostAddress += Math.pow(2, 32 - prefixLength); } // Add host address part int part = 4; while (hostAddress > 0 && part > 0) { ipRaw[part - 1] += (hostAddress & 0xff); hostAddress >>= 8; } return Inet4Address.getByAddress(ipRaw).getHostAddress(); } // TODO Support ipv6? } catch (UnknownHostException | SocketException e) { m_log.debug(e); } return def; }
From source file:org.openhab.binding.network.service.NetworkUtils.java
/** * Gets every IPv4 Address on each Interface except the loopback * The Address format is ip/subnet/* w ww.j a v a 2 s .c o m*/ * * @return The collected IPv4 Addresses */ public static TreeSet<String> getInterfaceIPs() { TreeSet<String> interfaceIPs = new TreeSet<String>(); try { // For each interface ... for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface networkInterface = en.nextElement(); if (!networkInterface.isLoopback()) { // .. and for each address ... for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it .hasNext();) { // ... get IP and Subnet InterfaceAddress interfaceAddress = it.next(); interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/" + interfaceAddress.getNetworkPrefixLength()); } } } } catch (SocketException e) { } return interfaceIPs; }
From source file:de.uni_bonn.bit.IPAddressHelper.java
/** * Returns a list of IP addresses that can be used to communicate with a peer on a * different machine. The IP addresses are in CIDR notation (e.g. 192.168.2.1/24). * @return// w w w . ja v a2s .c om */ public static List<String> getAllUsableIPAddresses() { List<String> output = new ArrayList<String>(); try { for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InterfaceAddress address : ni.getInterfaceAddresses()) { if (!address.getAddress().isMulticastAddress() && !address.getAddress().isLinkLocalAddress() && !address.getAddress().isLoopbackAddress()) { output.add(address.getAddress().getHostAddress() + "/" + address.getNetworkPrefixLength()); } } } } catch (SocketException e) { } return output; }
From source file:org.openhab.binding.network.service.NetworkService.java
/** * Gets every IPv4 Address on each Interface except the loopback * The Address format is ip/subnet// ww w .ja v a 2 s . com * * @return The collected IPv4 Addresses */ private static TreeSet<String> getInterfaceIPs() { TreeSet<String> interfaceIPs = new TreeSet<String>(); try { // For each interface ... for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface networkInterface = en.nextElement(); if (!networkInterface.isLoopback()) { // .. and for each address ... for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it .hasNext();) { // ... get IP and Subnet InterfaceAddress interfaceAddress = it.next(); interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/" + interfaceAddress.getNetworkPrefixLength()); } } } } catch (SocketException e) { } return interfaceIPs; }
From source file:nz.co.fortytwo.signalk.util.Util.java
public static boolean sameNetwork(String localAddress, String remoteAddress) throws Exception { InetAddress addr = InetAddress.getByName(localAddress); NetworkInterface networkInterface = NetworkInterface.getByInetAddress(addr); short netmask = -1; for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { if (address.getAddress().equals(addr)) { netmask = address.getNetworkPrefixLength(); }// w ww .jav a2 s. c o m } byte[] a1 = InetAddress.getByName(localAddress).getAddress(); byte[] a2 = InetAddress.getByName(remoteAddress).getAddress(); byte[] m = InetAddress.getByName(normalizeFromCIDR(netmask)).getAddress(); for (int i = 0; i < a1.length; i++) if ((a1[i] & m[i]) != (a2[i] & m[i])) return false; return true; }
From source file:android_network.hetnet.vpn_service.Util.java
public static String getNetworkInfo(Context context) { StringBuilder sb = new StringBuilder(); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ani = cm.getActiveNetworkInfo(); List<NetworkInfo> listNI = new ArrayList<>(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) listNI.addAll(Arrays.asList(cm.getAllNetworkInfo())); else/* ww w. j ava 2 s . com*/ for (Network network : cm.getAllNetworks()) { NetworkInfo ni = cm.getNetworkInfo(network); if (ni != null) listNI.add(ni); } for (NetworkInfo ni : listNI) { sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ') .append(ni.getDetailedState()) .append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo()) .append(ni.getType() == ConnectivityManager.TYPE_MOBILE ? " " + Util.getNetworkGeneration(ni.getSubtype()) : "") .append(ni.isRoaming() ? " R" : "") .append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype() ? " *" : "") .append("\r\n"); } try { Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); if (nis != null) while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); if (ni != null && !ni.isLoopback()) { List<InterfaceAddress> ias = ni.getInterfaceAddresses(); if (ias != null) for (InterfaceAddress ia : ias) sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress()) .append('/').append(ia.getNetworkPrefixLength()).append(' ') .append(ni.getMTU()).append(' ').append(ni.isUp() ? '^' : 'v') .append("\r\n"); } } } catch (Throwable ex) { sb.append(ex.toString()).append("\r\n"); } if (sb.length() > 2) sb.setLength(sb.length() - 2); return sb.toString(); }
From source file:com.master.metehan.filtereagle.Util.java
public static String getNetworkInfo(Context context) { StringBuilder sb = new StringBuilder(); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ani = cm.getActiveNetworkInfo(); List<NetworkInfo> listNI = new ArrayList<>(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) listNI.addAll(Arrays.asList(cm.getAllNetworkInfo())); else/*from w ww .j av a2 s . co m*/ for (Network network : cm.getAllNetworks()) { NetworkInfo ni = cm.getNetworkInfo(network); if (ni != null) listNI.add(ni); } for (NetworkInfo ni : listNI) { sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ') .append(ni.getDetailedState()) .append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo()) .append(ni.getType() == ConnectivityManager.TYPE_MOBILE ? " " + Util.getNetworkGeneration(ni.getSubtype()) : "") .append(ni.isRoaming() ? " R" : "") .append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype() ? " *" : "") .append("\r\n"); } try { Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); if (nis != null) { sb.append("\r\n"); while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); if (ni != null) { List<InterfaceAddress> ias = ni.getInterfaceAddresses(); if (ias != null) for (InterfaceAddress ia : ias) sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress()) .append('/').append(ia.getNetworkPrefixLength()).append(' ') .append(ni.getMTU()).append("\r\n"); } } } } catch (Throwable ex) { sb.append(ex.toString()).append("\r\n"); } if (sb.length() > 2) sb.setLength(sb.length() - 2); return sb.toString(); }
From source file:org.openhab.binding.networkhealth.discovery.NetworkHealthDiscoveryService.java
/** * Gets every IPv4 Address on each Interface except the loopback * The Address format is ip/subnet//from w w w . ja v a 2 s .c o m * @return The collected IPv4 Addresses */ private TreeSet<String> getInterfaceIPs() { TreeSet<String> interfaceIPs = new TreeSet<String>(); try { // For each interface ... for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface networkInterface = en.nextElement(); if (!networkInterface.isLoopback()) { // .. and for each address ... for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it .hasNext();) { // ... get IP and Subnet InterfaceAddress interfaceAddress = it.next(); interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/" + interfaceAddress.getNetworkPrefixLength()); } } } } catch (SocketException e) { } return interfaceIPs; }
From source file:org.openhab.binding.plclogo.internal.discovery.PLCDiscoveryService.java
@Override protected void startScan() { stopScan();/*w w w .j a v a 2 s. c o m*/ logger.debug("Start scan for LOGO! bridge"); Enumeration<NetworkInterface> devices = null; try { devices = NetworkInterface.getNetworkInterfaces(); } catch (SocketException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } Set<String> addresses = new TreeSet<>(); while ((devices != null) && devices.hasMoreElements()) { NetworkInterface device = devices.nextElement(); try { if (!device.isUp() || device.isLoopback()) { continue; } } catch (SocketException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } for (InterfaceAddress iface : device.getInterfaceAddresses()) { InetAddress address = iface.getAddress(); if (address instanceof Inet4Address) { String prefix = String.valueOf(iface.getNetworkPrefixLength()); SubnetUtils utilities = new SubnetUtils(address.getHostAddress() + "/" + prefix); addresses.addAll(Arrays.asList(utilities.getInfo().getAllAddresses())); } } } ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); for (String address : addresses) { try { executor.execute(new Runner(address)); } catch (RejectedExecutionException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } } try { executor.awaitTermination(CONNECTION_TIMEOUT * addresses.size(), TimeUnit.MILLISECONDS); } catch (InterruptedException exception) { logger.warn("LOGO! bridge discovering: {}.", exception.toString()); } executor.shutdown(); stopScan(); }