List of usage examples for java.net InetAddress isLinkLocalAddress
public boolean isLinkLocalAddress()
From source file:com.jagornet.dhcp.client.GenerateTestConfig.java
private boolean parseOptions(String args[]) { try {//from ww w . ja va2s . c om CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("?")) { return false; } if (cmd.hasOption("f")) { filename = cmd.getOptionValue("f"); } else { filename = "dhcpserver-test-config.xml"; } if (cmd.hasOption("i")) { networkInterface = NetworkInterface.getByName(cmd.getOptionValue("i")); } else { networkInterface = NetworkInterface.getNetworkInterfaces().nextElement(); } Enumeration<InetAddress> ipAddrs = networkInterface.getInetAddresses(); while (ipAddrs.hasMoreElements()) { InetAddress ipAddr = ipAddrs.nextElement(); if ((ipAddr instanceof Inet4Address) && !ipAddr.isLinkLocalAddress() && !ipAddr.isLoopbackAddress()) { ipv4Address = ipAddr; return true; } } System.err.println("No IPv4 address found for interface: " + networkInterface.getName()); return false; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:Comman.Tool.java
public InetAddress getCurrentIp() { try {// ww w. j a v a 2 s .co m Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement(); Enumeration<InetAddress> nias = ni.getInetAddresses(); while (nias.hasMoreElements()) { InetAddress ia = (InetAddress) nias.nextElement(); if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { return ia; } } } } catch (SocketException e) { } return null; }
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(); }// w ww . ja va 2s . co m } } } return null; }
From source file:com.taobao.adfs.util.Utilities.java
public static List<InetAddress> getInetAddressList(List<InetAddress> addressList, String type) throws IOException { type = type.toLowerCase();//from w w w .j av a2 s. c o m List<InetAddress> newAddressList = new ArrayList<InetAddress>(); for (InetAddress address : addressList) { if (address.isLinkLocalAddress()) { if (type.equals("linklocal")) newAddressList.add(address); } else if (address.isLoopbackAddress()) { if (type.equals("loopback")) newAddressList.add(address); } else if (address.isSiteLocalAddress()) { if (type.equals("lan")) newAddressList.add(address); } else { if (type.equals("wan")) newAddressList.add(address); } } Collections.sort(newAddressList, inetAddressComparator); return newAddressList; }
From source file:org.midonet.midolman.tools.MmCtl.java
private MmCtlResult listHosts() { try {//from w w w . java 2 s . c o m UUID myHostId = getHostId(); String indent = ""; for (Host h : dataClient.hostsGetAll()) { String thisHostMarker = ""; if (h.getId().equals(myHostId)) { thisHostMarker = "(*)"; } String format = "Host: id=%s %s\n name=%s\n isAlive=%s"; String output = String.format(format, h.getId(), thisHostMarker, h.getName(), h.getIsAlive()); System.out.println(output); indent = " "; System.out.println(indent + "addresses: "); for (InetAddress ia : h.getAddresses()) { if (!ia.isLinkLocalAddress()) System.out.println(" " + ia); } System.out.println(indent + "vport-host-if-bindings:"); for (VirtualPortMapping vpm : dataClient.hostsGetVirtualPortMappingsByHost(h.getId())) { System.out.println(indent + indent + vpm.getData().toString()); } System.out.println(); } } catch (StateAccessException e) { e.printStackTrace(); return MM_CTL_RET_CODE.STATE_ERROR.getResult(e); } catch (SerializationException e) { e.printStackTrace(); return MM_CTL_RET_CODE.UNKNOWN_ERROR.getResult(e); } catch (IOException e) { e.printStackTrace(); return MM_CTL_RET_CODE.UNKNOWN_ERROR.getResult(e); } return MM_CTL_RET_CODE.SUCCESS.getResult(); }
From source file:org.peercast.core.PeerCastServiceController.java
private String getIpAddress() { Enumeration<NetworkInterface> netIFs; try {// www .j a va 2s.c o m netIFs = NetworkInterface.getNetworkInterfaces(); while (netIFs.hasMoreElements()) { NetworkInterface netIF = netIFs.nextElement(); Enumeration<InetAddress> ipAddrs = netIF.getInetAddresses(); while (ipAddrs.hasMoreElements()) { InetAddress ip = ipAddrs.nextElement(); if (!ip.isLoopbackAddress() && !ip.isLinkLocalAddress() && ip.isSiteLocalAddress()) { String ipStr = ip.getHostAddress().toString(); Log.d(TAG, "IP: " + ipStr); return ipStr; } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
From source file:io.github.gsteckman.rpi_rest.SsdpHandler.java
/** * Constructs a new instance of this class. *///from w ww. j av a2 s. c o m private SsdpHandler() { LOG.info("Instantiating SsdpHandler"); try { // Use first IPv4 address that isn't loopback, any, or link local as the server address Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements() && serverAddress == null) { NetworkInterface i = interfaces.nextElement(); Enumeration<InetAddress> addresses = i.getInetAddresses(); while (addresses.hasMoreElements() && serverAddress == null) { InetAddress address = addresses.nextElement(); if (address instanceof Inet4Address) { if (!address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isLoopbackAddress() && !address.isMulticastAddress()) { serverAddress = address; } } } } if (serverAddress == null) { LOG.warn("Server address unknown"); } svc = SsdpService.forAllMulticastAvailableNetworkInterfaces(this); svc.listen(); // setup Multicast for Notify messages notifySocket = new MulticastSocket(); notifySocket.setTimeToLive(TTL); notifyTimer = new Timer("UPnP Notify Timer", true); notifyTimer.scheduleAtFixedRate(new NotifySender(), 5000, MAX_AGE * 1000 / 2); } catch (Exception e) { LOG.error("SsdpHandler in unknown state due to exception in constructor.", e); } }
From source file:org.apache.geode.internal.net.SocketCreator.java
/** * returns a set of the non-loopback InetAddresses for this machine *///from w ww. j a v a 2s .c om public static Set<InetAddress> getMyAddresses() { Set<InetAddress> result = new HashSet<InetAddress>(); Set<InetAddress> locals = new HashSet<InetAddress>(); Enumeration<NetworkInterface> interfaces; try { interfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { throw new IllegalArgumentException( LocalizedStrings.StartupMessage_UNABLE_TO_EXAMINE_NETWORK_INTERFACES.toLocalizedString(), e); } while (interfaces.hasMoreElements()) { NetworkInterface face = interfaces.nextElement(); boolean faceIsUp = false; try { faceIsUp = face.isUp(); } catch (SocketException e) { InternalDistributedSystem ids = InternalDistributedSystem.getAnyInstance(); if (ids != null) { logger.info("Failed to check if network interface is up. Skipping {}", face, e); } } 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 { return result; } }
From source file:com.orangelabs.rcs.platform.network.AndroidNetworkFactory.java
/** * Returns the local IP address of a given network interface * /*from w w w.jav a 2 s . c om*/ * @param dnsEntry remote address to find an according local socket address * @param type the type of the network interface, should be either * {@link android.net.ConnectivityManager#TYPE_WIFI} or {@link android.net.ConnectivityManager#TYPE_MOBILE} * @return Address */ // Changed by Deutsche Telekom public String getLocalIpAddress(DnsResolvedFields dnsEntry, int type) { String ipAddress = null; try { // What kind of remote address (P-CSCF) are we trying to reach? boolean isIpv4 = InetAddressUtils.isIPv4Address(dnsEntry.ipAddress); // check all available interfaces for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); (en != null) && en.hasMoreElements();) { NetworkInterface netIntf = (NetworkInterface) en.nextElement(); for (Enumeration<InetAddress> addr = netIntf.getInetAddresses(); addr.hasMoreElements();) { InetAddress inetAddress = addr.nextElement(); ipAddress = IpAddressUtils.extractHostAddress(inetAddress.getHostAddress()); // if IP address version doesn't match to remote address // version then skip if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && (InetAddressUtils.isIPv4Address(ipAddress) == isIpv4)) { String intfName = netIntf.getDisplayName().toLowerCase(); // some devices do list several interfaces though only // one is active if (((type == ConnectivityManager.TYPE_WIFI) && intfName.startsWith("wlan")) || ((type == ConnectivityManager.TYPE_MOBILE) && !intfName.startsWith("wlan"))) { return ipAddress; } } } } } catch (Exception e) { if (logger.isActivated()) { logger.error("getLocalIpAddress failed with ", e); } } return ipAddress; }
From source file:ac.dynam.rundeck.plugin.resources.ovirt.InstanceToNodeMapper.java
/** * Convert an oVirt Instance to a RunDeck INodeEntry based on the mapping input */// w w w. j ava 2s . com @SuppressWarnings("unchecked") static INodeEntry instanceToNode(final VM inst) throws GeneratorException { final NodeEntryImpl node = new NodeEntryImpl(); node.setNodename(inst.getName()); node.setOsArch(inst.getCpu().getArchitecture()); node.setOsName(inst.getOs().getType()); node.setDescription(inst.getDescription()); node.setUsername("root"); InetAddress address = null; if (inst.getGuestInfo() != null) { try { address = InetAddress.getByName(inst.getGuestInfo().getFqdn()); logger.debug("Host " + node.getNodename() + " Guest FQDN " + inst.getGuestInfo().getFqdn() + " Address: " + address.getHostName()); if (address.getHostName() == "localhost") throw new UnknownHostException(); } catch (UnknownHostException e) { /* try the first IP instead then */ logger.warn("Host " + node.getNodename() + " address " + inst.getGuestInfo().getFqdn() + " does not resolve. Trying IP addresses instead"); for (int i = 0; i < inst.getGuestInfo().getIps().getIPs().size(); i++) { logger.debug("Host " + node.getNodename() + " Trying " + inst.getGuestInfo().getIps().getIPs().get(i).getAddress()); try { address = InetAddress.getByName(inst.getGuestInfo().getIps().getIPs().get(i).getAddress()); if (address != null) { if (address.isLinkLocalAddress() || address.isMulticastAddress()) { logger.warn("Host " + node.getNodename() + " ip address is not valid: " + inst.getGuestInfo().getIps().getIPs().get(i).getAddress()); continue; } logger.debug("Host " + node.getNodename() + " ip address " + address.getHostAddress() + " will be used instead"); break; } } catch (UnknownHostException e1) { logger.warn("Host " + node.getNodename() + " IP Address " + inst.getGuestInfo().getIps().getIPs().get(i).getAddress() + " is invalid"); } } } } if (address == null) { /* try resolving based on name */ try { address = InetAddress.getByName(node.getNodename()); } catch (UnknownHostException e) { logger.warn("Unable to Find IP address for Host " + node.getNodename()); return null; } } if (address != null) node.setHostname(address.getCanonicalHostName()); if (inst.getTags() != null) { VMTags tags = inst.getTags(); final HashSet<String> tagset = new HashSet<String>(); try { for (int j = 0; j < tags.list().size(); j++) { tagset.add(tags.list().get(j).getName()); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ServerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (null == node.getTags()) { node.setTags(tagset); } else { final HashSet<String> orig = new HashSet<String>(node.getTags()); orig.addAll(tagset); node.setTags(orig); } } if (inst.getHighAvailability().getEnabled()) node.setAttribute("HighAvailability", "true"); if (inst.getType() != null) node.setAttribute("Host Type", inst.getType()); node.setAttribute("oVirt VM", "true"); node.setAttribute("oVirt Host", inst.getHost().getName()); return node; }