| 190 | } |
| 191 | |
| 192 | bool TUdpSocket::CheckPacketIntegrity(const char* buf, const size_t size, const TSockAddrPair& addr) { |
| 193 | // ignore empty or small packets |
| 194 | if (size < UDP_LOW_LEVEL_HEADER_SIZE) { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | if (ReadVersion(buf) != UDP_NETLIBA_VERSION_V12) { |
| 199 | fprintf(stderr, "NETLIBA::TUdpSocket: version mismatch\n"); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | const size_t packetDataSize = ReadPacketDataSize(buf); |
| 204 | if (packetDataSize + UDP_LOW_LEVEL_HEADER_SIZE > size) { |
| 205 | fprintf(stderr, "NETLIBA::TUdpSocket: bad packet size in header\n"); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | const ui64 expectedCrc = *(ui64*)buf; |
| 210 | |
| 211 | const ui64 crc = CalcChecksum(buf + UDP_CRC_SIZE, packetDataSize + (UDP_LOW_LEVEL_HEADER_SIZE - UDP_CRC_SIZE)) + PROTO_REV; |
| 212 | if (!CrcMatches(expectedCrc, crc + PortCrc, addr.RemoteAddr)) { |
| 213 | fprintf(stderr, "NETLIBA::TUdpSocket: udp packet crc failure %s, expected %" PRIu64 ", %" PRIu64 ", %u \n", |
| 214 | GetAddressAsString(GetUdpAddress(addr.RemoteAddr)).c_str(), expectedCrc, crc, PortCrc); |
| 215 | return false; |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | /////////////////////////////////////////////////////////////////////////////// |
| 221 |
nothing calls this directly
no test coverage detected