For a school project, I have to develop a TFTP server in C. I have to build a packet in a array of char like this :
2 bytes 2 bytes n bytes
+--------+-----------+--------+
| CODE | Block # | Data |
+--------+-----------+--------+
Here is how I build this packet:
int tftp_make_data(char *buffer, size_t *length, uint16_t block, const char *data, size_t n) {
memset(buffer, 0, *length);
*length = 0;
*(buffer) = DATA;
*length += sizeof(uint16_t);
*(buffer + *length) = block;
*length += sizeof(uint16_t);
memcpy(buffer + *length, data, n);
*length += n;
return 0;
}
This function fill buffer
with the packet content and fill length
with the size of the packet.
This works fine if block
is lower than 128. If it's greeter than 128, it becomes -128.
Can you help me?