00001
00002
00003 #include "ace/SOCK_SEQPACK_Acceptor.h"
00004
00005 #include "ace/Auto_Ptr.h"
00006 #include "ace/Log_Msg.h"
00007 #include "ace/OS_Memory.h"
00008 #include "ace/OS_NS_string.h"
00009 #include "ace/OS_NS_sys_socket.h"
00010 #include "ace/os_include/os_fcntl.h"
00011
00012 #if !defined (__ACE_INLINE__)
00013 #include "ace/SOCK_SEQPACK_Acceptor.inl"
00014 #endif
00015
00016 ACE_RCSID(ace, SOCK_SEQPACK_Acceptor, "SOCK_SEQPACK_Acceptor.cpp,v 4.30 2002/03/08 23:18:09 spark Exp")
00017
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019
00020 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_SEQPACK_Acceptor)
00021
00022
00023
00024 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (void)
00025 {
00026 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
00027 }
00028
00029
00030
00031 int
00032 ACE_SOCK_SEQPACK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
00033 int restart,
00034 int &in_blocking_mode) const
00035 {
00036 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_start");
00037
00038 ACE_HANDLE handle = this->get_handle ();
00039
00040
00041 if (timeout != 0)
00042 {
00043 if (ACE::handle_timed_accept (handle,
00044 timeout,
00045 restart) == -1)
00046 return -1;
00047 else
00048 {
00049 in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
00050 ACE_NONBLOCK);
00051
00052
00053 if (in_blocking_mode
00054 && ACE::set_flags (handle,
00055 ACE_NONBLOCK) == -1)
00056 return -1;
00057 }
00058 }
00059
00060 return 0;
00061 }
00062
00063 int
00064 ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish (ACE_SOCK_SEQPACK_Association new_association,
00065 int in_blocking_mode,
00066 int reset_new_handle) const
00067 {
00068 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish ()");
00069
00070 ACE_HANDLE new_handle = new_association.get_handle ();
00071
00072
00073
00074
00075 if (in_blocking_mode)
00076 {
00077
00078 ACE_Errno_Guard error (errno);
00079
00080
00081
00082 ACE::clr_flags (this->get_handle (),
00083 ACE_NONBLOCK);
00084 ACE::clr_flags (new_handle,
00085 ACE_NONBLOCK);
00086 }
00087
00088 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00089 if (reset_new_handle)
00090
00091 ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
00092 #else
00093 ACE_UNUSED_ARG (reset_new_handle);
00094 #endif
00095
00096 return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
00097 }
00098
00099
00100
00101 int
00102 ACE_SOCK_SEQPACK_Acceptor::accept (ACE_SOCK_SEQPACK_Association &new_association,
00103 ACE_Addr *remote_addr,
00104 ACE_Time_Value *timeout,
00105 int restart,
00106 int reset_new_handle) const
00107 {
00108 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::accept");
00109
00110 int in_blocking_mode = 0;
00111 if (this->shared_accept_start (timeout,
00112 restart,
00113 in_blocking_mode) == -1)
00114 return -1;
00115 else
00116 {
00117
00118
00119 int *len_ptr = 0;
00120 sockaddr *addr = 0;
00121 int len = 0;
00122
00123 if (remote_addr != 0)
00124 {
00125 len = remote_addr->get_size ();
00126 len_ptr = &len;
00127 addr = (sockaddr *) remote_addr->get_addr ();
00128 }
00129
00130 do
00131 new_association.set_handle (ACE_OS::accept (this->get_handle (),
00132 addr,
00133 len_ptr));
00134 while (new_association.get_handle () == ACE_INVALID_HANDLE
00135 && restart != 0
00136 && errno == EINTR
00137 && timeout == 0);
00138
00139
00140
00141 if (new_association.get_handle () != ACE_INVALID_HANDLE
00142 && remote_addr != 0)
00143 {
00144 remote_addr->set_size (len);
00145 remote_addr->set_type (addr->sa_family);
00146 }
00147 }
00148
00149 return this->shared_accept_finish (new_association,
00150 in_blocking_mode,
00151 reset_new_handle);
00152 }
00153
00154 void
00155 ACE_SOCK_SEQPACK_Acceptor::dump (void) const
00156 {
00157 #if defined (ACE_HAS_DUMP)
00158 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::dump");
00159 #endif
00160 }
00161
00162 int
00163 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Addr &local_sap,
00164 int protocol_family,
00165 int backlog)
00166 {
00167 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
00168 int error = 0;
00169
00170 #if defined (ACE_HAS_IPV6)
00171 ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
00172
00173 if (protocol_family == PF_INET6)
00174 {
00175 sockaddr_in6 local_inet6_addr;
00176 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
00177 0,
00178 sizeof local_inet6_addr);
00179
00180 if (local_sap == ACE_Addr::sap_any)
00181 {
00182 local_inet6_addr.sin6_family = AF_INET6;
00183 local_inet6_addr.sin6_port = 0;
00184 local_inet6_addr.sin6_addr = in6addr_any;
00185 }
00186 else
00187 local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
00188
00189
00190
00191
00192 if (ACE_OS::bind (this->get_handle (),
00193 reinterpret_cast<sockaddr *> (&local_inet6_addr),
00194 sizeof local_inet6_addr) == -1)
00195 error = 1;
00196 }
00197 else
00198 #endif
00199 if (protocol_family == PF_INET)
00200 {
00201 sockaddr_in local_inet_addr;
00202 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
00203 0,
00204 sizeof local_inet_addr);
00205
00206 if (local_sap == ACE_Addr::sap_any)
00207 {
00208 local_inet_addr.sin_port = 0;
00209 }
00210 else
00211 local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
00212 if (local_inet_addr.sin_port == 0)
00213 {
00214 if (ACE::bind_port (this->get_handle ()) == -1)
00215 error = 1;
00216 }
00217 else if (ACE_OS::bind (this->get_handle (),
00218 reinterpret_cast<sockaddr *> (&local_inet_addr),
00219 sizeof local_inet_addr) == -1)
00220 error = 1;
00221 }
00222 else if (ACE_OS::bind (this->get_handle (),
00223 (sockaddr *) local_sap.get_addr (),
00224 local_sap.get_size ()) == -1)
00225 error = 1;
00226
00227 if (error != 0
00228 || ACE_OS::listen (this->get_handle (),
00229 backlog) == -1)
00230 {
00231 error = 1;
00232 this->close ();
00233 }
00234
00235 return error ? -1 : 0;
00236 }
00237
00238
00239
00240 int
00241 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Multihomed_INET_Addr &local_sap,
00242 int protocol_family,
00243 int backlog)
00244 {
00245 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
00246 int error = 0;
00247
00248
00249 #if defined (ACE_HAS_IPV6)
00250 ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
00251
00252 if (protocol_family == PF_INET6)
00253 {
00254 sockaddr_in6 local_inet6_addr;
00255 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
00256 0,
00257 sizeof local_inet6_addr);
00258
00259 if (local_sap.ACE_Addr::operator== (ACE_Addr::sap_any))
00260 {
00261 local_inet6_addr.sin6_family = AF_INET6;
00262 local_inet6_addr.sin6_port = 0;
00263 local_inet6_addr.sin6_addr = in6addr_any;
00264 }
00265 else
00266 local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
00267
00268
00269
00270
00271 if (ACE_OS::bind (this->get_handle (),
00272 reinterpret_cast<sockaddr *> (&local_inet6_addr),
00273 sizeof local_inet6_addr) == -1)
00274 error = 1;
00275 }
00276 else
00277 #endif
00278 if (protocol_family == PF_INET)
00279 {
00280 sockaddr_in local_inet_addr;
00281 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
00282 0,
00283 sizeof local_inet_addr);
00284
00285 if (local_sap.ACE_Addr::operator== (ACE_Addr::sap_any))
00286 {
00287 local_inet_addr.sin_port = 0;
00288 }
00289 else
00290 local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 {
00330
00331
00332 size_t num_addresses = local_sap.get_num_secondary_addresses() + 1;
00333
00334
00335
00336
00337 sockaddr_in* local_inet_addrs = 0;
00338 ACE_NEW_NORETURN (local_inet_addrs,
00339 sockaddr_in[num_addresses]);
00340
00341 if (!local_inet_addrs)
00342 error = 1;
00343 else
00344 {
00345
00346
00347 local_sap.get_addresses(local_inet_addrs,
00348 num_addresses);
00349
00350 #if defined (ACE_HAS_LKSCTP)
00351
00352 sockaddr_in *local_sockaddr = 0;
00353
00354
00355 if (ACE_OS::bind (this->get_handle (),
00356 reinterpret_cast<sockaddr *> (&(local_inet_addrs[0])),
00357 sizeof(sockaddr)) == -1)
00358 {
00359 error = 1;
00360 }
00361
00362
00363 if (num_addresses > 1)
00364 {
00365 ACE_NEW_NORETURN(local_sockaddr,
00366 sockaddr_in[num_addresses - 1]);
00367
00368
00369 for (size_t i = 1; i < num_addresses; i++)
00370 {
00371 local_inet_addrs[i].sin_port = local_inet_addrs[0].sin_port;
00372 }
00373
00374
00375 for (size_t i = 0; i < num_addresses - 1; i++)
00376 {
00377 ACE_OS::memcpy(&(local_sockaddr[i]),
00378 &(local_inet_addrs[i + 1]),
00379 sizeof(sockaddr_in));
00380 }
00381
00382
00383 if (!error && sctp_bindx(this->get_handle (),
00384 reinterpret_cast<sockaddr *> (local_sockaddr),
00385 num_addresses - 1,
00386 SCTP_BINDX_ADD_ADDR))
00387 {
00388 error = 1;
00389 }
00390
00391 delete [] local_sockaddr;
00392 }
00393 #else
00394
00395 size_t name_len = (sizeof local_inet_addr) * num_addresses;
00396 if (ACE_OS::bind (this->get_handle (),
00397 reinterpret_cast<sockaddr *> (local_inet_addrs),
00398 static_cast<int> (name_len)) == -1)
00399 error = 1;
00400 #endif
00401 }
00402
00403 delete [] local_inet_addrs;
00404 }
00405 }
00406 else if (ACE_OS::bind (this->get_handle (),
00407 (sockaddr *) local_sap.get_addr (),
00408 local_sap.get_size ()) == -1)
00409 error = 1;
00410
00411 if (error != 0
00412 || ACE_OS::listen (this->get_handle (),
00413 backlog) == -1)
00414 {
00415 error = 1;
00416 this->close ();
00417 }
00418
00419 return error ? -1 : 0;
00420 }
00421
00422 int
00423 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr &local_sap,
00424 ACE_Protocol_Info *protocolinfo,
00425 ACE_SOCK_GROUP g,
00426 u_long flags,
00427 int reuse_addr,
00428 int protocol_family,
00429 int backlog,
00430 int protocol)
00431 {
00432 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
00433
00434 if (protocol_family == PF_UNSPEC)
00435 protocol_family = local_sap.get_type ();
00436
00437 #if defined (ACE_HAS_LKSCTP)
00438 if (ACE_SOCK::open (SOCK_STREAM,
00439 #else
00440 if (ACE_SOCK::open (SOCK_SEQPACKET,
00441 #endif
00442 protocol_family,
00443 protocol,
00444 protocolinfo,
00445 g,
00446 flags,
00447 reuse_addr) == -1)
00448 return -1;
00449 else
00450 return this->shared_open (local_sap,
00451 protocol_family,
00452 backlog);
00453 }
00454
00455 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr &local_sap,
00456 ACE_Protocol_Info *protocolinfo,
00457 ACE_SOCK_GROUP g,
00458 u_long flags,
00459 int reuse_addr,
00460 int protocol_family,
00461 int backlog,
00462 int protocol)
00463 {
00464 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
00465 if (this->open (local_sap,
00466 protocolinfo,
00467 g,
00468 flags,
00469 reuse_addr,
00470 protocol_family,
00471 backlog,
00472 protocol) == -1)
00473 ACE_ERROR ((LM_ERROR,
00474 ACE_LIB_TEXT ("%p\n"),
00475 ACE_LIB_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
00476 }
00477
00478
00479
00480 int
00481 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr &local_sap,
00482 int reuse_addr,
00483 int protocol_family,
00484 int backlog,
00485 int protocol)
00486 {
00487 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
00488
00489 if (local_sap != ACE_Addr::sap_any)
00490 protocol_family = local_sap.get_type ();
00491 else if (protocol_family == PF_UNSPEC)
00492 {
00493 #if defined (ACE_HAS_IPV6)
00494 protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
00495 #else
00496 protocol_family = PF_INET;
00497 #endif
00498 }
00499
00500 #if defined (ACE_HAS_LKSCTP)
00501 if (ACE_SOCK::open (SOCK_STREAM,
00502 #else
00503 if (ACE_SOCK::open (SOCK_SEQPACKET,
00504 #endif
00505 protocol_family,
00506 protocol,
00507 reuse_addr) == -1)
00508 return -1;
00509 else
00510 return this->shared_open (local_sap,
00511 protocol_family,
00512 backlog);
00513 }
00514
00515
00516
00517 int
00518 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Multihomed_INET_Addr &local_sap,
00519 int reuse_addr,
00520 int protocol_family,
00521 int backlog,
00522 int protocol)
00523 {
00524 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
00525
00526 if (local_sap.ACE_Addr::operator!= (ACE_Addr::sap_any))
00527 protocol_family = local_sap.get_type ();
00528 else if (protocol_family == PF_UNSPEC)
00529 {
00530 #if defined (ACE_HAS_IPV6)
00531 protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
00532 #else
00533 protocol_family = PF_INET;
00534 #endif
00535 }
00536
00537 #if defined (ACE_HAS_LKSCTP)
00538 if (ACE_SOCK::open (SOCK_STREAM,
00539 #else
00540 if (ACE_SOCK::open (SOCK_SEQPACKET,
00541 #endif
00542 protocol_family,
00543 protocol,
00544 reuse_addr) == -1)
00545 return -1;
00546 else
00547 return this->shared_open (local_sap,
00548 protocol_family,
00549 backlog);
00550 }
00551
00552
00553
00554 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr &local_sap,
00555 int reuse_addr,
00556 int protocol_family,
00557 int backlog,
00558 int protocol)
00559 {
00560 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
00561 if (this->open (local_sap,
00562 reuse_addr,
00563 protocol_family,
00564 backlog,
00565 protocol) == -1)
00566 ACE_ERROR ((LM_ERROR,
00567 ACE_LIB_TEXT ("%p\n"),
00568 ACE_LIB_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
00569 }
00570
00571
00572
00573 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Multihomed_INET_Addr &local_sap,
00574 int reuse_addr,
00575 int protocol_family,
00576 int backlog,
00577 int protocol)
00578 {
00579 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
00580 if (this->open (local_sap,
00581 reuse_addr,
00582 protocol_family,
00583 backlog,
00584 protocol) == -1)
00585 ACE_ERROR ((LM_ERROR,
00586 ACE_LIB_TEXT ("%p\n"),
00587 ACE_LIB_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
00588 }
00589
00590 int
00591 ACE_SOCK_SEQPACK_Acceptor::close (void)
00592 {
00593 return ACE_SOCK::close ();
00594 }
00595
00596 ACE_END_VERSIONED_NAMESPACE_DECL