|
|
|
@ -243,29 +243,33 @@ public class ProtocolP2PDatagram {
|
|
|
|
|
this.hostR = hostR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int computeCheckSum(byte [] datagram){
|
|
|
|
|
private int computeCheckSum(byte [] datagram) throws SizeError {
|
|
|
|
|
int checksum = 0;
|
|
|
|
|
for (int i=CHECKSUM_POSITION+2; i<datagram.length; ++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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setCheckSum(byte [] datagram) throws InternalError{
|
|
|
|
|
private void setCheckSum(byte [] datagram) throws InternalError {
|
|
|
|
|
try {
|
|
|
|
|
int checksum = computeCheckSum(datagram);
|
|
|
|
|
try{
|
|
|
|
|
BytesArrayTools.write16Bits(datagram,CHECKSUM_POSITION,checksum);
|
|
|
|
|
} catch (SizeError e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkCheckSum(byte [] datagram) throws TransmissionError{
|
|
|
|
|
private void checkCheckSum(byte [] datagram) throws TransmissionError {
|
|
|
|
|
try {
|
|
|
|
|
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
|
|
|
|
|
int computedCheckSum = computeCheckSum(datagram);
|
|
|
|
|
if (computedCheckSum != checksum){
|
|
|
|
|
throw new TransmissionError();
|
|
|
|
|
}
|
|
|
|
|
} catch(SizeError e) {
|
|
|
|
|
throw new TransmissionError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|