@@ -28,7 +28,7 @@ pub use self::addr::{
28
28
Ipv6Addr ,
29
29
LinkAddr ,
30
30
} ;
31
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
31
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
32
32
pub use :: sys:: socket:: addr:: netlink:: NetlinkAddr ;
33
33
34
34
pub use libc:: {
@@ -153,7 +153,7 @@ libc_bitflags!{
153
153
/// This flag specifies that queued errors should be received from
154
154
/// the socket error queue. (For more details, see
155
155
/// [recvfrom(2)](https://linux.die.net/man/2/recvfrom))
156
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
156
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
157
157
MSG_ERRQUEUE ;
158
158
/// Set the `close-on-exec` flag for the file descriptor received via a UNIX domain
159
159
/// file descriptor using the `SCM_RIGHTS` operation (described in
@@ -526,6 +526,23 @@ pub enum ControlMessage<'a> {
526
526
/// nix::unistd::close(in_socket).unwrap();
527
527
/// ```
528
528
ScmTimestamp ( & ' a TimeVal ) ,
529
+
530
+ #[ cfg( any(
531
+ target_os = "android" ,
532
+ target_os = "ios" ,
533
+ target_os = "linux" ,
534
+ target_os = "macos"
535
+ ) ) ]
536
+ Ipv4PacketInfo ( & ' a libc:: in_pktinfo ) ,
537
+ #[ cfg( any(
538
+ target_os = "android" ,
539
+ target_os = "freebsd" ,
540
+ target_os = "ios" ,
541
+ target_os = "linux" ,
542
+ target_os = "macos"
543
+ ) ) ]
544
+ Ipv6PacketInfo ( & ' a libc:: in6_pktinfo ) ,
545
+
529
546
/// Catch-all variant for unimplemented cmsg types.
530
547
#[ doc( hidden) ]
531
548
Unknown ( UnknownCmsg < ' a > ) ,
@@ -565,19 +582,79 @@ impl<'a> ControlMessage<'a> {
565
582
ControlMessage :: ScmTimestamp ( t) => {
566
583
mem:: size_of_val ( t)
567
584
} ,
585
+ #[ cfg( any(
586
+ target_os = "android" ,
587
+ target_os = "ios" ,
588
+ target_os = "linux" ,
589
+ target_os = "macos"
590
+ ) ) ]
591
+ ControlMessage :: Ipv4PacketInfo ( pktinfo) => {
592
+ mem:: size_of_val ( pktinfo)
593
+ } ,
594
+ #[ cfg( any(
595
+ target_os = "android" ,
596
+ target_os = "freebsd" ,
597
+ target_os = "ios" ,
598
+ target_os = "linux" ,
599
+ target_os = "macos"
600
+ ) ) ]
601
+ ControlMessage :: Ipv6PacketInfo ( pktinfo) => {
602
+ mem:: size_of_val ( pktinfo)
603
+ } ,
568
604
ControlMessage :: Unknown ( UnknownCmsg ( _, bytes) ) => {
569
605
mem:: size_of_val ( bytes)
570
606
}
571
607
}
572
608
}
573
609
610
+ /// Returns the value to put into the `cmsg_level` field of the header.
611
+ fn cmsg_level ( & self ) -> libc:: c_int {
612
+ match * self {
613
+ ControlMessage :: ScmRights ( _) => libc:: SOL_SOCKET ,
614
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
615
+ ControlMessage :: ScmCredentials ( _) => libc:: SOL_SOCKET ,
616
+ ControlMessage :: ScmTimestamp ( _) => libc:: SOL_SOCKET ,
617
+ #[ cfg( any(
618
+ target_os = "android" ,
619
+ target_os = "ios" ,
620
+ target_os = "linux" ,
621
+ target_os = "macos"
622
+ ) ) ]
623
+ ControlMessage :: Ipv4PacketInfo ( _) => libc:: IPPROTO_IP ,
624
+ #[ cfg( any(
625
+ target_os = "android" ,
626
+ target_os = "freebsd" ,
627
+ target_os = "ios" ,
628
+ target_os = "linux" ,
629
+ target_os = "macos"
630
+ ) ) ]
631
+ ControlMessage :: Ipv6PacketInfo ( _) => libc:: IPPROTO_IPV6 ,
632
+ ControlMessage :: Unknown ( ref cmsg) => cmsg. 0 . cmsg_level ,
633
+ }
634
+ }
635
+
574
636
/// Returns the value to put into the `cmsg_type` field of the header.
575
637
fn cmsg_type ( & self ) -> libc:: c_int {
576
638
match * self {
577
639
ControlMessage :: ScmRights ( _) => libc:: SCM_RIGHTS ,
578
640
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
579
641
ControlMessage :: ScmCredentials ( _) => libc:: SCM_CREDENTIALS ,
580
642
ControlMessage :: ScmTimestamp ( _) => libc:: SCM_TIMESTAMP ,
643
+ #[ cfg( any(
644
+ target_os = "android" ,
645
+ target_os = "ios" ,
646
+ target_os = "linux" ,
647
+ target_os = "macos"
648
+ ) ) ]
649
+ ControlMessage :: Ipv4PacketInfo ( _) => libc:: IP_PKTINFO ,
650
+ #[ cfg( any(
651
+ target_os = "android" ,
652
+ target_os = "freebsd" ,
653
+ target_os = "ios" ,
654
+ target_os = "linux" ,
655
+ target_os = "macos"
656
+ ) ) ]
657
+ ControlMessage :: Ipv6PacketInfo ( _) => libc:: IPV6_PKTINFO ,
581
658
ControlMessage :: Unknown ( ref cmsg) => cmsg. 0 . cmsg_type ,
582
659
}
583
660
}
@@ -598,7 +675,7 @@ impl<'a> ControlMessage<'a> {
598
675
} else {
599
676
let cmsg = cmsghdr {
600
677
cmsg_len : self . len ( ) as _ ,
601
- cmsg_level : libc :: SOL_SOCKET ,
678
+ cmsg_level : self . cmsg_level ( ) ,
602
679
cmsg_type : self . cmsg_type ( ) ,
603
680
..mem:: zeroed ( ) // zero out platform-dependent padding fields
604
681
} ;
@@ -615,10 +692,29 @@ impl<'a> ControlMessage<'a> {
615
692
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
616
693
ControlMessage :: ScmCredentials ( creds) => {
617
694
copy_bytes ( creds, buf)
618
- }
695
+ } ,
619
696
ControlMessage :: ScmTimestamp ( t) => {
620
697
copy_bytes ( t, buf)
621
698
} ,
699
+ #[ cfg( any(
700
+ target_os = "android" ,
701
+ target_os = "ios" ,
702
+ target_os = "linux" ,
703
+ target_os = "macos"
704
+ ) ) ]
705
+ ControlMessage :: Ipv4PacketInfo ( pktinfo) => {
706
+ copy_bytes ( pktinfo, buf)
707
+ } ,
708
+ #[ cfg( any(
709
+ target_os = "android" ,
710
+ target_os = "freebsd" ,
711
+ target_os = "ios" ,
712
+ target_os = "linux" ,
713
+ target_os = "macos"
714
+ ) ) ]
715
+ ControlMessage :: Ipv6PacketInfo ( pktinfo) => {
716
+ copy_bytes ( pktinfo, buf)
717
+ }
622
718
ControlMessage :: Unknown ( _) => unreachable ! ( ) ,
623
719
}
624
720
} ;
@@ -650,6 +746,28 @@ impl<'a> ControlMessage<'a> {
650
746
ControlMessage :: ScmTimestamp (
651
747
& * ( data. as_ptr ( ) as * const _ ) )
652
748
} ,
749
+ #[ cfg( any(
750
+ target_os = "android" ,
751
+ target_os = "freebsd" ,
752
+ target_os = "ios" ,
753
+ target_os = "linux" ,
754
+ target_os = "macos"
755
+ ) ) ]
756
+ ( libc:: IPPROTO_IPV6 , libc:: IPV6_PKTINFO ) => {
757
+ ControlMessage :: Ipv6PacketInfo (
758
+ & * ( data. as_ptr ( ) as * const _ ) )
759
+ }
760
+ #[ cfg( any(
761
+ target_os = "android" ,
762
+ target_os = "ios" ,
763
+ target_os = "linux" ,
764
+ target_os = "macos"
765
+ ) ) ]
766
+ ( libc:: IPPROTO_IP , libc:: IP_PKTINFO ) => {
767
+ ControlMessage :: Ipv4PacketInfo (
768
+ & * ( data. as_ptr ( ) as * const _ ) )
769
+ }
770
+
653
771
( _, _) => {
654
772
ControlMessage :: Unknown ( UnknownCmsg ( header, data) )
655
773
}
@@ -1055,7 +1173,7 @@ pub unsafe fn sockaddr_storage_to_addr(
1055
1173
let pathlen = len - offset_of ! ( sockaddr_un, sun_path) ;
1056
1174
Ok ( SockAddr :: Unix ( UnixAddr ( sun, pathlen) ) )
1057
1175
}
1058
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
1176
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
1059
1177
libc:: AF_NETLINK => {
1060
1178
use libc:: sockaddr_nl;
1061
1179
Ok ( SockAddr :: Netlink ( NetlinkAddr ( * ( addr as * const _ as * const sockaddr_nl ) ) ) )
0 commit comments