List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:com.qwazr.server.configuration.ServerConfiguration.java
/** * Manage that kind of pattern:// w w w . j a va 2 s. com * 192.168.0.0/16,172.168.0.0/16 * 192.168.0.0/16 * 10.3.12.12 * * @param addressPattern a mask or an ip address * @param collect a collection filled with the matching addresses * @throws SocketException */ private static void findMatchingAddress(final String addressPattern, final Collection<String> collect) throws SocketException { final String[] patterns = StringUtils.split(addressPattern, ",; "); if (patterns == null) return; for (String pattern : patterns) { if (pattern == null) continue; pattern = pattern.trim(); if (!pattern.contains("/")) { collect.add(pattern); continue; } final SubnetUtils.SubnetInfo subnet = pattern.contains("/") ? new SubnetUtils(pattern).getInfo() : null; final Enumeration<NetworkInterface> enumInterfaces = NetworkInterface.getNetworkInterfaces(); while (enumInterfaces != null && enumInterfaces.hasMoreElements()) { final NetworkInterface ifc = enumInterfaces.nextElement(); if (!ifc.isUp()) continue; final Enumeration<InetAddress> enumAddresses = ifc.getInetAddresses(); while (enumAddresses != null && enumAddresses.hasMoreElements()) { final InetAddress inetAddress = enumAddresses.nextElement(); if (!(inetAddress instanceof Inet4Address)) continue; final String address = inetAddress.getHostAddress(); if (subnet != null && subnet.isInRange(address) || address.equals(pattern)) collect.add(address); } } } }
From source file:com.git.original.common.utils.IPUtils.java
/** * ?IP?/*from w ww . java2 s. c o m*/ * <p> * :<br/> * 1. 192.xxx.xxx.xxx<br> * 2. 172.xxx.xxx.xxx<br> * 3. 10.xxx.xxx.xxx<br> * other<br> * * @return ipv4? * @throws SocketException */ private static synchronized int doGetLocalIp() throws SocketException { if (localIp != 0) { return localIp; } Integer ipStartWith10 = null; Integer ipStartWith172 = null; Integer other = null; /* * ?IP? */ Enumeration<NetworkInterface> interfaceEnum = NetworkInterface.getNetworkInterfaces(); while (interfaceEnum.hasMoreElements()) { NetworkInterface netInterface = interfaceEnum.nextElement(); if (!netInterface.isUp()) { continue; } Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses(); while (addrEnum.hasMoreElements()) { InetAddress addr = addrEnum.nextElement(); String hostAddr = addr.getHostAddress(); if (hostAddr.startsWith("192.")) { localIp = ByteUtils.toInt(addr.getAddress()); return localIp; } else if (ipStartWith172 == null && hostAddr.startsWith("172.")) { ipStartWith172 = ByteUtils.toInt(addr.getAddress()); } else if (ipStartWith10 == null && hostAddr.startsWith("10.")) { ipStartWith10 = ByteUtils.toInt(addr.getAddress()); } else if (other == null && (addr instanceof Inet4Address)) { other = ByteUtils.toInt(addr.getAddress()); } } } if (ipStartWith172 != null) { localIp = ipStartWith172; return localIp; } else if (ipStartWith10 != null) { localIp = ipStartWith10; return localIp; } else if (other != null) { localIp = other; return localIp; } throw new RuntimeException("can not get Local Server IPv4 Address"); }
From source file:org.apache.synapse.transport.nhttp.DefaultHttpGetProcessor.java
/** * Whatever this method returns as the IP is ignored by the actual http/s listener when * its getServiceEPR is invoked. This was originally copied from axis2 * * @return Returns String./*from www. j av a 2 s . c om*/ * @throws java.net.SocketException if the socket can not be accessed */ protected static String getIpAddress() throws SocketException { Enumeration e = NetworkInterface.getNetworkInterfaces(); String address = "127.0.0.1"; while (e.hasMoreElements()) { NetworkInterface netface = (NetworkInterface) e.nextElement(); Enumeration addresses = netface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress ip = (InetAddress) addresses.nextElement(); if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) { return ip.getHostAddress(); } } } return address; }
From source file:com.entertailion.android.slideshow.utils.Utils.java
public static final InetAddress getLocalInetAddress() { InetAddress selectedInetAddress = null; try {// ww w. j a va 2 s. com for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.isUp()) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { if (inetAddress instanceof Inet4Address) { // only // want // ipv4 // address if (inetAddress.getHostAddress().toString().charAt(0) != '0') { if (selectedInetAddress == null) { selectedInetAddress = inetAddress; } else if (intf.getName().startsWith("eth")) { // prefer // wired // interface selectedInetAddress = inetAddress; } } } } } } } return selectedInetAddress; } catch (Throwable e) { Log.e(LOG_TAG, "Failed to get the IP address", e); } return null; }
From source file:de.madvertise.android.sdk.MadvertiseUtil.java
/** * Fetch the address of the enabled interface * /*from w w w . j a v a 2 s . c om*/ * @return ip address as string */ public static String getLocalIpAddress(MadvertiseViewCallbackListener listener) { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { if (inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress().toString(); } } } } } catch (SocketException e) { if (listener != null) { listener.onError(e); } e.printStackTrace(); } if (listener != null) { listener.onError(new IllegalArgumentException("Couldn't obtain the local ip address")); } return ""; }
From source file:com.screenslicer.common.CommonUtil.java
public static String ip() { if (myIp != null) { return myIp; }/*from w w w .ja v a2s.c o m*/ try { Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); Enumeration<InetAddress> addrs = iface.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); String ip = addr.getHostAddress(); if (ip.contains(".") && !ip.startsWith("127.") && !ip.startsWith("192.168.") && !ip.startsWith("10.") && !ip.startsWith("172.16.") && !ip.startsWith("172.17.") && !ip.startsWith("172.18.") && !ip.startsWith("172.19.") && !ip.startsWith("172.20.") && !ip.startsWith("172.21.") && !ip.startsWith("172.22.") && !ip.startsWith("172.23.") && !ip.startsWith("172.24.") && !ip.startsWith("172.25.") && !ip.startsWith("172.26.") && !ip.startsWith("172.27.") && !ip.startsWith("172.28.") && !ip.startsWith("172.29.") && !ip.startsWith("172.30.") && !ip.startsWith("172.31.") && !ip.startsWith("169.254.") && !ip.startsWith("224.") && !ip.startsWith("225.") && !ip.startsWith("226.") && !ip.startsWith("227.") && !ip.startsWith("228.") && !ip.startsWith("229.") && !ip.startsWith("230.") && !ip.startsWith("231.") && !ip.startsWith("232.") && !ip.startsWith("233.") && !ip.startsWith("234.") && !ip.startsWith("235.") && !ip.startsWith("236.") && !ip.startsWith("237.") && !ip.startsWith("238.") && !ip.startsWith("239.") && !ip.startsWith("255.255.255.255")) { return ip; } } } } catch (SocketException e) { Log.exception(e); } return "127.0.0.1"; }
From source file:net.grinder.util.NetworkUtils.java
/** * Check if the current machine support IP6 * * @return true if the IP6 is supported. *///from w ww . j av a 2 s . co m public static boolean isIP6Supported() { final Enumeration<NetworkInterface> networkInterfaces; try { networkInterfaces = getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { final NetworkInterface networkInterface = networkInterfaces.nextElement(); if (networkInterface.isUp() && !networkInterface.isLoopback() && !networkInterface.isPointToPoint()) { final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { final InetAddress inetAddress = inetAddresses.nextElement(); if (inetAddress instanceof Inet6Address) { return true; } } } } } catch (SocketException e) { LOGGER.error("Error while resolving non look back local addresses.", e); } return false; }
From source file:hu.netmind.beankeeper.node.impl.NodeManagerImpl.java
/** * Get the server addresses from interfaces. *//* w ww . j a va2s .com*/ public static String getHostAddresses() { try { Enumeration interfaceEnumeration = NetworkInterface.getNetworkInterfaces(); // Copy from enumeration to addresses vector, but filter loopback addresses ArrayList addresses = new ArrayList(); while (interfaceEnumeration.hasMoreElements()) { NetworkInterface intf = (NetworkInterface) interfaceEnumeration.nextElement(); // Remove loopback addresses Enumeration addressEnumeration = intf.getInetAddresses(); while (addressEnumeration.hasMoreElements()) { InetAddress address = (InetAddress) addressEnumeration.nextElement(); // Insert to addresses only if not loopback and not link local if ((!address.isLoopbackAddress()) && (!address.isLinkLocalAddress())) addresses.add(address); } } // Pick one address from the remaining address space logger.debug("server available local addresses: " + addresses); // Now, multiple addresses are in the list, so copy all of them // into the result string. StringBuffer ips = new StringBuffer(); for (int i = 0; i < addresses.size(); i++) { InetAddress address = (InetAddress) addresses.get(i); if (ips.length() > 0) ips.append(","); ips.append(address.getHostAddress()); } return ips.toString(); } catch (StoreException e) { throw e; } catch (Exception e) { throw new StoreException("exception while determining server address", e); } }
From source file:edu.umass.cs.gigapaxos.testing.TESTPaxosConfig.java
/** * @param myID// w ww .ja v a 2 s. com * @return True if found my IP. * @throws SocketException */ public static boolean findMyIP(Integer myID) throws SocketException { if (myID == null) return false; Enumeration<NetworkInterface> netfaces = NetworkInterface.getNetworkInterfaces(); ArrayList<InetAddress> myIPs = new ArrayList<InetAddress>(); while (netfaces.hasMoreElements()) { NetworkInterface iface = netfaces.nextElement(); Enumeration<InetAddress> allIPs = iface.getInetAddresses(); while (allIPs.hasMoreElements()) { InetAddress addr = allIPs.nextElement(); if ((addr instanceof Inet4Address)) myIPs.add((InetAddress) addr); } } System.out.println(myIPs); boolean found = false; if (myIPs.contains(getNodeConfig().getNodeAddress(myID))) { found = true; } if (found) System.out.println("Found my IP"); else { System.out.println("\n\n****Could not locally find the IP " + getNodeConfig().getNodeAddress(myID) + "; should change all addresses to localhost instead.****\n\n."); } return found; }
From source file:org.nuxeo.ecm.core.management.statuses.NuxeoInstanceIdentifierHelper.java
public static String generateHardwareUID() throws Exception { String hwUID = ""; String javaVersion = System.getProperty("java.version"); Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); while (ifs.hasMoreElements()) { NetworkInterface ni = ifs.nextElement(); if (javaVersion.contains("1.6")) { // ni.getHardwareAddress() only in jdk 1.6 Method[] methods = ni.getClass().getMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("getHardwareAddress")) { byte[] hwAddr = (byte[]) method.invoke(ni); if (hwAddr != null) { hwUID = hwUID + "-" + Base64.encodeBytes(hwAddr); }//from w w w . j a v a 2 s . c om break; } } } else { Enumeration<InetAddress> addrs = ni.getInetAddresses(); while (addrs.hasMoreElements()) { hwUID = hwUID + "-" + Base64.encodeBytes(addrs.nextElement().getAddress()); } } } return hwUID; }