// File: akw0088[P]openssl/download.cpp
// Function: file_upload(char*, unsigned short)
int file_upload(char *file, unsigned short port)
{
	int			connfd;
	unsigned int		size = sizeof(struct sockaddr_in);
	struct sockaddr_in	servaddr, client;
	time_t			ticks;
	int listenfd;

#ifdef _WIN32
	WSADATA		WSAData;

	WSAStartup(MAKEWORD(2, 0), &WSAData);
#endif

	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listenfd == -1)
	{
		perror("socket error");
		return -1;
	}

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(port);

	if ((::bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) == -1)
	{
		perror("bind error");
		return 0;
	}

	printf("Server listening on: %s:%d\n", inet_ntoa(servaddr.sin_addr), htons(servaddr.sin_port));

	if (listen(listenfd, 3) == -1)
	{
		perror("listen error");
		return -1;
	}

	for (;;)
	{
		char response[1024] = { 0 };

		printf("listening for connections...\n");
		connfd = accept(listenfd, (struct sockaddr *)&client, (socklen_t *)&size);
		if (connfd == INVALID_SOCKET)
			continue;

		ticks = time(NULL);
		snprintf(response, sizeof(response), "%.24s\r\n", ctime(&ticks));
		printf("Client: %s - %s", inet_ntoa(client.sin_addr), response);

		unsigned int file_size = 0;
		char *data = get_file(file, &file_size);
		char file_name[128] = { 0 };

		memcpy(file_name, file, MIN(127, strlen(file)));
		send(connfd, (char *)&file_size, sizeof(int), 0);
		send(connfd, (char *)&file_name, 128, 0);
		send(connfd, data, file_size, 0);
		closesocket(connfd);
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x510, %rsp            # imm = 0x510
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movl $0x10, -0x1c(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x10a0
movl %eax, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0x16ef
leaq 0xac0(%rip), %rdi       # 0x219e
callq 0x1150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1942
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1110
movw $0x2, -0x2c(%rbp)
xorl %edi, %edi
callq 0x1120
movl %eax, -0x28(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x1070
movw %ax, -0x2a(%rbp)
movl -0x4c(%rbp), %edi
leaq -0x2c(%rbp), %rsi
movl $0x10, %edx
callq 0x1080
cmpl $-0x1, %eax
jne 0x174a
leaq 0xa72(%rip), %rdi       # 0x21ab
callq 0x1150
movl $0x0, -0x4(%rbp)
jmp 0x1942
movl -0x28(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %edi
callq 0x10f0
movq %rax, -0x4e8(%rbp)
movzwl -0x2a(%rbp), %edi
callq 0x1070
movq -0x4e8(%rbp), %rsi
movzwl %ax, %edx
leaq 0xa3d(%rip), %rdi       # 0x21b6
movb $0x0, %al
callq 0x1060
movl -0x4c(%rbp), %edi
movl $0x3, %esi
callq 0x11a0
cmpl $-0x1, %eax
jne 0x17aa
leaq 0xa39(%rip), %rdi       # 0x21d2
callq 0x1150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1942
jmp 0x17ac
leaq -0x450(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1110
leaq 0xa19(%rip), %rdi       # 0x21df
movb $0x0, %al
callq 0x1060
movl -0x4c(%rbp), %edi
leaq -0x3c(%rbp), %rsi
leaq -0x1c(%rbp), %rdx
callq 0x10b0
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x17e8
jmp 0x17ac
xorl %eax, %eax
movl %eax, %edi
callq 0x1160
movq %rax, -0x48(%rbp)
leaq -0x450(%rbp), %rax
movq %rax, -0x500(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x10d0
movq -0x500(%rbp), %rdi
movq %rax, %rcx
movl $0x400, %esi            # imm = 0x400
leaq 0x9db(%rip), %rdx       # 0x21fd
movb $0x0, %al
callq 0x1200
movl -0x38(%rbp), %eax
movl %eax, -0x454(%rbp)
movl -0x454(%rbp), %edi
callq 0x10f0
movq %rax, %rsi
leaq -0x450(%rbp), %rdx
leaq 0x9b7(%rip), %rdi       # 0x2205
movb $0x0, %al
callq 0x1060
movl $0x0, -0x458(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x458(%rbp), %rsi
callq 0x1330
movq %rax, -0x460(%rbp)
leaq -0x4e0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1110
leaq -0x4e0(%rbp), %rax
movq %rax, -0x4f8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x4f0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10e0
movq %rax, %rcx
movl $0x7f, %eax
cmpq %rcx, %rax
jae 0x18c6
movl $0x7f, %eax
movq %rax, -0x508(%rbp)
jmp 0x18d6
movq -0x10(%rbp), %rdi
callq 0x10e0
movq %rax, -0x508(%rbp)
movq -0x4f0(%rbp), %rsi
movq -0x4f8(%rbp), %rdi
movq -0x508(%rbp), %rdx
callq 0x1130
movl -0x18(%rbp), %edi
leaq -0x458(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1100
movl -0x18(%rbp), %edi
leaq -0x4e0(%rbp), %rsi
movl $0x80, %edx
xorl %ecx, %ecx
callq 0x1100
movl -0x18(%rbp), %edi
movq -0x460(%rbp), %rsi
movl -0x458(%rbp), %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x1100
movl -0x18(%rbp), %edi
callq 0x1220
jmp 0x17ac
movl -0x4(%rbp), %eax
addq $0x510, %rsp            # imm = 0x510
popq %rbp
retq
nop