forked from goburrow/modbus
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tlsclient.go
215 lines (186 loc) · 5.01 KB
/
tlsclient.go
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
// 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 (
"crypto/tls"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"sync"
"time"
)
// TLSClientHandler implements Packager and Transporter interface.
type TLSClientHandler struct {
tcpPackager
tlsTransporter
}
// NewTLSClientHandler allocates a new TLSClientHandler.
func NewTLSClientHandler(address, key, cert string, insecure bool) *TLSClientHandler {
h := &TLSClientHandler{}
h.Address = address
h.Timeout = tcpTimeout
h.IdleTimeout = tcpIdleTimeout
h.key = key
h.crt = cert
h.insecure = insecure
return h
}
// TLSClient creates TLS client with default handler and given connect string.
func TLSClient(address, key, cert string, insecure bool) Client {
handler := NewTLSClientHandler(address, key, cert, insecure)
return NewClient(handler)
}
// tlsTransporter implements Transporter interface.
type tlsTransporter struct {
// Connect string
Address string
// Connect & Read timeout
Timeout time.Duration
// Idle timeout to close the connection
IdleTimeout time.Duration
// Transmission logger
Logger *log.Logger
// TCP connection
mu sync.Mutex
conn net.Conn
closeTimer *time.Timer
lastActivity time.Time
key, crt string
insecure bool
}
// Send sends data to server and ensures response length is greater than header length.
func (mb *tlsTransporter) Send(aduRequest []byte) (aduResponse []byte, err error) {
mb.mu.Lock()
defer mb.mu.Unlock()
// Establish a new connection if not connected
if err = mb.connect(); err != nil {
return
}
// Set timer to close when idle
mb.lastActivity = time.Now()
mb.startCloseTimer()
// Set write and read timeout
var timeout time.Time
if mb.Timeout > 0 {
timeout = mb.lastActivity.Add(mb.Timeout)
}
if err = mb.conn.SetDeadline(timeout); err != nil {
return
}
// Send data
mb.logf("modbus: sending % x", aduRequest)
if _, err = mb.conn.Write(aduRequest); err != nil {
return
}
// Read header first
var data [tcpMaxLength]byte
if _, err = io.ReadFull(mb.conn, data[:tcpHeaderSize]); err != nil {
return
}
// Read length, ignore transaction & protocol id (4 bytes)
length := int(binary.BigEndian.Uint16(data[4:]))
if length <= 0 {
mb.flush(data[:])
err = fmt.Errorf("modbus: length in response header '%v' must not be zero", length)
return
}
if length > (tcpMaxLength - (tcpHeaderSize - 1)) {
mb.flush(data[:])
err = fmt.Errorf("modbus: length in response header '%v' must not greater than '%v'", length, tcpMaxLength-tcpHeaderSize+1)
return
}
// Skip unit id
length += tcpHeaderSize - 1
if _, err = io.ReadFull(mb.conn, data[tcpHeaderSize:length]); err != nil {
return
}
aduResponse = data[:length]
mb.logf("modbus: received % x\n", aduResponse)
return
}
// Connect establishes a new TLS connection to the address in Address.
// Connect and Close are exported so that multiple requests can be done with one session
func (mb *tlsTransporter) Connect() error {
mb.mu.Lock()
defer mb.mu.Unlock()
return mb.connect()
}
func (mb *tlsTransporter) connect() error {
if mb.conn == nil {
crt, err := tls.LoadX509KeyPair(mb.crt, mb.key)
if err != nil {
return err
}
config := &tls.Config{
Certificates: []tls.Certificate{crt},
InsecureSkipVerify: mb.insecure,
}
dialer := &net.Dialer{Timeout: mb.Timeout}
conn, err := tls.DialWithDialer(dialer, "tcp", mb.Address, config)
if err != nil {
return err
}
mb.conn = conn
}
return nil
}
func (mb *tlsTransporter) 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 *tlsTransporter) Close() error {
mb.mu.Lock()
defer mb.mu.Unlock()
return mb.close()
}
// flush flushes pending data in the connection,
// returns io.EOF if connection is closed.
func (mb *tlsTransporter) flush(b []byte) (err error) {
if err = mb.conn.SetReadDeadline(time.Now()); err != nil {
return
}
// Timeout setting will be reset when reading
if _, err = mb.conn.Read(b); err != nil {
// Ignore timeout error
if netError, ok := err.(net.Error); ok && netError.Timeout() {
err = nil
}
}
return
}
func (mb *tlsTransporter) 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 *tlsTransporter) close() (err error) {
if mb.conn != nil {
err = mb.conn.Close()
mb.conn = nil
}
return
}
// closeIdle closes the connection if last activity is passed behind IdleTimeout.
func (mb *tlsTransporter) closeIdle() {
mb.mu.Lock()
defer mb.mu.Unlock()
if mb.IdleTimeout <= 0 {
return
}
idle := time.Now().Sub(mb.lastActivity)
if idle >= mb.IdleTimeout {
mb.logf("modbus: closing connection due to idle timeout: %v", idle)
mb.close()
}
}