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); }
byte[] ipv4 = new byte[4]; System.arraycopy(addr, offset, ipv4, 0, 4); address = InetAddress.getByAddress(ipv4); break; System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default: throw new Error(); return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new Error("Should never happen", e);
private IntObjectMap<DnsQueryContext> getOrCreateContextMap(InetSocketAddress nameServerAddr) { synchronized (map) { final IntObjectMap<DnsQueryContext> contexts = map.get(nameServerAddr); if (contexts != null) { return contexts; } final IntObjectMap<DnsQueryContext> newContexts = new IntObjectHashMap<DnsQueryContext>(); final InetAddress a = nameServerAddr.getAddress(); final int port = nameServerAddr.getPort(); map.put(nameServerAddr, newContexts); if (a instanceof Inet4Address) { // Also add the mapping for the IPv4-compatible IPv6 address. final Inet4Address a4 = (Inet4Address) a; if (a4.isLoopbackAddress()) { map.put(new InetSocketAddress(NetUtil.LOCALHOST6, port), newContexts); } else { map.put(new InetSocketAddress(toCompactAddress(a4), port), newContexts); } } else if (a instanceof Inet6Address) { // Also add the mapping for the IPv4 address if this IPv6 address is compatible. final Inet6Address a6 = (Inet6Address) a; if (a6.isLoopbackAddress()) { map.put(new InetSocketAddress(NetUtil.LOCALHOST4, port), newContexts); } else if (a6.isIPv4CompatibleAddress()) { map.put(new InetSocketAddress(toIPv4Address(a6), port), newContexts); } } return newContexts; } }
if (!ip.isIPv4CompatibleAddress()) { return false; byte[] bytes = ip.getAddress(); if ((bytes[12] == 0) && (bytes[13] == 0)
/** * 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; }
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()); }
protected SocketAddress localAddress(DatagramChannel handle) throws Exception { InetSocketAddress inetSocketAddress = (InetSocketAddress) handle.socket().getLocalSocketAddress(); InetAddress inetAddress = inetSocketAddress.getAddress(); if ((inetAddress instanceof Inet6Address) && (((Inet6Address) inetAddress).isIPv4CompatibleAddress())) { // Ugly hack to workaround a problem on linux : the ANY address is always converted to IPV6 // even if the original address was an IPV4 address. We do store the two IPV4 and IPV6 // ANY address in the map. byte[] ipV6Address = ((Inet6Address) inetAddress).getAddress(); byte[] ipV4Address = new byte[4]; System.arraycopy(ipV6Address, 12, ipV4Address, 0, 4); InetAddress inet4Adress = Inet4Address.getByAddress(ipV4Address); return new InetSocketAddress(inet4Adress, inetSocketAddress.getPort()); } else { return inetSocketAddress; } }
finder.initializeLocalAddresses(Arrays.asList(new InetSocketAddress(InetAddress.getLocalHost(), 1000))); InetSocketAddress node1 = new InetSocketAddress(InetAddress.getLocalHost(), 1000); InetSocketAddress node2 = new InetSocketAddress(InetAddress.getLocalHost(), 1001); InetSocketAddress node3 = new InetSocketAddress( Inet6Address.getByName("2001:0db8:85a3:08d3:1319:47ff:fe3b:7fd3"), 1002);
List <NSDServiceInfo> Data = new ArrayList<>(); InetAddress hostInet =InetAddress.getByName(Data.get(position).getHost().GetHostAddress()); byte [] addressBytes = hostInet.getAddress(); Inet6Address dest6 = Inet6Address.getByAddress(Data.get(position).getHost().GetHostAddress(), addressBytes, NetworkInterface.getByInetAddress(hostInet)); Inet4Address dest4 = (Inet4Address) Inet4Address.getByAddress (Data.get(position).getHost().GetHostAddress(), addressBytes); Log.d ( "IP", "IPv4" + dest4.getHostAddress ()); Log.d ( "IP", "IPv6:" + dest6.getHostAddress ());
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; } }
private DNSRecord.Pointer getDNS4ReverseAddressRecord(boolean unique, int ttl) { if (this.getInetAddress() instanceof Inet4Address) { return new DNSRecord.Pointer(this.getInetAddress().getHostAddress() + ".in-addr.arpa.", DNSRecordClass.CLASS_IN, unique, ttl, this.getName()); } if ((this.getInetAddress() instanceof Inet6Address) && (((Inet6Address) this.getInetAddress()).isIPv4CompatibleAddress())) { byte[] rawAddress = this.getInetAddress().getAddress(); String address = (rawAddress[12] & 0xff) + "." + (rawAddress[13] & 0xff) + "." + (rawAddress[14] & 0xff) + "." + (rawAddress[15] & 0xff); return new DNSRecord.Pointer(address + ".in-addr.arpa.", DNSRecordClass.CLASS_IN, unique, ttl, this.getName()); } return null; }
@Test public void testTableInputFormatBaseReverseDNSForIPv6() throws UnknownHostException { String address = "ipv6.google.com"; String localhost = null; InetAddress addr = null; TableInputFormat inputFormat = new TableInputFormat(); try { localhost = InetAddress.getByName(address).getCanonicalHostName(); addr = Inet6Address.getByName(address); } catch (UnknownHostException e) { // google.com is down, we can probably forgive this test. return; } System.out.println("Should retrun the hostname for this host " + localhost + " addr : " + addr); String actualHostName = inputFormat.reverseDNS(addr); assertEquals("Should retrun the hostname for this host. Expected : " + localhost + " Actual : " + actualHostName, localhost, actualHostName); }
for (String ips : ipset) { try { InetAddress addr = InetAddress.getByName(ips); if (ips.contains(":") && (addr instanceof Inet6Address)) { Inet6Address v6addr = (Inet6Address) addr; if (_log.shouldWarn()) { for (Inet6Address addr : tempV6Addresses) { _log.warn("Not binding to temporary address " + addr.getHostAddress()); byte[] ip = ia.getAddress(); t.externalAddressReceived(SOURCE_INTERFACE, ip, 0);
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); } }
@Test // Everything works fine, everyone gets through. public void testNewDNSServer() throws Exception { String format = "192.168.0.%d"; byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; InetAddress i6addr = Inet6Address.getByName("::1"); InetAddress i4addr = Inet4Address.getByName("127.0.0.1"); thread.newDNSServer(builder, format, ipv6Template, i4addr); assertTrue(thread.upstreamDnsServers.contains(i4addr)); assertTrue(serversAdded.contains(InetAddress.getByName("192.168.0.2"))); thread.newDNSServer(builder, format, ipv6Template, i6addr); assertTrue(thread.upstreamDnsServers.contains(i6addr)); assertEquals(3, ipv6Template[ipv6Template.length - 1]); assertTrue(serversAdded.contains(InetAddress.getByAddress(ipv6Template))); }
@Test // IPv6 is disabled: We only get IPv4 servers through public void testNewDNSServer_ipv6disabled() throws Exception { byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; InetAddress i6addr = Inet6Address.getByName("::1"); thread.newDNSServer(builder, "192.168.0.%d", null, i6addr); assertTrue(serversAdded.isEmpty()); assertTrue(thread.upstreamDnsServers.isEmpty()); InetAddress i4addr = Inet4Address.getByName("127.0.0.1"); thread.newDNSServer(builder, "192.168.0.%d", null, i4addr); assertTrue(serversAdded.contains(InetAddress.getByName("192.168.0.2"))); assertTrue(thread.upstreamDnsServers.contains(i4addr)); }
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); }
byte[] bytes = ip.getAddress(); boolean leadingBytesOfZero = true; for (int i = 0; i < 15; ++i) { if (hasEmbeddedIPv4ClientAddress(ip6)) { addressAsLong = getEmbeddedIPv4ClientAddress(ip6).hashCode(); } else { addressAsLong = ByteBuffer.wrap(ip6.getAddress(), 0, 8).getLong(); return getInet4Address(Ints.toByteArray(coercedHash));
/** * checks if the provided address is a global-scope ipv6 unicast address */ public static boolean isGlobalAddressV6(InetAddress addr) { return addr instanceof Inet6Address && !addr.isAnyLocalAddress() && !addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && !addr.isMulticastAddress() && !addr.isSiteLocalAddress() && !((Inet6Address)addr).isIPv4CompatibleAddress(); }
throw Assert.unreachableCode(); final byte[] bytes = networkAddress.getAddress(); maskBits0(bytes, netmaskBits); String name = Inet.toOptimalString(bytes); try { if (bytes.length == 4) { return new CidrAddress(InetAddress.getByAddress(name, bytes), netmaskBits); } else { return new CidrAddress(Inet6Address.getByAddress(name, bytes, scopeId), netmaskBits);