lib.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #![cfg_attr(not(any(test, feature = "std")), no_std)]
  2. #![deny(unsafe_code)]
  3. #![cfg_attr(
  4. all(
  5. any(feature = "proto-ipv4", feature = "proto-ipv6"),
  6. feature = "medium-ethernet"
  7. ),
  8. deny(unused)
  9. )]
  10. //! The _smoltcp_ library is built in a layered structure, with the layers corresponding
  11. //! to the levels of API abstraction. Only the highest layers would be used by a typical
  12. //! application; however, the goal of _smoltcp_ is not just to provide a simple interface
  13. //! for writing applications but also to be a toolbox of networking primitives, so
  14. //! every layer is fully exposed and documented.
  15. //!
  16. //! When discussing networking stacks and layering, often the [OSI model][osi] is invoked.
  17. //! _smoltcp_ makes no effort to conform to the OSI model as it is not applicable to TCP/IP.
  18. //!
  19. //! # The socket layer
  20. //! The socket layer APIs are provided in the module [socket](socket/index.html); currently,
  21. //! raw, ICMP, TCP, and UDP sockets are provided. The socket API provides the usual primitives,
  22. //! but necessarily differs in many from the [Berkeley socket API][berk], as the latter was
  23. //! not designed to be used without heap allocation.
  24. //!
  25. //! The socket layer provides the buffering, packet construction and validation, and (for
  26. //! stateful sockets) the state machines, but it is interface-agnostic. An application must
  27. //! use sockets together with a network interface.
  28. //!
  29. //! # The interface layer
  30. //! The interface layer APIs are provided in the module [iface](iface/index.html); currently,
  31. //! Ethernet interface is provided.
  32. //!
  33. //! The interface layer handles the control messages, physical addressing and neighbor discovery.
  34. //! It routes packets to and from sockets.
  35. //!
  36. //! # The physical layer
  37. //! The physical layer APIs are provided in the module [phy](phy/index.html); currently,
  38. //! raw socket and TAP interface are provided. In addition, two _middleware_ interfaces
  39. //! are provided: the _tracer device_, which prints a human-readable representation of packets,
  40. //! and the _fault injector device_, which randomly introduces errors into the transmitted
  41. //! and received packet sequences.
  42. //!
  43. //! The physical layer handles interaction with a platform-specific network device.
  44. //!
  45. //! # The wire layers
  46. //! Unlike the higher layers, the wire layer APIs will not be used by a typical application.
  47. //! They however are the bedrock of _smoltcp_, and everything else is built on top of them.
  48. //!
  49. //! The wire layer APIs are designed by the principle "make illegal states irrepresentable".
  50. //! If a wire layer object can be constructed, then it can also be parsed from or emitted to
  51. //! a lower level.
  52. //!
  53. //! The wire layer APIs also provide _tcpdump_-like pretty printing.
  54. //!
  55. //! ## The representation layer
  56. //! The representation layer APIs are provided in the module [wire].
  57. //!
  58. //! The representation layer exists to reduce the state space of raw packets. Raw packets
  59. //! may be nonsensical in a multitude of ways: invalid checksums, impossible combinations of flags,
  60. //! pointers to fields out of bounds, meaningless options... Representations shed all that,
  61. //! as well as any features not supported by _smoltcp_.
  62. //!
  63. //! ## The packet layer
  64. //! The packet layer APIs are also provided in the module [wire].
  65. //!
  66. //! The packet layer exists to provide a more structured way to work with packets than
  67. //! treating them as sequences of octets. It makes no judgement as to content of the packets,
  68. //! except where necessary to provide safe access to fields, and strives to implement every
  69. //! feature ever defined, to ensure that, when the representation layer is unable to make sense
  70. //! of a packet, it is still logged correctly and in full.
  71. //!
  72. //! # Minimum Supported Rust Version (MSRV)
  73. //!
  74. //! This crate is guaranteed to compile on stable Rust 1.40 and up with any valid set of features.
  75. //! It *might* compile on older versions but that may change in any new patch release.
  76. //!
  77. //! The exception is when using the `defmt` feature, in which case `defmt`'s MSRV applies, which
  78. //! is higher than 1.40.
  79. //!
  80. //! [wire]: wire/index.html
  81. //! [osi]: https://en.wikipedia.org/wiki/OSI_model
  82. //! [berk]: https://en.wikipedia.org/wiki/Berkeley_sockets
  83. /* XXX compiler bug
  84. #![cfg(not(any(feature = "socket-raw",
  85. feature = "socket-udp",
  86. feature = "socket-tcp")))]
  87. compile_error!("at least one socket needs to be enabled"); */
  88. #![allow(clippy::match_like_matches_macro)]
  89. #![allow(clippy::redundant_field_names)]
  90. #![allow(clippy::identity_op)]
  91. #![allow(clippy::option_map_unit_fn)]
  92. #![allow(clippy::unit_arg)]
  93. #[cfg(any(feature = "std", feature = "alloc"))]
  94. extern crate alloc;
  95. #[cfg(not(any(feature = "proto-ipv4", feature = "proto-ipv6")))]
  96. compile_error!("You must enable at least one of the following features: proto-ipv4, proto-ipv6");
  97. #[cfg(all(
  98. feature = "socket",
  99. not(any(
  100. feature = "socket-raw",
  101. feature = "socket-udp",
  102. feature = "socket-tcp",
  103. feature = "socket-icmp",
  104. ))
  105. ))]
  106. compile_error!("If you enable the socket feature, you must enable at least one of the following features: socket-raw, socket-udp, socket-tcp, socket-icmp");
  107. #[cfg(all(
  108. feature = "socket",
  109. not(any(feature = "medium-ethernet", feature = "medium-ip",))
  110. ))]
  111. compile_error!("If you enable the socket feature, you must enable at least one of the following features: medium-ip, medium-ethernet");
  112. #[cfg(all(feature = "defmt", feature = "log"))]
  113. compile_error!("You must enable at most one of the following features: defmt, log");
  114. use core::fmt;
  115. #[macro_use]
  116. mod macros;
  117. mod parsers;
  118. pub mod iface;
  119. pub mod phy;
  120. #[cfg(feature = "socket")]
  121. pub mod socket;
  122. pub mod storage;
  123. pub mod time;
  124. pub mod wire;
  125. /// The error type for the networking stack.
  126. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  127. #[non_exhaustive]
  128. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  129. pub enum Error {
  130. /// An operation cannot proceed because a buffer is empty or full.
  131. Exhausted,
  132. /// An operation is not permitted in the current state.
  133. Illegal,
  134. /// An endpoint or address of a remote host could not be translated to a lower level address.
  135. /// E.g. there was no an Ethernet address corresponding to an IPv4 address in the ARP cache,
  136. /// or a TCP connection attempt was made to an unspecified endpoint.
  137. Unaddressable,
  138. /// The operation is finished.
  139. /// E.g. when reading from a TCP socket, there's no more data to read because the remote
  140. /// has closed the connection.
  141. Finished,
  142. /// An incoming packet could not be parsed because some of its fields were out of bounds
  143. /// of the received data.
  144. Truncated,
  145. /// An incoming packet had an incorrect checksum and was dropped.
  146. Checksum,
  147. /// An incoming packet could not be recognized and was dropped.
  148. /// E.g. an Ethernet packet with an unknown EtherType.
  149. Unrecognized,
  150. /// An incoming IP packet has been split into several IP fragments and was dropped,
  151. /// since IP reassembly is not supported.
  152. Fragmented,
  153. /// An incoming packet was recognized but was self-contradictory.
  154. /// E.g. a TCP packet with both SYN and FIN flags set.
  155. Malformed,
  156. /// An incoming packet was recognized but contradicted internal state.
  157. /// E.g. a TCP packet addressed to a socket that doesn't exist.
  158. Dropped,
  159. }
  160. #[cfg(feature = "std")]
  161. impl std::error::Error for Error {}
  162. /// The result type for the networking stack.
  163. pub type Result<T> = core::result::Result<T, Error>;
  164. impl fmt::Display for Error {
  165. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  166. match *self {
  167. Error::Exhausted => write!(f, "buffer space exhausted"),
  168. Error::Illegal => write!(f, "illegal operation"),
  169. Error::Unaddressable => write!(f, "unaddressable destination"),
  170. Error::Finished => write!(f, "operation finished"),
  171. Error::Truncated => write!(f, "truncated packet"),
  172. Error::Checksum => write!(f, "checksum error"),
  173. Error::Unrecognized => write!(f, "unrecognized packet"),
  174. Error::Fragmented => write!(f, "fragmented packet"),
  175. Error::Malformed => write!(f, "malformed packet"),
  176. Error::Dropped => write!(f, "dropped by socket"),
  177. }
  178. }
  179. }