List of usage examples for java.net NetworkInterface getInterfaceAddresses
public java.util.List<InterfaceAddress> getInterfaceAddresses()
From source file:org.programmatori.domotica.own.plugin.system.System.java
private Value getNetMask() { Value v = null;// w w w . j a v a2 s . co m try { InetAddress thisIp = InetAddress.getLocalHost(); String ip = ""; NetworkInterface networkInterface = NetworkInterface.getByInetAddress(thisIp); short mask = networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength(); switch (mask) { // IPv4 case 8: ip = "255.0.0.0"; break; case 16: ip = "255.255.0.0"; break; case 24: ip = "255.255.255.0"; break; // IPv6 // case 128: // ip = "::1/128"; // break; // // case 10: // ip = "fe80::203:baff:fe27:1243/10"; // break; default: ip = "255.255.255.0"; break; } for (int i = 0; i < 3; i++) { if (i == 0) { v = new Value(ip.substring(0, ip.indexOf('.'))); // IP Part } else { v.addValue(ip.substring(0, ip.indexOf('.'))); // IP Part } ip = ip.substring(ip.indexOf('.') + 1); } v.addValue(ip); // IP End Part } catch (Exception e) { e.printStackTrace(); } return v; }
From source file:com.sixt.service.framework.registry.consul.RegistrationManager.java
private void updateIpAddress() { try {// w w w. ja 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.openhab.binding.harmonyhub.discovery.HarmonyHubDiscovery.java
/** * Send broadcast message over all active interfaces * * @param discoverString//from w w w . jav a 2 s . c o m * String to be used for the discovery */ private void sendDiscoveryMessage(String discoverString) { DatagramSocket bcSend = null; try { bcSend = new DatagramSocket(); bcSend.setBroadcast(true); byte[] sendData = discoverString.getBytes(); // Broadcast the message over all the network interfaces Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; } for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress[] broadcast = null; if (StringUtils.isNotBlank(optionalHost)) { try { broadcast = new InetAddress[] { InetAddress.getByName(optionalHost) }; } catch (Exception e) { logger.error("Could not use host for hub discovery", e); return; } } else { broadcast = new InetAddress[] { InetAddress.getByName("224.0.0.1"), InetAddress.getByName("255.255.255.255"), interfaceAddress.getBroadcast() }; } for (InetAddress bc : broadcast) { // Send the broadcast package! if (bc != null) { try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc, DISCO_PORT); bcSend.send(sendPacket); } catch (IOException e) { logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage()); } catch (Exception e) { logger.debug(e.getMessage(), e); } logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(), networkInterface.getDisplayName()); } } } } } catch (IOException e) { logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage()); } finally { try { if (bcSend != null) { bcSend.close(); } } catch (Exception e) { // Ignore } } }
From source file:org.sltpaya.tool.Utils.java
/** * ?APN?/* w w w . j a va2 s . c o m*/ * @return ? */ private boolean getApnStatus() { Matcher matcher; String name; try { Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces(); if (niList != null) { for (NetworkInterface intf : Collections.list(niList)) { if (!intf.isUp() || intf.getInterfaceAddresses().size() == 0) { continue; } Log.d(TAG, "isVpnUsed() NetworkInterface Name: " + intf.getName()); name = intf.getName(); matcher = Pattern.compile("tun\\d").matcher(name); if (matcher.find()) { System.out.println("vpn??????" + matcher.group()); return true; } } } } catch (Throwable e) { e.printStackTrace(); } return false; }
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 . ja v a 2s. 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; }/*from w ww . ja va 2 s . c om*/ 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:nfinity.FindPeer.java
public String ScanNetwork() { try {/* w w w . j a v a 2 s. co m*/ Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) { Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { if (!inetAddress.getClass().toString().equals("class java.net.Inet6Address")) { if (!inetAddress.isLoopbackAddress()) { try { SubnetUtils utils = new SubnetUtils(inetAddress.getHostAddress() + "/" + netint.getInterfaceAddresses().get(1).getNetworkPrefixLength()); String[] allIps = utils.getInfo().getAllAddresses(); return ConnectIP(allIps, inetAddress); } catch (IllegalArgumentException ex) { int prefix = NetworkInterface.getByInetAddress(inetAddress).getInterfaceAddresses() .get(0).getNetworkPrefixLength(); if (!inetAddress.isLoopbackAddress()) { System.out .println("IP: " + inetAddress.getHostAddress() + " Prefix: " + prefix); SubnetUtils utils = new SubnetUtils( inetAddress.getHostAddress() + "/" + prefix); String[] allIps = utils.getInfo().getAllAddresses(); return ConnectIP(allIps, inetAddress); } } } } } } } catch (SocketException ex) { System.out.println("Connection failed " + ex.getMessage()); } return "Peer not found"; }
From source file:com.cloud.utils.net.NetUtils.java
public static String[] getNetworkParams(final NetworkInterface nic) { final List<InterfaceAddress> addrs = nic.getInterfaceAddresses(); if (addrs == null || addrs.size() == 0) { return null; }//ww w. ja v a2 s. c o m InterfaceAddress addr = null; for (final InterfaceAddress iaddr : addrs) { final InetAddress inet = iaddr.getAddress(); if (!inet.isLinkLocalAddress() && !inet.isLoopbackAddress() && !inet.isMulticastAddress() && inet.getAddress().length == 4) { addr = iaddr; break; } } if (addr == null) { return null; } final String[] result = new String[3]; result[0] = addr.getAddress().getHostAddress(); try { final byte[] mac = nic.getHardwareAddress(); result[1] = byte2Mac(mac); } catch (final SocketException e) { s_logger.debug("Caught exception when trying to get the mac address ", e); } result[2] = prefix2Netmask(addr.getNetworkPrefixLength()); return result; }
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 w w w .ja v a2s . c o 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.eclipse.smarthome.binding.lifx.internal.LifxLightDiscovery.java
@Override protected void activate(Map<String, Object> configProperties) { super.activate(configProperties); broadcastAddresses = new ArrayList<InetSocketAddress>(); interfaceAddresses = new ArrayList<InetAddress>(); Enumeration<NetworkInterface> networkInterfaces = null; try {/* w w w . j a v a 2s . c o m*/ networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { logger.debug("An exception occurred while discovering LIFX lights : '{}'", e.getMessage()); } if (networkInterfaces != null) { while (networkInterfaces.hasMoreElements()) { NetworkInterface iface = networkInterfaces.nextElement(); try { if (iface.isUp() && !iface.isLoopback()) { for (InterfaceAddress ifaceAddr : iface.getInterfaceAddresses()) { if (ifaceAddr.getAddress() instanceof Inet4Address) { logger.debug("Adding '{}' as interface address with MTU {}", ifaceAddr.getAddress(), iface.getMTU()); if (iface.getMTU() > bufferSize) { bufferSize = iface.getMTU(); } interfaceAddresses.add(ifaceAddr.getAddress()); if (ifaceAddr.getBroadcast() != null) { logger.debug("Adding '{}' as broadcast address", ifaceAddr.getBroadcast()); broadcastAddresses .add(new InetSocketAddress(ifaceAddr.getBroadcast(), BROADCAST_PORT)); } } } } } catch (SocketException e) { logger.debug("An exception occurred while discovering LIFX lights : '{}'", e.getMessage()); } } } }