interface.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. HasMoreData() bool
  72. }
  73. // A ReceiveStream is a unidirectional Receive Stream.
  74. type ReceiveStream interface {
  75. // see Stream.StreamID
  76. StreamID() StreamID
  77. // see Stream.Read
  78. io.Reader
  79. // see Stream.CancelRead
  80. CancelRead(ErrorCode) error
  81. // see Stream.SetReadDealine
  82. SetReadDeadline(t time.Time) error
  83. HasMoreData() bool
  84. }
  85. // A SendStream is a unidirectional Send Stream.
  86. type SendStream interface {
  87. // see Stream.StreamID
  88. StreamID() StreamID
  89. // see Stream.Write
  90. io.Writer
  91. // see Stream.Close
  92. io.Closer
  93. // see Stream.CancelWrite
  94. CancelWrite(ErrorCode) error
  95. // see Stream.Context
  96. Context() context.Context
  97. // see Stream.SetWriteDeadline
  98. SetWriteDeadline(t time.Time) error
  99. }
  100. // StreamError is returned by Read and Write when the peer cancels the stream.
  101. type StreamError interface {
  102. error
  103. Canceled() bool
  104. ErrorCode() ErrorCode
  105. }
  106. // A Session is a QUIC connection between two peers.
  107. type Session interface {
  108. // AcceptStream returns the next stream opened by the peer, blocking until one is available.
  109. AcceptStream() (Stream, error)
  110. // AcceptUniStream returns the next unidirectional stream opened by the peer, blocking until one is available.
  111. AcceptUniStream() (ReceiveStream, error)
  112. // OpenStream opens a new bidirectional QUIC stream.
  113. // It returns a special error when the peer's concurrent stream limit is reached.
  114. // There is no signaling to the peer about new streams:
  115. // The peer can only accept the stream after data has been sent on the stream.
  116. // TODO(#1152): Enable testing for the special error
  117. OpenStream() (Stream, error)
  118. // OpenStreamSync opens a new bidirectional QUIC stream.
  119. // It blocks until the peer's concurrent stream limit allows a new stream to be opened.
  120. OpenStreamSync() (Stream, error)
  121. // OpenUniStream opens a new outgoing unidirectional QUIC stream.
  122. // It returns a special error when the peer's concurrent stream limit is reached.
  123. // TODO(#1152): Enable testing for the special error
  124. OpenUniStream() (SendStream, error)
  125. // OpenUniStreamSync opens a new outgoing unidirectional QUIC stream.
  126. // It blocks until the peer's concurrent stream limit allows a new stream to be opened.
  127. OpenUniStreamSync() (SendStream, error)
  128. // LocalAddr returns the local address.
  129. LocalAddr() net.Addr
  130. // RemoteAddr returns the address of the peer.
  131. RemoteAddr() net.Addr
  132. // Close the connection.
  133. io.Closer
  134. // Close the connection with an error.
  135. // The error must not be nil.
  136. CloseWithError(ErrorCode, error) error
  137. // The context is cancelled when the session is closed.
  138. // Warning: This API should not be considered stable and might change soon.
  139. Context() context.Context
  140. // ConnectionState returns basic details about the QUIC connection.
  141. // Warning: This API should not be considered stable and might change soon.
  142. ConnectionState() ConnectionState
  143. }
  144. // Config contains all configuration data needed for a QUIC server or client.
  145. type Config struct {
  146. // The QUIC versions that can be negotiated.
  147. // If not set, it uses all versions available.
  148. // Warning: This API should not be considered stable and will change soon.
  149. Versions []VersionNumber
  150. // The length of the connection ID in bytes.
  151. // It can be 0, or any value between 4 and 18.
  152. // If not set, the interpretation depends on where the Config is used:
  153. // If used for dialing an address, a 0 byte connection ID will be used.
  154. // If used for a server, or dialing on a packet conn, a 4 byte connection ID will be used.
  155. // When dialing on a packet conn, the ConnectionIDLength value must be the same for every Dial call.
  156. ConnectionIDLength int
  157. // HandshakeTimeout is the maximum duration that the cryptographic handshake may take.
  158. // If the timeout is exceeded, the connection is closed.
  159. // If this value is zero, the timeout is set to 10 seconds.
  160. HandshakeTimeout time.Duration
  161. // IdleTimeout is the maximum duration that may pass without any incoming network activity.
  162. // This value only applies after the handshake has completed.
  163. // If the timeout is exceeded, the connection is closed.
  164. // If this value is zero, the timeout is set to 30 seconds.
  165. IdleTimeout time.Duration
  166. // AcceptCookie determines if a Cookie is accepted.
  167. // It is called with cookie = nil if the client didn't send an Cookie.
  168. // If not set, it verifies that the address matches, and that the Cookie was issued within the last 24 hours.
  169. // This option is only valid for the server.
  170. AcceptCookie func(clientAddr net.Addr, cookie *Cookie) bool
  171. // MaxReceiveStreamFlowControlWindow is the maximum stream-level flow control window for receiving data.
  172. // If this value is zero, it will default to 1 MB for the server and 6 MB for the client.
  173. MaxReceiveStreamFlowControlWindow uint64
  174. // MaxReceiveConnectionFlowControlWindow is the connection-level flow control window for receiving data.
  175. // If this value is zero, it will default to 1.5 MB for the server and 15 MB for the client.
  176. MaxReceiveConnectionFlowControlWindow uint64
  177. // MaxIncomingStreams is the maximum number of concurrent bidirectional streams that a peer is allowed to open.
  178. // If not set, it will default to 100.
  179. // If set to a negative value, it doesn't allow any bidirectional streams.
  180. MaxIncomingStreams int
  181. // MaxIncomingUniStreams is the maximum number of concurrent unidirectional streams that a peer is allowed to open.
  182. // If not set, it will default to 100.
  183. // If set to a negative value, it doesn't allow any unidirectional streams.
  184. MaxIncomingUniStreams int
  185. // KeepAlive defines whether this peer will periodically send PING frames to keep the connection alive.
  186. KeepAlive bool
  187. }
  188. // A Listener for incoming QUIC connections
  189. type Listener interface {
  190. // Close the server, sending CONNECTION_CLOSE frames to each peer.
  191. Close() error
  192. // Addr returns the local network addr that the server is listening on.
  193. Addr() net.Addr
  194. // Accept returns new sessions. It should be called in a loop.
  195. Accept() (Session, error)
  196. }