/** * 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); } }
private static IpAddress getIpRangeMin(IpAddress ipAddress, int maskLength) { BigInteger ip = ipAddress.toBigInteger(); BigInteger subnetMask = new BigInteger( 1, new byte[]{-1, -1, -1, -1}) .shiftRight(maskLength) .xor(new BigInteger(1, new byte[]{-1, -1, -1, -1})); BigInteger networkAddress = ip.and(subnetMask); if (ipAddress instanceof Ipv4Address) { return Ipv4Address.gain(networkAddress.intValue()); } else if (ipAddress instanceof Ipv6Address) { throw new UnsupportedAddressTypeException(); } else { throw new IllegalArgumentException("Address acquisition failure"); } }
public boolean connect (SocketAddress remote, int timeout) throws IOException { if (!isOpen()) throw new ClosedChannelException(); if (isConnected()) throw new AlreadyConnectedException(); if (connectionPending) throw new ConnectionPendingException(); if (!(remote instanceof InetSocketAddress)) throw new UnsupportedAddressTypeException(); connectAddress = (InetSocketAddress) remote; if (connectAddress.isUnresolved()) throw new UnresolvedAddressException(); connected = channel.connect(connectAddress, timeout); connectionPending = !connected; return connected; }
/** @hide Until ready for a public API change */ @Override synchronized public DatagramChannel bind(SocketAddress local) throws IOException { checkOpen(); if (isBound) { throw new AlreadyBoundException(); } if (local == null) { local = new InetSocketAddress(Inet4Address.ANY, 0); } else if (!(local instanceof InetSocketAddress)) { throw new UnsupportedAddressTypeException(); } InetSocketAddress localAddress = (InetSocketAddress) local; IoBridge.bind(fd, localAddress.getAddress(), localAddress.getPort()); onBind(true /* updateSocketState */); return this; }
/** @hide Until ready for a public API change */ @Override synchronized public final SocketChannel bind(SocketAddress local) throws IOException { if (!isOpen()) { throw new ClosedChannelException(); } if (isBound) { throw new AlreadyBoundException(); } if (local == null) { local = new InetSocketAddress(Inet4Address.ANY, 0); } else if (!(local instanceof InetSocketAddress)) { throw new UnsupportedAddressTypeException(); } InetSocketAddress localAddress = (InetSocketAddress) local; IoBridge.bind(fd, localAddress.getAddress(), localAddress.getPort()); onBind(true /* updateSocketState */); return this; }
/** @hide Until ready for a public API change */ @Override public final ServerSocketChannel bind(SocketAddress localAddr, int backlog) throws IOException { if (!isOpen()) { throw new ClosedChannelException(); } if (socket.isBound()) { throw new AlreadyBoundException(); } if (localAddr != null && !(localAddr instanceof InetSocketAddress)) { throw new UnsupportedAddressTypeException(); } socket.bind(localAddr, backlog); return this; }
/** * * @param torrent */ protected UDPTrackerClient(SharedTorrent torrent, Peer peer, URI tracker) throws UnknownHostException { super(torrent, peer, tracker); /** * The UDP announce request protocol only supports IPv4 * * @see http://bittorrent.org/beps/bep_0015.html#ipv6 */ if (! (InetAddress.getByName(peer.getIp()) instanceof Inet4Address)) { throw new UnsupportedAddressTypeException(); } this.address = new InetSocketAddress( tracker.getHost(), tracker.getPort()); this.socket = null; this.random = new Random(); this.connectionExpiration = null; this.stop = false; }
/** * Returns a usable {@link Inet4Address} for the given interface name. * * <p> * If an interface name is given, return the first usable IPv4 address for * that interface. If no interface name is given or if that interface * doesn't have an IPv4 address, return's localhost address (if IPv4). * </p> * * <p> * It is understood this makes the client IPv4 only, but it is important to * remember that most BitTorrent extensions (like compact peer lists from * trackers and UDP tracker support) are IPv4-only anyway. * </p> * * @param iface The network interface name. * @return A usable IPv4 address as a {@link Inet4Address}. * @throws UnsupportedAddressTypeException If no IPv4 address was available * to bind on. */ private static Inet4Address getIPv4Address(String iface) throws SocketException, UnsupportedAddressTypeException, UnknownHostException { if (iface != null) { Enumeration<InetAddress> addresses = NetworkInterface.getByName(iface).getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address) { return (Inet4Address)addr; } } } InetAddress localhost = InetAddress.getLocalHost(); if (localhost instanceof Inet4Address) { return (Inet4Address)localhost; } throw new UnsupportedAddressTypeException(); }
/** * Test method for 'DatagramChannelImpl.connect(SocketAddress)' * * @throws IOException */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies UnsupportedAddressTypeException.", method = "connect", args = {java.net.SocketAddress.class} ) public void testConnect_UnsupportedType() throws IOException { assertFalse(this.channel1.isConnected()); class SubSocketAddress extends SocketAddress { private static final long serialVersionUID = 1L; public SubSocketAddress() { super(); } } SocketAddress newTypeAddress = new SubSocketAddress(); try { this.channel1.connect(newTypeAddress); fail("Should throw an UnsupportedAddressTypeException here."); } catch (UnsupportedAddressTypeException e) { // OK. } }
/** * @tests serialization/deserialization compatibility. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationSelf", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "UnsupportedAddressTypeException", args = {} ) }) public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new UnsupportedAddressTypeException()); }
/** * @tests serialization/deserialization compatibility with RI. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationGolden", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "UnsupportedAddressTypeException", args = {} ) }) public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new UnsupportedAddressTypeException()); }
/** * Returns a usable {@link java.net.Inet4Address} for the given interface name. * * <p> * If an interface name is given, return the first usable IPv4 address for * that interface. If no interface name is given or if that interface * doesn't have an IPv4 address, return's localhost address (if IPv4). * </p> * * <p> * It is understood this makes the client IPv4 only, but it is important to * remember that most BitTorrent extensions (like compact peer lists from * trackers and UDP tracker support) are IPv4-only anyway. * </p> * * @param iface The network interface name. * @return A usable IPv4 address as a {@link java.net.Inet4Address}. * @throws java.nio.channels.UnsupportedAddressTypeException If no IPv4 address was available * to bind on. */ private static Inet4Address getIPv4Address(String iface) throws SocketException, UnsupportedAddressTypeException, UnknownHostException { if (iface != null) { Enumeration<InetAddress> addresses = NetworkInterface.getByName(iface).getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address) { return (Inet4Address) addr; } } } InetAddress localhost = InetAddress.getLocalHost(); if (localhost instanceof Inet4Address) { return (Inet4Address) localhost; } throw new UnsupportedAddressTypeException(); }
/** * Test method for 'DatagramChannelImpl.connect(SocketAddress)' * * @throws IOException */ public void testConnect_UnsupportedType() throws IOException { assertFalse(this.channel1.isConnected()); class SubSocketAddress extends SocketAddress { private static final long serialVersionUID = 1L; public SubSocketAddress() { super(); } } SocketAddress newTypeAddress = new SubSocketAddress(); try { this.channel1.connect(newTypeAddress); fail("Should throw an UnsupportedAddressTypeException here."); } catch (UnsupportedAddressTypeException e) { // OK. } }
public void testCFII_UnsupportedType() throws Exception { class SubSocketAddress extends SocketAddress { private static final long serialVersionUID = 1L; //empty public SubSocketAddress() { super(); } } statusNotConnected_NotPending(); SocketAddress newTypeAddress = new SubSocketAddress(); try { this.channel1.connect(newTypeAddress); fail("Should throw an UnsupportedAddressTypeException here."); } catch (UnsupportedAddressTypeException e) { // OK. } }
private static Inet4Address getIPv4Address(String iface) throws SocketException, UnsupportedAddressTypeException, UnknownHostException { if (iface != null) { Enumeration<InetAddress> addresses = NetworkInterface.getByName(iface).getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address) { return (Inet4Address) addr; } } } InetAddress localhost = InetAddress.getLocalHost(); if (localhost instanceof Inet4Address) { return (Inet4Address) localhost; } throw new UnsupportedAddressTypeException(); }
/** * 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); } }
@Override public InetAddress getSingleHomedServiceBindAddress(int proto) { InetAddress[] addrs = currentBindIPs; if(proto == IP_PROTOCOL_VERSION_AUTO){ return addrs[0]; }else{ for( InetAddress addr: addrs ){ if( (proto == IP_PROTOCOL_VERSION_REQUIRE_V4 && addr instanceof Inet4Address || addr.isAnyLocalAddress()) || (proto == IP_PROTOCOL_VERSION_REQUIRE_V6 && addr instanceof Inet6Address) ){ if ( addr.isAnyLocalAddress()){ if ( proto == IP_PROTOCOL_VERSION_REQUIRE_V4 ){ return( anyLocalAddressIPv4 ); }else{ return( anyLocalAddressIPv6 ); } }else{ return( addr ); } } } } throw new UnsupportedAddressTypeException(); }
public InetAddress getSingleHomedServiceBindAddress(int proto) { InetAddress[] addrs = currentBindIPs; if(proto == IP_PROTOCOL_VERSION_AUTO){ return addrs[0]; }else{ for( InetAddress addr: addrs ){ if( (proto == IP_PROTOCOL_VERSION_REQUIRE_V4 && addr instanceof Inet4Address || addr.isAnyLocalAddress()) || (proto == IP_PROTOCOL_VERSION_REQUIRE_V6 && addr instanceof Inet6Address) ){ if ( addr.isAnyLocalAddress()){ if ( proto == IP_PROTOCOL_VERSION_REQUIRE_V4 ){ return( anyLocalAddressIPv4 ); }else{ return( anyLocalAddressIPv6 ); } }else{ return( addr ); } } } } throw new UnsupportedAddressTypeException(); }
static InetSocketAddress validateAddress(SocketAddress socketAddress) { if (socketAddress == null) { throw new IllegalArgumentException("socketAddress == null"); } if (!(socketAddress instanceof InetSocketAddress)) { throw new UnsupportedAddressTypeException(); } InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; if (inetSocketAddress.isUnresolved()) { throw new UnresolvedAddressException(); } return inetSocketAddress; }
static InetSocketAddress validateAddress(SocketAddress socketAddress) { if (null == socketAddress) { throw new IllegalArgumentException(); } if (!(socketAddress instanceof InetSocketAddress)) { throw new UnsupportedAddressTypeException(); } InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; if (inetSocketAddress.isUnresolved()) { throw new UnresolvedAddressException(); } return inetSocketAddress; }
/** * @tests {@link java.nio.channels.UnsupportedAddressTypeException#UnsupportedAddressTypeException()} */ public void test_Constructor() { UnsupportedAddressTypeException e = new UnsupportedAddressTypeException(); assertNull(e.getMessage()); assertNull(e.getLocalizedMessage()); assertNull(e.getCause()); }