@@ -20,7 +20,7 @@ use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
20
20
#[ cfg( feature = "all" ) ]
21
21
use std:: path:: Path ;
22
22
use std:: time:: Duration ;
23
- use std:: { fmt , io, ptr} ;
23
+ use std:: { io, ptr} ;
24
24
25
25
#[ cfg( not( target_vendor = "apple" ) ) ]
26
26
use libc:: ssize_t;
@@ -652,6 +652,95 @@ fn into_secs(duration: Duration) -> c_int {
652
652
min ( duration. as_secs ( ) , c_int:: max_value ( ) as u64 ) as c_int
653
653
}
654
654
655
+ /// Add `flag` to the current set flags of `F_GETFD`.
656
+ fn fcntl_add ( fd : SysSocket , get_cmd : c_int , set_cmd : c_int , flag : c_int ) -> io:: Result < ( ) > {
657
+ let previous = syscall ! ( fcntl( fd, get_cmd) ) ?;
658
+ let new = previous | flag;
659
+ if new != previous {
660
+ syscall ! ( fcntl( fd, set_cmd, new) ) . map ( |_| ( ) )
661
+ } else {
662
+ // Flag was already set.
663
+ Ok ( ( ) )
664
+ }
665
+ }
666
+
667
+ /// Remove `flag` to the current set flags of `F_GETFD`.
668
+ fn fcntl_remove ( fd : SysSocket , get_cmd : c_int , set_cmd : c_int , flag : c_int ) -> io:: Result < ( ) > {
669
+ let previous = syscall ! ( fcntl( fd, get_cmd) ) ?;
670
+ let new = previous & !flag;
671
+ if new != previous {
672
+ syscall ! ( fcntl( fd, set_cmd, new) ) . map ( |_| ( ) )
673
+ } else {
674
+ // Flag was already set.
675
+ Ok ( ( ) )
676
+ }
677
+ }
678
+
679
+ /// Caller must ensure `T` is the correct type for `opt` and `val`.
680
+ pub ( crate ) unsafe fn getsockopt < T > ( fd : SysSocket , opt : c_int , val : c_int ) -> io:: Result < T > {
681
+ let mut payload: MaybeUninit < T > = MaybeUninit :: uninit ( ) ;
682
+ let mut len = size_of :: < T > ( ) as libc:: socklen_t ;
683
+ syscall ! ( getsockopt(
684
+ fd,
685
+ opt,
686
+ val,
687
+ payload. as_mut_ptr( ) . cast( ) ,
688
+ & mut len,
689
+ ) )
690
+ . map ( |_| {
691
+ debug_assert_eq ! ( len as usize , size_of:: <T >( ) ) ;
692
+ // Safety: `getsockopt` initialised `payload` for us.
693
+ payload. assume_init ( )
694
+ } )
695
+ }
696
+
697
+ /// Caller must ensure `T` is the correct type for `opt` and `val`.
698
+ pub ( crate ) unsafe fn setsockopt < T > (
699
+ fd : SysSocket ,
700
+ opt : c_int ,
701
+ val : c_int ,
702
+ payload : T ,
703
+ ) -> io:: Result < ( ) > {
704
+ let payload = & payload as * const T as * const c_void ;
705
+ syscall ! ( setsockopt(
706
+ fd,
707
+ opt,
708
+ val,
709
+ payload,
710
+ mem:: size_of:: <T >( ) as libc:: socklen_t,
711
+ ) )
712
+ . map ( |_| ( ) )
713
+ }
714
+
715
+ pub ( crate ) fn close ( fd : SysSocket ) {
716
+ unsafe {
717
+ let _ = libc:: close ( fd) ;
718
+ }
719
+ }
720
+
721
+ pub ( crate ) fn to_in_addr ( addr : & Ipv4Addr ) -> in_addr {
722
+ // `s_addr` is stored as BE on all machines, and the array is in BE order.
723
+ // So the native endian conversion method is used so that it's never
724
+ // swapped.
725
+ in_addr {
726
+ s_addr : u32:: from_ne_bytes ( addr. octets ( ) ) ,
727
+ }
728
+ }
729
+
730
+ pub ( crate ) fn from_in_addr ( in_addr : in_addr ) -> Ipv4Addr {
731
+ Ipv4Addr :: from ( in_addr. s_addr . to_ne_bytes ( ) )
732
+ }
733
+
734
+ pub ( crate ) fn to_in6_addr ( addr : & Ipv6Addr ) -> libc:: in6_addr {
735
+ let mut ret: libc:: in6_addr = unsafe { mem:: zeroed ( ) } ;
736
+ ret. s6_addr = addr. octets ( ) ;
737
+ return ret;
738
+ }
739
+
740
+ pub ( crate ) fn from_in6_addr ( in6_addr : in6_addr ) -> Ipv6Addr {
741
+ Ipv6Addr :: from ( in6_addr. s6_addr )
742
+ }
743
+
655
744
/// Unix only API.
656
745
impl crate :: Socket {
657
746
/// Accept a new incoming connection from this listener.
@@ -869,124 +958,6 @@ impl crate::Socket {
869
958
}
870
959
}
871
960
872
- /// Add `flag` to the current set flags of `F_GETFD`.
873
- fn fcntl_add ( fd : SysSocket , get_cmd : c_int , set_cmd : c_int , flag : c_int ) -> io:: Result < ( ) > {
874
- let previous = syscall ! ( fcntl( fd, get_cmd) ) ?;
875
- let new = previous | flag;
876
- if new != previous {
877
- syscall ! ( fcntl( fd, set_cmd, new) ) . map ( |_| ( ) )
878
- } else {
879
- // Flag was already set.
880
- Ok ( ( ) )
881
- }
882
- }
883
-
884
- /// Remove `flag` to the current set flags of `F_GETFD`.
885
- fn fcntl_remove ( fd : SysSocket , get_cmd : c_int , set_cmd : c_int , flag : c_int ) -> io:: Result < ( ) > {
886
- let previous = syscall ! ( fcntl( fd, get_cmd) ) ?;
887
- let new = previous & !flag;
888
- if new != previous {
889
- syscall ! ( fcntl( fd, set_cmd, new) ) . map ( |_| ( ) )
890
- } else {
891
- // Flag was already set.
892
- Ok ( ( ) )
893
- }
894
- }
895
-
896
- /// Caller must ensure `T` is the correct type for `opt` and `val`.
897
- pub ( crate ) unsafe fn getsockopt < T > ( fd : SysSocket , opt : c_int , val : c_int ) -> io:: Result < T > {
898
- let mut payload: MaybeUninit < T > = MaybeUninit :: uninit ( ) ;
899
- let mut len = size_of :: < T > ( ) as libc:: socklen_t ;
900
- syscall ! ( getsockopt(
901
- fd,
902
- opt,
903
- val,
904
- payload. as_mut_ptr( ) . cast( ) ,
905
- & mut len,
906
- ) )
907
- . map ( |_| {
908
- debug_assert_eq ! ( len as usize , size_of:: <T >( ) ) ;
909
- // Safety: `getsockopt` initialised `payload` for us.
910
- payload. assume_init ( )
911
- } )
912
- }
913
-
914
- /// Caller must ensure `T` is the correct type for `opt` and `val`.
915
- pub ( crate ) unsafe fn setsockopt < T > (
916
- fd : SysSocket ,
917
- opt : c_int ,
918
- val : c_int ,
919
- payload : T ,
920
- ) -> io:: Result < ( ) > {
921
- let payload = & payload as * const T as * const c_void ;
922
- syscall ! ( setsockopt(
923
- fd,
924
- opt,
925
- val,
926
- payload,
927
- mem:: size_of:: <T >( ) as libc:: socklen_t,
928
- ) )
929
- . map ( |_| ( ) )
930
- }
931
-
932
- pub ( crate ) fn to_in_addr ( addr : & Ipv4Addr ) -> in_addr {
933
- // `s_addr` is stored as BE on all machines, and the array is in BE order.
934
- // So the native endian conversion method is used so that it's never
935
- // swapped.
936
- in_addr {
937
- s_addr : u32:: from_ne_bytes ( addr. octets ( ) ) ,
938
- }
939
- }
940
-
941
- pub ( crate ) fn from_in_addr ( in_addr : in_addr ) -> Ipv4Addr {
942
- Ipv4Addr :: from ( in_addr. s_addr . to_ne_bytes ( ) )
943
- }
944
-
945
- #[ repr( transparent) ] // Required during rewriting.
946
- pub struct Socket {
947
- fd : SysSocket ,
948
- }
949
-
950
- impl Socket {
951
- pub fn inner ( self ) -> SysSocket {
952
- self . fd
953
- }
954
- }
955
-
956
- impl fmt:: Debug for Socket {
957
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
958
- let mut f = f. debug_struct ( "Socket" ) ;
959
- f. field ( "fd" , & self . fd ) ;
960
- if let Ok ( addr) = getsockname ( self . fd ) {
961
- f. field ( "local_addr" , & addr) ;
962
- }
963
- if let Ok ( addr) = getpeername ( self . fd ) {
964
- f. field ( "peer_addr" , & addr) ;
965
- }
966
- f. finish ( )
967
- }
968
- }
969
-
970
- impl AsRawFd for Socket {
971
- fn as_raw_fd ( & self ) -> c_int {
972
- self . fd
973
- }
974
- }
975
-
976
- impl IntoRawFd for Socket {
977
- fn into_raw_fd ( self ) -> c_int {
978
- let fd = self . fd ;
979
- mem:: forget ( self ) ;
980
- return fd;
981
- }
982
- }
983
-
984
- impl FromRawFd for Socket {
985
- unsafe fn from_raw_fd ( fd : c_int ) -> Socket {
986
- Socket { fd : fd }
987
- }
988
- }
989
-
990
961
impl AsRawFd for crate :: Socket {
991
962
fn as_raw_fd ( & self ) -> c_int {
992
963
self . inner
@@ -1003,9 +974,7 @@ impl IntoRawFd for crate::Socket {
1003
974
1004
975
impl FromRawFd for crate :: Socket {
1005
976
unsafe fn from_raw_fd ( fd : c_int ) -> crate :: Socket {
1006
- crate :: Socket {
1007
- inner : Socket :: from_raw_fd ( fd) . inner ( ) ,
1008
- }
977
+ crate :: Socket { inner : fd }
1009
978
}
1010
979
}
1011
980
@@ -1057,22 +1026,6 @@ impl From<UnixDatagram> for crate::Socket {
1057
1026
}
1058
1027
}
1059
1028
1060
- pub ( crate ) fn close ( fd : SysSocket ) {
1061
- unsafe {
1062
- let _ = libc:: close ( fd) ;
1063
- }
1064
- }
1065
-
1066
- pub ( crate ) fn to_in6_addr ( addr : & Ipv6Addr ) -> libc:: in6_addr {
1067
- let mut ret: libc:: in6_addr = unsafe { mem:: zeroed ( ) } ;
1068
- ret. s6_addr = addr. octets ( ) ;
1069
- return ret;
1070
- }
1071
-
1072
- pub ( crate ) fn from_in6_addr ( in6_addr : in6_addr ) -> Ipv6Addr {
1073
- Ipv6Addr :: from ( in6_addr. s6_addr )
1074
- }
1075
-
1076
1029
#[ test]
1077
1030
fn test_ip ( ) {
1078
1031
let ip = Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ;
0 commit comments