here is something I have been working on for quite a while and can’t solve why it is not working properly, hope you can help me work something out!. I’ll try to be as descriptive as possible.
The idea is to serialize a structure (NIPC packet structure), to send via TCP to another process, then deserialize and re-build this structure. But somehow I can’t get it to work properly, just partialy. Here are the structures.
typedef struct {
char pct_type;
short int payload_lenght;
}__attribute__((__packed__)) t_header;
typedef struct {
char* path;
long unsigned int size;
long int offset;
}__attribute__ ((__packed__)) t_payload_read;
typedef struct {
t_header header;
t_payload_read payload;
}__attribute__ ((__packed__)) t_pct_read;
Then, I serialized the payload first (offset,size and path), to calculate the payload’s length, then the entire packet, including the header to send it. And then Deserialized, but there is no way to get it working, on the other side i get the header OK and the path field “OK”, but i get weird numbers on offset and size, for example, using this hardcoded values on the structure:
type: 4
offset: 5
size: 15
path: ~/Desarrollo/Workspace
i get:
pct->header.pct_type: 4
pct->header.payload_length: 28
pct->payload.path: ~/Desarrollo/Workspac
pct->payload.size: 143994937
pct->payload.offset: 143994941
Here I append the serializing and deserializing functions I designed, I tried to be as descriptive and careful as possible.
char* serialize_read(t_pct_read* packet) {
char* buffer = malloc(
sizeof(t_header) + sizeof(packet->payload.offset)
+ sizeof(packet->payload.size)
+ strlen(packet->payload.path + 1));
char* payload = malloc(
sizeof(packet->payload.offset) + sizeof(packet->payload.size)
+ strlen(packet->payload.path) + 1);
int offset;
// First I serialize the Payload, in order to calculate It's lenght
memcpy(payload, packet->payload.path, strlen(packet->payload.path) + 1);
offset = strlen(packet->payload.path)+1;
memcpy(payload + offset, &packet->payload.size, sizeof(long unsigned int));
offset += (sizeof(long unsigned int));
memcpy(payload + offset, &packet->payload.offset, sizeof(long int));
packet->header.payload_lenght = offset+1; // Here I get payload's length
offset = 0;
// Same procedure here, but for the entire packet, including the header.
memcpy(buffer, &packet->header.pct_type, sizeof(char));
offset = sizeof(char);
memcpy(buffer + offset, &packet->header.payload_lenght, sizeof(short int));
offset += sizeof(short int);
memcpy(buffer + offset, packet->payload.path,
strlen(packet->payload.path) + 1);
offset += strlen(packet->payload.path + 1);
memcpy(buffer + offset, &packet->payload.size, sizeof(long unsigned int));
offset += (sizeof(long unsigned int));
memcpy(buffer + offset, &packet->payload.offset, sizeof(long int));
return buffer;
}
t_pct_read* deserialize_read(char* stream) {
t_pct_read* packet = malloc(sizeof(t_pct_read));
int offset;
int alloc_size;
memcpy(&packet->header.pct_type, stream, sizeof(char));
offset = sizeof(char);
memcpy(&packet->header.payload_lenght, stream + offset, sizeof(short int));
offset += sizeof(short int);
for (alloc_size = 1; (stream + offset)[alloc_size - 1] != '\0';
alloc_size++) {
packet->payload.path = malloc(alloc_size)+1;
}
memcpy(packet->payload.path, stream + offset, alloc_size+1);
offset += strlen(packet->payload.path);
memcpy(&packet->payload.size, stream + offset, sizeof(long unsigned int));
offset += (sizeof(long unsigned int));
memcpy(&packet->payload.offset, stream + offset, sizeof(long int));
return packet;
}
strlen(packet->payload.path + 1)looks suspicious, especially compared tostrlen(packet->payload.path) + 1).Are your sure this is what you mean?