问题
I am trying to read data from packet tunnel NEPacketTunnelProvider. Right now what i am doing is trying to read the data using
self.packetFlow.readPackets { [weak self] (packets: [Data], protocols: [NSNumber]) in }
function. It seems to be working fine. But now i want to read the packets by using the network handle of self.packetFlow function like this
let tunFd = self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32
and use this file descriptor to read the packet data from the tunnel. like this
char *buffer1 = malloc(1024);
ssize_t length = read(tunFd, buffer1, 1024);
But now when i read the data from socket like this. It seems like data is not similar to what i am receiving earlier using the self.packetFlow.
Does anybody else know why it is happening. and what should i do get the similar data as in self.packetFlow.readPackets..
Any pointer of help will be appreciated.
回答1:
I found the answer or my own query. There are 4 bytes of extra protocol information added to the data while we use file descriptor. We need to handle it as well.
来源:https://stackoverflow.com/questions/53914735/difference-b-w-self-packetflow-readbytes-vs-socket-read