From 53a085962964f4db118619a5e2156c901aba55a6 Mon Sep 17 00:00:00 2001 From: Plex Date: Mon, 25 Oct 2021 16:35:18 +0200 Subject: serialization underway --- .gitignore | 2 ++ Makefile | 2 +- mcping | Bin 20792 -> 21816 bytes mcping.c | 48 ++++++++++++++++++++++++++++-------------------- mctypes.c | 45 +++++++++++++++++++++++++++++++++++++++------ mctypes.h | 4 +++- 6 files changed, 73 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index c6127b3..1a7b40e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +mcping + # Prerequisites *.d diff --git a/Makefile b/Makefile index 38ff01b..0682d88 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=clang -CFLAGS=-Wall -g +CFLAGS=-Wall -g -D_DEBUG mcping: mcping.c mctypes.o $(CC) $(CFLAGS) $^ -o $@ diff --git a/mcping b/mcping index 324c41c..56aee3d 100755 Binary files a/mcping and b/mcping differ diff --git a/mcping.c b/mcping.c index 0f28346..20fbb68 100644 --- a/mcping.c +++ b/mcping.c @@ -11,39 +11,41 @@ int main(int argc, char *argv[]) { - char *port_s, *address = argv[1]; + char *port_s, *address; unsigned short port; int protocol_ver; int p_flag = 0; if (argc == 1) { - printf("Usage: mcping [ADDRESS[:PORT]]\nPings a Minecraft server using the PING protocol.\n"); + printf + ("Usage: mcping ADDRESS[:PORT]\nPings a Minecraft server using the PING protocol.\n\n -h\tdisplay this help and exit\n -p [PROTOCOL NUMBER]\tUses specified protocol number."); exit(EXIT_SUCCESS); } int opt; while ((opt = getopt(argc, argv, "hp:")) != -1) { switch (opt) { - case 'h': - exit(EXIT_SUCCESS); - case 'p': - protocol_ver = atoi(optarg); - p_flag = 1; - break; - default: - break; + case 'h': + exit(EXIT_SUCCESS); + case 'p': + protocol_ver = atoi(optarg); + p_flag = 1; + break; + default: + break; } } - if((port_s = strchr(address, ':')) != NULL) { - address[strlen(address)-strlen(port_s)] = '\0'; + address = argv[1]; + if ((port_s = strchr(address, ':')) != NULL) { + address[strlen(address) - strlen(port_s)] = '\0'; port_s++; } else port_s = "25565"; - if(p_flag == 0) { + if (p_flag == 0) { protocol_ver = 756; } port = atoi(port_s); int s; - if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) + if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("bad socket\n"); struct addrinfo *mc_info = malloc(sizeof(struct addrinfo)); @@ -53,24 +55,30 @@ int main(int argc, char *argv[]) hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; - if(getaddrinfo(address, "80", &hints, &mc_info) != 0) + if (getaddrinfo(address, "80", &hints, &mc_info) != 0) perror("bad addrinfo\n"); struct sockaddr_in mc_sock; mc_sock.sin_family = AF_INET; mc_sock.sin_port = htons(port); - memcpy(&mc_sock.sin_addr, mc_info->ai_addr->sa_data+2, mc_info->ai_addrlen); // stupidity + memcpy(&mc_sock.sin_addr, mc_info->ai_addr->sa_data + 2, mc_info->ai_addrlen); // stupidity - if(connect(s, (struct sockaddr *)&mc_sock, sizeof(mc_sock)) < 0) + if (connect(s, (struct sockaddr *) &mc_sock, sizeof(mc_sock)) < 0) perror("connection failed\n"); handshake hs = { writeVarInt(protocol_ver), - writeVarInt(strlen(address)), - address, + {writeVarInt(strlen(address)), address}, port, writeVarInt(1) }; - // printf("%02X\n", ); + + char u; + char* raw_hs = &u; + 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]); + } return 0; } diff --git a/mctypes.c b/mctypes.c index 6a6637a..30319ee 100644 --- a/mctypes.c +++ b/mctypes.c @@ -7,13 +7,13 @@ varint writeVarInt(unsigned int x) { varint n = malloc(sizeof(u_int8_t)); unsigned int size = 1; - while(1) { + while (1) { n = reallocarray(n, size, sizeof(u_int8_t)); - n[size-1] = x & 0x7F; + n[size - 1] = x & 0x7F; x >>= 7; - if(x == 0) + if (x == 0) return n; - n[size-1] |= 0x80; + n[size - 1] |= 0x80; size++; } return n; @@ -24,9 +24,42 @@ int readVarInt(varint x) unsigned int offset = 0; unsigned int res = 0; do { - if(offset == 5) exit(EXIT_FAILURE); - res |= (x[offset] & 0b01111111) << offset*7; + if (offset == 5) + exit(EXIT_FAILURE); + res |= (x[offset] & 0b01111111) << offset * 7; offset++; } while ((x[offset-1] & 0b10000000) != 0); return (int) res; } + +size_t serializeHandshake(handshake hs, void *buf) +{ + size_t size = 0; + size_t vi_size; + 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; + + 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; + + 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+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; + + return size; +} diff --git a/mctypes.h b/mctypes.h index dc18063..8230b53 100644 --- a/mctypes.h +++ b/mctypes.h @@ -16,7 +16,7 @@ typedef struct { typedef struct { varint protocol_version; string server_address; - unsigned int server_port; + unsigned short server_port; varint next_state; }__attribute__((packed)) handshake; @@ -26,3 +26,5 @@ 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); -- cgit v1.2.3