I am working on a network application where I send and receive TCP packets. However, I’m encountering an issue where the data received does not match the data sent. I’m using raw sockets and constructing the packet manually, and I suspect there might be an issue with either the packet construction or the reception process.
Here’s what I send:
45 00 00 35 00 00 00 00 FF 06 3C C2 00 00 00 00 7F 00 00 01 31 D4 39 30 29 00 00 00 00 00 00 00 50 18 FF FF A5 71 00 00 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
And here’s what I receive:
45 00 00 3C F8 A9 00 00 80 FF 00 00 7F 00 00 01 7F 00 00 01 45 00 00 28 00 00 00 00 FF 06 BD CD 7F 00 00 01 7F 00 00 01 31 D4 39 30 29 00 00 00 00 00 00 2A 50 12 FF FF 1B BF 00 00
Problem:
The received packet does not match the sent packet. Specifically
The IP header length (45 00 00 ...) differs.
The TCP header length and flags seem inconsistent.
The payload data (which should be "Hello, World!") is different.
Steps Taken:
Packet Construction: I am manually constructing the IP and TCP headers, setting fields such as source and destination IP addresses, ports, sequence numbers, etc.
Checksum Calculation: I compute the checksums for both IP and TCP headers, but the received packet data does not align with what is expected. Packet Reception: I use recv to receive the packet data. Questions:
What might cause the discrepancy between the sent and received packet data? Are there any common issues with packet construction or reception that could lead to such differences?
How can I debug this issue effectively to ensure that the packet sent matches what is received?
receiver code :
recv_len = receive_packet(sock, packet);
if (recv_len < 0) {
std::cerr << "Failed to receive packet: " << WSAGetLastError() << std::endl;
return false;
}
// Output for debugging
std::cout << "Raw packet data (hex): ";
for (int i = 0; i < recv_len; ++i) {
printf("%02X ", static_cast<unsigned char>(packet[i]));
}
sender code:
// Create the data packet
create_data_packet(saddr, daddr, seq, ack, data, data_len, packet, packet_len);
// Send the data packet to the destination address
if (sendto(sock, packet.get(), packet_len, 0, reinterpret_cast<sockaddr*>(&daddr), sizeof(daddr)) == SOCKET_ERROR) {
std::cerr << "Failed to send data packet: " << WSAGetLastError() << std::endl;
closesocket(sock); // Close the socket before exiting
WSACleanup(); // Clean up Winsock before exiting
return 1; // Exit with an error code if sending the packet fails
}
std::cout << "Data packet sent successfully." << std::endl;
I manually constructed and sent a TCP packet using raw sockets. The packet included both IP and TCP headers, as well as a payload. I computed and set the checksums for both the IP and TCP headers correctly. After sending the packet, I attempted to receive the same packet on the other end using raw sockets as well.
What I Expected:
I expected that the received packet would exactly match the sent packet, including the headers and payload. Specifically, the IP and TCP headers should align with what was sent, and the payload should be identical to what was transmitted.
5(five 32-bit words, or 20 octets), is exactly the same in both the sent and received IP Packets.