3 // $Id: OS_NS_sys_socket.inl 82342 2008-07-17 19:52:57Z shuston $
5 #include "ace/OS_NS_errno.h"
6 #include "ace/OS_NS_macros.h"
7 #include "ace/OS_NS_sys_uio.h"
8 #include "ace/OS_NS_stdio.h"
9 #include "ace/OS_QoS.h"
10 #include "ace/Global_Macros.h"
11 #include "ace/os_include/netinet/os_in.h"
13 #if defined (ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO) \
14 && (ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO == 1)
15 #include "ace/OS_NS_string.h"
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 #if defined (ACE_HAS_VOIDPTR_SOCKOPT)
21 typedef void *ACE_SOCKOPT_TYPE1;
22 #elif defined (ACE_HAS_CHARPTR_SOCKOPT)
23 typedef char *ACE_SOCKOPT_TYPE1;
25 typedef const char *ACE_SOCKOPT_TYPE1;
26 #endif /* ACE_HAS_VOIDPTR_SOCKOPT */
29 ACE_OS::accept (ACE_HANDLE handle,
30 struct sockaddr *addr,
33 ACE_OS_TRACE ("ACE_OS::accept");
34 // On a non-blocking socket with no connections to accept, this
35 // system call will return EWOULDBLOCK or EAGAIN, depending on the
36 // platform. UNIX 98 allows either errno, and they may be the same
37 // numeric value. So to make life easier for upper ACE layers as
38 // well as application programmers, always change EAGAIN to
39 // EWOULDBLOCK. Rather than hack the ACE_OSCALL_RETURN macro, it's
40 // handled explicitly here. If the ACE_OSCALL macro ever changes,
41 // this function needs to be reviewed. On Win32, the regular macros
42 // can be used, as this is not an issue.
44 #if defined (ACE_LACKS_ACCEPT)
45 ACE_UNUSED_ARG (handle);
46 ACE_UNUSED_ARG (addr);
47 ACE_UNUSED_ARG (addrlen);
48 ACE_NOTSUP_RETURN (ACE_INVALID_HANDLE);
49 #elif defined (ACE_WIN32)
50 ACE_SOCKCALL_RETURN (::accept ((ACE_SOCKET) handle,
52 (ACE_SOCKET_LEN *) addrlen),
56 # if defined (ACE_HAS_BROKEN_ACCEPT_ADDR)
57 // Apparently some platforms like VxWorks can't correctly deal with
60 sockaddr_in fake_addr;
64 addrlen = &fake_addrlen;
68 addr = (sockaddr *) &fake_addr;
69 *addrlen = sizeof fake_addr;
71 # endif /* ACE_HAS_BROKEN_ACCEPT_ADDR */
72 ACE_HANDLE ace_result = ::accept ((ACE_SOCKET) handle,
74 (ACE_SOCKET_LEN *) addrlen);
76 # if !(defined (EAGAIN) && defined (EWOULDBLOCK) && EAGAIN == EWOULDBLOCK)
77 // Optimize this code out if we can detect that EAGAIN ==
78 // EWOULDBLOCK at compile time. If we cannot detect equality at
79 // compile-time (e.g. if EAGAIN or EWOULDBLOCK are not preprocessor
80 // macros) perform the check at run-time. The goal is to avoid two
81 // TSS accesses in the _REENTRANT case when EAGAIN == EWOULDBLOCK.
82 if (ace_result == ACE_INVALID_HANDLE
83 # if !defined (EAGAIN) || !defined (EWOULDBLOCK)
84 && EAGAIN != EWOULDBLOCK
85 # endif /* !EAGAIN || !EWOULDBLOCK */
90 # endif /* EAGAIN != EWOULDBLOCK*/
94 #endif /* defined (ACE_WIN32) */
98 ACE_OS::bind (ACE_HANDLE handle, struct sockaddr *addr, int addrlen)
100 ACE_OS_TRACE ("ACE_OS::bind");
101 #if defined (ACE_LACKS_BIND)
102 ACE_UNUSED_ARG (handle);
103 ACE_UNUSED_ARG (addr);
104 ACE_UNUSED_ARG (addrlen);
105 ACE_NOTSUP_RETURN (-1);
106 #elif defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x640)
107 // VxWorks clears the sin_port member after a succesfull bind when
108 // sin_addr != INADDR_ANY, so after the bind we do retrieve the
109 // original address so that user code can safely check the addr
110 // after the bind. See bugzilla 3107 for more details
112 ACE_SOCKCALL (::bind ((ACE_SOCKET) handle,
114 (ACE_SOCKET_LEN) addrlen), int, -1, result);
118 return ACE_OS::getsockname (handle, addr, &addrlen);
120 ACE_SOCKCALL_RETURN (::bind ((ACE_SOCKET) handle,
122 (ACE_SOCKET_LEN) addrlen), int, -1);
127 ACE_OS::closesocket (ACE_HANDLE handle)
129 ACE_OS_TRACE ("ACE_OS::closesocket");
130 #if defined (ACE_WIN32)
131 // @note Do not shutdown the write end here. Doing so will break
132 // applications that duplicate a handle on fork(), for
133 // example, and expect to continue writing in the fork()ed
136 ACE_SOCKCALL_RETURN (::closesocket ((SOCKET) handle), int, -1);
138 //FUZZ: disable check_for_lack_ACE_OS
139 ACE_OSCALL_RETURN (::close (handle), int, -1);
140 //FUZZ: enable check_for_lack_ACE_OS
141 #endif /* ACE_WIN32 */
145 ACE_OS::connect (ACE_HANDLE handle,
146 struct sockaddr *addr,
149 ACE_OS_TRACE ("ACE_OS::connect");
150 #if defined (ACE_LACKS_CONNECT)
151 ACE_UNUSED_ARG (handle);
152 ACE_UNUSED_ARG (addr);
153 ACE_UNUSED_ARG (addrlen);
154 ACE_NOTSUP_RETURN (-1);
156 ACE_SOCKCALL_RETURN (::connect ((ACE_SOCKET) handle,
158 (ACE_SOCKET_LEN) addrlen), int, -1);
159 #endif /* ACE_LACKS_CONNECT */
163 ACE_OS::enum_protocols (int *protocols,
164 ACE_Protocol_Info *protocol_buffer,
165 u_long *buffer_length)
167 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
169 ACE_SOCKCALL_RETURN (::WSAEnumProtocols (protocols,
176 ACE_UNUSED_ARG (protocols);
177 ACE_UNUSED_ARG (protocol_buffer);
178 ACE_UNUSED_ARG (buffer_length);
179 ACE_NOTSUP_RETURN (-1);
180 #endif /* ACE_HAS_WINSOCK2 */
184 ACE_OS::getpeername (ACE_HANDLE handle, struct sockaddr *addr,
187 ACE_OS_TRACE ("ACE_OS::getpeername");
189 #if defined (ACE_LACKS_GETPEERNAME)
190 ACE_UNUSED_ARG (handle);
191 ACE_UNUSED_ARG (addr);
192 ACE_UNUSED_ARG (addrlen);
193 ACE_NOTSUP_RETURN (-1);
194 #elif defined (ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO) \
195 && (ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO == 1)
197 ACE_SOCKCALL (::getpeername ((ACE_SOCKET) handle,
199 (ACE_SOCKET_LEN *) addrlen),
204 // Some platforms, like older versions of the Linux kernel, do not
205 // initialize the sin_zero field since that field is generally only
206 // used for padding/alignment purposes. On those platforms
207 // memcmp()-based comparisons of the sockaddr_in structure, such as
208 // the one in the ACE_INET_Addr equality operator, may fail due to
209 // random bytes in the sin_zero field even though that field is
210 // unused. Prevent equality comparison of two different sockaddr_in
211 // instances that refer to the same socket from failing by
212 // explicitly initializing the sockaddr_in::sin_zero field to a
213 // consistent value, e.g. zero.
214 if (result != -1 && addr->sa_family == AF_INET)
216 ACE_OS::memset (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero,
218 sizeof (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero));
223 ACE_SOCKCALL_RETURN (::getpeername ((ACE_SOCKET) handle,
225 (ACE_SOCKET_LEN *) addrlen),
228 #endif /* ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO */
232 ACE_OS::getsockname (ACE_HANDLE handle,
233 struct sockaddr *addr,
236 ACE_OS_TRACE ("ACE_OS::getsockname");
237 #if defined (ACE_LACKS_GETSOCKNAME)
238 ACE_UNUSED_ARG (handle);
239 ACE_UNUSED_ARG (addr);
240 ACE_UNUSED_ARG (addrlen);
241 ACE_NOTSUP_RETURN (-1);
242 #elif defined (ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO) \
243 && (ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO == 1)
245 ACE_SOCKCALL (::getsockname ((ACE_SOCKET) handle,
247 (ACE_SOCKET_LEN *) addrlen),
250 // Some platforms, like older versions of the Linux kernel, do not
251 // initialize the sin_zero field since that field is generally only
252 // used for padding/alignment purposes. On those platforms
253 // memcmp()-based comparisons of the sockaddr_in structure, such as
254 // the one in the ACE_INET_Addr equality operator, may fail due to
255 // random bytes in the sin_zero field even though that field is
256 // unused. Prevent equality comparison of two different sockaddr_in
257 // instances that refer to the same socket from failing by
258 // explicitly initializing the sockaddr_in::sin_zero field to a
259 // consistent value, e.g. zero.
260 if (result != -1 && addr->sa_family == AF_INET)
262 ACE_OS::memset (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero,
264 sizeof (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero));
269 ACE_SOCKCALL_RETURN (::getsockname ((ACE_SOCKET) handle,
271 (ACE_SOCKET_LEN *) addrlen),
273 #endif /* ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO */
277 ACE_OS::getsockopt (ACE_HANDLE handle,
283 ACE_OS_TRACE ("ACE_OS::getsockopt");
284 #if defined (ACE_LACKS_GETSOCKOPT)
285 ACE_UNUSED_ARG (handle);
286 ACE_UNUSED_ARG (level);
287 ACE_UNUSED_ARG (optname);
288 ACE_UNUSED_ARG (optval);
289 ACE_UNUSED_ARG (optlen);
290 ACE_NOTSUP_RETURN (-1);
292 ACE_SOCKCALL_RETURN (::getsockopt ((ACE_SOCKET) handle,
296 (ACE_SOCKET_LEN *) optlen),
299 #endif /* ACE_LACKS_GETSOCKOPT */
303 ACE_OS::listen (ACE_HANDLE handle, int backlog)
305 ACE_OS_TRACE ("ACE_OS::listen");
306 #if defined (ACE_LACKS_LISTEN)
307 ACE_UNUSED_ARG (handle);
308 ACE_UNUSED_ARG (backlog);
309 ACE_NOTSUP_RETURN (-1);
311 ACE_SOCKCALL_RETURN (::listen ((ACE_SOCKET) handle, backlog), int, -1);
312 #endif /* ACE_LACKS_LISTEN */
316 ACE_OS::recv (ACE_HANDLE handle, char *buf, size_t len, int flags)
318 ACE_OS_TRACE ("ACE_OS::recv");
320 // On UNIX, a non-blocking socket with no data to receive, this
321 // system call will return EWOULDBLOCK or EAGAIN, depending on the
322 // platform. UNIX 98 allows either errno, and they may be the same
323 // numeric value. So to make life easier for upper ACE layers as
324 // well as application programmers, always change EAGAIN to
325 // EWOULDBLOCK. Rather than hack the ACE_OSCALL_RETURN macro, it's
326 // handled explicitly here. If the ACE_OSCALL macro ever changes,
327 // this function needs to be reviewed. On Win32, the regular macros
328 // can be used, as this is not an issue.
329 #if defined (ACE_LACKS_RECV)
330 ACE_UNUSED_ARG (handle);
331 ACE_UNUSED_ARG (buf);
332 ACE_UNUSED_ARG (len);
333 ACE_UNUSED_ARG (flags);
334 ACE_NOTSUP_RETURN (-1);
335 #elif defined (ACE_WIN32)
336 ACE_SOCKCALL_RETURN (::recv ((ACE_SOCKET) handle, buf,
337 static_cast<int> (len), flags), ssize_t, -1);
340 ace_result_ = ::recv ((ACE_SOCKET) handle, buf, len, flags);
342 # if !(defined (EAGAIN) && defined (EWOULDBLOCK) && EAGAIN == EWOULDBLOCK)
343 // Optimize this code out if we can detect that EAGAIN ==
344 // EWOULDBLOCK at compile time. If we cannot detect equality at
345 // compile-time (e.g. if EAGAIN or EWOULDBLOCK are not preprocessor
346 // macros) perform the check at run-time. The goal is to avoid two
347 // TSS accesses in the _REENTRANT case when EAGAIN == EWOULDBLOCK.
348 if (ace_result_ == -1
349 # if !defined (EAGAIN) || !defined (EWOULDBLOCK)
350 && EAGAIN != EWOULDBLOCK
351 # endif /* !EAGAIN || !EWOULDBLOCK */
356 # endif /* EAGAIN != EWOULDBLOCK*/
359 #endif /* ACE_LACKS_RECV */
363 ACE_OS::recvfrom (ACE_HANDLE handle,
367 struct sockaddr *addr,
370 ACE_OS_TRACE ("ACE_OS::recvfrom");
371 #if defined (ACE_LACKS_RECVFROM)
372 ACE_UNUSED_ARG (handle);
373 ACE_UNUSED_ARG (buf);
374 ACE_UNUSED_ARG (len);
375 ACE_UNUSED_ARG (flags);
376 ACE_UNUSED_ARG (addr);
377 ACE_UNUSED_ARG (addrlen);
378 ACE_NOTSUP_RETURN (-1);
379 #elif defined (ACE_WIN32)
380 int const shortened_len = static_cast<int> (len);
381 int const result = ::recvfrom ((ACE_SOCKET) handle,
386 (ACE_SOCKET_LEN *) addrlen);
387 if (result == SOCKET_ERROR)
389 ACE_OS::set_errno_to_wsa_last_error ();
390 if (errno == WSAEMSGSIZE &&
391 ACE_BIT_ENABLED (flags, MSG_PEEK))
392 return shortened_len;
398 # if defined (ACE_HAS_PHARLAP)
399 // Pharlap ETS (at least to v13) returns a legit address but doesn't
400 // include the sin_zero[8] bytes in the count. Correct for this here.
401 if (addrlen != 0 && addr != 0 &&
402 *addrlen == 8 && addr->sa_family == AF_INET)
403 *addrlen = sizeof(sockaddr_in);
404 # endif /* ACE_HAS_PHARLAP */
407 #else /* non Win32 */
408 ACE_SOCKCALL_RETURN (::recvfrom ((ACE_SOCKET) handle,
413 (ACE_SOCKET_LEN *) addrlen),
415 #endif /* ACE_LACKS_RECVFROM */
419 ACE_OS::recvfrom (ACE_HANDLE handle,
422 size_t &number_of_bytes_recvd,
424 struct sockaddr *addr,
426 ACE_OVERLAPPED *overlapped,
427 ACE_OVERLAPPED_COMPLETION_FUNC func)
429 ACE_OS_TRACE ("ACE_OS::recvfrom");
431 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
433 DWORD the_flags = flags;
434 int result = ::WSARecvFrom ((SOCKET) handle,
444 ACE_OS::set_errno_to_wsa_last_error ();
447 number_of_bytes_recvd = static_cast<size_t> (bytes_recvd);
450 ACE_UNUSED_ARG (handle);
451 ACE_UNUSED_ARG (buffers);
452 ACE_UNUSED_ARG (buffer_count);
453 ACE_UNUSED_ARG (number_of_bytes_recvd);
454 ACE_UNUSED_ARG (flags);
455 ACE_UNUSED_ARG (addr);
456 ACE_UNUSED_ARG (addrlen);
457 ACE_UNUSED_ARG (overlapped);
458 ACE_UNUSED_ARG (func);
459 ACE_NOTSUP_RETURN (-1);
460 #endif /* defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) */
464 ACE_OS::recvmsg (ACE_HANDLE handle, struct msghdr *msg, int flags)
466 ACE_OS_TRACE ("ACE_OS::recvmsg");
467 #if !defined (ACE_LACKS_RECVMSG)
468 # if (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
469 DWORD bytes_received = 0;
471 int result = ::WSARecvFrom ((SOCKET) handle,
472 (WSABUF *) msg->msg_iov,
483 ACE_OS::set_errno_to_wsa_last_error ();
487 return bytes_received;
488 # else /* ACE_HAS_WINSOCK2 */
489 ACE_SOCKCALL_RETURN (::recvmsg (handle, msg, flags), ssize_t, -1);
490 # endif /* ACE_HAS_WINSOCK2 */
492 ACE_UNUSED_ARG (flags);
493 ACE_UNUSED_ARG (msg);
494 ACE_UNUSED_ARG (handle);
496 ACE_NOTSUP_RETURN (-1);
497 #endif /* ACE_LACKS_RECVMSG */
501 ACE_OS::recvv (ACE_HANDLE handle,
505 #if defined (ACE_HAS_WINSOCK2)
507 DWORD bytes_received = 0;
510 // Winsock 2 has WSARecv and can do this directly, but Winsock 1 needs
511 // to do the recvs piece-by-piece.
513 # if (ACE_HAS_WINSOCK2 != 0)
515 result = ::WSARecv ((SOCKET) handle,
526 // Step through the buffers requested by caller; for each one, cycle
527 // through reads until it's filled or an error occurs.
528 for (i = 0; i < n && result > 0; ++i)
530 chunkp = buffers[i].iov_base; // Point to part of chunk being read
531 chunklen = buffers[i].iov_len; // Track how much to read to chunk
532 while (chunklen > 0 && result > 0)
534 result = ::recv ((SOCKET) handle, chunkp, chunklen, 0);
539 bytes_received += result;
543 # endif /* ACE_HAS_WINSOCK2 != 0 */
545 if (result == SOCKET_ERROR)
547 ACE_OS::set_errno_to_wsa_last_error ();
551 return (ssize_t) bytes_received;
553 return ACE_OS::readv (handle, buffers, n);
554 #endif /* ACE_HAS_WINSOCK2 */
558 ACE_OS::send (ACE_HANDLE handle, const char *buf, size_t len, int flags)
560 ACE_OS_TRACE ("ACE_OS::send");
562 // On UNIX, a non-blocking socket with no data to receive, this
563 // system call will return EWOULDBLOCK or EAGAIN, depending on the
564 // platform. UNIX 98 allows either errno, and they may be the same
565 // numeric value. So to make life easier for upper ACE layers as
566 // well as application programmers, always change EAGAIN to
567 // EWOULDBLOCK. Rather than hack the ACE_OSCALL_RETURN macro, it's
568 // handled explicitly here. If the ACE_OSCALL macro ever changes,
569 // this function needs to be reviewed. On Win32, the regular macros
570 // can be used, as this is not an issue.
571 #if defined (ACE_LACKS_SEND)
572 ACE_UNUSED_ARG (handle);
573 ACE_UNUSED_ARG (buf);
574 ACE_UNUSED_ARG (len);
575 ACE_UNUSED_ARG (flags);
576 ACE_NOTSUP_RETURN (-1);
577 #elif defined (ACE_WIN32)
578 ACE_SOCKCALL_RETURN (::send ((ACE_SOCKET) handle,
580 static_cast<int> (len),
581 flags), ssize_t, -1);
583 ssize_t const ace_result_ = ::send ((ACE_SOCKET) handle, buf, len, flags);
585 # if !(defined (EAGAIN) && defined (EWOULDBLOCK) && EAGAIN == EWOULDBLOCK)
586 // Optimize this code out if we can detect that EAGAIN ==
587 // EWOULDBLOCK at compile time. If we cannot detect equality at
588 // compile-time (e.g. if EAGAIN or EWOULDBLOCK are not preprocessor
589 // macros) perform the check at run-time. The goal is to avoid two
590 // TSS accesses in the _REENTRANT case when EAGAIN == EWOULDBLOCK.
591 if (ace_result_ == -1
592 # if !defined (EAGAIN) || !defined (EWOULDBLOCK)
593 && EAGAIN != EWOULDBLOCK
594 # endif /* !EAGAIN || !EWOULDBLOCK */
599 # endif /* EAGAIN != EWOULDBLOCK*/
602 #endif /* defined (ACE_WIN32) */
606 ACE_OS::sendmsg (ACE_HANDLE handle,
607 const struct msghdr *msg,
610 ACE_OS_TRACE ("ACE_OS::sendmsg");
611 #if !defined (ACE_LACKS_SENDMSG)
612 # if (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
613 DWORD bytes_sent = 0;
614 int result = ::WSASendTo ((SOCKET) handle,
615 (WSABUF *) msg->msg_iov,
626 ACE_OS::set_errno_to_wsa_last_error ();
630 return (ssize_t) bytes_sent;
631 # elif defined (ACE_HAS_NONCONST_SENDMSG)
632 ACE_SOCKCALL_RETURN (::sendmsg (handle,
633 const_cast<struct msghdr *>(msg),
634 flags), ssize_t, -1);
636 ACE_SOCKCALL_RETURN (::sendmsg (handle, msg, flags), ssize_t, -1);
639 ACE_UNUSED_ARG (flags);
640 ACE_UNUSED_ARG (msg);
641 ACE_UNUSED_ARG (handle);
643 ACE_NOTSUP_RETURN (-1);
644 #endif /* ACE_LACKS_SENDMSG */
648 ACE_OS::sendto (ACE_HANDLE handle,
652 const struct sockaddr *addr,
655 ACE_OS_TRACE ("ACE_OS::sendto");
656 #if defined (ACE_LACKS_SENDTO)
657 ACE_UNUSED_ARG (handle);
658 ACE_UNUSED_ARG (buf);
659 ACE_UNUSED_ARG (len);
660 ACE_UNUSED_ARG (flags);
661 ACE_UNUSED_ARG (addr);
662 ACE_UNUSED_ARG (addrlen);
663 ACE_NOTSUP_RETURN (-1);
664 #elif defined (ACE_VXWORKS)
665 ACE_SOCKCALL_RETURN (::sendto ((ACE_SOCKET) handle,
666 const_cast <char *> (buf),
669 const_cast<struct sockaddr *> (addr),
672 #elif defined (ACE_WIN32)
673 ACE_SOCKCALL_RETURN (::sendto ((ACE_SOCKET) handle,
675 static_cast<int> (len),
677 const_cast<struct sockaddr *> (addr),
681 ACE_SOCKCALL_RETURN (::sendto ((ACE_SOCKET) handle,
685 const_cast<struct sockaddr *> (addr),
688 #endif /* ACE_LACKS_SENDTO */
692 ACE_OS::sendto (ACE_HANDLE handle,
693 const iovec *buffers,
695 size_t &number_of_bytes_sent,
697 const struct sockaddr *addr,
699 ACE_OVERLAPPED *overlapped,
700 ACE_OVERLAPPED_COMPLETION_FUNC func)
702 ACE_OS_TRACE ("ACE_OS::sendto");
703 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
704 DWORD bytes_sent = 0;
705 int result = ::WSASendTo ((SOCKET) handle,
715 ACE_OS::set_errno_to_wsa_last_error ();
717 number_of_bytes_sent = static_cast<size_t> (bytes_sent);
718 return (ssize_t) result;
720 ACE_UNUSED_ARG (overlapped);
721 ACE_UNUSED_ARG (func);
723 number_of_bytes_sent = 0;
727 for (int i = 0; i < buffer_count; ++i)
729 result = ACE_OS::sendto (handle,
730 reinterpret_cast<char *> (
731 buffers[i].iov_base),
738 number_of_bytes_sent += static_cast<size_t> (result);
742 #endif /* defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) */
746 ACE_OS::sendv (ACE_HANDLE handle,
747 const iovec *buffers,
750 #if defined (ACE_HAS_WINSOCK2)
751 DWORD bytes_sent = 0;
754 // Winsock 2 has WSASend and can do this directly, but Winsock 1
755 // needs to do the sends one-by-one.
756 # if (ACE_HAS_WINSOCK2 != 0)
757 result = ::WSASend ((SOCKET) handle,
764 if (result == SOCKET_ERROR)
766 ACE_OS::set_errno_to_wsa_last_error ();
770 for (int i = 0; i < n; ++i)
772 result = ::send ((SOCKET) handle,
777 if (result == SOCKET_ERROR)
779 // There is a subtle difference in behaviour depending on
780 // whether or not any data was sent. If no data was sent,
781 // then always return -1. Otherwise return bytes_sent.
782 // This gives the caller an opportunity to keep track of
783 // bytes that have already been sent.
788 ACE_OS::set_errno_to_wsa_last_error ();
794 // Gets ignored on error anyway
795 bytes_sent += result;
797 // If the transfer isn't complete just drop out of the loop.
798 if (result < (int)buffers[i].iov_len)
802 # endif /* ACE_HAS_WINSOCK2 != 0 */
804 return (ssize_t) bytes_sent;
806 #elif defined (ACE_HAS_SOCK_BUF_SIZE_MAX)
808 // Platform limits the maximum socket message size. Pare down the
809 // iovec, if necessary, to obey the limit.
810 iovec local_iov[ACE_IOV_MAX];
813 for (int i = 0; i < n; i++)
815 local_iov[i].iov_base = buffers[i].iov_base;
816 local_iov[i].iov_len = buffers[i].iov_len;
818 new_total = total + buffers[i].iov_len;
819 if (new_total >= ACE_HAS_SOCK_BUF_SIZE_MAX_VALUE)
821 local_iov[i].iov_len = ACE_HAS_SOCK_BUF_SIZE_MAX_VALUE - total;
827 return ACE_OS::writev (handle, local_iov, n);
830 return ACE_OS::writev (handle, buffers, n);
831 #endif /* ACE_HAS_WINSOCK2 */
835 ACE_OS::setsockopt (ACE_HANDLE handle,
841 ACE_OS_TRACE ("ACE_OS::setsockopt");
842 #if defined (ACE_LACKS_SETSOCKOPT)
843 ACE_UNUSED_ARG (handle);
844 ACE_UNUSED_ARG (level);
845 ACE_UNUSED_ARG (optname);
846 ACE_UNUSED_ARG (optval);
847 ACE_UNUSED_ARG (optlen);
848 ACE_NOTSUP_RETURN (-1);
850 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) && defined(SO_REUSEPORT)
851 // To work around an inconsistency with Microsofts implementation of
852 // sockets, we will check for SO_REUSEADDR, and ignore it. Winsock
853 // always behaves as if SO_REUSEADDR=1. Some implementations have
854 // the same behaviour as Winsock, but use a new name for
855 // it. SO_REUSEPORT. If you want the normal behaviour for
856 // SO_REUSEADDR=0, then NT 4 sp4 and later supports
857 // SO_EXCLUSIVEADDRUSE. This also requires using an updated Platform
858 // SDK so it was decided to ignore the option for now. (Especially
859 // since Windows always sets SO_REUSEADDR=1, which we can mimic by doing
861 if (level == SOL_SOCKET) {
862 if (optname == SO_REUSEADDR) {
863 return 0; // Not supported by Winsock
865 if (optname == SO_REUSEPORT) {
866 optname = SO_REUSEADDR;
869 #endif /*ACE_HAS_WINSOCK2*/
872 ACE_SOCKCALL (::setsockopt ((ACE_SOCKET) handle,
875 (ACE_SOCKOPT_TYPE1) optval,
880 #if defined (WSAEOPNOTSUPP)
881 if (result == -1 && errno == WSAEOPNOTSUPP)
884 #endif /* WSAEOPNOTSUPP */
891 ACE_OS::shutdown (ACE_HANDLE handle, int how)
893 ACE_OS_TRACE ("ACE_OS::shutdown");
894 #if defined (ACE_LACKS_SHUTDOWN)
895 ACE_UNUSED_ARG (handle);
896 ACE_UNUSED_ARG (how);
897 ACE_NOTSUP_RETURN (-1);
899 ACE_SOCKCALL_RETURN (::shutdown ((ACE_SOCKET) handle, how), int, -1);
900 #endif /* ACE_LACKS_SHUTDOWN */
903 ACE_INLINE ACE_HANDLE
904 ACE_OS::socket (int domain,
908 ACE_OS_TRACE ("ACE_OS::socket");
909 #if defined (ACE_LACKS_SOCKET)
910 ACE_UNUSED_ARG (domain);
911 ACE_UNUSED_ARG (type);
912 ACE_UNUSED_ARG (proto);
913 ACE_NOTSUP_RETURN (ACE_INVALID_HANDLE);
915 ACE_SOCKCALL_RETURN (::socket (domain,
920 #endif /* ACE_LACKS_SOCKET */
923 ACE_INLINE ACE_HANDLE
924 ACE_OS::socket (int domain,
927 ACE_Protocol_Info *protocolinfo,
931 ACE_OS_TRACE ("ACE_OS::socket");
933 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
934 ACE_SOCKCALL_RETURN (::WSASocket (domain,
943 ACE_UNUSED_ARG (protocolinfo);
945 ACE_UNUSED_ARG (flags);
947 return ACE_OS::socket (domain,
950 #endif /* ACE_HAS_WINSOCK2 */
954 ACE_OS::socketpair (int domain, int type,
955 int protocol, ACE_HANDLE sv[2])
957 ACE_OS_TRACE ("ACE_OS::socketpair");
958 #if defined (ACE_LACKS_SOCKETPAIR)
959 ACE_UNUSED_ARG (domain);
960 ACE_UNUSED_ARG (type);
961 ACE_UNUSED_ARG (protocol);
964 ACE_NOTSUP_RETURN (-1);
966 ACE_OSCALL_RETURN (::socketpair (domain, type, protocol, sv),
968 #endif /* ACE_LACKS_SOCKETPAIR */
971 #if defined (__linux__) && defined (ACE_HAS_IPV6)
972 ACE_INLINE unsigned int
973 ACE_OS::if_nametoindex (const char *ifname)
975 ACE_OS_TRACE ("ACE_OS::if_nametoindex");
976 ACE_OSCALL_RETURN (::if_nametoindex (ifname), int, 0);
980 ACE_OS::if_indextoname (unsigned int ifindex, char *ifname)
982 ACE_OS_TRACE ("ACE_OS::if_indextoname");
983 ACE_OSCALL_RETURN (::if_indextoname (ifindex, ifname), char *, 0);
986 ACE_INLINE struct if_nameindex *
987 ACE_OS::if_nameindex (void)
989 ACE_OS_TRACE ("ACE_OS::if_nameindex");
990 ACE_OSCALL_RETURN (::if_nameindex (), struct if_nameindex *, 0);
994 ACE_OS::if_freenameindex (struct if_nameindex *ptr)
996 ACE_OS_TRACE ("ACE_OS::if_freenameindex");
998 ::if_freenameindex (ptr);
1000 #endif /* __linux__ && ACE_HAS_IPV6 */
1002 ACE_END_VERSIONED_NAMESPACE_DECL