Traffic records¶
One JSON body per DNP3 application fragment, as the DNP3 traffic schema, version 1, defines it, and the envelope that carries it. DNP3 traffic records is the guide; this is the reference.
py1815.traffic ¶
DNP3 traffic records: one JSON body per application fragment, and its envelope.
An application that embeds the outstation or the master knows every fragment
it sends and receives. This module describes each fragment as a JSON body that
follows the DNP3 traffic schema, version 1 (:data:SCHEMA_ID), and wraps the
body in a :class:~py1815.envelope.ProtocolMessage, so the application can
publish its own traffic to consumers that also take captured DNP3 traffic. It
builds the bodies from a :class:~py1815.observers.RequestRecord from the
outstation, and from an exchange or an unsolicited response from the master.
The schema, not any one decoder's output, defines the body (D103). It ships
with the package, and :func:load_schema returns it. A body describes the
fragment in IEEE 1815 terms:
- The link addresses, the application function code, the link layer's primary-message bit, which end sent the fragment, the application control's UNS bit, and a response's internal indications by name.
- The IP address and port of each end of the connection, where known.
- One entry per object header, with its qualifier, its range or count, and the octets of objects that are not points, such as a time write.
- Every point the fragment carries, in wire order, with its point type, object group and variation, the index on the wire, its value, flag octet and time, and the fields of a control object.
The fragment is read by this module's own walk of the object headers, written
from the object layouts of IEEE 1815-2012. An object the master's decoder also
reads takes its layout from :data:py1815.decode.LAYOUTS, so both read its
octets the same way. The walk reads requests and responses,
every qualifier the standard defines, and the command objects a master sends
and an outstation echoes. A fragment it cannot read to its end is described as
rejected, with no objects and no points (:data:REJECTED_MALFORMED,
:data:REJECTED_POINT_COUNT, :data:REJECTED_TOO_LARGE).
Copyright 2026 DER Security Corp. Licensed under the Apache License, Version 2.0.
Direction ¶
Bases: StrEnum
Which end sent a fragment: the master sends requests, the outstation responses.
Source code in src/py1815/traffic.py
load_schema ¶
Return the JSON Schema the bodies follow, read from the copy in this package.
Source code in src/py1815/traffic.py
fragment_body ¶
fragment_body(
fragment: bytes,
*,
direction: Direction | str,
source: int,
destination: int,
sender: Endpoint | None = None,
receiver: Endpoint | None = None,
) -> dict[str, Any]
Return the body for one application fragment, request or response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fragment
|
bytes
|
The application fragment, from the application control octet to the last object. |
required |
direction
|
Direction | str
|
|
required |
source
|
int
|
The link address of the station that sent the fragment. |
required |
destination
|
int
|
The link address it was sent to. |
required |
sender
|
Endpoint | None
|
The sender's end of the connection, for |
None
|
receiver
|
Endpoint | None
|
The receiver's end, for |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the fragment is shorter than a control octet and a
function code, or |
Source code in src/py1815/traffic.py
record_bodies ¶
record_bodies(
record: RequestRecord,
*,
outstation: Endpoint | None = None,
) -> list[dict[str, Any]]
Return the bodies of one request record: the request, then the response.
An unsolicited response gives one body, and a request that was not
answered gives the request alone. outstation is this outstation's end
of the connection, for a record that does not carry one in local.
Source code in src/py1815/traffic.py
exchange_bodies ¶
exchange_bodies(
exchange: Exchange,
*,
master_address: int,
outstation_address: int,
master: Endpoint | None = None,
outstation: Endpoint | None = None,
) -> list[dict[str, Any]]
Return the bodies of one master exchange: the request, then each response fragment.
master and outstation are the two ends of the master's connection.
The request is described once, however many times it was sent again.
Source code in src/py1815/traffic.py
unsolicited_body ¶
unsolicited_body(
unsolicited: Unsolicited,
*,
master_address: int,
outstation_address: int,
master: Endpoint | None = None,
outstation: Endpoint | None = None,
) -> dict[str, Any]
Return the body of one unsolicited response the master received.
Source code in src/py1815/traffic.py
envelope ¶
envelope(
body: dict[str, Any],
*,
outstation: Endpoint,
at: float,
forwarder_id: str = "",
capture_origin: str = CAPTURE_ORIGIN_LIVE,
) -> ProtocolMessage
Return the envelope that carries one body.
The envelope names the outstation: source is its address and port and
client_id its link address, the source address of a response and the
destination address of a request. direction is upstream for every
fragment, whichever end sent it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
dict[str, Any]
|
A body from :func: |
required |
outstation
|
Endpoint
|
The outstation's address and port. |
required |
at
|
float
|
When the fragment was sent, in seconds since the Unix epoch. |
required |
forwarder_id
|
str
|
Names the system publishing the message. |
''
|
capture_origin
|
str
|
Where the traffic was captured; |
CAPTURE_ORIGIN_LIVE
|
Source code in src/py1815/traffic.py
record_messages ¶
record_messages(
record: RequestRecord,
*,
outstation: Endpoint | None = None,
forwarder_id: str = "",
capture_origin: str = CAPTURE_ORIGIN_LIVE,
) -> list[ProtocolMessage]
Return the envelopes of one request record, stamped with the record's time.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the record has no |
Source code in src/py1815/traffic.py
exchange_messages ¶
exchange_messages(
exchange: Exchange,
*,
master_address: int,
outstation_address: int,
outstation: Endpoint,
master: Endpoint | None = None,
at: float | None = None,
forwarder_id: str = "",
capture_origin: str = CAPTURE_ORIGIN_LIVE,
) -> list[ProtocolMessage]
Return the envelopes of one master exchange.
at is when the exchange ended, now if not given. The responses are
stamped with it, and the request with the time it was first sent.
Source code in src/py1815/traffic.py
unsolicited_message ¶
unsolicited_message(
unsolicited: Unsolicited,
*,
master_address: int,
outstation_address: int,
outstation: Endpoint,
master: Endpoint | None = None,
at: float | None = None,
forwarder_id: str = "",
capture_origin: str = CAPTURE_ORIGIN_LIVE,
) -> ProtocolMessage
Return the envelope of one unsolicited response, stamped at or now.
Source code in src/py1815/traffic.py
py1815.envelope ¶
The message envelope DNP3 traffic records are published in.
Each DNP3 application fragment, captured from the network or reported by one
end of the association, is published as a :class:ProtocolMessage: an
envelope that names the protocol, the device and the time, and carries the
fragment's description as a JSON payload. :mod:py1815.traffic builds those
payloads and envelopes from what the outstation and the master report, so an
application can publish their traffic beside captured traffic without
converting it. This module is the envelope alone. It knows nothing of DNP3
beyond the protocol's name, and nothing in the outstation or the master depends
on it.
The envelope is a published contract shared with consumers that also ingest
other protocols. It is copied here, not imported, so the library keeps no
runtime dependency (D1). Its serialized form is the contract, as
:meth:ProtocolMessage.to_dict emits it:
version,protocol,direction,timestamp,client_id,forwarder_id,payload,source,hashandis_validare always present.destination,protocol_dataandvalidation_errorappear only when set. An optional field is left out, not written as null: a consumer matches on presence, and adding a field is a compatible change.timestampis an ISO 8601 date and time with a UTC offset. A trailingZis rewritten to+00:00on construction. A date with no time, or a time with no offset, is refused: neither names one instant.hashis a UUIDv3 over the protocol, the client id, the timestamp and the payload text, so a consumer can find a message published twice without asking the producer.
Copyright 2026 DER Security Corp. Licensed under the Apache License, Version 2.0.
WireDirection ¶
Bases: StrEnum
Which way the message travelled, as the envelope carries it.
UPSTREAM is from the device toward a server, DOWNSTREAM the other
way.
Source code in src/py1815/envelope.py
Protocol ¶
Bases: StrEnum
The application protocol a message was captured from.
Source code in src/py1815/envelope.py
from_string
classmethod
¶
Return the protocol named by a wire value, ignoring case.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value names no protocol this module knows. |
Source code in src/py1815/envelope.py
PayloadEnvelope
dataclass
¶
A payload, with the content type and encoding a consumer reads it by.
Source code in src/py1815/envelope.py
NetworkEndpoint
dataclass
¶
One end of the connection a message was seen on.
mac is part of the format. A producer that sees frames sets it; this
library sees connections, and leaves it unset.
Source code in src/py1815/envelope.py
ProtocolMetadata
dataclass
¶
Detail about a message beyond its payload.
The format names many fields for other protocols. None of them applies to
DNP3, so this class models only extra, the format's free-form
dictionary, and keeps any other key it reads in passthrough, so that a
message read and written again keeps its shape.
Source code in src/py1815/envelope.py
to_dict ¶
Return the metadata as the envelope carries it, without empty fields.
from_dict
classmethod
¶
Rebuild metadata from :meth:to_dict.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/py1815/envelope.py
ProtocolMessage
dataclass
¶
One captured protocol message, ready to publish.
payload may be given as a dictionary, which is stored as a JSON
payload. The hash is computed on construction unless one is given.
Source code in src/py1815/envelope.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
body
property
¶
Return the payload as a dictionary, or None when it is not JSON.
to_dict ¶
Return the message in its serialized form. The module docstring states the contract.
Source code in src/py1815/envelope.py
to_json ¶
from_dict
classmethod
¶
Rebuild a message from its serialized form.
hash is taken as given and not computed again: it is the
producer's key.
Raises:
| Type | Description |
|---|---|
KeyError
|
If a field the contract always carries is missing. |
Source code in src/py1815/envelope.py
from_json
classmethod
¶
wire_version ¶
Return the value for the envelope's version field.
Consumers accept a bare semantic version or "2.0" and reject anything
else, so the installed version is not passed through as it is. A
development build carries a suffix such as 0.4.0.dev1, and a source
tree that was never installed has no version. The release segment is used
when there is one, and "2.0" otherwise.