log.info(String.format("PointToPoint? %s", networkInterface.isPointToPoint())); log.info(String.format("Supports multicast? %s", networkInterface.supportsMulticast())); log.info(String.format("Virtual? %s", networkInterface.isVirtual()));
/** * Check if the current machine support IP6 * * @return true if the IP6 is supported. */ public static boolean isIP6Supported() { final Enumeration<NetworkInterface> networkInterfaces; try { networkInterfaces = getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { final NetworkInterface networkInterface = networkInterfaces.nextElement(); if (networkInterface.isUp() && !networkInterface.isLoopback() && !networkInterface.isPointToPoint()) { final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { final InetAddress inetAddress = inetAddresses.nextElement(); if (inetAddress instanceof Inet6Address) { return true; } } } } } catch (SocketException e) { LOGGER.error("Error while resolving non look back local addresses.", e); } return false; }
private @Nullable String getFirstLocalIPv4Address() { try { String hostAddress = null; final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { final NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) { continue; } final Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { final InetAddress currentAddr = addresses.nextElement(); if (currentAddr.isLoopbackAddress() || (currentAddr instanceof Inet6Address)) { continue; } if (hostAddress != null) { LOGGER.warn("Found multiple local interfaces - ignoring {}", currentAddr.getHostAddress()); } else { hostAddress = currentAddr.getHostAddress(); } } } return hostAddress; } catch (SocketException ex) { LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex); return null; } }
/** format network interface flags */ private static String formatFlags(NetworkInterface nic) throws SocketException { StringBuilder flags = new StringBuilder(); if (nic.isUp()) { flags.append("UP "); } if (nic.supportsMulticast()) { flags.append("MULTICAST "); } if (nic.isLoopback()) { flags.append("LOOPBACK "); } if (nic.isPointToPoint()) { flags.append("POINTOPOINT "); } if (nic.isVirtual()) { flags.append("VIRTUAL "); } flags.append("mtu:").append(nic.getMTU()); flags.append(" index:").append(nic.getIndex()); return flags.toString(); } }
while (interfaces.hasMoreElements()) { final NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) { continue;
private @Nullable String getIPv4inSubnet(String ipAddress, String subnetMask) { try { final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { final NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) { continue; } for (InterfaceAddress ifAddr : current.getInterfaceAddresses()) { InetAddress addr = ifAddr.getAddress(); if (addr.isLoopbackAddress() || (addr instanceof Inet6Address)) { continue; } String ipv4AddressOnInterface = addr.getHostAddress(); String subnetStringOnInterface = getIpv4NetAddress(ipv4AddressOnInterface, ifAddr.getNetworkPrefixLength()) + "/" + String.valueOf(ifAddr.getNetworkPrefixLength()); String configuredSubnetString = getIpv4NetAddress(ipAddress, Short.parseShort(subnetMask)) + "/" + subnetMask; // use first IP within this subnet if (subnetStringOnInterface.equals(configuredSubnetString)) { return ipv4AddressOnInterface; } } } } catch (SocketException ex) { LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex); } return null; }
log.info(String.format("PointToPoint? %s", networkInterface.isPointToPoint())); log.info(String.format("Supports multicast? %s", networkInterface.supportsMulticast())); log.info(String.format("Virtual? %s", networkInterface.isVirtual()));
/** * {@inheritDoc} * * @return <code>address</code> if <code>networkInterface</code> is a * {@link NetworkInterface#isPointToPoint() point-to-point interface}. */ @Override protected InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException { if( networkInterface.isPointToPoint() ) return address; return null; }
/** * {@inheritDoc} * * @return <code>address</code> if <code>networkInterface</code> is a * {@link NetworkInterface#isPointToPoint() point-to-point interface}. */ @Override protected InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException { if( networkInterface.isPointToPoint() ) return address; return null; }
/** * {@inheritDoc} * * @return <code>address</code> if <code>networkInterface</code> is a * {@link NetworkInterface#isPointToPoint() point-to-point interface}. */ @Override protected InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException { if( networkInterface.isPointToPoint() ) return address; return null; }
/** * {@inheritDoc} * * @return <code>true</code> if <code>networkInterface</code> is a * {@link NetworkInterface#isPointToPoint() point-to-point interface}. */ @Override public boolean isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException { return networkInterface.isPointToPoint(); }
/** * {@inheritDoc} * * @return <code>address</code> if <code>networkInterface</code> is a * {@link NetworkInterface#isPointToPoint() point-to-point interface}. */ @Override protected InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException { if( networkInterface.isPointToPoint() ) return address; return null; }
@Override public boolean use(NetworkInterface nif, InetAddress adr) throws SocketException { return (adr != null) && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()); } }
@Override public boolean use(NetworkInterface nif, InetAddress adr) throws SocketException { return (adr != null) && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()); } }
@Override public boolean use(NetworkInterface nif, InetAddress adr) throws SocketException { return (adr != null) && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()); } }
@Override public boolean use(NetworkInterface nif, InetAddress adr) throws SocketException { return (adr != null) && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()); } }
public static String getHardwareAddress() throws Exception { InetAddress ip = InetAddress.getLocalHost(); NetworkInterface ni = NetworkInterface.getByInetAddress(ip); if (!ni.isVirtual() && !ni.isLoopback() && !ni.isPointToPoint() && ni.isUp()) { final byte[] bb = ni.getHardwareAddress(); return IntStream.generate(ByteBuffer.wrap(bb)::get).limit(bb.length) .mapToObj(b -> String.format("%02X", (byte)b)) .collect(Collectors.joining("-")); } return null; }
public static String getActiveNetworkMacAddress() throws Exception { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while ( interfaces.hasMoreElements() ) { NetworkInterface nif = interfaces.nextElement(); // Obvious what the next IF is doing, but it must be connected, non-PPP, non-loopback, and non-virtual if ( ( nif.isUp() ) && ( !nif.isPointToPoint() ) && ( !nif.isLoopback() ) && ( !nif.isVirtual() ) ) { return getInterfaceInfo( nif ); } } return null; }
public Ifaddr(Ruby runtime, RubyClass metaClass, NetworkInterface ni) throws Exception { super(runtime, metaClass); this.isUp = ni.isUp(); this.name = ni.getDisplayName(); this.isLoopback = ni.isLoopback(); this.isPointToPoint = ni.isPointToPoint(); this.networkInterface = ni; this.isLink = true; setAddr(runtime); setIndex(ni); setInspectString(ni); }
public Ifaddr(Ruby runtime, RubyClass metaClass, NetworkInterface ni) throws Exception { super(runtime, metaClass); this.isUp = ni.isUp(); this.name = ni.getDisplayName(); this.isLoopback = ni.isLoopback(); this.isPointToPoint = ni.isPointToPoint(); this.networkInterface = ni; this.isLink = true; setAddr(runtime); setIndex(ni); setInspectString(ni); }