|
|
@ -243,7 +243,20 @@ 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);
|
|
|
@ -252,6 +265,11 @@ public class ProtocolP2PDatagram {
|
|
|
|
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);
|
|
|
|