@@ -24,6 +24,8 @@ use std::net::{Ipv4Addr, Ipv6Addr};
24
24
target_os = "watchos" ,
25
25
target_os = "illumos" ,
26
26
target_os = "solaris" ,
27
+ target_os = "linux" ,
28
+ target_os = "android" ,
27
29
)
28
30
) ) ]
29
31
use std:: num:: NonZeroU32 ;
@@ -1876,7 +1878,7 @@ impl crate::Socket {
1876
1878
. map ( |_| ( ) )
1877
1879
}
1878
1880
1879
- /// Sets the value for `IP_BOUND_IF` option on this socket.
1881
+ /// Sets the value for `IP_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket.
1880
1882
///
1881
1883
/// If a socket is bound to an interface, only packets received from that
1882
1884
/// particular interface are processed by the socket.
@@ -1896,14 +1898,38 @@ impl crate::Socket {
1896
1898
target_os = "watchos" ,
1897
1899
target_os = "illumos" ,
1898
1900
target_os = "solaris" ,
1901
+ target_os = "linux" ,
1902
+ target_os = "android" ,
1899
1903
)
1900
1904
) ) ]
1901
1905
pub fn bind_device_by_index_v4 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1902
1906
let index = interface. map_or ( 0 , NonZeroU32 :: get) ;
1903
- unsafe { setsockopt ( self . as_raw ( ) , IPPROTO_IP , libc:: IP_BOUND_IF , index) }
1907
+
1908
+ #[ cfg( any(
1909
+ target_os = "ios" ,
1910
+ target_os = "visionos" ,
1911
+ target_os = "macos" ,
1912
+ target_os = "tvos" ,
1913
+ target_os = "watchos" ,
1914
+ target_os = "illumos" ,
1915
+ target_os = "solaris" ,
1916
+ ) ) ]
1917
+ unsafe {
1918
+ setsockopt ( self . as_raw ( ) , IPPROTO_IP , libc:: IP_BOUND_IF , index)
1919
+ }
1920
+
1921
+ #[ cfg( any( target_os = "linux" , target_os = "android" , ) ) ]
1922
+ unsafe {
1923
+ setsockopt (
1924
+ self . as_raw ( ) ,
1925
+ libc:: SOL_SOCKET ,
1926
+ libc:: SO_BINDTOIFINDEX ,
1927
+ index,
1928
+ )
1929
+ }
1904
1930
}
1905
1931
1906
- /// Sets the value for `IPV6_BOUND_IF` option on this socket.
1932
+ /// Sets the value for `IPV6_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket.
1907
1933
///
1908
1934
/// If a socket is bound to an interface, only packets received from that
1909
1935
/// particular interface are processed by the socket.
@@ -1923,15 +1949,39 @@ impl crate::Socket {
1923
1949
target_os = "watchos" ,
1924
1950
target_os = "illumos" ,
1925
1951
target_os = "solaris" ,
1952
+ target_os = "linux" ,
1953
+ target_os = "android" ,
1926
1954
)
1927
1955
) ) ]
1928
1956
pub fn bind_device_by_index_v6 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1929
1957
let index = interface. map_or ( 0 , NonZeroU32 :: get) ;
1930
- unsafe { setsockopt ( self . as_raw ( ) , IPPROTO_IPV6 , libc:: IPV6_BOUND_IF , index) }
1958
+
1959
+ #[ cfg( any(
1960
+ target_os = "ios" ,
1961
+ target_os = "visionos" ,
1962
+ target_os = "macos" ,
1963
+ target_os = "tvos" ,
1964
+ target_os = "watchos" ,
1965
+ target_os = "illumos" ,
1966
+ target_os = "solaris" ,
1967
+ ) ) ]
1968
+ unsafe {
1969
+ setsockopt ( self . as_raw ( ) , IPPROTO_IPV6 , libc:: IPV6_BOUND_IF , index)
1970
+ }
1971
+
1972
+ #[ cfg( any( target_os = "linux" , target_os = "android" , ) ) ]
1973
+ unsafe {
1974
+ setsockopt (
1975
+ self . as_raw ( ) ,
1976
+ libc:: SOL_SOCKET ,
1977
+ libc:: SO_BINDTOIFINDEX ,
1978
+ index,
1979
+ )
1980
+ }
1931
1981
}
1932
1982
1933
- /// Gets the value for `IP_BOUND_IF` option on this socket, i.e. the index
1934
- /// for the interface to which the socket is bound.
1983
+ /// Gets the value for `IP_BOUND_IF` or `SO_BINDTOIFINDEX` option on this
1984
+ /// socket, i.e. the index for the interface to which the socket is bound.
1935
1985
///
1936
1986
/// Returns `None` if the socket is not bound to any interface, otherwise
1937
1987
/// returns an interface index.
@@ -1945,16 +1995,33 @@ impl crate::Socket {
1945
1995
target_os = "watchos" ,
1946
1996
target_os = "illumos" ,
1947
1997
target_os = "solaris" ,
1998
+ target_os = "linux" ,
1999
+ target_os = "android" ,
1948
2000
)
1949
2001
) ) ]
1950
2002
pub fn device_index_v4 ( & self ) -> io:: Result < Option < NonZeroU32 > > {
2003
+ #[ cfg( any(
2004
+ target_os = "ios" ,
2005
+ target_os = "visionos" ,
2006
+ target_os = "macos" ,
2007
+ target_os = "tvos" ,
2008
+ target_os = "watchos" ,
2009
+ target_os = "illumos" ,
2010
+ target_os = "solaris" ,
2011
+ ) ) ]
1951
2012
let index =
1952
2013
unsafe { getsockopt :: < libc:: c_uint > ( self . as_raw ( ) , IPPROTO_IP , libc:: IP_BOUND_IF ) ? } ;
2014
+
2015
+ #[ cfg( any( target_os = "linux" , target_os = "android" , ) ) ]
2016
+ let index = unsafe {
2017
+ getsockopt :: < libc:: c_uint > ( self . as_raw ( ) , libc:: SOL_SOCKET , libc:: SO_BINDTOIFINDEX ) ?
2018
+ } ;
2019
+
1953
2020
Ok ( NonZeroU32 :: new ( index) )
1954
2021
}
1955
2022
1956
- /// Gets the value for `IPV6_BOUND_IF` option on this socket, i.e. the index
1957
- /// for the interface to which the socket is bound.
2023
+ /// Gets the value for `IPV6_BOUND_IF` or `SO_BINDTOIFINDEX` option on this
2024
+ /// socket, i.e. the index for the interface to which the socket is bound.
1958
2025
///
1959
2026
/// Returns `None` if the socket is not bound to any interface, otherwise
1960
2027
/// returns an interface index.
@@ -1968,12 +2035,29 @@ impl crate::Socket {
1968
2035
target_os = "watchos" ,
1969
2036
target_os = "illumos" ,
1970
2037
target_os = "solaris" ,
2038
+ target_os = "linux" ,
2039
+ target_os = "android" ,
1971
2040
)
1972
2041
) ) ]
1973
2042
pub fn device_index_v6 ( & self ) -> io:: Result < Option < NonZeroU32 > > {
2043
+ #[ cfg( any(
2044
+ target_os = "ios" ,
2045
+ target_os = "visionos" ,
2046
+ target_os = "macos" ,
2047
+ target_os = "tvos" ,
2048
+ target_os = "watchos" ,
2049
+ target_os = "illumos" ,
2050
+ target_os = "solaris" ,
2051
+ ) ) ]
1974
2052
let index = unsafe {
1975
2053
getsockopt :: < libc:: c_uint > ( self . as_raw ( ) , IPPROTO_IPV6 , libc:: IPV6_BOUND_IF ) ?
1976
2054
} ;
2055
+
2056
+ #[ cfg( any( target_os = "linux" , target_os = "android" , ) ) ]
2057
+ let index = unsafe {
2058
+ getsockopt :: < libc:: c_uint > ( self . as_raw ( ) , libc:: SOL_SOCKET , libc:: SO_BINDTOIFINDEX ) ?
2059
+ } ;
2060
+
1977
2061
Ok ( NonZeroU32 :: new ( index) )
1978
2062
}
1979
2063
0 commit comments