mirror of
https://github.com/Tiiffi/mcrcon.git
synced 2025-10-28 03:41:07 -04:00
net_init_WSA() function and error reporting improvements.
This commit is contained in:
48
mcrcon.c
48
mcrcon.c
@ -27,6 +27,8 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* for name resolving on windows */
|
/* for name resolving on windows */
|
||||||
@ -36,7 +38,6 @@
|
|||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#else
|
#else
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
@ -78,10 +79,10 @@ typedef struct _rc_packet {
|
|||||||
bool is_bigendian(void);
|
bool is_bigendian(void);
|
||||||
int32_t reverse_int32(int32_t n);
|
int32_t reverse_int32(int32_t n);
|
||||||
|
|
||||||
|
// Network related functions
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
void net_init_WSA(void);
|
void net_init_WSA(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void net_close(int sd);
|
void net_close(int sd);
|
||||||
int net_connect(const char *host, const char *port);
|
int net_connect(const char *host, const char *port);
|
||||||
int net_send(int sd, const uint8_t *buffer, size_t size);
|
int net_send(int sd, const uint8_t *buffer, size_t size);
|
||||||
@ -108,7 +109,6 @@ int get_line(char *buffer, int len);
|
|||||||
int run_terminal_mode(int rsock);
|
int run_terminal_mode(int rsock);
|
||||||
int run_commands(int argc, char *argv[]);
|
int run_commands(int argc, char *argv[]);
|
||||||
|
|
||||||
|
|
||||||
// =============================================
|
// =============================================
|
||||||
// GLOBAL VARIABLES
|
// GLOBAL VARIABLES
|
||||||
// =============================================
|
// =============================================
|
||||||
@ -266,6 +266,26 @@ void error(char *errstring)
|
|||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
void net_init_WSA(void)
|
||||||
|
{
|
||||||
|
WSADATA wsadata;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = WSAStartup(MAKEWORD(1, 1), &wsadata);
|
||||||
|
if(err != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "WSAStartup failed. Errno: %d.\n", err);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
/*
|
||||||
|
void net_get_last_error(int ret)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
/* socket close and cleanup */
|
/* socket close and cleanup */
|
||||||
void net_close(int sd)
|
void net_close(int sd)
|
||||||
{
|
{
|
||||||
@ -278,6 +298,8 @@ void net_close(int sd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Opens and connects socket */
|
/* Opens and connects socket */
|
||||||
|
/* http://man7.org/linux/man-pages/man3/getaddrinfo.3.html */
|
||||||
|
/* https://bugs.chromium.org/p/chromium/issues/detail?id=44489 */
|
||||||
int net_connect(const char *host, const char *port)
|
int net_connect(const char *host, const char *port)
|
||||||
{
|
{
|
||||||
int sd;
|
int sd;
|
||||||
@ -297,7 +319,12 @@ int net_connect(const char *host, const char *port)
|
|||||||
int ret = getaddrinfo(host, port, &hints, &server_info);
|
int ret = getaddrinfo(host, port, &hints, &server_info);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
fprintf("getaddrinfo(): %s\n", gai_strerror(ret));
|
fprintf(stderr, "Name resolution failed.\n");
|
||||||
|
#ifdef _WIN32
|
||||||
|
fprintf(stderr, "Error %d: %s", ret, gai_strerror(ret));
|
||||||
|
#else
|
||||||
|
fprintf(stderr, "Error %d: %s\n", ret, gai_strerror(ret));
|
||||||
|
#endif
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,14 +345,19 @@ int net_connect(const char *host, const char *port)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(server_info);
|
|
||||||
|
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to connect.\n");
|
/* TODO (Tiiffi): Check why windows does not report errors */
|
||||||
|
fprintf(stderr, "Connection failed.\n");
|
||||||
|
#ifndef _WIN32
|
||||||
|
fprintf(stderr, "Error %d: %s\n", errno, strerror(errno));
|
||||||
|
#endif
|
||||||
|
freeaddrinfo(server_info);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(server_info);
|
||||||
|
|
||||||
fprintf(stdout, "Connected to %s:%s\n", host, port);
|
fprintf(stdout, "Connected to %s:%s\n", host, port);
|
||||||
|
|
||||||
return sd;
|
return sd;
|
||||||
@ -635,7 +667,7 @@ int rcon_auth(int rsock, char *passwd)
|
|||||||
|
|
||||||
int rcon_command(int rsock, char *command)
|
int rcon_command(int rsock, char *command)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret; (void) ret;
|
||||||
|
|
||||||
size_t size;
|
size_t size;
|
||||||
uint8_t *p = packet_build_malloc(&size, RCON_PID, RCON_EXEC_COMMAND, command);
|
uint8_t *p = packet_build_malloc(&size, RCON_PID, RCON_EXEC_COMMAND, command);
|
||||||
|
|||||||
Reference in New Issue
Block a user