/crypto/external/bsd/heimdal/dist/lib/roken/sendmsg.c

http://www.minix3.org/ · C · 150 lines · 40 code · 15 blank · 95 comment · 8 complexity · a9d9ad2a211fcc28b9004704a97251f7 MD5 · raw file

  1. /* $NetBSD: sendmsg.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
  2. /*
  3. * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Hรถgskolan
  4. * (Royal Institute of Technology, Stockholm, Sweden).
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * 3. Neither the name of the Institute nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <krb5/roken.h>
  36. #ifndef _WIN32
  37. ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL
  38. sendmsg(rk_socket_t s, const struct msghdr *msg, int flags)
  39. {
  40. ssize_t ret;
  41. size_t tot = 0;
  42. int i;
  43. char *buf, *p;
  44. struct iovec *iov = msg->msg_iov;
  45. for(i = 0; i < msg->msg_iovlen; ++i)
  46. tot += iov[i].iov_len;
  47. buf = malloc(tot);
  48. if (tot != 0 && buf == NULL) {
  49. errno = ENOMEM;
  50. return -1;
  51. }
  52. p = buf;
  53. for (i = 0; i < msg->msg_iovlen; ++i) {
  54. memcpy (p, iov[i].iov_base, iov[i].iov_len);
  55. p += iov[i].iov_len;
  56. }
  57. ret = sendto (s, buf, tot, flags, msg->msg_name, msg->msg_namelen);
  58. free (buf);
  59. return ret;
  60. }
  61. #else /* _WIN32 */
  62. /***********************************************************************
  63. * Copyright (c) 2009, Secure Endpoints Inc.
  64. * All rights reserved.
  65. *
  66. * Redistribution and use in source and binary forms, with or without
  67. * modification, are permitted provided that the following conditions
  68. * are met:
  69. *
  70. * - Redistributions of source code must retain the above copyright
  71. * notice, this list of conditions and the following disclaimer.
  72. *
  73. * - Redistributions in binary form must reproduce the above copyright
  74. * notice, this list of conditions and the following disclaimer in
  75. * the documentation and/or other materials provided with the
  76. * distribution.
  77. *
  78. * - Neither the name of Secure Endpoints Inc. nor the names of its
  79. * contributors may be used to endorse or promote products derived
  80. * from this software without specific prior written permission.
  81. *
  82. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  83. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  84. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  85. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  86. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  87. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  88. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  90. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  91. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  92. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  93. * OF THE POSSIBILITY OF SUCH DAMAGE.
  94. *
  95. **********************************************************************/
  96. /**
  97. * Implementation of sendmsg() for WIN32
  98. *
  99. * We are using a contrived definition of msghdr which actually uses
  100. * an array of ::_WSABUF structures instead of ::iovec . This allows
  101. * us to call WSASend directly using the given ::msghdr instead of
  102. * having to allocate another array of ::_WSABUF and copying data for
  103. * each call.
  104. *
  105. * Limitations:
  106. *
  107. * - msg->msg_name is ignored. So is msg->control.
  108. * - WSASend() only supports ::MSG_DONTROUTE, ::MSG_OOB and
  109. * ::MSG_PARTIAL.
  110. *
  111. * @param[in] s The socket to use.
  112. * @param[in] msg The message
  113. * @param[in] flags Flags. A combination of ::MSG_DONTROUTE,
  114. * ::MSG_OOB and ::MSG_PARTIAL
  115. *
  116. * @return The number of bytes sent, on success. Or -1 on error.
  117. */
  118. ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL
  119. sendmsg_w32(rk_socket_t s, const struct msghdr * msg, int flags)
  120. {
  121. int srv;
  122. DWORD num_bytes_sent = 0;
  123. /* TODO: For _WIN32_WINNT >= 0x0600 we can use WSASendMsg using
  124. WSAMSG which is a much more direct analogue to sendmsg(). */
  125. srv = WSASend(s, msg->msg_iov, msg->msg_iovlen,
  126. &num_bytes_sent, flags, NULL, NULL);
  127. if (srv == 0)
  128. return (int) num_bytes_sent;
  129. /* srv == SOCKET_ERROR and WSAGetLastError() == WSA_IO_PENDING
  130. indicates that a non-blocking transfer has been scheduled.
  131. We'll have to check for that if we ever support non-blocking
  132. I/O. */
  133. return -1;
  134. }
  135. #endif /* !_WIN32 */