// File: st0ne[P]XReflectorPlus/dxrfd.cpp
// Function: main
int main(int argc, char **argv)
{
   int rc = 0;
   struct sigaction act;
   inbound_type::iterator inbound_pos; 
   inbound *inbound_ptr;
   short int i;

   // R3ABM Patch
   if (argc != 2) /* 3 */
   {
      printf("Usage:  dxrfd <configFile>\n");
      printf("Example: dxrfd dxrfd.cfg\n");

      //printf("Usage:  dxrfd <configFile> <logFile>\n");
      //printf("Example: dxrfd dxrfd.cfg dxrfd.log\n");
      return 1;
   }

   // R3ABM Patch
   /*

   if (access(argv[2], F_OK) == 0)
   {
      printf("Log file %s exists, is dxrfd already running?, if not running, remove log file and restart\n", argv[2]);
      return 1;
   }
   */

   rc = regcomp(&preg,
        "^(([1-9][A-Z])|([A-Z][0-9])|([A-Z][A-Z][0-9]))[0-9A-Z]*[A-Z][ ]*[ A-RT-Z]$",
        REG_EXTENDED | REG_NOSUB);
   if (rc != REG_NOERROR)
   {
      traceit("The regular expression is NOT valid\n");
      return 1;
   }

   /* Open log file */
   // R3ABM Patch
   /*

   logfp = fopen(argv[2], "a");
   if (!logfp)
   {
      printf("Cant create log file %s, error=%d\n",
              argv[2],errno);
      return 1;
   }
   */

   tzset();

   /* line buffering only */
   // R3ABM Patch
   //setvbuf(logfp, (char *)NULL, _IOLBF, 0);

   act.sa_handler = sigCatch;
   sigemptyset(&act.sa_mask);
   act.sa_flags = SA_RESTART;
   if (sigaction(SIGTERM, &act, 0) != 0)
   {
      traceit("sigaction-TERM failed, error=%d\n", errno);
      return 1;
   }
   if (sigaction(SIGINT, &act, 0) != 0)
   {
      traceit("sigaction-INT failed, error=%d\n", errno);
      return 1;
   }

   do
   {
      /* process configuration file */
      rc = read_config(argv[1]);
      if (rc != 0)
      {
         traceit("Failed to process config file %s\n", argv[1]);
         break;
      }

    /* Open DB */
      rc = open_users(USERS);
      if (rc != 0)
      {
         traceit("Failed to open %s\n", USERS);
         break;
      }

      rc = open_blocks(BLOCKS);
      if (rc != 0)
      {
         traceit("Failed to open %s\n", BLOCKS);
         break;
      }

      /* get ready to accept incoming connections */
      rc = srv_open();
      if (rc != 0)
      {
         traceit("srv_open() failed\n");
         break;
      }

      /* get ready to accept shell commands from netcat */
      rc = cmd_open();
      if (rc != 0)
      {
         traceit("cmd_open() failed\n");
         break;
      }

      rc = ref_open();
      if (rc != 0)
      {
         traceit("ref_open() failed\n");
         break;
      }
  
      for (i = 0; i < 5; i++)
      {
         temp_x[i].s_addr = 0;
         memset(temp_x[i].hdr, 0, 56);

         temp_r[i].s_addr = 0;
         memset(temp_r[i].hdr, 0, 58);
      }

      traceit("dxrfd %s initialized...entering processing loop\n", VERSION);

      print_links_file();
      runit();
      traceit("Leaving processing loop...\n");

   } while (false);

   /* Close all handles */
   if (srv_sock != -1)
      close(srv_sock);

   if (cmd_sock != -1)
      close(cmd_sock);

   /* notify dvap, dvtool ... */
   if (ref_sock != -1)
   {
      refbuf[0] = 5;
      refbuf[1] = 0;
      refbuf[2] = 24;
      refbuf[3] = 0;
      refbuf[4] = 0;
      for (inbound_pos = inbound_list.begin(); inbound_pos != inbound_list.end(); inbound_pos++)
      {
         inbound_ptr = (inbound *)inbound_pos->second;
         sendto(ref_sock,(char *)refbuf,5,0,
                (struct sockaddr *)&(inbound_ptr->sin),
                sizeof(struct sockaddr_in));
      }
      inbound_list.clear();
      close(ref_sock);
   }

   /* Empty out the status file */
   statusfp = fopen(STATUS_FILE, "w");
   if (statusfp)
      fclose(statusfp);
   else
      traceit("Failed to empty out status file %s\n", STATUS_FILE);

   traceit("dxrfd exiting\n");
   // R3ABM Patch
   //fclose(logfp);
 
   return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x10840
cmpl $0x2, -0x8(%rbp)
je 0x391a
leaq 0x1370b(%rip), %rdi     # 0x17004
movb $0x0, %al
callq 0x3060
leaq 0x13719(%rip), %rdi     # 0x17020
movb $0x0, %al
callq 0x3060
movl $0x1, -0x4(%rbp)
jmp 0x3d3c
leaq 0x1bb6f(%rip), %rdi     # 0x1f490
leaq 0x13712(%rip), %rsi     # 0x1703a
movl $0x9, %edx
callq 0x3360
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3955
leaq 0x13743(%rip), %rdi     # 0x17085
movb $0x0, %al
callq 0x3820
movl $0x1, -0x4(%rbp)
jmp 0x3d3c
callq 0x3090
leaq 0x3ef(%rip), %rax       # 0x3d50
movq %rax, -0xb0(%rbp)
leaq -0xb0(%rbp), %rdi
addq $0x8, %rdi
callq 0x33e0
movl $0x10000000, -0x28(%rbp) # imm = 0x10000000
movl $0xf, %edi
leaq -0xb0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x33f0
cmpl $0x0, %eax
je 0x39ba
callq 0x3050
movl (%rax), %esi
leaq 0x13703(%rip), %rdi     # 0x170aa
movb $0x0, %al
callq 0x3820
movl $0x1, -0x4(%rbp)
jmp 0x3d3c
movl $0x2, %edi
leaq -0xb0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x33f0
cmpl $0x0, %eax
je 0x39f5
callq 0x3050
movl (%rax), %esi
leaq 0x136e9(%rip), %rdi     # 0x170cb
movb $0x0, %al
callq 0x3820
movl $0x1, -0x4(%rbp)
jmp 0x3d3c
jmp 0x39f7
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3d70
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3a28
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x136cf(%rip), %rdi     # 0x170eb
movb $0x0, %al
callq 0x3820
jmp 0x3bca
leaq 0x1baa1(%rip), %rdi     # 0x1f4d0
callq 0x4610
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3a57
leaq 0x136c9(%rip), %rdi     # 0x1710d
leaq 0x1ba85(%rip), %rsi     # 0x1f4d0
movb $0x0, %al
callq 0x3820
jmp 0x3bca
leaq 0x1ca82(%rip), %rdi     # 0x204e0
callq 0x49a0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3a86
leaq 0x1369a(%rip), %rdi     # 0x1710d
leaq 0x1ca66(%rip), %rsi     # 0x204e0
movb $0x0, %al
callq 0x3820
jmp 0x3bca
callq 0x4d50
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3aa7
leaq 0x13685(%rip), %rdi     # 0x17120
movb $0x0, %al
callq 0x3820
jmp 0x3bca
callq 0x4e60
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3ac8
leaq 0x13677(%rip), %rdi     # 0x17133
movb $0x0, %al
callq 0x3820
jmp 0x3bca
callq 0x4f70
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3ae9
leaq 0x13669(%rip), %rdi     # 0x17146
movb $0x0, %al
callq 0x3820
jmp 0x3bca
movw $0x0, -0xc2(%rbp)
movswl -0xc2(%rbp), %eax
cmpl $0x5, %eax
jge 0x3b9d
movswq -0xc2(%rbp), %rcx
leaq 0x1d9df(%rip), %rax     # 0x214f0
shlq $0x6, %rcx
addq %rcx, %rax
movl $0x0, (%rax)
movswq -0xc2(%rbp), %rax
leaq 0x1d9c3(%rip), %rdi     # 0x214f0
shlq $0x6, %rax
addq %rax, %rdi
addq $0x4, %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x31a0
movswq -0xc2(%rbp), %rcx
leaq 0x1dadd(%rip), %rax     # 0x21630
shlq $0x6, %rcx
addq %rcx, %rax
movl $0x0, (%rax)
movswq -0xc2(%rbp), %rax
leaq 0x1dac1(%rip), %rdi     # 0x21630
shlq $0x6, %rax
addq %rax, %rdi
addq $0x4, %rdi
xorl %esi, %esi
movl $0x3a, %edx
callq 0x31a0
movw -0xc2(%rbp), %ax
addw $0x1, %ax
movw %ax, -0xc2(%rbp)
jmp 0x3af2
leaq 0x135b5(%rip), %rdi     # 0x17159
leaq 0x135df(%rip), %rsi     # 0x1718a
movb $0x0, %al
callq 0x3820
callq 0x5070
callq 0x54e0
leaq 0x135cc(%rip), %rdi     # 0x1718f
movb $0x0, %al
callq 0x3820
cmpl $-0x1, 0x1b707(%rip)    # 0x1f2d8
je 0x3bde
movl 0x1b6ff(%rip), %edi     # 0x1f2d8
callq 0x3570
cmpl $-0x1, 0x1b6f7(%rip)    # 0x1f2dc
je 0x3bf2
movl 0x1b6ef(%rip), %edi     # 0x1f2dc
callq 0x3570
cmpl $-0x1, 0x1b6e7(%rip)    # 0x1f2e0
je 0x3ce1
movb $0x5, 0x1db6a(%rip)     # 0x21770
movb $0x0, 0x1db64(%rip)     # 0x21771
movb $0x18, 0x1db5e(%rip)    # 0x21772
movb $0x0, 0x1db58(%rip)     # 0x21773
movb $0x0, 0x1db52(%rip)     # 0x21774
leaq 0x1b727(%rip), %rdi     # 0x1f350
callq 0x10860
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq 0x1b706(%rip), %rdi     # 0x1f350
callq 0x108c0
movq %rax, -0xd8(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x10890
testb $0x1, %al
jne 0x3c6f
jmp 0x3cca
leaq -0xb8(%rbp), %rdi
callq 0x108f0
movq 0x20(%rax), %rax
movq %rax, -0xc0(%rbp)
movl 0x1b654(%rip), %edi     # 0x1f2e0
movq -0xc0(%rbp), %r8
addq $0x18, %r8
leaq 0x1dad2(%rip), %rsi     # 0x21770
movl $0x5, %edx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x3080
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
callq 0x10910
movq %rax, -0xe0(%rbp)
jmp 0x3c43
leaq 0x1b67f(%rip), %rdi     # 0x1f350
callq 0x10950
movl 0x1b604(%rip), %edi     # 0x1f2e0
callq 0x3570
leaq 0x1de88(%rip), %rdi     # 0x21b70
leaq 0x134bc(%rip), %rsi     # 0x171ab
callq 0x3380
movq %rax, 0x1ee7d(%rip)     # 0x22b78
cmpq $0x0, 0x1ee75(%rip)     # 0x22b78
je 0x3d13
movq 0x1ee6c(%rip), %rdi     # 0x22b78
callq 0x32d0
jmp 0x3d28
leaq 0x13493(%rip), %rdi     # 0x171ad
leaq 0x1de4f(%rip), %rsi     # 0x21b70
movb $0x0, %al
callq 0x3820
leaq 0x134a2(%rip), %rdi     # 0x171d1
movb $0x0, %al
callq 0x3820
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)