My application implements VpnService to intercept network traffic and provide individual responses. The goal is to process traffic at specific addresses and drop other requests.
Currently, I will be able to parse incoming requests and build and send responses. The problem, however, is that these responses do not arrive as an actual response to the original request; socket testing is simply disabled.
To make this distinction, I now parse the source IP packets from the VpnService input stream as follows:
VpnService.Builder b = new VpnService.Builder();
b.addAddress("10.2.3.4", 28);
b.addRoute("0.0.0.0", 0);
b.setMtu(1500);
...
ParcelFileDescriptor vpnInterface = b.establish();
final FileInputStream in = new FileInputStream(
vpnInterface.getFileDescriptor());
final FileOutputStream out = new FileOutputStream(
vpnInterface.getFileDescriptor());
ByteBuffer packet = ByteBuffer.allocate(32767);
try {
while (vpnInterface != null && vpnInterface.getFileDescriptor() != null
&& vpnInterface.getFileDescriptor().valid()) {
packet.clear();
SystemClock.sleep(10);
final byte[] data = packet.array();
int length = in.read(data);
if (length > 0) {
packet.limit(length);
final IpDatagram ip = IpDatagram.create(packet);
...
}
}
IpDatagram - , create()
IP-, IP, . . IPv4 TCP, , TCP.
IpDatagram IP- ( IP) ( TCP). TCP- (, SYN, ACK PSH) . :

IpDatagram , :
IpDatagram VpnServer:
TcpDatagram tcp = new TcpDatagram(tcpHeader, tcpOptions, tcpBody);
IpDatagram ip = new Ip4Datagram(ipHeader, ipOptions, tcp);
out.write(ip.toBytes());
, , , , - .

TCP/IP :
4500003c7de04000400605f10a0203044faa5a3bb9240050858bc52b00000000a00239089a570000020405b40402080a00bfb8cb0000000001030306
TCP/IP :
450000bb30394000800613194faa5a3b0a0203040050b92400a00000858bc52b501820001fab0000485454502f312e3120323030204f4b0a446174653a205475652c203139204e6f7620323031332031323a32333a303320474d540a436f6e74656e742d547970653a20746578742f68746d6c0a436f6e74656e742d4c656e6774683a2031320a457870697265733a205475652c203139204e6f7620323031332031323a32333a303320474d540a0a48656c6c6f20776f726c6421
; IP- , , , .
? , ?