add doc to checksum
This commit is contained in:
parent
460964bd6e
commit
1e979a5fc1
@ -243,15 +243,33 @@ public class ProtocolP2PDatagram {
|
|||||||
this.hostR = hostR;
|
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 {
|
private int computeCheckSum(byte [] datagram) throws SizeError {
|
||||||
|
/*
|
||||||
|
* The checksum field is the 16 bit one’s complement of the one’s 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;
|
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 += BytesArrayTools.readInt16Bits(datagram,i);
|
||||||
checksum &= 0xffff;
|
checksum &= 0xffff;
|
||||||
}
|
}
|
||||||
return checksum ^ 0xffff;
|
return checksum ^ 0xffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Used to set checksum into datagram
|
||||||
|
* @param datagram full datagram
|
||||||
|
* @throws InternalError
|
||||||
|
*/
|
||||||
|
|
||||||
private void setCheckSum(byte [] datagram) throws InternalError {
|
private void setCheckSum(byte [] datagram) throws InternalError {
|
||||||
try {
|
try {
|
||||||
int checksum = computeCheckSum(datagram);
|
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 {
|
private void checkCheckSum(byte [] datagram) throws TransmissionError {
|
||||||
try {
|
try {
|
||||||
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
|
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
|
||||||
|
@ -63,10 +63,10 @@ public class BytesArrayTools {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read int from a bytearray
|
/** Read int from a bytearray of 16 bits
|
||||||
* @param array the array to read
|
* @param array the array to read
|
||||||
* @param start where to begin reading
|
* @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) {
|
public static int readInt16Bits(byte[] array, int start) {
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user