Search in sources :

Example 96 with Inet6Address

use of java.net.Inet6Address in project netty by netty.

the class DnsNameResolver method resolveAll.

private Future<List<DnsRecord>> resolveAll(DnsQuestion question, DnsRecord[] additionals, Promise<List<DnsRecord>> promise) {
    checkNotNull(question, "question");
    checkNotNull(promise, "promise");
    // Respect /etc/hosts as well if the record type is A or AAAA.
    final DnsRecordType type = question.type();
    final String hostname = question.name();
    if (type == DnsRecordType.A || type == DnsRecordType.AAAA) {
        final List<InetAddress> hostsFileEntries = resolveHostsFileEntries(hostname);
        if (hostsFileEntries != null) {
            List<DnsRecord> result = new ArrayList<DnsRecord>();
            for (InetAddress hostsFileEntry : hostsFileEntries) {
                ByteBuf content = null;
                if (hostsFileEntry instanceof Inet4Address) {
                    if (type == DnsRecordType.A) {
                        content = Unpooled.wrappedBuffer(hostsFileEntry.getAddress());
                    }
                } else if (hostsFileEntry instanceof Inet6Address) {
                    if (type == DnsRecordType.AAAA) {
                        content = Unpooled.wrappedBuffer(hostsFileEntry.getAddress());
                    }
                }
                if (content != null) {
                    // Our current implementation does not support reloading the hosts file,
                    // so use a fairly large TTL (1 day, i.e. 86400 seconds).
                    result.add(new DefaultDnsRawRecord(hostname, type, 86400, content));
                }
            }
            if (!result.isEmpty()) {
                trySuccess(promise, result);
                return promise;
            }
        }
    }
    // It was not A/AAAA question or there was no entry in /etc/hosts.
    final DnsServerAddressStream nameServerAddrs = dnsServerAddressStreamProvider.nameServerAddressStream(hostname);
    new DnsRecordResolveContext(this, promise, question, additionals, nameServerAddrs, maxQueriesPerResolve).resolve(promise);
    return promise;
}
Also used : Inet4Address(java.net.Inet4Address) ArrayList(java.util.ArrayList) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) Inet6Address(java.net.Inet6Address) ByteBuf(io.netty.buffer.ByteBuf) DefaultDnsRawRecord(io.netty.handler.codec.dns.DefaultDnsRawRecord) DnsRecord(io.netty.handler.codec.dns.DnsRecord) InetAddress(java.net.InetAddress)

Example 97 with Inet6Address

use of java.net.Inet6Address in project netty by netty.

the class NetUtilTest method testIpv4MappedIp6GetByName.

@Test
public void testIpv4MappedIp6GetByName() {
    for (Entry<String, String> testEntry : ipv4MappedToIPv6AddressStrings.entrySet()) {
        String srcIp = testEntry.getKey();
        String dstIp = testEntry.getValue();
        Inet6Address inet6Address = getByName(srcIp, true);
        assertNotNull(inet6Address, srcIp + ", " + dstIp);
        assertEquals(dstIp, toAddressString(inet6Address, true), srcIp);
    }
}
Also used : Inet6Address(java.net.Inet6Address) Test(org.junit.jupiter.api.Test)

Example 98 with Inet6Address

use of java.net.Inet6Address in project netty by netty.

the class BsdSocket method connectx.

/**
 * Establish a connection to the given destination address, and send the given data to it.
 *
 * <strong>Note:</strong> This method relies on the {@code connectx(2)} system call, which is MacOS specific.
 *
 * @param source      the source address we are connecting from.
 * @param destination the destination address we are connecting to.
 * @param data        the data to copy to the kernel-side socket buffer.
 * @param tcpFastOpen if {@code true}, set the flags needed to enable TCP FastOpen connecting.
 * @return The number of bytes copied to the kernel-side socket buffer, or the number of bytes sent to the
 * destination. This number is <em>negative</em> if connecting is left in an in-progress state,
 * or <em>positive</em> if the connection was immediately established.
 * @throws IOException if an IO error occurs, if the {@code data} is too big to send in one go,
 * or if the system call is not supported on your platform.
 */
int connectx(InetSocketAddress source, InetSocketAddress destination, IovArray data, boolean tcpFastOpen) throws IOException {
    checkNotNull(destination, "Destination InetSocketAddress cannot be null.");
    int flags = tcpFastOpen ? CONNECT_TCP_FASTOPEN : 0;
    boolean sourceIPv6;
    byte[] sourceAddress;
    int sourceScopeId;
    int sourcePort;
    if (source == null) {
        sourceIPv6 = false;
        sourceAddress = null;
        sourceScopeId = 0;
        sourcePort = 0;
    } else {
        InetAddress sourceInetAddress = source.getAddress();
        sourceIPv6 = useIpv6(this, sourceInetAddress);
        if (sourceInetAddress instanceof Inet6Address) {
            sourceAddress = sourceInetAddress.getAddress();
            sourceScopeId = ((Inet6Address) sourceInetAddress).getScopeId();
        } else {
            // convert to ipv4 mapped ipv6 address;
            sourceScopeId = 0;
            sourceAddress = ipv4MappedIpv6Address(sourceInetAddress.getAddress());
        }
        sourcePort = source.getPort();
    }
    InetAddress destinationInetAddress = destination.getAddress();
    boolean destinationIPv6 = useIpv6(this, destinationInetAddress);
    byte[] destinationAddress;
    int destinationScopeId;
    if (destinationInetAddress instanceof Inet6Address) {
        destinationAddress = destinationInetAddress.getAddress();
        destinationScopeId = ((Inet6Address) destinationInetAddress).getScopeId();
    } else {
        // convert to ipv4 mapped ipv6 address;
        destinationScopeId = 0;
        destinationAddress = ipv4MappedIpv6Address(destinationInetAddress.getAddress());
    }
    int destinationPort = destination.getPort();
    long iovAddress;
    int iovCount;
    int iovDataLength;
    if (data == null || data.count() == 0) {
        iovAddress = 0;
        iovCount = 0;
        iovDataLength = 0;
    } else {
        iovAddress = data.memoryAddress(0);
        iovCount = data.count();
        long size = data.size();
        if (size > Integer.MAX_VALUE) {
            throw new IOException("IovArray.size() too big: " + size + " bytes.");
        }
        iovDataLength = (int) size;
    }
    int result = connectx(intValue(), UNSPECIFIED_SOURCE_INTERFACE, sourceIPv6, sourceAddress, sourceScopeId, sourcePort, destinationIPv6, destinationAddress, destinationScopeId, destinationPort, flags, iovAddress, iovCount, iovDataLength);
    if (result == ERRNO_EINPROGRESS_NEGATIVE) {
        // Tell the channel the data was written, so the outbound buffer can update its position.
        return -iovDataLength;
    }
    if (result < 0) {
        return ioResult("connectx", result);
    }
    return result;
}
Also used : Inet6Address(java.net.Inet6Address) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 99 with Inet6Address

use of java.net.Inet6Address in project netty by netty.

the class LinuxSocket method joinGroup.

void joinGroup(InetAddress group, NetworkInterface netInterface, InetAddress source) throws IOException {
    final NativeInetAddress g = NativeInetAddress.newInstance(group);
    final boolean isIpv6 = group instanceof Inet6Address;
    final NativeInetAddress i = NativeInetAddress.newInstance(deriveInetAddress(netInterface, isIpv6));
    if (source != null) {
        if (source.getClass() != group.getClass()) {
            throw new IllegalArgumentException("Source address is different type to group");
        }
        final NativeInetAddress s = NativeInetAddress.newInstance(source);
        joinSsmGroup(intValue(), ipv6 && isIpv6, g.address(), i.address(), g.scopeId(), interfaceIndex(netInterface), s.address());
    } else {
        joinGroup(intValue(), ipv6 && isIpv6, g.address(), i.address(), g.scopeId(), interfaceIndex(netInterface));
    }
}
Also used : NativeInetAddress(io.netty.channel.unix.NativeInetAddress) Inet6Address(java.net.Inet6Address)

Example 100 with Inet6Address

use of java.net.Inet6Address in project netty by netty.

the class NativeTest method testAddressIpv6.

@Test
public void testAddressIpv6() throws Exception {
    Inet6Address address = NetUtil.LOCALHOST6;
    InetSocketAddress inetAddress = new InetSocketAddress(address, 9999);
    byte[] bytes = new byte[24];
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.put(address.getAddress());
    buffer.putInt(address.getScopeId());
    buffer.putInt(inetAddress.getPort());
    assertEquals(inetAddress, address(buffer.array(), 0, bytes.length));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

Inet6Address (java.net.Inet6Address)286 InetAddress (java.net.InetAddress)196 Inet4Address (java.net.Inet4Address)110 NetworkInterface (java.net.NetworkInterface)54 UnknownHostException (java.net.UnknownHostException)51 SocketException (java.net.SocketException)32 InetSocketAddress (java.net.InetSocketAddress)31 IOException (java.io.IOException)29 LinkAddress (android.net.LinkAddress)28 Test (org.junit.Test)26 RouteInfo (android.net.RouteInfo)21 IpPrefix (android.net.IpPrefix)19 ArrayList (java.util.ArrayList)19 LinkProperties (android.net.LinkProperties)15 ByteBuffer (java.nio.ByteBuffer)9 InterfaceAddress (java.net.InterfaceAddress)7 StringJoiner (java.util.StringJoiner)7 PrintWriter (java.io.PrintWriter)6 HashMap (java.util.HashMap)6 Test (org.junit.jupiter.api.Test)6