raw_socket.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. use crate::Result;
  9. /// A socket that captures or transmits the complete frame.
  10. #[derive(Debug)]
  11. pub struct RawSocket {
  12. medium: Medium,
  13. lower: Rc<RefCell<sys::RawSocketDesc>>,
  14. mtu: usize,
  15. }
  16. impl AsRawFd for RawSocket {
  17. fn as_raw_fd(&self) -> RawFd {
  18. self.lower.borrow().as_raw_fd()
  19. }
  20. }
  21. impl RawSocket {
  22. /// Creates a raw socket, bound to the interface called `name`.
  23. ///
  24. /// This requires superuser privileges or a corresponding capability bit
  25. /// set on the executable.
  26. pub fn new(name: &str, medium: Medium) -> io::Result<RawSocket> {
  27. let mut lower = sys::RawSocketDesc::new(name, medium)?;
  28. lower.bind_interface()?;
  29. let mut mtu = lower.interface_mtu()?;
  30. #[cfg(feature = "medium-ethernet")]
  31. if medium == Medium::Ethernet {
  32. // SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
  33. // smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
  34. mtu += crate::wire::EthernetFrame::<&[u8]>::header_len()
  35. }
  36. Ok(RawSocket {
  37. medium,
  38. lower: Rc::new(RefCell::new(lower)),
  39. mtu: mtu,
  40. })
  41. }
  42. }
  43. impl<'a> Device<'a> for RawSocket {
  44. type RxToken = RxToken;
  45. type TxToken = TxToken;
  46. fn capabilities(&self) -> DeviceCapabilities {
  47. DeviceCapabilities {
  48. max_transmission_unit: self.mtu,
  49. medium: self.medium,
  50. ..DeviceCapabilities::default()
  51. }
  52. }
  53. fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
  54. let mut lower = self.lower.borrow_mut();
  55. let mut buffer = vec![0; self.mtu];
  56. match lower.recv(&mut buffer[..]) {
  57. Ok(size) => {
  58. buffer.resize(size, 0);
  59. let rx = RxToken { buffer };
  60. let tx = TxToken {
  61. lower: self.lower.clone(),
  62. };
  63. Some((rx, tx))
  64. }
  65. Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => None,
  66. Err(err) => panic!("{}", err),
  67. }
  68. }
  69. fn transmit(&'a mut self) -> Option<Self::TxToken> {
  70. Some(TxToken {
  71. lower: self.lower.clone(),
  72. })
  73. }
  74. }
  75. #[doc(hidden)]
  76. pub struct RxToken {
  77. buffer: Vec<u8>,
  78. }
  79. impl phy::RxToken for RxToken {
  80. fn consume<R, F>(mut self, _timestamp: Instant, f: F) -> Result<R>
  81. where
  82. F: FnOnce(&mut [u8]) -> Result<R>,
  83. {
  84. f(&mut self.buffer[..])
  85. }
  86. }
  87. #[doc(hidden)]
  88. pub struct TxToken {
  89. lower: Rc<RefCell<sys::RawSocketDesc>>,
  90. }
  91. impl phy::TxToken for TxToken {
  92. fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> Result<R>
  93. where
  94. F: FnOnce(&mut [u8]) -> Result<R>,
  95. {
  96. let mut lower = self.lower.borrow_mut();
  97. let mut buffer = vec![0; len];
  98. let result = f(&mut buffer);
  99. match lower.send(&buffer[..]) {
  100. Ok(_) => result,
  101. Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(crate::Error::Exhausted),
  102. Err(err) => panic!("{}", err),
  103. }
  104. }
  105. }