// File: /workspace/github_cpp_makefile_star2_O2/lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::FtpOpenPasv(ftphandle*, ftphandle**, ftplib::transfermode, int, char*)
int ftplib::FtpOpenPasv(ftphandle *nControl, ftphandle **nData, transfermode mode, int dir, char *cmd)
{
	int sData;
    int errorCode;
    int retry = 0;
	union {
		struct sockaddr sa;
		struct sockaddr_in in;
	} sin;
	struct linger lng = { 0, 0 };
	unsigned int l;
	int on=1;
	ftphandle *ctrl;
	char *cp;
	unsigned char v[6];
	int ret;

	if (nControl->dir != FTPLIB_CONTROL) 
        return -1;
	if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE)) 	{
		sprintf(nControl->response, "Invalid direction %d\n", dir);
		return -1;
	}
	if ((mode != ftplib::ascii) && (mode != ftplib::image)) {
		sprintf(nControl->response, "Invalid mode %c\n", mode);
		return -1;
	}
	l = sizeof(sin);

	memset(&sin, 0, l);
	sin.in.sin_family = AF_INET;
	if (!FtpSendCmd("PASV",'2',nControl)) 
        return -1;
	cp = strchr(nControl->response,'(');
	if (cp == NULL) 
        return -1;
	cp++;
#if defined(_WIN32)
	unsigned int v_i[6];
    sscanf(cp,"%u,%u,%u,%u,%u,%u",&v_i[2],&v_i[3],&v_i[4],&v_i[5],&v_i[0],&v_i[1]);
    for (int i = 0; i < 6; i++) 
        v[i] = (unsigned char) v_i[i]; 
#else
	sscanf(cp,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
#endif
	if (nControl->correctpasv) 
        if (!CorrectPasvResponse(v)) 
            return -1;
	sin.sa.sa_data[2] = v[2];
	sin.sa.sa_data[3] = v[3];
	sin.sa.sa_data[4] = v[4];
	sin.sa.sa_data[5] = v[5];
	sin.sa.sa_data[0] = v[0];
	sin.sa.sa_data[1] = v[1];

	if (mp_ftphandle->offset != 0)
	{
		char buf[256];
        sprint_rest(buf, mp_ftphandle->offset);
		if (!FtpSendCmd(buf,'3',nControl)) 
            return 0;
	}

	sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if (sData == -1) 	{
		perror("socket");
		return -1;
	}
	if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR, SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1) {
		perror("setsockopt");
		net_close(sData);
		return -1;
	}
	if (setsockopt(sData,SOL_SOCKET,SO_LINGER, SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1) 	{
		perror("setsockopt");
		net_close(sData);
		return -1;
	}

	if (nControl->dir != FTPLIB_CONTROL) 
        return -1;
	memcpy(cmd + strlen(cmd), "\r\n\0", 3);
#ifndef NOSSL
	if (nControl->tlsctrl) 
        ret = SSL_write(nControl->ssl,cmd,strlen(cmd));
	else 
        ret = net_write(nControl->handle,cmd,strlen(cmd));
#else
	ret = net_write(nControl->handle,cmd,strlen(cmd));
#endif

    if(gFtpDebug)
        printf("Thread:%-8ld  sock:%-4d  %s", syscall(SYS_gettid), nControl->handle, cmd);

	if (ret <= 0) 	{
	    errorCode = errno;
        printf("Thread:%-8ld  sock:%-4d  %s  send failed:%s\n", syscall(SYS_gettid), nControl->handle, cmd, strerror(errno));
		return -1;
	}

    while (true) {
        retry++;
        if (connect(sData, &sin.sa, sizeof(sin.sa)) == -1)  {
            if (retry >= 5) {
                errorCode = errno;
                std::cerr << "connect to establish data connnection failed: " << strerror(errorCode) << std::endl;
                net_close(sData);
                return -1;
            }
        } else {
            break;
        }
    }

    // 此处的步骤是： 先建立数据连接，然后再等待控制连接的应答
	if (!readresp('1', nControl)) 	{
		net_close(sData);
		return -1;
	}
    if(gFtpDebug)
        printf("Thread:%-8ld  sock:%-4d  %s\n", syscall(SYS_gettid), nControl->handle,  nControl->response);
    
	ctrl = static_cast<ftphandle*>(calloc(1,sizeof(ftphandle)));
	if (ctrl == NULL) 	{
		perror("calloc");
		net_close(sData);
		return -1;
	}
	if ((mode == 'A') && ((ctrl->buf = static_cast<char*>(malloc(FTPLIB_BUFSIZ))) == NULL)) 	{
		perror("calloc");
		net_close(sData);
		free(ctrl);
		return -1;
	}
	ctrl->handle = sData;
	ctrl->dir = dir;
	ctrl->ctrl = (nControl->cmode == ftplib::pasv) ? nControl : NULL;
	ctrl->idletime = nControl->idletime;
	ctrl->cbarg = nControl->cbarg;
	ctrl->xfered = 0;
	ctrl->xfered1 = 0;
	ctrl->cbbytes = nControl->cbbytes;
	if (ctrl->idletime.tv_sec || ctrl->idletime.tv_usec) ctrl->idlecb = nControl->idlecb;
	else ctrl->idlecb = NULL;
	if (ctrl->cbbytes ) ctrl->xfercb = nControl->xfercb;
	else ctrl->xfercb = NULL;
	*nData = ctrl;

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
andq $0x0, 0x48(%rsp)
movl $0x1, 0x24(%rsp)
pushq $-0x1
popq %r14
cmpl $0x0, 0x28(%rsi)
jne 0xd22e
movl %r8d, %r15d
movq %rsi, %rbx
leal -0x3(%r15), %eax
cmpl $-0x3, %eax
ja 0xcfcb
addq $0x88, %rbx
leaq 0x4848(%rip), %rsi      # 0x11808
movq %rbx, %rdi
movl %r15d, %edx
jmp 0xd227
movl %ecx, %ebp
movl %ecx, %eax
andl $-0x9, %eax
cmpl $0x41, %eax
jne 0xd214
movq %r9, %r12
movq %rdi, %r13
movq %rdx, 0x40(%rsp)
andq $0x0, 0x30(%rsp)
movw $0x2, 0x28(%rsp)
leaq 0x4870(%rip), %rsi      # 0x1186a
pushq $0x32
popq %rdx
movq %rbx, %rcx
callq 0xc9ae
testl %eax, %eax
je 0xd22e
leaq 0x88(%rbx), %rdi
pushq $0x28
popq %rsi
movq %rdi, 0x38(%rsp)
callq 0x4140
testq %rax, %rax
je 0xd22e
incq %rax
leaq 0x12(%rsp), %rdx
leaq 0x13(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x15(%rsp), %r9
leaq 0x11(%rsp), %r10
leaq 0x10(%rsp), %r11
leaq 0x47fa(%rip), %rsi      # 0x1184c
movq %rax, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x43b0
popq %rcx
popq %rdx
cmpb $0x1, 0x1b8(%rbx)
jne 0xd080
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0xd7b8
testl %eax, %eax
je 0xd22e
movzwl 0x14(%rsp), %eax
movw %ax, 0x2e(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x2a(%rsp)
movq (%r13), %rax
movq 0x1b0(%rax), %rdx
testq %rdx, %rdx
je 0xd0d3
leaq 0x4582(%rip), %rsi      # 0x1162b
xorl %r14d, %r14d
leaq 0x50(%rsp), %rdi
xorl %eax, %eax
callq 0x4090
pushq $0x33
popq %rdx
movq %r13, %rdi
leaq 0x50(%rsp), %rsi
movq %rbx, %rcx
callq 0xc9ae
testl %eax, %eax
je 0xd22e
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x40b0
cmpl $-0x1, %eax
je 0xd362
movl %eax, %r14d
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
leaq 0x24(%rsp), %rcx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x46c0
cmpl $-0x1, %eax
je 0xd370
movl %r14d, %edi
pushq $0x1
popq %r14
pushq $0xd
popq %rdx
leaq 0x48(%rsp), %rcx
pushq $0x8
popq %r8
movl %edi, 0xc(%rsp)
movl %r14d, %esi
callq 0x46c0
cmpl $-0x1, %eax
je 0xd381
cmpl $0x0, 0x28(%rbx)
jne 0xd396
movq %r12, %rdi
callq 0x4160
movw $0xa0d, (%r12,%rax)     # imm = 0xA0D
movb $0x0, 0x2(%r12,%rax)
movl 0x10(%rbx), %eax
movl %eax, 0x18(%rsp)
movq %r12, %rdi
callq 0x4160
movl 0x18(%rsp), %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x4120
movq %rax, %rcx
leaq 0xa345(%rip), %rax      # 0x174c0
cmpb $0x1, (%rax)
jne 0xd1ad
movl $0xba, %edi
xorl %eax, %eax
movq %rcx, 0x18(%rsp)
callq 0x4060
movl 0x10(%rbx), %edx
leaq 0x46d4(%rip), %rdi      # 0x1186f
movq %rax, %rsi
movq %r12, %rcx
xorl %eax, %eax
callq 0x4050
movq 0x18(%rsp), %rcx
testl %ecx, %ecx
jle 0xd243
pushq $0x5
popq %r12
movl 0xc(%rsp), %edi
leaq 0x28(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x42f0
cmpl $-0x1, %eax
jne 0xd283
decl %r12d
jne 0xd1b9
callq 0x4040
movl (%rax), %ebx
movq 0x9e0a(%rip), %rdi      # 0x16ff0
leaq 0x46cb(%rip), %rsi      # 0x118b8
callq 0x43c0
movq %rax, %r14
movl %ebx, %edi
callq 0x4110
movq %r14, %rdi
movq %rax, %rsi
callq 0x43c0
movq %rax, %rdi
callq 0x41b0
jmp 0xd38d
addq $0x88, %rbx
leaq 0x45fc(%rip), %rsi      # 0x1181e
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x4090
movl %r14d, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4040
movq %rax, %r14
movl $0xba, %edi
xorl %eax, %eax
callq 0x4060
movq %rax, %r15
movl 0x10(%rbx), %ebx
movl (%r14), %edi
callq 0x4110
leaq 0x461f(%rip), %rdi      # 0x1188b
movq %r15, %rsi
movl %ebx, %edx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x4050
jmp 0xd396
pushq $0x31
popq %rsi
movq %r13, %rdi
movq %rbx, %rdx
callq 0xc65c
testl %eax, %eax
je 0xd38d
leaq 0xa220(%rip), %rax      # 0x174c0
cmpb $0x1, (%rax)
jne 0xd2ca
movl $0xba, %edi
xorl %eax, %eax
callq 0x4060
movl 0x10(%rbx), %edx
leaq 0x449b(%rip), %rdi      # 0x11756
movq %rax, %rsi
movq 0x38(%rsp), %rcx
xorl %eax, %eax
callq 0x4050
pushq $0x1
popq %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4240
testq %rax, %rax
je 0xd3ae
movq %rax, %r12
cmpl $0x41, %ebp
jne 0xd300
movl $0x400, %edi            # imm = 0x400
callq 0x44f0
movq %rax, 0x20(%r12)
testq %rax, %rax
je 0xd3b7
movl 0xc(%rsp), %eax
movl %eax, 0x10(%r12)
movl %r15d, 0x28(%r12)
xorl %eax, %eax
cmpl $0x1, 0x38(%rbx)
cmoveq %rbx, %rax
movq %rax, 0x30(%r12)
movups 0x40(%rbx), %xmm0
movups %xmm0, 0x40(%r12)
movq 0x68(%rbx), %rax
movq %rax, 0x68(%r12)
movq 0x78(%rbx), %rax
movq %rax, 0x78(%r12)
cmpq $0x0, 0x40(%r12)
movq 0x40(%rsp), %rdx
jne 0xd34e
cmpq $0x0, 0x48(%r12)
je 0xd357
movq 0x58(%rbx), %rcx
movq %rcx, 0x58(%r12)
testq %rax, %rax
je 0xd39f
movq 0x50(%rbx), %rax
jmp 0xd3a1
leaq 0x438b(%rip), %rdi      # 0x116f4
callq 0x4300
jmp 0xd396
leaq 0x4384(%rip), %rdi      # 0x116fb
callq 0x4300
movl %r14d, %edi
jmp 0xd391
leaq 0x4373(%rip), %rdi      # 0x116fb
callq 0x4300
movl 0xc(%rsp), %edi
callq 0x4740
pushq $-0x1
popq %r14
jmp 0xd22e
xorl %eax, %eax
movq %rax, 0x50(%r12)
movq %r12, (%rdx)
jmp 0xd22e
leaq 0x426f(%rip), %rdi      # 0x11624
jmp 0xd388
leaq 0x4266(%rip), %rdi      # 0x11624
callq 0x4300
movl 0xc(%rsp), %edi
callq 0x4740
movq %r12, %rdi
callq 0x4480
jmp 0xd396