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