// File: qian-jiahong[P]uvncrep017-ws/repeater.cpp
// Function: openConnectionToVncServer(char const*, unsigned short, char*)
static int openConnectionToVncServer(const char *host, unsigned short port, char *serverIp)
{
    int s;
    struct sockaddr_in saddr;
    struct hostent *h;
        
    h = gethostbyname(host);
    if (NULL == h) {
        debug(LEVEL_2, "openConnectionToVncServer(): can't resolve hostname: %s\n", host);
        return -1;
    }
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(port);
    
    //Interesting;-) typecast / indirection thing copied from "Beej's Guide to network programming". 
    //See http://beej.us/guide/bgnet/ for more info
    saddr.sin_addr = *((struct in_addr *)h->h_addr);

    memset(&(saddr.sin_zero), '\0', 8); // zero the rest of the struct
    
    strlcpy(serverIp, inet_ntoa(saddr.sin_addr), MAX_IP_LEN);
    
    //Check server addresses against list of allowed addresses / ranges
    if (requireListedServer == 1) {
        if (!isServerAddressAllowed(serverIp)) {
            debug(LEVEL_2, "openConnectionToVncServer(): server is not allowed (%s)\n", serverIp);
            return -1;
        }
    }
    
    debug(LEVEL_3, "openConnectionToVncServer(): connecting to %s:%u\n", serverIp, port);

    s = socket(AF_INET, SOCK_STREAM, 0);
    
    //Trying to connect with timeout
    if (connectWithTimeout(s, (struct sockaddr *) &saddr, sizeof(saddr), TIMEOUT_10SECS) != 0) {
        debug(LEVEL_2, "openConnectionToVncServer(): connectWithTimeout() failed.\n");
        close(s);
        strlcpy(serverIp, "", MAX_IP_LEN);
        return -1;
    }
    else
        return s;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2240
movq %rax, -0x40(%rbp)
xorl %eax, %eax
cmpq -0x40(%rbp), %rax
jne 0x5c9f
movq -0x10(%rbp), %rdx
movl $0x2, %edi
leaq 0x5b48(%rip), %rsi      # 0xb7d4
movb $0x0, %al
callq 0x2480
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dc4
movw $0x2, -0x34(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x2040
movw %ax, -0x32(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
leaq -0x34(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2120
movq -0x20(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %edi
callq 0x20f0
movq -0x50(%rbp), %rdi
movq %rax, %rsi
movl $0x32, %edx
callq 0x76d0
leaq 0x125ec(%rip), %rax     # 0x182f0
cmpl $0x1, (%rax)
jne 0x5d3b
movq -0x20(%rbp), %rdi
callq 0x6360
testb $0x1, %al
jne 0x5d39
movq -0x20(%rbp), %rdx
movl $0x2, %edi
leaq 0x5ae7(%rip), %rsi      # 0xb80d
movb $0x0, %al
callq 0x2480
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dc4
jmp 0x5d3b
movq -0x20(%rbp), %rdx
movzwl -0x12(%rbp), %ecx
movl $0x3, %edi
leaq 0x5af7(%rip), %rsi      # 0xb846
movb $0x0, %al
callq 0x2480
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2070
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
leaq -0x34(%rbp), %rsi
movl $0x10, %edx
movl $0xa, %ecx
callq 0x2800
cmpl $0x0, %eax
je 0x5dbe
movl $0x2, %edi
leaq 0x5ae7(%rip), %rsi      # 0xb878
movb $0x0, %al
callq 0x2480
movl -0x24(%rbp), %edi
callq 0x2370
movq -0x20(%rbp), %rdi
leaq 0x582f(%rip), %rsi      # 0xb5da
movl $0x32, %edx
callq 0x76d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dc4
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)