Documentation ¶
Overview ¶
package seqs implements TCP control flow.
Transmission Control Block ¶
The Transmission Control Block (TCB) is the core data structure of TCP. It stores core state of the TCP connection such as the send and receive sequence number spaces, the current state of the connection, and the pending control segment flags.
Values and Sizes ¶
All arithmetic dealing with sequence numbers must be performed modulo 2**32 which brings with it subtleties to computer modulo arithmetic.
Index ¶
- func InRange(v, a, b Value) bool
- func InWindow(v, first Value, size Size) bool
- func LessThan(v, w Value) bool
- func LessThanEq(v, w Value) bool
- func StringExchange(seg Segment, A, B State, invertDir bool) string
- type ControlBlock
- func (tcb *ControlBlock) Close() (err error)
- func (tcb *ControlBlock) HasPending() bool
- func (tcb *ControlBlock) ISS() Value
- func (tcb *ControlBlock) IncomingIsKeepalive(incomingSegment Segment) bool
- func (tcb *ControlBlock) MakeKeepalive() Segment
- func (tcb *ControlBlock) MaxInFlightData() Size
- func (tcb *ControlBlock) Open(iss Value, wnd Size, state State) (err error)
- func (tcb *ControlBlock) PendingSegment(payloadLen int) (_ Segment, ok bool)
- func (tcb *ControlBlock) Recv(seg Segment) (err error)
- func (tcb *ControlBlock) RecvNext() Value
- func (tcb *ControlBlock) RecvWindow() Size
- func (tcb *ControlBlock) Send(seg Segment) error
- func (tcb *ControlBlock) SetLogger(log *slog.Logger)
- func (tcb *ControlBlock) SetRecvWindow(wnd Size)
- func (tcb *ControlBlock) State() State
- type Flags
- type RejectError
- type Segment
- type Size
- type State
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InWindow ¶
InWindow checks if v is in the window that starts at 'first' and spans 'size' sequence numbers (modulo 32).
func LessThanEq ¶
LessThanEq returns true if v==w or v is before (modulo 32) i.e., v < w.
func StringExchange ¶
StringExchange returns a string representation of a segment exchange over a network in RFC9293 styled visualization. invertDir inverts the arrow directions. i.e:
SynSent --> <SEQ=300><ACK=91>[SYN,ACK] --> SynRcvd
Types ¶
type ControlBlock ¶
type ControlBlock struct {
// contains filtered or unexported fields
}
ControlBlock is a partial Transmission Control Block (TCB) implementation as per RFC 9293 in section 3.3.1. In contrast with the description in RFC9293, this implementation is limited to receiving only sequential segments. This means buffer management is left up entirely to the user of the ControlBlock. Use ControlBlock as the building block that solves Sequence Number calculation and validation in a full TCP implementation.
A ControlBlock's internal state is modified by the available "System Calls" as defined in RFC9293, such as Close, Listen/Open, Send, and Receive. Sent and received data is represented with the Segment struct type.
func (*ControlBlock) Close ¶
func (tcb *ControlBlock) Close() (err error)
Close implements a passive/active closing of a connection. It does not immediately delete the TCB but initiates the process so that pending outgoing segments initiate the closing process. After a call to Close users should not send more data. Close returns an error if the connection is already closed or closing.
func (*ControlBlock) HasPending ¶
func (tcb *ControlBlock) HasPending() bool
HasPending returns true if there is a pending control segment to send. Calls to Send will advance the pending queue.
func (*ControlBlock) ISS ¶
func (tcb *ControlBlock) ISS() Value
ISS returns the initial sequence number of the connection that was defined on a call to Open by user.
func (*ControlBlock) IncomingIsKeepalive ¶
func (tcb *ControlBlock) IncomingIsKeepalive(incomingSegment Segment) bool
IncomingIsKeepalive checks if an incoming segment is a keepalive segment. Segments which are keepalives should not be passed into Recv or Send methods.
func (*ControlBlock) MakeKeepalive ¶
func (tcb *ControlBlock) MakeKeepalive() Segment
MakeKeepalive creates a TCP keepalive segment. This segment should not be passed into Recv or Send methods.
func (*ControlBlock) MaxInFlightData ¶
func (tcb *ControlBlock) MaxInFlightData() Size
MaxInFlightData returns the maximum size of a segment that can be sent by taking into account the send window size and the unacked data. Returns 0 before StateSynRcvd.
func (*ControlBlock) Open ¶
func (tcb *ControlBlock) Open(iss Value, wnd Size, state State) (err error)
Open implements a passive/active opening of a connection. state must be StateListen or StateSynSent.
func (*ControlBlock) PendingSegment ¶
func (tcb *ControlBlock) PendingSegment(payloadLen int) (_ Segment, ok bool)
PendingSegment calculates a suitable next segment to send from a payload length. It does not modify the ControlBlock state or pending segment queue.
func (*ControlBlock) Recv ¶
func (tcb *ControlBlock) Recv(seg Segment) (err error)
Recv processes a segment that is being received from the network. It updates the TCB if there is no error. The ControlBlock can only receive segments that are the next expected sequence number which means the caller must handle the out-of-order case and buffering that comes with it.
func (*ControlBlock) RecvNext ¶
func (tcb *ControlBlock) RecvNext() Value
RecvNext returns the next sequence number expected to be received from remote. This implementation will reject segments that are not the next expected sequence. RecvNext returns 0 before StateSynRcvd.
func (*ControlBlock) RecvWindow ¶
func (tcb *ControlBlock) RecvWindow() Size
RecvWindow returns the receive window size. If connection is closed will return 0.
func (*ControlBlock) Send ¶
func (tcb *ControlBlock) Send(seg Segment) error
Send processes a segment that is being sent to the network. It updates the TCB if there is no error.
func (*ControlBlock) SetLogger ¶
func (tcb *ControlBlock) SetLogger(log *slog.Logger)
SetLogger sets the logger to be used by the ControlBlock.
func (*ControlBlock) SetRecvWindow ¶
func (tcb *ControlBlock) SetRecvWindow(wnd Size)
SetWindow sets the local receive window size. This represents the maximum amount of data that is permitted to be in flight.
func (*ControlBlock) State ¶
func (tcb *ControlBlock) State() State
State returns the current state of the connection.
type Flags ¶
type Flags uint16
Flags is a TCP flags masked implementation i.e: SYN, FIN, ACK.
const ( FlagFIN Flags = 1 << iota // FlagFIN - No more data from sender. FlagSYN // FlagSYN - Synchronize sequence numbers. FlagRST // FlagRST - Reset the connection. FlagPSH // FlagPSH - Push function. FlagACK // FlagACK - Acknowledgment field significant. FlagURG // FlagURG - Urgent pointer field significant. FlagECE // FlagECE - ECN-Echo has a nonce-sum in the SYN/ACK. FlagCWR // FlagCWR - Congestion Window Reduced. FlagNS // FlagNS - Nonce Sum flag (see RFC 3540). )
func (Flags) AppendFormat ¶
AppendFormat appends a human readable flag string to b returning the extended buffer.
type RejectError ¶
type RejectError struct {
// contains filtered or unexported fields
}
RejectError represents an error that arises during admission of a segment into the Transmission Control Block logic in which the packet cannot be processed by the TCB.
func (*RejectError) Error ¶
func (e *RejectError) Error() string
type Segment ¶
type Segment struct { SEQ Value // sequence number of first octet of segment. If SYN is set it is the initial sequence number (ISN) and the first data octet is ISN+1. ACK Value // acknowledgment number. If ACK is set it is sequence number of first octet the sender of the segment is expecting to receive next. DATALEN Size // The number of octets occupied by the data (payload) not counting SYN and FIN. WND Size // segment window Flags Flags // TCP flags. }
Segment represents an incoming/outgoing TCP segment in the sequence space.
type State ¶
type State uint8
State enumerates states a TCP connection progresses through during its lifetime.
const ( // CLOSED - represents no connection state at all. Is not a valid state of the TCP state machine but rather a pseudo-state pre-initialization. StateClosed State = iota // LISTEN - represents waiting for a connection request from any remote TCP and port. StateListen // SYN-RECEIVED - represents waiting for a confirming connection request acknowledgment // after having both received and sent a connection request. StateSynRcvd // SYN-SENT - represents waiting for a matching connection request after having sent a connection request. StateSynSent // ESTABLISHED - represents an open connection, data received can be delivered // to the user. The normal state for the data transfer phase of the connection. StateEstablished // FIN-WAIT-1 - represents waiting for a connection termination request // from the remote TCP, or an acknowledgment of the connection // termination request previously sent. StateFinWait1 // FIN-WAIT-2 - represents waiting for a connection termination request // from the remote TCP. StateFinWait2 // CLOSING - represents waiting for a connection termination request // acknowledgment from the remote TCP. StateClosing // TIME-WAIT - represents waiting for enough time to pass to be sure the remote // TCP received the acknowledgment of its connection termination request. StateTimeWait // CLOSE-WAIT - represents waiting for a connection termination request // from the local user. StateCloseWait // LAST-ACK - represents waiting for an acknowledgment of the // connection termination request previously sent to the remote TCP // (which includes an acknowledgment of its connection termination request). StateLastAck )
func (State) IsClosed ¶
IsClosed returns true if the connection closed and can possibly relieved of all state related to the remote connection. It returns true if Closed or in TimeWait.
func (State) IsClosing ¶
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 (State) IsPreestablished ¶
IsPreestablished returns true if the connection is in a state preceding the established state. Returns false for Closed pseudo state.
func (State) IsSynchronized ¶
IsSynchronized returns true if the connection has gone through the Established state.
type Value ¶
type Value uint32
Value represents the value of a sequence number.
func DefaultNewISS ¶
DefaultNewISS returns a new initial send sequence number. It's implementation is suggested by RFC9293.
func (*Value) UpdateForward ¶
UpdateForward updates v such that it becomes v + s.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
package eth implements Ethernet, ARP, IP, TCP among other datagram and protocol frame processing and manipulation tools.
|
package eth implements Ethernet, ARP, IP, TCP among other datagram and protocol frame processing and manipulation tools. |
dns
Code generated by "stringer" command for the types within this file; DO NOT EDIT.
|
Code generated by "stringer" command for the types within this file; DO NOT EDIT. |
ntp
package ntp implements the NTP protocol as described in RFC 5905.
|
package ntp implements the NTP protocol as described in RFC 5905. |