May 17, 2012

How to read byte buffer in Java

In telecommunications we need to read and analyze raw byte buffers frequently because this is usual representation of a network packet. However there is an issue with the Java language which does not have unsigned integer types. All integer types in Java are signed. So it is not possible just to read a byte and then analyze it.

For example, if we have byte with value 0x96 we will get -106 instead 150. Why does this happen and what to do?

Negative numbers are stored in the memory in two's complement system. According to this approach we need to make the following conversation to write negative number into the memory:
Step 1. invert all bits
Step 2. add 1

For example, let's try to convert a number -6 to the two's complement system representation.
The binary representation of the number 6 is 00000110.
Step 1. After invert conversion we will have 11111001.
Step 2. 11111001 + 1 = 11111010
So the negative number -6 will look in the two's complement system as 11111010. This number corresponds to unsigned byte 250.

Now let's see what Java makes with our number 0x96 or 150. Since there is no unsigned types in Java, this number is considered as number with a sign. Maximum value for signed byte is 127. So Java makes reverse conversion.
The binary representation of the number 0x96 is 10010110.
Step 1. subtract 1:  10010110-1=10010101
Step 2. invert all bits: ~10010101=01101010
01101010 is -106.
That is why Java considers the byte 0x96 as the negative number -106.

How to fix this Java feature? We need to make two things:
1. Convert a byte into int because the maximum value of a signed byte is only 127. At this step Java will fill the high-order bits with 1 to keep the number negative.
2. Then we need to zero high-order bits to get the original value.

The following code does this:

byte b = (byte) 0x96;
int i = (int) b & 0xFF; 

February 1, 2012

How to build jNetPcap library


Access to the source code of jNetPcap
 
jNetPcap source code is located under SVN control.
To access the repository you can use TortoiseSVN client.
Then you can get the source code using following link:https://jnetpcap.svn.sourceforge.net/svnroot/jnetpcap/jnetpcap/trunk

Preparation

The library should be linked statically with the libraries LIBGCC_S_DW2-1.DLL and LIBSTDC++-6.DLL which is located in bin folder of MinGW. The default build.xml doesn't contain necessary options. So before to start build procedure you need to update the build.xml file.

Both sections with compilation options for 32 and 64 bits platforms should be updated.

Find section "comp-jni-win32" and "comp-jni-win64" and update the section <exec executable...> inside both of them. The following lines should be added there "<arg value="-static" />"
So in result section <exec executable..."> should look like this code:

 <exec executable="${compiler.ld.cmd}">
   <arg value="-shared" />
   <arg value="-g" />
   <arg value="-Wl,--add-stdcall-alias" />
   <arg value="-static" />
   <arg line="-o ${jni.build.filepath}" />
   <arg line="${jni.obj.files}" />
   <arg line="-L ${pcap.lib.dir}" />
   <arg line="-l ${pcap.lib.name}" />
   <arg line="-l ${ms.lib.name}" />
</exec>

How to build jNetPcap library for 32-bits platform

Download and install Apache Ant building software.
It could be taken from the official site. To install it you need just open zip archive and extract the files into any directory. Then you need to create Environment variable ANT_HOME with the path to the ant directory. It is also necessary to add the path to ant bins into the PATH environment variable.

Download and install compiler MinGW.
It could be taken from the official site. During the setup you need to select "Download latest repository catalogues". It is important at least for Windows XP. Then you need to update the PATH environment variable again and add the path to MinGW bins there.

Build the jNetPcap library
Go into the jnetpcap directory with the source code and run the following command to get the complete list of possible targets:
ant help
You need to compile jnetpcap.jar and jnetpcap.dll files. Execute the following command one by one.
ant comp-java
ant build-jar
ant comp-jni

 
How to build jNetPcap library for 64-bits platform


Download and install Apache Ant building software.
It could be taken from the official site. To install it you need just open zip archive and extract the files into any directory. Then you need to create Environment variable ANT_HOME with the path to the ant directory. It is also necessary to add the path to ant bins into the PATH environment variable.

Download and install compiler MinGW64.
It could be taken from the official site. But there is no binaries ready to execute. You can find there only source code and you will need to compile the compiler yourself. The compilation procedure is enough complicated.
So it would be much faster to get already build binaries from another site. You can use Windows XP/2003 x64 binaries for Windows 7. It is ok.

Build the jNetPcap library
Go into the jnetpcap directory with the source code and run the following command to get the complete list of possible targets:
ant help

You need to compile jnetpcap.jar and jnetpcap.dll files. Execute the following command one by one.
ant comp-java
ant build-jar
ant comp-jni