tuntap_interface.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use std::cell::RefCell;
  2. use std::io;
  3. use std::os::unix::io::{AsRawFd, RawFd};
  4. use std::rc::Rc;
  5. use std::vec::Vec;
  6. use crate::phy::{self, sys, Device, DeviceCapabilities, Medium};
  7. use crate::time::Instant;
  8. /// A virtual TUN (IP) or TAP (Ethernet) interface.
  9. #[derive(Debug)]
  10. pub struct TunTapInterface {
  11. lower: Rc<RefCell<sys::TunTapInterfaceDesc>>,
  12. mtu: usize,
  13. medium: Medium,
  14. }
  15. impl AsRawFd for TunTapInterface {
  16. fn as_raw_fd(&self) -> RawFd {
  17. self.lower.borrow().as_raw_fd()
  18. }
  19. }
  20. impl TunTapInterface {
  21. /// Attaches to a TUN/TAP interface called `name`, or creates it if it does not exist.
  22. ///
  23. /// If `name` is a persistent interface configured with UID of the current user,
  24. /// no special privileges are needed. Otherwise, this requires superuser privileges
  25. /// or a corresponding capability set on the executable.
  26. pub fn new(name: &str, medium: Medium) -> io::Result<TunTapInterface> {
  27. let mut lower = sys::TunTapInterfaceDesc::new(name, medium)?;
  28. lower.attach_interface()?;
  29. let mtu = lower.interface_mtu()?;
  30. Ok(TunTapInterface {
  31. lower: Rc::new(RefCell::new(lower)),
  32. mtu,
  33. medium,
  34. })
  35. }
  36. }
  37. impl Device for TunTapInterface {
  38. type RxToken<'a> = RxToken;
  39. type TxToken<'a> = TxToken;
  40. fn capabilities(&self) -> DeviceCapabilities {
  41. DeviceCapabilities {
  42. max_transmission_unit: self.mtu,
  43. medium: self.medium,
  44. ..DeviceCapabilities::default()
  45. }
  46. }
  47. fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
  48. let mut lower = self.lower.borrow_mut();
  49. let mut buffer = vec![0; self.mtu];
  50. match lower.recv(&mut buffer[..]) {
  51. Ok(size) => {
  52. buffer.resize(size, 0);
  53. let rx = RxToken { buffer };
  54. let tx = TxToken {
  55. lower: self.lower.clone(),
  56. };
  57. Some((rx, tx))
  58. }
  59. Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => None,
  60. Err(err) => panic!("{}", err),
  61. }
  62. }
  63. fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
  64. Some(TxToken {
  65. lower: self.lower.clone(),
  66. })
  67. }
  68. }
  69. #[doc(hidden)]
  70. pub struct RxToken {
  71. buffer: Vec<u8>,
  72. }
  73. impl phy::RxToken for RxToken {
  74. fn consume<R, F>(mut self, f: F) -> R
  75. where
  76. F: FnOnce(&mut [u8]) -> R,
  77. {
  78. f(&mut self.buffer[..])
  79. }
  80. }
  81. #[doc(hidden)]
  82. pub struct TxToken {
  83. lower: Rc<RefCell<sys::TunTapInterfaceDesc>>,
  84. }
  85. impl phy::TxToken for TxToken {
  86. fn consume<R, F>(self, len: usize, f: F) -> R
  87. where
  88. F: FnOnce(&mut [u8]) -> R,
  89. {
  90. let mut lower = self.lower.borrow_mut();
  91. let mut buffer = vec![0; len];
  92. let result = f(&mut buffer);
  93. match lower.send(&buffer[..]) {
  94. Ok(_) => {}
  95. Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
  96. net_debug!("phy: tx failed due to WouldBlock")
  97. }
  98. Err(err) => panic!("{}", err),
  99. }
  100. result
  101. }
  102. }