Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / OS_NS_sys_socket.inl
blobdb63c418d8432fccf1d579e4d14e556c51d7e578
1 // -*- C++ -*-
2 //
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"
16 #endif
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;
24 #else
25 typedef const char *ACE_SOCKOPT_TYPE1;
26 #endif /* ACE_HAS_VOIDPTR_SOCKOPT */
28 ACE_INLINE ACE_HANDLE
29 ACE_OS::accept (ACE_HANDLE handle,
30                 struct sockaddr *addr,
31                 int *addrlen)
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,
51                                  addr,
52                                  (ACE_SOCKET_LEN *) addrlen),
53                        ACE_HANDLE,
54                        ACE_INVALID_HANDLE);
55 #else
56 #  if defined (ACE_HAS_BROKEN_ACCEPT_ADDR)
57   // Apparently some platforms like VxWorks can't correctly deal with
58   // a NULL addr.
60    sockaddr_in fake_addr;
61    int fake_addrlen;
63    if (addrlen == 0)
64      addrlen = &fake_addrlen;
66    if (addr == 0)
67      {
68        addr = (sockaddr *) &fake_addr;
69        *addrlen = sizeof fake_addr;
70      }
71 #  endif /* ACE_HAS_BROKEN_ACCEPT_ADDR */
72   ACE_HANDLE ace_result = ::accept ((ACE_SOCKET) handle,
73                                     addr,
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 */
86       && errno == EAGAIN)
87     {
88       errno = EWOULDBLOCK;
89     }
90 # endif /* EAGAIN != EWOULDBLOCK*/
92   return ace_result;
94 #endif /* defined (ACE_WIN32) */
97 ACE_INLINE int
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
111   int result;
112   ACE_SOCKCALL (::bind ((ACE_SOCKET) handle,
113                         addr,
114                         (ACE_SOCKET_LEN) addrlen), int, -1, result);
115   if (result == -1)
116     return -1;
117   else
118     return ACE_OS::getsockname (handle, addr, &addrlen);
119 #else
120   ACE_SOCKCALL_RETURN (::bind ((ACE_SOCKET) handle,
121                                addr,
122                                (ACE_SOCKET_LEN) addrlen), int, -1);
123 #endif
126 ACE_INLINE int
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
134   //       process.
136   ACE_SOCKCALL_RETURN (::closesocket ((SOCKET) handle), int, -1);
137 #else
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 */
144 ACE_INLINE int
145 ACE_OS::connect (ACE_HANDLE handle,
146                  struct sockaddr *addr,
147                  int addrlen)
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);
155 #else
156   ACE_SOCKCALL_RETURN (::connect ((ACE_SOCKET) handle,
157                                   addr,
158                                   (ACE_SOCKET_LEN) addrlen), int, -1);
159 #endif /* ACE_LACKS_CONNECT */
162 ACE_INLINE int
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,
170                                            protocol_buffer,
171                                            buffer_length),
172                        int,
173                        SOCKET_ERROR);
175 #else
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 */
183 ACE_INLINE int
184 ACE_OS::getpeername (ACE_HANDLE handle, struct sockaddr *addr,
185                      int *addrlen)
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)
196   int result;
197   ACE_SOCKCALL (::getpeername ((ACE_SOCKET) handle,
198                                addr,
199                                (ACE_SOCKET_LEN *) addrlen),
200                int,
201                 -1,
202                 result);
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)
215     {
216       ACE_OS::memset (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero,
217                       0,
218                       sizeof (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero));
219     }
221   return result;
222 #else
223   ACE_SOCKCALL_RETURN (::getpeername ((ACE_SOCKET) handle,
224                                       addr,
225                                       (ACE_SOCKET_LEN *) addrlen),
226                        int,
227                        -1);
228 #endif /* ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO */
231 ACE_INLINE int
232 ACE_OS::getsockname (ACE_HANDLE handle,
233                      struct sockaddr *addr,
234                      int *addrlen)
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)
244   int result;
245   ACE_SOCKCALL (::getsockname ((ACE_SOCKET) handle,
246                                addr,
247                                (ACE_SOCKET_LEN *) addrlen),
248                int, -1, result);
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)
261     {
262       ACE_OS::memset (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero,
263                       0,
264                       sizeof (reinterpret_cast<struct sockaddr_in *> (addr)->sin_zero));
265     }
267   return result;
268 #else
269   ACE_SOCKCALL_RETURN (::getsockname ((ACE_SOCKET) handle,
270                                       addr,
271                                       (ACE_SOCKET_LEN *) addrlen),
272                        int, -1);
273 #endif /* ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO */
276 ACE_INLINE int
277 ACE_OS::getsockopt (ACE_HANDLE handle,
278                     int level,
279                     int optname,
280                     char *optval,
281                     int *optlen)
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);
291 #else
292   ACE_SOCKCALL_RETURN (::getsockopt ((ACE_SOCKET) handle,
293                                      level,
294                                      optname,
295                                      optval,
296                                      (ACE_SOCKET_LEN *) optlen),
297                        int,
298                        -1);
299 #endif /* ACE_LACKS_GETSOCKOPT */
302 ACE_INLINE int
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);
310 #else
311   ACE_SOCKCALL_RETURN (::listen ((ACE_SOCKET) handle, backlog), int, -1);
312 #endif /* ACE_LACKS_LISTEN */
315 ACE_INLINE ssize_t
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);
338 #else
339   ssize_t ace_result_;
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 */
352       && errno == EAGAIN)
353     {
354       errno = EWOULDBLOCK;
355     }
356 # endif /* EAGAIN != EWOULDBLOCK*/
358   return ace_result_;
359 #endif /* ACE_LACKS_RECV */
362 ACE_INLINE ssize_t
363 ACE_OS::recvfrom (ACE_HANDLE handle,
364                   char *buf,
365                   size_t len,
366                   int flags,
367                   struct sockaddr *addr,
368                   int *addrlen)
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,
382                                  buf,
383                                  shortened_len,
384                                  flags,
385                                  addr,
386                                  (ACE_SOCKET_LEN *) addrlen);
387   if (result == SOCKET_ERROR)
388     {
389       ACE_OS::set_errno_to_wsa_last_error ();
390       if (errno == WSAEMSGSIZE &&
391           ACE_BIT_ENABLED (flags, MSG_PEEK))
392         return shortened_len;
393       else
394         return -1;
395     }
396   else
397     {
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 */
405       return result;
406     }
407 #else /* non Win32 */
408   ACE_SOCKCALL_RETURN (::recvfrom ((ACE_SOCKET) handle,
409                                    buf,
410                                    len,
411                                    flags,
412                                    addr,
413                                    (ACE_SOCKET_LEN *) addrlen),
414                        ssize_t, -1);
415 #endif /* ACE_LACKS_RECVFROM */
418 ACE_INLINE ssize_t
419 ACE_OS::recvfrom (ACE_HANDLE handle,
420                   iovec *buffers,
421                   int buffer_count,
422                   size_t &number_of_bytes_recvd,
423                   int &flags,
424                   struct sockaddr *addr,
425                   int *addrlen,
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)
432   DWORD bytes_recvd;
433   DWORD the_flags = flags;
434   int result = ::WSARecvFrom ((SOCKET) handle,
435                               (WSABUF*)buffers,
436                               buffer_count,
437                               &bytes_recvd,
438                               &the_flags,
439                               addr,
440                               addrlen,
441                               overlapped,
442                               func);
443   if (result != 0) {
444     ACE_OS::set_errno_to_wsa_last_error ();
445   }
446   flags = the_flags;
447   number_of_bytes_recvd = static_cast<size_t> (bytes_recvd);
448   return result;
449 #else
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) */
463 ACE_INLINE ssize_t
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,
473                               msg->msg_iovlen,
474                               &bytes_received,
475                               (DWORD *) &flags,
476                               msg->msg_name,
477                               &msg->msg_namelen,
478                               0,
479                               0);
481   if (result != 0)
482     {
483       ACE_OS::set_errno_to_wsa_last_error ();
484       return -1;
485     }
486   else
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 */
491 #else
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 */
500 ACE_INLINE ssize_t
501 ACE_OS::recvv (ACE_HANDLE handle,
502                iovec *buffers,
503                int n)
505 #if defined (ACE_HAS_WINSOCK2)
507   DWORD bytes_received = 0;
508   int result = 1;
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)
514   DWORD flags = 0;
515   result = ::WSARecv ((SOCKET) handle,
516                       (WSABUF *) buffers,
517                       n,
518                       &bytes_received,
519                       &flags,
520                       0,
521                       0);
522 # else
523   int i, chunklen;
524   char *chunkp = 0;
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)
529     {
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)
533         {
534           result = ::recv ((SOCKET) handle, chunkp, chunklen, 0);
535           if (result > 0)
536             {
537               chunkp += result;
538               chunklen -= result;
539               bytes_received += result;
540             }
541         }
542     }
543 # endif /* ACE_HAS_WINSOCK2 != 0 */
545   if (result == SOCKET_ERROR)
546     {
547       ACE_OS::set_errno_to_wsa_last_error ();
548       return -1;
549     }
550   else
551     return (ssize_t) bytes_received;
552 #else
553   return ACE_OS::readv (handle, buffers, n);
554 #endif /* ACE_HAS_WINSOCK2 */
557 ACE_INLINE ssize_t
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,
579                                buf,
580                                static_cast<int> (len),
581                                flags), ssize_t, -1);
582 #else
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 */
595       && errno == EAGAIN)
596     {
597       errno = EWOULDBLOCK;
598     }
599 # endif /* EAGAIN != EWOULDBLOCK*/
601   return ace_result_;
602 #endif /* defined (ACE_WIN32) */
605 ACE_INLINE ssize_t
606 ACE_OS::sendmsg (ACE_HANDLE handle,
607                  const struct msghdr *msg,
608                  int flags)
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,
616                             msg->msg_iovlen,
617                             &bytes_sent,
618                             flags,
619                             msg->msg_name,
620                             msg->msg_namelen,
621                             0,
622                             0);
624   if (result != 0)
625     {
626       ACE_OS::set_errno_to_wsa_last_error ();
627       return -1;
628     }
629   else
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);
635 # else
636   ACE_SOCKCALL_RETURN (::sendmsg (handle, msg, flags), ssize_t, -1);
637 # endif
638 #else
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 */
647 ACE_INLINE ssize_t
648 ACE_OS::sendto (ACE_HANDLE handle,
649                 const char *buf,
650                 size_t len,
651                 int flags,
652                 const struct sockaddr *addr,
653                 int addrlen)
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),
667                                  len,
668                                  flags,
669                                  const_cast<struct sockaddr *> (addr),
670                                  addrlen),
671                        ssize_t, -1);
672 #elif defined (ACE_WIN32)
673   ACE_SOCKCALL_RETURN (::sendto ((ACE_SOCKET) handle,
674                                  buf,
675                                  static_cast<int> (len),
676                                  flags,
677                                  const_cast<struct sockaddr *> (addr),
678                                  addrlen),
679                        ssize_t, -1);
680 #else
681   ACE_SOCKCALL_RETURN (::sendto ((ACE_SOCKET) handle,
682                                  buf,
683                                  len,
684                                  flags,
685                                  const_cast<struct sockaddr *> (addr),
686                                  addrlen),
687                        ssize_t, -1);
688 #endif /* ACE_LACKS_SENDTO */
691 ACE_INLINE ssize_t
692 ACE_OS::sendto (ACE_HANDLE handle,
693                 const iovec *buffers,
694                 int buffer_count,
695                 size_t &number_of_bytes_sent,
696                 int flags,
697                 const struct sockaddr *addr,
698                 int addrlen,
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,
706                             (WSABUF*) buffers,
707                             buffer_count,
708                             &bytes_sent,
709                             flags,
710                             addr,
711                             addrlen,
712                             overlapped,
713                             func);
714   if (result != 0) {
715     ACE_OS::set_errno_to_wsa_last_error ();
716   }
717   number_of_bytes_sent = static_cast<size_t> (bytes_sent);
718   return (ssize_t) result;
719 #else
720   ACE_UNUSED_ARG (overlapped);
721   ACE_UNUSED_ARG (func);
723   number_of_bytes_sent = 0;
725   ssize_t result = 0;
727   for (int i = 0; i < buffer_count; ++i)
728     {
729        result = ACE_OS::sendto (handle,
730                                 reinterpret_cast<char *> (
731                                                  buffers[i].iov_base),
732                                 buffers[i].iov_len,
733                                 flags,
734                                 addr,
735                                 addrlen);
736        if (result == -1)
737          break;
738        number_of_bytes_sent += static_cast<size_t> (result);
739     }
741   return result;
742 #endif /* defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) */
745 ACE_INLINE ssize_t
746 ACE_OS::sendv (ACE_HANDLE handle,
747                const iovec *buffers,
748                int n)
750 #if defined (ACE_HAS_WINSOCK2)
751   DWORD bytes_sent = 0;
752   ssize_t result = 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,
758                       (WSABUF *) buffers,
759                       n,
760                       &bytes_sent,
761                       0,
762                       0,
763                       0);
764   if (result == SOCKET_ERROR)
765     {
766       ACE_OS::set_errno_to_wsa_last_error ();
767       return -1;
768     }
769 # else
770   for (int i = 0; i < n; ++i)
771     {
772       result = ::send ((SOCKET) handle,
773                        buffers[i].iov_base,
774                        buffers[i].iov_len,
775                        0);
777       if (result == SOCKET_ERROR)
778         {
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.
784           if (bytes_sent > 0)
785             break;
786           else
787             {
788               ACE_OS::set_errno_to_wsa_last_error ();
789               return -1;
790             }
791         }
792       else
793         {
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)
799             break;
800         }
801     }
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];
811   long total = 0;
812   long new_total = 0;
813   for (int i = 0; i < n; i++)
814     {
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)
820         {
821           local_iov[i].iov_len = ACE_HAS_SOCK_BUF_SIZE_MAX_VALUE - total;
822           n = i+1;
823           break;
824         }
825       total = new_total;
826     }
827   return ACE_OS::writev (handle, local_iov, n);
829 #else
830   return ACE_OS::writev (handle, buffers, n);
831 #endif /* ACE_HAS_WINSOCK2 */
834 ACE_INLINE int
835 ACE_OS::setsockopt (ACE_HANDLE handle,
836                     int level,
837                     int optname,
838                     const char *optval,
839                     int optlen)
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);
849 #else
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
860   // nothing.)
861   if (level == SOL_SOCKET) {
862     if (optname == SO_REUSEADDR) {
863       return 0; // Not supported by Winsock
864     }
865     if (optname == SO_REUSEPORT) {
866       optname = SO_REUSEADDR;
867     }
868   }
869 #endif /*ACE_HAS_WINSOCK2*/
871   int result;
872   ACE_SOCKCALL (::setsockopt ((ACE_SOCKET) handle,
873                               level,
874                               optname,
875                               (ACE_SOCKOPT_TYPE1) optval,
876                               optlen),
877                 int,
878                 -1,
879                 result);
880 #if defined (WSAEOPNOTSUPP)
881   if (result == -1 && errno == WSAEOPNOTSUPP)
882 #else
883   if (result == -1)
884 #endif /* WSAEOPNOTSUPP */
885     errno = ENOTSUP;
886   return result;
887 #endif
890 ACE_INLINE int
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);
898 #else
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,
905                 int type,
906                 int proto)
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);
914 #else
915   ACE_SOCKCALL_RETURN (::socket (domain,
916                                  type,
917                                  proto),
918                        ACE_HANDLE,
919                        ACE_INVALID_HANDLE);
920 #endif /* ACE_LACKS_SOCKET */
923 ACE_INLINE ACE_HANDLE
924 ACE_OS::socket (int domain,
925                 int type,
926                 int proto,
927                 ACE_Protocol_Info *protocolinfo,
928                 ACE_SOCK_GROUP g,
929                 u_long flags)
931   ACE_OS_TRACE ("ACE_OS::socket");
933 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
934   ACE_SOCKCALL_RETURN (::WSASocket (domain,
935                                     type,
936                                     proto,
937                                     protocolinfo,
938                                     g,
939                                     flags),
940                        ACE_HANDLE,
941                        ACE_INVALID_HANDLE);
942 #else
943   ACE_UNUSED_ARG (protocolinfo);
944   ACE_UNUSED_ARG (g);
945   ACE_UNUSED_ARG (flags);
947   return ACE_OS::socket (domain,
948                          type,
949                          proto);
950 #endif /* ACE_HAS_WINSOCK2 */
953 ACE_INLINE int
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);
962   ACE_UNUSED_ARG (sv);
964   ACE_NOTSUP_RETURN (-1);
965 #else
966   ACE_OSCALL_RETURN (::socketpair (domain, type, protocol, sv),
967                      int, -1);
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);
979 ACE_INLINE char *
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);
993 ACE_INLINE void
994 ACE_OS::if_freenameindex (struct if_nameindex *ptr)
996   ACE_OS_TRACE ("ACE_OS::if_freenameindex");
997   if (ptr != 0)
998     ::if_freenameindex (ptr);
1000 #endif /* __linux__ && ACE_HAS_IPV6 */
1002 ACE_END_VERSIONED_NAMESPACE_DECL