1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*******************************************************************************
* shroudBNC - an object-oriented framework for IRC *
* Copyright (C) 2005-2014 Gunnar Beutner *
* *
* 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 LISTENER_H
#define LISTENER_H
/**
* CListenerBase<InheritedClass>
*
* Implements a generic socket listener.
*/
template<typename InheritedClass>
class CListenerBase : public CSocketEvents {
private:
SOCKET m_Listener; /**< the listening socket */
virtual int Read(bool DontProcess) {
sockaddr_storage PeerAddress;
socklen_t PeerSize = sizeof(PeerAddress);
SOCKET Client;
Client = accept(m_Listener, (sockaddr *)&PeerAddress, &PeerSize);
if (Client != INVALID_SOCKET) {
Accept(Client, (sockaddr *)&PeerAddress);
}
return 0;
}
virtual int Write(void) { return 0; }
virtual void Error(int ErrorCode) { }
virtual bool HasQueuedData(void) const { return false; }
virtual bool ShouldDestroy(void) const { return false; }
virtual const char *GetClassName(void) const { return "CListenerBase"; }
protected:
virtual void Accept(SOCKET Client, const sockaddr *PeerAddress) = 0;
public:
/**
* CListenerBase
*
* Constructs a new CListenerBase object.
*
* @param Port the port of the socket listener
* @param BindIp the ip address used for binding the listener
* @param Family the socket family of the listener (AF_INET or AF_INET6)
*/
CListenerBase(unsigned int Port, const char *BindIp = NULL, int Family = AF_INET) {
m_Listener = INVALID_SOCKET;
if (m_Listener == INVALID_SOCKET) {
m_Listener = g_Bouncer->CreateListener(Port, BindIp, Family);
}
if (m_Listener != INVALID_SOCKET) {
g_Bouncer->RegisterSocket(m_Listener, static_cast<CSocketEvents *>(this));
}
}
/**
* ~CListenerBase
*
* Destructs a listener object.
*/
virtual ~CListenerBase(void) {
if (g_Bouncer != NULL && m_Listener != INVALID_SOCKET) {
g_Bouncer->UnregisterSocket(m_Listener);
}
if (m_Listener != INVALID_SOCKET) {
closesocket(m_Listener);
}
}
/**
* Destroy
*
* Destroys a listener object and calls its destructor.
*/
virtual void Destroy(void) {
delete this;
}
/**
* IsValid
*
* Verifies that the underlying socket object is valid.
*/
virtual bool IsValid(void) const {
if (m_Listener != INVALID_SOCKET) {
return true;
} else {
return false;
}
}
/**
* GetSocket
*
* Returns the socket which is used by the listener object.
*/
virtual SOCKET GetSocket(void) const {
return m_Listener;
}
virtual unsigned int GetPort(void) const {
sockaddr_storage Address;
socklen_t Length = sizeof(Address);
if (m_Listener == INVALID_SOCKET || getsockname(m_Listener, (sockaddr *)&Address, &Length) != 0) {
return 0;
} else {
if (Address.ss_family == AF_INET) {
return ntohs(((sockaddr_in *)&Address)->sin_port);
} else if (Address.ss_family == AF_INET6) {
return ntohs(((sockaddr_in6 *)&Address)->sin6_port);
} else {
return 0;
}
}
}
};
/**
* IMPL_SOCKETLISTENER
*
* Can be used to implement a custom listener.
*/
#define IMPL_SOCKETLISTENER(ClassName) class ClassName : public CListenerBase<ClassName>
#ifdef SBNC
/**
* CClientListener
*
* A listener which can be used by bouncer clients to connect to the bouncer.
*/
IMPL_SOCKETLISTENER(CClientListener) {
bool m_SSL; /**< whether this is an SSL listener */
public:
/**
* CClientListener
*
* Constructs a new client listener.
*
* @param Port the port
* @param BindIp the bind address (or NULL)
* @param Family socket family (AF_INET or AF_INET6)
* @param SSL whether the listener should be using ssl
*/
CClientListener(unsigned int Port, const char *BindIp = NULL, int Family = AF_INET,
bool SSL = false) : CListenerBase<CClientListener>(Port, BindIp, Family) {
m_SSL = SSL;
}
/**
* Accept
*
* Accepts a new client.
*
* @param Client the client socket
* @param PeerAddress the remote address of the client
*/
virtual void Accept(SOCKET Client, const sockaddr *PeerAddress) {
CClientConnection *ClientObject;
unsigned long lTrue = 1;
ioctlsocket(Client, FIONBIO, &lTrue);
// destruction is controlled by the main loop
ClientObject = new CClientConnection(Client, m_SSL);
}
/**
* SetSSL
*
* Sets whether this listener should accept SSL clients.
*
* @param SSL boolean flag
*/
void SetSSL(bool SSL) {
m_SSL = SSL;
}
/**
* GetSSL
*
* Checks whether this listener accepts SSL clients.
*/
bool GetSSL(void) const {
return m_SSL;
}
};
#else /* SBNC */
class CClientListener;
#endif /* SBNC */
#endif /* LISTENER_H */
|