Java Code Examples for java.net.InetAddress#isLinkLocalAddress()
The following examples show how to use
java.net.InetAddress#isLinkLocalAddress() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonUtils.java From WeBASE-Front with Apache License 2.0 | 6 votes |
/** * get server ip. * * @return */ public static String getCurrentIp() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface ni = networkInterfaces.nextElement(); Enumeration<InetAddress> nias = ni.getInetAddresses(); while (nias.hasMoreElements()) { InetAddress ia = nias.nextElement(); if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { return ia.getHostAddress(); } } } } catch (SocketException e) { log.error("getCurrentIp error."); } return null; }
Example 2
Source File: BeaconService.java From c5-replicator with Apache License 2.0 | 6 votes |
@NotNull private List<String> getLocalIPs() throws SocketException { List<String> ips = new LinkedList<>(); for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) { NetworkInterface networkInterface = interfaces.nextElement(); if (networkInterface.isPointToPoint()) { continue; //ignore tunnel type interfaces } for (Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); addrs.hasMoreElements(); ) { InetAddress addr = addrs.nextElement(); if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isAnyLocalAddress()) { continue; } ips.add(addr.getHostAddress()); } } return ips; }
Example 3
Source File: InternetProtocolUtils.java From AuthMeReloaded with GNU General Public License v3.0 | 6 votes |
/** * Checks if the specified address is a private or loopback address * * @param address address to check * @return true if the address is a local (site and link) or loopback address, false otherwise */ public static boolean isLocalAddress(String address) { try { InetAddress inetAddress = InetAddress.getByName(address); // Examples: 127.0.0.1, localhost or [::1] return isLoopbackAddress(address) // Example: 10.0.0.0, 172.16.0.0, 192.168.0.0, fec0::/10 (deprecated) // Ref: https://en.wikipedia.org/wiki/IP_address#Private_addresses || inetAddress.isSiteLocalAddress() // Example: 169.254.0.0/16, fe80::/10 // Ref: https://en.wikipedia.org/wiki/IP_address#Address_autoconfiguration || inetAddress.isLinkLocalAddress() // non deprecated unique site-local that java doesn't check yet -> fc00::/7 || isIPv6UniqueSiteLocal(inetAddress); } catch (UnknownHostException e) { return false; } }
Example 4
Source File: HostInfo.java From DeviceConnect-Android with MIT License | 6 votes |
boolean shouldIgnorePacket(DatagramPacket packet) { boolean result = false; if (this.getInetAddress() != null) { InetAddress from = packet.getAddress(); if (from != null) { if (from.isLinkLocalAddress() && (!this.getInetAddress().isLinkLocalAddress())) { // Ignore linklocal packets on regular interfaces, unless this is // also a linklocal interface. This is to avoid duplicates. This is // a terrible hack caused by the lack of an API to get the address // of the interface on which the packet was received. result = true; } if (from.isLoopbackAddress() && (!this.getInetAddress().isLoopbackAddress())) { // Ignore loopback packets on a regular interface unless this is also a loopback interface. result = true; } } } return result; }
Example 5
Source File: LinkAddress.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
/** * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and * RFC 6724 section 3.2. * @hide */ private static int scopeForUnicastAddress(InetAddress addr) { if (addr.isAnyLocalAddress()) { return RT_SCOPE_HOST; } if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) { return RT_SCOPE_LINK; } // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2 // says that they are assigned global scope. if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) { return RT_SCOPE_SITE; } return RT_SCOPE_UNIVERSE; }
Example 6
Source File: NetworkUtils.java From letv with Apache License 2.0 | 6 votes |
public static String getIP() { try { Enumeration<?> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { Enumeration<InetAddress> enumIpAddr = ((NetworkInterface) e.nextElement()).getInetAddresses(); while (enumIpAddr.hasMoreElements()) { InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement(); if (!(inetAddress.isLoopbackAddress() || inetAddress.isLinkLocalAddress())) { String ip = inetAddress.getHostAddress().toString(); String[] nums = ip.split("\\."); if (nums.length == 4 && !"0".equals(nums[1])) { return ip; } } } } } catch (Throwable e2) { LogTool.e(TAG, "", e2); } return ""; }
Example 7
Source File: MainActivity.java From ADBToolKitsInstaller with GNU General Public License v3.0 | 6 votes |
public String getLocalIpAddress() { 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() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) { return inetAddress.getHostAddress(); } } } } catch (SocketException ex) { ex.printStackTrace(); } return "0.0.0.0"; }
Example 8
Source File: NetUtil.java From openhab-core with Eclipse Public License 2.0 | 6 votes |
/** * Get all broadcast addresses on the current host * * @return list of broadcast addresses, empty list if no broadcast addresses found */ public static List<String> getAllBroadcastAddresses() { List<String> broadcastAddresses = new LinkedList<>(); try { final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { final NetworkInterface networkInterface = networkInterfaces.nextElement(); final List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses(); for (InterfaceAddress interfaceAddress : interfaceAddresses) { final InetAddress addr = interfaceAddress.getAddress(); if (addr instanceof Inet4Address && !addr.isLinkLocalAddress() && !addr.isLoopbackAddress()) { InetAddress broadcast = interfaceAddress.getBroadcast(); if (broadcast != null) { broadcastAddresses.add(broadcast.getHostAddress()); } } } } } catch (SocketException ex) { LOGGER.error("Could not find broadcast address: {}", ex.getMessage(), ex); } return broadcastAddresses; }
Example 9
Source File: GatewayDns.java From arcusplatform with Apache License 2.0 | 6 votes |
public static InetAddress resolv(String host) throws UnknownHostException { try { InetAddress result = InetAddress.getByName(host); // NOTE: We check for a non-routable here and consider // that a failed hostname lookup. The DNS server of the // 4G dongle will do DNS hijacking in some cases to // redirect traffic to itself. if (denySiteLocal && (result.isSiteLocalAddress() || result.isLinkLocalAddress() || result.isLoopbackAddress())) { throw new UnknownHostException(host); } resolved.put(host, result); return result; } catch (Exception ex) { InetAddress previous = resolved.get(host); if (previous != null) { return previous; } throw ex; } }
Example 10
Source File: LinkLocalInterfaceCriteria.java From wildfly-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * {@inheritDoc} * * @return <code>address</code> if <code>address</code> is * {@link InetAddress#isLinkLocalAddress() link-local}. */ @Override protected InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException { if( address.isLinkLocalAddress() ) return address; return null; }
Example 11
Source File: Net.java From Bytecoder with Apache License 2.0 | 5 votes |
static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort) throws IOException { if (remote.isLinkLocalAddress()) { remote = IPAddressUtil.toScopedAddress(remote); } boolean preferIPv6 = isIPv6Available() && (family != StandardProtocolFamily.INET); return connect0(preferIPv6, fd, remote, remotePort); }
Example 12
Source File: TestRegionServerHostname.java From hbase with Apache License 2.0 | 5 votes |
@Test public void testConflictRegionServerHostnameConfigurationsAbortServer() throws Exception { Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); while (netInterfaceList.hasMoreElements()) { NetworkInterface ni = netInterfaceList.nextElement(); Enumeration<InetAddress> addrList = ni.getInetAddresses(); // iterate through host addresses and use each as hostname while (addrList.hasMoreElements()) { InetAddress addr = addrList.nextElement(); if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) { continue; } String hostName = addr.getHostName(); LOG.info("Found " + hostName + " on " + ni); TEST_UTIL.getConfiguration().set(DNS.MASTER_HOSTNAME_KEY, hostName); // "hbase.regionserver.hostname" and "hbase.regionserver.hostname.disable.master.reversedns" // are mutually exclusive. Exception should be thrown if both are used. TEST_UTIL.getConfiguration().set(DNS.RS_HOSTNAME_KEY, hostName); TEST_UTIL.getConfiguration().setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); try { StartMiniClusterOption option = StartMiniClusterOption.builder() .numMasters(NUM_MASTERS).numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); TEST_UTIL.startMiniCluster(option); } catch (Exception e) { Throwable t1 = e.getCause(); Throwable t2 = t1.getCause(); assertTrue(t1.getMessage()+" - "+t2.getMessage(), t2.getMessage().contains( HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY + " and " + DNS.RS_HOSTNAME_KEY + " are mutually exclusive")); return; } finally { TEST_UTIL.shutdownMiniCluster(); } assertTrue("Failed to validate against conflict hostname configurations", false); } } }
Example 13
Source File: DHTTransportUDPImpl.java From BiglyBT with GNU General Public License v2.0 | 5 votes |
protected boolean invalidExternalAddress( InetAddress ia ) { return( ia.isLinkLocalAddress() || ia.isLoopbackAddress() || ia.isSiteLocalAddress()); }
Example 14
Source File: TestRegionServerHostname.java From hbase with Apache License 2.0 | 5 votes |
@Test public void testRegionServerHostname() throws Exception { Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); while (netInterfaceList.hasMoreElements()) { NetworkInterface ni = netInterfaceList.nextElement(); Enumeration<InetAddress> addrList = ni.getInetAddresses(); // iterate through host addresses and use each as hostname while (addrList.hasMoreElements()) { InetAddress addr = addrList.nextElement(); if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress() || !addr.isSiteLocalAddress()) { continue; } String hostName = addr.getHostName(); LOG.info("Found " + hostName + " on " + ni + ", addr=" + addr); TEST_UTIL.getConfiguration().set(DNS.MASTER_HOSTNAME_KEY, hostName); TEST_UTIL.getConfiguration().set(DNS.RS_HOSTNAME_KEY, hostName); StartMiniClusterOption option = StartMiniClusterOption.builder() .numMasters(NUM_MASTERS).numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); TEST_UTIL.startMiniCluster(option); try { ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher(); List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode); // there would be NUM_RS+1 children - one for the master assertTrue(servers.size() == NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 1: 0)); for (String server : servers) { assertTrue("From zookeeper: " + server + " hostname: " + hostName, server.startsWith(hostName.toLowerCase(Locale.ROOT)+",")); } zkw.close(); } finally { TEST_UTIL.shutdownMiniCluster(); } } } }
Example 15
Source File: Helperfunctions.java From open-rmbt with Apache License 2.0 | 5 votes |
public static String IpType(InetAddress inetAddress) { try { final String ipVersion; if (inetAddress instanceof Inet4Address) ipVersion = "ipv4"; else if (inetAddress instanceof Inet6Address) ipVersion = "ipv6"; else ipVersion = "ipv?"; if (inetAddress.isAnyLocalAddress()) return "wildcard_" + ipVersion; if (inetAddress.isSiteLocalAddress()) return "site_local_" + ipVersion; if (inetAddress.isLinkLocalAddress()) return "link_local_" + ipVersion; if (inetAddress.isLoopbackAddress()) return "loopback_" + ipVersion; return "public_" + ipVersion; } catch (final IllegalArgumentException e) { return "illegal_ip"; } }
Example 16
Source File: JainSipClient.java From restcomm-android-sdk with GNU Affero General Public License v3.0 | 4 votes |
private String interface2Address(boolean useIPv4, String networkInterfacePrefix) throws SocketException { RCLogger.i(TAG, "interface2Address(): searching for address using prefix regex: " + networkInterfacePrefix); String stringAddress = ""; List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { RCLogger.i(TAG, "interface2Address(): Current interface: " + intf.toString()); if (intf.isUp()) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { // IP-Address has to be of Global Scope to make sip requests if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress() && !addr.isAnyLocalAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); RCLogger.i(TAG, "interface2Address(): Current address (if): " + sAddr + " (" + intf.getName() + ")"); //regex match example: v4-rmnet-data0, rmnet-data0, radio0, eth0, etc.. if (intf.getName().matches("(.+)?"+networkInterfacePrefix+".*")) { boolean isIPv4 = addr instanceof Inet4Address; //InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { stringAddress = sAddr; break; } } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port // suffix stringAddress = delim < 0 ? sAddr : sAddr.substring(0, delim); break; } } } } } } else { RCLogger.i(TAG, "interface2Address(): Interface not matching or down: " + intf.toString() + " isUp: " + intf.isUp()); } } // One issue that isn't 100% from resources around the web is whether the interface names are standard across Android flavours. We assume that cellular data will always be // rmnet* and ethernet will always be eth*. To that end let's print out all the interfaces in case we cannot find an ip address for current network type so that we can troubleshoot // right away in that unlikely event. RCLogger.v(TAG, "interface2Address(): stringAddress: " + stringAddress + ", for currently active network: " + networkInterfacePrefix + ", interfaces: " + interfaces.toString()); if (stringAddress.isEmpty()) { RCLogger.e(TAG, "interface2Address(): Couldn't retrieve IP address for currently active network"); throw new RuntimeException("Failed to find a viable network interface to use for signaling facilities"); } return stringAddress; }
Example 17
Source File: ClientSharedUtils.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** returns a set of the non-loopback InetAddresses for this machine */ public static Set<InetAddress> getMyAddresses(boolean includeLocal) throws SocketException { Set<InetAddress> result = new HashSet<InetAddress>(); Set<InetAddress> locals = new HashSet<InetAddress>(); Enumeration<NetworkInterface> interfaces = NetworkInterface .getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface face = interfaces.nextElement(); boolean faceIsUp = false; try { // invoking using JdkHelper since GemFireXD JDBC3 clients require JDK 1.5 faceIsUp = helper.isInterfaceUp(face); } catch (SocketException se) { final Logger log = getLogger(); if (log != null) { LogRecord lr = new LogRecord(Level.INFO, "Failed to check if network interface is up. Skipping " + face); lr.setThrown(se); log.log(lr); } } if (faceIsUp) { Enumeration<InetAddress> addrs = face.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); if (addr.isLoopbackAddress() || addr.isAnyLocalAddress() || (!useLinkLocalAddresses && addr.isLinkLocalAddress())) { locals.add(addr); } else { result.add(addr); } } // while } } // while // fix for bug #42427 - allow product to run on a standalone box by using // local addresses if there are no non-local addresses available if (result.size() == 0) { return locals; } else { if (includeLocal) { result.addAll(locals); } return result; } }
Example 18
Source File: AddressUtils.java From cosmic with Apache License 2.0 | 4 votes |
public static boolean isLocalAddress(final String address) throws UnknownHostException { final InetAddress inetAddress = InetAddress.getByName(address); return inetAddress.isLinkLocalAddress() || inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress(); }
Example 19
Source File: ServiceInstanceBuilder.java From xian with Apache License 2.0 | 4 votes |
@Override public boolean use(NetworkInterface nif, InetAddress adr) throws SocketException { return (adr != null) && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()); }
Example 20
Source File: PlatformTorrentUtils.java From BiglyBT with GNU General Public License v2.0 | 3 votes |
public static boolean isPlatformHost(String host) { Object[] domains = getPlatformHosts().toArray(); host = host.toLowerCase(); for (int i = 0; i < domains.length; i++) { String domain = (String) domains[i]; if (domain.equals(host)) { return (true); } if (host.endsWith("." + domain)) { return (true); } } if ( Constants.isCVSVersion()){ // allow local addresses for testing try{ InetAddress ia = InetAddress.getByName( host ); return( ia.isLoopbackAddress() || ia.isLinkLocalAddress() || ia.isSiteLocalAddress()); }catch( Throwable e ){ } } return (false); }