aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPlex <thinkplex@riseup.net>2021-10-25 18:41:59 +0200
committerPlex <thinkplex@riseup.net>2021-10-25 18:41:59 +0200
commitd23f7f0aaa97103670e229abd402ad66a6e703de (patch)
tree29a252121af2bb3a93f2ad0b28e0160687b31601
parent927c5b7219041a353010141cea139642ac6ca0bf (diff)
refactoring !!
-rw-r--r--Makefile2
-rwxr-xr-xmcpingbin22720 -> 23832 bytes
-rw-r--r--mcping.c72
-rw-r--r--mctypes.c12
-rw-r--r--mctypes.h2
5 files changed, 20 insertions, 68 deletions
diff --git a/Makefile b/Makefile
index 0682d88..2541a16 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CC=clang
CFLAGS=-Wall -g -D_DEBUG
-mcping: mcping.c mctypes.o
+mcping: mcping.c mctypes.o mcnetwork.o
$(CC) $(CFLAGS) $^ -o $@
%.o: %.c %.h
diff --git a/mcping b/mcping
index bb3c964..b1c2648 100755
--- a/mcping
+++ b/mcping
Binary files differ
diff --git a/mcping.c b/mcping.c
index dfc1676..c9372fa 100644
--- a/mcping.c
+++ b/mcping.c
@@ -2,25 +2,28 @@
#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 "mctypes.h"
+#include "mcnetwork.h"
+
+
+
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;
+ size_t json_size;
int protocol_ver;
int p_flag = 0;
if (argc == 1) {
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.");
+ ("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");
exit(EXIT_SUCCESS);
}
int opt;
@@ -51,61 +54,12 @@ int main(int argc, char *argv[])
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
perror("bad socket\n");
- struct addrinfo *mc_info = malloc(sizeof(struct addrinfo));
- memset(mc_info, 0, sizeof(struct addrinfo));
- struct addrinfo hints;
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- 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
-
- if (connect(s, (struct sockaddr *) &mc_sock, sizeof(mc_sock)) < 0)
- perror("connection failed\n");
-
- handshake hs = {
- to_varint(protocol_ver),
- {to_varint(strlen(address)), address},
- port,
- to_varint(1)
- };
-
- 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);
+ 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)
+ // 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 6a2b4d6..125a1aa 100644
--- a/mctypes.c
+++ b/mctypes.c
@@ -1,8 +1,6 @@
#include "mctypes.h"
#include <stdlib.h>
#include <string.h>
-#include <sys/socket.h>
-
varint to_varint(unsigned int x)
{
@@ -33,7 +31,7 @@ int from_varint(varint x)
return (int) res;
}
-size_t len_varint(varint x)
+size_t size_varint(varint x)
{
size_t v_size;
for (v_size = 1; (x[v_size - 1] & 0x80) != 0; v_size++);
@@ -46,12 +44,12 @@ size_t serialize_handshake(handshake hs, void **buf)
size_t varint_s;
*buf = malloc(size);
- varint_s = len_varint(hs.protocol_version);
+ varint_s = size_varint(hs.protocol_version);
*buf = realloc(*buf, size+varint_s);
memcpy(*buf + size, hs.protocol_version, varint_s);
size += varint_s;
- varint_s = len_varint(hs.server_address.length);
+ varint_s = size_varint(hs.server_address.length);
*buf = realloc(*buf, size+varint_s);
memcpy(*buf + size, hs.server_address.length, varint_s);
size += varint_s;
@@ -64,7 +62,7 @@ size_t serialize_handshake(handshake hs, void **buf)
memcpy(*buf + size, &hs.server_port, sizeof(unsigned short));
size += sizeof(unsigned short);
- varint_s = len_varint(hs.next_state);
+ varint_s = size_varint(hs.next_state);
*buf = realloc(*buf, size+varint_s);
memcpy(*buf + size, hs.next_state, varint_s);
size += varint_s;
@@ -74,7 +72,7 @@ size_t serialize_handshake(handshake hs, void **buf)
size_t serialize_packet(packet p, void **buf)
{
- size_t v_size = len_varint(p.length);
+ size_t v_size = size_varint(p.length);
size_t p_size = from_varint(p.length) + v_size; // total size of packet
size_t size = 0;
diff --git a/mctypes.h b/mctypes.h
index 45d1ed6..04362e2 100644
--- a/mctypes.h
+++ b/mctypes.h
@@ -31,7 +31,7 @@ typedef long pong;
int from_varint(varint x);
varint to_varint(unsigned int x);
-size_t len_varint(varint x);
+size_t size_varint(varint x);
size_t serialize_handshake(handshake hs, void **buf);
size_t serialize_packet(packet p, void **buf);