From dd1779c4c7581b0b863886371f891d9d545d6b27 Mon Sep 17 00:00:00 2001 From: Plex Date: Sun, 24 Oct 2021 22:50:22 +0200 Subject: writeVarInt --- Makefile | 14 ++++++++++++++ mcping | Bin 0 -> 17840 bytes mcping.c | 32 ++++++++++++++++++++++++++++++++ mctypes.c | 36 ++++++++++++++++++++++++++++++++++++ mctypes.h | 18 ++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 Makefile create mode 100755 mcping create mode 100644 mcping.c create mode 100644 mctypes.c create mode 100644 mctypes.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..38ff01b --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC=clang +CFLAGS=-Wall -g + +mcping: mcping.c mctypes.o + $(CC) $(CFLAGS) $^ -o $@ + +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< + +lint: + indent -kr -ts4 *.c + +clean: + rm -f *.o *~ diff --git a/mcping b/mcping new file mode 100755 index 0000000..6ab6d49 Binary files /dev/null and b/mcping differ diff --git a/mcping.c b/mcping.c new file mode 100644 index 0000000..a228114 --- /dev/null +++ b/mcping.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include "mctypes.h" + +int main(int argc, char *argv[]) +{ + if (argc == 1) { + printf("Usage: mcping [ADDRESS[:PORT]]\nPings a Minecraft server using the PING protocol.\n"); + exit(EXIT_SUCCESS); + } + int opt; + while ((opt = getopt(argc, argv, "h")) != -1) { + switch (opt) { + case 'h': + exit(EXIT_SUCCESS); + default: + break; + } + } + varint n; + n = writeVarInt(atoi(argv[1])); + while((*n & 0b10000000) != 0) { + printf("%02X ", *n); + n++; + } + printf("%02X ", *n); + printf("\n"); + return 0; +} diff --git a/mctypes.c b/mctypes.c new file mode 100644 index 0000000..ad855e4 --- /dev/null +++ b/mctypes.c @@ -0,0 +1,36 @@ +#include "mctypes.h" +#include +#include +#include + +varint writeVarInt(unsigned int x) +{ + // unsigned int v = x; + varint n = malloc(sizeof(u_int8_t)); + unsigned int size = 1; + while(1) { + n = reallocarray(n, size, sizeof(u_int8_t)); + n[size-1] = x & 0x7F; + x >>= 7; + if(x == 0) + return n; + n[size-1] |= 0b10000000; + size++; + } + return n; +} +/* +writeVarInt(varint x, int s) +{ + while (1) { + if ((x & 0xFFFFFF80) == 0) { + if (send(s, &x, 8, 0) == -1) + exit(EXIT_FAILURE); + return; + } + if (send(s, &x, 8, 0) == -1) + exit(EXIT_FAILURE); + + x >>= 7; + } +} */ diff --git a/mctypes.h b/mctypes.h new file mode 100644 index 0000000..3fe4a7f --- /dev/null +++ b/mctypes.h @@ -0,0 +1,18 @@ +#include +typedef u_int8_t *varint; +typedef u_int8_t *varlong; + +typedef struct { + varint length; + char *content; +} string; + +typedef struct { + varint length; + varint id; + char *data;; +} packet; + + +int writeVarInt(varint x); +varint writeVarInt(unsigned int x); -- cgit v1.2.3