interface.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package quic
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "io"
  6. "net"
  7. "time"
  8. "github.com/Psiphon-Labs/quic-go/internal/protocol"
  9. "github.com/Psiphon-Labs/quic-go/quictrace"
  10. )
  11. // The StreamID is the ID of a QUIC stream.
  12. type StreamID = protocol.StreamID
  13. // A VersionNumber is a QUIC version number.
  14. type VersionNumber = protocol.VersionNumber
  15. // A Token can be used to verify the ownership of the client address.
  16. type Token struct {
  17. // IsRetryToken encodes how the client received the token. There are two ways:
  18. // * In a Retry packet sent when trying to establish a new connection.
  19. // * In a NEW_TOKEN frame on a previous connection.
  20. IsRetryToken bool
  21. RemoteAddr string
  22. SentTime time.Time
  23. }
  24. // A ClientToken is a token received by the client.
  25. // It can be used to skip address validation on future connection attempts.
  26. type ClientToken struct {
  27. data []byte
  28. }
  29. type TokenStore interface {
  30. // Pop searches for a ClientToken associated with the given key.
  31. // Since tokens are not supposed to be reused, it must remove the token from the cache.
  32. // It returns nil when no token is found.
  33. Pop(key string) (token *ClientToken)
  34. // Put adds a token to the cache with the given key. It might get called
  35. // multiple times in a connection.
  36. Put(key string, token *ClientToken)
  37. }
  38. // An ErrorCode is an application-defined error code.
  39. // Valid values range between 0 and MAX_UINT62.
  40. type ErrorCode = protocol.ApplicationErrorCode
  41. // Stream is the interface implemented by QUIC streams
  42. type Stream interface {
  43. // StreamID returns the stream ID.
  44. StreamID() StreamID
  45. // Read reads data from the stream.
  46. // Read can be made to time out and return a net.Error with Timeout() == true
  47. // after a fixed time limit; see SetDeadline and SetReadDeadline.
  48. // If the stream was canceled by the peer, the error implements the StreamError
  49. // interface, and Canceled() == true.
  50. // If the session was closed due to a timeout, the error satisfies
  51. // the net.Error interface, and Timeout() will be true.
  52. io.Reader
  53. // Write writes data to the stream.
  54. // Write can be made to time out and return a net.Error with Timeout() == true
  55. // after a fixed time limit; see SetDeadline and SetWriteDeadline.
  56. // If the stream was canceled by the peer, the error implements the StreamError
  57. // interface, and Canceled() == true.
  58. // If the session was closed due to a timeout, the error satisfies
  59. // the net.Error interface, and Timeout() will be true.
  60. io.Writer
  61. // Close closes the write-direction of the stream.
  62. // Future calls to Write are not permitted after calling Close.
  63. // It must not be called concurrently with Write.
  64. // It must not be called after calling CancelWrite.
  65. io.Closer
  66. // CancelWrite aborts sending on this stream.
  67. // Data already written, but not yet delivered to the peer is not guaranteed to be delivered reliably.
  68. // Write will unblock immediately, and future calls to Write will fail.
  69. // When called multiple times or after closing the stream it is a no-op.
  70. CancelWrite(ErrorCode)
  71. // CancelRead aborts receiving on this stream.
  72. // It will ask the peer to stop transmitting stream data.
  73. // Read will unblock immediately, and future Read calls will fail.
  74. // When called multiple times or after reading the io.EOF it is a no-op.
  75. CancelRead(ErrorCode)
  76. // The context is canceled as soon as the write-side of the stream is closed.
  77. // This happens when Close() or CancelWrite() is called, or when the peer
  78. // cancels the read-side of their stream.
  79. // Warning: This API should not be considered stable and might change soon.
  80. Context() context.Context
  81. // SetReadDeadline sets the deadline for future Read calls and
  82. // any currently-blocked Read call.
  83. // A zero value for t means Read will not time out.
  84. SetReadDeadline(t time.Time) error
  85. // SetWriteDeadline sets the deadline for future Write calls
  86. // and any currently-blocked Write call.
  87. // Even if write times out, it may return n > 0, indicating that
  88. // some of the data was successfully written.
  89. // A zero value for t means Write will not time out.
  90. SetWriteDeadline(t time.Time) error
  91. // SetDeadline sets the read and write deadlines associated
  92. // with the connection. It is equivalent to calling both
  93. // SetReadDeadline and SetWriteDeadline.
  94. SetDeadline(t time.Time) error
  95. }
  96. // A ReceiveStream is a unidirectional Receive Stream.
  97. type ReceiveStream interface {
  98. // see Stream.StreamID
  99. StreamID() StreamID
  100. // see Stream.Read
  101. io.Reader
  102. // see Stream.CancelRead
  103. CancelRead(ErrorCode)
  104. // see Stream.SetReadDealine
  105. SetReadDeadline(t time.Time) error
  106. }
  107. // A SendStream is a unidirectional Send Stream.
  108. type SendStream interface {
  109. // see Stream.StreamID
  110. StreamID() StreamID
  111. // see Stream.Write
  112. io.Writer
  113. // see Stream.Close
  114. io.Closer
  115. // see Stream.CancelWrite
  116. CancelWrite(ErrorCode)
  117. // see Stream.Context
  118. Context() context.Context
  119. // see Stream.SetWriteDeadline
  120. SetWriteDeadline(t time.Time) error
  121. }
  122. // StreamError is returned by Read and Write when the peer cancels the stream.
  123. type StreamError interface {
  124. error
  125. Canceled() bool
  126. ErrorCode() ErrorCode
  127. }
  128. // A Session is a QUIC connection between two peers.
  129. type Session interface {
  130. // AcceptStream returns the next stream opened by the peer, blocking until one is available.
  131. // If the session was closed due to a timeout, the error satisfies
  132. // the net.Error interface, and Timeout() will be true.
  133. AcceptStream(context.Context) (Stream, error)
  134. // AcceptUniStream returns the next unidirectional stream opened by the peer, blocking until one is available.
  135. // If the session was closed due to a timeout, the error satisfies
  136. // the net.Error interface, and Timeout() will be true.
  137. AcceptUniStream(context.Context) (ReceiveStream, error)
  138. // OpenStream opens a new bidirectional QUIC stream.
  139. // There is no signaling to the peer about new streams:
  140. // The peer can only accept the stream after data has been sent on the stream.
  141. // If the error is non-nil, it satisfies the net.Error interface.
  142. // When reaching the peer's stream limit, err.Temporary() will be true.
  143. // If the session was closed due to a timeout, Timeout() will be true.
  144. OpenStream() (Stream, error)
  145. // OpenStreamSync opens a new bidirectional QUIC stream.
  146. // It blocks until a new stream can be opened.
  147. // If the error is non-nil, it satisfies the net.Error interface.
  148. // If the session was closed due to a timeout, Timeout() will be true.
  149. OpenStreamSync(context.Context) (Stream, error)
  150. // OpenUniStream opens a new outgoing unidirectional QUIC stream.
  151. // If the error is non-nil, it satisfies the net.Error interface.
  152. // When reaching the peer's stream limit, Temporary() will be true.
  153. // If the session was closed due to a timeout, Timeout() will be true.
  154. OpenUniStream() (SendStream, error)
  155. // OpenUniStreamSync opens a new outgoing unidirectional QUIC stream.
  156. // It blocks until a new stream can be opened.
  157. // If the error is non-nil, it satisfies the net.Error interface.
  158. // If the session was closed due to a timeout, Timeout() will be true.
  159. OpenUniStreamSync(context.Context) (SendStream, error)
  160. // LocalAddr returns the local address.
  161. LocalAddr() net.Addr
  162. // RemoteAddr returns the address of the peer.
  163. RemoteAddr() net.Addr
  164. // Close the connection.
  165. io.Closer
  166. // Close the connection with an error.
  167. // The error string will be sent to the peer.
  168. CloseWithError(ErrorCode, string) error
  169. // The context is cancelled when the session is closed.
  170. // Warning: This API should not be considered stable and might change soon.
  171. Context() context.Context
  172. // ConnectionState returns basic details about the QUIC connection.
  173. // Warning: This API should not be considered stable and might change soon.
  174. ConnectionState() tls.ConnectionState
  175. }
  176. // An EarlySession is a session that is handshaking.
  177. // Data sent during the handshake is encrypted using the forward secure keys.
  178. // When using client certificates, the client's identity is only verified
  179. // after completion of the handshake.
  180. type EarlySession interface {
  181. Session
  182. // Blocks until the handshake completes (or fails).
  183. // Data sent before completion of the handshake is encrypted with 1-RTT keys.
  184. // Note that the client's identity hasn't been verified yet.
  185. HandshakeComplete() context.Context
  186. }
  187. // Config contains all configuration data needed for a QUIC server or client.
  188. type Config struct {
  189. // The QUIC versions that can be negotiated.
  190. // If not set, it uses all versions available.
  191. // Warning: This API should not be considered stable and will change soon.
  192. Versions []VersionNumber
  193. // The length of the connection ID in bytes.
  194. // It can be 0, or any value between 4 and 18.
  195. // If not set, the interpretation depends on where the Config is used:
  196. // If used for dialing an address, a 0 byte connection ID will be used.
  197. // If used for a server, or dialing on a packet conn, a 4 byte connection ID will be used.
  198. // When dialing on a packet conn, the ConnectionIDLength value must be the same for every Dial call.
  199. ConnectionIDLength int
  200. // HandshakeTimeout is the maximum duration that the cryptographic handshake may take.
  201. // If the timeout is exceeded, the connection is closed.
  202. // If this value is zero, the timeout is set to 10 seconds.
  203. HandshakeTimeout time.Duration
  204. // IdleTimeout is the maximum duration that may pass without any incoming network activity.
  205. // This value only applies after the handshake has completed.
  206. // If the timeout is exceeded, the connection is closed.
  207. // If this value is zero, the timeout is set to 30 seconds.
  208. IdleTimeout time.Duration
  209. // AcceptToken determines if a Token is accepted.
  210. // It is called with token = nil if the client didn't send a token.
  211. // If not set, a default verification function is used:
  212. // * it verifies that the address matches, and
  213. // * if the token is a retry token, that it was issued within the last 5 seconds
  214. // * else, that it was issued within the last 24 hours.
  215. // This option is only valid for the server.
  216. AcceptToken func(clientAddr net.Addr, token *Token) bool
  217. // The TokenStore stores tokens received from the server.
  218. // Tokens are used to skip address validation on future connection attempts.
  219. // The key used to store tokens is the ServerName from the tls.Config, if set
  220. // otherwise the token is associated with the server's IP address.
  221. TokenStore TokenStore
  222. // MaxReceiveStreamFlowControlWindow is the maximum stream-level flow control window for receiving data.
  223. // If this value is zero, it will default to 1 MB for the server and 6 MB for the client.
  224. MaxReceiveStreamFlowControlWindow uint64
  225. // MaxReceiveConnectionFlowControlWindow is the connection-level flow control window for receiving data.
  226. // If this value is zero, it will default to 1.5 MB for the server and 15 MB for the client.
  227. MaxReceiveConnectionFlowControlWindow uint64
  228. // MaxIncomingStreams is the maximum number of concurrent bidirectional streams that a peer is allowed to open.
  229. // If not set, it will default to 100.
  230. // If set to a negative value, it doesn't allow any bidirectional streams.
  231. MaxIncomingStreams int
  232. // MaxIncomingUniStreams is the maximum number of concurrent unidirectional streams that a peer is allowed to open.
  233. // If not set, it will default to 100.
  234. // If set to a negative value, it doesn't allow any unidirectional streams.
  235. MaxIncomingUniStreams int
  236. // The StatelessResetKey is used to generate stateless reset tokens.
  237. // If no key is configured, sending of stateless resets is disabled.
  238. StatelessResetKey []byte
  239. // KeepAlive defines whether this peer will periodically send a packet to keep the connection alive.
  240. KeepAlive bool
  241. // QUIC Event Tracer.
  242. // Warning: Experimental. This API should not be considered stable and will change soon.
  243. QuicTracer quictrace.Tracer
  244. }
  245. // A Listener for incoming QUIC connections
  246. type Listener interface {
  247. // Close the server. All active sessions will be closed.
  248. Close() error
  249. // Addr returns the local network addr that the server is listening on.
  250. Addr() net.Addr
  251. // Accept returns new sessions. It should be called in a loop.
  252. Accept(context.Context) (Session, error)
  253. }
  254. // An EarlyListener listens for incoming QUIC connections,
  255. // and returns them before the handshake completes.
  256. type EarlyListener interface {
  257. // Close the server. All active sessions will be closed.
  258. Close() error
  259. // Addr returns the local network addr that the server is listening on.
  260. Addr() net.Addr
  261. // Accept returns new early sessions. It should be called in a loop.
  262. Accept(context.Context) (EarlySession, error)
  263. }