From 42939a6a1c6e5ca9d544ee8e066ec087621a7830 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Mon, 15 Apr 2019 18:27:31 +0300 Subject: [PATCH 01/52] Markdown headers fixed. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4b37ab2..fbdadaa 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -###Installing: +### Installing: from sources: ```sh @@ -15,7 +15,7 @@ You can also download precompiled binaries*: https://github.com/Tiiffi/mcrcon/re --- -###Usage: +### Usage: mcrcon [OPTIONS]... [COMMANDS]... Sends rcon commands to Minecraft server. @@ -47,7 +47,7 @@ Example: --- -###Enable rcon on server +### Enable rcon on server Remember to enable rcon by adding following lines to ```server.properties``` file. ``` enable-rcon=true @@ -57,7 +57,7 @@ rcon.password=your_rcon_pasword --- -####Contact: +#### Contact: * WWW: https://github.com/Tiiffi/mcrcon/ * MAIL: tiiffi_at_gmail_dot_com From 5dc3de375a11fcca12d34156ccf00b0bbe73b823 Mon Sep 17 00:00:00 2001 From: srakitnican Date: Fri, 3 May 2019 16:33:25 +0200 Subject: [PATCH 02/52] Makefile: Use DESTDIR --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 25d14c5..92a18f6 100644 --- a/Makefile +++ b/Makefile @@ -35,13 +35,13 @@ $(EXENAME): mcrcon.c ifneq ($(OS), Windows_NT) .PHONY: install install: - $(INSTALL) -vD $(EXENAME) $(PREFIX)/bin/$(EXENAME) - $(INSTALL) -vD -m 0644 mcrcon.1 $(PREFIX)/share/man/man1/mcrcon.1 + $(INSTALL) -vD $(EXENAME) $(DESTDIR)$(PREFIX)/bin/$(EXENAME) + $(INSTALL) -vD -m 0644 mcrcon.1 $(DESTDIR)$(PREFIX)/share/man/man1/mcrcon.1 @echo "\nmcrcon installed. Run 'make uninstall' if you want to uninstall.\n" .PHONY: uninstall uninstall: - rm -f $(PREFIX)/bin/$(EXENAME) $(PREFIX)/share/man/man1/mcrcon.1 + rm -f $(DESTDIR)$(PREFIX)/bin/$(EXENAME) $(DESTDIR)$(PREFIX)/share/man/man1/mcrcon.1 @echo "\nmcrcon uninstalled.\n" endif From 67a665a2e46684d755c84ab41e3b2372b6cafec2 Mon Sep 17 00:00:00 2001 From: Thomas Ingram Date: Sun, 30 Jun 2019 23:51:53 -0400 Subject: [PATCH 03/52] Fixed markdown headings in changelog --- CHANGELOG.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18613fb..45030d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ -####Version history: -######0.6.1 +#### Version history: +###### 0.6.1 - Color coding fixed * Thanks to Hagb @ Github -######0.6.0 +###### 0.6.0 - Version numbering changed to more sane system (0.0.5 -> 0.6.0) - Fixed munged output - Support for using environment variables to set some basic options @@ -14,7 +14,7 @@ - Man page added - Proper makefile added -######0.0.5 +###### 0.0.5 - IPv6 support! * Thanks to 'Tanja84dk' for addressing the real need of IPv6. @@ -34,20 +34,20 @@ - Client now tries to clean the incoming socket data if last package was out of spec. -######0.0.4 +###### 0.0.4 - Reverted back to default getopts options error handler (opterr = 1). Custom error handler requires rewriting. - Some cosmetic changes in program output strings. - Program usage(); function now waits for enter before exiting on Windows. -######0.0.3 +###### 0.0.3 - Colors are now supported on Windows too! - Terminal mode is now triggered with "-t" flag. "-i" flag still works for backwards compatibility. - Bug fixes (Packet size check always evaluating false and color validity check always evaluating true). -######0.0.2 +###### 0.0.2 - License changed from 'ISC License' to 'zlib/libpng License'. - Bug fixes & code cleanups - Interactive mode (-i flag). Client acts as interactive terminal. @@ -55,14 +55,14 @@ If connecting or authentication fails, the return value is -1. - Colors are now enabled by default. Now '-c' flag disables the color support. -######0.0.1 +###### 0.0.1 - Added experimental support for bukkit colors. Should work with any sh compatible shell. - Packet string data limited to max 2048 (DATA_BUFFSIZE) bytes. No idea how Minecraft handles multiple rcon packets. If someone knows, please mail me so I can implement it. -####TODO: +#### TODO: - Make the receive buffer dynamic?? - Change some of the packet size issues to fatal errors. - Code cleanups. From 872ddccbfc0aad5f85680a829b96db9db2595971 Mon Sep 17 00:00:00 2001 From: Thomas Ingram Date: Mon, 1 Jul 2019 00:03:41 -0400 Subject: [PATCH 04/52] Set default address to localhost --- CHANGELOG.md | 4 ++++ mcrcon.1 | 2 +- mcrcon.c | 13 +++++-------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45030d8..32aed56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ #### Version history: + +###### 0.6.2 + - Set default address to localhost + ###### 0.6.1 - Color coding fixed * Thanks to Hagb @ Github diff --git a/mcrcon.1 b/mcrcon.1 index 51ee6ba..4f9b26c 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -17,7 +17,7 @@ It is well suited for remote administration and server maintenance scripts. .IP -h Print usage .IP -H -Server address +Server address (default is localhost) .IP -P Port (default is 25575) .IP -p diff --git a/mcrcon.c b/mcrcon.c index 1d94ddd..62cd976 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -147,7 +147,10 @@ int main(int argc, char *argv[]) if (!port) port = "25575"; - if(argc < 2 && host == NULL && pass == NULL) + if (!host) + host = "localhost"; + + if(argc < 1 && pass == NULL) usage(); // default getopt error handler enabled @@ -185,12 +188,6 @@ int main(int argc, char *argv[]) } } - if (host == NULL) - { - fputs("Host not defined (-H flag). Try 'mcrcon -h' or 'man mcrcon' for more information.\n\n", stdout); - return 0; - } - if (pass == NULL) { fputs("Password not defined (-p flag). Try 'mcrcon -h' 'man mcrcon' for more information.\n\n", stdout); @@ -240,7 +237,7 @@ void usage(void) "Sends rcon commands to Minecraft server.\n\n" "Option:\n" " -h\t\tPrint usage\n" - " -H\t\tServer address\n" + " -H\t\tServer address (default is localhost)\n" " -P\t\tPort (default is 25575)\n" " -p\t\tRcon password\n" " -t\t\tInteractive terminal mode\n" From 60fa0e42d8ce0f144828e1fe3fa520ea42d945c1 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 20 Sep 2019 13:20:35 +0300 Subject: [PATCH 05/52] Added 'global' prefix for global variables and fixed 'rsock' variable shadowing. --- mcrcon.c | 112 +++++++++++++++++++++++++++---------------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 62cd976..ab33748 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2016, Tiiffi gmail_dot_com> + * Copyright (c) 2012-2019, Tiiffi gmail_dot_com> * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -32,7 +32,7 @@ #ifdef _WIN32 // for name resolving on windows - // enable this if you get compiler whine about getaddrinfo on windows + // enable this if you get compiler whine about getaddrinfo() on windows //#define _WIN32_WINNT 0x0501 #include @@ -73,46 +73,46 @@ typedef struct _rc_packet { // =================================== // endianness related functions -bool is_bigendian(void); -int32_t reverse_int32(int32_t n); +bool is_bigendian(void); +int32_t reverse_int32(int32_t n); // Network related functions #ifdef _WIN32 void net_init_WSA(void); #endif void net_close(int sd); -int net_connect(const char *host, const char *port); -int net_send(int sd, const uint8_t *buffer, size_t size); -int net_send_packet(int sd, rc_packet *packet); +int net_connect(const char *host, const char *port); +int net_send(int sd, const uint8_t *buffer, size_t size); +int net_send_packet(int sd, rc_packet *packet); rc_packet* net_recv_packet(int sd); -int net_clean_incoming(int sd, int size); +int net_clean_incoming(int sd, int size); // Misc stuff -void usage(void); +void usage(void); #ifndef _WIN32 -void print_color(int color); +void print_color(int color); #endif -int get_line(char *buffer, int len); -int run_terminal_mode(int rsock); -int run_commands(int argc, char *argv[]); +int get_line(char *buffer, int len); +int run_terminal_mode(int sock); +int run_commands(int argc, char *argv[]); // Rcon protocol related functions rc_packet* packet_build(int id, int cmd, char *s1); uint8_t *packet_build_malloc(size_t *size, int32_t id, int32_t cmd, char *string); void packet_print(rc_packet *packet); -int rcon_auth(int rsock, char *passwd); -int rcon_command(int rsock, char *command); +int rcon_auth(int sock, char *passwd); +int rcon_command(int sock, char *command); // ============================================= // GLOBAL VARIABLES // ============================================= -static int raw_output = 0; -static int silent_mode = 0; -static int print_colors = 1; -static int connection_alive = 1; -static int rsock; +static int global_raw_output = 0; +static int global_silent_mode = 0; +static int global_print_colors = 1; +static int global_connection_alive = 1; +static int global_rsock; #ifdef _WIN32 // console coloring on windows @@ -122,14 +122,14 @@ static int rsock; // safety stuff (windows is still misbehaving) void exit_proc(void) { - if (rsock != -1) - net_close(rsock); + if (global_rsock != -1) + net_close(global_rsock); } // Check windows & linux behaviour !!! void sighandler(/*int sig*/) { - connection_alive = 0; + global_connection_alive = 0; #ifndef _WIN32 exit(EXIT_SUCCESS); #endif @@ -160,18 +160,18 @@ int main(int argc, char *argv[]) { switch (opt) { - case 'H': host = optarg; break; - case 'P': port = optarg; break; - case 'p': pass = optarg; break; + case 'H': host = optarg; break; + case 'P': port = optarg; break; + case 'p': pass = optarg; break; case 'C': - case 'c': print_colors = 0; break; + case 'c': global_print_colors = 0; break; case 'S': - case 's': silent_mode = 1; break; + case 's': global_silent_mode = 1; break; case 'T': case 't': case 'I': - case 'i': terminal_mode = 1; break; - case 'r': raw_output = 1; break; + case 'i': terminal_mode = 1; break; + case 'r': global_raw_output = 1; break; case 'v': puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon"); exit(EXIT_SUCCESS); @@ -211,21 +211,21 @@ int main(int argc, char *argv[]) #endif // open socket - rsock = net_connect(host, port); + global_rsock = net_connect(host, port); // auth & commands - if (rcon_auth(rsock, pass)) + if (rcon_auth(global_rsock, pass)) { if (terminal_mode) - run_terminal_mode(rsock); + run_terminal_mode(global_rsock); else run_commands(argc, argv); } else // auth failed fprintf(stdout, "Authentication failed!\n"); - net_close(rsock); - rsock = -1; + net_close(global_rsock); + global_rsock = -1; return EXIT_SUCCESS; } @@ -408,14 +408,14 @@ rc_packet *net_recv_packet(int sd) if (ret == 0) { fprintf(stderr, "Connection lost.\n"); - connection_alive = 0; + global_connection_alive = 0; return NULL; } if (ret != sizeof(int)) { fprintf(stderr, "Error: recv() failed. Invalid packet size (%d).\n", ret); - connection_alive = 0; + global_connection_alive = 0; return NULL; } @@ -435,7 +435,7 @@ rc_packet *net_recv_packet(int sd) if (ret == 0) { fprintf(stderr, "Connection lost.\n"); - connection_alive = 0; + global_connection_alive = 0; return NULL; } @@ -458,7 +458,7 @@ int net_clean_incoming(int sd, int size) if(ret == 0) { fprintf(stderr, "Connection lost.\n"); - connection_alive = 0; + global_connection_alive = 0; } return ret; @@ -512,7 +512,7 @@ void print_color(int color) // this hacky mess might use some optimizing void packet_print(rc_packet *packet) { - if (raw_output == 1) + if (global_raw_output == 1) { for (int i = 0; packet->data[i] != 0; ++i) putchar(packet->data[i]); return; @@ -531,7 +531,7 @@ void packet_print(rc_packet *packet) #endif // colors enabled so try to handle the bukkit colors for terminal - if (print_colors == 1) + if (global_print_colors == 1) { for (i = 0; (unsigned char) packet->data[i] != 0; ++i) { @@ -642,7 +642,7 @@ struct rcon_packet packet_build_new(int32_t id, int32_t cmd, char *string) return packet; } -int rcon_auth(int rsock, char *passwd) +int rcon_auth(int sock, char *passwd) { int ret; @@ -650,11 +650,11 @@ int rcon_auth(int rsock, char *passwd) if (packet == NULL) return 0; - ret = net_send_packet(rsock, packet); + ret = net_send_packet(sock, packet); if (!ret) return 0; // send failed - packet = net_recv_packet(rsock); + packet = net_recv_packet(sock); if (packet == NULL) return 0; @@ -662,7 +662,7 @@ int rcon_auth(int rsock, char *passwd) return packet->id == -1 ? 0 : 1; } -int rcon_command(int rsock, char *command) +int rcon_command(int sock, char *command) { int ret; (void) ret; @@ -670,26 +670,26 @@ int rcon_command(int rsock, char *command) uint8_t *p = packet_build_malloc(&size, RCON_PID, RCON_EXEC_COMMAND, command); if (p == NULL) { - connection_alive = 0; + global_connection_alive = 0; return 0; } - net_send(rsock, p, size); + net_send(sock, p, size); free(p); - //ret = net_send_packet(rsock, packet); + //ret = net_send_packet(sock, packet); //if(!ret) return 0; /* send failed */ rc_packet *packet; - packet = net_recv_packet(rsock); + packet = net_recv_packet(sock); if (packet == NULL) return 0; if (packet->id != RCON_PID) return 0; - if (!silent_mode) + if (!global_silent_mode) { /* if(packet->size == 10) { @@ -710,7 +710,7 @@ int run_commands(int argc, char *argv[]) for (i = optind; i < argc && ok; i++) { - ok = rcon_command(rsock, argv[i]); + ok = rcon_command(global_rsock, argv[i]); ret += ok; } @@ -718,21 +718,21 @@ int run_commands(int argc, char *argv[]) } // interactive terminal mode -int run_terminal_mode(int rsock) +int run_terminal_mode(int sock) { int ret = 0; char command[DATA_BUFFSIZE] = {0x00}; puts("Logged in. Type \"Q\" to quit!"); - while (connection_alive) + while (global_connection_alive) { int len = get_line(command, DATA_BUFFSIZE); if(command[0] == 'Q' && command[1] == 0) break; - if(len > 0 && connection_alive) - ret += rcon_command(rsock, command); + if(len > 0 && global_connection_alive) + ret += rcon_command(sock, command); command[0] = len = 0; } @@ -749,7 +749,7 @@ int get_line(char *buffer, int bsize) (void) fgets(buffer, bsize, stdin); if (buffer[0] == 0) - connection_alive = 0; + global_connection_alive = 0; // remove unwanted characters from the buffer buffer[strcspn(buffer, "\r\n")] = '\0'; From ac31d9bc5acb719dbd4abf841e176ee2e43e369e Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 20 Sep 2019 13:36:49 +0300 Subject: [PATCH 06/52] Added check for fgets() return value. --- mcrcon.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index ab33748..6af7a07 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -746,10 +746,10 @@ int get_line(char *buffer, int bsize) int ch, len; fputs(">", stdout); - (void) fgets(buffer, bsize, stdin); + char *ret = fgets(buffer, bsize, stdin); + if (ret == NULL) exit(EXIT_FAILURE); - if (buffer[0] == 0) - global_connection_alive = 0; + if (buffer[0] == 0) global_connection_alive = 0; // remove unwanted characters from the buffer buffer[strcspn(buffer, "\r\n")] = '\0'; From 21337ef5d77a8a8788d837f11de0f6371bb17550 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 20 Sep 2019 13:50:20 +0300 Subject: [PATCH 07/52] Small fix. :-D --- mcrcon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcrcon.c b/mcrcon.c index 6af7a07..c033ce5 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -190,7 +190,7 @@ int main(int argc, char *argv[]) if (pass == NULL) { - fputs("Password not defined (-p flag). Try 'mcrcon -h' 'man mcrcon' for more information.\n\n", stdout); + fputs("Password not defined (-p flag). Try 'mcrcon -h' or 'man mcrcon' for more information.\n\n", stdout); return 0; } From 888fe0dd079fb53c905624d2a3deba7fc248c398 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 27 Sep 2019 16:23:04 +0300 Subject: [PATCH 08/52] Fix file install locations. --- INSTALL.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index c1f04cc..d91f314 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -16,7 +16,7 @@ Or you can just run "make": make uninstall - removes binaries and manpage from the system file install locations: - /usr/share/bin/mcrcon - /usr/share/man/man1/mcrcon.1 + /usr/local/bin/mcrcon + /usr/local/share/man/man1/mcrcon.1 Makefile "install" and "uninstall" rules are disabled on windows. From a4166d73904b5b375bea76cbb79e14ec23f6cdd0 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 27 Sep 2019 16:23:42 +0300 Subject: [PATCH 09/52] Makefile cleanup. --- Makefile | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 92a18f6..3d4b6ce 100644 --- a/Makefile +++ b/Makefile @@ -6,14 +6,15 @@ EXENAME = mcrcon PREFIX ?= /usr/local -EXTRAFLAGS ?= -fstack-protector-strong - INSTALL = install LINKER = -RM = rm -f +RM = rm -v -f + +CC = cc +CFLAGS = -std=gnu99 -Wall -Wextra -Wpedantic -Os -s +EXTRAFLAGS ?= -fstack-protector-strong ifeq ($(OS), Windows_NT) - CC = gcc LINKER = -lws2_32 EXENAME = mcrcon.exe RM = cmd /C del /F @@ -21,9 +22,7 @@ endif ifeq ($(shell uname), Darwin) INSTALL = ginstall - CFLAGS ?= -std=gnu99 -Wall -Wextra -Wpedantic -Os -else - CFLAGS ?= -std=gnu99 -Wall -Wextra -Wpedantic -Os -s + CFLAGS = -std=gnu99 -Wall -Wextra -Wpedantic -Os endif .PHONY: all @@ -41,7 +40,7 @@ install: .PHONY: uninstall uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/$(EXENAME) $(DESTDIR)$(PREFIX)/share/man/man1/mcrcon.1 + $(RM) $(DESTDIR)$(PREFIX)/bin/$(EXENAME) $(DESTDIR)$(PREFIX)/share/man/man1/mcrcon.1 @echo "\nmcrcon uninstalled.\n" endif From 4f791c5e542c27ba0da2c8f60c127fe16c0dc0b6 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 27 Sep 2019 18:18:01 +0300 Subject: [PATCH 10/52] Update year. --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index a3e4a6e..74e485f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012-2016, Tiiffi +Copyright (c) 2012-2019, Tiiffi This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages From 408ec3b1c1465f1727d7855214e2bd27ff6e496e Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Tue, 15 Oct 2019 15:11:04 +0300 Subject: [PATCH 11/52] Add *.plist to .gitignore and change exit code (fix #29) --- .gitignore | 1 + mcrcon.c | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 190da4c..fd7687d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ *.project mcrcon todo +*.plist diff --git a/mcrcon.c b/mcrcon.c index c033ce5..d105d2a 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -100,7 +100,6 @@ int run_commands(int argc, char *argv[]); rc_packet* packet_build(int id, int cmd, char *s1); uint8_t *packet_build_malloc(size_t *size, int32_t id, int32_t cmd, char *string); void packet_print(rc_packet *packet); - int rcon_auth(int sock, char *passwd); int rcon_command(int sock, char *command); @@ -213,21 +212,26 @@ int main(int argc, char *argv[]) // open socket global_rsock = net_connect(host, port); + int exit_code = EXIT_SUCCESS; + // auth & commands if (rcon_auth(global_rsock, pass)) { if (terminal_mode) run_terminal_mode(global_rsock); else - run_commands(argc, argv); + exit_code = run_commands(argc, argv); } else // auth failed + { fprintf(stdout, "Authentication failed!\n"); + exit_code = EXIT_FAILURE; + } net_close(global_rsock); global_rsock = -1; - return EXIT_SUCCESS; + return exit_code; } void usage(void) @@ -697,7 +701,7 @@ int rcon_command(int sock, char *command) } else */ - if (packet->size > 10) + if (packet->size > 10) packet_print(packet); } @@ -706,14 +710,22 @@ int rcon_command(int sock, char *command) int run_commands(int argc, char *argv[]) { - int i, ok = 1, ret = 0; + int i, ok = 1, ret = EXIT_SUCCESS; for (i = optind; i < argc && ok; i++) { ok = rcon_command(global_rsock, argv[i]); - ret += ok; + if (!ok) { + ret = EXIT_FAILURE; + break; + } + ++ret; } + // Check if amount of successfully sent commands + // matches amount of requested commands + if (ret == optind) ret = EXIT_SUCCESS; + return ret; } From 0525c3776e4c8c63e7a2604b0e21763c306ea114 Mon Sep 17 00:00:00 2001 From: northkillz Date: Thu, 17 Oct 2019 16:06:10 +0900 Subject: [PATCH 12/52] Handle fragmented data transmission * Resolve #25 --- mcrcon.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 62cd976..080f1e8 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -431,19 +431,18 @@ rc_packet *net_recv_packet(int sd) packet.size = psize; - ret = recv(sd, (char *) &packet + sizeof(int), psize, 0); - if (ret == 0) + int received = 0; + while (received < psize) { - fprintf(stderr, "Connection lost.\n"); - connection_alive = 0; - return NULL; - } + ret = recv(sd, (char *) &packet + sizeof(int) + received, psize - received, 0); + if (ret == 0) /* connection closed before completing receving */ + { + fprintf(stderr, "Connection lost.\n"); + connection_alive = 0; + return NULL; + } - if(ret != psize) - { - fprintf(stderr, "Warning: recv() return value (%d) does not match expected packet size (%d).\n", ret, psize); - net_clean_incoming(sd, DATA_BUFFSIZE); /* Should be enough. Needs some checking */ - return NULL; + received += ret; } return &packet; From 6bfd6801c917f5f15c64f676e4925f9171cd2f3b Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sun, 20 Oct 2019 15:49:06 +0300 Subject: [PATCH 13/52] Update version number and man page --- mcrcon.1 | 2 +- mcrcon.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mcrcon.1 b/mcrcon.1 index 4f9b26c..480a50e 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -1,7 +1,7 @@ .\" Process this file with .\" groff -man -Tascii mcrcon.1 .\" -.TH MCRCON 1 "December 2016" "Version 0.6.0" +.TH MCRCON 1 "October 2019" "Version 0.6.2" .SH NAME mcrcon \- sends rcon commands to a Minecraft server .SH SYNOPSIS diff --git a/mcrcon.c b/mcrcon.c index a175553..3d33a84 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -46,7 +46,7 @@ #include #endif -#define VERSION "0.6.1" +#define VERSION "0.6.2" #define IN_NAME "mcrcon" #define VER_STR IN_NAME" "VERSION" (built: "__DATE__" "__TIME__")" From 989dfd6928f045052d9f94186c78a8d4eb654a39 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sun, 20 Oct 2019 16:01:45 +0300 Subject: [PATCH 14/52] Remove unused code --- mcrcon.c | 76 +++----------------------------------------------------- 1 file changed, 4 insertions(+), 72 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 3d33a84..ae22e9d 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -98,7 +98,6 @@ int run_commands(int argc, char *argv[]); // Rcon protocol related functions rc_packet* packet_build(int id, int cmd, char *s1); -uint8_t *packet_build_malloc(size_t *size, int32_t id, int32_t cmd, char *string); void packet_print(rc_packet *packet); int rcon_auth(int sock, char *passwd); int rcon_command(int sock, char *command); @@ -585,66 +584,6 @@ rc_packet *packet_build(int id, int cmd, char *s1) return &packet; } -// TODO(Tiiffi): String length limit? -uint8_t *packet_build_malloc(size_t *size, int32_t id, int32_t cmd, char *string) -{ - size_t string_length = strlen(string); - - *size = 3 * sizeof(int32_t) + string_length + 2; - uint8_t *packet = malloc(*size); - if (packet == NULL) return NULL; - - int32_t *p = (int32_t *) packet; - p[0] = (int32_t) *size - sizeof(int32_t); - p[1] = id; - p[2] = cmd; - - memcpy(&p[3], string, string_length); - - packet[12 + string_length] = 0; - packet[13 + string_length] = 0; - - return packet; -} - -// rcon packet structure -#define MAX_PACKET_SIZE (size_t) 1460 // including size member -#define MIN_PACKET_SIZE (size_t) 10 -#define MAX_STRING_SIZE (size_t) (MAX_PACKET_SIZE - 2 - 3 * sizeof(int32_t)) -#define SIZEOF_PACKET(x) (size_t) (x.size + sizeof(int32_t)) - -struct rcon_packet -{ - int32_t size; - int32_t id; - int32_t cmd; - uint8_t data[MAX_STRING_SIZE]; -}; - -struct rcon_packet packet_build_new(int32_t id, int32_t cmd, char *string) -{ - struct rcon_packet packet; - size_t string_length = strlen(string); - - if (string_length > MAX_STRING_SIZE) - { - string_length = MAX_STRING_SIZE; - fprintf(stderr, - "Warning: command string is too long. Truncating to " - "%u characters.\n", (unsigned) MAX_STRING_SIZE - ); - } - - packet.size = 2 * sizeof(int32_t) + string_length + 2; - packet.id = id; - packet.cmd = cmd; - memcpy(packet.data, string, string_length); - packet.data[string_length] = 0; - packet.data[string_length + 1] = 0; - - return packet; -} - int rcon_auth(int sock, char *passwd) { int ret; @@ -667,24 +606,17 @@ int rcon_auth(int sock, char *passwd) int rcon_command(int sock, char *command) { - int ret; (void) ret; - - size_t size; - uint8_t *p = packet_build_malloc(&size, RCON_PID, RCON_EXEC_COMMAND, command); - if (p == NULL) + rc_packet *packet = packet_build(RCON_PID, RCON_EXEC_COMMAND, command); + if (packet == NULL) { global_connection_alive = 0; return 0; } - net_send(sock, p, size); + net_send_packet(sock, packet); - free(p); + free(packet); - //ret = net_send_packet(sock, packet); - //if(!ret) return 0; /* send failed */ - - rc_packet *packet; packet = net_recv_packet(sock); if (packet == NULL) return 0; From cbbfdad94ed5575daf1ca6727bd3ab710c4e55a5 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sun, 20 Oct 2019 16:06:15 +0300 Subject: [PATCH 15/52] Add newline --- LICENSE | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 74e485f..1dab25d 100644 --- a/LICENSE +++ b/LICENSE @@ -17,4 +17,5 @@ freely, subject to the following restrictions: misrepresented as being the original software. 3. This notice may not be removed or altered from any source - distribution. \ No newline at end of file + distribution. + \ No newline at end of file From 208e0403056cd564e34bd99137a94ef81715351f Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sun, 20 Oct 2019 16:08:51 +0300 Subject: [PATCH 16/52] Fix invalid free --- mcrcon.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index ae22e9d..eeac12b 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -615,8 +615,6 @@ int rcon_command(int sock, char *command) net_send_packet(sock, packet); - free(packet); - packet = net_recv_packet(sock); if (packet == NULL) return 0; From 7d5cb8016ee0b6a70019e7b2864f025676f43999 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Mon, 21 Oct 2019 14:23:41 +0300 Subject: [PATCH 17/52] Change DATA_BUFFSIZE to 4096 bytes --- mcrcon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcrcon.c b/mcrcon.c index eeac12b..9c1a71b 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -57,7 +57,7 @@ #define RCON_PID 0xBADC0DE // a bit too big perhaps? -#define DATA_BUFFSIZE 10240 +#define DATA_BUFFSIZE 4096 // rcon packet structure typedef struct _rc_packet { From 8f561f803ab0e65233db6490ed9e43de3b146014 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Mon, 21 Oct 2019 20:58:14 +0300 Subject: [PATCH 18/52] Change bug report email address --- mcrcon.1 | 2 +- mcrcon.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mcrcon.1 b/mcrcon.1 index 480a50e..39ccf49 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -60,4 +60,4 @@ Send three commands to server (say, save-all and stop) \fBmcrcon\fR -H my.minecraft.server -p password "say Server is restarting!" save-all stop .RE .SH BUGS -Bugs can be reported to \fBtiiffi_at_gmail_dot_com\fR or \fBhttps://github.com/Tiiffi/mcrcon/issues/\fR +Bugs can be reported to \fBtiiffi+mcrcon at gmail\fR or \fBhttps://github.com/Tiiffi/mcrcon/issues/\fR diff --git a/mcrcon.c b/mcrcon.c index 9c1a71b..fdd32c8 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -258,7 +258,7 @@ void usage(void) puts("Command-line options will override environment variables."); puts("Rcon commands with arguments must be enclosed in quotes.\n"); puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password \"say Server is restarting!\" save-all stop\n"); - puts(VER_STR"\nReport bugs to tiiffi_at_gmail_dot_com or https://github.com/Tiiffi/mcrcon/issues/\n"); + puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/\n"); #ifdef _WIN32 puts("Press enter to exit."); From 16c2db5c78493c70d0cfff9b7c6228c7b980fad7 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Mon, 21 Oct 2019 21:35:38 +0300 Subject: [PATCH 19/52] Tidy up man page and usage text --- mcrcon.1 | 27 ++++++++++++++------------- mcrcon.c | 27 ++++++++++++++------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/mcrcon.1 b/mcrcon.1 index 39ccf49..b0d83de 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -3,7 +3,7 @@ .\" .TH MCRCON 1 "October 2019" "Version 0.6.2" .SH NAME -mcrcon \- sends rcon commands to a Minecraft server +mcrcon \- send rcon commands to a Minecraft server .SH SYNOPSIS .B mcrcon [ options @@ -11,31 +11,32 @@ options commands .B ] .SH DESCRIPTION -mcrcon is Minecraft rcon client / terminal with bukkit coloring support. -It is well suited for remote administration and server maintenance scripts. +mcrcon is Minecraft rcon client for remote administration and server maintenance scripts. .SH OPTIONS -.IP -h -Print usage .IP -H -Server address (default is localhost) +Server address (default: localhost) .IP -P -Port (default is 25575) +Port (default: 25575) .IP -p Rcon password .IP -t -Interactive terminal mode +Terminal mode .IP -s -Silent mode (do not print received packets) +Silent mode .IP -c Disable colors .IP -r -Output raw packets (for debugging and custom handling) +Output raw packets +.IP -h +Print usage .IP -v Output version information .PP -Commands with arguments must be enclosed in quotes. +Commands with spaces must be enclosed in quotes. +.br +mcrcon will start in terminal mode if no commands are given. .SH ENVIRONMENTAL VARIABLES -Server address, port and password can be set using following environment variables: +Server address, port and password can be set with following environment variables: .PP \fBMCRCON_HOST .br @@ -55,7 +56,7 @@ Send "weather clear" command to server using custom port 1337 \fBmcrcon\fR -H my.minecraft.server -P 1337 -p password "weather clear" .RE .PP -Send three commands to server (say, save-all and stop) +Send three commands to server ("say", "save-all" and "stop") .RS \fBmcrcon\fR -H my.minecraft.server -p password "say Server is restarting!" save-all stop .RE diff --git a/mcrcon.c b/mcrcon.c index fdd32c8..2c07293 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -188,7 +188,7 @@ int main(int argc, char *argv[]) if (pass == NULL) { - fputs("Password not defined (-p flag). Try 'mcrcon -h' or 'man mcrcon' for more information.\n\n", stdout); + fputs("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n\n", stdout); return 0; } @@ -236,27 +236,28 @@ int main(int argc, char *argv[]) void usage(void) { fputs( - "Usage: "IN_NAME" [OPTIONS]... [COMMANDS]...\n\n" - "Sends rcon commands to Minecraft server.\n\n" - "Option:\n" - " -h\t\tPrint usage\n" - " -H\t\tServer address (default is localhost)\n" - " -P\t\tPort (default is 25575)\n" + "Usage: "IN_NAME" [OPTIONS] [COMMANDS]\n\n" + "Send rcon commands to Minecraft server.\n\n" + "Options:\n" + " -H\t\tServer address (default: localhost)\n" + " -P\t\tPort (default: 25575)\n" " -p\t\tRcon password\n" - " -t\t\tInteractive terminal mode\n" - " -s\t\tSilent mode (do not print received packets)\n" + " -t\t\tTerminal mode\n" + " -s\t\tSilent mode\n" " -c\t\tDisable colors\n" - " -r\t\tOutput raw packets (debugging and custom handling)\n" - " -v\t\tOutput version information\n\n" - "Server address, port and password can be set using following environment variables:\n" + " -r\t\tOutput raw packets\n" + " -h\t\tPrint usage\n" + " -v\t\tVersion information\n\n" + "Server address, port and password can be set with following environment variables:\n" " MCRCON_HOST\n" " MCRCON_PORT\n" " MCRCON_PASS\n\n" ,stdout ); + puts("mcrcon will start in terminal mode if no commands are given."); puts("Command-line options will override environment variables."); - puts("Rcon commands with arguments must be enclosed in quotes.\n"); + puts("Rcon commands with spaces must be enclosed in quotes.\n"); puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password \"say Server is restarting!\" save-all stop\n"); puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/\n"); From 9667e8f066cf3575f18e099392a7a2287f9e2ab6 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Mon, 21 Oct 2019 21:43:54 +0300 Subject: [PATCH 20/52] Change email text --- LICENSE | 2 +- mcrcon.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 1dab25d..2db5c06 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012-2019, Tiiffi +Copyright (c) 2012-2019, Tiiffi This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages diff --git a/mcrcon.c b/mcrcon.c index 2c07293..c8edd3e 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2019, Tiiffi gmail_dot_com> + * Copyright (c) 2012-2019, Tiiffi * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages From 2732e81ca85ecc66f2a20e8d2c0c0911c6887e9c Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 21:32:26 +0200 Subject: [PATCH 21/52] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index fbdadaa..2164b9c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +# mcrcon # + +mcrcon is console based Minecraft [rcon](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) client for remote administration and server maintenance scripts. + +--- + ### Installing: from sources: From a9ec7dc6ac12806561c151228f284bfacf614b9f Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 21:39:24 +0200 Subject: [PATCH 22/52] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2164b9c..3430816 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# mcrcon # +# mcrcon mcrcon is console based Minecraft [rcon](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) client for remote administration and server maintenance scripts. @@ -53,8 +53,8 @@ Example: --- -### Enable rcon on server -Remember to enable rcon by adding following lines to ```server.properties``` file. +##### Enable rcon on server +Remember to enable rcon by adding following lines to [```server.properties```](https://minecraft.gamepedia.com/Server.properties) file. ``` enable-rcon=true rcon.port=25575 From 3f1ea0275681cda876e621803e610b74d56831cd Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 21:40:48 +0200 Subject: [PATCH 23/52] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3430816..4861c7a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ cd mcrcon make sudo make install ``` -Check **INSTALL** for more details. +Check **INSTALL.md** for more details. You can also download precompiled binaries*: https://github.com/Tiiffi/mcrcon/releases/latest From 9eb7af2d71b886d7608f140348ba35a40dc119e4 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 21:56:56 +0200 Subject: [PATCH 24/52] Update README.md --- README.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 4861c7a..9b6ca3e 100644 --- a/README.md +++ b/README.md @@ -22,31 +22,32 @@ You can also download precompiled binaries*: https://github.com/Tiiffi/mcrcon/re --- ### Usage: -mcrcon [OPTIONS]... [COMMANDS]... +mcrcon [OPTIONS] [COMMANDS] Sends rcon commands to Minecraft server. ``` Option: - -h Print usage - -H Server address - -P Port (default is 25575) + -H Server address (default: localhost) + -P Port (default: 25575) -p Rcon password - -t Interactive terminal mode - -s Silent mode (do not print received packets) + -t Terminal mode + -s Silent mode -c Disable colors - -r Output raw packets (debugging and custom handling) - -v Output version information + -r Output raw packets + -h Print usage + -v Version information ``` -Commands with arguments must be enclosed in quotes. - Server address, port and password can be set using following environment variables: ``` MCRCON_HOST MCRCON_PORT MCRCON_PASS ``` -Note that command-line options will override environment variables. +###### Notes: +- mcrcon will start in terminal mode if no commands are given +- Command-line options will override environment variables +- Rcon commands with spaces must be enclosed in quotes Example: ```mcrcon -H my.minecraft.server -p password "say Server is restarting!" save-all stop``` From bc33bbe09e5517151e76c1f681d6893b0715703f Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 22:10:35 +0200 Subject: [PATCH 25/52] Update README.md --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b6ca3e..f9426a2 100644 --- a/README.md +++ b/README.md @@ -64,14 +64,20 @@ rcon.password=your_rcon_pasword --- -#### Contact: +##### Contact: * WWW: https://github.com/Tiiffi/mcrcon/ -* MAIL: tiiffi_at_gmail_dot_com +* MAIL: tiiffi -> gmail * IRC: tiiffi @ quakenet * BUG REPORTS: https://github.com/Tiiffi/mcrcon/issues/ --- +### License + +This project is licensed under the zlib License - see the [LICENSE.md](LICENSE.md) file for details. + +--- + Master: ![Master build](https://api.travis-ci.org/Tiiffi/mcrcon.svg?branch=master) Develop: ![Develop build](https://api.travis-ci.org/Tiiffi/mcrcon.svg?branch=develop) From c27f6835738b4aab1f5e880abb3b1acf63a8b70e Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 22:11:40 +0200 Subject: [PATCH 26/52] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f9426a2..303bc7c 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ rcon.password=your_rcon_pasword ##### Contact: * WWW: https://github.com/Tiiffi/mcrcon/ -* MAIL: tiiffi -> gmail +* MAIL: tiiffi at gmail * IRC: tiiffi @ quakenet * BUG REPORTS: https://github.com/Tiiffi/mcrcon/issues/ @@ -75,7 +75,7 @@ rcon.password=your_rcon_pasword ### License -This project is licensed under the zlib License - see the [LICENSE.md](LICENSE.md) file for details. +This project is licensed under the zlib License - see the [LICENSE](LICENSE) file for details. --- From 5c47b744f51eb307f5ca73bd0036d12c9836934b Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 22:20:32 +0200 Subject: [PATCH 27/52] Update README.md --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 303bc7c..dd68d00 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,20 @@ mcrcon is console based Minecraft [rcon](https://developer.valvesoftware.com/wik ### Installing: -from sources: +##### via packet manager: +See https://pkgs.org/download/mcrcon for available packages in various Linux distros. + +- Gentoo Linux: https://packages.gentoo.org/packages/games-util/mcrcon +- Arch Linux: https://aur.archlinux.org/packages/mcrcon/ + +##### from sources: ```sh git clone https://github.com/Tiiffi/mcrcon.git cd mcrcon make sudo make install ``` -Check **INSTALL.md** for more details. +Check [INSTALL.md](INSTALL.md) for more details. You can also download precompiled binaries*: https://github.com/Tiiffi/mcrcon/releases/latest From 4aab776687963d02be1a17fefdf0e30008ded883 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 8 Nov 2019 23:16:03 +0200 Subject: [PATCH 28/52] Remove TODO --- CHANGELOG.md | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32aed56..fb8af4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,19 +65,3 @@ - Packet string data limited to max 2048 (DATA_BUFFSIZE) bytes. No idea how Minecraft handles multiple rcon packets. If someone knows, please mail me so I can implement it. - -#### TODO: - - Make the receive buffer dynamic?? - - Change some of the packet size issues to fatal errors. - - Code cleanups. - - Check global variables (remove if possible). - - Add some protocol checks (proper packet id check etc..). - - Preprocessor (#ifdef / #ifndef) cleanups. - - Follow valve rcon protocol standard strictly? - - Multiple packet support if minecraft supports it?! - - Investigate if player chat messages gets sent through rcon. - If they are, the messaging system requires rewriting. - - Name resolving should be integrated to connection creation function. - - Dont try to cleanup the socket if not authenticated - - Better sockets error reporting - - Better error function (VA_ARGS support) From fed62d9e14c4042dcb2bbb2bdf080dcf14b7df17 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sat, 9 Nov 2019 03:50:17 +0200 Subject: [PATCH 29/52] Update INSTALL.md --- INSTALL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/INSTALL.md b/INSTALL.md index d91f314..859b037 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -1,7 +1,7 @@ Compiling and installing ------------------------ -Only dependency is C library with POSIX getopt support. +Only dependency is C library and POSIX getopt support. Compiling with GCC or CLANG: From a16d8756265ee38e217d4092d78e8ad90b89d0bf Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sat, 14 Dec 2019 17:46:08 +0100 Subject: [PATCH 30/52] Adds a `-w` option to throttle commands execution --- README.md | 1 + mcrcon.1 | 6 ++++-- mcrcon.c | 42 ++++++++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index dd68d00..5e25b73 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Option: -r Output raw packets -h Print usage -v Version information + -w Wait for specified duration (seconds) between each command ``` Server address, port and password can be set using following environment variables: ``` diff --git a/mcrcon.1 b/mcrcon.1 index b0d83de..a7653e8 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -31,6 +31,8 @@ Output raw packets Print usage .IP -v Output version information +.IP -w +Wait for specified duration (seconds) between each command .PP Commands with spaces must be enclosed in quotes. .br @@ -56,9 +58,9 @@ Send "weather clear" command to server using custom port 1337 \fBmcrcon\fR -H my.minecraft.server -P 1337 -p password "weather clear" .RE .PP -Send three commands to server ("say", "save-all" and "stop") +Send three commands to server ("say", "save-all" and "stop"), and wait 2 seconds between them .RS -\fBmcrcon\fR -H my.minecraft.server -p password "say Server is restarting!" save-all stop +\fBmcrcon\fR -H my.minecraft.server -p password -w 2 "say Server is restarting!" save-all stop .RE .SH BUGS Bugs can be reported to \fBtiiffi+mcrcon at gmail\fR or \fBhttps://github.com/Tiiffi/mcrcon/issues/\fR diff --git a/mcrcon.c b/mcrcon.c index c8edd3e..d34008b 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -110,6 +110,7 @@ static int global_raw_output = 0; static int global_silent_mode = 0; static int global_print_colors = 1; static int global_connection_alive = 1; +static int global_wait_seconds = 0; static int global_rsock; #ifdef _WIN32 @@ -154,7 +155,7 @@ int main(int argc, char *argv[]) // default getopt error handler enabled opterr = 1; - while ((opt = getopt(argc, argv, "vrtcshH:p:P:i")) != -1) + while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:i")) != -1) { switch (opt) { @@ -173,7 +174,15 @@ int main(int argc, char *argv[]) case 'v': puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon"); exit(EXIT_SUCCESS); - break; + case 'W': + case 'w': + global_wait_seconds = strtol(optarg, NULL, 10); + if (errno != 0) + { + fprintf(stderr, "Error %d: %s\n", errno, strerror(errno)); + exit(EXIT_FAILURE); + } + break; case 'h': case '?': usage(); break; /* @@ -247,7 +256,8 @@ void usage(void) " -c\t\tDisable colors\n" " -r\t\tOutput raw packets\n" " -h\t\tPrint usage\n" - " -v\t\tVersion information\n\n" + " -v\t\tVersion information\n" + " -w\t\tWait for specified duration (seconds) between each command\n\n" "Server address, port and password can be set with following environment variables:\n" " MCRCON_HOST\n" " MCRCON_PORT\n" @@ -640,23 +650,27 @@ int rcon_command(int sock, char *command) int run_commands(int argc, char *argv[]) { - int i, ok = 1, ret = EXIT_SUCCESS; + int i = optind; - for (i = optind; i < argc && ok; i++) + for (;;) { - ok = rcon_command(global_rsock, argv[i]); - if (!ok) { - ret = EXIT_FAILURE; + if (!rcon_command(global_rsock, argv[i])) + return EXIT_FAILURE; + + if (++i >= argc) break; + + if (global_wait_seconds > 0) + { + #ifdef _WIN32 + Sleep(global_wait_seconds * 1000); + #else + sleep(global_wait_seconds); + #endif } - ++ret; } - // Check if amount of successfully sent commands - // matches amount of requested commands - if (ret == optind) ret = EXIT_SUCCESS; - - return ret; + return EXIT_SUCCESS; } // interactive terminal mode From 5a0b9ea4e5cec32cf8c142a79ff9748660a56176 Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sat, 14 Dec 2019 18:06:12 +0100 Subject: [PATCH 31/52] Let's avoid one more statement --- mcrcon.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index d34008b..5f1d529 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -658,7 +658,7 @@ int run_commands(int argc, char *argv[]) return EXIT_FAILURE; if (++i >= argc) - break; + return EXIT_SUCCESS; if (global_wait_seconds > 0) { @@ -669,8 +669,6 @@ int run_commands(int argc, char *argv[]) #endif } } - - return EXIT_SUCCESS; } // interactive terminal mode From 52e717a92bb1eb0884e8219273d0edf33e35ead6 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 18:00:11 +0200 Subject: [PATCH 32/52] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb8af4a..aa9fded 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ #### Version history: +###### 0.7.0 + - Add -w option for rcon command throttling + ###### 0.6.2 - Set default address to localhost From 7737bc2a32bf23cc0bf2dbccd9b6e2046aec1ccc Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 18:00:31 +0200 Subject: [PATCH 33/52] Update mcrcon.1 --- mcrcon.1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mcrcon.1 b/mcrcon.1 index a7653e8..f84d799 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -1,7 +1,7 @@ .\" Process this file with .\" groff -man -Tascii mcrcon.1 .\" -.TH MCRCON 1 "October 2019" "Version 0.6.2" +.TH MCRCON 1 "December 2019" "Version 0.7.0" .SH NAME mcrcon \- send rcon commands to a Minecraft server .SH SYNOPSIS @@ -27,12 +27,12 @@ Silent mode Disable colors .IP -r Output raw packets +.IP -w +Wait for specified duration (1 - 600 seconds) between each command .IP -h Print usage .IP -v Output version information -.IP -w -Wait for specified duration (seconds) between each command .PP Commands with spaces must be enclosed in quotes. .br From 8ff710e231a43263004f4c14f984e98a69d383f7 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 18:00:45 +0200 Subject: [PATCH 34/52] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e25b73..0a19e93 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,9 @@ Option: -s Silent mode -c Disable colors -r Output raw packets + -w Wait for specified duration (1 - 600 seconds) between each command -h Print usage -v Version information - -w Wait for specified duration (seconds) between each command ``` Server address, port and password can be set using following environment variables: ``` From 2f1f925ac193e4deb296978712bd18fb006ab24f Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 18:02:20 +0200 Subject: [PATCH 35/52] Make command throttling more robust --- mcrcon.c | 91 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 5f1d529..26959e1 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef _WIN32 // for name resolving on windows @@ -46,7 +47,7 @@ #include #endif -#define VERSION "0.6.2" +#define VERSION "0.7.0" #define IN_NAME "mcrcon" #define VER_STR IN_NAME" "VERSION" (built: "__DATE__" "__TIME__")" @@ -56,7 +57,7 @@ #define RCON_AUTH_RESPONSE 2 #define RCON_PID 0xBADC0DE -// a bit too big perhaps? + #define DATA_BUFFSIZE 4096 // rcon packet structure @@ -65,7 +66,7 @@ typedef struct _rc_packet { int id; int cmd; char data[DATA_BUFFSIZE]; - // ignoring string2 atm. + // ignoring string2 for now } rc_packet; // =================================== @@ -73,8 +74,8 @@ typedef struct _rc_packet { // =================================== // endianness related functions -bool is_bigendian(void); -int32_t reverse_int32(int32_t n); +bool is_bigendian(void); +int32_t reverse_int32(int32_t n); // Network related functions #ifdef _WIN32 @@ -108,10 +109,10 @@ int rcon_command(int sock, char *command); // ============================================= static int global_raw_output = 0; static int global_silent_mode = 0; -static int global_print_colors = 1; +static int global_disable_colors = 0; static int global_connection_alive = 1; -static int global_wait_seconds = 0; static int global_rsock; +static int global_wait_seconds = 0; #ifdef _WIN32 // console coloring on windows @@ -134,6 +135,25 @@ void sighandler(/*int sig*/) #endif } +#define MAX_WAIT_TIME 600 + +unsigned int mcrcon_parse_seconds(char *str) +{ + long result = strtol(str, NULL, 10); + + if (errno != 0) { + fprintf(stderr, "-w invalid value.\nerror %d: %s\n", errno, strerror(errno)); + exit(EXIT_FAILURE); + } + + if (result <= 0 || result > MAX_WAIT_TIME) { + fprintf(stderr, "-w option value is out of range.\nAcceptable value is 1 - %d seconds.\n", MAX_WAIT_TIME); + exit(EXIT_FAILURE); + } + + return (unsigned int) result; +} + int main(int argc, char *argv[]) { int opt; @@ -159,37 +179,28 @@ int main(int argc, char *argv[]) { switch (opt) { - case 'H': host = optarg; break; - case 'P': port = optarg; break; - case 'p': pass = optarg; break; + case '?': + puts("Try 'mcrcon -h' or 'man mcrcon' for help.\n"); + exit(EXIT_FAILURE); + + case 'H': host = optarg; break; + case 'P': port = optarg; break; + case 'p': pass = optarg; break; case 'C': - case 'c': global_print_colors = 0; break; + case 'c': global_disable_colors = 1; break; case 'S': - case 's': global_silent_mode = 1; break; + case 's': global_silent_mode = 1; break; case 'T': case 't': case 'I': - case 'i': terminal_mode = 1; break; - case 'r': global_raw_output = 1; break; - case 'v': - puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon"); - exit(EXIT_SUCCESS); - case 'W': + case 'i': terminal_mode = 1; break; + case 'r': global_raw_output = 1; break; case 'w': - global_wait_seconds = strtol(optarg, NULL, 10); - if (errno != 0) - { - fprintf(stderr, "Error %d: %s\n", errno, strerror(errno)); - exit(EXIT_FAILURE); - } - break; - case 'h': - case '?': usage(); break; - /* - if(optopt == 'P' || optopt == 'H' || optopt == 'p') - fprintf (stderr, "Option -%c requires an argument.\n\n", optopt); - else fprintf (stderr, "Unknown option -%c\n\n", optopt); - */ + global_wait_seconds = mcrcon_parse_seconds(optarg); break; + case 'v': + puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon\n"); + exit(EXIT_SUCCESS); + case 'h': usage(); break; default: exit(EXIT_FAILURE); } @@ -197,7 +208,7 @@ int main(int argc, char *argv[]) if (pass == NULL) { - fputs("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n\n", stdout); + puts("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n"); return 0; } @@ -255,9 +266,9 @@ void usage(void) " -s\t\tSilent mode\n" " -c\t\tDisable colors\n" " -r\t\tOutput raw packets\n" + " -w\t\tWait for specified duration (1 - 600 seconds) between each command\n" " -h\t\tPrint usage\n" - " -v\t\tVersion information\n" - " -w\t\tWait for specified duration (seconds) between each command\n\n" + " -v\t\tVersion information\n\n" "Server address, port and password can be set with following environment variables:\n" " MCRCON_HOST\n" " MCRCON_PORT\n" @@ -265,10 +276,10 @@ void usage(void) ,stdout ); - puts("mcrcon will start in terminal mode if no commands are given."); - puts("Command-line options will override environment variables."); - puts("Rcon commands with spaces must be enclosed in quotes.\n"); - puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password \"say Server is restarting!\" save-all stop\n"); + puts("- mcrcon will start in terminal mode if no commands are given"); + puts("- Command-line options will override environment variables"); + puts("- Rcon commands with spaces must be enclosed in quotes\n"); + puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n"); puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/\n"); #ifdef _WIN32 @@ -544,7 +555,7 @@ void packet_print(rc_packet *packet) #endif // colors enabled so try to handle the bukkit colors for terminal - if (global_print_colors == 1) + if (global_disable_colors != 1) { for (i = 0; (unsigned char) packet->data[i] != 0; ++i) { From 83457075b92d4615b6a9d2ac5a02c925f9541df6 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 18:02:20 +0200 Subject: [PATCH 36/52] Make command throttling argument parsing more robust --- mcrcon.c | 91 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 5f1d529..26959e1 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef _WIN32 // for name resolving on windows @@ -46,7 +47,7 @@ #include #endif -#define VERSION "0.6.2" +#define VERSION "0.7.0" #define IN_NAME "mcrcon" #define VER_STR IN_NAME" "VERSION" (built: "__DATE__" "__TIME__")" @@ -56,7 +57,7 @@ #define RCON_AUTH_RESPONSE 2 #define RCON_PID 0xBADC0DE -// a bit too big perhaps? + #define DATA_BUFFSIZE 4096 // rcon packet structure @@ -65,7 +66,7 @@ typedef struct _rc_packet { int id; int cmd; char data[DATA_BUFFSIZE]; - // ignoring string2 atm. + // ignoring string2 for now } rc_packet; // =================================== @@ -73,8 +74,8 @@ typedef struct _rc_packet { // =================================== // endianness related functions -bool is_bigendian(void); -int32_t reverse_int32(int32_t n); +bool is_bigendian(void); +int32_t reverse_int32(int32_t n); // Network related functions #ifdef _WIN32 @@ -108,10 +109,10 @@ int rcon_command(int sock, char *command); // ============================================= static int global_raw_output = 0; static int global_silent_mode = 0; -static int global_print_colors = 1; +static int global_disable_colors = 0; static int global_connection_alive = 1; -static int global_wait_seconds = 0; static int global_rsock; +static int global_wait_seconds = 0; #ifdef _WIN32 // console coloring on windows @@ -134,6 +135,25 @@ void sighandler(/*int sig*/) #endif } +#define MAX_WAIT_TIME 600 + +unsigned int mcrcon_parse_seconds(char *str) +{ + long result = strtol(str, NULL, 10); + + if (errno != 0) { + fprintf(stderr, "-w invalid value.\nerror %d: %s\n", errno, strerror(errno)); + exit(EXIT_FAILURE); + } + + if (result <= 0 || result > MAX_WAIT_TIME) { + fprintf(stderr, "-w option value is out of range.\nAcceptable value is 1 - %d seconds.\n", MAX_WAIT_TIME); + exit(EXIT_FAILURE); + } + + return (unsigned int) result; +} + int main(int argc, char *argv[]) { int opt; @@ -159,37 +179,28 @@ int main(int argc, char *argv[]) { switch (opt) { - case 'H': host = optarg; break; - case 'P': port = optarg; break; - case 'p': pass = optarg; break; + case '?': + puts("Try 'mcrcon -h' or 'man mcrcon' for help.\n"); + exit(EXIT_FAILURE); + + case 'H': host = optarg; break; + case 'P': port = optarg; break; + case 'p': pass = optarg; break; case 'C': - case 'c': global_print_colors = 0; break; + case 'c': global_disable_colors = 1; break; case 'S': - case 's': global_silent_mode = 1; break; + case 's': global_silent_mode = 1; break; case 'T': case 't': case 'I': - case 'i': terminal_mode = 1; break; - case 'r': global_raw_output = 1; break; - case 'v': - puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon"); - exit(EXIT_SUCCESS); - case 'W': + case 'i': terminal_mode = 1; break; + case 'r': global_raw_output = 1; break; case 'w': - global_wait_seconds = strtol(optarg, NULL, 10); - if (errno != 0) - { - fprintf(stderr, "Error %d: %s\n", errno, strerror(errno)); - exit(EXIT_FAILURE); - } - break; - case 'h': - case '?': usage(); break; - /* - if(optopt == 'P' || optopt == 'H' || optopt == 'p') - fprintf (stderr, "Option -%c requires an argument.\n\n", optopt); - else fprintf (stderr, "Unknown option -%c\n\n", optopt); - */ + global_wait_seconds = mcrcon_parse_seconds(optarg); break; + case 'v': + puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon\n"); + exit(EXIT_SUCCESS); + case 'h': usage(); break; default: exit(EXIT_FAILURE); } @@ -197,7 +208,7 @@ int main(int argc, char *argv[]) if (pass == NULL) { - fputs("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n\n", stdout); + puts("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n"); return 0; } @@ -255,9 +266,9 @@ void usage(void) " -s\t\tSilent mode\n" " -c\t\tDisable colors\n" " -r\t\tOutput raw packets\n" + " -w\t\tWait for specified duration (1 - 600 seconds) between each command\n" " -h\t\tPrint usage\n" - " -v\t\tVersion information\n" - " -w\t\tWait for specified duration (seconds) between each command\n\n" + " -v\t\tVersion information\n\n" "Server address, port and password can be set with following environment variables:\n" " MCRCON_HOST\n" " MCRCON_PORT\n" @@ -265,10 +276,10 @@ void usage(void) ,stdout ); - puts("mcrcon will start in terminal mode if no commands are given."); - puts("Command-line options will override environment variables."); - puts("Rcon commands with spaces must be enclosed in quotes.\n"); - puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password \"say Server is restarting!\" save-all stop\n"); + puts("- mcrcon will start in terminal mode if no commands are given"); + puts("- Command-line options will override environment variables"); + puts("- Rcon commands with spaces must be enclosed in quotes\n"); + puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n"); puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/\n"); #ifdef _WIN32 @@ -544,7 +555,7 @@ void packet_print(rc_packet *packet) #endif // colors enabled so try to handle the bukkit colors for terminal - if (global_print_colors == 1) + if (global_disable_colors != 1) { for (i = 0; (unsigned char) packet->data[i] != 0; ++i) { From f9d9a02348c0a486efc133def0df6f1a7b8bf600 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 18:29:31 +0200 Subject: [PATCH 37/52] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0a19e93..87c5d54 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ mcrcon is console based Minecraft [rcon](https://developer.valvesoftware.com/wik ### Installing: ##### via packet manager: -See https://pkgs.org/download/mcrcon for available packages in various Linux distros. +See https://pkgs.org/download/mcrcon for available packages in various Linux distros (note that available packages might be outdated). - Gentoo Linux: https://packages.gentoo.org/packages/games-util/mcrcon - Arch Linux: https://aur.archlinux.org/packages/mcrcon/ @@ -21,7 +21,7 @@ sudo make install ``` Check [INSTALL.md](INSTALL.md) for more details. -You can also download precompiled binaries*: https://github.com/Tiiffi/mcrcon/releases/latest +Precompiled binaries (if provided)*: https://github.com/Tiiffi/mcrcon/releases/latest *At the moment binaries are provided for Linux and Windows. @@ -57,7 +57,7 @@ MCRCON_PASS - Rcon commands with spaces must be enclosed in quotes Example: - ```mcrcon -H my.minecraft.server -p password "say Server is restarting!" save-all stop``` + ```mcrcon -H my.minecraft.server -p password -w 2 "say Server is restarting!" save-all stop``` --- From 0f796b6bbc9de4e2f8322e8975121564d07c83c3 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 19:06:37 +0200 Subject: [PATCH 38/52] Tidy code formatting --- mcrcon.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 26959e1..97acb39 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -181,7 +181,7 @@ int main(int argc, char *argv[]) { case '?': puts("Try 'mcrcon -h' or 'man mcrcon' for help.\n"); - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); case 'H': host = optarg; break; case 'P': port = optarg; break; @@ -196,25 +196,25 @@ int main(int argc, char *argv[]) case 'i': terminal_mode = 1; break; case 'r': global_raw_output = 1; break; case 'w': - global_wait_seconds = mcrcon_parse_seconds(optarg); break; + global_wait_seconds = mcrcon_parse_seconds(optarg); + break; + case 'v': puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon\n"); exit(EXIT_SUCCESS); + case 'h': usage(); break; default: exit(EXIT_FAILURE); } } - if (pass == NULL) - { + if (pass == NULL) { puts("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n"); return 0; } - if(optind == argc && terminal_mode == 0) - terminal_mode = 1; - + if(optind == argc && terminal_mode == 0) terminal_mode = 1; // safety features to prevent "IO: Connection reset" bug on the server side atexit(&exit_proc); @@ -276,7 +276,7 @@ void usage(void) ,stdout ); - puts("- mcrcon will start in terminal mode if no commands are given"); + puts("- mcrcon will start in terminal mode if no commands are given"); puts("- Command-line options will override environment variables"); puts("- Rcon commands with spaces must be enclosed in quotes\n"); puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n"); From 018592abc59dd610a67e57d11f077b017c4eb63b Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 19:16:06 +0200 Subject: [PATCH 39/52] Code cleanups --- mcrcon.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 97acb39..1c2787f 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -75,7 +75,7 @@ typedef struct _rc_packet { // endianness related functions bool is_bigendian(void); -int32_t reverse_int32(int32_t n); +int32_t reverse_int32(int32_t n); // Network related functions #ifdef _WIN32 @@ -89,19 +89,19 @@ rc_packet* net_recv_packet(int sd); int net_clean_incoming(int sd, int size); // Misc stuff -void usage(void); +void usage(void); #ifndef _WIN32 -void print_color(int color); +void print_color(int color); #endif -int get_line(char *buffer, int len); -int run_terminal_mode(int sock); -int run_commands(int argc, char *argv[]); +int get_line(char *buffer, int len); +int run_terminal_mode(int sock); +int run_commands(int argc, char *argv[]); // Rcon protocol related functions rc_packet* packet_build(int id, int cmd, char *s1); void packet_print(rc_packet *packet); -int rcon_auth(int sock, char *passwd); -int rcon_command(int sock, char *command); +int rcon_auth(int sock, char *passwd); +int rcon_command(int sock, char *command); // ============================================= @@ -169,8 +169,7 @@ int main(int argc, char *argv[]) if (!host) host = "localhost"; - if(argc < 1 && pass == NULL) - usage(); + if(argc < 1 && pass == NULL) usage(); // default getopt error handler enabled opterr = 1; From fcc9d6d09b2bb0d179c5e58da852898cf4bfb794 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 19:19:30 +0200 Subject: [PATCH 40/52] Tabs are evil --- mcrcon.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 1c2787f..57a4d08 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -74,34 +74,34 @@ typedef struct _rc_packet { // =================================== // endianness related functions -bool is_bigendian(void); -int32_t reverse_int32(int32_t n); +bool is_bigendian(void); +int32_t reverse_int32(int32_t n); // Network related functions #ifdef _WIN32 -void net_init_WSA(void); +void net_init_WSA(void); #endif -void net_close(int sd); -int net_connect(const char *host, const char *port); -int net_send(int sd, const uint8_t *buffer, size_t size); -int net_send_packet(int sd, rc_packet *packet); -rc_packet* net_recv_packet(int sd); -int net_clean_incoming(int sd, int size); +void net_close(int sd); +int net_connect(const char *host, const char *port); +int net_send(int sd, const uint8_t *buffer, size_t size); +int net_send_packet(int sd, rc_packet *packet); +rc_packet* net_recv_packet(int sd); +int net_clean_incoming(int sd, int size); // Misc stuff -void usage(void); +void usage(void); #ifndef _WIN32 -void print_color(int color); +void print_color(int color); #endif -int get_line(char *buffer, int len); -int run_terminal_mode(int sock); -int run_commands(int argc, char *argv[]); +int get_line(char *buffer, int len); +int run_terminal_mode(int sock); +int run_commands(int argc, char *argv[]); // Rcon protocol related functions -rc_packet* packet_build(int id, int cmd, char *s1); -void packet_print(rc_packet *packet); -int rcon_auth(int sock, char *passwd); -int rcon_command(int sock, char *command); +rc_packet* packet_build(int id, int cmd, char *s1); +void packet_print(rc_packet *packet); +int rcon_auth(int sock, char *passwd); +int rcon_command(int sock, char *command); // ============================================= From 0c48fbb847190e0b0af303f337774142c9c14e1e Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Wed, 18 Dec 2019 19:34:08 +0200 Subject: [PATCH 41/52] Remove unused option characters --- mcrcon.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 57a4d08..4f03b80 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -182,18 +182,14 @@ int main(int argc, char *argv[]) puts("Try 'mcrcon -h' or 'man mcrcon' for help.\n"); exit(EXIT_FAILURE); - case 'H': host = optarg; break; - case 'P': port = optarg; break; - case 'p': pass = optarg; break; - case 'C': - case 'c': global_disable_colors = 1; break; - case 'S': - case 's': global_silent_mode = 1; break; - case 'T': - case 't': - case 'I': - case 'i': terminal_mode = 1; break; - case 'r': global_raw_output = 1; break; + case 'H': host = optarg; break; + case 'P': port = optarg; break; + case 'p': pass = optarg; break; + case 'c': global_disable_colors = 1; break; + case 's': global_silent_mode = 1; break; + case 'i': + case 't': terminal_mode = 1; break; + case 'r': global_raw_output = 1; break; case 'w': global_wait_seconds = mcrcon_parse_seconds(optarg); break; From 5fb20c2b836403491d31386c2a8e66d8fbd72b1a Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Thu, 19 Dec 2019 17:32:47 +0200 Subject: [PATCH 42/52] Update mcrcon.1 and README.md --- README.md | 2 +- mcrcon.1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 87c5d54..bd18792 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Option: -s Silent mode -c Disable colors -r Output raw packets - -w Wait for specified duration (1 - 600 seconds) between each command + -w Wait for specified duration (seconds) between each command (1 - 600s) -h Print usage -v Version information ``` diff --git a/mcrcon.1 b/mcrcon.1 index f84d799..4ff3d0b 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -28,7 +28,7 @@ Disable colors .IP -r Output raw packets .IP -w -Wait for specified duration (1 - 600 seconds) between each command +Wait for specified duration (seconds) between each command (1 - 600s) .IP -h Print usage .IP -v From 4f3a455095b5facf8c2d84b9a237fa800585e85c Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Thu, 19 Dec 2019 17:34:19 +0200 Subject: [PATCH 43/52] Add more validation to mcrcon_parse_seconds() --- mcrcon.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 4f03b80..e66702d 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -139,15 +139,21 @@ void sighandler(/*int sig*/) unsigned int mcrcon_parse_seconds(char *str) { - long result = strtol(str, NULL, 10); + char *end; + long result = strtol(str, &end, 10); if (errno != 0) { fprintf(stderr, "-w invalid value.\nerror %d: %s\n", errno, strerror(errno)); exit(EXIT_FAILURE); } + if (end == str) { + fprintf(stderr, "-w invalid value (not a number?)\n"); + exit(EXIT_FAILURE); + } + if (result <= 0 || result > MAX_WAIT_TIME) { - fprintf(stderr, "-w option value is out of range.\nAcceptable value is 1 - %d seconds.\n", MAX_WAIT_TIME); + fprintf(stderr, "-w value out of range.\nAcceptable value is 1 - %d (seconds).\n", MAX_WAIT_TIME); exit(EXIT_FAILURE); } @@ -178,10 +184,6 @@ int main(int argc, char *argv[]) { switch (opt) { - case '?': - puts("Try 'mcrcon -h' or 'man mcrcon' for help.\n"); - exit(EXIT_FAILURE); - case 'H': host = optarg; break; case 'P': port = optarg; break; case 'p': pass = optarg; break; @@ -195,17 +197,19 @@ int main(int argc, char *argv[]) break; case 'v': - puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon\n"); + puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon"); exit(EXIT_SUCCESS); case 'h': usage(); break; - - default: exit(EXIT_FAILURE); + case '?': + default: + puts("Try 'mcrcon -h' or 'man mcrcon' for help."); + exit(EXIT_FAILURE); } } if (pass == NULL) { - puts("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n"); + puts("You must give password (-p password).\nTry 'mcrcon -h' or 'man mcrcon' for help."); return 0; } @@ -261,7 +265,7 @@ void usage(void) " -s\t\tSilent mode\n" " -c\t\tDisable colors\n" " -r\t\tOutput raw packets\n" - " -w\t\tWait for specified duration (1 - 600 seconds) between each command\n" + " -w\t\tWait for specified duration (seconds) between each command (1 - 600s)\n" " -h\t\tPrint usage\n" " -v\t\tVersion information\n\n" "Server address, port and password can be set with following environment variables:\n" @@ -275,7 +279,7 @@ void usage(void) puts("- Command-line options will override environment variables"); puts("- Rcon commands with spaces must be enclosed in quotes\n"); puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n"); - puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/\n"); + puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/"); #ifdef _WIN32 puts("Press enter to exit."); From 8b75ddf4bfd9280e6ce5237293f55cc3b96242eb Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 20 Dec 2019 19:56:05 +0200 Subject: [PATCH 44/52] Indentation style change --- mcrcon.c | 185 ++++++++++++++++++++++++------------------------------- 1 file changed, 79 insertions(+), 106 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index e66702d..a1a7ee1 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -169,11 +169,8 @@ int main(int argc, char *argv[]) char *pass = getenv("MCRCON_PASS"); char *port = getenv("MCRCON_PORT"); - if (!port) - port = "25575"; - - if (!host) - host = "localhost"; + if (!port) port = "25575"; + if (!host) host = "localhost"; if(argc < 1 && pass == NULL) usage(); @@ -182,8 +179,7 @@ int main(int argc, char *argv[]) while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:i")) != -1) { - switch (opt) - { + switch (opt) { case 'H': host = optarg; break; case 'P': port = optarg; break; case 'p': pass = optarg; break; @@ -213,7 +209,8 @@ int main(int argc, char *argv[]) return 0; } - if(optind == argc && terminal_mode == 0) terminal_mode = 1; + if(optind == argc && terminal_mode == 0) + terminal_mode = 1; // safety features to prevent "IO: Connection reset" bug on the server side atexit(&exit_proc); @@ -224,7 +221,8 @@ int main(int argc, char *argv[]) #ifdef _WIN32 net_init_WSA(); console_handle = GetStdHandle(STD_OUTPUT_HANDLE); - if (console_handle == INVALID_HANDLE_VALUE) console_handle = NULL; + if (console_handle == INVALID_HANDLE_VALUE) + console_handle = NULL; #endif // open socket @@ -233,15 +231,13 @@ int main(int argc, char *argv[]) int exit_code = EXIT_SUCCESS; // auth & commands - if (rcon_auth(global_rsock, pass)) - { + if (rcon_auth(global_rsock, pass)) { if (terminal_mode) run_terminal_mode(global_rsock); else exit_code = run_commands(argc, argv); } - else // auth failed - { + else { // auth failed fprintf(stdout, "Authentication failed!\n"); exit_code = EXIT_FAILURE; } @@ -299,8 +295,7 @@ void net_init_WSA(void) WORD version = MAKEWORD(2, 2); int err = WSAStartup(version, &wsadata); - if (err != 0) - { + if (err != 0) { fprintf(stderr, "WSAStartup failed. Error: %d.\n", err); exit(EXIT_FAILURE); } @@ -310,12 +305,12 @@ void net_init_WSA(void) // socket close and cleanup void net_close(int sd) { -#ifdef _WIN32 - closesocket(sd); - WSACleanup(); -#else - close(sd); -#endif + #ifdef _WIN32 + closesocket(sd); + WSACleanup(); + #else + close(sd); + #endif } // Opens and connects socket @@ -338,27 +333,26 @@ int net_connect(const char *host, const char *port) #endif int ret = getaddrinfo(host, port, &hints, &server_info); - if (ret != 0) - { + if (ret != 0) { fprintf(stderr, "Name resolution failed.\n"); #ifdef _WIN32 fprintf(stderr, "Error %d: %s", ret, gai_strerror(ret)); #else fprintf(stderr, "Error %d: %s\n", ret, gai_strerror(ret)); #endif + exit(EXIT_FAILURE); } // Go through the hosts and try to connect - for (p = server_info; p != NULL; p = p->ai_next) - { + for (p = server_info; p != NULL; p = p->ai_next) { sd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (sd == -1) continue; ret = connect(sd, p->ai_addr, p->ai_addrlen); - if (ret == -1) - { + if (ret == -1) { net_close(sd); continue; } @@ -366,19 +360,18 @@ int net_connect(const char *host, const char *port) break; } - if (p == NULL) - { + if (p == NULL) { /* TODO (Tiiffi): Check why windows does not report errors */ fprintf(stderr, "Connection failed.\n"); #ifndef _WIN32 fprintf(stderr, "Error %d: %s\n", errno, strerror(errno)); #endif + freeaddrinfo(server_info); exit(EXIT_FAILURE); } freeaddrinfo(server_info); - return sd; } @@ -387,11 +380,11 @@ int net_send(int sd, const uint8_t *buff, size_t size) size_t sent = 0; size_t left = size; - while (sent < size) - { + while (sent < size) { int result = send(sd, (const char *) buff + sent, left, 0); - if (result == -1) return -1; + if (result == -1) + return -1; sent += result; left -= sent; @@ -409,10 +402,9 @@ int net_send_packet(int sd, rc_packet *packet) bytesleft = len = packet->size + sizeof(int); - while(total < len) - { + while (total < len) { ret = send(sd, (char *) packet + total, bytesleft, 0); - if(ret == -1) { break; } + if(ret == -1) break; total += ret; bytesleft -= ret; } @@ -429,22 +421,19 @@ rc_packet *net_recv_packet(int sd) int ret = recv(sd, (char *) &psize, sizeof(int), 0); - if (ret == 0) - { + if (ret == 0) { fprintf(stderr, "Connection lost.\n"); global_connection_alive = 0; return NULL; } - if (ret != sizeof(int)) - { + if (ret != sizeof(int)) { fprintf(stderr, "Error: recv() failed. Invalid packet size (%d).\n", ret); global_connection_alive = 0; return NULL; } - if (psize < 10 || psize > DATA_BUFFSIZE) - { + if (psize < 10 || psize > DATA_BUFFSIZE) { fprintf(stderr, "Warning: invalid packet size (%d). Must over 10 and less than %d.\n", psize, DATA_BUFFSIZE); if(psize > DATA_BUFFSIZE || psize < 0) psize = DATA_BUFFSIZE; @@ -456,11 +445,9 @@ rc_packet *net_recv_packet(int sd) packet.size = psize; int received = 0; - while (received < psize) - { + while (received < psize) { ret = recv(sd, (char *) &packet + sizeof(int) + received, psize - received, 0); - if (ret == 0) /* connection closed before completing receving */ - { + if (ret == 0) { /* connection closed before completing receving */ fprintf(stderr, "Connection lost.\n"); global_connection_alive = 0; return NULL; @@ -475,11 +462,9 @@ rc_packet *net_recv_packet(int sd) int net_clean_incoming(int sd, int size) { char tmp[size]; - int ret = recv(sd, tmp, size, 0); - if(ret == 0) - { + if(ret == 0) { fprintf(stderr, "Connection lost.\n"); global_connection_alive = 0; } @@ -491,8 +476,7 @@ void print_color(int color) { // sh compatible color array #ifndef _WIN32 - char *colors[] = - { + char *colors[] = { "\033[0;30m", /* 00 BLACK 0x30 */ "\033[0;34m", /* 01 BLUE 0x31 */ "\033[0;32m", /* 02 GREEN 0x32 */ @@ -512,22 +496,22 @@ void print_color(int color) "\033[4m" /* 16 UNDERLINE 0x6e */ }; - if(color == 0 || color == 0x72) /* 0x72: 'r' */ - { - fputs("\033[0m", stdout); /* CANCEL COLOR */ - } + /* 0x72: 'r' */ + if(color == 0 || color == 0x72) fputs("\033[0m", stdout); /* CANCEL COLOR */ else #endif { if(color >= 0x61 && color <= 0x66) color -= 0x57; - else if(color >= 0x30 && color <= 0x39) color -= 0x30; - else if(color == 0x6e) color=16; /* 0x6e: 'n' */ + else if(color >= 0x30 && color <= 0x39) + color -= 0x30; + else if(color == 0x6e) + color=16; /* 0x6e: 'n' */ else return; #ifndef _WIN32 - fputs(colors[color], stdout); + fputs(colors[color], stdout); #else - SetConsoleTextAttribute(console_handle, color); + SetConsoleTextAttribute(console_handle, color); #endif } } @@ -535,9 +519,10 @@ void print_color(int color) // this hacky mess might use some optimizing void packet_print(rc_packet *packet) { - if (global_raw_output == 1) - { - for (int i = 0; packet->data[i] != 0; ++i) putchar(packet->data[i]); + if (global_raw_output == 1) { + for (int i = 0; packet->data[i] != 0; ++i) + putchar(packet->data[i]); + return; } @@ -545,21 +530,18 @@ void packet_print(rc_packet *packet) int def_color = 0; #ifdef _WIN32 - CONSOLE_SCREEN_BUFFER_INFO console_info; + CONSOLE_SCREEN_BUFFER_INFO console_info; - if (GetConsoleScreenBufferInfo(console_handle, &console_info) != 0) - def_color = console_info.wAttributes + 0x30; - else - def_color = 0x37; + if (GetConsoleScreenBufferInfo(console_handle, &console_info) != 0) { + def_color = console_info.wAttributes + 0x30; + } else def_color = 0x37; #endif // colors enabled so try to handle the bukkit colors for terminal - if (global_disable_colors != 1) - { - for (i = 0; (unsigned char) packet->data[i] != 0; ++i) - { + if (global_disable_colors != 1) { + for (i = 0; (unsigned char) packet->data[i] != 0; ++i) { if (packet->data[i] == 0x0A) print_color(def_color); - else if((unsigned char) packet->data[i] == 0xc2 && (unsigned char) packet->data[i+1] == 0xa7){ + else if((unsigned char) packet->data[i] == 0xc2 && (unsigned char) packet->data[i+1] == 0xa7) { print_color(packet->data[i+=2]); continue; } @@ -568,11 +550,9 @@ void packet_print(rc_packet *packet) print_color(def_color); // cancel coloring } // strip colors - else - { - for (i = 0; (unsigned char) packet->data[i] != 0; ++i) - { - if ((unsigned char) packet->data[i] == 0xc2 && (unsigned char) packet->data[i+1] == 0xa7){ + else { + for (i = 0; (unsigned char) packet->data[i] != 0; ++i) { + if ((unsigned char) packet->data[i] == 0xc2 && (unsigned char) packet->data[i+1] == 0xa7) { i+=2; continue; } @@ -581,8 +561,7 @@ void packet_print(rc_packet *packet) } // print newline if string has no newline - if (packet->data[i-1] != 10 && packet->data[i-1] != 13) - putchar('\n'); + if (packet->data[i-1] != 10 && packet->data[i-1] != 13) putchar('\n'); } rc_packet *packet_build(int id, int cmd, char *s1) @@ -591,8 +570,7 @@ rc_packet *packet_build(int id, int cmd, char *s1) // size + id + cmd + s1 + s2 NULL terminator int s1_len = strlen(s1); - if (s1_len > DATA_BUFFSIZE) - { + if (s1_len > DATA_BUFFSIZE) { fprintf(stderr, "Warning: Command string too long (%d). Maximum allowed: %d.\n", s1_len, DATA_BUFFSIZE); return NULL; } @@ -628,8 +606,7 @@ int rcon_auth(int sock, char *passwd) int rcon_command(int sock, char *command) { rc_packet *packet = packet_build(RCON_PID, RCON_EXEC_COMMAND, command); - if (packet == NULL) - { + if (packet == NULL) { global_connection_alive = 0; return 0; } @@ -643,16 +620,9 @@ int rcon_command(int sock, char *command) if (packet->id != RCON_PID) return 0; - if (!global_silent_mode) - { - /* - if(packet->size == 10) { - printf("Unknown command \"%s\". Type \"help\" or \"?\" for help.\n", command); - } - else - */ - if (packet->size > 10) - packet_print(packet); + if (!global_silent_mode) { + if (packet->size > 10) + packet_print(packet); } return 1; @@ -662,16 +632,14 @@ int run_commands(int argc, char *argv[]) { int i = optind; - for (;;) - { + for (;;) { if (!rcon_command(global_rsock, argv[i])) return EXIT_FAILURE; if (++i >= argc) return EXIT_SUCCESS; - if (global_wait_seconds > 0) - { + if (global_wait_seconds > 0) { #ifdef _WIN32 Sleep(global_wait_seconds * 1000); #else @@ -689,9 +657,9 @@ int run_terminal_mode(int sock) puts("Logged in. Type \"Q\" to quit!"); - while (global_connection_alive) - { + while (global_connection_alive) { int len = get_line(command, DATA_BUFFSIZE); + if(command[0] == 'Q' && command[1] == 0) break; @@ -707,22 +675,24 @@ int run_terminal_mode(int sock) // gets line from stdin and deals with rubbish left in the input buffer int get_line(char *buffer, int bsize) { - int ch, len; - fputs(">", stdout); char *ret = fgets(buffer, bsize, stdin); - if (ret == NULL) exit(EXIT_FAILURE); + if (ret == NULL) + exit(EXIT_FAILURE); - if (buffer[0] == 0) global_connection_alive = 0; + if (buffer[0] == 0) + global_connection_alive = 0; // remove unwanted characters from the buffer buffer[strcspn(buffer, "\r\n")] = '\0'; - len = strlen(buffer); + int len = strlen(buffer); // clean input buffer if needed - if (len == bsize - 1) + if (len == bsize - 1) { + int ch; while ((ch = getchar()) != '\n' && ch != EOF); + } return len; } @@ -731,7 +701,10 @@ int get_line(char *buffer, int bsize) bool is_bigendian(void) { const int32_t n = 1; - if (*(uint8_t *) &n == 0 ) return true; + + if (*(const uint8_t *) &n == 0 ) + return true; + return false; } From c62d5c8a7019b7ac34a6cdb732fbe2cbcddb7c4d Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 20 Dec 2019 20:58:40 +0200 Subject: [PATCH 45/52] Update usage and version texts --- mcrcon.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index a1a7ee1..c2637c7 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -57,7 +57,6 @@ #define RCON_AUTH_RESPONSE 2 #define RCON_PID 0xBADC0DE - #define DATA_BUFFSIZE 4096 // rcon packet structure @@ -193,7 +192,8 @@ int main(int argc, char *argv[]) break; case 'v': - puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon"); + puts(VER_STR" - https://github.com/Tiiffi/mcrcon"); + puts("Bug reports: tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/"); exit(EXIT_SUCCESS); case 'h': usage(); break; @@ -219,9 +219,9 @@ int main(int argc, char *argv[]) signal(SIGINT, &sighandler); #ifdef _WIN32 - net_init_WSA(); - console_handle = GetStdHandle(STD_OUTPUT_HANDLE); - if (console_handle == INVALID_HANDLE_VALUE) + net_init_WSA(); + console_handle = GetStdHandle(STD_OUTPUT_HANDLE); + if (console_handle == INVALID_HANDLE_VALUE) console_handle = NULL; #endif @@ -250,7 +250,7 @@ int main(int argc, char *argv[]) void usage(void) { - fputs( + puts( "Usage: "IN_NAME" [OPTIONS] [COMMANDS]\n\n" "Send rcon commands to Minecraft server.\n\n" "Options:\n" @@ -267,15 +267,15 @@ void usage(void) "Server address, port and password can be set with following environment variables:\n" " MCRCON_HOST\n" " MCRCON_PORT\n" - " MCRCON_PASS\n\n" - ,stdout + " MCRCON_PASS\n" ); - puts("- mcrcon will start in terminal mode if no commands are given"); - puts("- Command-line options will override environment variables"); - puts("- Rcon commands with spaces must be enclosed in quotes\n"); + puts ( + "- mcrcon will start in terminal mode if no commands are given\n" + "- Command-line options will override environment variables\n" + "- Rcon commands with spaces must be enclosed in quotes\n" + ); puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n"); - puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/"); #ifdef _WIN32 puts("Press enter to exit."); From cffa73d2235bf97fe75210372324ab1f69e283ad Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Fri, 20 Dec 2019 21:02:21 +0200 Subject: [PATCH 46/52] Remove unused functions --- mcrcon.c | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index c2637c7..c703665 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -68,14 +68,11 @@ typedef struct _rc_packet { // ignoring string2 for now } rc_packet; + // =================================== // FUNCTION DEFINITIONS // =================================== -// endianness related functions -bool is_bigendian(void); -int32_t reverse_int32(int32_t n); - // Network related functions #ifdef _WIN32 void net_init_WSA(void); @@ -696,28 +693,3 @@ int get_line(char *buffer, int bsize) return len; } - -// http://www.ibm.com/developerworks/aix/library/au-endianc/ -bool is_bigendian(void) -{ - const int32_t n = 1; - - if (*(const uint8_t *) &n == 0 ) - return true; - - return false; -} - -int32_t reverse_int32(int32_t n) -{ - int32_t tmp; - uint8_t *t = (uint8_t *) &tmp; - uint8_t *p = (uint8_t *) &n; - - t[0] = p[3]; - t[1] = p[2]; - t[2] = p[1]; - t[3] = p[0]; - - return tmp; -} From d2ebbd8f12f78547bbe13a6e7ede11407e0df0e7 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sat, 21 Dec 2019 22:16:34 +0200 Subject: [PATCH 47/52] Update various document files --- CHANGELOG.md | 1 + INSTALL.md | 8 ++++---- README.md | 6 ++++-- mcrcon.1 | 8 ++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa9fded..1df0bc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ###### 0.7.0 - Add -w option for rcon command throttling + * Thanks HorlogeSkynet @ Github ###### 0.6.2 - Set default address to localhost diff --git a/INSTALL.md b/INSTALL.md index 859b037..6a3bec9 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -1,4 +1,4 @@ -Compiling and installing +Building and installing ------------------------ Only dependency is C library and POSIX getopt support. @@ -7,9 +7,9 @@ Compiling with GCC or CLANG: cc -std=gnu99 -Wpedantic -Wall -Wextra -Os -s -o mcrcon mcrcon.c -Note: on Window remember to link with winsockets by adding "-lws2_32" to your compiler command line. +Note: on Windows remember to link with winsock by adding `-lws2_32` to your compiler command line. -Or you can just run "make": +Or you can just run "**make**": make - compiles mcrcon make install - installs compiled binaries and manpage to the system @@ -19,4 +19,4 @@ Or you can just run "make": /usr/local/bin/mcrcon /usr/local/share/man/man1/mcrcon.1 -Makefile "install" and "uninstall" rules are disabled on windows. +Makefile "**install**" and "**uninstall**" rules are disabled on windows. diff --git a/README.md b/README.md index bd18792..300695a 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ See https://pkgs.org/download/mcrcon for available packages in various Linux dis - Gentoo Linux: https://packages.gentoo.org/packages/games-util/mcrcon - Arch Linux: https://aur.archlinux.org/packages/mcrcon/ -##### from sources: +##### building from sources: ```sh git clone https://github.com/Tiiffi/mcrcon.git cd mcrcon @@ -57,7 +57,9 @@ MCRCON_PASS - Rcon commands with spaces must be enclosed in quotes Example: - ```mcrcon -H my.minecraft.server -p password -w 2 "say Server is restarting!" save-all stop``` +> Send three commands ("say", "save-all", "stop") and wait five seconds between the commands. + + ```mcrcon -H my.minecraft.server -p password -w 5 "say Server is restarting!" save-all stop``` --- diff --git a/mcrcon.1 b/mcrcon.1 index 4ff3d0b..e39ec9e 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -37,7 +37,7 @@ Output version information Commands with spaces must be enclosed in quotes. .br mcrcon will start in terminal mode if no commands are given. -.SH ENVIRONMENTAL VARIABLES +.SH ENVIRONMENT VARIABLES Server address, port and password can be set with following environment variables: .PP \fBMCRCON_HOST @@ -58,9 +58,9 @@ Send "weather clear" command to server using custom port 1337 \fBmcrcon\fR -H my.minecraft.server -P 1337 -p password "weather clear" .RE .PP -Send three commands to server ("say", "save-all" and "stop"), and wait 2 seconds between them +Send three commands ("say", "save-all", "stop") and wait five seconds between the commands. .RS -\fBmcrcon\fR -H my.minecraft.server -p password -w 2 "say Server is restarting!" save-all stop +\fBmcrcon\fR -H my.minecraft.server -p password -w 5 "say Server is restarting!" save-all stop .RE .SH BUGS -Bugs can be reported to \fBtiiffi+mcrcon at gmail\fR or \fBhttps://github.com/Tiiffi/mcrcon/issues/\fR +Report bugs to \fBtiiffi+mcrcon at gmail\fR or \fBhttps://github.com/Tiiffi/mcrcon/issues/\fR From 3c071c0c454560c483e7f97b52995cc1a45a5507 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sat, 21 Dec 2019 22:25:08 +0200 Subject: [PATCH 48/52] - Add exit string detection - Stylistic change in getopt loop --- mcrcon.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index c703665..d9f26be 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -158,7 +158,6 @@ unsigned int mcrcon_parse_seconds(char *str) int main(int argc, char *argv[]) { - int opt; int terminal_mode = 0; char *host = getenv("MCRCON_HOST"); @@ -172,8 +171,7 @@ int main(int argc, char *argv[]) // default getopt error handler enabled opterr = 1; - - while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:i")) != -1) + for (int opt = getopt(argc, argv, "vrtcshw:H:p:P:i"); opt != -1;) { switch (opt) { case 'H': host = optarg; break; @@ -190,7 +188,7 @@ int main(int argc, char *argv[]) case 'v': puts(VER_STR" - https://github.com/Tiiffi/mcrcon"); - puts("Bug reports: tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/"); + puts("Bug reports:\n\ttiiffi+mcrcon at gmail\n\thttps://github.com/Tiiffi/mcrcon/issues/"); exit(EXIT_SUCCESS); case 'h': usage(); break; @@ -528,14 +526,13 @@ void packet_print(rc_packet *packet) #ifdef _WIN32 CONSOLE_SCREEN_BUFFER_INFO console_info; - if (GetConsoleScreenBufferInfo(console_handle, &console_info) != 0) { def_color = console_info.wAttributes + 0x30; } else def_color = 0x37; #endif // colors enabled so try to handle the bukkit colors for terminal - if (global_disable_colors != 1) { + if (global_disable_colors == 0) { for (i = 0; (unsigned char) packet->data[i] != 0; ++i) { if (packet->data[i] == 0x0A) print_color(def_color); else if((unsigned char) packet->data[i] == 0xc2 && (unsigned char) packet->data[i+1] == 0xa7) { @@ -652,11 +649,14 @@ int run_terminal_mode(int sock) int ret = 0; char command[DATA_BUFFSIZE] = {0x00}; - puts("Logged in. Type \"Q\" to quit!"); + puts("Logged in. Type 'quit' or 'exit' to quit."); while (global_connection_alive) { int len = get_line(command, DATA_BUFFSIZE); + if ((strcmp(command, "exit") && strcmp(command, "quit")) == 0) + break; + if(command[0] == 'Q' && command[1] == 0) break; @@ -673,6 +673,7 @@ int run_terminal_mode(int sock) int get_line(char *buffer, int bsize) { fputs(">", stdout); + char *ret = fgets(buffer, bsize, stdin); if (ret == NULL) exit(EXIT_FAILURE); From 876201fe5a9a51653ce0b580638e54a6116008e3 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sat, 21 Dec 2019 23:12:39 +0200 Subject: [PATCH 49/52] Add workaround to prevent server-side bug. https://bugs.mojang.com/browse/MC-154617 --- CHANGELOG.md | 4 ++++ mcrcon.c | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1df0bc5..9ccc0d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ #### Version history: +###### 0.7.1 + - Add workaround to prevent server-side bug. + https://bugs.mojang.com/browse/MC-154617 + ###### 0.7.0 - Add -w option for rcon command throttling * Thanks HorlogeSkynet @ Github diff --git a/mcrcon.c b/mcrcon.c index d9f26be..a213bf5 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -47,7 +47,7 @@ #include #endif -#define VERSION "0.7.0" +#define VERSION "0.7.1" #define IN_NAME "mcrcon" #define VER_STR IN_NAME" "VERSION" (built: "__DATE__" "__TIME__")" @@ -171,7 +171,8 @@ int main(int argc, char *argv[]) // default getopt error handler enabled opterr = 1; - for (int opt = getopt(argc, argv, "vrtcshw:H:p:P:i"); opt != -1;) + int opt; + while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:i")) != -1) { switch (opt) { case 'H': host = optarg; break; @@ -652,6 +653,7 @@ int run_terminal_mode(int sock) puts("Logged in. Type 'quit' or 'exit' to quit."); while (global_connection_alive) { + putchar('>'); int len = get_line(command, DATA_BUFFSIZE); if ((strcmp(command, "exit") && strcmp(command, "quit")) == 0) @@ -663,6 +665,17 @@ int run_terminal_mode(int sock) if(len > 0 && global_connection_alive) ret += rcon_command(sock, command); + /* Special case for "stop" command to prevent server-side bug. + * https://bugs.mojang.com/browse/MC-154617 + * + * NOTE: This is hacky workaround which should be handled better to + * ensure compatibility with other servers using source RCON. + * NOTE: strcasecmp() is POSIX function. + */ + if (strcasecmp(command, "stop") == 0) { + break; + } + command[0] = len = 0; } @@ -672,8 +685,6 @@ int run_terminal_mode(int sock) // gets line from stdin and deals with rubbish left in the input buffer int get_line(char *buffer, int bsize) { - fputs(">", stdout); - char *ret = fgets(buffer, bsize, stdin); if (ret == NULL) exit(EXIT_FAILURE); From 83cdaae3628f8d99d949d52bcd8abfabea438b9f Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sat, 21 Dec 2019 23:13:19 +0200 Subject: [PATCH 50/52] Update version number --- mcrcon.1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcrcon.1 b/mcrcon.1 index e39ec9e..3fb80fa 100644 --- a/mcrcon.1 +++ b/mcrcon.1 @@ -1,7 +1,7 @@ .\" Process this file with .\" groff -man -Tascii mcrcon.1 .\" -.TH MCRCON 1 "December 2019" "Version 0.7.0" +.TH MCRCON 1 "December 2019" "Version 0.7.1" .SH NAME mcrcon \- send rcon commands to a Minecraft server .SH SYNOPSIS From af5e88b528136f30efd7a86eee1bffb795eb36e3 Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sun, 22 Dec 2019 00:05:31 +0200 Subject: [PATCH 51/52] Update Makefile --- Makefile | 13 ++++++++----- mcrcon.c | 10 +++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 3d4b6ce..c755128 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ -# if you want to cross compile -# export PATH=$PATH:/path/to/compiler/bin -# export CROSS_COMPILE=arm-none-linux-gnueabi- -# make +# if you want to cross compile: +# export PATH=$PATH:/path/to/compiler/bin +# export CROSS_COMPILE=arm-none-linux-gnueabi- +# make +# +# Windows cross compile: +# i686-w64-mingw32-gcc -std=gnu99 -Wall -Wextra -Wpedantic -Os -s -o mcrcon.exe mcrcon.c -lws2_32 EXENAME = mcrcon PREFIX ?= /usr/local @@ -10,7 +13,7 @@ INSTALL = install LINKER = RM = rm -v -f -CC = cc +CC = gcc CFLAGS = -std=gnu99 -Wall -Wextra -Wpedantic -Os -s EXTRAFLAGS ?= -fstack-protector-strong diff --git a/mcrcon.c b/mcrcon.c index a213bf5..5486ca7 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -493,15 +493,15 @@ void print_color(int color) }; /* 0x72: 'r' */ - if(color == 0 || color == 0x72) fputs("\033[0m", stdout); /* CANCEL COLOR */ + if (color == 0 || color == 0x72) fputs("\033[0m", stdout); /* CANCEL COLOR */ else #endif { - if(color >= 0x61 && color <= 0x66) color -= 0x57; - else if(color >= 0x30 && color <= 0x39) + if (color >= 0x61 && color <= 0x66) color -= 0x57; + else if (color >= 0x30 && color <= 0x39) color -= 0x30; - else if(color == 0x6e) - color=16; /* 0x6e: 'n' */ + else if (color == 0x6e) + color = 16; /* 0x6e: 'n' */ else return; #ifndef _WIN32 From f351fb134e4880eecc09d82a0677dbdf117c095a Mon Sep 17 00:00:00 2001 From: Tiiffi Date: Sun, 22 Dec 2019 16:27:36 +0200 Subject: [PATCH 52/52] - Deprecate -i flag for terminal mode - Use strcasecmp() instead strcmp() to check exit command --- mcrcon.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mcrcon.c b/mcrcon.c index 5486ca7..b5d694a 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -172,7 +172,7 @@ int main(int argc, char *argv[]) // default getopt error handler enabled opterr = 1; int opt; - while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:i")) != -1) + while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:")) != -1) { switch (opt) { case 'H': host = optarg; break; @@ -180,7 +180,7 @@ int main(int argc, char *argv[]) case 'p': pass = optarg; break; case 'c': global_disable_colors = 1; break; case 's': global_silent_mode = 1; break; - case 'i': + case 'i': /* reserved for interp mode */break; case 't': terminal_mode = 1; break; case 'r': global_raw_output = 1; break; case 'w': @@ -656,7 +656,7 @@ int run_terminal_mode(int sock) putchar('>'); int len = get_line(command, DATA_BUFFSIZE); - if ((strcmp(command, "exit") && strcmp(command, "quit")) == 0) + if ((strcasecmp(command, "exit") && strcasecmp(command, "quit")) == 0) break; if(command[0] == 'Q' && command[1] == 0)