66
77use std:: {
88 ffi:: CStr ,
9- io:: { Error , ErrorKind , Read , Result , Write } ,
10- mem:: size_of,
9+ io:: { Error , ErrorKind , Read as _, Result , Write as _} ,
1110 net:: IpAddr ,
1211 num:: TryFromIntError ,
1312 ptr, slice,
@@ -40,10 +39,10 @@ asserted_const_with_type!(NLM_F_REQUEST, u16, libc::NLM_F_REQUEST, c_int);
4039asserted_const_with_type ! ( NLM_F_ACK , u16 , libc:: NLM_F_ACK , c_int) ;
4140asserted_const_with_type ! ( NLMSG_ERROR , u16 , libc:: NLMSG_ERROR , c_int) ;
4241
43- const_assert ! ( size_of:: <nlmsghdr>( ) <= u8 :: MAX as usize ) ;
44- const_assert ! ( size_of:: <rtmsg>( ) <= u8 :: MAX as usize ) ;
45- const_assert ! ( size_of:: <rtattr>( ) <= u8 :: MAX as usize ) ;
46- const_assert ! ( size_of:: <ifinfomsg>( ) <= u8 :: MAX as usize ) ;
42+ const_assert ! ( std :: mem :: size_of:: <nlmsghdr>( ) <= u8 :: MAX as usize ) ;
43+ const_assert ! ( std :: mem :: size_of:: <rtmsg>( ) <= u8 :: MAX as usize ) ;
44+ const_assert ! ( std :: mem :: size_of:: <rtattr>( ) <= u8 :: MAX as usize ) ;
45+ const_assert ! ( std :: mem :: size_of:: <ifinfomsg>( ) <= u8 :: MAX as usize ) ;
4746
4847const NETLINK_BUFFER_SIZE : usize = 8192 ; // See netlink(7) man page.
4948
@@ -96,8 +95,10 @@ impl IfIndexMsg {
9695 let addr = AddrBytes :: new ( remote) ;
9796 #[ allow( clippy:: cast_possible_truncation) ]
9897 // Structs lens are <= u8::MAX per `const_assert!`s above; `addr_bytes` is max. 16 for IPv6.
99- let nlmsg_len =
100- ( size_of :: < nlmsghdr > ( ) + size_of :: < rtmsg > ( ) + size_of :: < rtattr > ( ) + addr. len ( ) ) as u32 ;
98+ let nlmsg_len = ( std:: mem:: size_of :: < nlmsghdr > ( )
99+ + std:: mem:: size_of :: < rtmsg > ( )
100+ + std:: mem:: size_of :: < rtattr > ( )
101+ + addr. len ( ) ) as u32 ;
101102 Self {
102103 nlmsg : nlmsghdr {
103104 nlmsg_len,
@@ -123,7 +124,7 @@ impl IfIndexMsg {
123124 rt : rtattr {
124125 #[ allow( clippy:: cast_possible_truncation) ]
125126 // Structs len is <= u8::MAX per `const_assert!` above; `addr_bytes` is max. 16 for IPv6.
126- rta_len : ( size_of :: < rtattr > ( ) + addr. len ( ) ) as u16 ,
127+ rta_len : ( std :: mem :: size_of :: < rtattr > ( ) + addr. len ( ) ) as u16 ,
127128 rta_type : RTA_DST ,
128129 } ,
129130 addr : addr. into ( ) ,
@@ -132,7 +133,7 @@ impl IfIndexMsg {
132133
133134 const fn len ( & self ) -> usize {
134135 let len = self . nlmsg . nlmsg_len as usize ;
135- debug_assert ! ( len <= size_of:: <Self >( ) ) ;
136+ debug_assert ! ( len <= std :: mem :: size_of:: <Self >( ) ) ;
136137 len
137138 }
138139}
@@ -147,16 +148,18 @@ impl TryFrom<&[u8]> for nlmsghdr {
147148 type Error = Error ;
148149
149150 fn try_from ( value : & [ u8 ] ) -> Result < Self > {
150- if value. len ( ) < size_of :: < Self > ( ) {
151+ if value. len ( ) < std :: mem :: size_of :: < Self > ( ) {
151152 return Err ( default_err ( ) ) ;
152153 }
153154 Ok ( unsafe { ptr:: read_unaligned ( value. as_ptr ( ) . cast ( ) ) } )
154155 }
155156}
156157
157158fn parse_c_int ( buf : & [ u8 ] ) -> Result < c_int > {
158- let bytes = <& [ u8 ] as TryInto < [ u8 ; size_of :: < c_int > ( ) ] > >:: try_into ( & buf[ ..size_of :: < c_int > ( ) ] )
159- . map_err ( |_| default_err ( ) ) ?;
159+ let bytes = <& [ u8 ] as TryInto < [ u8 ; std:: mem:: size_of :: < c_int > ( ) ] > >:: try_into (
160+ & buf[ ..std:: mem:: size_of :: < c_int > ( ) ] ,
161+ )
162+ . map_err ( |_| default_err ( ) ) ?;
160163 Ok ( c_int:: from_ne_bytes ( bytes) )
161164}
162165
@@ -165,13 +168,13 @@ fn read_msg_with_seq(fd: &mut RouteSocket, seq: u32, kind: u16) -> Result<(nlmsg
165168 let buf = & mut [ 0u8 ; NETLINK_BUFFER_SIZE ] ;
166169 let len = fd. read ( buf. as_mut_slice ( ) ) ?;
167170 let mut next = & buf[ ..len] ;
168- while size_of :: < nlmsghdr > ( ) <= next. len ( ) {
169- let ( hdr, mut msg) = next. split_at ( size_of :: < nlmsghdr > ( ) ) ;
171+ while std :: mem :: size_of :: < nlmsghdr > ( ) <= next. len ( ) {
172+ let ( hdr, mut msg) = next. split_at ( std :: mem :: size_of :: < nlmsghdr > ( ) ) ;
170173 let hdr: nlmsghdr = hdr. try_into ( ) ?;
171174 // `msg` has the remainder of this message plus any following messages.
172175 // Strip those it off and assign them to `next`.
173- debug_assert ! ( size_of:: <nlmsghdr>( ) <= hdr. nlmsg_len as usize ) ;
174- ( msg, next) = msg. split_at ( hdr. nlmsg_len as usize - size_of :: < nlmsghdr > ( ) ) ;
176+ debug_assert ! ( std :: mem :: size_of:: <nlmsghdr>( ) <= hdr. nlmsg_len as usize ) ;
177+ ( msg, next) = msg. split_at ( hdr. nlmsg_len as usize - std :: mem :: size_of :: < nlmsghdr > ( ) ) ;
175178
176179 if hdr. nlmsg_seq != seq {
177180 continue ;
@@ -195,7 +198,7 @@ impl TryFrom<&[u8]> for rtattr {
195198 type Error = Error ;
196199
197200 fn try_from ( value : & [ u8 ] ) -> Result < Self > {
198- if value. len ( ) < size_of :: < Self > ( ) {
201+ if value. len ( ) < std :: mem :: size_of :: < Self > ( ) {
199202 return Err ( default_err ( ) ) ;
200203 }
201204 Ok ( unsafe { ptr:: read_unaligned ( value. as_ptr ( ) . cast ( ) ) } )
@@ -209,12 +212,12 @@ struct RtAttr<'a> {
209212
210213impl < ' a > RtAttr < ' a > {
211214 fn new ( bytes : & ' a [ u8 ] ) -> Result < Self > {
212- debug_assert ! ( bytes. len( ) >= size_of:: <rtattr>( ) ) ;
213- let ( hdr, mut msg) = bytes. split_at ( size_of :: < rtattr > ( ) ) ;
215+ debug_assert ! ( bytes. len( ) >= std :: mem :: size_of:: <rtattr>( ) ) ;
216+ let ( hdr, mut msg) = bytes. split_at ( std :: mem :: size_of :: < rtattr > ( ) ) ;
214217 let hdr: rtattr = hdr. try_into ( ) ?;
215218 let aligned_len = aligned_by ( hdr. rta_len . into ( ) , 4 ) ;
216- debug_assert ! ( size_of:: <rtattr>( ) <= aligned_len) ;
217- ( msg, _) = msg. split_at ( aligned_len - size_of :: < rtattr > ( ) ) ;
219+ debug_assert ! ( std :: mem :: size_of:: <rtattr>( ) <= aligned_len) ;
220+ ( msg, _) = msg. split_at ( aligned_len - std :: mem :: size_of :: < rtattr > ( ) ) ;
218221 Ok ( Self { hdr, msg } )
219222 }
220223}
@@ -225,7 +228,7 @@ impl<'a> Iterator for RtAttrs<'a> {
225228 type Item = RtAttr < ' a > ;
226229
227230 fn next ( & mut self ) -> Option < Self :: Item > {
228- if size_of :: < rtattr > ( ) <= self . 0 . len ( ) {
231+ if std :: mem :: size_of :: < rtattr > ( ) <= self . 0 . len ( ) {
229232 let attr = RtAttr :: new ( self . 0 ) . ok ( ) ?;
230233 let aligned_len = aligned_by ( attr. hdr . rta_len . into ( ) , 4 ) ;
231234 debug_assert ! ( self . 0 . len( ) >= aligned_len) ;
@@ -245,8 +248,8 @@ fn if_index(remote: IpAddr, fd: &mut RouteSocket) -> Result<i32> {
245248
246249 // Receive RTM_GETROUTE response.
247250 let ( _hdr, mut buf) = read_msg_with_seq ( fd, msg_seq, RTM_NEWROUTE ) ?;
248- debug_assert ! ( size_of:: <rtmsg>( ) <= buf. len( ) ) ;
249- let buf = buf. split_off ( size_of :: < rtmsg > ( ) ) ;
251+ debug_assert ! ( std :: mem :: size_of:: <rtmsg>( ) <= buf. len( ) ) ;
252+ let buf = buf. split_off ( std :: mem :: size_of :: < rtmsg > ( ) ) ;
250253
251254 // Parse through the attributes to find the interface index.
252255 for attr in RtAttrs ( buf. as_slice ( ) ) . by_ref ( ) {
@@ -268,7 +271,7 @@ impl IfInfoMsg {
268271 fn new ( if_index : i32 , nlmsg_seq : u32 ) -> Self {
269272 #[ allow( clippy:: cast_possible_truncation) ]
270273 // Structs lens are <= u8::MAX per `const_assert!`s above.
271- let nlmsg_len = ( size_of :: < nlmsghdr > ( ) + size_of :: < ifinfomsg > ( ) ) as u32 ;
274+ let nlmsg_len = ( std :: mem :: size_of :: < nlmsghdr > ( ) + std :: mem :: size_of :: < ifinfomsg > ( ) ) as u32 ;
272275 Self {
273276 nlmsg : nlmsghdr {
274277 nlmsg_len,
@@ -293,7 +296,7 @@ impl IfInfoMsg {
293296
294297impl From < & IfInfoMsg > for & [ u8 ] {
295298 fn from ( value : & IfInfoMsg ) -> Self {
296- debug_assert ! ( value. len( ) >= size_of:: <Self >( ) ) ;
299+ debug_assert ! ( value. len( ) >= std :: mem :: size_of:: <Self >( ) ) ;
297300 unsafe { slice:: from_raw_parts ( ptr:: from_ref ( value) . cast ( ) , value. len ( ) ) }
298301 }
299302}
@@ -306,8 +309,8 @@ fn if_name_mtu(if_index: i32, fd: &mut RouteSocket) -> Result<(String, usize)> {
306309
307310 // Receive RTM_GETLINK response.
308311 let ( _hdr, mut buf) = read_msg_with_seq ( fd, msg_seq, RTM_NEWLINK ) ?;
309- debug_assert ! ( size_of:: <ifinfomsg>( ) <= buf. len( ) ) ;
310- let buf = buf. split_off ( size_of :: < ifinfomsg > ( ) ) ;
312+ debug_assert ! ( std :: mem :: size_of:: <ifinfomsg>( ) <= buf. len( ) ) ;
313+ let buf = buf. split_off ( std :: mem :: size_of :: < ifinfomsg > ( ) ) ;
311314
312315 // Parse through the attributes to find the interface name and MTU.
313316 let mut ifname = None ;
0 commit comments