diff options
Diffstat (limited to 'mcping.c')
-rw-r--r-- | mcping.c | 72 |
1 files changed, 13 insertions, 59 deletions
@@ -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; } |