mod.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*! Communication between endpoints.
  2. The `socket` module deals with *network endpoints* and *buffering*.
  3. It provides interfaces for accessing buffers of data, and protocol state machines
  4. for filling and emptying these buffers.
  5. The programming interface implemented here differs greatly from the common Berkeley socket
  6. interface. Specifically, in the Berkeley interface the buffering is implicit:
  7. the operating system decides on the good size for a buffer and manages it.
  8. The interface implemented by this module uses explicit buffering: you decide on the good
  9. size for a buffer, allocate it, and let the networking stack use it.
  10. */
  11. use crate::iface::Context;
  12. use crate::time::Instant;
  13. #[cfg(feature = "socket-dhcpv4")]
  14. pub mod dhcpv4;
  15. #[cfg(feature = "socket-dns")]
  16. pub mod dns;
  17. #[cfg(feature = "socket-icmp")]
  18. pub mod icmp;
  19. #[cfg(feature = "socket-raw")]
  20. pub mod raw;
  21. #[cfg(feature = "socket-tcp")]
  22. pub mod tcp;
  23. #[cfg(feature = "socket-udp")]
  24. pub mod udp;
  25. #[cfg(feature = "async")]
  26. mod waker;
  27. #[cfg(feature = "async")]
  28. pub(crate) use self::waker::WakerRegistration;
  29. /// Gives an indication on the next time the socket should be polled.
  30. #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy)]
  31. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  32. pub(crate) enum PollAt {
  33. /// The socket needs to be polled immediately.
  34. Now,
  35. /// The socket needs to be polled at given [Instant][struct.Instant].
  36. Time(Instant),
  37. /// The socket does not need to be polled unless there are external changes.
  38. Ingress,
  39. }
  40. /// A network socket.
  41. ///
  42. /// This enumeration abstracts the various types of sockets based on the IP protocol.
  43. /// To downcast a `Socket` value to a concrete socket, use the [AnySocket] trait,
  44. /// e.g. to get `udp::Socket`, call `udp::Socket::downcast(socket)`.
  45. ///
  46. /// It is usually more convenient to use [SocketSet::get] instead.
  47. ///
  48. /// [AnySocket]: trait.AnySocket.html
  49. /// [SocketSet::get]: struct.SocketSet.html#method.get
  50. #[derive(Debug)]
  51. pub enum Socket<'a> {
  52. #[cfg(feature = "socket-raw")]
  53. Raw(raw::Socket<'a>),
  54. #[cfg(feature = "socket-icmp")]
  55. Icmp(icmp::Socket<'a>),
  56. #[cfg(feature = "socket-udp")]
  57. Udp(udp::Socket<'a>),
  58. #[cfg(feature = "socket-tcp")]
  59. Tcp(tcp::Socket<'a>),
  60. #[cfg(feature = "socket-dhcpv4")]
  61. Dhcpv4(dhcpv4::Socket<'a>),
  62. #[cfg(feature = "socket-dns")]
  63. Dns(dns::Socket<'a>),
  64. }
  65. impl<'a> Socket<'a> {
  66. pub(crate) fn poll_at(&self, cx: &mut Context) -> PollAt {
  67. match self {
  68. #[cfg(feature = "socket-raw")]
  69. Socket::Raw(s) => s.poll_at(cx),
  70. #[cfg(feature = "socket-icmp")]
  71. Socket::Icmp(s) => s.poll_at(cx),
  72. #[cfg(feature = "socket-udp")]
  73. Socket::Udp(s) => s.poll_at(cx),
  74. #[cfg(feature = "socket-tcp")]
  75. Socket::Tcp(s) => s.poll_at(cx),
  76. #[cfg(feature = "socket-dhcpv4")]
  77. Socket::Dhcpv4(s) => s.poll_at(cx),
  78. #[cfg(feature = "socket-dns")]
  79. Socket::Dns(s) => s.poll_at(cx),
  80. }
  81. }
  82. }
  83. /// A conversion trait for network sockets.
  84. pub trait AnySocket<'a> {
  85. fn upcast(self) -> Socket<'a>;
  86. fn downcast<'c>(socket: &'c Socket<'a>) -> Option<&'c Self>
  87. where
  88. Self: Sized;
  89. fn downcast_mut<'c>(socket: &'c mut Socket<'a>) -> Option<&'c mut Self>
  90. where
  91. Self: Sized;
  92. }
  93. macro_rules! from_socket {
  94. ($socket:ty, $variant:ident) => {
  95. impl<'a> AnySocket<'a> for $socket {
  96. fn upcast(self) -> Socket<'a> {
  97. Socket::$variant(self)
  98. }
  99. fn downcast<'c>(socket: &'c Socket<'a>) -> Option<&'c Self> {
  100. #[allow(unreachable_patterns)]
  101. match socket {
  102. Socket::$variant(socket) => Some(socket),
  103. _ => None,
  104. }
  105. }
  106. fn downcast_mut<'c>(socket: &'c mut Socket<'a>) -> Option<&'c mut Self> {
  107. #[allow(unreachable_patterns)]
  108. match socket {
  109. Socket::$variant(socket) => Some(socket),
  110. _ => None,
  111. }
  112. }
  113. }
  114. };
  115. }
  116. #[cfg(feature = "socket-raw")]
  117. from_socket!(raw::Socket<'a>, Raw);
  118. #[cfg(feature = "socket-icmp")]
  119. from_socket!(icmp::Socket<'a>, Icmp);
  120. #[cfg(feature = "socket-udp")]
  121. from_socket!(udp::Socket<'a>, Udp);
  122. #[cfg(feature = "socket-tcp")]
  123. from_socket!(tcp::Socket<'a>, Tcp);
  124. #[cfg(feature = "socket-dhcpv4")]
  125. from_socket!(dhcpv4::Socket<'a>, Dhcpv4);
  126. #[cfg(feature = "socket-dns")]
  127. from_socket!(dns::Socket<'a>, Dns);