本文整理汇总了Java中java.net.InetAddress.isLinkLocalAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.isLinkLocalAddress方法的具体用法?Java InetAddress.isLinkLocalAddress怎么用?Java InetAddress.isLinkLocalAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.isLinkLocalAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getGlobalAddresses
import java.net.InetAddress; //导入方法依赖的package包/类
/** Returns all global scope addresses for interfaces that are up. */
static InetAddress[] getGlobalAddresses() throws SocketException {
List<InetAddress> list = new ArrayList<>();
for (NetworkInterface intf : getInterfaces()) {
if (intf.isUp()) {
for (InetAddress address : Collections.list(intf.getInetAddresses())) {
if (address.isLoopbackAddress() == false &&
address.isSiteLocalAddress() == false &&
address.isLinkLocalAddress() == false) {
list.add(address);
}
}
}
}
if (list.isEmpty()) {
throw new IllegalArgumentException("No up-and-running global-scope (public) addresses found, got " + getInterfaces());
}
return list.toArray(new InetAddress[list.size()]);
}
示例2: getLocalInetAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Retrieve the first validated local ip address(the Public and LAN ip addresses are validated).
*
* @return the local address
* @throws SocketException the socket exception
*/
public static InetAddress getLocalInetAddress() throws SocketException {
// enumerates all network interfaces
Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();
while (enu.hasMoreElements()) {
NetworkInterface ni = enu.nextElement();
if (ni.isLoopback()) {
continue;
}
Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
while (addressEnumeration.hasMoreElements()) {
InetAddress address = addressEnumeration.nextElement();
// ignores all invalidated addresses
if (address.isLinkLocalAddress() || address.isLoopbackAddress() || address.isAnyLocalAddress()) {
continue;
}
return address;
}
}
throw new RuntimeException("No validated local address!");
}
示例3: testRDNS
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* TestCase: get our local address and reverse look it up
*/
@Test
public void testRDNS() throws Exception {
InetAddress localhost = getLocalIPAddr();
try {
String s = DNS.reverseDns(localhost, null);
LOG.info("Local revers DNS hostname is " + s);
} catch (NameNotFoundException e) {
if (!localhost.isLinkLocalAddress() || localhost.isLoopbackAddress()) {
//these addresses probably won't work with rDNS anyway, unless someone
//has unusual entries in their DNS server mapping 1.0.0.127 to localhost
LOG.info("Reverse DNS failing as due to incomplete networking", e);
LOG.info("Address is " + localhost
+ " Loopback=" + localhost.isLoopbackAddress()
+ " Linklocal=" + localhost.isLinkLocalAddress());
}
}
}
示例4: getInventoryEndpoints
import java.net.InetAddress; //导入方法依赖的package包/类
public List<String> getInventoryEndpoints() {
List<String> endpoints = new ArrayList<>(2);
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress addr : Collections.list(inetAddresses)) {
if (!addr.isLinkLocalAddress() && addr.getClass() == Inet4Address.class) {
endpoints.add(
String.format("http://%s:%d/v1/grapher/inventory", addr.getHostAddress(), port));
}
}
}
} catch (SocketException e) {
logger.error("Exception looking up network interfaces", e);
}
return endpoints;
}
示例5: getPublicIPs6
import java.net.InetAddress; //导入方法依赖的package包/类
static public List<String> getPublicIPs6() {
List<String> ips6 = new ArrayList<String>();
Enumeration<NetworkInterface> ifs;
try {
ifs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
return ips6;
}
while (ifs.hasMoreElements()) {
NetworkInterface iface = ifs.nextElement();
Enumeration<InetAddress> iad = iface.getInetAddresses();
while (iad.hasMoreElements()) {
InetAddress localIP = iad.nextElement();
if (!localIP.isSiteLocalAddress() && !localIP.isLinkLocalAddress() && !localIP.isLoopbackAddress()) {
if (localIP instanceof java.net.Inet6Address)
ips6.add(localIP.getHostAddress());
}
}
}
return ips6;
}
示例6: findLoopbackInterface
import java.net.InetAddress; //导入方法依赖的package包/类
private NetworkInterface findLoopbackInterface(InetAddress address) throws SocketException, HekateException {
NetworkInterface lo = null;
for (NetworkInterface nif : AddressUtils.activeNetworks()) {
if (nif.isUp() && nif.isLoopback()) {
for (InetAddress nifAddress : Collections.list(nif.getInetAddresses())) {
if (!nifAddress.isLinkLocalAddress() && nifAddress.isLoopbackAddress()) {
if (lo != null) {
throw new HekateException("Failed to resolve a loopback network interface. "
+ "Multiple loopback interfaces were detected [address=" + address + ", interface1=" + lo.getName()
+ ", interface2=" + nif.getName() + ']');
}
lo = nif;
break;
}
}
}
}
return lo;
}
示例7: getInterfaceMask
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns interface mask by index.
*
* @param interfaceIndex interface index
* @return interface IP by index
*/
private String getInterfaceMask(int interfaceIndex) {
String subnetMask = null;
try {
Ip4Address ipAddress = getInterfaceIp(interfaceIndex);
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(
InetAddress.getByName(ipAddress.toString()));
Enumeration ipAddresses = networkInterface.getInetAddresses();
int index = 0;
while (ipAddresses.hasMoreElements()) {
InetAddress address = (InetAddress) ipAddresses.nextElement();
if (!address.isLinkLocalAddress()) {
break;
}
index++;
}
int prfLen = networkInterface.getInterfaceAddresses().get(index).getNetworkPrefixLength();
int shft = 0xffffffff << (32 - prfLen);
int oct1 = ((byte) ((shft & 0xff000000) >> 24)) & 0xff;
int oct2 = ((byte) ((shft & 0x00ff0000) >> 16)) & 0xff;
int oct3 = ((byte) ((shft & 0x0000ff00) >> 8)) & 0xff;
int oct4 = ((byte) (shft & 0x000000ff)) & 0xff;
subnetMask = oct1 + "." + oct2 + "." + oct3 + "." + oct4;
} catch (Exception e) {
log.debug("Error while getting Interface network mask by index");
return subnetMask;
}
return subnetMask;
}
示例8: getGatewayIpAddress
import java.net.InetAddress; //导入方法依赖的package包/类
public static void getGatewayIpAddress(Context c) {
// get wifi ip
final WifiManager manager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);
StringBuilder IFCONFIG = new StringBuilder();
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()) {
IFCONFIG.append(inetAddress.getHostAddress().toString() + "\n");
}
}
}
} catch (SocketException ex) {
Log.e("LOG_TAG", ex.toString());
}
MLog.d(TAG, "ifconfig " + IFCONFIG.toString());
MLog.d(TAG, "hotspot address is " + address);
}
示例9: invalidExternalAddress
import java.net.InetAddress; //导入方法依赖的package包/类
protected boolean
invalidExternalAddress(
InetAddress ia )
{
return( ia.isLinkLocalAddress() ||
ia.isLoopbackAddress() ||
ia.isSiteLocalAddress());
}
示例10: getLinkLocalAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Determines the link-local IPv6 address which is configured on the network interface provided
* in the properties file.
* @return The link-local address given as a String
*/
public static Inet6Address getLinkLocalAddress() {
String networkInterfaceConfig = getPropertyValue("network.interface").toString();
NetworkInterface nif = null;
try {
if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
} else {
nif = NetworkInterface.getByName(networkInterfaceConfig);
}
Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress.getClass() == Inet6Address.class && inetAddress.isLinkLocalAddress()) {
return (Inet6Address) inetAddress;
}
}
getLogger().fatal("No IPv6 link-local address found on the network interface '" +
nif.getDisplayName() + "' configured in the properties file");
} catch (SocketException e) {
getLogger().fatal("SocketException while trying to get network interface for configured name " +
networkInterfaceConfig + "'", e);
} catch (NullPointerException | NumberFormatException e2) {
getLogger().fatal("No network interface for configured network interface index '" +
networkInterfaceConfig + "' found");
}
return null;
}
示例11: scrubInetAddress
import java.net.InetAddress; //导入方法依赖的package包/类
@Nullable
public static String scrubInetAddress(InetAddress address) {
// don't scrub link and site local addresses
if (address.isLinkLocalAddress() || address.isSiteLocalAddress())
return address.toString();
// completely scrub IPv6 addresses
if (address instanceof Inet6Address) return "[scrubbed]";
// keep first and last octet of IPv4 addresses
return scrubInetAddress(address.toString());
}
示例12: isLocal
import java.net.InetAddress; //导入方法依赖的package包/类
public boolean isLocal() {
InetAddress a = address.getAddress();
if (a.isLinkLocalAddress() || a.isLoopbackAddress() || a.isSiteLocalAddress()) {
log.error("Loopback address: {}", a);
return true;
}
return false;
}
示例13: systemHasLocalIpv4Address
import java.net.InetAddress; //导入方法依赖的package包/类
private boolean systemHasLocalIpv4Address() throws Exception {
for (NetworkInterface i : Collections.list(
NetworkInterface.getNetworkInterfaces())) {
for (InetAddress a : Collections.list(i.getInetAddresses())) {
if (a instanceof Inet4Address)
return a.isLinkLocalAddress() || a.isSiteLocalAddress();
}
}
return false;
}
示例14: getInterfaceMask
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns interface MAC by index.
*
* @param interfaceIndex interface index
* @return interface IP by index
*/
private String getInterfaceMask(int interfaceIndex) {
String subnetMask = null;
try {
Ip4Address ipAddress = getInterfaceIp(interfaceIndex);
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(
InetAddress.getByName(ipAddress.toString()));
Enumeration ipAddresses = networkInterface.getInetAddresses();
int index = 0;
while (ipAddresses.hasMoreElements()) {
InetAddress address = (InetAddress) ipAddresses.nextElement();
if (!address.isLinkLocalAddress()) {
break;
}
index++;
}
int prfLen = networkInterface.getInterfaceAddresses().get(index).getNetworkPrefixLength();
int shft = 0xffffffff << (32 - prfLen);
int oct1 = ((byte) ((shft & 0xff000000) >> 24)) & 0xff;
int oct2 = ((byte) ((shft & 0x00ff0000) >> 16)) & 0xff;
int oct3 = ((byte) ((shft & 0x0000ff00) >> 8)) & 0xff;
int oct4 = ((byte) (shft & 0x000000ff)) & 0xff;
subnetMask = oct1 + "." + oct2 + "." + oct3 + "." + oct4;
} catch (Exception e) {
log.debug("Error while getting Interface network mask by index");
return subnetMask;
}
return subnetMask;
}
示例15: isIPLocal
import java.net.InetAddress; //导入方法依赖的package包/类
public static boolean isIPLocal(final InetAddress adr)
{
return adr.isLinkLocalAddress() || adr.isLoopbackAddress() || adr.isSiteLocalAddress();
}