00001
00002
00003 #include "ace/SOCK_Acceptor.h"
00004
00005 #include "ace/Log_Msg.h"
00006 #include "ace/OS_Errno.h"
00007 #include "ace/OS_NS_string.h"
00008 #include "ace/OS_NS_sys_socket.h"
00009 #include "ace/os_include/os_fcntl.h"
00010
00011 #if !defined (__ACE_INLINE__)
00012 #include "ace/SOCK_Acceptor.inl"
00013 #endif
00014
00015 #if !defined (ACE_HAS_WINCE)
00016 #include "ace/OS_QoS.h"
00017 #endif // ACE_HAS_WINCE
00018
00019 ACE_RCSID(ace, SOCK_Acceptor, "SOCK_Acceptor.cpp,v 4.43 2006/04/04 18:56:21 shuston Exp")
00020
00021 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00022
00023 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_Acceptor)
00024
00025
00026
00027 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (void)
00028 {
00029 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00030 }
00031
00032
00033
00034 int
00035 ACE_SOCK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
00036 int restart,
00037 int &in_blocking_mode) const
00038 {
00039 ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_start");
00040
00041 ACE_HANDLE handle = this->get_handle ();
00042
00043
00044 if (timeout != 0)
00045 {
00046 if (ACE::handle_timed_accept (handle,
00047 timeout,
00048 restart) == -1)
00049 return -1;
00050 else
00051 {
00052 in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
00053 ACE_NONBLOCK);
00054
00055
00056 if (in_blocking_mode
00057 && ACE::set_flags (handle,
00058 ACE_NONBLOCK) == -1)
00059 return -1;
00060 }
00061 }
00062
00063 return 0;
00064 }
00065
00066 int
00067 ACE_SOCK_Acceptor::shared_accept_finish (ACE_SOCK_Stream new_stream,
00068 int in_blocking_mode,
00069 int reset_new_handle) const
00070 {
00071 ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_finish ()");
00072
00073 ACE_HANDLE new_handle = new_stream.get_handle ();
00074
00075
00076
00077
00078 if (in_blocking_mode)
00079 {
00080
00081 ACE_Errno_Guard error (errno);
00082
00083
00084
00085 ACE::clr_flags (this->get_handle (),
00086 ACE_NONBLOCK);
00087 ACE::clr_flags (new_handle,
00088 ACE_NONBLOCK);
00089 }
00090
00091 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00092 if (reset_new_handle)
00093
00094 ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
00095 #else
00096 ACE_UNUSED_ARG (reset_new_handle);
00097 #endif
00098
00099 return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
00100 }
00101
00102
00103
00104 int
00105 ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream &new_stream,
00106 ACE_Addr *remote_addr,
00107 ACE_Time_Value *timeout,
00108 int restart,
00109 int reset_new_handle) const
00110 {
00111 ACE_TRACE ("ACE_SOCK_Acceptor::accept");
00112
00113 int in_blocking_mode = 0;
00114 if (this->shared_accept_start (timeout,
00115 restart,
00116 in_blocking_mode) == -1)
00117 return -1;
00118 else
00119 {
00120
00121
00122 int *len_ptr = 0;
00123 sockaddr *addr = 0;
00124 int len = 0;
00125
00126 if (remote_addr != 0)
00127 {
00128 len = remote_addr->get_size ();
00129 len_ptr = &len;
00130 addr = (sockaddr *) remote_addr->get_addr ();
00131 }
00132
00133 do
00134 new_stream.set_handle (ACE_OS::accept (this->get_handle (),
00135 addr,
00136 len_ptr));
00137 while (new_stream.get_handle () == ACE_INVALID_HANDLE
00138 && restart != 0
00139 && errno == EINTR
00140 && timeout == 0);
00141
00142
00143
00144 if (new_stream.get_handle () != ACE_INVALID_HANDLE
00145 && remote_addr != 0)
00146 {
00147 remote_addr->set_size (len);
00148 if (addr)
00149 remote_addr->set_type (addr->sa_family);
00150 }
00151 }
00152
00153 return this->shared_accept_finish (new_stream,
00154 in_blocking_mode,
00155 reset_new_handle);
00156 }
00157
00158 #if !defined (ACE_HAS_WINCE)
00159 int
00160 ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream &new_stream,
00161 ACE_Accept_QoS_Params qos_params,
00162 ACE_Addr *remote_addr,
00163 ACE_Time_Value *timeout,
00164 int restart,
00165 int reset_new_handle) const
00166 {
00167 ACE_TRACE ("ACE_SOCK_Acceptor::accept");
00168
00169 int in_blocking_mode = 0;
00170 if (this->shared_accept_start (timeout,
00171 restart,
00172 in_blocking_mode) == -1)
00173 return -1;
00174 else
00175 {
00176
00177
00178 int *len_ptr = 0;
00179 int len = 0;
00180 sockaddr *addr = 0;
00181
00182 if (remote_addr != 0)
00183 {
00184 len = remote_addr->get_size ();
00185 len_ptr = &len;
00186 addr = (sockaddr *) remote_addr->get_addr ();
00187 }
00188
00189 do
00190 new_stream.set_handle (ACE_OS::accept (this->get_handle (),
00191 addr,
00192 len_ptr,
00193 qos_params));
00194 while (new_stream.get_handle () == ACE_INVALID_HANDLE
00195 && restart != 0
00196 && errno == EINTR
00197 && timeout == 0);
00198
00199
00200
00201 if (new_stream.get_handle () != ACE_INVALID_HANDLE
00202 && remote_addr != 0)
00203 remote_addr->set_size (len);
00204 }
00205
00206 return this->shared_accept_finish (new_stream,
00207 in_blocking_mode,
00208 reset_new_handle);
00209 }
00210 #endif // ACE_HAS_WINCE
00211
00212 void
00213 ACE_SOCK_Acceptor::dump (void) const
00214 {
00215 #if defined (ACE_HAS_DUMP)
00216 ACE_TRACE ("ACE_SOCK_Acceptor::dump");
00217 #endif
00218 }
00219
00220 int
00221 ACE_SOCK_Acceptor::shared_open (const ACE_Addr &local_sap,
00222 int protocol_family,
00223 int backlog)
00224 {
00225 ACE_TRACE ("ACE_SOCK_Acceptor::shared_open");
00226 int error = 0;
00227
00228 #if defined (ACE_HAS_IPV6)
00229 ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
00230
00231 if (protocol_family == PF_INET6)
00232 {
00233 sockaddr_in6 local_inet6_addr;
00234 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
00235 0,
00236 sizeof local_inet6_addr);
00237
00238 if (local_sap == ACE_Addr::sap_any)
00239 {
00240 local_inet6_addr.sin6_family = AF_INET6;
00241 local_inet6_addr.sin6_port = 0;
00242 local_inet6_addr.sin6_addr = in6addr_any;
00243 }
00244 else
00245 local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
00246
00247
00248
00249
00250 if (ACE_OS::bind (this->get_handle (),
00251 reinterpret_cast<sockaddr *> (&local_inet6_addr),
00252 sizeof local_inet6_addr) == -1)
00253 error = 1;
00254 }
00255 else
00256 #endif
00257 if (protocol_family == PF_INET)
00258 {
00259 sockaddr_in local_inet_addr;
00260 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
00261 0,
00262 sizeof local_inet_addr);
00263
00264 if (local_sap == ACE_Addr::sap_any)
00265 {
00266 local_inet_addr.sin_port = 0;
00267 }
00268 else
00269 local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
00270 if (local_inet_addr.sin_port == 0)
00271 {
00272 if (ACE::bind_port (this->get_handle (),
00273 ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
00274 error = 1;
00275 }
00276 else if (ACE_OS::bind (this->get_handle (),
00277 reinterpret_cast<sockaddr *> (&local_inet_addr),
00278 sizeof local_inet_addr) == -1)
00279 error = 1;
00280 }
00281 else if (ACE_OS::bind (this->get_handle (),
00282 (sockaddr *) local_sap.get_addr (),
00283 local_sap.get_size ()) == -1)
00284 error = 1;
00285
00286 if (error != 0
00287 || ACE_OS::listen (this->get_handle (),
00288 backlog) == -1)
00289 {
00290 ACE_Errno_Guard g (errno);
00291 error = 1;
00292 this->close ();
00293 }
00294
00295 return error ? -1 : 0;
00296 }
00297
00298 int
00299 ACE_SOCK_Acceptor::open (const ACE_Addr &local_sap,
00300 ACE_Protocol_Info *protocolinfo,
00301 ACE_SOCK_GROUP g,
00302 u_long flags,
00303 int reuse_addr,
00304 int protocol_family,
00305 int backlog,
00306 int protocol)
00307 {
00308 ACE_TRACE ("ACE_SOCK_Acceptor::open");
00309
00310 if (protocol_family == PF_UNSPEC)
00311 protocol_family = local_sap.get_type ();
00312
00313 if (ACE_SOCK::open (SOCK_STREAM,
00314 protocol_family,
00315 protocol,
00316 protocolinfo,
00317 g,
00318 flags,
00319 reuse_addr) == -1)
00320 return -1;
00321 else
00322 return this->shared_open (local_sap,
00323 protocol_family,
00324 backlog);
00325 }
00326
00327 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
00328 ACE_Protocol_Info *protocolinfo,
00329 ACE_SOCK_GROUP g,
00330 u_long flags,
00331 int reuse_addr,
00332 int protocol_family,
00333 int backlog,
00334 int protocol)
00335 {
00336 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00337 if (this->open (local_sap,
00338 protocolinfo,
00339 g,
00340 flags,
00341 reuse_addr,
00342 protocol_family,
00343 backlog,
00344 protocol) == -1)
00345 ACE_ERROR ((LM_ERROR,
00346 ACE_LIB_TEXT ("%p\n"),
00347 ACE_LIB_TEXT ("ACE_SOCK_Acceptor")));
00348 }
00349
00350
00351
00352 int
00353 ACE_SOCK_Acceptor::open (const ACE_Addr &local_sap,
00354 int reuse_addr,
00355 int protocol_family,
00356 int backlog,
00357 int protocol)
00358 {
00359 ACE_TRACE ("ACE_SOCK_Acceptor::open");
00360
00361 if (local_sap != ACE_Addr::sap_any)
00362 protocol_family = local_sap.get_type ();
00363 else if (protocol_family == PF_UNSPEC)
00364 {
00365 #if defined (ACE_HAS_IPV6)
00366 protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
00367 #else
00368 protocol_family = PF_INET;
00369 #endif
00370 }
00371
00372 if (ACE_SOCK::open (SOCK_STREAM,
00373 protocol_family,
00374 protocol,
00375 reuse_addr) == -1)
00376 return -1;
00377 else
00378 return this->shared_open (local_sap,
00379 protocol_family,
00380 backlog);
00381 }
00382
00383
00384
00385 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
00386 int reuse_addr,
00387 int protocol_family,
00388 int backlog,
00389 int protocol)
00390 {
00391 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00392 if (this->open (local_sap,
00393 reuse_addr,
00394 protocol_family,
00395 backlog,
00396 protocol) == -1)
00397 ACE_ERROR ((LM_ERROR,
00398 ACE_LIB_TEXT ("%p\n"),
00399 ACE_LIB_TEXT ("ACE_SOCK_Acceptor")));
00400 }
00401
00402 int
00403 ACE_SOCK_Acceptor::close (void)
00404 {
00405 return ACE_SOCK::close ();
00406 }
00407
00408 ACE_END_VERSIONED_NAMESPACE_DECL