title: C Program to receive and send the same packets out over another interface
body: <p>I have a linux system with two physical interfaces. I need to intercept (read) incoming packets over one interface, read (or process) the data and send it out over the other interface as it is - just like a middleman. I am able to extract all the header fields and payload data from the packets but I am not able to put it back on the wire again.
  How do I send the packet on its way through the other interface?</p>
  <pre><code>// All #includes 
  
  struct sockaddr_in source,dest;
  int i,j,k;
  int main()
  {
      int saddr_size , data_size;
      struct sockaddr_in saddr;
      unsigned char *buffer=malloc(65535);
  
  
      int sock_raw = socket( AF_PACKET , SOCK_RAW , htons(ETH_P_ALL)) ;
  
       if(sock_raw &lt; 0)
          perror(&quot;setsockopt&quot;);
  
      setsockopt(sock_raw , SOL_SOCKET , SO_BINDTODEVICE , &quot;eth0&quot; , strlen(&quot;eth0&quot;)+ 1 );
  
      if(sock_raw &lt; 0)
      {
          perror(&quot;Socket Error&quot;);
          return 1;
      }
  
      while(1)
      {
          saddr_size = sizeof (struct sockaddr);
          //Receive a packet
          data_size = recvfrom(sock_raw , buffer , 65536 , 0 ,(struct sockaddr *) &amp;saddr , (socklen_t*)&amp;saddr_size);
  
          if(data_size &lt;0 )
          {
              printf(&quot;Recvfrom error , failed to get packets\n&quot;);
              return 1;
          }
          else{
          printf(&quot;Received %d bytes\n&quot;,data_size);
  
          //Huge code to process the packet
  
          //Send it out through &quot;eth1&quot; here 
  
          }
      }
      close(sock_raw);
      return 0;
  }
  </code></pre>
  <p>Just assume only UDP or ICMP packets if it makes it easier to explain (using a simple &quot;sendto&quot; function maybe)- I can handle the sorting. Do not worry about the intended destination, I only want to put the packets back on the wire - delivery is not important.</p>
  <p><em>Edit 1</em>:</p>
  <p>If I do this it gives me a runtime error saying &quot;Invalid argument&quot;. It doesn't matter if I'm sending the buffer or even &quot;Hello World&quot;.</p>
  <pre><code>bytes_sent=sendto(sock_raw, buffer, 65536, 0,(struct sockaddr *) &amp;saddr ,saddr_size);
  if (bytes_sent &lt; 0) {
      perror(&quot;sendto&quot;);
      exit(1);
      }
  </code></pre>
  <p><em>Edit 2</em> :
  Let me make it simpler-
  I have two pipes A and B. Balls roll in from A and I receive them. I just want to put them in pipe B and send them on their way.
  Ethernet bridges work in a similar way - just sending all packets over all interfaces involved. I would have definitely used a bridge if I didn't have to get some basic information from the packet headers. And I'm not good at modifying the kernel bridge drivers.</p>
  <p><em>Edit 3</em> :
  I'll try one last time with a different question. If I have received a complete raw packet with source/destination addresses included in the headers and all, how do I simply send it ANYWHERE (i don't care where) using sendto ? Should I add any information to the &quot;struct sockaddr&quot; in the sendto call , or can I simply use the same one I did in the recvfrom call ?</p>