title: How to handle the leading 4-byte protocol of tun FD packets when reading tun FD directly in C instead of through packetFlow.readPackets in Swift?
body: <p>In iOS SimpleTunnel VPN demo app, it reads the virtual tun interface through packetFlow.readPackets(). In my case, Swift hands over control to C to move data packets between the tunnel and the virtual tun interface. The tun file descriptor will be sent over from Swift to C through this conversion: </p>
  
  <pre><code>let tunFd = self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32
  </code></pre>
  
  <p>as explained by <a href="https://forums.developer.apple.com/thread/13503" rel="nofollow noreferrer">spensaurus in this post</a>, and also <a href="https://stackoverflow.com/questions/53914735/difference-b-w-self-packetflow-readbytes-vs-socket-read/54072019#54072019">Dinesh's post</a>.</p>
  
  <p>But I'm stuck with the issue that there is no internet connection even though VPN is showing activated on the device and logs show that the C code is functioning in moving data packets. Following the idea to remove the leading 4 bytes out of the packets read from tun FD doesn't seem to help. </p>
  
  <p>I realize that Apple support made it clear that the above conversion and the way of using tun FD is not supported. I still want to take this approach because of this seems to be a much easier approach instead of porting a whole C library over which can take a lot more time. </p>
  
  <p>So my questions are: 
  1. Stripping off the 4 leading bytes of the tun packets should be done all the time, or just under what conditions? 
  2. Is it required to add leading 4 bytes protocol to any packets into tun FD? 
  3. Anything else you are doing and I might have missed? </p>
  
  <p>I'd very much appreciate any insight. </p>