1 #include "ace/SOCK_SEQPACK_Acceptor.h"
3 #include "ace/Log_Category.h"
4 #include "ace/OS_Memory.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_sys_socket.h"
7 #include "ace/os_include/os_fcntl.h"
9 #if !defined (__ACE_INLINE__)
10 #include "ace/SOCK_SEQPACK_Acceptor.inl"
11 #endif /* __ACE_INLINE__ */
13 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
15 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_SEQPACK_Acceptor
)
17 // Do nothing routine for constructor.
18 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor ()
20 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
23 // Performs the timed accept operation.
25 ACE_SOCK_SEQPACK_Acceptor::shared_accept_start (ACE_Time_Value
*timeout
,
27 int &in_blocking_mode
) const
29 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_start");
31 ACE_HANDLE handle
= this->get_handle ();
33 // Handle the case where we're doing a timed <accept>.
36 if (ACE::handle_timed_accept (handle
,
42 in_blocking_mode
= ACE_BIT_DISABLED (ACE::get_flags (handle
),
44 // Set the handle into non-blocking mode if it's not already
47 && ACE::set_flags (handle
,
57 ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish (ACE_SOCK_SEQPACK_Association new_association
,
59 bool reset_new_handle
) const
61 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish ()");
63 ACE_HANDLE new_handle
= new_association
.get_handle ();
65 // Check to see if we were originally in blocking mode, and if so,
66 // set the <new_association>'s handle and <this> handle to be in blocking
70 // Save/restore errno.
71 ACE_Errno_Guard
error (errno
);
73 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
75 ACE::clr_flags (this->get_handle (),
77 ACE::clr_flags (new_handle
,
81 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
83 // Reset the event association inherited by the new handle.
84 ::WSAEventSelect ((SOCKET
) new_handle
, 0, 0);
86 ACE_UNUSED_ARG (reset_new_handle
);
87 #endif /* ACE_WIN32 */
89 return new_handle
== ACE_INVALID_HANDLE
? -1 : 0;
92 // General purpose routine for accepting new connections.
94 ACE_SOCK_SEQPACK_Acceptor::accept (ACE_SOCK_SEQPACK_Association
&new_association
,
95 ACE_Addr
*remote_addr
,
96 ACE_Time_Value
*timeout
,
98 bool reset_new_handle
) const
100 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::accept");
102 int in_blocking_mode
= 0;
103 if (this->shared_accept_start (timeout
,
105 in_blocking_mode
) == -1)
109 // On Win32 the third parameter to <accept> must be a NULL
110 // pointer if we want to ignore the client's address.
115 if (remote_addr
!= 0)
117 len
= remote_addr
->get_size ();
119 addr
= (sockaddr
*) remote_addr
->get_addr ();
123 new_association
.set_handle (ACE_OS::accept (this->get_handle (),
126 while (new_association
.get_handle () == ACE_INVALID_HANDLE
131 // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
133 if (new_association
.get_handle () != ACE_INVALID_HANDLE
136 remote_addr
->set_size (len
);
137 remote_addr
->set_type (addr
->sa_family
);
141 return this->shared_accept_finish (new_association
,
147 ACE_SOCK_SEQPACK_Acceptor::dump () const
149 #if defined (ACE_HAS_DUMP)
150 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::dump");
151 #endif /* ACE_HAS_DUMP */
155 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Addr
&local_sap
,
159 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
162 #if defined (ACE_HAS_IPV6)
163 ACE_ASSERT (protocol_family
== PF_INET
|| protocol_family
== PF_INET6
);
165 if (protocol_family
== PF_INET6
)
167 sockaddr_in6 local_inet6_addr
;
168 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr
),
170 sizeof local_inet6_addr
);
172 if (local_sap
== ACE_Addr::sap_any
)
174 local_inet6_addr
.sin6_family
= AF_INET6
;
175 local_inet6_addr
.sin6_port
= 0;
176 local_inet6_addr
.sin6_addr
= in6addr_any
;
179 local_inet6_addr
= *reinterpret_cast<sockaddr_in6
*> (local_sap
.get_addr ());
181 // We probably don't need a bind_port written here.
182 // There are currently no supported OS's that define
183 // ACE_LACKS_WILDCARD_BIND.
184 if (ACE_OS::bind (this->get_handle (),
185 reinterpret_cast<sockaddr
*> (&local_inet6_addr
),
186 sizeof local_inet6_addr
) == -1)
191 if (protocol_family
== PF_INET
)
193 sockaddr_in local_inet_addr
;
194 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr
),
196 sizeof local_inet_addr
);
198 if (local_sap
== ACE_Addr::sap_any
)
200 local_inet_addr
.sin_port
= 0;
203 local_inet_addr
= *reinterpret_cast<sockaddr_in
*> (local_sap
.get_addr ());
204 if (local_inet_addr
.sin_port
== 0)
206 if (ACE::bind_port (this->get_handle ()) == -1)
209 else if (ACE_OS::bind (this->get_handle (),
210 reinterpret_cast<sockaddr
*> (&local_inet_addr
),
211 sizeof local_inet_addr
) == -1)
214 else if (ACE_OS::bind (this->get_handle (),
215 (sockaddr
*) local_sap
.get_addr (),
216 local_sap
.get_size ()) == -1)
220 || ACE_OS::listen (this->get_handle (),
227 return error
? -1 : 0;
230 // Multihomed version of same.
233 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Multihomed_INET_Addr
&local_sap
,
237 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
240 // TODO: Add multi-address support to IPV6
241 #if defined (ACE_HAS_IPV6)
242 ACE_ASSERT (protocol_family
== PF_INET
|| protocol_family
== PF_INET6
);
244 if (protocol_family
== PF_INET6
)
246 sockaddr_in6 local_inet6_addr
;
247 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr
),
249 sizeof local_inet6_addr
);
251 if (local_sap
.ACE_Addr::operator== (ACE_Addr::sap_any
))
253 local_inet6_addr
.sin6_family
= AF_INET6
;
254 local_inet6_addr
.sin6_port
= 0;
255 local_inet6_addr
.sin6_addr
= in6addr_any
;
258 local_inet6_addr
= *reinterpret_cast<sockaddr_in6
*> (local_sap
.get_addr ());
260 // We probably don't need a bind_port written here.
261 // There are currently no supported OS's that define
262 // ACE_LACKS_WILDCARD_BIND.
263 if (ACE_OS::bind (this->get_handle (),
264 reinterpret_cast<sockaddr
*> (&local_inet6_addr
),
265 sizeof local_inet6_addr
) == -1)
270 if (protocol_family
== PF_INET
)
272 sockaddr_in local_inet_addr
;
273 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr
),
275 sizeof local_inet_addr
);
277 if (local_sap
.ACE_Addr::operator== (ACE_Addr::sap_any
))
279 local_inet_addr
.sin_port
= 0;
283 local_inet_addr
= *reinterpret_cast<sockaddr_in
*> (local_sap
.get_addr ());
286 // A port number of 0 means that the user is requesting that the
287 // operating system choose an arbitrary, unused port. Since some
288 // operating systems don't provide this service, ACE provides an
289 // emulation layer. Therefore, the "ACE way" to bind an arbitrary,
290 // unused port is to call ACE:bind_port, which either
292 // (1) Calls ACE_OS::bind with port 0, if the operating system
293 // directly supports the automated selection, or
295 // (2) Performs more complicated logic to emulate this feature if
296 // it's missing from the OS.
298 // The emulation logic in choice (2) is compiled if and only if
299 // ACE_LACKS_WILDCARD_BIND is defined at compile time.
301 // Along these lines, the following block of code seems like it would
302 // be sufficient to support the wildcard bind operation:
304 // if (local_inet_addr.sin_port == 0)
306 // if (ACE::bind_port (this->get_handle (),
307 // ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
313 // Unfortunately, this code is insufficient because it does not
314 // address the possibility of secondary addresses.
316 // However, rather than writing the correct code now, I'm putting it
317 // off, because this class, ACE_SOCK_SEQPACK_Acceptor, is currently
318 // only used with SCTP, and ACE currently supports SCTP only through
319 // the OpenSS7 and LKSCTP implmentations, which are available only on
320 // Linux. Linux has native support for the wildcard bind, so the
321 // following code works regardless of whether or not the port is 0.
324 // The total number of addresses is the number of secondary
325 // addresses plus one.
326 size_t num_addresses
= local_sap
.get_num_secondary_addresses() + 1;
328 // Create an array of sockaddr_in to hold the underlying
329 // representations of the primary and secondary
331 sockaddr_in
* local_inet_addrs
= 0;
332 #if defined(ACE_HAS_ALLOC_HOOKS)
333 ACE_ALLOCATOR_NORETURN (local_inet_addrs
,
334 static_cast<sockaddr_in
*>(ACE_Allocator::instance()->malloc(sizeof(sockaddr_in
) * num_addresses
)));
336 ACE_NEW_NORETURN (local_inet_addrs
,
337 sockaddr_in
[num_addresses
]);
340 if (!local_inet_addrs
)
344 // Populate the array by invoking the get_addresses method
345 // on the Multihomed_INET_Addr
346 local_sap
.get_addresses(local_inet_addrs
,
349 #if defined (ACE_HAS_LKSCTP)
351 sockaddr_in
*local_sockaddr
= 0;
353 // bind the primary first
354 if (ACE_OS::bind (this->get_handle (),
355 reinterpret_cast<sockaddr
*> (&(local_inet_addrs
[0])),
356 sizeof(sockaddr
)) == -1)
361 // do we need to bind multiple addresses?
362 if (num_addresses
> 1)
364 ACE_NEW_NORETURN(local_sockaddr
,
365 sockaddr_in
[num_addresses
- 1]);
367 // all of the secondary addresses need the local port set
368 for (size_t i
= 1; i
< num_addresses
; i
++)
370 local_inet_addrs
[i
].sin_port
= local_inet_addrs
[0].sin_port
;
373 // copy only the sockaddrs that we need to bindx
374 for (size_t i
= 0; i
< num_addresses
- 1; i
++)
376 ACE_OS::memcpy(&(local_sockaddr
[i
]),
377 &(local_inet_addrs
[i
+ 1]),
378 sizeof(sockaddr_in
));
382 if (!error
&& sctp_bindx(this->get_handle (),
383 reinterpret_cast<sockaddr
*> (local_sockaddr
),
385 SCTP_BINDX_ADD_ADDR
))
390 delete [] local_sockaddr
;
394 size_t name_len
= (sizeof local_inet_addr
) * num_addresses
;
395 if (ACE_OS::bind (this->get_handle (),
396 reinterpret_cast<sockaddr
*> (local_inet_addrs
),
397 static_cast<int> (name_len
)) == -1)
399 #endif /* ACE_HAS_LKSCTP */
402 #if defined (ACE_HAS_ALLOC_HOOKS)
403 ACE_Allocator::instance()->free(local_inet_addrs
);
405 delete [] local_inet_addrs
;
406 #endif /* ACE_HAS_ALLOC_HOOKS */
409 else if (ACE_OS::bind (this->get_handle (),
410 (sockaddr
*) local_sap
.get_addr (),
411 local_sap
.get_size ()) == -1)
414 if (error
!= 0 || ACE_OS::listen (this->get_handle (), backlog
) == -1)
420 return error
? -1 : 0;
424 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr
&local_sap
,
425 ACE_Protocol_Info
*protocolinfo
,
433 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
435 if (protocol_family
== PF_UNSPEC
)
436 protocol_family
= local_sap
.get_type ();
438 #if defined (ACE_HAS_LKSCTP)
439 if (ACE_SOCK::open (SOCK_STREAM
,
441 if (ACE_SOCK::open (SOCK_SEQPACKET
,
451 return this->shared_open (local_sap
, protocol_family
, backlog
);
454 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr
&local_sap
,
455 ACE_Protocol_Info
*protocolinfo
,
463 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
464 if (this->open (local_sap
,
472 ACELIB_ERROR ((LM_ERROR
,
474 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
477 // General purpose routine for performing server ACE_SOCK creation.
480 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr
&local_sap
,
486 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
488 if (local_sap
!= ACE_Addr::sap_any
)
489 protocol_family
= local_sap
.get_type ();
490 else if (protocol_family
== PF_UNSPEC
)
492 #if defined (ACE_HAS_IPV6)
493 protocol_family
= ACE::ipv6_enabled () ? PF_INET6
: PF_INET
;
495 protocol_family
= PF_INET
;
496 #endif /* ACE_HAS_IPV6 */
499 #if defined (ACE_HAS_LKSCTP)
500 if (ACE_SOCK::open (SOCK_STREAM
,
502 if (ACE_SOCK::open (SOCK_SEQPACKET
,
509 return this->shared_open (local_sap
,
514 // Multihomed version of same.
517 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Multihomed_INET_Addr
&local_sap
,
523 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
525 if (local_sap
.ACE_Addr::operator!= (ACE_Addr::sap_any
))
526 protocol_family
= local_sap
.get_type ();
527 else if (protocol_family
== PF_UNSPEC
)
529 #if defined (ACE_HAS_IPV6)
530 protocol_family
= ACE::ipv6_enabled () ? PF_INET6
: PF_INET
;
532 protocol_family
= PF_INET
;
533 #endif /* ACE_HAS_IPV6 */
536 #if defined (ACE_HAS_LKSCTP)
537 if (ACE_SOCK::open (SOCK_STREAM
,
539 if (ACE_SOCK::open (SOCK_SEQPACKET
,
546 return this->shared_open (local_sap
,
551 // General purpose routine for performing server ACE_SOCK creation.
553 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr
&local_sap
,
559 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
560 if (this->open (local_sap
,
565 ACELIB_ERROR ((LM_ERROR
,
567 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
570 // Multihomed version of same.
572 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Multihomed_INET_Addr
&local_sap
,
578 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
579 if (this->open (local_sap
,
584 ACELIB_ERROR ((LM_ERROR
,
586 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
590 ACE_SOCK_SEQPACK_Acceptor::close ()
592 return ACE_SOCK::close ();
595 ACE_END_VERSIONED_NAMESPACE_DECL