/** * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address. * <p> * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated. * "IPv4 mapped" format as * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported. * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address} * @param ipv4Mapped * <ul> * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li> * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li> * </ul> * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address. */ public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped) { byte[] bytes = getIPv6ByName(ip, ipv4Mapped); if (bytes == null) { return null; } try { return Inet6Address.getByAddress(null, bytes, -1); } catch (UnknownHostException e) { throw new RuntimeException(e); // Should never happen } }
public void readFrom(DataInput in) throws Exception { int len=in.readByte(); if(len > 0 && (len != Global.IPV4_SIZE && len != Global.IPV6_SIZE)) throw new IOException("length has to be " + Global.IPV4_SIZE + " or " + Global.IPV6_SIZE + " bytes (was " + len + " bytes)"); byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6) in.readFully(a); if(len == Global.IPV6_SIZE) { int scope_id=in.readInt(); this.ip_addr=Inet6Address.getByAddress(null, a, scope_id); } else { this.ip_addr=InetAddress.getByAddress(a); } // changed from readShort(): we need the full 65535, with a short we'd only get up to 32K ! port=in.readUnsignedShort(); }
/** * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address. * <p> * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated. * "IPv4 mapped" format as * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported. * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address} * @param ipv4Mapped * <ul> * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li> * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li> * </ul> * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address. */ public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped) { byte[] bytes = getIPv6ByName(ip, ipv4Mapped); if (bytes == null) { return null; } try { return Inet6Address.getByAddress(null, bytes, -1); } catch (UnknownHostException e) { throw new RuntimeException(e); // Should never happen } }
@Override public void readFrom(DataInput in) throws Exception { int len = in.readByte(); if (len > 0 && (len != Global.IPV4_SIZE && len != Global.IPV6_SIZE)) throw new IOException("length has to be " + Global.IPV4_SIZE + " or " + Global.IPV6_SIZE + " bytes (was " + len + " bytes)"); byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6) in.readFully(a); if (len == Global.IPV6_SIZE) { int scope_id = in.readInt(); this.ip_addr = Inet6Address.getByAddress(null, a, scope_id); } else { this.ip_addr = InetAddress.getByAddress(a); } // changed from readShort(): we need the full 65535, with a short we'd only get up to 32K ! port = in.readUnsignedShort(); vmViewId = in.readInt(); mostSigBits = in.readLong(); leastSigBits = in.readLong(); }
System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default:
/** * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address. * <p> * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated. * "IPv4 mapped" format as * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported. * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address} * @param ipv4Mapped * <ul> * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li> * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li> * </ul> * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address. */ public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped) { byte[] bytes = getIPv6ByName(ip, ipv4Mapped); if (bytes == null) { return null; } try { return Inet6Address.getByAddress(null, bytes, -1); } catch (UnknownHostException e) { throw new RuntimeException(e); // Should never happen } }
/** * Get the IPv6 equivalent of the given address. If the address is IPv4 then it is returned as a compatibility * address. * * @param inetAddress the address to convert (must not be {@code null}) * @return the converted address (not {@code null}) */ public static Inet6Address toInet6Address(InetAddress inetAddress) { if (inetAddress instanceof Inet6Address) { return (Inet6Address) inetAddress; } else { assert inetAddress instanceof Inet4Address; final byte[] addr = new byte[16]; addr[10] = addr[11] = (byte) 0xff; System.arraycopy(inetAddress.getAddress(), 0, addr, 12, 4); // get unresolved host name try { return Inet6Address.getByAddress(getHostNameIfResolved(inetAddress), addr, 0); } catch (UnknownHostException e) { // not possible throw new IllegalStateException(e); } } }
public InetSocketAddress toInetSocketAddress() { try { Inet6Address address = Inet6Address.getByAddress(null, sin6_addr(), (int)sin6_scope_id()); return new InetSocketAddress(address, sin6_port()); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } }
System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default:
NetworkInterface nif) throws UnknownHostException { Inet6Address address = Inet6Address.getByAddress(host, addr, 0);
Inet6Address inetAddress; try { inetAddress = Inet6Address.getByAddress(hostName == null ? toOptimalStringV6(bytes) : hostName, bytes, 0); } catch (UnknownHostException e) { inetAddress = Inet6Address.getByAddress(hostName == null ? toOptimalStringV6(bytes) : hostName, bytes, scopeId); } catch (UnknownHostException e) {
public static void bind(FileDescriptor fd, InetAddress address, int port) throws SocketException { if (address instanceof Inet6Address && ((Inet6Address) address).getScopeId() == 0) { // Linux won't let you bind a link-local address without a scope id. Find one. NetworkInterface nif = NetworkInterface.getByInetAddress(address); if (nif == null) { throw new SocketException("Can't bind to a link-local address without a scope id: " + address); } try { address = Inet6Address.getByAddress(address.getHostName(), address.getAddress(), nif.getIndex()); } catch (UnknownHostException ex) { throw new AssertionError(ex); // Can't happen. } } try { Libcore.os.bind(fd, address, port); } catch (ErrnoException errnoException) { throw new BindException(errnoException.getMessage(), errnoException); } }
return Inet6Address.getByAddress(null, bytes, -1); } catch (UnknownHostException e) { throw new RuntimeException(e); // Should never happen
/** * @throws Exception If failed. */ @Test public void testInet6Address() throws Exception { final InetAddress address = Inet6Address.getByAddress(new byte[16]); assertEquals(address, marshalUnmarshal(address)); }
return new CidrAddress(InetAddress.getByAddress(name, bytes), netmaskBits); } else { return new CidrAddress(Inet6Address.getByAddress(name, bytes, scopeId), netmaskBits);
bytes[15] = (byte) s8; try { return Inet6Address.getByAddress(toOptimalStringV6(bytes), bytes, 0); } catch (UnknownHostException e) {
/** * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address. * <p> * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated. * "IPv4 mapped" format as * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported. * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address} * @param ipv4Mapped * <ul> * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li> * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li> * </ul> * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address. */ public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped) { byte[] bytes = getIPv6ByName(ip, ipv4Mapped); if (bytes == null) { return null; } try { return Inet6Address.getByAddress(null, bytes, -1); } catch (UnknownHostException e) { throw new RuntimeException(e); // Should never happen } }
private InetAddress addDnsServer(Builder builder, String format, byte[] ipv6Template, String addr) throws UnknownHostException { int size = dnsServers.size(); size++; if (addr.contains("/")) {//https uri String alias = String.format(format, size + 1); dnsServers.put(alias, addr); builder.addRoute(alias, 32); return InetAddress.getByName(alias); } InetAddress address = InetAddress.getByName(addr); if (address instanceof Inet6Address && ipv6Template == null) { Log.i(TAG, "addDnsServer: Ignoring DNS server " + address); } else if (address instanceof Inet4Address) { String alias = String.format(format, size + 1); dnsServers.put(alias, address.getHostAddress()); builder.addRoute(alias, 32); return InetAddress.getByName(alias); } else if (address instanceof Inet6Address) { ipv6Template[ipv6Template.length - 1] = (byte) (size + 1); InetAddress i6addr = Inet6Address.getByAddress(ipv6Template); dnsServers.put(i6addr.getHostAddress(), address.getHostAddress()); return i6addr; } return null; }
void newDNSServer(VpnService.Builder builder, String format, byte[] ipv6Template, InetAddress addr) throws UnknownHostException { // Optimally we'd allow either one, but the forwarder checks if upstream size is empty, so // we really need to acquire both an ipv6 and an ipv4 subnet. if (addr instanceof Inet6Address && ipv6Template == null) { Log.i(TAG, "newDNSServer: Ignoring DNS server " + addr); } else if (addr instanceof Inet4Address && format == null) { Log.i(TAG, "newDNSServer: Ignoring DNS server " + addr); } else if (addr instanceof Inet4Address) { upstreamDnsServers.add(addr); String alias = String.format(format, upstreamDnsServers.size() + 1); Log.i(TAG, "configure: Adding DNS Server " + addr + " as " + alias); builder.addDnsServer(alias); builder.addRoute(alias, 32); vpnWatchDog.setTarget(InetAddress.getByName(alias)); } else if (addr instanceof Inet6Address) { upstreamDnsServers.add(addr); ipv6Template[ipv6Template.length - 1] = (byte) (upstreamDnsServers.size() + 1); InetAddress i6addr = Inet6Address.getByAddress(ipv6Template); Log.i(TAG, "configure: Adding DNS Server " + addr + " as " + i6addr); builder.addDnsServer(i6addr); vpnWatchDog.setTarget(i6addr); } }
InetAddress addr = Inet6Address.getByAddress(ipv6Template); Log.d(TAG, "configure: Adding IPv6 address" + addr); builder.addAddress(addr, 120);