Java Code Examples for java.net.StandardProtocolFamily#INET6

The following examples show how to use java.net.StandardProtocolFamily#INET6 . 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: JdpBroadcaster.java    From TencentKona-8 with GNU General Public License v2.0 6 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);
        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 2
Source File: JdpBroadcaster.java    From jdk8u60 with GNU General Public License v2.0 6 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);
        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 3
Source File: JdpBroadcaster.java    From jdk8u-jdk with GNU General Public License v2.0 6 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);
        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 4
Source File: JdpBroadcaster.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 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);
        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 5
Source File: AddressUtils.java    From bt with Apache License 2.0 6 votes vote down vote up
public static InetAddress getDefaultRoute(Class<? extends InetAddress> type) {
	InetAddress target = null;
	
	ProtocolFamily family = type == Inet6Address.class ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
	
	try(DatagramChannel chan=DatagramChannel.open(family)) {
		if(type == Inet4Address.class)
			target = InetAddress.getByAddress(new byte[] {8,8,8,8});
		if(type == Inet6Address.class)
			target = InetAddress.getByName("2001:4860:4860::8888");
		
		chan.connect(new InetSocketAddress(target,63));
		
		InetSocketAddress soa = (InetSocketAddress) chan.getLocalAddress();
		InetAddress local = soa.getAddress();
		
		if(type.isInstance(local) && !local.isAnyLocalAddress())
			return local;
		return null;
	} catch (IOException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 6
Source File: AddressUtils.java    From mldht with Mozilla Public License 2.0 6 votes vote down vote up
public static InetAddress getDefaultRoute(Class<? extends InetAddress> type) {
	InetAddress target = null;
	
	ProtocolFamily family = type == Inet6Address.class ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
	
	try(DatagramChannel chan=DatagramChannel.open(family)) {
		if(type == Inet4Address.class)
			target = InetAddress.getByAddress(new byte[] {8,8,8,8});
		if(type == Inet6Address.class)
			target = InetAddress.getByName("2001:4860:4860::8888");
		
		chan.connect(new InetSocketAddress(target,63));
		
		InetSocketAddress soa = (InetSocketAddress) chan.getLocalAddress();
		InetAddress local = soa.getAddress();
		
		if(type.isInstance(local) && !local.isAnyLocalAddress())
			return local;
		return null;
	} catch (IOException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 7
Source File: JdpBroadcaster.java    From hottub with GNU General Public License v2.0 6 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);
        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 8
Source File: JdpBroadcaster.java    From jdk8u_jdk with GNU General Public License v2.0 6 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);
        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: JdpBroadcaster.java    From dragonwell8_jdk with GNU General Public License v2.0 6 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);
        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 10
Source File: JdpBroadcaster.java    From openjdk-8 with GNU General Public License v2.0 6 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);
        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 11
Source File: SocketChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public <T> SocketChannel setOption(SocketOption<T> name, T value)
    throws IOException
{
    Objects.requireNonNull(name);
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");
    if (!name.type().isInstance(value))
        throw new IllegalArgumentException("Invalid value '" + value + "'");

    synchronized (stateLock) {
        ensureOpen();

        if (name == StandardSocketOptions.IP_TOS) {
            ProtocolFamily family = Net.isIPv6Available() ?
                StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
            Net.setSocketOption(fd, family, name, value);
            return this;
        }

        if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
            // SO_REUSEADDR emulated when using exclusive bind
            isReuseAddress = (Boolean)value;
            return this;
        }

        // no options that require special handling
        Net.setSocketOption(fd, name, value);
        return this;
    }
}
 
Example 12
Source File: ProtocolFamilyConverter.java    From simple-netty-source with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;

    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 13
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 14
Source File: NioSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the socket protocol family.
 */
private static ProtocolFamily family() {
    if (Net.isIPv6Available()) {
        return StandardProtocolFamily.INET6;
    } else {
        return StandardProtocolFamily.INET;
    }
}
 
Example 15
Source File: SocketChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> T getOption(SocketOption<T> name)
    throws IOException
{
    Objects.requireNonNull(name);
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    synchronized (stateLock) {
        ensureOpen();

        if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }

        // special handling for IP_TOS: always return 0 when IPv6
        if (name == StandardSocketOptions.IP_TOS) {
            ProtocolFamily family = Net.isIPv6Available() ?
                StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
            return (T) Net.getSocketOption(fd, family, name);
        }

        // no options that require special handling
        return (T) Net.getSocketOption(fd, name);
    }
}
 
Example 16
Source File: ProtocolFamilyConverter.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;
    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 17
Source File: ProtocolFamilyConverter.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;
    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 18
Source File: DatagramChannelImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
    throws IOException
{
    super(sp);

    NativeSocketAddress[] sockAddrs = null;

    ResourceManager.beforeUdpCreate();
    boolean initialized = false;
    try {
        this.family = Net.isIPv6Available()
                ? StandardProtocolFamily.INET6
                : StandardProtocolFamily.INET;
        this.fd = fd;
        this.fdVal = IOUtil.fdVal(fd);

        sockAddrs = NativeSocketAddress.allocate(3);
        readLock.lock();
        try {
            this.sourceSockAddr = sockAddrs[0];
            this.cachedSockAddr = sockAddrs[1];
        } finally {
            readLock.unlock();
        }
        this.targetSockAddr = sockAddrs[2];

        initialized = true;
    } finally {
        if (!initialized) {
            if (sockAddrs != null) NativeSocketAddress.freeAll(sockAddrs);
            nd.close(fd);
            ResourceManager.afterUdpClose();
        }
    }

    Runnable releaser = releaserFor(fd, sockAddrs);
    this.cleaner = CleanerFactory.cleaner().register(this, releaser);

    synchronized (stateLock) {
        this.localAddress = Net.localAddress(fd);
    }
}
 
Example 19
Source File: DatagramChannelImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family)
    throws IOException
{
    super(sp);

    Objects.requireNonNull(family, "'family' is null");
    if ((family != StandardProtocolFamily.INET) &&
            (family != StandardProtocolFamily.INET6)) {
        throw new UnsupportedOperationException("Protocol family not supported");
    }
    if (family == StandardProtocolFamily.INET6 && !Net.isIPv6Available()) {
        throw new UnsupportedOperationException("IPv6 not available");
    }

    FileDescriptor fd = null;
    NativeSocketAddress[] sockAddrs = null;

    ResourceManager.beforeUdpCreate();
    boolean initialized = false;
    try {
        this.family = family;
        this.fd = fd = Net.socket(family, false);
        this.fdVal = IOUtil.fdVal(fd);

        sockAddrs = NativeSocketAddress.allocate(3);
        readLock.lock();
        try {
            this.sourceSockAddr = sockAddrs[0];
            this.cachedSockAddr = sockAddrs[1];
        } finally {
            readLock.unlock();
        }
        this.targetSockAddr = sockAddrs[2];

        initialized = true;
    } finally {
        if (!initialized) {
            if (sockAddrs != null) NativeSocketAddress.freeAll(sockAddrs);
            if (fd != null) nd.close(fd);
            ResourceManager.afterUdpClose();
        }
    }

    Runnable releaser = releaserFor(fd, sockAddrs);
    this.cleaner = CleanerFactory.cleaner().register(this, releaser);
}
 
Example 20
Source File: DatagramChannelImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public DatagramChannelImpl(SelectorProvider sp) throws IOException {
    this(sp, (Net.isIPv6Available()
            ? StandardProtocolFamily.INET6
            : StandardProtocolFamily.INET));
}