Code style changes.

This commit is contained in:
Tiiffi
2016-11-15 20:28:33 +02:00
parent f9595188d5
commit a0e638edc9

118
mcrcon.c
View File

@ -31,7 +31,8 @@
#include <unistd.h> #include <unistd.h>
#ifdef _WIN32 #ifdef _WIN32
/* for name resolving on windows */ // for name resolving on windows
// enable this if you get compiler whine about getaddrinfo on windows
//#define _WIN32_WINNT 0x0501 //#define _WIN32_WINNT 0x0501
#include <ws2tcpip.h> #include <ws2tcpip.h>
@ -45,9 +46,9 @@
#include <netdb.h> #include <netdb.h>
#endif #endif
/* absolute value macro #define VERSION "0.0.6"
#define absolute(x) (x < 0) ? (0 - x) : x #define IN_NAME "mcrcon"
*/ #define VER_STR IN_NAME" "VERSION" (built: "__DATE__" "__TIME__")"
#define RCON_EXEC_COMMAND 2 #define RCON_EXEC_COMMAND 2
#define RCON_AUTHENTICATE 3 #define RCON_AUTHENTICATE 3
@ -58,10 +59,6 @@
/* 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.6"
#define IN_NAME "mcrcon"
#define VER_STR IN_NAME" "VERSION" (built: "__DATE__" "__TIME__")"
// rcon packet structure // rcon packet structure
typedef struct _rc_packet { typedef struct _rc_packet {
int size; int size;
@ -92,7 +89,6 @@ int net_clean_incoming(int sd, int size);
// Misc stuff // Misc stuff
void usage(void); void usage(void);
void error(char *errstring);
#ifndef _WIN32 #ifndef _WIN32
void print_color(int color); void print_color(int color);
#endif #endif
@ -135,7 +131,7 @@ void sighandler(/*int sig*/)
{ {
connection_alive = 0; connection_alive = 0;
#ifndef _WIN32 #ifndef _WIN32
exit(-1); exit(-1);
#endif #endif
} }
@ -176,11 +172,11 @@ int main(int argc, char *argv[])
break; break;
case 'h': case 'h':
case '?': usage(); break; case '?': usage(); break;
/* /*
if(optopt == 'P' || optopt == 'H' || optopt == 'p') if(optopt == 'P' || optopt == 'H' || optopt == 'p')
fprintf (stderr, "Option -%c requires an argument.\n\n", optopt); fprintf (stderr, "Option -%c requires an argument.\n\n", optopt);
else fprintf (stderr, "Unknown option -%c\n\n", optopt);
else fprintf (stderr, "Unknown option -%c\n\n", optopt); */ */
default: exit(-1); default: exit(-1);
} }
@ -202,25 +198,25 @@ int main(int argc, char *argv[])
terminal_mode = 1; terminal_mode = 1;
/* 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);
signal(SIGABRT, &sighandler); signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler); signal(SIGTERM, &sighandler);
signal(SIGINT, &sighandler); signal(SIGINT, &sighandler);
#ifdef _WIN32 #ifdef _WIN32
net_init_WSA(); net_init_WSA();
console_handle = GetStdHandle(STD_OUTPUT_HANDLE); 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 #endif
/* open socket */ // open socket
rsock = net_connect(host, port); rsock = net_connect(host, port);
/* auth & commands */ // auth & commands
if(rcon_auth(rsock, pass)) if (rcon_auth(rsock, pass))
{ {
if(terminal_mode) if (terminal_mode)
ret = run_terminal_mode(rsock); ret = run_terminal_mode(rsock);
else else
ret = run_commands(argc, argv); ret = run_commands(argc, argv);
@ -261,19 +257,13 @@ void usage(void)
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_at_gmail_dot_com or https://github.com/Tiiffi/mcrcon/issues/\n");
#ifdef _WIN32 #ifdef _WIN32
puts("Press enter to exit."); puts("Press enter to exit.");
getchar(); getchar();
#endif #endif
exit(0); exit(0);
} }
void error(char *errstring)
{
fputs(errstring, stderr);
exit(-1);
}
#ifdef _WIN32 #ifdef _WIN32
void net_init_WSA(void) void net_init_WSA(void)
{ {
@ -364,8 +354,6 @@ int net_connect(const char *host, const char *port)
freeaddrinfo(server_info); freeaddrinfo(server_info);
fprintf(stdout, "Connected to %s:%s\n", host, port);
return sd; return sd;
} }
@ -532,24 +520,25 @@ void packet_print(rc_packet *packet)
int def_color = 0; int def_color = 0;
#ifdef _WIN32 #ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO console_info; CONSOLE_SCREEN_BUFFER_INFO console_info;
if(GetConsoleScreenBufferInfo(console_handle, &console_info) != 0) if(GetConsoleScreenBufferInfo(console_handle, &console_info) != 0)
def_color = console_info.wAttributes + 0x30; def_color = console_info.wAttributes + 0x30;
else def_color = 0x37; else def_color = 0x37;
#endif #endif
/* colors enabled so try to handle the bukkit colors for terminal */ /* colors enabled so try to handle the bukkit colors for terminal */
if(print_colors == 1) { if (print_colors == 1) {
for(i = 0; (unsigned char) packet->data[i] != 0; ++i) for (i = 0; (unsigned char) packet->data[i] != 0; ++i)
{ {
if((unsigned char) packet->data[i] == 0xa7) if ((unsigned char) packet->data[i] == 0xa7)
{ {
++i; ++i;
print_color(packet->data[i]); print_color(packet->data[i]);
continue; continue;
} }
if(packet->data[i] == 0x0A) print_color(def_color); if (packet->data[i] == 0x0A) print_color(def_color);
putchar(packet->data[i]); putchar(packet->data[i]);
} }
print_color(def_color); /* cancel coloring */ print_color(def_color); /* cancel coloring */
@ -558,19 +547,20 @@ void packet_print(rc_packet *packet)
/* strip colors */ /* strip colors */
else else
{ {
for(i = 0; (unsigned char) packet->data[i] != 0; ++i) for (i = 0; (unsigned char) packet->data[i] != 0; ++i)
{ {
if((unsigned char) packet->data[i] == 0xa7) if ((unsigned char) packet->data[i] == 0xa7)
{ {
++i; ++i;
continue; continue;
} }
putchar(packet->data[i]); putchar(packet->data[i]);
} }
} }
/* print newline if string has no newline */ /* print newline if string has no newline */
if(packet->data[i-1] != 10 && packet->data[i-1] != 13) if (packet->data[i-1] != 10 && packet->data[i-1] != 13)
putchar('\n'); putchar('\n');
} }
@ -580,7 +570,7 @@ rc_packet *packet_build(int id, int cmd, char *s1)
/* size + id + cmd + s1 + s2 NULL terminator */ /* size + id + cmd + s1 + s2 NULL terminator */
int s1_len = strlen(s1); 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); fprintf(stderr, "Warning: Command string too long (%d). Maximum allowed: %d.\n", s1_len, DATA_BUFFSIZE);
return NULL; return NULL;
@ -659,13 +649,16 @@ int rcon_auth(int rsock, char *passwd)
int ret; int ret;
rc_packet *packet = packet_build(RCON_PID, RCON_AUTHENTICATE, passwd); rc_packet *packet = packet_build(RCON_PID, RCON_AUTHENTICATE, passwd);
if(packet == NULL) return 0; if (packet == NULL)
return 0;
ret = net_send_packet(rsock, packet); ret = net_send_packet(rsock, packet);
if(!ret) return 0; /* send failed */ if (!ret)
return 0; /* send failed */
packet = net_recv_packet(rsock); packet = net_recv_packet(rsock);
if(packet == NULL) return 0; if (packet == NULL)
return 0;
/* return 1 if authentication OK */ /* return 1 if authentication OK */
return packet->id == -1 ? 0 : 1; return packet->id == -1 ? 0 : 1;
@ -692,11 +685,13 @@ int rcon_command(int rsock, char *command)
rc_packet *packet; rc_packet *packet;
packet = net_recv_packet(rsock); packet = net_recv_packet(rsock);
if(packet == NULL) return 0; if (packet == NULL)
return 0;
if(packet->id != RCON_PID) return 0; /* wrong packet id */ if (packet->id != RCON_PID)
return 0; /* wrong packet id */
if(!silent_mode) if (!silent_mode)
{ {
/* /*
if(packet->size == 10) { if(packet->size == 10) {
@ -704,7 +699,7 @@ int rcon_command(int rsock, char *command)
} }
else else
*/ */
if(packet->size > 10) if (packet->size > 10)
packet_print(packet); packet_print(packet);
} }
@ -716,7 +711,7 @@ int run_commands(int argc, char *argv[])
{ {
int i, ok = 1, ret = 0; int i, ok = 1, ret = 0;
for(i = optind; i < argc && ok; i++) for (i = optind; i < argc && ok; i++)
{ {
ok = rcon_command(rsock, argv[i]); ok = rcon_command(rsock, argv[i]);
ret += ok; ret += ok;
@ -733,12 +728,14 @@ int run_terminal_mode(int rsock)
puts("Logged in. Type \"Q\" to quit!"); puts("Logged in. Type \"Q\" to quit!");
while(connection_alive) while (connection_alive)
{ {
int len = get_line(command, DATA_BUFFSIZE); int len = get_line(command, DATA_BUFFSIZE);
if(command[0] == 'Q' && command[1] == 0) break; if(command[0] == 'Q' && command[1] == 0)
break;
if(len > 0 && connection_alive) ret += rcon_command(rsock, command); if(len > 0 && connection_alive)
ret += rcon_command(rsock, command);
command[0] = len = 0; command[0] = len = 0;
} }
@ -751,10 +748,11 @@ int get_line(char *buffer, int bsize)
{ {
int ch, len; int ch, len;
fputs("> ", stdout); fputs("/", stdout);
fgets(buffer, bsize, stdin); (void) fgets(buffer, bsize, stdin);
if(buffer[0] == 0) connection_alive = 0; if (buffer[0] == 0)
connection_alive = 0;
/* remove unwanted characters from the buffer */ /* remove unwanted characters from the buffer */
buffer[strcspn(buffer, "\r\n")] = '\0'; buffer[strcspn(buffer, "\r\n")] = '\0';
@ -762,7 +760,7 @@ int get_line(char *buffer, int bsize)
len = strlen(buffer); len = strlen(buffer);
/* clean input buffer if needed */ /* clean input buffer if needed */
if(len == bsize - 1) if (len == bsize - 1)
while ((ch = getchar()) != '\n' && ch != EOF); while ((ch = getchar()) != '\n' && ch != EOF);
return len; return len;