interface.go 9.6 KB

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