lib.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #![cfg_attr(feature = "use_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 owns the sockets and routes packets to and from them.
  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. #[cfg(any(test, feature = "use_std"))]
  70. #[macro_use]
  71. extern crate std;
  72. #[cfg(feature = "use_std")]
  73. extern crate libc;
  74. #[cfg(feature = "use_alloc")]
  75. extern crate alloc;
  76. #[cfg(feature = "use_log")]
  77. #[macro_use(trace, log)]
  78. extern crate log;
  79. macro_rules! net_trace {
  80. ($($arg:expr),*) => {
  81. #[cfg(feature = "use_log")]
  82. trace!($($arg),*);
  83. #[cfg(not(feature = "use_log"))]
  84. $( let _ = $arg );*; // suppress unused variable warnings
  85. }
  86. }
  87. use core::fmt;
  88. mod managed;
  89. pub mod phy;
  90. pub mod wire;
  91. pub mod iface;
  92. pub mod socket;
  93. pub use managed::Managed;
  94. /// The error type for the networking stack.
  95. #[derive(Debug, PartialEq, Eq, Clone, Copy)]
  96. pub enum Error {
  97. /// An incoming packet could not be parsed, or an outgoing packet could not be emitted
  98. /// because a field was out of bounds for the underlying buffer.
  99. Truncated,
  100. /// An incoming packet could not be recognized and was dropped.
  101. /// E.g. a packet with an unknown EtherType.
  102. Unrecognized,
  103. /// An incoming packet was recognized but contained invalid data.
  104. /// E.g. a packet with IPv4 EtherType but containing a value other than 4
  105. /// in the version field.
  106. Malformed,
  107. /// An incoming packet had an incorrect checksum and was dropped.
  108. Checksum,
  109. /// An incoming packet has been fragmented and was dropped.
  110. Fragmented,
  111. /// An outgoing packet could not be sent because a protocol address could not be mapped
  112. /// to hardware address. E.g. an IPv4 packet did not have an Ethernet address
  113. /// corresponding to its IPv4 destination address.
  114. Unaddressable,
  115. /// A buffer for incoming packets is empty, or a buffer for outgoing packets is full.
  116. Exhausted,
  117. /// An incoming packet does not match the socket endpoint.
  118. Rejected,
  119. #[doc(hidden)]
  120. __Nonexhaustive
  121. }
  122. impl fmt::Display for Error {
  123. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  124. match self {
  125. &Error::Truncated => write!(f, "truncated packet"),
  126. &Error::Unrecognized => write!(f, "unrecognized packet"),
  127. &Error::Malformed => write!(f, "malformed packet"),
  128. &Error::Checksum => write!(f, "checksum error"),
  129. &Error::Fragmented => write!(f, "fragmented packet"),
  130. &Error::Unaddressable => write!(f, "unaddressable destination"),
  131. &Error::Exhausted => write!(f, "buffer space exhausted"),
  132. &Error::Rejected => write!(f, "rejected by socket"),
  133. &Error::__Nonexhaustive => unreachable!()
  134. }
  135. }
  136. }