SOCK_SEQPACK_Acceptor.cpp

Go to the documentation of this file.
00001 // SOCK_SEQPACK_Acceptor.cpp,v 4.17 2005/10/28 16:14:55 ossama Exp
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 /* __ACE_INLINE__ */
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 // Do nothing routine for constructor.
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 // Performs the timed accept operation.
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   // Handle the case where we're doing a timed <accept>.
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           // Set the handle into non-blocking mode if it's not already
00052           // in it.
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   // Check to see if we were originally in blocking mode, and if so,
00073   // set the <new_association>'s handle and <this> handle to be in blocking
00074   // mode.
00075   if (in_blocking_mode)
00076     {
00077       // Save/restore errno.
00078       ACE_Errno_Guard error (errno);
00079 
00080       // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
00081       // originally.
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     // Reset the event association inherited by the new handle.
00091     ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
00092 #else
00093   ACE_UNUSED_ARG (reset_new_handle);
00094 #endif /* ACE_WIN32 */
00095 
00096   return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
00097 }
00098 
00099 // General purpose routine for accepting new connections.
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       // On Win32 the third parameter to <accept> must be a NULL
00118       // pointer if we want to ignore the client's address.
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       // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
00140       // is known.
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 /* ACE_HAS_DUMP */
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       // We probably don't need a bind_port written here.
00190       // There are currently no supported OS's that define
00191       // ACE_LACKS_WILDCARD_BIND.
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 // Multihomed version of same.
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   // TODO: Add multi-address support to IPV6
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       // We probably don't need a bind_port written here.
00269       // There are currently no supported OS's that define
00270       // ACE_LACKS_WILDCARD_BIND.
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 //  A port number of 0 means that the user is requesting that the
00293 //  operating system choose an arbitrary, unused port.  Since some
00294 //  operating systems don't provide this service, ACE provides an
00295 //  emulation layer.  Therefore, the "ACE way" to bind an arbitrary,
00296 //  unused port is to call ACE:bind_port, which either
00297 //
00298 //    (1)  Calls ACE_OS::bind with port 0, if the operating system
00299 //         directly supports the automated selection, or
00300 //
00301 //    (2)  Performs more complicated logic to emulate this feature if
00302 //         it's missing from the OS.
00303 //
00304 //  The emulation logic in choice (2) is compiled if and only if
00305 //  ACE_LACKS_WILDCARD_BIND is defined at compile time.
00306 //
00307 //  Along these lines, the following block of code seems like it would
00308 //  be sufficient to support the wildcard bind operation:
00309 //
00310 //      if (local_inet_addr.sin_port == 0)
00311 //         {
00312 //           if (ACE::bind_port (this->get_handle (),
00313 //               ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
00314 //             error = 1;
00315 //
00316 //         }
00317 //      else
00318 //
00319 //  Unfortunately, this code is insufficient because it does not
00320 //  address the possibility of secondary addresses.
00321 //
00322 //  However, rather than writing the correct code now, I'm putting it
00323 //  off, because this class, ACE_SOCK_SEQPACK_Acceptor, is currently
00324 //  only used with SCTP, and ACE currently supports SCTP only through
00325 //  the OpenSS7 and LKSCTP implmentations, which are available only on
00326 //  Linux.  Linux has native support for the wildcard bind, so the
00327 //  following code works regardless of whether or not the port is 0.
00328 
00329         {
00330           // The total number of addresses is the number of secondary
00331           // addresses plus one.
00332           size_t num_addresses = local_sap.get_num_secondary_addresses() + 1;
00333 
00334           // Create an array of sockaddr_in to hold the underlying
00335           // representations of the primary and secondary
00336           // addresses.
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               // Populate the array by invoking the get_addresses method
00346               // on the Multihomed_INET_Addr
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               // bind the primary first
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               // do we need to bind multiple addresses?
00363               if (num_addresses > 1)
00364               {
00365                 ACE_NEW_NORETURN(local_sockaddr,
00366                                sockaddr_in[num_addresses - 1]);
00367 
00368                 // all of the secondary addresses need the local port set
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                 // copy only the sockaddrs that we need to bindx
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                 // now call bindx
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               // Call bind
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 /* ACE_HAS_LKSCTP */
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 // General purpose routine for performing server ACE_SOCK creation.
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 /* ACE_HAS_IPV6 */
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 // Multihomed version of same.
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 /* ACE_HAS_IPV6 */
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 // General purpose routine for performing server ACE_SOCK creation.
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 // Multihomed version of same.
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

Generated on Thu Nov 9 09:42:04 2006 for ACE by doxygen 1.3.6