interface.go 8.9 KB

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