-
Notifications
You must be signed in to change notification settings - Fork 89
/
server.go
901 lines (773 loc) · 24.6 KB
/
server.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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
package modbus
import (
"crypto/tls"
"crypto/x509"
"encoding/asn1"
"errors"
"fmt"
"log"
"net"
"strings"
"sync"
"time"
)
// Modbus Role PEM OID (see R-21 of the MBAPS spec)
var modbusRoleOID asn1.ObjectIdentifier = asn1.ObjectIdentifier{
1, 3, 6, 1, 4, 1, 50316, 802, 1,
}
// Server configuration object.
type ServerConfiguration struct {
// URL defines where to listen at e.g. tcp://[::]:502
URL string
// Timeout sets the idle session timeout (client connections will
// be closed if idle for this long)
Timeout time.Duration
// MaxClients sets the maximum number of concurrent client connections
MaxClients uint
// TLSServerCert sets the server-side TLS key pair (tcp+tls only)
TLSServerCert *tls.Certificate
// TLSClientCAs sets the list of CA certificates used to authenticate
// client connections (tcp+tls only). Leaf (i.e. client) certificates can
// also be used in case of self-signed certs, or if cert pinning is required.
TLSClientCAs *x509.CertPool
// Logger provides a custom sink for log messages.
// If nil, messages will be written to stdout.
Logger *log.Logger
}
// Request object passed to the coil handler.
type CoilsRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitId uint8 // the requested unit id (slave id)
Addr uint16 // the base coil address requested
Quantity uint16 // the number of consecutive coils covered by this request
// (first address: Addr, last address: Addr + Quantity - 1)
IsWrite bool // true if the request is a write, false if a read
Args []bool // a slice of bool values of the coils to be set, ordered
// from Addr to Addr + Quantity - 1 (for writes only)
}
// Request object passed to the discrete input handler.
type DiscreteInputsRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitId uint8 // the requested unit id (slave id)
Addr uint16 // the base discrete input address requested
Quantity uint16 // the number of consecutive discrete inputs covered by this request
}
// Request object passed to the holding register handler.
type HoldingRegistersRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitId uint8 // the requested unit id (slave id)
Addr uint16 // the base register address requested
Quantity uint16 // the number of consecutive registers covered by this request
IsWrite bool // true if the request is a write, false if a read
Args []uint16 // a slice of register values to be set, ordered from
// Addr to Addr + Quantity - 1 (for writes only)
}
// Request object passed to the input register handler.
type InputRegistersRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitId uint8 // the requested unit id (slave id)
Addr uint16 // the base register address requested
Quantity uint16 // the number of consecutive registers covered by this request
}
// The RequestHandler interface should be implemented by the handler
// object passed to NewServer (see reqHandler in NewServer()).
// After decoding and validating an incoming request, the server will
// invoke the appropriate handler function, depending on the function code
// of the request.
type RequestHandler interface {
// HandleCoils handles the read coils (0x01), write single coil (0x05)
// and write multiple coils (0x0f) function codes.
// A CoilsRequest object is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of bools containing the coil values to be sent to back
// to the client (only sent for reads),
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
// If nil, a positive modbus response is sent back to the client
// along with the returned data.
// If non-nil, a negative modbus response is sent back, with the
// exception code set depending on the error
// (again, see mapErrorToExceptionCode()).
HandleCoils (req *CoilsRequest) (res []bool, err error)
// HandleDiscreteInputs handles the read discrete inputs (0x02) function code.
// A DiscreteInputsRequest oibject is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of bools containing the discrete input values to be
// sent back to the client,
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
HandleDiscreteInputs (req *DiscreteInputsRequest) (res []bool, err error)
// HandleHoldingRegisters handles the read holding registers (0x03),
// write single register (0x06) and write multiple registers (0x10).
// A HoldingRegistersRequest object is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of uint16 containing the register values to be sent
// to back to the client (only sent for reads),
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
HandleHoldingRegisters (req *HoldingRegistersRequest) (res []uint16, err error)
// HandleInputRegisters handles the read input registers (0x04) function code.
// An InputRegistersRequest object is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of uint16 containing the register values to be sent
// back to the client,
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
HandleInputRegisters (req *InputRegistersRequest) (res []uint16, err error)
}
// Modbus server object.
type ModbusServer struct {
conf ServerConfiguration
logger *logger
lock sync.Mutex
started bool
handler RequestHandler
tcpListener net.Listener
tcpClients []net.Conn
transportType transportType
}
// Returns a new modbus server.
// reqHandler should be a user-provided handler object satisfying the RequestHandler
// interface.
func NewServer(conf *ServerConfiguration, reqHandler RequestHandler) (
ms *ModbusServer, err error) {
var serverType string
var splitURL []string
ms = &ModbusServer{
conf: *conf,
handler: reqHandler,
}
splitURL = strings.SplitN(ms.conf.URL, "://", 2)
if len(splitURL) == 2 {
serverType = splitURL[0]
ms.conf.URL = splitURL[1]
}
ms.logger = newLogger(
fmt.Sprintf("modbus-server(%s)", ms.conf.URL), ms.conf.Logger)
if ms.conf.URL == "" {
ms.logger.Errorf("missing host part in URL '%s'", conf.URL)
err = ErrConfigurationError
return
}
switch serverType {
case "tcp":
if ms.conf.Timeout == 0 {
ms.conf.Timeout = 120 * time.Second
}
if ms.conf.MaxClients == 0 {
ms.conf.MaxClients = 10
}
ms.transportType = modbusTCP
case "tcp+tls":
if ms.conf.Timeout == 0 {
ms.conf.Timeout = 120 * time.Second
}
if ms.conf.MaxClients == 0 {
ms.conf.MaxClients = 10
}
// expect a server-side certificate
if ms.conf.TLSServerCert == nil {
ms.logger.Errorf("missing server certificate")
err = ErrConfigurationError
return
}
// expect a CertPool object containing at least 1 CA or
// leaf certificate to validate client-side certificates
if ms.conf.TLSClientCAs == nil {
ms.logger.Errorf("missing CA/client certificates")
err = ErrConfigurationError
return
}
ms.transportType = modbusTCPOverTLS
default:
err = ErrConfigurationError
return
}
return
}
// Starts accepting client connections.
func (ms *ModbusServer) Start() (err error) {
ms.lock.Lock()
defer ms.lock.Unlock()
if ms.started {
return
}
switch ms.transportType {
case modbusTCP, modbusTCPOverTLS:
// bind to a TCP socket
ms.tcpListener, err = net.Listen("tcp", ms.conf.URL)
if err != nil {
return
}
// accept client connections in a goroutine
go ms.acceptTCPClients()
default:
err = ErrConfigurationError
return
}
ms.started = true
return
}
// Stops accepting new client connections and closes any active session.
func (ms *ModbusServer) Stop() (err error) {
ms.lock.Lock()
defer ms.lock.Unlock()
if !ms.started {
return
}
ms.started = false
if ms.transportType == modbusTCP || ms.transportType == modbusTCPOverTLS {
// close the server socket if we're listening over TCP
err = ms.tcpListener.Close()
// close all active TCP clients
for _, sock := range ms.tcpClients{
sock.Close()
}
}
return
}
// Accepts new client connections if the configured connection limit allows it.
// Each connection is served from a dedicated goroutine to allow for concurrent
// connections.
func (ms *ModbusServer) acceptTCPClients() {
var sock net.Conn
var err error
var accepted bool
for {
sock, err = ms.tcpListener.Accept()
if err != nil {
// if the server socket has just been closed, return here as
// this goroutine isn't going to see any new client connection
if errors.Is(err, net.ErrClosed) {
return
}
ms.logger.Warningf("failed to accept client connection: %v", err)
continue
}
ms.lock.Lock()
// apply a connection limit
if ms.started && uint(len(ms.tcpClients)) < ms.conf.MaxClients {
accepted = true
// add the new client connection to the pool
ms.tcpClients = append(ms.tcpClients, sock)
} else {
accepted = false
}
ms.lock.Unlock()
if accepted {
// spin a client handler goroutine to serve the new client
go ms.handleTCPClient(sock)
} else {
ms.logger.Warningf("max. number of concurrent connections " +
"reached, rejecting %v", sock.RemoteAddr())
// discard the connection
sock.Close()
}
}
// never reached
return
}
// Handles a TCP client connection.
// Once handleTransport() returns (i.e. the connection has either closed, timed
// out, or an unrecoverable error happened), the TCP socket is closed and removed
// from the list of active client connections.
func (ms *ModbusServer) handleTCPClient(sock net.Conn) {
var err error
var clientRole string
var tlsSock net.Conn
switch ms.transportType {
case modbusTCP:
// serve modbus requests over the raw TCP connection
ms.handleTransport(
newTCPTransport(sock, ms.conf.Timeout, ms.conf.Logger),
sock.RemoteAddr().String(), "")
case modbusTCPOverTLS:
// start TLS negotiation over the raw TCP connection
tlsSock, clientRole, err = ms.startTLS(sock)
if err != nil {
ms.logger.Warningf("TLS handshake with %s failed: %v",
sock.RemoteAddr().String(), err)
} else {
// serve modbus requests over the TLS tunnel
ms.handleTransport(
newTCPTransport(tlsSock, ms.conf.Timeout, ms.conf.Logger),
sock.RemoteAddr().String(), clientRole)
}
default:
ms.logger.Errorf("unimplemented transport type %v", ms.transportType)
}
// once done, remove our connection from the list of active client conns
ms.lock.Lock()
for i := range ms.tcpClients {
if ms.tcpClients[i] == sock {
ms.tcpClients[i] = ms.tcpClients[len(ms.tcpClients)-1]
ms.tcpClients = ms.tcpClients[:len(ms.tcpClients)-1]
break
}
}
ms.lock.Unlock()
// close the connection
sock.Close()
return
}
// For each request read from the transport, performs decoding and validation,
// calls the user-provided handler, then encodes and writes the response
// to the transport.
func (ms *ModbusServer) handleTransport(t transport, clientAddr string, clientRole string) {
var req *pdu
var res *pdu
var err error
var addr uint16
var quantity uint16
for {
req, err = t.ReadRequest()
if err != nil {
return
}
switch req.functionCode {
case fcReadCoils, fcReadDiscreteInputs:
var coils []bool
var resCount int
if len(req.payload) != 4 {
err = ErrProtocolError
break
}
// decode address and quantity fields
addr = bytesToUint16(BIG_ENDIAN, req.payload[0:2])
quantity = bytesToUint16(BIG_ENDIAN, req.payload[2:4])
// ensure the reply never exceeds the maximum PDU length and we
// never read past 0xffff
if quantity > 2000 || quantity == 0 {
err = ErrProtocolError
break
}
if uint32(addr) + uint32(quantity) - 1 > 0xffff {
err = ErrIllegalDataAddress
break
}
// invoke the appropriate handler
if req.functionCode == fcReadCoils {
coils, err = ms.handler.HandleCoils(&CoilsRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
IsWrite: false,
Args: nil,
})
} else {
coils, err = ms.handler.HandleDiscreteInputs(
&DiscreteInputsRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
})
}
resCount = len(coils)
// make sure the handler returned the expected number of items
if err == nil && resCount != int(quantity) {
ms.logger.Errorf("handler returned %v bools, " +
"expected %v", resCount, quantity)
err = ErrServerDeviceFailure
break
}
if err != nil {
break
}
// assemble a response PDU
res = &pdu{
unitId: req.unitId,
functionCode: req.functionCode,
payload: []byte{0},
}
// byte count (1 byte for 8 coils)
res.payload[0] = uint8(resCount / 8)
if resCount % 8 != 0 {
res.payload[0]++
}
// coil values
res.payload = append(res.payload, encodeBools(coils)...)
case fcWriteSingleCoil:
if len(req.payload) != 4 {
err = ErrProtocolError
break
}
// decode the address field
addr = bytesToUint16(BIG_ENDIAN, req.payload[0:2])
// validate the value field (should be either 0xff00 or 0x0000)
if ((req.payload[2] != 0xff && req.payload[2] != 0x00) ||
req.payload[3] != 0x00) {
err = ErrProtocolError
break
}
// invoke the coil handler
_, err = ms.handler.HandleCoils(&CoilsRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: 1, // request for a single coil
IsWrite: true, // this is a write request
Args: []bool{(req.payload[2] == 0xff)},
})
if err != nil {
break
}
// assemble a response PDU
res = &pdu{
unitId: req.unitId,
functionCode: req.functionCode,
}
// echo the address and value in the response
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, addr)...)
res.payload = append(res.payload,
req.payload[2], req.payload[3])
case fcWriteMultipleCoils:
var expectedLen int
if len(req.payload) < 6 {
err = ErrProtocolError
break
}
// decode address and quantity fields
addr = bytesToUint16(BIG_ENDIAN, req.payload[0:2])
quantity = bytesToUint16(BIG_ENDIAN, req.payload[2:4])
// ensure the reply never exceeds the maximum PDU length and we
// never read past 0xffff
if quantity > 0x7b0 || quantity == 0 {
err = ErrProtocolError
break
}
if uint32(addr) + uint32(quantity) - 1 > 0xffff {
err = ErrIllegalDataAddress
break
}
// validate the byte count field (1 byte for 8 coils)
expectedLen = int(quantity) / 8
if quantity % 8 != 0 {
expectedLen++
}
if req.payload[4] != uint8(expectedLen) {
err = ErrProtocolError
break
}
// make sure we have enough bytes
if len(req.payload) - 5 != expectedLen {
err = ErrProtocolError
break
}
// invoke the coil handler
_, err = ms.handler.HandleCoils(&CoilsRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
IsWrite: true, // this is a write request
Args: decodeBools(quantity, req.payload[5:]),
})
if err != nil {
break
}
// assemble a response PDU
res = &pdu{
unitId: req.unitId,
functionCode: req.functionCode,
}
// echo the address and quantity in the response
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, addr)...)
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, quantity)...)
case fcReadHoldingRegisters, fcReadInputRegisters:
var regs []uint16
var resCount int
if len(req.payload) != 4 {
err = ErrProtocolError
break
}
// decode address and quantity fields
addr = bytesToUint16(BIG_ENDIAN, req.payload[0:2])
quantity = bytesToUint16(BIG_ENDIAN, req.payload[2:4])
// ensure the reply never exceeds the maximum PDU length and we
// never read past 0xffff
if quantity > 0x007d || quantity == 0 {
err = ErrProtocolError
break
}
if uint32(addr) + uint32(quantity) - 1 > 0xffff {
err = ErrIllegalDataAddress
break
}
// invoke the appropriate handler
if req.functionCode == fcReadHoldingRegisters {
regs, err = ms.handler.HandleHoldingRegisters(
&HoldingRegistersRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
IsWrite: false,
Args: nil,
})
} else {
regs, err = ms.handler.HandleInputRegisters(
&InputRegistersRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
})
}
resCount = len(regs)
// make sure the handler returned the expected number of items
if err == nil && resCount != int(quantity) {
ms.logger.Errorf("handler returned %v 16-bit values, " +
"expected %v", resCount, quantity)
err = ErrServerDeviceFailure
break
}
if err != nil {
break
}
// assemble a response PDU
res = &pdu{
unitId: req.unitId,
functionCode: req.functionCode,
payload: []byte{0},
}
// byte count (2 bytes per register)
res.payload[0] = uint8(resCount * 2)
// register values
res.payload = append(res.payload,
uint16sToBytes(BIG_ENDIAN, regs)...)
case fcWriteSingleRegister:
var value uint16
if len(req.payload) != 4 {
err = ErrProtocolError
break
}
// decode address and value fields
addr = bytesToUint16(BIG_ENDIAN, req.payload[0:2])
value = bytesToUint16(BIG_ENDIAN, req.payload[2:4])
// invoke the handler
_, err = ms.handler.HandleHoldingRegisters(
&HoldingRegistersRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: 1, // request for a single register
IsWrite: true, // request is a write
Args: []uint16{value},
})
if err != nil {
break
}
// assemble a response PDU
res = &pdu{
unitId: req.unitId,
functionCode: req.functionCode,
}
// echo the address and value in the response
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, addr)...)
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, value)...)
case fcWriteMultipleRegisters:
var expectedLen int
if len(req.payload) < 6 {
err = ErrProtocolError
break
}
// decode address and quantity fields
addr = bytesToUint16(BIG_ENDIAN, req.payload[0:2])
quantity = bytesToUint16(BIG_ENDIAN, req.payload[2:4])
// ensure the reply never exceeds the maximum PDU length and we
// never read past 0xffff
if quantity > 0x007b || quantity == 0 {
err = ErrProtocolError
break
}
if uint32(addr) + uint32(quantity) - 1 > 0xffff {
err = ErrIllegalDataAddress
break
}
// validate the byte count field (2 bytes per register)
expectedLen = int(quantity) * 2
if req.payload[4] != uint8(expectedLen) {
err = ErrProtocolError
break
}
// make sure we have enough bytes
if len(req.payload) - 5 != expectedLen {
err = ErrProtocolError
break
}
// invoke the holding register handler
_, err = ms.handler.HandleHoldingRegisters(
&HoldingRegistersRequest{
ClientAddr: clientAddr,
ClientRole: clientRole,
UnitId: req.unitId,
Addr: addr,
Quantity: quantity,
IsWrite: true, // this is a write request
Args: bytesToUint16s(BIG_ENDIAN, req.payload[5:]),
})
if err != nil {
break
}
// assemble a response PDU
res = &pdu{
unitId: req.unitId,
functionCode: req.functionCode,
}
// echo the address and quantity in the response
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, addr)...)
res.payload = append(res.payload,
uint16ToBytes(BIG_ENDIAN, quantity)...)
default:
res = &pdu{
// reply with the request target unit ID
unitId: req.unitId,
// set the error bit
functionCode: (0x80 | req.functionCode),
// set the exception code to illegal function to indicate that
// the server does not know how to handle this function code.
payload: []byte{exIllegalFunction},
}
}
// if there was no error processing the request but the response is nil
// (which should never happen), emit a server failure exception code
// and log an error
if err == nil && res == nil {
err = ErrServerDeviceFailure
ms.logger.Errorf("internal server error (req: %v, res: %v, err: %v)",
req, res, err)
}
// map go errors to modbus errors, unless the error is a protocol error,
// in which case close the transport and return.
if err != nil {
if err == ErrProtocolError {
ms.logger.Warningf(
"protocol error, closing link (client address: '%s')",
clientAddr)
t.Close()
return
} else {
res = &pdu{
unitId: req.unitId,
functionCode: (0x80 | req.functionCode),
payload: []byte{mapErrorToExceptionCode(err)},
}
}
}
// write the response to the transport
err = t.WriteResponse(res)
if err != nil {
ms.logger.Warningf("failed to write response: %v", err)
}
// avoid holding on to stale data
req = nil
res = nil
}
// never reached
return
}
// startTLS performs a full TLS handshake (with client authentication) on tcpSock
// and returns a 'wrapped' clear-text socket suitable for use by the TCP transport.
func (ms *ModbusServer) startTLS(tcpSock net.Conn) (
tlsSock *tls.Conn, clientRole string, err error) {
var connState tls.ConnectionState
// set a 30s timeout for the TLS handshake to complete
err = tcpSock.SetDeadline(time.Now().Add(30 * time.Second))
if err != nil {
return
}
// start TLS negotiation over the raw TCP connection
tlsSock = tls.Server(tcpSock, &tls.Config{
Certificates: []tls.Certificate{
*ms.conf.TLSServerCert,
},
ClientCAs: ms.conf.TLSClientCAs,
// require a valid (verified) certificate from the client
// (see R-06, R-08 and R-10 of the MBAPS spec)
ClientAuth: tls.RequireAndVerifyClientCert,
// mandate TLSv1.2 or higher (see R-01 of the MBAPS spec)
MinVersion: tls.VersionTLS12,
})
// complete the full TLS handshake (with client cert validation)
err = tlsSock.Handshake()
if err != nil {
return
}
// look for and extract the client's role, if any
connState = tlsSock.ConnectionState()
if len(connState.PeerCertificates) == 0 {
err = errors.New("no client certificate received")
return
}
// From the tls.ConnectionState doc:
// "The first element is the leaf certificate that the connection is
// verified against."
clientRole = ms.extractRole(connState.PeerCertificates[0])
return
}
// extractRole looks for Modbus Role extensions in a certificate and returns the
// role as a string.
// If no role extension is found, a nil string is returned (R-23).
// If multiple or invalid role extensions are found, a nil string is returned (R-65, R-22).
func (ms *ModbusServer) extractRole(cert *x509.Certificate) (role string) {
var err error
var found bool
var badCert bool
// walk through all extensions looking for Modbus Role OIDs
for _, ext := range cert.Extensions {
if ext.Id.Equal(modbusRoleOID) {
// there must be only one role extension per cert (R-65)
if found {
ms.logger.Warning("client certificate contains more than one role OIDs")
badCert = true
break
}
found = true
// the role extension must use UTF8String encoding (R-22)
// (the ASN1 tag for UTF8String is 0x0c)
if len(ext.Value) < 2 || ext.Value[0] != 0x0c {
badCert = true
break
}
// extract the ASN1 string
_, err = asn1.Unmarshal(ext.Value, &role)
if err != nil {
ms.logger.Warningf("failed to decode Modbus Role extension: %v", err)
badCert = true
break
}
}
}
// blank the role if we found more than one Role extension
if badCert {
role = ""
}
return
}