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