Also use Objects as part of an operation but as a result don't generate Any operation...
[ACE_TAO.git] / ACE / ace / SOCK_SEQPACK_Acceptor.cpp
blobfc1bc669b228004a44bb82889d959322ea3d4857
1 #include "ace/SOCK_SEQPACK_Acceptor.h"
3 #include "ace/Auto_Ptr.h"
4 #include "ace/Log_Category.h"
5 #include "ace/OS_Memory.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/OS_NS_sys_socket.h"
8 #include "ace/os_include/os_fcntl.h"
10 #if !defined (__ACE_INLINE__)
11 #include "ace/SOCK_SEQPACK_Acceptor.inl"
12 #endif /* __ACE_INLINE__ */
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
18 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_SEQPACK_Acceptor)
20 // Do nothing routine for constructor.
22 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (void)
24 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
27 // Performs the timed accept operation.
29 int
30 ACE_SOCK_SEQPACK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
31 bool restart,
32 int &in_blocking_mode) const
34 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_start");
36 ACE_HANDLE handle = this->get_handle ();
38 // Handle the case where we're doing a timed <accept>.
39 if (timeout != 0)
41 if (ACE::handle_timed_accept (handle,
42 timeout,
43 restart) == -1)
44 return -1;
45 else
47 in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
48 ACE_NONBLOCK);
49 // Set the handle into non-blocking mode if it's not already
50 // in it.
51 if (in_blocking_mode
52 && ACE::set_flags (handle,
53 ACE_NONBLOCK) == -1)
54 return -1;
58 return 0;
61 int
62 ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish (ACE_SOCK_SEQPACK_Association new_association,
63 int in_blocking_mode,
64 bool reset_new_handle) const
66 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish ()");
68 ACE_HANDLE new_handle = new_association.get_handle ();
70 // Check to see if we were originally in blocking mode, and if so,
71 // set the <new_association>'s handle and <this> handle to be in blocking
72 // mode.
73 if (in_blocking_mode)
75 // Save/restore errno.
76 ACE_Errno_Guard error (errno);
78 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
79 // originally.
80 ACE::clr_flags (this->get_handle (),
81 ACE_NONBLOCK);
82 ACE::clr_flags (new_handle,
83 ACE_NONBLOCK);
86 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
87 if (reset_new_handle)
88 // Reset the event association inherited by the new handle.
89 ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
90 #else
91 ACE_UNUSED_ARG (reset_new_handle);
92 #endif /* ACE_WIN32 */
94 return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
97 // General purpose routine for accepting new connections.
99 int
100 ACE_SOCK_SEQPACK_Acceptor::accept (ACE_SOCK_SEQPACK_Association &new_association,
101 ACE_Addr *remote_addr,
102 ACE_Time_Value *timeout,
103 bool restart,
104 bool reset_new_handle) const
106 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::accept");
108 int in_blocking_mode = 0;
109 if (this->shared_accept_start (timeout,
110 restart,
111 in_blocking_mode) == -1)
112 return -1;
113 else
115 // On Win32 the third parameter to <accept> must be a NULL
116 // pointer if we want to ignore the client's address.
117 int *len_ptr = 0;
118 sockaddr *addr = 0;
119 int len = 0;
121 if (remote_addr != 0)
123 len = remote_addr->get_size ();
124 len_ptr = &len;
125 addr = (sockaddr *) remote_addr->get_addr ();
129 new_association.set_handle (ACE_OS::accept (this->get_handle (),
130 addr,
131 len_ptr));
132 while (new_association.get_handle () == ACE_INVALID_HANDLE
133 && restart != 0
134 && errno == EINTR
135 && timeout == 0);
137 // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
138 // is known.
139 if (new_association.get_handle () != ACE_INVALID_HANDLE
140 && remote_addr != 0)
142 remote_addr->set_size (len);
143 remote_addr->set_type (addr->sa_family);
147 return this->shared_accept_finish (new_association,
148 in_blocking_mode,
149 reset_new_handle);
152 void
153 ACE_SOCK_SEQPACK_Acceptor::dump (void) const
155 #if defined (ACE_HAS_DUMP)
156 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::dump");
157 #endif /* ACE_HAS_DUMP */
161 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Addr &local_sap,
162 int protocol_family,
163 int backlog)
165 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
166 int error = 0;
168 #if defined (ACE_HAS_IPV6)
169 ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
171 if (protocol_family == PF_INET6)
173 sockaddr_in6 local_inet6_addr;
174 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
176 sizeof local_inet6_addr);
178 if (local_sap == ACE_Addr::sap_any)
180 local_inet6_addr.sin6_family = AF_INET6;
181 local_inet6_addr.sin6_port = 0;
182 local_inet6_addr.sin6_addr = in6addr_any;
184 else
185 local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
187 // We probably don't need a bind_port written here.
188 // There are currently no supported OS's that define
189 // ACE_LACKS_WILDCARD_BIND.
190 if (ACE_OS::bind (this->get_handle (),
191 reinterpret_cast<sockaddr *> (&local_inet6_addr),
192 sizeof local_inet6_addr) == -1)
193 error = 1;
195 else
196 #endif
197 if (protocol_family == PF_INET)
199 sockaddr_in local_inet_addr;
200 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
202 sizeof local_inet_addr);
204 if (local_sap == ACE_Addr::sap_any)
206 local_inet_addr.sin_port = 0;
208 else
209 local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
210 if (local_inet_addr.sin_port == 0)
212 if (ACE::bind_port (this->get_handle ()) == -1)
213 error = 1;
215 else if (ACE_OS::bind (this->get_handle (),
216 reinterpret_cast<sockaddr *> (&local_inet_addr),
217 sizeof local_inet_addr) == -1)
218 error = 1;
220 else if (ACE_OS::bind (this->get_handle (),
221 (sockaddr *) local_sap.get_addr (),
222 local_sap.get_size ()) == -1)
223 error = 1;
225 if (error != 0
226 || ACE_OS::listen (this->get_handle (),
227 backlog) == -1)
229 error = 1;
230 this->close ();
233 return error ? -1 : 0;
236 // Multihomed version of same.
239 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Multihomed_INET_Addr &local_sap,
240 int protocol_family,
241 int backlog)
243 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
244 int error = 0;
246 // TODO: Add multi-address support to IPV6
247 #if defined (ACE_HAS_IPV6)
248 ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
250 if (protocol_family == PF_INET6)
252 sockaddr_in6 local_inet6_addr;
253 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
255 sizeof local_inet6_addr);
257 if (local_sap.ACE_Addr::operator== (ACE_Addr::sap_any))
259 local_inet6_addr.sin6_family = AF_INET6;
260 local_inet6_addr.sin6_port = 0;
261 local_inet6_addr.sin6_addr = in6addr_any;
263 else
264 local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
266 // We probably don't need a bind_port written here.
267 // There are currently no supported OS's that define
268 // ACE_LACKS_WILDCARD_BIND.
269 if (ACE_OS::bind (this->get_handle (),
270 reinterpret_cast<sockaddr *> (&local_inet6_addr),
271 sizeof local_inet6_addr) == -1)
272 error = 1;
274 else
275 #endif
276 if (protocol_family == PF_INET)
278 sockaddr_in local_inet_addr;
279 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
281 sizeof local_inet_addr);
283 if (local_sap.ACE_Addr::operator== (ACE_Addr::sap_any))
285 local_inet_addr.sin_port = 0;
287 else
288 local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
290 // A port number of 0 means that the user is requesting that the
291 // operating system choose an arbitrary, unused port. Since some
292 // operating systems don't provide this service, ACE provides an
293 // emulation layer. Therefore, the "ACE way" to bind an arbitrary,
294 // unused port is to call ACE:bind_port, which either
296 // (1) Calls ACE_OS::bind with port 0, if the operating system
297 // directly supports the automated selection, or
299 // (2) Performs more complicated logic to emulate this feature if
300 // it's missing from the OS.
302 // The emulation logic in choice (2) is compiled if and only if
303 // ACE_LACKS_WILDCARD_BIND is defined at compile time.
305 // Along these lines, the following block of code seems like it would
306 // be sufficient to support the wildcard bind operation:
308 // if (local_inet_addr.sin_port == 0)
309 // {
310 // if (ACE::bind_port (this->get_handle (),
311 // ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
312 // error = 1;
314 // }
315 // else
317 // Unfortunately, this code is insufficient because it does not
318 // address the possibility of secondary addresses.
320 // However, rather than writing the correct code now, I'm putting it
321 // off, because this class, ACE_SOCK_SEQPACK_Acceptor, is currently
322 // only used with SCTP, and ACE currently supports SCTP only through
323 // the OpenSS7 and LKSCTP implmentations, which are available only on
324 // Linux. Linux has native support for the wildcard bind, so the
325 // following code works regardless of whether or not the port is 0.
328 // The total number of addresses is the number of secondary
329 // addresses plus one.
330 size_t num_addresses = local_sap.get_num_secondary_addresses() + 1;
332 // Create an array of sockaddr_in to hold the underlying
333 // representations of the primary and secondary
334 // addresses.
335 sockaddr_in* local_inet_addrs = 0;
336 #if defined(ACE_HAS_ALLOC_HOOKS)
337 ACE_ALLOCATOR_NORETURN (local_inet_addrs,
338 static_cast<sockaddr_in*>(ACE_Allocator::instance()->malloc(sizeof(sockaddr_in) * num_addresses)));
339 #else
340 ACE_NEW_NORETURN (local_inet_addrs,
341 sockaddr_in[num_addresses]);
342 #endif
344 if (!local_inet_addrs)
345 error = 1;
346 else
348 // Populate the array by invoking the get_addresses method
349 // on the Multihomed_INET_Addr
350 local_sap.get_addresses(local_inet_addrs,
351 num_addresses);
353 #if defined (ACE_HAS_LKSCTP)
355 sockaddr_in *local_sockaddr = 0;
357 // bind the primary first
358 if (ACE_OS::bind (this->get_handle (),
359 reinterpret_cast<sockaddr *> (&(local_inet_addrs[0])),
360 sizeof(sockaddr)) == -1)
362 error = 1;
365 // do we need to bind multiple addresses?
366 if (num_addresses > 1)
368 ACE_NEW_NORETURN(local_sockaddr,
369 sockaddr_in[num_addresses - 1]);
371 // all of the secondary addresses need the local port set
372 for (size_t i = 1; i < num_addresses; i++)
374 local_inet_addrs[i].sin_port = local_inet_addrs[0].sin_port;
377 // copy only the sockaddrs that we need to bindx
378 for (size_t i = 0; i < num_addresses - 1; i++)
380 ACE_OS::memcpy(&(local_sockaddr[i]),
381 &(local_inet_addrs[i + 1]),
382 sizeof(sockaddr_in));
385 // now call bindx
386 if (!error && sctp_bindx(this->get_handle (),
387 reinterpret_cast<sockaddr *> (local_sockaddr),
388 num_addresses - 1,
389 SCTP_BINDX_ADD_ADDR))
391 error = 1;
394 delete [] local_sockaddr;
396 #else
397 // Call bind
398 size_t name_len = (sizeof local_inet_addr) * num_addresses;
399 if (ACE_OS::bind (this->get_handle (),
400 reinterpret_cast<sockaddr *> (local_inet_addrs),
401 static_cast<int> (name_len)) == -1)
402 error = 1;
403 #endif /* ACE_HAS_LKSCTP */
406 #if defined (ACE_HAS_ALLOC_HOOKS)
407 ACE_Allocator::instance()->free(local_inet_addrs);
408 #else
409 delete [] local_inet_addrs;
410 #endif /* ACE_HAS_ALLOC_HOOKS */
413 else if (ACE_OS::bind (this->get_handle (),
414 (sockaddr *) local_sap.get_addr (),
415 local_sap.get_size ()) == -1)
416 error = 1;
418 if (error != 0
419 || ACE_OS::listen (this->get_handle (),
420 backlog) == -1)
422 error = 1;
423 this->close ();
426 return error ? -1 : 0;
430 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr &local_sap,
431 ACE_Protocol_Info *protocolinfo,
432 ACE_SOCK_GROUP g,
433 u_long flags,
434 int reuse_addr,
435 int protocol_family,
436 int backlog,
437 int protocol)
439 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
441 if (protocol_family == PF_UNSPEC)
442 protocol_family = local_sap.get_type ();
444 #if defined (ACE_HAS_LKSCTP)
445 if (ACE_SOCK::open (SOCK_STREAM,
446 #else
447 if (ACE_SOCK::open (SOCK_SEQPACKET,
448 #endif
449 protocol_family,
450 protocol,
451 protocolinfo,
453 flags,
454 reuse_addr) == -1)
455 return -1;
456 else
457 return this->shared_open (local_sap,
458 protocol_family,
459 backlog);
462 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr &local_sap,
463 ACE_Protocol_Info *protocolinfo,
464 ACE_SOCK_GROUP g,
465 u_long flags,
466 int reuse_addr,
467 int protocol_family,
468 int backlog,
469 int protocol)
471 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
472 if (this->open (local_sap,
473 protocolinfo,
475 flags,
476 reuse_addr,
477 protocol_family,
478 backlog,
479 protocol) == -1)
480 ACELIB_ERROR ((LM_ERROR,
481 ACE_TEXT ("%p\n"),
482 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
485 // General purpose routine for performing server ACE_SOCK creation.
488 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr &local_sap,
489 int reuse_addr,
490 int protocol_family,
491 int backlog,
492 int protocol)
494 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
496 if (local_sap != ACE_Addr::sap_any)
497 protocol_family = local_sap.get_type ();
498 else if (protocol_family == PF_UNSPEC)
500 #if defined (ACE_HAS_IPV6)
501 protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
502 #else
503 protocol_family = PF_INET;
504 #endif /* ACE_HAS_IPV6 */
507 #if defined (ACE_HAS_LKSCTP)
508 if (ACE_SOCK::open (SOCK_STREAM,
509 #else
510 if (ACE_SOCK::open (SOCK_SEQPACKET,
511 #endif
512 protocol_family,
513 protocol,
514 reuse_addr) == -1)
515 return -1;
516 else
517 return this->shared_open (local_sap,
518 protocol_family,
519 backlog);
522 // Multihomed version of same.
525 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Multihomed_INET_Addr &local_sap,
526 int reuse_addr,
527 int protocol_family,
528 int backlog,
529 int protocol)
531 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
533 if (local_sap.ACE_Addr::operator!= (ACE_Addr::sap_any))
534 protocol_family = local_sap.get_type ();
535 else if (protocol_family == PF_UNSPEC)
537 #if defined (ACE_HAS_IPV6)
538 protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
539 #else
540 protocol_family = PF_INET;
541 #endif /* ACE_HAS_IPV6 */
544 #if defined (ACE_HAS_LKSCTP)
545 if (ACE_SOCK::open (SOCK_STREAM,
546 #else
547 if (ACE_SOCK::open (SOCK_SEQPACKET,
548 #endif
549 protocol_family,
550 protocol,
551 reuse_addr) == -1)
552 return -1;
553 else
554 return this->shared_open (local_sap,
555 protocol_family,
556 backlog);
559 // General purpose routine for performing server ACE_SOCK creation.
561 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr &local_sap,
562 int reuse_addr,
563 int protocol_family,
564 int backlog,
565 int protocol)
567 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
568 if (this->open (local_sap,
569 reuse_addr,
570 protocol_family,
571 backlog,
572 protocol) == -1)
573 ACELIB_ERROR ((LM_ERROR,
574 ACE_TEXT ("%p\n"),
575 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
578 // Multihomed version of same.
580 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Multihomed_INET_Addr &local_sap,
581 int reuse_addr,
582 int protocol_family,
583 int backlog,
584 int protocol)
586 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
587 if (this->open (local_sap,
588 reuse_addr,
589 protocol_family,
590 backlog,
591 protocol) == -1)
592 ACELIB_ERROR ((LM_ERROR,
593 ACE_TEXT ("%p\n"),
594 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
598 ACE_SOCK_SEQPACK_Acceptor::close (void)
600 return ACE_SOCK::close ();
603 ACE_END_VERSIONED_NAMESPACE_DECL