2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / recvmsg.c
blob33897b8ce8cae0f50ed221d531e65944fdba004e
1 /* Copyright (C) 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/socket.h>
23 #include <hurd.h>
24 #include <hurd/fd.h>
25 #include <hurd/socket.h>
27 /* Receive a message as described by MESSAGE from socket FD.
28 Returns the number of bytes read or -1 for errors. */
29 ssize_t
30 __libc_recvmsg (int fd, struct msghdr *message, int flags)
32 error_t err;
33 addr_port_t aport;
34 char *data = NULL;
35 mach_msg_type_number_t len = 0;
36 mach_port_t *ports;
37 mach_msg_type_number_t nports = 0;
38 char *cdata = NULL;
39 mach_msg_type_number_t clen = 0;
40 size_t amount;
41 char *buf;
42 int i;
44 /* Find the total number of bytes to be read. */
45 amount = 0;
46 for (i = 0; i < message->msg_iovlen; i++)
48 amount += message->msg_iov[i].iov_len;
50 /* As an optimization, we set the initial values of DATA and LEN
51 from the first non-empty iovec. This kicks-in in the case
52 where the whole packet fits into that iovec buffer. */
53 if (data == NULL && message->msg_iov[i].iov_len > 0)
55 data = message->msg_iov[i].iov_base;
56 len = message->msg_iov[i].iov_len;
60 buf = data;
61 if (err = HURD_DPORT_USE (fd, __socket_recv (port, &aport,
62 flags, &data, &len,
63 &ports, &nports,
64 &cdata, &clen,
65 &message->msg_flags, amount)))
66 return __hurd_sockfail (fd, flags, err);
68 if (message->msg_name != NULL)
70 char *buf = message->msg_name;
71 mach_msg_type_number_t buflen = message->msg_namelen;
72 int type;
74 err = __socket_whatis_address (aport, &type, &buf, &buflen);
75 if (err == EOPNOTSUPP)
76 /* If the protocol server can't tell us the address, just return a
77 zero-length one. */
79 buf = message->msg_name;
80 buflen = 0;
81 err = 0;
84 if (err)
86 __mach_port_deallocate (__mach_task_self (), aport);
87 return __hurd_sockfail (fd, flags, err);
90 if (message->msg_namelen > buflen)
91 message->msg_namelen = buflen;
93 if (buf != message->msg_name)
95 memcpy (message->msg_name, buf, message->msg_namelen);
96 __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
99 if (buflen > 0)
100 ((struct sockaddr *) message->msg_name)->sa_family = type;
103 __mach_port_deallocate (__mach_task_self (), aport);
105 if (buf == data)
106 buf += len;
107 else
109 /* Copy the data into MSG. */
110 if (len > amount)
111 message->msg_flags |= MSG_TRUNC;
112 else
113 amount = len;
115 buf = data;
116 for (i = 0; i < message->msg_iovlen; i++)
118 #define min(a, b) ((a) > (b) ? (b) : (a))
119 size_t copy = min (message->msg_iov[i].iov_len, amount);
121 memcpy (message->msg_iov[i].iov_base, buf, copy);
123 buf += copy;
124 amount -= copy;
125 if (len == 0)
126 break;
129 __vm_deallocate (__mach_task_self (), (vm_address_t) data, len);
132 /* Copy the control message into MSG. */
133 if (clen > message->msg_controllen)
134 message->msg_flags |= MSG_CTRUNC;
135 else
136 message->msg_controllen = clen;
137 memcpy (message->msg_control, cdata, message->msg_controllen);
139 __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
141 return (buf - data);
144 weak_alias (__libc_recvmsg, recvmsg)
145 weak_alias (__libc_recvmsg, __recvmsg)