List of usage examples for java.net NetworkInterface getDisplayName
public String getDisplayName()
From source file:ru.asmsoft.p2p.incoming.LocalAddressesFilteringService.java
@PostConstruct public void init() { try {/* w ww. j av a2 s .co m*/ logger.info("Full list of Network Interfaces:"); for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); logger.info(" " + intf.getName() + " " + intf.getDisplayName()); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { String ip = enumIpAddr.nextElement().toString().replace("/", ""); logger.info(" " + ip); localIpAddresses.add(ip); } } } catch (SocketException e) { logger.info(" (error retrieving network interface list)"); } }
From source file:org.springframework.vault.authentication.MacAddressUserId.java
private NetworkInterface getNetworkInterface(String hint, List<NetworkInterface> interfaces) { for (NetworkInterface anInterface : interfaces) { if (hint.equals(anInterface.getDisplayName()) || hint.equals(anInterface.getName())) { return anInterface; }/*ww w .j a v a2 s. c om*/ } return null; }
From source file:org.trafodion.rest.util.NetworkConfiguration.java
public NetworkConfiguration(Configuration conf) throws Exception { this.conf = conf; ia = InetAddress.getLocalHost(); String dnsInterface = conf.get(Constants.REST_DNS_INTERFACE, Constants.DEFAULT_REST_DNS_INTERFACE); if (dnsInterface.equalsIgnoreCase("default")) { intHostAddress = extHostAddress = ia.getHostAddress(); canonicalHostName = ia.getCanonicalHostName(); LOG.info("Using local host [" + canonicalHostName + "," + extHostAddress + "]"); } else {//from w w w . ja v a 2 s.co m // For all nics get all hostnames and addresses // and try to match against rest.dns.interface property Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); while (nics.hasMoreElements() && !matchedInterface) { InetAddress inet = null; NetworkInterface ni = nics.nextElement(); LOG.info("Found interface [" + ni.getDisplayName() + "]"); if (dnsInterface.equalsIgnoreCase(ni.getDisplayName())) { LOG.info("Matched specified interface [" + ni.getName() + "]"); inet = getInetAddress(ni); getCanonicalHostName(ni, inet); } else { Enumeration<NetworkInterface> subIfs = ni.getSubInterfaces(); for (NetworkInterface subIf : Collections.list(subIfs)) { LOG.debug("Sub Interface Display name [" + subIf.getDisplayName() + "]"); if (dnsInterface.equalsIgnoreCase(subIf.getDisplayName())) { LOG.info("Matched subIf [" + subIf.getName() + "]"); inet = getInetAddress(subIf); getCanonicalHostName(subIf, inet); break; } } } } } if (!matchedInterface) checkCloud(); }
From source file:org.jgrades.lic.service.MacRule.java
private boolean performValidation(Licence licence, LicenceProperty property) { LOGGER.debug("Mac property found for licence with uid {}", licence.getUid()); String requestedMac = property.getValue(); LOGGER.debug("Requested MAC: {}", requestedMac); for (NetworkInterface networkInterface : networkInterfaces) { try {/*from ww w . jav a 2 s . c o m*/ String currentMac = getCurrentMac(networkInterface); LOGGER.trace("Proceeded network interface: {}, with MAC: {}", networkInterface.getDisplayName(), currentMac); if (currentMac.equalsIgnoreCase(requestedMac)) { LOGGER.debug("MAC {} is matching to expected MAC", currentMac); return true; } } catch (SocketException e) { LOGGER.debug("Error during extracting MAC for network interface {}", networkInterface.getDisplayName(), e); } } LOGGER.debug("There is no any network interfaces or all has not correctly MAC"); return false; }
From source file:org.cc86.MMC.client.Main.java
public static String serverDiscovery() { String res = "0.0.0.0"; DatagramSocket c;//from w ww.jav a2s.c o m // Find the server using UDP broadcast try { //Open a random port to send the package c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "DISCOVER_MMC_REQUEST".getBytes(); //Try the 255.255.255.255 first try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 0xCC86); c.send(sendPacket); l.info("Request packet sent to: 255.255.255.255 (DEFAULT)"); } catch (Exception e) { } // Broadcast the message over all the network interfaces Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement(); if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; // Don't want to broadcast to the loopback interface } for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress broadcast = interfaceAddress.getBroadcast(); if (broadcast == null) { continue; } // Send the broadcast package! try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, 8888); c.send(sendPacket); } catch (Exception e) { } l.info("Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName()); } } l.info("Done looping over all network interfaces. Now waiting for a reply!"); //Wait for a response byte[] recvBuf = new byte[15000]; DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); c.receive(receivePacket); //We have a response l.info("Broadcast response from server: " + receivePacket.getAddress().getHostAddress()); //Check if the message is correct String message = new String(receivePacket.getData()).trim(); if (message.equals("DISCOVER_MMC_RESPONSE")) { //DO SOMETHING WITH THE SERVER'S IP (for example, store it in your controller) res = (receivePacket.getAddress() + "").substring(1); } //Close the port! c.close(); } catch (IOException ex) { } return res; }
From source file:org.openbaton.nfvo.system.SystemStartup.java
@Override public void run(String... args) throws Exception { log.info("Initializing OpenBaton"); log.debug(Arrays.asList(args).toString()); propFileLocation = propFileLocation.replace("file:", ""); log.debug("Property file: " + propFileLocation); InputStream is = new FileInputStream(propFileLocation); Properties properties = new Properties(); properties.load(is);//from w w w . j av a2s . co m log.debug("Config Values are: " + properties.values()); Configuration c = new Configuration(); c.setName("system"); c.setConfigurationParameters(new HashSet<ConfigurationParameter>()); /** * Adding properties from file */ for (Entry<Object, Object> entry : properties.entrySet()) { ConfigurationParameter cp = new ConfigurationParameter(); cp.setConfKey((String) entry.getKey()); cp.setValue((String) entry.getValue()); c.getConfigurationParameters().add(cp); } /** * Adding system properties */ Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) { ConfigurationParameter cp = new ConfigurationParameter(); log.trace("Display name: " + netint.getDisplayName()); log.trace("Name: " + netint.getName()); cp.setConfKey("ip-" + netint.getName().replaceAll("\\s", "")); Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { if (inetAddress.getHostAddress().contains(".")) { log.trace("InetAddress: " + inetAddress.getHostAddress()); cp.setValue(inetAddress.getHostAddress()); } } log.trace(""); c.getConfigurationParameters().add(cp); } configurationRepository.save(c); if (installPlugin) { startPlugins(pluginDir); } }
From source file:org.springframework.cloud.commons.util.InetUtils.java
public InetAddress findFirstNonLoopbackAddress() { InetAddress result = null;//from w w w . j a v a 2 s.c o m try { int lowest = Integer.MAX_VALUE; for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics .hasMoreElements();) { NetworkInterface ifc = nics.nextElement(); if (ifc.isUp()) { log.trace("Testing interface: " + ifc.getDisplayName()); if (ifc.getIndex() < lowest || result == null) { lowest = ifc.getIndex(); } else if (result != null) { continue; } // @formatter:off if (!ignoreInterface(ifc.getDisplayName())) { for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) { InetAddress address = addrs.nextElement(); if (address instanceof Inet4Address && !address.isLoopbackAddress() && !ignoreAddress(address)) { log.trace("Found non-loopback interface: " + ifc.getDisplayName()); result = address; } } } // @formatter:on } } } catch (IOException ex) { log.error("Cannot get first non-loopback address", ex); } if (result != null) { return result; } try { return InetAddress.getLocalHost(); } catch (UnknownHostException e) { log.warn("Unable to retrieve localhost"); } return null; }
From source file:org.trafodion.rest.util.NetworkConfiguration.java
public void getCanonicalHostName(NetworkInterface ni, InetAddress inet) throws Exception { if (inet.getCanonicalHostName().contains(".")) { intHostAddress = extHostAddress = inet.getHostAddress(); canonicalHostName = inet.getCanonicalHostName(); LOG.info("Using interface [" + ni.getDisplayName() + "," + canonicalHostName + "," + extHostAddress + "]"); ia = inet;/*from w w w . j a v a 2 s. c o m*/ } }
From source file:org.trafodion.rest.util.NetworkConfiguration.java
public InetAddress getInetAddress(NetworkInterface ni) throws Exception { InetAddress inet = null;/*from w w w .jav a 2 s .c o m*/ Enumeration<InetAddress> rawAdrs = ni.getInetAddresses(); while (rawAdrs.hasMoreElements()) { inet = rawAdrs.nextElement(); LOG.info("Match Found interface [" + ni.toString() + "," + ni.getDisplayName() + "," + inet.getCanonicalHostName() + "," + inet.getHostAddress() + "]"); } matchedInterface = true; return inet; }
From source file:com.elixsr.portforwarder.forwarding.ForwardingService.java
private InetSocketAddress generateFromIpUsingInterface(String interfaceName, int port) throws SocketException, ObjectNotFoundException { String address = null;/*from w w w. j a v a2 s . c om*/ InetSocketAddress inetSocketAddress; for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); Log.d(TAG, intf.getDisplayName() + " vs " + interfaceName); if (intf.getDisplayName().equals(interfaceName)) { Log.i(TAG, "Found the relevant Interface. Will attempt to fetch IP Address"); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); address = new String(inetAddress.getHostAddress().toString()); if (address != null & address.length() > 0 && inetAddress instanceof Inet4Address) { inetSocketAddress = new InetSocketAddress(address, port); return inetSocketAddress; } } } } //Failed to find the relevant interface //TODO: complete // Toast.makeText(this, "Could not find relevant network interface.", // Toast.LENGTH_LONG).show(); throw new ObjectNotFoundException("Could not find IP Address for Interface " + interfaceName); }