diff options
author | Plex <thinkplex@riseup.net> | 2021-10-25 17:30:45 +0200 |
---|---|---|
committer | Plex <thinkplex@riseup.net> | 2021-10-25 17:30:45 +0200 |
commit | 927c5b7219041a353010141cea139642ac6ca0bf (patch) | |
tree | c51153dbf7b21bb9ebc63f8722ba9a2f51739c4f | |
parent | d25c1b105da1606d3e372974d7546c1f7726a198 (diff) |
serialize_packet + refactor
-rwxr-xr-x | mcping | bin | 21856 -> 22720 bytes | |||
-rw-r--r-- | mcping.c | 43 | ||||
-rw-r--r-- | mctypes.c | 66 | ||||
-rw-r--r-- | mctypes.h | 17 |
4 files changed, 95 insertions, 31 deletions
Binary files differ @@ -13,6 +13,9 @@ int main(int argc, char *argv[]) { char *port_s, *address; unsigned short port; + size_t p_size, v_size; + unsigned char *raw_handshake, *raw_packet; + varint packet_id; int protocol_ver; int p_flag = 0; if (argc == 1) { @@ -67,18 +70,42 @@ int main(int argc, char *argv[]) perror("connection failed\n"); handshake hs = { - writeVarInt(protocol_ver), - {writeVarInt(strlen(address)), address}, + to_varint(protocol_ver), + {to_varint(strlen(address)), address}, port, - writeVarInt(1) + to_varint(1) }; - unsigned char *raw_hs; - size_t hs_size = serializeHandshake(hs, &raw_hs); - printf("%lu\n", hs_size); - for (int i = 0; i < hs_size; ++i) { - printf("%02X ", raw_hs[i]); + size_t handshake_size = serialize_handshake(hs, (void**)&raw_handshake); + // printf("%lu\n", hs_size); + printf(" "); + for (int i = 0; i < handshake_size; ++i) { + printf("%02X ", raw_handshake[i]); } printf("\n"); + packet_id = to_varint(0x00); + for (v_size = 1; (packet_id[v_size - 1] & 0x80) != 0; v_size++); + + packet handshake_packet = { + to_varint(handshake_size + v_size), + packet_id, + (void*) raw_handshake + }; + + p_size = serialize_packet(handshake_packet, (void**)&raw_packet); + for (int i = 0; i < p_size; ++i) { + printf("%02X ", raw_packet[i]); + } + printf("\n"); + + for (v_size = 1; (packet_id[v_size - 1] & 0x80) != 0; v_size++); + packet request_packet = { + to_varint(len_varint(to_varint(0x0))), + to_varint(0x0), + NULL + }; + + p_size = serialize_packet(request_packet, (void**)&raw_packet); + return 0; } @@ -3,7 +3,8 @@ #include <string.h> #include <sys/socket.h> -varint writeVarInt(unsigned int x) + +varint to_varint(unsigned int x) { varint n = malloc(sizeof(u_int8_t)); unsigned int size = 1; @@ -19,7 +20,7 @@ varint writeVarInt(unsigned int x) return n; } -int readVarInt(varint x) +int from_varint(varint x) { unsigned int offset = 0; unsigned int res = 0; @@ -32,34 +33,61 @@ int readVarInt(varint x) return (int) res; } -size_t serializeHandshake(handshake hs, void **buf) +size_t len_varint(varint x) +{ + size_t v_size; + for (v_size = 1; (x[v_size - 1] & 0x80) != 0; v_size++); + return v_size; +} + +size_t serialize_handshake(handshake hs, void **buf) { size_t size = 0; - size_t vi_size; + size_t varint_s; *buf = malloc(size); - for (vi_size = 1; (hs.protocol_version[vi_size - 1] & 0x80) != 0; vi_size++); - *buf = realloc(*buf, size+vi_size); - memcpy(*buf + size, hs.protocol_version, vi_size); - size += vi_size; + varint_s = len_varint(hs.protocol_version); + *buf = realloc(*buf, size+varint_s); + memcpy(*buf + size, hs.protocol_version, varint_s); + size += varint_s; - for (vi_size = 1; (hs.server_address.length[vi_size - 1] & 0x80) != 0; vi_size++); - *buf = realloc(*buf, size+vi_size); - memcpy(*buf + size, hs.server_address.length, vi_size); - size += vi_size; + varint_s = len_varint(hs.server_address.length); + *buf = realloc(*buf, size+varint_s); + memcpy(*buf + size, hs.server_address.length, varint_s); + size += varint_s; - *buf = realloc(*buf, size+readVarInt(hs.server_address.length)); - memcpy(*buf + size, hs.server_address.content, readVarInt(hs.server_address.length)); - size += readVarInt(hs.server_address.length); + *buf = realloc(*buf, size+from_varint(hs.server_address.length)); + memcpy(*buf + size, hs.server_address.content, from_varint(hs.server_address.length)); + size += from_varint(hs.server_address.length); *buf = realloc(*buf, size+sizeof(unsigned short)); memcpy(*buf + size, &hs.server_port, sizeof(unsigned short)); size += sizeof(unsigned short); - for (vi_size = 1; (hs.next_state[vi_size - 1] & 0x80) != 0; vi_size++); - *buf = realloc(*buf, size+vi_size); - memcpy(*buf + size, hs.next_state, vi_size); - size += vi_size; + varint_s = len_varint(hs.next_state); + *buf = realloc(*buf, size+varint_s); + memcpy(*buf + size, hs.next_state, varint_s); + size += varint_s; return size; } + +size_t serialize_packet(packet p, void **buf) +{ + size_t v_size = len_varint(p.length); + size_t p_size = from_varint(p.length) + v_size; // total size of packet + size_t size = 0; + + *buf = malloc(p_size); + memcpy(*buf, p.length, v_size); + size += v_size; + + for (v_size = 1; (p.id[v_size - 1] & 0x80) != 0; v_size++); + memcpy(*buf+size, p.id, v_size); + size += v_size; + + memcpy(*buf+size, p.data, from_varint(p.length) - v_size); + + return p_size; +} + @@ -1,4 +1,9 @@ +#ifndef MCTYPES_H +#define MCTYPES_H + #include <sys/types.h> + + typedef u_int8_t *varint; typedef u_int8_t *varlong; @@ -24,7 +29,11 @@ typedef struct { typedef long ping; typedef long pong; -int readVarInt(varint x); -varint writeVarInt(unsigned int x); -size_t serializeHandshake(handshake hs, void **buf); -size_t serializePacket(packet p, void *buf); +int from_varint(varint x); +varint to_varint(unsigned int x); +size_t len_varint(varint x); + +size_t serialize_handshake(handshake hs, void **buf); +size_t serialize_packet(packet p, void **buf); + +#endif // MCTYPES_H |