TPDU Types¶
- icspacket.proto.cotp.structs.checksum(tpdu_data: Iterable[int], checksum_off: int)[source]¶
Compute the checksum for a TPDU according to Annex D.3 of X.224.
The algorithm iterates through each octet, maintaining two accumulators
C0andC1. After processing, the two checksum octetsXandYare computed based on the position of the checksum parameter and the total TPDU length.- Algorithm (Annex D.3):
Initialize
C0 = 0andC1 = 0.For each octet in the TPDU, add its value to
C0and then add the newC0toC1.Compute the checksum values: -
X = (-C1 + (L - n) * C0) mod 256-Y = (C1 - (L - n + 1) * C0) mod 256
- Parameters:
tpdu_data (Iterable[int]) – Sequence of TPDU octets.
checksum_off (int) – Offset where the two checksum octets reside.
- Returns:
Two-byte checksum value (X, Y).
- Return type:
bytes
- icspacket.proto.cotp.structs.verify_checksum(tpdu_data: bytes, checksum_off: int) bool[source]¶
Verify the checksum of a TPDU.
The function temporarily zeroes out the checksum field and recomputes it using
checksum(). If the recomputed checksum matches the original octets at the checksum offset, the TPDU is considered valid.- Parameters:
tpdu_data (bytes) – The TPDU octets containing a checksum field.
checksum_off (int) – Offset to the start of the checksum field.
- Returns:
Trueif checksum matches,Falseotherwise.- Return type:
bool
- class icspacket.proto.cotp.structs.TPDU_Code(*values)[source]¶
13.2.2.2 TPDU code
- ED = 1¶
- EA = 2¶
- RJ = 3¶
- AK = 6¶
- ER = 7¶
- DR = 8¶
- DC = 12¶
- CC = 13¶
- CR = 14¶
- DT = 15¶
- class icspacket.proto.cotp.structs.TPDU_Class(*values)[source]¶
Protocol classes
- CLASS0 = 0¶
Simple class (Class 0)
Class 0 provides only the functions needed for connection establishment with negotiation, data transfer with segmenting and protocol error reporting.
- CLASS1 = 1¶
Basic error recovery class (Class 1)
Class 1 provides transport connections with flow control based on the network service provided flow control, error recovery, expedited data transfer, disconnection, and also the ability to support consecutive transport connections on a network connection.
- CLASS2 = 2¶
Multiplexing class (Class 2)
Class 2 provides transport connections with or without individual flow control; no error detection or error recovery is provided.
- CLASS3 = 3¶
Error recovery and multiplexing class
Class 3 provides the functionality of class 2 (with use of explicit flow control) plus the ability to recover after a failure signalled by the Network Layer without involving the TS-user.
- CLASS4 = 4¶
Error detection and recovery class (Class 4)
Class 4 provides the functionality of class 3, plus the ability to detect and recover from lost, duplicated, or out of sequence TPDUs without involving the TS-user.
- class icspacket.proto.cotp.structs.TPDU_DisconnectReason(*values)[source]¶
Defines the reason for disconnecting the transport connection.
- NORMAL = 128¶
Normal disconnect initiated by session entity.
- REMOTE_CONGEST = 129¶
Remote transport entity congestion at connect request time.
- NEGO_FAILED = 130¶
Connection negotiation failed [i.e. proposed class(es) not supported].
- DUPLICATE_SOURCE = 131¶
Duplicate source reference detected for the same pair of NSAPs.
- MISMATCHED_REFERENCES = 132¶
Mismatched references.
- PROTOCOL_ERROR = 133¶
Protocol error.
- REF_OVERFLOW = 135¶
Reference overflow.
- CONN_REFUSED = 136¶
Connection request refused on this network connection.
- INVALID_LENGTH = 138¶
Header or parameter length invalid.
- UNSPECIFIED = 0¶
Reason not specified.
- TSAP_CONGESTION = 1¶
Congestion at TSAP.
- ENTITIY_NOT_ATTACHED = 2¶
Session entity not attached to TSAP.
- UNKNOWN_ADDRESS = 3¶
Address unknown.
- class icspacket.proto.cotp.structs.TPDU_Size(*values)[source]¶
Defines the proposed maximum TPDU size (in octets including the header) to be used over the requested transport connection
- SIZE_8192 = 13¶
- SIZE_4096 = 12¶
- SIZE_2048 = 11¶
- SIZE_1024 = 10¶
- SIZE_512 = 9¶
- SIZE_256 = 8¶
- SIZE_128 = 7¶
- class icspacket.proto.cotp.structs.TPDU_RejectCause(*values)[source]¶
Cause of rejection of a connection request
- UNSPECIFIED = 0¶
Cause not specified
- INVALID_PARAMETER_CODE = 1¶
Invalid parameter code
- INVALID_PDU_TYPE = 2¶
Invalid PDU type
- INVALID_PARAMETER_VALUE = 3¶
Invalid parameter value
- class icspacket.proto.cotp.structs.TPDU_AdditionalOptions(unused: bool = False, non_blocking: bool = False, use_request_ack: bool = False, use_selective_ack: bool = False, speed_up: bool = False, use_receipt_info: bool = False, use_checksum_16bit: bool = False, use_transport_speed_up: bool = True)[source]¶
Additional option selection (not used if class 0 is the preferred class)
- unused: bool = False¶
- non_blocking: bool = False¶
Use of non-blocking expedited data in class 4
- use_request_ack: bool = False¶
Use of request acknowledgement in class 1, 3, 4
- use_selective_ack: bool = False¶
Use of selective acknowledgement in class 4
- speed_up: bool = False¶
Use of network expedited in class 1
- use_receipt_info: bool = False¶
True: Use of receipt confirmation in class 1
False: Use of explicit AK variant in class 1
- use_checksum_16bit: bool = False¶
16-bit checksum defined in 6.17 shall be used in class 4
- use_transport_speed_up: bool = True¶
Use of transport expedited data transfer service
- class icspacket.proto.cotp.structs.TPDU_TransitDelay(calling_target_value: int = 0, calling_maximum_acceptable: int = 0, called_target_value: int = 0, called_maximum_acceptable: int = 0)[source]¶
Transit delay (not used if class 0 is the preferred class)
- calling_target_value: int = 0¶
target value, calling-called user direction;
- calling_maximum_acceptable: int = 0¶
maximum acceptable, calling-called user direction;
- called_target_value: int = 0¶
target value, called-calling user direction;
- called_maximum_acceptable: int = 0¶
maximum acceptable, called-calling user direction;
- class icspacket.proto.cotp.structs.TPDU_ResidualErrorRate(target_value: int = 0, minimum_acceptable: int = 0, tsdu_size_of_interest: int = 0)[source]¶
Residual error rate (not used if class 0 is the preferred class)
- target_value: int = 0¶
target value, power of 10;
- minimum_acceptable: int = 0¶
minimum acceptable, power of 10;
- tsdu_size_of_interest: int = 0¶
TSDU size of interest, expressed as a power of 2.
- class icspacket.proto.cotp.structs.Parameter_Code(*values)[source]¶
Defines different parameter types used accross TPDUs
- CALLED_T_SELECTOR = 193¶
- CALLING_T_SELECTOR = 194¶
- TPDU_SIZE = 192¶
- MAX_TPDU_SIZE = 240¶
- VERSION = 196¶
- PROTECTION = 197¶
- CHECKSUM = 195¶
- ADDITIONAL_OPTS = 198¶
- ALTERNATIVE_CLASSES = 199¶
- ACK_TIME = 133¶
- THROUGHPUT = 137¶
- ERROR_RATE = 134¶
- PRIORITY = 135¶
- TRANSIT_DELAY = 136¶
- REASSIGNMENT_TIME = 139¶
- INACTIVITY = 242¶
- ADDTITIONAL_INFO = 224¶
- SUBSEQUENCE_NUM = 138¶
- FLOW_CONTROL_INFO = 140¶
- ACK_PARAMS = 143¶
- class icspacket.proto.cotp.structs.Parameter(type_id: Parameter_Code | int = 0, value: Any | TPDU_Size | int | bytes | TPDU_AdditionalOptions | list[int] | TPDU_ResidualErrorRate | TPDU_TransitDelay = b'')[source]¶
13.2.3 Variable part
The variable part is used to define less frequently used parameters. If the variable part is present, it shall contain one or more parameters.
- type_id: Parameter_Code | int = 0¶
The parameter code
- value: Any | TPDU_Size | int | bytes | TPDU_AdditionalOptions | list[int] | TPDU_ResidualErrorRate | TPDU_TransitDelay = b''¶
The parameter length indication indicates the length, in octets, of the parameter value field.
The parameter value field contains the value of the parameter identified in the parameter code field.
- class icspacket.proto.cotp.structs.TPDU(li: int = 0, code: int = 0)[source]¶
Transport Protocol Data Units (TPDUs)
- li: int = 0¶
13.2.1 Length indicator field
The length indicated shall be the header length in octets including parameters, but excluding the length indicator field and user data, if any. The value 255 (1111 1111) is reserved for possible extensions.
- code: int = 0¶
13.2.2.2 TPDU code
This field contains the TPDU code. It is used to define the structure of the remaining header.
- property code_arg: int¶
Argument bits of the TPDU code
- property fixed_size: int¶
Returns the number of octets that make up the fixed (header) part of this TPDU, excluding the variable part and user data.
This value is normally defined per TPDU type in X.224 and stored as the class attribute
TPDU_FIXED_SIZE. If the subclass does not define it, a default of 1 octet is returned.The fixed size is used when computing the length indicator (LI), and also when locating parameter offsets, such as the position of the checksum parameter.
- property first_checksum_octet: int¶
Returns the zero-based index (offset) of the first checksum octet within the serialized TPDU, or -1 if no parameters are present.
For ease of computation, the checksum parameter MUST appear in the first position of the variable part when present. This means:
Offset starts at
fixed_size(end of fixed header)Add 1 byte for the Length Indicator (LI)
Add 2 bytes for the TLV header (parameter code and length)
- get_checksum() bytes[source]¶
Returns the current 2-byte checksum value from the variable part, if a checksum parameter is present. If no checksum parameter is found, returns an empty byte string.
The checksum parameter is identified by its type code
Parameter_Code.CHECKSUMand should be the first parameter.
- set_checksum(value: bytes)[source]¶
Sets the checksum parameter value in the TPDU.
If a checksum parameter already exists, its value is replaced. If it does not exist, it is inserted into the first position of the parameter list. This ensures the checksum field is correctly located for both building and verification.
- is_valid() bool[source]¶
Verifies the TPDU checksum if present.
If no checksum parameter is found, returns True (valid by default).
If present, recomputes the checksum over the TPDU using the Annex D.3 algorithm, comparing the calculated value to the stored one.
This method does not rebuild the TPDU using
build()to avoid unintentional mutations; it uses the raw packed representation for verification.
- build(add_checksum: bool = False) bytes[source]¶
Serializes the TPDU into its octet representation.
This method constructs a valid TPDU by encoding its fixed and variable parts, and optionally adds a checksum parameter.
Behavior depends on
add_checksum:False (default): The TPDU is serialized normally without any checksum.
- True:
A placeholder checksum parameter (two zero bytes) is inserted.
The TPDU is packed into octets.
The checksum is recomputed across the entire TPDU (with zeros in the checksum field).
The placeholder checksum is replaced with the computed value and the TPDU is repacked.
Example:
>>> pdu = TPDU_ConnectionRequest() >>> pdu.build(add_checksum=True) b'\n\xe0\x00\x00\x00\x00\x00\xc3\x02|\xd5' >>> parsed = TPDU_ConnectionRequest.from_octets(_) TPDU_ConnectionRequest(li=10, code=224,...parameters=[Parameter(type_id=<Parameter_Code.CHECKSUM: 195>, value=b'|\xd5')])
- Parameters:
add_checksum – Whether to generate and insert a checksum parameter during the build process.
- Returns:
Byte string representing the complete TPDU.
- classmethod from_octets(octets: bytes)[source]¶
Deserialize raw octets into a TPDU (or subclass) instance.
This method unpacks the raw TPDU structure into the corresponding class representation.
- Parameters:
octets (bytes) – Encoded TPDU octets.
- Returns:
TPDU instance populated from octets.
- Return type:
- class icspacket.proto.cotp.structs.TPDU_ClassOption(class_id: TPDU_Class = TPDU_Class.CLASS0, reserved: int = 0, extended_formats: bool = False, explicit_flow_control: bool = False)[source]¶
CLASS OPTION
Defines the preferred transport protocol class to be operated over the requested transport connection.
- class_id: TPDU_Class = 0¶
- reserved: int = 0¶
- extended_formats: bool = False¶
- explicit_flow_control: bool = False¶
- icspacket.proto.cotp.structs.TPDU_VariablePart = Field('<unnamed>', arch='x86_64', order='SysNative', seq=True, struct=Struct(fields=['type_id', 'value']), cond=False, options=False)¶
13.2.3 Variable part
The variable part is used to define less frequently used parameters. If the variable part is present, it shall contain one or more parameters.
- icspacket.proto.cotp.structs.TPDU_UserData = <Bytes>¶
13.2.4 Data field
This field contains transparent user data
- class icspacket.proto.cotp.structs.TPDU_ConnectionRequest(li: int = 0, code: int = 0, dst_ref: int = 0, src_ref: int = 0, class_opt: TPDU_ClassOption = None, parameters: list[Parameter] = None, user_data: bytes = None)[source]¶
13.3 Connection Request (CR) TPDU
- TPDU_FIXED_SIZE = 6¶
- dst_ref: int = 0¶
DST-REF - Set to zero.
- src_ref: int = 0¶
SRC-REF
Reference selected by the transport entity initiating the CR-TPDU to identify the requested transport connection.
- class_opt: TPDU_ClassOption = None¶
CLASS OPTION
Bits 8 to 5 of octet 7 define the preferred transport protocol class to be operated over the requested transport connection. The CR-TPDU contains the first choice of class in the fixed part. Second and subsequent choices are listed in the variable part if required.
- parameters: list[Parameter] = None¶
13.3.4 Variable part
The following parameters are permitted in the variable part:
Transport-Selector (T-selector)
TPDU size
Preferred maximum TPDU size
Version number (not used if class 0 is the preferred class)
Protection parameters (not used is class 0 is the preferred class)
Checksum (used only if class 4 is the preferred class)
Alternative protocol class(es) (not used if class 0 is the preferred class or when operating over CLNS) Parameter code:
Acknowledgement time (used only if class 4 is the preferred class)
Throughput (not used if class 0 is the preferred class)
Residual error rate (not used if class 0 is the preferred class)
Priority (not used if class 0 is the preferred class)
Transit delay (not used if class 0 is the preferred class)
Reassignment time (not used if class 0 or 2 is the preferred class; if class 4 is preferred and class 3 is an alternate, it may be used)
Inactivity timer (used only if class 4 is the preferred or selected class)
- user_data: bytes = None¶
No user data are permitted in class 0, and are optional in other classes.
- class icspacket.proto.cotp.structs.TPDU_ConnectionConfirm(li: int = 0, code: int = 0, dst_ref: int = 0, src_ref: int = 0, class_opt: TPDU_ClassOption = None, parameters: list[Parameter] = None, user_data: bytes = b'')[source]¶
13.4Connection Confirm (CC) TPDU
- TPDU_FIXED_SIZE = 6¶
- dst_ref: int = 0¶
DST-REF
Reference identifying the requested transport connection at the remote transport entity.
- src_ref: int = 0¶
SRC-REF
Reference selected by the transport entity initiating the CC-TPDU to identify the confirmed transport connection.
- class_opt: TPDU_ClassOption = None¶
CLASS OPTION
Defines the selected transport protocol class and option to be operated over the accepted transport connection.
- parameters: list[Parameter] = None¶
Same as in
TPDU_ConnectionRequest
- user_data: bytes = b''¶
13.4.5 User Data
No user data are permitted in class 0, and are optional in the other classes.
- class icspacket.proto.cotp.structs.TPDU_DisconnectRequest(li: int = 0, code: int = 0, dst_ref: int = 0, src_ref: int = 0, reason: TPDU_DisconnectReason | int = 0, parameters: list[Parameter] = None, user_data: bytes = None)[source]¶
13.5 Disconnect Request (DR) TPDU
The DR-TPDU is used to terminate a transport connection. It may carry parameters and optional user data.
- TPDU_FIXED_SIZE = 6¶
- dst_ref: int = 0¶
Destination reference — identifies the transport connection to be released.
- src_ref: int = 0¶
Source reference — identifies the transport connection from the sender’s perspective.
- reason: TPDU_DisconnectReason | int = 0¶
Reason code for disconnection (X.224 §13.5.4).
- parameters: list[Parameter] = None¶
Optional parameters (variable part)
Allowed parameters:
Additional information
Checksum
- user_data: bytes = None¶
Optional user data — must not exceed 64 octets.
- class icspacket.proto.cotp.structs.TPDU_DisconnectConfirm(li: int = 0, code: int = 0, dst_ref: int = 0, src_ref: int = 0, parameters: list[Parameter] = None)[source]¶
3.6 Disconnect Confirm (DC) TPDU
The DC-TPDU is sent in response to a DR-TPDU to confirm the disconnection of a transport connection.
- TPDU_FIXED_SIZE = 5¶
- dst_ref: int = 0¶
Destination reference — identifies the transport connection being confirmed as disconnected.
- src_ref: int = 0¶
Source reference — identifies the transport connection from the sender’s perspective.
- class icspacket.proto.cotp.structs.TPDU_Number(eot: bool = False, value: int = 0)[source]¶
- eot: bool = False¶
EOT
When set to ONE, it indicates that the current DT-TPDU is the last data unit of a complete DT-TPDU sequence (end of TSDU).
- value: int = 0¶
TPDU-NR
TPDU send sequence number (zero in class 0). May take any value in class 2 without explicit flow control. TPDU-NR is bits 7 to 1 of octet 3 for classes 0 and 1, bits 7 to 1 of octet 5 for classes 2, 3 and 4.
- class icspacket.proto.cotp.structs.TPDU_Data(li: int = 0, code: int = 0, nr: TPDU_Number = None, user_data: bytes = b'')[source]¶
13.7 Data (DT) TPDU
The DA-TPDU is used to transfer data over a transport connection. It may carry parameters and optional user data.
- TPDU_FIXED_SIZE = 2¶
- nr: TPDU_Number = None¶
TPDU-NR
- user_data: bytes = b''¶
This field contains data of the TSDU being transmitted.
- property tpdu_nr: int¶
- property is_last: bool¶
- class icspacket.proto.cotp.structs.TPDU_ExpeditedData(li: int = 0, code: int = 0, dst_ref: int = 0, ed_nr: TPDU_Number = None, parameters: list[Parameter] = None, user_data: bytes = None)[source]¶
13.8 Expedited Data (ED) TPDU
The ED-TPDU is used to send expedited (urgent) data across the transport connection.
- TPDU_FIXED_SIZE = 4¶
- dst_ref: int = 0¶
Destination reference — identifies the transport connection to which the expedited data belongs.
- ed_nr: TPDU_Number = None¶
Sequence number for expedited data (X.224 §13.8.4).
- user_data: bytes = None¶
The expedited data payload — must not exceed the maximum allowed for expedited service.
- class icspacket.proto.cotp.structs.TPDU_DataAcknowledgement(li: int = 0, code: int = 0, dst_ref: int = 0, next_nr: TPDU_Number = None, credit: int = 0, parameters: list[Parameter] = None)[source]¶
13.9 Data Acknowledgement (AK) TPDU
The AK-TPDU acknowledges receipt of data and communicates flow control information.
- TPDU_FIXED_SIZE = 6¶
- dst_ref: int = 0¶
Destination reference — identifies the transport connection.
- next_nr: TPDU_Number = None¶
Next expected TPDU sequence number.
- credit: int = 0¶
Flow control credit — number of TPDUs the sender is prepared to receive.
- parameters: list[Parameter] = None¶
Optional parameters allowed in AK-TPDU:
Checksum
Subsequence number when optionally used under the conditions defined in class 4.
Flow control confirmation when optionally used under the conditions defined in class 4.
Selective acknowledgement parameters when optionally used, under conditions defined in class 4.
- class icspacket.proto.cotp.structs.TPDU_ExpeditedDataAcknowledgement(li: int = 0, code: int = 0, dst_ref: int = 0, ed_nr: TPDU_Number = None, parameters: list[Parameter] = None)[source]¶
13.10 Expedited Data Acknowledgement (EA) TPDU
The EA-TPDU is used to acknowledge receipt of expedited data (ED TPDUs).
- TPDU_FIXED_SIZE = 4¶
- dst_ref: int = 0¶
Destination reference — identifies the transport connection.
- ed_nr: TPDU_Number = None¶
Expedited data sequence number being acknowledged.
- class icspacket.proto.cotp.structs.TPDU_Reject(li: int = 0, code: int = 0, dst_ref: int = 0, y_nr: int = 0)[source]¶
13.11 Reject (RJ) TPDU
The RJ-TPDU requests retransmission of certain TPDUs due to detected errors.
- TPDU_FIXED_SIZE = 5¶
- dst_ref: int = 0¶
Destination reference — identifies the transport connection.
- y_nr: int = 0¶
Next expected TPDU sequence number (Y(R)).
- class icspacket.proto.cotp.structs.ER_RejectCause(*values)[source]¶
- REASON_NOT_SPECIFIED = 0¶
- INVALID_PARAMETER_CODE = 1¶
- INVALID_TPDU_TYPE = 2¶
- INVALID_PARAMETER_VALUE = 3¶
- class icspacket.proto.cotp.structs.TPDU_Error(li: int = 0, code: int = 0, dst_ref: int = 0, reject_cause: ER_RejectCause | int = ER_RejectCause.REASON_NOT_SPECIFIED, parameters: list[Parameter] = None)[source]¶
13.12 TPDU error (ER) TPDU
No user data.
- TPDU_FIXED_SIZE = 4¶
- dst_ref: int = 0¶
Destination reference (see §13.4.3).
- reject_cause: ER_RejectCause | int = 0¶
Reject cause (§13.12.3).
- icspacket.proto.cotp.structs.parse_tpdu(octets: bytes) TPDU | TPDU_ConnectionConfirm | TPDU_ConnectionRequest | TPDU_Data | TPDU_DataAcknowledgement | TPDU_DisconnectConfirm | TPDU_DisconnectRequest | TPDU_Error | TPDU_ExpeditedData | TPDU_ExpeditedDataAcknowledgement | TPDU_Reject[source]¶
Parse a TPDU (Transport Protocol Data Unit) from raw octets.
First decodes a generic
TPDUto extract the TPDU code and uses this to dispatch to the corresponding TPDU subclass implementation defined inTPDU_TYPES.Example:
>>> tpdu = parse_tpdu(...) >>> isinstance(tpdu, TPDU_Data) True >>> data: bytes = tpdu.user_data
- Parameters:
octets (bytes) – Raw TPDU octets to parse.
- Raises:
ValueError – If the octet buffer is shorter than two bytes.
- Returns:
A parsed TPDU instance corresponding to the TPDU code (e.g.,
TPDU_ConnectionRequest).- Return type:
_TPDULike