// File: dguglielmi[P]dlan-linux-software/common/packetinterface-linux.cpp
// Function: CPacketInterfaceLinux::ReceivePackets(IPacketReceiver&, unsigned long)
bool CPacketInterfaceLinux::ReceivePackets(IPacketReceiver& receiver, unsigned long ulTimeout)
{
  size_t buffersize = 3000;
  UnknownPacket packet(buffersize);

  bool infinite_timeout = (ulTimeout == (unsigned long)-1);
  struct timeval start, now, overall_timeout, select_timeout;

  if(!infinite_timeout)
  {
    overall_timeout.tv_sec = (long)(ulTimeout / 1000);
    overall_timeout.tv_usec = (long)(ulTimeout % 1000) * 1000;

    gettimeofday(&start, NULL);
    now = start;
  }
  bool stop_receiving = false;
  bool packet_indication = false;
  do
  {
    fd_set recset;
    int nfds;
    buildfdset(recset, nfds);

    if(!infinite_timeout)
      select_timeout = overall_timeout - (now - start);

    if(select(nfds, &recset, NULL, NULL, infinite_timeout ? 0 : &select_timeout) > 0)
    {
      std::map<CMACAddress,Interface>::const_iterator itIfc;
      if((itIfc = nextifcfromfdset(recset)) != m_ifcs.end())
      {
        struct sockaddr_ll source = { 0 };
        socklen_t sourcelen = sizeof(source);
        size_t recvbytes = (size_t) recvfrom(itIfc->second.fdsocket,
          (void*)packet.Data(), buffersize, 0, SOCKADDR_STAR(&source), &sourcelen);
        packet.SetSize(recvbytes);

        packet_indication = receiver.PacketIndication(itIfc->first, packet);
      }
      else if(FD_ISSET(m_interruptPipe[0], &recset))
      {
        char buffer[10];
        read(m_interruptPipe[0], buffer, sizeof(buffer));
        stop_receiving = true;
      }
    }

    if(!infinite_timeout)
      gettimeofday(&now, NULL);
  }
  while(!stop_receiving && !packet_indication && (infinite_timeout || ((now - start) < overall_timeout))); 

  return packet_indication;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq $0xbb8, -0x20(%rbp)     # imm = 0xBB8
movq -0x20(%rbp), %rsi
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x349d0
cmpq $-0x1, -0x18(%rbp)
sete %al
andb $0x1, %al
movb %al, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
jne 0x33d76
movq -0x18(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
imulq $0x3e8, %rdx, %rax      # imm = 0x3E8
movq %rax, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x54d0
movq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movb $0x0, -0x81(%rbp)
movb $0x0, -0x82(%rbp)
movq -0x1b8(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0x10c(%rbp), %rdx
callq 0x34130
jmp 0x33da0
testb $0x1, -0x39(%rbp)
jne 0x33e17
leaq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x33c60
movq %rax, -0x140(%rbp)
movq %rdx, -0x138(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x33c60
movq %rax, -0x130(%rbp)
movq %rdx, -0x128(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x33e17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x34aa0
jmp 0x3411a
movl -0x10c(%rbp), %eax
movl %eax, -0x1bc(%rbp)
testb $0x1, -0x39(%rbp)
je 0x33e34
xorl %eax, %eax
movq %rax, -0x1c8(%rbp)
jmp 0x33e41
leaq -0x80(%rbp), %rax
movq %rax, -0x1c8(%rbp)
jmp 0x33e41
movl -0x1bc(%rbp), %edi
movq -0x1c8(%rbp), %r8
xorl %eax, %eax
movl %eax, %ecx
leaq -0x108(%rbp), %rsi
movq %rcx, %rdx
callq 0x5310
movl %eax, -0x1cc(%rbp)
jmp 0x33e69
movl -0x1cc(%rbp), %eax
cmpl $0x0, %eax
jle 0x34064
leaq -0x148(%rbp), %rdi
callq 0x34940
movq -0x1b8(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x342e0
movq %rax, -0x1d8(%rbp)
jmp 0x33ea0
movq -0x1b8(%rbp), %rdi
movq -0x1d8(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
movq %rax, -0x148(%rbp)
addq $0x68, %rdi
callq 0x34670
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x34990
leaq -0x148(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x34960
testb $0x1, %al
jne 0x33f02
jmp 0x33fde
xorps %xmm0, %xmm0
movaps %xmm0, -0x180(%rbp)
movl $0x0, -0x170(%rbp)
movl $0x14, -0x184(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x349b0
movl 0x2c(%rax), %eax
movl %eax, -0x1e4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x34a60
movq %rax, -0x1e0(%rbp)
jmp 0x33f47
movq -0x1e0(%rbp), %rsi
movl -0x1e4(%rbp), %edi
movq -0x20(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x180(%rbp), %r8
leaq -0x184(%rbp), %r9
callq 0x5290
movq %rax, -0x1f0(%rbp)
jmp 0x33f76
movq -0x1f0(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x34a80
jmp 0x33f96
movq -0x10(%rbp), %rax
movq %rax, -0x200(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x349b0
movq -0x200(%rbp), %rdi
movq %rax, %rsi
movq (%rdi), %rax
movq (%rax), %rax
leaq -0x38(%rbp), %rdx
callq *%rax
movb %al, -0x1f1(%rbp)
jmp 0x33fcb
movb -0x1f1(%rbp), %al
andb $0x1, %al
movb %al, -0x82(%rbp)
jmp 0x34062
movq -0x1b8(%rbp), %rax
movl 0x98(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x1b8(%rbp), %rax
movslq %ecx, %rcx
movq -0x108(%rbp,%rcx,8), %rcx
movq %rcx, -0x208(%rbp)
movl 0x98(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x208(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x34060
movq -0x1b8(%rbp), %rax
movl 0x98(%rax), %edi
leaq -0x19a(%rbp), %rsi
movl $0xa, %edx
callq 0x5470
jmp 0x34059
movb $0x1, -0x81(%rbp)
jmp 0x34062
jmp 0x34064
testb $0x1, -0x39(%rbp)
jne 0x34077
leaq -0x60(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x54d0
jmp 0x34079
xorl %eax, %eax
testb $0x1, -0x81(%rbp)
movb %al, -0x209(%rbp)
jne 0x340e6
xorl %eax, %eax
testb $0x1, -0x82(%rbp)
movb %al, -0x209(%rbp)
jne 0x340e6
movb $0x1, %al
testb $0x1, -0x39(%rbp)
movb %al, -0x20a(%rbp)
jne 0x340da
leaq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x33c60
movq %rax, -0x1b0(%rbp)
movq %rdx, -0x1a8(%rbp)
leaq -0x1b0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x33c00
movb %al, -0x20a(%rbp)
movb -0x20a(%rbp), %al
movb %al, -0x209(%rbp)
movb -0x209(%rbp), %al
testb $0x1, %al
jne 0x33d84
movb -0x82(%rbp), %al
movb %al, -0x20b(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x34aa0
movb -0x20b(%rbp), %al
andb $0x1, %al
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x118(%rbp), %rdi
callq 0x5530
nopw %cs:(%rax,%rax)