server

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package server is the meter side of DLMS/COSEM: a logical device that holds COSEM objects (package classes) and serves them to clients over the wrapper (TCP or UDP) and HDLC.

It accepts application associations per client address with no security, low-level security or HLS (mechanisms 2 to 7), security suites 0 to 2 with authenticated, encrypted or authenticated and encrypted APDUs under the global or a dedicated key, in service-specific, general-glo or general-ded ciphering. It answers GET, SET and ACTION (normal, with list, and in data blocks both ways), the ACCESS service, and, with ShortNames, the short name services (read, write, unconfirmed write, with data blocks and parameterized access). Long answers go in data blocks, or in general block transfer when the client negotiated it. Data notifications are pushed to a client address or a push setup's destination. The current association object (class 15, or class 12 with short names) is built from the server's objects and the client's rights.

The protection of APDUs is cosem's (Protect, UnprotectKeys and the HLS answers), so a client of this module and this server share one implementation; dlms-sim's meter, written separately, and Gurux's client check both.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Association

type Association struct {
	// ClientAddress is the client's SAP (wrapper wPort, HDLC client
	// address); 16 is the public client.
	ClientAddress uint16
	// Mechanism is the authentication: 0 none, 1 low-level (Secret is the
	// password), 2 to 7 HLS (Secret is the secret of 2, 3, 4 and 6).
	Mechanism byte
	Secret    []byte
	// Policy protects APDUs after the association: 0 none, or
	// cosem.SecAuthentication and/or cosem.SecEncryption. Suite is the
	// security suite (0, 1 or 2); Key and AuthKey the global unicast and
	// authentication keys; BroadcastKey the global broadcast key.
	Policy       byte
	Suite        byte
	Key, AuthKey []byte
	BroadcastKey []byte
	// SigningKey is the server's key and ClientKey the client's public key
	// (mechanism 7, general signing, and the ephemeral keys of one-pass
	// agreement in general ciphering).
	SigningKey *ecdsa.PrivateKey
	ClientKey  *ecdsa.PublicKey
	// Signing requires general-signing on every request after the AARQ;
	// the answers are signed with SigningKey. A signed request is accepted
	// and answered signed without it too.
	Signing bool
	// General ciphering (IEC 62056-5-3:2023 5.5): MasterKey is the KEK of
	// wrapped content keys; KAKey is the server's key agreement key and
	// ClientKAKey the client's (one-pass and static agreed keys). Requests
	// in general ciphering are answered with a fresh key of the kind the
	// request used, repeating its transaction-id.
	MasterKey   []byte
	KAKey       *ecdsa.PrivateKey
	ClientKAKey *ecdsa.PublicKey
	// Compression is the server's V.44 setting (suites 1 and 2): compressed
	// requests in general-glo, general-ded and general ciphering are
	// decompressed after deciphering; with CompressAnswers the answers in
	// those forms are compressed when that makes them smaller. Nil refuses
	// compressed requests.
	Compression     *cosem.Compression
	CompressAnswers bool
	// ThirdParties are the third parties (IEC 62056-5-3:2023 annex J) whose
	// general-signing and general-ciphering layers are accepted inside the
	// client's general-glo, general-ded or general ciphering; the answer
	// carries the server's layers for the third party inside the client's.
	ThirdParties []cosem.ResponderThirdParty
	// Context, when not 0, is the only application context accepted (1 LN,
	// 2 SN, 3 LN with ciphering, 4 SN with ciphering); an AARQ proposing
	// another is refused with diagnostic 2. 0 accepts the server's
	// referencing with or without ciphering.
	Context byte
	// InvocationCounter, when set, is the logical name of a class 1 object
	// holding the client's last invocation counter (readable by the public
	// client): an AARQ whose counter is not above it is refused.
	InvocationCounter string
	// Allow, when set, decides each access (rights of the association);
	// nil allows everything the objects allow.
	Allow func(class uint16, ln string, id int8, op Op) bool
	// Conformance is the services offered to this client (0: the server's).
	Conformance uint32
}

Association is what the server accepts from one client address.

type Op

type Op byte

Op is what a client does to an attribute or method.

const (
	OpRead Op = iota + 1
	OpWrite
	OpExecute
)

Operations checked by Association.Allow.

type PushOptions

type PushOptions struct {
	// ClientAddress is the wPort pushes go to (default 64, the push
	// client of IS 15959 and of Gurux's examples).
	ClientAddress uint16
	// Security, when set, protects pushes in general-glo-ciphering with
	// its policy, suite and keys (its client address is not used).
	Security *Association
	// Time stamps each notification (the date-time field) when set.
	Time func() time.Time
	// Loc is the zone of the date-time (default UTC).
	Loc *time.Location
}

PushOptions are how pushes are sent.

type Server

type Server struct {
	// SystemTitle is the server's, 8 octets (needed with protected APDUs
	// and mechanisms 5 to 7).
	SystemTitle []byte
	// Associations are the clients accepted, by client address; a client
	// address with none is refused.
	Associations []*Association
	// ShortNames serves short name referencing instead of logical names;
	// objects get base names as they are added (AddSN to choose one).
	ShortNames bool
	// MaxPDU is the largest APDU the server receives and sends (default
	// 1024); BlockSize, when set, bounds the data in one block.
	MaxPDU    uint16
	BlockSize int
	// Conformance is the services offered (default: get, set, action,
	// selective access, block transfer in all three, multiple references,
	// ACCESS, data notification and general protection; with ShortNames
	// read, write, unconfirmed write, parameterized access, multiple
	// references and block transfer). GBT adds general block transfer.
	Conformance uint32
	GBT         bool
	// Address is the server's SAP: the wrapper wPort and the HDLC upper
	// address (default 1). HDLCAddress is the lower (physical) address and
	// HDLCAddressSize the size of the server address (1, 2 or 4; default
	// 1); HDLCParams the data link parameters (zero: the defaults).
	Address         uint16
	HDLCAddress     int
	HDLCAddressSize int
	HDLCParams      hdlc.Params
	HDLCInactivity  time.Duration
	// Logf, when set, receives events (refused associations, protocol
	// errors).
	Logf func(format string, args ...any)
	// contains filtered or unexported fields
}

Server is one logical device. Set the fields, Add objects, then serve.

func (*Server) Add

func (s *Server) Add(objs ...classes.Object)

Add puts objects in the server. An object with the class and logical name of one already there replaces it.

func (*Server) AddSN

func (s *Server) AddSN(o classes.Object, base uint16)

AddSN puts an object in the server at a chosen base name (short names).

func (*Server) BaseName

func (s *Server) BaseName(class uint16, ln string) (uint16, bool)

BaseName is an object's base name (short names).

func (*Server) Close

func (s *Server) Close() error

Close stops every listener and closes open connections.

func (*Server) DataNotification

func (s *Server) DataNotification(invokeID uint32, at time.Time, loc *time.Location, body *axdr.DlmsData) ([]byte, error)

DataNotification is the APDU of a data-notification with body.

func (*Server) ListenHDLC

func (s *Server) ListenHDLC(addr string) (string, error)

ListenHDLC serves HDLC over TCP at addr (a meter behind a serial to Ethernet converter) until Close.

func (*Server) ListenUDP

func (s *Server) ListenUDP(addr string) (string, error)

ListenUDP serves the wrapper over UDP at addr until Close: one WPDU per datagram each way, a session per client address and port.

func (*Server) ListenWrapper

func (s *Server) ListenWrapper(addr string) (string, error)

ListenWrapper serves the wrapper over TCP at addr ("127.0.0.1:0", ":4059") until Close, and returns the address listened on.

func (*Server) NewSession added in v0.3.0

func (s *Server) NewSession(client uint16) (*Session, error)

NewSession starts a session for a client address; the client then sends its AARQ through Handle. It fails when the server's settings cannot be served.

func (*Server) Object

func (s *Server) Object(class uint16, ln string) (classes.Object, bool)

Object returns the object of a class and logical name.

func (*Server) Objects

func (s *Server) Objects() []classes.Object

Objects lists the server's objects in the order they were added.

func (*Server) Push

func (s *Server) Push(setupLN string, o PushOptions) error

Push runs the push of a push setup object (class 40): its object list read from the server's objects, as a structure, to its destination (send_destination_and_method: TCP or UDP, "host:port").

func (*Server) PushHDLC

func (s *Server) PushHDLC(body *axdr.DlmsData, o PushOptions) error

PushHDLC queues a data-notification with body on every open HDLC link: it goes in a UI frame at the next right to talk.

func (*Server) PushTo

func (s *Server) PushTo(network, addr string, body *axdr.DlmsData, o PushOptions) error

PushTo sends a data-notification with body to network ("tcp" or "udp") address addr in a WPDU from the server's wPort.

func (*Server) Resolve

func (s *Server) Resolve(class uint16, ln string, attr int8) (*axdr.DlmsData, error)

Resolve reads an attribute of another object (classes.Resolver).

func (*Server) ServeHDLC

func (s *Server) ServeHDLC(rw io.ReadWriter) error

ServeHDLC answers HDLC on rw (a serial port, a converter's connection) until it ends: the server is a secondary station at Address (upper) and HDLCAddress (lower); each client address is a data link of its own, with its session from SNRM to DISC. APDUs in UI frames are served and never answered.

func (*Server) ServeWrapper

func (s *Server) ServeWrapper(c io.ReadWriter) error

ServeWrapper answers the wrapper on one connection (TCP, or a stream someone else opened) until it ends; each client wPort has its own session. WPDUs for another server wPort are dropped; the all-station wPort is served but never answered.

func (*Server) Stats

func (s *Server) Stats() Stats

Stats returns the counters so far.

type Session added in v0.3.0

type Session struct {
	// contains filtered or unexported fields
}

Session is one client's session with the server for callers that carry APDUs themselves: a data concentrator's logical devices, a transport this package does not have. It is safe for use by one goroutine at a time.

func (*Session) Associated added in v0.3.0

func (x *Session) Associated() bool

Associated says whether the client's association is open.

func (*Session) Handle added in v0.3.0

func (x *Session) Handle(apdu []byte) []byte

Handle answers one APDU (nil: nothing is answered), with block transfer and general block transfer as negotiated.

type Stats

type Stats struct {
	Associations int // accepted (after HLS pass 3 when HLS is used)
	Refused      int // AARQs refused and failed HLS passes
	Blocks       int // data blocks sent (GET, ACTION, short name read)
	GBTBlocks    int // general-block-transfer APDUs sent
	// Protected requests opened, by form.
	ServiceSpecific, GeneralGlo, GeneralDed int
	// GeneralCiphering counts general-ciphering requests opened, Signed
	// the general-signing requests answered, BadSignatures the signed or
	// third-party layers refused, ThirdParty the requests carrying a third
	// party's layers, Compressed the compressed requests decompressed.
	GeneralCiphering, Signed, BadSignatures, ThirdParty, Compressed int
}

Stats counts what the server did.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL