List of usage examples for java.net NetworkInterface isUp
public boolean isUp() throws SocketException
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 w w . j av a2 s .co m*/ 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:com.cloud.utils.net.NetUtils.java
public static String[] getLocalCidrs() { final String defaultHostIp = getDefaultHostIp(); final List<String> cidrList = new ArrayList<String>(); try {//from ww w .ja v a2 s . c o m for (final NetworkInterface ifc : IteratorUtil .enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) { for (final InterfaceAddress address : ifc.getInterfaceAddresses()) { final InetAddress addr = address.getAddress(); final int prefixLength = address.getNetworkPrefixLength(); if (prefixLength < MAX_CIDR && prefixLength > 0) { final String ip = ipFromInetAddress(addr); if (ip.equalsIgnoreCase(defaultHostIp)) { cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength))); } } } } } } catch (final SocketException e) { s_logger.warn("UnknownHostException in getLocalCidrs().", e); } return cidrList.toArray(new String[0]); }
From source file:com.git.original.common.utils.IPUtils.java
/** * ?IP?/* www. j a va2 s.c om*/ * <p> * :<br/> * 1. 220.xxx.xxx.xxx<br> * 2. 123.xxx.xxx.xxx<br> * other<br> * * @return * @throws SocketException * If an I/O error occurs. * @throws NullPointerException * can not found the interface * @throws RuntimeException * can not get net address */ public static InetAddress getWANIpv4Address(String interfaceName) throws SocketException, NullPointerException, RuntimeException { InetAddress ipStartWith123 = null; InetAddress ipv4Addr = null; if (StringUtils.isNotEmpty(interfaceName)) { // ? NetworkInterface netInterface = NetworkInterface.getByName(interfaceName.trim()); if (netInterface == null) { throw new NullPointerException("can not found the network interface by name: " + interfaceName); } Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses(); while (addrEnum.hasMoreElements()) { InetAddress addr = addrEnum.nextElement(); String hostAddr = addr.getHostAddress(); if (hostAddr.startsWith("220.")) { return addr; } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) { ipStartWith123 = addr; } else if (addr instanceof Inet4Address) { ipv4Addr = addr; } } } else { /* * ??? */ Enumeration<NetworkInterface> interfaceEnum = NetworkInterface.getNetworkInterfaces(); while (interfaceEnum.hasMoreElements()) { NetworkInterface netInterface = interfaceEnum.nextElement(); if (netInterface.isLoopback() || !netInterface.isUp()) { continue; } Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses(); while (addrEnum.hasMoreElements()) { InetAddress addr = addrEnum.nextElement(); String hostAddr = addr.getHostAddress(); if (hostAddr.startsWith("220.")) { return addr; } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) { ipStartWith123 = addr; } else if (addr instanceof Inet4Address) { ipv4Addr = addr; } } } } if (ipStartWith123 != null) { return ipStartWith123; } else if (ipv4Addr != null) { return ipv4Addr; } throw new RuntimeException("can not get WAN Address"); }
From source file:com.att.arocollector.AROCollectorActivity.java
/** * check a network interface by name/*from w w w .ja v a 2 s . c om*/ * * @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:com.portfolio.data.attachment.FileServlet.java
@Override public void init(ServletConfig config) { /// List possible local address try {//from ww w .j av a 2 s . co m Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue; Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress current_addr = addresses.nextElement(); if (current_addr instanceof Inet4Address) ourIPs.add(current_addr.getHostAddress()); } } } catch (Exception e) { } servContext = config.getServletContext(); backend = config.getServletContext().getInitParameter("backendserver"); server = config.getServletContext().getInitParameter("fileserver"); try { String dataProviderName = config.getInitParameter("dataProviderClass"); dataProvider = (DataProvider) Class.forName(dataProviderName).newInstance(); InitialContext cxt = new InitialContext(); if (cxt == null) { throw new Exception("no context found!"); } /// Init this here, might fail depending on server hosting ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/portfolio-backend"); if (ds == null) { throw new Exception("Data jdbc/portfolio-backend source not found!"); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.archive.modules.fetcher.FetchHTTPTests.java
public void testHttpBindAddress() throws Exception { List<InetAddress> addrList = new ArrayList<InetAddress>(); for (NetworkInterface ifc : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (ifc.isUp()) { for (InetAddress addr : Collections.list(ifc.getInetAddresses())) { if (addr instanceof Inet4Address) { addrList.add(addr);//from www . j ava 2 s. co m } } } } if (addrList.size() < 2) { fail("unable to test binding to different local addresses: only " + addrList.size() + " addresses available"); } for (InetAddress addr : addrList) { tryHttpBindAddress(addr.getHostAddress()); } }
From source file:com.jagornet.dhcpv6.server.DhcpV6Server.java
/** * Gets the IPv6 network interfaces for the supplied interface names. * /*from w w w . j a v a 2s . c o m*/ * @param ifnames the interface names to locate NetworkInterfaces by * * @return the list of NetworkInterfaces that are up, support multicast, * and have at least one IPv6 address configured * * @throws SocketException the socket exception */ private List<NetworkInterface> getIPv6NetIfs(String[] ifnames) throws SocketException { List<NetworkInterface> netIfs = new ArrayList<NetworkInterface>(); for (String ifname : ifnames) { if (ifname.equals("*")) { return getAllIPv6NetIfs(); } NetworkInterface netIf = NetworkInterface.getByName(ifname); if (netIf == null) { // if not found by name, see if the name is actually an address try { InetAddress ipaddr = InetAddress.getByName(ifname); netIf = NetworkInterface.getByInetAddress(ipaddr); } catch (UnknownHostException ex) { log.warn("Unknown interface: " + ifname + ": " + ex); } } if (netIf != null) { if (netIf.isUp()) { // for multicast, the loopback interface is excluded if (netIf.supportsMulticast() && !netIf.isLoopback()) { boolean isV6 = false; List<InterfaceAddress> ifAddrs = netIf.getInterfaceAddresses(); for (InterfaceAddress ifAddr : ifAddrs) { if (ifAddr.getAddress() instanceof Inet6Address) { netIfs.add(netIf); isV6 = true; break; } } if (!isV6) { System.err.println("Interface is not configured for IPv6: " + netIf.getDisplayName()); return null; } } else { System.err.println("Interface does not support multicast: " + netIf.getDisplayName()); return null; } } else { System.err.println("Interface is not up: " + netIf.getDisplayName()); return null; } } else { System.err.println("Interface not found or inactive: " + ifname); return null; } } return netIfs; }
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 w w w.j av a 2 s .co 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:com.jagornet.dhcp.server.JagornetDhcpServer.java
private NetworkInterface getIPv4NetIf(String ifname) throws SocketException { NetworkInterface netIf = NetworkInterface.getByName(ifname); if (netIf == null) { // if not found by name, see if the name is actually an address try {/*from www . jav a 2 s .com*/ InetAddress ipaddr = InetAddress.getByName(ifname); netIf = NetworkInterface.getByInetAddress(ipaddr); } catch (UnknownHostException ex) { log.warn("Unknown interface: " + ifname + ": " + ex); } } if (netIf != null) { if (netIf.isUp()) { // the loopback interface is excluded if (!netIf.isLoopback()) { boolean isV4 = false; List<InterfaceAddress> ifAddrs = netIf.getInterfaceAddresses(); for (InterfaceAddress ifAddr : ifAddrs) { if (ifAddr.getAddress() instanceof Inet4Address) { isV4 = true; break; } } if (!isV4) { System.err.println("Interface is not configured for IPv4: " + netIf); return null; } } else { System.err.println("Interface is loopback: " + netIf); return null; } } else { System.err.println("Interface is not up: " + netIf); return null; } } else { System.err.println("Interface not found or inactive: " + ifname); return null; } return netIf; }
From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java
/** * Gets the IPv6 network interfaces for the supplied interface names. * //from w ww .j a v a 2 s . co m * @param ifnames the interface names to locate NetworkInterfaces by * * @return the list of NetworkInterfaces that are up, support multicast, * and have at least one IPv6 address configured * * @throws SocketException the socket exception */ private List<NetworkInterface> getIPv6NetIfs(String[] ifnames) throws SocketException { List<NetworkInterface> netIfs = new ArrayList<NetworkInterface>(); for (String ifname : ifnames) { if (ifname.equals("*")) { return getAllIPv6NetIfs(); } NetworkInterface netIf = NetworkInterface.getByName(ifname); if (netIf == null) { // if not found by name, see if the name is actually an address try { InetAddress ipaddr = InetAddress.getByName(ifname); netIf = NetworkInterface.getByInetAddress(ipaddr); } catch (UnknownHostException ex) { log.warn("Unknown interface: " + ifname + ": " + ex); } } if (netIf != null) { if (netIf.isUp()) { // for multicast, the loopback interface is excluded if (netIf.supportsMulticast() && !netIf.isLoopback()) { boolean isV6 = false; List<InterfaceAddress> ifAddrs = netIf.getInterfaceAddresses(); for (InterfaceAddress ifAddr : ifAddrs) { if (ifAddr.getAddress() instanceof Inet6Address) { netIfs.add(netIf); isV6 = true; break; } } if (!isV6) { System.err.println("Interface is not configured for IPv6: " + netIf); return null; } } else { System.err.println("Interface does not support multicast: " + netIf); return null; } } else { System.err.println("Interface is not up: " + netIf); return null; } } else { System.err.println("Interface not found or inactive: " + ifname); return null; } } return netIfs; }