summaryrefslogtreecommitdiff
path: root/src/syncListenSocket.h
blob: 9740ecf07dc34e28aa76269e4919a320dbeb967d (plain) (blame)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
Copyright (C) 2004-2007  Anders Hedstrom

This library is made available under the terms of the GNU GPL.

If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about 
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.

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 _SOCKETS_SyncListenSocket_H
#define _SOCKETS_SyncListenSocket_H
#include "Sockets/sockets-config.h"

#ifdef _WIN32
#include <stdlib.h>
#else
#include <errno.h>
#endif

#include "Sockets/ISocketHandler.h"
#include "Sockets/Socket.h"
#include "Sockets/Utility.h"
#include "Sockets/SctpSocket.h"
#include "Sockets/Ipv4Address.h"
#include "Sockets/Ipv6Address.h"
#ifdef ENABLE_EXCEPTIONS
#include "Sockets/Exception.h"
#endif

#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif


/** Binds incoming port number to new Socket class X. 
	\ingroup basic */
template <class X,class Y>
class SyncListenSocket : public Socket
{
public:
	/** Constructor.
		\param h ISocketHandler reference
		\param use_creator Optional use of creator (default true) */
	SyncListenSocket(ISocketHandler& h, Y & y,bool use_creator = true) : Socket(h), m_depth(0), m_creator(NULL),
                                                                       m_bHasCreate(false),y_(y)
	{
		if (use_creator)
		{
			m_creator = new X(h,y);
			Socket *tmp = m_creator -> Create();
			if (tmp && dynamic_cast<X *>(tmp))
			{
				m_bHasCreate = true;
			}
			if (tmp)
			{
				delete tmp;
			}
		}
	}
	~SyncListenSocket() {
		if (m_creator)
		{
			delete m_creator;
		}
	}

	/** Close file descriptor. */
	int Close() {
		if (GetSocket() != INVALID_SOCKET)
		{
			closesocket(GetSocket());
		}
		return 0;
	}

	/** Bind and listen to any interface.
		\param port Port (0 is random)
		\param depth Listen queue depth */
	int Bind(port_t port,int depth = 20) {
#ifdef ENABLE_IPV6
#ifdef IPPROTO_IPV6
		if (IsIpv6())
		{
			Ipv6Address ad(port);
			return Bind(ad, depth);
		}
		else
#endif
#endif
		{
			Ipv4Address ad(port);
			return Bind(ad, depth);
		}
	}

	int Bind(SocketAddress& ad,int depth) {
#ifdef USE_SCTP
		if (dynamic_cast<SctpSocket *>(m_creator))
		{
			return Bind(ad, "sctp", depth);
		}
#endif
		return Bind(ad, "tcp", depth);
	}

	/** Bind and listen to any interface, with optional protocol.
		\param port Port (0 is random)
		\param protocol Network protocol
		\param depth Listen queue depth */
	int Bind(port_t port,const std::string& protocol,int depth = 20) {
#ifdef ENABLE_IPV6
#ifdef IPPROTO_IPV6
		if (IsIpv6())
		{
			Ipv6Address ad(port);
			return Bind(ad, protocol, depth);
		}
		else
#endif
#endif
		{
			Ipv4Address ad(port);
			return Bind(ad, protocol, depth);
		}
	}

	/** Bind and listen to specific interface.
		\param intf Interface hostname
		\param port Port (0 is random)
		\param depth Listen queue depth */
	int Bind(const std::string& intf,port_t port,int depth = 20) {
#ifdef ENABLE_IPV6
#ifdef IPPROTO_IPV6
		if (IsIpv6())
		{
			Ipv6Address ad(intf, port);
			if (ad.IsValid())
			{
				return Bind(ad, depth);
			}
			Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
			return -1;
		}
		else
#endif
#endif
		{
			Ipv4Address ad(intf, port);
			if (ad.IsValid())
			{
				return Bind(ad, depth);
			}
			Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
			return -1;
		}
	}

	/** Bind and listen to specific interface.
		\param intf Interface hostname
		\param port Port (0 is random)
		\param protocol Network protocol
		\param depth Listen queue depth */
	int Bind(const std::string& intf,port_t port,const std::string& protocol,int depth = 20) {
#ifdef ENABLE_IPV6
#ifdef IPPROTO_IPV6
		if (IsIpv6())
		{
			Ipv6Address ad(intf, port);
			if (ad.IsValid())
			{
				return Bind(ad, protocol, depth);
			}
			Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
			return -1;
		}
		else
#endif
#endif
		{
			Ipv4Address ad(intf, port);
			if (ad.IsValid())
			{
				return Bind(ad, protocol, depth);
			}
			Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
			return -1;
		}
	}

	/** Bind and listen to ipv4 interface.
		\param a Ipv4 interface address
		\param port Port (0 is random)
		\param depth Listen queue depth */
	int Bind(ipaddr_t a,port_t port,int depth = 20) {
		Ipv4Address ad(a, port);
#ifdef USE_SCTP
		if (dynamic_cast<SctpSocket *>(m_creator))
		{
			return Bind(ad, "sctp", depth);
		}
#endif
		return Bind(ad, "tcp", depth);
	}
	/** Bind and listen to ipv4 interface.
		\param a Ipv4 interface address
		\param port Port (0 is random)
		\param protocol Network protocol
		\param depth Listen queue depth */
	int Bind(ipaddr_t a,port_t port,const std::string& protocol,int depth) {
		Ipv4Address ad(a, port);
		return Bind(ad, protocol, depth);
	}

#ifdef ENABLE_IPV6
#ifdef IPPROTO_IPV6
	/** Bind and listen to ipv6 interface.
		\param a Ipv6 interface address
		\param port Port (0 is random)
		\param depth Listen queue depth */
	int Bind(in6_addr a,port_t port,int depth = 20) {
		Ipv6Address ad(a, port);
#ifdef USE_SCTP
		if (dynamic_cast<SctpSocket *>(m_creator))
		{
			return Bind(ad, "sctp", depth);
		}
#endif
		return Bind(ad, "tcp", depth);
	}
	/** Bind and listen to ipv6 interface.
		\param a Ipv6 interface address
		\param port Port (0 is random)
		\param protocol Network protocol
		\param depth Listen queue depth */
	int Bind(in6_addr a,port_t port,const std::string& protocol,int depth) {
		Ipv6Address ad(a, port);
		return Bind(ad, protocol, depth);
	}
#endif
#endif

	/** Bind and listen to network interface.
		\param ad Interface address
		\param protocol Network protocol
		\param depth Listen queue depth */
	int Bind(SocketAddress& ad,const std::string& protocol,int depth) {
		SOCKET s;
		if ( (s = CreateSocket(ad.GetFamily(), SOCK_STREAM, protocol)) == INVALID_SOCKET)
		{
			return -1;
		}
		if (bind(s, ad, ad) == -1)
		{
			Handler().LogError(this, "bind", Errno, StrError(Errno), LOG_LEVEL_FATAL);
			closesocket(s);
#ifdef ENABLE_EXCEPTIONS
			throw Exception("bind() failed for port " + Utility::l2string(ad.GetPort()) + ": " + StrError(Errno));
#endif
			return -1;
		}
		if (listen(s, depth) == -1)
		{
			Handler().LogError(this, "listen", Errno, StrError(Errno), LOG_LEVEL_FATAL);
			closesocket(s);
#ifdef ENABLE_EXCEPTIONS
			throw Exception("listen() failed for port " + Utility::l2string(ad.GetPort()) + ": " + StrError(Errno));
#endif
			return -1;
		}
		m_depth = depth;
		Attach(s);
		return 0;
	}

	/** Return assigned port number. */
	port_t GetPort()
	{
		return GetSockPort();
	}

	/** Return listen queue depth. */
	int GetDepth()
	{
		return m_depth;
	}

	/** OnRead on a ListenSocket receives an incoming connection. */
	void OnRead()
	{
		struct sockaddr sa;
		socklen_t sa_len = sizeof(struct sockaddr);
		SOCKET a_s = accept(GetSocket(), &sa, &sa_len);

		if (a_s == INVALID_SOCKET)
		{
			Handler().LogError(this, "accept", Errno, StrError(Errno), LOG_LEVEL_ERROR);
			return;
		}
		if (!Handler().OkToAccept(this))
		{
			Handler().LogError(this, "accept", -1, "Not OK to accept", LOG_LEVEL_WARNING);
			closesocket(a_s);
			return;
		}
		if (Handler().GetCount() >= FD_SETSIZE)
		{
			Handler().LogError(this, "accept", (int)Handler().GetCount(), "ISocketHandler fd_set limit reached", LOG_LEVEL_FATAL);
			closesocket(a_s);
			return;
		}
		Socket *tmp = m_bHasCreate ? m_creator -> Create() : new X(Handler(),y_);
#ifdef ENABLE_IPV6
		tmp -> SetIpv6( IsIpv6() );
#endif
		tmp -> SetParent(this);
		tmp -> Attach(a_s);
		tmp -> SetNonblocking(true);
		{
#ifdef ENABLE_IPV6
#ifdef IPPROTO_IPV6
			if (sa_len == sizeof(struct sockaddr_in6))
			{
				struct sockaddr_in6 *p = (struct sockaddr_in6 *)&sa;
				if (p -> sin6_family == AF_INET6)
				{
					Ipv6Address ad(p -> sin6_addr,ntohs(p -> sin6_port));
					ad.SetFlowinfo(p -> sin6_flowinfo);
#ifndef _WIN32
					ad.SetScopeId(p -> sin6_scope_id);
#endif
					tmp -> SetRemoteAddress(ad);
				}
			}
#endif
#endif
			if (sa_len == sizeof(struct sockaddr_in))
			{
				struct sockaddr_in *p = (struct sockaddr_in *)&sa;
				if (p -> sin_family == AF_INET)
				{
					Ipv4Address ad(p -> sin_addr,ntohs(p -> sin_port));
					tmp -> SetRemoteAddress(ad);
				}
			}
		}
		tmp -> SetConnected(true);
		tmp -> Init();
		tmp -> SetDeleteByHandler(true);
		Handler().Add(tmp);
#ifdef HAVE_OPENSSL
		if (tmp -> IsSSL()) // SSL Enabled socket
		{
			// %! OnSSLAccept calls SSLNegotiate that can finish in this one call.
			// %! If that happens and negotiation fails, the 'tmp' instance is
			// %! still added to the list of active sockets in the sockethandler.
			// %! See bugfix for this in SocketHandler::Select - don't Set rwx
			// %! flags if CloseAndDelete() flag is true.
			// %! An even better fugbix (see TcpSocket::OnSSLAccept) now avoids
			// %! the Add problem altogether, so ignore the above.
			// %! (OnSSLAccept does no longer call SSLNegotiate().)
			tmp -> OnSSLAccept();
		}
		else
#endif
		{
			tmp -> OnAccept();
		}
	}

	/** Please don't use this method.
		"accept()" is handled automatically in the OnRead() method. */
        virtual SOCKET Accept(SOCKET socket, struct sockaddr *saptr, socklen_t *lenptr)
        {
                return accept(socket, saptr, lenptr);
        }

        bool HasCreator() { return m_bHasCreate; }

	void OnOptions(int,int,int,SOCKET) {
		SetSoReuseaddr(true);
	}

protected:
	SyncListenSocket(const SyncListenSocket& s) : Socket(s) {}
private:
	SyncListenSocket& operator=(const SyncListenSocket& ) { return *this; }
	int m_depth;
	X *m_creator;
	bool m_bHasCreate;
	Y & y_;
};



#ifdef SOCKETS_NAMESPACE
}
#endif

#endif // _SOCKETS_SyncListenSocket_H