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