mod.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //! Communication between endpoints.
  2. //!
  3. //! The `socket` module deals with *network endpoints* and *buffering*.
  4. //! It provides interfaces for accessing buffers of data, and protocol state machines
  5. //! for filling and emptying these buffers.
  6. //!
  7. //! The programming interface implemented here differs greatly from the common Berkeley socket
  8. //! interface. Specifically, in the Berkeley interface the buffering is implicit:
  9. //! the operating system decides on the good size for a buffer and manages it.
  10. //! The interface implemented by this module uses explicit buffering: you decide on the good
  11. //! size for a buffer, allocate it, and let the networking stack use it.
  12. use Error;
  13. use wire::IpRepr;
  14. mod udp;
  15. mod tcp;
  16. pub use self::udp::PacketBuffer as UdpPacketBuffer;
  17. pub use self::udp::SocketBuffer as UdpSocketBuffer;
  18. pub use self::udp::UdpSocket;
  19. pub use self::tcp::SocketBuffer as TcpSocketBuffer;
  20. pub use self::tcp::State as TcpState;
  21. pub use self::tcp::TcpSocket;
  22. /// A network socket.
  23. ///
  24. /// This enumeration abstracts the various types of sockets based on the IP protocol.
  25. /// To downcast a `Socket` value down to a concrete socket, use
  26. /// the [AsSocket](trait.AsSocket.html) trait, and call e.g. `socket.as_socket::<UdpSocket<_>>()`.
  27. ///
  28. /// The `process` and `dispatch` functions are fundamentally asymmetric and thus differ in
  29. /// their use of the [trait PacketRepr](trait.PacketRepr.html). When `process` is called,
  30. /// the packet length is already known and no allocation is required; on the other hand,
  31. /// `process` would have to downcast a `&PacketRepr` to e.g. an `&UdpRepr` through `Any`,
  32. /// which is rather inelegant. Conversely, when `dispatch` is called, the packet length is
  33. /// not yet known and the packet storage has to be allocated; but the `&PacketRepr` is sufficient
  34. /// since the lower layers treat the packet as an opaque octet sequence.
  35. pub enum Socket<'a, 'b: 'a> {
  36. Udp(UdpSocket<'a, 'b>),
  37. Tcp(TcpSocket<'a>),
  38. #[doc(hidden)]
  39. __Nonexhaustive
  40. }
  41. impl<'a, 'b> Socket<'a, 'b> {
  42. /// Process a packet received from a network interface.
  43. ///
  44. /// This function checks if the packet contained in the payload matches the socket endpoint,
  45. /// and if it does, copies it into the internal buffer, otherwise, `Err(Error::Rejected)`
  46. /// is returned.
  47. ///
  48. /// This function is used internally by the networking stack.
  49. pub fn process(&mut self, ip_repr: &IpRepr, payload: &[u8]) -> Result<(), Error> {
  50. match self {
  51. &mut Socket::Udp(ref mut socket) =>
  52. socket.process(ip_repr, payload),
  53. &mut Socket::Tcp(ref mut socket) =>
  54. socket.process(ip_repr, payload),
  55. &mut Socket::__Nonexhaustive => unreachable!()
  56. }
  57. }
  58. /// Prepare a packet to be transmitted to a network interface.
  59. ///
  60. /// This function checks if the internal buffer is empty, and if it is not, calls `f` with
  61. /// the representation of the packet to be transmitted, otherwise, `Err(Error::Exhausted)`
  62. /// is returned.
  63. ///
  64. /// This function is used internally by the networking stack.
  65. pub fn dispatch<F, R>(&mut self, emit: &mut F) -> Result<R, Error>
  66. where F: FnMut(&IpRepr, &IpPayload) -> Result<R, Error> {
  67. match self {
  68. &mut Socket::Udp(ref mut socket) =>
  69. socket.dispatch(emit),
  70. &mut Socket::Tcp(ref mut socket) =>
  71. socket.dispatch(emit),
  72. &mut Socket::__Nonexhaustive => unreachable!()
  73. }
  74. }
  75. }
  76. /// An IP-encapsulated packet representation.
  77. ///
  78. /// This trait abstracts the various types of packets layered under the IP protocol,
  79. /// and serves as an accessory to [trait Socket](trait.Socket.html).
  80. pub trait IpPayload {
  81. /// Return the length of the buffer required to serialize this high-level representation.
  82. fn buffer_len(&self) -> usize;
  83. /// Emit this high-level representation into a sequence of octets.
  84. fn emit(&self, ip_repr: &IpRepr, payload: &mut [u8]);
  85. }
  86. /// A conversion trait for network sockets.
  87. ///
  88. /// This trait is used to concisely downcast [Socket](trait.Socket.html) values to their
  89. /// concrete types.
  90. pub trait AsSocket<T> {
  91. fn as_socket(&mut self) -> &mut T;
  92. }
  93. impl<'a, 'b> AsSocket<UdpSocket<'a, 'b>> for Socket<'a, 'b> {
  94. fn as_socket(&mut self) -> &mut UdpSocket<'a, 'b> {
  95. match self {
  96. &mut Socket::Udp(ref mut socket) => socket,
  97. _ => panic!(".as_socket::<UdpSocket> called on wrong socket type")
  98. }
  99. }
  100. }
  101. impl<'a, 'b> AsSocket<TcpSocket<'a>> for Socket<'a, 'b> {
  102. fn as_socket(&mut self) -> &mut TcpSocket<'a> {
  103. match self {
  104. &mut Socket::Tcp(ref mut socket) => socket,
  105. _ => panic!(".as_socket::<TcpSocket> called on wrong socket type")
  106. }
  107. }
  108. }