// 
// Decompiled by Procyon v0.6.0
// 

package io.netty.handler.codec.quic;

import io.netty.util.internal.PlatformDependent;
import org.jetbrains.annotations.Nullable;
import java.net.UnknownHostException;
import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

final class SockaddrIn
{
    static final byte[] IPV4_MAPPED_IPV6_PREFIX;
    static final int IPV4_ADDRESS_LENGTH = 4;
    static final int IPV6_ADDRESS_LENGTH = 16;
    static final byte[] SOCKADDR_IN6_EMPTY_ARRAY;
    static final byte[] SOCKADDR_IN_EMPTY_ARRAY;
    
    private SockaddrIn() {
    }
    
    static int cmp(final long memory, final long memory2) {
        return Quiche.sockaddr_cmp(memory, memory2);
    }
    
    static int setAddress(final ByteBuffer memory, final InetSocketAddress address) {
        final InetAddress addr = address.getAddress();
        return setAddress(addr instanceof Inet6Address, memory, address);
    }
    
    static int setAddress(final boolean ipv6, final ByteBuffer memory, final InetSocketAddress address) {
        if (ipv6) {
            return setIPv6(memory, address.getAddress(), address.getPort());
        }
        return setIPv4(memory, address.getAddress(), address.getPort());
    }
    
    static int setIPv4(final ByteBuffer memory, final InetAddress address, final int port) {
        final int position = memory.position();
        try {
            memory.put(SockaddrIn.SOCKADDR_IN_EMPTY_ARRAY);
            memory.putShort(position + Quiche.SOCKADDR_IN_OFFSETOF_SIN_FAMILY, Quiche.AF_INET);
            memory.putShort(position + Quiche.SOCKADDR_IN_OFFSETOF_SIN_PORT, (short)port);
            final byte[] bytes = address.getAddress();
            int offset = 0;
            if (bytes.length == 16) {
                offset = SockaddrIn.IPV4_MAPPED_IPV6_PREFIX.length;
            }
            assert bytes.length == offset + 4;
            memory.position();
            memory.put(bytes, offset, 4);
            return Quiche.SIZEOF_SOCKADDR_IN;
        }
        finally {
            memory.position();
        }
    }
    
    static int setIPv6(final ByteBuffer memory, final InetAddress address, final int port) {
        final int position = memory.position();
        try {
            memory.put(SockaddrIn.SOCKADDR_IN6_EMPTY_ARRAY);
            memory.putShort(position + Quiche.SOCKADDR_IN6_OFFSETOF_SIN6_FAMILY, Quiche.AF_INET6);
            memory.putShort(position + Quiche.SOCKADDR_IN6_OFFSETOF_SIN6_PORT, (short)port);
            final byte[] bytes = address.getAddress();
            final int offset = Quiche.SOCKADDR_IN6_OFFSETOF_SIN6_ADDR + Quiche.IN6_ADDRESS_OFFSETOF_S6_ADDR;
            if (bytes.length == 4) {
                memory.position();
                memory.put(SockaddrIn.IPV4_MAPPED_IPV6_PREFIX);
                memory.position();
                memory.put(bytes, 0, 4);
            }
            else {
                memory.position();
                memory.put(bytes, 0, 16);
                memory.putInt(position + Quiche.SOCKADDR_IN6_OFFSETOF_SIN6_SCOPE_ID, ((Inet6Address)address).getScopeId());
            }
            return Quiche.SIZEOF_SOCKADDR_IN6;
        }
        finally {
            memory.position();
        }
    }
    
    @Nullable
    static InetSocketAddress getIPv4(final ByteBuffer memory, final byte[] tmpArray) {
        assert tmpArray.length == 4;
        final int position = memory.position();
        try {
            final int port = memory.getShort(position + Quiche.SOCKADDR_IN_OFFSETOF_SIN_PORT) & 0xFFFF;
            memory.position();
            memory.get(tmpArray);
            try {
                return new InetSocketAddress(InetAddress.getByAddress(tmpArray), port);
            }
            catch (final UnknownHostException ignore) {
                return null;
            }
        }
        finally {
            memory.position();
        }
    }
    
    @Nullable
    static InetSocketAddress getIPv6(final ByteBuffer memory, final byte[] ipv6Array, final byte[] ipv4Array) {
        assert ipv6Array.length == 16;
        assert ipv4Array.length == 4;
        final int position = memory.position();
        try {
            final int port = memory.getShort(position + Quiche.SOCKADDR_IN6_OFFSETOF_SIN6_PORT) & 0xFFFF;
            memory.position();
            memory.get(ipv6Array);
            if (PlatformDependent.equals(ipv6Array, 0, SockaddrIn.IPV4_MAPPED_IPV6_PREFIX, 0, SockaddrIn.IPV4_MAPPED_IPV6_PREFIX.length)) {
                System.arraycopy(ipv6Array, SockaddrIn.IPV4_MAPPED_IPV6_PREFIX.length, ipv4Array, 0, 4);
                try {
                    return new InetSocketAddress(InetAddress.getByAddress(ipv4Array), port);
                }
                catch (final UnknownHostException ignore) {
                    return null;
                }
            }
            final int scopeId = memory.getInt(position + Quiche.SOCKADDR_IN6_OFFSETOF_SIN6_SCOPE_ID);
            try {
                return new InetSocketAddress(Inet6Address.getByAddress(null, ipv6Array, scopeId), port);
            }
            catch (final UnknownHostException ignore2) {
                return null;
            }
        }
        finally {
            memory.position();
        }
    }
    
    static {
        IPV4_MAPPED_IPV6_PREFIX = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1 };
        SOCKADDR_IN6_EMPTY_ARRAY = new byte[Quiche.SIZEOF_SOCKADDR_IN6];
        SOCKADDR_IN_EMPTY_ARRAY = new byte[Quiche.SIZEOF_SOCKADDR_IN];
    }
}
