|
1 | 1 | use libc::{c_int, c_void, size_t, sockaddr, socklen_t, MSG_PEEK};
|
2 | 2 |
|
3 |
| -use crate::ffi::CStr; |
| 3 | +use crate::ffi::{CStr, CString}; |
4 | 4 | use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
|
5 | 5 | use crate::net::{Shutdown, SocketAddr};
|
6 | 6 | use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
|
@@ -564,18 +564,24 @@ impl Socket {
|
564 | 564 | }
|
565 | 565 |
|
566 | 566 | #[cfg(any(target_os = "linux", target_os = "haiku", target_os = "vxworks"))]
|
567 |
| - pub fn todevice(&self) -> io::Result<&CStr> { |
| 567 | + pub fn todevice(&self) -> io::Result<CString> { |
568 | 568 | let buf: [libc::c_char; libc::IFNAMSIZ] =
|
569 | 569 | getsockopt(self, libc::SOL_SOCKET, libc::SO_BINDTODEVICE)?;
|
570 | 570 | let s: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len()) };
|
571 |
| - let ifrname = CStr::from_bytes_until_nul(s).unwrap(); |
572 |
| - Ok(ifrname) |
| 571 | + let name = CStr::from_bytes_until_nul(s).unwrap(); |
| 572 | + Ok(CString::new(name.to_bytes()).unwrap()) |
573 | 573 | }
|
574 | 574 |
|
575 | 575 | #[cfg(any(target_os = "linux", target_os = "haiku", target_os = "vxworks"))]
|
576 | 576 | pub fn set_todevice(&self, ifrname: &CStr) -> io::Result<()> {
|
| 577 | + let istr = ifrname.to_bytes(); |
| 578 | + |
| 579 | + if istr.len() >= libc::IFNAMSIZ { |
| 580 | + return Err(io::Error::from_raw_os_error(libc::ENAMETOOLONG)); |
| 581 | + } |
| 582 | + |
577 | 583 | let mut buf = [0; libc::IFNAMSIZ];
|
578 |
| - for (src, dst) in ifrname.to_bytes().iter().zip(&mut buf[..libc::IFNAMSIZ - 1]) { |
| 584 | + for (src, dst) in istr.iter().zip(&mut buf[..libc::IFNAMSIZ - 1]) { |
579 | 585 | *dst = *src as libc::c_char;
|
580 | 586 | }
|
581 | 587 | setsockopt(self, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, buf)
|
|
0 commit comments