-
Notifications
You must be signed in to change notification settings - Fork 13.4k
std::net: adding new option todevice. #129172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ use super::{peer_cred, UCred}; | |
#[cfg(any(doc, target_os = "android", target_os = "linux"))] | ||
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary}; | ||
use super::{sockaddr_un, SocketAddr}; | ||
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))] | ||
use crate::ffi::CStr; | ||
use crate::fmt; | ||
use crate::io::{self, IoSlice, IoSliceMut}; | ||
use crate::net::Shutdown; | ||
|
@@ -405,6 +407,58 @@ impl UnixStream { | |
self.0.set_mark(mark) | ||
} | ||
|
||
/// Bind the socket to an interface | ||
/// | ||
#[cfg_attr( | ||
any(target_os = "linux", target_os = "haiku", target_os = "vxworks"), | ||
doc = "```no_run" | ||
)] | ||
#[cfg_attr( | ||
not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")), | ||
doc = "```ignore" | ||
)] | ||
/// #![feature(unix_set_todevice)] | ||
/// use std::os::unix::net::UnixStream; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let socket = UnixStream::connect("/tmp/sock")?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this using The examples should be things expected to work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, it looks like these are being added to UNIX sockets? Shouldn't these be for TCP/UDP, not UNIX? |
||
/// socket.set_todevice(c"eth0")?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))] | ||
#[unstable(feature = "unix_set_todevice", issue = "129182")] | ||
pub fn set_todevice(&self, ifrname: &CStr) -> io::Result<()> { | ||
self.0.set_todevice(ifrname) | ||
} | ||
|
||
/// Get the interface this socket is bound to | ||
/// | ||
#[cfg_attr( | ||
any(target_os = "linux", target_os = "haiku", target_os = "vxworks"), | ||
doc = "```no_run" | ||
)] | ||
#[cfg_attr( | ||
not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")), | ||
doc = "```ignore" | ||
)] | ||
/// #![feature(unix_set_todevice)] | ||
/// use std::os::unix::net::UnixStream; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let socket = UnixStream::connect("/tmp/sock")?; | ||
/// socket.set_todevice(c"eth0")?; | ||
/// let name = socket.todevice()?; | ||
/// assert_eq!(Ok("eth0"), name.to_str()); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))] | ||
#[unstable(feature = "unix_set_todevice", issue = "129182")] | ||
pub fn todevice(&self) -> io::Result<&CStr> { | ||
self.0.todevice() | ||
} | ||
|
||
/// Returns the value of the `SO_ERROR` option. | ||
/// | ||
/// # Examples | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -563,6 +563,24 @@ impl Socket { | |
setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int) | ||
} | ||
|
||
#[cfg(any(target_os = "linux", target_os = "haiku", target_os = "vxworks"))] | ||
pub fn todevice(&self) -> io::Result<&CStr> { | ||
let buf: [libc::c_char; libc::IFNAMSIZ] = | ||
getsockopt(self, libc::SOL_SOCKET, libc::SO_BINDTODEVICE)?; | ||
let s: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len()) }; | ||
let ifrname = CStr::from_bytes_until_nul(s).unwrap(); | ||
Ok(ifrname) | ||
} | ||
kennytm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[cfg(any(target_os = "linux", target_os = "haiku", target_os = "vxworks"))] | ||
pub fn set_todevice(&self, ifrname: &CStr) -> io::Result<()> { | ||
let mut buf = [0; libc::IFNAMSIZ]; | ||
for (src, dst) in ifrname.to_bytes().iter().zip(&mut buf[..libc::IFNAMSIZ - 1]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should probably check if the input name is too long rather than silently truncating it because you used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That value is already truncated to size specified by optlen when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plus length of interface is 16 on most unix systems, so no value is truncated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what I mean is, assuming |
||
*dst = *src as libc::c_char; | ||
} | ||
setsockopt(self, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, buf) | ||
} | ||
|
||
pub fn take_error(&self) -> io::Result<Option<io::Error>> { | ||
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?; | ||
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something seems odd here.
The assertion fails.