Indentation style change

This commit is contained in:
Tiiffi
2019-12-20 19:56:05 +02:00
parent 4f3a455095
commit 8b75ddf4bf

185
mcrcon.c
View File

@ -169,11 +169,8 @@ int main(int argc, char *argv[])
char *pass = getenv("MCRCON_PASS"); char *pass = getenv("MCRCON_PASS");
char *port = getenv("MCRCON_PORT"); char *port = getenv("MCRCON_PORT");
if (!port) if (!port) port = "25575";
port = "25575"; if (!host) host = "localhost";
if (!host)
host = "localhost";
if(argc < 1 && pass == NULL) usage(); 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) while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:i")) != -1)
{ {
switch (opt) switch (opt) {
{
case 'H': host = optarg; break; case 'H': host = optarg; break;
case 'P': port = optarg; break; case 'P': port = optarg; break;
case 'p': pass = optarg; break; case 'p': pass = optarg; break;
@ -213,7 +209,8 @@ int main(int argc, char *argv[])
return 0; 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 // safety features to prevent "IO: Connection reset" bug on the server side
atexit(&exit_proc); atexit(&exit_proc);
@ -224,7 +221,8 @@ int main(int argc, char *argv[])
#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
@ -233,15 +231,13 @@ int main(int argc, char *argv[])
int exit_code = EXIT_SUCCESS; int exit_code = EXIT_SUCCESS;
// auth & commands // auth & commands
if (rcon_auth(global_rsock, pass)) if (rcon_auth(global_rsock, pass)) {
{
if (terminal_mode) if (terminal_mode)
run_terminal_mode(global_rsock); run_terminal_mode(global_rsock);
else else
exit_code = run_commands(argc, argv); exit_code = run_commands(argc, argv);
} }
else // auth failed else { // auth failed
{
fprintf(stdout, "Authentication failed!\n"); fprintf(stdout, "Authentication failed!\n");
exit_code = EXIT_FAILURE; exit_code = EXIT_FAILURE;
} }
@ -299,8 +295,7 @@ void net_init_WSA(void)
WORD version = MAKEWORD(2, 2); WORD version = MAKEWORD(2, 2);
int err = WSAStartup(version, &wsadata); int err = WSAStartup(version, &wsadata);
if (err != 0) if (err != 0) {
{
fprintf(stderr, "WSAStartup failed. Error: %d.\n", err); fprintf(stderr, "WSAStartup failed. Error: %d.\n", err);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -310,12 +305,12 @@ void net_init_WSA(void)
// socket close and cleanup // socket close and cleanup
void net_close(int sd) void net_close(int sd)
{ {
#ifdef _WIN32 #ifdef _WIN32
closesocket(sd); closesocket(sd);
WSACleanup(); WSACleanup();
#else #else
close(sd); close(sd);
#endif #endif
} }
// Opens and connects socket // Opens and connects socket
@ -338,27 +333,26 @@ int net_connect(const char *host, const char *port)
#endif #endif
int ret = getaddrinfo(host, port, &hints, &server_info); int ret = getaddrinfo(host, port, &hints, &server_info);
if (ret != 0) if (ret != 0) {
{
fprintf(stderr, "Name resolution failed.\n"); fprintf(stderr, "Name resolution failed.\n");
#ifdef _WIN32 #ifdef _WIN32
fprintf(stderr, "Error %d: %s", ret, gai_strerror(ret)); fprintf(stderr, "Error %d: %s", ret, gai_strerror(ret));
#else #else
fprintf(stderr, "Error %d: %s\n", ret, gai_strerror(ret)); fprintf(stderr, "Error %d: %s\n", ret, gai_strerror(ret));
#endif #endif
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Go through the hosts and try to connect // 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); sd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sd == -1) if (sd == -1)
continue; continue;
ret = connect(sd, p->ai_addr, p->ai_addrlen); ret = connect(sd, p->ai_addr, p->ai_addrlen);
if (ret == -1) if (ret == -1) {
{
net_close(sd); net_close(sd);
continue; continue;
} }
@ -366,19 +360,18 @@ int net_connect(const char *host, const char *port)
break; break;
} }
if (p == NULL) if (p == NULL) {
{
/* TODO (Tiiffi): Check why windows does not report errors */ /* TODO (Tiiffi): Check why windows does not report errors */
fprintf(stderr, "Connection failed.\n"); fprintf(stderr, "Connection failed.\n");
#ifndef _WIN32 #ifndef _WIN32
fprintf(stderr, "Error %d: %s\n", errno, strerror(errno)); fprintf(stderr, "Error %d: %s\n", errno, strerror(errno));
#endif #endif
freeaddrinfo(server_info); freeaddrinfo(server_info);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
freeaddrinfo(server_info); freeaddrinfo(server_info);
return sd; return sd;
} }
@ -387,11 +380,11 @@ int net_send(int sd, const uint8_t *buff, size_t size)
size_t sent = 0; size_t sent = 0;
size_t left = size; size_t left = size;
while (sent < size) while (sent < size) {
{
int result = send(sd, (const char *) buff + sent, left, 0); int result = send(sd, (const char *) buff + sent, left, 0);
if (result == -1) return -1; if (result == -1)
return -1;
sent += result; sent += result;
left -= sent; left -= sent;
@ -409,10 +402,9 @@ int net_send_packet(int sd, rc_packet *packet)
bytesleft = len = packet->size + sizeof(int); bytesleft = len = packet->size + sizeof(int);
while(total < len) while (total < len) {
{
ret = send(sd, (char *) packet + total, bytesleft, 0); ret = send(sd, (char *) packet + total, bytesleft, 0);
if(ret == -1) { break; } if(ret == -1) break;
total += ret; total += ret;
bytesleft -= ret; bytesleft -= ret;
} }
@ -429,22 +421,19 @@ rc_packet *net_recv_packet(int sd)
int ret = recv(sd, (char *) &psize, sizeof(int), 0); int ret = recv(sd, (char *) &psize, sizeof(int), 0);
if (ret == 0) if (ret == 0) {
{
fprintf(stderr, "Connection lost.\n"); fprintf(stderr, "Connection lost.\n");
global_connection_alive = 0; global_connection_alive = 0;
return NULL; return NULL;
} }
if (ret != sizeof(int)) if (ret != sizeof(int)) {
{
fprintf(stderr, "Error: recv() failed. Invalid packet size (%d).\n", ret); fprintf(stderr, "Error: recv() failed. Invalid packet size (%d).\n", ret);
global_connection_alive = 0; global_connection_alive = 0;
return NULL; 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); 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; if(psize > DATA_BUFFSIZE || psize < 0) psize = DATA_BUFFSIZE;
@ -456,11 +445,9 @@ rc_packet *net_recv_packet(int sd)
packet.size = psize; packet.size = psize;
int received = 0; int received = 0;
while (received < psize) while (received < psize) {
{
ret = recv(sd, (char *) &packet + sizeof(int) + received, psize - received, 0); 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"); fprintf(stderr, "Connection lost.\n");
global_connection_alive = 0; global_connection_alive = 0;
return NULL; return NULL;
@ -475,11 +462,9 @@ rc_packet *net_recv_packet(int sd)
int net_clean_incoming(int sd, int size) int net_clean_incoming(int sd, int size)
{ {
char tmp[size]; char tmp[size];
int ret = recv(sd, tmp, size, 0); int ret = recv(sd, tmp, size, 0);
if(ret == 0) if(ret == 0) {
{
fprintf(stderr, "Connection lost.\n"); fprintf(stderr, "Connection lost.\n");
global_connection_alive = 0; global_connection_alive = 0;
} }
@ -491,8 +476,7 @@ void print_color(int color)
{ {
// sh compatible color array // sh compatible color array
#ifndef _WIN32 #ifndef _WIN32
char *colors[] = char *colors[] = {
{
"\033[0;30m", /* 00 BLACK 0x30 */ "\033[0;30m", /* 00 BLACK 0x30 */
"\033[0;34m", /* 01 BLUE 0x31 */ "\033[0;34m", /* 01 BLUE 0x31 */
"\033[0;32m", /* 02 GREEN 0x32 */ "\033[0;32m", /* 02 GREEN 0x32 */
@ -512,22 +496,22 @@ void print_color(int color)
"\033[4m" /* 16 UNDERLINE 0x6e */ "\033[4m" /* 16 UNDERLINE 0x6e */
}; };
if(color == 0 || color == 0x72) /* 0x72: 'r' */ /* 0x72: 'r' */
{ if(color == 0 || color == 0x72) fputs("\033[0m", stdout); /* CANCEL COLOR */
fputs("\033[0m", stdout); /* CANCEL COLOR */
}
else else
#endif #endif
{ {
if(color >= 0x61 && color <= 0x66) color -= 0x57; if(color >= 0x61 && color <= 0x66) color -= 0x57;
else if(color >= 0x30 && color <= 0x39) color -= 0x30; else if(color >= 0x30 && color <= 0x39)
else if(color == 0x6e) color=16; /* 0x6e: 'n' */ color -= 0x30;
else if(color == 0x6e)
color=16; /* 0x6e: 'n' */
else return; else return;
#ifndef _WIN32 #ifndef _WIN32
fputs(colors[color], stdout); fputs(colors[color], stdout);
#else #else
SetConsoleTextAttribute(console_handle, color); SetConsoleTextAttribute(console_handle, color);
#endif #endif
} }
} }
@ -535,9 +519,10 @@ void print_color(int color)
// this hacky mess might use some optimizing // this hacky mess might use some optimizing
void packet_print(rc_packet *packet) void packet_print(rc_packet *packet)
{ {
if (global_raw_output == 1) if (global_raw_output == 1) {
{ for (int i = 0; packet->data[i] != 0; ++i)
for (int i = 0; packet->data[i] != 0; ++i) putchar(packet->data[i]); putchar(packet->data[i]);
return; return;
} }
@ -545,21 +530,18 @@ 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 } else def_color = 0x37;
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 (global_disable_colors != 1) if (global_disable_colors != 1) {
{ for (i = 0; (unsigned char) packet->data[i] != 0; ++i) {
for (i = 0; (unsigned char) packet->data[i] != 0; ++i)
{
if (packet->data[i] == 0x0A) print_color(def_color); 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]); print_color(packet->data[i+=2]);
continue; continue;
} }
@ -568,11 +550,9 @@ void packet_print(rc_packet *packet)
print_color(def_color); // cancel coloring print_color(def_color); // cancel coloring
} }
// 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] == 0xc2 && (unsigned char) packet->data[i+1] == 0xa7) {
{
if ((unsigned char) packet->data[i] == 0xc2 && (unsigned char) packet->data[i+1] == 0xa7){
i+=2; i+=2;
continue; continue;
} }
@ -581,8 +561,7 @@ void packet_print(rc_packet *packet)
} }
// 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');
} }
rc_packet *packet_build(int id, int cmd, char *s1) 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 // 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;
} }
@ -628,8 +606,7 @@ int rcon_auth(int sock, char *passwd)
int rcon_command(int sock, char *command) int rcon_command(int sock, char *command)
{ {
rc_packet *packet = packet_build(RCON_PID, RCON_EXEC_COMMAND, command); rc_packet *packet = packet_build(RCON_PID, RCON_EXEC_COMMAND, command);
if (packet == NULL) if (packet == NULL) {
{
global_connection_alive = 0; global_connection_alive = 0;
return 0; return 0;
} }
@ -643,16 +620,9 @@ int rcon_command(int sock, char *command)
if (packet->id != RCON_PID) if (packet->id != RCON_PID)
return 0; return 0;
if (!global_silent_mode) if (!global_silent_mode) {
{ if (packet->size > 10)
/* packet_print(packet);
if(packet->size == 10) {
printf("Unknown command \"%s\". Type \"help\" or \"?\" for help.\n", command);
}
else
*/
if (packet->size > 10)
packet_print(packet);
} }
return 1; return 1;
@ -662,16 +632,14 @@ int run_commands(int argc, char *argv[])
{ {
int i = optind; int i = optind;
for (;;) for (;;) {
{
if (!rcon_command(global_rsock, argv[i])) if (!rcon_command(global_rsock, argv[i]))
return EXIT_FAILURE; return EXIT_FAILURE;
if (++i >= argc) if (++i >= argc)
return EXIT_SUCCESS; return EXIT_SUCCESS;
if (global_wait_seconds > 0) if (global_wait_seconds > 0) {
{
#ifdef _WIN32 #ifdef _WIN32
Sleep(global_wait_seconds * 1000); Sleep(global_wait_seconds * 1000);
#else #else
@ -689,9 +657,9 @@ int run_terminal_mode(int sock)
puts("Logged in. Type \"Q\" to quit!"); puts("Logged in. Type \"Q\" to quit!");
while (global_connection_alive) while (global_connection_alive) {
{
int len = get_line(command, DATA_BUFFSIZE); int len = get_line(command, DATA_BUFFSIZE);
if(command[0] == 'Q' && command[1] == 0) if(command[0] == 'Q' && command[1] == 0)
break; break;
@ -707,22 +675,24 @@ int run_terminal_mode(int sock)
// gets line from stdin and deals with rubbish left in the input buffer // gets line from stdin and deals with rubbish left in the input buffer
int get_line(char *buffer, int bsize) int get_line(char *buffer, int bsize)
{ {
int ch, len;
fputs(">", stdout); fputs(">", stdout);
char *ret = fgets(buffer, bsize, stdin); 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 // remove unwanted characters from the buffer
buffer[strcspn(buffer, "\r\n")] = '\0'; buffer[strcspn(buffer, "\r\n")] = '\0';
len = strlen(buffer); int len = strlen(buffer);
// clean input buffer if needed // clean input buffer if needed
if (len == bsize - 1) if (len == bsize - 1) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF); while ((ch = getchar()) != '\n' && ch != EOF);
}
return len; return len;
} }
@ -731,7 +701,10 @@ int get_line(char *buffer, int bsize)
bool is_bigendian(void) bool is_bigendian(void)
{ {
const int32_t n = 1; const int32_t n = 1;
if (*(uint8_t *) &n == 0 ) return true;
if (*(const uint8_t *) &n == 0 )
return true;
return false; return false;
} }