diff options
author | Plex <thinkplex@riseup.net> | 2021-10-24 22:50:22 +0200 |
---|---|---|
committer | Plex <thinkplex@riseup.net> | 2021-10-24 22:50:22 +0200 |
commit | dd1779c4c7581b0b863886371f891d9d545d6b27 (patch) | |
tree | 721db5e98337f206e3c20ef82db71ecd6fa3fede | |
parent | a74a07ab26fbda8235011f2819a78a6f314a1ef2 (diff) |
writeVarInt
-rw-r--r-- | Makefile | 14 | ||||
-rwxr-xr-x | mcping | bin | 0 -> 17840 bytes | |||
-rw-r--r-- | mcping.c | 32 | ||||
-rw-r--r-- | mctypes.c | 36 | ||||
-rw-r--r-- | mctypes.h | 18 |
5 files changed, 100 insertions, 0 deletions
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 *~ Binary files differdiff --git a/mcping.c b/mcping.c new file mode 100644 index 0000000..a228114 --- /dev/null +++ b/mcping.c @@ -0,0 +1,32 @@ +#include <string.h> +#include <getopt.h> +#include <stdlib.h> +#include <stdio.h> +#include <sys/types.h> +#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 <stdlib.h> +#include <string.h> +#include <sys/socket.h> + +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 <sys/types.h> +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); |