Where i can get packet information to make .pcap log ?
Closed this issue · 8 comments
@hexene could you explain to me how i can convert the packet data to the .pcap ?
In which part of code I can get the information (packages) that i must save to create a log ?
Please don't close this, i have some question.
Thanks so much
Please check the code in LocalVPNService. The device->network data would be available in the variable bufferToNetwork and network->device data in bufferFromNetwork.
byte[] bytes = new byte[bufferCopy.remaining()];
bufferCopy.get(bytes);
String packet = new String(bytes);
FileOutputStream stream = new FileOutputStream(file);
stream.write(packet.getBytes());
bufferCopy is a copy of bufferFromNetwork, is it correct this ?
This is what you'll need:
bufferCopy = bufferToNetwork/FromNetwork.duplicate();
bufferCopy.flip(); // Assuming that bufferToNetwork/FromNetwork hasn't already been flipped
bufferSize = bufferCopy.limit();
Please refer to the documentation for ByteBuffer for more details.
I apologize for the bad explanation , I wanted to know is:
Through these variables can obtain the information needed to create a file in .pcap format? This it would take to generate a .har and make the traffic test.
it is fair to say that the information that I need are in:
bufferToNetwork and in bufferFromNetwor ?
Yes.
Is it correct this reasoning ?
ByteBuffer bufferFromNetwork = networkToDeviceQueue.poll();
if (bufferFromNetwork != null){
bufferFromNetwork.flip();
// Other copy of bufferFromNetwork, necessary to write log
bufferCopy = bufferFromNetwork.duplicate();
bufferCopy.flip();
bufferSize = bufferCopy.limit();
while (bufferFromNetwork.hasRemaining()) {
vpnOutput.write(bufferFromNetwork);
// For each element in buffer i write a line in a file
byte[] bytes = new byte[bufferCopy.remaining()];
bufferCopy.get(bytes);
String packet = new String(bytes);
FileOutputStream stream = new FileOutputStream(file);
stream.write(packet.getBytes());
}
dataReceived = true;
I'm sorry for the many questions but I need to generate the log, and I find myself in difficulty in implementing this function.
Thanks to the availability
As I'd mentioned earlier, please familiarize yourself with how a ByteBuffer works, specifically how its limit and position are updated. Please also pay close attention to the code snipped I'd posted earlier.
In short, this is what you'll need:
FileOutputStream stream.....
....
ByteBuffer bufferFromNetwork = networkToDeviceQueue.poll();
if (bufferFromNetwork != null) {
bufferFromNetwork.flip();
ByteBuffer bufferCopy = bufferFromNetwork.duplicate();
byte[] packetData = new byte[bufferCopy.limit()];
bufferCopy.get(packetData);
stream.write(packetData);
....
while (bufferFromNetwork.hasRemaining())
Please note that this will only write the raw packet data to the file, you'll still have to convert it to the pcap format.
If all you need to do is to decode and print the packet data, you could do this instead:
Packet packet = new Packet(bufferCopy);
stream.write(packet.toString().getBytes());