// File: /workspace/github_cpp_makefile_star2_O2/Distrotech[P]stund/server.cxx
// Function: main
int
main(int argc, char* argv[])
{
   assert( sizeof(UInt8 ) == 1 );
   assert( sizeof(UInt16) == 2 );
   assert( sizeof(UInt32) == 4 );

   initNetwork();

   clog << "STUN server version "  <<  STUN_VERSION << endl;
      
   StunAddress4 myAddr;
   StunAddress4 altAddr;
   bool verbose=false;
   bool background=false;
   
   myAddr.addr = 0;
   altAddr.addr = 0;
   myAddr.port = STUN_PORT;
   altAddr.port = STUN_PORT+1;
   int myPort = 0;
   int altPort = 0;
   int myMediaPort = 0;
   
   UInt32 interfaces[10];
   int numInterfaces = stunFindLocalInterfaces(interfaces,10);

   if (numInterfaces == 2)
   {
      myAddr.addr = interfaces[0];
      myAddr.port = STUN_PORT;
      altAddr.addr = interfaces[1];
      altAddr.port = STUN_PORT+1;
   }

   for ( int arg = 1; arg<argc; arg++ )
   {
      if ( !strcmp( argv[arg] , "-v" ) )
      {
         verbose = true;
      }
      else if ( !strcmp( argv[arg] , "-b" ) )
      {
         background = true;
      }
      else if ( !strcmp( argv[arg] , "-h" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         stunParseServerName(argv[arg], myAddr);
      }
      else if ( !strcmp( argv[arg] , "-a" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         stunParseServerName(argv[arg], altAddr);
      }
      else if ( !strcmp( argv[arg] , "-p" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         myPort = UInt16(strtol( argv[arg], NULL, 10));
      }
      else if ( !strcmp( argv[arg] , "-o" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         altPort = UInt16(strtol( argv[arg], NULL, 10));
      }
      else if ( !strcmp( argv[arg] , "-m" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         myMediaPort = UInt16(strtol( argv[arg], NULL, 10));
      }
      else
      {
         usage();
         exit(-1);
      }
   }

   if ( myPort != 0 )
   {
      myAddr.port = myPort;
   }
   if ( altPort != 0 )
   {
      altAddr.port = altPort;
   }
   
   if (  (myAddr.addr == 0) || (altAddr.addr == 0) )
   {
      clog << "If your machine does not have exactly two ethernet interfaces, "
           << "you must specify the server and alt server" << endl;
      //usage();
      //exit(-1);
   }

   if ( myAddr.port == altAddr.port )
   {
      altAddr.port = myAddr.port+1;
   }

   if ( verbose )
   {
      clog << "Running with on interface "
           << myAddr << " with alternate " 
           << altAddr << endl;
   }
    
   if (
      ( myAddr.addr  == 0 ) ||
      ( myAddr.port  == 0 ) ||
      //( altAddr.addr == 0 ) ||
      ( altAddr.port == 0 ) 
      )
   {
      cerr << "Bad command line" << endl;
      exit(1);
   }
   
   if ( altAddr.addr == 0 )
   {
      cerr << "Warning - no alternate ip address STUN will not work" << endl;
      //exit(1);
   }
   
#if defined(WIN32)
   int pid=0;

   if ( background )
   {
      cerr << "The -b background option does not work in windows" << endl;
      exit(-1);
   }
#else
   pid_t pid=0;

   if ( background )
   {
      pid = fork();

      if (pid < 0)
      {
         cerr << "fork: unable to fork" << endl;
         exit(-1);
      }
   }
#endif

   if (pid == 0) //child or not using background
   {
      StunServerInfo info;
      bool ok = stunInitServer(info, myAddr, altAddr, myMediaPort, verbose);
      
      int c=0;
      while (ok)
      {
         ok = stunServerProcess(info, verbose);
         c++;
         if ( verbose && (c%1000 == 0) ) 
         {
            clog << "*";
         }
      }
      // Notreached
   }
   
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f68, %rsp           # imm = 0x2F68
movq %rsi, %r14
movl %edi, %ebp
callq 0x693b
movq 0x8a29(%rip), %rdi      # 0xafc0
leaq 0x4ca3(%rip), %rsi      # 0x7241
callq 0x21c0
leaq 0x4cac(%rip), %rsi      # 0x7256
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
andl $0x0, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
movw $0xd96, 0x14(%rsp)      # imm = 0xD96
movw $0xd97, 0xc(%rsp)       # imm = 0xD97
leaq 0x30(%rsp), %rdi
pushq $0xa
popq %rsi
callq 0x515e
cmpl $0x2, %eax
jne 0x2602
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
movl %eax, 0x18(%rsp)
movw $0xd96, 0x14(%rsp)      # imm = 0xD96
movl %ecx, 0x10(%rsp)
movw $0xd97, 0xc(%rsp)       # imm = 0xD97
pushq $0x1
popq %r15
movq $0x0, 0x28(%rsp)
pushq $0xa
popq %r13
xorl %r12d, %r12d
movl $0x0, 0x1c(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
cmpl %ebp, %r15d
jge 0x277b
movslq %r15d, %rax
movq (%r14,%rax,8), %rax
cmpb $0x2d, (%rax)
jne 0x2923
cmpb $0x76, 0x1(%rax)
jne 0x2652
cmpb $0x0, 0x2(%rax)
je 0x26db
cmpb $0x62, 0x1(%rax)
jne 0x2662
cmpb $0x0, 0x2(%rax)
je 0x26e7
cmpb $0x68, 0x1(%rax)
jne 0x2672
cmpb $0x0, 0x2(%rax)
je 0x26f3
cmpb $0x61, 0x1(%rax)
jne 0x2682
cmpb $0x0, 0x2(%rax)
je 0x270d
cmpb $0x70, 0x1(%rax)
jne 0x2692
cmpb $0x0, 0x2(%rax)
je 0x272c
cmpb $0x6f, 0x1(%rax)
jne 0x26a2
cmpb $0x0, 0x2(%rax)
je 0x274f
cmpb $0x6d, 0x1(%rax)
jne 0x2923
cmpb $0x0, 0x2(%rax)
jne 0x2923
incl %r15d
cmpl %ebp, %r15d
jge 0x2923
movslq %r15d, %rax
movq (%r14,%rax,8), %rdi
xorl %esi, %esi
movl %r13d, %edx
callq 0x2210
movzwl %ax, %ebx
jmp 0x2773
movb $0x1, %al
movq %rax, 0x20(%rsp)
jmp 0x2773
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x2773
incl %r15d
cmpl %ebp, %r15d
jge 0x2923
movslq %r15d, %rax
movq (%r14,%rax,8), %rdi
leaq 0x14(%rsp), %rsi
jmp 0x2725
incl %r15d
cmpl %ebp, %r15d
jge 0x2923
movslq %r15d, %rax
movq (%r14,%rax,8), %rdi
leaq 0xc(%rsp), %rsi
callq 0x3ff9
jmp 0x2773
incl %r15d
cmpl %ebp, %r15d
jge 0x2923
movslq %r15d, %rax
movq (%r14,%rax,8), %rdi
xorl %esi, %esi
movl %r13d, %edx
callq 0x2210
movzwl %ax, %r12d
jmp 0x2773
incl %r15d
cmpl %ebp, %r15d
jge 0x2923
movslq %r15d, %rax
movq (%r14,%rax,8), %rdi
xorl %esi, %esi
movl %r13d, %edx
callq 0x2210
movzwl %ax, %eax
movl %eax, 0x1c(%rsp)
incl %r15d
jmp 0x2629
testl %r12d, %r12d
je 0x2786
movw %r12w, 0x14(%rsp)
movl 0x1c(%rsp), %eax
testl %eax, %eax
je 0x2793
movw %ax, 0xc(%rsp)
cmpl $0x0, 0x18(%rsp)
je 0x27a1
cmpl $0x0, 0x10(%rsp)
jne 0x27cb
movq 0x8818(%rip), %rdi      # 0xafc0
leaq 0x4aac(%rip), %rsi      # 0x725b
callq 0x21c0
leaq 0x4ae0(%rip), %rsi      # 0x729b
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
movzwl 0x14(%rsp), %eax
movzwl 0xc(%rsp), %ecx
cmpw %cx, %ax
jne 0x27e2
leal 0x1(%rax), %ecx
movw %cx, 0xc(%rsp)
testb $0x1, 0x20(%rsp)
je 0x283b
movq 0x87d0(%rip), %rdi      # 0xafc0
leaq 0x4acf(%rip), %rsi      # 0x72c6
callq 0x21c0
leaq 0x14(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x334a
leaq 0x4ace(%rip), %rsi      # 0x72e1
movq %rax, %rdi
callq 0x21c0
leaq 0xc(%rsp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x334a
movq %rax, %rdi
callq 0x20d0
movzwl (%r14), %eax
movzwl (%r15), %ecx
cmpl $0x0, 0x18(%rsp)
je 0x2930
testw %ax, %ax
je 0x2930
testw %cx, %cx
je 0x2930
cmpl $0x0, 0x10(%rsp)
jne 0x287a
movq 0x878a(%rip), %rdi      # 0xaff0
leaq 0x4a96(%rip), %rsi      # 0x7303
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
testb $0x1, 0x28(%rsp)
je 0x28a4
callq 0x2090
testl %eax, %eax
js 0x294f
je 0x28a4
xorl %eax, %eax
addq $0x2f68, %rsp           # imm = 0x2F68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x20(%rsp), %ebp
andl $0x1, %ebp
leaq 0x60(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movl %ebx, %ecx
movl %ebp, %r8d
callq 0x4758
movl %eax, %r13d
pushq $-0x1
popq %r15
pushq $0x1
popq %rbx
movl $0x3e8, %r12d           # imm = 0x3E8
movl %ebx, %eax
xorl %edx, %edx
divl %r12d
testb $0x1, %r13b
je 0x2890
imull $0x3e8, %eax, %r14d     # imm = 0x3E8
addl %r15d, %r14d
leaq 0x60(%rsp), %rdi
movl %ebp, %esi
callq 0x49a2
movl %eax, %r13d
testl %r14d, %r14d
sete %al
andb 0x20(%rsp), %al
cmpb $0x1, %al
jne 0x291c
movq 0x86b0(%rip), %rdi      # 0xafc0
leaq 0x4a36(%rip), %rsi      # 0x734d
callq 0x21c0
decl %r15d
incl %ebx
jmp 0x28d5
callq 0x243c
pushq $-0x1
popq %rdi
callq 0x2200
movq 0x86b9(%rip), %rdi      # 0xaff0
leaq 0x49b4(%rip), %rsi      # 0x72f2
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
pushq $0x1
jmp 0x292a
movq 0x869a(%rip), %rdi      # 0xaff0
leaq 0x49db(%rip), %rsi      # 0x7338
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
jmp 0x2928