Go library
The iec104 packages of go-iec60870-5 implement IEC 60870-5-104 with both roles: a client (controlling station) and a server (controlled station), with the application-layer security of IEC 62351-5. The same module implements IEC 101, IEC 102 and IEC 103, and IEC 104 shares its ASDU model with IEC 101. It has no cgo and no dependencies outside the standard library. This page follows v0.5.0. The license is MIT.
Install
Section titled “Install”go get github.com/otfabric/go-iec60870-5Requires Go 1.26 or later.
| Package | Import path | Contents |
|---|---|---|
iec104 |
github.com/otfabric/go-iec60870-5/iec104 |
Shared errors, State, Metrics |
client |
.../iec104/client |
The controlling station, and Group for redundancy |
server |
.../iec104/server |
The controlled station, sessions, Mux, FileServer |
apci |
.../iec104/apci |
APDU framing, sequence arithmetic and the k/w/t0..t3 parameters |
asdu |
github.com/otfabric/go-iec60870-5/asdu |
ASDU model and codec, independent of the transport and shared with IEC 60870-5-101 |
sec |
github.com/otfabric/go-iec60870-5/sec |
IEC 62351-5 application-layer security: identities, trust, the security endpoint and its messages |
iec60870 |
github.com/otfabric/go-iec60870-5 |
The Logger interfaces and constructors, shared by every companion standard |
Client: connect, interrogate, command
Section titled “Client: connect, interrogate, command”client.Dial connects and sends STARTDT. Every ASDU the station sends reaches your handler, in wire order. Request methods block until the station answers.
package main
import ( "context" "fmt" "log" "time"
"github.com/otfabric/go-iec60870-5/asdu" "github.com/otfabric/go-iec60870-5/iec104/client")
func main() { ctx := context.Background()
// Dial connects and sends STARTDT. Every ASDU the station sends reaches // the handler, in order. c, err := client.Dial(ctx, "10.0.0.5:2404", client.WithReconnect(client.Reconnect{}), client.WithHandler(client.HandlerFunc(func(a *asdu.ASDU) { for _, obj := range a.Objects { if m, ok := obj.(asdu.MeasuredFloat); ok { fmt.Printf("%s ioa=%d value=%g quality=%s\n", a.Cause, m.IOA, m.Value, m.Quality) } } }))) if err != nil { log.Fatal(err) } defer c.Close()
// General interrogation of station 1: returns when the station sends // the activation termination. data, err := c.Interrogate(ctx, 1, asdu.QOIStation) if err != nil { log.Fatal(err) } fmt.Printf("interrogation returned %d ASDUs\n", len(data))
if err := c.ClockSync(ctx, 1, time.Now()); err != nil { log.Fatal(err) }
// Select before operate. cmd := asdu.SingleCommand{IOA: 6001, Value: true, Select: true} if err := c.Command(ctx, 1, cmd); err != nil { log.Fatal(err) } cmd.Select = false if err := c.Command(ctx, 1, cmd); err != nil { log.Fatal(err) }}The address is host:port. A missing port becomes 2404, or 19998 with WithTLS.
Request methods
Section titled “Request methods”| Method | Sends | Returns when |
|---|---|---|
Interrogate(ctx, ca, qoi) |
C_IC_NA_1 activation |
Activation termination; result is the data with the matching “interrogated by” cause |
CounterInterrogate(ctx, ca, request, freeze) |
C_CI_NA_1 activation |
Activation termination; result is the counters |
Read(ctx, ca, ioa) |
C_RD_NA_1 request |
The ASDU with cause “request” for that address |
ClockSync(ctx, ca, t) |
C_CS_NA_1 activation |
Activation confirmation |
Command(ctx, ca, obj) |
Process command, activation | Activation confirmation |
Deactivate(ctx, ca, obj) |
Process command, deactivation | Deactivation confirmation |
TestCommand(ctx, ca) |
C_TS_TA_1 activation |
Activation confirmation |
ResetProcess(ctx, ca, qualifier) |
C_RP_NA_1 activation |
Activation confirmation |
GetFile(ctx, ca, ioa, name) |
File select, calls and acknowledgements | The file is acknowledged; result is its content |
PutFile(ctx, ca, ioa, name, sections...) |
File ready, then sections and segments | The station acknowledged the file |
ListFiles(ctx, ca, ioa) |
F_SC_NA_1 with cause “request” |
The last directory entry arrived |
Command accepts SingleCommand, DoubleCommand, StepCommand, SetpointNormalized, SetpointScaled, SetpointFloat and BitstringCommand. A non-zero Time selects the CP56Time2a type. Collected ASDUs also reach the handler: request methods are a convenience on top of the stream, never a filter in front of it.
IEC 104 has no transaction identifier. The client matches an answer by type, common address, object address and cause, so two identical requests cannot be pending at once; the second fails with ErrBusy.
For anything the request methods leave out (parameters, private types, file deletion) use Send(ctx, a), which transmits one ASDU and does not wait for an answer.
StartDT, StopDT and TestLink drive the U-frame procedures by hand. WithAutoStart(false) connects without sending STARTDT.
The ASDU model
Section titled “The ASDU model”Information objects are plain structs named for what they mean. asdu.New picks the type identification from the first object: no time tag when Time is zero, CP56Time2a otherwise. CP56Time2a carries a two-digit year, read as 1970 to 2069: a time outside that window goes out flagged invalid (IV) instead of silently landing in another century.
a := asdu.New(asdu.CauseSpontaneous, 1, asdu.MeasuredFloat{IOA: 4001, Value: 49.98, Time: asdu.Now()})wire, err := a.Encode(asdu.IEC104) // M_ME_TF_1back, err := asdu.Decode(wire, asdu.IEC104)| Struct | Without time tag | CP24Time2a | CP56Time2a |
|---|---|---|---|
SinglePoint |
M_SP_NA_1 |
M_SP_TA_1 |
M_SP_TB_1 |
DoublePoint |
M_DP_NA_1 |
M_DP_TA_1 |
M_DP_TB_1 |
MeasuredFloat |
M_ME_NC_1 |
M_ME_TC_1 |
M_ME_TF_1 |
SingleCommand |
C_SC_NA_1 |
C_SC_TA_1 |
Set ASDU.Type for the other variants and ASDU.Sequence for the SQ = 1 encoding. The object set is closed; consume it with a type switch. Types 128..255 have no object model and travel as ASDU.Raw in both directions. asdu.Params carries the field sizes, so the same codec serves the shorter IEC 60870-5-101 addresses (client.WithASDUParams). Time tags are UTC unless Params.Location (or Params.In) says otherwise.
Server: a controlled station
Section titled “Server: a controlled station”server.Mux routes by type identification and answers what you did not register with the refusals of the standard.
package main
import ( "log"
"github.com/otfabric/go-iec60870-5/asdu" "github.com/otfabric/go-iec60870-5/iec104/server")
func main() { mux := server.NewMux()
mux.HandleFunc(asdu.C_IC_NA_1, func(s *server.Session, req *asdu.ASDU) { qoi := req.First().(asdu.Interrogation).Qualifier _ = s.Confirm(req) _ = s.Send(s.Context(), asdu.New(qoi.Cause(), req.CommonAddr, asdu.SinglePoint{IOA: 1001, Value: true}, asdu.SinglePoint{IOA: 1002, Value: false})) _ = s.Terminate(req) })
mux.HandleFunc(asdu.C_SC_NA_1, func(s *server.Session, req *asdu.ASDU) { cmd := req.First().(asdu.SingleCommand) if cmd.IOA != 6001 { _ = s.Reject(req, asdu.CauseUnknownIOA) return } _ = s.Confirm(req) // operate the process ... })
srv, err := server.New(mux, server.WithCommonAddrs(1)) if err != nil { log.Fatal(err) } log.Fatal(srv.ListenAndServe(":2404"))}Mux answers a type without a handler, and an ASDU without information objects, with “unknown type identification” and an invalid cause with “unknown cause of transmission”. WithCommonAddrs answers a foreign station address with “unknown common address”. The reply helpers on Session are Confirm, Negative, Terminate, Reject (causes 44..47) and Reply.
The library checks the encoding, not the meaning. Select-before-operate state, value ranges and interlocks are the handler’s job: keep the selection in your handler and refuse an execute that does not match it.
Events and redundancy groups
Section titled “Events and redundancy groups”Server.Enqueue sends events with acknowledged delivery. Each redundancy group delivers them, in order, to its one started connection, keeps them while no control centre is connected, and sends again what was not acknowledged when a connection is lost. Session.Send and Server.Broadcast send immediately and buffer nothing.
srv, err := server.New(mux, server.WithRedundancyGroups( server.RedundancyGroup{Name: "main", Allow: []string{"10.0.0.0/24"}}, server.RedundancyGroup{Name: "backup"}), // everyone else server.WithEventQueue(10000))
_ = srv.Enqueue(asdu.New(asdu.CauseSpontaneous, 1, asdu.SinglePoint{IOA: 1001, Value: true, Time: asdu.Now()}))The queue holds 1024 events per group by default and drops the oldest when full; Pending(group) and Dropped(group) report its state. The queue is in memory: it survives an absent control centre, not a restart of the station.
Other server options: WithMaxSessions, WithAccept (filter by address before any exchange), WithStateHandler, WithTLS, WithSecurity, WithParams. File transfer is served by server.NewFileServer, registered with mux.Handle(server.NewFileServer(source), server.FileTypes...).
Redundancy on the client side
Section titled “Redundancy on the client side”client.NewGroup keeps several connections to one station established, exactly one started, the standbys supervised by test frames. It fails over automatically and offers Switchover.
g, err := client.NewGroup([]string{"10.0.0.5:2404", "10.1.0.5:2404"}, client.WithHandler(h), client.WithSwitchHandler(func(active *client.Client) { /* failover happened */ }))if err := g.Connect(ctx); err != nil { ... }defer g.Close()
data, err := g.Interrogate(ctx, 1, asdu.QOIStation) // on the active connectionA select and its execute must use the same connection: select again after a switchover.
TLS (IEC 62351-3)
Section titled “TLS (IEC 62351-3)”client.WithTLS and server.WithTLS take a standard *tls.Config that the library does not weaken. Set MinVersion to TLS 1.2 or later. For mutual authentication set ClientAuth: tls.RequireAndVerifyClientCert and ClientCAs on the server and Certificates on the client. Without it, a TLS server still accepts commands from any client. In a handler, Session.Conn() returns the *tls.Conn, whose ConnectionState().PeerCertificates identifies the controlling station. The tls example shows both sides.
TLS protects the connection. The application-layer security below protects every ASDU and authorizes every request; the two combine.
Application-layer security (IEC 62351-5)
Section titled “Application-layer security (IEC 62351-5)”Package sec implements the A-Profile of IEC 62351-5:2023 as IEC TS 60870-5-7:2025 applies it to IEC 104, for both roles. client.WithSecurity and server.WithSecurity switch it on. After STARTDT the stations run a Station Association with X.509 certificates (ECDH on secp256r1 or Curve25519, HKDF), then a Session Key Change (AES-256 key wrap), and from then on every application ASDU travels as Secure Data (S_SD_NA_1) with a sequence number. One sec.Endpoint holds one association:
ep, err := sec.New(sec.Config{ Role: sec.Controlling, AssociationID: 1, Identity: id, // sec.LoadIdentity("scada.pem", "scada.key") Trust: sec.Trust{Authorities: []*x509.Certificate{plantCA}}, DataProtection: sec.AES256GCM, // or HMAC only, the default Store: sec.FileStore{Path: "/var/lib/scada/station1.keys"},})c, err := client.Dial(ctx, "10.0.0.5:2404", client.WithSecurity(ep)) // returns with the session establishedOn the station, server.WithSecurity takes a function that makes the endpoint of each controlling station by its AIM (the controlling station’s half of the association ID), or refuses it. Give each AIM a Trust that accepts only its own station, and the Roles it may use:
srv, err := server.New(mux, server.WithCommonAddrs(1), server.WithSecurity(func(group string, aim uint16) (*sec.Endpoint, error) { if aim != 1 { return nil, fmt.Errorf("no association for controlling station %d", aim) } return sec.New(sec.Config{ Role: sec.Controlled, AssociationID: 1, Identity: stationID, Trust: centreOnly, CommonAddr: 1, Roles: []sec.AccessRole{sec.RoleViewer}, // may look, not operate }) }))| Item | Support |
|---|---|
| Procedures | Station Association, Session Key Change, Secure Data, Session Initiation Request; segmentation of long security messages |
| Trust | Pinned self-signed certificates or public keys, or Central Authorities with an optional list of subjects |
| MAC algorithms | HMAC-SHA-256 with 16 (default) or 8 octets, HMAC-SHA3-256 |
| Data protection | The MAC algorithms, or AES-256-GCM, which also encrypts |
| Access control | The seven roles of IEC TS 60870-5-7; a request no role permits is answered with cause 17 and never reaches the handler. The client reports it as *iec104.NegativeError |
| Several controlling stations | One association per AIM; the connections of a client redundancy group share theirs |
| Keys at rest | sec.FileStore keeps an association over a restart (mode 0600), or implement sec.KeyStore against a vault or HSM |
| Observability | The counters (Endpoint.Statistics) and events with their mnemonics (Config.OnEvent) of IEC 62351-5 |
| Not implemented | secp256k1, X448 and brainpool curves, BLAKE2s, the broadcast session key of IEC 60870-5-101, IEC 62351-8 access tokens, certificate revocation lists |
The server discards unprotected ASDUs. Session.Security returns the association of the session’s controlling station, so a handler can see who sent a request.
Protocol parameters
Section titled “Protocol parameters”Both sides take apci.Params; the defaults are those of the standard (k 12, w 8, t0 30 s, t1 15 s, t2 10 s, t3 20 s). Zero fields take defaults and T3: 0 disables the idle test. Validate enforces k and w each in 1..32767 (edition 1 lets w exceed k) and T2 < T1.
p := apci.DefaultParams()p.T3 = 10 * time.Secondc, err := client.Dial(ctx, addr, client.WithParams(p))Errors
Section titled “Errors”Always test with errors.Is and errors.As.
| Situation | Error | Connection |
|---|---|---|
| Station refuses (P/N = 1, cause 44..47, or 17 not authorized under IEC 62351-5) | *iec104.NegativeError; Cause() tells why |
stays up |
| No answer before the deadline | wraps context.DeadlineExceeded |
stays up |
| Identical request pending | iec104.ErrBusy |
stays up |
| Request method called from a client handler | iec104.ErrInHandler |
stays up |
| Connection could not be made | iec104.ErrConnectFailed, wrapping the cause |
|
| No connection / data transfer stopped | iec104.ErrNotConnected / iec104.ErrNotStarted |
|
| Peer closed or transport failed | iec104.ErrConnectionLost |
closed |
No acknowledgement within t1 |
iec104.ErrTimeout |
closed |
| Peer broke the protocol | iec104.ErrProtocol |
closed |
| Value cannot be encoded | asdu.ErrInvalidValue, asdu.ErrTypeMismatch, … |
stays up, nothing sent |
err := c.Command(ctx, 1, asdu.SingleCommand{IOA: 6001, Value: true})
var neg *iec104.NegativeErrorswitch {case err == nil: // confirmedcase errors.As(err, &neg): if neg.Cause() == asdu.CauseUnknownIOA { // no such object }case errors.Is(err, context.DeadlineExceeded): // the station did not answer; the connection is still upcase errors.Is(err, iec104.ErrNotConnected), errors.Is(err, iec104.ErrConnectionLost): // no connection: retry after the state handler reports StateStartedcase errors.Is(err, asdu.ErrInvalidValue): // programming error: the command cannot be encoded}A received ASDU that does not decode is dropped and reported through Metrics.OnDecodeError; the connection stays up.
Timeouts, retries and concurrency
Section titled “Timeouts, retries and concurrency”Connectis bounded by the context and byt0. A request without a context deadline is bounded byWithRequestTimeout(default 10 s). On expiry the slot is released and the connection stays up; a late answer still reaches the handler.WithReconnectre-dials with exponential backoff (MinDelay1 s,MaxDelay30 s).WithStartHandlerruns after every connect that started data transfer, the place for a general interrogation.WithRetry(off by default; 3 attempts, 500 ms backoff doubling to 10 s) repeats only requests that read:Interrogate,CounterInterrogatewithasdu.FreezeRead,Read,TestCommand.Command,Deactivate,ClockSync,ResetProcess, a counter freeze or reset andSendare never repeated.- Every method of
Client,Server,SessionandMuxis safe for concurrent use. A client handler runs on one goroutine per connection, a server handler on one per session. Request methods called from inside a client handler fail at once withErrInHandler; use another goroutine. Sendblocks whilekI frames are unacknowledged. A slow handler does not stall acknowledgements or test frames: the protocol machine keeps running and memory per connection is bounded.
Logging and metrics
Section titled “Logging and metrics”Both are off by default and cost nothing when off.
c, err := client.Dial(ctx, "10.0.0.5:2404", client.WithLogger(iec60870.NewSlogLogger(slog.NewJSONHandler(os.Stderr, nil))), client.WithMetrics(myMetrics), client.WithReconnect(client.Reconnect{}), client.WithRetry(client.Retry{Attempts: 3}), client.WithStartHandler(func(ctx context.Context, c *client.Client) { _, _ = c.Interrogate(ctx, 1, asdu.QOIStation) // refresh after every (re)connect }))Log entries are constant messages with fields (component, remote, state, type, error, …); per-frame entries are logged at debug level. iec104.Metrics has OnConnect, OnDisconnect, OnFrameSent, OnFrameReceived and OnDecodeError; the optional RequestMetrics (client) and HandlerMetrics (server) add request outcomes, retries and handler duration. Embed iec104.NopMetrics to implement a subset. Callbacks run on the protocol path and must not block.
Examples
Section titled “Examples”Eight examples are self-contained (station and client in one process over loopback):
go run ./examples/quickstart # the smallest station and clientgo run ./examples/commands # select-before-operate, set points, refusalsgo run ./examples/files # download, upload and directorygo run ./examples/events # event queue, broadcast, end of initializationgo run ./examples/redundancy # redundancy group, switchover, failovergo run ./examples/tls # TLS with certificates on both sidesgo run ./examples/secure # IEC 62351-5: association, Secure Data, rolesgo run ./examples/observability # logs, states, metrics, retries, reconnectLimitations
Section titled “Limitations”- IEC 62351-5 is tested against one other implementation, cB-Sigma/iec60870, which it was derived from; no third implementation or certified test equipment has checked it.
- No IEC 60870-5-101 serial link layer. The
asdupackage takes the 101 field sizes, so a separate module could add it. - No combined station (both directions on one connection); run a client and a server side by side.
- File upload and the directory are verified by the repository’s own tests only: no reference stack offers them through the interop images yet. File deletion and the query log have their types in the codec but no procedure.
- A client waiting for a command confirmation does not also wait for the activation termination; the termination reaches the handler.
- Verified against lib60870-C, j60870, wendy512/iec104 and cB-Sigma/iec60870, not against certified test equipment or field devices. See Testing and interop.