@@ -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 ;
@@ -1820,7 +1822,7 @@ impl crate::Socket {
1820
1822
. map ( |_| ( ) )
1821
1823
}
1822
1824
1823
- /// Sets the value for `IP_BOUND_IF` option on this socket.
1825
+ /// Sets the value for `IP_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket.
1824
1826
///
1825
1827
/// If a socket is bound to an interface, only packets received from that
1826
1828
/// particular interface are processed by the socket.
@@ -1840,14 +1842,38 @@ impl crate::Socket {
1840
1842
target_os = "watchos" ,
1841
1843
target_os = "illumos" ,
1842
1844
target_os = "solaris" ,
1845
+ target_os = "linux" ,
1846
+ target_os = "android" ,
1843
1847
)
1844
1848
) ) ]
1845
1849
pub fn bind_device_by_index_v4 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1846
1850
let index = interface. map_or ( 0 , NonZeroU32 :: get) ;
1847
- unsafe { setsockopt ( self . as_raw ( ) , IPPROTO_IP , libc:: IP_BOUND_IF , index) }
1851
+
1852
+ #[ cfg( any(
1853
+ target_os = "ios" ,
1854
+ target_os = "visionos" ,
1855
+ target_os = "macos" ,
1856
+ target_os = "tvos" ,
1857
+ target_os = "watchos" ,
1858
+ target_os = "illumos" ,
1859
+ target_os = "solaris" ,
1860
+ ) ) ]
1861
+ unsafe {
1862
+ setsockopt ( self . as_raw ( ) , IPPROTO_IP , libc:: IP_BOUND_IF , index)
1863
+ }
1864
+
1865
+ #[ cfg( any( target_os = "linux" , target_os = "android" , ) ) ]
1866
+ unsafe {
1867
+ setsockopt (
1868
+ self . as_raw ( ) ,
1869
+ libc:: SOL_SOCKET ,
1870
+ libc:: SO_BINDTOIFINDEX ,
1871
+ index,
1872
+ )
1873
+ }
1848
1874
}
1849
1875
1850
- /// Sets the value for `IPV6_BOUND_IF` option on this socket.
1876
+ /// Sets the value for `IPV6_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket.
1851
1877
///
1852
1878
/// If a socket is bound to an interface, only packets received from that
1853
1879
/// particular interface are processed by the socket.
@@ -1867,11 +1893,35 @@ impl crate::Socket {
1867
1893
target_os = "watchos" ,
1868
1894
target_os = "illumos" ,
1869
1895
target_os = "solaris" ,
1896
+ target_os = "linux" ,
1897
+ target_os = "android" ,
1870
1898
)
1871
1899
) ) ]
1872
1900
pub fn bind_device_by_index_v6 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1873
1901
let index = interface. map_or ( 0 , NonZeroU32 :: get) ;
1874
- unsafe { setsockopt ( self . as_raw ( ) , IPPROTO_IPV6 , libc:: IPV6_BOUND_IF , index) }
1902
+
1903
+ #[ cfg( any(
1904
+ target_os = "ios" ,
1905
+ target_os = "visionos" ,
1906
+ target_os = "macos" ,
1907
+ target_os = "tvos" ,
1908
+ target_os = "watchos" ,
1909
+ target_os = "illumos" ,
1910
+ target_os = "solaris" ,
1911
+ ) ) ]
1912
+ unsafe {
1913
+ setsockopt ( self . as_raw ( ) , IPPROTO_IPV6 , libc:: IPV6_BOUND_IF , index)
1914
+ }
1915
+
1916
+ #[ cfg( any( target_os = "linux" , target_os = "android" , ) ) ]
1917
+ unsafe {
1918
+ setsockopt (
1919
+ self . as_raw ( ) ,
1920
+ libc:: SOL_SOCKET ,
1921
+ libc:: SO_BINDTOIFINDEX ,
1922
+ index,
1923
+ )
1924
+ }
1875
1925
}
1876
1926
1877
1927
/// Gets the value for `IP_BOUND_IF` option on this socket, i.e. the index
0 commit comments