Skip to content

Commit 7537a8f

Browse files
committed
migrate to tcp/udp layers.
1 parent 4be9631 commit 7537a8f

File tree

11 files changed

+151
-74
lines changed

11 files changed

+151
-74
lines changed

library/std/src/net/tcp.rs

+42
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#[cfg(all(test, not(any(target_os = "emscripten", target_os = "xous"))))]
44
mod tests;
55

6+
use crate::ffi::CString;
67
use crate::fmt;
78
use crate::io::prelude::*;
89
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
@@ -603,6 +604,47 @@ impl TcpStream {
603604
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
604605
self.0.set_nonblocking(nonblocking)
605606
}
607+
608+
/// Bind the socket to an interface
609+
///
610+
/// ```no_run
611+
/// #![feature(unix_set_device)]
612+
///
613+
/// use std::net::TcpStream;
614+
///
615+
/// fn main() -> std::io::Result<()> {
616+
/// let stream = TcpStream::connect("127.0.0.1:8080")
617+
/// .expect("Couldn't connect to the server...");
618+
/// stream.set_device("eth0")?;
619+
/// Ok(())
620+
/// }
621+
///
622+
/// ```
623+
#[unstable(feature = "unix_set_device", issue = "129182")]
624+
pub fn set_device(&self, ifrname: &str) -> io::Result<()> {
625+
self.0.set_device(ifrname)
626+
}
627+
628+
/// Get the interface this socket is bound to
629+
///
630+
/// ```no_run
631+
/// #![feature(unix_set_device)]
632+
///
633+
/// use std::net::TcpStream;
634+
///
635+
/// fn main() -> std::io::Result<()> {
636+
/// let stream = TcpStream::connect("127.0.0.1:8080")
637+
/// .expect("Couldn't connect to the server...");
638+
/// stream.set_device("eth0")?;
639+
/// let name = stream.device()?;
640+
/// assert_eq!(Ok("eth0"), name.to_str());
641+
/// Ok(())
642+
/// }
643+
/// ```
644+
#[unstable(feature = "unix_set_device", issue = "129182")]
645+
pub fn device(&self) -> io::Result<CString> {
646+
self.0.device()
647+
}
606648
}
607649

608650
// In addition to the `impl`s here, `TcpStream` also has `impl`s for

library/std/src/net/udp.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[cfg(all(test, not(any(target_os = "emscripten", target_env = "sgx", target_os = "xous"))))]
22
mod tests;
33

4+
use crate::ffi::CString;
45
use crate::fmt;
56
use crate::io::{self, ErrorKind};
67
use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};
@@ -806,6 +807,45 @@ impl UdpSocket {
806807
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
807808
self.0.set_nonblocking(nonblocking)
808809
}
810+
811+
/// Bind the socket to an interface
812+
///
813+
/// ```no_run
814+
/// #![feature(unix_set_device)]
815+
///
816+
/// use std::net::UdpSocket;
817+
///
818+
/// fn main() -> std::io::Result<()> {
819+
/// let socket = UdpSocket::bind("127.0.0.1:7878").unwrap();
820+
/// socket.set_device("eth0")?;
821+
/// Ok(())
822+
/// }
823+
///
824+
/// ```
825+
#[unstable(feature = "unix_set_device", issue = "129182")]
826+
pub fn set_device(&self, ifrname: &str) -> io::Result<()> {
827+
self.0.set_device(ifrname)
828+
}
829+
830+
/// Get the interface this socket is bound to
831+
///
832+
/// ```no_run
833+
/// #![feature(unix_set_device)]
834+
///
835+
/// use std::net::UdpSocket;
836+
///
837+
/// fn main() -> std::io::Result<()> {
838+
/// let socket = UdpSocket::bind("127.0.0.1:7878").unwrap();
839+
/// socket.set_device("eth0")?;
840+
/// let name = socket.device()?;
841+
/// assert_eq!(Ok("eth0"), name.to_str());
842+
/// Ok(())
843+
/// }
844+
/// ```
845+
#[unstable(feature = "unix_set_device", issue = "129182")]
846+
pub fn device(&self) -> io::Result<CString> {
847+
self.0.device()
848+
}
809849
}
810850

811851
// In addition to the `impl`s here, `UdpSocket` also has `impl`s for

library/std/src/os/unix/net/datagram.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use libc::MSG_NOSIGNAL;
1515
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
1616
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
1717
use super::{sockaddr_un, SocketAddr};
18-
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
1918
use crate::ffi::CString;
2019
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
2120
use crate::io::{IoSlice, IoSliceMut};
@@ -840,14 +839,7 @@ impl UnixDatagram {
840839

841840
/// Bind the socket to an interface
842841
///
843-
#[cfg_attr(
844-
any(target_os = "linux", target_os = "haiku", target_os = "vxworks"),
845-
doc = "```no_run"
846-
)]
847-
#[cfg_attr(
848-
not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")),
849-
doc = "```ignore"
850-
)]
842+
/// ```no_run
851843
/// #![feature(unix_set_device)]
852844
/// use std::os::unix::net::UnixDatagram;
853845
///
@@ -857,22 +849,14 @@ impl UnixDatagram {
857849
/// Ok(())
858850
/// }
859851
/// ```
860-
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
861852
#[unstable(feature = "unix_set_device", issue = "129182")]
862853
pub fn set_device(&self, ifrname: &str) -> io::Result<()> {
863854
self.0.set_device(ifrname)
864855
}
865856

866857
/// Get the interface this socket is bound to
867858
///
868-
#[cfg_attr(
869-
any(target_os = "linux", target_os = "haiku", target_os = "vxworks"),
870-
doc = "```no_run"
871-
)]
872-
#[cfg_attr(
873-
not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")),
874-
doc = "```ignore"
875-
)]
859+
/// ```no_run
876860
/// #![feature(unix_set_device)]
877861
/// use std::os::unix::net::UnixDatagram;
878862
///
@@ -884,7 +868,6 @@ impl UnixDatagram {
884868
/// Ok(())
885869
/// }
886870
/// ```
887-
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
888871
#[unstable(feature = "unix_set_device", issue = "129182")]
889872
pub fn device(&self) -> io::Result<CString> {
890873
self.0.device()

library/std/src/os/unix/net/stream.rs

-54
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use super::{peer_cred, UCred};
1212
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
1313
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
1414
use super::{sockaddr_un, SocketAddr};
15-
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
16-
use crate::ffi::CString;
1715
use crate::fmt;
1816
use crate::io::{self, IoSlice, IoSliceMut};
1917
use crate::net::Shutdown;
@@ -407,58 +405,6 @@ impl UnixStream {
407405
self.0.set_mark(mark)
408406
}
409407

410-
/// Bind the socket to an interface
411-
///
412-
#[cfg_attr(
413-
any(target_os = "linux", target_os = "haiku", target_os = "vxworks"),
414-
doc = "```no_run"
415-
)]
416-
#[cfg_attr(
417-
not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")),
418-
doc = "```ignore"
419-
)]
420-
/// #![feature(unix_set_device)]
421-
/// use std::os::unix::net::UnixStream;
422-
///
423-
/// fn main() -> std::io::Result<()> {
424-
/// let socket = UnixStream::connect("/tmp/sock")?;
425-
/// socket.set_device("eth0")?;
426-
/// Ok(())
427-
/// }
428-
/// ```
429-
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
430-
#[unstable(feature = "unix_set_device", issue = "129182")]
431-
pub fn set_device(&self, ifrname: &str) -> io::Result<()> {
432-
self.0.set_device(ifrname)
433-
}
434-
435-
/// Get the interface this socket is bound to
436-
///
437-
#[cfg_attr(
438-
any(target_os = "linux", target_os = "haiku", target_os = "vxworks"),
439-
doc = "```no_run"
440-
)]
441-
#[cfg_attr(
442-
not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")),
443-
doc = "```ignore"
444-
)]
445-
/// #![feature(unix_set_device)]
446-
/// use std::os::unix::net::UnixStream;
447-
///
448-
/// fn main() -> std::io::Result<()> {
449-
/// let socket = UnixStream::connect("/tmp/sock")?;
450-
/// socket.set_device("eth0")?;
451-
/// let name = socket.device()?;
452-
/// assert_eq!(Ok("eth0"), name.to_str());
453-
/// Ok(())
454-
/// }
455-
/// ```
456-
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
457-
#[unstable(feature = "unix_set_device", issue = "129182")]
458-
pub fn device(&self) -> io::Result<CString> {
459-
self.0.device()
460-
}
461-
462408
/// Returns the value of the `SO_ERROR` option.
463409
///
464410
/// # Examples

library/std/src/sys/pal/hermit/net.rs

+8
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ impl Socket {
312312
pub fn as_raw(&self) -> RawFd {
313313
self.0.as_raw_fd()
314314
}
315+
316+
pub fn device(&self) -> io::Result<crate::ffi::CString> {
317+
unimplemented!()
318+
}
319+
320+
pub fn set_device(&self, _: &str) -> io::Result<()> {
321+
unimplemented!()
322+
}
315323
}
316324

317325
impl AsInner<FileDesc> for Socket {

library/std/src/sys/pal/sgx/net.rs

+8
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ impl TcpStream {
207207
pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
208208
sgx_ineffective(())
209209
}
210+
211+
pub fn device(&self) -> io::Result<crate::ffi::CString> {
212+
unimplemented!()
213+
}
214+
215+
pub fn set_device(&self, _: &str) -> io::Result<()> {
216+
unimplemented!()
217+
}
210218
}
211219

212220
impl AsInner<Socket> for TcpStream {

library/std/src/sys/pal/solid/net.rs

+8
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ impl Socket {
380380
pub fn as_raw(&self) -> c_int {
381381
self.as_raw_fd()
382382
}
383+
384+
pub fn device(&self) -> io::Result<crate::ffi::CString> {
385+
unimplemented!()
386+
}
387+
388+
pub fn set_device(&self, _: &str) -> io::Result<()> {
389+
unimplemented!()
390+
}
383391
}
384392

385393
impl FromInner<OwnedFd> for Socket {

library/std/src/sys/pal/unix/net.rs

+10
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ impl Socket {
572572
Ok(crate::ffi::CString::new(name.to_bytes()).unwrap())
573573
}
574574

575+
#[cfg(not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")))]
576+
pub fn device(&self) -> io::Result<crate::ffi::CString> {
577+
unimplemented!()
578+
}
579+
575580
#[cfg(any(target_os = "linux", target_os = "haiku", target_os = "vxworks"))]
576581
pub fn set_device(&self, ifrname: &str) -> io::Result<()> {
577582
let istr = ifrname.as_bytes();
@@ -587,6 +592,11 @@ impl Socket {
587592
setsockopt(self, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, buf)
588593
}
589594

595+
#[cfg(not(any(target_os = "linux", target_os = "haiku", target_os = "vxworks")))]
596+
pub fn set_device(&self, _: &str) -> io::Result<()> {
597+
unimplemented!()
598+
}
599+
590600
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
591601
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
592602
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }

library/std/src/sys/pal/wasi/net.rs

+8
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ impl TcpStream {
194194
pub fn into_socket(self) -> Socket {
195195
self.inner
196196
}
197+
198+
pub fn device(&self) -> io::Result<crate::ffi::CString> {
199+
unimplemented!()
200+
}
201+
202+
pub fn set_device(&self, _: &str) -> io::Result<()> {
203+
unimplemented!()
204+
}
197205
}
198206

199207
impl FromInner<Socket> for TcpStream {

library/std/src/sys/pal/windows/net.rs

+8
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,14 @@ impl Socket {
521521
debug_assert_eq!(mem::align_of::<c::SOCKET>(), mem::align_of::<RawSocket>());
522522
unsafe { Self::from_raw_socket(raw as RawSocket) }
523523
}
524+
525+
pub fn device(&self) -> io::Result<crate::ffi::CString> {
526+
unimplemented!()
527+
}
528+
529+
pub fn set_device(&self, _: &str) -> io::Result<()> {
530+
unimplemented!()
531+
}
524532
}
525533

526534
#[unstable(reason = "not public", issue = "none", feature = "fd_read")]

library/std/src/sys_common/net.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(test)]
22
mod tests;
33

4-
use crate::ffi::{c_int, c_void};
4+
use crate::ffi::{c_int, c_void, CString};
55
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut};
66
use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
77
use crate::sys::common::small_c_string::run_with_cstr;
@@ -352,6 +352,14 @@ impl TcpStream {
352352
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
353353
self.inner.set_nonblocking(nonblocking)
354354
}
355+
356+
pub fn device(&self) -> io::Result<CString> {
357+
self.inner.device()
358+
}
359+
360+
pub fn set_device(&self, ifrname: &str) -> io::Result<()> {
361+
self.inner.set_device(ifrname)
362+
}
355363
}
356364

357365
impl AsInner<Socket> for TcpStream {
@@ -702,6 +710,14 @@ impl UdpSocket {
702710
let (addr, len) = addr?.into_inner();
703711
cvt_r(|| unsafe { c::connect(self.inner.as_raw(), addr.as_ptr(), len) }).map(drop)
704712
}
713+
714+
pub fn device(&self) -> io::Result<CString> {
715+
self.inner.device()
716+
}
717+
718+
pub fn set_device(&self, ifrname: &str) -> io::Result<()> {
719+
self.inner.set_device(ifrname)
720+
}
705721
}
706722

707723
impl FromInner<Socket> for UdpSocket {

0 commit comments

Comments
 (0)