33use std:: {
44 io:: { Error , Result } ,
55 mem,
6- os:: unix:: io:: { AsRawFd , FromRawFd , RawFd } ,
6+ os:: {
7+ fd:: { AsFd , BorrowedFd , FromRawFd } ,
8+ unix:: io:: { AsRawFd , RawFd } ,
9+ } ,
710} ;
811
912use crate :: SocketAddr ;
@@ -60,6 +63,12 @@ impl AsRawFd for Socket {
6063 }
6164}
6265
66+ impl AsFd for Socket {
67+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
68+ unsafe { BorrowedFd :: borrow_raw ( self . 0 ) }
69+ }
70+ }
71+
6372impl FromRawFd for Socket {
6473 unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
6574 Socket ( fd)
@@ -68,7 +77,7 @@ impl FromRawFd for Socket {
6877
6978impl Drop for Socket {
7079 fn drop ( & mut self ) {
71- unsafe { libc:: close ( self . 0 ) } ;
80+ unsafe { libc:: close ( self . as_raw_fd ( ) ) } ;
7281 }
7382}
7483
@@ -94,7 +103,7 @@ impl Socket {
94103 /// Bind the socket to the given address
95104 pub fn bind ( & mut self , addr : & SocketAddr ) -> Result < ( ) > {
96105 let ( addr_ptr, addr_len) = addr. as_raw ( ) ;
97- let res = unsafe { libc:: bind ( self . 0 , addr_ptr, addr_len) } ;
106+ let res = unsafe { libc:: bind ( self . as_raw_fd ( ) , addr_ptr, addr_len) } ;
98107 if res < 0 {
99108 return Err ( Error :: last_os_error ( ) ) ;
100109 }
@@ -115,7 +124,9 @@ impl Socket {
115124 let ( addr_ptr, mut addr_len) = addr. as_raw_mut ( ) ;
116125 let addr_len_copy = addr_len;
117126 let addr_len_ptr = & mut addr_len as * mut libc:: socklen_t ;
118- let res = unsafe { libc:: getsockname ( self . 0 , addr_ptr, addr_len_ptr) } ;
127+ let res = unsafe {
128+ libc:: getsockname ( self . as_raw_fd ( ) , addr_ptr, addr_len_ptr)
129+ } ;
119130 if res < 0 {
120131 return Err ( Error :: last_os_error ( ) ) ;
121132 }
@@ -128,8 +139,9 @@ impl Socket {
128139 /// Make this socket non-blocking
129140 pub fn set_non_blocking ( & self , non_blocking : bool ) -> Result < ( ) > {
130141 let mut non_blocking = non_blocking as libc:: c_int ;
131- let res =
132- unsafe { libc:: ioctl ( self . 0 , libc:: FIONBIO , & mut non_blocking) } ;
142+ let res = unsafe {
143+ libc:: ioctl ( self . as_raw_fd ( ) , libc:: FIONBIO , & mut non_blocking)
144+ } ;
133145 if res < 0 {
134146 return Err ( Error :: last_os_error ( ) ) ;
135147 }
@@ -152,7 +164,7 @@ impl Socket {
152164 /// 2. connect it to the kernel with [`Socket::connect`]
153165 /// 3. send a request to the kernel with [`Socket::send`]
154166 /// 4. read the response (which can span over several messages)
155- /// [`Socket::recv`]
167+ /// [`Socket::recv`]
156168 ///
157169 /// ```rust
158170 /// use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
@@ -216,7 +228,7 @@ impl Socket {
216228 // - https://stackoverflow.com/a/14046386/1836144
217229 // - https://lists.isc.org/pipermail/bind-users/2009-August/077527.html
218230 let ( addr, addr_len) = remote_addr. as_raw ( ) ;
219- let res = unsafe { libc:: connect ( self . 0 , addr, addr_len) } ;
231+ let res = unsafe { libc:: connect ( self . as_raw_fd ( ) , addr, addr_len) } ;
220232 if res < 0 {
221233 return Err ( Error :: last_os_error ( ) ) ;
222234 }
@@ -293,7 +305,7 @@ impl Socket {
293305
294306 let res = unsafe {
295307 libc:: recvfrom (
296- self . 0 ,
308+ self . as_raw_fd ( ) ,
297309 buf_ptr,
298310 buf_len,
299311 flags,
@@ -324,7 +336,8 @@ impl Socket {
324336 let buf_ptr = chunk. as_mut_ptr ( ) as * mut libc:: c_void ;
325337 let buf_len = chunk. len ( ) as libc:: size_t ;
326338
327- let res = unsafe { libc:: recv ( self . 0 , buf_ptr, buf_len, flags) } ;
339+ let res =
340+ unsafe { libc:: recv ( self . as_raw_fd ( ) , buf_ptr, buf_len, flags) } ;
328341 if res < 0 {
329342 return Err ( Error :: last_os_error ( ) ) ;
330343 } else {
@@ -367,7 +380,14 @@ impl Socket {
367380 let buf_len = buf. len ( ) as libc:: size_t ;
368381
369382 let res = unsafe {
370- libc:: sendto ( self . 0 , buf_ptr, buf_len, flags, addr_ptr, addr_len)
383+ libc:: sendto (
384+ self . as_raw_fd ( ) ,
385+ buf_ptr,
386+ buf_len,
387+ flags,
388+ addr_ptr,
389+ addr_len,
390+ )
371391 } ;
372392 if res < 0 {
373393 return Err ( Error :: last_os_error ( ) ) ;
@@ -382,7 +402,8 @@ impl Socket {
382402 let buf_ptr = buf. as_ptr ( ) as * const libc:: c_void ;
383403 let buf_len = buf. len ( ) as libc:: size_t ;
384404
385- let res = unsafe { libc:: send ( self . 0 , buf_ptr, buf_len, flags) } ;
405+ let res =
406+ unsafe { libc:: send ( self . as_raw_fd ( ) , buf_ptr, buf_len, flags) } ;
386407 if res < 0 {
387408 return Err ( Error :: last_os_error ( ) ) ;
388409 }
@@ -391,12 +412,17 @@ impl Socket {
391412
392413 pub fn set_pktinfo ( & mut self , value : bool ) -> Result < ( ) > {
393414 let value: libc:: c_int = value. into ( ) ;
394- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_PKTINFO , value)
415+ setsockopt (
416+ self . as_raw_fd ( ) ,
417+ libc:: SOL_NETLINK ,
418+ libc:: NETLINK_PKTINFO ,
419+ value,
420+ )
395421 }
396422
397423 pub fn get_pktinfo ( & self ) -> Result < bool > {
398424 let res = getsockopt :: < libc:: c_int > (
399- self . 0 ,
425+ self . as_raw_fd ( ) ,
400426 libc:: SOL_NETLINK ,
401427 libc:: NETLINK_PKTINFO ,
402428 ) ?;
@@ -405,7 +431,7 @@ impl Socket {
405431
406432 pub fn add_membership ( & mut self , group : u32 ) -> Result < ( ) > {
407433 setsockopt (
408- self . 0 ,
434+ self . as_raw_fd ( ) ,
409435 libc:: SOL_NETLINK ,
410436 libc:: NETLINK_ADD_MEMBERSHIP ,
411437 group,
@@ -414,7 +440,7 @@ impl Socket {
414440
415441 pub fn drop_membership ( & mut self , group : u32 ) -> Result < ( ) > {
416442 setsockopt (
417- self . 0 ,
443+ self . as_raw_fd ( ) ,
418444 libc:: SOL_NETLINK ,
419445 libc:: NETLINK_DROP_MEMBERSHIP ,
420446 group,
@@ -434,7 +460,7 @@ impl Socket {
434460 pub fn set_broadcast_error ( & mut self , value : bool ) -> Result < ( ) > {
435461 let value: libc:: c_int = value. into ( ) ;
436462 setsockopt (
437- self . 0 ,
463+ self . as_raw_fd ( ) ,
438464 libc:: SOL_NETLINK ,
439465 libc:: NETLINK_BROADCAST_ERROR ,
440466 value,
@@ -443,7 +469,7 @@ impl Socket {
443469
444470 pub fn get_broadcast_error ( & self ) -> Result < bool > {
445471 let res = getsockopt :: < libc:: c_int > (
446- self . 0 ,
472+ self . as_raw_fd ( ) ,
447473 libc:: SOL_NETLINK ,
448474 libc:: NETLINK_BROADCAST_ERROR ,
449475 ) ?;
@@ -454,12 +480,17 @@ impl Socket {
454480 /// unicast and broadcast listeners to avoid receiving `ENOBUFS` errors.
455481 pub fn set_no_enobufs ( & mut self , value : bool ) -> Result < ( ) > {
456482 let value: libc:: c_int = value. into ( ) ;
457- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_NO_ENOBUFS , value)
483+ setsockopt (
484+ self . as_raw_fd ( ) ,
485+ libc:: SOL_NETLINK ,
486+ libc:: NETLINK_NO_ENOBUFS ,
487+ value,
488+ )
458489 }
459490
460491 pub fn get_no_enobufs ( & self ) -> Result < bool > {
461492 let res = getsockopt :: < libc:: c_int > (
462- self . 0 ,
493+ self . as_raw_fd ( ) ,
463494 libc:: SOL_NETLINK ,
464495 libc:: NETLINK_NO_ENOBUFS ,
465496 ) ?;
@@ -474,7 +505,7 @@ impl Socket {
474505 pub fn set_listen_all_namespaces ( & mut self , value : bool ) -> Result < ( ) > {
475506 let value: libc:: c_int = value. into ( ) ;
476507 setsockopt (
477- self . 0 ,
508+ self . as_raw_fd ( ) ,
478509 libc:: SOL_NETLINK ,
479510 libc:: NETLINK_LISTEN_ALL_NSID ,
480511 value,
@@ -483,7 +514,7 @@ impl Socket {
483514
484515 pub fn get_listen_all_namespaces ( & self ) -> Result < bool > {
485516 let res = getsockopt :: < libc:: c_int > (
486- self . 0 ,
517+ self . as_raw_fd ( ) ,
487518 libc:: SOL_NETLINK ,
488519 libc:: NETLINK_LISTEN_ALL_NSID ,
489520 ) ?;
@@ -498,12 +529,17 @@ impl Socket {
498529 /// acknowledgment.
499530 pub fn set_cap_ack ( & mut self , value : bool ) -> Result < ( ) > {
500531 let value: libc:: c_int = value. into ( ) ;
501- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_CAP_ACK , value)
532+ setsockopt (
533+ self . as_raw_fd ( ) ,
534+ libc:: SOL_NETLINK ,
535+ libc:: NETLINK_CAP_ACK ,
536+ value,
537+ )
502538 }
503539
504540 pub fn get_cap_ack ( & self ) -> Result < bool > {
505541 let res = getsockopt :: < libc:: c_int > (
506- self . 0 ,
542+ self . as_raw_fd ( ) ,
507543 libc:: SOL_NETLINK ,
508544 libc:: NETLINK_CAP_ACK ,
509545 ) ?;
@@ -515,12 +551,17 @@ impl Socket {
515551 /// NLMSG_ERROR and NLMSG_DONE messages.
516552 pub fn set_ext_ack ( & mut self , value : bool ) -> Result < ( ) > {
517553 let value: libc:: c_int = value. into ( ) ;
518- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_EXT_ACK , value)
554+ setsockopt (
555+ self . as_raw_fd ( ) ,
556+ libc:: SOL_NETLINK ,
557+ libc:: NETLINK_EXT_ACK ,
558+ value,
559+ )
519560 }
520561
521562 pub fn get_ext_ack ( & self ) -> Result < bool > {
522563 let res = getsockopt :: < libc:: c_int > (
523- self . 0 ,
564+ self . as_raw_fd ( ) ,
524565 libc:: SOL_NETLINK ,
525566 libc:: NETLINK_EXT_ACK ,
526567 ) ?;
@@ -534,13 +575,13 @@ impl Socket {
534575 /// the maximum allowed value is set by the /proc/sys/net/core/rmem_max
535576 /// file. The minimum (doubled) value for this option is 256.
536577 pub fn set_rx_buf_sz < T > ( & self , size : T ) -> Result < ( ) > {
537- setsockopt ( self . 0 , libc:: SOL_SOCKET , libc:: SO_RCVBUF , size)
578+ setsockopt ( self . as_raw_fd ( ) , libc:: SOL_SOCKET , libc:: SO_RCVBUF , size)
538579 }
539580
540581 /// Gets socket receive buffer in bytes
541582 pub fn get_rx_buf_sz ( & self ) -> Result < usize > {
542583 let res = getsockopt :: < libc:: c_int > (
543- self . 0 ,
584+ self . as_raw_fd ( ) ,
544585 libc:: SOL_SOCKET ,
545586 libc:: SO_RCVBUF ,
546587 ) ?;
@@ -552,7 +593,7 @@ impl Socket {
552593 pub fn set_netlink_get_strict_chk ( & self , value : bool ) -> Result < ( ) > {
553594 let value: u32 = value. into ( ) ;
554595 setsockopt (
555- self . 0 ,
596+ self . as_raw_fd ( ) ,
556597 libc:: SOL_NETLINK ,
557598 libc:: NETLINK_GET_STRICT_CHK ,
558599 value,
0 commit comments