// File: /workspace/github_cpp_makefile_star2_O2/Distrotech[P]stund/client.cxx
// Function: main
int
main(int argc, char* argv[])
{
   assert( sizeof(UInt8 ) == 1 );
   assert( sizeof(UInt16) == 2 );
   assert( sizeof(UInt32) == 4 );
    
   initNetwork();
    
   cout << "STUN client version " << STUN_VERSION << endl;
   
   int testNum = 0;
   bool verbose = false;
	
   StunAddress4 stunServerAddr;
   stunServerAddr.addr=0;

   int srcPort=0;
   StunAddress4 sAddr[MAX_NIC];
   int retval[MAX_NIC];
   int numNic=0;

   for ( int i=0; i<MAX_NIC; i++ )
   {
      sAddr[i].addr=0; 
      sAddr[i].port=0; 
      retval[i]=0;
   }
   
   for ( int arg = 1; arg<argc; arg++ )
   {
      if ( !strcmp( argv[arg] , "-v" ) )
      {
         verbose = true;
      }
      else if ( !strcmp( argv[arg] , "-i" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         if ( numNic >= MAX_NIC )
         {  
            cerr << "Can not have more than "<<  MAX_NIC <<" -i options" << endl;
            usage();
            exit(-1);
         }
         
         stunParseServerName(argv[arg], sAddr[numNic++]);
      }
      else if ( !strcmp( argv[arg] , "-p" ) )
      {
         arg++;
         if ( argc <= arg ) 
         {
            usage();
            exit(-1);
         }
         srcPort = strtol( argv[arg], NULL, 10);
      }
      else    
      {
        char* ptr;
        int t =  strtol( argv[arg], &ptr, 10 );
        if ( *ptr == 0 )
        { 
           // conversion worked
           testNum = t;
           cout << "running test number " << testNum  << endl; 
        }
        else
        {
           bool ret = stunParseServerName( argv[arg], stunServerAddr);
           if ( ret != true )
           {
              cerr << argv[arg] << " is not a valid host name " << endl;
              usage();
              exit(-1);
           }
	}	
      }
   }

   if ( srcPort == 0 )
   {
      srcPort = stunRandomPort();
   }
   
   if ( numNic == 0 )
   {
      // use default 
      numNic = 1;
   }
   
   for ( int nic=0; nic<numNic; nic++ )
   {
      sAddr[nic].port=srcPort;
      if ( stunServerAddr.addr == 0 )
      {
         usage();
         exit(-1);
      }
   
      if (testNum==0)
      {
         bool presPort=false;
         bool hairpin=false;
		
         NatType stype = stunNatType( stunServerAddr, verbose, &presPort, &hairpin, 
                                      srcPort, &sAddr[nic]);
		
         if ( nic == 0 )
         {
            cout << "Primary: ";
         }
         else
         {
            cout << "Secondary: ";
         }
         
         switch (stype)
         {
            case StunTypeFailure:
               cout << "Some stun error detetecting NAT type";
	       retval[nic] = -1;
               exit(-1);
               break;
            case StunTypeUnknown:
               cout << "Some unknown type error detetecting NAT type";
	       retval[nic] = 0xEE;
               break;
            case StunTypeOpen:
               cout << "Open";
	       retval[nic] = 0x00; 
               break;
            case StunTypeIndependentFilter:
               cout << "Independent Mapping, Independent Filter";
               if ( presPort ) cout << ", preserves ports"; else cout << ", random port";
               if ( hairpin  ) cout << ", will hairpin"; else cout << ", no hairpin";
               retval[nic] = 0x02;
               break;
            case StunTypeDependentFilter:
               cout << "Independent Mapping, Address Dependent Filter";
               if ( presPort ) cout << ", preserves ports"; else cout << ", random port";
               if ( hairpin  ) cout << ", will hairpin"; else cout << ", no hairpin";
               retval[nic] = 0x04;
               break;
            case StunTypePortDependedFilter:
               cout << "Independent Mapping, Port Dependent Filter";
               if ( presPort ) cout << ", preserves ports"; else cout << ", random port";
               if ( hairpin  ) cout << ", will hairpin"; else cout << ", no hairpin";
               retval[nic] = 0x06;
               break;
            case StunTypeDependentMapping:
               cout << "Dependent Mapping";
               if ( presPort ) cout << ", preserves ports"; else cout << ", random port";
               if ( hairpin  ) cout << ", will hairpin"; else cout << ", no hairpin";
               retval[nic] = 0x08;
               break;
            case StunTypeFirewall:
               cout << "Firewall";
               retval[nic] = 0x0A;
               break;
            case StunTypeBlocked:
               cout << "Blocked or could not reach STUN server";
               retval[nic] = 0x0C;
               break;
            default:
               cout << stype;
               cout << "Unkown NAT type";
               retval[nic] = 0x0E;  // Unknown NAT type
               break;
         }
         cout << "\t"; cout.flush();
         
         if (!hairpin)
         {
             retval[nic] |= 0x10;
         }       

         if (presPort)
         {
             retval[nic] |= 0x01;
         }
      }
      else if (testNum==100)
      {
         Socket myFd = openPort(srcPort,sAddr[nic].addr,verbose);
      
         StunMessage req;
         memset(&req, 0, sizeof(StunMessage));
      
         StunAtrString username;
         StunAtrString password;
         username.sizeValue = 0;
         password.sizeValue = 0;
      
         stunBuildReqSimple( &req, username, 
                             false , false , 
                             0x0c );
      
         char buf[STUN_MAX_MESSAGE_SIZE];
         int len = STUN_MAX_MESSAGE_SIZE;
      
         len = stunEncodeMessage( req, buf, len, password,verbose );
      
         if ( verbose )
         {
            cout << "About to send msg of len " << len 
                 << " to " << stunServerAddr << endl;
         }
      
         while (1)
         {
            for ( int i=0; i<100; i++ )
            {
               sendMessage( myFd,
                            buf, len, 
                            stunServerAddr.addr, 
                            stunServerAddr.port,verbose );
            }
#ifdef WIN32 // !cj! TODO - should fix this up in windows
            clock_t now = clock();
            assert( CLOCKS_PER_SEC == 1000 );
            while ( clock() <= now+10 ) { };
#else
            usleep(10*1000);
#endif
         }
      }
      else if (testNum==-2)
      {
         const int numPort = 5;
         int fd[numPort];
         StunAddress4 mappedAddr;
         
         for( int i=0; i<numPort; i++ )
         {
            fd[i] = stunOpenSocket( stunServerAddr, &mappedAddr,
                                    (srcPort==0)?0:(srcPort+i), &sAddr[nic],
                                    verbose );
            cout << "Got port at " << mappedAddr.port << endl;
         }
          
         for( int i=0; i<numPort; i++ )
         {
            closesocket(fd[i]);
         }
      }
      else if (testNum==-1)
      {
         int fd3,fd4;
         StunAddress4 mappedAddr;
         
         bool ok = stunOpenSocketPair(stunServerAddr,
                                      &mappedAddr,
                                      &fd3,
                                      &fd4,
                                      srcPort, 
                                      &sAddr[nic],
                                      verbose);
         if ( ok )
         {
            closesocket(fd3);
            closesocket(fd4);
            cout << "Got port pair at " << mappedAddr.port << endl;
         }
         else
         {
            cerr << "Opened a stun socket pair FAILED" << endl;
         }
      }
      else
      {
         stunTest( stunServerAddr,testNum,verbose,&(sAddr[nic]) );
      }
   } // end of for loop 
   cout << endl;
   
   UInt32 ret=0;
   for ( int i=numNic-1; i>=0; i-- )
   {
      if ( retval[i] == -1 )
      {
         ret = 0xFFFFFFFF;
         break;
      }
      ret = ret << 8;
      ret = ret | ( retval[i] & 0xFF );
   }
   
   cout << "Return value is " << hex << "0x";
   cout.fill('0');
   cout.width(6);
   cout << ret << dec << endl;
   cout.fill(' ');
   
   return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf48, %rsp            # imm = 0xF48
movq %rsi, %rbx
movl %edi, %ebp
callq 0x6d23
movq 0x8aab(%rip), %rdi      # 0xafd0
leaq 0x4c46(%rip), %rsi      # 0x7172
callq 0x21d0
leaq 0x4c4f(%rip), %rsi      # 0x7187
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20c0
andl $0x0, 0x20(%rsp)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x256a
andl $0x0, 0x54(%rsp,%rax,8)
andw $0x0, 0x50(%rsp,%rax,8)
andl $0x0, 0x3c(%rsp,%rax,4)
incq %rax
jmp 0x254f
movq $0x0, 0x28(%rsp)
pushq $0x1
popq %r12
movq $0x0, 0x10(%rsp)
movq $0x0, 0x30(%rsp)
xorl %r13d, %r13d
cmpl %ebp, %r12d
jge 0x26a9
movl %r13d, %r14d
movslq %r12d, %r13
movq (%rbx,%r13,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x25d0
cmpb $0x76, 0x1(%rdi)
jne 0x25b4
cmpb $0x0, 0x2(%rdi)
je 0x2636
cmpb $0x69, 0x1(%rdi)
jne 0x25c0
cmpb $0x0, 0x2(%rdi)
je 0x263f
cmpb $0x70, 0x1(%rdi)
jne 0x25d0
cmpb $0x0, 0x2(%rdi)
je 0x267c
leaq 0x640(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x2220
movq %rax, %r15
movq 0x640(%rsp), %rax
cmpb $0x0, (%rax)
je 0x260b
movq (%rbx,%r13,8), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x43e1
testb %al, %al
jne 0x269e
jmp 0x2ce1
movq 0x89be(%rip), %rdi      # 0xafd0
leaq 0x4b97(%rip), %rsi      # 0x71b0
callq 0x21d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x22a0
movq %rax, %rdi
callq 0x20c0
movl %r15d, %r14d
jmp 0x269e
movb $0x1, %al
movq %rax, 0x30(%rsp)
jmp 0x269e
incl %r12d
cmpl %ebp, %r12d
jge 0x2d08
movq 0x28(%rsp), %rcx
cmpl $0x3, %ecx
jge 0x2d15
movl %r14d, %r13d
movslq %r12d, %rax
movq (%rbx,%rax,8), %rdi
movslq %ecx, %rax
incl %ecx
movq %rcx, 0x28(%rsp)
leaq (%rsp,%rax,8), %rsi
addq $0x50, %rsi
callq 0x43e1
jmp 0x26a1
incl %r12d
cmpl %ebp, %r12d
jge 0x2d08
movslq %r12d, %rax
movq (%rbx,%rax,8), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2220
movq %rax, 0x10(%rsp)
movl %r14d, %r13d
incl %r12d
jmp 0x258c
movq 0x10(%rsp), %rbp
testl %ebp, %ebp
jne 0x26b9
callq 0x40cb
movl %eax, %ebp
movq 0x28(%rsp), %rax
cmpl $0x1, %eax
adcl $0x0, %eax
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %r14d
cmovlel %ecx, %eax
movq %rax, 0x48(%rsp)
movl %ebp, %r12d
movzbl 0x30(%rsp), %r15d
andl $0x1, %r15d
xorl %ebx, %ebx
movl %r13d, 0x24(%rsp)
movq %rbp, 0x10(%rsp)
cmpq 0x48(%rsp), %rbx
je 0x2afb
movw %bp, 0x50(%rsp,%rbx,8)
cmpl $0x0, 0x20(%rsp)
je 0x2d08
leaq (%rsp,%rbx,8), %rbp
addq $0x50, %rbp
cmpl $-0x2, %r13d
je 0x27bb
cmpl $-0x1, %r13d
je 0x274d
testl %r13d, %r13d
je 0x2849
cmpl $0x64, %r13d
je 0x2be6
leaq 0x1c(%rsp), %rdi
movl %r13d, %esi
movl %r15d, %edx
movq %rbp, %rcx
callq 0x5709
jmp 0x2aee
movl %r15d, (%rsp)
leaq 0x1c(%rsp), %rdi
leaq 0x640(%rsp), %rsi
leaq 0x178(%rsp), %rdx
leaq 0x74(%rsp), %rcx
movl %r12d, %r8d
movq %rbp, %r9
callq 0x64ec
testb %al, %al
je 0x28ce
movl 0x178(%rsp), %edi
callq 0x22c0
movl 0x74(%rsp), %edi
callq 0x22c0
movq 0x8836(%rip), %rdi      # 0xafd0
leaq 0x4be9(%rip), %rsi      # 0x738a
callq 0x21d0
movzwl 0x640(%rsp), %esi
movq %rax, %rdi
callq 0x2290
jmp 0x28e1
xorl %r13d, %r13d
cmpq $0x5, %r13
je 0x281f
leal (%r12,%r13), %edx
testq %r12, %r12
cmovel %r14d, %edx
leaq 0x1c(%rsp), %rdi
leaq 0x178(%rsp), %rsi
movq %rbp, %rcx
movl %r15d, %r8d
callq 0x6375
movl %eax, 0x640(%rsp,%r13,4)
movq 0x87da(%rip), %rdi      # 0xafd0
leaq 0x4b80(%rip), %rsi      # 0x737d
callq 0x21d0
movzwl 0x178(%rsp), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
callq 0x20c0
incq %r13
jmp 0x27be
xorl %r13d, %r13d
movq 0x10(%rsp), %rbp
cmpq $0x5, %r13
je 0x283f
movl 0x640(%rsp,%r13,4), %edi
callq 0x22c0
incq %r13
jmp 0x2827
movl 0x24(%rsp), %r13d
jmp 0x2af3
xorl %eax, %eax
movb %al, 0x640(%rsp)
xorl %r14d, %r14d
movb %al, 0x178(%rsp)
leaq 0x1c(%rsp), %rdi
movl %r15d, %esi
leaq 0x640(%rsp), %rdx
leaq 0x178(%rsp), %rcx
movl %r12d, %r8d
movq %rbp, %r9
callq 0x5ad5
movl %eax, %ebp
testq %rbx, %rbx
leaq 0x495f(%rip), %rsi      # 0x71ea
leaq 0x494e(%rip), %rax      # 0x71e0
cmoveq %rax, %rsi
movq 0x8733(%rip), %rdi      # 0xafd0
callq 0x21d0
cmpl $0x8, %ebp
ja 0x2a89
movl %ebp, %eax
leaq 0x4750(%rip), %rcx      # 0x7004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xee, %ebp
leaq 0x4952(%rip), %rsi      # 0x721b
jmp 0x29cb
movq 0x871b(%rip), %rdi      # 0xaff0
leaq 0x4ac0(%rip), %rsi      # 0x739c
callq 0x21d0
movq 0x10(%rsp), %rbp
movq %rax, %rdi
callq 0x20c0
jmp 0x2af3
movq 0x86d6(%rip), %rbp      # 0xafd0
movq %rbp, %rdi
leaq 0x49ad(%rip), %rsi      # 0x72b1
callq 0x21d0
cmpb $0x0, 0x640(%rsp)
leaq 0x496f(%rip), %rsi      # 0x7287
leaq 0x4956(%rip), %rax      # 0x7275
cmovneq %rax, %rsi
movq %rbp, %r13
movq %rbp, %rdi
callq 0x21d0
cmpb $0x0, 0x178(%rsp)
leaq 0x4967(%rip), %rsi      # 0x72a4
leaq 0x4951(%rip), %rax      # 0x7295
cmovneq %rax, %rsi
pushq $0x4
jmp 0x2aa3
xorl %ebp, %ebp
leaq 0x48f0(%rip), %rsi      # 0x7248
jmp 0x29cb
movq 0x866f(%rip), %rbp      # 0xafd0
movq %rbp, %rdi
leaq 0x48e2(%rip), %rsi      # 0x724d
callq 0x21d0
cmpb $0x0, 0x640(%rsp)
leaq 0x4908(%rip), %rsi      # 0x7287
leaq 0x48ef(%rip), %rax      # 0x7275
cmovneq %rax, %rsi
movq %rbp, %r13
movq %rbp, %rdi
callq 0x21d0
cmpb $0x0, 0x178(%rsp)
leaq 0x4900(%rip), %rsi      # 0x72a4
leaq 0x48ea(%rip), %rax      # 0x7295
cmovneq %rax, %rsi
pushq $0x2
jmp 0x2aa3
leaq 0x4968(%rip), %rsi      # 0x7325
pushq $0xc
jmp 0x29ca
leaq 0x4954(%rip), %rsi      # 0x731c
pushq $0xa
popq %rbp
movq 0x85fe(%rip), %r13      # 0xafd0
jmp 0x2aa4
movq 0x85f2(%rip), %rbp      # 0xafd0
movq %rbp, %rdi
leaq 0x48f7(%rip), %rsi      # 0x72df
callq 0x21d0
cmpb $0x0, 0x640(%rsp)
leaq 0x488b(%rip), %rsi      # 0x7287
leaq 0x4872(%rip), %rax      # 0x7275
cmovneq %rax, %rsi
movq %rbp, %r13
movq %rbp, %rdi
callq 0x21d0
cmpb $0x0, 0x178(%rsp)
leaq 0x4883(%rip), %rsi      # 0x72a4
leaq 0x486d(%rip), %rax      # 0x7295
cmovneq %rax, %rsi
pushq $0x6
jmp 0x2aa3
movq 0x8599(%rip), %rbp      # 0xafd0
movq %rbp, %rdi
leaq 0x48c9(%rip), %rsi      # 0x730a
callq 0x21d0
cmpb $0x0, 0x640(%rsp)
leaq 0x4832(%rip), %rsi      # 0x7287
leaq 0x4819(%rip), %rax      # 0x7275
cmovneq %rax, %rsi
movq %rbp, %r13
movq %rbp, %rdi
callq 0x21d0
cmpb $0x0, 0x178(%rsp)
leaq 0x482a(%rip), %rsi      # 0x72a4
leaq 0x4814(%rip), %rax      # 0x7295
cmovneq %rax, %rsi
pushq $0x8
jmp 0x2aa3
movq 0x8540(%rip), %rdi      # 0xafd0
movq %rdi, %r13
movl %ebp, %esi
callq 0x22a0
leaq 0x48ab(%rip), %rsi      # 0x734c
pushq $0xe
popq %rbp
movq %r13, %rdi
callq 0x21d0
movl %ebp, 0x3c(%rsp,%rbx,4)
movq %r13, %rdi
leaq 0x48a2(%rip), %rsi      # 0x735c
callq 0x21d0
movq %r13, %rdi
callq 0x2140
cmpb $0x0, 0x178(%rsp)
jne 0x2ad8
orl $0x10, %ebp
movl %ebp, 0x3c(%rsp,%rbx,4)
movl 0x24(%rsp), %r13d
cmpb $0x1, 0x640(%rsp)
jne 0x2aee
orl $0x1, %ebp
movl %ebp, 0x3c(%rsp,%rbx,4)
movq 0x10(%rsp), %rbp
incq %rbx
jmp 0x26ef
movq 0x84ce(%rip), %rbx      # 0xafd0
movq %rbx, %rdi
callq 0x20c0
movq 0x28(%rsp), %rdx
cmpl $0x1, %edx
adcl $0x0, %edx
xorl %ebp, %ebp
pushq $-0x1
popq %rax
testl %edx, %edx
jle 0x2b36
movl 0x38(%rsp,%rdx,4), %ecx
cmpl $-0x1, %ecx
je 0x2b34
shll $0x8, %ebp
movzbl %cl, %ecx
orl %ecx, %ebp
decq %rdx
jmp 0x2b1a
movl %eax, %ebp
leaq 0x4880(%rip), %rsi      # 0x73bd
movq %rbx, %rdi
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x486d(%rip), %rsi      # 0x73ce
movq %rax, %rdi
callq 0x21d0
movq (%rbx), %rax
movq -0x18(%rax), %r14
leaq (%rbx,%r14), %rdi
callq 0x20e0
movb $0x30, 0xe0(%rbx,%r14)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%rbx,%rax)
movl %ebp, %esi
movq %rbx, %rdi
callq 0x2130
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
callq 0x20c0
movq (%rbx), %rax
movq -0x18(%rax), %r14
leaq (%rbx,%r14), %rdi
callq 0x20e0
movb $0x20, 0xe0(%rbx,%r14)
movl %ebp, %eax
addq $0xf48, %rsp            # imm = 0xF48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rbp), %esi
movzwl 0x10(%rsp), %edi
movl %r15d, %edx
callq 0x67f4
movl %eax, %ebx
leaq 0x178(%rsp), %r14
xorl %r13d, %r13d
movl $0x4c8, %edx            # imm = 0x4C8
movq %r14, %rdi
xorl %esi, %esi
callq 0x2110
leaq 0x74(%rsp), %rsi
andw $0x0, 0x100(%rsi)
leaq 0xe46(%rsp), %r12
andw $0x0, 0x100(%r12)
pushq $0xc
popq %r8
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x562c
leaq 0x640(%rsp), %rsi
movq %r14, %rdi
movl $0x800, %edx            # imm = 0x800
movq %r12, %rcx
movl %r15d, %r8d
callq 0x3849
movl %eax, %ebp
testb $0x1, 0x30(%rsp)
je 0x2ca6
movq 0x8364(%rip), %rdi      # 0xafd0
leaq 0x46eb(%rip), %rsi      # 0x735e
callq 0x21d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x22a0
leaq 0x46ef(%rip), %rsi      # 0x7378
movq %rax, %rdi
callq 0x21d0
leaq 0x1c(%rsp), %rsi
movq %rax, %rdi
callq 0x3732
movq %rax, %rdi
callq 0x20c0
leaq 0x640(%rsp), %r14
cmpl $0x64, %r13d
je 0x2cd2
movl 0x20(%rsp), %ecx
movzwl 0x1c(%rsp), %r8d
movl %ebx, %edi
movq %r14, %rsi
movl %ebp, %edx
movl %r15d, %r9d
callq 0x6b66
incl %r13d
jmp 0x2cae
movl $0x2710, %edi           # imm = 0x2710
callq 0x2240
xorl %r13d, %r13d
jmp 0x2cae
movq (%rbx,%r13,8), %rsi
movq 0x8304(%rip), %rdi      # 0xaff0
callq 0x21d0
leaq 0x44cd(%rip), %rsi      # 0x71c5
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20c0
callq 0x244c
pushq $-0x1
popq %rdi
callq 0x2210
movq 0x82d4(%rip), %rdi      # 0xaff0
leaq 0x4469(%rip), %rsi      # 0x718c
callq 0x21d0
pushq $0x3
popq %rsi
movq %rax, %rdi
callq 0x22a0
leaq 0x446a(%rip), %rsi      # 0x71a4
jmp 0x2cf8
movq 0x828d(%rip), %rdi      # 0xafd0
leaq 0x44ac(%rip), %rsi      # 0x71f6
callq 0x21d0
jmp 0x2d0d
nopl (%rax)