New indentation and changes in net_connect error reporting.

This commit is contained in:
Tiiffi
2016-10-26 21:08:09 +03:00
parent 9c6c8e3069
commit c5731c1109

226
mcrcon.c
View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2015, Tiiffi <tiiffi -> gmail_dot_com>
* Copyright (c) 2012-2016, Tiiffi <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
@@ -23,8 +23,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#ifdef _WIN32
@@ -35,6 +36,7 @@
#include <winsock2.h>
#include <windows.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -68,19 +70,21 @@ typedef struct _rc_packet {
/* ignoring string2 atm.. */
} rc_packet;
/* =================================== */
/* FUNCTION DEFINITIONS */
/* =================================== */
// =============================================
// FUNCTIONS
// =============================================
// endianness related functions
bool is_bigendian(void);
int32_t reverse_int32(int32_t n);
// Networking
#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 char *buffer, size_t size);
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);
@@ -120,12 +124,14 @@ int rsock; /* rcon socket */
#endif
/* safety stuff (windows is still misbehaving) */
void exit_proc(void) {
void exit_proc(void)
{
if (rsock != -1) net_close(rsock);
}
/* Check windows & linux behaviour !!! */
void sighandler(/*int sig*/) {
void sighandler(/*int sig*/)
{
connection_alive = 0;
#ifndef _WIN32
exit(-1);
@@ -144,6 +150,7 @@ int main(int argc, char *argv[])
if(argc < 2) usage();
opterr = 1; /* default error handler enabled */
while ((opt = getopt(argc, argv, "rtcshH:p:P:i")) != -1)
{
switch (opt)
@@ -176,12 +183,14 @@ int main(int argc, char *argv[])
}
}
if(host == NULL) {
if (host == NULL)
{
fputs("Host not defined. Check -H flag.\n\n", stdout);
usage();
}
if(optind == argc && terminal_mode == 0) {
if(optind == argc && terminal_mode == 0)
{
fputs("No commands specified.\n\n", stdout);
usage();
}
@@ -236,7 +245,8 @@ void usage(void)
" -P\t\tPort. Default: 25575.\n"
" -c\t\tDisable colors.\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("Example:\n "IN_NAME" -c -H 192.168.1.42 -P 25575 -p password cmd1 \"cmd2 arg1 arg2\"\n");
@@ -246,6 +256,7 @@ void usage(void)
puts("Press enter to exit.");
getchar();
#endif
exit(0);
}
@@ -283,21 +294,10 @@ int net_connect(const char *host, const char *port)
#endif
// Get host address info
int result = getaddrinfo(host, port, &hints, &server_info);
if (result != 0)
int ret = getaddrinfo(host, port, &hints, &server_info);
if (ret != 0)
{
if (result == EAI_SERVICE)
{
fprintf(stderr, "Invalid port %s.\n", port);
}
if (result == EAI_NONAME)
{
fprintf(stderr, "Unable to resolve hostname %s.\n", host);
}
else
{
fprintf(stderr, "getaddrinfo() error %d.\n", result);
}
fprintf("getaddrinfo(): %s\n", gai_strerror(ret));
exit(EXIT_FAILURE);
}
@@ -305,32 +305,33 @@ int net_connect(const char *host, const char *port)
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;
if (sd == -1)
continue;
result = connect(sd, p->ai_addr, p->ai_addrlen);
if (result == -1)
ret = connect(sd, p->ai_addr, p->ai_addrlen);
if (ret == -1)
{
net_close(sd);
continue;
}
// Get out of the loop when connect is successful
break;
}
if (p == NULL) {
freeaddrinfo(server_info);
if (p == NULL)
{
fprintf(stderr, "Failed to connect.\n");
exit(EXIT_FAILURE);
}
// Cheating because Windows is retarded (inet_ntop function missing)
fprintf(stdout, "Connected to %s:%s.\n", host, port);
freeaddrinfo(server_info);
fprintf(stdout, "Connected to %s:%s\n", host, port);
return sd;
}
int net_send(int sd, const char *buffer, size_t size)
int net_send(int sd, const uint8_t *buffer, size_t size)
{
size_t sent = 0;
size_t left = size;
@@ -378,34 +379,42 @@ 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");
connection_alive = 0;
return NULL;
}
if(ret != sizeof(int)) {
if (ret != sizeof(int))
{
fprintf(stderr, "Error: recv() failed. Invalid packet size (%d).\n", ret);
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;
net_clean_incoming(sd, psize);
return NULL;
}
packet.size = psize;
ret = recv(sd, (char *) &packet + sizeof(int), psize, 0);
if(ret == 0) {
if (ret == 0)
{
fprintf(stderr, "Connection lost.\n");
connection_alive = 0;
return NULL;
}
if(ret != psize) {
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;
@@ -420,7 +429,8 @@ int net_clean_incoming(int sd, int size)
int ret = recv(sd, tmp, size, 0);
if(ret == 0) {
if(ret == 0)
{
fprintf(stderr, "Connection lost.\n");
connection_alive = 0;
}
@@ -432,7 +442,8 @@ 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 */
@@ -451,7 +462,8 @@ void print_color(int color)
"\033[1;37m", /* 15 WHITE 0x66 */
};
if(color == 0) {
if(color == 0)
{
fputs("\033[0m", stdout); /* CANCEL COLOR */
}
else
@@ -472,7 +484,8 @@ void print_color(int color)
/* this hacky mess might use some optimizing */
void packet_print(rc_packet *packet)
{
if (raw_output == 1) {
if (raw_output == 1)
{
for(int i = 0; packet->data[i] != 0; ++i) putchar(packet->data[i]);
return;
}
@@ -490,14 +503,15 @@ void packet_print(rc_packet *packet)
/* colors enabled so try to handle the bukkit colors for terminal */
if(print_colors == 1) {
for(i = 0; (unsigned char) packet->data[i] != 0; ++i) {
if((unsigned char) packet->data[i] == 0xa7) {
for(i = 0; (unsigned char) packet->data[i] != 0; ++i)
{
if((unsigned char) packet->data[i] == 0xa7)
{
++i;
print_color(packet->data[i]);
continue;
}
if(packet->data[i] == 0x0A) print_color(def_color);
putchar(packet->data[i]);
}
print_color(def_color); /* cancel coloring */
@@ -506,8 +520,10 @@ void packet_print(rc_packet *packet)
/* strip colors */
else
{
for(i = 0; (unsigned char) packet->data[i] != 0; ++i) {
if((unsigned char) packet->data[i] == 0xa7) {
for(i = 0; (unsigned char) packet->data[i] != 0; ++i)
{
if((unsigned char) packet->data[i] == 0xa7)
{
++i;
continue;
}
@@ -526,7 +542,8 @@ 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;
}
@@ -539,6 +556,66 @@ 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 "
"%d characters.\n", 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 rsock, char *passwd)
{
int ret;
@@ -560,21 +637,29 @@ int rcon_command(int rsock, char *command)
{
int ret;
rc_packet *packet = packet_build(RCON_PID, RCON_EXEC_COMMAND, command);
if(packet == NULL) {
size_t size;
uint8_t *p = packet_build_malloc(&size, RCON_PID, RCON_EXEC_COMMAND, command);
if (p == NULL)
{
connection_alive = 0;
return 0;
}
ret = net_send_packet(rsock, packet);
if(!ret) return 0; /* send failed */
net_send(rsock, p, size);
free(p);
//ret = net_send_packet(rsock, packet);
//if(!ret) return 0; /* send failed */
rc_packet *packet;
packet = net_recv_packet(rsock);
if(packet == NULL) return 0;
if(packet->id != RCON_PID) return 0; /* wrong packet id */
if(!silent_mode) {
if(!silent_mode)
{
/*
if(packet->size == 10) {
printf("Unknown command \"%s\". Type \"help\" or \"?\" for help.\n", command);
@@ -593,7 +678,8 @@ int run_commands(int argc, char *argv[])
{
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]);
ret += ok;
}
@@ -609,8 +695,8 @@ int run_terminal_mode(int rsock)
puts("Logged in. Type \"Q\" to quit!");
while(connection_alive) {
while(connection_alive)
{
int len = get_line(command, DATA_BUFFSIZE);
if(command[0] == 'Q' && command[1] == 0) break;
@@ -643,3 +729,25 @@ 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 (*(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;
}