title: Receiving packets from any protocol on a raw linux socket
body: <p>I would like to create an IP tunnel. For that, I create a raw socket in C# on the client and bind it to port 4999:</p>
  
  <pre><code>Socket mysock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
  mysock.Bind(new IPEndPoint(IPAddress.Loopback, 4999));
  mysock.Connect(new IPEndPoint(IPAddress.Loopback, 4999));
  </code></pre>
  
  <p>Now I can just call <code>mysock.Receive(byte[])</code>, and I get all the IP packets, including the IP header. For example, if I try to open a TCP connection to <code>127.0.0.1:4999</code>, the C# application captures 3 packets. I can also send packets over this socket and it will work just fine.</p>
  
  <p>The C# application then transfers those packets to a C++ application running on a linux machine. There, I change the sender port (that's the easy part, just write it into the TCP header) and then I would like to simply send it out and have everything else done for me, just like it works in C#. And obviously, I would also like to receive packets from the raw linux socket, so I can transfer them back to the C# application and get some communication going.</p>
  
  <p>I have already implemented <em>everything except the raw linux socket</em>. <strong>That's my question</strong>: How can I write the above C# code in C/C++ for linux? According to <a href="http://linux.die.net/man/7/raw" rel="nofollow">raw(7)</a>, I won't get TCP or UDP packets if I just do <code>socket(AF_INET, SOCK_RAW, IPPROTO_RAW)</code>, right?</p>
  
  <p>Edit: By the way, I'm running the C# code on windows but that is not my problem.</p>