// File: DavieV[P]virtual-usb-printer/server.cc
// Function: run_server(UsbPrinter)
void run_server(UsbPrinter printer) {
  int listenfd = setup_server_socket();
  struct sockaddr_in server = bind_server_socket(listenfd);
  char address[INET_ADDRSTRLEN];

  if (inet_ntop(AF_INET, &server.sin_addr, address, INET_ADDRSTRLEN)) {
    printf("Bound server to address %s:%hu\n", address, server.sin_port);
  } else {
    printf("inet_ntop error : %s\n", strerror(errno));
    exit(1);
  }

  if (listen(listenfd, SOMAXCONN) < 0) {
    printf("listen error : %s\n", strerror(errno));
    exit(1);
  }

  while (1) {
    int connection = accept_connection(listenfd);
    int attached = 0;

    while (1) {
      if (!attached) {
        // Read in the header first in order to determine whether the request is
        // an OP_REQ_DEVLIST or an OP_REQ_IMPORT.
        OP_HEADER request;
        ssize_t received = recv(connection, &request, sizeof(request), 0);
        if (received != sizeof(request)) {
          printf("receive error : %s\n", strerror (errno));
          break;
        }

        request.command = ntohs(request.command);
        printf("Header Packet\n");
        printf("command: 0x%02X\n", request.command);

        // Request is OP_REQ_DEVLIST 
        if (request.command == OP_REQ_DEVLIST_CMD) {
          handle_device_list(printer, connection);
        // Request is OP_REQ_IMPORT
        } else if (request.command == OP_REQ_IMPORT_CMD) {
          if (!handle_attach(printer, connection)) {
            attached = 1;
          }
        }
      } else {
        printf("------------------------------------------------\n");
        printf("handles requests\n");
        USBIP_CMD_SUBMIT command;
        ssize_t received = recv(connection, &command, sizeof(command), 0);
        if (received != sizeof(command)) {
          printf("receive error : %s\n", strerror(errno));
          break;
        }

        unpack_usbip((int *)&command, sizeof(command));
        print_usbip_cmd_submit(command);

        if (command.command == 1) {
          printer.HandleUsbRequest(connection, command);
        }

        // Unlink URB
        if (command.command == 2) {
          printf("####################### Unlink URB %u  (not working!!!)\n",
                 command.transfer_flags);
          // FIXME
          /*
           USBIP_RET_UNLINK ret;
           printf("####################### Unlink URB %u\n",cmd.transfer_flags);
           ret.command=htonl(0x04);
           ret.devid=htonl(cmd.devid);
           ret.direction=htonl(cmd.direction);
           ret.ep=htonl(cmd.ep);
           ret.seqnum=htonl(cmd.seqnum);
           ret.status=htonl(1);

           if (send (sockfd, (char *)&ret, sizeof(USBIP_RET_UNLINK), 0) !=
           sizeof(USBIP_RET_UNLINK))
           {
             printf ("send error : %s \n", strerror (errno));
             exit(-1);
           }
          */
        }

        if (command.command > 2) {
          printf("Unknown USBIP cmd!\n");
          close(connection);
          return;
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x88(%rbp)
movq %rdi, -0x8(%rbp)
callq 0x6360
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
callq 0x63f0
movq %rax, -0x1c(%rbp)
movq %rdx, -0x14(%rbp)
leaq -0x1c(%rbp), %rsi
addq $0x4, %rsi
leaq -0x30(%rbp), %rdx
movl $0x2, %edi
movl $0x10, %ecx
callq 0x21b0
cmpq $0x0, %rax
je 0x6567
leaq -0x30(%rbp), %rsi
movzwl -0x1a(%rbp), %edx
leaq 0x2eac(%rip), %rdi      # 0x940a
movb $0x0, %al
callq 0x2050
jmp 0x658e
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq %rax, %rsi
leaq 0x2ead(%rip), %rdi      # 0x942a
movb $0x0, %al
callq 0x2050
movl $0x1, %edi
callq 0x21f0
movl -0xc(%rbp), %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x21d0
cmpl $0x0, %eax
jge 0x65c7
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq %rax, %rsi
leaq 0x2e8a(%rip), %rdi      # 0x9440
movb $0x0, %al
callq 0x2050
movl $0x1, %edi
callq 0x21f0
jmp 0x65c9
movl -0xc(%rbp), %edi
callq 0x6480
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x669d
movl -0x34(%rbp), %edi
leaq -0x40(%rbp), %rsi
movl $0x8, %edx
xorl %ecx, %ecx
callq 0x20c0
movq %rax, -0x48(%rbp)
cmpq $0x8, -0x48(%rbp)
je 0x6625
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq %rax, %rsi
leaq 0x2e3a(%rip), %rdi      # 0x9453
movb $0x0, %al
callq 0x2050
jmp 0x6769
movzwl -0x3e(%rbp), %edi
callq 0x22a0
movw %ax, -0x3e(%rbp)
leaq 0x2e2e(%rip), %rdi      # 0x9467
movb $0x0, %al
callq 0x2050
movzwl -0x3e(%rbp), %esi
leaq 0x2e2b(%rip), %rdi      # 0x9476
movb $0x0, %al
callq 0x2050
movzwl -0x3e(%rbp), %eax
cmpl $0x8005, %eax           # imm = 0x8005
jne 0x666e
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x2720
jmp 0x6698
movzwl -0x3e(%rbp), %eax
cmpl $0x8003, %eax           # imm = 0x8003
jne 0x6696
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x2920
cmpl $0x0, %eax
jne 0x6694
movl $0x1, -0x38(%rbp)
jmp 0x6696
jmp 0x6698
jmp 0x6764
leaq 0x2de3(%rip), %rdi      # 0x9487
movb $0x0, %al
callq 0x2050
leaq 0x2e07(%rip), %rdi      # 0x94b9
movb $0x0, %al
callq 0x2050
movl -0x34(%rbp), %edi
leaq -0x78(%rbp), %rsi
movl $0x30, %edx
xorl %ecx, %ecx
callq 0x20c0
movq %rax, -0x80(%rbp)
cmpq $0x30, -0x80(%rbp)
je 0x66f6
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq %rax, %rsi
leaq 0x2d66(%rip), %rdi      # 0x9453
movb $0x0, %al
callq 0x2050
jmp 0x6769
leaq -0x78(%rbp), %rdi
movl $0x30, %esi
callq 0x2b10
leaq -0x78(%rbp), %rdi
callq 0x2ba0
cmpl $0x1, -0x78(%rbp)
jne 0x6726
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %esi
leaq -0x78(%rbp), %rdx
callq 0x3a60
cmpl $0x2, -0x78(%rbp)
jne 0x673d
movl -0x64(%rbp), %esi
leaq 0x2d95(%rip), %rdi      # 0x94cb
movb $0x0, %al
callq 0x2050
cmpl $0x2, -0x78(%rbp)
jle 0x6762
leaq 0x2dba(%rip), %rdi      # 0x9504
movb $0x0, %al
callq 0x2050
movl -0x34(%rbp), %edi
callq 0x22b0
addq $0x90, %rsp
popq %rbp
retq
jmp 0x6764
jmp 0x65db
jmp 0x65c9
nop