33use std:: {
44 io:: { Error , Result } ,
55 mem,
6- os:: unix:: io:: { AsRawFd , FromRawFd , RawFd } ,
6+ os:: {
7+ fd:: { AsFd , BorrowedFd } ,
8+ unix:: io:: { AsRawFd , RawFd } ,
9+ } ,
710} ;
811
912use crate :: SocketAddr ;
@@ -51,7 +54,7 @@ use crate::SocketAddr;
5154/// }
5255/// }
5356/// ```
54- #[ derive( Clone , Debug ) ]
57+ #[ derive( Debug ) ]
5558pub struct Socket ( RawFd ) ;
5659
5760impl AsRawFd for Socket {
@@ -60,15 +63,15 @@ impl AsRawFd for Socket {
6063 }
6164}
6265
63- impl FromRawFd for Socket {
64- unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
65- Socket ( fd )
66+ impl AsFd for Socket {
67+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
68+ unsafe { BorrowedFd :: borrow_raw ( self . 0 ) }
6669 }
6770}
6871
6972impl Drop for Socket {
7073 fn drop ( & mut self ) {
71- unsafe { libc:: close ( self . 0 ) } ;
74+ unsafe { libc:: close ( self . as_raw_fd ( ) ) } ;
7275 }
7376}
7477
@@ -94,7 +97,7 @@ impl Socket {
9497 /// Bind the socket to the given address
9598 pub fn bind ( & mut self , addr : & SocketAddr ) -> Result < ( ) > {
9699 let ( addr_ptr, addr_len) = addr. as_raw ( ) ;
97- let res = unsafe { libc:: bind ( self . 0 , addr_ptr, addr_len) } ;
100+ let res = unsafe { libc:: bind ( self . as_raw_fd ( ) , addr_ptr, addr_len) } ;
98101 if res < 0 {
99102 return Err ( Error :: last_os_error ( ) ) ;
100103 }
@@ -115,7 +118,9 @@ impl Socket {
115118 let ( addr_ptr, mut addr_len) = addr. as_raw_mut ( ) ;
116119 let addr_len_copy = addr_len;
117120 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) } ;
121+ let res = unsafe {
122+ libc:: getsockname ( self . as_raw_fd ( ) , addr_ptr, addr_len_ptr)
123+ } ;
119124 if res < 0 {
120125 return Err ( Error :: last_os_error ( ) ) ;
121126 }
@@ -128,8 +133,9 @@ impl Socket {
128133 /// Make this socket non-blocking
129134 pub fn set_non_blocking ( & self , non_blocking : bool ) -> Result < ( ) > {
130135 let mut non_blocking = non_blocking as libc:: c_int ;
131- let res =
132- unsafe { libc:: ioctl ( self . 0 , libc:: FIONBIO , & mut non_blocking) } ;
136+ let res = unsafe {
137+ libc:: ioctl ( self . as_raw_fd ( ) , libc:: FIONBIO , & mut non_blocking)
138+ } ;
133139 if res < 0 {
134140 return Err ( Error :: last_os_error ( ) ) ;
135141 }
@@ -152,7 +158,7 @@ impl Socket {
152158 /// 2. connect it to the kernel with [`Socket::connect`]
153159 /// 3. send a request to the kernel with [`Socket::send`]
154160 /// 4. read the response (which can span over several messages)
155- /// [`Socket::recv`]
161+ /// [`Socket::recv`]
156162 ///
157163 /// ```rust
158164 /// use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
@@ -216,7 +222,7 @@ impl Socket {
216222 // - https://stackoverflow.com/a/14046386/1836144
217223 // - https://lists.isc.org/pipermail/bind-users/2009-August/077527.html
218224 let ( addr, addr_len) = remote_addr. as_raw ( ) ;
219- let res = unsafe { libc:: connect ( self . 0 , addr, addr_len) } ;
225+ let res = unsafe { libc:: connect ( self . as_raw_fd ( ) , addr, addr_len) } ;
220226 if res < 0 {
221227 return Err ( Error :: last_os_error ( ) ) ;
222228 }
@@ -293,7 +299,7 @@ impl Socket {
293299
294300 let res = unsafe {
295301 libc:: recvfrom (
296- self . 0 ,
302+ self . as_raw_fd ( ) ,
297303 buf_ptr,
298304 buf_len,
299305 flags,
@@ -324,7 +330,8 @@ impl Socket {
324330 let buf_ptr = chunk. as_mut_ptr ( ) as * mut libc:: c_void ;
325331 let buf_len = chunk. len ( ) as libc:: size_t ;
326332
327- let res = unsafe { libc:: recv ( self . 0 , buf_ptr, buf_len, flags) } ;
333+ let res =
334+ unsafe { libc:: recv ( self . as_raw_fd ( ) , buf_ptr, buf_len, flags) } ;
328335 if res < 0 {
329336 return Err ( Error :: last_os_error ( ) ) ;
330337 } else {
@@ -367,7 +374,14 @@ impl Socket {
367374 let buf_len = buf. len ( ) as libc:: size_t ;
368375
369376 let res = unsafe {
370- libc:: sendto ( self . 0 , buf_ptr, buf_len, flags, addr_ptr, addr_len)
377+ libc:: sendto (
378+ self . as_raw_fd ( ) ,
379+ buf_ptr,
380+ buf_len,
381+ flags,
382+ addr_ptr,
383+ addr_len,
384+ )
371385 } ;
372386 if res < 0 {
373387 return Err ( Error :: last_os_error ( ) ) ;
@@ -382,7 +396,8 @@ impl Socket {
382396 let buf_ptr = buf. as_ptr ( ) as * const libc:: c_void ;
383397 let buf_len = buf. len ( ) as libc:: size_t ;
384398
385- let res = unsafe { libc:: send ( self . 0 , buf_ptr, buf_len, flags) } ;
399+ let res =
400+ unsafe { libc:: send ( self . as_raw_fd ( ) , buf_ptr, buf_len, flags) } ;
386401 if res < 0 {
387402 return Err ( Error :: last_os_error ( ) ) ;
388403 }
@@ -391,12 +406,17 @@ impl Socket {
391406
392407 pub fn set_pktinfo ( & mut self , value : bool ) -> Result < ( ) > {
393408 let value: libc:: c_int = value. into ( ) ;
394- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_PKTINFO , value)
409+ setsockopt (
410+ self . as_raw_fd ( ) ,
411+ libc:: SOL_NETLINK ,
412+ libc:: NETLINK_PKTINFO ,
413+ value,
414+ )
395415 }
396416
397417 pub fn get_pktinfo ( & self ) -> Result < bool > {
398418 let res = getsockopt :: < libc:: c_int > (
399- self . 0 ,
419+ self . as_raw_fd ( ) ,
400420 libc:: SOL_NETLINK ,
401421 libc:: NETLINK_PKTINFO ,
402422 ) ?;
@@ -405,7 +425,7 @@ impl Socket {
405425
406426 pub fn add_membership ( & mut self , group : u32 ) -> Result < ( ) > {
407427 setsockopt (
408- self . 0 ,
428+ self . as_raw_fd ( ) ,
409429 libc:: SOL_NETLINK ,
410430 libc:: NETLINK_ADD_MEMBERSHIP ,
411431 group,
@@ -414,7 +434,7 @@ impl Socket {
414434
415435 pub fn drop_membership ( & mut self , group : u32 ) -> Result < ( ) > {
416436 setsockopt (
417- self . 0 ,
437+ self . as_raw_fd ( ) ,
418438 libc:: SOL_NETLINK ,
419439 libc:: NETLINK_DROP_MEMBERSHIP ,
420440 group,
@@ -434,7 +454,7 @@ impl Socket {
434454 pub fn set_broadcast_error ( & mut self , value : bool ) -> Result < ( ) > {
435455 let value: libc:: c_int = value. into ( ) ;
436456 setsockopt (
437- self . 0 ,
457+ self . as_raw_fd ( ) ,
438458 libc:: SOL_NETLINK ,
439459 libc:: NETLINK_BROADCAST_ERROR ,
440460 value,
@@ -443,7 +463,7 @@ impl Socket {
443463
444464 pub fn get_broadcast_error ( & self ) -> Result < bool > {
445465 let res = getsockopt :: < libc:: c_int > (
446- self . 0 ,
466+ self . as_raw_fd ( ) ,
447467 libc:: SOL_NETLINK ,
448468 libc:: NETLINK_BROADCAST_ERROR ,
449469 ) ?;
@@ -454,12 +474,17 @@ impl Socket {
454474 /// unicast and broadcast listeners to avoid receiving `ENOBUFS` errors.
455475 pub fn set_no_enobufs ( & mut self , value : bool ) -> Result < ( ) > {
456476 let value: libc:: c_int = value. into ( ) ;
457- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_NO_ENOBUFS , value)
477+ setsockopt (
478+ self . as_raw_fd ( ) ,
479+ libc:: SOL_NETLINK ,
480+ libc:: NETLINK_NO_ENOBUFS ,
481+ value,
482+ )
458483 }
459484
460485 pub fn get_no_enobufs ( & self ) -> Result < bool > {
461486 let res = getsockopt :: < libc:: c_int > (
462- self . 0 ,
487+ self . as_raw_fd ( ) ,
463488 libc:: SOL_NETLINK ,
464489 libc:: NETLINK_NO_ENOBUFS ,
465490 ) ?;
@@ -474,7 +499,7 @@ impl Socket {
474499 pub fn set_listen_all_namespaces ( & mut self , value : bool ) -> Result < ( ) > {
475500 let value: libc:: c_int = value. into ( ) ;
476501 setsockopt (
477- self . 0 ,
502+ self . as_raw_fd ( ) ,
478503 libc:: SOL_NETLINK ,
479504 libc:: NETLINK_LISTEN_ALL_NSID ,
480505 value,
@@ -483,7 +508,7 @@ impl Socket {
483508
484509 pub fn get_listen_all_namespaces ( & self ) -> Result < bool > {
485510 let res = getsockopt :: < libc:: c_int > (
486- self . 0 ,
511+ self . as_raw_fd ( ) ,
487512 libc:: SOL_NETLINK ,
488513 libc:: NETLINK_LISTEN_ALL_NSID ,
489514 ) ?;
@@ -498,12 +523,17 @@ impl Socket {
498523 /// acknowledgment.
499524 pub fn set_cap_ack ( & mut self , value : bool ) -> Result < ( ) > {
500525 let value: libc:: c_int = value. into ( ) ;
501- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_CAP_ACK , value)
526+ setsockopt (
527+ self . as_raw_fd ( ) ,
528+ libc:: SOL_NETLINK ,
529+ libc:: NETLINK_CAP_ACK ,
530+ value,
531+ )
502532 }
503533
504534 pub fn get_cap_ack ( & self ) -> Result < bool > {
505535 let res = getsockopt :: < libc:: c_int > (
506- self . 0 ,
536+ self . as_raw_fd ( ) ,
507537 libc:: SOL_NETLINK ,
508538 libc:: NETLINK_CAP_ACK ,
509539 ) ?;
@@ -515,12 +545,17 @@ impl Socket {
515545 /// NLMSG_ERROR and NLMSG_DONE messages.
516546 pub fn set_ext_ack ( & mut self , value : bool ) -> Result < ( ) > {
517547 let value: libc:: c_int = value. into ( ) ;
518- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_EXT_ACK , value)
548+ setsockopt (
549+ self . as_raw_fd ( ) ,
550+ libc:: SOL_NETLINK ,
551+ libc:: NETLINK_EXT_ACK ,
552+ value,
553+ )
519554 }
520555
521556 pub fn get_ext_ack ( & self ) -> Result < bool > {
522557 let res = getsockopt :: < libc:: c_int > (
523- self . 0 ,
558+ self . as_raw_fd ( ) ,
524559 libc:: SOL_NETLINK ,
525560 libc:: NETLINK_EXT_ACK ,
526561 ) ?;
@@ -534,13 +569,13 @@ impl Socket {
534569 /// the maximum allowed value is set by the /proc/sys/net/core/rmem_max
535570 /// file. The minimum (doubled) value for this option is 256.
536571 pub fn set_rx_buf_sz < T > ( & self , size : T ) -> Result < ( ) > {
537- setsockopt ( self . 0 , libc:: SOL_SOCKET , libc:: SO_RCVBUF , size)
572+ setsockopt ( self . as_raw_fd ( ) , libc:: SOL_SOCKET , libc:: SO_RCVBUF , size)
538573 }
539574
540575 /// Gets socket receive buffer in bytes
541576 pub fn get_rx_buf_sz ( & self ) -> Result < usize > {
542577 let res = getsockopt :: < libc:: c_int > (
543- self . 0 ,
578+ self . as_raw_fd ( ) ,
544579 libc:: SOL_SOCKET ,
545580 libc:: SO_RCVBUF ,
546581 ) ?;
@@ -552,7 +587,7 @@ impl Socket {
552587 pub fn set_netlink_get_strict_chk ( & self , value : bool ) -> Result < ( ) > {
553588 let value: u32 = value. into ( ) ;
554589 setsockopt (
555- self . 0 ,
590+ self . as_raw_fd ( ) ,
556591 libc:: SOL_NETLINK ,
557592 libc:: NETLINK_GET_STRICT_CHK ,
558593 value,
0 commit comments