lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 ir-representable".
  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.56 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.
  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(
  96. feature = "proto-ipv4",
  97. feature = "proto-ipv6",
  98. feature = "proto-sixlowpan"
  99. )))]
  100. compile_error!("You must enable at least one of the following features: proto-ipv4, proto-ipv6, proto-sixlowpan");
  101. #[cfg(all(
  102. feature = "socket",
  103. not(any(
  104. feature = "socket-raw",
  105. feature = "socket-udp",
  106. feature = "socket-tcp",
  107. feature = "socket-icmp",
  108. feature = "socket-dhcp",
  109. feature = "socket-dns",
  110. ))
  111. ))]
  112. 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, socket-dhcp, socket-dns");
  113. #[cfg(all(
  114. feature = "socket",
  115. not(any(
  116. feature = "medium-ethernet",
  117. feature = "medium-ip",
  118. feature = "medium-ieee802154",
  119. ))
  120. ))]
  121. compile_error!("If you enable the socket feature, you must enable at least one of the following features: medium-ip, medium-ethernet, medium-ieee802154");
  122. #[cfg(all(feature = "defmt", feature = "log"))]
  123. compile_error!("You must enable at most one of the following features: defmt, log");
  124. use core::fmt;
  125. #[macro_use]
  126. mod macros;
  127. mod parsers;
  128. mod rand;
  129. #[cfg(any(
  130. feature = "medium-ethernet",
  131. feature = "medium-ip",
  132. feature = "medium-ieee802154"
  133. ))]
  134. pub mod iface;
  135. pub mod phy;
  136. #[cfg(feature = "socket")]
  137. pub mod socket;
  138. pub mod storage;
  139. pub mod time;
  140. pub mod wire;
  141. /// The error type for the networking stack.
  142. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  143. #[non_exhaustive]
  144. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  145. pub enum Error {
  146. /// An operation cannot proceed because a buffer is empty or full.
  147. Exhausted,
  148. /// An operation is not permitted in the current state.
  149. Illegal,
  150. /// An endpoint or address of a remote host could not be translated to a lower level address.
  151. /// E.g. there was no an Ethernet address corresponding to an IPv4 address in the ARP cache,
  152. /// or a TCP connection attempt was made to an unspecified endpoint.
  153. Unaddressable,
  154. /// The operation is finished.
  155. /// E.g. when reading from a TCP socket, there's no more data to read because the remote
  156. /// has closed the connection.
  157. Finished,
  158. /// An incoming packet could not be parsed because some of its fields were out of bounds
  159. /// of the received data.
  160. Truncated,
  161. /// An incoming packet had an incorrect checksum and was dropped.
  162. Checksum,
  163. /// An incoming packet could not be recognized and was dropped.
  164. /// E.g. an Ethernet packet with an unknown EtherType.
  165. Unrecognized,
  166. /// An incoming IP packet has been split into several IP fragments and was dropped,
  167. /// since IP reassembly is not supported.
  168. Fragmented,
  169. /// An incoming packet was recognized but was self-contradictory.
  170. /// E.g. a TCP packet with both SYN and FIN flags set.
  171. Malformed,
  172. /// An incoming packet was recognized but contradicted internal state.
  173. /// E.g. a TCP packet addressed to a socket that doesn't exist.
  174. Dropped,
  175. /// An incoming fragment arrived too late.
  176. ReassemblyTimeout,
  177. /// The packet assembler is not initialized, thus it cannot know what the final size of the
  178. /// packet would be.
  179. PacketAssemblerNotInit,
  180. /// The buffer of the assembler is to small and thus the final packet wont fit into it.
  181. PacketAssemblerBufferTooSmall,
  182. /// The packet assembler did not receive all the fragments for assembling the final packet.
  183. PacketAssemblerIncomplete,
  184. /// There are too many holes in the packet assembler (should be fixed in the future?).
  185. PacketAssemblerTooManyHoles,
  186. /// There was an overlap when adding data to the packet assembler.
  187. PacketAssemblerOverlap,
  188. /// The packet assembler set has no place for assembling a new stream of fragments.
  189. PacketAssemblerSetFull,
  190. /// The key was not found in the packet assembler set.
  191. PacketAssemblerSetKeyNotFound,
  192. /// An incoming packet was recognized but some parts are not supported by smoltcp.
  193. /// E.g. some bit configuration in a packet header is not supported, but is defined in an RFC.
  194. NotSupported,
  195. }
  196. #[cfg(feature = "std")]
  197. impl std::error::Error for Error {}
  198. /// The result type for the networking stack.
  199. pub type Result<T> = core::result::Result<T, Error>;
  200. impl fmt::Display for Error {
  201. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  202. match *self {
  203. Error::Exhausted => write!(f, "buffer space exhausted"),
  204. Error::Illegal => write!(f, "illegal operation"),
  205. Error::Unaddressable => write!(f, "unaddressable destination"),
  206. Error::Finished => write!(f, "operation finished"),
  207. Error::Truncated => write!(f, "truncated packet"),
  208. Error::Checksum => write!(f, "checksum error"),
  209. Error::Unrecognized => write!(f, "unrecognized packet"),
  210. Error::Fragmented => write!(f, "fragmented packet"),
  211. Error::Malformed => write!(f, "malformed packet"),
  212. Error::Dropped => write!(f, "dropped by socket"),
  213. Error::ReassemblyTimeout => write!(f, "incoming fragment arrived too late"),
  214. Error::PacketAssemblerNotInit => write!(f, "packet assembler was not initialized"),
  215. Error::PacketAssemblerBufferTooSmall => {
  216. write!(f, "packet assembler buffer too small for final packet")
  217. }
  218. Error::PacketAssemblerIncomplete => write!(f, "packet assembler incomplete"),
  219. Error::PacketAssemblerTooManyHoles => write!(
  220. f,
  221. "packet assembler has too many holes (internal smoltcp error)"
  222. ),
  223. Error::PacketAssemblerOverlap => {
  224. write!(f, "overlap when adding data to packet assembler")
  225. }
  226. Error::PacketAssemblerSetFull => write!(f, "packet assembler set is full"),
  227. Error::PacketAssemblerSetKeyNotFound => {
  228. write!(f, "packet assembler set does not find key")
  229. }
  230. Error::NotSupported => write!(f, "not supported by smoltcp"),
  231. }
  232. }
  233. }
  234. impl From<wire::Error> for Error {
  235. fn from(_: wire::Error) -> Self {
  236. Error::Malformed
  237. }
  238. }