-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtcpclient.go
More file actions
605 lines (537 loc) · 20 KB
/
Copy pathtcpclient.go
File metadata and controls
605 lines (537 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package modbus
import (
"context"
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"sync"
"sync/atomic"
"syscall"
"time"
)
const (
tcpProtocolIdentifier uint16 = 0x0000
// Modbus Application Protocol
tcpHeaderSize = 7
tcpMaxLength = 260
// Default TCP timeout is not set
tcpTimeout = 10 * time.Second
tcpIdleTimeout = 60 * time.Second
)
// ErrTCPHeaderLength informs about a wrong header length.
type ErrTCPHeaderLength int
func (length ErrTCPHeaderLength) Error() string {
return fmt.Sprintf("modbus: length in response header '%d' must not be zero or greater than '%v'",
length, tcpMaxLength-tcpHeaderSize+1)
}
// ErrStreamDesynced reports that responses on the connection can no longer be
// paired with the requests that were sent. It is returned wrapped around the
// underlying cause, and covers two conditions that cannot be told apart from the
// outside:
//
// - The byte stream is misaligned. A read failed part-way through a frame, so
// the rest of that frame is still queued and the next read would parse those
// leftover bytes as a header.
// - A complete, well-formed frame arrived that cannot be attributed to the
// request that was sent, and is not a late response that can be accounted
// for. Here the bytes are still frame-aligned, but a request is left
// outstanding whose answer would corrupt the next exchange, and there is no
// way to tell how many such orphans are queued.
//
// Neither is recoverable in place: the transporter closes the connection so that
// the next Send dials afresh. Callers that want the request retried must issue it
// again, which encodes a new transaction ID. The transporter deliberately does
// not retry by itself — re-writing the same ADU would put a duplicate frame with
// an identical transaction ID on the wire, and for a write function code that
// makes the device execute the command twice.
var ErrStreamDesynced = errors.New("modbus: response stream out of sync with requests")
// desynced marks err as a loss of request/response pairing, see
// [ErrStreamDesynced].
func desynced(err error) error {
return fmt.Errorf("%w: %w", ErrStreamDesynced, err)
}
// TCPClientHandler implements Packager and Transporter interface.
type TCPClientHandler struct {
tcpPackager
tcpTransporter
}
// NewTCPClientHandler allocates a new TCPClientHandler with the given options.
func NewTCPClientHandler(address string, options ...TCPClientHandlerOption) *TCPClientHandler {
h := &TCPClientHandler{}
for _, o := range options {
o(h)
}
h.Address = address
h.Timeout = tcpTimeout
h.IdleTimeout = tcpIdleTimeout
if h.Dial == nil {
h.Dial = defaultDialFunc(h.Timeout)
}
return h
}
// TCPClientHandlerOption configures a TCPClientHandler.
type TCPClientHandlerOption func(*TCPClientHandler)
// WithDialer returns a TCPClientHandlerOption that sets a custom Dial function.
func WithDialer(d DialFunc) TCPClientHandlerOption {
return func(h *TCPClientHandler) {
h.Dial = d
}
}
// DialFunc is the prototype of a function that connects to an address on a
// named network. It Satisfies the [net.Dialer.DialContext] function signature.
type DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
func defaultDialFunc(timeout time.Duration) DialFunc {
return (&net.Dialer{Timeout: timeout}).DialContext
}
// WithTLSConfig returns a TCPClientHandlerOption that enables TLS encryption with the given options.
func WithTLSConfig(config *tls.Config) TCPClientHandlerOption {
return func(h *TCPClientHandler) {
h.tlsConfig = config
}
}
// TCPClient creates TCP client with default handler and given connect string.
func TCPClient(address string) Client {
handler := NewTCPClientHandler(address)
return NewClient(handler)
}
// tcpPackager implements Packager interface.
type tcpPackager struct {
// For synchronization between messages of server & client
transactionID uint32
// Broadcast address is 0
SlaveID byte
}
// SetSlave sets modbus slave id for the next client operations
func (mb *tcpPackager) SetSlave(slaveID byte) {
mb.SlaveID = slaveID
}
// Encode adds modbus application protocol header:
//
// Transaction identifier: 2 bytes
// Protocol identifier: 2 bytes
// Length: 2 bytes
// Unit identifier: 1 byte
// Function code: 1 byte
// Data: n bytes
func (mb *tcpPackager) Encode(pdu *ProtocolDataUnit) (adu []byte, err error) {
adu = make([]byte, tcpHeaderSize+1+len(pdu.Data))
// Transaction identifier
transactionID := atomic.AddUint32(&mb.transactionID, 1)
binary.BigEndian.PutUint16(adu, uint16(transactionID))
// Protocol identifier
binary.BigEndian.PutUint16(adu[2:], tcpProtocolIdentifier)
// Length = sizeof(SlaveID) + sizeof(FunctionCode) + Data
length := uint16(1 + 1 + len(pdu.Data))
binary.BigEndian.PutUint16(adu[4:], length)
// Unit identifier
adu[6] = mb.SlaveID
// PDU
adu[tcpHeaderSize] = pdu.FunctionCode
copy(adu[tcpHeaderSize+1:], pdu.Data)
return
}
// Verify confirms transaction, protocol and unit id.
func (mb *tcpPackager) Verify(aduRequest []byte, aduResponse []byte) error {
return verify(aduRequest, aduResponse)
}
// Decode extracts PDU from TCP frame:
//
// Transaction identifier: 2 bytes
// Protocol identifier: 2 bytes
// Length: 2 bytes
// Unit identifier: 1 byte
func (mb *tcpPackager) Decode(adu []byte) (pdu *ProtocolDataUnit, err error) {
// Read length value in the header
length := binary.BigEndian.Uint16(adu[4:])
pduLength := len(adu) - tcpHeaderSize
if pduLength <= 0 || pduLength != int(length-1) {
err = fmt.Errorf("modbus: length in response '%v' does not match pdu data length '%v'", length-1, pduLength)
return
}
pdu = &ProtocolDataUnit{}
// The first byte after header is function code
pdu.FunctionCode = adu[tcpHeaderSize]
pdu.Data = adu[tcpHeaderSize+1:]
return
}
// tcpTransporter implements Transporter interface.
type tcpTransporter struct {
// Connect string
Address string
// Connect & Read timeout
Timeout time.Duration
// Idle timeout to close the connection.
// If negative, cached connections do not time out.
// If zero, disables the caching of TCP connections and only uses dialed
// connections for a single Send.
IdleTimeout time.Duration
// Recovery timeout if tcp communication misbehaves
LinkRecoveryTimeout time.Duration
// Recovery timeout if the protocol is malformed, e.g. wrong transaction ID
ProtocolRecoveryTimeout time.Duration
// Silent period after successful connection
ConnectDelay time.Duration
// Transmission logger
Logger Logger
// Dial specifies the dial function for creating TCP connections.
// If nil, the transporter dials using the net package.
Dial DialFunc
// TCP connection
mu sync.Mutex
conn net.Conn
closeTimer *time.Timer
lastActivity time.Time
lastAttemptedTransactionID uint16
lastSuccessfulTransactionID uint16
tlsConfig *tls.Config
}
// Send sends data to server and ensures response length is greater than header length.
func (mb *tcpTransporter) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error) {
mb.mu.Lock()
defer mb.mu.Unlock()
if mb.IdleTimeout == 0 {
defer mb.close()
}
var data [tcpMaxLength]byte
linkRecoveryDeadline := time.Now().Add(mb.LinkRecoveryTimeout)
protocolRecoveryDeadline := time.Now().Add(mb.ProtocolRecoveryTimeout)
for {
// Establish a new connection if not connected
if err = mb.connect(ctx); err != nil {
err = fmt.Errorf("modbus: connect: %w", err)
return
}
// Set timer to close when idle
mb.lastActivity = time.Now()
mb.startCloseTimer()
// Set write and read timeout
if mb.Timeout > 0 {
if err = mb.conn.SetDeadline(mb.lastActivity.Add(mb.Timeout)); err != nil {
err = fmt.Errorf("modbus: set deadline: %w", err)
return
}
}
// Recorded before the write, not after: close() collapses the late-response
// window onto this value, and on a write error it must name the request we
// are attempting rather than the previous one - otherwise the window still
// spans this transaction and a frame carrying its ID would be drained as a
// late response on the next connection.
mb.lastAttemptedTransactionID = binary.BigEndian.Uint16(aduRequest)
// Send data
mb.logf("modbus: send % x", aduRequest)
if _, err = mb.conn.Write(aduRequest); err != nil {
// Close on any write error, regardless of its type. [net.Conn.Write]
// has no context parameter, so every error it returns is an OS- or
// network-level condition:
//
// - EPIPE / ECONNRESET – peer closed the connection.
// - Timeout (net.Error.Timeout) – deadline expired mid-write; TCP
// may have flushed 0..N bytes. The server received a partial
// request and the stream is desynchronized.
// - Any other error (ENETDOWN, EHOSTUNREACH, partial write, …) –
// the connection state is equally unknown.
//
// For a request/response protocol like Modbus there is no
// recoverable write error: either the server got nothing (broken
// pipe) or it got a partial frame (stream corruption). In both cases
// the only safe action is to close and reconnect, so that the next
// Send() dials fresh via connect() (which is a no-op when
// mb.conn != nil).
mb.logf("modbus: write error, closing connection: %v", err)
mb.close()
err = fmt.Errorf("modbus: write: %w", err)
return
}
var reconnectAndReissue bool
aduResponse, reconnectAndReissue, err = mb.readResponse(aduRequest, data[:], linkRecoveryDeadline, protocolRecoveryDeadline)
if err != nil {
mb.logf("modbus: read response error: %v, reconnect: %v", err, reconnectAndReissue)
}
if reconnectAndReissue {
mb.logf("modbus: close connection and retry reading response, because of %v", err)
mb.close()
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
continue
}
}
if err != nil {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() && !errors.Is(err, ErrStreamDesynced) {
// Clean timeout: nothing of the response was consumed, so the
// stream is still aligned to a frame boundary and the device may
// simply be slow. Keep the connection open so the late response
// can be drained on the next Send via ProtocolRecoveryTimeout
// transaction-ID matching.
//
// A desync leaves a partial frame queued, so that connection
// cannot be kept - see [ErrStreamDesynced].
mb.logf("modbus: read response timeout, keeping connection: %v", err)
} else {
mb.logf("modbus: read response error: closing connection: %v", err)
mb.close()
}
err = fmt.Errorf("modbus: read response: %w", err)
return
}
mb.lastSuccessfulTransactionID = binary.BigEndian.Uint16(aduResponse)
return
}
}
func (mb *tcpTransporter) shouldRecover(err error) bool {
return errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.ECONNRESET)
}
// isRepeatable reports whether aduRequest may be written to the wire a second
// time, i.e. whether reissuing it during link recovery is free of side effects on
// the device. The policy lives in funcCodeRepeatable; this only knows where MBAP
// keeps the function code.
func isRepeatable(aduRequest []byte) bool {
if len(aduRequest) <= tcpHeaderSize {
return false
}
return funcCodeRepeatable(aduRequest[tcpHeaderSize])
}
// readResponse reads one response frame. It returns reconnectAndReissue when the
// caller should drop the connection, redial and write the request again - only
// ever for a request that is safe to repeat, see [funcCodeRepeatable]. Otherwise
// err is nil on success, or holds the failure to report.
func (mb *tcpTransporter) readResponse(aduRequest []byte, data []byte, recoveryDeadline time.Time, protocolDeadline time.Time) (aduResponse []byte, reconnectAndReissue bool, err error) {
for {
var n int
if n, err = io.ReadFull(mb.conn, data[:tcpHeaderSize]); err != nil {
if n > 0 && !mb.shouldRecover(err) {
// A partial header was consumed and the connection is still up, so
// the rest of that frame is queued and the stream is misaligned.
//
// Errors shouldRecover covers are excluded deliberately: there the
// socket is gone, so nothing is left queued to misalign us and the
// recovery path below can reconnect. That path decides for itself
// whether reissuing the request is safe.
err = desynced(err)
return
}
// recovery disabled or deadline reached - report error
if mb.LinkRecoveryTimeout == 0 || time.Until(recoveryDeadline) < 0 {
return
}
if mb.shouldRecover(err) && isRepeatable(aduRequest) {
// The stream is empty rather than misaligned, and the request is a
// read, so reconnecting and reissuing it has no effect on the device.
// A write is reported instead - it may already have been executed
// before the connection dropped - leaving the retry to the caller,
// which re-encodes with a fresh transaction ID.
mb.logf("modbus: connection closed by remote side: %v", err)
reconnectAndReissue = true
}
return
}
aduResponse, err = mb.processResponse(data[:]) // this also does io
if err != nil {
if mb.shouldRecover(err) {
// The socket is gone rather than misaligned: nothing of this frame is
// left queued, so a fresh connection starts clean. Same situation as
// the header read above, and treated the same way.
if mb.LinkRecoveryTimeout != 0 && time.Until(recoveryDeadline) >= 0 && isRepeatable(aduRequest) {
mb.logf("modbus: connection closed by remote side: %v", err)
reconnectAndReissue = true
}
return
}
// The announced length was nonsense - so what we read as a header was
// really the tail of an earlier frame - or the body read failed some
// other way with the connection still up. Either way the stream is
// mid-frame and cannot be resynchronised.
err = desynced(err)
return
}
err = verify(aduRequest, aduResponse)
if err != nil {
mb.logf("modbus: verify error: %v", err)
var mismatch errTransactionIDMismatch
if errors.As(err, &mismatch) && mb.ProtocolRecoveryTimeout > 0 &&
time.Until(protocolDeadline) >= 0 && mb.isLateResponse(mismatch.got) {
// This is the (late) response to an earlier query that already timed
// out. The frame boundary is intact, so discard it and keep reading:
// the response we are waiting for should follow *without* sending
// another query. (If we sent another query with transaction ID X+1
// here, we would again get a mismatch for the response to X already
// sitting in the buffer.)
continue
}
// Frame-aligned, but our request stays outstanding and this frame
// belongs to nothing we can account for. A genuinely misaligned stream
// also lands here, when an earlier frame's tail parses as a plausible
// header. See [ErrStreamDesynced].
err = desynced(err)
return
}
mb.logf("modbus: recv % x\n", aduResponse)
return // everything is OK
}
}
// isLateResponse reports whether got identifies the response to an earlier
// request that we already gave up on, i.e. whether it lies strictly between the
// last successfully verified and the currently attempted transaction ID, taking
// uint16 wraparound into account.
func (mb *tcpTransporter) isLateResponse(got uint16) bool {
if mb.lastAttemptedTransactionID < mb.lastSuccessfulTransactionID {
// The counter wrapped between the two, so the open interval is the union
// of the two ends of the uint16 range.
return got > mb.lastSuccessfulTransactionID || got < mb.lastAttemptedTransactionID
}
return got > mb.lastSuccessfulTransactionID && got < mb.lastAttemptedTransactionID
}
func (mb *tcpTransporter) processResponse(data []byte) (aduResponse []byte, err error) {
// Read length, ignore transaction & protocol id (4 bytes)
// An implausible length means the bytes just read were not a frame header at
// all - most likely the tail of an earlier frame. There is nothing to salvage:
// the caller marks this as a desync and Send discards the connection, so the
// receive buffer goes with it and draining it here would be pointless.
length := int(binary.BigEndian.Uint16(data[4:]))
if length <= 0 {
err = ErrTCPHeaderLength(length)
return
}
if length > (tcpMaxLength - (tcpHeaderSize - 1)) {
err = ErrTCPHeaderLength(length)
return
}
// Skip unit id
length += tcpHeaderSize - 1
if _, err = io.ReadFull(mb.conn, data[tcpHeaderSize:length]); err != nil {
return
}
aduResponse = data[:length]
if len(aduResponse) == 0 {
err = io.EOF
return
}
return
}
type errTransactionIDMismatch struct {
got, expected uint16
}
func (e errTransactionIDMismatch) Error() string {
return fmt.Sprintf("modbus: response transaction id '%v' does not match request '%v'", e.got, e.expected)
}
type errProtocolIDMismatch struct {
got, expected uint16
}
func (e errProtocolIDMismatch) Error() string {
return fmt.Sprintf("modbus: response protocol id '%v' does not match request '%v'", e.got, e.expected)
}
type errUnitIDMismatch struct {
got, expected byte
}
func (e errUnitIDMismatch) Error() string {
return fmt.Sprintf("modbus: response unit id '%v' does not match request '%v'", e.got, e.expected)
}
const sizeInt16 = 2
func verify(aduRequest []byte, aduResponse []byte) (err error) {
// len guard check for conversion
if len(aduRequest) < sizeInt16 {
return ErrADURequestLength(len(aduRequest))
}
if len(aduResponse) < sizeInt16 {
return ErrADUResponseLength(len(aduResponse))
}
// Transaction id
responseVal := binary.BigEndian.Uint16(aduResponse)
requestVal := binary.BigEndian.Uint16(aduRequest)
if responseVal != requestVal {
err = errTransactionIDMismatch{got: responseVal, expected: requestVal}
return
}
// Protocol id
responseVal = binary.BigEndian.Uint16(aduResponse[2:])
requestVal = binary.BigEndian.Uint16(aduRequest[2:])
if responseVal != requestVal {
err = errProtocolIDMismatch{got: responseVal, expected: requestVal}
return
}
// Unit id (1 byte)
if aduResponse[6] != aduRequest[6] {
err = errUnitIDMismatch{got: aduResponse[6], expected: aduRequest[6]}
return
}
return
}
// Connect establishes a new connection to the address in Address.
// Connect and Close are exported so that multiple requests can be done with one session
func (mb *tcpTransporter) Connect(ctx context.Context) error {
mb.mu.Lock()
defer mb.mu.Unlock()
return mb.connect(ctx)
}
func (mb *tcpTransporter) connect(ctx context.Context) error {
if mb.conn == nil {
conn, err := mb.Dial(ctx, "tcp", mb.Address)
if err != nil {
return err
}
if mb.tlsConfig != nil {
conn = tls.Client(conn, mb.tlsConfig)
}
mb.conn = conn
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(mb.ConnectDelay): // silent period
}
}
return nil
}
func (mb *tcpTransporter) startCloseTimer() {
if mb.IdleTimeout <= 0 {
return
}
if mb.closeTimer == nil {
mb.closeTimer = time.AfterFunc(mb.IdleTimeout, mb.closeIdle)
} else {
mb.closeTimer.Reset(mb.IdleTimeout)
}
}
// Close closes current connection.
func (mb *tcpTransporter) Close() error {
mb.mu.Lock()
defer mb.mu.Unlock()
return mb.close()
}
func (mb *tcpTransporter) logf(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Printf(format, v...)
}
}
// closeLocked closes current connection. Caller must hold the mutex before calling this method.
func (mb *tcpTransporter) close() (err error) {
if mb.conn != nil {
err = mb.conn.Close()
mb.conn = nil
}
// Collapse the late-response window. Nothing can still be in flight on a
// connection that no longer exists, so no transaction ID may be treated as a
// late response until a new exchange succeeds on the next connection. See
// TestTCPCloseResetsLateResponseWindow for what goes wrong without this.
mb.lastSuccessfulTransactionID = mb.lastAttemptedTransactionID
return
}
// closeIdle closes the connection if last activity is passed behind IdleTimeout.
func (mb *tcpTransporter) closeIdle() {
mb.mu.Lock()
defer mb.mu.Unlock()
if mb.IdleTimeout < 0 {
return
}
if idle := time.Since(mb.lastActivity); idle >= mb.IdleTimeout {
mb.logf("modbus: closing connection due to idle timeout: %v", idle)
mb.close()
}
}