/** * Get the scope ID of the given address (if it is an IPv6 address). * * @return the scope ID, or 0 if there is none or the address is an IPv4 address */ public static int getScopeId(InetAddress address) { return address instanceof Inet6Address ? ((Inet6Address) address).getScopeId() : 0; }
public void writeTo(DataOutput out) throws Exception { if(ip_addr != null) { byte[] address=ip_addr.getAddress(); // 4 bytes (IPv4) or 16 bytes (IPv6) out.writeByte(address.length); // 1 byte out.write(address, 0, address.length); if(ip_addr instanceof Inet6Address) out.writeInt(((Inet6Address)ip_addr).getScopeId()); } else { out.writeByte(0); } out.writeShort(port); }
public static boolean connect(SocketChannel ch, SocketAddress dest) throws IOException { if(dest instanceof InetSocketAddress) { InetAddress addr=((InetSocketAddress)dest).getAddress(); if(addr instanceof Inet6Address) { Inet6Address tmp=(Inet6Address)addr; if(tmp.getScopeId() != 0) { dest=new InetSocketAddress(InetAddress.getByAddress(tmp.getAddress()),((InetSocketAddress)dest).getPort()); } } } return ch.connect(dest); }
/** * normalize the ipv6 Address, convert scope name to scope id. * e.g. * convert * fe80:0:0:0:894:aeec:f37d:23e1%en0 * to * fe80:0:0:0:894:aeec:f37d:23e1%5 * <p> * The %5 after ipv6 address is called scope id. * see java doc of {@link Inet6Address} for more details. * * @param address the input address * @return the normalized address, with scope id converted to int */ static InetAddress normalizeV6Address(Inet6Address address) { String addr = address.getHostAddress(); int i = addr.lastIndexOf('%'); if (i > 0) { try { return InetAddress.getByName(addr.substring(0, i) + '%' + address.getScopeId()); } catch (UnknownHostException e) { // ignore logger.debug("Unknown IPV6 address: ", e); } } return address; }
/** * normalize the ipv6 Address, convert scope name to scope id. * e.g. * convert * fe80:0:0:0:894:aeec:f37d:23e1%en0 * to * fe80:0:0:0:894:aeec:f37d:23e1%5 * <p> * The %5 after ipv6 address is called scope id. * see java doc of {@link Inet6Address} for more details. * * @param address the input address * @return the normalized address, with scope id converted to int */ static InetAddress normalizeV6Address(Inet6Address address) { String addr = address.getHostAddress(); int i = addr.lastIndexOf('%'); if (i > 0) { try { return InetAddress.getByName(addr.substring(0, i) + '%' + address.getScopeId()); } catch (UnknownHostException e) { // ignore logger.debug("Unknown IPV6 address: ", e); } } return address; }
@Override public void writeTo(DataOutput out) throws Exception { if (ip_addr != null) { byte[] address = ip_addr.getAddress(); // 4 bytes (IPv4) or 16 bytes (IPv6) out.writeByte(address.length); // 1 byte out.write(address, 0, address.length); if (ip_addr instanceof Inet6Address) out.writeInt(((Inet6Address) ip_addr).getScopeId()); } else { out.writeByte(0); } out.writeShort(port); out.writeInt(vmViewId); out.writeLong(mostSigBits); out.writeLong(leastSigBits); }
/** * Utility method. If the dest address is IPv6, convert scoped link-local addrs into unscoped ones * @param sock * @param dest * @param sock_conn_timeout * @throws IOException */ public static void connect(Socket sock,SocketAddress dest,int sock_conn_timeout) throws IOException { if(dest instanceof InetSocketAddress) { InetAddress addr=((InetSocketAddress)dest).getAddress(); if(addr instanceof Inet6Address) { Inet6Address tmp=(Inet6Address)addr; if(tmp.getScopeId() != 0) { dest=new InetSocketAddress(InetAddress.getByAddress(tmp.getAddress()),((InetSocketAddress)dest).getPort()); } } } sock.connect(dest, sock_conn_timeout); }
public static NativeInetAddress newInstance(InetAddress addr) { byte[] bytes = addr.getAddress(); if (addr instanceof Inet6Address) { return new NativeInetAddress(bytes, ((Inet6Address) addr).getScopeId()); } else { // convert to ipv4 mapped ipv6 address; return new NativeInetAddress(ipv4MappedIpv6Address(bytes)); } }
public sockaddr_in6(InetSocketAddress address) { this(); sin6_port((short)address.getPort()); InetAddress addr = address.getAddress(); if (!(addr instanceof Inet6Address)) { throw new IllegalArgumentException("address must be a valid IPv6 address!"); } sin6_addr(addr.getAddress()); sin6_scope_id(((Inet6Address)addr).getScopeId()); }
public static NativeInetAddress newInstance(InetAddress addr) { byte[] bytes = addr.getAddress(); if (addr instanceof Inet6Address) { return new NativeInetAddress(bytes, ((Inet6Address) addr).getScopeId()); } else { // convert to ipv4 mapped ipv6 address; return new NativeInetAddress(ipv4MappedIpv6Address(bytes)); } }
/** * Init this instance and return {@code true} if the init was successful. */ private boolean init(ByteBuf buf, InetSocketAddress recipient) { array.clear(); if (!array.add(buf)) { return false; } // always start from offset 0 memoryAddress = array.memoryAddress(0); count = array.count(); InetAddress address = recipient.getAddress(); if (address instanceof Inet6Address) { addr = address.getAddress(); scopeId = ((Inet6Address) address).getScopeId(); } else { addr = ipv4MappedIpv6Address(address.getAddress()); scopeId = 0; } port = recipient.getPort(); return true; } }
public final int sendTo(ByteBuffer buf, int pos, int limit, InetAddress addr, int port) throws IOException { // just duplicate the toNativeInetAddress code here to minimize object creation as this method is expected // to be called frequently byte[] address; int scopeId; if (addr instanceof Inet6Address) { address = addr.getAddress(); scopeId = ((Inet6Address) addr).getScopeId(); } else { // convert to ipv4 mapped ipv6 address; scopeId = 0; address = ipv4MappedIpv6Address(addr.getAddress()); } int res = sendTo(fd, buf, pos, limit, address, scopeId, port); if (res >= 0) { return res; } if (res == ERROR_ECONNREFUSED_NEGATIVE) { throw new PortUnreachableException("sendTo failed"); } return ioResult("sendTo", res, SEND_TO_CONNECTION_RESET_EXCEPTION, SEND_TO_CLOSED_CHANNEL_EXCEPTION); }
public final int sendToAddresses(long memoryAddress, int length, InetAddress addr, int port) throws IOException { // just duplicate the toNativeInetAddress code here to minimize object creation as this method is expected // to be called frequently byte[] address; int scopeId; if (addr instanceof Inet6Address) { address = addr.getAddress(); scopeId = ((Inet6Address) addr).getScopeId(); } else { // convert to ipv4 mapped ipv6 address; scopeId = 0; address = ipv4MappedIpv6Address(addr.getAddress()); } int res = sendToAddresses(fd, memoryAddress, length, address, scopeId, port); if (res >= 0) { return res; } if (res == ERROR_ECONNREFUSED_NEGATIVE) { throw new PortUnreachableException("sendToAddresses failed"); } return ioResult("sendToAddresses", res, CONNECTION_RESET_EXCEPTION_SENDMSG, SEND_TO_ADDRESSES_CLOSED_CHANNEL_EXCEPTION); }
public final int sendToAddress(long memoryAddress, int pos, int limit, InetAddress addr, int port) throws IOException { // just duplicate the toNativeInetAddress code here to minimize object creation as this method is expected // to be called frequently byte[] address; int scopeId; if (addr instanceof Inet6Address) { address = addr.getAddress(); scopeId = ((Inet6Address) addr).getScopeId(); } else { // convert to ipv4 mapped ipv6 address; scopeId = 0; address = ipv4MappedIpv6Address(addr.getAddress()); } int res = sendToAddress(fd, memoryAddress, pos, limit, address, scopeId, port); if (res >= 0) { return res; } if (res == ERROR_ECONNREFUSED_NEGATIVE) { throw new PortUnreachableException("sendToAddress failed"); } return ioResult("sendToAddress", res, SEND_TO_ADDRESS_CONNECTION_RESET_EXCEPTION, SEND_TO_ADDRESS_CLOSED_CHANNEL_EXCEPTION); }
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); } }
public static int getScopeId(NetworkInterface networkInterface, InetAddress compareWith) { Assert.checkNotNullParam("networkInterface", networkInterface); Inet6Address cw6 = compareWith instanceof Inet6Address ? (Inet6Address) compareWith : null; Inet6Address address = doPrivileged((PrivilegedAction<Inet6Address>) () -> { final Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { final InetAddress a = addresses.nextElement(); if (a instanceof Inet6Address) { final Inet6Address a6 = (Inet6Address) a; if (cw6 == null || a6.isLinkLocalAddress() == cw6.isLinkLocalAddress() && a6.isSiteLocalAddress() == cw6.isSiteLocalAddress() ) { return a6; } } } return null; }); return address == null ? 0 : address.getScopeId(); }
/** * Init this instance and return {@code true} if the init was successful. */ private boolean init(ByteBuf buf, InetSocketAddress recipient) { array.clear(); if (!array.add(buf)) { return false; } // always start from offset 0 memoryAddress = array.memoryAddress(0); count = array.count(); InetAddress address = recipient.getAddress(); if (address instanceof Inet6Address) { addr = address.getAddress(); scopeId = ((Inet6Address) address).getScopeId(); } else { addr = ipv4MappedIpv6Address(address.getAddress()); scopeId = 0; } port = recipient.getPort(); return true; } }
public final int sendTo(ByteBuffer buf, int pos, int limit, InetAddress addr, int port) throws IOException { // just duplicate the toNativeInetAddress code here to minimize object creation as this method is expected // to be called frequently byte[] address; int scopeId; if (addr instanceof Inet6Address) { address = addr.getAddress(); scopeId = ((Inet6Address) addr).getScopeId(); } else { // convert to ipv4 mapped ipv6 address; scopeId = 0; address = ipv4MappedIpv6Address(addr.getAddress()); } int res = sendTo(fd, buf, pos, limit, address, scopeId, port); if (res >= 0) { return res; } if (res == ERROR_ECONNREFUSED_NEGATIVE) { throw new PortUnreachableException("sendTo failed"); } return ioResult("sendTo", res, SEND_TO_CONNECTION_RESET_EXCEPTION, SEND_TO_CLOSED_CHANNEL_EXCEPTION); }
public final int sendToAddresses(long memoryAddress, int length, InetAddress addr, int port) throws IOException { // just duplicate the toNativeInetAddress code here to minimize object creation as this method is expected // to be called frequently byte[] address; int scopeId; if (addr instanceof Inet6Address) { address = addr.getAddress(); scopeId = ((Inet6Address) addr).getScopeId(); } else { // convert to ipv4 mapped ipv6 address; scopeId = 0; address = ipv4MappedIpv6Address(addr.getAddress()); } int res = sendToAddresses(fd, memoryAddress, length, address, scopeId, port); if (res >= 0) { return res; } if (res == ERROR_ECONNREFUSED_NEGATIVE) { throw new PortUnreachableException("sendToAddresses failed"); } return ioResult("sendToAddresses", res, CONNECTION_RESET_EXCEPTION_SENDMSG, SEND_TO_ADDRESSES_CLOSED_CHANNEL_EXCEPTION); }
/** * Determine if this CIDR address matches the given address. * * @param address the address to test * @return {@code true} if the address matches, {@code false} otherwise */ public boolean matches(Inet6Address address) { Assert.checkNotNullParam("address", address); return networkAddress instanceof Inet6Address && bitsMatch(cachedBytes, address.getAddress(), netmaskBits) && (getScopeId() == 0 || getScopeId() == address.getScopeId()); }