go-dnp3 is a Go library for parsing and encoding DNP3 (Distributed Network Protocol) frames.
*dnp3.Frame implements the standard gopacket interfaces (Layer, DecodingLayer, SerializableLayer, ApplicationLayer). TCP and UDP port 20000 (DNP3-over-IP) are auto-registered, so gopacket.NewPacket decodes DNP3 automatically.
- Parsing: Use
gopacket.NewPacket(data, dnp3.LayerTypeDNP3, gopacket.Default), ordnp3.NewFrameFromBytes(data)for raw frame bytes, orframe.DecodeFromBytes(data, df)to drivegopacket.DecodingLayerParser. - Encoding: Use
gopacket.SerializeLayers(buf, opts, frame).Frame.SerializeTorecomputesDataLink.Lengthand inserts DNP3 CRCs on the fly. - Stream parsing: Use
dnp3.ParseFrames(data)to consume multiple DNP3 frames out of a single TCP read (handles partial trailing frames). - Inspection: Use
String()for a human-readable, indented packet dump (excludes reserved fields and CRCs). - Serialization: Full support for
json.Marshal()to convert packets into machine-friendly JSON.
// Auto-decode (NewPacket dispatches on LayerTypeDNP3 directly, or on TCP/UDP port 20000 in a pcap):
pkt := gopacket.NewPacket(input, dnp3.LayerTypeDNP3, gopacket.Default)
frame := pkt.Layer(dnp3.LayerTypeDNP3).(*dnp3.Frame)
// Build outbound:
buf := gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(buf, gopacket.SerializeOptions{}, frame)
wire := buf.Bytes()See example.go for a full end-to-end demo, including in-place point mutation and round-tripping.
Run make setup to install development tools used by this repository.
Data for tests is sourced from opendnp3 conformance reports
Run make test to run basic tests.
Pass a full PCAP file using the -args option -pcaps=comma.pcap,delimited.pcap,list.pcap.
go test ./dnp3 -v -args -pcaps=my-custom.pcapView the string and json outputs of test cases using the -args flag -print-string and -print-json.
go test ./dnp3 -args -printgolangci-lint is used for lint and format checking. Run make lint to check for errors, and make fix to try to automatically fix linting or formatting errors.
Based on Wireshark's parser and publicly available documents (such as this validation guide), as access to the official DNP3 specification is restricted.
