title: Receive raw ethernet packets in c
body: <p>I'm wirting two programms for sending and receiving raw ethernet packets. I can send packets and I can receive, but I have two problems with my receiver. </p>
  
  <p>First: All packtes are duplicated, it means all in comming packets are shown twice. It's only with a connection on 2 virtual networkinterfaces.(e.g. src tap0 mac 00:00:8F:00:00:02 -> dest tap1 mac 00:00:8F:00:00:03)</p>
  
  <p>Second: All packets in the network are received form my programm, but it should receive packets from the specific interface.</p>
  
  <p>Informations: I work on Ubuntu 12.04.01 LTS in a VirtualBox</p>
  
  <p>Here my receiver code:</p>
  
  <pre><code>/*read while first char not * */
  while(frame.data[0] != '*'){
      length_in_byte = recvfrom(sock_desc, buffer, ETH_FRAME_LEN,0,NULL,NULL);
      if(length_in_byte == -1){
          error handling
      }
  
      frame.dest_mac[0] = buffer[0];
      frame.dest_mac[1] = buffer[1];
      frame.dest_mac[2] = buffer[2];
      frame.dest_mac[3] = buffer[3];
      frame.dest_mac[4] = buffer[4];
      frame.dest_mac[5] = buffer[5];
  
      frame.src_mac[0] = buffer[6];
      frame.src_mac[1] = buffer[7];
      frame.src_mac[2] = buffer[8];
      frame.src_mac[3] = buffer[9];
      frame.src_mac[4] = buffer[10];
      frame.src_mac[5] = buffer[11];
  
      for(dataRun = 14; dataRun &lt; length_in_byte; dataRun++){
          frame.data[dataRun-14] = buffer[dataRun];
      }
      /*print struct*/
      printf("\n");
      printf("src: %02x:%02x:%02x:%02x:%02x:%02x\t-&gt;\tdest: %02x:%02x:%02x:%02x:%02x:%02x\n",frame.src_mac[0],frame.src_mac[1],frame.src_mac[2],frame.src_mac[3],frame.src_mac[4],frame.src_mac[5],frame.dest_mac[0],frame.dest_mac[1],frame.dest_mac[2],frame.dest_mac[3],frame.dest_mac[4],frame.dest_mac[5]);
      printf("Data: %s\n", frame.data);
  
  }
  </code></pre>
  
  <p>Terminal Output:
  send data = test</p>
  
  <pre><code>src: 00:00:8f:00:00:03  -&gt;  dest: 00:00:8f:00:00:02
  Data: test
  
  src: 00:00:8f:00:00:03  -&gt;  dest: 00:00:8f:00:00:02
  Data: test
  </code></pre>
  
  <p>send data = a</p>
  
  <pre><code>src: 00:00:8f:00:00:03  -&gt;  dest: 00:00:8f:00:00:02
  Data: a
  
  src: 00:00:8f:00:00:03  -&gt;  dest: 00:00:8f:00:00:02
  Data: a
  
  src: 00:00:8f:00:00:03  -&gt;  dest: 33:33:00:00:00:fb
  Data: `
  
  src: 00:00:8f:00:00:03  -&gt;  dest: 33:33:00:00:00:fb
  Data: `
  </code></pre>