Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / SOCK_SEQPACK_Acceptor.cpp
blobab0590564e0a9f0111a32f4f4a7824c9be067824
1 // $Id: SOCK_SEQPACK_Acceptor.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/SOCK_SEQPACK_Acceptor.h"
5 #include "ace/Auto_Ptr.h"
6 #include "ace/Log_Msg.h"
7 #include "ace/OS_Memory.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_sys_socket.h"
10 #include "ace/os_include/os_fcntl.h"
12 #if !defined (__ACE_INLINE__)
13 #include "ace/SOCK_SEQPACK_Acceptor.inl"
14 #endif /* __ACE_INLINE__ */
16 ACE_RCSID(ace, SOCK_SEQPACK_Acceptor, "SOCK_SEQPACK_Acceptor.cpp,v 4.30 2002/03/08 23:18:09 spark Exp")
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_SEQPACK_Acceptor)
22 // Do nothing routine for constructor.
24 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (void)
26 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
29 // Performs the timed accept operation.
31 int
32 ACE_SOCK_SEQPACK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
33 int restart,
34 int &in_blocking_mode) const
36 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_start");
38 ACE_HANDLE handle = this->get_handle ();
40 // Handle the case where we're doing a timed <accept>.
41 if (timeout != 0)
43 if (ACE::handle_timed_accept (handle,
44 timeout,
45 restart) == -1)
46 return -1;
47 else
49 in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
50 ACE_NONBLOCK);
51 // Set the handle into non-blocking mode if it's not already
52 // in it.
53 if (in_blocking_mode
54 && ACE::set_flags (handle,
55 ACE_NONBLOCK) == -1)
56 return -1;
60 return 0;
63 int
64 ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish (ACE_SOCK_SEQPACK_Association new_association,
65 int in_blocking_mode,
66 int reset_new_handle) const
68 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_accept_finish ()");
70 ACE_HANDLE new_handle = new_association.get_handle ();
72 // Check to see if we were originally in blocking mode, and if so,
73 // set the <new_association>'s handle and <this> handle to be in blocking
74 // mode.
75 if (in_blocking_mode)
77 // Save/restore errno.
78 ACE_Errno_Guard error (errno);
80 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
81 // originally.
82 ACE::clr_flags (this->get_handle (),
83 ACE_NONBLOCK);
84 ACE::clr_flags (new_handle,
85 ACE_NONBLOCK);
88 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
89 if (reset_new_handle)
90 // Reset the event association inherited by the new handle.
91 ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
92 #else
93 ACE_UNUSED_ARG (reset_new_handle);
94 #endif /* ACE_WIN32 */
96 return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
99 // General purpose routine for accepting new connections.
102 ACE_SOCK_SEQPACK_Acceptor::accept (ACE_SOCK_SEQPACK_Association &new_association,
103 ACE_Addr *remote_addr,
104 ACE_Time_Value *timeout,
105 int restart,
106 int reset_new_handle) const
108 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::accept");
110 int in_blocking_mode = 0;
111 if (this->shared_accept_start (timeout,
112 restart,
113 in_blocking_mode) == -1)
114 return -1;
115 else
117 // On Win32 the third parameter to <accept> must be a NULL
118 // pointer if we want to ignore the client's address.
119 int *len_ptr = 0;
120 sockaddr *addr = 0;
121 int len = 0;
123 if (remote_addr != 0)
125 len = remote_addr->get_size ();
126 len_ptr = &len;
127 addr = (sockaddr *) remote_addr->get_addr ();
131 new_association.set_handle (ACE_OS::accept (this->get_handle (),
132 addr,
133 len_ptr));
134 while (new_association.get_handle () == ACE_INVALID_HANDLE
135 && restart != 0
136 && errno == EINTR
137 && timeout == 0);
139 // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
140 // is known.
141 if (new_association.get_handle () != ACE_INVALID_HANDLE
142 && remote_addr != 0)
144 remote_addr->set_size (len);
145 remote_addr->set_type (addr->sa_family);
149 return this->shared_accept_finish (new_association,
150 in_blocking_mode,
151 reset_new_handle);
154 void
155 ACE_SOCK_SEQPACK_Acceptor::dump (void) const
157 #if defined (ACE_HAS_DUMP)
158 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::dump");
159 #endif /* ACE_HAS_DUMP */
163 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Addr &local_sap,
164 int protocol_family,
165 int backlog)
167 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
168 int error = 0;
170 #if defined (ACE_HAS_IPV6)
171 ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
173 if (protocol_family == PF_INET6)
175 sockaddr_in6 local_inet6_addr;
176 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
178 sizeof local_inet6_addr);
180 if (local_sap == ACE_Addr::sap_any)
182 local_inet6_addr.sin6_family = AF_INET6;
183 local_inet6_addr.sin6_port = 0;
184 local_inet6_addr.sin6_addr = in6addr_any;
186 else
187 local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
189 // We probably don't need a bind_port written here.
190 // There are currently no supported OS's that define
191 // ACE_LACKS_WILDCARD_BIND.
192 if (ACE_OS::bind (this->get_handle (),
193 reinterpret_cast<sockaddr *> (&local_inet6_addr),
194 sizeof local_inet6_addr) == -1)
195 error = 1;
197 else
198 #endif
199 if (protocol_family == PF_INET)
201 sockaddr_in local_inet_addr;
202 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
204 sizeof local_inet_addr);
206 if (local_sap == ACE_Addr::sap_any)
208 local_inet_addr.sin_port = 0;
210 else
211 local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
212 if (local_inet_addr.sin_port == 0)
214 if (ACE::bind_port (this->get_handle ()) == -1)
215 error = 1;
217 else if (ACE_OS::bind (this->get_handle (),
218 reinterpret_cast<sockaddr *> (&local_inet_addr),
219 sizeof local_inet_addr) == -1)
220 error = 1;
222 else if (ACE_OS::bind (this->get_handle (),
223 (sockaddr *) local_sap.get_addr (),
224 local_sap.get_size ()) == -1)
225 error = 1;
227 if (error != 0
228 || ACE_OS::listen (this->get_handle (),
229 backlog) == -1)
231 error = 1;
232 this->close ();
235 return error ? -1 : 0;
238 // Multihomed version of same.
241 ACE_SOCK_SEQPACK_Acceptor::shared_open (const ACE_Multihomed_INET_Addr &local_sap,
242 int protocol_family,
243 int backlog)
245 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::shared_open");
246 int error = 0;
248 // TODO: Add multi-address support to IPV6
249 #if defined (ACE_HAS_IPV6)
250 ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
252 if (protocol_family == PF_INET6)
254 sockaddr_in6 local_inet6_addr;
255 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
257 sizeof local_inet6_addr);
259 if (local_sap.ACE_Addr::operator== (ACE_Addr::sap_any))
261 local_inet6_addr.sin6_family = AF_INET6;
262 local_inet6_addr.sin6_port = 0;
263 local_inet6_addr.sin6_addr = in6addr_any;
265 else
266 local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
268 // We probably don't need a bind_port written here.
269 // There are currently no supported OS's that define
270 // ACE_LACKS_WILDCARD_BIND.
271 if (ACE_OS::bind (this->get_handle (),
272 reinterpret_cast<sockaddr *> (&local_inet6_addr),
273 sizeof local_inet6_addr) == -1)
274 error = 1;
276 else
277 #endif
278 if (protocol_family == PF_INET)
280 sockaddr_in local_inet_addr;
281 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
283 sizeof local_inet_addr);
285 if (local_sap.ACE_Addr::operator== (ACE_Addr::sap_any))
287 local_inet_addr.sin_port = 0;
289 else
290 local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
292 // A port number of 0 means that the user is requesting that the
293 // operating system choose an arbitrary, unused port. Since some
294 // operating systems don't provide this service, ACE provides an
295 // emulation layer. Therefore, the "ACE way" to bind an arbitrary,
296 // unused port is to call ACE:bind_port, which either
298 // (1) Calls ACE_OS::bind with port 0, if the operating system
299 // directly supports the automated selection, or
301 // (2) Performs more complicated logic to emulate this feature if
302 // it's missing from the OS.
304 // The emulation logic in choice (2) is compiled if and only if
305 // ACE_LACKS_WILDCARD_BIND is defined at compile time.
307 // Along these lines, the following block of code seems like it would
308 // be sufficient to support the wildcard bind operation:
310 // if (local_inet_addr.sin_port == 0)
311 // {
312 // if (ACE::bind_port (this->get_handle (),
313 // ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
314 // error = 1;
316 // }
317 // else
319 // Unfortunately, this code is insufficient because it does not
320 // address the possibility of secondary addresses.
322 // However, rather than writing the correct code now, I'm putting it
323 // off, because this class, ACE_SOCK_SEQPACK_Acceptor, is currently
324 // only used with SCTP, and ACE currently supports SCTP only through
325 // the OpenSS7 and LKSCTP implmentations, which are available only on
326 // Linux. Linux has native support for the wildcard bind, so the
327 // following code works regardless of whether or not the port is 0.
330 // The total number of addresses is the number of secondary
331 // addresses plus one.
332 size_t num_addresses = local_sap.get_num_secondary_addresses() + 1;
334 // Create an array of sockaddr_in to hold the underlying
335 // representations of the primary and secondary
336 // addresses.
337 sockaddr_in* local_inet_addrs = 0;
338 ACE_NEW_NORETURN (local_inet_addrs,
339 sockaddr_in[num_addresses]);
341 if (!local_inet_addrs)
342 error = 1;
343 else
345 // Populate the array by invoking the get_addresses method
346 // on the Multihomed_INET_Addr
347 local_sap.get_addresses(local_inet_addrs,
348 num_addresses);
350 #if defined (ACE_HAS_LKSCTP)
352 sockaddr_in *local_sockaddr = 0;
354 // bind the primary first
355 if (ACE_OS::bind (this->get_handle (),
356 reinterpret_cast<sockaddr *> (&(local_inet_addrs[0])),
357 sizeof(sockaddr)) == -1)
359 error = 1;
362 // do we need to bind multiple addresses?
363 if (num_addresses > 1)
365 ACE_NEW_NORETURN(local_sockaddr,
366 sockaddr_in[num_addresses - 1]);
368 // all of the secondary addresses need the local port set
369 for (size_t i = 1; i < num_addresses; i++)
371 local_inet_addrs[i].sin_port = local_inet_addrs[0].sin_port;
374 // copy only the sockaddrs that we need to bindx
375 for (size_t i = 0; i < num_addresses - 1; i++)
377 ACE_OS::memcpy(&(local_sockaddr[i]),
378 &(local_inet_addrs[i + 1]),
379 sizeof(sockaddr_in));
382 // now call bindx
383 if (!error && sctp_bindx(this->get_handle (),
384 reinterpret_cast<sockaddr *> (local_sockaddr),
385 num_addresses - 1,
386 SCTP_BINDX_ADD_ADDR))
388 error = 1;
391 delete [] local_sockaddr;
393 #else
394 // Call bind
395 size_t name_len = (sizeof local_inet_addr) * num_addresses;
396 if (ACE_OS::bind (this->get_handle (),
397 reinterpret_cast<sockaddr *> (local_inet_addrs),
398 static_cast<int> (name_len)) == -1)
399 error = 1;
400 #endif /* ACE_HAS_LKSCTP */
403 delete [] local_inet_addrs;
406 else if (ACE_OS::bind (this->get_handle (),
407 (sockaddr *) local_sap.get_addr (),
408 local_sap.get_size ()) == -1)
409 error = 1;
411 if (error != 0
412 || ACE_OS::listen (this->get_handle (),
413 backlog) == -1)
415 error = 1;
416 this->close ();
419 return error ? -1 : 0;
423 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr &local_sap,
424 ACE_Protocol_Info *protocolinfo,
425 ACE_SOCK_GROUP g,
426 u_long flags,
427 int reuse_addr,
428 int protocol_family,
429 int backlog,
430 int protocol)
432 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
434 if (protocol_family == PF_UNSPEC)
435 protocol_family = local_sap.get_type ();
437 #if defined (ACE_HAS_LKSCTP)
438 if (ACE_SOCK::open (SOCK_STREAM,
439 #else
440 if (ACE_SOCK::open (SOCK_SEQPACKET,
441 #endif
442 protocol_family,
443 protocol,
444 protocolinfo,
446 flags,
447 reuse_addr) == -1)
448 return -1;
449 else
450 return this->shared_open (local_sap,
451 protocol_family,
452 backlog);
455 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr &local_sap,
456 ACE_Protocol_Info *protocolinfo,
457 ACE_SOCK_GROUP g,
458 u_long flags,
459 int reuse_addr,
460 int protocol_family,
461 int backlog,
462 int protocol)
464 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
465 if (this->open (local_sap,
466 protocolinfo,
468 flags,
469 reuse_addr,
470 protocol_family,
471 backlog,
472 protocol) == -1)
473 ACE_ERROR ((LM_ERROR,
474 ACE_TEXT ("%p\n"),
475 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
478 // General purpose routine for performing server ACE_SOCK creation.
481 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Addr &local_sap,
482 int reuse_addr,
483 int protocol_family,
484 int backlog,
485 int protocol)
487 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
489 if (local_sap != ACE_Addr::sap_any)
490 protocol_family = local_sap.get_type ();
491 else if (protocol_family == PF_UNSPEC)
493 #if defined (ACE_HAS_IPV6)
494 protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
495 #else
496 protocol_family = PF_INET;
497 #endif /* ACE_HAS_IPV6 */
500 #if defined (ACE_HAS_LKSCTP)
501 if (ACE_SOCK::open (SOCK_STREAM,
502 #else
503 if (ACE_SOCK::open (SOCK_SEQPACKET,
504 #endif
505 protocol_family,
506 protocol,
507 reuse_addr) == -1)
508 return -1;
509 else
510 return this->shared_open (local_sap,
511 protocol_family,
512 backlog);
515 // Multihomed version of same.
518 ACE_SOCK_SEQPACK_Acceptor::open (const ACE_Multihomed_INET_Addr &local_sap,
519 int reuse_addr,
520 int protocol_family,
521 int backlog,
522 int protocol)
524 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::open");
526 if (local_sap.ACE_Addr::operator!= (ACE_Addr::sap_any))
527 protocol_family = local_sap.get_type ();
528 else if (protocol_family == PF_UNSPEC)
530 #if defined (ACE_HAS_IPV6)
531 protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
532 #else
533 protocol_family = PF_INET;
534 #endif /* ACE_HAS_IPV6 */
537 #if defined (ACE_HAS_LKSCTP)
538 if (ACE_SOCK::open (SOCK_STREAM,
539 #else
540 if (ACE_SOCK::open (SOCK_SEQPACKET,
541 #endif
542 protocol_family,
543 protocol,
544 reuse_addr) == -1)
545 return -1;
546 else
547 return this->shared_open (local_sap,
548 protocol_family,
549 backlog);
552 // General purpose routine for performing server ACE_SOCK creation.
554 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Addr &local_sap,
555 int reuse_addr,
556 int protocol_family,
557 int backlog,
558 int protocol)
560 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
561 if (this->open (local_sap,
562 reuse_addr,
563 protocol_family,
564 backlog,
565 protocol) == -1)
566 ACE_ERROR ((LM_ERROR,
567 ACE_TEXT ("%p\n"),
568 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
571 // Multihomed version of same.
573 ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor (const ACE_Multihomed_INET_Addr &local_sap,
574 int reuse_addr,
575 int protocol_family,
576 int backlog,
577 int protocol)
579 ACE_TRACE ("ACE_SOCK_SEQPACK_Acceptor::ACE_SOCK_SEQPACK_Acceptor");
580 if (this->open (local_sap,
581 reuse_addr,
582 protocol_family,
583 backlog,
584 protocol) == -1)
585 ACE_ERROR ((LM_ERROR,
586 ACE_TEXT ("%p\n"),
587 ACE_TEXT ("ACE_SOCK_SEQPACK_Acceptor")));
591 ACE_SOCK_SEQPACK_Acceptor::close (void)
593 return ACE_SOCK::close ();
596 ACE_END_VERSIONED_NAMESPACE_DECL