From 31dadd6fa25db736f96d2ca4b7d01ae30e68d93d Mon Sep 17 00:00:00 2001 From: lotodore Date: Tue, 16 Jun 2009 22:00:07 +0000 Subject: [PATCH] Removing old native socket stuff which is now unused (replaced by asio calls). --- pokerth_lib.pro | 7 -- src/net/clientcontext.h | 10 +- src/net/common/clientcontext.cpp | 2 +- src/net/common/clientthread.cpp | 4 +- src/net/common/connectdata.cpp | 36 ------- src/net/common/ircthread.cpp | 5 +- src/net/common/netpacket.cpp | 2 + src/net/common/servermanager.cpp | 1 - src/net/common/sessionmanager.cpp | 70 ------------ src/net/common/socket_startup_cmn.cpp | 32 +++--- src/net/common/transferhelper.cpp | 7 +- src/net/connectdata.h | 46 -------- src/net/linux/socket_helper.cpp | 79 -------------- src/net/linux/socket_startup.cpp | 43 -------- src/net/sessionmanager.h | 1 - src/net/socket_helper.h | 72 +------------ src/net/socket_startup.h | 4 - src/net/win32/socket_helper.cpp | 146 -------------------------- src/net/win32/socket_startup.cpp | 69 ------------ 19 files changed, 38 insertions(+), 598 deletions(-) delete mode 100644 src/net/common/connectdata.cpp delete mode 100644 src/net/connectdata.h delete mode 100644 src/net/linux/socket_helper.cpp delete mode 100644 src/net/linux/socket_startup.cpp delete mode 100644 src/net/win32/socket_helper.cpp delete mode 100644 src/net/win32/socket_startup.cpp diff --git a/pokerth_lib.pro b/pokerth_lib.pro index c6d70fa7..164f6da8 100644 --- a/pokerth_lib.pro +++ b/pokerth_lib.pro @@ -63,7 +63,6 @@ HEADERS += \ src/engine/playerinterface.h \ src/engine/berointerface.h \ src/gui/guiinterface.h \ - src/net/connectdata.h \ src/net/clientcallback.h \ src/net/clientcontext.h \ src/net/clientexception.h \ @@ -162,7 +161,6 @@ SOURCES += \ src/engine/network_engine/clienthand.cpp \ src/engine/network_engine/clientplayer.cpp \ src/engine/network_engine/clientbero.cpp \ - src/net/common/connectdata.cpp \ src/net/common/clientcallback.cpp \ src/net/common/clientcontext.cpp \ src/net/common/clientstate.cpp \ @@ -182,7 +180,6 @@ SOURCES += \ src/net/common/sessiondatacallback.cpp \ src/net/common/sessionmanager.cpp \ src/net/common/socket_startup_cmn.cpp \ - src/net/common/socket_helper_cmn.cpp \ src/net/common/clientexception.cpp \ src/net/common/netcontext.cpp \ src/net/common/netexception.cpp \ @@ -200,8 +197,6 @@ win32{ DEFINES += CURL_STATICLIB DEFINES += _WIN32_WINNT=0x0501 DEPENDPATH += src/net/win32/ src/core/win32 - SOURCES += src/net/win32/socket_helper.cpp \ - src/net/win32/socket_startup.cpp INCLUDEPATH += ../boost/ ../GnuTLS/include ../curl/include ../zlib } !win32{ @@ -209,8 +204,6 @@ win32{ #QMAKE_CXXFLAGS += -ffunction-sections -fdata-sections DEPENDPATH += src/net/linux/ src/core/linux - SOURCES += src/net/linux/socket_helper.cpp \ - src/net/linux/socket_startup.cpp } mac{ diff --git a/src/net/clientcontext.h b/src/net/clientcontext.h index c3cfb841..45098986 100644 --- a/src/net/clientcontext.h +++ b/src/net/clientcontext.h @@ -37,10 +37,10 @@ public: void SetSessionData(boost::shared_ptr sessionData); boost::shared_ptr GetResolver() const; void SetResolver(boost::shared_ptr resolver); - int GetProtocol() const - {return m_protocol;} - void SetProtocol(int protocol) - {m_protocol = protocol;} + bool GetSctp() const + {return m_sctp;} + void SetSctp(bool sctp) + {m_sctp = sctp;} int GetAddrFamily() const {return m_addrFamily;} void SetAddrFamily(int addrFamily) @@ -92,7 +92,7 @@ public: private: boost::shared_ptr m_sessionData; boost::shared_ptr m_resolver; - int m_protocol; + bool m_sctp; int m_addrFamily; std::string m_serverAddr; std::string m_serverListUrl; diff --git a/src/net/common/clientcontext.cpp b/src/net/common/clientcontext.cpp index cc4fc42d..1bc3cf04 100644 --- a/src/net/common/clientcontext.cpp +++ b/src/net/common/clientcontext.cpp @@ -21,7 +21,7 @@ ClientContext::ClientContext() -: m_protocol(0), m_addrFamily(AF_INET), m_useServerList(false), m_serverPort(0), +: m_sctp(false), m_addrFamily(AF_INET), m_useServerList(false), m_serverPort(0), m_hasSubscribedLobbyMsg(true) { } diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index ce3c2902..bab76489 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -96,7 +96,7 @@ ClientThread::Init( ClientContext &context = GetContext(); - context.SetProtocol(sctp ? SOCKET_IPPROTO_SCTP : 0); + context.SetSctp(sctp); context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET); context.SetServerAddr(serverAddress); context.SetServerListUrl(serverListUrl); @@ -839,7 +839,7 @@ ClientThread::CreateContextSession() { } if (!validSocket) - throw ClientException(__FILE__, __LINE__, ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO()); + throw ClientException(__FILE__, __LINE__, ERR_SOCK_CREATION_FAILED, 0); } ClientState & diff --git a/src/net/common/connectdata.cpp b/src/net/common/connectdata.cpp deleted file mode 100644 index f6b6d19b..00000000 --- a/src/net/common/connectdata.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Lothar May * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#include -#include - -using namespace std; - -ConnectData::ConnectData() -: m_sockfd(INVALID_SOCKET), m_peerAddrSize(sizeof(struct sockaddr_storage)) -{ - memset(&m_peerAddr, 0, sizeof(m_peerAddr)); -} - -ConnectData::~ConnectData() -{ - if (m_sockfd != INVALID_SOCKET) - CLOSESOCKET(m_sockfd); -} - diff --git a/src/net/common/ircthread.cpp b/src/net/common/ircthread.cpp index eef09d75..3d225d38 100644 --- a/src/net/common/ircthread.cpp +++ b/src/net/common/ircthread.cpp @@ -29,6 +29,7 @@ using namespace std; #define IRC_WAIT_TERMINATION_MSEC 500 +#define IRC_RECV_TIMEOUT_MSEC 50 #define IRC_MAX_RENAME_TRIES 5 #define IRC_MIN_RECONNECT_INTERVAL_SEC 60 @@ -415,12 +416,12 @@ IrcThread::IrcMain() FD_ZERO(&readSet); FD_ZERO(&writeSet); timeout.tv_sec = 0; - timeout.tv_usec = RECV_TIMEOUT_MSEC * 1000; + timeout.tv_usec = IRC_RECV_TIMEOUT_MSEC * 1000; irc_add_select_descriptors(s, &readSet, &writeSet, &maxfd); int selectResult = select(maxfd + 1, &readSet, &writeSet, 0, &timeout); - if (!IS_VALID_SELECT(selectResult)) + if (selectResult == -1) { GetCallback().SignalIrcError(ERR_IRC_SELECT_FAILED); break; diff --git a/src/net/common/netpacket.cpp b/src/net/common/netpacket.cpp index 6328f641..8a79c133 100644 --- a/src/net/common/netpacket.cpp +++ b/src/net/common/netpacket.cpp @@ -17,6 +17,8 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include + #include #include #include diff --git a/src/net/common/servermanager.cpp b/src/net/common/servermanager.cpp index 42f9b9b1..0f41992d 100644 --- a/src/net/common/servermanager.cpp +++ b/src/net/common/servermanager.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include diff --git a/src/net/common/sessionmanager.cpp b/src/net/common/sessionmanager.cpp index 3e2ada94..081a160c 100644 --- a/src/net/common/sessionmanager.cpp +++ b/src/net/common/sessionmanager.cpp @@ -80,76 +80,6 @@ SessionManager::RemoveSession(SessionId session) m_sessionMap.erase(session); } -SessionWrapper -SessionManager::Select(unsigned timeoutMsec) -{ - SessionWrapper retSession; - SOCKET maxSock = INVALID_SOCKET; - fd_set rdset; - FD_ZERO(&rdset); - - { - boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); - SessionMap::iterator i = m_sessionMap.begin(); - SessionMap::iterator end = m_sessionMap.end(); - - while (i != end) - { - // Collect all sockets. - SOCKET tmpSock = i->second.sessionData->GetSocket(); - FD_SET(tmpSock, &rdset); - if (tmpSock > maxSock || maxSock == INVALID_SOCKET) - maxSock = tmpSock; - - // Check if a packet is available. - if (!i->second.sessionData->GetReceiveBuffer().receivedPackets.empty()) - { - retSession = i->second; - break; - } - ++i; - } - } - - if (!retSession.sessionData.get()) - { - if (maxSock == INVALID_SOCKET) - { - Thread::Msleep(timeoutMsec); // just sleep if there is no session - } - else - { - // wait for data - struct timeval timeout; - timeout.tv_sec = timeoutMsec / 1000; - timeout.tv_usec = (timeoutMsec % 1000) * 1000; - int selectResult = select(maxSock + 1, &rdset, NULL, NULL, &timeout); - if (!IS_VALID_SELECT(selectResult)) - { - throw ServerException(__FILE__, __LINE__, ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO()); - } - if (selectResult > 0) // one (or more) of the sockets is readable - { - // Check which socket is readable, return the first. - boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); - SessionMap::iterator i = m_sessionMap.begin(); - SessionMap::iterator end = m_sessionMap.end(); - - while (i != end) - { - if (FD_ISSET(i->second.sessionData->GetSocket(), &rdset)) - { - retSession = i->second; - break; - } - ++i; - } - } - } - } - return retSession; -} - SessionWrapper SessionManager::GetSessionById(SessionId id) const { diff --git a/src/net/common/socket_startup_cmn.cpp b/src/net/common/socket_startup_cmn.cpp index 3b6129be..78caa17a 100644 --- a/src/net/common/socket_startup_cmn.cpp +++ b/src/net/common/socket_startup_cmn.cpp @@ -17,15 +17,16 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include +#include // solve compatibility issues + #include -#include #include #ifdef PKTH_USE_GNUTLS #include -#include -#include // solve compatibility issues extern "C" { @@ -68,7 +69,7 @@ extern "C" { #endif // PKTH_USE_GNUTLS bool -internal_socket_startup() +socket_startup() { #ifdef PKTH_USE_GNUTLS gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_boost); @@ -78,7 +79,7 @@ internal_socket_startup() } void -internal_socket_cleanup() +socket_cleanup() { } @@ -87,11 +88,12 @@ bool socket_has_sctp() { #ifdef IPPROTO_SCTP - SOCKET test = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); + boost::asio::detail::socket_type test = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); - if (test == INVALID_SOCKET) + if (test == boost::asio::detail::invalid_socket) return false; - CLOSESOCKET(test); + boost::system::error_code ec; + boost::asio::detail::socket_ops::close(test, ec); return true; #else return false; @@ -101,11 +103,12 @@ socket_has_sctp() bool socket_has_ipv6() { - SOCKET test = socket(AF_INET6, SOCK_STREAM, 0); + boost::asio::detail::socket_type test = socket(AF_INET6, SOCK_STREAM, 0); - if (test == INVALID_SOCKET) + if (test == boost::asio::detail::invalid_socket) return false; - CLOSESOCKET(test); + boost::system::error_code ec; + boost::asio::detail::socket_ops::close(test, ec); return true; } @@ -113,14 +116,15 @@ bool socket_has_dual_stack() { bool retVal = false; - SOCKET test = socket(AF_INET6, SOCK_STREAM, 0); + boost::asio::detail::socket_type test = socket(AF_INET6, SOCK_STREAM, 0); - if (test != INVALID_SOCKET) + if (test != boost::asio::detail::invalid_socket) { int ipv6only = 0; if (setsockopt(test, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)) == 0) retVal = true; - CLOSESOCKET(test); + boost::system::error_code ec; + boost::asio::detail::socket_ops::close(test, ec); } return retVal; } diff --git a/src/net/common/transferhelper.cpp b/src/net/common/transferhelper.cpp index 9ff9fc9a..7990a165 100644 --- a/src/net/common/transferhelper.cpp +++ b/src/net/common/transferhelper.cpp @@ -27,6 +27,7 @@ using namespace std; +#define CURL_RECV_TIMEOUT_MSEC 50 TransferHelper::TransferHelper() { @@ -91,15 +92,15 @@ TransferHelper::Process() FD_ZERO(&exceptSet); timeout.tv_sec = 0; - timeout.tv_usec = RECV_TIMEOUT_MSEC * 1000; + timeout.tv_usec = CURL_RECV_TIMEOUT_MSEC * 1000; curl_multi_fdset(m_data->curlMultiHandle, &readSet, &writeSet, &exceptSet, &maxfd); if (maxfd >= 0) { int selectResult = select(maxfd+1, &readSet, &writeSet, &exceptSet, &timeout); - if (!IS_VALID_SELECT(selectResult)) - throw NetException(__FILE__, __LINE__, ERR_SOCK_TRANSFER_SELECT_FAILED, SOCKET_ERRNO()); + if (selectResult == -1) + throw NetException(__FILE__, __LINE__, ERR_SOCK_TRANSFER_SELECT_FAILED, 0); } } else diff --git a/src/net/connectdata.h b/src/net/connectdata.h deleted file mode 100644 index f882e032..00000000 --- a/src/net/connectdata.h +++ /dev/null @@ -1,46 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Lothar May * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -/* Connection data. */ - -#ifndef _CONNECTDATA_H_ -#define _CONNECTDATA_H_ - -#include - -class ConnectData -{ -public: - ConnectData(); - ~ConnectData(); - - SOCKET GetSocket() const - {return m_sockfd;} - void SetSocket(SOCKET sockfd) - {m_sockfd = sockfd;} - - SOCKET GetSocket(); - -private: - - SOCKET m_sockfd; - struct sockaddr_storage m_peerAddr; - int m_peerAddrSize; -}; - -#endif diff --git a/src/net/linux/socket_helper.cpp b/src/net/linux/socket_helper.cpp deleted file mode 100644 index 4d186a38..00000000 --- a/src/net/linux/socket_helper.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Lothar May * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -#ifdef _WIN32 -#error This source code is not for Win32. -#endif - -#include - - -bool -socket_string_to_addr(const char *str, int addrFamily, struct sockaddr *addr, int /*addrLen*/) -{ - if (addrFamily == AF_INET) - return (inet_pton(addrFamily, str, &((struct sockaddr_in *)addr)->sin_addr) == 1); - else if (addrFamily == AF_INET6) - return (inet_pton(addrFamily, str, &((struct sockaddr_in6 *)addr)->sin6_addr) == 1); - else - return false; -} - -bool -socket_addr_to_string(struct sockaddr *addr, int /*addrLen*/, int addrFamily, char *str, int strLen) -{ - if (addrFamily == AF_INET) - return (inet_ntop(addrFamily, &((struct sockaddr_in *)addr)->sin_addr, str, strLen) != NULL); - else if (addrFamily == AF_INET6) - return (inet_ntop(addrFamily, &((struct sockaddr_in6 *)addr)->sin6_addr, str, strLen) != NULL); - else - return false; -} - -bool -socket_resolve(const char *str, const char *port, int addrFamily, int sockType, int protocol, struct sockaddr *addr, int addrLen) -{ - bool retVal = false; - - if (str && *str != 0) - { - struct addrinfo aiHints; - struct addrinfo *aiList = NULL; - - memset(&aiHints, 0, sizeof(aiHints)); - aiHints.ai_family = addrFamily; - aiHints.ai_socktype = sockType; - aiHints.ai_protocol = protocol; - - // Try to resolve the name. - // Will (hopefully) use UTF-8 if called on Linux. - bool success = (getaddrinfo(str, port, &aiHints, &aiList) == 0); - - if (success && aiList) - { - if ((int)aiList->ai_addrlen <= addrLen) - { - memcpy(addr, aiList->ai_addr, aiList->ai_addrlen); - retVal = true; - } - freeaddrinfo(aiList); - } - } - return retVal; -} - diff --git a/src/net/linux/socket_startup.cpp b/src/net/linux/socket_startup.cpp deleted file mode 100644 index e61c388f..00000000 --- a/src/net/linux/socket_startup.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Lothar May * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -#ifdef _WIN32 -#error This source code is not for Win32. -#endif - -#include - -#include -#include -#include -#include - -using namespace std; - -bool -socket_startup() -{ - return internal_socket_startup(); -} - -void -socket_cleanup() -{ - internal_socket_cleanup(); -} - diff --git a/src/net/sessionmanager.h b/src/net/sessionmanager.h index 75007f55..c358849f 100644 --- a/src/net/sessionmanager.h +++ b/src/net/sessionmanager.h @@ -53,7 +53,6 @@ public: void SetSessionPlayerData(SessionId session, boost::shared_ptr playerData); void RemoveSession(SessionId session); - SessionWrapper Select(unsigned timeoutMsec); SessionWrapper GetSessionById(SessionId id) const; SessionWrapper GetSessionByPlayerName(const std::string playerName) const; SessionWrapper GetSessionByUniquePlayerId(unsigned uniqueId) const; diff --git a/src/net/socket_helper.h b/src/net/socket_helper.h index e0f7378d..ba1dfe26 100644 --- a/src/net/socket_helper.h +++ b/src/net/socket_helper.h @@ -16,28 +16,16 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -/* Socket helper defines and functions. */ +/* Socket helper defines. */ #ifndef _SOCKET_HELPER_H_ #define _SOCKET_HELPER_H_ -#include - #ifndef bzero #include #define bzero(_ptr, _n) std::memset(_ptr, 0, _n) #endif #ifdef _WIN32 -#define CLOSESOCKET closesocket -#define IOCTLSOCKET ioctlsocket -#define SOCKET_ERRNO() WSAGetLastError() -#define IS_SOCKET_ERR_WOULDBLOCK(_e) ((_e) == WSAEWOULDBLOCK) -#define SOCKET_ERR_NOTCONN WSAENOTCONN -#define SOCKET_ERR_NOTSOCK WSAENOTSOCK - -#ifndef IPV6_V6ONLY - #define IPV6_V6ONLY 27 -#endif #ifdef __GNUC__ /* mingw provides stdint.h */ #include @@ -53,66 +41,12 @@ typedef unsigned char u_char; #else -#define SOCKET int -#define SOCKET_ERROR -1 -#define INVALID_SOCKET -1 -#define CLOSESOCKET close -#define SOCKET_ERRNO() errno -#define IOCTLSOCKET ioctl -#define SOCKET_ERR_NOTCONN ENOTCONN -#define SOCKET_ERR_NOTSOCK ENOTSOCK -#define IS_SOCKET_ERR_WOULDBLOCK(_e) ((_e) == EINPROGRESS || (_e) == EAGAIN || (_e) == EWOULDBLOCK) +#include -#endif - -#define IS_VALID_SOCKET(_s) ((_s) != INVALID_SOCKET) -#define IS_VALID_CONNECT(_c) ((_c) == 0) -#define IS_VALID_BIND(_b) ((_b) != SOCKET_ERROR) -#define IS_VALID_LISTEN(_l) ((_l) != SOCKET_ERROR) -#define IS_VALID_RECV(_r) ((_r) != SOCKET_ERROR) -#define IS_VALID_SEND(_s) ((_s) != SOCKET_ERROR) -#define IS_VALID_SELECT(_s) ((_s) != SOCKET_ERROR) - -#define RECV_TIMEOUT_MSEC 50 - -#ifdef MSG_NOSIGNAL - #define SOCKET_SEND_FLAGS MSG_NOSIGNAL -#else - #define SOCKET_SEND_FLAGS 0 -#endif - -#ifdef IPPROTO_SCTP - #define SOCKET_IPPROTO_SCTP IPPROTO_SCTP -#else - #define SOCKET_IPPROTO_SCTP 0 -#endif +#endif // _WIN32 #define MAX_ADDR_STRING_LEN 256 -// All char *s are assumed to be UTF-8. - -/** - * Convert an address string to a numeric address. - * str is assumed to be UTF-8 encoded. - */ -bool socket_string_to_addr(const char *str, int addrFamily, struct sockaddr *addr, int addrLen); - -/** - * Convert a numeric address to an address string. - */ -bool socket_addr_to_string(struct sockaddr *addr, int addrLen, int addrFamily, char *str, int strLen); - -/** - * Resolve a name to a numeric address. - * str is assumed to be UTF-8 encoded. - */ -bool socket_resolve(const char *str, const char *port, int addrFamily, int sockType, int protocol, struct sockaddr *addr, int addrLen); - -/** - * Set the port in the sockaddr structure. - */ -bool socket_set_port(unsigned port, int addrFamily, struct sockaddr *addr, int addrLen); - #endif diff --git a/src/net/socket_startup.h b/src/net/socket_startup.h index 0fe90bf0..b4334517 100644 --- a/src/net/socket_startup.h +++ b/src/net/socket_startup.h @@ -27,10 +27,6 @@ bool socket_has_sctp(); bool socket_has_ipv6(); bool socket_has_dual_stack(); -/* Internal common functions. */ -bool internal_socket_startup(); -void internal_socket_cleanup(); - #endif diff --git a/src/net/win32/socket_helper.cpp b/src/net/win32/socket_helper.cpp deleted file mode 100644 index 8a8c3bce..00000000 --- a/src/net/win32/socket_helper.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Lothar May * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -#ifndef _WIN32 -#error This source code is Win32 only. -#endif - -#include - -#include -#include -#include - -using namespace std; - -typedef int (WSAAPI * getaddrinfo_ptr_t) (const char *, const char* , const struct addrinfo *, struct addrinfo **); -typedef void (WSAAPI * freeaddrinfo_ptr_t) (struct addrinfo*); - -static wstring -utf8ToWchar(const char *str) -{ - // convert str from UTF-8 to UTF-16 (Win32 byte order) - wstring retStr; - if (str) - { - size_t len = strlen(str) + 1; - if (len > 1) - { - size_t reqLen = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, len, NULL, 0); - - if (reqLen) - { - wchar_t *wstr = new wchar_t[reqLen]; - wstr[0] = L'\0'; - if (::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, len, wstr, reqLen) == (int)reqLen) - retStr = wstr; - delete[] wstr; - } - } - } - return retStr; -} - -bool -socket_string_to_addr(const char *str, int addrFamily, struct sockaddr * addr, int addrLen) -{ - bool retVal = false; -#ifdef UNICODE - // convert str from UTF-8 to UTF-16 (Win32 byte order) - wstring wstr(utf8ToWchar(str)); - if (!wstr.empty()) - retVal = (WSAStringToAddress((LPWSTR)wstr.c_str(), addrFamily, NULL, addr, &addrLen) != SOCKET_ERROR); -#else - if (str && *str != 0) - retVal = (WSAStringToAddress(str, addrFamily, NULL, addr, &addrLen) != SOCKET_ERROR); -#endif - - return retVal; -} - -bool -socket_addr_to_string(struct sockaddr *addr, int addrLen, int /*addrFamily*/, char *str, int strLen) -{ - bool retVal = false; - retVal = (WSAAddressToStringA(addr, addrLen, NULL, str, (DWORD *)&strLen) != SOCKET_ERROR); - - return retVal; -} - -bool -socket_resolve(const char *str, const char *port, int addrFamily, int sockType, int protocol, struct sockaddr *addr, int addrLen) -{ - bool retVal = false; - bool useGetaddrinfo = false; - - if (str && *str != 0) - { - HMODULE hWsock = ::LoadLibraryA("ws2_32"); - - if (hWsock) - { - // Determine functions at runtime, because windows systems < XP do not - // support getaddrinfo. - getaddrinfo_ptr_t getaddrinfo_ptr = (getaddrinfo_ptr_t)::GetProcAddress(hWsock, "getaddrinfo"); - freeaddrinfo_ptr_t freeaddrinfo_ptr = (freeaddrinfo_ptr_t)::GetProcAddress(hWsock, "freeaddrinfo"); - - if (getaddrinfo_ptr && freeaddrinfo_ptr) - { - useGetaddrinfo = true; - - struct addrinfo aiHints; - struct addrinfo *aiList = NULL; - - memset(&aiHints, 0, sizeof(aiHints)); - aiHints.ai_family = addrFamily; - aiHints.ai_socktype = sockType; - aiHints.ai_protocol = protocol; - - // Try to resolve the name. - bool success = (getaddrinfo_ptr(str, port, &aiHints, &aiList) == 0); - - if (success && aiList) - { - if ((int)aiList->ai_addrlen <= addrLen) - { - memcpy(addr, aiList->ai_addr, aiList->ai_addrlen); - retVal = true; - } - freeaddrinfo_ptr(aiList); - } - } - ::FreeLibrary(hWsock); - } - // If we cannot use getaddrinfo (OS older than XP), - // we call the "classic" gethostbyname. - if (!useGetaddrinfo && addrFamily == AF_INET) - { - struct hostent *host = gethostbyname(str); - if (host && host->h_addr_list) - { - struct sockaddr_in *addr_in = (struct sockaddr_in *)addr; - addr_in->sin_family = AF_INET; - addr_in->sin_port = htons(atoi(port)); - memcpy(&addr_in->sin_addr, host->h_addr_list[0], host->h_length); - retVal = true; - } - } - } - return retVal; -} - diff --git a/src/net/win32/socket_startup.cpp b/src/net/win32/socket_startup.cpp deleted file mode 100644 index 0c5f75e7..00000000 --- a/src/net/win32/socket_startup.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by Lothar May * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ -#ifndef _WIN32 -#error This source code is Win32 only. -#endif - -#include -#include - -#define USE_SOCKET_VERSION_MAJOR 2 -#define USE_SOCKET_VERSION_MINOR 2 - -bool -socket_startup() -{ - WORD wVersionRequested; - WSADATA wsaData; - int err; - - wVersionRequested = MAKEWORD(USE_SOCKET_VERSION_MAJOR, USE_SOCKET_VERSION_MINOR); - err = WSAStartup(wVersionRequested, &wsaData); - if (err != 0) - { - /* Tell the user that we could not find a usable */ - /* WinSock DLL. */ - return false; - } - - /* Confirm that the WinSock DLL supports 2.2.*/ - /* Note that if the DLL supports versions greater */ - /* than 2.2 in addition to 2.2, it will still return */ - /* 2.2 in wVersion since that is the version we */ - /* requested. */ - - if (LOBYTE(wsaData.wVersion) != USE_SOCKET_VERSION_MAJOR || - HIBYTE(wsaData.wVersion) != USE_SOCKET_VERSION_MINOR) { - /* Tell the user that we could not find a usable */ - /* WinSock DLL. */ - WSACleanup(); - return false; - } - - /* The WinSock DLL is acceptable. Proceed. */ - return internal_socket_startup(); -} - -void -socket_cleanup() -{ - internal_socket_cleanup(); - WSACleanup(); -} -