Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / recvfrom.c
blob4e1a3eb8c71498bf7faf3f62cee6ffcb2113e31a
1 /* Copyright (C) 1994-2014 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
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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; if not, see
16 <http://www.gnu.org/licenses/>. */
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>
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 (fd, buf, n, flags, addrarg, addr_len)
31 int fd;
32 void *buf;
33 size_t n;
34 int flags;
35 __SOCKADDR_ARG addrarg;
36 socklen_t *addr_len;
38 error_t err;
39 mach_port_t addrport;
40 char *bufp = buf;
41 mach_msg_type_number_t nread = n;
42 mach_port_t *ports;
43 mach_msg_type_number_t nports = 0;
44 char *cdata = NULL;
45 mach_msg_type_number_t clen = 0;
46 struct sockaddr *addr = addrarg.__sockaddr__;
48 if (err = HURD_DPORT_USE (fd, __socket_recv (port, &addrport,
49 flags, &bufp, &nread,
50 &ports, &nports,
51 &cdata, &clen,
52 &flags,
53 n)))
54 return __hurd_sockfail (fd, flags, err);
56 /* Get address data for the returned address port if requested. */
57 if (addr != NULL && addrport != MACH_PORT_NULL)
59 char *buf = (char *) addr;
60 mach_msg_type_number_t buflen = *addr_len;
61 int type;
63 err = __socket_whatis_address (addrport, &type, &buf, &buflen);
64 if (err == EOPNOTSUPP)
65 /* If the protocol server can't tell us the address, just return a
66 zero-length one. */
68 buf = (char *)addr;
69 buflen = 0;
70 err = 0;
73 if (err)
75 __mach_port_deallocate (__mach_task_self (), addrport);
76 return __hurd_sockfail (fd, flags, err);
79 if (*addr_len > buflen)
80 *addr_len = buflen;
82 if (buf != (char *) addr)
84 memcpy (addr, buf, *addr_len);
85 __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
88 if (buflen > 0)
89 addr->sa_family = type;
91 else if (addr_len != NULL)
92 *addr_len = 0;
94 __mach_port_deallocate (__mach_task_self (), addrport);
96 /* Toss control data; we don't care. */
97 __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
99 if (bufp != buf)
101 memcpy (buf, bufp, nread);
102 __vm_deallocate (__mach_task_self (), (vm_address_t) bufp, nread);
105 return nread;
108 weak_alias (__recvfrom, recvfrom)