/usr/include/ace/Asynch_Acceptor.cpp is in libace-dev 6.0.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | /* -*- C++ -*- */
// $Id: Asynch_Acceptor.cpp 93093 2011-01-17 20:51:25Z shuston $
#ifndef ACE_ASYNCH_ACCEPTOR_C
#define ACE_ASYNCH_ACCEPTOR_C
#include "ace/Asynch_Acceptor.h"
#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */
#if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
// This only works on platforms that support async i/o.
#include "ace/OS_Errno.h"
#include "ace/OS_Memory.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/Log_Msg.h"
#include "ace/Message_Block.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Stream.h"
#include "ace/Sock_Connect.h"
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
template <class HANDLER>
ACE_Asynch_Acceptor<HANDLER>::ACE_Asynch_Acceptor (void)
: listen_handle_ (ACE_INVALID_HANDLE),
pass_addresses_ (false),
validate_new_connection_ (false),
reissue_accept_ (1),
bytes_to_read_ (0)
{
}
template <class HANDLER>
ACE_Asynch_Acceptor<HANDLER>::~ACE_Asynch_Acceptor (void)
{
// Close down the listen socket
if (this->listen_handle_ != ACE_INVALID_HANDLE)
{
ACE_OS::closesocket (this->listen_handle_);
this->listen_handle_ = ACE_INVALID_HANDLE;
}
}
template <class HANDLER> int
ACE_Asynch_Acceptor<HANDLER>::open (const ACE_INET_Addr &address,
size_t bytes_to_read,
bool pass_addresses,
int backlog,
int reuse_addr,
ACE_Proactor *proactor,
bool validate_new_connection,
int reissue_accept,
int number_of_initial_accepts)
{
ACE_TRACE ("ACE_Asynch_Acceptor<>::open");
this->proactor (proactor);
this->pass_addresses_ = pass_addresses;
this->bytes_to_read_ = bytes_to_read;
this->validate_new_connection_ = validate_new_connection;
this->reissue_accept_ = reissue_accept;
this->addr_family_ = address.get_type ();
// Create the listener socket
this->listen_handle_ = ACE_OS::socket (address.get_type (), SOCK_STREAM, 0);
if (this->listen_handle_ == ACE_INVALID_HANDLE)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE_OS::socket")),
-1);
// Initialize the ACE_Asynch_Accept
if (this->asynch_accept_.open (*this,
this->listen_handle_,
0,
this->proactor ()) == -1)
{
ACE_Errno_Guard g (errno);
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE_Asynch_Accept::open")));
ACE_OS::closesocket (this->listen_handle_);
this->listen_handle_ = ACE_INVALID_HANDLE;
return -1;
}
if (reuse_addr)
{
// Reuse the address
int one = 1;
if (ACE_OS::setsockopt (this->listen_handle_,
SOL_SOCKET,
SO_REUSEADDR,
(const char*) &one,
sizeof one) == -1)
{
ACE_Errno_Guard g (errno);
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE_OS::setsockopt")));
ACE_OS::closesocket (this->listen_handle_);
this->listen_handle_ = ACE_INVALID_HANDLE;
return -1;
}
}
// If port is not specified, bind to any port.
static ACE_INET_Addr sa (ACE_sap_any_cast (const ACE_INET_Addr &));
if (address == sa &&
ACE::bind_port (this->listen_handle_,
INADDR_ANY,
address.get_type()) == -1)
{
ACE_Errno_Guard g (errno);
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE::bind_port")));
ACE_OS::closesocket (this->listen_handle_);
this->listen_handle_ = ACE_INVALID_HANDLE;
return -1;
}
// Bind to the specified port.
if (ACE_OS::bind (this->listen_handle_,
reinterpret_cast<sockaddr *> (address.get_addr ()),
address.get_size ()) == -1)
{
ACE_Errno_Guard g (errno);
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE_OS::bind")));
ACE_OS::closesocket (this->listen_handle_);
this->listen_handle_ = ACE_INVALID_HANDLE;
return -1;
}
// Start listening.
if (ACE_OS::listen (this->listen_handle_, backlog) == -1)
{
ACE_Errno_Guard g (errno);
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE_OS::listen")));
ACE_OS::closesocket (this->listen_handle_);
this->listen_handle_ = ACE_INVALID_HANDLE;
return -1;
}
// For the number of <intial_accepts>.
if (number_of_initial_accepts == -1)
number_of_initial_accepts = backlog;
for (int i = 0; i < number_of_initial_accepts; i++)
{
// Initiate accepts.
if (this->accept (bytes_to_read) == -1)
{
ACE_Errno_Guard g (errno);
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE_Asynch_Acceptor::accept")));
ACE_OS::closesocket (this->listen_handle_);
this->listen_handle_ = ACE_INVALID_HANDLE;
return -1;
}
}
return 0;
}
template <class HANDLER> int
ACE_Asynch_Acceptor<HANDLER>::set_handle (ACE_HANDLE listen_handle)
{
ACE_TRACE ("ACE_Asynch_Acceptor<>::set_handle");
// Take ownership of the <listen_handle>
this->listen_handle_ = listen_handle;
// Reinitialize the ACE_Asynch_Accept
if (this->asynch_accept_.open (*this,
this->listen_handle_,
0,
this->proactor ()) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("ACE_Asynch_Accept::open")),
-1);
return 0;
}
template <class HANDLER> ACE_HANDLE
ACE_Asynch_Acceptor<HANDLER>::get_handle (void) const
{
return this->listen_handle_;
}
template <class HANDLER> int
ACE_Asynch_Acceptor<HANDLER>::accept (size_t bytes_to_read, const void *act)
{
ACE_TRACE ("ACE_Asynch_Acceptor<>::accept");
ACE_Message_Block *message_block = 0;
// The space_needed calculation is drive by needs of Windows. POSIX doesn't
// need to extra 16 bytes, but it doesn't hurt.
size_t space_needed = sizeof (sockaddr_in) + 16;
#if defined (ACE_HAS_IPV6)
if (PF_INET6 == this->addr_family_)
space_needed = sizeof (sockaddr_in6) + 16;
#endif /* ACE_HAS_IPV6 */
space_needed = (2 * space_needed) + bytes_to_read;
// Create a new message block big enough for the addresses and data
ACE_NEW_RETURN (message_block,
ACE_Message_Block (space_needed),
-1);
// Initiate asynchronous accepts
if (this->asynch_accept_.accept (*message_block,
bytes_to_read,
ACE_INVALID_HANDLE,
act,
0,
ACE_SIGRTMIN,
this->addr_family_) == -1)
{
// Cleanup on error
message_block->release ();
return -1;
}
return 0;
}
template <class HANDLER> void
ACE_Asynch_Acceptor<HANDLER>::handle_accept (const ACE_Asynch_Accept::Result &result)
{
ACE_TRACE ("ACE_Asynch_Acceptor<>::handle_accept");
// Variable for error tracking
int error = 0;
// If the asynchronous accept fails.
if (!result.success () || result.accept_handle () == ACE_INVALID_HANDLE)
{
error = 1;
}
#if defined (ACE_WIN32)
// In order to use accept handle with other Window Sockets 1.1
// functions, we call the setsockopt function with the
// SO_UPDATE_ACCEPT_CONTEXT option. This option initializes the
// socket so that other Windows Sockets routines to access the
// socket correctly.
if (!error &&
ACE_OS::setsockopt (result.accept_handle (),
SOL_SOCKET,
SO_UPDATE_ACCEPT_CONTEXT,
(char *) &this->listen_handle_,
sizeof (this->listen_handle_)) == -1)
{
error = 1;
}
#endif /* ACE_WIN32 */
// Parse address.
ACE_INET_Addr local_address;
ACE_INET_Addr remote_address;
if (!error &&
(this->validate_new_connection_ || this->pass_addresses_))
// Parse the addresses.
this->parse_address (result,
remote_address,
local_address);
// Validate remote address
if (!error &&
this->validate_new_connection_ &&
(this->validate_connection (result, remote_address, local_address) == -1))
{
error = 1;
}
HANDLER *new_handler = 0;
if (!error)
{
// The Template method
new_handler = this->make_handler ();
if (new_handler == 0)
{
error = 1;
}
}
// If no errors
if (!error)
{
// Update the Proactor unless make_handler() or constructed handler
// set up its own.
if (new_handler->proactor () == 0)
new_handler->proactor (this->proactor ());
// Pass the addresses
if (this->pass_addresses_)
new_handler->addresses (remote_address,
local_address);
// Pass the ACT
if (result.act () != 0)
new_handler->act (result.act ());
// Set up the handler's new handle value
new_handler->handle (result.accept_handle ());
// Initiate the handler
new_handler->open (result.accept_handle (),
result.message_block ());
}
// On failure, no choice but to close the socket
if (error &&
result.accept_handle() != ACE_INVALID_HANDLE )
ACE_OS::closesocket (result.accept_handle ());
// Delete the dynamically allocated message_block
result.message_block ().release ();
// Start off another asynchronous accept to keep the backlog going,
// unless we closed the listen socket already (from the destructor),
// or this callback is the result of a canceled/aborted accept.
if (this->should_reissue_accept () &&
this->listen_handle_ != ACE_INVALID_HANDLE
#if defined (ACE_WIN32)
&& result.error () != ERROR_OPERATION_ABORTED
#else
&& result.error () != ECANCELED
#endif
)
this->accept (this->bytes_to_read_, result.act ());
}
template <class HANDLER> int
ACE_Asynch_Acceptor<HANDLER>::validate_connection
(const ACE_Asynch_Accept::Result& /* result */,
const ACE_INET_Addr& /* remote */,
const ACE_INET_Addr& /* local */)
{
// Default implementation always validates the remote address.
return 0;
}
template <class HANDLER> int
ACE_Asynch_Acceptor<HANDLER>::cancel (void)
{
ACE_TRACE ("ACE_Asynch_Acceptor<>::cancel");
// All I/O operations that are canceled will complete with the error
// ERROR_OPERATION_ABORTED. All completion notifications for the I/O
// operations will occur normally.
#if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
return (int) ::CancelIo (this->listen_handle_);
#else
// Supported now
return this->asynch_accept_.cancel();
#endif /* defined (ACE_HAS_WIN32_OVERLAPPED_IO) */
}
template <class HANDLER> void
ACE_Asynch_Acceptor<HANDLER>::parse_address (const
ACE_Asynch_Accept::Result &result,
ACE_INET_Addr &remote_address,
ACE_INET_Addr &local_address)
{
ACE_TRACE ("ACE_Asynch_Acceptor<>::parse_address");
#if defined (ACE_HAS_AIO_CALLS)
// Use an ACE_SOCK to get the addresses - it knows how to deal with
// ACE_INET_Addr objects and get IPv4/v6 addresses.
ACE_SOCK_Stream str (result.accept_handle ());
str.get_local_addr (local_address);
str.get_remote_addr (remote_address);
#elif defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
ACE_Message_Block &message_block = result.message_block ();
sockaddr *local_addr = 0;
sockaddr *remote_addr = 0;
int local_size = 0;
int remote_size = 0;
// This matches setup in accept().
size_t addr_size = sizeof (sockaddr_in) + 16;
#if defined (ACE_HAS_IPV6)
if (this->addr_family_ == PF_INET6)
addr_size = sizeof (sockaddr_in6) + 16;
#endif /* ACE_HAS_IPV6 */
::GetAcceptExSockaddrs (message_block.rd_ptr (),
static_cast<DWORD> (this->bytes_to_read_),
static_cast<DWORD> (addr_size),
static_cast<DWORD> (addr_size),
&local_addr,
&local_size,
&remote_addr,
&remote_size);
local_address.set (reinterpret_cast<sockaddr_in *> (local_addr),
local_size);
remote_address.set (reinterpret_cast<sockaddr_in *> (remote_addr),
remote_size);
#else
// just in case
errno = ENOTSUP;
#endif /* defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) */
return;
}
template <class HANDLER> ACE_HANDLE
ACE_Asynch_Acceptor<HANDLER>::handle (void) const
{
return this->listen_handle_;
}
template <class HANDLER> void
ACE_Asynch_Acceptor<HANDLER>::handle (ACE_HANDLE h)
{
ACE_Handler::handle (h);
}
template <class HANDLER> ACE_Asynch_Accept &
ACE_Asynch_Acceptor<HANDLER>::asynch_accept (void)
{
return this->asynch_accept_;
}
template <class HANDLER> HANDLER *
ACE_Asynch_Acceptor<HANDLER>::make_handler (void)
{
// Default behavior
HANDLER *handler = 0;
ACE_NEW_RETURN (handler,
HANDLER,
0);
return handler;
}
template <class HANDLER> bool
ACE_Asynch_Acceptor<HANDLER>::pass_addresses (void) const
{
return this->pass_addresses_;
}
template <class HANDLER> void
ACE_Asynch_Acceptor<HANDLER>::pass_addresses (bool new_value)
{
this->pass_addresses_ = new_value;
}
template <class HANDLER> bool
ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (void) const
{
return this->validate_new_connection_;
}
template <class HANDLER> void
ACE_Asynch_Acceptor<HANDLER>::validate_new_connection (bool new_value)
{
this->validate_new_connection_ = new_value;
}
template <class HANDLER> int
ACE_Asynch_Acceptor<HANDLER>::reissue_accept (void) const
{
return this->reissue_accept_;
}
template <class HANDLER> void
ACE_Asynch_Acceptor<HANDLER>::reissue_accept (int new_value)
{
this->reissue_accept_ = new_value;
}
template <class HANDLER> size_t
ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (void) const
{
return this->bytes_to_read_;
}
template <class HANDLER> void
ACE_Asynch_Acceptor<HANDLER>::bytes_to_read (size_t new_value)
{
this->bytes_to_read_ = new_value;
}
template <class HANDLER> int
ACE_Asynch_Acceptor<HANDLER>::should_reissue_accept (void)
{
return this->reissue_accept_;
}
ACE_END_VERSIONED_NAMESPACE_DECL
#endif /* ACE_WIN32 || ACE_HAS_AIO_CALLS */
#endif /* ACE_ASYNCH_ACCEPTOR_C */
|