lib.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #![cfg_attr(feature = "alloc", feature(alloc))]
  2. #![no_std]
  3. //! The _smoltcp_ library is built in a layered structure, with the layers corresponding
  4. //! to the levels of API abstraction. Only the highest layers would be used by a typical
  5. //! application; however, the goal of _smoltcp_ is not just to provide a simple interface
  6. //! for writing applications but also to be a toolbox of networking primitives, so
  7. //! every layer is fully exposed and documented.
  8. //!
  9. //! When discussing networking stacks and layering, often the [OSI model][osi] is invoked.
  10. //! _smoltcp_ makes no effort to conform to the OSI model as it is not applicable to TCP/IP.
  11. //! [osi]: https://en.wikipedia.org/wiki/OSI_model
  12. //!
  13. //! # The socket layer
  14. //! The socket layer APIs are provided in the module [socket](socket/index.html); currently,
  15. //! TCP and UDP sockets are provided. The socket API provides the usual primitives, but
  16. //! necessarily differs in many from the [Berkeley socket API][berk], as the latter was not
  17. //! designed to be used without heap allocation.
  18. //! [berk]: https://en.wikipedia.org/wiki/Berkeley_sockets
  19. //!
  20. //! The socket layer provides the buffering, packet construction and validation, and (for
  21. //! stateful sockets) the state machines, but it is interface-agnostic. An application must
  22. //! use sockets together with a network interface.
  23. //!
  24. //! # The interface layer
  25. //! The interface layer APIs are provided in the module [iface](iface/index.html); currently,
  26. //! Ethernet interface is provided.
  27. //!
  28. //! The interface layer handles the control messages, physical addressing and neighbor discovery.
  29. //! It routes packets to and from sockets.
  30. //!
  31. //! # The physical layer
  32. //! The physical layer APIs are provided in the module [phy](phy/index.html); currently,
  33. //! raw socket and TAP interface are provided. In addition, two _middleware_ interfaces
  34. //! are provided: the _tracer device_, which prints a human-readable representation of packets,
  35. //! and the _fault injector device_, which randomly introduces errors into the transmitted
  36. //! and received packet sequences.
  37. //!
  38. //! The physical layer handles interaction with a platform-specific network device.
  39. //!
  40. //! # The wire layers
  41. //! Unlike the higher layers, the wire layer APIs will not be used by a typical application.
  42. //! They however are the bedrock of _smoltcp_, and everything else is built on top of them.
  43. //!
  44. //! The wire layer APIs are designed by the principle "make illegal states irrepresentable".
  45. //! If a wire layer object can be constructed, then it can also be parsed from or emitted to
  46. //! a lower level.
  47. //!
  48. //! The wire layer APIs also provide _tcpdump_-like pretty printing.
  49. //!
  50. //! ## The representation layer
  51. //! The representation layer APIs are provided in the module [wire](wire/index.html); currently,
  52. //! Ethernet, ARP, generic IP, IPv4, ICMPv4, TCP and UDP packet representations are provided.
  53. //!
  54. //! The representation layer exists to reduce the state space of raw packets. Raw packets
  55. //! may be nonsensical in a multitude of ways: invalid checksums, impossible combinations of flags,
  56. //! pointers to fields out of bounds, meaningless options... Representations shed all that,
  57. //! as well as any features not supported by _smoltcp_.
  58. //!
  59. //! ## The packet layer
  60. //! The packet layer APIs are also provided in the module [wire](wire/index.html); currently,
  61. //! Ethernet, ARP, IPv4, ICMPv4, TCP and UDP packet representations are provided.
  62. //!
  63. //! The packet layer exists to provide a more structured way to work with packets than
  64. //! treating them as sequences of octets. It makes no judgement as to content of the packets,
  65. //! except where necessary to provide safe access to fields, and strives to implement every
  66. //! feature ever defined, to ensure that, when the representation layer is unable to make sense
  67. //! of a packet, it is still logged correctly and in full.
  68. extern crate byteorder;
  69. extern crate managed;
  70. #[cfg(any(test, feature = "std"))]
  71. #[macro_use]
  72. extern crate std;
  73. #[cfg(any(feature = "phy-raw_socket", feature = "phy-tap_interface"))]
  74. extern crate libc;
  75. #[cfg(feature = "alloc")]
  76. extern crate alloc;
  77. #[cfg(any(test, feature = "log"))]
  78. #[macro_use(log, log_enabled, trace, debug)]
  79. extern crate log;
  80. use core::fmt;
  81. #[macro_use]
  82. mod macros;
  83. mod parsers;
  84. pub mod storage;
  85. pub mod phy;
  86. pub mod wire;
  87. pub mod iface;
  88. pub mod socket;
  89. /// The error type for the networking stack.
  90. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  91. pub enum Error {
  92. /// An operation cannot proceed because a buffer is empty or full.
  93. Exhausted,
  94. /// An operation is not permitted in the current state.
  95. Illegal,
  96. /// An endpoint or address of a remote host could not be translated to a lower level address.
  97. /// E.g. there was no an Ethernet address corresponding to an IPv4 address in the ARP cache,
  98. /// or a TCP connection attempt was made to an unspecified endpoint.
  99. Unaddressable,
  100. /// An incoming packet could not be parsed because some of its fields were out of bounds
  101. /// of the received data.
  102. Truncated,
  103. /// An incoming packet had an incorrect checksum and was dropped.
  104. Checksum,
  105. /// An incoming packet could not be recognized and was dropped.
  106. /// E.g. an Ethernet packet with an unknown EtherType.
  107. Unrecognized,
  108. /// An incoming IP packet has been split into several IP fragments and was dropped,
  109. /// since IP reassembly is not supported.
  110. Fragmented,
  111. /// An incoming packet was recognized but was self-contradictory.
  112. /// E.g. a TCP packet with both SYN and FIN flags set.
  113. Malformed,
  114. /// An incoming packet was recognized but contradicted internal state.
  115. /// E.g. a TCP packet addressed to a socket that doesn't exist.
  116. Dropped,
  117. #[doc(hidden)]
  118. __Nonexhaustive
  119. }
  120. /// The result type for the networking stack.
  121. pub type Result<T> = core::result::Result<T, Error>;
  122. impl fmt::Display for Error {
  123. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  124. match self {
  125. &Error::Exhausted => write!(f, "buffer space exhausted"),
  126. &Error::Illegal => write!(f, "illegal operation"),
  127. &Error::Unaddressable => write!(f, "unaddressable destination"),
  128. &Error::Truncated => write!(f, "truncated packet"),
  129. &Error::Checksum => write!(f, "checksum error"),
  130. &Error::Unrecognized => write!(f, "unrecognized packet"),
  131. &Error::Fragmented => write!(f, "fragmented packet"),
  132. &Error::Malformed => write!(f, "malformed packet"),
  133. &Error::Dropped => write!(f, "dropped by socket"),
  134. &Error::__Nonexhaustive => unreachable!()
  135. }
  136. }
  137. }