mirror of
https://github.com/soypat/lneto.git
synced 2026-08-21 23:19:03 +00:00
clean up tcp package; add Abort method
This commit is contained in:
+25
-22
@@ -153,17 +153,18 @@ func (tcb *ControlBlock) Open(iss Value, wnd Size) (err error) {
|
||||
tcb.logerr("tcb:open", slog.String("err", err.Error()))
|
||||
return err
|
||||
}
|
||||
tcb._state = StateListen
|
||||
tcb.prepareToHandshake(iss, wnd)
|
||||
|
||||
tcb.prepareToHandshake(iss, wnd, StateListen)
|
||||
tcb.trace("tcb:open-server")
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepareToHandshake initializes the TCB send/receive spaces with initial send sequence number and local window.
|
||||
func (tcb *ControlBlock) prepareToHandshake(iss Value, wnd Size) {
|
||||
func (tcb *ControlBlock) prepareToHandshake(iss Value, wnd Size, newState State) {
|
||||
tcb.reset()
|
||||
tcb.resetRcv(wnd, 0)
|
||||
tcb.resetSnd(iss, 1)
|
||||
tcb.pending = [2]Flags{}
|
||||
tcb._state = newState
|
||||
}
|
||||
|
||||
// HasPending returns true if there is a pending control segment to send. Calls to Send will advance the pending queue.
|
||||
@@ -256,7 +257,7 @@ func (tcb *ControlBlock) Recv(seg Segment) (err error) {
|
||||
case StateCloseWait:
|
||||
case StateLastAck:
|
||||
if seg.Flags.HasAny(FlagACK) {
|
||||
tcb.close()
|
||||
tcb.Abort()
|
||||
}
|
||||
case StateClosing:
|
||||
// Thanks to @knieriem for finding and reporting this bug.
|
||||
@@ -309,11 +310,10 @@ func (tcb *ControlBlock) Send(seg Segment) error {
|
||||
switch tcb._state {
|
||||
case StateClosed:
|
||||
if seg.Flags == FlagSYN {
|
||||
tcb._state = StateSynSent
|
||||
tcb.prepareToHandshake(seg.SEQ, seg.WND)
|
||||
tcb.prepareToHandshake(seg.SEQ, seg.WND, StateSynSent)
|
||||
tcb.trace("tcb:open-client")
|
||||
}
|
||||
case StateSynRcvd:
|
||||
case StateSynRcvd, StateEstablished:
|
||||
if hasFIN {
|
||||
tcb._state = StateFinWait1 // RFC 9293: 3.10.4 CLOSE call.
|
||||
}
|
||||
@@ -321,10 +321,6 @@ func (tcb *ControlBlock) Send(seg Segment) error {
|
||||
if hasACK {
|
||||
tcb._state = StateTimeWait
|
||||
}
|
||||
case StateEstablished:
|
||||
if hasFIN {
|
||||
tcb._state = StateFinWait1
|
||||
}
|
||||
case StateCloseWait:
|
||||
if hasFIN {
|
||||
tcb._state = StateLastAck
|
||||
@@ -494,7 +490,7 @@ func (tcb *ControlBlock) handleRST(seq Value) error {
|
||||
tcb.resetSnd(tcb.snd.ISS+tcb.rstJump(), tcb.snd.WND)
|
||||
tcb.resetRcv(tcb.rcv.WND, 3_14159_2653^tcb.rcv.IRS)
|
||||
} else {
|
||||
tcb.close() // Enter closed state and return.
|
||||
tcb.Abort() // Enter closed state and return.
|
||||
return net.ErrClosed
|
||||
}
|
||||
return errDropSegment
|
||||
@@ -504,13 +500,17 @@ func (tcb *ControlBlock) rstJump() Value {
|
||||
return 100
|
||||
}
|
||||
|
||||
// close sets ControlBlock state to closed and resets all sequence numbers and pending flag.
|
||||
func (tcb *ControlBlock) close() {
|
||||
tcb._state = StateClosed
|
||||
tcb.pending = [2]Flags{}
|
||||
tcb.resetRcv(0, 0)
|
||||
tcb.resetSnd(0, 0)
|
||||
tcb.debug("tcb:close")
|
||||
// Abort sets ControlBlock state to Closed and resets all sequence numbers and pending flag.
|
||||
// No more data can be sent nor received after the connection is aborted until opened again.
|
||||
func (tcb *ControlBlock) Abort() {
|
||||
tcb.reset()
|
||||
tcb.debug("tcb:abort")
|
||||
}
|
||||
|
||||
func (tcb *ControlBlock) reset() {
|
||||
*tcb = ControlBlock{
|
||||
logger: tcb.logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Close implements a passive/active closing of a connection. It does not immediately
|
||||
@@ -521,15 +521,18 @@ func (tcb *ControlBlock) Close() (err error) {
|
||||
// See RFC 9293: 3.10.4 CLOSE call.
|
||||
switch tcb._state {
|
||||
case StateClosed:
|
||||
err = errConnNotexist
|
||||
err = errConnNotExist
|
||||
case StateCloseWait:
|
||||
tcb._state = StateLastAck
|
||||
tcb.pending = [2]Flags{FlagFIN, FlagACK}
|
||||
case StateListen, StateSynSent:
|
||||
tcb.close()
|
||||
// In Listen State there is no established connection.
|
||||
// In SynSent the remote endpoint is not yet synchronized and upon receiving an RST will abort connection.
|
||||
tcb.Abort()
|
||||
case StateSynRcvd, StateEstablished:
|
||||
// We suppose user has no more pending data to send, so we flag FIN to be sent.
|
||||
// Users of this API should call Close only when they have no more data to send.
|
||||
// When FIN is sent SCB will transition to FinWait1.
|
||||
tcb.pending[0] = (tcb.pending[0] & FlagACK) | FlagFIN
|
||||
case StateFinWait2, StateTimeWait:
|
||||
err = errConnectionClosing
|
||||
|
||||
+15
-3
@@ -19,7 +19,7 @@ var (
|
||||
errBufferTooSmall = errors.New("buffer too small")
|
||||
errNeedClosedTCBToOpen = errors.New("need closed TCB to call open")
|
||||
errInvalidState = errors.New("invalid state")
|
||||
errConnNotexist = errors.New("connection does not exist")
|
||||
errConnNotExist = errors.New("connection does not exist")
|
||||
errConnectionClosing = errors.New("connection closing")
|
||||
errExpectedSYN = errors.New("seqs:expected SYN")
|
||||
errBadSegack = errors.New("seqs:bad segack")
|
||||
@@ -286,7 +286,7 @@ func (s State) IsPreestablished() bool {
|
||||
// IsClosing returns true if the connection is in a closing state but not yet terminated (relieved of remote connection state).
|
||||
// Returns false for Closed pseudo state.
|
||||
func (s State) IsClosing() bool {
|
||||
return !(s <= StateEstablished)
|
||||
return s == StateFinWait1 || s == StateFinWait2 || s == StateClosing || s == StateLastAck || s == StateCloseWait
|
||||
}
|
||||
|
||||
// IsClosed returns true if the connection closed and can possibly relieved of
|
||||
@@ -297,7 +297,19 @@ func (s State) IsClosed() bool {
|
||||
|
||||
// IsSynchronized returns true if the connection has gone through the Established state.
|
||||
func (s State) IsSynchronized() bool {
|
||||
return s >= StateEstablished
|
||||
return s >= StateEstablished && !s.IsClosed()
|
||||
}
|
||||
|
||||
// txOpen returns true if the TCP state machine allows data to be sent by user.
|
||||
func (s State) txOpen() bool {
|
||||
// In CloseWait state the remote endpoint has closed
|
||||
// our receive hald of the connection but we can still transmit indefinitely.
|
||||
return s == StateEstablished || s == StateCloseWait
|
||||
}
|
||||
|
||||
// rxOpen returns true if the TCP state machine allows data to be received from remote endpoint.
|
||||
func (s State) rxOpen() bool {
|
||||
return s == StateEstablished || s == StateFinWait1 || s == StateFinWait2
|
||||
}
|
||||
|
||||
// IsDataOpen returns true if the connection allows sending and receiving of data.
|
||||
|
||||
+1
-1
@@ -128,7 +128,7 @@ func (tfrm Frame) Payload() []byte {
|
||||
|
||||
// Segment returns the [Segment] representation of the TCP header and data length.
|
||||
func (tfrm Frame) Segment(payloadSize int) Segment {
|
||||
if payloadSize > math.MaxUint32 {
|
||||
if payloadSize > math.MaxInt32 {
|
||||
panic("TCP overflow payload size")
|
||||
}
|
||||
return Segment{
|
||||
|
||||
Reference in New Issue
Block a user