package p138.p329.p362.p365;

import com.hierynomus.mssmb2.SMBApiException;
import com.hierynomus.protocol.commons.buffer.Buffer;
import com.hierynomus.protocol.transport.TransportException;
import com.hierynomus.smbj.common.SMBRuntimeException;
import com.hierynomus.spnego.SpnegoException;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import p138.p176.p177.p242.p246.p251.C4303;
import p138.p329.p330.C6077;
import p138.p329.p330.p331.p333.C6083;
import p138.p329.p330.p334.AbstractC6087;
import p138.p329.p330.p334.AbstractC6088;
import p138.p329.p330.p334.EnumC6100;
import p138.p329.p330.p334.p335.C6101;
import p138.p329.p330.p334.p335.C6109;
import p138.p329.p330.p334.p336.C6128;
import p138.p329.p340.C6157;
import p138.p329.p341.EnumC6158;
import p138.p329.p344.C6217;
import p138.p329.p344.p345.C6218;
import p138.p329.p346.C6231;
import p138.p329.p346.C6234;
import p138.p329.p346.C6237;
import p138.p329.p346.C6239;
import p138.p329.p346.EnumC6222;
import p138.p329.p346.EnumC6230;
import p138.p329.p346.p347.C6242;
import p138.p329.p346.p347.C6253;
import p138.p329.p346.p347.C6254;
import p138.p329.p346.p347.C6263;
import p138.p329.p351.InterfaceC6286;
import p138.p329.p351.p352.InterfaceC6290;
import p138.p329.p351.p352.p353.AbstractC6294;
import p138.p329.p351.p352.p353.C6293;
import p138.p329.p351.p352.p354.C6299;
import p138.p329.p351.p352.p354.C6302;
import p138.p329.p351.p352.p354.C6303;
import p138.p329.p351.p352.p354.InterfaceC6301;
import p138.p329.p351.p356.C6306;
import p138.p329.p351.p356.InterfaceC6305;
import p138.p329.p351.p356.InterfaceC6307;
import p138.p329.p361.AbstractC6334;
import p138.p329.p361.AbstractC6335;
import p138.p329.p362.C6339;
import p138.p329.p362.C6340;
import p138.p329.p362.p363.C6342;
import p138.p329.p362.p363.C6343;
import p138.p329.p362.p363.InterfaceC6344;
import p138.p329.p362.p364.C6352;
import p138.p329.p362.p366.C6364;
import p138.p329.p362.p366.C6365;
import p138.p329.p362.p369.C6386;
import p138.p329.p362.p371.AbstractRunnableC6414;
import p138.p329.p362.p371.p372.p373.C6415;
import p138.p329.p362.p371.p372.p373.C6416;
import p138.p329.p362.p371.p372.p373.C6417;
import p138.p329.p374.C6418;
import p138.p329.p374.C6421;
import p421.p422.p423.p424.C6713;
import p463.p481.C7162;
import p463.p481.InterfaceC7161;

/* JADX INFO: renamed from: ދ.ށ.މ.ބ.֏, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C6354 extends C6352<C6354> implements Closeable, InterfaceC6307<AbstractC6335<?>> {

    /* JADX INFO: renamed from: ޑ, reason: contains not printable characters */
    public static final InterfaceC7161 f17737 = C7162.m10575((Class<?>) C6354.class);

    /* JADX INFO: renamed from: ޒ, reason: contains not printable characters */
    public static final C6356 f17738 = new C6356(new C6239(), new C6217());

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public C6357 f17739;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public C6362 f17743;

    /* JADX INFO: renamed from: ފ, reason: contains not printable characters */
    public String f17745;

    /* JADX INFO: renamed from: ދ, reason: contains not printable characters */
    public C6339 f17746;

    /* JADX INFO: renamed from: ތ, reason: contains not printable characters */
    public C6340 f17747;

    /* JADX INFO: renamed from: ލ, reason: contains not printable characters */
    public C6416<AbstractC6334<?, ?>> f17748;

    /* JADX INFO: renamed from: ގ, reason: contains not printable characters */
    public final C6365 f17749;

    /* JADX INFO: renamed from: ސ, reason: contains not printable characters */
    public int f17751;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public C6363 f17740 = new C6363();

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public C6363 f17741 = new C6363();

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public C6359 f17742 = new C6359();

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public C6234 f17744 = new C6234();

    /* JADX INFO: renamed from: ޏ, reason: contains not printable characters */
    public final ReentrantLock f17750 = new ReentrantLock();

    /* JADX INFO: renamed from: ދ.ށ.މ.ބ.֏$֏, reason: contains not printable characters */
    public class C6355 implements C6299.InterfaceC6300 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public C6360 f17752;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public long f17753;

        public C6355(C6360 c6360, long j) {
            this.f17752 = c6360;
            this.f17753 = j;
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public void m9580() {
            EnumC6222 enumC6222 = C6354.this.f17739.f17759.f17764;
            C6360 c6360 = this.f17752;
            C6242 c6242 = new C6242(enumC6222, c6360.f17773, c6360.f17776);
            try {
                C6354.this.f17740.m9584(Long.valueOf(this.f17753)).m9606(c6242);
            } catch (TransportException unused) {
                C6354.f17737.mo10555("Failed to send {}", c6242);
            }
        }
    }

    /* JADX INFO: renamed from: ދ.ށ.މ.ބ.֏$ؠ, reason: contains not printable characters */
    public static class C6356 implements InterfaceC6305<AbstractC6335<?>> {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public InterfaceC6305<?>[] f17755;

        public C6356(InterfaceC6305<?>... interfaceC6305Arr) {
            this.f17755 = interfaceC6305Arr;
        }

        @Override // p138.p329.p351.p356.InterfaceC6305
        /* JADX INFO: renamed from: ֏ */
        public InterfaceC6286 mo9495(byte[] bArr) throws IOException {
            for (InterfaceC6305<?> interfaceC6305 : this.f17755) {
                if (interfaceC6305.mo9496(bArr)) {
                    return (AbstractC6335) interfaceC6305.mo9495(bArr);
                }
            }
            throw new IOException("Unknown packet format received.");
        }

        @Override // p138.p329.p351.p356.InterfaceC6305
        /* JADX INFO: renamed from: ؠ */
        public boolean mo9496(byte[] bArr) {
            for (InterfaceC6305<?> interfaceC6305 : this.f17755) {
                if (interfaceC6305.mo9496(bArr)) {
                    return true;
                }
            }
            return false;
        }
    }

    public C6354(C6340 c6340, C6339 c6339, C6365 c6365) {
        this.f17747 = c6340;
        this.f17746 = c6339;
        C6417<AbstractC6335<?>, AbstractC6334<?, ?>> c6417 = c6340.f17705;
        C6306 c6306 = new C6306(new C6361(), this, f17738);
        if (c6417 == null) {
            throw null;
        }
        this.f17748 = new C6416<>(c6340.f17693, c6340.f17709, c6306);
        this.f17749 = c6365;
        c6365.m9587(this);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        if (this.f17733.decrementAndGet() <= 0) {
            try {
                C6363 c6363 = this.f17740;
                c6363.f17779.lock();
                try {
                    ArrayList<C6386> arrayList = new ArrayList(c6363.f17780.values());
                    c6363.f17779.unlock();
                    for (C6386 c6386 : arrayList) {
                        try {
                            c6386.m9608();
                        } catch (IOException e) {
                            f17737.mo10571("Exception while closing session {}", Long.valueOf(c6386.f17825), e);
                        }
                    }
                } catch (Throwable th) {
                    c6363.f17779.unlock();
                    throw th;
                }
            } finally {
                this.f17748.m9638();
                f17737.mo10561("Closed connection to {}", this.f17745);
                ((C6713) this.f17749.f17784).m9911(new C6364(this.f17745, this.f17751));
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:10:0x003d  */
    /* JADX WARN: Removed duplicated region for block: B:18:0x0052 A[Catch: all -> 0x00d7, TryCatch #0 {all -> 0x00d7, blocks: (B:3:0x0005, B:5:0x000e, B:7:0x0027, B:9:0x0033, B:16:0x0048, B:18:0x0052, B:19:0x0061, B:21:0x00c1, B:22:0x00cb, B:14:0x0044, B:23:0x00cc), top: B:29:0x0005 }] */
    /* JADX WARN: Removed duplicated region for block: B:21:0x00c1 A[Catch: all -> 0x00d7, TryCatch #0 {all -> 0x00d7, blocks: (B:3:0x0005, B:5:0x000e, B:7:0x0027, B:9:0x0033, B:16:0x0048, B:18:0x0052, B:19:0x0061, B:21:0x00c1, B:22:0x00cb, B:14:0x0044, B:23:0x00cc), top: B:29:0x0005 }] */
    /* JADX WARN: Removed duplicated region for block: B:22:0x00cb A[Catch: all -> 0x00d7, TryCatch #0 {all -> 0x00d7, blocks: (B:3:0x0005, B:5:0x000e, B:7:0x0027, B:9:0x0033, B:16:0x0048, B:18:0x0052, B:19:0x0061, B:21:0x00c1, B:22:0x00cb, B:14:0x0044, B:23:0x00cc), top: B:29:0x0005 }] */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public <T extends C6237> Future<T> m9573(C6237 c6237) {
        C6302<C6237, SMBRuntimeException> c6302;
        this.f17750.lock();
        try {
            C6299 c6299 = null;
            if (!(c6237.mo9510() instanceof C6242)) {
                int iAvailablePermits = this.f17743.f17778.availablePermits();
                int iAbs = Math.abs((c6237.mo9504() - 1) / 65536) + 1;
                if (iAbs > 1) {
                    if (this.f17739.f17762.contains(EnumC6230.SMB2_GLOBAL_CAP_LARGE_MTU)) {
                        if (iAbs >= iAvailablePermits) {
                            if (iAbs > 1 && iAvailablePermits > 1) {
                                iAbs = iAvailablePermits - 1;
                            }
                        }
                        c6237.mo9556().f17386 = iAbs;
                        if (iAvailablePermits == 0) {
                            f17737.mo10566("There are no credits left to send {}, will block until there are more credits available.", c6237.mo9556().f17389);
                        }
                        long[] jArrM9583 = this.f17743.m9583(iAbs);
                        c6237.mo9556().f17390 = jArrM9583[0];
                        f17737.mo10558("Granted {} (out of {}) credits to {}", Integer.valueOf(iAbs), Integer.valueOf(iAvailablePermits), c6237);
                        c6237.mo9556().f17387 = Math.max((512 - iAvailablePermits) - iAbs, iAbs);
                        C6360 c6360 = new C6360(c6237.mo9510(), jArrM9583[0], UUID.randomUUID());
                        this.f17742.m9582(c6360);
                        C6355 c6355 = new C6355(c6360, c6237.mo9556().f17392);
                        c6302 = c6360.f17771;
                        if (c6302 != null) {
                            throw null;
                        }
                        c6299 = new C6299(new C6303(c6302), c6355);
                    } else {
                        f17737.mo10570("Connection to {} does not support multi-credit requests.", this.f17745);
                    }
                    iAbs = 1;
                    c6237.mo9556().f17386 = iAbs;
                    if (iAvailablePermits == 0) {
                    }
                    long[] jArrM95832 = this.f17743.m9583(iAbs);
                    c6237.mo9556().f17390 = jArrM95832[0];
                    f17737.mo10558("Granted {} (out of {}) credits to {}", Integer.valueOf(iAbs), Integer.valueOf(iAvailablePermits), c6237);
                    c6237.mo9556().f17387 = Math.max((512 - iAvailablePermits) - iAbs, iAbs);
                    C6360 c63602 = new C6360(c6237.mo9510(), jArrM95832[0], UUID.randomUUID());
                    this.f17742.m9582(c63602);
                    C6355 c63552 = new C6355(c63602, c6237.mo9556().f17392);
                    c6302 = c63602.f17771;
                    if (c6302 != null) {
                    }
                }
            }
            this.f17748.m9640(c6237);
            return c6299;
        } finally {
            this.f17750.unlock();
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C6237 m9574() {
        return (C6237) C4303.m7011(m9573(new C6253(this.f17747.m9560(), this.f17739.f17760, this.f17747.f17696)), this.f17747.f17706, TimeUnit.MILLISECONDS, TransportException.f2172);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C6263 m9575(byte[] bArr, long j) {
        C6263 c6263 = new C6263(this.f17739.f17759.f17764, EnumSet.of(C6263.EnumC6264.SMB2_NEGOTIATE_SIGNING_ENABLED), this.f17739.f17761);
        c6263.f17515 = bArr;
        ((C6231) c6263.f17677).f17392 = j;
        return (C6263) ((C6237) C4303.m7011(m9573(c6263), this.f17747.f17706, TimeUnit.MILLISECONDS, TransportException.f2172));
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public C6386 m9576(C6343 c6343) {
        try {
            InterfaceC6344 interfaceC6344M9579 = m9579(c6343);
            interfaceC6344M9579.mo9562(this.f17747);
            C6386 c6386 = new C6386(this, c6343, this.f17749, this.f17746.f17686, this.f17747.f17699);
            C6263 c6263M9575 = m9575(m9578(interfaceC6344M9579, c6343, this.f17739.m9581(), c6386), 0L);
            long j = ((C6231) c6263M9575.f17677).f17392;
            if (j != 0) {
                this.f17741.m9585(Long.valueOf(j), c6386);
            }
            while (((C6231) c6263M9575.f17677).f17394 == EnumC6158.STATUS_MORE_PROCESSING_REQUIRED.f17193) {
                try {
                    f17737.mo10556("More processing required for authentication of {} using {}", c6343.f17714, interfaceC6344M9579);
                    c6263M9575 = m9575(m9578(interfaceC6344M9579, c6343, c6263M9575.f17515, c6386), j);
                } catch (Throwable th) {
                    if (j != 0) {
                        this.f17741.m9586(Long.valueOf(j));
                    }
                    throw th;
                }
            }
            if (((C6231) c6263M9575.f17677).f17394 != EnumC6158.STATUS_SUCCESS.f17193) {
                throw new SMBApiException((C6231) c6263M9575.f17677, String.format("Authentication failed for '%s' using %s", c6343.f17714, interfaceC6344M9579));
            }
            c6386.f17825 = ((C6231) c6263M9575.f17677).f17392;
            if (c6263M9575.f17515 != null) {
                m9578(interfaceC6344M9579, c6343, c6263M9575.f17515, c6386);
            }
            c6386.m9609(c6263M9575);
            f17737.mo10569("Successfully authenticated {} on {}, session is {}", c6343.f17714, this.f17745, Long.valueOf(c6386.f17825));
            this.f17740.m9585(Long.valueOf(c6386.f17825), c6386);
            if (j != 0) {
                this.f17741.m9586(Long.valueOf(j));
            }
            return c6386;
        } catch (SpnegoException | IOException e) {
            throw new SMBRuntimeException(e);
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public void m9577(String str, int i) throws Throwable {
        Object objM9574;
        if (this.f17748.m9641()) {
            throw new IllegalStateException(String.format("This connection is already connected to %s", this.f17745));
        }
        this.f17745 = str;
        this.f17751 = i;
        C6416<AbstractC6334<?, ?>> c6416 = this.f17748;
        InetSocketAddress inetSocketAddress = new InetSocketAddress(str, i);
        if (c6416 == null) {
            throw null;
        }
        String hostString = inetSocketAddress.getHostString();
        Socket socketCreateSocket = c6416.f17913.createSocket(hostString, inetSocketAddress.getPort());
        c6416.f17915 = socketCreateSocket;
        socketCreateSocket.setSoTimeout(c6416.f17914);
        c6416.f17916 = new BufferedOutputStream(c6416.f17915.getOutputStream(), 9000);
        InputStream inputStream = c6416.f17915.getInputStream();
        C6306<D, P> c6306 = c6416.f17911;
        C6415 c6415 = new C6415(hostString, inputStream, c6306.f17652, c6306.f17651);
        c6416.f17917 = c6415;
        AbstractRunnableC6414.f17904.mo10573("Starting PacketReader on thread: {}", c6415.f17908.getName());
        c6415.f17908.start();
        this.f17743 = new C6362();
        this.f17739 = new C6357(this.f17747.f17695, str);
        f17737.mo10556("Negotiating dialects {} with server {}", this.f17747.m9560(), this.f17745);
        C6340 c6340 = this.f17747;
        if (c6340.f17698) {
            C6218 c6218 = new C6218(c6340.m9560());
            long j = this.f17743.m9583(1)[0];
            if (j != 0) {
                throw new IllegalStateException("The SMBv1 SMB_COM_NEGOTIATE packet needs to be the first packet sent.");
            }
            C6360 c6360 = new C6360(c6218, j, UUID.randomUUID());
            this.f17742.m9582(c6360);
            this.f17748.m9640(c6218);
            C6302<C6237, SMBRuntimeException> c6302 = c6360.f17771;
            if (c6302 == null) {
                throw null;
            }
            new AtomicBoolean(false);
            new ReentrantReadWriteLock();
            long j2 = this.f17747.f17706;
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            InterfaceC6301<TransportException> interfaceC6301 = TransportException.f2172;
            try {
                C6237 c6237M9539 = c6302.m9539(j2, timeUnit);
                if (c6237M9539 == null) {
                    throw c6302.f17641.mo1479(new TimeoutException("Timeout expired"));
                }
                C6237 c6237 = c6237M9539;
                if (!(c6237 instanceof C6254)) {
                    throw new IllegalStateException("Expected a SMB2 NEGOTIATE Response to our SMB_COM_NEGOTIATE, but got: " + c6237);
                }
                C6254 c6254 = (C6254) c6237;
                EnumC6222 enumC6222 = c6254.f17476;
                objM9574 = c6254;
                if (enumC6222 == EnumC6222.SMB_2XX) {
                    objM9574 = m9574();
                }
            } catch (Throwable th) {
                try {
                    throw new ExecutionException(th);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw ((TransportException.C0347) interfaceC6301).mo1479(e);
                } catch (ExecutionException e2) {
                    e = e2;
                    throw ((TransportException.C0347) interfaceC6301).mo1479(e);
                } catch (TimeoutException e3) {
                    e = e3;
                    throw ((TransportException.C0347) interfaceC6301).mo1479(e);
                }
            }
        } else {
            objM9574 = m9574();
        }
        if (!(objM9574 instanceof C6254)) {
            throw new IllegalStateException("Expected a SMB2 NEGOTIATE Response, but got: " + objM9574);
        }
        C6254 c62542 = (C6254) objM9574;
        if (!EnumC6158.m9480(((C6231) c62542.f17677).f17394)) {
            throw new SMBApiException((C6231) c62542.f17677, "Failure during dialect negotiation");
        }
        C6357 c6357 = this.f17739;
        if (c6357 == null) {
            throw null;
        }
        c6357.f17757 = c62542.f17477;
        EnumSet<EnumC6230> enumSetM7041 = C4303.m7041(c62542.f17478, EnumC6230.class);
        c6357.f17762 = enumSetM7041;
        c6357.f17759 = new C6358(c62542.f17476, c62542.f17479, c62542.f17480, c62542.f17481, enumSetM7041.contains(EnumC6230.SMB2_GLOBAL_CAP_LARGE_MTU));
        c6357.f17763 = c62542.f17475;
        System.currentTimeMillis();
        C6157 c6157 = c62542.f17482;
        if (c6157 == null) {
            throw null;
        }
        TimeUnit.MILLISECONDS.convert((c6157.f17123 - 116444736000000000L) * 100, TimeUnit.NANOSECONDS);
        f17737.mo10573("Negotiated the following connection settings: {}", this.f17739);
        f17737.mo10561("Successfully connected to: {}", this.f17745);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final byte[] m9578(InterfaceC6344 interfaceC6344, C6343 c6343, byte[] bArr, C6386 c6386) {
        C6342 c6342Mo9561 = interfaceC6344.mo9561(c6343, bArr, c6386);
        if (c6342Mo9561 == null) {
            return null;
        }
        if (this.f17739 == null) {
            throw null;
        }
        byte[] bArr2 = c6342Mo9561.f17710;
        byte[] bArr3 = c6342Mo9561.f17711;
        if (bArr3 != null) {
            c6386.f17826.m9605(bArr3);
        }
        return bArr2;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final InterfaceC6344 m9579(C6343 c6343) throws SpnegoException {
        C6340 c6340 = this.f17747;
        if (c6340 == null) {
            throw null;
        }
        ArrayList arrayList = new ArrayList(new ArrayList(c6340.f17692));
        List arrayList2 = new ArrayList();
        if (this.f17739.m9581().length > 0) {
            C6418 c6418 = new C6418();
            try {
                C6077 c6077 = new C6077(new C6083(), new C6293(new Buffer.C0346(this.f17739.m9581(), AbstractC6294.f17633)));
                try {
                    C6109 c6109 = (C6109) c6077.m9449();
                    if (c6109.f17009.f17020 != EnumC6100.APPLICATION) {
                        throw new SpnegoException("Incorrect GSS-API ASN.1 token received, expected to find an [APPLICATION 0], not: " + c6109);
                    }
                    C6101 c6101 = (C6101) c6109.m9465(AbstractC6088.f17019);
                    AbstractC6087 abstractC6087 = c6101.f17031.get(0);
                    if (!(abstractC6087 instanceof C6128)) {
                        throw new SpnegoException("Expected to find the SPNEGO OID (" + C6421.f17924 + "), not: " + abstractC6087);
                    }
                    c6418.m9648(c6101.f17031.get(1));
                    c6077.close();
                    arrayList2 = c6418.f17918;
                } finally {
                }
            } catch (IOException e) {
                throw new SpnegoException("Could not read NegTokenInit from buffer", e);
            }
        }
        for (InterfaceC6290.InterfaceC6291 interfaceC6291 : new ArrayList(arrayList)) {
            if (arrayList2.isEmpty() || arrayList2.contains(new C6128(interfaceC6291.mo9524()))) {
                InterfaceC6344 interfaceC6344 = (InterfaceC6344) interfaceC6291.mo9523();
                if (interfaceC6344.mo9563(c6343)) {
                    return interfaceC6344;
                }
            }
        }
        throw new SMBRuntimeException("Could not find a configured authenticator for mechtypes: " + arrayList2 + " and authentication context: " + c6343);
    }
}
