java.net.ProtocolFamily Java Examples

The following examples show how to use java.net.ProtocolFamily. 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: NativeSocketAddress.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes the given InetSocketAddress into this socket address.
 * @param protocolFamily protocol family
 * @param isa the InetSocketAddress to encode
 * @return the size of the socket address (sizeof sockaddr or sockaddr6)
 * @throws UnsupportedAddressTypeException if the address type is not supported
 */
int encode(ProtocolFamily protocolFamily, InetSocketAddress isa) {
    if (protocolFamily == StandardProtocolFamily.INET) {
        // struct sockaddr
        InetAddress ia = isa.getAddress();
        if (!(ia instanceof Inet4Address))
            throw new UnsupportedAddressTypeException();
        putFamily(AF_INET);
        putAddress(AF_INET, ia);
        putPort(AF_INET, isa.getPort());
        return SIZEOF_SOCKADDR4;
    } else {
        // struct sockaddr6
        putFamily(AF_INET6);
        putAddress(AF_INET6, isa.getAddress());
        putPort(AF_INET6, isa.getPort());
        UNSAFE.putInt(address + OFFSET_SIN6_FLOWINFO, 0);
        return SIZEOF_SOCKADDR6;
    }
}
 
Example #2
Source File: JdpBroadcaster.java    From openjdk-8-source 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 openjdk-jdk8u 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 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 #5
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 #6
Source File: LocalServiceDiscoveryInfo.java    From bt with Apache License 2.0 6 votes vote down vote up
public LocalServiceDiscoveryInfo(
        Set<SocketChannelConnectionAcceptor> socketAcceptors,
        Collection<AnnounceGroup> announceGroups) {

    this.localPorts = unmodifiableSet(collectLocalPorts(socketAcceptors));

    Collection<NetworkInterface> networkInterfaces = new HashSet<>();
    boolean acceptIP4 = false;
    boolean acceptIP6 = false;
    for (SocketChannelConnectionAcceptor acceptor : socketAcceptors) {
        networkInterfaces.add(acceptor.getNetworkInterface());
        InetSocketAddress address = acceptor.getLocalAddress();
        ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(address.getAddress());
        if (protocolFamily == StandardProtocolFamily.INET) {
            acceptIP4 = true;
        } else {
            acceptIP6 = true;
        }
        if (acceptIP4 && acceptIP6) {
            break; // no need to look further
        }
    }

    this.compatibleGroups = unmodifiableCollection(collectCompatibleGroups(announceGroups, acceptIP4, acceptIP6));
    this.networkInterfaces = unmodifiableCollection(networkInterfaces);
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: DHT.java    From bt with Apache License 2.0 5 votes vote down vote up
private DHTtype(String shortName, int nodeslength, int addresslength, Class<? extends InetAddress> addresstype, int header, int maxSize, ProtocolFamily family) {
	
	this.shortName = shortName;
	this.NODES_ENTRY_LENGTH = nodeslength;
	this.PREFERRED_ADDRESS_TYPE = addresstype;
	this.ADDRESS_ENTRY_LENGTH = addresslength;
	this.HEADER_LENGTH = header;
	this.MAX_PACKET_SIZE = maxSize;
	this.PROTO_FAMILY = family;
}
 
Example #12
Source File: DatagramChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the protocol family to specify to set/getSocketOption for the
 * given socket option.
 */
private ProtocolFamily familyFor(SocketOption<?> name) {
    assert Thread.holdsLock(stateLock);

    // unspecified (most options)
    if (SocketOptionRegistry.findOption(name, Net.UNSPEC) != null)
        return Net.UNSPEC;

    // IPv4 socket
    if (family == StandardProtocolFamily.INET)
        return StandardProtocolFamily.INET;

    // IPv6 socket that is unbound
    if (localAddress == null)
        return StandardProtocolFamily.INET6;

    // IPv6 socket bound to wildcard or IPv6 address
    InetAddress address = localAddress.getAddress();
    if (address.isAnyLocalAddress() || (address instanceof Inet6Address))
        return StandardProtocolFamily.INET6;

    // IPv6 socket bound to IPv4 address
    if (Net.canUseIPv6OptionsWithIPv4LocalAddress()) {
        // IPV6_XXX options can be used
        return StandardProtocolFamily.INET6;
    } else {
        // IPV6_XXX options cannot be used
        return StandardProtocolFamily.INET;
    }
}
 
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: 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 #15
Source File: DNSDatagramSocketFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
DNSDatagramSocketFactory(Random random,
                         ProtocolFamily family,
                         int deviation,
                         int threshold) {
    this.random = Objects.requireNonNull(random);
    this.history = new PortHistory(HISTORY, random);
    this.family = family;
    this.deviation = Math.max(1, deviation);
    this.thresholdCount = Math.max(2, threshold);
}
 
Example #16
Source File: DNSDatagramSocketFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
DNSDatagramSocketFactory(Random random,
                         ProtocolFamily family,
                         int deviation,
                         int threshold) {
    this.random = Objects.requireNonNull(random);
    this.history = new PortHistory(HISTORY, random);
    this.family = family;
    this.deviation = Math.max(1, deviation);
    this.thresholdCount = Math.max(2, threshold);
}
 
Example #17
Source File: DNSDatagramSocketFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
DNSDatagramSocketFactory(Random random,
                         ProtocolFamily family,
                         int deviation,
                         int threshold) {
    this.random = Objects.requireNonNull(random);
    this.history = new PortHistory(HISTORY, random);
    this.family = family;
    this.deviation = Math.max(1, deviation);
    this.thresholdCount = Math.max(2, threshold);
}
 
Example #18
Source File: DNSDatagramSocketFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
DNSDatagramSocketFactory(Random random,
                         ProtocolFamily family,
                         int deviation,
                         int threshold) {
    this.random = Objects.requireNonNull(random);
    this.history = new PortHistory(HISTORY, random);
    this.family = family;
    this.deviation = Math.max(1, deviation);
    this.thresholdCount = Math.max(2, threshold);
}
 
Example #19
Source File: DNSDatagramSocketFactory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
DNSDatagramSocketFactory(Random random,
                         ProtocolFamily family,
                         int deviation,
                         int threshold) {
    this.random = Objects.requireNonNull(random);
    this.history = new PortHistory(HISTORY, random);
    this.family = family;
    this.deviation = Math.max(1, deviation);
    this.thresholdCount = Math.max(2, threshold);
}
 
Example #20
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 #21
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 #22
Source File: AnnounceGroupChannel.java    From bt with Apache License 2.0 5 votes vote down vote up
private synchronized DatagramChannel getChannel() throws IOException {
    if (channel == null || !channel.isOpen()) {
        if (shutdown.get()) {
            throw new IllegalStateException("Channel has been shut down");
        }
        ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(group.getAddress().getAddress());
        DatagramChannel _channel = selector.provider().openDatagramChannel(protocolFamily);
        _channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        // bind to any-local before setting TTL
        int port = group.getAddress().getPort();
        if (protocolFamily == StandardProtocolFamily.INET) {
            _channel.bind(new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port));
        } else {
            _channel.bind(new InetSocketAddress(Inet6Address.getByName("[::]"), port));
        }
        int timeToLive = group.getTimeToLive();
        if (timeToLive != 1) {
            _channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, timeToLive);
        }

        for (NetworkInterface iface : networkInterfaces) {
            _channel.join(group.getAddress().getAddress(), iface);
        }

        _channel.configureBlocking(false);
        channel = _channel;
    }
    return channel;
}
 
Example #23
Source File: InternetProtocolUtils.java    From bt with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@link StandardProtocolFamily#INET} for IPv4 address or {@link StandardProtocolFamily#INET6} for IPv6 address
 * @throws IllegalArgumentException if the address is neither IPv4 or IPv6
 * @since 1.6
 */
public static ProtocolFamily getProtocolFamily(InetAddress address) {
    if (address.getAddress().length == IP4_BYTES) {
        return StandardProtocolFamily.INET;
    } else if (address.getAddress().length == IP6_BYTES) {
        return StandardProtocolFamily.INET6;
    } else {
        throw new IllegalArgumentException("Can't determine protocol family for address: " + address);
    }
}
 
Example #24
Source File: Net.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
    throws IOException
{
    if (remote.isLinkLocalAddress()) {
        remote = IPAddressUtil.toScopedAddress(remote);
    }
    boolean preferIPv6 = isIPv6Available() &&
        (family != StandardProtocolFamily.INET);
    return connect0(preferIPv6, fd, remote, remotePort);
}
 
Example #25
Source File: Net.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static void bind(ProtocolFamily family, FileDescriptor fd,
                 InetAddress addr, int port) throws IOException
{
    boolean preferIPv6 = isIPv6Available() &&
        (family != StandardProtocolFamily.INET);
    if (addr.isLinkLocalAddress()) {
        addr = IPAddressUtil.toScopedAddress(addr);
    }
    bind0(fd, preferIPv6, exclusiveBind, addr, port);
}
 
Example #26
Source File: Net.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name)
    throws IOException
{
    Class<?> type = name.type();

    if (extendedOptions.isOptionSupported(name)) {
        return extendedOptions.getOption(fd, name);
    }

    // only simple values supported by this method
    if (type != Integer.class && type != Boolean.class)
        throw new AssertionError("Should not reach here");

    // map option name to platform level/name
    OptionKey key = SocketOptionRegistry.findOption(name, family);
    if (key == null)
        throw new AssertionError("Option not found");

    boolean mayNeedConversion = (family == UNSPEC);
    int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());

    if (type == Integer.class) {
        return Integer.valueOf(value);
    } else {
        return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
    }
}
 
Example #27
Source File: Net.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static InetSocketAddress checkAddress(SocketAddress sa, ProtocolFamily family) {
    InetSocketAddress isa = checkAddress(sa);
    if (family == StandardProtocolFamily.INET) {
        InetAddress addr = isa.getAddress();
        if (!(addr instanceof Inet4Address))
            throw new UnsupportedAddressTypeException();
    }
    return isa;
}
 
Example #28
Source File: DatagramChannel.java    From jtransc with Apache License 2.0 4 votes vote down vote up
public static DatagramChannel open(ProtocolFamily family) throws IOException {
	return SelectorProvider.provider().openDatagramChannel(family);
}
 
Example #29
Source File: SelectorProviderImpl.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example #30
Source File: DatagramChannelImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public <T> DatagramChannel 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();

        ProtocolFamily family = familyFor(name);

        if (name == StandardSocketOptions.IP_MULTICAST_IF) {
            NetworkInterface interf = (NetworkInterface)value;
            if (family == StandardProtocolFamily.INET6) {
                int index = interf.getIndex();
                if (index == -1)
                    throw new IOException("Network interface cannot be identified");
                Net.setInterface6(fd, index);
            } else {
                // need IPv4 address to identify interface
                Inet4Address target = Net.anyInet4Address(interf);
                if (target == null)
                    throw new IOException("Network interface not configured for IPv4");
                int targetAddress = Net.inet4AsInt(target);
                Net.setInterface4(fd, targetAddress);
            }
            return this;
        }
        if (name == StandardSocketOptions.SO_REUSEADDR
            && Net.useExclusiveBind() && localAddress != null) {
            reuseAddressEmulated = true;
            this.isReuseAddress = (Boolean)value;
        }

        // remaining options don't need any special handling
        Net.setSocketOption(fd, family, name, value);
        return this;
    }
}