aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPlex <thinkplex@riseup.net>2021-10-25 03:41:36 +0200
committerPlex <thinkplex@riseup.net>2021-10-25 03:41:36 +0200
commitd33fabf0c5e1ea9a4e3118836dc459fa971d7a1d (patch)
treef830f516ee909f06a9ea6be6733efe159937001c
parenta5099fdeec6f5e45d88108e28e7c00aa1cf3573c (diff)
socket connected
-rwxr-xr-xmcpingbin18080 -> 20792 bytes
-rw-r--r--mcping.c60
-rw-r--r--mctypes.c15
-rw-r--r--mctypes.h13
4 files changed, 64 insertions, 24 deletions
diff --git a/mcping b/mcping
index eb5e986..324c41c 100755
--- a/mcping
+++ b/mcping
Binary files differ
diff --git a/mcping.c b/mcping.c
index 24e979f..0f28346 100644
--- a/mcping.c
+++ b/mcping.c
@@ -3,30 +3,74 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
#include "mctypes.h"
int main(int argc, char *argv[])
{
+ char *port_s, *address = argv[1];
+ 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");
exit(EXIT_SUCCESS);
}
int opt;
- while ((opt = getopt(argc, argv, "h")) != -1) {
+ 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;
}
}
- varint n, m;
- m = n = writeVarInt(atoi(argv[1]));
- while((*n & 0b10000000) != 0) {
- printf("%02X ", *n);
- n++;
+ if((port_s = strchr(address, ':')) != NULL) {
+ address[strlen(address)-strlen(port_s)] = '\0';
+ port_s++;
+ } else
+ port_s = "25565";
+ if(p_flag == 0) {
+ protocol_ver = 756;
}
- printf("%02X\n", *n);
- printf("%d\n", readVarInt(m));
+ port = atoi(port_s);
+
+ int s;
+ 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 = {
+ writeVarInt(protocol_ver),
+ writeVarInt(strlen(address)),
+ address,
+ port,
+ writeVarInt(1)
+ };
+ // printf("%02X\n", );
return 0;
}
diff --git a/mctypes.c b/mctypes.c
index 6afa230..6a6637a 100644
--- a/mctypes.c
+++ b/mctypes.c
@@ -18,21 +18,6 @@ varint writeVarInt(unsigned int x)
}
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;
- }
-} */
int readVarInt(varint x)
{
diff --git a/mctypes.h b/mctypes.h
index eb19867..dc18063 100644
--- a/mctypes.h
+++ b/mctypes.h
@@ -10,8 +10,19 @@ typedef struct {
typedef struct {
varint length;
varint id;
- char *data;
+ void *data;
} packet;
+typedef struct {
+ varint protocol_version;
+ string server_address;
+ unsigned int server_port;
+ varint next_state;
+}__attribute__((packed)) handshake;
+
+
+typedef long ping;
+typedef long pong;
+
int readVarInt(varint x);
varint writeVarInt(unsigned int x);