From 9e621985464718327eb21637f8041eecacc7bd8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Tue, 29 Sep 2015 00:01:51 +0200 Subject: [PATCH] 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. --- CMakeLists.txt | 6 +++++- src/network/network.cpp | 22 +++++++++++++++++++- src/network/network.h | 46 ++++++++++++++++++++++++++--------------- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 019c29e6a7..d919fe4d5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/src/network/network.cpp b/src/network/network.cpp index 18b4497fd9..f72fb0c4b1 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -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; } diff --git a/src/network/network.h b/src/network/network.h index 04f99d8aba..9f86bb5bf1 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -44,24 +44,32 @@ extern "C" { #ifndef DISABLE_NETWORK -#ifdef _WIN32 -#include -#include -#define LAST_SOCKET_ERROR() WSAGetLastError() -#undef EWOULDBLOCK -#define EWOULDBLOCK WSAEWOULDBLOCK +#ifndef __MINGW32__ +#define USE_INET_PTON #else -#include -#include -#include -#include -#include -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 + #ifdef USE_INET_PTON + #include + #endif + #define LAST_SOCKET_ERROR() WSAGetLastError() + #undef EWOULDBLOCK + #define EWOULDBLOCK WSAEWOULDBLOCK +#else + #include + #include + #include + #include + #include + 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 }