// File: /workspace/github_c_makefile_star2_O2/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 0x59850
movq 0xc46d8(%rip), %rax     # 0x1618f0
addq $0x10, %rax
movq %rax, (%rbx)
movb $0x0, 0x3c(%rbx)
orl $-0x1, 0x38(%rbx)
orq $-0x1, 0x8(%rbx)
movq %r14, %rdi
xorl %esi, %esi
callq 0x56280
testb %al, %al
jne 0x9d249
movq %r14, %rdi
callq 0x58370
cmpl $0x100, %eax            # imm = 0x100
jl 0x9d255
movl $0x2, 0x18(%rbx)
jmp 0x9d551
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x57780
movl %eax, 0x38(%rbx)
cmpl $-0x1, %eax
je 0x9d54a
movb $0x1, 0x3c(%rbx)
movq %r15, %rdi
callq 0x598d0
movq %rax, %rdi
callq 0x57640
testq %rax, %rax
je 0x9d351
leaq 0x8(%rsp), %rsi
movw $0x2, (%rsi)
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, 0x4(%rsi)
rolw $0x8, %bp
movw %bp, 0x2(%rsi)
andq $0x0, 0x8(%rsi)
movl 0x38(%rbx), %edi
pushq $0x10
popq %rdx
callq 0x57320
cmpl $-0x1, %eax
je 0x9d35f
movq %r14, %rdi
callq 0x598d0
movq %rax, %rdi
callq 0x57640
movq %rax, %r15
testq %rax, %rax
je 0x9d36d
pushq $0x9
popq %rdi
callq 0x577a0
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
pushq $0x9
popq %rdx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x57220
movq %rax, %r14
movq %rbp, %rdi
callq 0x572d0
cmpq $0x8, %r14
jg 0x9d4e9
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0x9d54a
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0x9d54a
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0x9d54a
movq %r14, %rdi
callq 0x598d0
movq %rax, %rdi
callq 0x59f10
cmpl $-0x1, %eax
je 0x9d42c
pushq $0x9
popq %rdi
callq 0x577a0
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 0x598d0
movq %rax, %rdi
callq 0x59f10
movb %al, 0x4(%rbp)
movq %r14, %rdi
callq 0x598d0
movq %rax, %rdi
callq 0x59f10
movb %ah, 0x5(%rbp)
movq %r14, %rdi
callq 0x598d0
movq %rax, %rdi
callq 0x59f10
shrl $0x10, %eax
movb %al, 0x6(%rbp)
movq %r14, %rdi
callq 0x598d0
movq %rax, %rdi
callq 0x59f10
shrl $0x18, %eax
movb %al, 0x7(%rbp)
movb $0x0, 0x8(%rbp)
movl 0x38(%rbx), %edi
pushq $0x9
popq %rdx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x57220
movq %rax, %r14
movq %rbp, %rdi
callq 0x572d0
cmpq $0x8, %r14
jg 0x9d4e9
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0x9d54a
movq %r14, %rdi
callq 0x598d0
movq %rax, %rdi
callq 0x57460
movq %rax, %r12
shlq $0x20, %rax
movabsq $0xa00000000, %r15      # imm = 0xA00000000
movq %rax, (%rsp)
addq %rax, %r15
sarq $0x20, %r15
movq %r15, %rdi
callq 0x577a0
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)
xorl %r13d, %r13d
testl %r12d, %r12d
cmovlel %r13d, %r12d
cmpq %r13, %r12
je 0x9d4a4
movq %r14, %rdi
movl %r13d, %esi
callq 0x59270
movb %al, 0x9(%rbp,%r13)
incq %r13
jmp 0x9d48a
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 0x57220
movq %rax, %r14
movq %rbp, %rdi
callq 0x572d0
cmpq %r15, %r14
jge 0x9d4e9
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0x9d54a
pushq $0x8
popq %rdi
callq 0x577a0
movq %rax, %r14
xorl %ebp, %ebp
pushq $0x8
popq %r15
cmpl $0x8, %ebp
je 0x9d52f
movl 0x38(%rbx), %edi
movl %ebp, %esi
addq %r14, %rsi
movl %r15d, %eax
subl %ebp, %eax
movslq %eax, %rdx
xorl %ecx, %ecx
callq 0x59580
addl %eax, %ebp
testl %eax, %eax
jg 0x9d4fa
movq %r14, %rdi
callq 0x572d0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
jmp 0x9d54a
movb 0x1(%r14), %bpl
movq %r14, %rdi
callq 0x572d0
cmpb $0x5a, %bpl
je 0x9d551
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 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
jmp 0x9d578
movq %rax, %r14
movq %rbx, %rdi
callq 0x57db0
movq %r14, %rdi
callq 0x58260
nop