Skip to content

Instantly share code, notes, and snippets.

@Low-power
Last active May 10, 2020 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Low-power/7febd1a57abc23ffbd35ba543539c196 to your computer and use it in GitHub Desktop.
Save Low-power/7febd1a57abc23ffbd35ba543539c196 to your computer and use it in GitHub Desktop.
A fake Minecraft server that response only to legacy ping requests; useful for quickly verifying your Minecraft server to public server list sites.
/* Minecraft Ping-Only Server
Copyright 2015-2020 Rivoreo
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#ifndef USE_STDIO_FOR_WRITING_SOCKET
#include "syncrw.h"
#endif
#include <signal.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <iconv.h>
#include <errno.h>
static void print_usage(const char *name) {
fprintf(stderr, "Usage: %s [-b [<bind-address>:]<bind-port>] [-V <protocol-version>] -s <version-string> [-m <message-of-the-day>] [-M <max-player-count>]\n", name);
}
static void signal_handler(int sig) {
if(sig != SIGCHLD) return;
while(1) {
pid_t pid = waitpid(-1, NULL, WNOHANG);
if(pid == -1) {
if(errno == EINTR) continue;
if(errno == ECHILD) return;
perror("waitpid");
return;
}
if(!pid) return;
}
}
static int read_byte(int fd) {
uint8_t b;
int s;
do {
s = read(fd, &b, 1);
} while(s < 0 && errno == EINTR);
switch(s) {
case -1:
return -1;
case 0:
return -3;
default:
return b;
}
}
static unsigned int get_online_player_count(int max_count) {
// TODO
return 0;
}
int main(int argc, char **argv) {
const char *bind_address_s = NULL;
int protocol_version = 127;
const char *version_string = NULL;
const char *message_of_the_day = "A Minecraft ping server";
unsigned int max_player_count = 8;
while(1) {
int c = getopt(argc, argv, "b:V:s:m:M:h");
if(c == -1) break;
switch(c) {
case 'b':
bind_address_s = optarg;
break;
case 'V':
{
char *end_p;
protocol_version = strtol(optarg, &end_p, 0);
if(*end_p || protocol_version < 1) {
fprintf(stderr, "%s: Invalid protocol version number '%s'\n", argv[0], optarg);
return -1;
}
}
break;
case 's':
version_string = optarg;
break;
case 'm':
message_of_the_day = optarg;
break;
case 'M':
max_player_count = strtoul(optarg, NULL, 10);
break;
case 'h':
print_usage(argv[0]);
return 0;
case '?':
print_usage(argv[0]);
return -1;
}
}
if(!version_string) {
fprintf(stderr, "%s: Server version string is required; please use option '-s'\n", argv[0]);
print_usage(argv[0]);
return -1;
}
iconv_t utf8_to_utf16be_cd = iconv_open("UTF-16BE", "UTF-8");
if(utf8_to_utf16be_cd == (iconv_t)-1) {
perror("iconv_open");
return 1;
}
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(fd == -1) {
perror("socket");
return 1;
}
static const int reuseaddr = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof reuseaddr) < 0) perror("setsockopt");
if(bind_address_s) {
struct sockaddr_in bindaddr;
bindaddr.sin_family = AF_INET;
char *col = strchr(bind_address_s, ':');
if(col) {
if(col == bind_address_s) {
memset(&bindaddr.sin_addr, 0, sizeof bindaddr.sin_addr);
} else {
*col = 0;
if(inet_pton(AF_INET, bind_address_s, &bindaddr.sin_addr) < 1) {
fprintf(stderr, "%s: Invalid bind address '%s'\n", argv[0], bind_address_s);
return -1;
}
}
bindaddr.sin_port = htons(atoi(col + 1));
} else {
memset(&bindaddr.sin_addr, 0, sizeof bindaddr.sin_addr);
bindaddr.sin_port = htons(atoi(bind_address_s));
}
while(bind(fd, (struct sockaddr *)&bindaddr, sizeof bindaddr) < 0) {
if(errno == EAGAIN || errno == EINTR) continue;
perror("bind");
return 1;
}
}
struct sigaction act = { .sa_handler = signal_handler };
sigaction(SIGCHLD, &act, NULL);
if(listen(fd, 16) < 0) {
perror("listen");
return 1;
}
while(1) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof client_addr;
int c_fd;
do {
c_fd = accept(fd, (struct sockaddr *)&client_addr, &client_addr_len);
} while(c_fd == -1 && errno == EINTR);
if(c_fd == -1) {
perror("accept");
sleep(1);
continue;
}
pid_t pid = fork();
if(pid < 0) {
perror("fork");
sleep(4);
continue;
}
if(pid) {
close(c_fd);
continue;
}
close(fd);
pid = getpid();
{
char from_addr_s[16];
fprintf(stderr, "%d: Received connection from %s:%u\n", (int)pid,
inet_ntop(AF_INET, &client_addr.sin_addr, from_addr_s, sizeof from_addr_s),
(unsigned int)ntohs(client_addr.sin_port));
}
int b = read_byte(c_fd);
switch(b) {
case -1:
fprintf(stderr, "%d: read: %s\n", (int)pid, strerror(errno));
_exit(1);
case -3:
_exit(1);
case 254:
break;
default:
fprintf(stderr, "%d: Unsupported packet type '%hhu'\n", (int)pid, b);
_exit(1);
}
fd_set rfdset;
FD_ZERO(&rfdset);
FD_SET(c_fd, &rfdset);
struct timeval timeout = { .tv_sec = 0, .tv_usec = 500000 };
while(1) switch(select(c_fd + 1, &rfdset, NULL, NULL, &timeout)) {
case -1:
if(errno == EINTR) continue;
perror("select");
_exit(1);
default:
b = read_byte(c_fd);
if(b == -1) {
fprintf(stderr, "%d: read: %s\n", (int)pid, strerror(errno));
_exit(1);
}
size_t output_character_count;
char *output_buffer;
size_t output_buffer_size;
if(b == 1) {
//buffer_size = asprintf(&buffer, "§1%d%c%s%c%s%c%u%c%u", protocol_version, 0, version_string, 0, message_of_the_day, 0, get_online_count(max_player_count), 0, max_player_count);
static const char prefix_o[] = { 0x00, 0xa7, 0x00, 0x31 };
output_character_count = 2;
char protocol_version_s[12];
const char *input_p = protocol_version_s;
size_t input_len = snprintf(protocol_version_s, sizeof protocol_version_s, "%d", protocol_version);
char protocol_version_o[input_len * 2];
char *output_p = protocol_version_o;
size_t output_len = input_len * 2;
size_t len = iconv(utf8_to_utf16be_cd, (char **)&input_p, &input_len, &output_p, &output_len);
if(len == (size_t)-1) {
fprintf(stderr, "%d: iconv: %s\n", (int)pid, strerror(errno));
_exit(1);
}
size_t protocol_version_byte_count = output_p - protocol_version_o;
output_character_count += 1 + protocol_version_byte_count / 2;
input_p = version_string;
input_len = strlen(version_string);
char version_string_o[input_len * 2];
output_p = version_string_o;
output_len = input_len * 2;
len = iconv(utf8_to_utf16be_cd, (char **)&input_p, &input_len, &output_p, &output_len);
if(len == (size_t)-1) {
fprintf(stderr, "%d: iconv: %s\n", (int)pid, strerror(errno));
_exit(1);
}
size_t version_string_byte_count = output_p - version_string_o;
output_character_count += 1 + version_string_byte_count / 2;
input_p = message_of_the_day;
input_len = strlen(message_of_the_day);
char motd_o[input_len * 2];
output_p = motd_o;
output_len = input_len * 2;
len = iconv(utf8_to_utf16be_cd, (char **)&input_p, &input_len, &output_p, &output_len);
if(len == (size_t)-1) {
fprintf(stderr, "%d: iconv: %s\n", (int)pid, strerror(errno));
_exit(1);
}
size_t motd_byte_count = output_p - motd_o;
output_character_count += 1 + motd_byte_count / 2;
char online_player_count[12];
input_p = online_player_count;
input_len = snprintf(online_player_count, sizeof online_player_count, "%u", get_online_player_count(max_player_count));
char online_count_o[input_len * 2];
output_p = online_count_o;
output_len = input_len * 2;
len = iconv(utf8_to_utf16be_cd, (char **)&input_p, &input_len, &output_p, &output_len);
if(len == (size_t)-1) {
fprintf(stderr, "%d: iconv: %s\n", (int)pid, strerror(errno));
_exit(1);
}
size_t online_count_byte_count = output_p - online_count_o;
output_character_count += 1 + online_count_byte_count / 2;
char max_player_count_s[12];
input_p = max_player_count_s;
input_len = snprintf(max_player_count_s, sizeof max_player_count_s, "%u", max_player_count);
char max_count_o[input_len * 2];
output_p = max_count_o;
output_len = input_len * 2;
len = iconv(utf8_to_utf16be_cd, (char **)&input_p, &input_len, &output_p, &output_len);
if(len == (size_t)-1) {
fprintf(stderr, "%d: iconv: %s\n", (int)pid, strerror(errno));
_exit(1);
}
size_t max_count_byte_count = output_p - max_count_o;
output_character_count += 1 + max_count_byte_count / 2;
output_buffer_size = 4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2 + motd_byte_count + 2 + online_count_byte_count + 2 + max_count_byte_count;
output_buffer = malloc(output_buffer_size);
if(!output_buffer) {
fprintf(stderr, "%d: Out of memory\n", (int)pid);
_exit(1);
}
memcpy(output_buffer, prefix_o, 4);
output_buffer[4] = 0;
output_buffer[4 + 1] = 0;
memcpy(output_buffer + 4 + 2, protocol_version_o, protocol_version_byte_count);
output_buffer[4 + 2 + protocol_version_byte_count] = 0;
output_buffer[4 + 2 + protocol_version_byte_count + 1] = 0;
memcpy(output_buffer + 4 + 2 + protocol_version_byte_count + 2, version_string_o, version_string_byte_count);
output_buffer[4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count] = 0;
output_buffer[4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 1] = 0;
memcpy(output_buffer + 4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2, motd_o, motd_byte_count);
output_buffer[4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2 + motd_byte_count] = 0;
output_buffer[4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2 + motd_byte_count + 1] = 0;
memcpy(output_buffer + 4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2 + motd_byte_count + 2, online_count_o, online_count_byte_count);
output_buffer[4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2 + motd_byte_count + 2 + online_count_byte_count] = 0;
output_buffer[4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2 + motd_byte_count + 2 + online_count_byte_count + 1] = 0;
memcpy(output_buffer + 4 + 2 + protocol_version_byte_count + 2 + version_string_byte_count + 2 + motd_byte_count + 2 + online_count_byte_count + 2, max_count_o, max_count_byte_count);
} else {
char *buffer;
int buffer_size;
case 0:
buffer_size = asprintf(&buffer, "%s§%u§%u", message_of_the_day, get_online_player_count(max_player_count), max_player_count);
if(buffer_size < 0) {
fprintf(stderr, "%d: Out of memory\n", (int)pid);
_exit(1);
}
char *input_p = buffer;
size_t input_len = buffer_size;
output_buffer = malloc(input_len * 2);
if(!output_buffer) {
fprintf(stderr, "%d: Out of memory\n", (int)pid);
_exit(1);
}
char *output_p = output_buffer;
size_t output_len = input_len * 2;
size_t len = iconv(utf8_to_utf16be_cd, &input_p, &input_len, &output_p, &output_len);
if(len == (size_t)-1) {
fprintf(stderr, "%d: iconv: %s\n", (int)pid, strerror(errno));
_exit(1);
}
free(buffer);
output_buffer_size = output_p - output_buffer;
output_character_count = output_buffer_size / 2;
}
#ifdef USE_STDIO_FOR_WRITING_SOCKET
// Workaround for some broken clients.
FILE *f = fdopen(c_fd, "wb");
if(!f) {
fprintf(stderr, "%d: fdopen: %d: %s\n", (int)pid, c_fd, strerror(errno));
_exit(1);
}
#endif
uint8_t t = 255;
#ifdef USE_STDIO_FOR_WRITING_SOCKET
if(fwrite(&t, 1, 1, f) < 1) {
int e = errno;
if(!e) e = -1;
fprintf(stderr, "%d: fwrite: %s\n", (int)pid, strerror(e));
#else
if(sync_write(c_fd, &t, 1) < 0) {
fprintf(stderr, "%d: write: %s\n", (int)pid, strerror(errno));
#endif
_exit(1);
}
uint16_t len_be = htons(output_character_count);
#ifdef USE_STDIO_FOR_WRITING_SOCKET
if(fwrite(&len_be, 2, 1, f) < 1) {
int e = errno;
if(!e) e = -1;
fprintf(stderr, "%d: fwrite: %s\n", (int)pid, strerror(e));
#else
if(sync_write(c_fd, &len_be, 2) < 0) {
fprintf(stderr, "%d: write: %s\n", (int)pid, strerror(errno));
#endif
_exit(1);
}
#ifdef USE_STDIO_FOR_WRITING_SOCKET
if(fwrite(output_buffer, output_buffer_size, 1, f) < 1) {
int e = errno;
if(!e) e = -1;
fprintf(stderr, "%d: fwrite: %s\n", (int)pid, strerror(e));
#else
if(sync_write(c_fd, output_buffer, output_buffer_size) < 0) {
fprintf(stderr, "%d: write: %s\n", (int)pid, strerror(errno));
#endif
_exit(1);
}
#ifdef USE_STDIO_FOR_WRITING_SOCKET
if(fclose(f) == EOF) {
fprintf(stderr, "%d: fclose: %s\n", (int)pid, strerror(errno));
#else
if(close(c_fd) < 0) {
fprintf(stderr, "%d: close: %d: %s\n", (int)pid, c_fd, strerror(errno));
#endif
_exit(1);
}
_exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment