mirror of
https://github.com/Tiiffi/mcrcon.git
synced 2025-10-28 03:41:07 -04:00
Code style changes.
This commit is contained in:
62
mcrcon.c
62
mcrcon.c
@ -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
|
||||||
@ -179,8 +175,8 @@ int main(int argc, char *argv[])
|
|||||||
/*
|
/*
|
||||||
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,7 +198,7 @@ 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);
|
||||||
@ -214,10 +210,10 @@ int main(int argc, char *argv[])
|
|||||||
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)
|
||||||
@ -268,12 +264,6 @@ void usage(void)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -550,6 +538,7 @@ void packet_print(rc_packet *packet)
|
|||||||
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 */
|
||||||
@ -565,6 +554,7 @@ void packet_print(rc_packet *packet)
|
|||||||
++i;
|
++i;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
putchar(packet->data[i]);
|
putchar(packet->data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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,9 +685,11 @@ 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)
|
||||||
{
|
{
|
||||||
@ -736,9 +731,11 @@ int run_terminal_mode(int rsock)
|
|||||||
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';
|
||||||
|
|||||||
Reference in New Issue
Block a user