/crypto/heimdal/lib/roken/sendmsg.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 148 lines · 40 code · 14 blank · 94 comment · 8 complexity · 19d884a6c942853a3a9d94e3bd531906 MD5 · raw file

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