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