1295367Sdes/*
2276707Sdes * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska H�gskolan
3276707Sdes * (Royal Institute of Technology, Stockholm, Sweden).
4276707Sdes * All rights reserved.
5276707Sdes *
6276707Sdes * Redistribution and use in source and binary forms, with or without
7276707Sdes * modification, are permitted provided that the following conditions
8276707Sdes * are met:
9276707Sdes *
10276707Sdes * 1. Redistributions of source code must retain the above copyright
11276707Sdes *    notice, this list of conditions and the following disclaimer.
12276707Sdes *
13276707Sdes * 2. Redistributions in binary form must reproduce the above copyright
14276707Sdes *    notice, this list of conditions and the following disclaimer in the
15276707Sdes *    documentation and/or other materials provided with the distribution.
16276707Sdes *
17276707Sdes * 3. Neither the name of the Institute nor the names of its contributors
18276707Sdes *    may be used to endorse or promote products derived from this software
19276707Sdes *    without specific prior written permission.
20276707Sdes *
21276707Sdes * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22276707Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23276707Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24276707Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25276707Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26276707Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27276707Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28276707Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29276707Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30276707Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31276707Sdes * SUCH DAMAGE.
32276707Sdes */
33276707Sdes
34276707Sdes#ifdef HAVE_CONFIG_H
35276707Sdes#include <config.h>
36276707SdesRCSID("$Id: recvmsg.c 14773 2005-04-12 11:29:18Z lha $");
37276707Sdes#endif
38276707Sdes
39276707Sdes#include "roken.h"
40276707Sdes
41276707Sdesssize_t ROKEN_LIB_FUNCTION
42276707Sdesrecvmsg(int s, struct msghdr *msg, int flags)
43276707Sdes{
44276707Sdes    ssize_t ret, nb;
45276707Sdes    size_t tot = 0;
46276707Sdes    int i;
47276707Sdes    char *buf, *p;
48276707Sdes    struct iovec *iov = msg->msg_iov;
49276707Sdes
50276707Sdes    for(i = 0; i < msg->msg_iovlen; ++i)
51276707Sdes	tot += iov[i].iov_len;
52276707Sdes    buf = malloc(tot);
53276707Sdes    if (tot != 0 && buf == NULL) {
54276707Sdes	errno = ENOMEM;
55276707Sdes	return -1;
56276707Sdes    }
57276707Sdes    nb = ret = recvfrom (s, buf, tot, flags, msg->msg_name, &msg->msg_namelen);
58276707Sdes    p = buf;
59276707Sdes    while (nb > 0) {
60276707Sdes	ssize_t cnt = min(nb, iov->iov_len);
61276707Sdes
62276707Sdes	memcpy (iov->iov_base, p, cnt);
63276707Sdes	p += cnt;
64276707Sdes	nb -= cnt;
65276707Sdes	++iov;
66276707Sdes    }
67276707Sdes    free(buf);
68276707Sdes    return ret;
69276707Sdes}
70276707Sdes