Partially revert c93c1c7, for mingw builds only

Conditionally restore old functionality which got broken for mingw
builds with commit c93c1c7. Allow building with old version of code, but
generate a warning when doing so.
This commit is contained in:
Michał Janiszewski 2015-09-29 00:01:51 +02:00
parent abb0acde41
commit 9e62198546
3 changed files with 55 additions and 19 deletions

View File

@ -44,6 +44,10 @@ endif (DISABLE_HTTP_TWITCH)
option(DISABLE_NETWORK "Disable multiplayer functionality. Mainly for testing.")
if (DISABLE_NETWORK)
add_definitions(-DDISABLE_NETWORK)
else (DISABLE_NETWORK)
if (WIN32)
SET(NETWORKLIBS ${NETWORKLIBS} ws2_32)
endif (WIN32)
endif (DISABLE_NETWORK)
# include lib
@ -80,4 +84,4 @@ endif (WIN32)
# libopenrct2.dll -> openrct2.dll
set_target_properties(${PROJECT} PROPERTIES PREFIX "")
TARGET_LINK_LIBRARIES(${PROJECT} ${SDL2_LIBRARIES} ${ORCTLIBS_LIB} ${JANSSON_LIBRARIES} ${HTTPLIBS})
TARGET_LINK_LIBRARIES(${PROJECT} ${SDL2_LIBRARIES} ${ORCTLIBS_LIB} ${JANSSON_LIBRARIES} ${HTTPLIBS} ${NETWORKLIBS})

View File

@ -337,16 +337,20 @@ bool Network::BeginClient(const char* host, unsigned short port)
return false;
}
sockaddr_in server_address;
#ifdef USE_INET_PTON
char address[64];
if (!network_get_address(address, sizeof(address), host)) {
log_error("Unable to resolve hostname.");
return false;
}
sockaddr_in server_address;
if (inet_pton(AF_INET, address, &server_address.sin_addr) != 1) {
return false;
}
#else
server_address.sin_addr.S_un.S_addr = inet_addr(network_getAddress((char *)host));
#endif // USE_INET_PTON
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
@ -1093,6 +1097,7 @@ void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32
}
}
#ifdef USE_INET_PTON
static bool network_get_address(char *dst, size_t dstLength, const char *host)
{
struct addrinfo *remoteHost;
@ -1112,6 +1117,21 @@ static bool network_get_address(char *dst, size_t dstLength, const char *host)
// No IPv4 addresses found for host name
return false;
}
#else
static char *network_getAddress(char *host)
{
struct hostent *remoteHost;
struct in_addr addr;
remoteHost = gethostbyname(host);
if (remoteHost != NULL && remoteHost->h_addrtype == AF_INET && remoteHost->h_addr_list[0] != 0) {
addr.s_addr = *(u_long *)remoteHost->h_addr_list[0];
return inet_ntoa(addr);
}
return host;
}
#endif // USE_INET_PTON
#else
int network_get_mode() { return NETWORK_MODE_NONE; }

View File

@ -44,24 +44,32 @@ extern "C" {
#ifndef DISABLE_NETWORK
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define LAST_SOCKET_ERROR() WSAGetLastError()
#undef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#ifndef __MINGW32__
#define USE_INET_PTON
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
typedef int SOCKET;
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define LAST_SOCKET_ERROR() errno
#define closesocket close
#define ioctlsocket ioctl
#warning using deprecated network functions in lieu of inet_pton, inet_ntop
#endif // __MINGW32__
#ifdef _WIN32
#include <winsock2.h>
#ifdef USE_INET_PTON
#include <ws2tcpip.h>
#endif
#define LAST_SOCKET_ERROR() WSAGetLastError()
#undef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
typedef int SOCKET;
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define LAST_SOCKET_ERROR() errno
#define closesocket close
#define ioctlsocket ioctl
#endif // _WIN32
enum {
@ -264,7 +272,11 @@ void network_send_chat(const char* text);
void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback);
void network_print_error();
#ifdef USE_INET_PTON
static bool network_get_address(char *dst, size_t dstLength, const char *host);
#else
static char *network_getAddress(char *host);
#endif // USE_INET_PTON
#ifdef __cplusplus
}