00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _SOCKETS_ListenSocket_H
00033 #define _SOCKETS_ListenSocket_H
00034 #include "sockets-config.h"
00035
00036 #ifdef _WIN32
00037 #include <stdlib.h>
00038 #else
00039 #include <errno.h>
00040 #endif
00041
00042 #include "ISocketHandler.h"
00043 #include "Socket.h"
00044 #include "Utility.h"
00045 #include "SctpSocket.h"
00046 #include "Ipv4Address.h"
00047 #include "Ipv6Address.h"
00048 #ifdef ENABLE_EXCEPTIONS
00049 #include "Exception.h"
00050 #endif
00051 #include "Lock.h"
00052
00053 #ifdef SOCKETS_NAMESPACE
00054 namespace SOCKETS_NAMESPACE {
00055 #endif
00056
00057
00060 template <class X>
00061 class ListenSocket : public Socket
00062 {
00063 public:
00067 ListenSocket(ISocketHandler& h,bool use_creator = true) : Socket(h), m_depth(0), m_creator(NULL)
00068 ,m_bHasCreate(false)
00069 {
00070 if (use_creator)
00071 {
00072 m_creator = new X(h);
00073 Socket *tmp = m_creator -> Create();
00074 if (tmp && dynamic_cast<X *>(tmp))
00075 {
00076 m_bHasCreate = true;
00077 }
00078 if (tmp)
00079 {
00080 delete tmp;
00081 }
00082 }
00083 }
00084 ~ListenSocket() {
00085 if (m_creator)
00086 {
00087 delete m_creator;
00088 }
00089 }
00090
00092 int Close() {
00093 if (GetSocket() != INVALID_SOCKET)
00094 {
00095 if (closesocket(GetSocket()) == -1)
00096 {
00097 }
00098 else
00099 {
00100 Attach(INVALID_SOCKET);
00101 }
00102 }
00103 return 0;
00104 }
00105
00109 int Bind(port_t port,int depth = 20) {
00110 #ifdef ENABLE_IPV6
00111 #ifdef IPPROTO_IPV6
00112 if (IsIpv6())
00113 {
00114 Ipv6Address ad(port);
00115 return Bind(ad, depth);
00116 }
00117 else
00118 #endif
00119 #endif
00120 {
00121 Ipv4Address ad(port);
00122 return Bind(ad, depth);
00123 }
00124 }
00125
00126 int Bind(SocketAddress& ad,int depth) {
00127 #ifdef USE_SCTP
00128 if (dynamic_cast<SctpSocket *>(m_creator))
00129 {
00130 return Bind(ad, "sctp", depth);
00131 }
00132 #endif
00133 return Bind(ad, "tcp", depth);
00134 }
00135
00140 int Bind(port_t port,const std::string& protocol,int depth = 20) {
00141 #ifdef ENABLE_IPV6
00142 #ifdef IPPROTO_IPV6
00143 if (IsIpv6())
00144 {
00145 Ipv6Address ad(port);
00146 return Bind(ad, protocol, depth);
00147 }
00148 else
00149 #endif
00150 #endif
00151 {
00152 Ipv4Address ad(port);
00153 return Bind(ad, protocol, depth);
00154 }
00155 }
00156
00161 int Bind(const std::string& intf,port_t port,int depth = 20) {
00162 #ifdef ENABLE_IPV6
00163 #ifdef IPPROTO_IPV6
00164 if (IsIpv6())
00165 {
00166 Ipv6Address ad(intf, port);
00167 if (ad.IsValid())
00168 {
00169 return Bind(ad, depth);
00170 }
00171 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
00172 return -1;
00173 }
00174 else
00175 #endif
00176 #endif
00177 {
00178 Ipv4Address ad(intf, port);
00179 if (ad.IsValid())
00180 {
00181 return Bind(ad, depth);
00182 }
00183 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
00184 return -1;
00185 }
00186 }
00187
00193 int Bind(const std::string& intf,port_t port,const std::string& protocol,int depth = 20) {
00194 #ifdef ENABLE_IPV6
00195 #ifdef IPPROTO_IPV6
00196 if (IsIpv6())
00197 {
00198 Ipv6Address ad(intf, port);
00199 if (ad.IsValid())
00200 {
00201 return Bind(ad, protocol, depth);
00202 }
00203 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
00204 return -1;
00205 }
00206 else
00207 #endif
00208 #endif
00209 {
00210 Ipv4Address ad(intf, port);
00211 if (ad.IsValid())
00212 {
00213 return Bind(ad, protocol, depth);
00214 }
00215 Handler().LogError(this, "Bind", 0, "name resolution of interface name failed", LOG_LEVEL_FATAL);
00216 return -1;
00217 }
00218 }
00219
00224 int Bind(ipaddr_t a,port_t port,int depth = 20) {
00225 Ipv4Address ad(a, port);
00226 #ifdef USE_SCTP
00227 if (dynamic_cast<SctpSocket *>(m_creator))
00228 {
00229 return Bind(ad, "sctp", depth);
00230 }
00231 #endif
00232 return Bind(ad, "tcp", depth);
00233 }
00239 int Bind(ipaddr_t a,port_t port,const std::string& protocol,int depth) {
00240 Ipv4Address ad(a, port);
00241 return Bind(ad, protocol, depth);
00242 }
00243
00244 #ifdef ENABLE_IPV6
00245 #ifdef IPPROTO_IPV6
00246
00250 int Bind(in6_addr a,port_t port,int depth = 20) {
00251 Ipv6Address ad(a, port);
00252 #ifdef USE_SCTP
00253 if (dynamic_cast<SctpSocket *>(m_creator))
00254 {
00255 return Bind(ad, "sctp", depth);
00256 }
00257 #endif
00258 return Bind(ad, "tcp", depth);
00259 }
00265 int Bind(in6_addr a,port_t port,const std::string& protocol,int depth) {
00266 Ipv6Address ad(a, port);
00267 return Bind(ad, protocol, depth);
00268 }
00269 #endif
00270 #endif
00271
00276 int Bind(SocketAddress& ad,const std::string& protocol,int depth) {
00277 SOCKET s;
00278 if ( (s = CreateSocket(ad.GetFamily(), SOCK_STREAM, protocol)) == INVALID_SOCKET)
00279 {
00280 return -1;
00281 }
00282
00283 if (!SetNonblocking(true, s))
00284 {
00285 closesocket(s);
00286 return -1;
00287 }
00288 if (bind(s, ad, ad) == -1)
00289 {
00290 Handler().LogError(this, "bind", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00291 closesocket(s);
00292 #ifdef ENABLE_EXCEPTIONS
00293 throw Exception("bind() failed for port " + Utility::l2string(ad.GetPort()) + ": " + StrError(Errno));
00294 #endif
00295 return -1;
00296 }
00297 if (listen(s, depth) == -1)
00298 {
00299 Handler().LogError(this, "listen", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00300 closesocket(s);
00301 #ifdef ENABLE_EXCEPTIONS
00302 throw Exception("listen() failed for port " + Utility::l2string(ad.GetPort()) + ": " + StrError(Errno));
00303 #endif
00304 return -1;
00305 }
00306 m_depth = depth;
00307 Attach(s);
00308 return 0;
00309 }
00310
00312 port_t GetPort()
00313 {
00314 return GetSockPort();
00315 }
00316
00318 int GetDepth()
00319 {
00320 return m_depth;
00321 }
00322
00324 void OnRead()
00325 {
00326 struct sockaddr sa;
00327 int max = 10;
00328 while (max--)
00329 {
00330 socklen_t sa_len = sizeof(struct sockaddr);
00331 SOCKET a_s = accept(GetSocket(), &sa, &sa_len);
00332 if (a_s == INVALID_SOCKET)
00333 {
00334
00335 #ifdef _WIN32
00336 if (Errno != WSAEWOULDBLOCK)
00337 #else
00338 if (Errno != EWOULDBLOCK)
00339 #endif
00340 {
00341 Handler().LogError(this, "accept", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00342 }
00343 return;
00344 }
00345 if (!Handler().OkToAccept(this))
00346 {
00347 Handler().LogError(this, "accept", -1, "Not OK to accept", LOG_LEVEL_WARNING);
00348 closesocket(a_s);
00349 return;
00350 }
00351 if (Handler().GetCount() >= Handler().MaxCount())
00352 {
00353 Handler().LogError(this, "accept", (int)Handler().GetCount(), "ISocketHandler socket limit reached", LOG_LEVEL_FATAL);
00354 closesocket(a_s);
00355 return;
00356 }
00357
00358 Socket *tmp = NULL;
00359 if (Handler().IsThreaded())
00360 {
00361 ISocketHandler& h = Handler().GetRandomHandler();
00362 tmp = new X(h);
00363 #ifdef ENABLE_IPV6
00364 tmp -> SetIpv6( IsIpv6() );
00365 #endif
00366 tmp -> SetParent(this);
00367 tmp -> Attach(a_s);
00368 tmp -> SetNonblocking(true);
00369 #ifdef ENABLE_IPV6
00370 #ifdef IPPROTO_IPV6
00371 if (sa_len == sizeof(struct sockaddr_in6))
00372 {
00373 if (reinterpret_cast<struct sockaddr_in6&>(sa).sin6_family == AF_INET6)
00374 {
00375 Ipv6Address ad(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_addr,
00376 ntohs(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_port));
00377 ad.SetFlowinfo(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_flowinfo);
00378 #ifndef _WIN32
00379 ad.SetScopeId(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_scope_id);
00380 #endif
00381 tmp -> SetRemoteAddress(ad);
00382 }
00383 }
00384 #endif
00385 #endif
00386 if (sa_len == sizeof(struct sockaddr_in))
00387 {
00388 struct sockaddr_in *p = (struct sockaddr_in *)&sa;
00389 if (p -> sin_family == AF_INET)
00390 {
00391 Ipv4Address ad(p -> sin_addr,ntohs(p -> sin_port));
00392 tmp -> SetRemoteAddress(ad);
00393 }
00394 }
00395 tmp -> SetConnected(true);
00396 tmp -> Init();
00397 tmp -> SetDeleteByHandler(true);
00398 {
00399 Lock lock(h.GetMutex());
00400 h.Add(tmp);
00401 #ifdef HAVE_OPENSSL
00402 if (tmp -> IsSSL())
00403 {
00404
00405
00406
00407
00408
00409
00410
00411
00412 tmp -> OnSSLAccept();
00413 }
00414 else
00415 #endif
00416 {
00417 tmp -> OnAccept();
00418 }
00419 }
00420 h.Release();
00421 }
00422 else
00423 {
00424 tmp = m_bHasCreate ? m_creator -> Create() : new X(Handler());
00425 #ifdef ENABLE_IPV6
00426 tmp -> SetIpv6( IsIpv6() );
00427 #endif
00428 tmp -> SetParent(this);
00429 tmp -> Attach(a_s);
00430 tmp -> SetNonblocking(true);
00431 #ifdef ENABLE_IPV6
00432 #ifdef IPPROTO_IPV6
00433 if (sa_len == sizeof(struct sockaddr_in6))
00434 {
00435 if (reinterpret_cast<struct sockaddr_in6&>(sa).sin6_family == AF_INET6)
00436 {
00437 Ipv6Address ad(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_addr,
00438 ntohs(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_port));
00439 ad.SetFlowinfo(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_flowinfo);
00440 #ifndef _WIN32
00441 ad.SetScopeId(reinterpret_cast<struct sockaddr_in6&>(sa).sin6_scope_id);
00442 #endif
00443 tmp -> SetRemoteAddress(ad);
00444 }
00445 }
00446 #endif
00447 #endif
00448 if (sa_len == sizeof(struct sockaddr_in))
00449 {
00450 struct sockaddr_in *p = (struct sockaddr_in *)&sa;
00451 if (p -> sin_family == AF_INET)
00452 {
00453 Ipv4Address ad(p -> sin_addr,ntohs(p -> sin_port));
00454 tmp -> SetRemoteAddress(ad);
00455 }
00456 }
00457 tmp -> SetConnected(true);
00458 tmp -> Init();
00459 tmp -> SetDeleteByHandler(true);
00460 Handler().Add(tmp);
00461 #ifdef HAVE_OPENSSL
00462 if (tmp -> IsSSL())
00463 {
00464
00465
00466
00467
00468
00469
00470
00471
00472 tmp -> OnSSLAccept();
00473 }
00474 else
00475 #endif
00476 {
00477 tmp -> OnAccept();
00478 }
00479 }
00480 }
00481 }
00482
00485 virtual SOCKET Accept(SOCKET socket, struct sockaddr *saptr, socklen_t *lenptr)
00486 {
00487 return accept(socket, saptr, lenptr);
00488 }
00489
00490 bool HasCreator() { return m_bHasCreate; }
00491
00492 void OnOptions(int,int,int,SOCKET) {
00493 SetSoReuseaddr(true);
00494 }
00495
00496 protected:
00497 ListenSocket(const ListenSocket& s) : Socket(s) {}
00498 private:
00499 ListenSocket& operator=(const ListenSocket& ) { return *this; }
00500 int m_depth;
00501 X *m_creator;
00502 bool m_bHasCreate;
00503 };
00504
00505
00506
00507 #ifdef SOCKETS_NAMESPACE
00508 }
00509 #endif
00510
00511 #endif // _SOCKETS_ListenSocket_H
00512