@@ -1833,6 +1833,35 @@ impl crate::Socket {
1833
1833
. map ( |_| ( ) )
1834
1834
}
1835
1835
1836
+ /// This method is deprecated, use [`bind_device_by_index_v4`].
1837
+ ///
1838
+ /// [`bind_device_by_index_v4`]: crate::Socket::bind_device_by_index_v4
1839
+ #[ cfg( all(
1840
+ feature = "all" ,
1841
+ any(
1842
+ target_os = "ios" ,
1843
+ target_os = "macos" ,
1844
+ target_os = "tvos" ,
1845
+ target_os = "watchos" ,
1846
+ )
1847
+ ) ) ]
1848
+ #[ cfg_attr(
1849
+ docsrs,
1850
+ doc( cfg( all(
1851
+ feature = "all" ,
1852
+ any(
1853
+ target_os = "ios" ,
1854
+ target_os = "macos" ,
1855
+ target_os = "tvos" ,
1856
+ target_os = "watchos" ,
1857
+ )
1858
+ ) ) )
1859
+ ) ]
1860
+ #[ deprecated]
1861
+ pub fn bind_device_by_index ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1862
+ self . bind_device_by_index_v4 ( interface)
1863
+ }
1864
+
1836
1865
/// Sets the value for `IP_BOUND_IF` option on this socket.
1837
1866
///
1838
1867
/// If a socket is bound to an interface, only packets received from that
@@ -1864,11 +1893,47 @@ impl crate::Socket {
1864
1893
)
1865
1894
) ) )
1866
1895
) ]
1867
- pub fn bind_device_by_index ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1896
+ pub fn bind_device_by_index_v4 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1868
1897
let index = interface. map_or ( 0 , NonZeroU32 :: get) ;
1869
1898
unsafe { setsockopt ( self . as_raw ( ) , IPPROTO_IP , libc:: IP_BOUND_IF , index) }
1870
1899
}
1871
1900
1901
+ /// Sets the value for `IPV6_BOUND_IF` option on this socket.
1902
+ ///
1903
+ /// If a socket is bound to an interface, only packets received from that
1904
+ /// particular interface are processed by the socket.
1905
+ ///
1906
+ /// If `interface` is `None`, the binding is removed. If the `interface`
1907
+ /// index is not valid, an error is returned.
1908
+ ///
1909
+ /// One can use [`libc::if_nametoindex`] to convert an interface alias to an
1910
+ /// index.
1911
+ #[ cfg( all(
1912
+ feature = "all" ,
1913
+ any(
1914
+ target_os = "ios" ,
1915
+ target_os = "macos" ,
1916
+ target_os = "tvos" ,
1917
+ target_os = "watchos" ,
1918
+ )
1919
+ ) ) ]
1920
+ #[ cfg_attr(
1921
+ docsrs,
1922
+ doc( cfg( all(
1923
+ feature = "all" ,
1924
+ any(
1925
+ target_os = "ios" ,
1926
+ target_os = "macos" ,
1927
+ target_os = "tvos" ,
1928
+ target_os = "watchos" ,
1929
+ )
1930
+ ) ) )
1931
+ ) ]
1932
+ pub fn bind_device_by_index_v6 ( & self , interface : Option < NonZeroU32 > ) -> io:: Result < ( ) > {
1933
+ let index = interface. map_or ( 0 , NonZeroU32 :: get) ;
1934
+ unsafe { setsockopt ( self . as_raw ( ) , IPPROTO_IPV6 , libc:: IPV6_BOUND_IF , index) }
1935
+ }
1936
+
1872
1937
/// Gets the value for `IP_BOUND_IF` option on this socket, i.e. the index
1873
1938
/// for the interface to which the socket is bound.
1874
1939
///
@@ -1901,6 +1966,39 @@ impl crate::Socket {
1901
1966
Ok ( NonZeroU32 :: new ( index) )
1902
1967
}
1903
1968
1969
+ /// Gets the value for `IPV6_BOUND_IF` option on this socket, i.e. the index
1970
+ /// for the interface to which the socket is bound.
1971
+ ///
1972
+ /// Returns `None` if the socket is not bound to any interface, otherwise
1973
+ /// returns an interface index.
1974
+ #[ cfg( all(
1975
+ feature = "all" ,
1976
+ any(
1977
+ target_os = "ios" ,
1978
+ target_os = "macos" ,
1979
+ target_os = "tvos" ,
1980
+ target_os = "watchos" ,
1981
+ )
1982
+ ) ) ]
1983
+ #[ cfg_attr(
1984
+ docsrs,
1985
+ doc( cfg( all(
1986
+ feature = "all" ,
1987
+ any(
1988
+ target_os = "ios" ,
1989
+ target_os = "macos" ,
1990
+ target_os = "tvos" ,
1991
+ target_os = "watchos" ,
1992
+ )
1993
+ ) ) )
1994
+ ) ]
1995
+ pub fn device_index_v6 ( & self ) -> io:: Result < Option < NonZeroU32 > > {
1996
+ let index = unsafe {
1997
+ getsockopt :: < libc:: c_uint > ( self . as_raw ( ) , IPPROTO_IPV6 , libc:: IPV6_BOUND_IF ) ?
1998
+ } ;
1999
+ Ok ( NonZeroU32 :: new ( index) )
2000
+ }
2001
+
1904
2002
/// Get the value of the `SO_INCOMING_CPU` option on this socket.
1905
2003
///
1906
2004
/// For more information about this option, see [`set_cpu_affinity`].
0 commit comments