Wed May 29 12:53:10 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / sysdeps / mach / hurd / recvfrom.c
blob4a69af41ce2e0fec304f728207d3136cd8bd6d53
1 /* Copyright (C) 1994 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <sys/socket.h>
22 #include <hurd.h>
23 #include <hurd/fd.h>
24 #include <hurd/socket.h>
25 #include <string.h>
27 /* Read N bytes into BUF through socket FD from peer
28 at address ADDR (which is ADDR_LEN bytes long).
29 Returns the number read or -1 for errors. */
30 int
31 DEFUN(recvfrom, (fd, buf, n, flags, addr, addr_len),
32 int fd AND PTR buf AND size_t n AND int flags AND
33 struct sockaddr *addr AND size_t *addr_len)
35 error_t err;
36 mach_port_t addrport;
37 char *bufp = buf;
38 mach_msg_type_number_t nread = n;
39 mach_port_t *ports;
40 mach_msg_type_number_t nports;
41 char *cdata = NULL;
42 mach_msg_type_number_t clen = 0;
44 if (err = HURD_DPORT_USE (fd, __socket_recv (port, &addrport,
45 flags, &bufp, &nread,
46 &ports, &nports,
47 &cdata, &clen,
48 &flags,
49 n)))
50 return __hurd_dfail (fd, err);
52 /* Get address data for the returned address port. */
54 char *buf = (char *) addr;
55 mach_msg_type_number_t buflen = *addr_len;
56 int type;
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. */
63 buf = (char *)addr;
64 buflen = 0;
65 err = 0;
67 __mach_port_deallocate (__mach_task_self (), addrport);
68 if (err)
69 return __hurd_dfail (fd, err);
71 if (buf != (char *) addr)
73 if (*addr_len < buflen)
74 *addr_len = buflen;
75 memcpy (addr, buf, *addr_len);
76 __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
79 if (buflen > 0)
80 addr->sa_family = type;
83 /* Toss control data; we don't care. */
84 __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
86 if (bufp != buf)
88 memcpy (buf, bufp, nread);
89 __vm_deallocate (__mach_task_self (), (vm_address_t) bufp, nread);
92 return nread;