icinga2/base/socket.cpp

279 lines
6.0 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
2012-03-28 13:24:49 +02:00
#include "i2-base.h"
using namespace icinga;
/**
* Socket::Sockets
*
* A collection of weak pointers to Socket objects which have been
* registered with the socket sub-system.
*/
2012-04-19 08:46:41 +02:00
Socket::CollectionType Socket::Sockets;
2012-03-28 13:24:49 +02:00
/**
* Socket
*
* Constructor for the Socket class.
*/
2012-03-28 13:24:49 +02:00
Socket::Socket(void)
{
m_FD = INVALID_SOCKET;
}
/**
* ~Socket
*
* Destructor for the Socket class.
*/
2012-03-28 13:24:49 +02:00
Socket::~Socket(void)
{
2012-04-24 14:02:15 +02:00
CloseInternal(true);
2012-03-28 13:24:49 +02:00
}
/**
* Start
*
* Registers the socket and starts handling events for it.
*/
2012-03-28 13:24:49 +02:00
void Socket::Start(void)
{
2012-04-18 15:22:25 +02:00
assert(m_FD != INVALID_SOCKET);
OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this());
Sockets.push_back(static_pointer_cast<Socket>(shared_from_this()));
2012-03-28 13:24:49 +02:00
}
/**
* Stop
*
* Unregisters the sockets and stops handling events for it.
*/
2012-03-28 13:24:49 +02:00
void Socket::Stop(void)
{
Sockets.remove_if(weak_ptr_eq_raw<Socket>(this));
2012-03-28 13:24:49 +02:00
}
/**
* SetFD
*
* 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)
{
unsigned long lTrue = 1;
2012-05-11 12:44:04 +02:00
if (fd != INVALID_SOCKET) {
#ifdef F_GETFL
int flags;
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0)
throw PosixException("fcntl failed", errno);
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
throw PosixException("fcntl failed", errno);
#else /* F_GETFL */
ioctlsocket(fd, FIONBIO, &lTrue);
2012-05-11 12:44:04 +02:00
#endif /* F_GETFL */
}
2012-03-28 13:24:49 +02:00
m_FD = fd;
}
/**
* GetFD
*
* 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
{
return m_FD;
}
/**
* Close
*
* Closes the socket.
*/
2012-03-28 13:24:49 +02:00
void Socket::Close(void)
{
2012-04-24 14:02:15 +02:00
CloseInternal(false);
2012-03-28 13:24:49 +02:00
}
/**
* CloseInternal
*
* Closes the socket.
*
* @param from_dtor Whether this method was called from the destructor.
*/
2012-04-24 14:02:15 +02:00
void Socket::CloseInternal(bool from_dtor)
2012-03-28 13:24:49 +02:00
{
if (m_FD == INVALID_SOCKET)
return;
closesocket(m_FD);
m_FD = INVALID_SOCKET;
2012-03-28 13:24:49 +02:00
/* nobody can possibly have a valid event subscription when the
destructor has been called */
if (!from_dtor) {
2012-03-28 13:24:49 +02:00
Stop();
EventArgs ea;
ea.Source = shared_from_this();
OnClosed(ea);
}
2012-03-28 13:24:49 +02:00
}
/**
* HandleSocketError
*
* Handles a socket error by calling the OnError event.
*/
2012-04-22 16:45:31 +02:00
void Socket::HandleSocketError(void)
{
int opt;
socklen_t optlen = sizeof(opt);
int rc = getsockopt(GetFD(), SOL_SOCKET, SO_ERROR, (char *)&opt, &optlen);
2012-04-22 16:45:31 +02:00
if (rc >= 0 && opt != 0) {
2012-04-18 15:22:25 +02:00
SocketErrorEventArgs sea;
sea.Code = opt;
2012-04-22 16:45:31 +02:00
#ifdef _WIN32
sea.Message = Win32Exception::FormatErrorCode(sea.Code);
#else /* _WIN32 */
sea.Message = PosixException::FormatErrorCode(sea.Code);
#endif /* _WIN32 */
2012-04-18 15:22:25 +02:00
OnError(sea);
}
2012-04-22 16:45:31 +02:00
Close();
return;
}
/**
* ExceptionEventHandler
*
* Processes errors that have occured for the socket.
*
2012-05-10 13:46:04 +02:00
* @param - Event arguments for the socket error.
* @returns 0
*/
2012-05-10 13:46:04 +02:00
int Socket::ExceptionEventHandler(const EventArgs&)
2012-03-28 13:24:49 +02:00
{
2012-04-22 16:45:31 +02:00
HandleSocketError();
2012-03-28 13:24:49 +02:00
2012-04-22 16:45:31 +02:00
return 0;
2012-03-28 13:24:49 +02:00
}
/**
* WantsToRead
*
* Checks whether data should be read for this socket object.
*
* @returns true if the socket should be registered for reading, false otherwise.
*/
2012-03-28 13:24:49 +02:00
bool Socket::WantsToRead(void) const
{
return false;
}
/**
* WantsToWrite
*
* Checks whether data should be written for this socket object.
*
* @returns true if the socket should be registered for writing, false otherwise.
*/
2012-03-28 13:24:49 +02:00
bool Socket::WantsToWrite(void) const
{
return false;
}
/**
* GetAddressFromSockaddr
*
* Formats a sockaddr in a human-readable way.
*
* @returns A string describing the sockaddr.
*/
string Socket::GetAddressFromSockaddr(sockaddr *address, socklen_t len)
{
char host[NI_MAXHOST];
char service[NI_MAXSERV];
if (getnameinfo(address, len, host, sizeof(host), service, sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0)
throw InvalidArgumentException(); /* TODO: throw proper exception */
stringstream s;
s << "[" << host << "]:" << service;
return s.str();
}
/**
* GetClientAddress
*
* Returns a string describing the local address of the socket.
*
* @returns A string describing the local address.
*/
string Socket::GetClientAddress(void)
{
sockaddr_storage sin;
socklen_t len = sizeof(sin);
if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) {
HandleSocketError();
return string();
}
return GetAddressFromSockaddr((sockaddr *)&sin, len);
}
/**
* GetPeerAddress
*
* Returns a string describing the peer address of the socket.
*
* @returns A string describing the peer address.
*/
string Socket::GetPeerAddress(void)
{
sockaddr_storage sin;
socklen_t len = sizeof(sin);
if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) {
HandleSocketError();
return string();
}
return GetAddressFromSockaddr((sockaddr *)&sin, len);
}