1 | /* Copyright (C) 1994-2022 Free Software Foundation, Inc. |
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 |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the 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 |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <errno.h> |
19 | #include <string.h> |
20 | #include <sys/socket.h> |
21 | #include <hurd.h> |
22 | #include <hurd/fd.h> |
23 | #include <hurd/socket.h> |
24 | #include <sysdep-cancel.h> |
25 | |
26 | /* Read N bytes into BUF through socket FD. |
27 | If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of |
28 | the sender, and store the actual size of the address in *ADDR_LEN. |
29 | Returns the number of bytes read or -1 for errors. */ |
30 | ssize_t |
31 | __recvfrom (int fd, void *buf, size_t n, int flags, __SOCKADDR_ARG addrarg, |
32 | socklen_t *addr_len) |
33 | { |
34 | error_t err; |
35 | mach_port_t addrport; |
36 | char *bufp = buf; |
37 | mach_msg_type_number_t nread = n; |
38 | mach_port_t *ports; |
39 | mach_msg_type_number_t nports = 0; |
40 | char *cdata = NULL; |
41 | mach_msg_type_number_t clen = 0; |
42 | struct sockaddr *addr = addrarg.__sockaddr__; |
43 | int cancel_oldtype; |
44 | |
45 | cancel_oldtype = LIBC_CANCEL_ASYNC(); |
46 | err = HURD_DPORT_USE_CANCEL (fd, __socket_recv (port, &addrport, |
47 | flags, &bufp, &nread, |
48 | &ports, &nports, |
49 | &cdata, &clen, |
50 | &flags, |
51 | n)); |
52 | LIBC_CANCEL_RESET (cancel_oldtype); |
53 | |
54 | if (err) |
55 | return __hurd_sockfail (fd, flags, err); |
56 | |
57 | /* Get address data for the returned address port if requested. */ |
58 | if (addr != NULL && addrport != MACH_PORT_NULL) |
59 | { |
60 | char *buf = (char *) addr; |
61 | mach_msg_type_number_t buflen = *addr_len; |
62 | int type; |
63 | |
64 | cancel_oldtype = LIBC_CANCEL_ASYNC(); |
65 | err = __socket_whatis_address (addrport, &type, &buf, &buflen); |
66 | LIBC_CANCEL_RESET (cancel_oldtype); |
67 | if (err == EOPNOTSUPP) |
68 | /* If the protocol server can't tell us the address, just return a |
69 | zero-length one. */ |
70 | { |
71 | buf = (char *)addr; |
72 | buflen = 0; |
73 | err = 0; |
74 | } |
75 | |
76 | if (err) |
77 | { |
78 | __mach_port_deallocate (__mach_task_self (), addrport); |
79 | return __hurd_sockfail (fd, flags, err); |
80 | } |
81 | |
82 | if (*addr_len > buflen) |
83 | *addr_len = buflen; |
84 | |
85 | if (buf != (char *) addr) |
86 | { |
87 | memcpy (addr, buf, *addr_len); |
88 | __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen); |
89 | } |
90 | |
91 | if (buflen > 0) |
92 | addr->sa_family = type; |
93 | } |
94 | else if (addr_len != NULL) |
95 | *addr_len = 0; |
96 | |
97 | __mach_port_deallocate (__mach_task_self (), addrport); |
98 | |
99 | /* Toss control data; we don't care. */ |
100 | __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen); |
101 | |
102 | if (bufp != buf) |
103 | { |
104 | memcpy (buf, bufp, nread); |
105 | __vm_deallocate (__mach_task_self (), (vm_address_t) bufp, nread); |
106 | } |
107 | |
108 | return nread; |
109 | } |
110 | |
111 | weak_alias (__recvfrom, recvfrom) |
112 | |