Update to LGPL v2.1.
[jlayton/glibc.git] / sysdeps / mach / hurd / recvfrom.c
1 /* Copyright (C) 1994, 1997, 1999, 2001 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <hurd.h>
23 #include <hurd/fd.h>
24 #include <hurd/socket.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 (fd, buf, n, flags, addrarg, addr_len)
32      int fd;
33      void *buf;
34      size_t n;
35      int flags;
36      __SOCKADDR_ARG addrarg;
37      socklen_t *addr_len;
38 {
39   error_t err;
40   mach_port_t addrport;
41   char *bufp = buf;
42   mach_msg_type_number_t nread = n;
43   mach_port_t *ports;
44   mach_msg_type_number_t nports;
45   char *cdata = NULL;
46   mach_msg_type_number_t clen = 0;
47   struct sockaddr *addr = addrarg.__sockaddr__;
48
49   if (err = HURD_DPORT_USE (fd, __socket_recv (port, &addrport,
50                                                flags, &bufp, &nread,
51                                                &ports, &nports,
52                                                &cdata, &clen,
53                                                &flags,
54                                                n)))
55     return __hurd_dfail (fd, err);
56
57   /* Get address data for the returned address port if requested.  */
58   if (addr != NULL)
59     {
60       char *buf = (char *) addr;
61       mach_msg_type_number_t buflen = *addr_len;
62       int type;
63
64       err = __socket_whatis_address (addrport, &type, &buf, &buflen);
65       if (err == EOPNOTSUPP)
66         /* If the protocol server can't tell us the address, just return a
67            zero-length one.  */
68         {
69           buf = (char *)addr;
70           buflen = 0;
71           err = 0;
72         }
73
74       if (err)
75         {
76           __mach_port_deallocate (__mach_task_self (), addrport);
77           return __hurd_dfail (fd, err);
78         }
79       
80       if (*addr_len > buflen)
81         *addr_len = buflen;
82
83       if (buf != (char *) addr)
84         {
85           memcpy (addr, buf, *addr_len);
86           __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
87         }
88
89       if (buflen > 0)
90         addr->sa_family = type;
91     }
92
93   __mach_port_deallocate (__mach_task_self (), addrport);
94
95   /* Toss control data; we don't care.  */
96   __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
97
98   if (bufp != buf)
99     {
100       memcpy (buf, bufp, nread);
101       __vm_deallocate (__mach_task_self (), (vm_address_t) bufp, nread);
102     }
103
104   return nread;
105 }