1 /* Copyright (C) 2001, 2002 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 License as
6 published by the Free Software Foundation; either version 2.1 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 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; see the file COPYING.LIB. If
16 not, see <http://www.gnu.org/licenses/>. */
20 #include <sys/socket.h>
24 #include <hurd/socket.h>
26 /* Receive a message as described by MESSAGE from socket FD.
27 Returns the number of bytes read or -1 for errors. */
29 __libc_recvmsg (int fd
, struct msghdr
*message
, int flags
)
34 mach_msg_type_number_t len
= 0;
36 mach_msg_type_number_t nports
= 0;
38 mach_msg_type_number_t clen
= 0;
43 /* Find the total number of bytes to be read. */
45 for (i
= 0; i
< message
->msg_iovlen
; i
++)
47 amount
+= message
->msg_iov
[i
].iov_len
;
49 /* As an optimization, we set the initial values of DATA and LEN
50 from the first non-empty iovec. This kicks-in in the case
51 where the whole packet fits into that iovec buffer. */
52 if (data
== NULL
&& message
->msg_iov
[i
].iov_len
> 0)
54 data
= message
->msg_iov
[i
].iov_base
;
55 len
= message
->msg_iov
[i
].iov_len
;
60 if (err
= HURD_DPORT_USE (fd
, __socket_recv (port
, &aport
,
64 &message
->msg_flags
, amount
)))
65 return __hurd_sockfail (fd
, flags
, err
);
67 if (message
->msg_name
!= NULL
)
69 char *buf
= message
->msg_name
;
70 mach_msg_type_number_t buflen
= message
->msg_namelen
;
73 err
= __socket_whatis_address (aport
, &type
, &buf
, &buflen
);
74 if (err
== EOPNOTSUPP
)
75 /* If the protocol server can't tell us the address, just return a
78 buf
= message
->msg_name
;
85 __mach_port_deallocate (__mach_task_self (), aport
);
86 return __hurd_sockfail (fd
, flags
, err
);
89 if (message
->msg_namelen
> buflen
)
90 message
->msg_namelen
= buflen
;
92 if (buf
!= message
->msg_name
)
94 memcpy (message
->msg_name
, buf
, message
->msg_namelen
);
95 __vm_deallocate (__mach_task_self (), (vm_address_t
) buf
, buflen
);
99 ((struct sockaddr
*) message
->msg_name
)->sa_family
= type
;
102 __mach_port_deallocate (__mach_task_self (), aport
);
108 /* Copy the data into MSG. */
110 message
->msg_flags
|= MSG_TRUNC
;
115 for (i
= 0; i
< message
->msg_iovlen
; i
++)
117 #define min(a, b) ((a) > (b) ? (b) : (a))
118 size_t copy
= min (message
->msg_iov
[i
].iov_len
, amount
);
120 memcpy (message
->msg_iov
[i
].iov_base
, buf
, copy
);
128 __vm_deallocate (__mach_task_self (), (vm_address_t
) data
, len
);
131 /* Copy the control message into MSG. */
132 if (clen
> message
->msg_controllen
)
133 message
->msg_flags
|= MSG_CTRUNC
;
135 message
->msg_controllen
= clen
;
136 memcpy (message
->msg_control
, cdata
, message
->msg_controllen
);
138 __vm_deallocate (__mach_task_self (), (vm_address_t
) cdata
, clen
);
143 weak_alias (__libc_recvmsg
, recvmsg
)
144 weak_alias (__libc_recvmsg
, __recvmsg
)