// File: /workspace/github_cpp_makefile_star2_O2/renzibei[P]TSProxy/proxyclient.cpp
// Function: socksHandShake(int, int, ClientContext*, std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<int const, int>>>&)
int socksHandShake(int localFd, int serverFd, ClientContext *ctx, LocalFdMap &localFdMap) {
    LogHelper::log(Debug, "client start to socks handshake for localFd: %d", localFd);
    static uint8_t socksBuffer[sizeof(SocksTcpReply)];
    memset(socksBuffer, 0, sizeof(socksBuffer));
    int recvCode = readNBytes(localFd, socksBuffer, 2, constants::clientNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Warn, "Fail to socks handshake in reading header in the first step");
        return recvCode;
    }
    uint8_t socksVersion = socksBuffer[0];
    uint8_t methodsNum = socksBuffer[1];
    if (socksVersion != constants::SocksVersion) {
        LogHelper::log(Warn, "socks version mismatch, version: %d", socksVersion);
        return -1;
    }
    memset(socksBuffer, 0, 2);
    recvCode = readNBytes(localFd, socksBuffer, methodsNum, constants::clientNonBlocking);
    if (recvCode <= 0 || methodsNum != recvCode) {
        LogHelper::log(Warn, "Fail to socks handshake in reading methods");
        return recvCode;
    }
    uint8_t supportMethod = constants::SocksNoSupportMethod;
    for (int i = 0; i < methodsNum; ++i) {
        if (socksBuffer[i] == constants::SocksNoAuthMethod) {
            supportMethod = socksBuffer[i];
            break;
        }
    }
    if (supportMethod == constants::SocksNoSupportMethod) {
        LogHelper::log(Warn, "No supportMethod in socks");
        // return -2;
    }
    memset(socksBuffer, 0, methodsNum);
    socksBuffer[0] = constants::SocksVersion;
    socksBuffer[1] = supportMethod;
    int sendCode = writeNBytes(localFd, socksBuffer, 2, constants::clientNonBlocking);
    if (sendCode < 0) {
        LogHelper::log(Warn, "Failed to send second packet in socks handshake");
        return sendCode;
    }

    memset(socksBuffer, 0, sizeof(socksBuffer));
    recvCode = readNBytes(localFd, socksBuffer, 4, constants::clientNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Warn, "Fail to recv thrid socks handshake header");
        return recvCode;
    }
    socksVersion = socksBuffer[0];
    uint8_t requestCmd = socksBuffer[1];
    uint8_t reserved = socksBuffer[2];
    uint8_t dstAddrType = socksBuffer[3];
    
    memset(socksBuffer, 0, 4);
    size_t totalLen = 1;
    socksBuffer[0] = dstAddrType;
    if (dstAddrType == constants::SocksAddrDomainType) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 1, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv domain addr length in the third handshake recv");
            return recvCode;
        }
        uint8_t domainLen = socksBuffer[1];
        totalLen += 1;
        // socksBuffer[0] = 0;
        recvCode = readNBytes(localFd, socksBuffer + 2, domainLen, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv full domain addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 2 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else if (dstAddrType == constants::SocksAddrIpv4Type) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 4, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv ipv4 addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 1 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else if (dstAddrType == constants::SocksAddrIpv6Type) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 16, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv ipv6 addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 1 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else {
        LogHelper::log(Warn, "Not valid addr type in the third handshake recv, type: %d", dstAddrType);
        return recvCode;
    }

    static uint8_t addrBuffer[constants::SocksDomainMaxLength + 2];
    memset(addrBuffer, 0, sizeof(addrBuffer));
    memcpy(addrBuffer, socksBuffer, totalLen);
    memset(socksBuffer, 0, totalLen);
    socksBuffer[0] = requestCmd;
    memcpy(socksBuffer + 1, addrBuffer, totalLen);
    hton_copy4bytes(socksBuffer + 1 + totalLen, &localFd);
    // memcpy(addrBuffer, socksBuffer, recvCode);
    // memset(socksBuffer, 0, recvCode);
    sendCode = sendMessage(serverFd, socksBuffer, totalLen + 1 + sizeof(localFd), ctx, 6);
    if (sendCode < 0) {
        LogHelper::log(Error, "Fail to send socks handshake third request to server");
        return sendCode;
    }
    localFdMap[localFd] = LocalFdWaiting;
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r13
movq %rdx, %r14
movl %esi, %ebp
movl %edi, %r15d
movl %edi, 0xc(%rsp)
leaq 0x3633(%rip), %rsi      # 0x6448
pushq $0x3
popq %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x26e8
leaq 0x683e7(%rip), %rbx     # 0x6b210
movl $0x106, %edx            # imm = 0x106
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
movq %rbx, %rsi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
js 0x2eba
movzbl 0x683b8(%rip), %edx     # 0x6b210
cmpl $0x5, %edx
jne 0x2ec6
movq %r13, 0x10(%rsp)
leaq 0x683a7(%rip), %rbx     # 0x6b210
movzbl 0x683a0(%rip), %r13d    # 0x6b211
andw $0x0, 0x68397(%rip)     # 0x6b210
pushq $0x1
popq %rcx
movl %r15d, %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
setle %al
cmpl %r12d, %r13d
setne %cl
orb %al, %cl
jne 0x2ee0
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %eax, %eax
cmpq %rax, %r12
je 0x2eec
cmpb $0x0, (%rax,%rbx)
leaq 0x1(%rax), %rax
jne 0x2ea6
xorl %r12d, %r12d
jmp 0x2f00
leaq 0x35b7(%rip), %rsi      # 0x6478
jmp 0x31af
leaq 0x35e7(%rip), %rsi      # 0x64b4
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x26e8
pushq $-0x1
popq %r12
jmp 0x31b9
leaq 0x35f1(%rip), %rsi      # 0x64d8
jmp 0x31af
leaq 0x3610(%rip), %rsi      # 0x6503
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x26e8
movb $-0x1, %r12b
leaq 0x68309(%rip), %rbx     # 0x6b210
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2140
movb $0x5, 0x682f5(%rip)     # 0x6b210
movb %r12b, 0x682ef(%rip)    # 0x6b211
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
movq %rbx, %rsi
callq 0x46b8
movq %rax, %r12
testl %r12d, %r12d
js 0x3028
leaq 0x682ca(%rip), %rbx     # 0x6b210
movl $0x106, %edx            # imm = 0x106
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
pushq $0x4
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
movq %rbx, %rsi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
js 0x3034
movb 0x68298(%rip), %r13b    # 0x6b211
movzbl 0x68293(%rip), %edx     # 0x6b213
andl $0x0, 0x68289(%rip)     # 0x6b210
movb %dl, 0x68283(%rip)      # 0x6b210
cmpl $0x1, %edx
je 0x3069
cmpl $0x4, %edx
je 0x3040
cmpl $0x3, %edx
jne 0x316f
leaq 0x68262(%rip), %rsi     # 0x6b211
pushq $0x1
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
js 0x319f
movzbl 0x68241(%rip), %edx     # 0x6b211
leaq 0x6823b(%rip), %rsi     # 0x6b212
pushq $0x1
popq %rcx
movl %r15d, %edi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
js 0x31a8
movb %r13b, 0xb(%rsp)
movl %r12d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq (%rbx,%r13), %rsi
addq $0x2, %rsi
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
js 0x3182
addq $0x2, %r13
jmp 0x30bd
leaq 0x34ee(%rip), %rsi      # 0x651d
jmp 0x31af
leaq 0x3512(%rip), %rsi      # 0x654d
jmp 0x31af
leaq 0x681ca(%rip), %rsi     # 0x6b211
pushq $0x10
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
jns 0x308a
leaq 0x35ee(%rip), %rsi      # 0x6652
jmp 0x31af
leaq 0x681a1(%rip), %rsi     # 0x6b211
pushq $0x4
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
js 0x3196
movb %r13b, 0xb(%rsp)
movl %r12d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq (%rbx,%r13), %rsi
incq %rsi
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %edi
callq 0x460b
movq %rax, %r12
testl %r12d, %r12d
js 0x3182
incq %r13
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leaq (%r12,%r13), %rbx
leaq 0x68251(%rip), %rdi     # 0x6b320
movl $0x101, %edx            # imm = 0x101
xorl %esi, %esi
callq 0x2140
leaq 0x6812e(%rip), %rsi     # 0x6b210
leaq 0x68237(%rip), %rdi     # 0x6b320
movq %rbx, %rdx
callq 0x21a0
leaq 0x68118(%rip), %rdi     # 0x6b210
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2140
movb 0xb(%rsp), %al
movb %al, 0x68104(%rip)      # 0x6b210
leaq 0x680fe(%rip), %rdi     # 0x6b211
leaq 0x68206(%rip), %rsi     # 0x6b320
movq %rbx, %rdx
callq 0x21a0
bswapl %r15d
leaq 0x680e5(%rip), %rax     # 0x6b211
movl %r15d, (%rax,%rbx)
leaq (%r12,%r13), %rdx
addq $0x5, %rdx
pushq $0x6
popq %r8
movl %ebp, %edi
leaq 0x680cb(%rip), %rsi     # 0x6b210
movq %r14, %rcx
callq 0x295a
movq %rax, %r12
testl %r12d, %r12d
js 0x318b
leaq 0xc(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x3d66
movl $0x1, (%rax)
xorl %r12d, %r12d
jmp 0x31b9
leaq 0x350f(%rip), %rsi      # 0x6685
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x26e8
jmp 0x31b9
leaq 0x3464(%rip), %rsi      # 0x65ed
jmp 0x31af
leaq 0x352d(%rip), %rsi      # 0x66bf
xorl %edi, %edi
jmp 0x31b2
leaq 0x3482(%rip), %rsi      # 0x661f
jmp 0x31af
leaq 0x33d1(%rip), %rsi      # 0x6577
jmp 0x31af
leaq 0x3404(%rip), %rsi      # 0x65b3
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x26e8
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq