github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/tcp/header.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package tcp
     4  
     5  // Header represent a TCP packet, Due performance impact, don't use this type and its methods.
     6  // https://datatracker.ietf.org/doc/html/rfc793#section-3.1
     7  type header struct {
     8  	// Indicate source port number of TCP packet as stream identifier
     9  	SourcePort uint16
    10  	// Indicate destination port of TCP packet as protocol identifier
    11  	DestinationPort uint16
    12  
    13  	// Represents the TCP segment’s window index, mark the ordering of a group of messages.
    14  	// When handshaking, this contains the Initial Sequence Number (ISN).
    15  	SequenceNumber uint32
    16  
    17  	// Represents the window’s index of the next byte the sender expects to receive.
    18  	// After the handshake, the ACK field must always be populated.
    19  	AckNumber uint32
    20  
    21  	// Indicate the length of the header or offset of the data.
    22  	// It encode||decode as 4-bit **words** means 40 bytes encode as 10 words.
    23  	// The minimum size header is 5 words and the maximum is 15 words thus giving
    24  	// the minimum size of 20 bytes and maximum of 60 bytes,
    25  	// allowing for up to 40 bytes of options in the header.
    26  	DataOffset uint8
    27  
    28  	Flags flags
    29  
    30  	// The Window Size field is used to advertise the window size.
    31  	// In other words, this is the number of bytes the receiver is willing to accept.
    32  	// Since it is a 16-bit field, the maximum window size is 65,535 bytes.
    33  	// Window specifies the number of window size units[c] that the sender of this
    34  	// segment is currently willing to receive.
    35  	Window uint16
    36  
    37  	// Used to verify the integrity of the TCP segment. 
    38  	// Generated by the protocol sender as a mathematical technique
    39  	// to help the receiver detect messages that are corrupted or tampered with.
    40  	// If TCP carry by IP, the algorithm is the same as for the Internet Protocol(IP),
    41  	// but the input segment also contains the TCP data and also a pseudo-header from the IP datagram.
    42  	Checksum uint16
    43  
    44  	// The Urgent Pointer is used when the U-flag is set. The pointer indicates the position of the urgent data in the stream.
    45  	// It is often set to zero and ignored, but in conjunction with one of the control flags,
    46  	// it can be used as a data offset to mark a subset of a message as requiring priority processing.
    47  	UrgentPointer uint16
    48  
    49  	// After the header, several options (0 to 40 bytes) can be provided. An example of these options is:
    50  	// - The Maximum Segment Size (MSS), where the sender informs the other side of the maximum size of the segments.
    51  	// - Special acknowledgment
    52  	// - Window scaling algorithms
    53  	Options []option
    54  
    55  	Padding []byte
    56  
    57  	// After the possible options, the actual data follows. The data, however, is not required.
    58  	// For example, the handshake is accomplished with only TCP header
    59  	Payload []byte
    60  }
    61  
    62  // Each flag in TCP is an individual bit representing On or Off—to manage data flow in specific situations.
    63  // Reserved flags in TCP headers always has a value of zero.
    64  type flags struct {
    65  	// The first 3 bits (rsvd) are not used.
    66  	Reserved1, Reserved2, Reserved3 bool
    67  
    68  	// ECN-nonce - concealment protection
    69  	NS bool
    70  
    71  	// Congestion window reduced (CWR) flag is set by the sending host
    72  	// to indicate that it received a TCP segment with the ECE flag set and had
    73  	// responded in congestion control mechanism.
    74  	// Used for informing that the sender reduced its sending rate.
    75  	CWR bool
    76  
    77  	// ECN-Echo has a dual role, depending on the value of the SYN flag.
    78  	// It indicates:
    79  	// - If the SYN flag is set (1), that the TCP peer is ECN capable.
    80  	// - If the SYN flag is clear (0), that a packet with Congestion Experienced flag
    81  	//   set (ECN=11) in the IP header was received during normal transmission.
    82  	//   This serves as an indication of network congestion (or impending congestion) to the TCP sender.
    83  	ECE bool
    84  
    85  	// Urgent Pointer (U) indicates that the segment contains prioritized data.
    86  	URG bool
    87  
    88  	// ACK Indicates that the Acknowledgment field is significant.
    89  	// All packets after the initial SYN packet sent by the client should have this flag set.
    90  	ACK bool
    91  
    92  	// Push function is used to indicate that the receiver should “push” the data to the service logic as soon as possible.
    93  	PSH bool
    94  
    95  	// RST resets the TCP connection.
    96  	RST bool
    97  
    98  	// SYN (S) is used to synchronize sequence numbers in the initial handshake.
    99  	// So Only the first packet sent from each end should have this flag set.
   100  	// Some other flags and fields change meaning based on this flag,
   101  	// and some are only valid when it is set, and others when it is clear.
   102  	SYN bool
   103  
   104  	// FIN indicates that the sender has finished sending data.
   105  	FIN bool
   106  }
   107  
   108  // https://datatracker.ietf.org/doc/html/rfc4413#section-4.3.1
   109  type option struct {
   110  	Kind   optionKind
   111  	Length uint8 // including the header fields
   112  	Data   []byte
   113  }