// File: tenup-coin[P]seeder/dns.c
// Function: dnsserver
int dnsserver(dns_opt_t *opt) {
  struct sockaddr_in6 si_other;
  int senderSocket = -1;
  senderSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  if (senderSocket == -1) 
    return -3;

  int replySocket;
  if (listenSocket == -1) {
    struct sockaddr_in6 si_me;
    if ((listenSocket=socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP))==-1) {
      listenSocket = -1;
      return -1;
    }
    replySocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (replySocket == -1)
    {
      close(listenSocket);
      return -1;
    }
    int sockopt = 1;
    setsockopt(listenSocket, IPPROTO_IPV6, DSTADDR_SOCKOPT, &sockopt, sizeof sockopt);
    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin6_family = AF_INET6;
    si_me.sin6_port = htons(opt->port);
    si_me.sin6_addr = in6addr_any;
    if (bind(listenSocket, (struct sockaddr*)&si_me, sizeof(si_me))==-1)
      return -2;
  }
  
  unsigned char inbuf[BUFLEN], outbuf[BUFLEN];
  struct iovec iov[1] = {
    {
      .iov_base = inbuf,
      .iov_len = sizeof(inbuf),
    },
  };
  union control_data cmsg;
  struct msghdr msg = {
    .msg_name = &si_other,
    .msg_namelen = sizeof(si_other),
    .msg_iov = iov,
    .msg_iovlen = 1,
    .msg_control = &cmsg,
    .msg_controllen = sizeof(cmsg),
  };
  for (; 1; ++(opt->nRequests))
  {
    ssize_t insize = recvmsg(listenSocket, &msg, 0);
//    unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr;
//    printf("DNS: Request %llu from %i.%i.%i.%i:%i of %i bytes\n", (unsigned long long)(opt->nRequests), addr[0], addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize);
    if (insize <= 0)
      continue;

    ssize_t ret = dnshandle(opt, inbuf, insize, outbuf);
    if (ret <= 0)
      continue;

    bool handled = false;
    for (struct cmsghdr*hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr))
    {
      if (hdr->cmsg_level == IPPROTO_IP && hdr->cmsg_type == DSTADDR_SOCKOPT)
      {
        msg.msg_iov[0].iov_base = outbuf;
        msg.msg_iov[0].iov_len = ret;
        sendmsg(listenSocket, &msg, 0);
        msg.msg_iov[0].iov_base = inbuf;
        msg.msg_iov[0].iov_len = sizeof(inbuf);
        handled = true;
      }
    }
    if (!handled)
      sendto(listenSocket, outbuf, ret, 0, (struct sockaddr*)&si_other, sizeof(si_other));
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x500, %rsp            # imm = 0x500
movq %rdi, -0x10(%rbp)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x40f0
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x30(%rbp)
jne 0x4e1f
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x5120
cmpl $-0x1, 0x4566a(%rip)    # 0x4a490
jne 0x4f20
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x40f0
movl %eax, 0x4564a(%rip)     # 0x4a490
cmpl $-0x1, %eax
jne 0x4e61
movl $0xffffffff, 0x4563b(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5120
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x40f0
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
jne 0x4e95
movl 0x4560c(%rip), %edi     # 0x4a490
callq 0x48c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5120
movl $0x1, -0x54(%rbp)
movl 0x455ee(%rip), %edi     # 0x4a490
movl $0x29, %esi
movl $0x32, %edx
leaq -0x54(%rbp), %rcx
movl $0x4, %r8d
callq 0x4810
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x1c, %edx
callq 0x4270
movw $0xa, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movzwl %ax, %edi
callq 0x4080
movw %ax, -0x4e(%rbp)
movq 0x450ee(%rip), %rax     # 0x49fd8
movq (%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movl 0x45591(%rip), %edi     # 0x4a490
leaq -0x50(%rbp), %rsi
movl $0x1c, %edx
callq 0x40b0
cmpl $-0x1, %eax
jne 0x4f1e
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x5120
jmp 0x4f20
leaq -0x260(%rbp), %rax
movq %rax, -0x470(%rbp)
movq $0x200, -0x468(%rbp) # imm = 0x200
leaq -0x2c(%rbp), %rax
movq %rax, -0x4d0(%rbp)
movl $0x1c, -0x4c8(%rbp)
leaq -0x470(%rbp), %rax
movq %rax, -0x4c0(%rbp)
movq $0x1, -0x4b8(%rbp)
leaq -0x498(%rbp), %rax
movq %rax, -0x4b0(%rbp)
movq $0x28, -0x4a8(%rbp)
movl $0x0, -0x4a0(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0x4f95
jmp 0x5119
movl 0x454f5(%rip), %edi     # 0x4a490
leaq -0x4d0(%rbp), %rsi
xorl %edx, %edx
callq 0x4140
movq %rax, -0x4d8(%rbp)
cmpq $0x0, -0x4d8(%rbp)
jg 0x4fbf
jmp 0x5104
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movq -0x4d8(%rbp), %rdx
leaq -0x460(%rbp), %rcx
callq 0x5130
movq %rax, -0x4e0(%rbp)
cmpq $0x0, -0x4e0(%rbp)
jg 0x4ff3
jmp 0x5104
movb $0x0, -0x4e1(%rbp)
cmpq $0x10, -0x4a8(%rbp)
jb 0x5014
movq -0x4b0(%rbp), %rax
movq %rax, -0x4f8(%rbp)
jmp 0x501f
xorl %eax, %eax
movq %rax, -0x4f8(%rbp)
jmp 0x501f
movq -0x4f8(%rbp), %rax
movq %rax, -0x4f0(%rbp)
cmpq $0x0, -0x4f0(%rbp)
je 0x50d4
movq -0x4f0(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x50b3
movq -0x4f0(%rbp), %rax
cmpl $0x32, 0xc(%rax)
jne 0x50b3
leaq -0x460(%rbp), %rcx
movq -0x4c0(%rbp), %rax
movq %rcx, (%rax)
movq -0x4e0(%rbp), %rcx
movq -0x4c0(%rbp), %rax
movq %rcx, 0x8(%rax)
movl 0x45412(%rip), %edi     # 0x4a490
leaq -0x4d0(%rbp), %rsi
xorl %edx, %edx
callq 0x4410
leaq -0x260(%rbp), %rcx
movq -0x4c0(%rbp), %rax
movq %rcx, (%rax)
movq -0x4c0(%rbp), %rax
movq $0x200, 0x8(%rax)       # imm = 0x200
movb $0x1, -0x4e1(%rbp)
jmp 0x50b5
movq -0x4f0(%rbp), %rsi
leaq -0x4d0(%rbp), %rdi
callq 0x40e0
movq %rax, -0x4f0(%rbp)
jmp 0x502d
testb $0x1, -0x4e1(%rbp)
jne 0x5102
movl 0x453ad(%rip), %edi     # 0x4a490
leaq -0x460(%rbp), %rsi
movq -0x4e0(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x2c(%rbp), %r8
movl $0x1c, %r9d
callq 0x4090
jmp 0x5104
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x30(%rax)
jmp 0x4f8a
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x500, %rsp            # imm = 0x500
popq %rbp
retq
nopl (%rax)