net_init_WSA() function and error reporting improvements.

This commit is contained in:
Tiiffi
2016-10-27 03:25:28 +03:00
parent c9e3f86a89
commit cbbdbcca93

View File

@ -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,36 +79,35 @@ 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);
int net_connect(const char *host, const char *port);
int net_send(int sd, const uint8_t *buffer, size_t size);
void net_close(int sd); int net_send_packet(int sd, rc_packet *packet);
int net_connect(const char *host, const char *port); rc_packet* net_recv_packet(int sd);
int net_send(int sd, const uint8_t *buffer, size_t size);
int net_send_packet(int sd, rc_packet *packet); int net_clean_incoming(int sd, int size);
rc_packet* net_recv_packet(int sd);
int net_clean_incoming(int sd, int size);
// Other stuff // Other stuff
void usage(void); void usage(void);
void error(char *errstring); void error(char *errstring);
#ifndef _WIN32 #ifndef _WIN32
void print_color(int color); void print_color(int color);
#endif #endif
rc_packet* packet_build(int id, int cmd, char *s1); rc_packet* packet_build(int id, int cmd, char *s1);
void packet_print(rc_packet *packet); void packet_print(rc_packet *packet);
int rcon_auth(int rsock, char *passwd); int rcon_auth(int rsock, char *passwd);
int rcon_command(int rsock, char *command); int rcon_command(int rsock, char *command);
int get_line(char *buffer, int len);
int run_terminal_mode(int rsock);
int run_commands(int argc, char *argv[]);
int get_line(char *buffer, int len);
int run_terminal_mode(int rsock);
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);