Added Install / uninstall rules to Makefile and made changes to error printing. Also now password is not set by default and terminal mode is activated automatically if there are no commands to execute.

This commit is contained in:
Tiiffi
2016-11-13 18:12:03 +02:00
parent 449eac6358
commit d92a60bed4
2 changed files with 56 additions and 36 deletions

View File

@ -19,6 +19,18 @@ CFLAGS = -std=gnu99 -Wall -Wextra -Wpedantic -Os -s
all: all:
$(CROSS_COMPILE)$(CC) $(CFLAGS) -o $(EXENAME) mcrcon.c $(LINKER) $(CROSS_COMPILE)$(CC) $(CFLAGS) -o $(EXENAME) mcrcon.c $(LINKER)
ifneq ($(OS), Windows_NT)
install:
cp $(EXENAME) /usr/local/bin/$(EXENAME)
chmod 0755 /usr/local/bin/$(EXENAME)
cp mcrcon.1 /usr/local/share/man/man1/mcrcon.1
chmod 0644 /usr/local/share/man/man1/mcrcon.1
@echo "\nmcrcon installed. Run 'make uninstall' if you want to uninstall.\n"
uninstall:
rm -f /usr/local/bin/$(EXENAME)
rm -f /usr/local/share/man/man1/mcrcon.1
@echo "\nmcrcon uninstalled.\n"
endif
clean: clean:
$(RM) $(EXENAME) $(RM) $(EXENAME)

View File

@ -58,17 +58,17 @@
/* Safe value I think. This should me made dynamic for more stable performance! */ /* Safe value I think. This should me made dynamic for more stable performance! */
#define DATA_BUFFSIZE 10240 #define DATA_BUFFSIZE 10240
#define VERSION "0.0.5" #define VERSION "0.0.6"
#define IN_NAME "mcrcon" #define IN_NAME "mcrcon"
#define VER_STR IN_NAME" "VERSION #define VER_STR IN_NAME" "VERSION
/* rcon packet structure */ // rcon packet structure
typedef struct _rc_packet { typedef struct _rc_packet {
int size; int size;
int id; int id;
int cmd; int cmd;
char data[DATA_BUFFSIZE]; char data[DATA_BUFFSIZE];
/* ignoring string2 atm.. */ // ignoring string2 atm.
} rc_packet; } rc_packet;
/* =================================== */ /* =================================== */
@ -90,13 +90,17 @@ int net_send_packet(int sd, rc_packet *packet);
rc_packet* net_recv_packet(int sd); rc_packet* net_recv_packet(int sd);
int net_clean_incoming(int sd, int size); int net_clean_incoming(int sd, int size);
// Other stuff // Misc stuff
void usage(void); void usage(void);
void error(char *errstring); void error(char *errstring);
#ifndef _WIN32 #ifndef _WIN32
void print_color(int color); void print_color(int color);
#endif #endif
int get_line(char *buffer, int len);
int run_terminal_mode(int rsock);
int run_commands(int argc, char *argv[]);
// Rcon protocol related functions
rc_packet* packet_build(int id, int cmd, char *s1); 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); uint8_t *packet_build_malloc(size_t *size, int32_t id, int32_t cmd, char *string);
void packet_print(rc_packet *packet); void packet_print(rc_packet *packet);
@ -104,31 +108,29 @@ void packet_print(rc_packet *packet);
int rcon_auth(int rsock, char *passwd); int rcon_auth(int rsock, char *passwd);
int rcon_command(int rsock, char *command); int rcon_command(int rsock, char *command);
int get_line(char *buffer, int len);
int run_terminal_mode(int rsock);
int run_commands(int argc, char *argv[]);
// ============================================= // =============================================
// GLOBAL VARIABLES // GLOBAL VARIABLES
// ============================================= // =============================================
int raw_output = 0; static int raw_output = 0;
int silent_mode = 0; static int silent_mode = 0;
int print_colors = 1; static int print_colors = 1;
int connection_alive = 1; static int connection_alive = 1;
int rsock; /* rcon socket */ static int rsock;
#ifdef _WIN32 #ifdef _WIN32
/* console coloring on windows */ // console coloring on windows
HANDLE console_handle; HANDLE console_handle;
#endif #endif
/* safety stuff (windows is still misbehaving) */ // safety stuff (windows is still misbehaving)
void exit_proc(void) void exit_proc(void)
{ {
if (rsock != -1) net_close(rsock); if (rsock != -1)
net_close(rsock);
} }
/* Check windows & linux behaviour !!! */ // Check windows & linux behaviour !!!
void sighandler(/*int sig*/) void sighandler(/*int sig*/)
{ {
connection_alive = 0; connection_alive = 0;
@ -143,12 +145,14 @@ int main(int argc, char *argv[])
int terminal_mode = 0; int terminal_mode = 0;
char *host = NULL; char *host = NULL;
char *pass = ""; char *pass = NULL; // this should be null by default!
char *port = "25575"; char *port = "25575";
if(argc < 2) usage(); if(argc < 2)
usage();
opterr = 1; /* default error handler enabled */ // default getopt error handler enabled
opterr = 1;
while ((opt = getopt(argc, argv, "rtcshH:p:P:i")) != -1) while ((opt = getopt(argc, argv, "rtcshH:p:P:i")) != -1)
{ {
@ -184,15 +188,19 @@ int main(int argc, char *argv[])
if (host == NULL) if (host == NULL)
{ {
fputs("Host not defined. Check -H flag.\n\n", stdout); fputs("Host not defined (-H flag). Try 'mcrcon -h' for more information.\n\n", stdout);
usage(); return 0;
}
if (pass == NULL)
{
fputs("Password not defined (-p flag). Try 'mcrcon -h' for more information.\n\n", stdout);
return 0;
} }
if(optind == argc && terminal_mode == 0) if(optind == argc && terminal_mode == 0)
{ terminal_mode = 1;
fputs("No commands specified.\n\n", stdout);
usage();
}
/* safety features to prevent "IO: Connection reset" bug on the server side */ /* safety features to prevent "IO: Connection reset" bug on the server side */
atexit(&exit_proc); atexit(&exit_proc);
@ -233,23 +241,23 @@ int main(int argc, char *argv[])
void usage(void) void usage(void)
{ {
fputs( fputs(
"Usage: "IN_NAME" [OPTIONS]... [COMMANDS]...\n" "Usage: "IN_NAME" [OPTIONS]... [COMMANDS]...\n\n"
"Sends rcon commands to minecraft server.\n\n" "Sends rcon commands to Minecraft server.\n\n"
"Option:\n" "Option:\n"
" -h\t\tPrint usage.\n" " -h\t\tPrint usage.\n"
" -s\t\tSilent mode. Do not print data received from rcon.\n" " -H\t\tServer address.\n"
" -P\t\tPort. Default is 25575.\n"
" -p\t\tRcon password.\"\".\n"
" -t\t\tTerminal mode. Acts as interactive terminal.\n" " -t\t\tTerminal mode. Acts as interactive terminal.\n"
" -p\t\tRcon password. Default: \"\".\n" " -s\t\tSilent mode. Do not print data received from rcon.\n"
" -H\t\tHost address or ip.\n"
" -P\t\tPort. Default: 25575.\n"
" -c\t\tDisable colors.\n" " -c\t\tDisable colors.\n"
" -r\t\tOutput raw packets.\n\t\tGood for debugging and custom handling of the output.\n" " -r\t\tOutput raw packets.\n\t\tGood for debugging and custom handling of the output.\n"
,stdout ,stdout
); );
puts("\nCommands must be separated with spaces.\n"); puts("\nCommands must be separated with spaces.\n");
puts("Example:\n "IN_NAME" -c -H 192.168.1.42 -P 25575 -p password cmd1 \"cmd2 arg1 arg2\"\n"); puts("Example:\n "IN_NAME" -c -H my.minecraft.server -P 25575 -p password cmd1 \"cmd2 arg1 arg2\"\n");
puts("minecraft rcon ("IN_NAME") "VERSION".\nReport bugs to tiiffi_at_gmail_dot_com.\n"); puts(VER_STR"\nReport bugs to tiiffi_at_gmail_dot_com or http://github.com/tiiffi/mcrcon/issues/\n");
#ifdef _WIN32 #ifdef _WIN32
puts("Press enter to exit."); puts("Press enter to exit.");
@ -631,7 +639,7 @@ struct rcon_packet packet_build_new(int32_t id, int32_t cmd, char *string)
string_length = MAX_STRING_SIZE; string_length = MAX_STRING_SIZE;
fprintf(stderr, fprintf(stderr,
"Warning: command string is too long. Truncating to " "Warning: command string is too long. Truncating to "
"%u characters.\n", MAX_STRING_SIZE "%u characters.\n", (unsigned) MAX_STRING_SIZE
); );
} }