aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--README.md4
-rwxr-xr-xmcpingbin23832 -> 25088 bytes
-rw-r--r--mcping.c37
-rw-r--r--mctypes.c44
-rw-r--r--mctypes.h3
6 files changed, 64 insertions, 26 deletions
diff --git a/Makefile b/Makefile
index 2541a16..3119e48 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CC=clang
-CFLAGS=-Wall -g -D_DEBUG
+CFLAGS=-Wall -g
mcping: mcping.c mctypes.o mcnetwork.o
$(CC) $(CFLAGS) $^ -o $@
diff --git a/README.md b/README.md
index 78eda63..57b7d3d 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,6 @@
# mc-comm
Queries various information about Minecraft servers.
+
+mc-comm is a package for querying information about a Minecraft server on the commandline.
+
+`mcping` implements the [Server List Ping](https://wiki.vg/Server_List_Ping).
diff --git a/mcping b/mcping
index b1c2648..5d04d80 100755
--- a/mcping
+++ b/mcping
Binary files differ
diff --git a/mcping.c b/mcping.c
index c9372fa..e8adcf2 100644
--- a/mcping.c
+++ b/mcping.c
@@ -2,25 +2,16 @@
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <arpa/inet.h>
+#include <unistd.h>
#include "mctypes.h"
#include "mcnetwork.h"
-
-
-
int main(int argc, char *argv[])
{
char *port_s, *address;
unsigned short port;
- size_t json_size;
- int protocol_ver;
- int p_flag = 0;
+ unsigned int protocol_ver, p_flag = 0;
if (argc == 1) {
printf
("Usage: mcping ADDRESS[:PORT]\nPings a Minecraft server using the PING protocol.\n\n -h\t\t\tdisplay this help and exit\n -p PROTOCOL NUMBER\tUses specified protocol number.\n");
@@ -56,10 +47,28 @@ int main(int argc, char *argv[])
connect_mc_server(s, address, port);
send_handshake(s, protocol_ver, address, port, 1);
- send_packet(s, 0x0, 0, NULL); // request packet (0x0 with no fields)
+ send_packet(s, 0x0, 0, NULL); // request packet (0x0 with no fields)
+
+ packet response = recv_packet(s);
+#ifdef _DEBUG
+ printf("length:\t%u\n", from_varint(response.length));
+ printf("id:\t%d\n", from_varint(response.id));
+ for (int i = 0; i < from_varint(response.length) - size_varint(response.id); ++i) {
+ printf("%02X ", ((unsigned char*) response.data)[i]);
+ }
+ printf("\n");
+#endif
+ string json = deserialize_string(&response.data);
+
+#ifdef _DEBUG
+ printf("json length:\t%u\n", from_varint(json.length));
+#endif
+
+ for (int i = 0; i < from_varint(json.length); ++i) {
+ printf("%c", json.content[i]);
+ }
+ printf("\n");
- // json_size = from_varint(recv_varint(s));
- // printf("%lu\n", json_size);
close(s);
return 0;
}
diff --git a/mctypes.c b/mctypes.c
index 125a1aa..c4df793 100644
--- a/mctypes.c
+++ b/mctypes.c
@@ -25,9 +25,9 @@ int from_varint(varint x)
do {
if (offset == 5)
exit(EXIT_FAILURE);
- res |= (x[offset] & 0b01111111) << offset * 7;
+ res |= (x[offset] & 0x7F) << offset * 7;
offset++;
- } while ((x[offset-1] & 0b10000000) != 0);
+ } while ((x[offset-1] & 0x80) != 0);
return (int) res;
}
@@ -45,25 +45,26 @@ size_t serialize_handshake(handshake hs, void **buf)
*buf = malloc(size);
varint_s = size_varint(hs.protocol_version);
- *buf = realloc(*buf, size+varint_s);
+ *buf = realloc(*buf, size + varint_s);
memcpy(*buf + size, hs.protocol_version, varint_s);
size += varint_s;
varint_s = size_varint(hs.server_address.length);
- *buf = realloc(*buf, size+varint_s);
+ *buf = realloc(*buf, size + varint_s);
memcpy(*buf + size, hs.server_address.length, varint_s);
size += varint_s;
- *buf = realloc(*buf, size+from_varint(hs.server_address.length));
- memcpy(*buf + size, hs.server_address.content, from_varint(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));
+ *buf = realloc(*buf, size + sizeof(unsigned short));
memcpy(*buf + size, &hs.server_port, sizeof(unsigned short));
size += sizeof(unsigned short);
varint_s = size_varint(hs.next_state);
- *buf = realloc(*buf, size+varint_s);
+ *buf = realloc(*buf, size + varint_s);
memcpy(*buf + size, hs.next_state, varint_s);
size += varint_s;
@@ -73,7 +74,7 @@ size_t serialize_handshake(handshake hs, void **buf)
size_t serialize_packet(packet p, void **buf)
{
size_t v_size = size_varint(p.length);
- size_t p_size = from_varint(p.length) + v_size; // total size of packet
+ size_t p_size = from_varint(p.length) + v_size; // total size of packet
size_t size = 0;
*buf = malloc(p_size);
@@ -81,11 +82,32 @@ size_t serialize_packet(packet p, void **buf)
size += v_size;
for (v_size = 1; (p.id[v_size - 1] & 0x80) != 0; v_size++);
- memcpy(*buf+size, p.id, v_size);
+ memcpy(*buf + size, p.id, v_size);
size += v_size;
- memcpy(*buf+size, p.data, from_varint(p.length) - v_size);
+ memcpy(*buf + size, p.data, from_varint(p.length) - v_size);
return p_size;
}
+varint deserialize_varint(void **raw)
+{
+ varint v = NULL;
+ unsigned short i = 0;
+ do {
+ v = reallocarray(v, i+1, sizeof(u_int8_t));
+ memcpy(v+i, *raw, sizeof(u_int8_t));
+ (*raw)++;
+ i++;
+ } while ((v[i-1] & 0x80) != 0);
+ return v;
+}
+
+string deserialize_string(void **raw)
+{
+ string s;
+ s.length = deserialize_varint(raw);
+ s.content = malloc(from_varint(s.length));
+ memcpy(s.content, *raw, from_varint(s.length));
+ return s;
+};
diff --git a/mctypes.h b/mctypes.h
index 04362e2..9b2de0d 100644
--- a/mctypes.h
+++ b/mctypes.h
@@ -36,4 +36,7 @@ size_t size_varint(varint x);
size_t serialize_handshake(handshake hs, void **buf);
size_t serialize_packet(packet p, void **buf);
+varint deserialize_varint(void **raw);
+string deserialize_string(void **raw);
+
#endif // MCTYPES_H