List of usage examples for java.net NetworkInterface isLoopback
public boolean isLoopback() throws SocketException
From source file:net.ftb.util.OSUtils.java
/** * Grabs the mac address of computer and makes it 10 times longer * @return a byte array containing mac address *//*from ww w . ja v a 2s . c o m*/ public static byte[] getMacAddress() { if (cachedMacAddress != null && cachedMacAddress.length >= 10) { return cachedMacAddress; } try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface network = networkInterfaces.nextElement(); byte[] mac = network.getHardwareAddress(); if (mac != null && mac.length > 0 && !network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getName().substring(0, 3) != "ham") { Logger.logDebug("Interface: " + network.getDisplayName() + " : " + network.getName()); cachedMacAddress = new byte[mac.length * 10]; for (int i = 0; i < cachedMacAddress.length; i++) { cachedMacAddress[i] = mac[i - (Math.round(i / mac.length) * mac.length)]; } return cachedMacAddress; } } } catch (SocketException e) { Logger.logWarn("Exception getting MAC address", e); } Logger.logWarn("Failed to get MAC address, using default logindata key"); return new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; }
From source file:org.speechforge.zanzibar.sip.SipServer.java
public static InetAddress getLocalHost() throws SocketException, UnknownHostException { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); if (!networkInterface.isLoopback()) { Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); if (inetAddresses.hasMoreElements()) return inetAddresses.nextElement(); }/* w w w . ja v a 2 s.c om*/ } return InetAddress.getLocalHost(); }
From source file:com.distrimind.madkit.kernel.MadkitProperties.java
private static long getMacAddress() { long result = 0; long result2 = 0; try {// w w w .j a va 2s .c o m final Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { final NetworkInterface ni = e.nextElement(); if (!ni.isLoopback()) { long val = getHardwareAddress(ni.getHardwareAddress()); if (val != 0 && val != 224) { if (ni.isPointToPoint()) { result2 = val; } else { result = val; break; } } } } } catch (Exception e) { e.printStackTrace(); } if (result == 0) result = result2; return result; }
From source file:eu.stratosphere.nephele.discovery.DiscoveryService.java
/** * Returns the set of broadcast addresses available to the network interfaces of this host. In case of IPv6 the set * contains the IPv6 multicast address to reach all nodes on the local link. Moreover, all addresses of the loopback * interfaces are added to the set./* ww w . jav a 2 s . c o m*/ * * @return (possibly empty) set of broadcast addresses reachable by this host */ private static Set<InetAddress> getBroadcastAddresses() { final Set<InetAddress> broadcastAddresses = new HashSet<InetAddress>(); // get all network interfaces Enumeration<NetworkInterface> ie = null; try { ie = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Could not collect network interfaces of host", e); return broadcastAddresses; } while (ie.hasMoreElements()) { NetworkInterface nic = ie.nextElement(); try { if (!nic.isUp()) { continue; } if (nic.isLoopback()) { for (InterfaceAddress adr : nic.getInterfaceAddresses()) { broadcastAddresses.add(adr.getAddress()); } } else { // check all IPs bound to network interfaces for (InterfaceAddress adr : nic.getInterfaceAddresses()) { if (adr == null) { continue; } // collect all broadcast addresses if (USE_IPV6) { try { final InetAddress interfaceAddress = adr.getAddress(); if (interfaceAddress instanceof Inet6Address) { final Inet6Address ipv6Address = (Inet6Address) interfaceAddress; final InetAddress multicastAddress = InetAddress.getByName(IPV6MULTICASTADDRESS + "%" + Integer.toString(ipv6Address.getScopeId())); broadcastAddresses.add(multicastAddress); } } catch (UnknownHostException e) { LOG.error(e); } } else { final InetAddress broadcast = adr.getBroadcast(); if (broadcast != null) { broadcastAddresses.add(broadcast); } } } } } catch (SocketException e) { LOG.error("Socket exception when checking " + nic.getName() + ". " + "Ignoring this device.", e); } } return broadcastAddresses; }
From source file:org.chromium.ChromeSystemNetwork.java
private void getNetworkInterfaces(final CordovaArgs args, final CallbackContext callbackContext) { cordova.getThreadPool().execute(new Runnable() { @Override// w w w . j a v a 2 s . co m public void run() { try { JSONArray ret = new JSONArray(); ArrayList<NetworkInterface> interfaces = Collections .list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface iface : interfaces) { if (iface.isLoopback()) { continue; } for (InterfaceAddress interfaceAddress : iface.getInterfaceAddresses()) { InetAddress address = interfaceAddress.getAddress(); if (address == null) { continue; } JSONObject data = new JSONObject(); data.put("name", iface.getDisplayName()); // Strip address scope zones for IPv6 address. data.put("address", address.getHostAddress().replaceAll("%.*", "")); data.put("prefixLength", interfaceAddress.getNetworkPrefixLength()); ret.put(data); } } callbackContext.success(ret); } catch (Exception e) { Log.e(LOG_TAG, "Error occured while getting network interfaces", e); callbackContext.error("Could not get network interfaces"); } } }); }
From source file:org.peercast.pecaport.NetworkDeviceManager.java
/** * ?(eth0, eth1..)??/*from w w w . ja v a 2s .c om*/ */ public List<NetworkInterfaceInfo.Ethernet> getEthernetInterface() { List<NetworkInterfaceInfo.Ethernet> infos = new ArrayList<>(); try { for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (!ni.isLoopback() && !ni.isVirtual() && ni.getName().matches("^eth\\d+$") && // ni.getInetAddresses().hasMoreElements()) infos.add(new NetworkInterfaceInfo.Ethernet(ni)); } } catch (SocketException e) { Log.w(TAG, "getEthernetInterface()", e); } return infos; }
From source file:terrastore.communication.NodeConfiguration.java
private String getPublishAddresses() throws Exception { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); StringBuilder result = new StringBuilder(); while (nics != null && nics.hasMoreElements()) { NetworkInterface nic = nics.nextElement(); if (nic.isUp() && !nic.isLoopback() && !nic.isVirtual() && nic.getInetAddresses().hasMoreElements()) { Enumeration<InetAddress> addresses = nic.getInetAddresses(); while (addresses.hasMoreElements()) { if (result.length() > 0) { result.append(","); }/*from ww w. j a v a 2 s . c om*/ result.append(addresses.nextElement().getHostAddress()); } } } return result.toString(); }
From source file:org.openremote.controller.service.BeehiveCommandCheckService.java
public static String getMACAddresses() throws Exception { StringBuilder macs = new StringBuilder(); Enumeration<NetworkInterface> enum1 = NetworkInterface.getNetworkInterfaces(); while (enum1.hasMoreElements()) { NetworkInterface networkInterface = enum1.nextElement(); if (!networkInterface.isLoopback()) { boolean onlyLinkLocal = true; for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { if (!interfaceAddress.getAddress().isLinkLocalAddress()) { onlyLinkLocal = false; }/* w w w.java2 s.c o m*/ } if (onlyLinkLocal) { continue; } byte[] mac = networkInterface.getHardwareAddress(); if (mac != null) { macs.append(getMACString(networkInterface.getHardwareAddress())); macs.append(","); } } } if (macs.length() == 0) { return "no-mac-address-found"; } macs.deleteCharAt(macs.length() - 1); return macs.toString(); }
From source file:com.at.lic.LicenseControl.java
private String getMAC() throws Exception { String mac = "1:2:3:4:5:6:7:8"; // default mac address InetAddress addr = InetAddress.getLocalHost(); NetworkInterface ni = NetworkInterface.getByInetAddress(addr); if (ni.isLoopback() || ni.isVirtual()) { ni = null;//from www.j ava 2 s .c om Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface aNI = (NetworkInterface) nis.nextElement(); if (!aNI.isLoopback() && !aNI.isVirtual()) { ni = aNI; break; } } } if (ni != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); byte[] HAs = ni.getHardwareAddress(); for (int i = 0; i < HAs.length; i++) { ps.format("%02X:", HAs[i]); } mac = baos.toString(); if (mac.length() > 0) { mac = mac.replaceFirst(":$", ""); } ps.close(); baos.close(); } return mac; }
From source file:com.almende.arum.EventPusher.java
private String getHostAddress() throws SocketException { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); if (!n.isLoopback() && n.isUp() && !n.isVirtual()) { Enumeration<InetAddress> ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); if (i instanceof Inet4Address && !i.isLinkLocalAddress() && !i.isMulticastAddress()) { return i.getHostAddress().trim(); }/*from w w w .j a va 2 s . c o m*/ } } } return null; }