]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 2001-2019 Free Software Foundation, Inc. |
5301af2d MK |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
cc7375ce RM |
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 | |
5301af2d MK |
7 | License, or (at your option) any later version. |
8 | ||
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 | |
cc7375ce | 12 | Lesser General Public License for more details. |
5301af2d | 13 | |
cc7375ce | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; see the file COPYING.LIB. If |
16 | not, see <http://www.gnu.org/licenses/>. */ | |
5301af2d MK |
17 | |
18 | #include <errno.h> | |
19 | #include <string.h> | |
20 | #include <sys/socket.h> | |
21 | ||
22 | #include <hurd.h> | |
23 | #include <hurd/fd.h> | |
24 | #include <hurd/socket.h> | |
25 | ||
26 | /* Receive a message as described by MESSAGE from socket FD. | |
27 | Returns the number of bytes read or -1 for errors. */ | |
28 | ssize_t | |
29 | __libc_recvmsg (int fd, struct msghdr *message, int flags) | |
30 | { | |
31 | error_t err; | |
32 | addr_port_t aport; | |
33 | char *data = NULL; | |
34 | mach_msg_type_number_t len = 0; | |
35 | mach_port_t *ports; | |
d5a0160b | 36 | mach_msg_type_number_t nports = 0; |
5301af2d MK |
37 | char *cdata = NULL; |
38 | mach_msg_type_number_t clen = 0; | |
39 | size_t amount; | |
40 | char *buf; | |
41 | int i; | |
42 | ||
43 | /* Find the total number of bytes to be read. */ | |
44 | amount = 0; | |
45 | for (i = 0; i < message->msg_iovlen; i++) | |
46 | { | |
47 | amount += message->msg_iov[i].iov_len; | |
48 | ||
49 | /* As an optimization, we set the initial values of DATA and LEN | |
50 | from the first non-empty iovec. This kicks-in in the case | |
51 | where the whole packet fits into that iovec buffer. */ | |
52 | if (data == NULL && message->msg_iov[i].iov_len > 0) | |
53 | { | |
54 | data = message->msg_iov[i].iov_base; | |
55 | len = message->msg_iov[i].iov_len; | |
56 | } | |
57 | } | |
58 | ||
59 | buf = data; | |
60 | if (err = HURD_DPORT_USE (fd, __socket_recv (port, &aport, | |
61 | flags, &data, &len, | |
62 | &ports, &nports, | |
63 | &cdata, &clen, | |
64 | &message->msg_flags, amount))) | |
e66ecb22 | 65 | return __hurd_sockfail (fd, flags, err); |
5301af2d | 66 | |
a194625e | 67 | if (message->msg_name != NULL && aport != MACH_PORT_NULL) |
5301af2d MK |
68 | { |
69 | char *buf = message->msg_name; | |
70 | mach_msg_type_number_t buflen = message->msg_namelen; | |
71 | int type; | |
72 | ||
73 | err = __socket_whatis_address (aport, &type, &buf, &buflen); | |
74 | if (err == EOPNOTSUPP) | |
75 | /* If the protocol server can't tell us the address, just return a | |
76 | zero-length one. */ | |
77 | { | |
78 | buf = message->msg_name; | |
79 | buflen = 0; | |
80 | err = 0; | |
81 | } | |
82 | ||
83 | if (err) | |
84 | { | |
85 | __mach_port_deallocate (__mach_task_self (), aport); | |
e66ecb22 | 86 | return __hurd_sockfail (fd, flags, err); |
5301af2d MK |
87 | } |
88 | ||
89 | if (message->msg_namelen > buflen) | |
90 | message->msg_namelen = buflen; | |
91 | ||
92 | if (buf != message->msg_name) | |
93 | { | |
94 | memcpy (message->msg_name, buf, message->msg_namelen); | |
95 | __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen); | |
96 | } | |
97 | ||
98 | if (buflen > 0) | |
99 | ((struct sockaddr *) message->msg_name)->sa_family = type; | |
100 | } | |
a194625e ST |
101 | else if (message->msg_name != NULL) |
102 | message->msg_namelen = 0; | |
5301af2d MK |
103 | |
104 | __mach_port_deallocate (__mach_task_self (), aport); | |
105 | ||
106 | if (buf == data) | |
107 | buf += len; | |
108 | else | |
109 | { | |
110 | /* Copy the data into MSG. */ | |
111 | if (len > amount) | |
112 | message->msg_flags |= MSG_TRUNC; | |
113 | else | |
114 | amount = len; | |
115 | ||
116 | buf = data; | |
117 | for (i = 0; i < message->msg_iovlen; i++) | |
118 | { | |
119 | #define min(a, b) ((a) > (b) ? (b) : (a)) | |
120 | size_t copy = min (message->msg_iov[i].iov_len, amount); | |
121 | ||
122 | memcpy (message->msg_iov[i].iov_base, buf, copy); | |
123 | ||
124 | buf += copy; | |
125 | amount -= copy; | |
126 | if (len == 0) | |
127 | break; | |
128 | } | |
129 | ||
130 | __vm_deallocate (__mach_task_self (), (vm_address_t) data, len); | |
131 | } | |
132 | ||
133 | /* Copy the control message into MSG. */ | |
134 | if (clen > message->msg_controllen) | |
135 | message->msg_flags |= MSG_CTRUNC; | |
136 | else | |
137 | message->msg_controllen = clen; | |
138 | memcpy (message->msg_control, cdata, message->msg_controllen); | |
139 | ||
140 | __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen); | |
141 | ||
142 | return (buf - data); | |
143 | } | |
144 | ||
145 | weak_alias (__libc_recvmsg, recvmsg) | |
b2bffca2 | 146 | weak_alias (__libc_recvmsg, __recvmsg) |