// File: hinrikHelga[P]Botnet/server.cpp
// Function: main
int main(int argc, char* argv[])
{

    bool finished;
    int listenTCPSock;              // Socket for TCP connections to server
    int clientSock;                 // Socket of connecting client
    fd_set openSockets;             // Current open sockets
    fd_set readSockets;             // Socket list for select()
    fd_set exceptSockets;           // Exception socket list
    int maxfds;
                      // Passed to select() as max fd in set
    struct sockaddr_in client;
    socklen_t client_len;
    char buffer[1025];              // buffer for reading from clients

    myName = "V_group_65";

    if(argc != 2)
    {
        printf("Usage: chat_server <tcp port>\n");
        exit(0);
    }

    // Setup TCP socket for server to listen to

    listenTCPSock = openTcpSocket(atoi(argv[1]));
    printf("Listening on TCP port: %s\n", argv[1]);


    if(listen(listenTCPSock, BACKLOG) < 0)
    {
        printf("Listen failed on tcp port %s\n", argv[1]);
        exit(0);
    }
    else
    // Add listen socket to socket set
    {
        FD_SET(listenTCPSock, &openSockets);
        maxfds = listenTCPSock;
    }

    finished = false;

    while(!finished)
    {
        // Get modifiable copy of readSockets
        readSockets = exceptSockets = openSockets;
        memset(buffer, 0, sizeof(buffer));

        int n = select(maxfds + 1, &readSockets, NULL, &exceptSockets, NULL);

        if(n < 0)
        {
            perror("select failed - closing down\n");
            finished = true;
        }
        else
        {
            // Accept  any new connections to the server
            if(FD_ISSET(listenTCPSock, &readSockets))
            {
               clientSock = accept(listenTCPSock, (struct sockaddr *)&client,
                                   &client_len);

               FD_SET(clientSock, &openSockets);
               maxfds = std::max(maxfds, clientSock);

               n--;

               printf("Client connected on server: %d\n", clientSock);
            }
            // Now check for commands from clients
            while(n-- > 0)
            {
               for(auto const& pair : connectedServers)
               {
                  Node *client = pair.second;

                  if(FD_ISSET(client->sock, &readSockets))
                  {
                      if(recv(client->sock, buffer, sizeof(buffer), MSG_DONTWAIT) == 0)
                      {
                          printf("Client closed connection: %d", client->sock);
                          close(client->sock);

                          closeClient(client->sock, &openSockets, &maxfds);

                      }
                      else
                      {
                          std::cout << buffer << std::endl;
                          inputCommand(client->sock, &openSockets, &maxfds,
                                        buffer);
                      }
                  }
               }
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x600, %rsp            # imm = 0x600
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x7b20(%rip), %rdi      # 0xb250
leaq 0x3abd(%rip), %rsi      # 0x71f4
callq 0x22f0
cmpl $0x2, -0x8(%rbp)
je 0x3757
leaq 0x3ab6(%rip), %rdi      # 0x71ff
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x22a0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2370
movl %eax, %edi
callq 0x2740
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x3aa1(%rip), %rdi      # 0x721e
movb $0x0, %al
callq 0x2050
movl -0x18(%rbp), %edi
movl $0x5, %esi
callq 0x2280
cmpl $0x0, %eax
jge 0x37b3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x3a94(%rip), %rdi      # 0x7239
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x22a0
movl -0x18(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x18(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xa0(%rbp,%rax,8), %rcx
movq %rcx, -0xa0(%rbp,%rax,8)
movl -0x18(%rbp), %eax
movl %eax, -0x1a4(%rbp)
movb $0x0, -0x11(%rbp)
movb -0x11(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3805
jmp 0x3b13
leaq -0x1a0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x80, %edx
callq 0x21a0
leaq -0x120(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
movl $0x80, %edx
callq 0x21a0
leaq -0x5c0(%rbp), %rdi
xorl %esi, %esi
movl $0x401, %edx            # imm = 0x401
callq 0x2140
movl -0x1a4(%rbp), %edi
addl $0x1, %edi
leaq -0x120(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
leaq -0x1a0(%rbp), %rcx
movq %r8, %rdx
callq 0x2200
movl %eax, -0x5c4(%rbp)
cmpl $0x0, -0x5c4(%rbp)
jge 0x3890
leaq 0x39d5(%rip), %rdi      # 0x7257
callq 0x21c0
movb $0x1, -0x11(%rbp)
jmp 0x3b0e
movl -0x18(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x5f8(%rbp)
movl -0x18(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x5f8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x3960
movl -0x18(%rbp), %edi
leaq -0x1b4(%rbp), %rsi
leaq -0x1b8(%rbp), %rdx
callq 0x20c0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x1c(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xa0(%rbp,%rax,8), %rcx
movq %rcx, -0xa0(%rbp,%rax,8)
leaq -0x1a4(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x3c40
movl (%rax), %eax
movl %eax, -0x1a4(%rbp)
movl -0x5c4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c4(%rbp)
movl -0x1c(%rbp), %esi
leaq 0x391c(%rip), %rdi      # 0x7275
movb $0x0, %al
callq 0x2050
jmp 0x3962
movl -0x5c4(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x5c4(%rbp)
cmpl $0x0, %eax
jle 0x3b0c
leaq 0x789d(%rip), %rax      # 0xb220
movq %rax, -0x5d0(%rbp)
leaq 0x788f(%rip), %rdi      # 0xb220
callq 0x3b90
movq %rax, -0x5d8(%rbp)
leaq 0x787c(%rip), %rdi      # 0xb220
callq 0x3bc0
movq %rax, -0x5e0(%rbp)
leaq -0x5d8(%rbp), %rdi
leaq -0x5e0(%rbp), %rsi
callq 0x3bf0
testb $0x1, %al
jne 0x39cc
jmp 0x3b07
leaq -0x5d8(%rbp), %rdi
callq 0x3c20
movq %rax, -0x5e8(%rbp)
movq -0x5e8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x5f0(%rbp)
movq -0x5f0(%rbp), %rax
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x600(%rbp)
movq -0x5f0(%rbp), %rax
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x600(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x3af4
movq -0x5f0(%rbp), %rax
movl (%rax), %edi
leaq -0x5c0(%rbp), %rsi
movl $0x401, %edx            # imm = 0x401
movl $0x40, %ecx
callq 0x20d0
cmpq $0x0, %rax
jne 0x3aad
movq -0x5f0(%rbp), %rax
movl (%rax), %esi
leaq 0x381b(%rip), %rdi      # 0x7295
movb $0x0, %al
callq 0x2050
movq -0x5f0(%rbp), %rax
movl (%rax), %edi
callq 0x23e0
movq -0x5f0(%rbp), %rax
movl (%rax), %edi
leaq -0xa0(%rbp), %rsi
leaq -0x1a4(%rbp), %rdx
callq 0x2620
jmp 0x3af2
leaq -0x5c0(%rbp), %rsi
movq 0x751d(%rip), %rdi      # 0xafd8
callq 0x2220
movq %rax, %rdi
movq 0x74f6(%rip), %rsi      # 0xafc0
callq 0x2250
movq -0x5f0(%rbp), %rax
movl (%rax), %edi
leaq -0x5c0(%rbp), %rcx
leaq -0xa0(%rbp), %rsi
leaq -0x1a4(%rbp), %rdx
callq 0x2850
jmp 0x3af4
jmp 0x3af6
leaq -0x5d8(%rbp), %rdi
callq 0x3c80
jmp 0x39b0
jmp 0x3962
jmp 0x3b0e
jmp 0x37f7
movl -0x4(%rbp), %eax
addq $0x600, %rsp            # imm = 0x600
popq %rbp
retq
nop