// File: /workspace/github_c_makefile_star2_O3/enzo1982[P]smooth/classes/io/drivers/driver_socks4.cpp
// Function: smooth::IO::DriverSOCKS4::DriverSOCKS4(smooth::String const&, int, smooth::String const&, int)
S::IO::DriverSOCKS4::DriverSOCKS4(const String &proxy, Int socksPort, const String &hostName, Int port) : Driver()
{
	closeStream = False;

	stream	    = -1;
	size	    = -1;

	if (hostName == NIL || hostName.Length() > 255) { lastError = IO_ERROR_BADPARAM; return; }

	/* Open TCP/IP socket.
	 */
	stream = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (stream == (unsigned) (~0)) { lastError = IO_ERROR_UNEXPECTED; return; }

	closeStream = True;

	/* Get proxy and server hostname.
	 */
	hostent		*host = gethostbyname(proxy);

	if (host == NIL) { Close(); lastError = IO_ERROR_UNEXPECTED; return; }

	/* Connect to proxy.
	 */
	sockaddr_in	 saddr;

	saddr.sin_family = AF_INET;
	saddr.sin_addr	 = *((in_addr *) *host->h_addr_list);
	saddr.sin_port	 = htons((short) socksPort);

	memset(&saddr.sin_zero, 0, sizeof(saddr.sin_zero));

	if (connect(stream, (sockaddr *) &saddr, sizeof(struct sockaddr)) == -1) { Close(); lastError = IO_ERROR_UNEXPECTED; return; }

	/* Send connect request.
	 */
	hostent		*server_hostent = gethostbyname(hostName);

	if (server_hostent != NULL)
	{
		unsigned char	*socksdata = new unsigned char [9];

		socksdata[0] = 4;
		socksdata[1] = 1;
		socksdata[2] = htons((short) port) % 256;
		socksdata[3] = htons((short) port) / 256;
		socksdata[4] = server_hostent->h_addr_list[0][0];
		socksdata[5] = server_hostent->h_addr_list[0][1];
		socksdata[6] = server_hostent->h_addr_list[0][2];
		socksdata[7] = server_hostent->h_addr_list[0][3];
		socksdata[8] = 0;

		if (send(stream, (char *) socksdata, 9, 0) < 9) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		delete [] socksdata;
	}
	else if (inet_addr(hostName) != INADDR_NONE)
	{
		unsigned char	*socksdata = new unsigned char [9];

		socksdata[0] = 4;
		socksdata[1] = 1;
		socksdata[2] = htons((short) port) % 256;
		socksdata[3] = htons((short) port) / 256;
		socksdata[4] = IOGetByte(inet_addr(hostName), 0);
		socksdata[5] = IOGetByte(inet_addr(hostName), 1);
		socksdata[6] = IOGetByte(inet_addr(hostName), 2);
		socksdata[7] = IOGetByte(inet_addr(hostName), 3);
		socksdata[8] = 0;

		if (send(stream, (char *) socksdata, 9, 0) < 9) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		delete [] socksdata;
	}
	else
	{
		Int	 hostNameLen = strlen(hostName);

		unsigned char	*socksdata = new unsigned char [10 + hostNameLen];

		socksdata[0] = 4;
		socksdata[1] = 1;
		socksdata[2] = htons((short) port) % 256;
		socksdata[3] = htons((short) port) / 256;
		socksdata[4] = 0;
		socksdata[5] = 0;
		socksdata[6] = 0;
		socksdata[7] = 1;
		socksdata[8] = 0;

		for (Int i = 0; i < hostNameLen; i++) socksdata[9 + i] = hostName[i];

		socksdata[9 + hostNameLen] = 0;

		if (send(stream, (char *) socksdata, 10 + hostNameLen, 0) < 10 + hostNameLen) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		delete [] socksdata;
	}

	/* Receive answer.
	 */
	unsigned char	*socksdata = new unsigned char [8];
	int		 recbytes  = 0;

	while (recbytes != 8)
	{
		int	 bytes = recv(stream, (char *) socksdata + recbytes, 8 - recbytes, 0);

		if (bytes <= 0) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		recbytes += bytes;
	}

	/* Check if connect attempt was successful.
	 */
	if (socksdata[1] != 90) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

	delete [] socksdata;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %r13d
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
callq 0x58560
movq 0xca3b0(%rip), %rax     # 0x16b8f0
addq $0x10, %rax
movq %rax, (%rbx)
movb $0x0, 0x3c(%rbx)
movl $0xffffffff, 0x38(%rbx) # imm = 0xFFFFFFFF
movq $-0x1, 0x8(%rbx)
movq %r14, %rdi
xorl %esi, %esi
callq 0x55260
testb %al, %al
jne 0xa1577
movq %r14, %rdi
callq 0x57180
cmpl $0x100, %eax            # imm = 0x100
jl 0xa1583
movl $0x2, 0x18(%rbx)
jmp 0xa1895
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x56660
movl %eax, 0x38(%rbx)
cmpl $-0x1, %eax
je 0xa188e
movb $0x1, 0x3c(%rbx)
movq %r15, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x56530
testq %rax, %rax
je 0xa168e
leaq 0x8(%rsp), %rsi
movw $0x2, (%rsi)
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
rolw $0x8, %bp
movl %eax, 0x4(%rsi)
movw %bp, 0x2(%rsi)
movq $0x0, 0x8(%rsi)
movl 0x38(%rbx), %edi
movl $0x10, %edx
callq 0x56230
cmpl $-0x1, %eax
je 0xa169c
movq %r14, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x56530
movq %rax, %r15
testq %rax, %rax
je 0xa16aa
movl $0x9, %edi
callq 0x56670
movq %rax, %rbp
movw $0x104, (%rax)          # imm = 0x104
movl %r13d, %eax
rolw $0x8, %ax
movb %al, 0x2(%rbp)
movb %ah, 0x3(%rbp)
movq 0x18(%r15), %rax
movq (%rax), %rax
movb (%rax), %cl
movb %cl, 0x4(%rbp)
movb 0x1(%rax), %cl
movb %cl, 0x5(%rbp)
movb 0x2(%rax), %cl
movb %cl, 0x6(%rbp)
movb 0x3(%rax), %al
movb %al, 0x7(%rbp)
movb $0x0, 0x8(%rbp)
movl 0x38(%rbx), %edi
movl $0x9, %edx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x56130
movq %rax, %r14
movq %rbp, %rdi
callq 0x561e0
cmpq $0x8, %r14
jg 0xa182d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0xa188e
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0xa188e
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0xa188e
movq %r14, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x58bc0
cmpl $-0x1, %eax
je 0xa176d
movl $0x9, %edi
callq 0x56670
movq %rax, %rbp
movw $0x104, (%rax)          # imm = 0x104
movl %r13d, %eax
rolw $0x8, %ax
movb %al, 0x2(%rbp)
movb %ah, 0x3(%rbp)
movq %r14, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x58bc0
movb %al, 0x4(%rbp)
movq %r14, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x58bc0
movb %ah, 0x5(%rbp)
movq %r14, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x58bc0
shrl $0x10, %eax
movb %al, 0x6(%rbp)
movq %r14, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x58bc0
shrl $0x18, %eax
movb %al, 0x7(%rbp)
movb $0x0, 0x8(%rbp)
movl 0x38(%rbx), %edi
movl $0x9, %edx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x56130
movq %rax, %r14
movq %rbp, %rdi
callq 0x561e0
cmpq $0x8, %r14
jg 0xa182d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0xa188e
movq %r14, %rdi
callq 0x585e0
movq %rax, %rdi
callq 0x56370
movq %rax, %r12
shlq $0x20, %rax
movabsq $0xa00000000, %r15      # imm = 0xA00000000
movq %rax, (%rsp)
addq %rax, %r15
sarq $0x20, %r15
movq %r15, %rdi
callq 0x56670
movq %rax, %rbp
movw $0x104, (%rax)          # imm = 0x104
movl %r13d, %eax
rolw $0x8, %ax
movb %al, 0x2(%rbp)
movb %ah, 0x3(%rbp)
movl $0x1000000, 0x4(%rbp)   # imm = 0x1000000
movb $0x0, 0x8(%rbp)
testl %r12d, %r12d
jle 0xa17e8
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %r13d, %r13d
movq %r14, %rdi
movl %r13d, %esi
callq 0x57fe0
movb %al, 0x9(%rbp,%r13)
incq %r13
cmpq %r13, %r12
jne 0xa17d0
movabsq $0x900000000, %rax      # imm = 0x900000000
movq (%rsp), %rcx
addq %rax, %rcx
sarq $0x20, %rcx
movb $0x0, (%rbp,%rcx)
movl 0x38(%rbx), %edi
movq %rbp, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x56130
movq %rax, %r14
movq %rbp, %rdi
callq 0x561e0
cmpq %r15, %r14
jge 0xa182d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0xa188e
movl $0x8, %edi
callq 0x56670
movq %rax, %r14
xorl %ebp, %ebp
movl 0x38(%rbx), %edi
movl %ebp, %esi
addq %r14, %rsi
movl $0x8, %eax
subl %ebp, %eax
movslq %eax, %rdx
xorl %ecx, %ecx
callq 0x582d0
testl %eax, %eax
jle 0xa187d
addl %eax, %ebp
cmpl $0x8, %ebp
jne 0xa183c
movb 0x1(%r14), %bpl
movq %r14, %rdi
callq 0x561e0
cmpb $0x5a, %bpl
je 0xa1895
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0xa188e
movq %r14, %rdi
callq 0x561e0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
movl $0x3, 0x18(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
jmp 0xa18bc
movq %rax, %r14
movq %rbx, %rdi
callq 0x56c30
movq %r14, %rdi
callq 0x57090
nop