List of usage examples for java.net InterfaceAddress getAddress
public InetAddress getAddress()
From source file:org.openhab.binding.vera.internal.discovery.VeraBridgeDiscoveryService.java
private void scan() { logger.debug("Starting scan for Vera controller"); ValidateIPV4 validator = new ValidateIPV4(); try {//w ww . ja v a2s. co m Enumeration<NetworkInterface> enumNetworkInterface = NetworkInterface.getNetworkInterfaces(); while (enumNetworkInterface.hasMoreElements()) { NetworkInterface networkInterface = enumNetworkInterface.nextElement(); if (networkInterface.isUp() && !networkInterface.isVirtual() && !networkInterface.isLoopback()) { for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { if (validator.isValidIPV4(address.getAddress().getHostAddress())) { String ipAddress = address.getAddress().getHostAddress(); Short prefix = address.getNetworkPrefixLength(); logger.debug("Scan IP address for Vera Controller: {}", ipAddress); String subnet = ipAddress + "/" + prefix; SubnetUtils utils = new SubnetUtils(subnet); String[] addresses = utils.getInfo().getAllAddresses(); for (String addressInSubnet : addresses) { scheduler.execute(new VeraControllerScan(addressInSubnet)); } } } } } } catch (SocketException e) { logger.warn("Error occurred while searching Vera controller: ", e); } }
From source file:org.openhab.binding.plclogo.internal.discovery.PLCDiscoveryService.java
@Override protected void startScan() { stopScan();// ww w. ja v a 2s . 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(); }
From source file:ch.cyberduck.core.socket.NetworkInterfaceAwareSocketFactory.java
private NetworkInterface findIPv6Interface(Inet6Address address) throws IOException { if (blacklisted.isEmpty()) { if (log.isDebugEnabled()) { log.debug("Ignore IP6 default network interface setup with empty blacklist"); }/*w w w .j a v a 2 s .c o m*/ return null; } if (address.getScopeId() != 0) { if (log.isDebugEnabled()) { log.debug(String.format( "Ignore IP6 default network interface setup for address with scope identifier %d", address.getScopeId())); } return null; } // If we find an interface name en0 that supports IPv6 make it the default. // We must use the index of the network interface. Referencing the interface by name will still // set the scope id to '0' referencing the awdl0 interface that is first in the list of enumerated // network interfaces instead of its correct index in <code>java.net.Inet6Address</code> // Use private API to defer the numeric format for the address List<Integer> indexes = new ArrayList<Integer>(); final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { indexes.add(enumeration.nextElement().getIndex()); } for (Integer index : indexes) { final NetworkInterface n = NetworkInterface.getByIndex(index); if (log.isDebugEnabled()) { log.debug(String.format("Evaluate interface with %s index %d", n, index)); } if (!n.isUp()) { if (log.isDebugEnabled()) { log.debug(String.format("Ignore interface %s not up", n)); } continue; } if (blacklisted.contains(n.getName())) { log.warn(String.format("Ignore network interface %s disabled with blacklist", n)); continue; } for (InterfaceAddress i : n.getInterfaceAddresses()) { if (i.getAddress() instanceof Inet6Address) { if (log.isInfoEnabled()) { log.info(String.format("Selected network interface %s", n)); } return n; } } log.warn(String.format("No IPv6 for interface %s", n)); } log.warn("No network interface found for IPv6"); return null; }
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 www .j a v a 2s. 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.springframework.data.hadoop.util.net.DefaultHostInfoDiscovery.java
@Override public HostInfo getHostInfo() { List<NetworkInterface> interfaces; try {/*from w ww . j ava 2 s .c o m*/ interfaces = getAllAvailableInterfaces(); } catch (SocketException e) { return null; } // pre filter candidates interfaces = filterInterfaces(interfaces); // sort to prepare getting first match interfaces = sortInterfaces(interfaces); for (NetworkInterface nic : interfaces) { List<InetAddress> addresses = new ArrayList<InetAddress>(); for (InterfaceAddress interfaceAddress : nic.getInterfaceAddresses()) { addresses.add(interfaceAddress.getAddress()); } addresses = filterAddresses(addresses); if (!addresses.isEmpty()) { InetAddress address = addresses.get(0); return new HostInfo(address.getHostAddress(), address.getHostName()); } } return null; }
From source file:org.openhab.binding.network.internal.utils.NetworkUtils.java
/** * Gets every IPv4 Address on each Interface except the loopback * The Address format is ip/subnet/*from ww w . j a va 2s . co m*/ * * @return The collected IPv4 Addresses */ public Set<String> getInterfaceIPs() { Set<String> interfaceIPs = new HashSet<>(); Enumeration<NetworkInterface> interfaces; try { interfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException ignored) { // If we are not allowed to enumerate, we return an empty result set. return interfaceIPs; } // For each interface ... for (Enumeration<NetworkInterface> en = interfaces; en.hasMoreElements();) { NetworkInterface networkInterface = en.nextElement(); boolean isLoopback = true; try { isLoopback = networkInterface.isLoopback(); } catch (SocketException ignored) { } if (!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()); } } } return interfaceIPs; }
From source file:org.openhab.binding.russound.internal.discovery.RioSystemDiscovery.java
/** * Starts the scan. For each network interface (that is up and not a loopback), all addresses will be iterated * and checked for something open on port 9621. If that port is open, a russound controller "type" command will be * issued. If the response is a correct pattern, we assume it's a rio system device and will emit a * {{@link #thingDiscovered(DiscoveryResult)} */// w w w . j a v a2 s . c o m @Override protected void startScan() { final List<NetworkInterface> interfaces; try { interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); } catch (SocketException e1) { logger.debug("Exception getting network interfaces: {}", e1.getMessage(), e1); return; } nbrNetworkInterfacesScanning = interfaces.size(); executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 10); for (final NetworkInterface networkInterface : interfaces) { try { if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; } } catch (SocketException e) { continue; } for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it .hasNext();) { final InterfaceAddress interfaceAddress = it.next(); // don't bother with ipv6 addresses (russound doesn't support) if (interfaceAddress.getAddress() instanceof Inet6Address) { continue; } final String subnetRange = interfaceAddress.getAddress().getHostAddress() + "/" + interfaceAddress.getNetworkPrefixLength(); logger.debug("Scanning subnet: {}", subnetRange); final SubnetUtils utils = new SubnetUtils(subnetRange); final String[] addresses = utils.getInfo().getAllAddresses(); for (final String address : addresses) { executorService.execute(new Runnable() { @Override public void run() { scanAddress(address); } }); } } } // Finishes the scan and cleans up stopScan(); }
From source file:net.mm2d.dmsexplorer.ServerListActivity.java
private Collection<NetworkInterface> getWifiInterface() { final NetworkInfo ni = mConnectivityManager.getActiveNetworkInfo(); if (ni == null || !ni.isConnected() || ni.getType() != ConnectivityManager.TYPE_WIFI) { return null; }//w ww. j av a 2s. c o m final InetAddress address = getWifiInetAddress(); if (address == null) { return null; } final Enumeration<NetworkInterface> nis; try { nis = NetworkInterface.getNetworkInterfaces(); } catch (final SocketException e) { return null; } while (nis.hasMoreElements()) { final NetworkInterface nif = nis.nextElement(); try { if (nif.isLoopback() || nif.isPointToPoint() || nif.isVirtual() || !nif.isUp()) { continue; } final List<InterfaceAddress> ifas = nif.getInterfaceAddresses(); for (final InterfaceAddress a : ifas) { if (a.getAddress().equals(address)) { final Collection<NetworkInterface> c = new ArrayList<>(); c.add(nif); return c; } } } catch (final SocketException ignored) { } } return null; }
From source file:com.sixt.service.framework.registry.consul.RegistrationManager.java
private void updateIpAddress() { try {//from w ww .j a v a2s . c o m Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces(); ipAddress = null; while (b.hasMoreElements()) { NetworkInterface iface = b.nextElement(); if (iface.getName().startsWith("dock")) { continue; } for (InterfaceAddress f : iface.getInterfaceAddresses()) { if (f.getAddress().isSiteLocalAddress()) { ipAddress = f.getAddress().getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } }
From source file:org.geometerplus.fbreader.plugin.local_opds_scanner.ScanLocalNetworkActivity.java
private void scan() { final List<InterfaceAddress> addresses = Util.getInterfaceAddresses(); if (addresses.isEmpty()) { runOnUiThread(new Runnable() { public void run() { setErrorText(R.string.no_local_connection); }/*from w w w. j av a 2 s. c o m*/ }); } else { for (final InterfaceAddress a : addresses) { new Thread() { public void run() { new ServiceCollector(a.getAddress()); } }.start(); } } }