@@ -24,6 +24,8 @@ use std::net::{Ipv4Addr, Ipv6Addr};
2424 target_os = "watchos" ,
2525 target_os = "illumos" ,
2626 target_os = "solaris" ,
27+ target_os = "linux" ,
28+ target_os = "android" ,
2729 )
2830) ) ]
2931use std:: num:: NonZeroU32 ;
@@ -1820,7 +1822,7 @@ impl crate::Socket {
18201822 . map ( |_| ( ) )
18211823 }
18221824
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.
18241826 ///
18251827 /// If a socket is bound to an interface, only packets received from that
18261828 /// particular interface are processed by the socket.
@@ -1840,14 +1842,38 @@ impl crate::Socket {
18401842 target_os = "watchos" ,
18411843 target_os = "illumos" ,
18421844 target_os = "solaris" ,
1845+ target_os = "linux" ,
1846+ target_os = "android" ,
18431847 )
18441848 ) ) ]
18451849 pub fn bind_device_by_index_v4 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
18461850 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+ }
18481874 }
18491875
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.
18511877 ///
18521878 /// If a socket is bound to an interface, only packets received from that
18531879 /// particular interface are processed by the socket.
@@ -1867,11 +1893,35 @@ impl crate::Socket {
18671893 target_os = "watchos" ,
18681894 target_os = "illumos" ,
18691895 target_os = "solaris" ,
1896+ target_os = "linux" ,
1897+ target_os = "android" ,
18701898 )
18711899 ) ) ]
18721900 pub fn bind_device_by_index_v6 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
18731901 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+ }
18751925 }
18761926
18771927 /// Gets the value for `IP_BOUND_IF` option on this socket, i.e. the index
0 commit comments