@@ -877,6 +877,15 @@ pub struct RecvMsgResult {
877877 pub address : Option < RawSocketAddress > ,
878878}
879879
880+ #[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
881+ #[ derive( Debug ) ]
882+ pub struct RecvMsgIntoResult {
883+ pub bytes_read : usize ,
884+ pub ancdata : Vec < AncillaryMessage > ,
885+ pub msg_flags : i32 ,
886+ pub address : Option < RawSocketAddress > ,
887+ }
888+
880889#[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
881890#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
882891pub enum AncillaryPackError {
@@ -1001,33 +1010,79 @@ pub fn recvmsg(
10011010 MaybeUninit :: uninit ( )
10021011 } ;
10031012 let mut data_buf = vec ! [ initial; bufsize] ;
1004- let mut anc_buf: Vec < MaybeUninit < u8 > > = vec ! [ MaybeUninit :: uninit( ) ; ancbufsize] ;
1005- let mut addr_storage: libc:: sockaddr_storage = unsafe { core:: mem:: zeroed ( ) } ;
1013+ let result = recvmsg_into ( fd, & mut [ & mut data_buf] , ancbufsize, flags) ?;
1014+ let data = unsafe {
1015+ // Datagram MSG_TRUNC may report more bytes than the supplied storage.
1016+ data_buf. set_len ( result. bytes_read . min ( bufsize) ) ;
1017+ core:: mem:: transmute :: < Vec < MaybeUninit < u8 > > , Vec < u8 > > ( data_buf)
1018+ } ;
1019+ Ok ( RecvMsgResult {
1020+ data,
1021+ ancdata : result. ancdata ,
1022+ msg_flags : result. msg_flags ,
1023+ address : result. address ,
1024+ } )
1025+ }
10061026
1007- let mut iov = [ libc:: iovec {
1008- iov_base : data_buf. as_mut_ptr ( ) . cast ( ) ,
1009- iov_len : bufsize,
1010- } ] ;
1027+ #[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
1028+ pub fn recvmsg_into (
1029+ fd : BorrowedFd < ' _ > ,
1030+ buffers : & mut [ & mut [ core:: mem:: MaybeUninit < u8 > ] ] ,
1031+ ancbufsize : usize ,
1032+ flags : i32 ,
1033+ ) -> io:: Result < RecvMsgIntoResult > {
1034+ use core:: mem:: MaybeUninit ;
10111035
1036+ let as_iovec = |buf : & mut [ MaybeUninit < u8 > ] | libc:: iovec {
1037+ iov_base : buf. as_mut_ptr ( ) . cast ( ) ,
1038+ iov_len : buf. len ( ) ,
1039+ } ;
1040+ let mut single;
1041+ let mut multiple;
1042+ let iov: & mut [ libc:: iovec ] = if let [ buf] = buffers {
1043+ single = [ as_iovec ( buf) ] ;
1044+ & mut single
1045+ } else {
1046+ multiple = buffers
1047+ . iter_mut ( )
1048+ . map ( |buf| as_iovec ( buf) )
1049+ . collect :: < Vec < _ > > ( ) ;
1050+ & mut multiple
1051+ } ;
1052+ // CMSG_* requires aligned headers; initialize padding before exposing bytes.
1053+ let mut anc_buf = vec ! [
1054+ MaybeUninit :: <libc:: cmsghdr>:: zeroed( ) ;
1055+ ancbufsize. div_ceil( core:: mem:: size_of:: <libc:: cmsghdr>( ) )
1056+ ] ;
1057+ let mut addr_storage: libc:: sockaddr_storage = unsafe { core:: mem:: zeroed ( ) } ;
10121058 let mut msg: libc:: msghdr = unsafe { core:: mem:: zeroed ( ) } ;
10131059 msg. msg_name = ( & mut addr_storage as * mut libc:: sockaddr_storage ) . cast ( ) ;
10141060 msg. msg_namelen = core:: mem:: size_of :: < libc:: sockaddr_storage > ( ) as libc:: socklen_t ;
10151061 msg. msg_iov = iov. as_mut_ptr ( ) ;
1016- msg. msg_iovlen = 1 ;
1062+ // These msghdr fields are narrower on some Unix targets.
1063+ #[ allow( clippy:: useless_conversion) ]
1064+ let iovlen = iov
1065+ . len ( )
1066+ . try_into ( )
1067+ . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: InvalidInput ) ) ?;
1068+ msg. msg_iovlen = iovlen;
10171069 if ancbufsize > 0 {
10181070 msg. msg_control = anc_buf. as_mut_ptr ( ) . cast ( ) ;
1019- msg. msg_controllen = ancbufsize as _ ;
1071+ #[ allow( clippy:: useless_conversion) ]
1072+ let controllen = ancbufsize
1073+ . try_into ( )
1074+ . map_err ( |_| io:: Error :: from ( io:: ErrorKind :: InvalidInput ) ) ?;
1075+ msg. msg_controllen = controllen;
10201076 }
10211077
1078+ // SAFETY: all iovecs refer to disjoint writable slices; the control and address
1079+ // buffers are aligned and live until the syscall returns.
10221080 let ret = unsafe { libc:: recvmsg ( fd. as_raw_fd ( ) , & mut msg, flags) } . check_libc_neg ( ) ?;
1023-
1024- let data = unsafe {
1025- // Datagram MSG_TRUNC may report more bytes than the supplied storage.
1026- data_buf. set_len ( ( ret as usize ) . min ( bufsize) ) ;
1027- core:: mem:: transmute :: < Vec < MaybeUninit < u8 > > , Vec < u8 > > ( data_buf)
1028- } ;
10291081 let control = unsafe {
1030- core:: slice:: from_raw_parts ( anc_buf. as_ptr ( ) . cast :: < u8 > ( ) , msg. msg_controllen as usize )
1082+ core:: slice:: from_raw_parts (
1083+ anc_buf. as_ptr ( ) . cast :: < u8 > ( ) ,
1084+ ( msg. msg_controllen as usize ) . min ( ancbufsize) ,
1085+ )
10311086 } ;
10321087 let ancdata = parse_ancillary_messages ( control) ;
10331088 let address = if msg. msg_namelen > 0 {
@@ -1041,9 +1096,8 @@ pub fn recvmsg(
10411096 } else {
10421097 None
10431098 } ;
1044-
1045- Ok ( RecvMsgResult {
1046- data,
1099+ Ok ( RecvMsgIntoResult {
1100+ bytes_read : ret as usize ,
10471101 ancdata,
10481102 msg_flags : msg. msg_flags ,
10491103 address,
0 commit comments