add doc to checksum

pull/10/head
js 5 years ago
parent 460964bd6e
commit 1e979a5fc1

@ -243,15 +243,33 @@ public class ProtocolP2PDatagram {
this.hostR = hostR;
}
/** Used to compute Checksum of a specific datagram
* @param datagram full datagram
* @return checksum
* @throws SizeError
*/
private int computeCheckSum(byte [] datagram) throws SizeError {
/*
* The checksum field is the 16 bit ones complement of the ones complement sum of all 16-bit words
* in the header and text. If a segment contains an odd number of header and text octets to be checksummed,
* the last octet is padded on the right with zeros to form a 16-bit word for checksum purposes.
* The pad is not transmitted as part of the segment. While computing the checksum, the checksum field
* itself is replaced with zeros.
*/
int checksum = 0;
for (int i=CHECKSUM_POSITION+2; i<CHECKSUM_POSITION+2+4+Payload.getPayloadSize(datagram); ++i){
for (int i=CHECKSUM_POSITION+2; i<CHECKSUM_POSITION+2+4+Payload.getPayloadSize(datagram); ++i) {
checksum += BytesArrayTools.readInt16Bits(datagram,i);
checksum &= 0xffff;
}
return checksum ^ 0xffff;
}
/** Used to set checksum into datagram
* @param datagram full datagram
* @throws InternalError
*/
private void setCheckSum(byte [] datagram) throws InternalError {
try {
int checksum = computeCheckSum(datagram);
@ -261,6 +279,11 @@ public class ProtocolP2PDatagram {
}
}
/** Used to check if the checksum is correct
* @param datagram full datagram
* @throws TransmissionError
*/
private void checkCheckSum(byte [] datagram) throws TransmissionError {
try {
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);

@ -63,10 +63,10 @@ public class BytesArrayTools {
return size;
}
/** Read int from a bytearray
/** Read int from a bytearray of 16 bits
* @param array the array to read
* @param start where to begin reading
* @return value read as int
* @return value read as 16 bits int
*/
public static int readInt16Bits(byte[] array, int start) {
int size = 0;

Loading…
Cancel
Save