Question: i have found out that the cecksumcomputation over the payload is wrong / / Compute checksum over payload data uint 8 _ t byte =

i have found out that the cecksumcomputation over the payload is wrong
// Compute checksum over payload data
uint8_t byte =0;
for (size_t i =0; i < sz; i++){
// Extract each byte of the payload data and XOR it with the checksum
byte = buffer[i];
// Convert byte to network byte order before XOR
}
checksum ^=(byte <<8);
checksum ^=(byte >>8);
i have also tried this method
for (size_t i =0; i < sz; i++){
// XOR each byte of the payload data with the checksum
checksum ^= buffer[i];
}
both wrong, (black box server gets different checksum when ther is data but, it gets equal checkum when there is no data) so how do you compute the checksum over the data aka payload?
my entire checksum computation is within this function
int d1_send_data( D1Peer* peer, char* buffer, size_t sz )
{
// Check if peer or buffer is NULL
if (peer == NULL || buffer == NULL){
return -1; // Return negative value for error
}
// Calculate the total size of the packet (header + data)
size_t packet_size = sizeof(D1Header)+ sz;
// Check if the packet size exceeds the maximum allowed size
if (packet_size >1024){
fprintf(stderr, "Error: Packet size exceeds maximum allowed size
");
return -1; // Return negative value for error
}
// Create a D1Header and populate its fields
D1Header header;
header.flags = FLAG_DATA;
header.size = packet_size;
// Compute the checksum
uint16_t checksum =0;
// Compute checksum over flags and size fields
checksum ^= header.flags; // checksum for flag field
checksum ^= header.size >>16; // Compute checksum for the upper 16 bits of size
checksum ^= header.size & 0xFFFF; // Compute checksum for the lower 16 bits of size
// Compute checksum over payload data
uint8_t byte =0;
for (size_t i =0; i < sz; i++){
// Extract each byte of the payload data and XOR it with the checksum
byte = buffer[i];
// Convert byte to network byte order before XOR
}
checksum ^=(byte <<8);
checksum ^=(byte >>8);
// Add padding byte if the payload size is odd
if (sz %2!=0){
printf("odd data");
checksum ^=0x00;
}
// convert all fields to network byte order before sending the packet
header.flags = htons(header.flags);
header.size = htonl(header.size);
header.checksum = htons(checksum); // Convert checksum to network byte order
char packet[1024];
memcpy(packet, &header, sizeof(D1Header));
memcpy(packet + sizeof(D1Header), buffer, sz);
// Send the packet header to the peer using sendto
printf("sending header
");
ssize_t bytes_sent = sendto(peer->socket, packet, packet_size, 0,
(struct sockaddr*)&peer->addr, sizeof(struct sockaddr_in));
if (bytes_sent ==-1){
perror("Error sending header");
return -1; // Return negative value for error
}
// Receive and validate the ACK packet
D1Header ack_packet;
printf("waiting for ack
");
ssize_t ack_bytes_received = recv(peer->socket, (void*)&ack_packet, sizeof(D1Header),0);
if (ack_bytes_received ==-1){
perror("Error receiving ACK packet");
return -1; // Return negative value for error
}
// Validate the ACK packet
uint16_t ack_flags = ntohs(ack_packet.flags);
// Check if received packet is an ACK and sequence number matches
if ((ack_flags & FLAG_ACK) && ((ack_flags & SEQNO)== peer->next_seqno)){
// Valid ACK received
peer->next_seqno =(peer->next_seqno +1)%2;
return bytes_sent; // Success: return number of bytes sent
}
else {
d1_send_data(peer, buffer, sz);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!