// File: Distrotech[P]stund/stun.cxx
// Function: stunFindLocalInterfaces(unsigned int*, int)
int 
stunFindLocalInterfaces(UInt32* addresses,int maxRet)
{
#if defined(WIN32) || defined(__sparc__)
   return 0;
#else
   struct ifconf ifc;
	
   int s = socket( AF_INET, SOCK_DGRAM, 0 );
   int len = 100 * sizeof(struct ifreq);
	
   char buf[ len ];
	
   ifc.ifc_len = len;
   ifc.ifc_buf = buf;
	
   int e = ioctl(s,SIOCGIFCONF,&ifc);
   char *ptr = buf;
   int tl = ifc.ifc_len;
   int count=0;
	
   while ( (tl > 0) && ( count < maxRet) )
   {
      struct ifreq* ifr = (struct ifreq *)ptr;
		
      int si = sizeof(ifr->ifr_name) + sizeof(struct sockaddr);
      tl -= si;
      ptr += si;
      //char* name = ifr->ifr_ifrn.ifrn_name;
      //cerr << "name = " << name << endl;
		
      struct ifreq ifr2;
      ifr2 = *ifr;
		
      e = ioctl(s,SIOCGIFADDR,&ifr2);
      if ( e == -1 )
      {
         break;
      }
		
      //cerr << "ioctl addr e = " << e << endl;
		
      struct sockaddr a = ifr2.ifr_addr;
      struct sockaddr_in* addr = (struct sockaddr_in*) &a;
		
      UInt32 ai = ntohl( addr->sin_addr.s_addr );
      if (int((ai>>24)&0xFF) != 127)
      {
         addresses[count++] = ai;
      }
		
#if 0
      cerr << "Detected interface "
           << int((ai>>24)&0xFF) << "." 
           << int((ai>>16)&0xFF) << "." 
           << int((ai>> 8)&0xFF) << "." 
           << int((ai    )&0xFF) << endl;
#endif
   }
	
   closesocket(s);
	
   return count;
#endif
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x20a0
movl %eax, -0x24(%rbp)
movl $0xfa0, -0x28(%rbp)     # imm = 0xFA0
movl -0x28(%rbp), %eax
movl %eax, %ecx
movq %rsp, %rax
movq %rax, -0x30(%rbp)
movq %rcx, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rax
subq %rdx, %rax
movq %rax, -0xb0(%rbp)
movq %rax, %rsp
movq %rcx, -0x38(%rbp)
movl -0x28(%rbp), %ecx
movl %ecx, -0x20(%rbp)
movq %rax, -0x18(%rbp)
movl -0x24(%rbp), %edi
movl $0x8912, %esi           # imm = 0x8912
leaq -0x20(%rbp), %rdx
movb $0x0, %al
callq 0x2290
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movl %ecx, -0x3c(%rbp)
movq %rax, -0x48(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x4c(%rbp)
movb %al, -0xb1(%rbp)
jle 0x75cc
movl -0x50(%rbp), %eax
cmpl -0xc(%rbp), %eax
setl %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x75db
jmp 0x76a8
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x20, -0x5c(%rbp)
movl -0x5c(%rbp), %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
movl -0x5c(%rbp), %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movl $0x28, %edx
callq 0x2190
movl -0x24(%rbp), %edi
movl $0x8915, %esi           # imm = 0x8915
leaq -0x88(%rbp), %rdx
movb $0x0, %al
callq 0x2290
movl %eax, -0x3c(%rbp)
cmpl $-0x1, -0x3c(%rbp)
jne 0x763c
jmp 0x76a8
movq -0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x2250
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
cmpl $0x7f, %eax
je 0x76a3
movl -0xa4(%rbp), %edx
movq -0x8(%rbp), %rax
movl -0x50(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x50(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x75af
movl -0x24(%rbp), %edi
callq 0x31c0
movl -0x50(%rbp), %eax
movq -0x30(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop