mcpinger/network_utils.cpp
2024-05-08 10:24:32 +02:00

108 lines
3.7 KiB
C++

#include "network_utils.h"
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include "protocol_utils.h"
int sendData(int sockfd, const std::vector<unsigned char>& data) {
ssize_t sent = send(sockfd, data.data(), data.size(), 0);
if (sent < 0) {
perror("Error sending data");
return ERROR_SENDING_DATA;
}
return SUCCESS;
}
int receiveData(int sockfd, std::vector<unsigned char>& buffer) {
buffer.resize(1024);
ssize_t bytesReceived = recv(sockfd, buffer.data(), buffer.size(), 0);
if (bytesReceived < 0) {
perror("Error receiving data");
return ERROR_RECEIVING_DATA;
}
buffer.resize(bytesReceived);
return SUCCESS;
}
int pingMinecraftServer(const std::string& hostname, int port, const std::string& virtualHostname, int virtualPort, int protocolVersionIn, int nextStateIn) {
struct addrinfo hints{}, *res, *result;
int sockfd, err;
(void)memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
err = getaddrinfo(hostname.c_str(), std::to_string(port).c_str(), &hints, &result);
if (err != 0) {
std::cerr << "Error resolving hostname: " << gai_strerror(err) << std::endl;
return ERROR_RESOLVING_HOSTNAME;
}
for (res = result; res != nullptr; res = res->ai_next) {
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == -1) continue;
if (connect(sockfd, res->ai_addr, res->ai_addrlen) != -1) break;
(void)close(sockfd);
}
if (res == nullptr) {
std::cerr << "Could not connect to any address" << std::endl;
freeaddrinfo(result);
return ERROR_CONNECTION_FAILED;
}
freeaddrinfo(result);
// Handshake
std::vector<unsigned char> packet;
packet.push_back(0x00U); // Packet ID for handshake
std::vector<unsigned char> protocolVersion = encodeVarint(protocolVersionIn);
std::vector<unsigned char> serverAddress = encodeString(virtualHostname);
std::vector<unsigned char> serverPort = {static_cast<unsigned char>((virtualPort >> 8) & 0xFFU), static_cast<unsigned char>(virtualPort & 0xFFU) };
std::vector<unsigned char> nextState = encodeVarint(nextStateIn);
(void)packet.insert(packet.end(), protocolVersion.begin(), protocolVersion.end());
(void)packet.insert(packet.end(), serverAddress.begin(), serverAddress.end());
(void)packet.insert(packet.end(), serverPort.begin(), serverPort.end());
(void)packet.insert(packet.end(), nextState.begin(), nextState.end());
std::vector<unsigned char> handshakeLength = encodeVarint(packet.size());
(void)packet.insert(packet.begin(), handshakeLength.begin(), handshakeLength.end());
if (sendData(sockfd, packet) != SUCCESS) {
(void)close(sockfd);
return ERROR_SENDING_DATA;
}
// Status request
std::vector<unsigned char> statusRequest = {0x01U, 0x00U}; // Packet length and Packet ID for status request
if (sendData(sockfd, statusRequest) != SUCCESS) {
(void)close(sockfd);
return ERROR_SENDING_DATA;
}
// Read status response
std::vector<unsigned char> response;
if (receiveData(sockfd, response) != SUCCESS) {
(void)close(sockfd);
return ERROR_RECEIVING_DATA;
}
// Extract JSON string (assuming it starts from the position of the first '{' found in the response)
auto pos = std::find(response.begin(), response.end(), '{');
if (pos != response.end()) {
std::string json(pos, response.end());
std::cout << json << std::endl;
} else {
std::cerr << "Failed to find JSON response in server data." << std::endl;
}
(void)close(sockfd);
return SUCCESS;
}