interface.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package quic
  2. import (
  3. "context"
  4. "io"
  5. "net"
  6. "time"
  7. "github.com/lucas-clemente/quic-go/internal/handshake"
  8. "github.com/lucas-clemente/quic-go/internal/protocol"
  9. )
  10. // The StreamID is the ID of a QUIC stream.
  11. type StreamID = protocol.StreamID
  12. // A VersionNumber is a QUIC version number.
  13. type VersionNumber = protocol.VersionNumber
  14. // A Cookie can be used to verify the ownership of the client address.
  15. type Cookie struct {
  16. RemoteAddr string
  17. SentTime time.Time
  18. }
  19. // ConnectionState records basic details about the QUIC connection.
  20. type ConnectionState = handshake.ConnectionState
  21. // An ErrorCode is an application-defined error code.
  22. type ErrorCode = protocol.ApplicationErrorCode
  23. // Stream is the interface implemented by QUIC streams
  24. type Stream interface {
  25. // StreamID returns the stream ID.
  26. StreamID() StreamID
  27. // Read reads data from the stream.
  28. // Read can be made to time out and return a net.Error with Timeout() == true
  29. // after a fixed time limit; see SetDeadline and SetReadDeadline.
  30. // If the stream was canceled by the peer, the error implements the StreamError
  31. // interface, and Canceled() == true.
  32. io.Reader
  33. // Write writes data to the stream.
  34. // Write can be made to time out and return a net.Error with Timeout() == true
  35. // after a fixed time limit; see SetDeadline and SetWriteDeadline.
  36. // If the stream was canceled by the peer, the error implements the StreamError
  37. // interface, and Canceled() == true.
  38. io.Writer
  39. // Close closes the write-direction of the stream.
  40. // Future calls to Write are not permitted after calling Close.
  41. // It must not be called concurrently with Write.
  42. // It must not be called after calling CancelWrite.
  43. io.Closer
  44. // CancelWrite aborts sending on this stream.
  45. // It must not be called after Close.
  46. // Data already written, but not yet delivered to the peer is not guaranteed to be delivered reliably.
  47. // Write will unblock immediately, and future calls to Write will fail.
  48. CancelWrite(ErrorCode) error
  49. // CancelRead aborts receiving on this stream.
  50. // It will ask the peer to stop transmitting stream data.
  51. // Read will unblock immediately, and future Read calls will fail.
  52. CancelRead(ErrorCode) error
  53. // The context is canceled as soon as the write-side of the stream is closed.
  54. // This happens when Close() is called, or when the stream is reset (either locally or remotely).
  55. // Warning: This API should not be considered stable and might change soon.
  56. Context() context.Context
  57. // SetReadDeadline sets the deadline for future Read calls and
  58. // any currently-blocked Read call.
  59. // A zero value for t means Read will not time out.
  60. SetReadDeadline(t time.Time) error
  61. // SetWriteDeadline sets the deadline for future Write calls
  62. // and any currently-blocked Write call.
  63. // Even if write times out, it may return n > 0, indicating that
  64. // some of the data was successfully written.
  65. // A zero value for t means Write will not time out.
  66. SetWriteDeadline(t time.Time) error
  67. // SetDeadline sets the read and write deadlines associated
  68. // with the connection. It is equivalent to calling both
  69. // SetReadDeadline and SetWriteDeadline.
  70. SetDeadline(t time.Time) error
  71. }
  72. // A ReceiveStream is a unidirectional Receive Stream.
  73. type ReceiveStream interface {
  74. // see Stream.StreamID
  75. StreamID() StreamID
  76. // see Stream.Read
  77. io.Reader
  78. // see Stream.CancelRead
  79. CancelRead(ErrorCode) error
  80. // see Stream.SetReadDealine
  81. SetReadDeadline(t time.Time) error
  82. }
  83. // A SendStream is a unidirectional Send Stream.
  84. type SendStream interface {
  85. // see Stream.StreamID
  86. StreamID() StreamID
  87. // see Stream.Write
  88. io.Writer
  89. // see Stream.Close
  90. io.Closer
  91. // see Stream.CancelWrite
  92. CancelWrite(ErrorCode) error
  93. // see Stream.Context
  94. Context() context.Context
  95. // see Stream.SetWriteDeadline
  96. SetWriteDeadline(t time.Time) error
  97. }
  98. // StreamError is returned by Read and Write when the peer cancels the stream.
  99. type StreamError interface {
  100. error
  101. Canceled() bool
  102. ErrorCode() ErrorCode
  103. }
  104. // A Session is a QUIC connection between two peers.
  105. type Session interface {
  106. // AcceptStream returns the next stream opened by the peer, blocking until one is available.
  107. AcceptStream() (Stream, error)
  108. // AcceptUniStream returns the next unidirectional stream opened by the peer, blocking until one is available.
  109. AcceptUniStream() (ReceiveStream, error)
  110. // OpenStream opens a new bidirectional QUIC stream.
  111. // It returns a special error when the peer's concurrent stream limit is reached.
  112. // There is no signaling to the peer about new streams:
  113. // The peer can only accept the stream after data has been sent on the stream.
  114. // TODO(#1152): Enable testing for the special error
  115. OpenStream() (Stream, error)
  116. // OpenStreamSync opens a new bidirectional QUIC stream.
  117. // It blocks until the peer's concurrent stream limit allows a new stream to be opened.
  118. OpenStreamSync() (Stream, error)
  119. // OpenUniStream opens a new outgoing unidirectional QUIC stream.
  120. // It returns a special error when the peer's concurrent stream limit is reached.
  121. // TODO(#1152): Enable testing for the special error
  122. OpenUniStream() (SendStream, error)
  123. // OpenUniStreamSync opens a new outgoing unidirectional QUIC stream.
  124. // It blocks until the peer's concurrent stream limit allows a new stream to be opened.
  125. OpenUniStreamSync() (SendStream, error)
  126. // LocalAddr returns the local address.
  127. LocalAddr() net.Addr
  128. // RemoteAddr returns the address of the peer.
  129. RemoteAddr() net.Addr
  130. // Close the connection.
  131. io.Closer
  132. // Close the connection with an error.
  133. // The error must not be nil.
  134. CloseWithError(ErrorCode, error) error
  135. // The context is cancelled when the session is closed.
  136. // Warning: This API should not be considered stable and might change soon.
  137. Context() context.Context
  138. // ConnectionState returns basic details about the QUIC connection.
  139. // Warning: This API should not be considered stable and might change soon.
  140. ConnectionState() ConnectionState
  141. }
  142. // Config contains all configuration data needed for a QUIC server or client.
  143. type Config struct {
  144. // The QUIC versions that can be negotiated.
  145. // If not set, it uses all versions available.
  146. // Warning: This API should not be considered stable and will change soon.
  147. Versions []VersionNumber
  148. // The length of the connection ID in bytes.
  149. // It can be 0, or any value between 4 and 18.
  150. // If not set, the interpretation depends on where the Config is used:
  151. // If used for dialing an address, a 0 byte connection ID will be used.
  152. // If used for a server, or dialing on a packet conn, a 4 byte connection ID will be used.
  153. // When dialing on a packet conn, the ConnectionIDLength value must be the same for every Dial call.
  154. ConnectionIDLength int
  155. // HandshakeTimeout is the maximum duration that the cryptographic handshake may take.
  156. // If the timeout is exceeded, the connection is closed.
  157. // If this value is zero, the timeout is set to 10 seconds.
  158. HandshakeTimeout time.Duration
  159. // IdleTimeout is the maximum duration that may pass without any incoming network activity.
  160. // This value only applies after the handshake has completed.
  161. // If the timeout is exceeded, the connection is closed.
  162. // If this value is zero, the timeout is set to 30 seconds.
  163. IdleTimeout time.Duration
  164. // AcceptCookie determines if a Cookie is accepted.
  165. // It is called with cookie = nil if the client didn't send an Cookie.
  166. // If not set, it verifies that the address matches, and that the Cookie was issued within the last 24 hours.
  167. // This option is only valid for the server.
  168. AcceptCookie func(clientAddr net.Addr, cookie *Cookie) bool
  169. // MaxReceiveStreamFlowControlWindow is the maximum stream-level flow control window for receiving data.
  170. // If this value is zero, it will default to 1 MB for the server and 6 MB for the client.
  171. MaxReceiveStreamFlowControlWindow uint64
  172. // MaxReceiveConnectionFlowControlWindow is the connection-level flow control window for receiving data.
  173. // If this value is zero, it will default to 1.5 MB for the server and 15 MB for the client.
  174. MaxReceiveConnectionFlowControlWindow uint64
  175. // MaxIncomingStreams is the maximum number of concurrent bidirectional streams that a peer is allowed to open.
  176. // If not set, it will default to 100.
  177. // If set to a negative value, it doesn't allow any bidirectional streams.
  178. MaxIncomingStreams int
  179. // MaxIncomingUniStreams is the maximum number of concurrent unidirectional streams that a peer is allowed to open.
  180. // If not set, it will default to 100.
  181. // If set to a negative value, it doesn't allow any unidirectional streams.
  182. MaxIncomingUniStreams int
  183. // KeepAlive defines whether this peer will periodically send PING frames to keep the connection alive.
  184. KeepAlive bool
  185. }
  186. // A Listener for incoming QUIC connections
  187. type Listener interface {
  188. // Close the server, sending CONNECTION_CLOSE frames to each peer.
  189. Close() error
  190. // Addr returns the local network addr that the server is listening on.
  191. Addr() net.Addr
  192. // Accept returns new sessions. It should be called in a loop.
  193. Accept() (Session, error)
  194. }