]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/recvfrom.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / mach / hurd / recvfrom.c
1 /* Copyright (C) 1994-2020 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
25 /* Read N bytes into BUF through socket FD.
26 If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
27 the sender, and store the actual size of the address in *ADDR_LEN.
28 Returns the number of bytes read or -1 for errors. */
29 ssize_t
30 __recvfrom (int fd, void *buf, size_t n, int flags, __SOCKADDR_ARG addrarg,
31 socklen_t *addr_len)
32 {
33 error_t err;
34 mach_port_t addrport;
35 char *bufp = buf;
36 mach_msg_type_number_t nread = n;
37 mach_port_t *ports;
38 mach_msg_type_number_t nports = 0;
39 char *cdata = NULL;
40 mach_msg_type_number_t clen = 0;
41 struct sockaddr *addr = addrarg.__sockaddr__;
42
43 if (err = HURD_DPORT_USE (fd, __socket_recv (port, &addrport,
44 flags, &bufp, &nread,
45 &ports, &nports,
46 &cdata, &clen,
47 &flags,
48 n)))
49 return __hurd_sockfail (fd, flags, err);
50
51 /* Get address data for the returned address port if requested. */
52 if (addr != NULL && addrport != MACH_PORT_NULL)
53 {
54 char *buf = (char *) addr;
55 mach_msg_type_number_t buflen = *addr_len;
56 int type;
57
58 err = __socket_whatis_address (addrport, &type, &buf, &buflen);
59 if (err == EOPNOTSUPP)
60 /* If the protocol server can't tell us the address, just return a
61 zero-length one. */
62 {
63 buf = (char *)addr;
64 buflen = 0;
65 err = 0;
66 }
67
68 if (err)
69 {
70 __mach_port_deallocate (__mach_task_self (), addrport);
71 return __hurd_sockfail (fd, flags, err);
72 }
73
74 if (*addr_len > buflen)
75 *addr_len = buflen;
76
77 if (buf != (char *) addr)
78 {
79 memcpy (addr, buf, *addr_len);
80 __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
81 }
82
83 if (buflen > 0)
84 addr->sa_family = type;
85 }
86 else if (addr_len != NULL)
87 *addr_len = 0;
88
89 __mach_port_deallocate (__mach_task_self (), addrport);
90
91 /* Toss control data; we don't care. */
92 __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
93
94 if (bufp != buf)
95 {
96 memcpy (buf, bufp, nread);
97 __vm_deallocate (__mach_task_self (), (vm_address_t) bufp, nread);
98 }
99
100 return nread;
101 }
102
103 weak_alias (__recvfrom, recvfrom)