I need to convert packets to byte format and decode it . How is it possible to convert packets captured using jnetpcap library to array[bytes] and vice-versa in Java?
PcapPacket class has the method
public int transferStateAndDataTo(byte[] buffer) which will copy the contents of the packet to the byte array.
define the byte[] with size as packet.getTotalSize()
if you are looking for the payload
//opens an offline pcap file
Pcap pcap = Pcap.openOffline(pcapIpFile, errbuf);
//packet object
PcapPacket packet = new PcapPacket(JMemory.POINTER);
Payload pl = new Payload();
pcap.nextEx(packet); // retrieves the next packet from input loop thru until eof
if(packet.hasHeader(pl)) //this will check for and retrieve the payload
pl.data() // this will give you the data in the payload as a byte stream
for data in the different headers (ethernet/ip/tcp) there are other methods available with the implementation
Related
I am trying to implement the scrape method for an UDP tracker but I keep getting 0 seeders/leechers as response. I am still getting a 2 as the action so no error is reported.
I have hardcoded a hash here just to show that it has no effect on the result I am getting.
final ByteArrayOutputStream byteStream =new ByteArrayOutputStream();
final DataOutputStream dataStream =new DataOutputStream(byteStream);
dataStream.writeInt(connectResponse.get("connectionId0"));
dataStream.writeInt(connectResponse.get("connectionId1"));
dataStream.writeInt(2);
dataStream.write(connectResponse.get("transactionId"));
bencodeWriter.write(byteOut.toString());
dataStream.writeChars("1D19CC96C1A4965D184E4B215942DBC0A09FF8F2");
dataStream.close();
final byte[] scrapeBytes= byteStream.toByteArray();
I tried different trackers but get the same response. What might be the problem?
Edit: Added the hex dump of all the requests and responses:
Connect Request:
Connect Response:
Scrape Request:
Scrape Response:
In the Scrape request;
the transaction_id=0x36 is sent as a single byte instead of 4 bytes=0x00000036
and the info_hash is sent as a 80 bytes string that is hex-encoded were every character is prepended by a zero-byte 0x00 instead of as a raw 20 bytes string.
I.E. 0x0031004400310039... instead of 0x1D19...
The Scrape response has no peers, as there is no torrents with the info_hashes sent in the request.
I am sending MultiPart content to my remote server to store it in filesystem. For this I am using Java TCP/IP protocol. For avoiding network bandwidth and TCP Input / Output buffer memory , I am sending the data in GZIP compressed format. But , I cannot decompress the data received from the client. I got Unexpected end of ZLIB input stream Exception. Its due to the server is receiving data in chunks.
Java Code
Client
OutputStream out = new GZIPOutputStream(sock.getOutputStream());
byte[] dataToSend = FileUtil.readFile(new File("/Users/bharathi/Downloads/programming_in_go.pdf"));
out.write(dataToSend);
Server
out = new FileOutputStream("/Users/bharathi/Documents/request_trace.log");
InputStream in = new GZIPInputStream(clntSocket.getInputStream());
int totalBytesRead = 0;
int bytesRead;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = in.read(buffer)) != -1)
{
out.write(buffer , 0 , bytesRead);
totalBytesRead += bytesRead;
}
Is there any solution to send the data in GZIP compressed format in Socket?
GZIPOutputStream generates a GZIP file format, meaning that the other end has to receive the complete stream (which is a file) before it can process it, this is the reason for your error.
If you are looking to actually do a stream based data transfer, drop gzip, and go for zlib, I believe Zlib compression Using Deflate and Inflate classes in Java answers how to do this.
Try adding:
out.flush();
sock.shutdownOutput();
to your client code.
I have an image byte array which I need to send to a servlet on a server using HTTP client. We know how to send normal text data but unable to send the image data.
We created a string data from image byte array using the following code:
String imageData = new String(imagebyteArr);
And sent the above String to servlet through HTTP client, but when we again retrieve byte array from string using below code:
imageByteArr = imageData.toByteArray();
The resultant byte array is modified one, where in -127 is replaced on 63.
How to solve this unexpected behavior?
Strings get encoded. You have 2 posibilities: encode binary data as base64 (for example) send the base64 and decode on server side or binary upload with a PUT request.
I would totally discourage you with taking image byte array and converting to String as you will have to worry about character encoding.
One thing to do is send the byte array directly using ByteArrayEntity, as follows:
HttpPost post = new HttpPost(url);
post.setEntity(new ByteArrayEntity(bytes));
post.setHeader("Content-type", ""application/octet-stream");
Don't forget to set your Content-Type to the correct image appropriately.
I am using the official Dropbox API for Java.
So far, everything works smoothly. Authentication via oauth works and so do other functions (like directory listings).
Now, I tried to upload a file like this:
InputStream is = getInputStream();
byte[] bytes = is2Bytes(is); // Gets all bytes "behind" the stream
int len = bytes.length;
api.putFileOverwrite(path, is, len, null);
Now, when I do this call, my application hangs for about 15 seconds and then I get an exception thrown that Dropbox server did not respond.
So, first I asked Dropbox support if there was something wrong with their server. There isn't.
Then, I played around with the parameters of the putFileOverwrite method and I found out that if I set len=0 manually, the server responds and creates a 0 byte file with the correct file name.
As another test, I manually entered the value len=100 (the original file has 250KB so that should be ok). Again, the server does NOT respond.
So, what's wrong?
That is not weird at all. Since you use your self-made method is2Bytes, the steam is empty, because you read all the bytes to count them. The proper way of doing this would be either knowing how many bytes you are going to send or using the build-in method for sending a file.
public HttpResponse putFile(String root, String dbPath, File localFile)
Very weird. I was able to work around this by re-creating a new InputStream from the byte array and send that to Dropbox:
InputStream is = getInputStream();
byte[] bytes = is2Bytes(is); // Gets all bytes "behind" the stream
int len = bytes.length;
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
api.putFileOverwrite(path, bis, len, null);
I am currently sending and receiving SIP messages across a network.
When I want to add SDP data to a SIP message I use the SessionDescription object in jrtp.
This object is then added to the SIP message.
But when I get a SIP message from the server its SDP is in a byte[] array.
So I'm wondering is there anyway to convert the byte[] back into a SessionDescription object so I can use the SessionDescription object's methods to parse the data?
I think you can use Byte[] wrapper class of byte.