title: Packet sniffer only detects sent packets
body: <p>I have been working on a packet sniffer just for fun/education and it's been going quite well. The problem that I'm having is that the only packets appearing in <code>recvfrom()</code> are SENT packets i.e. packets with my IP as their source IP and not my WAN IP but my LAN IP (if that makes sense).
  I tried fixing it and spent hours reading everything I could find but to me there seems no solution.
  I am 100% positive that the code I've written does not detect all packets because wireshark detects way more (ingoing and outgoing) and it's obvious that there is not only outgoing traffic from my PC.</p>
  
  <pre><code>packetsize = recvfrom(sniffer , Buffer , 65536 , 0 , (SOCKADDR *)&amp;SenderAddr , &amp;SenderAddrSize); 
  for (int x = 12; x &lt; 16; x++) 
  {
      printf("%c ",Buffer[x]); 
  }
  std::cout &lt;&lt; "\n";
  
  iphdr = (IPV4_HDR *)Buffer;
  
  memset(&amp;source, 0, sizeof(source));
  source.sin_addr.s_addr = iphdr-&gt;ip_srcaddr;
  std::cout &lt;&lt; inet_ntoa(source.sin_addr) &lt;&lt; "\n\n";
  </code></pre>
  
  <p>That's the point where it all goes down (there is way more code but not important), the structures are all properly defined so there is no errors there (else I couldn't be getting the same IP adress over and over in every single packet).
  Also there is another, non-existent IP from my network showing up as source IP and very occasionally the source IP is 0.0.0.0 which I both find very weird.</p>
  
  <p>Buffer is declared as</p>
  
  <pre><code>char *Buffer = (char *)malloc(65536);
  </code></pre>
  
  <p>so it's signed char and I have no idea how I could manually "extract" the separate parts of the header from these values.</p>
  
  <p>I hope anyone can explain to me that recvfrom is actually not losing packets and that I'm doing something wrong.
  Also I'd like to know how to interpret the output from <code>recvfrom</code>, i.e. converting signed to unsigned or maybe some other way?</p>
  
  <p>I hope I made my problem clear, thanks in advance.</p>
  
  <p><strong>EDIT.</strong> Bigger chunk of my code</p>
  
  <pre><code>int main()
  {
      SOCKET sniffer;
      struct in_addr addr;
      int in;
  
      char hostname[100];
      struct hostent *local;
      WSADATA wsa;
  
  
      // Initialise Winsock
  
      printf("\nInitialising Winsock...");
      if (WSAStartup(MAKEWORD(2,2), &amp;wsa) != 0)
      {
          printf("WSAStartup() failed.\n");
          getchar();
          return 1;
      }
      printf("Initialised");
  
      // Create a RAW Socket
      printf("\nCreating RAW Socket....");
      sniffer = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
      if (sniffer == INVALID_SOCKET)
      {
          printf("Failed to create raw socket.\n");
          printf("WSA throws error code %d\n",WSAGetLastError());
          printf("Run as admin to fix\n");
          getchar();
          return 1;
      }
      printf("Created.");
  
      // Retrieve the local hostname
      if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR)
      {
          printf("WSA throws error code %d\n",WSAGetLastError());
          getchar();
          return 1;
      }
      printf("\nHost name : %s \n",hostname);
  
      bool retry = true;
      signed int maxnumber = -1;
  
      do
      {
          // Retrieve the available IPs of the local host
          local = gethostbyname(hostname);
          printf("\nAvailable Network Interfaces  \n");
          if (local == NULL)
          {
              printf("WSA throws error code %d.\n",WSAGetLastError());
              getchar();
              return 1;
          }
  
          // Available interfaces
          for (i = 0; local-&gt;h_addr_list[i] != 0; i++) // h_addr_list is null terminated
          {
              // local is hostent struct in which information about the host is stored
              // in_addr represents an IPv4 internet address. inet_ntoa: in_addr to string (ip adress)
              memmove(&amp;addr, local-&gt;h_addr_list[i], sizeof(struct in_addr));
              //memmove(destination, source, number of bytes to copy)
              printf("Interface Number : %d Address : %s\n", i, inet_ntoa(addr));
              maxnumber = i;
              printf("\n");
          }
  
          // Choose interface
          if (maxnumber &gt; 0)
          {
              printf("Enter the interface number you would like to sniff : ");
              scanf("%d",&amp;in);
              printf(" in: %d | maxnumber: %d \n", in, maxnumber);
              if (in &lt;= maxnumber)
              {
                  retry = false;
              } else {
                  printf("Interface number %d with adress %d does not exist \n\n", in, local-&gt;h_addr_list[in]);
              }
          }
          else
          {
              printf("Only one interface available");
              in = 0;
              retry = false;
          }
      } while (retry);
  
      self = addr;
      CreateFolder(self);
  
      memset(&amp;dest, 0, sizeof(dest));
      memcpy(&amp;dest.sin_addr.s_addr,local-&gt;h_addr_list[in],sizeof(dest.sin_addr.s_addr));
      dest.sin_family = AF_INET;
      dest.sin_port = htons(source.sin_port);//=0;
  
      printf("\nBinding socket to local system and port 0 ...");
      if (bind(sniffer,(struct sockaddr *)&amp;dest,sizeof(dest)) == SOCKET_ERROR)
      {
          printf("bind(%s) failed.\n", inet_ntoa(addr));
          getchar();
          return 1;
      }
      printf("Binding successful");
  
      j=1;
      unsigned long nbytesret;
      printf("\nSetting socket to sniff...");
      if (WSAIoctl(sniffer, SIO_RCVALL, &amp;j, sizeof(j), 0, 0, &amp;nbytesret , 0 , 0) == SOCKET_ERROR)
      {
          printf("WSAIoctl() failed.\n");
          getchar();
          return 1;
      }
      printf("Socket set\n");
      printf("Press enter to start\n");
      getchar();
  
      printf("Packet Capture Statistics...\n");
      StartSniffing(sniffer); 
  
      printf("Ending sniffer, press enter to exit \n");
      getchar();
      closesocket(sniffer);
      WSACleanup();
  
      return 0;
  }
  
  void StartSniffing(SOCKET sniffer)
  {
      char *Buffer = (char *)malloc(65536);
      int packetsize;
  
      if (Buffer == NULL)
      {
          printf("malloc() failed.\n");
      getchar();
      return;
      }
  
      do
      {
          packetsize = recvfrom(sniffer , Buffer , 65536 , 0 , (SOCKADDR *)&amp;SenderAddr , &amp;SenderAddrSize); 
  
          if(packetsize &gt; 0)
          {
              ProcessPacket(Buffer, packetsize);
          }
          else
          {
              printf( "recvfrom() failed.\n");
              getchar();
          }
  
      }
      while (packetsize &gt; 0);
  
      free(Buffer);
  }
  </code></pre>