2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* 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 *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/socket.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2014-06-05 15:45:37 +02:00
|
|
|
#include <iostream>
|
2013-03-18 17:04:22 +01:00
|
|
|
#include <boost/exception/errinfo_api_function.hpp>
|
|
|
|
#include <boost/exception/errinfo_errno.hpp>
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2014-04-23 13:42:59 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
# include <poll.h>
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the Socket class.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
Socket::Socket(void)
|
2013-04-04 16:08:02 +02:00
|
|
|
: m_FD(INVALID_SOCKET)
|
|
|
|
{ }
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2013-04-04 16:08:02 +02:00
|
|
|
* Constructor for the Socket class.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
Socket::Socket(SOCKET fd)
|
|
|
|
: m_FD(INVALID_SOCKET)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2013-04-04 16:08:02 +02:00
|
|
|
SetFD(fd);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
2013-04-04 16:08:02 +02:00
|
|
|
* Destructor for the Socket class.
|
2012-09-14 14:41:17 +02:00
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
Socket::~Socket(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2013-04-04 16:08:02 +02:00
|
|
|
Close();
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Sets the file descriptor for this socket object.
|
|
|
|
*
|
|
|
|
* @param fd The file descriptor.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Socket::SetFD(SOCKET fd)
|
|
|
|
{
|
2012-05-11 12:44:04 +02:00
|
|
|
if (fd != INVALID_SOCKET) {
|
2013-02-13 12:47:51 +01:00
|
|
|
#ifndef _WIN32
|
2013-10-18 08:26:48 +02:00
|
|
|
/* mark the socket as close-on-exec */
|
2013-02-13 13:03:21 +01:00
|
|
|
Utility::SetCloExec(fd);
|
2013-02-13 12:47:51 +01:00
|
|
|
#endif /* _WIN32 */
|
2012-05-11 12:44:04 +02:00
|
|
|
}
|
2012-03-29 13:15:54 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
ObjectLock olock(this);
|
2012-03-28 13:24:49 +02:00
|
|
|
m_FD = fd;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the file descriptor for this socket object.
|
|
|
|
*
|
|
|
|
* @returns The file descriptor.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
SOCKET Socket::GetFD(void) const
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
return m_FD;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* Closes the socket.
|
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
void Socket::Close(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2013-04-04 16:08:02 +02:00
|
|
|
ObjectLock olock(this);
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (m_FD != INVALID_SOCKET) {
|
|
|
|
closesocket(m_FD);
|
|
|
|
m_FD = INVALID_SOCKET;
|
2013-03-09 15:56:56 +01:00
|
|
|
}
|
2013-03-10 05:10:51 +01:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2012-05-26 20:00:03 +02:00
|
|
|
* Retrieves the last error that occured for the socket.
|
|
|
|
*
|
|
|
|
* @returns An error code.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-05-26 20:00:03 +02:00
|
|
|
int Socket::GetError(void) const
|
2012-04-04 12:22:46 +02:00
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
socklen_t optlen = sizeof(opt);
|
|
|
|
|
|
|
|
int rc = getsockopt(GetFD(), SOL_SOCKET, SO_ERROR, (char *)&opt, &optlen);
|
|
|
|
|
2012-05-26 20:00:03 +02:00
|
|
|
if (rc >= 0)
|
|
|
|
return opt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Formats a sockaddr in a human-readable way.
|
|
|
|
*
|
2012-08-02 09:38:08 +02:00
|
|
|
* @returns A String describing the sockaddr.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String Socket::GetAddressFromSockaddr(sockaddr *address, socklen_t len)
|
2012-04-27 11:44:05 +02:00
|
|
|
{
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
char service[NI_MAXSERV];
|
|
|
|
|
2012-07-17 20:41:06 +02:00
|
|
|
if (getnameinfo(address, len, host, sizeof(host), service,
|
2013-03-11 13:45:08 +01:00
|
|
|
sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
|
2013-03-11 14:03:01 +01:00
|
|
|
#ifndef _WIN32
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "getnameinfo() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getnameinfo")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 14:03:01 +01:00
|
|
|
#else /* _WIN32 */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "getnameinfo() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 14:03:01 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getnameinfo")
|
2013-03-18 22:40:40 +01:00
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-03-11 14:03:01 +01:00
|
|
|
#endif /* _WIN32 */
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2012-04-27 11:44:05 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream s;
|
2012-04-27 11:44:05 +02:00
|
|
|
s << "[" << host << "]:" << service;
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2012-08-02 09:38:08 +02:00
|
|
|
* Returns a String describing the local address of the socket.
|
2012-05-08 15:14:20 +02:00
|
|
|
*
|
2012-08-02 09:38:08 +02:00
|
|
|
* @returns A String describing the local address.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String Socket::GetClientAddress(void)
|
2012-04-27 11:44:05 +02:00
|
|
|
{
|
2012-08-13 13:06:43 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_SocketMutex);
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2012-04-27 11:44:05 +02:00
|
|
|
sockaddr_storage sin;
|
|
|
|
socklen_t len = sizeof(sin);
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) {
|
2013-03-11 14:03:01 +01:00
|
|
|
#ifndef _WIN32
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "getsockname() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getsockname")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 14:03:01 +01:00
|
|
|
#else /* _WIN32 */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "getsockname() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 14:03:01 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getsockname")
|
2013-03-18 22:40:40 +01:00
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-03-11 14:03:01 +01:00
|
|
|
#endif /* _WIN32 */
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2012-04-27 11:44:05 +02:00
|
|
|
|
2014-06-05 16:17:53 +02:00
|
|
|
String address;
|
|
|
|
try {
|
|
|
|
address = GetAddressFromSockaddr((sockaddr *)&sin, len);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {
|
2014-06-05 16:17:53 +02:00
|
|
|
/* already logged */
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
2012-04-27 11:44:05 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2012-08-02 09:38:08 +02:00
|
|
|
* Returns a String describing the peer address of the socket.
|
2012-05-08 15:14:20 +02:00
|
|
|
*
|
2012-08-02 09:38:08 +02:00
|
|
|
* @returns A String describing the peer address.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String Socket::GetPeerAddress(void)
|
2012-04-27 11:44:05 +02:00
|
|
|
{
|
2012-08-13 13:06:43 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_SocketMutex);
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2012-04-27 11:44:05 +02:00
|
|
|
sockaddr_storage sin;
|
|
|
|
socklen_t len = sizeof(sin);
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) {
|
2013-03-11 14:03:01 +01:00
|
|
|
#ifndef _WIN32
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "getpeername() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getpeername")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 14:03:01 +01:00
|
|
|
#else /* _WIN32 */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "getpeername() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 14:03:01 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getpeername")
|
2013-03-18 22:40:40 +01:00
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-03-11 14:03:01 +01:00
|
|
|
#endif /* _WIN32 */
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2012-04-27 11:44:05 +02:00
|
|
|
|
2014-06-05 16:17:53 +02:00
|
|
|
String address;
|
|
|
|
try {
|
|
|
|
address = GetAddressFromSockaddr((sockaddr *)&sin, len);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {
|
2014-06-05 16:17:53 +02:00
|
|
|
/* already logged */
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
2012-04-27 11:44:05 +02:00
|
|
|
}
|
2012-05-26 20:00:03 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
/**
|
|
|
|
* Starts listening for incoming client connections.
|
|
|
|
*/
|
|
|
|
void Socket::Listen(void)
|
|
|
|
{
|
2013-03-11 13:45:08 +01:00
|
|
|
if (listen(GetFD(), SOMAXCONN) < 0) {
|
2013-03-11 14:03:01 +01:00
|
|
|
#ifndef _WIN32
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "listen() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("listen")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 14:03:01 +01:00
|
|
|
#else /* _WIN32 */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "listen() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 14:03:01 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("listen")
|
2013-03-18 22:40:40 +01:00
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-03-11 14:03:01 +01:00
|
|
|
#endif /* _WIN32 */
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-18 09:11:21 +02:00
|
|
|
* Sends data for the socket.
|
2012-11-22 12:04:32 +01:00
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
size_t Socket::Write(const void *buffer, size_t count)
|
2012-11-22 12:04:32 +01:00
|
|
|
{
|
2013-04-19 15:27:35 +02:00
|
|
|
int rc = send(GetFD(), (const char *)buffer, count, 0);
|
2013-03-10 05:10:51 +01:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (rc < 0) {
|
2013-03-11 14:03:01 +01:00
|
|
|
#ifndef _WIN32
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "send() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function("send")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 14:03:01 +01:00
|
|
|
#else /* _WIN32 */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "send() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function("send")
|
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-03-11 14:03:01 +01:00
|
|
|
#endif /* _WIN32 */
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
2013-04-04 16:08:02 +02:00
|
|
|
|
|
|
|
return rc;
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes data that can be written for this socket.
|
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
size_t Socket::Read(void *buffer, size_t count)
|
2012-11-22 12:04:32 +01:00
|
|
|
{
|
2013-04-19 15:27:35 +02:00
|
|
|
int rc = recv(GetFD(), (char *)buffer, count, 0);
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (rc < 0) {
|
2013-03-11 14:03:01 +01:00
|
|
|
#ifndef _WIN32
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "recv() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function("recv")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 14:03:01 +01:00
|
|
|
#else /* _WIN32 */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "recv() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function("recv")
|
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-03-11 14:03:01 +01:00
|
|
|
#endif /* _WIN32 */
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
return rc;
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-04 16:08:02 +02:00
|
|
|
* Accepts a new client and creates a new client object for it.
|
2012-11-22 12:04:32 +01:00
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
Socket::Ptr Socket::Accept(void)
|
2012-11-22 12:04:32 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
sockaddr_storage addr;
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
|
|
|
|
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (fd < 0) {
|
2013-04-04 16:08:02 +02:00
|
|
|
#ifndef _WIN32
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "accept() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("accept")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-04-04 16:08:02 +02:00
|
|
|
#else /* _WIN32 */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "accept() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2013-03-11 14:03:01 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("accept")
|
2013-03-18 22:40:40 +01:00
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-04-04 16:08:02 +02:00
|
|
|
#endif /* _WIN32 */
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
return new Socket(fd);
|
2012-07-17 20:41:06 +02:00
|
|
|
}
|
2014-04-23 13:42:59 +02:00
|
|
|
|
2015-01-29 11:39:06 +01:00
|
|
|
bool Socket::Poll(bool read, bool write, struct timeval *timeout)
|
2014-04-23 13:42:59 +02:00
|
|
|
{
|
2014-07-04 09:31:43 +02:00
|
|
|
int rc;
|
|
|
|
|
2014-04-23 13:42:59 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
fd_set readfds, writefds, exceptfds;
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
if (read)
|
|
|
|
FD_SET(GetFD(), &readfds);
|
|
|
|
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
if (write)
|
|
|
|
FD_SET(GetFD(), &writefds);
|
|
|
|
|
|
|
|
FD_ZERO(&exceptfds);
|
|
|
|
FD_SET(GetFD(), &exceptfds);
|
|
|
|
|
2015-01-29 11:39:06 +01:00
|
|
|
rc = select(GetFD() + 1, &readfds, &writefds, &exceptfds, timeout);
|
2014-07-04 09:31:43 +02:00
|
|
|
|
|
|
|
if (rc < 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "select() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2014-04-23 13:42:59 +02:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function("select")
|
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2014-06-05 15:45:37 +02:00
|
|
|
}
|
2014-04-23 13:42:59 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
pollfd pfd;
|
|
|
|
pfd.fd = GetFD();
|
|
|
|
pfd.events = (read ? POLLIN : 0) | (write ? POLLOUT : 0);
|
|
|
|
pfd.revents = 0;
|
|
|
|
|
2015-01-29 11:39:06 +01:00
|
|
|
rc = poll(&pfd, 1, timeout ? (timeout->tv_sec + 1000 + timeout->tv_usec / 1000) : -1);
|
2014-07-04 09:31:43 +02:00
|
|
|
|
|
|
|
if (rc < 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "Socket")
|
|
|
|
<< "poll() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 15:45:37 +02:00
|
|
|
|
2014-04-23 13:42:59 +02:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function("poll")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2014-06-05 15:45:37 +02:00
|
|
|
}
|
2014-04-23 13:42:59 +02:00
|
|
|
#endif /* _WIN32 */
|
2014-07-04 09:31:43 +02:00
|
|
|
|
|
|
|
return (rc != 0);
|
2014-04-23 13:42:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::MakeNonBlocking(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
Utility::SetNonBlockingSocket(GetFD());
|
|
|
|
#else /* _WIN32 */
|
|
|
|
Utility::SetNonBlocking(GetFD());
|
|
|
|
#endif /* _WIN32 */
|
2014-07-04 09:31:43 +02:00
|
|
|
}
|