Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); System.out.println(ni.getName() + " " + ni.getDisplayName()); }
public List<String> call() throws IOException { List<String> names = new ArrayList<String>(); Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); LOGGER.log(Level.FINE, "Listing up IP addresses for {0}", ni.getDisplayName()); Enumeration<InetAddress> e = ni.getInetAddresses(); while (e.hasMoreElements()) { InetAddress ia = e.nextElement(); if(ia.isLoopbackAddress()) { LOGGER.log(Level.FINE, "{0} is a loopback address", ia); continue; } if(!(ia instanceof Inet4Address)) { LOGGER.log(Level.FINE, "{0} is not an IPv4 address", ia); continue; } LOGGER.log(Level.FINE, "{0} is a viable candidate", ia); names.add(ia.getHostAddress()); } } return names; } private static final long serialVersionUID = 1L;
public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()) { NetworkInterface i=en.nextElement(); System.out.println(i.getName() + ':'); System.out.println(" \t" + i.getDisplayName()); for(Enumeration<InetAddress> en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=en2.nextElement(); System.out.println(" \t" + addr + " (" + addr.getHostName() + ')'); } System.out.println("---------------------"); } }
/** * Used for debugging to get a human readable description of the channel. * * @return a human readable description of the channel. */ public String description() { final StringBuilder builder = new StringBuilder("UdpChannel - "); if (null != localInterface) { builder .append("interface: ") .append(localInterface.getDisplayName()) .append(", "); } builder .append("localData: ").append(localData) .append(", remoteData: ").append(remoteData) .append(", ttl: ").append(multicastTtl); return builder.toString(); }
@Override public InetAddress getLocalAddress(NetworkInterface networkInterface, boolean isIPv6, InetAddress remoteAddress) { // TODO: This is totally random because we can't access low level InterfaceAddress on Android! for (InetAddress localAddress : getInetAddresses(networkInterface)) { if (isIPv6 && localAddress instanceof Inet6Address) return localAddress; if (!isIPv6 && localAddress instanceof Inet4Address) return localAddress; } throw new IllegalStateException("Can't find any IPv4 or IPv6 address on interface: " + networkInterface.getDisplayName()); }
String ip; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + " " + ip); } } } catch (SocketException e) { throw new RuntimeException(e); }
protected void discoverNetworkInterfaces() throws InitializationException { try { Enumeration<NetworkInterface> interfaceEnumeration = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface iface : Collections.list(interfaceEnumeration)) { //displayInterfaceInformation(iface); log.finer("Analyzing network interface: " + iface.getDisplayName()); if (isUsableNetworkInterface(iface)) { log.fine("Discovered usable network interface: " + iface.getDisplayName()); synchronized (networkInterfaces) { networkInterfaces.add(iface); } } else { log.finer("Ignoring non-usable network interface: " + iface.getDisplayName()); } } } catch (Exception ex) { throw new InitializationException("Could not not analyze local network interfaces: " + ex, ex); } }
try { InetAddress localhost = InetAddress.getLocalHost(); LOG.info(" IP Addr: " + localhost.getHostAddress()); // Just in case this host has multiple IP addresses.... InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName()); if (allMyIps != null && allMyIps.length > 1) { LOG.info(" Full list of IP addresses:"); for (int i = 0; i < allMyIps.length; i++) { LOG.info(" " + allMyIps[i]); } } } catch (UnknownHostException e) { LOG.info(" (error retrieving server host name)"); } try { LOG.info("Full list of Network Interfaces:"); for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); LOG.info(" " + intf.getName() + " " + intf.getDisplayName()); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { LOG.info(" " + enumIpAddr.nextElement().toString()); } } } catch (SocketException e) { LOG.info(" (error retrieving network interface list)"); }
@Override void dump( Logger logger ) { try { Enumeration<NetworkInterface> networkInterfaces = getNetworkInterfaces(); while ( networkInterfaces.hasMoreElements() ) { NetworkInterface iface = networkInterfaces.nextElement(); logger.log( String.format( "Interface %s:", iface.getDisplayName() ) ); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while ( addresses.hasMoreElements() ) { InetAddress address = addresses.nextElement(); String hostAddress = address.getHostAddress(); logger.log( " address: %s", hostAddress ); } } } catch ( SocketException e ) { logger.log( "ERROR: failed to inspect network interfaces and addresses: " + e.getMessage() ); } } },
/** * Return a list of internet addresses sorted by importance (most important first). * This is the order we want to test for matches when selecting an internet address. */ public static ArrayList<java.net.InetAddress> calcPrioritizedInetAddressList() { ArrayList<java.net.InetAddress> ips = new ArrayList<java.net.InetAddress>(); { ArrayList<NetworkInterface> networkInterfaceList = calcPrioritizedInterfaceList(); for (int i = 0; i < networkInterfaceList.size(); i++) { NetworkInterface ni = networkInterfaceList.get(i); Enumeration<InetAddress> ias = ni.getInetAddresses(); while( ias.hasMoreElements() ) { InetAddress ia; ia = ias.nextElement(); ips.add(ia); Log.info("Possible IP Address: " + ni.getName() + " (" + ni.getDisplayName() + "), " + ia.getHostAddress()); } } } return ips; }
private static String errorNoMatchingInterfaces( final NetworkInterface[] filteredInterfaces, final InterfaceSearchAddress address) throws SocketException { final StringBuilder builder = new StringBuilder() .append("Unable to find multicast interface matching criteria: ") .append(address.getAddress()) .append('/') .append(address.getSubnetPrefix()); if (filteredInterfaces.length > 0) { builder.append(lineSeparator()).append(" Candidates:"); for (final NetworkInterface ifc : filteredInterfaces) { builder .append(lineSeparator()) .append(" - Name: ") .append(ifc.getDisplayName()) .append(", addresses: ") .append(ifc.getInterfaceAddresses()) .append(", multicast: ") .append(ifc.supportsMulticast()); } } return builder.toString(); }
private NetworkInterface findNetworkInterface() throws SocketException { Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces(); List<NetworkInterface> possibles = new ArrayList<NetworkInterface>(); while (ifcs.hasMoreElements()) { NetworkInterface ni = ifcs.nextElement(); try { if (ni.supportsMulticast() && ni.isUp()) { for (InterfaceAddress ia : ni.getInterfaceAddresses()) { if (ia != null && ia.getAddress() instanceof java.net.Inet4Address && !ia.getAddress().isLoopbackAddress() && (ni.getDisplayName()==null || !ni.getDisplayName().startsWith("vnic"))) { possibles.add(ni); } } } } catch (SocketException ignored) {} } return possibles.isEmpty() ? null : possibles.get(possibles.size() - 1); }
/** * Gets display name of the network interface this IP address belongs to. * * @param addr IP address for which to find network interface name. * @return Network interface name or {@code null} if can't be found. */ @Nullable public static String getNetworkInterfaceName(String addr) { assert addr != null; try { InetAddress inetAddr = InetAddress.getByName(addr); for (NetworkInterface itf : asIterable(NetworkInterface.getNetworkInterfaces())) for (InetAddress itfAddr : asIterable(itf.getInetAddresses())) if (itfAddr.equals(inetAddr)) return itf.getDisplayName(); } catch (UnknownHostException ignore) { return null; } catch (SocketException ignore) { return null; } return null; }
public InetAddress getLocalAddress(NetworkInterface networkInterface, boolean isIPv6, InetAddress remoteAddress) { // First try to find a local IP that is in the same subnet as the remote IP InetAddress localIPInSubnet = getBindAddressInSubnetOf(remoteAddress); if (localIPInSubnet != null) return localIPInSubnet; // There are two reasons why we end up here: // // - Windows Vista returns a 64 or 128 CIDR prefix if you ask it for the network prefix length of an IPv4 address! // // - We are dealing with genuine IPv6 addresses // // - Something is really wrong on the LAN and we received a multicast datagram from a source we can't reach via IP log.finer("Could not find local bind address in same subnet as: " + remoteAddress.getHostAddress()); // Next, just take the given interface (which is really totally random) and get the first address that we like for (InetAddress interfaceAddress: getInetAddresses(networkInterface)) { if (isIPv6 && interfaceAddress instanceof Inet6Address) return interfaceAddress; if (!isIPv6 && interfaceAddress instanceof Inet4Address) return interfaceAddress; } throw new IllegalStateException("Can't find any IPv4 or IPv6 address on interface: " + networkInterface.getDisplayName()); }
private void updateGateway() throws UnknownHostException { String gateway = NetworkHelper.getIfaceGateway(mInterface.getDisplayName()); if(gateway != null) { mGateway = new IP4Address(gateway); } else { Logger.warning("gateway not found"); } }
public Child dissect(Target target, OnAccountListener listener) throws ChildManager.ChildNotStartedException { StringBuilder sb = new StringBuilder(); sb.append("-Tpq -i "); try { sb.append(System.getNetwork().getInterface().getDisplayName()); } catch (Exception e) { System.errorLogging(e); throw new ChildManager.ChildNotStartedException(); } // poison the entire network if(target.getType() == Target.Type.NETWORK) sb.append(" /// ///"); // router -> target poison else { sb.append(" /"); sb.append(target.getCommandLineRepresentation()); sb.append("// ///"); } return super.async(sb.toString(), listener); }
public Child dnsSpoof(Target target, OnDNSSpoofedReceiver listener) throws ChildManager.ChildNotStartedException { StringBuilder sb = new StringBuilder(); sb.append("-Tq -P dns_spoof -i "); try { sb.append(System.getNetwork().getInterface().getDisplayName()); } catch (Exception e) { System.errorLogging(e); throw new ChildManager.ChildNotStartedException(); } // poison the entire network if(target.getType() == Target.Type.NETWORK) { sb.append(" /// ///"); // router -> target poison } else { sb.append(" /"); sb.append(target.getCommandLineRepresentation()); sb.append("// "); sb.append(" ///"); } //sb.append(" 1>&2 "); return super.async(sb.toString(), listener); } }
synchronized public void init(NetworkInterface networkInterface, Router router, NetworkAddressFactory networkAddressFactory, DatagramProcessor datagramProcessor) throws InitializationException { this.router = router; this.networkAddressFactory = networkAddressFactory; this.datagramProcessor = datagramProcessor; this.multicastInterface = networkInterface; try { log.info("Creating wildcard socket (for receiving multicast datagrams) on port: " + configuration.getPort()); multicastAddress = new InetSocketAddress(configuration.getGroup(), configuration.getPort()); socket = new MulticastSocket(configuration.getPort()); socket.setReuseAddress(true); socket.setReceiveBufferSize(32768); // Keep a backlog of incoming datagrams if we are not fast enough log.info("Joining multicast group: " + multicastAddress + " on network interface: " + multicastInterface.getDisplayName()); socket.joinGroup(multicastAddress, multicastInterface); } catch (Exception ex) { throw new InitializationException("Could not initialize " + getClass().getSimpleName() + ": " + ex); } }
public Child start(HostReceiver receiver) throws ChildManager.ChildNotStartedException { String ifName; if(System.getNetwork() == null) { throw new ChildManager.ChildNotStartedException(); } ifName = System.getNetwork().getInterface().getDisplayName(); return async(ifName, receiver); } }
public Child spoof(Target target, ArpSpoofReceiver receiver) throws ChildManager.ChildNotStartedException { String commandLine = ""; try{ String gw = System.getNetwork().getGatewayAddress().getHostAddress(); String iface = System.getNetwork().getInterface().getDisplayName(); if(target.getType() == Type.NETWORK) commandLine = "-i " + iface + " " + gw; else commandLine = "-i " + iface + " -t " + target.getCommandLineRepresentation() + " " + gw; } catch(Exception e) { System.errorLogging(e); } return super.async(commandLine, receiver); } }