Removing old native socket stuff which is now unused (replaced by asio calls).
This commit is contained in:
@@ -37,10 +37,10 @@ public:
|
||||
void SetSessionData(boost::shared_ptr<SessionData> sessionData);
|
||||
boost::shared_ptr<boost::asio::ip::tcp::resolver> GetResolver() const;
|
||||
void SetResolver(boost::shared_ptr<boost::asio::ip::tcp::resolver> 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<SessionData> m_sessionData;
|
||||
boost::shared_ptr<boost::asio::ip::tcp::resolver> m_resolver;
|
||||
int m_protocol;
|
||||
bool m_sctp;
|
||||
int m_addrFamily;
|
||||
std::string m_serverAddr;
|
||||
std::string m_serverListUrl;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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 &
|
||||
|
||||
@@ -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 <net/connectdata.h>
|
||||
#include <memory>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#include <net/netpacket.h>
|
||||
#include <net/netexception.h>
|
||||
#include <net/socket_msg.h>
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include <net/socket_helper.h>
|
||||
#include <net/servermanager.h>
|
||||
#include <net/ircthread.h>
|
||||
#include <net/connectdata.h>
|
||||
#include <net/serverlobbythread.h>
|
||||
#include <net/serveraccepthelper.h>
|
||||
#include <net/serverexception.h>
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -17,15 +17,16 @@
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/version.hpp> // solve compatibility issues
|
||||
|
||||
#include <net/socket_startup.h>
|
||||
#include <net/socket_helper.h>
|
||||
#include <core/openssl_wrapper.h>
|
||||
|
||||
#ifdef PKTH_USE_GNUTLS
|
||||
|
||||
#include <gcrypt.h>
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/version.hpp> // 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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 <net/socket_helper.h>
|
||||
|
||||
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
|
||||
@@ -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 <net/socket_helper.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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 <net/socket_startup.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool
|
||||
socket_startup()
|
||||
{
|
||||
return internal_socket_startup();
|
||||
}
|
||||
|
||||
void
|
||||
socket_cleanup()
|
||||
{
|
||||
internal_socket_cleanup();
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@ public:
|
||||
void SetSessionPlayerData(SessionId session, boost::shared_ptr<PlayerData> 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;
|
||||
|
||||
+3
-69
@@ -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 <net/genericsocket.h>
|
||||
|
||||
#ifndef bzero
|
||||
#include <cstring>
|
||||
#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 <stdint.h>
|
||||
@@ -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 <sys/types.h>
|
||||
|
||||
#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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 <net/socket_helper.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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 <net/socket_startup.h>
|
||||
#include <net/socket_helper.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user