@@ -9,7 +9,16 @@ use std::{
99 } ,
1010} ;
1111
12- use crate :: SocketAddr ;
12+ use crate :: {
13+ constants:: {
14+ NETLINK_ADD_MEMBERSHIP , NETLINK_BROADCAST_ERROR , NETLINK_CAP_ACK ,
15+ NETLINK_DROP_MEMBERSHIP , NETLINK_EXT_ACK , NETLINK_GET_STRICT_CHK ,
16+ NETLINK_LISTEN_ALL_NSID , NETLINK_NO_ENOBUFS , NETLINK_PKTINFO ,
17+ PF_NETLINK , SOL_NETLINK ,
18+ } ,
19+ sys:: sockaddr_nl,
20+ SocketAddr ,
21+ } ;
1322
1423/// A netlink socket.
1524///
@@ -89,7 +98,7 @@ impl Socket {
8998 pub fn new ( protocol : isize ) -> Result < Self > {
9099 let res = unsafe {
91100 libc:: socket (
92- libc :: PF_NETLINK ,
101+ PF_NETLINK as _ ,
93102 libc:: SOCK_DGRAM | libc:: SOCK_CLOEXEC ,
94103 protocol as libc:: c_int ,
95104 )
@@ -196,6 +205,7 @@ impl Socket {
196205 /// }
197206 /// }
198207 /// ```
208+ #[ cfg( not( target_os = "freebsd" ) ) ]
199209 pub fn connect ( & self , remote_addr : & SocketAddr ) -> Result < ( ) > {
200210 // FIXME:
201211 //
@@ -261,7 +271,7 @@ impl Socket {
261271 // library create a sockaddr_storage so that it works for any
262272 // address family, but here, we already know that we'll have a
263273 // Netlink address, so we can create the appropriate storage.
264- let mut addr = unsafe { mem:: zeroed :: < libc :: sockaddr_nl > ( ) } ;
274+ let mut addr = unsafe { mem:: zeroed :: < sockaddr_nl > ( ) } ;
265275
266276 // recvfrom takes a *sockaddr as parameter so that it can accept any
267277 // kind of address storage, so we need to create such a pointer
@@ -276,8 +286,7 @@ impl Socket {
276286 // +--------------+---------------+ +---------+--------+
277287 // / \ /
278288 // \
279- let addr_ptr =
280- & mut addr as * mut libc:: sockaddr_nl as * mut libc:: sockaddr ;
289+ let addr_ptr = & mut addr as * mut sockaddr_nl as * mut libc:: sockaddr ;
281290
282291 // Why do we need to pass the address length? We're passing a generic
283292 // *sockaddr to recvfrom. Somehow recvfrom needs to make sure
@@ -414,35 +423,35 @@ impl Socket {
414423 let value: libc:: c_int = value. into ( ) ;
415424 setsockopt (
416425 self . as_raw_fd ( ) ,
417- libc :: SOL_NETLINK ,
418- libc :: NETLINK_PKTINFO ,
426+ SOL_NETLINK as _ ,
427+ NETLINK_PKTINFO as _ ,
419428 value,
420429 )
421430 }
422431
423432 pub fn get_pktinfo ( & self ) -> Result < bool > {
424433 let res = getsockopt :: < libc:: c_int > (
425434 self . as_raw_fd ( ) ,
426- libc :: SOL_NETLINK ,
427- libc :: NETLINK_PKTINFO ,
435+ SOL_NETLINK as _ ,
436+ NETLINK_PKTINFO as _ ,
428437 ) ?;
429438 Ok ( res == 1 )
430439 }
431440
432441 pub fn add_membership ( & mut self , group : u32 ) -> Result < ( ) > {
433442 setsockopt (
434443 self . as_raw_fd ( ) ,
435- libc :: SOL_NETLINK ,
436- libc :: NETLINK_ADD_MEMBERSHIP ,
444+ SOL_NETLINK as _ ,
445+ NETLINK_ADD_MEMBERSHIP as _ ,
437446 group,
438447 )
439448 }
440449
441450 pub fn drop_membership ( & mut self , group : u32 ) -> Result < ( ) > {
442451 setsockopt (
443452 self . as_raw_fd ( ) ,
444- libc :: SOL_NETLINK ,
445- libc :: NETLINK_DROP_MEMBERSHIP ,
453+ SOL_NETLINK as _ ,
454+ NETLINK_DROP_MEMBERSHIP as _ ,
446455 group,
447456 )
448457 }
@@ -457,42 +466,46 @@ impl Socket {
457466 /// `NETLINK_BROADCAST_ERROR` (since Linux 2.6.30). When not set,
458467 /// `netlink_broadcast()` only reports `ESRCH` errors and silently
459468 /// ignore `NOBUFS` errors.
469+ #[ cfg( not( target_os = "freebsd" ) ) ]
460470 pub fn set_broadcast_error ( & mut self , value : bool ) -> Result < ( ) > {
461471 let value: libc:: c_int = value. into ( ) ;
462472 setsockopt (
463473 self . as_raw_fd ( ) ,
464- libc :: SOL_NETLINK ,
465- libc :: NETLINK_BROADCAST_ERROR ,
474+ SOL_NETLINK as _ ,
475+ NETLINK_BROADCAST_ERROR as _ ,
466476 value,
467477 )
468478 }
469479
480+ #[ cfg( not( target_os = "freebsd" ) ) ]
470481 pub fn get_broadcast_error ( & self ) -> Result < bool > {
471482 let res = getsockopt :: < libc:: c_int > (
472483 self . as_raw_fd ( ) ,
473- libc :: SOL_NETLINK ,
474- libc :: NETLINK_BROADCAST_ERROR ,
484+ SOL_NETLINK as _ ,
485+ NETLINK_BROADCAST_ERROR as _ ,
475486 ) ?;
476487 Ok ( res == 1 )
477488 }
478489
479490 /// `NETLINK_NO_ENOBUFS` (since Linux 2.6.30). This flag can be used by
480491 /// unicast and broadcast listeners to avoid receiving `ENOBUFS` errors.
492+ #[ cfg( not( target_os = "freebsd" ) ) ]
481493 pub fn set_no_enobufs ( & mut self , value : bool ) -> Result < ( ) > {
482494 let value: libc:: c_int = value. into ( ) ;
483495 setsockopt (
484496 self . as_raw_fd ( ) ,
485- libc :: SOL_NETLINK ,
486- libc :: NETLINK_NO_ENOBUFS ,
497+ SOL_NETLINK as _ ,
498+ NETLINK_NO_ENOBUFS as _ ,
487499 value,
488500 )
489501 }
490502
503+ #[ cfg( not( target_os = "freebsd" ) ) ]
491504 pub fn get_no_enobufs ( & self ) -> Result < bool > {
492505 let res = getsockopt :: < libc:: c_int > (
493506 self . as_raw_fd ( ) ,
494- libc :: SOL_NETLINK ,
495- libc :: NETLINK_NO_ENOBUFS ,
507+ SOL_NETLINK as _ ,
508+ NETLINK_NO_ENOBUFS as _ ,
496509 ) ?;
497510 Ok ( res == 1 )
498511 }
@@ -506,17 +519,17 @@ impl Socket {
506519 let value: libc:: c_int = value. into ( ) ;
507520 setsockopt (
508521 self . as_raw_fd ( ) ,
509- libc :: SOL_NETLINK ,
510- libc :: NETLINK_LISTEN_ALL_NSID ,
522+ SOL_NETLINK as _ ,
523+ NETLINK_LISTEN_ALL_NSID as _ ,
511524 value,
512525 )
513526 }
514527
515528 pub fn get_listen_all_namespaces ( & self ) -> Result < bool > {
516529 let res = getsockopt :: < libc:: c_int > (
517530 self . as_raw_fd ( ) ,
518- libc :: SOL_NETLINK ,
519- libc :: NETLINK_LISTEN_ALL_NSID ,
531+ SOL_NETLINK as _ ,
532+ NETLINK_LISTEN_ALL_NSID as _ ,
520533 ) ?;
521534 Ok ( res == 1 )
522535 }
@@ -531,17 +544,17 @@ impl Socket {
531544 let value: libc:: c_int = value. into ( ) ;
532545 setsockopt (
533546 self . as_raw_fd ( ) ,
534- libc :: SOL_NETLINK ,
535- libc :: NETLINK_CAP_ACK ,
547+ SOL_NETLINK as _ ,
548+ NETLINK_CAP_ACK as _ ,
536549 value,
537550 )
538551 }
539552
540553 pub fn get_cap_ack ( & self ) -> Result < bool > {
541554 let res = getsockopt :: < libc:: c_int > (
542555 self . as_raw_fd ( ) ,
543- libc :: SOL_NETLINK ,
544- libc :: NETLINK_CAP_ACK ,
556+ SOL_NETLINK as _ ,
557+ NETLINK_CAP_ACK as _ ,
545558 ) ?;
546559 Ok ( res == 1 )
547560 }
@@ -553,17 +566,17 @@ impl Socket {
553566 let value: libc:: c_int = value. into ( ) ;
554567 setsockopt (
555568 self . as_raw_fd ( ) ,
556- libc :: SOL_NETLINK ,
557- libc :: NETLINK_EXT_ACK ,
569+ SOL_NETLINK as _ ,
570+ NETLINK_EXT_ACK as _ ,
558571 value,
559572 )
560573 }
561574
562575 pub fn get_ext_ack ( & self ) -> Result < bool > {
563576 let res = getsockopt :: < libc:: c_int > (
564577 self . as_raw_fd ( ) ,
565- libc :: SOL_NETLINK ,
566- libc :: NETLINK_EXT_ACK ,
578+ SOL_NETLINK as _ ,
579+ NETLINK_EXT_ACK as _ ,
567580 ) ?;
568581 Ok ( res == 1 )
569582 }
@@ -594,8 +607,8 @@ impl Socket {
594607 let value: u32 = value. into ( ) ;
595608 setsockopt (
596609 self . as_raw_fd ( ) ,
597- libc :: SOL_NETLINK ,
598- libc :: NETLINK_GET_STRICT_CHK ,
610+ SOL_NETLINK as _ ,
611+ NETLINK_GET_STRICT_CHK as _ ,
599612 value,
600613 )
601614 }
@@ -668,6 +681,7 @@ mod test {
668681 Socket :: new ( NETLINK_ROUTE ) . unwrap ( ) ;
669682 }
670683
684+ #[ cfg( not( target_os = "freebsd" ) ) ]
671685 #[ test]
672686 fn connect ( ) {
673687 let sock = Socket :: new ( NETLINK_ROUTE ) . unwrap ( ) ;
@@ -704,15 +718,18 @@ mod test {
704718 sock. set_cap_ack ( false ) . unwrap ( ) ;
705719 assert ! ( !sock. get_cap_ack( ) . unwrap( ) ) ;
706720
707- sock. set_no_enobufs ( true ) . unwrap ( ) ;
708- assert ! ( sock. get_no_enobufs( ) . unwrap( ) ) ;
709- sock. set_no_enobufs ( false ) . unwrap ( ) ;
710- assert ! ( !sock. get_no_enobufs( ) . unwrap( ) ) ;
711-
712- sock. set_broadcast_error ( true ) . unwrap ( ) ;
713- assert ! ( sock. get_broadcast_error( ) . unwrap( ) ) ;
714- sock. set_broadcast_error ( false ) . unwrap ( ) ;
715- assert ! ( !sock. get_broadcast_error( ) . unwrap( ) ) ;
721+ #[ cfg( not( target_os = "freebsd" ) ) ]
722+ {
723+ sock. set_no_enobufs ( true ) . unwrap ( ) ;
724+ assert ! ( sock. get_no_enobufs( ) . unwrap( ) ) ;
725+ sock. set_no_enobufs ( false ) . unwrap ( ) ;
726+ assert ! ( !sock. get_no_enobufs( ) . unwrap( ) ) ;
727+
728+ sock. set_broadcast_error ( true ) . unwrap ( ) ;
729+ assert ! ( sock. get_broadcast_error( ) . unwrap( ) ) ;
730+ sock. set_broadcast_error ( false ) . unwrap ( ) ;
731+ assert ! ( !sock. get_broadcast_error( ) . unwrap( ) ) ;
732+ }
716733
717734 // FIXME: these require root permissions
718735 // sock.set_listen_all_namespaces(true).unwrap();
0 commit comments