Java Code Examples for java.net.NetworkInterface#supportsMulticast()

The following examples show how to use java.net.NetworkInterface#supportsMulticast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MulticastDiscoveryAgent.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
private static List<NetworkInterface> findNetworkInterfaces() throws SocketException {
    Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
    List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
    while (ifcs.hasMoreElements()) {
        NetworkInterface ni = ifcs.nextElement();
        LOG.trace("findNetworkInterfaces checking interface: {}", ni);

        if (ni.supportsMulticast() && ni.isUp()) {
            for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
                if (ia.getAddress() instanceof java.net.Inet4Address &&
                    !ia.getAddress().isLoopbackAddress() &&
                    !DEFAULT_EXCLUSIONS.contains(ni.getName())) {
                    // Add at the start, make usage order consistent with the
                    // existing ActiveMQ releases discovery will be used with.
                    interfaces.add(0, ni);
                    break;
                }
            }
        }
    }

    LOG.trace("findNetworkInterfaces returning: {}", interfaces);

    return interfaces;
}
 
Example 2
Source File: MulticastPulseAgent.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static NetworkInterface[] getNetworkInterfaces() {

        final HashSet<NetworkInterface> list = new HashSet<>();

        try {
            final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                final NetworkInterface next = interfaces.nextElement();

                if (next.supportsMulticast() && next.isUp()) {
                    list.add(next);
                }
            }
        } catch (final SocketException e) {
            //Ignore
        }

        return list.toArray(new NetworkInterface[list.size()]);
    }
 
Example 3
Source File: MulticastPulseClient.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static NetworkInterface[] getNetworkInterfaces() {

        final HashSet<NetworkInterface> list = new HashSet<NetworkInterface>();

        try {
            final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                final NetworkInterface next = interfaces.nextElement();

                if (next.supportsMulticast() && next.isUp()) {
                    list.add(next);
                }
            }
        } catch (SocketException e) {
            //Ignore
        }

        return list.toArray(new NetworkInterface[list.size()]);
    }
 
Example 4
Source File: IfConfig.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/** 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:" + nic.getMTU());
    flags.append(" index:" + nic.getIndex());
    return flags.toString();
}
 
Example 5
Source File: IfConfig.java    From crate with Apache License 2.0 6 votes vote down vote up
/** 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();
}
 
Example 6
Source File: UdpServerTests.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("JdkObsolete")
private boolean isMulticastEnabledIPv4Interface(NetworkInterface iface) {
	try {
		if (!iface.supportsMulticast() || !iface.isUp()) {
			return false;
		}
	}
	catch (SocketException se) {
		return false;
	}

	// Suppressed "JdkObsolete", usage of Enumeration is deliberate
	for (Enumeration<InetAddress> i = iface.getInetAddresses(); i.hasMoreElements(); ) {
		InetAddress address = i.nextElement();
		if (address.getClass() == Inet4Address.class) {
			return true;
		}
	}

	return false;
}
 
Example 7
Source File: PlainMulticastSender.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates an array of sockets with TTL and network interface set.
 *
 * @param mcastTTL multicast TTL.
 * @return an array of multicast sockets to broadcast on.
 * @throws IOException if I/O error occurred while creating a multicast socket.
 * @noinspection SocketOpenedButNotSafelyClosed, ConstantConditions
 */
private static MulticastSocket[] createSockets(final int mcastTTL) throws IOException {

   Exception lastException = null; // Records last error in case we could not create any sockets
   final List<MulticastSocket> socketList = new ArrayList<MulticastSocket>(11);
   final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
   while (enumeration.hasMoreElements()) {
      try {
         final NetworkInterface netIf = enumeration.nextElement();
         if (netIf.supportsMulticast()) {

            final MulticastSocket socket = new MulticastSocket(); // NOPMD
            socket.setTimeToLive(mcastTTL);
            socket.setNetworkInterface(netIf);
            socket.setSendBufferSize(SEND_BUFFER_SIZE);
            socketList.add(socket);
         }
      } catch (final Exception e) {

         lastException = e;
         ExceptionUtils.ignoreException(e, "continue to connect to those we can");
      }
   }
   if (socketList.isEmpty()) {
      throw new IOException("Could not create at least one multicast socket. Last error: " + lastException);
   }
   return socketList.toArray(new MulticastSocket[socketList.size()]);
}
 
Example 8
Source File: JdpBroadcaster.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);

        if (interf == null) {
            throw new JdpException("Unable to get network interface for " + srcAddress.toString());
        }

        if (!interf.isUp()) {
            throw new JdpException(interf.getName() + " is not up.");
        }

        if (!interf.supportsMulticast()) {
            throw new JdpException(interf.getName() + " does not support multicast.");
        }

        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 9
Source File: UDPLocator.java    From moleculer-java with MIT License 5 votes vote down vote up
protected void startReceivers(NetworkInterface ni, String udpMulticast, boolean udpBroadcast) throws Exception {
	if (ni == null || ni.isLoopback()) {
		return;
	}
	List<InterfaceAddress> list = ni.getInterfaceAddresses();
	if (list == null || list.isEmpty()) {
		return;
	}
	if (udpMulticast != null && ni.supportsMulticast()) {

		// Create multicast receiver
		receivers.add(new UDPMulticastReceiver(nodeID, udpMulticast, transporter, ni));
	}
	if (udpBroadcast) {
		for (InterfaceAddress ia : list) {
			if (ia == null) {
				continue;
			}
			InetAddress address = ia.getBroadcast();
			if (address == null || address.isLoopbackAddress()) {
				continue;
			}
			String udpAddress = address.getHostAddress();
			if (udpAddress == null || udpAddress.isEmpty() || udpAddress.startsWith("127.")) {
				continue;
			}

			// Create broadcast receiver
			receivers.add(new UDPBroadcastReceiver(nodeID, udpAddress, transporter));
		}
	}
}
 
Example 10
Source File: SupportsMulticastInterfaceCriteria.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @return <code>address</code> if <code>networkInterface</code>
 *         {@link NetworkInterface#supportsMulticast() supports multicast}.
 */
@Override
protected InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException {

    if( networkInterface.supportsMulticast() )
        return address;
    return null;
}
 
Example 11
Source File: SetGetNetworkInterfaceTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 12
Source File: SetGetNetworkInterfaceTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 13
Source File: SetGetNetworkInterfaceTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 14
Source File: SetGetNetworkInterfaceTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 15
Source File: SetGetNetworkInterfaceTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 16
Source File: SetGetNetworkInterfaceTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 17
Source File: SetGetNetworkInterfaceTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 18
Source File: SetGetNetworkInterfaceTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
 
Example 19
Source File: NetworkAddressFactoryImpl.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validation of every discovered network interface.
 * <p>
 * Override this method to customize which network interfaces are used.
 * </p>
 * <p>
 * The given implementation ignores interfaces which are
 * </p>
 * <ul>
 * <li>loopback (yes, we do not bind to lo0)</li>
 * <li>down</li>
 * <li>have no bound IP addresses</li>
 * <li>named "vmnet*" (OS X VMWare does not properly stop interfaces when it quits)</li>
 * <li>named "vnic*" (OS X Parallels interfaces should be ignored as well)</li>
 * <li>named "*virtual*" (VirtualBox interfaces, for example</li>
 * <li>named "ppp*"</li>
 * </ul>
 *
 * @param iface The interface to validate.
 * @return True if the given interface matches all validation criteria.
 * @throws Exception If any validation test failed with an un-recoverable error.
 */
protected boolean isUsableNetworkInterface(NetworkInterface iface) throws Exception {
    if (!iface.isUp()) {
        log.finer("Skipping network interface (down): " + iface.getDisplayName());
        return false;
    }

    if (getInetAddresses(iface).size() == 0) {
        log.finer("Skipping network interface without bound IP addresses: " + iface.getDisplayName());
        return false;
    }

    if (iface.getName().toLowerCase(Locale.ENGLISH).startsWith("vmnet") ||
    		(iface.getDisplayName() != null &&  iface.getDisplayName().toLowerCase(Locale.ENGLISH).contains("vmnet"))) {
        log.finer("Skipping network interface (VMWare): " + iface.getDisplayName());
        return false;
    }

    if (iface.getName().toLowerCase(Locale.ENGLISH).startsWith("vnic")) {
        log.finer("Skipping network interface (Parallels): " + iface.getDisplayName());
        return false;
    }

    if (iface.getName().toLowerCase(Locale.ENGLISH).contains("virtual")) {
        log.finer("Skipping network interface (named '*virtual*'): " + iface.getDisplayName());
        return false;
    }

    if (iface.getName().toLowerCase(Locale.ENGLISH).startsWith("ppp")) {
        log.finer("Skipping network interface (PPP): " + iface.getDisplayName());
        return false;
    }

    if (iface.isLoopback()) {
        log.finer("Skipping network interface (ignoring loopback): " + iface.getDisplayName());
        return false;
    }

    if (useInterfaces.size() > 0 && !useInterfaces.contains(iface.getName())) {
        log.finer("Skipping unwanted network interface (-D" + SYSTEM_PROPERTY_NET_IFACES + "): " + iface.getName());
        return false;
    }

    if (!iface.supportsMulticast())
        log.warning("Network interface may not be multicast capable: "  + iface.getDisplayName());

    return true;
}
 
Example 20
Source File: SetGetNetworkInterfaceTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
    System.out.println("checking netif == " + netIf.getName());
    return  (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}