mod.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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::{InternetAddress as Address, InternetProtocolType as ProtocolType};
  14. mod udp;
  15. pub use self::udp::Buffer as UdpBuffer;
  16. pub use self::udp::BufferElem as UdpBufferElem;
  17. pub use self::udp::UdpSocket as UdpSocket;
  18. /// A packet representation.
  19. ///
  20. /// This interface abstracts the various types of packets layered under the IP protocol,
  21. /// and serves as an accessory to [trait Socket](trait.Socket.html).
  22. pub trait PacketRepr {
  23. /// Return the length required to serialize this high-level representation.
  24. fn len(&self) -> usize;
  25. /// Emit this high-level representation into a sequence of octets.
  26. fn emit(&self, src_addr: &Address, dst_addr: &Address, payload: &mut [u8]);
  27. }
  28. /// A network socket.
  29. ///
  30. /// This interface abstracts the various types of sockets based on the IP protocol.
  31. /// It is necessarily implemented as a trait, and not as an enumeration, to allow using different
  32. /// buffering strategies in sockets assigned to the same interface.
  33. ///
  34. /// The `collect` and `dispatch` functions are fundamentally asymmetric and thus differ in
  35. /// their use of the [trait PacketRepr](trait.PacketRepr.html). When `collect` is called,
  36. /// the packet length is already known and no allocation is required; on the other hand,
  37. /// `collect` would have to downcast a `&PacketRepr` to e.g. an `&UdpRepr` through `Any`,
  38. /// which is rather inelegant. Conversely, when `dispatch` is called, the packet length is
  39. /// not yet known and the packet storage has to be allocated; but the `&PacketRepr` is sufficient
  40. /// since the lower layers treat the packet as an opaque octet sequence.
  41. pub trait Socket {
  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. fn collect(&mut self, src_addr: &Address, dst_addr: &Address,
  50. protocol: ProtocolType, payload: &[u8])
  51. -> Result<(), Error>;
  52. /// Prepare a packet to be transmitted to a network interface.
  53. ///
  54. /// This function checks if the internal buffer is empty, and if it is not, calls `f` with
  55. /// the representation of the packet to be transmitted, otherwise, `Err(Error::Exhausted)`
  56. /// is returned.
  57. ///
  58. /// This function is used internally by the networking stack.
  59. fn dispatch(&mut self, f: &mut FnMut(&Address, &Address,
  60. ProtocolType, &PacketRepr) -> Result<(), Error>)
  61. -> Result<(), Error>;
  62. }