List of usage examples for java.net NetworkInterface getName
public String getName()
From source file:com.att.arocollector.AROCollectorActivity.java
/** * check a network interface by name//from w w w .j a v a2 s. co m * * @param context * @param networkInterfaceName * @return true if interface exists and is active * @throws Exception */ private boolean checkForActiveInterface(Context context, String networkInterfaceName) throws Exception { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (intf.getName().equals(networkInterfaceName)) { return intf.isUp(); } } return false; }
From source file:fr.inria.ucn.collectors.NetworkStateCollector.java
private JSONArray getIfconfig(Map<String, JSONObject> stats) throws JSONException { JSONArray ifaces = new JSONArray(); // make sure the stats is read networkStats();/*from ww w. jav a 2 s . c o m*/ Enumeration<NetworkInterface> en = null; try { en = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { Log.d(Constants.LOGTAG, "failed to list interfaces", e); } if (en != null) { while (en.hasMoreElements()) { NetworkInterface intf = en.nextElement(); JSONObject iface = new JSONObject(); iface.put("display_name", intf.getDisplayName()); iface.put("name", intf.getName()); iface.put("is_virtual", intf.isVirtual()); iface.put("stats", stats.get(intf.getName())); try { iface.put("mtu", intf.getMTU()); iface.put("is_loopback", intf.isLoopback()); iface.put("is_ptop", intf.isPointToPoint()); iface.put("is_up", intf.isUp()); } catch (SocketException e) { Log.d(Constants.LOGTAG, "failed to read interface data", e); } JSONArray ips = new JSONArray(); List<InterfaceAddress> ilist = intf.getInterfaceAddresses(); for (InterfaceAddress ia : ilist) { ips.put(ia.getAddress().getHostAddress()); } iface.put("addresses", ips); ifaces.put(iface); } } else { for (String name : stats.keySet()) { JSONObject iface = new JSONObject(); iface.put("name", name); iface.put("stats", stats.get(name)); ifaces.put(iface); } } return ifaces; }
From source file:org.sltpaya.tool.Utils.java
/** * ?APN?/*from ww w . ja v a2 s .c o m*/ * @return ? */ private boolean getApnStatus() { Matcher matcher; String name; try { Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces(); if (niList != null) { for (NetworkInterface intf : Collections.list(niList)) { if (!intf.isUp() || intf.getInterfaceAddresses().size() == 0) { continue; } Log.d(TAG, "isVpnUsed() NetworkInterface Name: " + intf.getName()); name = intf.getName(); matcher = Pattern.compile("tun\\d").matcher(name); if (matcher.find()) { System.out.println("vpn??????" + matcher.group()); return true; } } } } catch (Throwable e) { e.printStackTrace(); } return false; }
From source file:com.cloud.utils.net.NetUtils.java
public static InetAddress[] getInterfaceInetAddresses(final String ifName) { final List<InetAddress> addrList = new ArrayList<InetAddress>(); try {/*from w ww .j a v a 2 s . com*/ for (final NetworkInterface ifc : IteratorUtil .enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { if (ifc.isUp() && !ifc.isVirtual() && ifc.getName().equals(ifName)) { for (final InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) { addrList.add(addr); } } } } catch (final SocketException e) { s_logger.warn("SocketException in getAllLocalInetAddresses().", e); } final InetAddress[] addrs = new InetAddress[addrList.size()]; if (addrList.size() > 0) { System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size()); } return addrs; }
From source file:org.apache.hadoop.hdfs.MiniAvatarCluster.java
/** * Retrieves the name of the loopback interface in a platform independent way. *///from w w w . j a v a 2 s . co m private static String getLoopBackInterface() throws IOException { String loopBack = "lo"; Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); if (iface.isLoopback()) { loopBack = iface.getName(); break; } } return loopBack; }
From source file:org.springframework.data.hadoop.util.net.DefaultHostInfoDiscovery.java
private List<NetworkInterface> filterInterfaces(List<NetworkInterface> interfaces) { List<NetworkInterface> filtered = new ArrayList<NetworkInterface>(); for (NetworkInterface nic : interfaces) { boolean match = false; try {/*w w w . j av a2 s. com*/ match = pointToPoint && nic.isPointToPoint(); } catch (SocketException e) { } try { match = !match && loopback && nic.isLoopback(); } catch (SocketException e) { } // last, if we didn't match anything, let all pass // if matchInterface is not set, otherwise do pattern // matching if (!match && !StringUtils.hasText(matchInterface)) { match = true; } else if (StringUtils.hasText(matchInterface)) { match = nic.getName().matches(matchInterface); } if (match) { filtered.add(nic); } } return filtered; }
From source file:org.apache.carbondata.core.dictionary.service.AbstractDictionaryServer.java
public String findLocalIpAddress(Logger LOGGER) { try {/* www . ja va2 s.c o m*/ String defaultIpOverride = System.getenv("SPARK_LOCAL_IP"); if (defaultIpOverride != null) { return defaultIpOverride; } else { InetAddress address = InetAddress.getLocalHost(); if (address.isLoopbackAddress()) { // Address resolves to something like 127.0.1.1, which happens on Debian; try to find // a better address using the local network interfaces // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order // on unix-like system. On windows, it returns in index order. // It's more proper to pick ip address following system output order. Enumeration<NetworkInterface> activeNetworkIFs = NetworkInterface.getNetworkInterfaces(); List<NetworkInterface> reOrderedNetworkIFs = new ArrayList<NetworkInterface>(); while (activeNetworkIFs.hasMoreElements()) { reOrderedNetworkIFs.add(activeNetworkIFs.nextElement()); } if (!SystemUtils.IS_OS_WINDOWS) { Collections.reverse(reOrderedNetworkIFs); } for (NetworkInterface ni : reOrderedNetworkIFs) { Enumeration<InetAddress> inetAddresses = ni.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress addr = inetAddresses.nextElement(); if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && addr instanceof Inet4Address) { // We've found an address that looks reasonable! LOGGER.warn("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + "; using " + addr.getHostAddress() + " instead (on interface " + ni.getName() + ")"); LOGGER.warn("Set SPARK_LOCAL_IP if you need to bind to another address"); return addr.getHostAddress(); } } LOGGER.warn("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + ", but we couldn't find any external IP address!"); LOGGER.warn("Set SPARK_LOCAL_IP if you need to bind to another address"); } } return address.getHostAddress(); } } catch (UnknownHostException e) { LOGGER.error("do not get local host address:" + e.getMessage(), e); throw new RuntimeException(e); } catch (SocketException e) { LOGGER.error("do not get net work interface:" + e.getMessage(), e); throw new RuntimeException(e); } }
From source file:org.apereo.portal.PortalInfoProviderImpl.java
protected String getNetworkInterfaceName(String networkInterfaceName) { if (networkInterfaceName == null) { return null; }/*from w ww. j ava2 s. c o m*/ this.logger.info("Attempting to resolve serverName using NetworkInterface named ({})", networkInterfaceName); final NetworkInterface networkInterface; try { networkInterface = NetworkInterface.getByName(networkInterfaceName); } catch (SocketException e) { logger.warn("Failed to get NetworkInterface for name (" + networkInterfaceName + ").", e); return null; } if (networkInterface == null) { logger.warn("No NetworkInterface could be found for name (" + networkInterfaceName + "). Available interface names: " + getNetworkInterfaceNames()); return null; } final Enumeration<InetAddress> inetAddressesEnum = networkInterface.getInetAddresses(); if (!inetAddressesEnum.hasMoreElements()) { logger.warn("NetworkInterface (" + networkInterface.getName() + ") has no InetAddresses to get a name from."); return null; } final InetAddress inetAddress = inetAddressesEnum.nextElement(); if (inetAddressesEnum.hasMoreElements()) { logger.warn("NetworkInterface (" + networkInterface.getName() + ") has more than one InetAddress, the hostName of the first will be returned."); } return inetAddress.getHostName(); }
From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java
/** * Start the DHCPv6 server. If multicast network interfaces have * been supplied on startup, then start a NetDhcpServer thread * on each of those interfaces. Start one NioDhcpServer thread * which will listen on all IPv6 interfaces on the local host. * /*from www. ja va 2s . com*/ * @throws Exception the exception */ protected void start() throws Exception { log.info("Starting Jagornet DHCP Server"); log.info(Version.getVersion()); int cores = Runtime.getRuntime().availableProcessors(); log.info("Number of available core processors: " + cores); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { log.info("Stopping Jagornet DHCPv6 Server"); System.out.println("Stopping Jagornet DHCPv6 Server: " + new Date()); } }); DhcpServerConfiguration.configFilename = configFilename; serverConfig = DhcpServerConfiguration.getInstance(); if (serverConfig == null) { throw new IllegalStateException("Failed to initialize server configuration file: " + configFilename); } String schemaType = DhcpServerPolicies.globalPolicy(Property.DATABASE_SCHEMA_TYTPE); int schemaVersion = DhcpServerPolicies.globalPolicyAsInt(Property.DATABASE_SCHEMA_VERSION); String[] appContext = getAppContextFiles(schemaType, schemaVersion); log.info("Loading application context: " + Arrays.toString(appContext)); context = new ClassPathXmlApplicationContext(appContext); if (context == null) { throw new IllegalStateException("Failed to initialize application context: " + appContext); } log.info("Application context loaded."); loadManagers(); registerLog4jInJmx(); String msg = null; // by default, all non-loopback, non-linklocal, // IPv6 addresses are selected for unicast if (v6UcastAddrs == null) { v6UcastAddrs = getFilteredIPv6Addrs(); } msg = "DHCPv6 Unicast addresses: " + Arrays.toString(v6UcastAddrs.toArray()); System.out.println(msg); log.info(msg); // for now, the mcast interfaces MUST be listed at // startup to get the mcast behavior at all... but // we COULD default to use all IPv6 interfaces if (v6McastNetIfs != null) { // msg = "DHCPv6 Multicast interfaces: " + Arrays.toString(mcastNetIfs.toArray()); StringBuilder sb = new StringBuilder(); sb.append("DHCPv6 Multicast interfaces: ["); for (NetworkInterface mcastNetIf : v6McastNetIfs) { sb.append(mcastNetIf.getName()); sb.append(", "); } sb.setLength(sb.length() - 2); // remove last ", " sb.append(']'); msg = sb.toString(); System.out.println(msg); log.info(msg); } else { msg = "DHCPv6 Multicast interfaces: none"; System.out.println(msg); log.info(msg); } msg = "DHCPv6 Port number: " + v6PortNumber; System.out.println(msg); log.info(msg); // by default, all non-loopback, non-linklocal, // IPv4 addresses are selected for unicast if (v4UcastAddrs == null) { v4UcastAddrs = getFilteredIPv4Addrs(); } msg = "DHCPv4 Unicast addresses: " + Arrays.toString(v4UcastAddrs.toArray()); System.out.println(msg); log.info(msg); if (v4BcastNetIf != null) { msg = "DHCPv4 Broadcast Interface: " + v4BcastNetIf.getName(); System.out.println(msg); log.info(msg); } else { msg = "DHCPv4 Broadcast interface: none"; System.out.println(msg); log.info(msg); } msg = "DHCPv4 Port number: " + v4PortNumber; System.out.println(msg); log.info(msg); NettyDhcpServer nettyServer = new NettyDhcpServer(v6UcastAddrs, v6McastNetIfs, v6PortNumber, v4UcastAddrs, v4BcastNetIf, v4PortNumber); nettyServer.start(); }
From source file:com.vuze.plugin.azVPN_PIA.Checker.java
private int handleBound(InetAddress bindIP, StringBuilder sReply) { int newStatusID = STATUS_ID_OK; String s;//from www . j av a2 s . c o m boolean isGoodExistingBind = matchesVPNIP(bindIP); if (isGoodExistingBind) { String niName = "Unknown Interface"; try { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(bindIP); niName = networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")"; } catch (Throwable e) { } addReply(sReply, CHAR_GOOD, "pia.bound.good", new String[] { "" + bindIP, niName }); vpnIP = bindIP; } else { addReply(sReply, CHAR_BAD, "pia.bound.bad", new String[] { "" + bindIP }); newStatusID = STATUS_ID_BAD; } try { // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows (in some cases), but on Mac returns a wildcard // address DatagramSocket socket = new DatagramSocket(); socket.connect(testSocketAddress, 0); InetAddress localAddress = socket.getLocalAddress(); socket.close(); if (!localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("pia.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); char replyChar = ' '; if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) { if (localAddress.equals(bindIP)) { replyChar = isGoodExistingBind ? CHAR_GOOD : CHAR_WARN; s += " " + texts.getLocalisedMessageText("pia.same.as.vuze"); } else { // Vuze is bound, default routing goes somewhere else // This is ok, since Vuze will not accept incoming from "somewhere else" // We'll warn, but not update the status id replyChar = CHAR_WARN; s += " " + texts.getLocalisedMessageText("pia.not.same"); if (isGoodExistingBind) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } } addLiteralReply(sReply, replyChar + " " + s); if (!isGoodExistingBind && rebindNetworkInterface) { rebindNetworkInterface(networkInterface, localAddress, sReply); // Should we redo test? } } else { // Vuze is bound, default routing goes to somewhere else. // Probably network splitting replyChar = isGoodExistingBind ? CHAR_WARN : CHAR_BAD; if (isGoodExistingBind) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } addLiteralReply(sReply, replyChar + " " + s); } } } catch (Throwable t) { t.printStackTrace(); } return newStatusID; }