File: Listener.h

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (221 lines) | stat: -rw-r--r-- 6,044 bytes parent folder | download | duplicates (3)
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
217
218
219
220
221
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2007 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. *
 *******************************************************************************/

/**
 * CListenerBase<InheritedClass>
 *
 * Implements a generic socket listener.
 */
template<typename InheritedClass>
class CListenerBase : public CSocketEvents, public CPersistable {
private:
	SOCKET m_Listener; /**< the listening socket */

	virtual int Read(bool DontProcess) {
		char PeerAddress[MAX_SOCKADDR_LEN];
		socklen_t PeerSize = sizeof(PeerAddress);
		SOCKET Client;

		Client = safe_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, safe_box_t Box, const char *BindIp = NULL, int Family = AF_INET) {
		m_Listener = INVALID_SOCKET;

		SetBox(Box);

		if (Box != NULL) {
			m_Listener = safe_get_integer(Box, "Socket");
		}
		
		if (m_Listener == INVALID_SOCKET || m_Listener == 0) {
			m_Listener = g_Bouncer->CreateListener(Port, BindIp, Family);
		}

		if (m_Listener != INVALID_SOCKET) {
			g_Bouncer->RegisterSocket(m_Listener, static_cast<CSocketEvents *>(this));

			if (Box != NULL) {
				safe_put_integer(Box, "Socket", m_Listener);
			}
		}
	}

	/**
	 * ~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) {
			safe_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 short GetPort(void) const {
		sockaddr_in Address;
		socklen_t Length = sizeof(Address);

		if (m_Listener == INVALID_SOCKET || safe_getsockname(m_Listener, (sockaddr *)&Address, &Length) != 0) {
			return 0;
		} else {
			return ntohs(Address.sin_port);
		}
	}
};

/**
 * 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, safe_box_t Box, const char *BindIp = NULL, int Family = AF_INET, bool SSL = false) : CListenerBase<CClientListener>(Port, Box, 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;
		safe_box_t ClientsBox, ClientBox = NULL;
		unsigned long lTrue = 1;

		safe_ioctlsocket(Client, FIONBIO, &lTrue);

		ClientsBox = safe_put_box(NULL, "Clients");

		if (ClientsBox != NULL) {
			ClientBox = safe_put_box(ClientsBox, NULL);
		}

		// destruction is controlled by the main loop
		ClientObject = new CClientConnection(Client, ClientBox, 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
class CClientListener;
#endif