Compute CRC structures in Python

I have the following structure, from the NRPE daemon code in C:

typedef struct packet_struct {
  int16_t packet_version;
  int16_t packet_type;
  uint32_t crc32_value;
  int16_t result_code;
  char buffer[1024];
} packet;

I want to send this data format to a C-image from Python. CRC is calculated when crc32_value 0, then it is placed in the structure. My Python code for this is as follows:

cmd = '_NRPE_CHECK'
pkt = struct.pack('hhIh1024s', 2, 1, 0, 0, cmd)
# pkt has length of 1034, as it should
checksum = zlib.crc32(pkt) & 0xFFFFFFFF
pkt = struct.pack('hhIh1024s', 2, 1, checksum, 0, cmd)
socket.send(....)

The daemon receives the following values: version=2 type=1 crc=FE4BBC49 result=0

But he calculates crc=3731C3FD

Actual C code for calculating CRC:

https://github.com/KristianLyng/nrpe/blob/master/src/utils.c

and it is called through:

calculate_crc32((char *)packet, sizeof(packet));

When I ported these two functions to Python, I get the same thing that returns zlib.crc32.

Is my call struct.packright? Why are my CRC calculations different from the server?

+5
source share
1 answer

Python:

, , , : . , .

'!' , . , . CRC .

+2

All Articles