Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / SOCK_SEQPACK_Acceptor.cpp
blob98e368550f53939fa848f44cc5aac3255724f69a
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.
24 int
25 ACE_SOCK_SEQPACK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
26 bool restart,
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>.
34 if (timeout != 0)
36 if (ACE::handle_timed_accept (handle,
37 timeout,
38 restart) == -1)
39 return -1;
40 else
42 in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
43 ACE_NONBLOCK);
44 // Set the handle into non-blocking mode if it's not already
45 // in it.
46 if (in_blocking_mode
47 && ACE::set_flags (handle,
48 ACE_NONBLOCK) == -1)
49 return -1;
53 return 0;
56 int
57 ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish (ACE_SOCK_SEQPACK_Association new_association,
58 int in_blocking_mode,
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
67 // mode.
68 if (in_blocking_mode)
70 // Save/restore errno.
71 ACE_Errno_Guard error (errno);
73 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
74 // originally.
75 ACE::clr_flags (this->get_handle (),
76 ACE_NONBLOCK);
77 ACE::clr_flags (new_handle,
78 ACE_NONBLOCK);
81 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
82 if (reset_new_handle)
83 // Reset the event association inherited by the new handle.
84 ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
85 #else
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.
93 int
94 ACE_SOCK_SEQPACK_Acceptor::accept (ACE_SOCK_SEQPACK_Association &new_association,
95 ACE_Addr *remote_addr,
96 ACE_Time_Value *timeout,
97 bool restart,
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,
104 restart,
105 in_blocking_mode) == -1)
106 return -1;
107 else
109 // On Win32 the third parameter to <accept> must be a NULL
110 // pointer if we want to ignore the client's address.
111 int *len_ptr = 0;
112 sockaddr *addr = 0;
113 int len = 0;
115 if (remote_addr != 0)
117 len = remote_addr->get_size ();
118 len_ptr = &len;
119 addr = (sockaddr *) remote_addr->get_addr ();
123 new_association.set_handle (ACE_OS::accept (this->get_handle (),
124 addr,
125 len_ptr));
126 while (new_association.get_handle () == ACE_INVALID_HANDLE
127 && restart != 0
128 && errno == EINTR
129 && timeout == 0);
131 // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
132 // is known.
133 if (new_association.get_handle () != ACE_INVALID_HANDLE
134 && remote_addr != 0)
136 remote_addr->set_size (len);
137 remote_addr->set_type (addr->sa_family);
141 return this->shared_accept_finish (new_association,
142 in_blocking_mode,
143 reset_new_handle);
146 void
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,
156 int protocol_family,
157 int backlog)
159 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
160 int error = 0;
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;
178 else
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)
187 error = 1;
189 else
190 #endif
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;
202 else
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)
207 error = 1;
209 else if (ACE_OS::bind (this->get_handle (),
210 reinterpret_cast<sockaddr *> (&local_inet_addr),
211 sizeof local_inet_addr) == -1)
212 error = 1;
214 else if (ACE_OS::bind (this->get_handle (),
215 (sockaddr *) local_sap.get_addr (),
216 local_sap.get_size ()) == -1)
217 error = 1;
219 if (error != 0
220 || ACE_OS::listen (this->get_handle (),
221 backlog) == -1)
223 error = 1;
224 this->close ();
227 return error ? -1 : 0;
230 // Multihomed version of same.
233 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Multihomed_INET_Addr &local_sap,
234 int protocol_family,
235 int backlog)
237 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
238 int error = 0;
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;
257 else
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)
266 error = 1;
268 else
269 #endif
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;
281 else
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)
305 // {
306 // if (ACE::bind_port (this->get_handle (),
307 // ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
308 // error = 1;
310 // }
311 // else
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
330 // addresses.
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)));
335 #else
336 ACE_NEW_NORETURN (local_inet_addrs,
337 sockaddr_in[num_addresses]);
338 #endif
340 if (!local_inet_addrs)
341 error = 1;
342 else
344 // Populate the array by invoking the get_addresses method
345 // on the Multihomed_INET_Addr
346 local_sap.get_addresses(local_inet_addrs,
347 num_addresses);
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)
358 error = 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));
381 // now call bindx
382 if (!error && sctp_bindx(this->get_handle (),
383 reinterpret_cast<sockaddr *> (local_sockaddr),
384 num_addresses - 1,
385 SCTP_BINDX_ADD_ADDR))
387 error = 1;
390 delete [] local_sockaddr;
392 #else
393 // Call bind
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)
398 error = 1;
399 #endif /* ACE_HAS_LKSCTP */
402 #if defined (ACE_HAS_ALLOC_HOOKS)
403 ACE_Allocator::instance()->free(local_inet_addrs);
404 #else
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)
412 error = 1;
414 if (error != 0 || ACE_OS::listen (this->get_handle (), backlog) == -1)
416 error = 1;
417 this->close ();
420 return error ? -1 : 0;
424 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr &local_sap,
425 ACE_Protocol_Info *protocolinfo,
426 ACE_SOCK_GROUP g,
427 u_long flags,
428 int reuse_addr,
429 int protocol_family,
430 int backlog,
431 int protocol)
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,
440 #else
441 if (ACE_SOCK::open (SOCK_SEQPACKET,
442 #endif
443 protocol_family,
444 protocol,
445 protocolinfo,
447 flags,
448 reuse_addr) == -1)
449 return -1;
450 else
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,
456 ACE_SOCK_GROUP g,
457 u_long flags,
458 int reuse_addr,
459 int protocol_family,
460 int backlog,
461 int protocol)
463 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
464 if (this->open (local_sap,
465 protocolinfo,
467 flags,
468 reuse_addr,
469 protocol_family,
470 backlog,
471 protocol) == -1)
472 ACELIB_ERROR ((LM_ERROR,
473 ACE_TEXT ("%p\n"),
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,
481 int reuse_addr,
482 int protocol_family,
483 int backlog,
484 int protocol)
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;
494 #else
495 protocol_family = PF_INET;
496 #endif /* ACE_HAS_IPV6 */
499 #if defined (ACE_HAS_LKSCTP)
500 if (ACE_SOCK::open (SOCK_STREAM,
501 #else
502 if (ACE_SOCK::open (SOCK_SEQPACKET,
503 #endif
504 protocol_family,
505 protocol,
506 reuse_addr) == -1)
507 return -1;
508 else
509 return this->shared_open (local_sap,
510 protocol_family,
511 backlog);
514 // Multihomed version of same.
517 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Multihomed_INET_Addr &local_sap,
518 int reuse_addr,
519 int protocol_family,
520 int backlog,
521 int protocol)
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;
531 #else
532 protocol_family = PF_INET;
533 #endif /* ACE_HAS_IPV6 */
536 #if defined (ACE_HAS_LKSCTP)
537 if (ACE_SOCK::open (SOCK_STREAM,
538 #else
539 if (ACE_SOCK::open (SOCK_SEQPACKET,
540 #endif
541 protocol_family,
542 protocol,
543 reuse_addr) == -1)
544 return -1;
545 else
546 return this->shared_open (local_sap,
547 protocol_family,
548 backlog);
551 // General purpose routine for performing server ACE_SOCK creation.
553 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr &local_sap,
554 int reuse_addr,
555 int protocol_family,
556 int backlog,
557 int protocol)
559 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
560 if (this->open (local_sap,
561 reuse_addr,
562 protocol_family,
563 backlog,
564 protocol) == -1)
565 ACELIB_ERROR ((LM_ERROR,
566 ACE_TEXT ("%p\n"),
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,
573 int reuse_addr,
574 int protocol_family,
575 int backlog,
576 int protocol)
578 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
579 if (this->open (local_sap,
580 reuse_addr,
581 protocol_family,
582 backlog,
583 protocol) == -1)
584 ACELIB_ERROR ((LM_ERROR,
585 ACE_TEXT ("%p\n"),
586 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
590 ACE_SOCK_SEQPACK_Acceptor::close ()
592 return ACE_SOCK::close ();
595 ACE_END_VERSIONED_NAMESPACE_DECL